diff -r 3d801e8dda3a -r 3e021c5f18a0 ui/qt/menu.cpp --- a/ui/qt/menu.cpp Sun Jan 11 15:29:38 2015 +0100 +++ b/ui/qt/menu.cpp Sun Jan 11 16:33:28 2015 +0100 @@ -81,11 +81,18 @@ 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); - //UiEventWrapper *ev = new UiEventWrapper(callback, userdata); QObject::connect(action, SIGNAL(triggered()), action, SLOT(trigger())); } @@ -116,6 +123,41 @@ } +/* -------------------------- 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) { @@ -126,12 +168,22 @@ } void UiAction::trigger() { + if(!callback) { + return; + } + UiEvent e; e.obj = obj; e.window = obj->window; e.document = obj->ctx->document; e.eventdata = NULL; - e.intval = 0; + + if(isCheckable()) { + e.intval = isChecked(); + } else { + e.intval = 0; + } + callback(&e, userdata); } @@ -215,8 +267,42 @@ 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(); @@ -226,3 +312,15 @@ 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); +}