added grid container (Motif)

Sun, 04 Jan 2015 22:12:07 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 04 Jan 2015 22:12:07 +0100
changeset 62
70d2aee84432
parent 61
7ee124a58fe3
child 63
46a42f0c4f93

added grid container (Motif)

application/main.c file | annotate | diff | comparison | revisions
ui/gtk/container.c file | annotate | diff | comparison | revisions
ui/motif/button.c file | annotate | diff | comparison | revisions
ui/motif/container.c file | annotate | diff | comparison | revisions
ui/motif/container.h file | annotate | diff | comparison | revisions
ui/motif/label.c file | annotate | diff | comparison | revisions
ui/motif/label.h file | annotate | diff | comparison | revisions
ui/motif/objs.mk file | annotate | diff | comparison | revisions
ui/motif/window.c file | annotate | diff | comparison | revisions
ui/ui/toolkit.h file | annotate | diff | comparison | revisions
--- a/application/main.c	Thu Jan 01 17:22:55 2015 +0100
+++ b/application/main.c	Sun Jan 04 22:12:07 2015 +0100
@@ -115,9 +115,10 @@
     ui_list_append(list, p3);
     ui_list_append(list, p4);
     
-    //ui_sidebar(window);
+    /*
+    ui_sidebar(window);
     ui_layout_fill(window, FALSE);
-    ui_hbox(window);
+    ui_vbox(window);
     ui_button(window, "Test1", NULL, NULL);
     ui_button(window, "Test2", NULL, NULL);
     ui_button(window, "Test3", NULL, NULL);
@@ -127,7 +128,24 @@
     ui_button(window, "Test7", NULL, NULL);
     ui_button(window, "Test8", NULL, NULL);
     ui_end(window);
+    ui_end(window);
     ui_table(window, list, model);
+    */
+    ui_layout_fill(window, FALSE);
+    ui_grid(window);
+    ui_button(window, "Test", NULL, NULL);
+    ui_label(window, "Test");
+    ui_button(window, "Test", NULL, NULL);
+    ui_newline(window);
+    ui_button(window, "Test_______", NULL, NULL);
+    ui_button(window, "Test", NULL, NULL);
+    ui_newline(window);
+    ui_button(window, "Test", NULL, NULL);
+    ui_button(window, "Test", NULL, NULL);
+    ui_button(window, "Test", NULL, NULL);
+    ui_end(window);
+    
+    ui_textarea_nv(window, "text");
     
     //ui_end(window);
     /*
--- a/ui/gtk/container.c	Thu Jan 01 17:22:55 2015 +0100
+++ b/ui/gtk/container.c	Sun Jan 04 22:12:07 2015 +0100
@@ -105,6 +105,32 @@
     ui_reset_layout(ct->layout);
 }
 
+UIWIDGET ui_vbox(UiObject *obj) {
+    UiContainer *ct = uic_get_current_container(obj);
+    
+    GtkWidget *vbox = ui_gtk_vbox_new();
+    ct->add(ct, vbox, TRUE);
+    
+    UiObject *newobj = uic_object_new(obj, vbox);
+    newobj->container = ui_box_container(obj, vbox);
+    uic_obj_add(obj, newobj);
+    
+    return vbox;
+}
+
+UIWIDGET ui_hbox(UiObject *obj) {
+    UiContainer *ct = uic_get_current_container(obj);
+    
+    GtkWidget *hbox = ui_gtk_hbox_new();
+    ct->add(ct, hbox, TRUE);
+    
+    UiObject *newobj = uic_object_new(obj, hbox);
+    newobj->container = ui_box_container(obj, hbox);
+    uic_obj_add(obj, newobj);
+    
+    return hbox;
+}
+
 
 /* -------------------- Sidebar -------------------- */
 UIWIDGET ui_sidebar(UiObject *obj) {
--- a/ui/motif/button.c	Thu Jan 01 17:22:55 2015 +0100
+++ b/ui/motif/button.c	Sun Jan 04 22:12:07 2015 +0100
@@ -42,9 +42,10 @@
     int n = 0;
     Arg args[16];
     
-    Widget parent = ct->prepare(ct, args, &n, FALSE);
     XtSetArg(args[n], XmNlabelString, str);
     n++;
+    
+    Widget parent = ct->prepare(ct, args, &n, FALSE);
     Widget button = XmCreatePushButton(parent, "button", args, n);
     ct->add(ct, button);
     
--- a/ui/motif/container.c	Thu Jan 01 17:22:55 2015 +0100
+++ b/ui/motif/container.c	Sun Jan 04 22:12:07 2015 +0100
@@ -28,11 +28,14 @@
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <inttypes.h>
 
 #include "container.h"
 #include "../common/context.h"
 #include "../common/object.h"
 
+#define UI_GRID_MAX_COLUMNS 512
+
 static UiBool ui_lb2bool(UiLayoutBool b) {
     return b == UI_LAYOUT_TRUE ? TRUE : FALSE;
 }
@@ -54,12 +57,11 @@
 }
 
 Widget ui_frame_container_prepare(UiContainer *ct, Arg *args, int *n, UiBool fill) {
-    ui_reset_layout(ct->layout);
     return ct->widget;
 }
 
 void ui_frame_container_add(UiContainer *ct, Widget widget) {
-    
+    ui_reset_layout(ct->layout);
 }
 
 
