ui/gtk/toolbar.c

branch
newapi
changeset 178
7c3ff86ee9d4
parent 174
0358f1d9c506
child 253
087cc9216f28
equal deleted inserted replaced
177:e79a60b3a7cb 178:7c3ff86ee9d4
32 32
33 #include "toolbar.h" 33 #include "toolbar.h"
34 #include "button.h" 34 #include "button.h"
35 #include "image.h" 35 #include "image.h"
36 #include "tree.h" 36 #include "tree.h"
37 #include <ucx/mempool.h> 37 #include <cx/basic_mempool.h>
38 #include <cx/hash_map.h>
39 #include <cx/linked_list.h>
40 #include <cx/array_list.h>
38 #include "../common/context.h" 41 #include "../common/context.h"
39 42
40 static UcxMap *toolbar_items; 43 static CxMap *toolbar_items;
41 static UcxList *defaults; 44 static CxList *defaults;
42 45
43 void ui_toolbar_init() { 46 void ui_toolbar_init() {
44 toolbar_items = ucx_map_new(16); 47 toolbar_items = cxHashMapCreate(cxDefaultAllocator, CX_STORE_POINTERS, 16);
48 defaults = cxLinkedListCreateSimple(CX_STORE_POINTERS);
45 } 49 }
46 50
47 void ui_toolitem(char *name, char *label, ui_callback f, void *udata) { 51 void ui_toolitem(char *name, char *label, ui_callback f, void *udata) {
48 ui_toolitem_img(name, label, NULL, f, udata); 52 ui_toolitem_img(name, label, NULL, f, udata);
49 } 53 }
78 item->callback = f; 82 item->callback = f;
79 item->userdata = udata; 83 item->userdata = udata;
80 item->isimportant = 0; 84 item->isimportant = 0;
81 item->groups = NULL; 85 item->groups = NULL;
82 86
83 ucx_map_cstr_put(toolbar_items, name, item); 87 cxMapPut(toolbar_items, name, item);
84 } 88 }
85 89
86 void ui_toolitem_vstgr( 90 void ui_toolitem_vstgr(
87 char *name, 91 char *name,
88 char *stockid, 92 char *stockid,
100 item->isimportant = isimportant; 104 item->isimportant = isimportant;
101 105
102 // add groups 106 // add groups
103 int group; 107 int group;
104 while((group = va_arg(ap, int)) != -1) { 108 while((group = va_arg(ap, int)) != -1) {
105 item->groups = ucx_list_append(item->groups, (void*)(intptr_t)group); 109 if(!item->groups) {
106 } 110 item->groups = cxArrayListCreateSimple(sizeof(int), 16);
107 111 }
108 ucx_map_cstr_put(toolbar_items, name, item); 112 cxListAdd(item->groups, &group);
113 }
114
115 cxMapPut(toolbar_items, name, item);
109 } 116 }
110 117
111 void ui_toolitem_toggle(const char *name, const char *label, const char *img, UiInteger *i) { 118 void ui_toolitem_toggle(const char *name, const char *label, const char *img, UiInteger *i) {
112 UiToggleToolItem *item = malloc(sizeof(UiToggleToolItem)); 119 UiToggleToolItem *item = malloc(sizeof(UiToggleToolItem));
113 item->item.add_to = (ui_toolbar_add_f)add_toolitem_toggle_widget; 120 item->item.add_to = (ui_toolbar_add_f)add_toolitem_toggle_widget;
117 item->groups = NULL; 124 item->groups = NULL;
118 item->isimportant = 0; 125 item->isimportant = 0;
119 item->value = i; 126 item->value = i;
120 item->var = NULL; 127 item->var = NULL;
121 128
122 ucx_map_cstr_put(toolbar_items, name, item); 129 cxMapPut(toolbar_items, name, item);
123 } 130 }
124 131
125 void ui_toolitem_toggle_st(const char *name, const char *stockid, UiInteger *i) { 132 void ui_toolitem_toggle_st(const char *name, const char *stockid, UiInteger *i) {
126 UiToggleToolItem *item = malloc(sizeof(UiToggleToolItem)); 133 UiToggleToolItem *item = malloc(sizeof(UiToggleToolItem));
127 item->item.add_to = (ui_toolbar_add_f)add_toolitem_toggle_widget; 134 item->item.add_to = (ui_toolbar_add_f)add_toolitem_toggle_widget;
131 item->groups = NULL; 138 item->groups = NULL;
132 item->isimportant = 0; 139 item->isimportant = 0;
133 item->value = i; 140 item->value = i;
134 item->var = NULL; 141 item->var = NULL;
135 142
136 ucx_map_cstr_put(toolbar_items, name, item); 143 cxMapPut(toolbar_items, name, item);
137 } 144 }
138 145
139 void ui_toolitem_toggle_nv(const char *name, const char *label, const char *img, const char *intvar) { 146 void ui_toolitem_toggle_nv(const char *name, const char *label, const char *img, const char *intvar) {
140 UiToggleToolItem *item = malloc(sizeof(UiToggleToolItem)); 147 UiToggleToolItem *item = malloc(sizeof(UiToggleToolItem));
141 item->item.add_to = (ui_toolbar_add_f)add_toolitem_toggle_widget; 148 item->item.add_to = (ui_toolbar_add_f)add_toolitem_toggle_widget;
145 item->groups = NULL; 152 item->groups = NULL;
146 item->isimportant = 0; 153 item->isimportant = 0;
147 item->value = NULL; 154 item->value = NULL;
148 item->var = intvar; 155 item->var = intvar;
149 156
150 ucx_map_cstr_put(toolbar_items, name, item); 157 cxMapPut(toolbar_items, name, item);
151 } 158 }
152 159
153 void ui_toolitem_toggle_stnv(const char *name, const char *stockid, const char *intvar) { 160 void ui_toolitem_toggle_stnv(const char *name, const char *stockid, const char *intvar) {
154 UiToggleToolItem *item = malloc(sizeof(UiToggleToolItem)); 161 UiToggleToolItem *item = malloc(sizeof(UiToggleToolItem));
155 item->item.add_to = (ui_toolbar_add_f)add_toolitem_toggle_widget; 162 item->item.add_to = (ui_toolbar_add_f)add_toolitem_toggle_widget;
159 item->groups = NULL; 166 item->groups = NULL;
160 item->isimportant = 0; 167 item->isimportant = 0;
161 item->value = NULL; 168 item->value = NULL;
162 item->var = intvar; 169 item->var = intvar;
163 170
164 ucx_map_cstr_put(toolbar_items, name, item); 171 cxMapPut(toolbar_items, name, item);
165 } 172 }
166 173
167 174
168 void ui_toolbar_combobox( 175 void ui_toolbar_combobox(
169 char *name, 176 char *name,
182 cb->var = var; 189 cb->var = var;
183 cb->getvalue = getvalue; 190 cb->getvalue = getvalue;
184 cb->callback = f; 191 cb->callback = f;
185 cb->userdata = udata; 192 cb->userdata = udata;
186 193
187 ucx_map_cstr_put(toolbar_items, name, cb); 194 cxMapPut(toolbar_items, name, cb);
188 } 195 }
189 196
190 void ui_toolbar_combobox_str( 197 void ui_toolbar_combobox_str(
191 char *name, 198 char *name,
192 UiList *list, 199 UiList *list,
208 cb->listname = listname; 215 cb->listname = listname;
209 cb->getvalue = getvalue; 216 cb->getvalue = getvalue;
210 cb->callback = f; 217 cb->callback = f;
211 cb->userdata = udata; 218 cb->userdata = udata;
212 219
213 ucx_map_cstr_put(toolbar_items, name, cb); 220 cxMapPut(toolbar_items, name, cb);
214 } 221 }
215 222
216 223
217 void ui_toolbar_add_default(char *name) { 224 void ui_toolbar_add_default(char *name) {
218 char *s = strdup(name); 225 char *s = strdup(name);
219 defaults = ucx_list_append(defaults, s); 226 cxListAdd(defaults, s);
220 } 227 }
221 228
222 GtkWidget* ui_create_toolbar(UiObject *obj) { 229 GtkWidget* ui_create_toolbar(UiObject *obj) {
223 if(!defaults) { 230 if(!defaults) {
224 return NULL; 231 return NULL;
230 gtk_widget_get_style_context(toolbar), 237 gtk_widget_get_style_context(toolbar),
231 GTK_STYLE_CLASS_PRIMARY_TOOLBAR); 238 GTK_STYLE_CLASS_PRIMARY_TOOLBAR);
232 #endif 239 #endif
233 240
234 GtkToolbar *tb = GTK_TOOLBAR(toolbar); 241 GtkToolbar *tb = GTK_TOOLBAR(toolbar);
235 UCX_FOREACH(elm, defaults) { 242 CxIterator i = cxListIterator(defaults);
236 UiToolItemI *item = ucx_map_cstr_get(toolbar_items, elm->data); 243 cx_foreach(char *, def, i) {
244 UiToolItemI *item = cxMapGet(toolbar_items, def);
237 if(item) { 245 if(item) {
238 item->add_to(tb, item, obj); 246 item->add_to(tb, item, obj);
239 } else if(!strcmp(elm->data, "@separator")) { 247 } else if(!strcmp(def, "@separator")) {
240 gtk_toolbar_insert(tb, gtk_separator_tool_item_new(), -1); 248 gtk_toolbar_insert(tb, gtk_separator_tool_item_new(), -1);
241 } else { 249 } else {
242 fprintf(stderr, "UI Error: Unknown toolbar item: %s\n", elm->data); 250 fprintf(stderr, "UI Error: Unknown toolbar item: %s\n", def);
243 } 251 }
244 } 252 }
245 253
246 return toolbar; 254 return toolbar;
247 } 255 }
256 } else { 264 } else {
257 gtk_tool_item_set_is_important(button, TRUE); 265 gtk_tool_item_set_is_important(button, TRUE);
258 } 266 }
259 267
260 if(item->callback) { 268 if(item->callback) {
261 UiEventData *event = ucx_mempool_malloc( 269 UiEventData *event = cxMalloc(
262 obj->ctx->mempool, 270 obj->ctx->allocator,
263 sizeof(UiEventData)); 271 sizeof(UiEventData));
264 event->obj = obj; 272 event->obj = obj;
265 event->userdata = item->userdata; 273 event->userdata = item->userdata;
266 event->callback = item->callback; 274 event->callback = item->callback;
267 275
285 if(item->isimportant) { 293 if(item->isimportant) {
286 gtk_tool_item_set_is_important(button, TRUE); 294 gtk_tool_item_set_is_important(button, TRUE);
287 } 295 }
288 296
289 if(item->callback) { 297 if(item->callback) {
290 UiEventData *event = ucx_mempool_malloc( 298 UiEventData *event = cxMalloc(
291 obj->ctx->mempool, 299 obj->ctx->allocator,
292 sizeof(UiEventData)); 300 sizeof(UiEventData));
293 event->obj = obj; 301 event->obj = obj;
294 event->userdata = item->userdata; 302 event->userdata = item->userdata;
295 event->callback = item->callback; 303 event->callback = item->callback;
296 304
347 } 355 }
348 } 356 }
349 357
350 // register event 358 // register event
351 // the event func will call the UiInteger observer callbacks 359 // the event func will call the UiInteger observer callbacks
352 UiEventData *event = ucx_mempool_malloc( 360 UiEventData *event = cxMalloc(
353 obj->ctx->mempool, 361 obj->ctx->allocator,
354 sizeof(UiEventData)); 362 sizeof(UiEventData));
355 event->obj = obj; 363 event->obj = obj;
356 event->userdata = var; 364 event->userdata = var;
357 event->callback = NULL; 365 event->callback = NULL;
358 366

mercurial