|
1 /* |
|
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
|
3 * |
|
4 * Copyright 2023 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 "menu.h" |
|
30 |
|
31 #include <cx/linked_list.h> |
|
32 #include <cx/array_list.h> |
|
33 |
|
34 static UiMenu *menus_begin; |
|
35 static UiMenu *menus_end; |
|
36 static CxList *current; |
|
37 |
|
38 static void add_menu(UiMenu *menu) { |
|
39 cx_linked_list_add( |
|
40 (void**)&menus_begin, |
|
41 (void**)&menus_end, |
|
42 offsetof(UiMenu, item.prev), |
|
43 offsetof(UiMenu, item.next), |
|
44 menu); |
|
45 } |
|
46 |
|
47 static void add_item(UiMenuItemI *item) { |
|
48 UiMenu *menu = cxListAt(current, 0); |
|
49 cx_linked_list_add( |
|
50 (void**)&menu->items_begin, |
|
51 (void**)&menu->items_end, |
|
52 offsetof(UiMenu, item.prev), |
|
53 offsetof(UiMenu, item.next), |
|
54 item); |
|
55 } |
|
56 |
|
57 void ui_menu(char *label) { |
|
58 if(!current) { |
|
59 current = cxLinkedListCreate(cxDefaultAllocator, NULL, CX_STORE_POINTERS); |
|
60 } else { |
|
61 // free current menu hierarchy |
|
62 cxListClear(current); |
|
63 } |
|
64 |
|
65 // create menu |
|
66 UiMenu *menu = malloc(sizeof(UiMenu)); |
|
67 menu->item.prev = NULL; |
|
68 menu->item.next = NULL; |
|
69 menu->item.type = UI_MENU; |
|
70 |
|
71 menu->label = label; |
|
72 menu->items_begin = NULL; |
|
73 menu->items_end = NULL; |
|
74 menu->parent = NULL; |
|
75 |
|
76 add_menu(menu); |
|
77 cxListAdd(current, menu); |
|
78 } |
|
79 |
|
80 void ui_submenu(char *label) { |
|
81 UiMenu *menu = malloc(sizeof(UiMenu)); |
|
82 menu->item.prev = NULL; |
|
83 menu->item.next = NULL; |
|
84 menu->item.type = UI_MENU_SUBMENU; |
|
85 |
|
86 menu->label = label; |
|
87 menu->items_begin = NULL; |
|
88 menu->items_end = NULL; |
|
89 menu->parent = NULL; |
|
90 |
|
91 // add submenu to current menu |
|
92 add_item((UiMenuItemI*)menu); |
|
93 |
|
94 // set the submenu to current menu |
|
95 cxListInsert(current, 0, menu); |
|
96 } |
|
97 |
|
98 void ui_submenu_end() { |
|
99 if(current->size < 2) { |
|
100 return; |
|
101 } |
|
102 cxListRemove(current, 0); |
|
103 } |
|
104 |
|
105 void ui_menuitem(char *label, ui_callback f, void *userdata) { |
|
106 ui_menuitem_gr(label, f, userdata, -1); |
|
107 } |
|
108 |
|
109 void ui_menuitem_st(char *stockid, ui_callback f, void *userdata) { |
|
110 ui_menuitem_stgr(stockid, f, userdata, -1); |
|
111 } |
|
112 |
|
113 void ui_menuitem_gr(char *label, ui_callback f, void *userdata, ...) { |
|
114 if(!current) { |
|
115 return; |
|
116 } |
|
117 |
|
118 UiMenuItem *item = malloc(sizeof(UiMenuItem)); |
|
119 item->item.prev = NULL; |
|
120 item->item.next = NULL; |
|
121 item->item.type = UI_MENU_ITEM; |
|
122 |
|
123 item->label = label; |
|
124 item->userdata = userdata; |
|
125 item->callback = f; |
|
126 item->groups = NULL; |
|
127 |
|
128 // add groups |
|
129 va_list ap; |
|
130 va_start(ap, userdata); |
|
131 int group; |
|
132 while((group = va_arg(ap, int)) != -1) { |
|
133 if(!item->groups) { |
|
134 item->groups = cxArrayListCreate(cxDefaultAllocator, NULL, sizeof(int), 8); |
|
135 } |
|
136 cxListAdd(item->groups, &group); |
|
137 } |
|
138 va_end(ap); |
|
139 |
|
140 add_item((UiMenuItemI*)item); |
|
141 } |
|
142 |
|
143 void ui_menuitem_stgr(char *stockid, ui_callback f, void *userdata, ...) { |
|
144 if(!current) { |
|
145 return; |
|
146 } |
|
147 |
|
148 UiStMenuItem *item = malloc(sizeof(UiStMenuItem)); |
|
149 item->item.prev = NULL; |
|
150 item->item.next = NULL; |
|
151 item->item.type = UI_MENU_STOCK_ITEM; |
|
152 |
|
153 item->stockid = stockid; |
|
154 item->userdata = userdata; |
|
155 item->callback = f; |
|
156 item->groups = NULL; |
|
157 |
|
158 // add groups |
|
159 va_list ap; |
|
160 va_start(ap, userdata); |
|
161 int group; |
|
162 while((group = va_arg(ap, int)) != -1) { |
|
163 if(!item->groups) { |
|
164 item->groups = cxArrayListCreate(cxDefaultAllocator, NULL, sizeof(int), 8); |
|
165 } |
|
166 cxListAdd(item->groups, &group); |
|
167 } |
|
168 va_end(ap); |
|
169 |
|
170 add_item((UiMenuItemI*)item); |
|
171 } |
|
172 |
|
173 void ui_menuseparator() { |
|
174 if(!current) { |
|
175 return; |
|
176 } |
|
177 |
|
178 UiMenuItemI *item = malloc(sizeof(UiMenuItemI)); |
|
179 item->prev = NULL; |
|
180 item->next = NULL; |
|
181 item->type = UI_MENU_SEPARATOR; |
|
182 |
|
183 add_item((UiMenuItemI*)item); |
|
184 } |
|
185 |
|
186 void ui_checkitem(char *label, ui_callback f, void *userdata) { |
|
187 if(!current) { |
|
188 return; |
|
189 } |
|
190 |
|
191 UiCheckItem *item = malloc(sizeof(UiCheckItem)); |
|
192 item->item.prev = NULL; |
|
193 item->item.next = NULL; |
|
194 item->item.type = UI_MENU_CHECK_ITEM; |
|
195 item->label = label; |
|
196 item->callback = f; |
|
197 item->userdata = userdata; |
|
198 |
|
199 add_item((UiMenuItemI*)item); |
|
200 } |
|
201 |
|
202 void ui_checkitem_nv(char *label, char *vname) { |
|
203 if(!current) { |
|
204 return; |
|
205 } |
|
206 |
|
207 UiCheckItemNV *item = malloc(sizeof(UiCheckItemNV)); |
|
208 item->item.prev = NULL; |
|
209 item->item.next = NULL; |
|
210 item->item.type = UI_MENU_CHECK_ITEM_NV; |
|
211 item->varname = vname; |
|
212 item->label = label; |
|
213 |
|
214 add_item((UiMenuItemI*)item); |
|
215 } |
|
216 |
|
217 void ui_menuitem_list(UiList *items, ui_callback f, void *userdata) { |
|
218 if(!current) { |
|
219 return; |
|
220 } |
|
221 |
|
222 UiMenuItemList *item = malloc(sizeof(UiMenuItemList)); |
|
223 item->item.prev = NULL; |
|
224 item->item.next = NULL; |
|
225 item->item.type = UI_MENU_ITEM_LIST; |
|
226 item->callback = f; |
|
227 item->userdata = userdata; |
|
228 item->list = items; |
|
229 |
|
230 add_item((UiMenuItemI*)item); |
|
231 } |
|
232 |
|
233 void ui_menuitem_list_nv(const char *varname, ui_callback f, void *userdata) { |
|
234 if(!current) { |
|
235 return; |
|
236 } |
|
237 |
|
238 UiMenuItemListNV *item = malloc(sizeof(UiMenuItemListNV)); |
|
239 item->item.prev = NULL; |
|
240 item->item.next = NULL; |
|
241 item->item.type = UI_MENU_ITEM_LIST; |
|
242 item->callback = f; |
|
243 item->userdata = userdata; |
|
244 item->varname = varname; |
|
245 |
|
246 add_item((UiMenuItemI*)item); |
|
247 } |
|
248 |
|
249 |
|
250 |
|
251 UiMenu* uic_get_menu_list(void) { |
|
252 return menus_begin; |
|
253 } |