add splitpane (GTK)

Tue, 08 Dec 2020 14:00:12 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Tue, 08 Dec 2020 14:00:12 +0100
changeset 165
3f32db79a76e
parent 164
1d912f78fd1d
child 166
6e48030cf2db

add splitpane (GTK)

ui/gtk/container.c file | annotate | diff | comparison | revisions
ui/gtk/container.h file | annotate | diff | comparison | revisions
ui/ui/container.h file | annotate | diff | comparison | revisions
--- a/ui/gtk/container.c	Sun Dec 06 14:41:37 2020 +0100
+++ b/ui/gtk/container.c	Tue Dec 08 14:00:12 2020 +0100
@@ -28,8 +28,11 @@
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <limits.h>
 
 #include "container.h"
+#include "toolkit.h"
+
 #include "../common/context.h"
 #include "../common/object.h"
 
@@ -366,8 +369,88 @@
     gtk_notebook_set_current_page(GTK_NOTEBOOK(tabview), tab);
 }
 
+/* -------------------- Splitpane -------------------- */
 
-/* -------------------- Sidebar -------------------- */
+static GtkWidget* create_paned(UiOrientation orientation) {
+#ifdef UI_GTK3
+    switch(orientation) {
+        case UI_HORIZONTAL: return gtk_paned_new(GTK_ORIENTATION_HORIZONTAL);
+        case UI_VERTICAL: return gtk_paned_new(GTK_ORIENTATION_VERTICAL);
+    }
+#else
+    switch(orientation) {
+        case UI_HORIZONTAL: return gtk_hpaned_new();
+        case UI_VERTICAL: return gtk_vpaned_new();
+    }
+#endif
+    return NULL;
+}
+
+UIWIDGET ui_splitpane(UiObject *obj, int max, UiOrientation orientation) {
+    GtkWidget *paned = create_paned(orientation);
+    UiContainer *ct = uic_get_current_container(obj);
+    ct->add(ct, paned, TRUE);
+    
+    if(max <= 0) max = INT_MAX;
+    
+    UiPanedContainer *pctn = ucx_mempool_calloc(
+            obj->ctx->mempool,
+            1,
+            sizeof(UiPanedContainer));
+    pctn->container.widget = paned;
+    pctn->container.add = ui_paned_container_add;
+    pctn->current_pane = paned;
+    pctn->orientation = orientation;
+    pctn->max = max;
+    pctn->cur = 0;
+    
+    UiObject *pobj = uic_object_new(obj, paned);
+    pobj->container = (UiContainer*)pctn;
+    
+    uic_obj_add(obj, pobj);
+    
+    return paned;
+}
+
+UIWIDGET ui_hsplitpane(UiObject *obj, int max) {
+    return ui_splitpane(obj, max, UI_HORIZONTAL);
+}
+
+UIWIDGET ui_vsplitpane(UiObject *obj, int max) {
+    return ui_splitpane(obj, max, UI_VERTICAL);
+}
+
+void ui_paned_container_add(UiContainer *ct, GtkWidget *widget, UiBool fill) {
+    UiPanedContainer *pctn = (UiPanedContainer*)ct;
+    
+    gboolean resize = (ct->layout.hexpand || ct->layout.vexpand) ? TRUE : FALSE;
+    int width = ct->layout.width;
+    ui_reset_layout(ct->layout);
+    
+    if(pctn->cur == 0) {
+        gtk_paned_pack1(GTK_PANED(pctn->current_pane), widget, resize, resize);
+    } else if(pctn->cur < pctn->max-1) {
+        GtkWidget *nextPane = create_paned(pctn->orientation);
+        gtk_paned_pack2(GTK_PANED(pctn->current_pane), nextPane, TRUE, TRUE);
+        gtk_paned_pack1(GTK_PANED(nextPane), widget, resize, resize);
+        pctn->current_pane = nextPane;
+    } else if(pctn->cur == pctn->max-1) {
+        gtk_paned_pack2(GTK_PANED(pctn->current_pane), widget, resize, resize);
+        width = 0; // disable potential call of gtk_paned_set_position
+    } else {
+        fprintf(stderr, "Splitpane max reached: %d\n", pctn->max);
+        return;
+    }
+    
+    if(width > 0) {
+        gtk_paned_set_position(GTK_PANED(pctn->current_pane), width);
+    }
+    
+    pctn->cur++;
+}
+
+
+/* -------------------- Sidebar (deprecated) -------------------- */
 UIWIDGET ui_sidebar(UiObject *obj) {
 #ifdef UI_GTK3
     GtkWidget *paned = gtk_paned_new(GTK_ORIENTATION_HORIZONTAL);
@@ -512,6 +595,11 @@
     ct->layout.vexpand = expand;
 }
 
+void ui_layout_width(UiObject *obj, int width) {
+    UiContainer *ct = uic_get_current_container(obj);
+    ct->layout.width = width;
+}
+
 void ui_layout_gridwidth(UiObject *obj, int width) {
     UiContainer *ct = uic_get_current_container(obj);
     ct->layout.gridwidth = width;
--- a/ui/gtk/container.h	Sun Dec 06 14:41:37 2020 +0100
+++ b/ui/gtk/container.h	Tue Dec 08 14:00:12 2020 +0100
@@ -60,6 +60,7 @@
     char         *label;
     UiBool       hexpand;
     UiBool       vexpand;
+    int          width;
     int          gridwidth;
 };
 
@@ -87,6 +88,14 @@
 #endif
 } UiGridContainer;
 
+typedef struct UiPanedContainer {
+    UiContainer container;
+    GtkWidget *current_pane;
+    int orientation;
+    int max;
+    int cur;
+} UiPanedContainer;
+
 typedef struct UiTabViewContainer {
     UiContainer container;
 } UiTabViewContainer;
@@ -109,6 +118,8 @@
 UiContainer* ui_tabview_container(UiObject *obj, GtkWidget *tabview);
 void ui_tabview_container_add(UiContainer *ct, GtkWidget *widget, UiBool fill);
 
+void ui_paned_container_add(UiContainer *ct, GtkWidget *widget, UiBool fill);
+
 void ui_split_container_add1(UiContainer *ct, GtkWidget *widget, UiBool fill);
 void ui_split_container_add2(UiContainer *ct, GtkWidget *widget, UiBool fill);
 
--- a/ui/ui/container.h	Sun Dec 06 14:41:37 2020 +0100
+++ b/ui/ui/container.h	Tue Dec 08 14:00:12 2020 +0100
@@ -49,6 +49,9 @@
 
 UIWIDGET ui_sidebar(UiObject *obj);
 
+UIWIDGET ui_hsplitpane(UiObject *obj, int max);
+UIWIDGET ui_vsplitpane(UiObject *obj, int max);
+
 UIWIDGET ui_tabview(UiObject *obj);
 void ui_tab(UiObject *obj, char *title);
 void ui_select_tab(UIWIDGET tabview, int tab);
@@ -58,6 +61,7 @@
 // grid container layout functions
 void ui_layout_hexpand(UiObject *obj, UiBool expand);
 void ui_layout_vexpand(UiObject *obj, UiBool expand);
+void ui_layout_width(UiObject *obj, int width);
 void ui_layout_gridwidth(UiObject *obj, int width);
 void ui_newline(UiObject *obj);
 

mercurial