improved context menus

Mon, 25 Jan 2016 16:36:31 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Mon, 25 Jan 2016 16:36:31 +0100
changeset 115
102fc0b8fe3e
parent 114
909fe96e5659
child 116
480354705c2f

improved context menus

application/main.c file | annotate | diff | comparison | revisions
ui/gtk/draw_cairo.c file | annotate | diff | comparison | revisions
ui/gtk/graphics.c file | annotate | diff | comparison | revisions
ui/gtk/menu.c file | annotate | diff | comparison | revisions
ui/gtk/toolkit.c file | annotate | diff | comparison | revisions
ui/motif/graphics.c file | annotate | diff | comparison | revisions
ui/motif/menu.c file | annotate | diff | comparison | revisions
ui/qt/graphics.cpp file | annotate | diff | comparison | revisions
ui/qt/menu.cpp file | annotate | diff | comparison | revisions
ui/ui/graphics.h file | annotate | diff | comparison | revisions
ui/ui/menu.h file | annotate | diff | comparison | revisions
ui/ui/toolkit.h file | annotate | diff | comparison | revisions
--- a/application/main.c	Sun Jan 24 22:20:47 2016 +0100
+++ b/application/main.c	Mon Jan 25 16:36:31 2016 +0100
@@ -76,17 +76,27 @@
     
     ui_graphics_color(g, 255, 255, 255);
     ui_draw_text(g, 50, 50, text);
+    ui_draw_line(g, 50, 55 + h, 50+w, 55 +h);
+    ui_draw_line(g, 50, 55 + h, 50, 75 +h);
     
     ui_draw_line(g, 0, 120, width, 120);
     ui_draw_line(g, 200, 0, 200, height);
     
+    ui_draw_rect(g, 250, 250, 50, 50, FALSE);
+    
     //ui_text_free(text);
     //*/
 }
 
