ui/qt/menu.cpp

Mon, 12 Jan 2015 12:03:05 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Mon, 12 Jan 2015 12:03:05 +0100
changeset 72
a00b46d92c54
parent 71
3e021c5f18a0
child 73
473acef47ddd
permissions
-rw-r--r--

added list view (Qt)

/*
 * 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 <inttypes.h>
#include <QMenuBar>
#include <QAction>

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

static UcxList *menus;
static UcxList *current;

/* -------------------------- UiMenu -------------------------- */

UiMenu::UiMenu(char* label) {
    this->items = NULL;
    this->label = label;
}

void UiMenu::addMenuItem(UiMenuItemI* item) {
    items = ucx_list_append(items, item);
}

void UiMenu::addTo(UiObject *obj, QMenuBar *menubar, QMenu *menu) {
    QMenu *m = NULL;
    if(menubar) {
        m = menubar->addMenu(label);
    } else {
        m = menu->addMenu(label);
    }
    
    UCX_FOREACH(elm, items) {
        UiMenuItemI *item = (UiMenuItemI*)elm->data;
        item->addTo(obj, NULL, m);
    }
}


/* -------------------------- UiMenuItem -------------------------- */

UiMenuItem::UiMenuItem(char* label, ui_callback f, void* userdata) {
    this->label = label;
    this->callback = f;
    this->userdata = userdata;
    this->groups = NULL;
}

void UiMenuItem::addGroup(int group) {
    groups = ucx_list_append(groups, (void*)(intptr_t)group);
}

void UiMenuItem::setCheckable(bool c) {
    checkable = c;
}

void UiMenuItem::addTo(UiObject *obj, QMenuBar *menubar, QMenu *menu) {
    QString str = QString::fromUtf8(label);
    UiAction *action = new UiAction(obj, str, callback, userdata);
    action->setCheckable(checkable);
    if(checkable) {
        action->setChecked(false);
    }
    menu->addAction(action);
    QObject::connect(action, SIGNAL(triggered()), action, SLOT(trigger()));
}


/* -------------------------- UiStMenuItem -------------------------- */

UiStMenuItem::UiStMenuItem(char* stockid, ui_callback f, void* userdata) {
    this->stockid = stockid;
    this->callback = f;
    this->userdata = userdata;
    this->groups = NULL;
}

void UiStMenuItem::addGroup(int group) {
    groups = ucx_list_append(groups, (void*)(intptr_t)group);
}

void UiStMenuItem::addTo(UiObject *obj, QMenuBar *menubar, QMenu *menu) {
    UiStockItem *stockItem = ui_get_stock_item(stockid);
    
    QString str = QString::fromUtf8(stockItem->label);
    UiAction *action = new UiAction(obj, str, callback, userdata);
    action->setIcon(QIcon::fromTheme(stockItem->icon_name));
    action->setIconVisibleInMenu(true);
    menu->addAction(action);
    //UiEventWrapper *ev = new UiEventWrapper(callback, userdata);
    QObject::connect(action, SIGNAL(triggered()), action, SLOT(trigger()));
}


/* -------------------------- UiMenuSeparator -------------------------- */

void UiMenuSeparator::addTo(UiObject* obj, QMenuBar* menubar, QMenu* menu) {
    menu->addSeparator();
}


/* -------------------------- UiCheckItemNV -------------------------- */

UiCheckItemNV::UiCheckItemNV(char* label, char* varname) {
    this->label = label;
    this->varname = varname;
}

void UiCheckItemNV::addTo(UiObject* obj, QMenuBar* menubar, QMenu* menu) {
    QString str = QString::fromUtf8(label);
    UiAction *action = new UiAction(obj, str, NULL, NULL);
    action->setCheckable(true);
    menu->addAction(action);
    QObject::connect(action, SIGNAL(triggered()), action, SLOT(trigger()));
    
    UiVar *var = uic_connect_var(obj->ctx, varname, UI_VAR_INTEGER);
    if(var) {
        UiInteger *value = (UiInteger*)var->value;
        action->setChecked(value->value);
        value->obj = action;
        value->get = ui_checkitem_get;
        value->set = ui_checkitem_set;
        value = 0;
    } else {
        // TODO: error
    }
}


