ui/gtk/menu.c

Tue, 08 Oct 2024 22:19:24 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Tue, 08 Oct 2024 22:19:24 +0200
branch
newapi
changeset 330
d615aa36c98e
parent 322
22f6dadd9f46
permissions
-rw-r--r--

add tabview value/varname parameter

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2017 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 <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <stdarg.h>

#include "menu.h"
#include "toolkit.h"
#include "../common/context.h"
#include "../common/menu.h"
#include "../ui/properties.h"
#include "../ui/window.h"
#include "container.h"

#include <cx/linked_list.h>
#include <cx/array_list.h>

#if GTK_MAJOR_VERSION <= 3

static ui_menu_add_f createMenuItem[] = {
    /* UI_MENU                 */ add_menu_widget,
    /* UI_MENU_ITEM            */ add_menuitem_widget,
    /* UI_MENU_CHECK_ITEM      */ add_checkitem_widget,
    /* UI_MENU_RADIO_ITEM      */ add_radioitem_widget,
    /* UI_MENU_ITEM_LIST       */ add_menuitem_list_widget,
    /* UI_MENU_CHECKITEM_LIST  */ add_menuitem_list_widget,
    /* UI_MENU_RADIOITEM_LIST  */ add_menuitem_list_widget,
    /* UI_MENU_SEPARATOR       */ add_menuseparator_widget
};

// private menu functions
GtkWidget *ui_create_menubar(UiObject *obj) {
    UiMenu *menus_begin = uic_get_menu_list();
    if(menus_begin == NULL) {
        return NULL;
    }
    
    GtkWidget *mb = gtk_menu_bar_new();
    
    UiMenu *ls = menus_begin;
    while(ls) {
        UiMenu *menu = ls;
        add_menu_widget(mb, 0, &menu->item, obj);
        
        ls = (UiMenu*)ls->item.next;
    }
    
    return mb;
}

void ui_add_menu_items(GtkWidget *parent, int i, UiMenu *menu, UiObject *obj) {
    UiMenuItemI *it = menu->items_begin;
    int index = 0;
    while(it) {
        createMenuItem[it->type](parent, index, it, obj);
        it = it->next;
        index++;
    }
}

void add_menu_widget(GtkWidget *parent, int i, UiMenuItemI *item, UiObject *obj) {
    UiMenu *menu = (UiMenu*)item;
    
    GtkWidget *menu_widget = gtk_menu_new();
    GtkWidget *menu_item = gtk_menu_item_new_with_mnemonic(menu->label);
    gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu_item), menu_widget);
    
    ui_add_menu_items(menu_widget, i, menu, obj);
    
    
    gtk_menu_shell_append(GTK_MENU_SHELL(parent), menu_item);
}

void add_menuitem_widget(GtkWidget *parent, int index, UiMenuItemI *item, UiObject *obj) {
    UiMenuItem *i = (UiMenuItem*)item;
    
    //GtkWidget *widget = gtk_menu_item_new_with_label(i->title);
    GtkWidget *widget = gtk_menu_item_new_with_mnemonic(i->label);
    
    if(i->callback != NULL) {
        UiEventData *event = malloc(sizeof(UiEventData));
        event->obj = obj;
        event->userdata = i->userdata;
        event->callback = i->callback;
        event->value = 0;
        event->customdata = NULL;

        g_signal_connect(
                widget,
                "activate",
                G_CALLBACK(ui_menu_event_wrapper),
                event);
        g_signal_connect(
                widget,
                "destroy",
                G_CALLBACK(ui_destroy_userdata),
                event);
    }
    
    gtk_menu_shell_append(GTK_MENU_SHELL(parent), widget);
    
    if(i->groups) {
        CxList *groups = cxArrayListCreateSimple(sizeof(int), i->ngroups);
        cxListAddArray(groups, i->groups, i->ngroups);
        uic_add_group_widget(obj->ctx, widget, (ui_enablefunc)ui_set_enabled, groups);
        cxListDestroy(groups);
    }
}

/*
void add_menuitem_st_widget(
        GtkWidget *parent,
        int index,
        UiMenuItemI *item,
        UiObject *obj)
{
    UiStMenuItem *i = (UiStMenuItem*)item;
    
    GtkWidget *widget = gtk_image_menu_item_new_from_stock(i->stockid, obj->ctx->accel_group);
    
    if(i->callback != NULL) {
        UiEventData *event = malloc(sizeof(UiEventData));
        event->obj = obj;
        event->userdata = i->userdata;
        event->callback = i->callback;
        event->value = 0;

        g_signal_connect(
                widget,
                "activate",
                G_CALLBACK(ui_menu_event_wrapper),
                event);
        g_signal_connect(
                widget,
                "destroy",
                G_CALLBACK(ui_destroy_userdata),
                event);
    }
    
    gtk_menu_shell_append(GTK_MENU_SHELL(parent), widget);
    
    if(i->groups) {
        uic_add_group_widget(obj->ctx, widget, (ui_enablefunc)ui_set_enabled, i->groups);
    }
}
*/