+UIMENU ctxmenu;
 void click(UiEvent *event, void *data) {
     UiMouseEvent *me = event->eventdata;
     printf("click[%d](%d,%d)\n", me->type, me->x, me->y);
+    
+    if(me->button == 1) {
+        ui_contextmenu_popup(ctxmenu);
+    }
+    
 }
 
 void window_close(UiEvent *event, void *data) {
@@ -113,11 +123,17 @@
     
     UiObject *obj = ui_window("Test", NULL);
     ui_context_closefunc(obj->ctx, window_close, NULL);
+    
     UIWIDGET w = ui_drawingarea(obj, draw, NULL);
-    //ui_mouse_handler(obj, w, click, NULL);
+    ctxmenu = ui_contextmenu_w(obj, w);
+    ui_widget_menuitem(obj, "Test1", NULL, NULL);
+    ui_widget_menuitem(obj, "Test2", NULL, NULL);
+    ui_widget_menuitem(obj, "Test3", NULL, NULL);
+    ui_widget_menuitem(obj, "Test4", NULL, NULL);
+    ui_drawingarea_mousehandler(obj, w, click, NULL);
     
 /*
-    ui_vbox_sp(obj, 8, 4);
+    ui_vbox_sp(obj, 8, 4);   
     ui_button(obj, "Button", NULL, NULL);
     ui_button(obj, "Button", NULL, NULL);
     ui_button(obj, "Button", NULL, NULL);
--- a/ui/gtk/draw_cairo.c	Sun Jan 24 22:20:47 2016 +0100
+++ b/ui/gtk/draw_cairo.c	Mon Jan 25 16:36:31 2016 +0100
@@ -108,15 +108,15 @@
 void ui_draw_line(UiGraphics *g, int x1, int y1, int x2, int y2) {
     UiCairoGraphics *gr = (UiCairoGraphics*)g;
     cairo_set_line_width(gr->cr, 1);
-    cairo_move_to(gr->cr, x1, y1);
-    cairo_line_to(gr->cr, x2, y2);
+    cairo_move_to(gr->cr, (double)x1 + 0.5, (double)y1 + 0.5);
+    cairo_line_to(gr->cr, (double)x2 + 0.5, (double)y2 + 0.5);
     cairo_stroke(gr->cr);
 }
 
 void ui_draw_rect(UiGraphics *g, int x, int y, int w, int h, int fill) {
     UiCairoGraphics *gr = (UiCairoGraphics*)g;
     cairo_set_line_width(gr->cr, 1);
-    cairo_rectangle(gr->cr, x, y, w, h);
+    cairo_rectangle(gr->cr, x + 0.5, y + 0.5 , w, h);
     if(fill) {
         cairo_fill(gr->cr);
     } else {
@@ -126,7 +126,6 @@
 
 void ui_draw_text(UiGraphics *g, int x, int y, UiTextLayout *text) {
     UiCairoGraphics *gr = (UiCairoGraphics*)g; 
-    //gdk_draw_layout(gr->widget->window, gr->gc, x, y, text->layout);
     cairo_move_to(gr->cr, x, y);
     pango_cairo_show_layout(gr->cr, text->layout);
 }
--- a/ui/gtk/graphics.c	Sun Jan 24 22:20:47 2016 +0100
+++ b/ui/gtk/graphics.c	Mon Jan 25 16:36:31 2016 +0100
@@ -50,6 +50,57 @@
     return widget;
 }
 
+
+static gboolean widget_button_pressed(
+        GtkWidget *widget,
+        GdkEvent *event,
+        gpointer userdata)
+{
+    UiEventData *eventdata = userdata;
+    
+    UiMouseEvent me;
+    me.x = (int)event->button.x;
+    me.y = (int)event->button.y;
+    
+    int exec = 0;
+    if(event->button.type == GDK_BUTTON_PRESS) {
+        exec = 1;
+        me.type = UI_PRESS;
+    } else if(event->button.type == GDK_2BUTTON_PRESS) {
+        exec = 1;
+        me.type = UI_PRESS2;
+    }
+    
+    if(exec) {
+        UiEvent e;
+        e.obj = eventdata->obj;
+        e.window = eventdata->obj->window;
+        e.document = eventdata->obj->ctx->document;
+        e.eventdata = &me;
+        e.intval = 0;
+        eventdata->callback(&e, eventdata->userdata);
+    }
+    return TRUE;
+}
+
+void ui_drawingarea_mousehandler(UiObject *obj, UIWIDGET widget, ui_callback f, void *u) {
+    gtk_widget_set_events(widget, GDK_BUTTON_PRESS_MASK);
+    if(f) {
+        UiEventData *event = malloc(sizeof(UiEventData));
+        event->obj = obj;
+        event->callback = f;
+        event->userdata = u;
+        
+        g_signal_connect(G_OBJECT(widget),
+                "button-press-event",
+                G_CALLBACK(widget_button_pressed),
+                event);
+    } else {
+         // TODO: warning
+    }
+}
+
+
 // text layout
 UiTextLayout* ui_text(UiGraphics *g) {
     UiTextLayout *layout = malloc(sizeof(UiTextLayout));
--- a/ui/gtk/menu.c	Sun Jan 24 22:20:47 2016 +0100
+++ b/ui/gtk/menu.c	Mon Jan 25 16:36:31 2016 +0100
@@ -485,18 +485,23 @@
     return FALSE;
 }
 
-void ui_contextmenu(UiObject *obj) {
+UIMENU ui_contextmenu(UiObject *obj) {
     UiContainer *ct = uic_get_current_container(obj);
-    ui_contextmenu_w(obj, ct->current);
+    return ui_contextmenu_w(obj, ct->current);
 }
 
-void ui_contextmenu_w(UiObject *obj, UIWIDGET widget) {
+UIMENU ui_contextmenu_w(UiObject *obj, UIWIDGET widget) {
     UiContainer *ct = uic_get_current_container(obj);
     
     GtkMenu *menu = GTK_MENU(gtk_menu_new());
     g_signal_connect(widget, "button-press-event", (GCallback) ui_button_press_event, menu);
     
     ct->menu = menu;
+    return menu;
+}
+
+void ui_contextmenu_popup(UIMENU menu) {
+    gtk_menu_popup(menu, NULL, NULL, 0, 0, 0, gtk_get_current_event_time());
 }
 
 void ui_widget_menuitem(UiObject *obj, char *label, ui_callback f, void *userdata) {
@@ -521,6 +526,7 @@
     
     // create menuitem
     GtkWidget *widget = gtk_menu_item_new_with_mnemonic(label);
+    gtk_widget_show(widget);
     
     if(f) {
         UiEventData *event = malloc(sizeof(UiEventData));
@@ -570,6 +576,7 @@
     
     // create menuitem
     GtkWidget *widget = gtk_image_menu_item_new_from_stock(stockid, obj->ctx->accel_group);
+    gtk_widget_show(widget);
     
     if(f) {
         UiEventData *event = malloc(sizeof(UiEventData));
--- a/ui/gtk/toolkit.c	Sun Jan 24 22:20:47 2016 +0100
+++ b/ui/gtk/toolkit.c	Mon Jan 25 16:36:31 2016 +0100
@@ -174,54 +174,3 @@
 }
 
 
-/* -------------------- common widget functions -------------------- */
-
-static gboolean widget_button_pressed(
-        GtkWidget *widget,
-        GdkEvent *event,
-        gpointer userdata)
-{
-    UiEventData *eventdata = userdata;
-    
-    UiMouseEvent me;
-    me.x = (int)event->button.x;
-    me.y = (int)event->button.y;
-    
-    int exec = 0;
-    if(event->button.type == GDK_BUTTON_PRESS) {
-        exec = 1;
-        me.type = UI_PRESS;
-    } else if(event->button.type == GDK_2BUTTON_PRESS) {
-        exec = 1;
-        me.type = UI_PRESS2;
-    }
-    
-    if(exec) {
-        UiEvent e;
-        e.obj = eventdata->obj;
-        e.window = eventdata->obj->window;
-        e.document = eventdata->obj->ctx->document;
-        e.eventdata = &me;
-        e.intval = 0;
-        eventdata->callback(&e, eventdata->userdata);
-    }
-    return true;
-}
-
-void ui_mouse_handler(UiObject *obj, UIWIDGET widget, ui_callback f, void *u) {
-    gtk_widget_set_events(widget, GDK_BUTTON_PRESS_MASK);
-    if(f) {
-        UiEventData *event = malloc(sizeof(UiEventData));
-        event->obj = obj;
-        event->callback = f;
-        event->userdata = u;
-        
-        g_signal_connect(G_OBJECT(widget),
-                "button-press-event",
-                G_CALLBACK(widget_button_pressed),
-                event);
-    } else {
-         // TODO: warning
-    }
-}
-
--- a/ui/motif/graphics.c	Sun Jan 24 22:20:47 2016 +0100
+++ b/ui/motif/graphics.c	Mon Jan 25 16:36:31 2016 +0100
@@ -122,7 +122,7 @@
     
 }
 
