ui/motif/button.c

changeset 0
2483f517c562
equal deleted inserted replaced
-1:000000000000 0:2483f517c562
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2014 Olaf Wintermann. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
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 <cx/mempool.h>
36
37 #include <cx/linked_list.h>
38 #include <cx/array_list.h>
39 #include <cx/compare.h>
40
41
42 UIWIDGET ui_button(UiObject *obj, char *label, ui_callback f, void *data) {
43 UiContainer *ct = uic_get_current_container(obj);
44 XmString str = XmStringCreateLocalized(label);
45
46 int n = 0;
47 Arg args[16];
48
49 XtSetArg(args[n], XmNlabelString, str);
50 n++;
51
52 Widget parent = ct->prepare(ct, args, &n, FALSE);
53 Widget button = XmCreatePushButton(parent, "button", args, n);
54 ct->add(ct, button);
55
56 if(f) {
57 UiEventData *event = cxMalloc(
58 obj->ctx->allocator,
59 sizeof(UiEventData));
60 event->obj = obj;
61 event->userdata = data;
62 event->callback = f;
63 event->value = 0;
64 XtAddCallback(
65 button,
66 XmNactivateCallback,
67 (XtCallbackProc)ui_push_button_callback,
68 event);
69 }
70
71 XtManageChild(button);
72
73 return button;
74 }
75
76 // wrapper
77 int64_t ui_toggle_button_get(UiInteger *i) {
78 int state = 0;
79 XtVaGetValues(i->obj, XmNset, &state, NULL);
80 i->value = state;
81 return state;
82 }
83
84 void ui_toggle_button_set(UiInteger *i, int64_t value) {
85 Arg arg;
86 XtSetArg(arg, XmNset, value);
87 XtSetValues(i->obj, &arg, 1);
88 i->value = value;
89 }
90
91 void ui_toggle_button_callback(
92 Widget widget,
93 UiEventData *event,
94 XmToggleButtonCallbackStruct *tb)
95 {
96 UiEvent e;
97 e.obj = event->obj;
98 e.window = event->obj->window;
99 // TODO: e.document
100 e.intval = tb->set;
101 event->callback(&e, event->userdata);
102 }
103
104 void ui_push_button_callback(Widget widget, UiEventData *event, XtPointer d) {
105 UiEvent e;
106 e.obj = event->obj;
107 e.window = event->obj->window;
108 e.document = event->obj->ctx->document;
109 e.intval = event->value;
110 event->callback(&e, event->userdata);
111 }
112
113
114 static void radio_callback(
115 Widget widget,
116 RadioEventData *event,
117 XmToggleButtonCallbackStruct *tb)
118 {
119 if(tb->set) {
120 RadioButtonGroup *group = event->group;
121 if(group->current) {
122 Arg arg;
123 XtSetArg(arg, XmNset, FALSE);
124 XtSetValues(group->current, &arg, 1);
125 }
126 group->current = widget;
127 }
128 }
129
130 UIWIDGET ui_radiobutton(UiObject *obj, char *label, UiInteger *rgroup) {
131 UiContainer *ct = uic_get_current_container(obj);
132 XmString str = XmStringCreateLocalized(label);
133
134 int n = 0;
135 Arg args[16];
136
137 XtSetArg(args[n], XmNlabelString, str);
138 n++;
139 XtSetArg(args[n], XmNindicatorType, XmONE_OF_MANY_ROUND);
140 n++;
141
142 Widget parent = ct->prepare(ct, args, &n, FALSE);
143 Widget button = XmCreateToggleButton(parent, "radiobutton", args, n);
144 ct->add(ct, button);
145
146 if(rgroup) {
147 RadioButtonGroup *group;
148 if(rgroup->obj) {
149 group = rgroup->obj;
150 if(!group->buttons) {
151 group->buttons = cxArrayListCreate(cxDefaultAllocator, cx_cmp_uintptr, CX_STORE_POINTERS, 8);
152 }
153 cxListAdd(group->buttons, button);
154 group->ref++;
155 } else {
156 group = malloc(sizeof(RadioButtonGroup));
157 group->buttons = cxArrayListCreate(cxDefaultAllocator, cx_cmp_uintptr, CX_STORE_POINTERS, 8);
158 cxListAdd(group->buttons, button);
159 group->current = button;
160 // this is the first button in the radiobutton group
161 // so we should enable it
162 Arg arg;
163 XtSetArg(arg, XmNset, TRUE);
164 XtSetValues(button, &arg, 1);
165 rgroup->obj = group;
166
167 group->current = button;
168 }
169
170 RadioEventData *event = malloc(sizeof(RadioEventData));
171 event->obj = obj;
172 event->callback = NULL;
173 event->userdata = NULL;
174 event->group = group;
175 XtAddCallback(
176 button,
177 XmNvalueChangedCallback,
178 (XtCallbackProc)radio_callback,
179 event);
180
181 rgroup->get = ui_radiobutton_get;
182 rgroup->set = ui_radiobutton_set;
183 }
184
185 XtManageChild(button);
186 return button;
187 }
188
189 int64_t ui_radiobutton_get(UiInteger *value) {
190 RadioButtonGroup *group = value->obj;
191
192 int i = cxListFind(group->buttons, group->current);
193 if (i >= 0) {
194 value->value = i;
195 return i;
196 } else {
197 return 0;
198 }
199 }
200
201 void ui_radiobutton_set(UiInteger *value, int64_t i) {
202 RadioButtonGroup *group = value->obj;
203 Arg arg;
204
205 XtSetArg(arg, XmNset, FALSE);
206 XtSetValues(group->current, &arg, 1);
207
208 Widget button = cxListAt(group->buttons, i);
209 if(button) {
210 XtSetArg(arg, XmNset, TRUE);
211 XtSetValues(button, &arg, 1);
212 group->current = button;
213 }
214 }

mercurial