|
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 <ucx/map.h> |
|
30 #include <inttypes.h> |
|
31 |
|
32 #include "toolbar.h" |
|
33 #include "menu.h" |
|
34 #include "stock.h" |
|
35 |
|
36 static UcxMap *toolbar_items = ucx_map_new(16); |
|
37 static UcxList *defaults; |
|
38 |
|
39 /* ------------------------- UiToolItem ------------------------- */ |
|
40 |
|
41 UiToolItem::UiToolItem(char *label, ui_callback f, void *userdata) { |
|
42 this->label = label; |
|
43 this->image = NULL; |
|
44 this->callback = f; |
|
45 this->userdata = userdata; |
|
46 this->isimportant = false; |
|
47 this->groups = NULL; |
|
48 } |
|
49 |
|
50 void UiToolItem::addGroup(int group) { |
|
51 groups = ucx_list_append(groups, (void*)(intptr_t)group); |
|
52 } |
|
53 |
|
54 void UiToolItem::addTo(UiObject *obj, QToolBar *toolbar) { |
|
55 QString str = QString::fromUtf8(label); |
|
56 UiAction *action = new UiAction(obj, str, callback, userdata); |
|
57 action->setIcon(QIcon::fromTheme(image)); |
|
58 toolbar->addAction(action); |
|
59 QObject::connect(action, SIGNAL(triggered()), action, SLOT(trigger())); |
|
60 } |
|
61 |
|
62 |
|
63 /* ------------------------- UiStockToolItem ------------------------- */ |
|
64 |
|
65 UiStockToolItem::UiStockToolItem(char *stockid, ui_callback f, void *userdata) { |
|
66 this->stockid = stockid; |
|
67 this->callback = f; |
|
68 this->userdata = userdata; |
|
69 this->isimportant = false; |
|
70 this->groups = NULL; |
|
71 } |
|
72 |
|
73 void UiStockToolItem::addGroup(int group) { |
|
74 groups = ucx_list_append(groups, (void*)(intptr_t)group); |
|
75 } |
|
76 |
|
77 void UiStockToolItem::addTo(UiObject *obj, QToolBar *toolbar) { |
|
78 UiStockItem *stockItem = ui_get_stock_item(stockid); |
|
79 QString str = QString::fromUtf8(stockItem->label); |
|
80 |
|
81 UiAction *action = new UiAction(obj, str, callback, userdata); |
|
82 action->setIcon(QIcon::fromTheme(stockItem->icon_name)); |
|
83 toolbar->addAction(action); |
|
84 QObject::connect(action, SIGNAL(triggered()), action, SLOT(trigger())); |
|
85 } |
|
86 |
|
87 |
|
88 |
|
89 void ui_toolitem_vstgr( |
|
90 char *name, |
|
91 char *stockid, |
|
92 int isimportant, |
|
93 ui_callback f, |
|
94 void *userdata, |
|
95 va_list ap) |
|
96 { |
|
97 UiStockToolItem *item = new UiStockToolItem(stockid, f, userdata); |
|
98 item->isimportant = isimportant; |
|
99 |
|
100 // add groups |
|
101 int group; |
|
102 while((group = va_arg(ap, int)) != -1) { |
|
103 item->addGroup(group); |
|
104 } |
|
105 |
|
106 ucx_map_cstr_put(toolbar_items, name, item); |
|
107 } |
|
108 |
|
109 void ui_toolitem_img(char *name, char *label, char *img, ui_callback f, void *udata) { |
|
110 UiToolItem *item = new UiToolItem(label, f, udata); |
|
111 item->image = img; |
|
112 item->isimportant = false; |
|
113 |
|
114 ucx_map_cstr_put(toolbar_items, name, item); |
|
115 } |
|
116 |
|
117 void ui_toolitem(char *name, char *label, ui_callback f, void *udata) { |
|
118 ui_toolitem_img(name, label, NULL, f, udata); |
|
119 } |
|
120 |
|
121 void ui_toolitem_st(char *name, char *stockid, ui_callback f, void *userdata) { |
|
122 ui_toolitem_stgr(name, stockid, f, userdata, -1); |
|
123 } |
|
124 |
|
125 void ui_toolitem_sti(char *name, char *stockid, ui_callback f, void *userdata) { |
|
126 ui_toolitem_stgri(name, stockid, f, userdata, -1); |
|
127 } |
|
128 |
|
129 void ui_toolitem_stgr(char *name, char *stockid, ui_callback f, void *userdata, ...) { |
|
130 va_list ap; |
|
131 va_start(ap, userdata); |
|
132 ui_toolitem_vstgr(name, stockid, 0, f, userdata, ap); |
|
133 va_end(ap); |
|
134 } |
|
135 |
|
136 void ui_toolitem_stgri(char *name, char *stockid, ui_callback f, void *userdata, ...) { |
|
137 va_list ap; |
|
138 va_start(ap, userdata); |
|
139 ui_toolitem_vstgr(name, stockid, 1, f, userdata, ap); |
|
140 va_end(ap); |
|
141 } |
|
142 |
|
143 |
|
144 void ui_toolbar_add_default(char *name) { |
|
145 char *s = strdup(name); |
|
146 defaults = ucx_list_append(defaults, s); |
|
147 } |
|
148 |
|
149 |
|
150 QToolBar* ui_create_toolbar(UiObject *obj) { |
|
151 QToolBar *toolbar = new QToolBar(); |
|
152 |
|
153 UCX_FOREACH(elm, defaults) { |
|
154 UiToolItemI *item = (UiToolItemI*)ucx_map_cstr_get(toolbar_items, (char*)elm->data); |
|
155 if(item) { |
|
156 item->addTo(obj, toolbar); |
|
157 } else if(!strcmp((char*)elm->data, "@separator")) { |
|
158 |
|
159 } else { |
|
160 fprintf(stderr, "UI Error: Unknown toolbar item: %s\n", elm->data); |
|
161 } |
|
162 } |
|
163 |
|
164 return toolbar; |
|
165 } |