|
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 <inttypes.h> |
|
32 #include <QMenuBar> |
|
33 #include <QAction> |
|
34 |
|
35 #include "menu.h" |
|
36 #include "toolkit.h" |
|
37 #include "../common/context.h" |
|
38 #include "../ui/properties.h" |
|
39 #include "../ui/window.h" |
|
40 #include "stock.h" |
|
41 |
|
42 static UcxList *menus; |
|
43 static UcxList *current; |
|
44 |
|
45 /* -------------------------- UiMenu -------------------------- */ |
|
46 |
|
47 UiMenu::UiMenu(char* label) { |
|
48 this->items = NULL; |
|
49 this->label = label; |
|
50 } |
|
51 |
|
52 void UiMenu::addMenuItem(UiMenuItemI* item) { |
|
53 items = ucx_list_append(items, item); |
|
54 } |
|
55 |
|
56 void UiMenu::addTo(UiObject *obj, QMenuBar *menubar, QMenu *menu) { |
|
57 QMenu *m = NULL; |
|
58 if(menubar) { |
|
59 m = menubar->addMenu(label); |
|
60 } else { |
|
61 m = menu->addMenu(label); |
|
62 } |
|
63 |
|
64 UCX_FOREACH(elm, items) { |
|
65 UiMenuItemI *item = (UiMenuItemI*)elm->data; |
|
66 item->addTo(obj, NULL, m); |
|
67 } |
|
68 } |
|
69 |
|
70 |
|
71 /* -------------------------- UiMenuItem -------------------------- */ |
|
72 |
|
73 UiMenuItem::UiMenuItem(char* label, ui_callback f, void* userdata) { |
|
74 this->label = label; |
|
75 this->callback = f; |
|
76 this->userdata = userdata; |
|
77 this->groups = NULL; |
|
78 } |
|
79 |
|
80 void UiMenuItem::addGroup(int group) { |
|
81 groups = ucx_list_append(groups, (void*)(intptr_t)group); |
|
82 } |
|
83 |
|
84 void UiMenuItem::addTo(UiObject *obj, QMenuBar *menubar, QMenu *menu) { |
|
85 QString str = QString::fromUtf8(label); |
|
86 UiAction *action = new UiAction(obj, str, callback, userdata); |
|
87 menu->addAction(action); |
|
88 //UiEventWrapper *ev = new UiEventWrapper(callback, userdata); |
|
89 QObject::connect(action, SIGNAL(triggered()), action, SLOT(trigger())); |
|
90 } |
|
91 |
|
92 |
|
93 /* -------------------------- UiStMenuItem -------------------------- */ |
|
94 |
|
95 UiStMenuItem::UiStMenuItem(char* stockid, ui_callback f, void* userdata) { |
|
96 this->stockid = stockid; |
|
97 this->callback = f; |
|
98 this->userdata = userdata; |
|
99 this->groups = NULL; |
|
100 } |
|
101 |
|
102 void UiStMenuItem::addGroup(int group) { |
|
103 groups = ucx_list_append(groups, (void*)(intptr_t)group); |
|
104 } |
|
105 |
|
106 void UiStMenuItem::addTo(UiObject *obj, QMenuBar *menubar, QMenu *menu) { |
|
107 UiStockItem *stockItem = ui_get_stock_item(stockid); |
|
108 |
|
109 QString str = QString::fromUtf8(stockItem->label); |
|
110 UiAction *action = new UiAction(obj, str, callback, userdata); |
|
111 menu->addAction(action); |
|
112 //UiEventWrapper *ev = new UiEventWrapper(callback, userdata); |
|
113 QObject::connect(action, SIGNAL(triggered()), action, SLOT(trigger())); |
|
114 } |
|
115 |
|
116 |
|
117 /* -------------------------- UiAction -------------------------- */ |
|
118 |
|
119 UiAction::UiAction(UiObject *obj, QString &label, ui_callback f, void* userdata) : QAction(label, NULL) { |
|
120 //QAction(label, NULL); |
|
121 this->obj = obj; |
|
122 this->callback = f; |
|
123 this->userdata = userdata; |
|
124 } |
|
125 |
|
126 void UiAction::trigger() { |
|
127 UiEvent e; |
|
128 e.obj = obj; |
|
129 e.window = obj->window; |
|
130 e.document = obj->ctx->document; |
|
131 e.eventdata = NULL; |
|
132 e.intval = 0; |
|
133 callback(&e, userdata); |
|
134 } |
|
135 |
|
136 |
|
137 void ui_menu(char *label) { |
|
138 // free current menu hierarchy |
|
139 ucx_list_free(current); |
|
140 |
|
141 // create menu |
|
142 UiMenu *menu = new UiMenu(label); |
|
143 |
|
144 current = ucx_list_prepend(NULL, menu); |
|
145 menus = ucx_list_append(menus, menu); |
|
146 } |
|
147 |
|
148 void ui_submenu(char *label) { |
|
149 UiMenu *menu = new UiMenu(label); |
|
150 |
|
151 // add submenu to current menu |
|
152 UiMenu *cm = (UiMenu*)current->data; |
|
153 cm->addMenuItem(menu); |
|
154 |
|
155 // set the submenu to current menu |
|
156 current = ucx_list_prepend(current, menu); |
|
157 } |
|
158 |
|
159 void ui_submenu_end() { |
|
160 if(ucx_list_size(current) < 2) { |
|
161 return; |
|
162 } |
|
163 current = ucx_list_remove(current, current); |
|
164 //UcxList *c = current; |
|
165 } |
|
166 |
|
167 |
|
168 void ui_menuitem(char *label, ui_callback f, void *userdata) { |
|
169 ui_menuitem_gr(label, f, userdata, -1); |
|
170 } |
|
171 |
|
172 void ui_menuitem_st(char *stockid, ui_callback f, void *userdata) { |
|
173 ui_menuitem_stgr(stockid, f, userdata, -1); |
|
174 } |
|
175 |
|
176 void ui_menuitem_gr(char *label, ui_callback f, void *userdata, ...) { |
|
177 if(!current) { |
|
178 return; |
|
179 } |
|
180 |
|
181 UiMenuItem *item = new UiMenuItem(label, f, userdata); |
|
182 |
|
183 // add groups |
|
184 va_list ap; |
|
185 va_start(ap, userdata); |
|
186 int group; |
|
187 while((group = va_arg(ap, int)) != -1) { |
|
188 item->addGroup(group); |
|
189 } |
|
190 va_end(ap); |
|
191 |
|
192 UiMenu *cm = (UiMenu*)current->data; |
|
193 cm->addMenuItem(item); |
|
194 } |
|
195 |
|
196 void ui_menuitem_stgr(char *stockid, ui_callback f, void *userdata, ...) { |
|
197 if(!current) { |
|
198 return; |
|
199 } |
|
200 |
|
201 UiStMenuItem *item = new UiStMenuItem(stockid, f, userdata); |
|
202 |
|
203 // add groups |
|
204 va_list ap; |
|
205 va_start(ap, userdata); |
|
206 int group; |
|
207 while((group = va_arg(ap, int)) != -1) { |
|
208 item->addGroup(group); |
|
209 } |
|
210 va_end(ap); |
|
211 |
|
212 UiMenu *cm = (UiMenu*)current->data; |
|
213 cm->addMenuItem(item); |
|
214 } |
|
215 |
|
216 |
|
217 |
|
218 |
|
219 void ui_add_menus(UiObject *obj, QMainWindow *window) { |
|
220 QMenuBar *mb = window->menuBar(); |
|
221 |
|
222 UCX_FOREACH(elm, menus) { |
|
223 UiMenu *menu = (UiMenu*)elm->data; |
|
224 menu->addTo(obj, mb, NULL); |
|
225 } |
|
226 } |