ui/motif/button.c

Thu, 12 Dec 2024 20:01:43 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Thu, 12 Dec 2024 20:01:43 +0100
changeset 100
d2bd73d28ff1
parent 0
2483f517c562
permissions
-rw-r--r--

update 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
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
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
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
41 #include <Xm/XmAll.h>
0
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
42
100
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
43
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
44 UIWIDGET ui_button_create(UiObject* obj, UiButtonArgs args) {
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
45 Arg xargs[16];
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
46 int n = 0;
0
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
47
100
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
48 UiContainerPrivate *ctn = ui_obj_container(obj);
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
49 UI_APPLY_LAYOUT(ctn->layout, args);
0
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
50
100
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
51 Widget parent = ctn->prepare(ctn, xargs, &n);
0
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
52
100
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
53 XmString label = NULL;
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
54 if(args.label) {
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
55 label = XmStringCreateLocalized((char*)args.label);
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
56 XtSetArg(xargs[n], XmNlabelString, label); n++;
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
57 }
0
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
58
100
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
59 char *name = args.name ? (char*)args.name : "button";
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
60 Widget button = XmCreatePushButton(parent, name, xargs, n);
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
61 XtManageChild(button);
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
62 ctn->add(ctn, button);
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
63
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
64 ui_set_widget_groups(obj->ctx, button, args.groups);
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
65
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
66 if(args.onclick) {
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
67 UiEventData *eventdata = malloc(sizeof(UiEventData));
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
68 eventdata->callback = args.onclick;
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
69 eventdata->userdata = args.onclickdata;
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
70 eventdata->obj = obj;
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
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
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
76 eventdata);
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
77 XtAddCallback(
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
78 button,
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
79 XmNdestroyCallback,
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
80 (XtCallbackProc)ui_destroy_eventdata,
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
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
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
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
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
98 UIWIDGET ui_togglebutton_create(UiObject* obj, UiToggleArgs args) {
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
99 Arg xargs[16];
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
100 int n = 0;
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
101
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
102 UiContainerPrivate *ctn = ui_obj_container(obj);
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
103 UI_APPLY_LAYOUT(ctn->layout, args);
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
104
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
105 Widget parent = ctn->prepare(ctn, xargs, &n);
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
106 XtSetArg(xargs[n], XmNfillOnSelect, True); n++;
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
107 XtSetArg(xargs[n], XmNindicatorOn, False); n++;
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
108
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
109 XmString label = NULL;
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
110 if(args.label) {
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
111 label = XmStringCreateLocalized((char*)args.label);
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
112 XtSetArg(xargs[n], XmNlabelString, label); n++;
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
113 }
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
114
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
115 char *name = args.name ? (char*)args.name : "togglebutton";
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
116 Widget button = XmCreateToggleButton(parent, name, xargs, n);
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
117 XtManageChild(button);
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
118 ctn->add(ctn, button);
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
119
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
120 ui_set_widget_groups(obj->ctx, button, args.groups);
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
121
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
122 ui_bind_togglebutton(obj, button, args.varname, args.value, args.onchange, args.onchangedata, args.enable_group);
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
123
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
124 XmStringFree(label);
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
125 return button;
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
126 }
0
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
127
100
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
128 UIWIDGET ui_checkbox_create(UiObject* obj, UiToggleArgs args) {
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
129 Arg xargs[16];
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
130 int n = 0;
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
131
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
132 UiContainerPrivate *ctn = ui_obj_container(obj);
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
133 UI_APPLY_LAYOUT(ctn->layout, args);
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
134
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
135 Widget parent = ctn->prepare(ctn, xargs, &n);
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
136
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
137 XmString label = NULL;
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
138 if(args.label) {
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
139 label = XmStringCreateLocalized((char*)args.label);
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
140 XtSetArg(xargs[n], XmNlabelString, label); n++;
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
141 }
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
142
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
143 char *name = args.name ? (char*)args.name : "button";
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
144 Widget button = XmCreateToggleButton(parent, name, xargs, n);
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
145 XtManageChild(button);
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
146 ctn->add(ctn, button);
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
147
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
148 ui_set_widget_groups(obj->ctx, button, args.groups);
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
149
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
150 ui_bind_togglebutton(obj, button, args.varname, args.value, args.onchange, args.onchangedata, args.enable_group);
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
151
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
152 XmStringFree(label);
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
153 return button;
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
154 }
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
155
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
156 UIWIDGET ui_switch_create(UiObject* obj, UiToggleArgs args) {
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
157 return ui_checkbox_create(obj, args);
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
158 }
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
159
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
160 static void togglebutton_changed(Widget w, UiVarEventData *event, XmToggleButtonCallbackStruct *tb) {
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
161 if(event->value > 0) {
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
162 // button in configured to enable/disable states
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
163 if(tb->set) {
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
164 ui_set_group(event->obj->ctx, event->value);
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
165 } else {
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
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
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
168 }
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
169
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
170 UiEvent e;
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
171 e.obj = event->obj;
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
172 e.window = e.obj->window;
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
173 e.document = e.obj->ctx->document;
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
174 e.eventdata = NULL;
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
175 e.intval = XmToggleButtonGetState(w);
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
176
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
177 if(event->callback) {
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
178 event->callback(&e, event->userdata);
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
179 }
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
180
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
181 if(event->var && event->var->value) {
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
182 UiInteger *v = event->var->value;
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
183 v->value = e.intval;
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
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
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
188 void ui_bind_togglebutton(
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
189 UiObject *obj,
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
190 Widget widget,
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
191 const char *varname,
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
192 UiInteger *value,
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
193 ui_callback onchange,
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
194 void *onchangedata,
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
195 int enable_state)
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
196 {
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
197 UiVar* var = uic_widget_var(obj->ctx, obj->ctx, value, varname, UI_VAR_INTEGER);
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
198 if(var) {
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
199 value = (UiInteger*)var->value;
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
200 value->obj = widget;
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
201 value->get = ui_togglebutton_get;
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
202 value->set = ui_togglebutton_set;
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
203
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
204 if(value->value) {
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
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
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
209 UiVarEventData *event = malloc(sizeof(UiVarEventData));
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
210 event->obj = obj;
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
211 event->callback = onchange;
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
212 event->userdata = onchangedata;
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
213 event->var = var;
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
214 event->observers = NULL;
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
215 event->value = enable_state;
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
216 XtAddCallback(
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
217 widget,
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
218 XmNvalueChangedCallback,
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
219 (XtCallbackProc)togglebutton_changed,
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
220 event);
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
221 XtAddCallback(
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
222 widget,
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
223 XmNdestroyCallback,
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
224 (XtCallbackProc)ui_destroy_eventdata,
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
225 event);
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
226 }
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
227
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
228 int64_t ui_togglebutton_get(UiInteger *i) {
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
229 Widget togglebutton = i->obj;
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
230 Boolean state = XmToggleButtonGetState(togglebutton);
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
231 i->value = state;
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
232 return state;
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
233 }
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
234
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
235 void ui_togglebutton_set(UiInteger *i, int64_t value) {
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
236 Widget togglebutton = i->obj;
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
237 i->value = value;
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
238 XmToggleButtonSetState(togglebutton, (Boolean)value, False);
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
239 }
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
240
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
241 static void destroy_list(Widget w, CxList *list, XtPointer d) {
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
242 cxListDestroy(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
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
245 static void radiobutton_changed(Widget w, UiVarEventData *event, XmToggleButtonCallbackStruct *tb) {
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
246 if(event->value > 0) {
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
247 // button in configured to enable/disable states
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
248 if(tb->set) {
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
249 ui_set_group(event->obj->ctx, event->value);
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
250 } else {
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
251 ui_unset_group(event->obj->ctx, event->value);
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
252 }
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
253 }
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
254
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
255 if(!tb->set) {
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
256 return; // only handle set-events
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
257 }
0
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
258
100
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
259 UiInteger *value = NULL;
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
260 int64_t v = 0;
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
261 if(event->var) {
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
262 value = event->var->value;
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
263 // find widget index and update all radiobuttons
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
264 // the UiInteger value must always be up-to-date
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
265 CxList *list = value->obj;
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
266 CxIterator i = cxListIterator(list);
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
267 cx_foreach(Widget, button, i) {
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
268 Boolean state = False;
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
269 if(button == w) {
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
270 value->value = i.index+1; // update value
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
271 state = True;
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
272 }
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
273 XmToggleButtonSetState(button, state, False);
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
274 }
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
275 v = value->value;
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
276 }
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
277
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
278 UiEvent e;
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
279 e.obj = event->obj;
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
280 e.window = e.obj->window;
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
281 e.document = e.obj->ctx->document;
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
282 e.eventdata = value;
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
283 e.intval = v;
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
284
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
285 if(event->callback) {
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
286 event->callback(&e, event->userdata);
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
287 }
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
288
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
289 if(value) {
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
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
100
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
294 UIWIDGET ui_radiobutton_create(UiObject* obj, UiToggleArgs args) {
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
295 Arg xargs[16];
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
296 int n = 0;
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
297
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
298 UiContainerPrivate *ctn = ui_obj_container(obj);
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
299 UI_APPLY_LAYOUT(ctn->layout, args);
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
300
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
301 Widget parent = ctn->prepare(ctn, xargs, &n);
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
302 XtSetArg(xargs[n], XmNindicatorType, XmONE_OF_MANY_ROUND); n++;
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
303 XmString label = NULL;
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
304 if(args.label) {
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
305 label = XmStringCreateLocalized((char*)args.label);
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
306 XtSetArg(xargs[n], XmNlabelString, label); n++;
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
307 }
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
308
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
309 char *name = args.name ? (char*)args.name : "button";
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
310 Widget button = XmCreateToggleButton(parent, name, xargs, n);
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
311 XtManageChild(button);
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
312 ctn->add(ctn, button);
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
313
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
314 ui_set_widget_groups(obj->ctx, button, args.groups);
0
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
315
100
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
316 UiVar* var = uic_widget_var(obj->ctx, obj->ctx, args.value, args.varname, UI_VAR_INTEGER);
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
317 if(var) {
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
318 UiInteger *value = var->value;
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
319 CxList *rb = value->obj;
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
320 if(!rb) {
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
321 // first button in the radiobutton group
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
322 // create a list for all buttons and use the list as value obj
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
323 rb = cxArrayListCreateSimple(CX_STORE_POINTERS, 4);
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
324 value->obj = rb;
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
325 value->get = ui_radiobutton_get;
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
326 value->set = ui_radiobutton_set;
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
327
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
328 // the first radio button is also responsible for cleanup
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
329 XtAddCallback(
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
330 button,
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
331 XmNdestroyCallback,
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
332 (XtCallbackProc)destroy_list,
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
333 rb);
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
334 }
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
335 cxListAdd(rb, button);
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
336
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
337 // set the radiobutton state, if the value is already set
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
338 if(cxListSize(rb) == value->value) {
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
339 XmToggleButtonSetState(button, True, False);
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
340 }
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
341 }
0
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
342
100
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
343 // the radio button needs to handle change events to update all
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
344 // other buttons in the radio button group
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
345 UiVarEventData *event = malloc(sizeof(UiVarEventData));
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
346 event->obj = obj;
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
347 event->callback = args.onchange;
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
348 event->userdata = args.onchangedata;
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
349 event->observers = NULL;
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
350 event->var = var;
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
351 event->value = args.enable_group;
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
352 XtAddCallback(
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
353 button,
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
354 XmNvalueChangedCallback,
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
355 (XtCallbackProc)radiobutton_changed,
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
356 event);
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
357 XtAddCallback(
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
358 button,
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
359 XmNdestroyCallback,
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
360 (XtCallbackProc)ui_destroy_eventdata,
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
361 event);
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
362
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
363 XmStringFree(label);
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
364 return button;
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
365
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
366
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
367 }
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
368
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
369 int64_t ui_radiobutton_get(UiInteger *i) {
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
370 // the UiInteger should be updated automatically by change events
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
371 return i->value;
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
372 }
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
373
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
374 void ui_radiobutton_set(UiInteger *i, int64_t value) {
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
375 CxList *list = i->obj;
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
376 if(i->value > 0) {
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
377 Widget current = cxListAt(list, i->value-1);
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
378 if(current) {
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
379 XmToggleButtonSetState(current, False, False);
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
380 }
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
381 }
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
382 if(value > 0 && value <= cxListSize(list)) {
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
383 Widget button = cxListAt(list, value-1);
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
384 if(button) {
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
385 XmToggleButtonSetState(button, True, False);
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
386 i->value = value;
d2bd73d28ff1 update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
387 }
0
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
388 }
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
389 }

mercurial