Sun, 22 Sep 2024 19:47:26 +0200
add headerbar (GTK)
/* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * * Copyright 2023 Olaf Wintermann. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include "toolbar.h" #include <string.h> static CxMap* toolbar_items; static CxList* toolbar_defaults[3]; // 0: left 1: center 2: right static UiToolbarMenuItem* ui_appmenu; void uic_toolbar_init(void) { toolbar_items = cxHashMapCreate(cxDefaultAllocator, CX_STORE_POINTERS, 16); toolbar_defaults[0] = cxLinkedListCreateSimple(CX_STORE_POINTERS); toolbar_defaults[1] = cxLinkedListCreateSimple(CX_STORE_POINTERS); toolbar_defaults[2] = cxLinkedListCreateSimple(CX_STORE_POINTERS); } static char* nl_strdup(const char* str) { return str ? strdup(str) : NULL; } static UiToolbarItemArgs itemargs_copy(UiToolbarItemArgs args) { UiToolbarItemArgs newargs; newargs.label = nl_strdup(args.label); newargs.stockid = nl_strdup(args.stockid); newargs.icon = nl_strdup(args.icon); newargs.onclick = args.onclick; newargs.onclickdata = args.onclickdata; return newargs; } void ui_toolbar_item_create(const char* name, UiToolbarItemArgs args) { UiToolbarItem* item = malloc(sizeof(UiToolbarItem)); item->item.type = UI_TOOLBAR_ITEM; item->args = itemargs_copy(args); cxMapPut(toolbar_items, name, item); } static UiToolbarToggleItemArgs toggleitemargs_copy(UiToolbarToggleItemArgs args) { UiToolbarToggleItemArgs newargs; newargs.label = nl_strdup(args.label); newargs.stockid = nl_strdup(args.stockid); newargs.icon = nl_strdup(args.icon); newargs.varname = nl_strdup(args.varname); newargs.onchange = args.onchange; newargs.onchangedata = args.onchangedata; return newargs; } void ui_toolbar_toggleitem_create(const char* name, UiToolbarToggleItemArgs args) { UiToolbarToggleItem* item = malloc(sizeof(UiToolbarToggleItem)); item->item.type = UI_TOOLBAR_TOGGLEITEM; item->args = toggleitemargs_copy(args); cxMapPut(toolbar_items, name, item); } static UiToolbarMenuArgs menuargs_copy(UiToolbarMenuArgs args) { UiToolbarMenuArgs newargs; newargs.label = nl_strdup(args.label); newargs.stockid = nl_strdup(args.stockid); newargs.icon = nl_strdup(args.icon); return newargs; } UIEXPORT void ui_toolbar_menu_create(const char* name, UiToolbarMenuArgs args) { UiToolbarMenuItem* item = malloc(sizeof(UiToolbarMenuItem)); item->item.type = UI_TOOLBAR_MENU; memset(&item->menu, 0, sizeof(UiMenu)); item->args = menuargs_copy(args); item->end = 0; if (!name) { // special appmenu ui_appmenu = item; } else { // toplevel menu cxMapPut(toolbar_items, name, item); } uic_add_menu_to_stack(&item->menu); } CxMap* uic_get_toolbar_items(void) { return toolbar_items; } CxList* uic_get_toolbar_defaults(enum UiToolbarPos pos) { if (pos >= 0 && pos < 3) { return toolbar_defaults[pos]; } return NULL; } void ui_toolbar_add_default(const char* name, enum UiToolbarPos pos) { char* cp = strdup(name); if (pos >= 0 && pos < 3) { cxListAdd(toolbar_defaults[pos], cp); } else { // TODO: error } } UiBool uic_toolbar_isenabled(void) { return toolbar_defaults[0]->size + toolbar_defaults[1]->size + toolbar_defaults[2]->size > 0; } UiToolbarItemI* uic_toolbar_get_item(const char* name) { return cxMapGet(toolbar_items, name); } UiToolbarMenuItem* uic_get_appmenu(void) { return ui_appmenu; }