/* -------------------------- UiAction -------------------------- */

UiAction::UiAction(UiObject *obj, QString &label, ui_callback f, void* userdata) : QAction(label, NULL) {
    //QAction(label, NULL);
    this->obj = obj;
    this->callback = f;
    this->userdata = userdata;
}

void UiAction::trigger() {
    if(!callback) {
        return;
    }
    
    UiEvent e;
    e.obj = obj;
    e.window = obj->window;
    e.document = obj->ctx->document;
    e.eventdata = NULL;
    
    if(isCheckable()) {
        e.intval = isChecked();
    } else {
        e.intval = 0;
    }
    
    callback(&e, userdata);
}


void ui_menu(char *label) {
    // free current menu hierarchy
    ucx_list_free(current);
    
    // create menu
    UiMenu *menu = new UiMenu(label);
    
    current = ucx_list_prepend(NULL, menu);
    menus = ucx_list_append(menus, menu);
}

void ui_submenu(char *label) {
    UiMenu *menu = new UiMenu(label);
    
    // add submenu to current menu
    UiMenu *cm = (UiMenu*)current->data;
    cm->addMenuItem(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) {
    ui_menuitem_gr(label, f, userdata, -1);
}

void ui_menuitem_st(char *stockid, ui_callback f, void *userdata) {
    ui_menuitem_stgr(stockid, f, userdata, -1);
}

void ui_menuitem_gr(char *label, ui_callback f, void *userdata, ...) {
    if(!current) {
        return;
    }
    
    UiMenuItem *item = new UiMenuItem(label, f, userdata);
    
    // add groups
    va_list ap;
    va_start(ap, userdata);
    int group;
    while((group = va_arg(ap, int)) != -1) {
        item->addGroup(group);
    }
    va_end(ap);
    
    UiMenu *cm = (UiMenu*)current->data;
    cm->addMenuItem(item);
}

void ui_menuitem_stgr(char *stockid, ui_callback f, void *userdata, ...) {
    if(!current) {
        return;
    }
    
    UiStMenuItem *item = new UiStMenuItem(stockid, f, userdata);
    
    // add groups
    va_list ap;
    va_start(ap, userdata);
    int group;
    while((group = va_arg(ap, int)) != -1) {
        item->addGroup(group);
    }
    va_end(ap);
    
    UiMenu *cm = (UiMenu*)current->data;
    cm->addMenuItem(item);
}

void ui_menuseparator() {
    if(!current) {
        return;
    }
    
    UiMenuSeparator *item = new UiMenuSeparator();
    UiMenu *cm = (UiMenu*)current->data;
    cm->addMenuItem(item);
}

void ui_checkitem(char *label, ui_callback f, void *userdata) {
    if(!current) {
        return;
    }
    
    UiMenuItem *item = new UiMenuItem(label, f, userdata);
    item->setCheckable(true);
    
    UiMenu *cm = (UiMenu*)current->data;
    cm->addMenuItem(item);
}

void ui_checkitem_nv(char *label, char *vname) {
    if(!current) {
        return;
    }
    
    UiCheckItemNV *item = new UiCheckItemNV(label, vname);
    
    UiMenu *cm = (UiMenu*)current->data;
    cm->addMenuItem(item);
}

void ui_menuitem_list(UiList *items, ui_callback f, void *userdata) {
    
}

void ui_add_menus(UiObject *obj, QMainWindow *window) {
    QMenuBar *mb = window->menuBar();
    
    UCX_FOREACH(elm, menus) {
        UiMenu *menu = (UiMenu*)elm->data;
        menu->addTo(obj, mb, NULL);        
    }
}

int ui_checkitem_get(UiInteger *i) {
    QAction *action = (QAction*)i->obj;
    i->value = action->isChecked();
    return i->value;
}

void ui_checkitem_set(UiInteger *i, int value) {
    QAction *action = (QAction*)i->obj;
    i->value = value;
    action->setChecked(value);
}

mercurial