Mon, 06 Jan 2025 22:22:55 +0100
update ucx, toolkit
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1 | /* |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
3 | * |
100 | 4 | * Copyright 2024 Olaf Wintermann. All rights reserved. |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
5 | * |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
6 | * Redistribution and use in source and binary forms, with or without |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
7 | * modification, are permitted provided that the following conditions are met: |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
8 | * |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
9 | * 1. Redistributions of source code must retain the above copyright |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
10 | * notice, this list of conditions and the following disclaimer. |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
11 | * |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
12 | * 2. Redistributions in binary form must reproduce the above copyright |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
13 | * notice, this list of conditions and the following disclaimer in the |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
14 | * documentation and/or other materials provided with the distribution. |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
15 | * |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
26 | * POSSIBILITY OF SUCH DAMAGE. |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
27 | */ |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
28 | |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
29 | #include <stdio.h> |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
30 | #include <stdlib.h> |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
31 | |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
32 | #include "button.h" |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
33 | #include "container.h" |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
34 | #include "../common/context.h" |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
35 | #include <cx/mempool.h> |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
36 | |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
37 | #include <cx/linked_list.h> |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
38 | #include <cx/array_list.h> |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
39 | #include <cx/compare.h> |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
40 | |
100 | 41 | #include <Xm/XmAll.h> |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
42 | |
100 | 43 | |
44 | UIWIDGET ui_button_create(UiObject* obj, UiButtonArgs args) { | |
45 | Arg xargs[16]; | |
46 | int n = 0; | |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
47 | |
100 | 48 | UiContainerPrivate *ctn = ui_obj_container(obj); |
49 | UI_APPLY_LAYOUT(ctn->layout, args); | |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
50 | |
100 | 51 | Widget parent = ctn->prepare(ctn, xargs, &n); |
101
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
100
diff
changeset
|
52 | |
100 | 53 | XmString label = NULL; |
54 | if(args.label) { | |
55 | label = XmStringCreateLocalized((char*)args.label); | |
56 | XtSetArg(xargs[n], XmNlabelString, label); n++; | |
57 | } | |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
58 | |
100 | 59 | char *name = args.name ? (char*)args.name : "button"; |
60 | Widget button = XmCreatePushButton(parent, name, xargs, n); | |
61 | XtManageChild(button); | |
62 | ctn->add(ctn, button); | |
63 | ||
64 | ui_set_widget_groups(obj->ctx, button, args.groups); | |
65 | ||
66 | if(args.onclick) { | |
67 | UiEventData *eventdata = malloc(sizeof(UiEventData)); | |
68 | eventdata->callback = args.onclick; | |
69 | eventdata->userdata = args.onclickdata; | |
70 | eventdata->obj = obj; | |
71 | eventdata->value = 0; | |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
72 | XtAddCallback( |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
73 | button, |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
74 | XmNactivateCallback, |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
75 | (XtCallbackProc)ui_push_button_callback, |
100 | 76 | eventdata); |
77 | XtAddCallback( | |
78 | button, | |
79 | XmNdestroyCallback, | |
80 | (XtCallbackProc)ui_destroy_eventdata, | |
81 | eventdata); | |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
82 | } |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
83 | |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
84 | |
100 | 85 | XmStringFree(label); |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
86 | return button; |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
87 | } |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
88 | |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
89 | void ui_push_button_callback(Widget widget, UiEventData *event, XtPointer d) { |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
90 | UiEvent e; |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
91 | e.obj = event->obj; |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
92 | e.window = event->obj->window; |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
93 | e.document = event->obj->ctx->document; |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
94 | e.intval = event->value; |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
95 | event->callback(&e, event->userdata); |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
96 | } |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
97 | |
100 | 98 | UIWIDGET ui_togglebutton_create(UiObject* obj, UiToggleArgs args) { |
99 | Arg xargs[16]; | |
100 | int n = 0; | |
101 | ||
102 | UiContainerPrivate *ctn = ui_obj_container(obj); | |
103 | UI_APPLY_LAYOUT(ctn->layout, args); | |
104 | ||
105 | Widget parent = ctn->prepare(ctn, xargs, &n); | |
106 | XtSetArg(xargs[n], XmNfillOnSelect, True); n++; | |
107 | XtSetArg(xargs[n], XmNindicatorOn, False); n++; | |
108 | ||
109 | XmString label = NULL; | |
110 | if(args.label) { | |
111 | label = XmStringCreateLocalized((char*)args.label); | |
112 | XtSetArg(xargs[n], XmNlabelString, label); n++; | |
113 | } | |
114 | ||
115 | char *name = args.name ? (char*)args.name : "togglebutton"; | |
116 | Widget button = XmCreateToggleButton(parent, name, xargs, n); | |
117 | XtManageChild(button); | |
118 | ctn->add(ctn, button); | |
119 | ||
120 | ui_set_widget_groups(obj->ctx, button, args.groups); | |
121 | ||
122 | ui_bind_togglebutton(obj, button, args.varname, args.value, args.onchange, args.onchangedata, args.enable_group); | |
123 | ||
124 | XmStringFree(label); | |
125 | return button; | |
126 | } | |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
127 | |
100 | 128 | UIWIDGET ui_checkbox_create(UiObject* obj, UiToggleArgs args) { |
129 | Arg xargs[16]; | |
130 | int n = 0; | |
131 | ||
132 | UiContainerPrivate *ctn = ui_obj_container(obj); | |
133 | UI_APPLY_LAYOUT(ctn->layout, args); | |
134 | ||
135 | Widget parent = ctn->prepare(ctn, xargs, &n); | |
136 | ||
137 | XmString label = NULL; | |
138 | if(args.label) { | |
139 | label = XmStringCreateLocalized((char*)args.label); | |
140 | XtSetArg(xargs[n], XmNlabelString, label); n++; | |
141 | } | |
142 | ||
143 | char *name = args.name ? (char*)args.name : "button"; | |
144 | Widget button = XmCreateToggleButton(parent, name, xargs, n); | |
145 | XtManageChild(button); | |
146 | ctn->add(ctn, button); | |
147 | ||
148 | ui_set_widget_groups(obj->ctx, button, args.groups); | |
149 | ||
150 | ui_bind_togglebutton(obj, button, args.varname, args.value, args.onchange, args.onchangedata, args.enable_group); | |
151 | ||
152 | XmStringFree(label); | |
153 | return button; | |
154 | } | |
155 | ||
156 | UIWIDGET ui_switch_create(UiObject* obj, UiToggleArgs args) { | |
157 | return ui_checkbox_create(obj, args); | |
158 | } | |
159 | ||
160 | static void togglebutton_changed(Widget w, UiVarEventData *event, XmToggleButtonCallbackStruct *tb) { | |
161 | if(event->value > 0) { | |
162 | // button in configured to enable/disable states | |
163 | if(tb->set) { | |
164 | ui_set_group(event->obj->ctx, event->value); | |
165 | } else { | |
166 | ui_unset_group(event->obj->ctx, event->value); | |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
167 | } |
100 | 168 | } |
169 | ||
170 | UiEvent e; | |
171 | e.obj = event->obj; | |
172 | e.window = e.obj->window; | |
173 | e.document = e.obj->ctx->document; | |
174 | e.eventdata = NULL; | |
175 | e.intval = XmToggleButtonGetState(w); | |
176 | ||
177 | if(event->callback) { | |
178 | event->callback(&e, event->userdata); | |
179 | } | |
180 | ||
181 | if(event->var && event->var->value) { | |
182 | UiInteger *v = event->var->value; | |
183 | v->value = e.intval; | |
184 | ui_notify_evt(v->observers, &e); | |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
185 | } |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
186 | } |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
187 | |
100 | 188 | void ui_bind_togglebutton( |
189 | UiObject *obj, | |
190 | Widget widget, | |
191 | const char *varname, | |
192 | UiInteger *value, | |
193 | ui_callback onchange, | |
194 | void *onchangedata, | |
195 | int enable_state) | |
196 | { | |
197 | UiVar* var = uic_widget_var(obj->ctx, obj->ctx, value, varname, UI_VAR_INTEGER); | |
198 | if(var) { | |
199 | value = (UiInteger*)var->value; | |
200 | value->obj = widget; | |
201 | value->get = ui_togglebutton_get; | |
202 | value->set = ui_togglebutton_set; | |
203 | ||
204 | if(value->value) { | |
205 | XmToggleButtonSetState(widget, True, False); | |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
206 | } |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
207 | } |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
208 | |
100 | 209 | UiVarEventData *event = malloc(sizeof(UiVarEventData)); |
210 | event->obj = obj; | |
211 | event->callback = onchange; | |
212 | event->userdata = onchangedata; | |
213 | event->var = var; | |
214 | event->observers = NULL; | |
215 | event->value = enable_state; | |
216 | XtAddCallback( | |
217 | widget, | |
218 | XmNvalueChangedCallback, | |
219 | (XtCallbackProc)togglebutton_changed, | |
220 | event); | |
221 | XtAddCallback( | |
222 | widget, | |
223 | XmNdestroyCallback, | |
224 | (XtCallbackProc)ui_destroy_eventdata, | |
225 | event); | |
226 | } | |
227 | ||
228 | int64_t ui_togglebutton_get(UiInteger *i) { | |
229 | Widget togglebutton = i->obj; | |
230 | Boolean state = XmToggleButtonGetState(togglebutton); | |
231 | i->value = state; | |
232 | return state; | |
233 | } | |
234 | ||
235 | void ui_togglebutton_set(UiInteger *i, int64_t value) { | |
236 | Widget togglebutton = i->obj; | |
237 | i->value = value; | |
238 | XmToggleButtonSetState(togglebutton, (Boolean)value, False); | |
239 | } | |
240 | ||
241 | static void destroy_list(Widget w, CxList *list, XtPointer d) { | |
101
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
100
diff
changeset
|
242 | cxListFree(list); |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
243 | } |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
244 | |
100 | 245 | static void radiobutton_changed(Widget w, UiVarEventData *event, XmToggleButtonCallbackStruct *tb) { |
246 | if(event->value > 0) { | |
247 | // button in configured to enable/disable states | |
248 | if(tb->set) { | |
249 | ui_set_group(event->obj->ctx, event->value); | |
250 | } else { | |
251 | ui_unset_group(event->obj->ctx, event->value); | |
252 | } | |
253 | } | |
254 | ||
255 | if(!tb->set) { | |
256 | return; // only handle set-events | |
257 | } | |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
258 | |
100 | 259 | UiInteger *value = NULL; |
260 | int64_t v = 0; | |
261 | if(event->var) { | |
262 | value = event->var->value; | |
263 | // find widget index and update all radiobuttons | |
264 | // the UiInteger value must always be up-to-date | |
265 | CxList *list = value->obj; | |
266 | CxIterator i = cxListIterator(list); | |
267 | cx_foreach(Widget, button, i) { | |
268 | Boolean state = False; | |
269 | if(button == w) { | |
270 | value->value = i.index+1; // update value | |
271 | state = True; | |
272 | } | |
273 | XmToggleButtonSetState(button, state, False); | |
274 | } | |
275 | v = value->value; | |
276 | } | |
277 | ||
278 | UiEvent e; | |
279 | e.obj = event->obj; | |
280 | e.window = e.obj->window; | |
281 | e.document = e.obj->ctx->document; | |
282 | e.eventdata = value; | |
283 | e.intval = v; | |
284 | ||
285 | if(event->callback) { | |
286 | event->callback(&e, event->userdata); | |
287 | } | |
288 | ||
289 | if(value) { | |
290 | ui_notify_evt(value->observers, &e); | |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
291 | } |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
292 | } |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
293 | |
101
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
100
diff
changeset
|
294 | void ui_bind_radiobutton(UiObject *obj, Widget rbutton, UiInteger *value, const char *varname, ui_callback onchange, void *onchangedata, int enable_group) { |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
100
diff
changeset
|
295 | UiVar* var = uic_widget_var(obj->ctx, obj->ctx, value, varname, UI_VAR_INTEGER); |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
100
diff
changeset
|
296 | if(var) { |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
100
diff
changeset
|
297 | UiInteger *value = var->value; |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
100
diff
changeset
|
298 | CxList *rb = value->obj; |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
100
diff
changeset
|
299 | if(!rb) { |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
100
diff
changeset
|
300 | // first button in the radiobutton group |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
100
diff
changeset
|
301 | // create a list for all buttons and use the list as value obj |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
100
diff
changeset
|
302 | rb = cxArrayListCreateSimple(CX_STORE_POINTERS, 4); |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
100
diff
changeset
|
303 | value->obj = rb; |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
100
diff
changeset
|
304 | value->get = ui_radiobutton_get; |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
100
diff
changeset
|
305 | value->set = ui_radiobutton_set; |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
100
diff
changeset
|
306 | |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
100
diff
changeset
|
307 | // the first radio button is also responsible for cleanup |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
100
diff
changeset
|
308 | XtAddCallback( |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
100
diff
changeset
|
309 | rbutton, |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
100
diff
changeset
|
310 | XmNdestroyCallback, |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
100
diff
changeset
|
311 | (XtCallbackProc)destroy_list, |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
100
diff
changeset
|
312 | rb); |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
100
diff
changeset
|
313 | } |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
100
diff
changeset
|
314 | cxListAdd(rb, rbutton); |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
100
diff
changeset
|
315 | |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
100
diff
changeset
|
316 | // set the radiobutton state, if the value is already set |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
100
diff
changeset
|
317 | if(cxListSize(rb) == value->value) { |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
100
diff
changeset
|
318 | XmToggleButtonSetState(rbutton, True, False); |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
100
diff
changeset
|
319 | } |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
100
diff
changeset
|
320 | } |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
100
diff
changeset
|
321 | |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
100
diff
changeset
|
322 | // the radio button needs to handle change events to update all |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
100
diff
changeset
|
323 | // other buttons in the radio button group |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
100
diff
changeset
|
324 | UiVarEventData *event = malloc(sizeof(UiVarEventData)); |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
100
diff
changeset
|
325 | event->obj = obj; |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
100
diff
changeset
|
326 | event->callback = onchange; |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
100
diff
changeset
|
327 | event->userdata = onchangedata; |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
100
diff
changeset
|
328 | event->observers = NULL; |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
100
diff
changeset
|
329 | event->var = var; |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
100
diff
changeset
|
330 | event->value = enable_group; |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
100
diff
changeset
|
331 | XtAddCallback( |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
100
diff
changeset
|
332 | rbutton, |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
100
diff
changeset
|
333 | XmNvalueChangedCallback, |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
100
diff
changeset
|
334 | (XtCallbackProc)radiobutton_changed, |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
100
diff
changeset
|
335 | event); |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
100
diff
changeset
|
336 | XtAddCallback( |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
100
diff
changeset
|
337 | rbutton, |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
100
diff
changeset
|
338 | XmNdestroyCallback, |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
100
diff
changeset
|
339 | (XtCallbackProc)ui_destroy_eventdata, |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
100
diff
changeset
|
340 | event); |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
100
diff
changeset
|
341 | } |
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
100
diff
changeset
|
342 | |
100 | 343 | UIWIDGET ui_radiobutton_create(UiObject* obj, UiToggleArgs args) { |
344 | Arg xargs[16]; | |
345 | int n = 0; | |
346 | ||
347 | UiContainerPrivate *ctn = ui_obj_container(obj); | |
348 | UI_APPLY_LAYOUT(ctn->layout, args); | |
349 | ||
350 | Widget parent = ctn->prepare(ctn, xargs, &n); | |
351 | XtSetArg(xargs[n], XmNindicatorType, XmONE_OF_MANY_ROUND); n++; | |
352 | XmString label = NULL; | |
353 | if(args.label) { | |
354 | label = XmStringCreateLocalized((char*)args.label); | |
355 | XtSetArg(xargs[n], XmNlabelString, label); n++; | |
356 | } | |
357 | ||
358 | char *name = args.name ? (char*)args.name : "button"; | |
359 | Widget button = XmCreateToggleButton(parent, name, xargs, n); | |
360 | XtManageChild(button); | |
361 | ctn->add(ctn, button); | |
362 | ||
363 | ui_set_widget_groups(obj->ctx, button, args.groups); | |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
364 | |
100 | 365 | UiVar* var = uic_widget_var(obj->ctx, obj->ctx, args.value, args.varname, UI_VAR_INTEGER); |
366 | if(var) { | |
367 | UiInteger *value = var->value; | |
368 | CxList *rb = value->obj; | |
369 | if(!rb) { | |
370 | // first button in the radiobutton group | |
371 | // create a list for all buttons and use the list as value obj | |
372 | rb = cxArrayListCreateSimple(CX_STORE_POINTERS, 4); | |
373 | value->obj = rb; | |
374 | value->get = ui_radiobutton_get; | |
375 | value->set = ui_radiobutton_set; | |
376 | ||
377 | // the first radio button is also responsible for cleanup | |
378 | XtAddCallback( | |
379 | button, | |
380 | XmNdestroyCallback, | |
381 | (XtCallbackProc)destroy_list, | |
382 | rb); | |
383 | } | |
384 | cxListAdd(rb, button); | |
385 | ||
386 | // set the radiobutton state, if the value is already set | |
387 | if(cxListSize(rb) == value->value) { | |
388 | XmToggleButtonSetState(button, True, False); | |
389 | } | |
390 | } | |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
391 | |
100 | 392 | // the radio button needs to handle change events to update all |
393 | // other buttons in the radio button group | |
394 | UiVarEventData *event = malloc(sizeof(UiVarEventData)); | |
395 | event->obj = obj; | |
396 | event->callback = args.onchange; | |
397 | event->userdata = args.onchangedata; | |
398 | event->observers = NULL; | |
399 | event->var = var; | |
400 | event->value = args.enable_group; | |
401 | XtAddCallback( | |
402 | button, | |
403 | XmNvalueChangedCallback, | |
404 | (XtCallbackProc)radiobutton_changed, | |
405 | event); | |
406 | XtAddCallback( | |
407 | button, | |
408 | XmNdestroyCallback, | |
409 | (XtCallbackProc)ui_destroy_eventdata, | |
410 | event); | |
411 | ||
412 | XmStringFree(label); | |
413 | return button; | |
414 | ||
415 | ||
416 | } | |
417 | ||
418 | int64_t ui_radiobutton_get(UiInteger *i) { | |
419 | // the UiInteger should be updated automatically by change events | |
420 | return i->value; | |
421 | } | |
422 | ||
423 | void ui_radiobutton_set(UiInteger *i, int64_t value) { | |
424 | CxList *list = i->obj; | |
425 | if(i->value > 0) { | |
426 | Widget current = cxListAt(list, i->value-1); | |
427 | if(current) { | |
428 | XmToggleButtonSetState(current, False, False); | |
429 | } | |
430 | } | |
431 | if(value > 0 && value <= cxListSize(list)) { | |
432 | Widget button = cxListAt(list, value-1); | |
433 | if(button) { | |
434 | XmToggleButtonSetState(button, True, False); | |
435 | i->value = value; | |
436 | } | |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
437 | } |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
438 | } |