29 #include <stdio.h> |
29 #include <stdio.h> |
30 #include <stdlib.h> |
30 #include <stdlib.h> |
31 #include <string.h> |
31 #include <string.h> |
32 |
32 |
33 #include "toolbar.h" |
33 #include "toolbar.h" |
|
34 #include "menu.h" |
34 #include "button.h" |
35 #include "button.h" |
35 #include "image.h" |
36 #include "icon.h" |
36 #include "tree.h" |
37 #include "list.h" |
37 #include <ucx/mempool.h> |
38 #include <cx/mempool.h> |
|
39 #include <cx/hash_map.h> |
|
40 #include <cx/linked_list.h> |
|
41 #include <cx/array_list.h> |
38 #include "../common/context.h" |
42 #include "../common/context.h" |
39 |
43 |
40 static UcxMap *toolbar_items; |
44 |
41 static UcxList *defaults; |
45 #if UI_GTK2 || UI_GTK3 |
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(const char *name, const char *label, const char *img, UiInteger *i) { |
|
112 UiToggleToolItem *item = malloc(sizeof(UiToggleToolItem)); |
|
113 item->item.add_to = (ui_toolbar_add_f)add_toolitem_toggle_widget; |
|
114 item->label = label; |
|
115 item->image = img; |
|
116 item->stockid = NULL; |
|
117 item->groups = NULL; |
|
118 item->isimportant = 0; |
|
119 item->value = i; |
|
120 item->var = NULL; |
|
121 |
|
122 ucx_map_cstr_put(toolbar_items, name, item); |
|
123 } |
|
124 |
|
125 void ui_toolitem_toggle_st(const char *name, const char *stockid, UiInteger *i) { |
|
126 UiToggleToolItem *item = malloc(sizeof(UiToggleToolItem)); |
|
127 item->item.add_to = (ui_toolbar_add_f)add_toolitem_toggle_widget; |
|
128 item->label = NULL; |
|
129 item->image = NULL; |
|
130 item->stockid = stockid; |
|
131 item->groups = NULL; |
|
132 item->isimportant = 0; |
|
133 item->value = i; |
|
134 item->var = NULL; |
|
135 |
|
136 ucx_map_cstr_put(toolbar_items, name, item); |
|
137 } |
|
138 |
|
139 void ui_toolitem_toggle_nv(const char *name, const char *label, const char *img, const char *intvar) { |
|
140 UiToggleToolItem *item = malloc(sizeof(UiToggleToolItem)); |
|
141 item->item.add_to = (ui_toolbar_add_f)add_toolitem_toggle_widget; |
|
142 item->label = label; |
|
143 item->image = img; |
|
144 item->stockid = NULL; |
|
145 item->groups = NULL; |
|
146 item->isimportant = 0; |
|
147 item->value = NULL; |
|
148 item->var = intvar; |
|
149 |
|
150 ucx_map_cstr_put(toolbar_items, name, item); |
|
151 } |
|
152 |
|
153 void ui_toolitem_toggle_stnv(const char *name, const char *stockid, const char *intvar) { |
|
154 UiToggleToolItem *item = malloc(sizeof(UiToggleToolItem)); |
|
155 item->item.add_to = (ui_toolbar_add_f)add_toolitem_toggle_widget; |
|
156 item->label = NULL; |
|
157 item->image = NULL; |
|
158 item->stockid = stockid; |
|
159 item->groups = NULL; |
|
160 item->isimportant = 0; |
|
161 item->value = NULL; |
|
162 item->var = intvar; |
|
163 |
|
164 ucx_map_cstr_put(toolbar_items, name, item); |
|
165 } |
|
166 |
|
167 |
|
168 void ui_toolbar_combobox( |
|
169 char *name, |
|
170 UiList *list, |
|
171 ui_getvaluefunc getvalue, |
|
172 ui_callback f, |
|
173 void *udata) |
|
174 { |
|
175 UiToolbarComboBox *cb = malloc(sizeof(UiToolbarComboBox)); |
|
176 cb->item.add_to = (ui_toolbar_add_f)add_toolbar_combobox; |
|
177 UiVar *var = malloc(sizeof(UiVar)); |
|
178 var->value = list; |
|
179 var->type = UI_VAR_SPECIAL; |
|
180 var->from = NULL; |
|
181 var->from_ctx = NULL; |
|
182 cb->var = var; |
|
183 cb->getvalue = getvalue; |
|
184 cb->callback = f; |
|
185 cb->userdata = udata; |
|
186 |
|
187 ucx_map_cstr_put(toolbar_items, name, cb); |
|
188 } |
|
189 |
|
190 void ui_toolbar_combobox_str( |
|
191 char *name, |
|
192 UiList *list, |
|
193 ui_callback f, |
|
194 void *udata) |
|
195 { |
|
196 ui_toolbar_combobox(name, list, ui_strmodel_getvalue, f, udata); |
|
197 } |
|
198 |
|
199 void ui_toolbar_combobox_nv( |
|
200 char *name, |
|
201 char *listname, |
|
202 ui_getvaluefunc getvalue, |
|
203 ui_callback f, |
|
204 void *udata) |
|
205 { |
|
206 UiToolbarComboBoxNV *cb = malloc(sizeof(UiToolbarComboBoxNV)); |
|
207 cb->item.add_to = (ui_toolbar_add_f)add_toolbar_combobox_nv; |
|
208 cb->listname = listname; |
|
209 cb->getvalue = getvalue; |
|
210 cb->callback = f; |
|
211 cb->userdata = udata; |
|
212 |
|
213 ucx_map_cstr_put(toolbar_items, name, cb); |
|
214 } |
|
215 |
|
216 |
|
217 void ui_toolbar_add_default(char *name) { |
|
218 char *s = strdup(name); |
|
219 defaults = ucx_list_append(defaults, s); |
|
220 } |
|
221 |
46 |
222 GtkWidget* ui_create_toolbar(UiObject *obj) { |
47 GtkWidget* ui_create_toolbar(UiObject *obj) { |
223 if(!defaults) { |
|
224 return NULL; |
|
225 } |
|
226 |
|
227 GtkWidget *toolbar = gtk_toolbar_new(); |
48 GtkWidget *toolbar = gtk_toolbar_new(); |
228 #ifdef UI_GTK3 |
49 #ifdef UI_GTK3 |
229 gtk_style_context_add_class( |
50 gtk_style_context_add_class( |
230 gtk_widget_get_style_context(toolbar), |
51 gtk_widget_get_style_context(toolbar), |
231 GTK_STYLE_CLASS_PRIMARY_TOOLBAR); |
52 GTK_STYLE_CLASS_PRIMARY_TOOLBAR); |
232 #endif |
53 #endif |
233 |
54 |
|
55 CxMap *items = uic_get_toolbar_items(); |
|
56 CxList *left_defaults = uic_get_toolbar_defaults(UI_TOOLBAR_LEFT); |
|
57 CxList *center_defaults = uic_get_toolbar_defaults(UI_TOOLBAR_CENTER); |
|
58 CxList *right_defaults = uic_get_toolbar_defaults(UI_TOOLBAR_RIGHT); |
|
59 |
|
60 ui_toolbar_add_items(obj, toolbar, items, left_defaults); |
|
61 ui_toolbar_add_items(obj, toolbar, items, center_defaults); |
|
62 ui_toolbar_add_items(obj, toolbar, items, right_defaults); |
|
63 |
|
64 /* |
234 GtkToolbar *tb = GTK_TOOLBAR(toolbar); |
65 GtkToolbar *tb = GTK_TOOLBAR(toolbar); |
235 UCX_FOREACH(elm, defaults) { |
66 CxIterator i = cxListIterator(defaults); |
236 UiToolItemI *item = ucx_map_cstr_get(toolbar_items, elm->data); |
67 cx_foreach(char *, def, i) { |
|
68 UiToolItemI *item = cxMapGet(toolbar_items, def); |
237 if(item) { |
69 if(item) { |
238 item->add_to(tb, item, obj); |
70 item->add_to(tb, item, obj); |
239 } else if(!strcmp(elm->data, "@separator")) { |
71 } else if(!strcmp(def, "@separator")) { |
240 gtk_toolbar_insert(tb, gtk_separator_tool_item_new(), -1); |
72 gtk_toolbar_insert(tb, gtk_separator_tool_item_new(), -1); |
241 } else { |
73 } else { |
242 fprintf(stderr, "UI Error: Unknown toolbar item: %s\n", elm->data); |
74 fprintf(stderr, "UI Error: Unknown toolbar item: %s\n", def); |
243 } |
75 } |
244 } |
76 } |
|
77 */ |
245 |
78 |
246 return toolbar; |
79 return toolbar; |
247 } |
80 } |
248 |
81 |
249 void add_toolitem_widget(GtkToolbar *tb, UiToolItem *item, UiObject *obj) { |
82 static void create_item(UiObject *obj, GtkWidget *toolbar, UiToolbarItemI *i) { |
250 GtkToolItem *button = gtk_tool_button_new(NULL, item->label); |
83 GtkToolbar *tb = GTK_TOOLBAR(toolbar); |
|
84 switch(i->type) { |
|
85 case UI_TOOLBAR_ITEM: { |
|
86 add_toolitem_widget(tb, (UiToolbarItem*)i, obj); |
|
87 break; |
|
88 } |
|
89 case UI_TOOLBAR_TOGGLEITEM: { |
|
90 add_toolitem_toggle_widget(tb, (UiToolbarToggleItem*)i, obj); |
|
91 break; |
|
92 } |
|
93 case UI_TOOLBAR_MENU: { |
|
94 add_toolitem_menu_widget(tb, (UiToolbarMenuItem*)i, obj); |
|
95 break; |
|
96 } |
|
97 default: fprintf(stderr, "toolbar item type unimplemented: %d\n", (int)i->type); |
|
98 } |
|
99 } |
|
100 |
|
101 void ui_toolbar_add_items(UiObject *obj, GtkWidget *toolbar, CxMap *items, CxList *defaults) { |
|
102 // add pre-configured items |
|
103 CxIterator i = cxListIterator(defaults); |
|
104 cx_foreach(char*, def, i) { |
|
105 UiToolbarItemI* item = uic_toolbar_get_item(def); |
|
106 if (!item) { |
|
107 fprintf(stderr, "unknown toolbar item: %s\n", def); |
|
108 continue; |
|
109 } |
|
110 create_item(obj, toolbar, item); |
|
111 } |
|
112 } |
|
113 |
|
114 static void set_toolbutton_icon(GtkToolItem *item, const char *icon_name) { |
|
115 #if GTK_MAJOR_VERSION >= 3 |
|
116 gtk_tool_button_set_icon_name(GTK_TOOL_BUTTON(item), icon_name); |
|
117 #else |
|
118 UiIcon *icon = ui_icon(icon_name, 24); |
|
119 if(icon) { |
|
120 GdkPixbuf *pixbuf = ui_icon_pixbuf(icon); |
|
121 if(pixbuf) { |
|
122 GtkWidget *image = gtk_image_new_from_pixbuf(pixbuf); |
|
123 gtk_tool_button_set_icon_widget(GTK_TOOL_BUTTON(item), image); |
|
124 } |
|
125 } |
|
126 #endif |
|
127 } |
|
128 |
|
129 void add_toolitem_widget(GtkToolbar *tb, UiToolbarItem *item, UiObject *obj) { |
|
130 GtkToolItem *button; |
|
131 if(item->args.stockid) { |
|
132 #ifdef UI_GTK2 |
|
133 button = gtk_tool_button_new_from_stock(item->args.stockid); |
|
134 #else |
|
135 // TODO: gtk3 stock |
|
136 button = gtk_tool_button_new(NULL, item->args.label); |
|
137 #endif |
|
138 } else { |
|
139 button = gtk_tool_button_new(NULL, item->args.label); |
|
140 } |
|
141 |
251 gtk_tool_item_set_homogeneous(button, FALSE); |
142 gtk_tool_item_set_homogeneous(button, FALSE); |
252 if(item->image) { |
143 if(item->args.icon) { |
253 GdkPixbuf *pixbuf = ui_get_image(item->image); |
144 set_toolbutton_icon(button, item->args.icon); |
254 GtkWidget *image = gtk_image_new_from_pixbuf(pixbuf); |
145 } |
255 gtk_tool_button_set_icon_widget(GTK_TOOL_BUTTON(button), image); |
146 gtk_tool_item_set_is_important(button, TRUE); |
256 } else { |
147 |
257 gtk_tool_item_set_is_important(button, TRUE); |
148 ui_set_widget_ngroups(obj->ctx, GTK_WIDGET(button), item->args.groups, item->ngroups); |
258 } |
149 |
259 |
150 if(item->args.onclick) { |
260 if(item->callback) { |
151 UiEventData *event = cxMalloc( |
261 UiEventData *event = ucx_mempool_malloc( |
152 obj->ctx->allocator, |
262 obj->ctx->mempool, |
|
263 sizeof(UiEventData)); |
153 sizeof(UiEventData)); |
264 event->obj = obj; |
154 event->obj = obj; |
265 event->userdata = item->userdata; |
155 event->callback = item->args.onclick; |
266 event->callback = item->callback; |
156 event->userdata = item->args.onclickdata; |
|
157 event->customdata = NULL; |
|
158 event->value = 0; |
267 |
159 |
268 g_signal_connect( |
160 g_signal_connect( |
269 button, |
161 button, |
270 "clicked", |
162 "clicked", |
271 G_CALLBACK(ui_button_clicked), |
163 G_CALLBACK(ui_button_clicked), |
272 event); |
164 event); |
273 } |
165 } |
274 |
166 |
275 gtk_toolbar_insert(tb, button, -1); |
167 gtk_toolbar_insert(tb, button, -1); |
276 |
168 |
|
169 /* |
277 if(item->groups) { |
170 if(item->groups) { |
278 uic_add_group_widget(obj->ctx, button, (ui_enablefunc)ui_set_enabled, item->groups); |
171 uic_add_group_widget(obj->ctx, button, (ui_enablefunc)ui_set_enabled, item->groups); |
279 } |
172 } |
280 } |
173 */ |
281 |
174 } |
282 void add_toolitem_st_widget(GtkToolbar *tb, UiStToolItem *item, UiObject *obj) { |
175 |
283 GtkToolItem *button = gtk_tool_button_new_from_stock(item->stockid); |
176 void add_toolitem_toggle_widget(GtkToolbar *tb, UiToolbarToggleItem *item, UiObject *obj) { |
284 gtk_tool_item_set_homogeneous(button, FALSE); |
|
285 if(item->isimportant) { |
|
286 gtk_tool_item_set_is_important(button, TRUE); |
|
287 } |
|
288 |
|
289 if(item->callback) { |
|
290 UiEventData *event = ucx_mempool_malloc( |
|
291 obj->ctx->mempool, |
|
292 sizeof(UiEventData)); |
|
293 event->obj = obj; |
|
294 event->userdata = item->userdata; |
|
295 event->callback = item->callback; |
|
296 |
|
297 g_signal_connect( |
|
298 button, |
|
299 "clicked", |
|
300 G_CALLBACK(ui_button_clicked), |
|
301 event); |
|
302 } |
|
303 |
|
304 gtk_toolbar_insert(tb, button, -1); |
|
305 |
|
306 if(item->groups) { |
|
307 uic_add_group_widget(obj->ctx, button, (ui_enablefunc)ui_set_enabled, item->groups); |
|
308 } |
|
309 } |
|
310 |
|
311 void add_toolitem_toggle_widget(GtkToolbar *tb, UiToggleToolItem *item, UiObject *obj) { |
|
312 GtkToolItem *button; |
177 GtkToolItem *button; |
313 if(item->stockid) { |
178 if(item->args.stockid) { |
314 button = gtk_toggle_tool_button_new_from_stock(item->stockid); |
179 #ifdef UI_GTK2 |
|
180 button = gtk_toggle_tool_button_new_from_stock(item->args.stockid); |
|
181 #else |
|
182 button = gtk_toggle_tool_button_new_from_stock(item->args.stockid); // TODO: gtk3 stock |
|
183 #endif |
315 } else { |
184 } else { |
316 button = gtk_toggle_tool_button_new(); |
185 button = gtk_toggle_tool_button_new(); |
317 gtk_tool_item_set_homogeneous(button, FALSE); |
186 gtk_tool_item_set_homogeneous(button, FALSE); |
318 if(item->label) { |
187 if(item->args.label) { |
319 gtk_tool_button_set_label(GTK_TOOL_BUTTON(button), item->label); |
188 gtk_tool_button_set_label(GTK_TOOL_BUTTON(button), item->args.label); |
320 } |
189 } |
321 if(item->image) { |
190 if(item->args.icon) { |
322 GdkPixbuf *pixbuf = ui_get_image(item->image); |
191 set_toolbutton_icon(button, item->args.icon); |
323 GtkWidget *image = gtk_image_new_from_pixbuf(pixbuf); |
|
324 gtk_tool_button_set_icon_widget(GTK_TOOL_BUTTON(button), image); |
|
325 } |
192 } |
326 } |
193 } |
327 |
194 ui_set_widget_ngroups(obj->ctx, GTK_WIDGET(button), item->args.groups, item->ngroups); |
328 UiVar *var; |
195 |
329 if(item->value) { |
196 UiVar* var = uic_widget_var(obj->ctx, obj->ctx, NULL, item->args.varname, UI_VAR_INTEGER); |
330 var = malloc(sizeof(UiVar)); |
197 if(var) { |
331 var->value = item->value; |
198 UiInteger *i = (UiInteger*)var->value; |
332 var->type = UI_VAR_SPECIAL; |
199 if(i) { |
333 var->from = NULL; |
200 i->get = ui_tool_toggle_button_get; |
334 var->from_ctx = NULL; |
201 i->set = ui_tool_toggle_button_set; |
335 } else { |
202 i->obj = button; |
336 var = uic_create_var(obj->ctx, item->var, UI_VAR_INTEGER); |
203 |
337 } |
204 if(i->value != 0) { |
338 |
205 gtk_toggle_tool_button_set_active(GTK_TOGGLE_TOOL_BUTTON(button), TRUE); |
339 if(var->value) { |
206 } |
340 UiInteger *i = var->value; |
207 } |
341 i->get = ui_tool_toggle_button_get; |
208 } |
342 i->set = ui_tool_toggle_button_set; |
209 |
343 i->obj = button; |
210 UiVarEventData *event = cxMalloc( |
344 |
211 obj->ctx->allocator, |
345 if(i->value != 0) { |
212 sizeof(UiVarEventData)); |
346 gtk_toggle_tool_button_set_active(GTK_TOGGLE_TOOL_BUTTON(button), TRUE); |
|
347 } |
|
348 } |
|
349 |
|
350 // register event |
|
351 // the event func will call the UiInteger observer callbacks |
|
352 UiEventData *event = ucx_mempool_malloc( |
|
353 obj->ctx->mempool, |
|
354 sizeof(UiEventData)); |
|
355 event->obj = obj; |
213 event->obj = obj; |
356 event->userdata = var; |
214 event->callback = item->args.onchange; |
357 event->callback = NULL; |
215 event->userdata = item->args.onchangedata; |
|
216 event->var = var; |
358 |
217 |
359 g_signal_connect( |
218 g_signal_connect( |
360 button, |
219 button, |
361 "toggled", |
220 "toggled", |
362 G_CALLBACK(ui_tool_button_toggled), |
221 G_CALLBACK(ui_tool_button_toggled), |
363 event); |
222 event); |
364 |
223 |
365 // add item to toolbar |
224 // add item to toolbar |
366 gtk_toolbar_insert(tb, button, -1); |
225 gtk_toolbar_insert(tb, button, -1); |
367 |
226 |
|
227 /* |
368 if(item->groups) { |
228 if(item->groups) { |
369 uic_add_group_widget(obj->ctx, button, (ui_enablefunc)ui_set_enabled, item->groups); |
229 uic_add_group_widget(obj->ctx, button, (ui_enablefunc)ui_set_enabled, item->groups); |
370 } |
230 } |
371 } |
231 */ |
372 |
232 } |
373 void ui_tool_button_toggled(GtkToggleToolButton *widget, UiEventData *event) { |
233 |
|
234 void ui_tool_button_toggled(GtkToggleToolButton *widget, UiVarEventData *event) { |
374 UiEvent e; |
235 UiEvent e; |
375 e.obj = event->obj; |
236 e.obj = event->obj; |
376 e.window = event->obj->window; |
237 e.window = event->obj->window; |
377 e.document = event->obj->ctx->document; |
238 e.document = event->obj->ctx->document; |
378 e.eventdata = NULL; |
239 e.eventdata = NULL; |
379 e.intval = gtk_toggle_tool_button_get_active(widget); |
240 e.intval = gtk_toggle_tool_button_get_active(widget); |
380 |
241 |
381 UiVar *var = event->userdata; |
242 if(event->callback) { |
382 UiInteger *i = var->value; |
243 event->callback(&e, event->userdata); |
383 |
244 } |
384 ui_notify_evt(i->observers, &e); |
245 |
|
246 UiVar *var = event->var; |
|
247 UiInteger *i = var ? var->value : NULL; |
|
248 |
|
249 if(i) { |
|
250 ui_notify_evt(i->observers, &e); |
|
251 } |
385 } |
252 } |
386 |
253 |
387 int64_t ui_tool_toggle_button_get(UiInteger *integer) { |
254 int64_t ui_tool_toggle_button_get(UiInteger *integer) { |
388 integer->value = gtk_toggle_tool_button_get_active(GTK_TOGGLE_TOOL_BUTTON(integer->obj)); |
255 integer->value = gtk_toggle_tool_button_get_active(GTK_TOGGLE_TOOL_BUTTON(integer->obj)); |
389 return integer->value; |
256 return integer->value; |