Sun, 24 Aug 2014 13:31:07 +0200
added Qt window and menu implementation
/* * 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::addTo(UiObject *obj, QMenuBar *menubar, QMenu *menu) { QString str = QString::fromUtf8(label); UiAction *action = new UiAction(obj, str, callback, userdata); menu->addAction(action); //UiEventWrapper *ev = new UiEventWrapper(callback, userdata); 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); menu->addAction(action); //UiEventWrapper *ev = new UiEventWrapper(callback, userdata); QObject::connect(action, SIGNAL(triggered()), action, SLOT(trigger())); } /* -------------------------- 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() { UiEvent e; e.obj = obj; e.window = obj->window; e.document = obj->ctx->document; e.eventdata = NULL; 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_add_menus(UiObject *obj, QMainWindow *window) { QMenuBar *mb = window->menuBar(); UCX_FOREACH(elm, menus) { UiMenu *menu = (UiMenu*)elm->data; menu->addTo(obj, mb, NULL); } }