ui/gtk/menu.c

Sat, 07 Dec 2013 12:14:59 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sat, 07 Dec 2013 12:14:59 +0100
changeset 0
1f419bd32da1
child 2
eeb50c534497
permissions
-rw-r--r--

added files

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2014 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 "menu.h"
#include "toolkit.h"
#include "../common/context.h"
#include "../ui/window.h"

UcxList *menus;
UcxList *current;

void ui_menu(char *label) {
    // free current menu hierarchy
    ucx_list_free(current);
    
    // create menu
    UiMenu *menu = malloc(sizeof(UiMenu));
    menu->item.add_to = (ui_menu_add_f)add_menu_widget;
    
    menu->label  = label;
    menu->items  = NULL;
    menu->parent = NULL;    
    
    current = ucx_list_prepend(NULL, menu);
    menus = ucx_list_append(menus, menu);
    
}

void ui_submenu(char *label) {
    UiMenu *menu = malloc(sizeof(UiMenu));
    menu->item.add_to = (ui_menu_add_f)add_menu_widget;
    
    menu->label  = label;
    menu->items  = NULL;
    menu->parent = NULL;
    
    // add submenu to current menu
    UiMenu *cm = current->data;
    cm->items = ucx_list_append(cm->items, menu);
    
    // set the submenu to current menu
    current = ucx_list_prepend(current, menu);
}

void ui_submenu_end() {
    if(ucx_list_size(current) < 2) {
        return;
    }
    current = ucx_list_remove(current, current);
    UcxList *c = current;
}

void ui_menuitem(char *label, ui_callback f, void *userdata) {
    if(!current) {
        return;
    }
    
    UiMenuItem *item = malloc(sizeof(UiMenuItem));
    item->item.add_to = (ui_menu_add_f)add_menuitem_widget;
    
    item->label = label;
    item->userdata = userdata;
    item->callback = f;
    
    UiMenu *cm = current->data;
    cm->items = ucx_list_append(cm->items, item);
}

void ui_menuseparator() {
    if(!current) {
        return;
    }
    
    UiMenuItemI  *item = malloc(sizeof(UiMenuItemI));
    item->add_to = (ui_menu_add_f)add_menuseparator_widget;
    
    UiMenu *cm = current->data;
    cm->items = ucx_list_append(cm->items, item);
}

void ui_checkitem(char *label, ui_callback f, void *userdata) {
    if(!current) {
        return;
    }
    
    UiCheckItem *item = malloc(sizeof(UiCheckItem));
    item->item.add_to = (ui_menu_add_f)add_checkitem_widget;
    item->label = label;
    item->callback = f;
    item->userdata = userdata;
    
    UiMenu *cm = current->data;
    cm->items = ucx_list_append(cm->items, item);
}

void ui_checkitem_nv(char *label, char *vname) {
    if(!current) {
        return;
    }
    
    UiCheckItemNV *item = malloc(sizeof(UiCheckItemNV));
    item->item.add_to = (ui_menu_add_f)add_checkitemnv_widget;
    item->varname = vname;
    item->label = label;
    
    UiMenu *cm = current->data;
    cm->items = ucx_list_append(cm->items, item);
}

// private menu functions
GtkWidget *ui_create_menubar(UiObject *obj) {
    if(menus == NULL) {
        return NULL;
    }
    
    GtkWidget *mb = gtk_menu_bar_new();
    
    UcxList *ls = menus;
    while(ls) {
        UiMenu *menu = ls->data;
        menu->item.add_to(mb, &menu->item, obj);
        
        ls = ls->next;
    }
    
    return mb;
}

void add_menu_widget(GtkWidget *parent, UiMenuItemI *item, UiObject *obj) {
    UiMenu *menu = (UiMenu*)item;
    
    GtkWidget *menu_widget = gtk_menu_new();
    GtkWidget *menu_item = gtk_menu_item_new_with_label(menu->label);
    gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu_item), menu_widget);
    
    UcxList *ls = menu->items;
    while(ls) {
        UiMenuItemI *i = ls->data;
        i->add_to(menu_widget, i, obj);
        
        ls = ls->next;
    }
    
    gtk_menu_shell_append(GTK_MENU_SHELL(parent), menu_item);
}

void add_menuitem_widget(GtkWidget *parent, 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->user_data = i->userdata;
        event->callback = i->callback;

        g_signal_connect(
                widget,
                "activate",
                G_CALLBACK(ui_menu_event_wrapper),
                event);
    }
    
    gtk_menu_shell_append(GTK_MENU_SHELL(parent), widget);
}

void add_menuitem_st_widget(
        GtkWidget *parent,
        UiMenuItemI *item,
        UiObject *obj)
{
    UiStMenuItem *i = (UiStMenuItem*)item;
    
    GtkWidget *widget = gtk_image_menu_item_new_from_stock(i->stockid, NULL);
    
    if(i->callback != NULL) {
        UiEventData *event = malloc(sizeof(UiEventData));
        event->obj = obj;
        event->user_data = i->userdata;
        event->callback = i->callback;

        g_signal_connect(
                widget,
                "activate",
                G_CALLBACK(ui_menu_event_wrapper),
                event);
    }
    
    gtk_menu_shell_append(GTK_MENU_SHELL(parent), widget);
}

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

void add_checkitem_widget(GtkWidget *p, UiMenuItemI *item, UiObject *obj) {
    UiCheckItem *ci = (UiCheckItem*)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->user_data = ci->userdata;
        event->callback = ci->callback;

        g_signal_connect(
                widget,
                "toggled",
                G_CALLBACK(ui_menu_event_toggled),
                event);
    }
}

void add_checkitemnv_widget(GtkWidget *p, 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_getvar(obj, ci->varname);
    if(!var) {
        ui_window_addint(obj, ci->varname);
        var = uic_getvar(obj, ci->varname);
    }
    if(var->type == 1) {
        UiInteger *value = var->value;
        value->obj = widget;
        value->get = ui_checkitem_get;
        value->set = ui_checkitem_set;
        value = 0;
    } else {
        // TODO: error message
    }
}



void ui_menu_event_wrapper(GtkMenuItem *item, UiEventData *event) {
    UiEvent evt;
    evt.obj = event->obj;
    evt.window = event->obj->window;
    evt.document = NULL;
    evt.intval = 0;
    event->callback(&evt, event->user_data);    
}

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

int 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, int value) {
    i->value = value;
    gtk_check_menu_item_set_active(i->obj, value);
}

mercurial