ui/gtk/button.c

changeset 0
804d8803eade
child 2
ea89bbb0c4c8
equal deleted inserted replaced
-1:000000000000 0:804d8803eade
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2017 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 <ucx/mempool.h>
35 #include "../common/context.h"
36 #include "../common/object.h"
37
38 UIWIDGET ui_button(UiObject *obj, char *label, ui_callback f, void *data) {
39 GtkWidget *button = gtk_button_new_with_label(label);
40
41 if(f) {
42 UiEventData *event = malloc(sizeof(UiEventData));
43 event->obj = obj;
44 event->userdata = data;
45 event->callback = f;
46 event->value = 0;
47
48 g_signal_connect(
49 button,
50 "clicked",
51 G_CALLBACK(ui_button_clicked),
52 event);
53 g_signal_connect(
54 button,
55 "destroy",
56 G_CALLBACK(ui_destroy_userdata),
57 event);
58 }
59
60 UiContainer *ct = uic_get_current_container(obj);
61 ct->add(ct, button, FALSE);
62
63 return button;
64 }
65
66
67 void ui_button_clicked(GtkWidget *widget, UiEventData *event) {
68 UiEvent e;
69 e.obj = event->obj;
70 e.window = event->obj->window;
71 e.document = event->obj->ctx->document;
72 e.eventdata = NULL;
73 e.intval = event->value;
74 event->callback(&e, event->userdata);
75 }
76
77 void ui_button_toggled(GtkToggleToolButton *widget, UiEventData *event) {
78 UiEvent e;
79 e.obj = event->obj;
80 e.window = event->obj->window;
81 e.document = event->obj->ctx->document;
82 e.eventdata = NULL;
83 e.intval = gtk_toggle_tool_button_get_active(widget);
84 event->callback(&e, event->userdata);
85 }
86
87 int64_t ui_toggle_button_get(UiInteger *integer) {
88 GtkToggleButton *button = integer->obj;
89 integer->value = (int)gtk_toggle_button_get_active(button);
90 return integer->value;
91 }
92
93 void ui_toggle_button_set(UiInteger *integer, int64_t value) {
94 GtkToggleButton *button = integer->obj;
95 integer->value = value;
96 gtk_toggle_button_set_active(button, value != 0 ? TRUE : FALSE);
97 }
98
99 void ui_toggled_obs(GtkToggleToolButton *widget, UiVarEventData *event) {
100 UiEvent e;
101 e.obj = event->obj;
102 e.window = event->obj->window;
103 e.document = event->obj->ctx->document;
104 e.eventdata = event->var->value;
105 e.intval = gtk_toggle_tool_button_get_active(widget);
106
107 UiInteger *i = event->var->value;
108 ui_notify_evt(i->observers, &e);
109 }
110
111 UIWIDGET ui_checkbox_var(UiObject *obj, char *label, UiVar *var) {
112 GtkWidget *button = gtk_check_button_new_with_label(label);
113
114 // bind value
115 if(var) {
116 UiInteger *value = var->value;
117 value->obj = GTK_TOGGLE_BUTTON(button);
118 value->get = ui_toggle_button_get;
119 value->set = ui_toggle_button_set;
120 gtk_toggle_button_set_active(value->obj, value->value);
121
122 UiVarEventData *event = malloc(sizeof(UiVarEventData));
123 event->obj = obj;
124 event->var = var;
125 event->observers = NULL;
126
127 g_signal_connect(
128 button,
129 "clicked",
130 G_CALLBACK(ui_toggled_obs),
131 event);
132 g_signal_connect(
133 button,
134 "destroy",
135 G_CALLBACK(ui_destroy_vardata),
136 event);
137 }
138
139 UiContainer *ct = uic_get_current_container(obj);
140 ct->add(ct, button, FALSE);
141
142 return button;
143 }
144
145 UIWIDGET ui_checkbox(UiObject *obj, char *label, UiInteger *value) {
146 UiVar *var = NULL;
147 if(value) {
148 var = malloc(sizeof(UiVar));
149 var->value = value;
150 var->type = UI_VAR_SPECIAL;
151 }
152 return ui_checkbox_var(obj, label, var);
153 }
154
155 UIWIDGET ui_checkbox_nv(UiObject *obj, char *label, char *varname) {
156 UiVar *var = uic_create_var(obj->ctx, varname, UI_VAR_INTEGER);
157 return ui_checkbox_var(obj, label, var);
158 }
159
160
161 UIWIDGET ui_radiobutton_var(UiObject *obj, char *label, UiVar *var) {
162 GSList *rg = NULL;
163 UiInteger *rgroup;
164
165 if(var) {
166 rgroup = var->value;
167 rg = rgroup->obj;
168 }
169
170 GtkWidget *rbutton = gtk_radio_button_new_with_label(rg, label);
171 rg = gtk_radio_button_get_group(GTK_RADIO_BUTTON(rbutton));
172
173 if(rgroup) {
174 rgroup->obj = rg;
175 rgroup->get = ui_radiobutton_get;
176 rgroup->set = ui_radiobutton_set;
177
178 ui_radiobutton_set(rgroup, rgroup->value);
179
180 UiVarEventData *event = malloc(sizeof(UiVarEventData));
181 event->obj = obj;
182 event->var = var;
183 event->observers = NULL;
184
185 g_signal_connect(
186 rbutton,
187 "clicked",
188 G_CALLBACK(ui_radio_obs),
189 event);
190 g_signal_connect(
191 rbutton,
192 "destroy",
193 G_CALLBACK(ui_destroy_vardata),
194 event);
195 }
196
197 UiContainer *ct = uic_get_current_container(obj);
198 ct->add(ct, rbutton, FALSE);
199
200 return rbutton;
201 }
202
203 UIWIDGET ui_radiobutton(UiObject *obj, char *label, UiInteger *rgroup) {
204 UiVar *var = NULL;
205 if(rgroup) {
206 var = malloc(sizeof(UiVar));
207 var->value = rgroup;
208 var->type = UI_VAR_SPECIAL;
209 }
210 return ui_radiobutton_var(obj, label, var);
211 }
212
213 UIWIDGET ui_radiobutton_nv(UiObject *obj, char *label, char *varname) {
214 UiVar *var = uic_create_var(obj->ctx, varname, UI_VAR_INTEGER);
215 return ui_radiobutton_var(obj, label, var);
216 }
217
218 void ui_radio_obs(GtkToggleToolButton *widget, UiVarEventData *event) {
219 UiInteger *i = event->var->value;
220
221 UiEvent e;
222 e.obj = event->obj;
223 e.window = event->obj->window;
224 e.document = event->obj->ctx->document;
225 e.eventdata = NULL;
226 e.intval = i->get(i);
227
228 ui_notify_evt(i->observers, &e);
229 }
230
231 int64_t ui_radiobutton_get(UiInteger *value) {
232 int selection = 0;
233 GSList *ls = value->obj;
234 int i = 0;
235 guint len = g_slist_length(ls);
236 while(ls) {
237 if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(ls->data))) {
238 selection = len - i - 1;
239 break;
240 }
241 ls = ls->next;
242 i++;
243 }
244
245 value->value = selection;
246 return selection;
247 }
248
249 void ui_radiobutton_set(UiInteger *value, int64_t i) {
250 GSList *ls = value->obj;
251 int s = g_slist_length(ls) - 1 - i;
252 int j = 0;
253 while(ls) {
254 if(j == s) {
255 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(ls->data), TRUE);
256 break;
257 }
258 ls = ls->next;
259 j++;
260 }
261
262 value->value = i;
263 }
264

mercurial