ui/common/toolbar.c

Sun, 07 Dec 2025 16:02:47 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 07 Dec 2025 16:02:47 +0100
changeset 973
aa39a986da78
parent 966
e411ed7c5f10
permissions
-rw-r--r--

fix gtk3 build

/*
 * 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 "menu.h"

#include <stdio.h>
#include <string.h>

static CxMap* toolbar_items;

static CxList* toolbar_defaults[8]; // 0: left 1: center 2: right

static UiToolbarMenuItem* ui_appmenu;


void uic_toolbar_init(void) {
    toolbar_items = cxHashMapCreate(cxDefaultAllocator, CX_STORE_POINTERS, 16);
    for(int i=0;i<8;i++) {
        toolbar_defaults[i] = cxLinkedListCreateSimple(CX_STORE_POINTERS);
    }
}

static char* nl_strdup(const char* str) {
    return str ? strdup(str) : NULL;
}

static UiToolbarItemArgs itemargs_copy(UiToolbarItemArgs *args, size_t *ngroups, size_t *nvstates) {
    UiToolbarItemArgs newargs;
    newargs.label = nl_strdup(args->label);
    newargs.icon = nl_strdup(args->icon);
    newargs.tooltip = nl_strdup(args->tooltip);
    newargs.onclick = args->onclick;
    newargs.onclickdata = args->onclickdata;
    newargs.states = uic_copy_states(args->states, ngroups);
    newargs.visibility_states = uic_copy_states(args->visibility_states, nvstates);
    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, &item->nstates, &item->nvstates);
    cxMapPut(toolbar_items, name, item);
}


static UiToolbarToggleItemArgs toggleitemargs_copy(UiToolbarToggleItemArgs *args, size_t *ngroups, size_t *nvstates) {
    UiToolbarToggleItemArgs newargs;
    newargs.label = nl_strdup(args->label);
    newargs.icon = nl_strdup(args->icon);
    newargs.tooltip = nl_strdup(args->tooltip);
    newargs.varname = nl_strdup(args->varname);
    newargs.onchange = args->onchange;
    newargs.onchangedata = args->onchangedata;
    newargs.states = uic_copy_states(args->states, ngroups);
    newargs.visibility_states = uic_copy_states(args->visibility_states, nvstates);
    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, &item->nstates, &item->nvstates);
    cxMapPut(toolbar_items, name, item);
}

static UiToolbarMenuArgs menuargs_copy(UiToolbarMenuArgs *args, size_t *nvstates) {
    UiToolbarMenuArgs newargs;
    newargs.label = nl_strdup(args->label);
    newargs.icon = nl_strdup(args->icon);
    newargs.tooltip = nl_strdup(args->tooltip);
    newargs.visibility_states = uic_copy_states(args->visibility_states, nvstates);
    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->nvstates);

    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 < 8) {
        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 < 8) {
        cxListAdd(toolbar_defaults[pos], cp);
    } else {
        fprintf(stderr, "Error: illegal toolbar position: %d\n", (int)pos);
    }
}

UiBool uic_toolbar_isenabled(void) {
    size_t size = 0;
    for(int i=0;i<8;i++) {
        size += cxListSize(toolbar_defaults[i]);
    }
    return size > 0;
}

UiToolbarItemI* uic_toolbar_get_item(const char* name) {
    return cxMapGet(toolbar_items, name);
}

UiToolbarMenuItem* uic_get_appmenu(void) {
    return ui_appmenu;
}

mercurial