ui/motif/toolbar.c

changeset 100
d2bd73d28ff1
parent 0
2483f517c562
equal deleted inserted replaced
99:b9767cb5b06b 100:d2bd73d28ff1
1 /* 1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 * 3 *
4 * Copyright 2014 Olaf Wintermann. All rights reserved. 4 * Copyright 2024 Olaf Wintermann. All rights reserved.
5 * 5 *
6 * Redistribution and use in source and binary forms, with or without 6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met: 7 * modification, are permitted provided that the following conditions are met:
8 * 8 *
9 * 1. Redistributions of source code must retain the above copyright 9 * 1. Redistributions of source code must retain the above copyright
40 #include <cx/linked_list.h> 40 #include <cx/linked_list.h>
41 #include <cx/array_list.h> 41 #include <cx/array_list.h>
42 42
43 #include "../common/context.h" 43 #include "../common/context.h"
44 44
45 static CxMap *toolbar_items;
46 static CxList *defaults;
47
48 void ui_toolbar_init() {
49 toolbar_items = cxHashMapCreate(cxDefaultAllocator, CX_STORE_POINTERS, 16);
50 defaults = cxLinkedListCreateSimple(CX_STORE_POINTERS);
51 }
52
53 void ui_toolitem(char *name, char *label, ui_callback f, void *userdata) {
54 UiToolItem *item = malloc(sizeof(UiToolItem));
55 item->item.add_to = (ui_toolbar_add_f)add_toolitem_widget;
56 item->label = label;
57 item->image = NULL;
58 item->callback = f;
59 item->userdata = userdata;
60 item->groups = NULL;
61 item->isimportant = FALSE;
62
63 cxMapPut(toolbar_items, name, item);
64 }
65
66 void ui_toolitem_st(char *name, char *stockid, ui_callback f, void *userdata) {
67 ui_toolitem_stgr(name, stockid, f, userdata, -1);
68 }
69
70 void ui_toolitem_stgr(char *name, char *stockid, ui_callback f, void *userdata, ...) {
71 UiStToolItem *item = malloc(sizeof(UiStToolItem));
72 item->item.add_to = (ui_toolbar_add_f)add_toolitem_st_widget;
73 item->stockid = stockid;
74 item->callback = f;
75 item->userdata = userdata;
76 item->groups = NULL;
77 item->isimportant = FALSE;
78
79 // add groups
80 va_list ap;
81 va_start(ap, userdata);
82 int group;
83 while((group = va_arg(ap, int)) != -1) {
84 if(!item->groups) {
85 item->groups = cxArrayListCreateSimple(sizeof(int), 16);
86 }
87 cxListAdd(item->groups, &group);
88 }
89 va_end(ap);
90
91 cxMapPut(toolbar_items, name, item);
92 }
93
94 void ui_toolitem_img(char *name, char *label, char *img, ui_callback f, void *udata) {
95 // TODO
96
97 UiToolItem *item = malloc(sizeof(UiToolItem));
98 item->item.add_to = (ui_toolbar_add_f)add_toolitem_widget;
99 item->label = label;
100 item->image = img;
101 item->callback = f;
102 item->userdata = udata;
103 item->groups = NULL;
104 item->isimportant = FALSE;
105
106 cxMapPut(toolbar_items, name, item);
107 }
108
109
110 void ui_toolitem_toggle_stgr(char *name, char *stockid, ui_callback f, void *udata, ...) {
111 // TODO
112
113 UiStToolItem *item = malloc(sizeof(UiStToolItem));
114 item->item.add_to = (ui_toolbar_add_f)add_toolitem_st_toggle_widget;
115 item->stockid = stockid;
116 item->callback = f;
117 item->userdata = udata;
118 item->groups = NULL;
119 item->isimportant = FALSE;
120
121 // add groups
122 va_list ap;
123 va_start(ap, udata);
124 int group;
125 while((group = va_arg(ap, int)) != -1) {
126 if(!item->groups) {
127 item->groups = cxArrayListCreateSimple(sizeof(int), 16);
128 }
129 cxListAdd(item->groups, &group);
130 }
131 va_end(ap);
132
133 cxMapPut(toolbar_items, name, item);
134 }
135
136 void ui_toolitem_toggle_imggr(char *name, char *label, char *img, ui_callback f, void *udata, ...) {
137 // TODO
138
139 UiToolItem *item = malloc(sizeof(UiToolItem));
140 item->item.add_to = (ui_toolbar_add_f)add_toolitem_toggle_widget;
141 item->label = label;
142 item->image = img;
143 item->callback = f;
144 item->userdata = udata;
145 item->groups = NULL;
146 item->isimportant = FALSE;
147
148 // add groups
149 va_list ap;
150 va_start(ap, udata);
151 int group;
152 while((group = va_arg(ap, int)) != -1) {
153 if(!item->groups) {
154 item->groups = cxArrayListCreateSimple(sizeof(int), 16);
155 }
156 cxListAdd(item->groups, &group);
157 }
158 va_end(ap);
159
160 cxMapPut(toolbar_items, name, item);
161 }
162
163 void ui_toolbar_combobox(
164 char *name,
165 UiList *list,
166 ui_getvaluefunc getvalue,
167 ui_callback f,
168 void *udata)
169 {
170 UiToolbarComboBox *cb = malloc(sizeof(UiToolbarComboBox));
171 cb->item.add_to = (ui_toolbar_add_f)add_toolbar_combobox;
172 cb->list = list;
173 cb->getvalue = getvalue;
174 cb->callback = f;
175 cb->userdata = udata;
176
177 cxMapPut(toolbar_items, name, cb);
178 }
179
180 void ui_toolbar_combobox_str(
181 char *name,
182 UiList *list,
183 ui_callback f,
184 void *udata)
185 {
186 ui_toolbar_combobox(name, list, ui_strmodel_getvalue, f, udata);
187 }
188
189 void ui_toolbar_combobox_nv(
190 char *name,
191 char *listname,
192 ui_getvaluefunc getvalue,
193 ui_callback f,
194 void *udata)
195 {
196 UiToolbarComboBoxNV *cb = malloc(sizeof(UiToolbarComboBoxNV));
197 cb->item.add_to = (ui_toolbar_add_f)add_toolbar_combobox_nv;
198 cb->listname = listname;
199 cb->getvalue = getvalue;
200 cb->callback = f;
201 cb->userdata = udata;
202
203 cxMapPut(toolbar_items, name, cb);
204 }
205
206
207 void ui_toolbar_add_default(char *name) {
208 char *s = strdup(name);
209 cxListAdd(defaults, s);
210 }
211
212 Widget ui_create_toolbar(UiObject *obj, Widget parent) {
213 if(!defaults) {
214 return NULL;
215 }
216
217 Arg args[8];
218 XtSetArg(args[0], XmNshadowType, XmSHADOW_ETCHED_OUT);
219 XtSetArg(args[1], XmNshadowThickness, 1);
220 XtSetArg(args[2], XmNtopAttachment, XmATTACH_FORM);
221 XtSetArg(args[3], XmNleftAttachment, XmATTACH_FORM);
222 XtSetArg(args[4], XmNrightAttachment, XmATTACH_FORM);
223 Widget frame = XmCreateFrame(parent, "toolbar_frame", args, 5);
224
225 XtSetArg(args[0], XmNorientation, XmHORIZONTAL);
226 XtSetArg(args[1], XmNpacking, XmPACK_TIGHT);
227 XtSetArg(args[2], XmNspacing, 1);
228 Widget toolbar = XmCreateRowColumn (frame, "toolbar", args, 3);
229
230 CxIterator i = cxListIterator(defaults);
231 cx_foreach(char *, def, i) {
232 UiToolItemI *item = cxMapGet(toolbar_items, def);
233 if(item) {
234 item->add_to(toolbar, item, obj);
235 } else if(!strcmp(def, "@separator")) {
236 // TODO
237 } else {
238 fprintf(stderr, "UI Error: Unknown toolbar item: %s\n", def);
239 }
240 }
241
242 XtManageChild(toolbar);
243 XtManageChild(frame);
244
245 return frame;
246 }
247
248 void add_toolitem_widget(Widget parent, UiToolItem *item, UiObject *obj) {
249 Arg args[4];
250
251 XmString label = XmStringCreateLocalized(item->label);
252 XtSetArg(args[0], XmNlabelString, label);
253 XtSetArg(args[1], XmNshadowThickness, 1);
254 XtSetArg(args[2], XmNtraversalOn, FALSE);
255 Widget button = XmCreatePushButton(parent, "toolbar_button", args, 3);
256
257 XmStringFree(label);
258
259 if(item->callback) {
260 UiEventData *event = cxMalloc(
261 obj->ctx->allocator,
262 sizeof(UiEventData));
263 event->obj = obj;
264 event->userdata = item->userdata;
265 event->callback = item->callback;
266 XtAddCallback(
267 button,
268 XmNactivateCallback,
269 (XtCallbackProc)ui_push_button_callback,
270 event);
271 }
272
273 XtManageChild(button);
274
275 if(item->groups) {
276 uic_add_group_widget(obj->ctx, button, (ui_enablefunc)XtSetSensitive, item->groups);
277 }
278 }
279
280 void add_toolitem_st_widget(Widget parent, UiStToolItem *item, UiObject *obj) {
281 Arg args[8];
282
283 UiStockItem *stock_item = ui_get_stock_item(item->stockid);
284
285 XmString label = XmStringCreateLocalized(stock_item->label);
286 XtSetArg(args[0], XmNlabelString, label);
287 XtSetArg(args[1], XmNshadowThickness, 1);
288 XtSetArg(args[2], XmNtraversalOn, FALSE);
289 Widget button = XmCreatePushButton(parent, "toolbar_button", args, 3);
290
291 XmStringFree(label);
292
293 if(item->callback) {
294 UiEventData *event = cxMalloc(
295 obj->ctx->allocator,
296 sizeof(UiEventData));
297 event->obj = obj;
298 event->userdata = item->userdata;
299 event->callback = item->callback;
300 XtAddCallback(
301 button,
302 XmNactivateCallback,
303 (XtCallbackProc)ui_push_button_callback,
304 event);
305 }
306
307 XtManageChild(button);
308
309 if(item->groups) {
310 uic_add_group_widget(obj->ctx, button, (ui_enablefunc)XtSetSensitive, item->groups);
311 }
312 }
313
314 void add_toolitem_toggle_widget(Widget parent, UiToolItem *item, UiObject *obj) {
315 Arg args[8];
316
317 XmString label = XmStringCreateLocalized(item->label);
318 XtSetArg(args[0], XmNlabelString, label);
319 XtSetArg(args[1], XmNshadowThickness, 1);
320 XtSetArg(args[2], XmNtraversalOn, FALSE);
321 XtSetArg(args[3], XmNindicatorOn, XmINDICATOR_NONE);
322 Widget button = XmCreateToggleButton(parent, "toolbar_toggle_button", args, 4);
323
324 XmStringFree(label);
325
326 if(item->callback) {
327 UiEventData *event = cxMalloc(
328 obj->ctx->allocator,
329 sizeof(UiEventData));
330 event->obj = obj;
331 event->userdata = item->userdata;
332 event->callback = item->callback;
333 XtAddCallback(
334 button,
335 XmNvalueChangedCallback,
336 (XtCallbackProc)ui_toggle_button_callback,
337 event);
338 }
339
340 XtManageChild(button);
341
342 if(item->groups) {
343 uic_add_group_widget(obj->ctx, button, (ui_enablefunc)XtSetSensitive, item->groups);
344 }
345 }
346
347 void add_toolitem_st_toggle_widget(Widget parent, UiStToolItem *item, UiObject *obj) {
348
349 }
350
351 void add_toolbar_combobox(Widget tb, UiToolbarComboBox *item, UiObject *obj) {
352 UiListView *listview = cxMalloc(
353 obj->ctx->allocator,
354 sizeof(UiListView));
355
356 UiVar *var = cxMalloc(obj->ctx->allocator, sizeof(UiVar));
357 var->value = item->list;
358 var->type = UI_VAR_SPECIAL;
359
360 Arg args[8];
361 XtSetArg(args[0], XmNshadowThickness, 1);
362 XtSetArg(args[1], XmNindicatorOn, XmINDICATOR_NONE);
363 XtSetArg(args[2], XmNtraversalOn, FALSE);
364 XtSetArg(args[3], XmNwidth, 120);
365 Widget combobox = XmCreateDropDownList(tb, "toolbar_combobox", args, 4);
366 XtManageChild(combobox);
367 listview->widget = combobox;
368 listview->list = var;
369 listview->getvalue = item->getvalue;
370
371 ui_listview_update(NULL, listview);
372
373 if(item->callback) {
374 // TODO:
375
376 }
377 }
378
379 void add_toolbar_combobox_nv(Widget tb, UiToolbarComboBoxNV *item, UiObject *obj) {
380
381 }

mercurial