void add_menuseparator_widget(
        GtkWidget *parent,
        int index,
        UiMenuItemI *item,
        UiObject *obj)
{
    gtk_menu_shell_append(
            GTK_MENU_SHELL(parent),
            gtk_separator_menu_item_new());
}

void add_checkitem_widget(GtkWidget *p, int index, UiMenuItemI *item, UiObject *obj) {
    UiMenuCheckItem *ci = (UiMenuCheckItem*)item;
    GtkWidget *widget = gtk_check_menu_item_new_with_mnemonic(ci->label);
    gtk_menu_shell_append(GTK_MENU_SHELL(p), widget);
    
    if(ci->callback) {
        UiEventData *event = malloc(sizeof(UiEventData));
        event->obj = obj;
        event->userdata = ci->userdata;
        event->callback = ci->callback;
        event->value = 0;
        event->customdata = NULL;
        
        g_signal_connect(
                widget,
                "toggled",
                G_CALLBACK(ui_menu_event_toggled),
                event);
        g_signal_connect(
                widget,
                "destroy",
                G_CALLBACK(ui_destroy_userdata),
                event);
    }
}

void add_radioitem_widget(GtkWidget *p, int index, UiMenuItemI *item, UiObject *obj) {
    // TODO
}

/*
void add_checkitemnv_widget(GtkWidget *p, int index, UiMenuItemI *item, UiObject *obj) {
    UiCheckItemNV *ci = (UiCheckItemNV*)item;
    GtkWidget *widget = gtk_check_menu_item_new_with_mnemonic(ci->label);
    gtk_menu_shell_append(GTK_MENU_SHELL(p), widget);
    
    UiVar *var = uic_create_var(obj->ctx, ci->varname, UI_VAR_INTEGER);
    if(var) {
        UiInteger *value = var->value;
        value->obj = widget;
        value->get = ui_checkitem_get;
        value->set = ui_checkitem_set;
        value = 0;
    } else {
        // TODO: error
    }
}
*/

void add_menuitem_list_widget(GtkWidget *p, int index, UiMenuItemI *item, UiObject *obj) {
    UiMenuItemList *il = (UiMenuItemList*)item;
    const CxAllocator *a = obj->ctx->allocator;
    
    UiActiveMenuItemList *ls = cxMalloc(
            a,
            sizeof(UiActiveMenuItemList));
    
    ls->object = obj;
    ls->menu = GTK_MENU_SHELL(p);
    ls->index = index;
    ls->oldcount = 0;
    ls->getvalue = il->getvalue;
    
    UiVar* var = uic_create_var(ui_global_context(), il->varname, UI_VAR_LIST);
    ls->list = var->value;
    
    ls->callback = il->callback;
    ls->userdata = il->userdata;
    
    ls->list->observers = ui_add_observer(
            ls->list->observers,
            (ui_callback)ui_update_menuitem_list,
            ls);
    
    ui_update_menuitem_list(NULL, ls);
}


void ui_update_menuitem_list(UiEvent *event, UiActiveMenuItemList *list) {    
    // remove old items
    if(list->oldcount > 0) {
        int i = 0;
        GList *mi = gtk_container_get_children(GTK_CONTAINER(list->menu));
        while(mi) {
            if(i >= list->index && i < list->index + list->oldcount) {
                //gtk_container_remove(GTK_CONTAINER(list->menu), mi->data);
                gtk_widget_destroy(mi->data);
            }
            mi = mi->next;
            i++;
        }
    }
    
    void* elm = ui_list_first(list->list);
    if(elm) {
        GtkWidget *widget = gtk_separator_menu_item_new();
        gtk_menu_shell_insert(list->menu, widget, list->index);
        gtk_widget_show(widget);
    }
    
    ui_getvaluefunc getvalue = list->getvalue;
    int i = 1;
    while(elm) {
        char *label = (char*) (getvalue ? getvalue(elm, 0) : elm);
        
        GtkWidget *widget = gtk_menu_item_new_with_label(label);
        gtk_menu_shell_insert(list->menu, widget, list->index + i);
        gtk_widget_show(widget);
        
        if(list->callback) {
            UiEventData *event = malloc(sizeof(UiEventData));
            event->obj = list->object;
            event->userdata = list->userdata;
            event->callback = list->callback;
            event->value = i - 1;
            event->customdata = elm;

            g_signal_connect(
                widget,
                "activate",
                G_CALLBACK(ui_menu_event_wrapper),
                event);
            g_signal_connect(
                widget,
                "destroy",
                G_CALLBACK(ui_destroy_userdata),
                event);
        }
        
        elm = ui_list_next(list->list);
        i++;
    }
    
    list->oldcount = i;
}

