1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 #include <stdio.h>
30 #include <stdlib.h>
31
32 #include "button.h"
33 #include "container.h"
34 #include "../common/context.h"
35 #include <ucx/mempool.h>
36
37
38 UIWIDGET ui_button(UiObject *obj,
char *label, ui_callback f,
void *data) {
39 UiContainer *ct = uic_get_current_container(obj);
40 XmString str = XmStringCreateLocalized(label);
41
42 int n =
0;
43 Arg args[
16];
44
45 XtSetArg(args[n], XmNlabelString, str);
46 n++;
47
48 Widget parent = ct->prepare(ct, args, &n,
FALSE);
49 Widget button = XmCreatePushButton(parent,
"button", args, n);
50 ct->add(ct, button);
51
52 if(f) {
53 UiEventData *event = ucx_mempool_malloc(
54 obj->ctx->mempool,
55 sizeof(UiEventData));
56 event->obj = obj;
57 event->userdata = data;
58 event->callback = f;
59 event->value =
0;
60 XtAddCallback(
61 button,
62 XmNactivateCallback,
63 (XtCallbackProc)ui_push_button_callback,
64 event);
65 }
66
67 XtManageChild(button);
68
69 return button;
70 }
71
72
73 int64_t ui_toggle_button_get(UiInteger *i) {
74 int state =
0;
75 XtVaGetValues(i->obj, XmNset, &state,
NULL);
76 i->value = state;
77 return state;
78 }
79
80 void ui_toggle_button_set(UiInteger *i,
int64_t value) {
81 Arg arg;
82 XtSetArg(arg, XmNset, value);
83 XtSetValues(i->obj, &arg,
1);
84 i->value = value;
85 }
86
87 void ui_toggle_button_callback(
88 Widget widget,
89 UiEventData *event,
90 XmToggleButtonCallbackStruct *tb)
91 {
92 UiEvent e;
93 e.obj = event->obj;
94 e.window = event->obj->window;
95
96 e.intval = tb->set;
97 event->callback(&e, event->userdata);
98 }
99
100 void ui_push_button_callback(Widget widget, UiEventData *event, XtPointer d) {
101 UiEvent e;
102 e.obj = event->obj;
103 e.window = event->obj->window;
104 e.document = event->obj->ctx->document;
105 e.intval = event->value;
106 event->callback(&e, event->userdata);
107 }
108
109
110 static void radio_callback(
111 Widget widget,
112 RadioEventData *event,
113 XmToggleButtonCallbackStruct *tb)
114 {
115 if(tb->set) {
116 RadioButtonGroup *group = event->group;
117 if(group->current) {
118 Arg arg;
119 XtSetArg(arg, XmNset,
FALSE);
120 XtSetValues(group->current, &arg,
1);
121 }
122 group->current = widget;
123 }
124 }
125
126 UIWIDGET ui_radiobutton(UiObject *obj,
char *label, UiInteger *rgroup) {
127 UiContainer *ct = uic_get_current_container(obj);
128 XmString str = XmStringCreateLocalized(label);
129
130 int n =
0;
131 Arg args[
16];
132
133 XtSetArg(args[n], XmNlabelString, str);
134 n++;
135 XtSetArg(args[n], XmNindicatorType, XmONE_OF_MANY_ROUND);
136 n++;
137
138 Widget parent = ct->prepare(ct, args, &n,
FALSE);
139 Widget button = XmCreateToggleButton(parent,
"radiobutton", args, n);
140 ct->add(ct, button);
141
142 if(rgroup) {
143 RadioButtonGroup *group;
144 if(rgroup->obj) {
145 group = rgroup->obj;
146 group->buttons = ucx_list_append(group->buttons, button);
147 group->ref++;
148 }
else {
149 group = malloc(
sizeof(RadioButtonGroup));
150 group->buttons = ucx_list_append(
NULL, button);
151 group->current = button;
152
153
154 Arg arg;
155 XtSetArg(arg, XmNset,
TRUE);
156 XtSetValues(button, &arg,
1);
157 rgroup->obj = group;
158
159 group->current = button;
160 }
161
162 RadioEventData *event = malloc(
sizeof(RadioEventData));
163 event->obj = obj;
164 event->callback =
NULL;
165 event->userdata =
NULL;
166 event->group = group;
167 XtAddCallback(
168 button,
169 XmNvalueChangedCallback,
170 (XtCallbackProc)radio_callback,
171 event);
172
173 rgroup->get = ui_radiobutton_get;
174 rgroup->set = ui_radiobutton_set;
175 }
176
177 XtManageChild(button);
178 return button;
179 }
180
181 int64_t ui_radiobutton_get(UiInteger *value) {
182 RadioButtonGroup *group = value->obj;
183
184 int i = ucx_list_find(group->buttons, group->current,
NULL,
NULL);
185 if (i >=
0) {
186 value->value = i;
187 return i;
188 }
else {
189 return 0;
190 }
191 }
192
193 void ui_radiobutton_set(UiInteger *value,
int64_t i) {
194 RadioButtonGroup *group = value->obj;
195 Arg arg;
196
197 XtSetArg(arg, XmNset,
FALSE);
198 XtSetValues(group->current, &arg,
1);
199
200 UcxList *elm = ucx_list_get(group->buttons, i);
201 if(elm) {
202 Widget button = elm->data;
203 XtSetArg(arg, XmNset,
TRUE);
204 XtSetValues(button, &arg,
1);
205 group->current = button;
206 }
207 }
208