ui/motif/tree.c

changeset 39
4e66271541e8
child 43
157a21a914ac
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ui/motif/tree.c	Thu May 15 20:06:28 2014 +0200
@@ -0,0 +1,150 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2014 Olaf Wintermann. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "tree.h"
+
+#include "container.h"
+#include "../common/object.h"
+#include "../../ucx/utils.h"
+
+UIWIDGET ui_table(UiObject *obj, UiList *model, UiModelInfo *modelinfo) {
+    // TODO: check if modelinfo is complete
+    
+    Arg args[16];
+    int n = 0;
+    
+    // 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]);
+    }
+    XtSetArg(args[n], XmNdetailColumnHeading, header);
+    n++;
+    XtSetArg(args[n], XmNdetailColumnHeadingCount, modelinfo->columns);
+    n++;
+    
+    // set res
+    XtSetArg(args[n], XmNlayoutType, XmDETAIL);
+    n++;
+    XtSetArg(args[n], XmNentryViewType, XmSMALL_ICON);
+    n++;
+    XtSetArg(args[n], XmNselectionPolicy, XmSINGLE_SELECT);
+    n++;
+    
+    // create widget
+    UiContainer *ct = uic_get_current_container(obj);
+    Widget parent = ct->add(ct, args, &n);
+    
+    Widget container = XmCreateContainer(parent, "table", args, n);
+    XtManageChild(container);
+    
+    // add initial data
+    void *data = model->first(model);
+    while(data) {
+        ui_add_icon_gadget(container, modelinfo, data);
+        data = model->next(model);
+    }
+    
+    // cleanup
+    for(int i=0;i<modelinfo->columns;i++) {
+        XmStringFree(header[i]);
+    }
+    XtFree((char*)header);
+    
+    return container;
+}
+
+void ui_add_icon_gadget(Widget container, UiModelInfo *modelinfo, void *data) {
+    if(modelinfo->columns == 0) {
+        return;
+    }
+    
+    XmString label = NULL;
+    Arg args[8];
+    Boolean f;
+    // first column
+    if(modelinfo->types[0] != 12345678) { // TODO: icon/label type
+        char *str = ui_type_to_string(
+                modelinfo->types[0],
+                modelinfo->getvalue(data, 0),
+                &f);
+        XmString label = XmStringCreateLocalized(str);
+        XtSetArg(args[0], XmNlabelString, label);
+        if(f) {
+            free(str);
+        }
+    } else {
+        // TODO
+    }
+            
+    // remaining columns are the icon gadget details
+    XmStringTable details = (XmStringTable)XtMalloc(
+            (modelinfo->columns - 1) * sizeof(XmString));
+    for(int i=1;i<modelinfo->columns;i++) {
+        char *str = ui_type_to_string(
+                modelinfo->types[i],
+                modelinfo->getvalue(data, i),
+                &f);
+        details[i - 1] = XmStringCreateLocalized(str);
+        if(f) {
+            free(str);
+        }
+    }
+    XtSetArg(args[1], XmNdetail, details);
+    XtSetArg(args[2], XmNdetailCount, modelinfo->columns - 1);
+    
+    // create widget
+    Widget item = XmCreateIconGadget(container, "table_item", args, 3);
+    XtManageChild(item);
+    
+    // cleanup
+    XmStringFree(label);
+    for(int i=0;i<modelinfo->columns-1;i++) {
+        XmStringFree(details[i]);
+    }
+    XtFree((char*)details);
+}
+
+char* ui_type_to_string(UiModelType type, void *data, Boolean *free) {
+    switch(type) {
+        case UI_STRING: *free = FALSE; return data;
+        case UI_INTEGER: {
+            *free = TRUE;
+            int *val = data;
+            sstr_t str = ucx_asprintf(ucx_default_allocator(), "%d", *val);
+            return str.ptr;
+        }
+    }
+    *free = FALSE;
+    return NULL;
+}

mercurial