void ui_menu_event_wrapper(GtkMenuItem *item, UiEventData *event) {
    UiEvent evt;
    evt.obj = event->obj;
    evt.window = event->obj->window;
    evt.document = event->obj->ctx->document;
    evt.eventdata = event->customdata;
    evt.intval = event->value;
    event->callback(&evt, event->userdata);    
}

void ui_menu_event_toggled(GtkCheckMenuItem *ci, UiEventData *event) {
    UiEvent evt;
    evt.obj = event->obj;
    evt.window = event->obj->window;
    evt.document = event->obj->ctx->document;
    evt.eventdata = NULL;
    evt.intval = gtk_check_menu_item_get_active(ci);
    event->callback(&evt, event->userdata);    
}

int64_t ui_checkitem_get(UiInteger *i) {
    int state = gtk_check_menu_item_get_active(i->obj);
    i->value = state;
    return state;
}

void ui_checkitem_set(UiInteger *i, int64_t value) {
    i->value = value;
    gtk_check_menu_item_set_active(i->obj, value);
}


/*
 * widget menu functions
 */

static gboolean ui_button_press_event(GtkWidget *widget, GdkEvent *event, GtkMenu *menu) {
    if(event->type == GDK_BUTTON_PRESS) {
        GdkEventButton *e = (GdkEventButton*)event;
        if(e->button == 3) {
            gtk_widget_show_all(GTK_WIDGET(menu));
            ui_contextmenu_popup(menu);
            return TRUE;
        }
    }
    return FALSE;
}

UIMENU ui_contextmenu(UiObject *obj) {
    UiContainer *ct = uic_get_current_container(obj);
    return ui_contextmenu_w(obj, ct->current);
}

UIMENU ui_contextmenu_w(UiObject *obj, UIWIDGET widget) {
    UiContainer *ct = uic_get_current_container(obj);
    
    GtkMenu *menu = GTK_MENU(gtk_menu_new());
    g_signal_connect(widget, "button-press-event", (GCallback) ui_button_press_event, menu);
    
    ct->menu = menu;
    return menu;
}

void ui_contextmenu_popup(UIMENU menu) {
#if GTK_MAJOR_VERSION >= 3 && GTK_MINOR_VERSION >= 16
    gtk_menu_popup_at_pointer(menu, NULL);
#else
    gtk_menu_popup(menu, NULL, NULL, 0, 0, 0, gtk_get_current_event_time());
#endif
}

void ui_widget_menuitem(UiObject *obj, char *label, ui_callback f, void *userdata) {
    ui_widget_menuitem_gr(obj, label, f, userdata, -1);
}

void ui_widget_menuitem_gr(UiObject *obj, char *label, ui_callback f, void *userdata, ...) {
    UiContainer *ct = uic_get_current_container(obj);
    if(!ct->menu) {
        return;
    }
    
    // add groups
    CxList *groups = NULL;
    va_list ap;
    va_start(ap, userdata);
    int group;
    while((group = va_arg(ap, int)) != -1) {
        if(!groups) {
            groups = cxArrayListCreate(cxDefaultAllocator, NULL, sizeof(int), 16);
        }
        cxListAdd(groups, &group);
    }
    va_end(ap);
    
    // create menuitem
    GtkWidget *widget = gtk_menu_item_new_with_mnemonic(label);
    gtk_widget_show(widget);
    
    if(f) {
        UiEventData *event = malloc(sizeof(UiEventData));
        event->obj = obj;
        event->userdata = userdata;
        event->callback = f;
        event->value = 0;
        event->customdata = NULL;

        g_signal_connect(
                widget,
                "activate",
                G_CALLBACK(ui_menu_event_wrapper),
                event);
        g_signal_connect(
                widget,
                "destroy",
                G_CALLBACK(ui_destroy_userdata),
                event);
    }
    
    gtk_menu_shell_append(GTK_MENU_SHELL(ct->menu), widget);
    
    if(groups) {
        uic_add_group_widget(obj->ctx, widget, (ui_enablefunc)ui_set_enabled, groups);
        cxListDestroy(groups);
    }
}

