ui/gtk/toolbar.c

changeset 33
458831c574f4
parent 27
77b09bb52ca0
child 42
29b2821d1262
equal deleted inserted replaced
32:9a5f47fbc5c3 33:458831c574f4
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 "button.h" 34 #include "button.h"
35 #include "image.h"
35 #include "../../ucx/mempool.h" 36 #include "../../ucx/mempool.h"
36 #include "../common/context.h" 37 #include "../common/context.h"
37 38
38 static UcxMap *toolbar_items; 39 static UcxMap *toolbar_items;
39 static UcxList *defaults; 40 static UcxList *defaults;
41 void ui_toolbar_init() { 42 void ui_toolbar_init() {
42 toolbar_items = ucx_map_new(16); 43 toolbar_items = ucx_map_new(16);
43 } 44 }
44 45
45 void ui_toolitem(char *name, char *label, ui_callback f, void *udata) { 46 void ui_toolitem(char *name, char *label, ui_callback f, void *udata) {
47 ui_toolitem_img(name, label, NULL, f, udata);
48 }
49
50 void ui_toolitem_st(char *name, char *stockid, ui_callback f, void *userdata) {
51 ui_toolitem_stgr(name, stockid, f, userdata, -1);
52 }
53
54 void ui_toolitem_sti(char *name, char *stockid, ui_callback f, void *userdata) {
55 ui_toolitem_stgri(name, stockid, f, userdata, -1);
56 }
57
58 void ui_toolitem_stgr(char *name, char *stockid, ui_callback f, void *userdata, ...) {
59 va_list ap;
60 va_start(ap, userdata);
61 ui_toolitem_vstgr(name, stockid, 0, f, userdata, ap);
62 va_end(ap);
63 }
64
65 void ui_toolitem_stgri(char *name, char *stockid, ui_callback f, void *userdata, ...) {
66 va_list ap;
67 va_start(ap, userdata);
68 ui_toolitem_vstgr(name, stockid, 1, f, userdata, ap);
69 va_end(ap);
70 }
71
72 void ui_toolitem_img(char *name, char *label, char *img, ui_callback f, void *udata) {
46 UiToolItem *item = malloc(sizeof(UiToolItem)); 73 UiToolItem *item = malloc(sizeof(UiToolItem));
47 item->item.add_to = (ui_toolbar_add_f)add_toolitem_widget; 74 item->item.add_to = (ui_toolbar_add_f)add_toolitem_widget;
48 item->label = label; 75 item->label = label;
76 item->image = img;
49 item->callback = f; 77 item->callback = f;
50 item->userdata = udata; 78 item->userdata = udata;
79 item->isimportant = 0;
80 item->groups = NULL;
51 81
52 ucx_map_cstr_put(toolbar_items, name, item); 82 ucx_map_cstr_put(toolbar_items, name, item);
53 } 83 }
54 84
55 void ui_toolitem_st(char *name, char *stockid, ui_callback f, void *userdata) { 85 void ui_toolitem_vstgr(
56 ui_toolitem_stgr(name, stockid, f, userdata, -1); 86 char *name,
57 } 87 char *stockid,
58 88 int isimportant,
59 void ui_toolitem_stgr(char *name, char *stockid, ui_callback f, void *userdata, ...) { 89 ui_callback f,
90 void *userdata,
91 va_list ap)
92 {
60 UiStToolItem *item = malloc(sizeof(UiStToolItem)); 93 UiStToolItem *item = malloc(sizeof(UiStToolItem));
61 item->item.add_to = (ui_toolbar_add_f)add_toolitem_st_widget; 94 item->item.add_to = (ui_toolbar_add_f)add_toolitem_st_widget;
62 item->stockid = stockid; 95 item->stockid = stockid;
63 item->callback = f; 96 item->callback = f;
64 item->userdata = userdata; 97 item->userdata = userdata;
98 item->groups = NULL;
99 item->isimportant = isimportant;
65 100
66 // add groups 101 // add groups
67 va_list ap;
68 va_start(ap, userdata);
69 int group; 102 int group;
70 while((group = va_arg(ap, int)) != -1) { 103 while((group = va_arg(ap, int)) != -1) {
71 item->groups = ucx_list_append(item->groups, (void*)(intptr_t)group); 104 item->groups = ucx_list_append(item->groups, (void*)(intptr_t)group);
72 } 105 }
106
107 ucx_map_cstr_put(toolbar_items, name, item);
108 }
109
110 void ui_toolitem_toggle_st(char *name, char *stockid, ui_callback f, void *udata, ...) {
111 ui_toolitem_toggle_stgr(name, stockid, f, udata, -1);
112 }
113
114 void ui_toolitem_toggle_stgr(char *name, char *stockid, ui_callback f, void *udata, ...) {
115 UiStToolItem *item = malloc(sizeof(UiStToolItem));
116 item->item.add_to = (ui_toolbar_add_f)add_toolitem_toggle_st_widget;
117 item->stockid = stockid;
118 item->callback = f;
119 item->userdata = udata;
120 item->groups = NULL;
121 item->isimportant = 0;
122
123 // add groups
124 va_list ap;
125 va_start(ap, udata);
126 int group;
127 while((group = va_arg(ap, int)) != -1) {
128 item->groups = ucx_list_append(item->groups, (void*)(intptr_t)group);
129 }
73 va_end(ap); 130 va_end(ap);
74 131
75 ucx_map_cstr_put(toolbar_items, name, item); 132 ucx_map_cstr_put(toolbar_items, name, item);
76 } 133 }
134
135 void ui_toolitem_toggle_imggr(char *name, char *label, char *img, ui_callback f, void *udata, ...) {
136 UiToolItem *item = malloc(sizeof(UiToolItem));
137 item->item.add_to = (ui_toolbar_add_f)add_toolitem_toggle_widget;
138 item->label = label;
139 item->image = img;
140 item->callback = f;
141 item->userdata = udata;
142 item->groups = NULL;
143 item->isimportant = 0;
144
145 // add groups
146 va_list ap;
147 va_start(ap, udata);
148 int group;
149 while((group = va_arg(ap, int)) != -1) {
150 item->groups = ucx_list_append(item->groups, (void*)(intptr_t)group);
151 }
152 va_end(ap);
153
154 ucx_map_cstr_put(toolbar_items, name, item);
155 }
156
77 157
78 void ui_toolbar_add_default(char *name) { 158 void ui_toolbar_add_default(char *name) {
79 char *s = strdup(name); 159 char *s = strdup(name);
80 defaults = ucx_list_append(defaults, s); 160 defaults = ucx_list_append(defaults, s);
81 } 161 }
108 } 188 }
109 189
110 void add_toolitem_widget(GtkToolbar *tb, UiToolItem *item, UiObject *obj) { 190 void add_toolitem_widget(GtkToolbar *tb, UiToolItem *item, UiObject *obj) {
111 GtkToolItem *button = gtk_tool_button_new(NULL, item->label); 191 GtkToolItem *button = gtk_tool_button_new(NULL, item->label);
112 gtk_tool_item_set_homogeneous(button, FALSE); 192 gtk_tool_item_set_homogeneous(button, FALSE);
193 if(item->image) {
194 GdkPixbuf *pixbuf = ui_get_image(item->image);
195 GtkWidget *image = gtk_image_new_from_pixbuf(pixbuf);
196 gtk_tool_button_set_icon_widget(GTK_TOOL_BUTTON(button), image);
197 }
113 198
114 if(item->callback) { 199 if(item->callback) {
115 UiEventData *event = ucx_mempool_malloc( 200 UiEventData *event = ucx_mempool_malloc(
116 obj->ctx->mempool, 201 obj->ctx->mempool,
117 sizeof(UiEventData)); 202 sizeof(UiEventData));
134 } 219 }
135 220
136 void add_toolitem_st_widget(GtkToolbar *tb, UiStToolItem *item, UiObject *obj) { 221 void add_toolitem_st_widget(GtkToolbar *tb, UiStToolItem *item, UiObject *obj) {
137 GtkToolItem *button = gtk_tool_button_new_from_stock(item->stockid); 222 GtkToolItem *button = gtk_tool_button_new_from_stock(item->stockid);
138 gtk_tool_item_set_homogeneous(button, FALSE); 223 gtk_tool_item_set_homogeneous(button, FALSE);
224 if(item->isimportant) {
225 gtk_tool_item_set_is_important(button, TRUE);
226 }
139 227
140 if(item->callback) { 228 if(item->callback) {
141 UiEventData *event = ucx_mempool_malloc( 229 UiEventData *event = ucx_mempool_malloc(
142 obj->ctx->mempool, 230 obj->ctx->mempool,
143 sizeof(UiEventData)); 231 sizeof(UiEventData));
156 244
157 if(item->groups) { 245 if(item->groups) {
158 uic_add_group_widget(obj->ctx, button, item->groups); 246 uic_add_group_widget(obj->ctx, button, item->groups);
159 } 247 }
160 } 248 }
249
250 void add_toolitem_toggle_widget(GtkToolbar *tb, UiToolItem *item, UiObject *obj) {
251 GtkToolItem *button = gtk_toggle_tool_button_new();
252 gtk_tool_item_set_homogeneous(button, FALSE);
253 if(item->label) {
254 gtk_tool_button_set_label(GTK_TOOL_BUTTON(button), item->label);
255 }
256 if(item->image) {
257 GdkPixbuf *pixbuf = ui_get_image(item->image);
258 GtkWidget *image = gtk_image_new_from_pixbuf(pixbuf);
259 gtk_tool_button_set_icon_widget(GTK_TOOL_BUTTON(button), image);
260 }
261
262 if(item->callback) {
263 UiEventData *event = ucx_mempool_malloc(
264 obj->ctx->mempool,
265 sizeof(UiEventData));
266 event->obj = obj;
267 event->user_data = item->userdata;
268 event->callback = item->callback;
269
270 g_signal_connect(
271 button,
272 "toggled",
273 G_CALLBACK(ui_button_toggled),
274 event);
275 }
276
277 gtk_toolbar_insert(tb, button, -1);
278
279 if(item->groups) {
280 uic_add_group_widget(obj->ctx, button, item->groups);
281 }
282 }
283
284 void add_toolitem_toggle_st_widget(GtkToolbar *tb, UiStToolItem *item, UiObject *obj) {
285 GtkToolItem *button = gtk_toggle_tool_button_new_from_stock(item->stockid);
286 gtk_tool_item_set_homogeneous(button, FALSE);
287
288 if(item->callback) {
289 UiEventData *event = ucx_mempool_malloc(
290 obj->ctx->mempool,
291 sizeof(UiEventData));
292 event->obj = obj;
293 event->user_data = item->userdata;
294 event->callback = item->callback;
295
296 g_signal_connect(
297 button,
298 "toggled",
299 G_CALLBACK(ui_button_toggled),
300 event);
301 }
302
303 gtk_toolbar_insert(tb, button, -1);
304
305 if(item->groups) {
306 uic_add_group_widget(obj->ctx, button, item->groups);
307 }
308 }

mercurial