Tue, 01 Apr 2014 13:47:29 +0200
added menu item lists (Motif)
0 | 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> | |
16
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
31 | #include <inttypes.h> |
0 | 32 | |
33 | #include "menu.h" | |
34 | #include "toolkit.h" | |
35 | #include "../common/context.h" | |
36 | #include "../ui/window.h" | |
37 | ||
16
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
38 | static UcxList *menus; |
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
39 | static UcxList *current; |
0 | 40 | |
41 | void ui_menu(char *label) { | |
42 | // free current menu hierarchy | |
43 | ucx_list_free(current); | |
44 | ||
45 | // create menu | |
46 | UiMenu *menu = malloc(sizeof(UiMenu)); | |
47 | menu->item.add_to = (ui_menu_add_f)add_menu_widget; | |
48 | ||
49 | menu->label = label; | |
50 | menu->items = NULL; | |
51 | menu->parent = NULL; | |
52 | ||
53 | current = ucx_list_prepend(NULL, menu); | |
54 | menus = ucx_list_append(menus, menu); | |
55 | ||
56 | } | |
57 | ||
58 | void ui_submenu(char *label) { | |
59 | UiMenu *menu = malloc(sizeof(UiMenu)); | |
60 | menu->item.add_to = (ui_menu_add_f)add_menu_widget; | |
61 | ||
62 | menu->label = label; | |
63 | menu->items = NULL; | |
64 | menu->parent = NULL; | |
65 | ||
66 | // add submenu to current menu | |
67 | UiMenu *cm = current->data; | |
68 | cm->items = ucx_list_append(cm->items, menu); | |
69 | ||
70 | // set the submenu to current menu | |
71 | current = ucx_list_prepend(current, menu); | |
72 | } | |
73 | ||
74 | void ui_submenu_end() { | |
75 | if(ucx_list_size(current) < 2) { | |
76 | return; | |
77 | } | |
78 | current = ucx_list_remove(current, current); | |
16
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
79 | //UcxList *c = current; |
0 | 80 | } |
81 | ||
82 | void ui_menuitem(char *label, ui_callback f, void *userdata) { | |
83 | if(!current) { | |
84 | return; | |
85 | } | |
86 | ||
87 | UiMenuItem *item = malloc(sizeof(UiMenuItem)); | |
88 | item->item.add_to = (ui_menu_add_f)add_menuitem_widget; | |
89 | ||
90 | item->label = label; | |
91 | item->userdata = userdata; | |
92 | item->callback = f; | |
93 | ||
94 | UiMenu *cm = current->data; | |
95 | cm->items = ucx_list_append(cm->items, item); | |
96 | } | |
97 | ||
98 | void ui_menuseparator() { | |
99 | if(!current) { | |
100 | return; | |
101 | } | |
102 | ||
103 | UiMenuItemI *item = malloc(sizeof(UiMenuItemI)); | |
104 | item->add_to = (ui_menu_add_f)add_menuseparator_widget; | |
105 | ||
106 | UiMenu *cm = current->data; | |
107 | cm->items = ucx_list_append(cm->items, item); | |
108 | } | |
109 | ||
110 | void ui_checkitem(char *label, ui_callback f, void *userdata) { | |
111 | if(!current) { | |
112 | return; | |
113 | } | |
114 | ||
115 | UiCheckItem *item = malloc(sizeof(UiCheckItem)); | |
116 | item->item.add_to = (ui_menu_add_f)add_checkitem_widget; | |
117 | item->label = label; | |
118 | item->callback = f; | |
119 | item->userdata = userdata; | |
120 | ||
121 | UiMenu *cm = current->data; | |
122 | cm->items = ucx_list_append(cm->items, item); | |
123 | } | |
124 | ||
125 | void ui_checkitem_nv(char *label, char *vname) { | |
126 | if(!current) { | |
127 | return; | |
128 | } | |
129 | ||
130 | UiCheckItemNV *item = malloc(sizeof(UiCheckItemNV)); | |
131 | item->item.add_to = (ui_menu_add_f)add_checkitemnv_widget; | |
132 | item->varname = vname; | |
133 | item->label = label; | |
134 | ||
135 | UiMenu *cm = current->data; | |
136 | cm->items = ucx_list_append(cm->items, item); | |
137 | } | |
138 | ||
16
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
139 | void ui_menuitem_list(UiList *items, ui_callback f, void *userdata) { |
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
140 | if(!current) { |
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
141 | return; |
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
142 | } |
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
143 | |
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
144 | UiMenuItemList *item = malloc(sizeof(UiMenuItemList)); |
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
145 | item->item.add_to = (ui_menu_add_f)add_menuitem_list_widget; |
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
146 | item->callback = f; |
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
147 | item->userdata = userdata; |
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
148 | item->list = items; |
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
149 | |
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
150 | UiMenu *cm = current->data; |
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
151 | cm->items = ucx_list_append(cm->items, item); |
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
152 | } |
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
153 | |
0 | 154 | // private menu functions |
155 | GtkWidget *ui_create_menubar(UiObject *obj) { | |
156 | if(menus == NULL) { | |
157 | return NULL; | |
158 | } | |
159 | ||
160 | GtkWidget *mb = gtk_menu_bar_new(); | |
161 | ||
162 | UcxList *ls = menus; | |
163 | while(ls) { | |
164 | UiMenu *menu = ls->data; | |
16
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
165 | menu->item.add_to(mb, 0, &menu->item, obj); |
0 | 166 | |
167 | ls = ls->next; | |
168 | } | |
169 | ||
170 | return mb; | |
171 | } | |
172 | ||
16
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
173 | void add_menu_widget(GtkWidget *parent, int i, UiMenuItemI *item, UiObject *obj) { |
0 | 174 | UiMenu *menu = (UiMenu*)item; |
175 | ||
176 | GtkWidget *menu_widget = gtk_menu_new(); | |
177 | GtkWidget *menu_item = gtk_menu_item_new_with_label(menu->label); | |
178 | gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu_item), menu_widget); | |
179 | ||
180 | UcxList *ls = menu->items; | |
16
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
181 | int index = 0; |
0 | 182 | while(ls) { |
183 | UiMenuItemI *i = ls->data; | |
16
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
184 | i->add_to(menu_widget, index, i, obj); |
0 | 185 | |
186 | ls = ls->next; | |
16
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
187 | index++; |
0 | 188 | } |
189 | ||
190 | gtk_menu_shell_append(GTK_MENU_SHELL(parent), menu_item); | |
191 | } | |
192 | ||
16
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
193 | void add_menuitem_widget(GtkWidget *parent, int index, UiMenuItemI *item, UiObject *obj) { |
0 | 194 | UiMenuItem *i = (UiMenuItem*)item; |
195 | ||
196 | //GtkWidget *widget = gtk_menu_item_new_with_label(i->title); | |
197 | GtkWidget *widget = gtk_menu_item_new_with_mnemonic(i->label); | |
198 | ||
199 | if(i->callback != NULL) { | |
200 | UiEventData *event = malloc(sizeof(UiEventData)); | |
201 | event->obj = obj; | |
202 | event->user_data = i->userdata; | |
203 | event->callback = i->callback; | |
16
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
204 | event->value = 0; |
0 | 205 | |
206 | g_signal_connect( | |
207 | widget, | |
208 | "activate", | |
209 | G_CALLBACK(ui_menu_event_wrapper), | |
210 | event); | |
211 | } | |
212 | ||
213 | gtk_menu_shell_append(GTK_MENU_SHELL(parent), widget); | |
214 | } | |
215 | ||
216 | void add_menuitem_st_widget( | |
217 | GtkWidget *parent, | |
16
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
218 | int index, |
0 | 219 | UiMenuItemI *item, |
220 | UiObject *obj) | |
221 | { | |
222 | UiStMenuItem *i = (UiStMenuItem*)item; | |
223 | ||
224 | GtkWidget *widget = gtk_image_menu_item_new_from_stock(i->stockid, NULL); | |
225 | ||
226 | if(i->callback != NULL) { | |
227 | UiEventData *event = malloc(sizeof(UiEventData)); | |
228 | event->obj = obj; | |
229 | event->user_data = i->userdata; | |
230 | event->callback = i->callback; | |
16
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
231 | event->value = 0; |
0 | 232 | |
233 | g_signal_connect( | |
234 | widget, | |
235 | "activate", | |
236 | G_CALLBACK(ui_menu_event_wrapper), | |
237 | event); | |
238 | } | |
239 | ||
240 | gtk_menu_shell_append(GTK_MENU_SHELL(parent), widget); | |
241 | } | |
242 | ||
243 | void add_menuseparator_widget( | |
244 | GtkWidget *parent, | |
16
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
245 | int index, |
0 | 246 | UiMenuItemI *item, |
247 | UiObject *obj) | |
248 | { | |
249 | gtk_menu_shell_append( | |
250 | GTK_MENU_SHELL(parent), | |
251 | gtk_separator_menu_item_new()); | |
252 | } | |
253 | ||
16
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
254 | void add_checkitem_widget(GtkWidget *p, int index, UiMenuItemI *item, UiObject *obj) { |
0 | 255 | UiCheckItem *ci = (UiCheckItem*)item; |
256 | GtkWidget *widget = gtk_check_menu_item_new_with_mnemonic(ci->label); | |
257 | gtk_menu_shell_append(GTK_MENU_SHELL(p), widget); | |
258 | ||
259 | if(ci->callback) { | |
260 | UiEventData *event = malloc(sizeof(UiEventData)); | |
261 | event->obj = obj; | |
262 | event->user_data = ci->userdata; | |
263 | event->callback = ci->callback; | |
16
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
264 | event->value = 0; |
0 | 265 | |
266 | g_signal_connect( | |
267 | widget, | |
268 | "toggled", | |
269 | G_CALLBACK(ui_menu_event_toggled), | |
270 | event); | |
271 | } | |
272 | } | |
273 | ||
16
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
274 | void add_checkitemnv_widget(GtkWidget *p, int index, UiMenuItemI *item, UiObject *obj) { |
0 | 275 | UiCheckItemNV *ci = (UiCheckItemNV*)item; |
276 | GtkWidget *widget = gtk_check_menu_item_new_with_mnemonic(ci->label); | |
277 | gtk_menu_shell_append(GTK_MENU_SHELL(p), widget); | |
278 | ||
2
eeb50c534497
added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
279 | UiVar *var = uic_connect_var(obj->ctx, ci->varname, UI_VAR_INTEGER); |
eeb50c534497
added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
280 | if(var) { |
0 | 281 | UiInteger *value = var->value; |
282 | value->obj = widget; | |
283 | value->get = ui_checkitem_get; | |
284 | value->set = ui_checkitem_set; | |
285 | value = 0; | |
286 | } else { | |
2
eeb50c534497
added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
287 | // TODO: error |
0 | 288 | } |
289 | } | |
290 | ||
16
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
291 | void add_menuitem_list_widget(GtkWidget *p, int index, UiMenuItemI *item, UiObject *obj) { |
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
292 | UiMenuItemList *il = (UiMenuItemList*)item; |
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
293 | UcxMempool *mp = obj->ctx->mempool; |
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
294 | |
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
295 | UiActiveMenuItemList *ls = ucx_mempool_malloc( |
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
296 | mp, |
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
297 | sizeof(UiActiveMenuItemList)); |
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
298 | |
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
299 | ls->object = obj; |
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
300 | ls->menu = GTK_MENU_SHELL(p); |
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
301 | ls->index = index; |
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
302 | ls->oldcount = 0; |
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
303 | ls->list = il->list; |
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
304 | ls->callback = il->callback; |
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
305 | ls->userdata = il->userdata; |
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
306 | |
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
307 | ls->list->observers = ui_add_observer( |
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
308 | ls->list->observers, |
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
309 | (ui_callback)ui_update_menuitem_list, |
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
310 | ls); |
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
311 | |
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
312 | ui_update_menuitem_list(NULL, ls); |
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
313 | } |
0 | 314 | |
315 | ||
16
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
316 | void ui_update_menuitem_list(UiEvent *event, UiActiveMenuItemList *list) { |
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
317 | // remove old items |
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
318 | if(list->oldcount > 0) { |
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
319 | int i = 0; |
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
320 | GList *mi = gtk_container_get_children(GTK_CONTAINER(list->menu)); |
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
321 | while(mi) { |
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
322 | if(i >= list->index && i < list->index + list->oldcount) { |
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
323 | gtk_container_remove(GTK_CONTAINER(list->menu), mi->data); |
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
324 | } |
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
325 | mi = mi->next; |
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
326 | i++; |
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
327 | } |
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
328 | } |
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
329 | |
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
330 | char *str = ui_list_first(list->list); |
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
331 | if(str) { |
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
332 | GtkWidget *widget = gtk_separator_menu_item_new(); |
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
333 | gtk_menu_shell_insert(list->menu, widget, list->index); |
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
334 | gtk_widget_show(widget); |
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
335 | } |
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
336 | int i = 1; |
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
337 | while(str) { |
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
338 | GtkWidget *widget = gtk_menu_item_new_with_label(str); |
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
339 | gtk_menu_shell_insert(list->menu, widget, list->index + i); |
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
340 | gtk_widget_show(widget); |
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
341 | |
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
342 | if(list->callback) { |
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
343 | // TODO: use mempool |
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
344 | UiEventData *event = malloc(sizeof(UiEventData)); |
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
345 | event->obj = list->object; |
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
346 | event->user_data = list->userdata; |
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
347 | event->callback = list->callback; |
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
348 | event->value = i - 1; |
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
349 | |
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
350 | g_signal_connect( |
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
351 | widget, |
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
352 | "activate", |
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
353 | G_CALLBACK(ui_menu_event_wrapper), |
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
354 | event); |
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
355 | } |
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
356 | |
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
357 | str = ui_list_next(list->list); |
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
358 | i++; |
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
359 | } |
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
360 | |
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
361 | list->oldcount = i; |
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
362 | } |
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
363 | |
0 | 364 | void ui_menu_event_wrapper(GtkMenuItem *item, UiEventData *event) { |
365 | UiEvent evt; | |
366 | evt.obj = event->obj; | |
367 | evt.window = event->obj->window; | |
2
eeb50c534497
added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
368 | evt.document = event->obj->document; |
16
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
369 | evt.eventdata = NULL; |
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
370 | evt.intval = event->value; |
0 | 371 | event->callback(&evt, event->user_data); |
372 | } | |
373 | ||
374 | void ui_menu_event_toggled(GtkCheckMenuItem *ci, UiEventData *event) { | |
375 | UiEvent evt; | |
376 | evt.obj = event->obj; | |
377 | evt.window = event->obj->window; | |
2
eeb50c534497
added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
378 | evt.document = event->obj->document; |
16
a499c8a72c15
added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
379 | evt.eventdata = NULL; |
0 | 380 | evt.intval = gtk_check_menu_item_get_active(ci); |
381 | event->callback(&evt, event->user_data); | |
382 | } | |
383 | ||
384 | int ui_checkitem_get(UiInteger *i) { | |
385 | int state = gtk_check_menu_item_get_active(i->obj); | |
386 | i->value = state; | |
387 | return state; | |
388 | } | |
389 | ||
390 | void ui_checkitem_set(UiInteger *i, int value) { | |
391 | i->value = value; | |
392 | gtk_check_menu_item_set_active(i->obj, value); | |
393 | } |