@@ -126,7 +128,7 @@
         XtSetArg(args[a], d1, XmATTACH_FORM); a++;
     }
     
-    *n += a;
+    *n = a;
     return ct->widget;
 }
 
@@ -175,6 +177,125 @@
     ui_reset_layout(ct->layout);
 }
 
+UiContainer* ui_grid_container(UiObject *obj, Widget form) {
+    UiGridContainer *ct = ucx_mempool_calloc(
+            obj->ctx->mempool,
+            1,
+            sizeof(UiGridContainer));
+    ct->container.widget = form;
+    ct->container.prepare = ui_grid_container_prepare;
+    ct->container.add = ui_grid_container_add;
+    return (UiContainer*)ct;
+}
+
+void ui_grid_newline(UiGridContainer *grid) {
+    if(grid->current) {
+        grid->current = NULL;
+    }
+    grid->container.layout.newline = FALSE;
+}
+
+Widget ui_grid_container_prepare(UiContainer *ct, Arg *args, int *n, UiBool fill) {
+    UiGridContainer *grid = (UiGridContainer*)ct;
+    if(ct->layout.newline) {
+        ui_grid_newline(grid);
+    }
+    return ct->widget;
+}
+
+void ui_grid_container_add(UiContainer *ct, Widget widget) {
+    UiGridContainer *grid = (UiGridContainer*)ct;
+    
+    if(grid->current) {
+        grid->current = ucx_list_append(grid->current, widget);
+    } else {
+        grid->current = ucx_list_append(grid->current, widget);
+        grid->lines = ucx_list_append(grid->lines, grid->current);
+    }
+    
+    ui_reset_layout(ct->layout);
+}
+
+static void ui_grid_resize(Widget widget, XtPointer udata, XtPointer cdata) {
+    UiGridContainer *grid = udata;
+    
+    UcxList *rowdim = NULL;
+    int coldim[UI_GRID_MAX_COLUMNS];
+    memset(coldim, 0, UI_GRID_MAX_COLUMNS*sizeof(int));
+    int numcol = 0;
+    
+    // get the minimum size of the columns and rows
+    int sumw = 0;
+    int sumh = 0;
+    UCX_FOREACH(row, grid->lines) {
+        int rheight = 0;
+        int i=0;
+        int sum_width = 0;
+        UCX_FOREACH(elm, row->data) {
+            Widget w = elm->data;
+            int widget_width = 0;
+            int widget_height = 0;
+            XtVaGetValues(
+                    w,
+                    XmNwidth,
+                    &widget_width,
+                    XmNheight,
+                    &widget_height, 
+                    0);
+            
+            // get the maximum height in this row
+            if(widget_height > rheight) {
+                rheight = widget_height;
+            }
+            rowdim = ucx_list_append(rowdim, (void*)(intptr_t)rheight);
+            
+            // get the maximum width in this column
+            if(widget_width > coldim[i]) {
+                coldim[i] = widget_width;
+            }
+            sum_width += widget_width;
+            if(sum_width > sumw) {
+                sumw = sum_width;
+            }
+            
+            i++;
+            if(i > numcol) {
+                numcol = i;
+            }
+        }
+        sumh += rheight;
+    }
+    
+    // check container size
+    int gwidth = 0;
+    int gheight = 0;
+    XtVaGetValues(widget, XmNwidth, &gwidth, XmNheight, &gheight, NULL);
+    if(gwidth < sumw || gheight < sumh) {
+        XtVaSetValues(widget, XmNwidth, sumw, XmNheight, sumh, NULL);
+        ucx_list_free(rowdim);
+        return;
+    }
+    
+    
+    // adjust the positions of all children
+    int y = 0;
+    UCX_FOREACH(row, grid->lines) {
+        int x = 0;       
+        int i=0;
+        UCX_FOREACH(elm, row->data) {
+            Widget w = elm->data;
+            XtVaSetValues(w, XmNx, x, XmNy, y, NULL);
+            
+            x += coldim[i];
+            i++;
+        }
+        y += (intptr_t)rowdim->data;
+        rowdim = rowdim->next;
+    }
+    
+    ucx_list_free(rowdim);
+}
+
 UIWIDGET ui_box(UiObject *obj, UiBoxOrientation orientation) {
     UiContainer *ct = uic_get_current_container(obj);
     
@@ -200,11 +321,31 @@
     return ui_box(obj, UI_BOX_HORIZONTAL);
 }
 
