added mouse event handler (Motif)

Mon, 30 Nov 2015 14:09:55 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Mon, 30 Nov 2015 14:09:55 +0100
changeset 98
efaae97bd95b
parent 97
1a786201465f
child 99
ea1a2d5de765

added mouse event handler (Motif)

application/main.c file | annotate | diff | comparison | revisions
ui/motif/button.c file | annotate | diff | comparison | revisions
ui/motif/graphics.c file | annotate | diff | comparison | revisions
ui/motif/graphics.h file | annotate | diff | comparison | revisions
ui/motif/list.c file | annotate | diff | comparison | revisions
ui/motif/menu.c file | annotate | diff | comparison | revisions
ui/motif/toolbar.c file | annotate | diff | comparison | revisions
ui/motif/toolkit.h file | annotate | diff | comparison | revisions
--- a/application/main.c	Sun Nov 29 21:43:03 2015 +0100
+++ b/application/main.c	Mon Nov 30 14:09:55 2015 +0100
@@ -64,6 +64,11 @@
     ui_text_free(text);
 }
 
+void click(UiEvent *event, void *data) {
+    UiMouseEvent *me = event->eventdata;
+    printf("click[%d](%d,%d)\n", me->type, me->x, me->y);
+}
+
 int main(int argc, char** argv) { 
     ui_init("app1", argc, argv);
     
@@ -83,7 +88,8 @@
     ui_toolbar_add_default("button2");
     
     UiObject *obj = ui_window("Test", NULL);
-    ui_drawingarea(obj, draw, NULL);
+    UIWIDGET w = ui_drawingarea(obj, draw, NULL);
+    ui_mouse_handler(obj, w, click, NULL);
     
     ui_show(obj);
     ui_main();
--- a/ui/motif/button.c	Sun Nov 29 21:43:03 2015 +0100
+++ b/ui/motif/button.c	Mon Nov 30 14:09:55 2015 +0100
@@ -54,7 +54,7 @@
                 obj->ctx->mempool,
                 sizeof(UiEventData));
         event->obj = obj;
-        event->user_data = data;
+        event->userdata = data;
         event->callback = f;
         event->value = 0;
         XtAddCallback(
@@ -94,7 +94,7 @@
     e.window = event->obj->window;
     // TODO: e.document
     e.intval = tb->set;
-    event->callback(&e, event->user_data); 
+    event->callback(&e, event->userdata); 
 }
 
 void ui_push_button_callback(Widget widget, UiEventData *event, XtPointer d) {
@@ -103,5 +103,5 @@
     e.window = event->obj->window;
     e.document = event->obj->ctx->document;
     e.intval = event->value;
-    event->callback(&e, event->user_data);
+    event->callback(&e, event->userdata);
 }
--- a/ui/motif/graphics.c	Sun Nov 29 21:43:03 2015 +0100
+++ b/ui/motif/graphics.c	Mon Nov 30 14:09:55 2015 +0100
@@ -92,6 +92,45 @@
     return drawingarea;
 }
 
+static void ui_drawingarea_input(Widget widget, XtPointer u, XtPointer c) {
+    XmDrawingAreaCallbackStruct *cbs = (XmDrawingAreaCallbackStruct*)c;
+    XEvent *xevent = cbs->event;
+    UiMouseEventData *event = u;
+    
+    if (cbs->reason == XmCR_INPUT) {
+        if (xevent->xany.type == ButtonPress) {
+            UiMouseEvent me;
+            me.x = xevent->xbutton.x;
+            me.y = xevent->xbutton.y;
+            // TODO: configurable double click time
+            me.type = xevent->xbutton.time - event->last_event > 300 ? UI_PRESS : UI_PRESS2;
+            
+            UiEvent e;
+            e.obj = event->obj;
+            e.window = event->obj->window;
+            e.document = event->obj->ctx->document;
+            e.eventdata = &me;
+            e.intval = 0;
+            event->callback(&e, event->userdata);
+            
+            
+            event->last_event = me.type == UI_PRESS2 ? 0 : xevent->xbutton.time;
+        }
+    }
+    
+}
+
+void ui_mouse_handler(UiObject *obj, UIWIDGET widget, ui_callback f, void *u) {
+    if(f) {
+        UiMouseEventData *event = malloc(sizeof(UiMouseEventData));
+        event->obj = obj;
+        event->callback = f;
+        event->userdata = u;
+        event->last_event = 0;
+        
+        XtAddCallback(widget, XmNinputCallback, ui_drawingarea_input, event);
+    }
+}
 
 /* -------------------- text layout functions -------------------- */
 UiTextLayout* ui_text(UiGraphics *g) {
--- a/ui/motif/graphics.h	Sun Nov 29 21:43:03 2015 +0100
+++ b/ui/motif/graphics.h	Mon Nov 30 14:09:55 2015 +0100
@@ -51,6 +51,13 @@
     UiXlibGraphics gr;
 } UiDrawEvent;
 
