added simple tabview (Motif)

Mon, 05 Jan 2015 14:47:19 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Mon, 05 Jan 2015 14:47:19 +0100
changeset 64
6ef2c7f73a30
parent 63
46a42f0c4f93
child 65
4697592e24ba

added simple tabview (Motif)

application/main.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/toolkit.c file | annotate | diff | comparison | revisions
ui/ui/toolkit.h file | annotate | diff | comparison | revisions
--- a/application/main.c	Mon Jan 05 11:49:46 2015 +0100
+++ b/application/main.c	Mon Jan 05 14:47:19 2015 +0100
@@ -55,6 +55,11 @@
     return NULL;
 }
 
+UiString name;
+UiString mail;
+
+UIWIDGET tabview = NULL;
+
 void action_activate(UiEvent *event, void *data) {
     UiListSelection *selection = event->eventdata;
     printf("activate: %d\n", event->intval);
@@ -72,7 +77,14 @@
 void action_button(UiEvent *event, void *data) {
     printf("button: %d\n", event->intval);
     char *s = ui_gettext(event->obj, "text");
-    printf("{%s}\n", s);    
+    printf("{%s}\n", s);
+    printf("name: {%s}\n", ui_getval(name));
+    printf("mail: {%s}\n", ui_getval(mail));
+    ui_select_tab(tabview, 0);
+}
+
+void action_test(UiEvent *event, void *data) {
+    ui_select_tab(tabview, 1);
 }
 
 int main(int argc, char** argv) { 
@@ -134,16 +146,26 @@
     ui_layout_fill(window, FALSE);
     ui_grid(window);
     ui_label(window, "Name");
-    ui_textfield(window, NULL);
+    ui_textfield(window, &name);
     ui_newline(window);
     ui_label(window, "Email");
-    ui_textfield(window, NULL);
+    ui_textfield(window, &mail);
     ui_newline(window);
     ui_end(window);
     
+    tabview = ui_tabview(window);
+    
+    ui_tab(window, "Editor");
     ui_textarea_nv(window, "text");
-    ui_button(window, "Submit", NULL, NULL);
-    //ui_end(window);
+    ui_button(window, "Submit", action_test, NULL);
+    ui_end(window);
+    
+    ui_tab(window, "Button");
+    ui_button(window, "TestButton", NULL, NULL);
+    ui_end(window);
+    
+    ui_end(window);
+    ui_button(window, "End", NULL, NULL);
     /*
     UiTabbedPane *view = ui_tabbed_document_view(window);
     
--- a/ui/motif/container.c	Mon Jan 05 11:49:46 2015 +0100
+++ b/ui/motif/container.c	Mon Jan 05 14:47:19 2015 +0100
@@ -302,6 +302,47 @@
     ucx_list_free(rowdim);
 }
 
+UiContainer* ui_tabview_container(UiObject *obj, Widget frame) {
+    UiTabViewContainer *ct = ucx_mempool_calloc(
+            obj->ctx->mempool,
+            1,
+            sizeof(UiTabViewContainer));
+    ct->context = obj->ctx;
+    ct->container.widget = frame;
+    ct->container.prepare = ui_tabview_container_prepare;
+    ct->container.add = ui_tabview_container_add;
+    return (UiContainer*)ct;
+}
+
+Widget ui_tabview_container_prepare(UiContainer *ct, Arg *args, int *n, UiBool fill) {
+    int a = *n;
+    XtSetArg(args[a], XmNleftAttachment, XmATTACH_FORM); a++;
+    XtSetArg(args[a], XmNrightAttachment, XmATTACH_FORM); a++;
+    XtSetArg(args[a], XmNtopAttachment, XmATTACH_FORM); a++;
+    XtSetArg(args[a], XmNbottomAttachment, XmATTACH_FORM); a++;
+    *n = a;
+    return ct->widget;
+}
+
+void ui_tabview_container_add(UiContainer *ct, Widget widget) {
+    UiTabViewContainer *tabview = (UiTabViewContainer*)ct; 
+    
+    if(tabview->current) {
+        XtUnmanageChild(tabview->current);
+    }
+    if(tabview->tabs) {
+        // not the first tab, so unmanage the new widget
+        //XtUnmanageChild(widget);
+    } else {
+        tabview->current = widget;
+    }
+    tabview->current = widget;
+    
+    tabview->tabs = ucx_list_append(tabview->tabs, widget);
+    
+    ui_reset_layout(ct->layout);
+}
+
 UIWIDGET ui_box(UiObject *obj, UiBoxOrientation orientation) {
     UiContainer *ct = uic_get_current_container(obj);
     
@@ -381,6 +422,59 @@
     return sidebar;
 }
 
+UIWIDGET ui_tabview(UiObject *obj) {
+    UiContainer *ct = uic_get_current_container(obj);
+    
+    // create a simple frame as container widget
+    // when tabs are selected, the current child will be replaced by the
+    // the new tab widget
+    Arg args[16];
+    int n = 0;
+    XtSetArg(args[n], XmNshadowType, XmSHADOW_ETCHED_OUT);
+    n++;
+    XtSetArg(args[n], XmNshadowThickness, 0);
+    n++;
+    Widget parent = ct->prepare(ct, args, &n, TRUE);
+    Widget form = XmCreateForm(parent, "tabview", args, n);
+    ct->add(ct, form);
+    XtManageChild(form);
+    
+    UiObject *tabviewobj = uic_object_new(obj, form);
+    tabviewobj->container = ui_tabview_container(obj, form);
+    uic_obj_add(obj, tabviewobj);
+    
+    XtVaSetValues(form, XmNuserData, tabviewobj->container, NULL);
+    
+    return form;
+}
+
+void ui_tab(UiObject *obj, char *title) {
+    UiContainer *ct = uic_get_current_container(obj);
+    ct->layout.label = title;
+    
+    ui_vbox(obj);
+}
+
+void ui_select_tab(UIWIDGET tabview, int tab) {
+    UiTabViewContainer *ct = NULL;
+    XtVaGetValues(tabview, XmNuserData, &ct, NULL);
+    if(ct) {
+        XtUnmanageChild(ct->current);
+        UcxList *elm = ucx_list_get(ct->tabs, tab);
+        if(elm) {
+            XtManageChild(elm->data);
+            ct->current = elm->data;
+        } else {
+            fprintf(stderr, "UiError: front tab index: %d\n", tab);
+        }
+    } else {
+        fprintf(stderr, "UiError: widget is not a tabview\n");
+    }
+}
+
+
+/* document tabview */
+
 UiTabbedPane* ui_tabbed_document_view(UiObject *obj) {
     int n = 0;
     Arg args[16];
--- a/ui/motif/container.h	Mon Jan 05 11:49:46 2015 +0100
+++ b/ui/motif/container.h	Mon Jan 05 14:47:19 2015 +0100
@@ -39,11 +39,12 @@
 
 #define ui_reset_layout(layout) memset(&(layout), 0, sizeof(UiLayout))
     
-typedef struct MotifTabbedPane MotifTabbedPane;
-typedef struct UiTab           UiTab;
-typedef struct UiBoxContainer  UiBoxContainer;
-typedef struct UiGridContainer UiGridContainer;
-typedef struct UiLayout        UiLayout;
+typedef struct MotifTabbedPane    MotifTabbedPane;
+typedef struct UiTab              UiTab;
+typedef struct UiBoxContainer     UiBoxContainer;
+typedef struct UiGridContainer    UiGridContainer;
+typedef struct UiTabViewContainer UiTabViewContainer;
+typedef struct UiLayout           UiLayout;
 
 typedef Widget (*ui_container_add_f)(UiContainer*, Arg*, int*, UiBool);
 
@@ -64,7 +65,8 @@
 
 struct UiLayout {
     UiLayoutBool fill;
-    UiBool newline;
+    UiBool       newline;
+    char         *label;
 };
 
 struct UiContainer {
@@ -76,15 +78,23 @@
 
 struct UiBoxContainer {
     UiContainer container;
-    Widget prev_widget;
-    UiBool has_fill;
+    Widget      prev_widget;
+    UiBool      has_fill;
     UiBoxOrientation orientation;
 };
 
 struct UiGridContainer {
     UiContainer container;
-    UcxList *lines;
-    UcxList *current;
+    UcxList     *lines;
+    UcxList     *current;
+};
+
+struct UiTabViewContainer {
+    UiContainer container;
+    UiContext   *context;
+    Widget      widget;
+    UcxList     *tabs;
+    Widget      current;
 };
 
 struct MotifTabbedPane {
@@ -113,6 +123,10 @@
 Widget ui_grid_container_prepare(UiContainer *ct, Arg *args, int *n, UiBool fill);
 void ui_grid_container_add(UiContainer *ct, Widget widget);
 
+UiContainer* ui_tabview_container(UiObject *obj, Widget rowcolumn);
+Widget ui_tabview_container_prepare(UiContainer *ct, Arg *args, int *n, UiBool fill);
+void ui_tabview_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);
 
--- a/ui/motif/toolkit.c	Mon Jan 05 11:49:46 2015 +0100
+++ b/ui/motif/toolkit.c	Mon Jan 05 14:47:19 2015 +0100
@@ -73,15 +73,21 @@
         "*rt*fontSize: 10",
 	NULL
 };
-*/
+//*/
+///*
 static String fallback[] = {
     	//"*fontList: -dt-interface system-medium-r-normal-s*utf*:",    
+        "*text_area*renderTable: f1",
+        "*f1*fontType: FONT_IS_XFT",
+        "*f1*fontName: Monospace",
+        "*f1*fontSize: 11",
         "*renderTable: rt",
         "*rt*fontType: FONT_IS_XFT",
         "*rt*fontName: Cantarell",
         "*rt*fontSize: 11",
 	NULL
 };
+//*/
 
 void input_proc(XtPointer data, int *source, XtInputId *iid) {
     void *ptr;
--- a/ui/ui/toolkit.h	Mon Jan 05 11:49:46 2015 +0100
+++ b/ui/ui/toolkit.h	Mon Jan 05 14:47:19 2015 +0100
@@ -231,6 +231,10 @@
 UIWIDGET ui_sidebar(UiObject *obj);
 void ui_end(UiObject *obj);
 
+UIWIDGET ui_tabview(UiObject *obj);
+void ui_tab(UiObject *obj, char *title);
+void ui_select_tab(UIWIDGET tabview, int tab);
+
 void ui_layout_fill(UiObject *obj, UiBool fill);
 void ui_newline(UiObject *obj);
 

mercurial