UNIXworkcode

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