|
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 #include <string.h> |
|
32 |
|
33 #include "toolbar.h" |
|
34 #include "button.h" |
|
35 #include "image.h" |
|
36 #include "tree.h" |
|
37 #include <ucx/mempool.h> |
|
38 #include "../common/context.h" |
|
39 |
|
40 static UcxMap *toolbar_items; |
|
41 static UcxList *defaults; |
|
42 |
|
43 void ui_toolbar_init() { |
|
44 toolbar_items = ucx_map_new(16); |
|
45 } |
|
46 |
|
47 void ui_toolitem(char *name, char *label, ui_callback f, void *udata) { |
|
48 ui_toolitem_img(name, label, NULL, f, udata); |
|
49 } |
|
50 |
|
51 void ui_toolitem_st(char *name, char *stockid, ui_callback f, void *userdata) { |
|
52 ui_toolitem_stgr(name, stockid, f, userdata, -1); |
|
53 } |
|
54 |
|
55 void ui_toolitem_sti(char *name, char *stockid, ui_callback f, void *userdata) { |
|
56 ui_toolitem_stgri(name, stockid, f, userdata, -1); |
|
57 } |
|
58 |
|
59 void ui_toolitem_stgr(char *name, char *stockid, ui_callback f, void *userdata, ...) { |
|
60 va_list ap; |
|
61 va_start(ap, userdata); |
|
62 ui_toolitem_vstgr(name, stockid, 0, f, userdata, ap); |
|
63 va_end(ap); |
|
64 } |
|
65 |
|
66 void ui_toolitem_stgri(char *name, char *stockid, ui_callback f, void *userdata, ...) { |
|
67 va_list ap; |
|
68 va_start(ap, userdata); |
|
69 ui_toolitem_vstgr(name, stockid, 1, f, userdata, ap); |
|
70 va_end(ap); |
|
71 } |
|
72 |
|
73 void ui_toolitem_img(char *name, char *label, char *img, ui_callback f, void *udata) { |
|
74 UiToolItem *item = malloc(sizeof(UiToolItem)); |
|
75 item->item.add_to = (ui_toolbar_add_f)add_toolitem_widget; |
|
76 item->label = label; |
|
77 item->image = img; |
|
78 item->callback = f; |
|
79 item->userdata = udata; |
|
80 item->isimportant = 0; |
|
81 item->groups = NULL; |
|
82 |
|
83 ucx_map_cstr_put(toolbar_items, name, item); |
|
84 } |
|
85 |
|
86 void ui_toolitem_vstgr( |
|
87 char *name, |
|
88 char *stockid, |
|
89 int isimportant, |
|
90 ui_callback f, |
|
91 void *userdata, |
|
92 va_list ap) |
|
93 { |
|
94 UiStToolItem *item = malloc(sizeof(UiStToolItem)); |
|
95 item->item.add_to = (ui_toolbar_add_f)add_toolitem_st_widget; |
|
96 item->stockid = stockid; |
|
97 item->callback = f; |
|
98 item->userdata = userdata; |
|
99 item->groups = NULL; |
|
100 item->isimportant = isimportant; |
|
101 |
|
102 // add groups |
|
103 int group; |
|
104 while((group = va_arg(ap, int)) != -1) { |
|
105 item->groups = ucx_list_append(item->groups, (void*)(intptr_t)group); |
|
106 } |
|
107 |
|
108 ucx_map_cstr_put(toolbar_items, name, item); |
|
109 } |
|
110 |
|
111 void ui_toolitem_toggle_st(char *name, char *stockid, ui_callback f, void *udata) { |
|
112 ui_toolitem_toggle_stgr(name, stockid, f, udata, -1); |
|
113 } |
|
114 |
|
115 void ui_toolitem_toggle_stgr(char *name, char *stockid, ui_callback f, void *udata, ...) { |
|
116 UiStToolItem *item = malloc(sizeof(UiStToolItem)); |
|
117 item->item.add_to = (ui_toolbar_add_f)add_toolitem_toggle_st_widget; |
|
118 item->stockid = stockid; |
|
119 item->callback = f; |
|
120 item->userdata = udata; |
|
121 item->groups = NULL; |
|
122 item->isimportant = 0; |
|
123 |
|
124 // add groups |
|
125 va_list ap; |
|
126 va_start(ap, udata); |
|
127 int group; |
|
128 while((group = va_arg(ap, int)) != -1) { |
|
129 item->groups = ucx_list_append(item->groups, (void*)(intptr_t)group); |
|
130 } |
|
131 va_end(ap); |
|
132 |
|
133 ucx_map_cstr_put(toolbar_items, name, item); |
|
134 } |
|
135 |
|
136 void ui_toolitem_toggle_imggr(char *name, char *label, char *img, ui_callback f, void *udata, ...) { |
|
137 UiToolItem *item = malloc(sizeof(UiToolItem)); |
|
138 item->item.add_to = (ui_toolbar_add_f)add_toolitem_toggle_widget; |
|
139 item->label = label; |
|
140 item->image = img; |
|
141 item->callback = f; |
|
142 item->userdata = udata; |
|
143 item->groups = NULL; |
|
144 item->isimportant = 0; |
|
145 |
|
146 // add groups |
|
147 va_list ap; |
|
148 va_start(ap, udata); |
|
149 int group; |
|
150 while((group = va_arg(ap, int)) != -1) { |
|
151 item->groups = ucx_list_append(item->groups, (void*)(intptr_t)group); |
|
152 } |
|
153 va_end(ap); |
|
154 |
|
155 ucx_map_cstr_put(toolbar_items, name, item); |
|
156 } |
|
157 |
|
158 |
|
159 void ui_toolbar_combobox( |
|
160 char *name, |
|
161 UiList *list, |
|
162 ui_getvaluefunc getvalue, |
|
163 ui_callback f, |
|
164 void *udata) |
|
165 { |
|
166 UiToolbarComboBox *cb = malloc(sizeof(UiToolbarComboBox)); |
|
167 cb->item.add_to = (ui_toolbar_add_f)add_toolbar_combobox; |
|
168 UiVar *var = malloc(sizeof(UiVar)); |
|
169 var->value = list; |
|
170 var->type = UI_VAR_SPECIAL; |
|
171 var->from = NULL; |
|
172 var->from_ctx = NULL; |
|
173 cb->var = var; |
|
174 cb->getvalue = getvalue; |
|
175 cb->callback = f; |
|
176 cb->userdata = udata; |
|
177 |
|
178 ucx_map_cstr_put(toolbar_items, name, cb); |
|
179 } |
|
180 |
|
181 void ui_toolbar_combobox_str( |
|
182 char *name, |
|
183 UiList *list, |
|
184 ui_callback f, |
|
185 void *udata) |
|
186 { |
|
187 ui_toolbar_combobox(name, list, ui_strmodel_getvalue, f, udata); |
|
188 } |
|
189 |
|
190 void ui_toolbar_combobox_nv( |
|
191 char *name, |
|
192 char *listname, |
|
193 ui_getvaluefunc getvalue, |
|
194 ui_callback f, |
|
195 void *udata) |
|
196 { |
|
197 UiToolbarComboBoxNV *cb = malloc(sizeof(UiToolbarComboBoxNV)); |
|
198 cb->item.add_to = (ui_toolbar_add_f)add_toolbar_combobox_nv; |
|
199 cb->listname = listname; |
|
200 cb->getvalue = getvalue; |
|
201 cb->callback = f; |
|
202 cb->userdata = udata; |
|
203 |
|
204 ucx_map_cstr_put(toolbar_items, name, cb); |
|
205 } |
|
206 |
|
207 |
|
208 void ui_toolbar_add_default(char *name) { |
|
209 char *s = strdup(name); |
|
210 defaults = ucx_list_append(defaults, s); |
|
211 } |
|
212 |
|
213 GtkWidget* ui_create_toolbar(UiObject *obj) { |
|
214 if(!defaults) { |
|
215 return NULL; |
|
216 } |
|
217 |
|
218 GtkWidget *toolbar = gtk_toolbar_new(); |
|
219 #ifdef UI_GTK3 |
|
220 gtk_style_context_add_class( |
|
221 gtk_widget_get_style_context(toolbar), |
|
222 GTK_STYLE_CLASS_PRIMARY_TOOLBAR); |
|
223 #endif |
|
224 |
|
225 GtkToolbar *tb = GTK_TOOLBAR(toolbar); |
|
226 UCX_FOREACH(elm, defaults) { |
|
227 UiToolItemI *item = ucx_map_cstr_get(toolbar_items, elm->data); |
|
228 if(item) { |
|
229 item->add_to(tb, item, obj); |
|
230 } else if(!strcmp(elm->data, "@separator")) { |
|
231 gtk_toolbar_insert(tb, gtk_separator_tool_item_new(), -1); |
|
232 } else { |
|
233 fprintf(stderr, "UI Error: Unknown toolbar item: %s\n", elm->data); |
|
234 } |
|
235 } |
|
236 |
|
237 return toolbar; |
|
238 } |
|
239 |
|
240 void add_toolitem_widget(GtkToolbar *tb, UiToolItem *item, UiObject *obj) { |
|
241 GtkToolItem *button = gtk_tool_button_new(NULL, item->label); |
|
242 gtk_tool_item_set_homogeneous(button, FALSE); |
|
243 if(item->image) { |
|
244 GdkPixbuf *pixbuf = ui_get_image(item->image); |
|
245 GtkWidget *image = gtk_image_new_from_pixbuf(pixbuf); |
|
246 gtk_tool_button_set_icon_widget(GTK_TOOL_BUTTON(button), image); |
|
247 } else { |
|
248 gtk_tool_item_set_is_important(button, TRUE); |
|
249 } |
|
250 |
|
251 if(item->callback) { |
|
252 UiEventData *event = ucx_mempool_malloc( |
|
253 obj->ctx->mempool, |
|
254 sizeof(UiEventData)); |
|
255 event->obj = obj; |
|
256 event->userdata = item->userdata; |
|
257 event->callback = item->callback; |
|
258 |
|
259 g_signal_connect( |
|
260 button, |
|
261 "clicked", |
|
262 G_CALLBACK(ui_button_clicked), |
|
263 event); |
|
264 } |
|
265 |
|
266 gtk_toolbar_insert(tb, button, -1); |
|
267 |
|
268 if(item->groups) { |
|
269 uic_add_group_widget(obj->ctx, button, item->groups); |
|
270 } |
|
271 } |
|
272 |
|
273 void add_toolitem_st_widget(GtkToolbar *tb, UiStToolItem *item, UiObject *obj) { |
|
274 GtkToolItem *button = gtk_tool_button_new_from_stock(item->stockid); |
|
275 gtk_tool_item_set_homogeneous(button, FALSE); |
|
276 if(item->isimportant) { |
|
277 gtk_tool_item_set_is_important(button, TRUE); |
|
278 } |
|
279 |
|
280 if(item->callback) { |
|
281 UiEventData *event = ucx_mempool_malloc( |
|
282 obj->ctx->mempool, |
|
283 sizeof(UiEventData)); |
|
284 event->obj = obj; |
|
285 event->userdata = item->userdata; |
|
286 event->callback = item->callback; |
|
287 |
|
288 g_signal_connect( |
|
289 button, |
|
290 "clicked", |
|
291 G_CALLBACK(ui_button_clicked), |
|
292 event); |
|
293 } |
|
294 |
|
295 gtk_toolbar_insert(tb, button, -1); |
|
296 |
|
297 if(item->groups) { |
|
298 uic_add_group_widget(obj->ctx, button, item->groups); |
|
299 } |
|
300 } |
|
301 |
|
302 void add_toolitem_toggle_widget(GtkToolbar *tb, UiToolItem *item, UiObject *obj) { |
|
303 GtkToolItem *button = gtk_toggle_tool_button_new(); |
|
304 gtk_tool_item_set_homogeneous(button, FALSE); |
|
305 if(item->label) { |
|
306 gtk_tool_button_set_label(GTK_TOOL_BUTTON(button), item->label); |
|
307 } |
|
308 if(item->image) { |
|
309 GdkPixbuf *pixbuf = ui_get_image(item->image); |
|
310 GtkWidget *image = gtk_image_new_from_pixbuf(pixbuf); |
|
311 gtk_tool_button_set_icon_widget(GTK_TOOL_BUTTON(button), image); |
|
312 } |
|
313 |
|
314 if(item->callback) { |
|
315 UiEventData *event = ucx_mempool_malloc( |
|
316 obj->ctx->mempool, |
|
317 sizeof(UiEventData)); |
|
318 event->obj = obj; |
|
319 event->userdata = item->userdata; |
|
320 event->callback = item->callback; |
|
321 |
|
322 g_signal_connect( |
|
323 button, |
|
324 "toggled", |
|
325 G_CALLBACK(ui_button_toggled), |
|
326 event); |
|
327 } |
|
328 |
|
329 gtk_toolbar_insert(tb, button, -1); |
|
330 |
|
331 if(item->groups) { |
|
332 uic_add_group_widget(obj->ctx, button, item->groups); |
|
333 } |
|
334 } |
|
335 |
|
336 void add_toolitem_toggle_st_widget(GtkToolbar *tb, UiStToolItem *item, UiObject *obj) { |
|
337 GtkToolItem *button = gtk_toggle_tool_button_new_from_stock(item->stockid); |
|
338 gtk_tool_item_set_homogeneous(button, FALSE); |
|
339 |
|
340 if(item->callback) { |
|
341 UiEventData *event = ucx_mempool_malloc( |
|
342 obj->ctx->mempool, |
|
343 sizeof(UiEventData)); |
|
344 event->obj = obj; |
|
345 event->userdata = item->userdata; |
|
346 event->callback = item->callback; |
|
347 |
|
348 g_signal_connect( |
|
349 button, |
|
350 "toggled", |
|
351 G_CALLBACK(ui_button_toggled), |
|
352 event); |
|
353 } |
|
354 |
|
355 gtk_toolbar_insert(tb, button, -1); |
|
356 |
|
357 if(item->groups) { |
|
358 uic_add_group_widget(obj->ctx, button, item->groups); |
|
359 } |
|
360 } |
|
361 |
|
362 void add_toolbar_combobox(GtkToolbar *tb, UiToolbarComboBox *cb, UiObject *obj) { |
|
363 UiModel *modelinfo = ui_model(obj->ctx, UI_STRING, "", -1); |
|
364 modelinfo->getvalue = cb->getvalue; |
|
365 UiListModel *model = ui_list_model_new(obj, cb->var, modelinfo); |
|
366 |
|
367 GtkWidget *combobox = ui_create_combobox(obj, model, cb->callback, cb->userdata); |
|
368 GtkToolItem *item = gtk_tool_item_new(); |
|
369 gtk_container_add(GTK_CONTAINER(item), combobox); |
|
370 gtk_toolbar_insert(tb, item, -1); |
|
371 } |
|
372 |
|
373 void add_toolbar_combobox_nv(GtkToolbar *tb, UiToolbarComboBoxNV *cb, UiObject *obj) { |
|
374 UiVar *var = uic_create_var(obj->ctx, cb->listname, UI_VAR_LIST); |
|
375 if(var) { |
|
376 UiModel *modelinfo = ui_model(obj->ctx, UI_STRING, "", -1); |
|
377 modelinfo->getvalue = cb->getvalue; |
|
378 UiListModel *model = ui_list_model_new(obj, var, modelinfo); |
|
379 |
|
380 GtkWidget *combobox = ui_create_combobox(obj, model, cb->callback, cb->userdata); |
|
381 GtkToolItem *item = gtk_tool_item_new(); |
|
382 gtk_container_add(GTK_CONTAINER(item), combobox); |
|
383 gtk_toolbar_insert(tb, item, -1); |
|
384 } |
|
385 } |
|
386 |