1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
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
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
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 }
166