added scrolled window for table widget (Motif)

Sat, 17 May 2014 10:48:59 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sat, 17 May 2014 10:48:59 +0200
changeset 45
cfeb2d5f1332
parent 44
a1571777eff2
child 46
4a5e0b9b6992

added scrolled window for table widget (Motif)

application/main.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/application/main.c	Fri May 16 19:20:17 2014 +0200
+++ b/application/main.c	Sat May 17 10:48:59 2014 +0200
@@ -100,6 +100,25 @@
     ui_list_append(list, p2);
     ui_list_append(list, p3);
     ui_list_append(list, p4);
+    ui_list_append(list, p1);
+    ui_list_append(list, p1);
+    ui_list_append(list, p1);
+    ui_list_append(list, p1);
+    ui_list_append(list, p1);
+    ui_list_append(list, p1);
+    ui_list_append(list, p1);
+    ui_list_append(list, p1);
+    ui_list_append(list, p1);
+    ui_list_append(list, p1);
+    ui_list_append(list, p1);
+    ui_list_append(list, p1);
+    ui_list_append(list, p1);
+    ui_list_append(list, p1);
+    ui_list_append(list, p1);
+    ui_list_append(list, p1);
+    ui_list_append(list, p1);
+    ui_list_append(list, p1);
+    
     ui_table(window, list, model);
     
     
--- a/ui/motif/tree.c	Fri May 16 19:20:17 2014 +0200
+++ b/ui/motif/tree.c	Sat May 17 10:48:59 2014 +0200
@@ -43,12 +43,24 @@
     Arg args[16];
     int n = 0;
     
+    // create scrolled window
+    UiContainer *ct = uic_get_current_container(obj);
+    Widget parent = ct->add(ct, args, &n);
+    
+    XtSetArg(args[n], XmNscrollingPolicy, XmAUTOMATIC);
+    n++;
+    XtSetArg(args[n], XmNshadowThickness, 0);
+    n++;
+    Widget scrollw = XmCreateScrolledWindow(parent, "scroll_win", args, n);
+    XtManageChild(scrollw);
+    
     // create table headers
     XmStringTable header = (XmStringTable)XtMalloc(
             modelinfo->columns * sizeof(XmString));
     for(int i=0;i<modelinfo->columns;i++) {
         header[i] = XmStringCreateLocalized(modelinfo->titles[i]);
     }
+    n = 0;
     XtSetArg(args[n], XmNdetailColumnHeading, header);
     n++;
     XtSetArg(args[n], XmNdetailColumnHeadingCount, modelinfo->columns);
@@ -61,12 +73,14 @@
     n++;
     XtSetArg(args[n], XmNselectionPolicy, XmSINGLE_SELECT);
     n++;
+    XtSetArg(args[n], XmNwidth, 600);
+    n++;
     
     // create widget
-    UiContainer *ct = uic_get_current_container(obj);
-    Widget parent = ct->add(ct, args, &n);
+    //UiContainer *ct = uic_get_current_container(obj);
+    //Widget parent = ct->add(ct, args, &n);
     
-    Widget container = XmCreateContainer(parent, "table", args, n);
+    Widget container = XmCreateContainer(scrollw, "table", args, n);
     XtManageChild(container);
     
     // add callbacks
@@ -93,30 +107,34 @@
     
     // add initial data
     void *data = model->first(model);
-    int i = 0;
+    int width = 0;
     while(data) {
-        ui_add_icon_gadget(container, modelinfo, data, i);
+        int w = ui_add_icon_gadget(container, modelinfo, data);
+        if(w > width) {
+            width = w;
+        }
         data = model->next(model);
-        i++;
     }
     
+    // set new XmContainer width
+    XtVaSetValues(container, XmNwidth, width, NULL);
+    
     // cleanup
     for(int i=0;i<modelinfo->columns;i++) {
         XmStringFree(header[i]);
     }
     XtFree((char*)header);
     
-    return container;
+    return scrollw;
 }
 
-void ui_add_icon_gadget(
-        Widget container,
-        UiModelInfo *modelinfo,
-        void *data,
-        int index)
-{
+#define UI_COL_CHAR_WIDTH 12
+
+int ui_add_icon_gadget(Widget container, UiModelInfo *modelinfo, void *data) {
+    int width = 50;
+    
     if(modelinfo->columns == 0) {
-        return;
+        return width;
     }
     
     XmString label = NULL;
@@ -128,6 +146,11 @@
                 modelinfo->types[0],
                 modelinfo->getvalue(data, 0),
                 &f);
+        
+        // column width
+        width += strlen(str) * UI_COL_CHAR_WIDTH;
+        
+        
         XmString label = XmStringCreateLocalized(str);
         XtSetArg(args[0], XmNlabelString, label);
         if(f) {
@@ -145,6 +168,10 @@
                 modelinfo->types[i],
                 modelinfo->getvalue(data, i),
                 &f);
+        
+        // column width
+        width += strlen(str) * UI_COL_CHAR_WIDTH;
+        
         details[i - 1] = XmStringCreateLocalized(str);
         if(f) {
             free(str);
@@ -152,11 +179,9 @@
     }
     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);    
+    XtSetArg(args[3], XmNshadowThickness, 0); 
     // create widget
-    Widget item = XmCreateIconGadget(container, "table_item", args, 5);
+    Widget item = XmCreateIconGadget(container, "table_item", args, 4);
     XtManageChild(item);
     
     // cleanup
@@ -165,6 +190,8 @@
         XmStringFree(details[i]);
     }
     XtFree((char*)details);
+    
+    return width;
 }
 
 char* ui_type_to_string(UiModelType type, void *data, Boolean *free) {
@@ -228,8 +255,8 @@
     selection->count = xs->selected_item_count;
     selection->rows = calloc(selection->count, sizeof(int));
     for(int i=0;i<selection->count;i++) {
-        intptr_t index;
-        XtVaGetValues(xs->selected_items[i], XmNuserData, &index, NULL);
+        int index;
+        XtVaGetValues(xs->selected_items[i], XmNpositionIndex, &index, NULL);
         selection->rows[i] = index;
     }
     return selection;
--- a/ui/motif/tree.h	Fri May 16 19:20:17 2014 +0200
+++ b/ui/motif/tree.h	Sat May 17 10:48:59 2014 +0200
@@ -43,11 +43,7 @@
     UiListSelection *last_selection;
 } UiTreeEventData;    
 
-void ui_add_icon_gadget(
-        Widget container,
-        UiModelInfo *modelinfo,
-        void *data,
-        int index);
+int ui_add_icon_gadget(Widget container, UiModelInfo *modelinfo, void *data);
 char* ui_type_to_string(UiModelType type, void *data, Boolean *free);
 
 void ui_table_action_callback(

mercurial