ui/motif/button.c

changeset 112
fdd33964b35f
parent 98
efaae97bd95b
child 153
ee49d1852a5f
equal deleted inserted replaced
111:40dbf1a7526a 112:fdd33964b35f
103 e.window = event->obj->window; 103 e.window = event->obj->window;
104 e.document = event->obj->ctx->document; 104 e.document = event->obj->ctx->document;
105 e.intval = event->value; 105 e.intval = event->value;
106 event->callback(&e, event->userdata); 106 event->callback(&e, event->userdata);
107 } 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 // this is the first button in the radiobutton group
153 // so we should enable it
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 int 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, int 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 }

mercurial