#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"
#include "container.h"
static UcxList *menus;
static UcxList *current;
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(
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(
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);
QObject::connect(action,
SIGNAL(triggered()), action,
SLOT(trigger()));
}
void UiMenuSeparator::addTo(UiObject* obj, QMenuBar* menubar, QMenu* menu) {
menu->addSeparator();
}
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 {
}
}
UiAction::UiAction(UiObject *obj, QString &label, ui_callback f,
void* userdata) : 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) {
ucx_list_free(current);
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);
UiMenu *cm = (UiMenu*)current->data;
cm->addMenuItem(menu);
current = ucx_list_prepend(current, menu);
}
void ui_submenu_end() {
if(ucx_list_size(current) <
2) {
return;
}
current = ucx_list_remove(current, 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);
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);
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);
}
UiContextMenuHandler::UiContextMenuHandler(QWidget *widget, QMenu* menu) {
this->widget = widget;
this->menu = menu;
}
void UiContextMenuHandler::contextMenuEvent(
const QPoint & pos) {
menu->popup(widget->mapToGlobal(pos));
}
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);
QMenu *menu = new QMenu(widget);
widget->setContextMenuPolicy(Qt::CustomContextMenu);
UiContextMenuHandler *handler = new UiContextMenuHandler(widget, menu);
QObject::connect(
widget,
SIGNAL(customContextMenuRequested(QPoint)),
handler,
SLOT(contextMenuEvent(QPoint)));
ct->menu = menu;
return menu;
}
void ui_contextmenu_popup(
UIMENU menu) {
}
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;
}
UcxList *groups =
NULL;
va_list ap;
va_start(ap, userdata);
int group;
while((group = va_arg(ap,
int)) != -
1) {
ucx_list_append(groups, (
void*)(
intptr_t)group);
}
va_end(ap);
QString str = QString::fromUtf8(label);
UiAction *action = new UiAction(obj, str, f, userdata);
ct->menu->addAction(action);
QObject::connect(action,
SIGNAL(triggered()), action,
SLOT(trigger()));
}
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;
}
UcxList *groups =
NULL;
va_list ap;
va_start(ap, userdata);
int group;
while((group = va_arg(ap,
int)) != -
1) {
ucx_list_append(groups, (
void*)(
intptr_t)group);
}
va_end(ap);
UiStockItem *stockItem = ui_get_stock_item(stockid);
QString str = QString::fromUtf8(stockItem->label);
UiAction *action = new UiAction(obj, str, f, userdata);
action->setIcon(QIcon::fromTheme(stockItem->icon_name));
action->setIconVisibleInMenu(true);
ct->menu->addAction(action);
QObject::connect(action,
SIGNAL(triggered()), action,
SLOT(trigger()));
}