|
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 "toolbar.h" |
|
30 #include "menu.h" |
|
31 |
|
32 #include <string.h> |
|
33 |
|
34 static CxMap* toolbar_items; |
|
35 |
|
36 static CxList* toolbar_defaults[3]; // 0: left 1: center 2: right |
|
37 |
|
38 static UiToolbarMenuItem* ui_appmenu; |
|
39 |
|
40 |
|
41 void uic_toolbar_init(void) { |
|
42 toolbar_items = cxHashMapCreate(cxDefaultAllocator, CX_STORE_POINTERS, 16); |
|
43 toolbar_defaults[0] = cxLinkedListCreateSimple(CX_STORE_POINTERS); |
|
44 toolbar_defaults[1] = cxLinkedListCreateSimple(CX_STORE_POINTERS); |
|
45 toolbar_defaults[2] = cxLinkedListCreateSimple(CX_STORE_POINTERS); |
|
46 } |
|
47 |
|
48 static char* nl_strdup(const char* str) { |
|
49 return str ? strdup(str) : NULL; |
|
50 } |
|
51 |
|
52 static UiToolbarItemArgs itemargs_copy(UiToolbarItemArgs args, size_t *ngroups) { |
|
53 UiToolbarItemArgs newargs; |
|
54 newargs.label = nl_strdup(args.label); |
|
55 newargs.stockid = nl_strdup(args.stockid); |
|
56 newargs.icon = nl_strdup(args.icon); |
|
57 newargs.onclick = args.onclick; |
|
58 newargs.onclickdata = args.onclickdata; |
|
59 newargs.groups = uic_copy_groups(args.groups, ngroups); |
|
60 return newargs; |
|
61 } |
|
62 |
|
63 void ui_toolbar_item_create(const char* name, UiToolbarItemArgs args) { |
|
64 UiToolbarItem* item = malloc(sizeof(UiToolbarItem)); |
|
65 item->item.type = UI_TOOLBAR_ITEM; |
|
66 item->args = itemargs_copy(args, &item->ngroups); |
|
67 cxMapPut(toolbar_items, name, item); |
|
68 } |
|
69 |
|
70 |
|
71 static UiToolbarToggleItemArgs toggleitemargs_copy(UiToolbarToggleItemArgs args, size_t *ngroups) { |
|
72 UiToolbarToggleItemArgs newargs; |
|
73 newargs.label = nl_strdup(args.label); |
|
74 newargs.stockid = nl_strdup(args.stockid); |
|
75 newargs.icon = nl_strdup(args.icon); |
|
76 newargs.varname = nl_strdup(args.varname); |
|
77 newargs.onchange = args.onchange; |
|
78 newargs.onchangedata = args.onchangedata; |
|
79 newargs.groups = uic_copy_groups(args.groups, ngroups); |
|
80 return newargs; |
|
81 } |
|
82 |
|
83 void ui_toolbar_toggleitem_create(const char* name, UiToolbarToggleItemArgs args) { |
|
84 UiToolbarToggleItem* item = malloc(sizeof(UiToolbarToggleItem)); |
|
85 item->item.type = UI_TOOLBAR_TOGGLEITEM; |
|
86 item->args = toggleitemargs_copy(args, &item->ngroups); |
|
87 cxMapPut(toolbar_items, name, item); |
|
88 } |
|
89 |
|
90 static UiToolbarMenuArgs menuargs_copy(UiToolbarMenuArgs args) { |
|
91 UiToolbarMenuArgs newargs; |
|
92 newargs.label = nl_strdup(args.label); |
|
93 newargs.stockid = nl_strdup(args.stockid); |
|
94 newargs.icon = nl_strdup(args.icon); |
|
95 return newargs; |
|
96 } |
|
97 |
|
98 UIEXPORT void ui_toolbar_menu_create(const char* name, UiToolbarMenuArgs args) { |
|
99 UiToolbarMenuItem* item = malloc(sizeof(UiToolbarMenuItem)); |
|
100 item->item.type = UI_TOOLBAR_MENU; |
|
101 memset(&item->menu, 0, sizeof(UiMenu)); |
|
102 item->args = menuargs_copy(args); |
|
103 |
|
104 item->end = 0; |
|
105 |
|
106 if (!name) { |
|
107 // special appmenu |
|
108 ui_appmenu = item; |
|
109 } else { |
|
110 // toplevel menu |
|
111 cxMapPut(toolbar_items, name, item); |
|
112 } |
|
113 |
|
114 uic_add_menu_to_stack(&item->menu); |
|
115 } |
|
116 |
|
117 |
|
118 CxMap* uic_get_toolbar_items(void) { |
|
119 return toolbar_items; |
|
120 } |
|
121 |
|
122 CxList* uic_get_toolbar_defaults(enum UiToolbarPos pos) { |
|
123 if (pos >= 0 && pos < 3) { |
|
124 return toolbar_defaults[pos]; |
|
125 } |
|
126 return NULL; |
|
127 } |
|
128 |
|
129 void ui_toolbar_add_default(const char* name, enum UiToolbarPos pos) { |
|
130 char* cp = strdup(name); |
|
131 if (pos >= 0 && pos < 3) { |
|
132 cxListAdd(toolbar_defaults[pos], cp); |
|
133 } else { |
|
134 // TODO: error |
|
135 } |
|
136 } |
|
137 |
|
138 UiBool uic_toolbar_isenabled(void) { |
|
139 return cxListSize(toolbar_defaults[0]) + cxListSize(toolbar_defaults[1]) + cxListSize(toolbar_defaults[2]) > 0; |
|
140 } |
|
141 |
|
142 UiToolbarItemI* uic_toolbar_get_item(const char* name) { |
|
143 return cxMapGet(toolbar_items, name); |
|
144 } |
|
145 |
|
146 UiToolbarMenuItem* uic_get_appmenu(void) { |
|
147 return ui_appmenu; |
|
148 } |