ui/common/toolbar.c

Wed, 11 Oct 2023 23:36:03 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Wed, 11 Oct 2023 23:36:03 +0200
branch
newapi
changeset 209
770b3548906c
parent 207
93b9f502cb88
child 226
4eef1d49f794
permissions
-rw-r--r--

use generated App class (WinUI3)

/*
 * 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;

static UiToolbarMenuItem* ui_appmenu;


void uic_toolbar_init(void) {
	toolbar_items = cxHashMapCreate(cxDefaultAllocator, CX_STORE_POINTERS, 16);
	toolbar_defaults = cxLinkedListCreateSimple(CX_STORE_POINTERS);
}

static char* nl_strdup(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(void) {
	return toolbar_defaults;
}

void ui_toolbar_add_default(const char* name) {
	char* cp = strdup(name);
	cxListAdd(toolbar_defaults, cp);
}

UiBool uic_toolbar_isenabled(void) {
	return toolbar_defaults->size > 0;
}

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

UiToolbarMenuItem* uic_get_appmenu(void) {
	return ui_appmenu;
}

mercurial