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