implement missing tabview functions (Motif)

5 weeks ago

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Fri, 07 Feb 2025 21:57:32 +0100 (5 weeks ago)
changeset 461
b480e133b576
parent 460
1274d84f44de
child 462
9e499276136a

implement missing tabview functions (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/ui/container.h file | annotate | diff | comparison | revisions
--- a/application/main.c	Thu Feb 06 22:36:29 2025 +0100
+++ b/application/main.c	Fri Feb 07 21:57:32 2025 +0100
@@ -575,6 +575,8 @@
 }
 
 typedef struct WData {
+    UIWIDGET tabview;
+    
     UiString *path;
     UiList *list;
     UiInteger *spinner;
@@ -605,6 +607,12 @@
     printf("\n");
 }
 
+static void action_remove_tab3(UiEvent *event, void *data) {
+    WData *wdata = event->window;
+    printf("remove tab 3\n");
+    ui_tabview_remove(wdata->tabview, 2);
+}
+
 void application_startup(UiEvent *event, void *data) {
     
     menulist = ui_list_new(ui_global_context(), "menulist");
@@ -638,7 +646,8 @@
             .onselection = action_listevent, .onselectiondata = "selection");
     */
     
-    ui_tabview(obj, .tabview = UI_TABVIEW_NAVIGATION_TOP, .fill = UI_ON) {
+    
+    ui_tabview_w(obj, wdata->tabview, .tabview = UI_TABVIEW_NAVIGATION_TOP, .fill = UI_ON) {
         ui_tab(obj, "Tab 1") {
             ui_textarea(obj, .varname = "text", .fill = UI_ON);
         }
@@ -648,7 +657,7 @@
         }
         
         ui_tab(obj, "Tab 3") {
-            ui_button(obj, .label = "Test Tab 3");
+            ui_button(obj, .label = "Test Tab 3", .onclick = action_remove_tab3);
         }
     }
     
--- a/ui/motif/container.c	Thu Feb 06 22:36:29 2025 +0100
+++ b/ui/motif/container.c	Fri Feb 07 21:57:32 2025 +0100
@@ -317,6 +317,7 @@
     Widget content = XmCreateFrame(form, "tabviewcontent", xargs, n);
     
     // setup tabview object, that holds all relevant objects
+    tabview->obj = obj;
     tabview->form = form;
     tabview->tabbar = tabbar;
     tabview->content = content;
@@ -325,7 +326,7 @@
     tabview->select = ui_motif_tabview_select;
     tabview->add = ui_motif_tabview_add_tab;
     tabview->remove = ui_motif_tabview_remove;
-    tabview->tabs = cxArrayListCreateSimple(sizeof(UiTab), 8);
+    tabview->tabs = cxArrayListCreate(obj->ctx->allocator, NULL, sizeof(UiTab), 8);
     
     UiTabViewContainer *ct = ui_malloc(obj->ctx, sizeof(UiTabViewContainer));
     ct->container.widget = form;
@@ -364,8 +365,59 @@
     tabview->add(tabview, -1, title, child);
 }
 
