2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 2014 Olaf Wintermann. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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.
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.
33 #include "container.h"
34 #include "../common/context.h"
35 #include <ucx/mempool.h>
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);
45 XtSetArg(args[n], XmNlabelString, str);
48 Widget parent = ct->prepare(ct, args, &n, FALSE);
49 Widget button = XmCreatePushButton(parent, "button", args, n);
53 UiEventData *event = ucx_mempool_malloc(
57 event->userdata = data;
63 (XtCallbackProc)ui_push_button_callback,
67 XtManageChild(button);
73 int64_t ui_toggle_button_get(UiInteger *i) {
75 XtVaGetValues(i->obj, XmNset, &state, NULL);
80 void ui_toggle_button_set(UiInteger *i, int64_t value) {
82 XtSetArg(arg, XmNset, value);
83 XtSetValues(i->obj, &arg, 1);
87 void ui_toggle_button_callback(
90 XmToggleButtonCallbackStruct *tb)
94 e.window = event->obj->window;
97 event->callback(&e, event->userdata);
100 void ui_push_button_callback(Widget widget, UiEventData *event, XtPointer d) {
103 e.window = event->obj->window;
104 e.document = event->obj->ctx->document;
105 e.intval = event->value;
106 event->callback(&e, event->userdata);
110 static void radio_callback(
112 RadioEventData *event,
113 XmToggleButtonCallbackStruct *tb)
116 RadioButtonGroup *group = event->group;
119 XtSetArg(arg, XmNset, FALSE);
120 XtSetValues(group->current, &arg, 1);
122 group->current = widget;
126 UIWIDGET ui_radiobutton(UiObject *obj, char *label, UiInteger *rgroup) {
127 UiContainer *ct = uic_get_current_container(obj);
128 XmString str = XmStringCreateLocalized(label);
133 XtSetArg(args[n], XmNlabelString, str);
135 XtSetArg(args[n], XmNindicatorType, XmONE_OF_MANY_ROUND);
138 Widget parent = ct->prepare(ct, args, &n, FALSE);
139 Widget button = XmCreateToggleButton(parent, "radiobutton", args, n);
143 RadioButtonGroup *group;
146 group->buttons = ucx_list_append(group->buttons, button);
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
155 XtSetArg(arg, XmNset, TRUE);
156 XtSetValues(button, &arg, 1);
159 group->current = button;
162 RadioEventData *event = malloc(sizeof(RadioEventData));
164 event->callback = NULL;
165 event->userdata = NULL;
166 event->group = group;
169 XmNvalueChangedCallback,
170 (XtCallbackProc)radio_callback,
173 rgroup->get = ui_radiobutton_get;
174 rgroup->set = ui_radiobutton_set;
177 XtManageChild(button);
181 int64_t ui_radiobutton_get(UiInteger *value) {
182 RadioButtonGroup *group = value->obj;
184 int i = ucx_list_find(group->buttons, group->current, NULL, NULL);
193 void ui_radiobutton_set(UiInteger *value, int64_t i) {
194 RadioButtonGroup *group = value->obj;
197 XtSetArg(arg, XmNset, FALSE);
198 XtSetValues(group->current, &arg, 1);
200 UcxList *elm = ucx_list_get(group->buttons, i);
202 Widget button = elm->data;
203 XtSetArg(arg, XmNset, TRUE);
204 XtSetValues(button, &arg, 1);
205 group->current = button;