ui/common/toolbar.c

branch
newapi
changeset 205
b1ac0dd1d38b
child 207
93b9f502cb88
equal deleted inserted replaced
204:4a258d47f964 205:b1ac0dd1d38b
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;
36
37 void uic_toolbar_init(void) {
38 toolbar_items = cxHashMapCreate(cxDefaultAllocator, CX_STORE_POINTERS, 16);
39 toolbar_defaults = cxLinkedListCreateSimple(CX_STORE_POINTERS);
40 }
41
42 static char* nl_strdup(char* str) {
43 return str ? strdup(str) : NULL;
44 }
45
46 static UiToolbarItemArgs itemargs_copy(UiToolbarItemArgs args) {
47 UiToolbarItemArgs newargs;
48 newargs.label = nl_strdup(args.label);
49 newargs.stockid = nl_strdup(args.stockid);
50 newargs.icon = nl_strdup(args.icon);
51 newargs.onclick = args.onclick;
52 newargs.onclickdata = args.onclickdata;
53 return newargs;
54 }
55
56 void ui_toolbar_item_create(const char* name, UiToolbarItemArgs args) {
57 UiToolbarItem* item = malloc(sizeof(UiToolbarItem));
58 item->item.type = UI_TOOLBAR_ITEM;
59 item->args = itemargs_copy(args);
60 cxMapPut(toolbar_items, name, item);
61 }
62
63
64 static UiToolbarToggleItemArgs toggleitemargs_copy(UiToolbarToggleItemArgs args) {
65 UiToolbarToggleItemArgs newargs;
66 newargs.label = nl_strdup(args.label);
67 newargs.stockid = nl_strdup(args.stockid);
68 newargs.icon = nl_strdup(args.icon);
69 newargs.varname = nl_strdup(args.varname);
70 newargs.onchange = args.onchange;
71 newargs.onchangedata = args.onchangedata;
72 return newargs;
73 }
74
75 void ui_toolbar_toggleitem_create(const char* name, UiToolbarToggleItemArgs args) {
76 UiToolbarToggleItem* item = malloc(sizeof(UiToolbarToggleItem));
77 item->item.type = UI_TOOLBAR_TOGGLEITEM;
78 item->args = toggleitemargs_copy(args);
79 cxMapPut(toolbar_items, name, item);
80 }
81
82
83 CxMap* uic_get_toolbar_items(void) {
84 return toolbar_items;
85 }
86
87 CxList* uic_get_toolbar_defaults(void) {
88 return toolbar_defaults;
89 }
90
91 void ui_toolbar_add_default(const char* name) {
92 char* cp = strdup(name);
93 cxListAdd(toolbar_defaults, cp);
94 }
95
96 UiBool uic_toolbar_isenabled(void) {
97 return toolbar_defaults->size > 0;
98 }
99
100 UiToolbarItemI* uic_toolbar_get_item(const char* name) {
101 return cxMapGet(toolbar_items, name);
102 }

mercurial