+void ui_tabview_select(UIWIDGET tabview, int tab) {
+    UiMotifTabView *tabviewdata = NULL;
+    XtVaGetValues(tabview, XmNuserData, &tabviewdata, NULL);
+    if(tabviewdata) {
+        ui_motif_tabview_select(tabviewdata, tab);
+    } else {
+        fprintf(stderr, "ui_tabview_select: widget is not a tabview\n");
+    }
+}
+
+void ui_tabview_remove(UIWIDGET tabview, int tab) {
+    UiMotifTabView *tabviewdata = NULL;
+    XtVaGetValues(tabview, XmNuserData, &tabviewdata, NULL);
+    if(tabviewdata) {
+        ui_motif_tabview_remove(tabviewdata, tab);
+    } else {
+        fprintf(stderr, "ui_tabview_select: widget is not a tabview\n");
+    }
+}
+
+UiObject* ui_tabview_add(UIWIDGET tabview, const char *name, int tab_index) {
+    UiMotifTabView *tabviewdata = NULL;
+    XtVaGetValues(tabview, XmNuserData, &tabviewdata, NULL);
+    if(tabviewdata) {
+        Arg args[16];
+        int n = 0;
+        
+        XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM); n++;
+        XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;
+        XtSetArg(args[n], XmNbottomAttachment, XmATTACH_FORM); n++;
+        XtSetArg(args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
+        XtSetArg(args[n], XmNtopWidget, tabviewdata->tabbar); n++;
+        
+        Widget grid = XtCreateManagedWidget("vbox", gridClass, tabviewdata->content, args, n);
+        
+        UiObject *newobj = ui_calloc(tabviewdata->obj->ctx, 1, sizeof(UiObject));
+        newobj->ctx = tabviewdata->obj->ctx;
+        newobj->widget = grid;
+        UiContainerX *container = ui_box_container(newobj, grid, UI_BOX_VERTICAL);
+        newobj->container_begin = container;
+        newobj->container_end = container;
+        return newobj;
+    } else {
+        fprintf(stderr, "ui_tabview_select: widget is not a tabview\n");
+        return NULL;
+    }
+}
+
 void ui_motif_tabview_select(UiMotifTabView *tabview, int tab) {
-    
+    UiTab *t = cxListAt(tabview->tabs, tab);
+    if(t) {
+        ui_motif_tabview_change_tab(tabview, t);
+    }
 }
 
 static void ui_tab_button_callback(Widget widget, UiTab *tab, XtPointer d) {
@@ -427,7 +479,23 @@
 }
 
 void ui_motif_tabview_remove(UiMotifTabView *tabview, int index) {
-    
+    UiTab *tab = cxListAt(tabview->tabs, index);
+    if(tab) {
+        if(tab == tabview->current_tab) {
+            if(index > 0) {
+                ui_motif_tabview_select(tabview, index-1);
+            } else {
+                if(index < cxListSize(tabview->tabs)) {
+                    ui_motif_tabview_select(tabview, index+1);
+                } else {
+                    tabview->current_tab = NULL;
+                }
+            }
+        }
+        XtDestroyWidget(tab->tab_button);
+        XtDestroyWidget(tab->child);
+        cxListRemove(tabview->tabs, index);
+    }
 }
 
 void ui_motif_tabview_change_tab(UiMotifTabView *tabview, UiTab *tab) {
--- a/ui/motif/container.h	Thu Feb 06 22:36:29 2025 +0100
+++ b/ui/motif/container.h	Fri Feb 07 21:57:32 2025 +0100
@@ -111,6 +111,7 @@
 
 typedef struct UiMotifTabView UiMotifTabView;
 struct UiMotifTabView {
+    UiObject *obj;
     Widget form;
     Widget tabbar;
     Widget content;
--- a/ui/ui/container.h	Thu Feb 06 22:36:29 2025 +0100
+++ b/ui/ui/container.h	Fri Feb 07 21:57:32 2025 +0100
@@ -256,6 +256,8 @@
 #define ui_headerbar0(obj) for(ui_headerbar_create(obj, (UiHeaderbarArgs){ 0 });ui_container_finish(obj);ui_container_begin_close(obj))
 #define ui_sidebar0(obj) for(ui_sidebar_create(obj, (UiSidebarArgs){ 0 });ui_container_finish(obj);ui_container_begin_close(obj))
 
+#define ui_tabview_w(obj, w, ...) for(w = ui_tabview_create(obj, (UiTabViewArgs){ __VA_ARGS__ });ui_container_finish(obj);ui_container_begin_close(obj))
+
 #define ui_hsplitpane(obj, ...) for(ui_hsplitpane_create(obj, (UiSplitPaneArgs){ __VA_ARGS__ });ui_container_finish(obj);ui_container_begin_close(obj))
 #define ui_vsplitpane(obj, ...) for(ui_vsplitpane_create(obj, (UiSplitPaneArgs){ __VA_ARGS__ });ui_container_finish(obj);ui_container_begin_close(obj))
 #define ui_hsplitpane0(obj) for(ui_hsplitpane_create(obj, (UiSplitPaneArgs){ 0 });ui_container_finish(obj);ui_container_begin_close(obj))

mercurial