implement stack set/get/remove (GTK) newapi

Wed, 09 Oct 2024 21:43:11 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Wed, 09 Oct 2024 21:43:11 +0200
branch
newapi
changeset 332
7e7c05af8f3a
parent 331
cdad8b7e83db
child 333
634e3ce80afe

implement stack set/get/remove (GTK)

application/main.c file | annotate | diff | comparison | revisions
ui/gtk/container.c file | annotate | diff | comparison | revisions
--- a/application/main.c	Tue Oct 08 22:53:27 2024 +0200
+++ b/application/main.c	Wed Oct 09 21:43:11 2024 +0200
@@ -41,6 +41,7 @@
     UiList *list;
     UiList *menulist;
     UiInteger *radio;
+    UiInteger *tabview;
 } MyDocument;
 
 MyDocument *doc1;
@@ -85,6 +86,7 @@
     ui_list_append(doc->list, "test2");
     ui_list_append(doc->list, "test3");
     doc->radio = ui_int_new(docctx, "radio");
+    doc->tabview = ui_int_new(docctx, "tabview");
     return doc;
 }
 
@@ -119,6 +121,13 @@
     printf("menu list item: %d\n", event->intval);
 }
 
+static int tab_x = 0;
+void action_tab2_button(UiEvent *event, void *userdata) {
+    MyDocument *doc = event->document;
+    printf("current page: %d\n", (int)ui_get(doc->tabview));
+    ui_set(doc->tabview, 0);
+}
+
 void application_startup(UiEvent *event, void *data) {
     // global list
     UiContext *global = ui_global_context();
@@ -134,7 +143,7 @@
     MyDocument *doc = create_doc();
     ui_attach_document(obj->ctx, doc);
     
-    ui_tabview(obj, .spacing=10, .margin=10, .tabview = UI_TABVIEW_NAVIGATION_SIDE) {
+    ui_tabview(obj, .spacing=10, .margin=10, .tabview = UI_TABVIEW_NAVIGATION_SIDE, .varname="tabview") {
         ui_tab(obj, "Tab 1") {
             ui_vbox(obj, .fill = UI_OFF, .margin = 15, .spacing = 15) {
                 ui_button(obj, .label = "Test Button", .icon = "application-x-generic", .onclick = action_button);
@@ -180,7 +189,7 @@
             }
         }
         ui_tab(obj, "Tab 2") {
-            ui_button(obj, .label = "Button");
+            ui_button(obj, .label = "Button", .onclick=action_tab2_button);
         }
     }
     
--- a/ui/gtk/container.c	Tue Oct 08 22:53:27 2024 +0200
+++ b/ui/gtk/container.c	Wed Oct 09 21:43:11 2024 +0200
@@ -361,24 +361,60 @@
 }
 
 
+static int stack_set_page(GtkWidget *stack, int index) {
+    GtkSelectionModel *pages = gtk_stack_get_pages(GTK_STACK(stack));
+    GListModel *list = G_LIST_MODEL(pages);
+    GtkStackPage *page = g_list_model_get_item(list, index);
+    if(page) {
+        gtk_stack_set_visible_child(GTK_STACK(stack), gtk_stack_page_get_child(page));
+    } else {
+        fprintf(stderr, "UI Error: ui_stack_set value out of bounds\n");
+        return -1;
+    }
+    return index;
+}
+
 void ui_stack_tab_select(UIWIDGET tabview, int tab) {
-    
+    stack_set_page(tabview, tab);
 }
 
 void ui_stack_tab_remove(UIWIDGET tabview, int tab) {
-    
+    GtkStack *stack = GTK_STACK(tabview);
+    GtkWidget *current = gtk_stack_get_visible_child(stack);
+    GtkSelectionModel *pages = gtk_stack_get_pages(stack);
+    GListModel *list = G_LIST_MODEL(pages);
+    GtkStackPage *page = g_list_model_get_item(list, tab);
+    if(page) {
+        gtk_stack_remove(stack, gtk_stack_page_get_child(page));
+    }
 }
 
 void ui_stack_tab_add(UIWIDGET widget, int index, const char *name, UIWIDGET child) {
-    GtkStackPage *page = gtk_stack_add_titled(GTK_STACK(widget), child, name, name);
+    (void)gtk_stack_add_titled(GTK_STACK(widget), child, name, name);
 }
 
 int64_t ui_stack_get(UiInteger *i) {
-    
+    GtkStack *stack = GTK_STACK(i->obj);
+    GtkWidget *current = gtk_stack_get_visible_child(stack);
+    GtkSelectionModel *pages = gtk_stack_get_pages(stack);
+    GListModel *list = G_LIST_MODEL(pages);
+    int nitems = g_list_model_get_n_items(list);
+    for(int p=0;p<nitems;p++) {
+        GtkStackPage *page = g_list_model_get_item(list, p);
+        GtkWidget *child = gtk_stack_page_get_child(page);
+        if(child == current) {
+            i->value = p;
+            break;
+        }
+    }
+    return i->value;
 }
 
 void ui_stack_set(UiInteger *i, int64_t value) {
-    
+    GtkWidget *widget = i->obj;
+    if(stack_set_page(widget, value) >= 0) {
+        i->value = value;
+    }
 }
 
 
@@ -451,7 +487,7 @@
         UiInteger *i = var->value;
         i->get = getfunc;
         i->set = setfunc;
-        i->obj = widget;
+        i->obj = data_widget;
     }
     
     g_object_set_data(G_OBJECT(widget), "ui_tabview", data);

mercurial