void ui_widget_menuitem_st(UiObject *obj, char *stockid, ui_callback f, void *userdata) {
    ui_widget_menuitem_stgr(obj, stockid, f, userdata, -1);
}

void ui_widget_menuitem_stgr(UiObject *obj, char *stockid, ui_callback f, void *userdata, ...) {
    UiContainer *ct = uic_get_current_container(obj);
    if(!ct->menu) {
        return;
    }
    
    // add groups
    CxList *groups = NULL;
    va_list ap;
    va_start(ap, userdata);
    int group;
    while((group = va_arg(ap, int)) != -1) {
        if(!groups) {
            groups = cxArrayListCreate(cxDefaultAllocator, NULL, sizeof(int), 16);
        }
        cxListAdd(groups, &group);
    }
    va_end(ap);
    
    // create menuitem
    GtkWidget *widget = gtk_image_menu_item_new_from_stock(stockid, obj->ctx->accel_group);
    gtk_widget_show(widget);
    
    if(f) {
        UiEventData *event = malloc(sizeof(UiEventData));
        event->obj = obj;
        event->userdata = userdata;
        event->callback = f;
        event->value = 0;
        event->customdata = NULL;

        g_signal_connect(
                widget,
                "activate",
                G_CALLBACK(ui_menu_event_wrapper),
                event);
        g_signal_connect(
                widget,
                "destroy",
                G_CALLBACK(ui_destroy_userdata),
                event);
    }
    
    gtk_menu_shell_append(GTK_MENU_SHELL(ct->menu), widget);
    
    if(groups) {
        uic_add_group_widget(obj->ctx, widget, (ui_enablefunc)ui_set_enabled, groups);
        cxListDestroy(groups);
    }
}

#endif /* GTK_MAJOR_VERSION <= 3 */



#if GTK_MAJOR_VERSION >= 4



static ui_gmenu_add_f createMenuItem[] = {
    /* UI_MENU                 */ ui_gmenu_add_menu,
    /* UI_MENU_ITEM            */ ui_gmenu_add_menuitem,
    /* UI_MENU_CHECK_ITEM      */ ui_gmenu_add_checkitem,
    /* UI_MENU_RADIO_ITEM      */ ui_gmenu_add_radioitem,
    /* UI_MENU_ITEM_LIST       */ ui_gmenu_add_menuitem_list,
    /* UI_MENU_CHECKITEM_LIST  */ ui_gmenu_add_menuitem_list,
    /* UI_MENU_RADIOITEM_LIST  */ ui_gmenu_add_menuitem_list,
    /* UI_MENU_SEPARATOR       */ ui_gmenu_add_menuseparator
};

void ui_gmenu_add_menu_items(GMenu *parent, int i, UiMenu *menu, UiObject *obj) {
    UiMenuItemI *it = menu->items_begin;
    int index = 0;
    int index_section = 0;
    GMenu *section = NULL;
    while(it) {
        if(it->type == UI_MENU_SEPARATOR) {
            section = g_menu_new();
            g_menu_append_section(parent, NULL, G_MENU_MODEL(section));
            index++;
            index_section = 0;
        } else {
            if(section) {
                createMenuItem[it->type](section, index_section++, it, obj);
            } else {
                createMenuItem[it->type](parent, index++, it, obj);
            }
        }
        it = it->next;
    }
}

void ui_gmenu_add_menu(GMenu *parent, int index, UiMenuItemI *item, UiObject *obj) {
    UiMenu *mi = (UiMenu*)item;
    GMenu *menu = g_menu_new();
    ui_gmenu_add_menu_items(menu, 0, mi, obj);
    g_menu_append_submenu(parent, mi->label, G_MENU_MODEL(menu));
}

void ui_gmenu_add_menuitem(GMenu *parent, int index, UiMenuItemI *item, UiObject *obj) {
    UiMenuItem *i = (UiMenuItem*)item;

    GSimpleAction *action = g_simple_action_new(item->id, NULL);
    g_action_map_add_action(obj->ctx->action_map, G_ACTION(action));
    
    if(i->callback != NULL) {
        UiEventData *event = malloc(sizeof(UiEventData));
        event->obj = obj;
        event->userdata = i->userdata;
        event->callback = i->callback;
        event->value = 0;
        event->customdata = NULL;

        g_signal_connect(
                action,
                "activate",
                G_CALLBACK(ui_activate_event_wrapper),
                event);
        g_signal_connect(
                obj->widget,
                "destroy",
                G_CALLBACK(ui_destroy_userdata),
                event);
    }
    
    char action_name[32];
    snprintf(action_name, 32, "win.%s", item->id); 
    g_menu_append(parent, i->label, action_name);
}

