added context menus (Motif)

Wed, 21 Jan 2015 20:22:27 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Wed, 21 Jan 2015 20:22:27 +0100
changeset 76
3782827378ab
parent 75
efe2f65bea17
child 77
bc0ed99e49c7

added context menus (Motif)

ui/motif/container.c file | annotate | diff | comparison | revisions
ui/motif/container.h file | annotate | diff | comparison | revisions
ui/motif/menu.c file | annotate | diff | comparison | revisions
--- a/ui/motif/container.c	Wed Jan 21 12:29:03 2015 +0100
+++ b/ui/motif/container.c	Wed Jan 21 20:22:27 2015 +0100
@@ -62,6 +62,7 @@
 
 void ui_frame_container_add(UiContainer *ct, Widget widget) {
     ui_reset_layout(ct->layout);
+    ct->current = widget;
 }
 
 
@@ -175,6 +176,7 @@
     bc->prev_widget = widget;
     
     ui_reset_layout(ct->layout);
+    ct->current = widget;
 }
 
 UiContainer* ui_grid_container(UiObject *obj, Widget form) {
@@ -214,6 +216,7 @@
     }
     
     ui_reset_layout(ct->layout);
+    ct->current = widget;
 }
 
 static void ui_grid_resize(Widget widget, XtPointer udata, XtPointer cdata) {
@@ -336,6 +339,7 @@
     
     ui_select_tab(ct->widget, 0);
     ui_reset_layout(ct->layout);
+    ct->current = widget;
 }
 
 UIWIDGET ui_box(UiObject *obj, UiBoxOrientation orientation) {
--- a/ui/motif/container.h	Wed Jan 21 12:29:03 2015 +0100
+++ b/ui/motif/container.h	Wed Jan 21 20:22:27 2015 +0100
@@ -74,6 +74,8 @@
     Widget   (*prepare)(UiContainer*, Arg *, int*, UiBool);
     void     (*add)(UiContainer*, Widget);
     UiLayout layout;
+    Widget   current;
+    Widget   menu;
 };
 
 struct UiBoxContainer {
--- a/ui/motif/menu.c	Wed Jan 21 12:29:03 2015 +0100
+++ b/ui/motif/menu.c	Wed Jan 21 20:22:27 2015 +0100
@@ -34,6 +34,7 @@
 #include "button.h"
 #include "toolkit.h"
 #include "stock.h"
+#include "container.h"
 #include "../common/context.h"
 #include "../ui/window.h"
 
@@ -525,3 +526,91 @@
     e.intval = 0;
     event->callback(&e, event->user_data);    
 }
+
+
+/*
+ * widget menu functions
+ */
+
+static void ui_popup_handler(Widget widget, XtPointer data,  XEvent *event, Boolean *c) {
+    Widget menu = data;
+    XmMenuPosition(menu, (XButtonPressedEvent *)event);
+    XtManageChild(menu);
+    
+    *c = FALSE;
+}
+
+void ui_contextmenu(UiObject *obj) {
+    UiContainer *ct = uic_get_current_container(obj);
+    ui_contextmenu_w(obj, ct->current);
+}
+
+void ui_contextmenu_w(UiObject *obj, UIWIDGET widget) {
+    UiContainer *ct = uic_get_current_container(obj);
+    
+    Widget menu = XmCreatePopupMenu(ct->current, "popup_menu", NULL, 0);
+    ct->menu = menu;
+    
+    XtAddEventHandler(widget, ButtonPressMask, FALSE, ui_popup_handler, 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;
+    }
+    
+    // add groups
+    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);
+    
+    // create menuitem
+    Arg args[4];
+    XmString labelstr = XmStringCreateLocalized(label);
+    XtSetArg(args[0], XmNlabelString, labelstr);
+    
+    Widget item = XmCreatePushButton(ct->menu, "menu_button", args, 1);
+    XtManageChild(item);
+    XmStringFree(labelstr);
+}
+
+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;
+    }
+    
+    // add groups
+    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);
+    
+    // create menuitem
+    UiStockItem *stockItem = ui_get_stock_item(stockid);
+    Arg args[4];
+    XmString labelstr = XmStringCreateLocalized(stockItem->label);
+    XtSetArg(args[0], XmNlabelString, labelstr);
+    
+    Widget item = XmCreatePushButton(ct->menu, "menu_button", args, 1);
+    XtManageChild(item);
+    XmStringFree(labelstr);
+}

mercurial