ui/common/menu.c

changeset 0
2483f517c562
child 3
f154867f54dc
equal deleted inserted replaced
-1:000000000000 0:2483f517c562
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 <stdarg.h>
32 #include <string.h>
33
34 #include <cx/linked_list.h>
35 #include <cx/array_list.h>
36
37 static UiMenu *menus_begin;
38 static UiMenu *menus_end;
39 static CxList *current;
40
41 static void add_menu(UiMenu *menu) {
42 cx_linked_list_add(
43 (void**)&menus_begin,
44 (void**)&menus_end,
45 offsetof(UiMenu, item.prev),
46 offsetof(UiMenu, item.next),
47 menu);
48 }
49
50 static void add_item(UiMenuItemI *item) {
51 UiMenu *menu = cxListAt(current, 0);
52 cx_linked_list_add(
53 (void**)&menu->items_begin,
54 (void**)&menu->items_end,
55 offsetof(UiMenu, item.prev),
56 offsetof(UiMenu, item.next),
57 item);
58 }
59
60 static char* nl_strdup(const char* s) {
61 return s ? strdup(s) : NULL;
62 }
63
64 static int* copy_groups(int* groups, size_t *ngroups) {
65 *ngroups = 0;
66 if (!groups) {
67 return NULL;
68 }
69
70 size_t n;
71 for (n = 0; groups[n] > -1; n++) { }
72
73 if (ngroups > 0) {
74 int* newarray = calloc(n, sizeof(int));
75 memcpy(newarray, groups, n);
76 *ngroups = n;
77 return newarray;
78 }
79 return NULL;
80 }
81
82 void ui_menu_create(const char *label) {
83 if(!current) {
84 current = cxLinkedListCreate(cxDefaultAllocator, NULL, CX_STORE_POINTERS);
85 }
86
87 // create menu
88 UiMenu *menu = malloc(sizeof(UiMenu));
89 menu->item.prev = NULL;
90 menu->item.next = NULL;
91 menu->item.type = UI_MENU;
92
93 menu->label = label;
94 menu->items_begin = NULL;
95 menu->items_end = NULL;
96 menu->parent = NULL;
97
98 menu->end = 0;
99
100 if (current->size == 0) {
101 add_menu(menu);
102 }
103 else {
104 add_item((UiMenuItemI*)menu);
105 }
106 uic_add_menu_to_stack(menu);
107 }
108
109 UIEXPORT void ui_menu_end(void) {
110 cxListRemove(current, 0);
111 }
112
113
114
115 void ui_menuitem_create(UiMenuItemArgs args) {
116 if (!current) {
117 return; // error?
118 }
119
120 UiMenuItem* item = malloc(sizeof(UiMenuItem));
121 item->item.prev = NULL;
122 item->item.next = NULL;
123 item->item.type = UI_MENU_ITEM;
124
125 item->label = nl_strdup(args.label);
126 item->stockid = nl_strdup(args.stockid);
127 item->icon = nl_strdup(args.icon);
128 item->userdata = args.onclickdata;
129 item->callback = args.onclick;
130 item->groups = copy_groups(args.groups, &item->ngroups);
131
132 add_item((UiMenuItemI*)item);
133 }
134
135 void ui_menuseparator() {
136 if(!current) {
137 return;
138 }
139
140 UiMenuItemI *item = malloc(sizeof(UiMenuItemI));
141 item->prev = NULL;
142 item->next = NULL;
143 item->type = UI_MENU_SEPARATOR;
144
145 add_item((UiMenuItemI*)item);
146 }
147
148 void ui_menu_toggleitem_create(UiMenuToggleItemArgs args) {
149 if(!current) {
150 return;
151 }
152
153 UiMenuCheckItem *item = malloc(sizeof(UiMenuCheckItem));
154 item->item.prev = NULL;
155 item->item.next = NULL;
156 item->item.type = UI_MENU_CHECK_ITEM;
157
158 item->label = nl_strdup(args.label);
159 item->stockid = nl_strdup(args.stockid);
160 item->icon = nl_strdup(args.icon);
161 item->varname = nl_strdup(args.varname);
162 item->userdata = args.onchangedata;
163 item->callback = args.onchange;
164 item->groups = copy_groups(args.groups, &item->ngroups);
165
166 add_item((UiMenuItemI*)item);
167 }
168
169 void ui_menu_radioitem_create(UiMenuToggleItemArgs args) {
170 if (!current) {
171 return;
172 }
173
174 UiMenuCheckItem* item = malloc(sizeof(UiMenuCheckItem));
175 item->item.prev = NULL;
176 item->item.next = NULL;
177 item->item.type = UI_MENU_CHECK_ITEM;
178
179 item->label = nl_strdup(args.label);
180 item->stockid = nl_strdup(args.stockid);
181 item->icon = nl_strdup(args.icon);
182 item->varname = nl_strdup(args.varname);
183 item->userdata = args.onchangedata;
184 item->callback = args.onchange;
185 item->groups = copy_groups(args.groups, &item->ngroups);
186
187 add_item((UiMenuItemI*)item);
188 }
189
190 void ui_menu_itemlist_create(UiMenuItemListArgs args) {
191 if(!current) {
192 return;
193 }
194
195 UiMenuItemList*item = malloc(sizeof(UiMenuItemList));
196 item->item.prev = NULL;
197 item->item.next = NULL;
198 item->item.type = UI_MENU_ITEM_LIST;
199 item->callback = args.onselect;
200 item->userdata = args.onselectdata;
201 item->varname = nl_strdup(args.varname);
202
203 add_item((UiMenuItemI*)item);
204 }
205
206 void ui_menu_checkitemlist_create(UiMenuItemListArgs args) {
207 if (!current) {
208 return;
209 }
210
211 UiMenuItemList* item = malloc(sizeof(UiMenuItemList));
212 item->item.prev = NULL;
213 item->item.next = NULL;
214 item->item.type = UI_MENU_ITEM_LIST;
215 item->callback = args.onselect;
216 item->userdata = args.onselectdata;
217 item->varname = nl_strdup(args.varname);
218
219 add_item((UiMenuItemI*)item);
220 }
221
222 void ui_menu_radioitemlist_create(UiMenuItemListArgs args) {
223 if (!current) {
224 return;
225 }
226
227 UiMenuItemList* item = malloc(sizeof(UiMenuItemList));
228 item->item.prev = NULL;
229 item->item.next = NULL;
230 item->item.type = UI_MENU_ITEM_LIST;
231 item->callback = args.onselect;
232 item->userdata = args.onselectdata;
233 item->varname = nl_strdup(args.varname);
234
235 add_item((UiMenuItemI*)item);
236 }
237
238
239 void uic_add_menu_to_stack(UiMenu* menu) {
240 cxListInsert(current, 0, menu);
241 }
242
243 UiMenu* uic_get_menu_list(void) {
244 return menus_begin;
245 }
246
247 UIEXPORT void ui_menu_close(void) {
248 UiMenu* menu = cxListAt(current, 0);
249 menu->end = 1;
250 }
251
252 UIEXPORT int ui_menu_is_open(void) {
253 UiMenu* menu = cxListAt(current, 0);
254 if (menu->end) {
255 ui_menu_end();
256 return 0;
257 }
258 return 1;
259 }

mercurial