# HG changeset patch # User Olaf Wintermann # Date 1607432412 -3600 # Node ID 3f32db79a76ef1e4c91c0df0dd3c733dd7b36c77 # Parent 1d912f78fd1d76530f182c8597bc4da80dbebd58 add splitpane (GTK) diff -r 1d912f78fd1d -r 3f32db79a76e ui/gtk/container.c --- 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 #include +#include #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; diff -r 1d912f78fd1d -r 3f32db79a76e ui/gtk/container.h --- 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); diff -r 1d912f78fd1d -r 3f32db79a76e ui/ui/container.h --- 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);