ui/gtk/menu.c

changeset 0
2483f517c562
child 29
3fc287f06305
equal deleted inserted replaced
-1:000000000000 0:2483f517c562
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 <inttypes.h>
32 #include <stdarg.h>
33
34 #include "menu.h"
35 #include "toolkit.h"
36 #include "../common/context.h"
37 #include "../common/menu.h"
38 #include "../ui/properties.h"
39 #include "../ui/window.h"
40 #include "container.h"
41
42 #include <cx/linked_list.h>
43 #include <cx/array_list.h>
44
45
46 static ui_menu_add_f createMenuItem[] = {
47 /* UI_MENU */ add_menu_widget,
48 /* UI_MENU_SUBMENU */ add_menu_widget,
49 /* UI_MENU_ITEM */ add_menuitem_widget,
50 /* UI_MENU_STOCK_ITEM */ add_menuitem_st_widget,
51 /* UI_MENU_CHECK_ITEM */ add_checkitem_widget,
52 /* UI_MENU_CHECK_ITEM_NV */ add_checkitemnv_widget,
53 /* UI_MENU_ITEM_LIST */ add_menuitem_list_widget,
54 /* UI_MENU_ITEM_LIST_NV */ NULL, // TODO
55 /* UI_MENU_SEPARATOR */ add_menuseparator_widget
56 };
57
58 // private menu functions
59 GtkWidget *ui_create_menubar(UiObject *obj) {
60 UiMenu *menus_begin = uic_get_menu_list();
61 if(menus_begin == NULL) {
62 return NULL;
63 }
64
65 GtkWidget *mb = gtk_menu_bar_new();
66
67 UiMenu *ls = menus_begin;
68 while(ls) {
69 UiMenu *menu = ls;
70 add_menu_widget(mb, 0, &menu->item, obj);
71
72 ls = (UiMenu*)ls->item.next;
73 }
74
75 return mb;
76 }
77
78 void add_menu_widget(GtkWidget *parent, int i, UiMenuItemI *item, UiObject *obj) {
79 UiMenu *menu = (UiMenu*)item;
80
81 GtkWidget *menu_widget = gtk_menu_new();
82 GtkWidget *menu_item = gtk_menu_item_new_with_mnemonic(menu->label);
83 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu_item), menu_widget);
84
85 UiMenuItemI *it = menu->items_begin;
86 int index = 0;
87 while(it) {
88 createMenuItem[it->type](menu_widget, index, it, obj);
89
90 it = it->next;
91 index++;
92 }
93
94 gtk_menu_shell_append(GTK_MENU_SHELL(parent), menu_item);
95 }
96
97 void add_menuitem_widget(GtkWidget *parent, int index, UiMenuItemI *item, UiObject *obj) {
98 UiMenuItem *i = (UiMenuItem*)item;
99
100 //GtkWidget *widget = gtk_menu_item_new_with_label(i->title);
101 GtkWidget *widget = gtk_menu_item_new_with_mnemonic(i->label);
102
103 if(i->callback != NULL) {
104 UiEventData *event = malloc(sizeof(UiEventData));
105 event->obj = obj;
106 event->userdata = i->userdata;
107 event->callback = i->callback;
108 event->value = 0;
109
110 g_signal_connect(
111 widget,
112 "activate",
113 G_CALLBACK(ui_menu_event_wrapper),
114 event);
115 g_signal_connect(
116 widget,
117 "destroy",
118 G_CALLBACK(ui_destroy_userdata),
119 event);
120 }
121
122 gtk_menu_shell_append(GTK_MENU_SHELL(parent), widget);
123
124 if(i->groups) {
125 uic_add_group_widget(obj->ctx, widget, (ui_enablefunc)ui_set_enabled, i->groups);
126 }
127 }
128
129 void add_menuitem_st_widget(
130 GtkWidget *parent,
131 int index,
132 UiMenuItemI *item,
133 UiObject *obj)
134 {
135 UiStMenuItem *i = (UiStMenuItem*)item;
136
137 GtkWidget *widget = gtk_image_menu_item_new_from_stock(i->stockid, obj->ctx->accel_group);
138
139 if(i->callback != NULL) {
140 UiEventData *event = malloc(sizeof(UiEventData));
141 event->obj = obj;
142 event->userdata = i->userdata;
143 event->callback = i->callback;
144 event->value = 0;
145
146 g_signal_connect(
147 widget,
148 "activate",
149 G_CALLBACK(ui_menu_event_wrapper),
150 event);
151 g_signal_connect(
152 widget,
153 "destroy",
154 G_CALLBACK(ui_destroy_userdata),
155 event);
156 }
157
158 gtk_menu_shell_append(GTK_MENU_SHELL(parent), widget);
159
160 if(i->groups) {
161 uic_add_group_widget(obj->ctx, widget, (ui_enablefunc)ui_set_enabled, i->groups);
162 }
163 }
164
165 void add_menuseparator_widget(
166 GtkWidget *parent,
167 int index,
168 UiMenuItemI *item,
169 UiObject *obj)
170 {
171 gtk_menu_shell_append(
172 GTK_MENU_SHELL(parent),
173 gtk_separator_menu_item_new());
174 }
175
176 void add_checkitem_widget(GtkWidget *p, int index, UiMenuItemI *item, UiObject *obj) {
177 UiCheckItem *ci = (UiCheckItem*)item;
178 GtkWidget *widget = gtk_check_menu_item_new_with_mnemonic(ci->label);
179 gtk_menu_shell_append(GTK_MENU_SHELL(p), widget);
180
181 if(ci->callback) {
182 UiEventData *event = malloc(sizeof(UiEventData));
183 event->obj = obj;
184 event->userdata = ci->userdata;
185 event->callback = ci->callback;
186 event->value = 0;
187
188 g_signal_connect(
189 widget,
190 "toggled",
191 G_CALLBACK(ui_menu_event_toggled),
192 event);
193 g_signal_connect(
194 widget,
195 "destroy",
196 G_CALLBACK(ui_destroy_userdata),
197 event);
198 }
199 }
200
201 void add_checkitemnv_widget(GtkWidget *p, int index, UiMenuItemI *item, UiObject *obj) {
202 UiCheckItemNV *ci = (UiCheckItemNV*)item;
203 GtkWidget *widget = gtk_check_menu_item_new_with_mnemonic(ci->label);
204 gtk_menu_shell_append(GTK_MENU_SHELL(p), widget);
205
206 UiVar *var = uic_create_var(obj->ctx, ci->varname, UI_VAR_INTEGER);
207 if(var) {
208 UiInteger *value = var->value;
209 value->obj = widget;
210 value->get = ui_checkitem_get;
211 value->set = ui_checkitem_set;
212 value = 0;
213 } else {
214 // TODO: error
215 }
216 }
217
218 void add_menuitem_list_widget(GtkWidget *p, int index, UiMenuItemI *item, UiObject *obj) {
219 UiMenuItemList *il = (UiMenuItemList*)item;
220 const CxAllocator *a = obj->ctx->allocator;
221
222 UiActiveMenuItemList *ls = cxMalloc(
223 a,
224 sizeof(UiActiveMenuItemList));
225
226 ls->object = obj;
227 ls->menu = GTK_MENU_SHELL(p);
228 ls->index = index;
229 ls->oldcount = 0;
230 ls->list = il->list;
231 ls->callback = il->callback;
232 ls->userdata = il->userdata;
233
234 ls->list->observers = ui_add_observer(
235 ls->list->observers,
236 (ui_callback)ui_update_menuitem_list,
237 ls);
238
239 ui_update_menuitem_list(NULL, ls);
240 }
241
242
243 void ui_update_menuitem_list(UiEvent *event, UiActiveMenuItemList *list) {
244 // remove old items
245 if(list->oldcount > 0) {
246 int i = 0;
247 GList *mi = gtk_container_get_children(GTK_CONTAINER(list->menu));
248 while(mi) {
249 if(i >= list->index && i < list->index + list->oldcount) {
250 //gtk_container_remove(GTK_CONTAINER(list->menu), mi->data);
251 gtk_widget_destroy(mi->data);
252 }
253 mi = mi->next;
254 i++;
255 }
256 }
257
258 char *str = ui_list_first(list->list);
259 if(str) {
260 GtkWidget *widget = gtk_separator_menu_item_new();
261 gtk_menu_shell_insert(list->menu, widget, list->index);
262 gtk_widget_show(widget);
263 }
264 int i = 1;
265 while(str) {
266 GtkWidget *widget = gtk_menu_item_new_with_label(str);
267 gtk_menu_shell_insert(list->menu, widget, list->index + i);
268 gtk_widget_show(widget);
269
270 if(list->callback) {
271 // TODO: use mempool
272 UiEventData *event = malloc(sizeof(UiEventData));
273 event->obj = list->object;
274 event->userdata = list->userdata;
275 event->callback = list->callback;
276 event->value = i - 1;
277
278 g_signal_connect(
279 widget,
280 "activate",
281 G_CALLBACK(ui_menu_event_wrapper),
282 event);
283 g_signal_connect(
284 widget,
285 "destroy",
286 G_CALLBACK(ui_destroy_userdata),
287 event);
288 }
289
290 str = ui_list_next(list->list);
291 i++;
292 }
293
294 list->oldcount = i;
295 }
296
297 void ui_menu_event_wrapper(GtkMenuItem *item, UiEventData *event) {
298 UiEvent evt;
299 evt.obj = event->obj;
300 evt.window = event->obj->window;
301 evt.document = event->obj->ctx->document;
302 evt.eventdata = NULL;
303 evt.intval = event->value;
304 event->callback(&evt, event->userdata);
305 }
306
307 void ui_menu_event_toggled(GtkCheckMenuItem *ci, UiEventData *event) {
308 UiEvent evt;
309 evt.obj = event->obj;
310 evt.window = event->obj->window;
311 evt.document = event->obj->ctx->document;
312 evt.eventdata = NULL;
313 evt.intval = gtk_check_menu_item_get_active(ci);
314 event->callback(&evt, event->userdata);
315 }
316
317 int64_t ui_checkitem_get(UiInteger *i) {
318 int state = gtk_check_menu_item_get_active(i->obj);
319 i->value = state;
320 return state;
321 }
322
323 void ui_checkitem_set(UiInteger *i, int64_t value) {
324 i->value = value;
325 gtk_check_menu_item_set_active(i->obj, value);
326 }
327
328
329 /*
330 * widget menu functions
331 */
332
333 static gboolean ui_button_press_event(GtkWidget *widget, GdkEvent *event, GtkMenu *menu) {
334 if(event->type == GDK_BUTTON_PRESS) {
335 GdkEventButton *e = (GdkEventButton*)event;
336 if(e->button == 3) {
337 gtk_widget_show_all(GTK_WIDGET(menu));
338 ui_contextmenu_popup(menu);
339 return TRUE;
340 }
341 }
342 return FALSE;
343 }
344
345 UIMENU ui_contextmenu(UiObject *obj) {
346 UiContainer *ct = uic_get_current_container(obj);
347 return ui_contextmenu_w(obj, ct->current);
348 }
349
350 UIMENU ui_contextmenu_w(UiObject *obj, UIWIDGET widget) {
351 UiContainer *ct = uic_get_current_container(obj);
352
353 GtkMenu *menu = GTK_MENU(gtk_menu_new());
354 g_signal_connect(widget, "button-press-event", (GCallback) ui_button_press_event, menu);
355
356 ct->menu = menu;
357 return menu;
358 }
359
360 void ui_contextmenu_popup(UIMENU menu) {
361 #if GTK_MAJOR_VERSION >= 3 && GTK_MINOR_VERSION >= 16
362 gtk_menu_popup_at_pointer(menu, NULL);
363 #else
364 gtk_menu_popup(menu, NULL, NULL, 0, 0, 0, gtk_get_current_event_time());
365 #endif
366 }
367
368 void ui_widget_menuitem(UiObject *obj, char *label, ui_callback f, void *userdata) {
369 ui_widget_menuitem_gr(obj, label, f, userdata, -1);
370 }
371
372 void ui_widget_menuitem_gr(UiObject *obj, char *label, ui_callback f, void *userdata, ...) {
373 UiContainer *ct = uic_get_current_container(obj);
374 if(!ct->menu) {
375 return;
376 }
377
378 // add groups
379 CxList *groups = NULL;
380 va_list ap;
381 va_start(ap, userdata);
382 int group;
383 while((group = va_arg(ap, int)) != -1) {
384 if(!groups) {
385 groups = cxArrayListCreate(cxDefaultAllocator, NULL, sizeof(int), 16);
386 }
387 cxListAdd(groups, &group);
388 }
389 va_end(ap);
390
391 // create menuitem
392 GtkWidget *widget = gtk_menu_item_new_with_mnemonic(label);
393 gtk_widget_show(widget);
394
395 if(f) {
396 UiEventData *event = malloc(sizeof(UiEventData));
397 event->obj = obj;
398 event->userdata = userdata;
399 event->callback = f;
400 event->value = 0;
401
402 g_signal_connect(
403 widget,
404 "activate",
405 G_CALLBACK(ui_menu_event_wrapper),
406 event);
407 g_signal_connect(
408 widget,
409 "destroy",
410 G_CALLBACK(ui_destroy_userdata),
411 event);
412 }
413
414 gtk_menu_shell_append(GTK_MENU_SHELL(ct->menu), widget);
415
416 if(groups) {
417 uic_add_group_widget(obj->ctx, widget, (ui_enablefunc)ui_set_enabled, groups);
418 cxListDestroy(groups);
419 }
420 }
421
422 void ui_widget_menuitem_st(UiObject *obj, char *stockid, ui_callback f, void *userdata) {
423 ui_widget_menuitem_stgr(obj, stockid, f, userdata, -1);
424 }
425
426 void ui_widget_menuitem_stgr(UiObject *obj, char *stockid, ui_callback f, void *userdata, ...) {
427 UiContainer *ct = uic_get_current_container(obj);
428 if(!ct->menu) {
429 return;
430 }
431
432 // add groups
433 CxList *groups = NULL;
434 va_list ap;
435 va_start(ap, userdata);
436 int group;
437 while((group = va_arg(ap, int)) != -1) {
438 if(!groups) {
439 groups = cxArrayListCreate(cxDefaultAllocator, NULL, sizeof(int), 16);
440 }
441 cxListAdd(groups, &group);
442 }
443 va_end(ap);
444
445 // create menuitem
446 GtkWidget *widget = gtk_image_menu_item_new_from_stock(stockid, obj->ctx->accel_group);
447 gtk_widget_show(widget);
448
449 if(f) {
450 UiEventData *event = malloc(sizeof(UiEventData));
451 event->obj = obj;
452 event->userdata = userdata;
453 event->callback = f;
454 event->value = 0;
455
456 g_signal_connect(
457 widget,
458 "activate",
459 G_CALLBACK(ui_menu_event_wrapper),
460 event);
461 g_signal_connect(
462 widget,
463 "destroy",
464 G_CALLBACK(ui_destroy_userdata),
465 event);
466 }
467
468 gtk_menu_shell_append(GTK_MENU_SHELL(ct->menu), widget);
469
470 if(groups) {
471 uic_add_group_widget(obj->ctx, widget, (ui_enablefunc)ui_set_enabled, groups);
472 cxListDestroy(groups);
473 }
474 }

mercurial