void ui_gmenu_add_menuseparator(GMenu *p, int index, UiMenuItemI *item, UiObject *obj) {
    
}

void ui_gmenu_add_checkitem(GMenu *p, int index, UiMenuItemI *item, UiObject *obj) {
    UiMenuCheckItem *checkitem = (UiMenuCheckItem*)item;
    
    // TODO
}

void ui_gmenu_add_radioitem(GMenu *p, int index, UiMenuItemI *item, UiObject *obj) {
    
}

void ui_gmenu_add_menuitem_list(GMenu *p, int index, UiMenuItemI *item, UiObject *obj) {
    UiMenuItemList *il = (UiMenuItemList*)item; 
    
    const CxAllocator *a = obj->ctx->allocator;
    
    UiActiveGMenuItemList *ls = cxMalloc(
            a,
            sizeof(UiActiveGMenuItemList));
    
    ls->object = obj;
    ls->menu = p;
    ls->index = index;
    ls->oldcount = 0;
    ls->getvalue = il->getvalue;
    
    UiVar* var = uic_create_var(ui_global_context(), il->varname, UI_VAR_LIST);
    ls->var = var;
    UiList *list = var->value;
    
    ls->callback = il->callback;
    ls->userdata = il->userdata;
    
    list->observers = ui_add_observer(
            list->observers,
            (ui_callback)ui_update_gmenu_item_list,
            ls);
    
    GSimpleAction *action = g_simple_action_new(item->id, g_variant_type_new("i"));
    g_action_map_add_action(obj->ctx->action_map, G_ACTION(action));
    snprintf(ls->action, 32, "win.%s", item->id);
    
    
    UiEventData *event = malloc(sizeof(UiEventData));
    event->obj = obj;
    event->userdata = il->userdata;
    event->callback = il->callback;
    event->customdata = var;
    event->value = 0;
    
    g_signal_connect(
            action,
            "activate",
            G_CALLBACK(ui_menu_list_item_activate_event_wrapper),
            event);
    g_signal_connect(
            obj->widget,
            "destroy",
            G_CALLBACK(ui_destroy_userdata),
            event);
    
    ui_update_gmenu_item_list(NULL, ls);
}

void ui_activate_event_wrapper(GSimpleAction* self, GVariant* parameter, UiEventData *event) {
    int intval = event->value;
    if(parameter && g_variant_is_of_type(parameter, G_VARIANT_TYPE_INT32)) {
        intval = g_variant_get_int32(parameter);
    }
    
    UiEvent evt;
    evt.obj = event->obj;
    evt.window = event->obj->window;
    evt.document = event->obj->ctx->document;
    evt.eventdata = event->customdata;
    evt.intval = intval;
    event->callback(&evt, event->userdata);    
}

void ui_menu_list_item_activate_event_wrapper(GSimpleAction* self, GVariant* parameter, UiEventData *event) {
    int index = g_variant_get_int32(parameter);
    UiVar *var = event->customdata;
    UiList *list = var->value;
    
    UiEvent evt;
    evt.obj = event->obj;
    evt.window = event->obj->window;
    evt.document = event->obj->ctx->document;
    evt.eventdata = ui_list_get(list, index);
    evt.intval = index;
    event->callback(&evt, event->userdata);    
    
}

void ui_update_gmenu_item_list(UiEvent *event, UiActiveGMenuItemList *list) {
    // remove old items
    for(int i=0;i<list->oldcount;i++) {
        g_menu_remove(list->menu, list->index);
    }
    UiList *ls = list->var->value;
    
    // add list items
    ui_getvaluefunc getvalue = list->getvalue;
    int i = 0;
    void* elm = ui_list_first(ls);
    while(elm) {
        char *label = (char*) (getvalue ? getvalue(elm, 0) : elm);
        
        GMenuItem *item = g_menu_item_new(label, NULL);
        GVariant *v = g_variant_new("i", i);
        g_menu_item_set_action_and_target_value(item, list->action, v);
        g_menu_insert_item(list->menu, list->index+i, item);
        
        elm = ui_list_next(ls);
        i++;
    }
    
    list->oldcount = i;
}

#endif

mercurial