+UIWIDGET ui_grid(UiObject *obj) {
+    UiContainer *ct = uic_get_current_container(obj);
+    
+    Arg args[16];
+    int n = 0;
+    Widget parent = ct->prepare(ct, args, &n, TRUE);
+    Widget grid = XmCreateDrawingArea(parent, "grid", args, n);
+    ct->add(ct, grid);
+    XtManageChild(grid);
+    
+    UiObject *newobj = uic_object_new(obj, grid);
+    newobj->container = ui_grid_container(obj, grid);
+    uic_obj_add(obj, newobj);
+    
+    XtAddCallback (grid, XmNresizeCallback , ui_grid_resize, newobj->container);
+    
+    return grid;
+}
+
 UIWIDGET ui_sidebar(UiObject *obj) {
     UiContainer *ct = uic_get_current_container(obj);
     
     Arg args[16];
     int n = 0;
+    
     XtSetArg(args[n], XmNorientation, XmHORIZONTAL);
     n++;
     
@@ -214,21 +355,19 @@
     XtManageChild(pane);
     
     // add sidebar widget
-    XtSetArg(args[0], XmNshadowType, XmSHADOW_ETCHED_OUT);
-    XtSetArg(args[1], XmNshadowThickness, 0);
-    Widget sidebar = XmCreateFrame(pane, "sidebar", args, 2);
+    Widget sidebar = XmCreateForm(pane, "sidebar", args, 0);
     XtManageChild(sidebar);
     
     UiObject *left = uic_object_new(obj, sidebar);
-    left->container = ui_frame_container(left, sidebar);
+    left->container = ui_box_container(left, sidebar, UI_BOX_VERTICAL);
     
     // add content widget
-    XtSetArg (args[2], XmNpaneMaximum, 8000);
-    Widget content = XmCreateFrame(pane, "content_area", args, 3);
+    XtSetArg (args[0], XmNpaneMaximum, 8000);
+    Widget content = XmCreateForm(pane, "content_area", args, 1);
     XtManageChild(content);
     
     UiObject *right = uic_object_new(obj, content);
-    right->container = ui_frame_container(right, content);
+    right->container = ui_box_container(right, content, UI_BOX_VERTICAL);
     
     uic_obj_add(obj, right);
     uic_obj_add(obj, left);
@@ -269,7 +408,6 @@
     tabbedpane->view.widget = tabct;
     tabbedpane->view.document = NULL;
     tabbedpane->tabbar = tabbar;
-    //tabbedpane->form = tabview;
     tabbedpane->tabs = NULL;
     tabbedpane->current = NULL;
     
@@ -450,3 +588,8 @@
     UiContainer *ct = uic_get_current_container(obj);
     ct->layout.fill = ui_bool2lb(fill);
 }
+
+void ui_newline(UiObject *obj) {
+    UiContainer *ct = uic_get_current_container(obj);
+    ct->layout.newline = TRUE;
+}
--- a/ui/motif/container.h	Thu Jan 01 17:22:55 2015 +0100
+++ b/ui/motif/container.h	Sun Jan 04 22:12:07 2015 +0100
@@ -42,6 +42,7 @@
 typedef struct MotifTabbedPane MotifTabbedPane;
 typedef struct UiTab           UiTab;
 typedef struct UiBoxContainer  UiBoxContainer;
+typedef struct UiGridContainer UiGridContainer;
 typedef struct UiLayout        UiLayout;
 
 typedef Widget (*ui_container_add_f)(UiContainer*, Arg*, int*, UiBool);
@@ -63,6 +64,7 @@
 
 struct UiLayout {
     UiLayoutBool fill;
+    UiBool newline;
 };
 
 struct UiContainer {
@@ -79,6 +81,12 @@
     UiBoxOrientation orientation;
 };
 