+typedef struct UiMouseEventData {
+    UiObject    *obj;
+    ui_callback callback;
+    void        *userdata;
+    Time        last_event;
+} UiMouseEventData;
+
 struct UiTextLayout {
     char     *text;
     size_t   length;
--- a/ui/motif/list.c	Sun Nov 29 21:43:03 2015 +0100
+++ b/ui/motif/list.c	Mon Nov 30 14:09:55 2015 +0100
@@ -83,7 +83,7 @@
                 obj->ctx->mempool,
                 sizeof(UiListViewEventData));
         event->event.obj = obj;
-        event->event.user_data = udata;
+        event->event.userdata = udata;
         event->event.callback = f;
         event->event.value = 0;
         event->list = list;
@@ -158,5 +158,5 @@
     e.document = event->event.obj->ctx->document;
     e.eventdata = event->list->list->get(event->list->list, cbs->item_position - 1);
     e.intval = cbs->item_position - 1;
-    event->event.callback(&e, event->event.user_data);
+    event->event.callback(&e, event->event.userdata);
 }
--- a/ui/motif/menu.c	Sun Nov 29 21:43:03 2015 +0100
+++ b/ui/motif/menu.c	Mon Nov 30 14:09:55 2015 +0100
@@ -264,7 +264,7 @@
                 obj->ctx->mempool,
                 sizeof(UiEventData));
         event->obj = obj;
-        event->user_data = mi->userdata;
+        event->userdata = mi->userdata;
         event->callback = mi->callback;
         event->value = 0;
         XtAddCallback(
@@ -323,7 +323,7 @@
                 obj->ctx->mempool,
                 sizeof(UiEventData));
         event->obj = obj;
-        event->user_data = mi->userdata;
+        event->userdata = mi->userdata;
         event->callback = mi->callback;
         event->value = 0;
         XtAddCallback(
@@ -376,7 +376,7 @@
                 obj->ctx->mempool,
                 sizeof(UiEventData));
         event->obj = obj;
-        event->user_data = ci->userdata;
+        event->userdata = ci->userdata;
         event->callback = ci->callback;
         XtAddCallback(
             checkbox,
@@ -499,7 +499,7 @@
             // TODO: use mempool
             UiEventData *event = malloc(sizeof(UiEventData));
             event->obj = list->object;
-            event->user_data = list->userdata;
+            event->userdata = list->userdata;
             event->callback = list->callback;
             event->value = i - 1;
 
@@ -524,7 +524,7 @@
     e.window = event->obj->window;
     e.document = event->obj->ctx->document;
     e.intval = 0;
-    event->callback(&e, event->user_data);    
+    event->callback(&e, event->userdata);    
 }
 
 
--- a/ui/motif/toolbar.c	Sun Nov 29 21:43:03 2015 +0100
+++ b/ui/motif/toolbar.c	Mon Nov 30 14:09:55 2015 +0100
@@ -246,7 +246,7 @@
                 obj->ctx->mempool,
                 sizeof(UiEventData));
         event->obj = obj;
-        event->user_data = item->userdata;
+        event->userdata = item->userdata;
         event->callback = item->callback;
         XtAddCallback(
                 button,
@@ -280,7 +280,7 @@
                 obj->ctx->mempool,
                 sizeof(UiEventData));
         event->obj = obj;
-        event->user_data = item->userdata;
+        event->userdata = item->userdata;
         event->callback = item->callback;
         XtAddCallback(
                 button,
@@ -313,7 +313,7 @@
                 obj->ctx->mempool,
                 sizeof(UiEventData));
         event->obj = obj;
-        event->user_data = item->userdata;
+        event->userdata = item->userdata;
         event->callback = item->callback;
         XtAddCallback(
                 button,
--- a/ui/motif/toolkit.h	Sun Nov 29 21:43:03 2015 +0100
+++ b/ui/motif/toolkit.h	Mon Nov 30 14:09:55 2015 +0100
@@ -43,7 +43,7 @@
 typedef struct UiEventData {
     UiObject    *obj;
     ui_callback callback;
-    void        *user_data;
+    void        *userdata;
     int         value;
 } UiEventData;
 

mercurial