|
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> |
|
31 #include <string.h> |
|
32 |
|
33 #include "toolbar.h" |
|
34 #include "button.h" |
|
35 #include "stock.h" |
|
36 #include "../../ucx/mempool.h" |
|
37 #include "../common/context.h" |
|
38 |
|
39 static UcxMap *toolbar_items; |
|
40 static UcxList *defaults; |
|
41 |
|
42 void ui_toolbar_init() { |
|
43 toolbar_items = ucx_map_new(16); |
|
44 } |
|
45 |
|
46 void ui_toolitem(char *name, char *label, ui_callback f, void *udata) { |
|
47 UiToolItem *item = malloc(sizeof(UiToolItem)); |
|
48 item->item.add_to = (ui_toolbar_add_f)add_toolitem_widget; |
|
49 item->label = label; |
|
50 item->callback = f; |
|
51 item->userdata = udata; |
|
52 |
|
53 ucx_map_cstr_put(toolbar_items, name, item); |
|
54 } |
|
55 |
|
56 void ui_toolitem_st(char *name, char *stockid, ui_callback f, void *ud) { |
|
57 UiStToolItem *item = malloc(sizeof(UiStToolItem)); |
|
58 item->item.add_to = (ui_toolbar_add_f)add_toolitem_st_widget; |
|
59 item->stockid = stockid; |
|
60 item->callback = f; |
|
61 item->userdata = ud; |
|
62 |
|
63 ucx_map_cstr_put(toolbar_items, name, item); |
|
64 } |
|
65 |
|
66 void ui_toolbar_add_default(char *name) { |
|
67 char *s = strdup(name); |
|
68 defaults = ucx_list_append(defaults, s); |
|
69 } |
|
70 |
|
71 void ui_create_toolbar(UiObject *obj, Widget parent) { |
|
72 if(!defaults) { |
|
73 return; |
|
74 } |
|
75 |
|
76 Arg args[8]; |
|
77 XtSetArg(args[0], XmNshadowType, XmSHADOW_ETCHED_OUT); |
|
78 XtSetArg(args[1], XmNshadowThickness, 1); |
|
79 XtSetArg(args[2], XmNtopAttachment, XmATTACH_FORM); |
|
80 XtSetArg(args[3], XmNleftAttachment, XmATTACH_FORM); |
|
81 XtSetArg(args[4], XmNrightAttachment, XmATTACH_FORM); |
|
82 Widget frame = XmCreateFrame(parent, "toolbar_frame", args, 5); |
|
83 |
|
84 XtSetArg(args[0], XmNorientation, XmHORIZONTAL); |
|
85 XtSetArg(args[1], XmNpacking, XmPACK_TIGHT); |
|
86 Widget toolbar = XmCreateRowColumn (frame, "toolbar", args, 2); |
|
87 |
|
88 UCX_FOREACH(elm, defaults) { |
|
89 UiToolItemI *item = ucx_map_cstr_get(toolbar_items, elm->data); |
|
90 if(item) { |
|
91 item->add_to(toolbar, item, obj); |
|
92 } else if(!strcmp(elm->data, "@separator")) { |
|
93 |
|
94 } else { |
|
95 fprintf(stderr, "UI Error: Unknown toolbar item: %s\n", elm->data); |
|
96 } |
|
97 } |
|
98 |
|
99 XtManageChild(toolbar); |
|
100 XtManageChild(frame); |
|
101 } |
|
102 |
|
103 void add_toolitem_widget(Widget parent, UiToolItem *item, UiObject *obj) { |
|
104 Arg args[4]; |
|
105 |
|
106 XmString label = XmStringCreateLocalized(item->label); |
|
107 XtSetArg(args[0], XmNlabelString, label); |
|
108 XtSetArg(args[1], XmNshadowThickness, 1); |
|
109 Widget button = XmCreatePushButton(parent, "toolbar_button", args, 2); |
|
110 |
|
111 XmStringFree(label); |
|
112 |
|
113 if(item->callback) { |
|
114 UiEventData *event = ucx_mempool_malloc( |
|
115 obj->ctx->mempool, |
|
116 sizeof(UiEventData)); |
|
117 event->obj = obj; |
|
118 event->user_data = item->userdata; |
|
119 event->callback = item->callback; |
|
120 XtAddCallback( |
|
121 button, |
|
122 XmNactivateCallback, |
|
123 (XtCallbackProc)ui_push_button_callback, |
|
124 event); |
|
125 } |
|
126 |
|
127 XtManageChild(button); |
|
128 } |
|
129 |
|
130 void add_toolitem_st_widget(Widget parent, UiStToolItem *item, UiObject *obj) { |
|
131 Arg args[4]; |
|
132 |
|
133 UiStockItem *stock_item = ui_get_stock_item(item->stockid); |
|
134 |
|
135 XmString label = XmStringCreateLocalized(stock_item->label); |
|
136 XtSetArg(args[0], XmNlabelString, label); |
|
137 XtSetArg(args[1], XmNshadowThickness, 1); |
|
138 Widget button = XmCreatePushButton(parent, "toolbar_button", args, 2); |
|
139 |
|
140 XmStringFree(label); |
|
141 |
|
142 if(item->callback) { |
|
143 UiEventData *event = ucx_mempool_malloc( |
|
144 obj->ctx->mempool, |
|
145 sizeof(UiEventData)); |
|
146 event->obj = obj; |
|
147 event->user_data = item->userdata; |
|
148 event->callback = item->callback; |
|
149 XtAddCallback( |
|
150 button, |
|
151 XmNactivateCallback, |
|
152 (XtCallbackProc)ui_push_button_callback, |
|
153 event); |
|
154 } |
|
155 |
|
156 XtManageChild(button); |
|
157 } |