+struct UiGridContainer {
+    UiContainer container;
+    UcxList *lines;
+    UcxList *current;
+};
+
 struct MotifTabbedPane {
     UiTabbedPane view;
     Widget       tabbar;
@@ -101,6 +109,10 @@
 Widget ui_box_container_prepare(UiContainer *ct, Arg *args, int *n, UiBool fill);
 void ui_box_container_add(UiContainer *ct, Widget widget);
 
+UiContainer* ui_grid_container(UiObject *obj, Widget rowcolumn);
+Widget ui_grid_container_prepare(UiContainer *ct, Arg *args, int *n, UiBool fill);
+void ui_grid_container_add(UiContainer *ct, Widget widget);
+
 void ui_tab_button_callback(Widget widget, UiTab *tab, XtPointer d);
 void ui_change_tab(MotifTabbedPane *pane, UiTab *tab);
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ui/motif/label.c	Sun Jan 04 22:12:07 2015 +0100
@@ -0,0 +1,53 @@
+/*
+ * 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 "label.h"
+#include "container.h"
+#include "../common/context.h"
+#include "../common/object.h"
+#include "../../ucx/mempool.h"
+
+UIWIDGET ui_label(UiObject *obj, char *label) {
+    UiContainer *ct = uic_get_current_container(obj);
+    XmString str = XmStringCreateLocalized(label);
+    
+    int n = 0;
+    Arg args[16]; 
+    XtSetArg(args[n], XmNlabelString, str);
+    n++;
+    
+    Widget parent = ct->prepare(ct, args, &n, FALSE);
+    Widget widget = XmCreateLabel(parent, "label", args, n);
+    ct->add(ct, widget);  
+    XtManageChild(widget);
+    
+    return widget;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ui/motif/label.h	Sun Jan 04 22:12:07 2015 +0100
@@ -0,0 +1,44 @@
+/*
+ * 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.
+ */
+
+#ifndef LABEL_H
+#define	LABEL_H
+
+#ifdef	__cplusplus
+extern "C" {
+#endif
+
+
+
+
+#ifdef	__cplusplus
+}
+#endif
+
+#endif	/* LABEL_H */
+
--- a/ui/motif/objs.mk	Thu Jan 01 17:22:55 2015 +0100
+++ b/ui/motif/objs.mk	Sun Jan 04 22:12:07 2015 +0100
@@ -36,6 +36,7 @@
 MOTIFOBJ += menu.o
 MOTIFOBJ += toolbar.o
 MOTIFOBJ += button.o
+MOTIFOBJ += label.o
 MOTIFOBJ += text.o
 MOTIFOBJ += list.o
 MOTIFOBJ += tree.o
--- a/ui/motif/window.c	Thu Jan 01 17:22:55 2015 +0100
+++ b/ui/motif/window.c	Sun Jan 04 22:12:07 2015 +0100
@@ -38,8 +38,8 @@
 
 static int nwindows = 0;
 
-static int window_default_width = 650;
-static int window_default_height = 550;
+static int window_default_width = 600;
+static int window_default_height = 500;
 
 static void window_close_handler(Widget window, void *udata, void *cdata) {
     nwindows--;
@@ -57,8 +57,12 @@
     int n = 0;
     
     XtSetArg(args[0], XmNtitle, title);
-    XtSetArg(args[1], XmNbaseWidth, window_default_width);
-    XtSetArg(args[2], XmNbaseHeight, window_default_height);
+    //XtSetArg(args[1], XmNbaseWidth, window_default_width);
+    //XtSetArg(args[2], XmNbaseHeight, window_default_height);
+    XtSetArg(args[1], XmNminWidth, 100);
+    XtSetArg(args[2], XmNminHeight, 50);
+    XtSetArg(args[3], XmNwidth, window_default_width);
+    XtSetArg(args[4], XmNheight, window_default_height);
     
     Widget toplevel = XtAppCreateShell(
             "Test123",
@@ -67,7 +71,7 @@
             vendorShellWidgetClass,
             ui_get_display(),
             args,
-            3);
+            5);
     
     Atom wm_delete_window;
     wm_delete_window = XmInternAtom(
--- a/ui/ui/toolkit.h	Thu Jan 01 17:22:55 2015 +0100
+++ b/ui/ui/toolkit.h	Sun Jan 04 22:12:07 2015 +0100
@@ -227,10 +227,12 @@
 
 UIWIDGET ui_vbox(UiObject *obj);
 UIWIDGET ui_hbox(UiObject *obj);
+UIWIDGET ui_grid(UiObject *obj);
 UIWIDGET ui_sidebar(UiObject *obj);
 void ui_end(UiObject *obj);
 
 void ui_layout_fill(UiObject *obj, UiBool fill);
+void ui_newline(UiObject *obj);
 
 
 UiTabbedPane* ui_tabbed_document_view(UiObject *obj);
@@ -305,6 +307,10 @@
 
 void ui_add_image(char *imgname, char *filename);
 
+
+
+UIWIDGET ui_label(UiObject *obj, char *label);
+
 #ifdef	__cplusplus
 }
 #endif

mercurial