Sat, 10 May 2014 15:43:22 +0200
added listview, sidebar and toolbar image button (GTK)
3 | 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> | |
27
77b09bb52ca0
added groups for toolbar items and copy & paste (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
32 | #include <stdarg.h> |
3 | 33 | |
34 | #include "toolbar.h" | |
35 | #include "button.h" | |
36 | #include "stock.h" | |
37 | #include "../../ucx/mempool.h" | |
38 | #include "../common/context.h" | |
39 | ||
40 | static UcxMap *toolbar_items; | |
41 | static UcxList *defaults; | |
42 | ||
43 | void ui_toolbar_init() { | |
44 | toolbar_items = ucx_map_new(16); | |
45 | } | |
46 | ||
27
77b09bb52ca0
added groups for toolbar items and copy & paste (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
47 | void ui_toolitem(char *name, char *label, ui_callback f, void *userdata) { |
3 | 48 | UiToolItem *item = malloc(sizeof(UiToolItem)); |
49 | item->item.add_to = (ui_toolbar_add_f)add_toolitem_widget; | |
50 | item->label = label; | |
51 | item->callback = f; | |
27
77b09bb52ca0
added groups for toolbar items and copy & paste (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
52 | item->userdata = userdata; |
77b09bb52ca0
added groups for toolbar items and copy & paste (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
53 | item->groups = NULL; |
3 | 54 | |
55 | ucx_map_cstr_put(toolbar_items, name, item); | |
56 | } | |
57 | ||
27
77b09bb52ca0
added groups for toolbar items and copy & paste (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
58 | void ui_toolitem_st(char *name, char *stockid, ui_callback f, void *userdata) { |
77b09bb52ca0
added groups for toolbar items and copy & paste (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
59 | ui_toolitem_stgr(name, stockid, f, userdata, -1); |
77b09bb52ca0
added groups for toolbar items and copy & paste (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
60 | } |
77b09bb52ca0
added groups for toolbar items and copy & paste (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
61 | |
77b09bb52ca0
added groups for toolbar items and copy & paste (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
62 | void ui_toolitem_stgr(char *name, char *stockid, ui_callback f, void *userdata, ...) { |
3 | 63 | UiStToolItem *item = malloc(sizeof(UiStToolItem)); |
64 | item->item.add_to = (ui_toolbar_add_f)add_toolitem_st_widget; | |
65 | item->stockid = stockid; | |
66 | item->callback = f; | |
27
77b09bb52ca0
added groups for toolbar items and copy & paste (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
67 | item->userdata = userdata; |
77b09bb52ca0
added groups for toolbar items and copy & paste (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
68 | item->groups = NULL; |
77b09bb52ca0
added groups for toolbar items and copy & paste (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
69 | |
77b09bb52ca0
added groups for toolbar items and copy & paste (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
70 | // add groups |
77b09bb52ca0
added groups for toolbar items and copy & paste (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
71 | va_list ap; |
77b09bb52ca0
added groups for toolbar items and copy & paste (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
72 | va_start(ap, userdata); |
77b09bb52ca0
added groups for toolbar items and copy & paste (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
73 | int group; |
77b09bb52ca0
added groups for toolbar items and copy & paste (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
74 | while((group = va_arg(ap, int)) != -1) { |
77b09bb52ca0
added groups for toolbar items and copy & paste (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
75 | item->groups = ucx_list_append(item->groups, (void*)(intptr_t)group); |
77b09bb52ca0
added groups for toolbar items and copy & paste (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
76 | } |
77b09bb52ca0
added groups for toolbar items and copy & paste (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
77 | va_end(ap); |
3 | 78 | |
79 | ucx_map_cstr_put(toolbar_items, name, item); | |
80 | } | |
81 | ||
82 | void ui_toolbar_add_default(char *name) { | |
83 | char *s = strdup(name); | |
84 | defaults = ucx_list_append(defaults, s); | |
85 | } | |
86 | ||
4
39b9b86ec452
added simple container
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
3
diff
changeset
|
87 | Widget ui_create_toolbar(UiObject *obj, Widget parent) { |
3 | 88 | if(!defaults) { |
4
39b9b86ec452
added simple container
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
3
diff
changeset
|
89 | return NULL; |
3 | 90 | } |
91 | ||
92 | Arg args[8]; | |
93 | XtSetArg(args[0], XmNshadowType, XmSHADOW_ETCHED_OUT); | |
94 | XtSetArg(args[1], XmNshadowThickness, 1); | |
95 | XtSetArg(args[2], XmNtopAttachment, XmATTACH_FORM); | |
96 | XtSetArg(args[3], XmNleftAttachment, XmATTACH_FORM); | |
97 | XtSetArg(args[4], XmNrightAttachment, XmATTACH_FORM); | |
98 | Widget frame = XmCreateFrame(parent, "toolbar_frame", args, 5); | |
99 | ||
100 | XtSetArg(args[0], XmNorientation, XmHORIZONTAL); | |
101 | XtSetArg(args[1], XmNpacking, XmPACK_TIGHT); | |
4
39b9b86ec452
added simple container
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
3
diff
changeset
|
102 | XtSetArg(args[2], XmNspacing, 1); |
39b9b86ec452
added simple container
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
3
diff
changeset
|
103 | Widget toolbar = XmCreateRowColumn (frame, "toolbar", args, 3); |
3 | 104 | |
105 | UCX_FOREACH(elm, defaults) { | |
106 | UiToolItemI *item = ucx_map_cstr_get(toolbar_items, elm->data); | |
107 | if(item) { | |
108 | item->add_to(toolbar, item, obj); | |
109 | } else if(!strcmp(elm->data, "@separator")) { | |
4
39b9b86ec452
added simple container
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
3
diff
changeset
|
110 | // TODO |
3 | 111 | } else { |
112 | fprintf(stderr, "UI Error: Unknown toolbar item: %s\n", elm->data); | |
113 | } | |
114 | } | |
115 | ||
116 | XtManageChild(toolbar); | |
117 | XtManageChild(frame); | |
4
39b9b86ec452
added simple container
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
3
diff
changeset
|
118 | |
39b9b86ec452
added simple container
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
3
diff
changeset
|
119 | return frame; |
3 | 120 | } |
121 | ||
122 | void add_toolitem_widget(Widget parent, UiToolItem *item, UiObject *obj) { | |
123 | Arg args[4]; | |
124 | ||
125 | XmString label = XmStringCreateLocalized(item->label); | |
126 | XtSetArg(args[0], XmNlabelString, label); | |
127 | XtSetArg(args[1], XmNshadowThickness, 1); | |
128 | Widget button = XmCreatePushButton(parent, "toolbar_button", args, 2); | |
129 | ||
130 | XmStringFree(label); | |
131 | ||
132 | if(item->callback) { | |
133 | UiEventData *event = ucx_mempool_malloc( | |
134 | obj->ctx->mempool, | |
135 | sizeof(UiEventData)); | |
136 | event->obj = obj; | |
137 | event->user_data = item->userdata; | |
138 | event->callback = item->callback; | |
139 | XtAddCallback( | |
140 | button, | |
141 | XmNactivateCallback, | |
142 | (XtCallbackProc)ui_push_button_callback, | |
143 | event); | |
144 | } | |
145 | ||
146 | XtManageChild(button); | |
27
77b09bb52ca0
added groups for toolbar items and copy & paste (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
147 | |
77b09bb52ca0
added groups for toolbar items and copy & paste (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
148 | if(item->groups) { |
77b09bb52ca0
added groups for toolbar items and copy & paste (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
149 | uic_add_group_widget(obj->ctx, button, item->groups); |
77b09bb52ca0
added groups for toolbar items and copy & paste (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
150 | } |
3 | 151 | } |
152 | ||
153 | void add_toolitem_st_widget(Widget parent, UiStToolItem *item, UiObject *obj) { | |
154 | Arg args[4]; | |
155 | ||
156 | UiStockItem *stock_item = ui_get_stock_item(item->stockid); | |
157 | ||
158 | XmString label = XmStringCreateLocalized(stock_item->label); | |
159 | XtSetArg(args[0], XmNlabelString, label); | |
160 | XtSetArg(args[1], XmNshadowThickness, 1); | |
161 | Widget button = XmCreatePushButton(parent, "toolbar_button", args, 2); | |
162 | ||
163 | XmStringFree(label); | |
164 | ||
165 | if(item->callback) { | |
166 | UiEventData *event = ucx_mempool_malloc( | |
167 | obj->ctx->mempool, | |
168 | sizeof(UiEventData)); | |
169 | event->obj = obj; | |
170 | event->user_data = item->userdata; | |
171 | event->callback = item->callback; | |
172 | XtAddCallback( | |
173 | button, | |
174 | XmNactivateCallback, | |
175 | (XtCallbackProc)ui_push_button_callback, | |
176 | event); | |
177 | } | |
178 | ||
179 | XtManageChild(button); | |
27
77b09bb52ca0
added groups for toolbar items and copy & paste (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
180 | |
77b09bb52ca0
added groups for toolbar items and copy & paste (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
181 | if(item->groups) { |
77b09bb52ca0
added groups for toolbar items and copy & paste (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
182 | uic_add_group_widget(obj->ctx, button, item->groups); |
77b09bb52ca0
added groups for toolbar items and copy & paste (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
183 | } |
3 | 184 | } |