1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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];
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
108 ui_appmenu = item;
109 }
else {
110
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
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 }
149