ui/qt/menu.cpp

changeset 73
473acef47ddd
parent 71
3e021c5f18a0
child 115
102fc0b8fe3e
equal deleted inserted replaced
72:a00b46d92c54 73:473acef47ddd
36 #include "toolkit.h" 36 #include "toolkit.h"
37 #include "../common/context.h" 37 #include "../common/context.h"
38 #include "../ui/properties.h" 38 #include "../ui/properties.h"
39 #include "../ui/window.h" 39 #include "../ui/window.h"
40 #include "stock.h" 40 #include "stock.h"
41 #include "container.h"
41 42
42 static UcxList *menus; 43 static UcxList *menus;
43 static UcxList *current; 44 static UcxList *current;
44 45
45 /* -------------------------- UiMenu -------------------------- */ 46 /* -------------------------- UiMenu -------------------------- */
322 void ui_checkitem_set(UiInteger *i, int value) { 323 void ui_checkitem_set(UiInteger *i, int value) {
323 QAction *action = (QAction*)i->obj; 324 QAction *action = (QAction*)i->obj;
324 i->value = value; 325 i->value = value;
325 action->setChecked(value); 326 action->setChecked(value);
326 } 327 }
328
329
330 /*
331 * widget menu functions
332 */
333
334 UiContextMenuHandler::UiContextMenuHandler(QWidget *widget, QMenu* menu) {
335 this->widget = widget;
336 this->menu = menu;
337 }
338
339 void UiContextMenuHandler::contextMenuEvent(const QPoint & pos) {
340 menu->popup(widget->mapToGlobal(pos));
341 }
342
343 void ui_contextmenu(UiObject *obj) {
344 UiContainer *ct = uic_get_current_container(obj);
345 ui_contextmenu_w(obj, ct->current);
346 }
347
348 void ui_contextmenu_w(UiObject *obj, UIWIDGET widget) {
349 UiContainer *ct = uic_get_current_container(obj);
350
351 QMenu *menu = new QMenu(widget);
352 widget->setContextMenuPolicy(Qt::CustomContextMenu);
353
354 UiContextMenuHandler *handler = new UiContextMenuHandler(widget, menu);
355 QObject::connect(
356 widget,
357 SIGNAL(customContextMenuRequested(QPoint)),
358 handler,
359 SLOT(contextMenuEvent(QPoint)));
360
361 ct->menu = menu;
362 }
363
364 void ui_widget_menuitem(UiObject *obj, char *label, ui_callback f, void *userdata) {
365 ui_widget_menuitem_gr(obj, label, f, userdata, -1);
366 }
367
368 void ui_widget_menuitem_gr(UiObject *obj, char *label, ui_callback f, void *userdata, ...) {
369 UiContainer *ct = uic_get_current_container(obj);
370 if(!ct->menu) {
371 return;
372 }
373
374 // add groups
375 UcxList *groups = NULL;
376 va_list ap;
377 va_start(ap, userdata);
378 int group;
379 while((group = va_arg(ap, int)) != -1) {
380 ucx_list_append(groups, (void*)(intptr_t)group);
381 }
382 va_end(ap);
383
384 // create menuitem
385 QString str = QString::fromUtf8(label);
386 UiAction *action = new UiAction(obj, str, f, userdata);
387 ct->menu->addAction(action);
388 QObject::connect(action, SIGNAL(triggered()), action, SLOT(trigger()));
389 }
390
391 void ui_widget_menuitem_st(UiObject *obj, char *stockid, ui_callback f, void *userdata) {
392 ui_widget_menuitem_stgr(obj, stockid, f, userdata, -1);
393 }
394
395 void ui_widget_menuitem_stgr(UiObject *obj, char *stockid, ui_callback f, void *userdata, ...) {
396 UiContainer *ct = uic_get_current_container(obj);
397 if(!ct->menu) {
398 return;
399 }
400
401 // add groups
402 UcxList *groups = NULL;
403 va_list ap;
404 va_start(ap, userdata);
405 int group;
406 while((group = va_arg(ap, int)) != -1) {
407 ucx_list_append(groups, (void*)(intptr_t)group);
408 }
409 va_end(ap);
410
411 // create menuitem
412 UiStockItem *stockItem = ui_get_stock_item(stockid);
413
414 QString str = QString::fromUtf8(stockItem->label);
415 UiAction *action = new UiAction(obj, str, f, userdata);
416 action->setIcon(QIcon::fromTheme(stockItem->icon_name));
417 action->setIconVisibleInMenu(true);
418 ct->menu->addAction(action);
419 QObject::connect(action, SIGNAL(triggered()), action, SLOT(trigger()));
420 }

mercurial