add existing code (build system, libs, initial mizucp code)
[mizunara.git] / ui / motif / button.c
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 <ucx/mempool.h>
36
37
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);
41     
42     int n = 0;
43     Arg args[16];
44     
45     XtSetArg(args[n], XmNlabelString, str);
46     n++;
47     
48     Widget parent = ct->prepare(ct, args, &n, FALSE);
49     Widget button = XmCreatePushButton(parent, "button", args, n);
50     ct->add(ct, button);
51     
52     if(f) {
53         UiEventData *event = ucx_mempool_malloc(
54                 obj->ctx->mempool,
55                 sizeof(UiEventData));
56         event->obj = obj;
57         event->userdata = data;
58         event->callback = f;
59         event->value = 0;
60         XtAddCallback(
61                 button,
62                 XmNactivateCallback,
63                 (XtCallbackProc)ui_push_button_callback,
64                 event);
65     }
66     
67     XtManageChild(button);
68     
69     return button;
70 }
71
72 // wrapper
73 int64_t ui_toggle_button_get(UiInteger *i) {
74     int state = 0;
75     XtVaGetValues(i->obj, XmNset, &state, NULL);
76     i->value = state;
77     return state;
78 }
79
80 void ui_toggle_button_set(UiInteger *i, int64_t value) {
81     Arg arg;
82     XtSetArg(arg, XmNset, value);
83     XtSetValues(i->obj, &arg, 1);
84     i->value = value;
85 }
86
87 void ui_toggle_button_callback(
88         Widget widget,
89         UiEventData *event,
90         XmToggleButtonCallbackStruct *tb)
91 {
92     UiEvent e;
93     e.obj = event->obj;
94     e.window = event->obj->window;
95     // TODO: e.document
96     e.intval = tb->set;
97     event->callback(&e, event->userdata); 
98 }
99
100 void ui_push_button_callback(Widget widget, UiEventData *event, XtPointer d) {
101     UiEvent e;
102     e.obj = event->obj;
103     e.window = event->obj->window;
104     e.document = event->obj->ctx->document;
105     e.intval = event->value;
106     event->callback(&e, event->userdata);
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 int64_t 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, int64_t 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 }