added table view events (Motif)

Fri, 16 May 2014 17:39:45 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Fri, 16 May 2014 17:39:45 +0200
changeset 43
157a21a914ac
parent 42
29b2821d1262
child 44
a1571777eff2

added table view events (Motif)

ui/motif/list.c file | annotate | diff | comparison | revisions
ui/motif/tree.c file | annotate | diff | comparison | revisions
ui/motif/tree.h file | annotate | diff | comparison | revisions
--- a/ui/motif/list.c	Fri May 16 16:19:46 2014 +0200
+++ b/ui/motif/list.c	Fri May 16 17:39:45 2014 +0200
@@ -149,7 +149,7 @@
 }
 
 void ui_list_selection_callback (Widget widget, UiListViewEventData *event, XtPointer data) {
-    XmListCallbackStruct *cbs = (XmListCallbackStruct *) data;
+    XmListCallbackStruct *cbs = (XmListCallbackStruct *)data;
     
     UiEvent e;
     e.obj = event->event.obj;
--- a/ui/motif/tree.c	Fri May 16 16:19:46 2014 +0200
+++ b/ui/motif/tree.c	Fri May 16 17:39:45 2014 +0200
@@ -29,6 +29,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <inttypes.h>
 
 #include "tree.h"
 
@@ -68,11 +69,34 @@
     Widget container = XmCreateContainer(parent, "table", args, n);
     XtManageChild(container);
     
+    // add callbacks
+    UiTreeEventData *event = ui_malloc(obj->ctx, sizeof(UiTreeEventData));
+    event->obj = obj;
+    event->activate = modelinfo->activate;
+    event->selection = modelinfo->selection;
+    event->userdata = modelinfo->userdata;
+    if(modelinfo->selection) {
+        XtAddCallback(
+                container,
+                XmNselectionCallback,
+                (XtCallbackProc)ui_table_select_callback,
+                event);
+    }
+    if(modelinfo->activate) {
+        XtAddCallback(
+                container,
+                XmNdefaultActionCallback,
+                (XtCallbackProc)ui_table_action_callback,
+                event);
+    }
+    
     // add initial data
     void *data = model->first(model);
+    int i = 0;
     while(data) {
-        ui_add_icon_gadget(container, modelinfo, data);
+        ui_add_icon_gadget(container, modelinfo, data, i);
         data = model->next(model);
+        i++;
     }
     
     // cleanup
@@ -84,7 +108,12 @@
     return container;
 }
 
-void ui_add_icon_gadget(Widget container, UiModelInfo *modelinfo, void *data) {
+void ui_add_icon_gadget(
+        Widget container,
+        UiModelInfo *modelinfo,
+        void *data,
+        int index)
+{
     if(modelinfo->columns == 0) {
         return;
     }
@@ -122,9 +151,11 @@
     }
     XtSetArg(args[1], XmNdetail, details);
     XtSetArg(args[2], XmNdetailCount, modelinfo->columns - 1);
-    
+    XtSetArg(args[3], XmNshadowThickness, 0);
+    intptr_t userdata = index;
+    XtSetArg(args[4], XmNuserData, userdata);    
     // create widget
-    Widget item = XmCreateIconGadget(container, "table_item", args, 3);
+    Widget item = XmCreateIconGadget(container, "table_item", args, 5);
     XtManageChild(item);
     
     // cleanup
@@ -148,3 +179,53 @@
     *free = FALSE;
     return NULL;
 }
+
+void ui_table_action_callback(
+        Widget widget,
+        UiTreeEventData *event,
+        XmContainerSelectCallbackStruct *sel)
+{ 
+    UiListSelection *selection = malloc(sizeof(UiListSelection));
+    selection->count = sel->selected_item_count;
+    selection->rows = calloc(selection->count, sizeof(int));
+    for(int i=0;i<selection->count;i++) {
+        intptr_t index;
+        XtVaGetValues(sel->selected_items[i], XmNuserData, &index, NULL);
+        selection->rows[i] = index;
+    }
+    
+    UiEvent e;
+    e.obj = event->obj;
+    e.window = event->obj->window;
+    e.document = event->obj->document;
+    e.eventdata = selection;
+    e.intval = selection->count > 0 ? selection->rows[0] : -1;
+    event->activate(&e, event->userdata);
+    
+    free(selection);
+}
+
+void ui_table_select_callback(
+        Widget widget,
+        UiTreeEventData *event,
+        XmContainerSelectCallbackStruct *sel)
+{ 
+    UiListSelection *selection = malloc(sizeof(UiListSelection));
+    selection->count = sel->selected_item_count;
+    selection->rows = calloc(selection->count, sizeof(int));
+    for(int i=0;i<selection->count;i++) {
+        intptr_t index;
+        XtVaGetValues(sel->selected_items[i], XmNuserData, &index, NULL);
+        selection->rows[i] = index;
+    }
+    
+    UiEvent e;
+    e.obj = event->obj;
+    e.window = event->obj->window;
+    e.document = event->obj->document;
+    e.eventdata = selection;
+    e.intval = selection->count > 0 ? selection->rows[0] : -1;
+    event->selection(&e, event->userdata);
+    
+    free(selection);
+}
--- a/ui/motif/tree.h	Fri May 16 16:19:46 2014 +0200
+++ b/ui/motif/tree.h	Fri May 16 17:39:45 2014 +0200
@@ -35,9 +35,29 @@
 extern "C" {
 #endif
 
-void ui_add_icon_gadget(Widget container, UiModelInfo *modelinfo, void *data);
+typedef struct UiTreeEventData {
+    UiObject    *obj;
+    ui_callback activate;
+    ui_callback selection;
+    void        *userdata;
+} UiTreeEventData;    
+
+void ui_add_icon_gadget(
+        Widget container,
+        UiModelInfo *modelinfo,
+        void *data,
+        int index);
 char* ui_type_to_string(UiModelType type, void *data, Boolean *free);
 
+void ui_table_action_callback(
+        Widget widget,
+        UiTreeEventData *event,
+        XmContainerSelectCallbackStruct *sel);
+void ui_table_select_callback(
+        Widget widget,
+        UiTreeEventData *event,
+        XmContainerSelectCallbackStruct *sel);
+
 
 #ifdef	__cplusplus
 }

mercurial