-void ui_mouse_handler(UiObject *obj, UIWIDGET widget, ui_callback f, void *u) {
+void ui_drawingarea_mousehandler(UiObject *obj, UIWIDGET widget, ui_callback f, void *u) {
     if(f) {
         UiMouseEventData *event = malloc(sizeof(UiMouseEventData));
         event->obj = obj;
--- a/ui/motif/menu.c	Sun Jan 24 22:20:47 2016 +0100
+++ b/ui/motif/menu.c	Mon Jan 25 16:36:31 2016 +0100
@@ -540,18 +540,28 @@
     *c = FALSE;
 }
 
-void ui_contextmenu(UiObject *obj) {
+UIMENU ui_contextmenu(UiObject *obj) {
     UiContainer *ct = uic_get_current_container(obj);
-    ui_contextmenu_w(obj, ct->current);
+    if(ct->current) {
+        return ui_contextmenu_w(obj, ct->current);
+    } else {
+        return NULL; // TODO: warn
+    }
 }
 
-void ui_contextmenu_w(UiObject *obj, UIWIDGET widget) {
+UIMENU ui_contextmenu_w(UiObject *obj, UIWIDGET widget) {
     UiContainer *ct = uic_get_current_container(obj);
     
-    Widget menu = XmCreatePopupMenu(ct->current, "popup_menu", NULL, 0);
+    Widget menu = XmCreatePopupMenu(widget, "popup_menu", NULL, 0);
     ct->menu = menu;
     
     XtAddEventHandler(widget, ButtonPressMask, FALSE, ui_popup_handler, menu);
+    
+    return menu;
+}
+
+void ui_contextmenu_popup(UIMENU menu) {
+    
 }
 
 void ui_widget_menuitem(UiObject *obj, char *label, ui_callback f, void *userdata) {
--- a/ui/qt/graphics.cpp	Sun Jan 24 22:20:47 2016 +0100
+++ b/ui/qt/graphics.cpp	Mon Jan 25 16:36:31 2016 +0100
@@ -69,6 +69,18 @@
     return widget;
 }
 
+void ui_drawingarea_mousehandler(UiObject *obj, UIWIDGET widget, ui_callback f, void *u) {
+    
+}
+
+void ui_drawingarea_getsize(UIWIDGET drawingarea, int *width, int *height) {
+    
+}
+
+void ui_drawingarea_redraw(UIWIDGET drawingarea) {
+    
+}
+
 
 /* -------------------- text layout functions -------------------- */
 
--- a/ui/qt/menu.cpp	Sun Jan 24 22:20:47 2016 +0100
+++ b/ui/qt/menu.cpp	Mon Jan 25 16:36:31 2016 +0100
@@ -339,13 +339,12 @@
 void UiContextMenuHandler::contextMenuEvent(const QPoint & pos) {
     menu->popup(widget->mapToGlobal(pos));
 }
-
-void ui_contextmenu(UiObject *obj) {
+UIMENU ui_contextmenu(UiObject *obj) {
     UiContainer *ct = uic_get_current_container(obj);
-    ui_contextmenu_w(obj, ct->current);
+    return ui_contextmenu_w(obj, ct->current);
 }
 
-void ui_contextmenu_w(UiObject *obj, UIWIDGET widget) {
+UIMENU ui_contextmenu_w(UiObject *obj, UIWIDGET widget) {
     UiContainer *ct = uic_get_current_container(obj);
     
     QMenu *menu = new QMenu(widget);
@@ -359,6 +358,12 @@
             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) {
--- a/ui/ui/graphics.h	Sun Jan 24 22:20:47 2016 +0100
+++ b/ui/ui/graphics.h	Mon Jan 25 16:36:31 2016 +0100
@@ -46,6 +46,7 @@
 };
 
 UIWIDGET ui_drawingarea(UiObject *obj, ui_drawfunc f, void *userdata);
+void ui_drawingarea_mousehandler(UiObject *obj, UIWIDGET widget, ui_callback f, void *u);
 void ui_drawingarea_getsize(UIWIDGET drawingarea, int *width, int *height);
 void ui_drawingarea_redraw(UIWIDGET drawingarea);
 
--- a/ui/ui/menu.h	Sun Jan 24 22:20:47 2016 +0100
+++ b/ui/ui/menu.h	Mon Jan 25 16:36:31 2016 +0100
@@ -57,8 +57,9 @@
 /*
  * widget menu functions
  */
-void ui_contextmenu(UiObject *obj);
-void ui_contextmenu_w(UiObject *obj, UIWIDGET widget);
+UIMENU ui_contextmenu(UiObject *obj);
+UIMENU ui_contextmenu_w(UiObject *obj, UIWIDGET widget);
+void ui_contextmenu_popup(UIMENU menu);
 
 void ui_widget_menuitem(UiObject *obj, char *label, ui_callback f, void *userdata);
 void ui_widget_menuitem_st(UiObject *obj, char *stockid, ui_callback f, void *userdata);
--- a/ui/ui/toolkit.h	Sun Jan 24 22:20:47 2016 +0100
+++ b/ui/ui/toolkit.h	Mon Jan 25 16:36:31 2016 +0100
@@ -42,24 +42,30 @@
 
 #include <gtk/gtk.h>
 #define UIWIDGET GtkWidget*
+#define UIMENU   GtkMenu*
 #define UI_GTK
 
 #elif UI_MOTIF
 
 #include <Xm/XmAll.h> 
 #define UIWIDGET Widget
+#define UIMENU   Widget
 
 #elif defined(UI_QT4) || defined(UI_QT5)
 #ifdef	__cplusplus
 #include <QApplication>
 #include <QWidget>
+#include <QMenu>
 #define UIWIDGET QWidget*
+#define UIMENU   QMenu*
 #else /* __cplusplus */
 #define UIWIDGET void*
+#define UIMENU   void*
 #endif
 
 #elif UI_WPF
 #define UIWIDGET void*
+#define UIMENU   void*
 #endif
 
 #ifndef TRUE
@@ -344,9 +350,6 @@
 
 void ui_add_image(char *imgname, char *filename);
 
-// common widget functions
-void ui_mouse_handler(UiObject *obj, UIWIDGET widget, ui_callback f, void *u);
-
 // label widgets
 UIWIDGET ui_label(UiObject *obj, char *label);
 UIWIDGET ui_llabel(UiObject *obj, char *label);

mercurial