implement hfill/vfill, def_hfill/def_vfill and override_defaults (GTK)

3 weeks ago

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 16 Feb 2025 21:15:17 +0100 (3 weeks ago)
changeset 465
00735562b25b
parent 464
e06714e6fa12
child 466
7c78b0b8b77d

implement hfill/vfill, def_hfill/def_vfill and override_defaults (GTK)

application/main.c file | annotate | diff | comparison | revisions
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
ui/ui/text.h file | annotate | diff | comparison | revisions
ui/ui/tree.h file | annotate | diff | comparison | revisions
--- a/application/main.c	Sun Feb 16 20:57:04 2025 +0100
+++ b/application/main.c	Sun Feb 16 21:15:17 2025 +0100
@@ -478,13 +478,28 @@
                 ui_button(obj, .label = "Button 2");
             }
         }
-      ui_tab(obj, "Tab 9") {
+        ui_tab(obj, "Tab 9") {
 #ifdef UI_WEBVIEW
           ui_webview(obj, .fill = UI_ON);
 #else
           ui_label(obj, "Webview is not supported");
 #endif
-      }  
+        }
+        ui_tab(obj, "Tab 10") {
+            ui_grid(obj, .margin = 10, .rowspacing = 10, .columnspacing = 10, .def_vfill = TRUE) {
+                ui_label(obj, .label = "Name");
+                ui_textfield(obj, .hexpand = TRUE);
+                ui_newline(obj);
+                
+                ui_label(obj, .label = "No vfill", .override_defaults = TRUE);
+                ui_textfield(obj, .hexpand = TRUE);
+                ui_newline(obj);
+                
+                ui_label(obj, .label = "Manual vfill", .override_defaults = TRUE, .vfill = TRUE);
+                ui_textfield(obj, .hexpand = TRUE);
+                ui_newline(obj);
+            }
+        }
     }
     
     /*
--- a/ui/gtk/container.c	Sun Feb 16 20:57:04 2025 +0100
+++ b/ui/gtk/container.c	Sun Feb 16 21:15:17 2025 +0100
@@ -107,7 +107,7 @@
         case UI_CONTAINER_GRID: {
             sub = ui_create_grid_widget(columnspacing, rowspacing);
             add = ui_box_set_margin(sub, margin);
-            newobj->container = ui_grid_container(newobj, sub);
+            newobj->container = ui_grid_container(newobj, sub, FALSE, FALSE, FALSE, FALSE);
             newobj->widget = sub;
             break;
         }
@@ -167,11 +167,22 @@
     ct->current = widget;
 }
 
-UiContainer* ui_grid_container(UiObject *obj, GtkWidget *grid) {
+UiContainer* ui_grid_container(
+        UiObject *obj,
+        GtkWidget *grid,
+        UiBool def_hexpand,
+        UiBool def_vexpand,
+        UiBool def_hfill,
+        UiBool def_vfill)
+{
     UiGridContainer *ct = cxCalloc(
             obj->ctx->allocator,
             1,
             sizeof(UiGridContainer));
+    ct->def_hexpand = def_hexpand;
+    ct->def_vexpand = def_vexpand;
+    ct->def_hfill = def_hfill;
+    ct->def_vfill = def_vfill;
     ct->container.widget = grid;
     ct->container.add = ui_grid_container_add;
     UI_GTK_V2(ct->width = 0);
@@ -193,15 +204,34 @@
     int vexpand = FALSE;
     int hfill = FALSE;
     int vfill = FALSE;
+    if(!ct->layout.override_defaults) {
+        if(grid->def_hexpand) {
+            hexpand = TRUE;
+            hfill = TRUE;
+        } else if(grid->def_hfill) {
+            hfill = TRUE;
+        }
+        if(grid->def_vexpand) {
+            vexpand = TRUE;
+            vfill = TRUE;
+        } else if(grid->def_vfill) {
+            vfill = TRUE;
+        }
+    }
+    
     if(ct->layout.fill != UI_LAYOUT_UNDEFINED) {
         fill = ui_lb2bool(ct->layout.fill);
     }
-    if(ct->layout.hexpand != UI_LAYOUT_UNDEFINED) {
-        hexpand = ct->layout.hexpand;
+    if(ct->layout.hexpand) {
+        hexpand = TRUE;
+        hfill = TRUE;
+    } else if(ct->layout.hfill) {
         hfill = TRUE;
     }
-    if(ct->layout.vexpand != UI_LAYOUT_UNDEFINED) {
-        vexpand = ct->layout.vexpand;
+    if(ct->layout.vexpand) {
+        vexpand = TRUE;
+        vfill = TRUE;
+    } else if(ct->layout.vfill) {
         vfill = TRUE;
     }
     if(fill) {
@@ -241,14 +271,57 @@
     
     int hexpand = FALSE;
     int vexpand = FALSE;
-    if(ct->layout.hexpand != UI_LAYOUT_UNDEFINED) {
-        hexpand = ct->layout.hexpand;
+    int hfill = FALSE;
+    int vfill = FALSE;
+    if(!ct->layout.override_defaults) {
+        if(grid->def_hexpand) {
+            hexpand = TRUE;
+            hfill = TRUE;
+        } else if(grid->def_hfill) {
+            hfill = TRUE;
+        }
+        if(grid->def_vexpand) {
+            vexpand = TRUE;
+            vfill = TRUE;
+        } else if(grid->def_vfill) {
+            vfill = TRUE;
+        }
+    }
+    
+    if(ct->layout.fill != UI_LAYOUT_UNDEFINED) {
+        fill = ui_lb2bool(ct->layout.fill);
+    }
+    if(ct->layout.hexpand) {
+        hexpand = TRUE;
+        hfill = TRUE;
+    } else if(ct->layout.hfill) {
+        hfill = TRUE;
     }
-    if(ct->layout.vexpand != UI_LAYOUT_UNDEFINED) {
-        vexpand = ct->layout.vexpand;
+    if(ct->layout.vexpand) {
+        vexpand = TRUE;
+        vfill = TRUE;
+    } else if(ct->layout.vfill) {
+        vfill = TRUE;
+    }
+    if(fill) {
+        hfill = TRUE;
+        vfill = TRUE;
     }
-    GtkAttachOptions xoptions = hexpand ? GTK_FILL | GTK_EXPAND : GTK_FILL;
-    GtkAttachOptions yoptions = vexpand ? GTK_FILL | GTK_EXPAND : GTK_FILL;
+    
+    GtkAttachOptions xoptions = 0;
+    GtkAttachOptions yoptions = 0;
+    if(hexpand) {
+        xoptions = GTK_EXPAND;
+    }
+    if(hfill) {
+        xoptions |= GTK_FILL;
+    }
+    if(vexpand) {
+        yoptions = GTK_EXPAND;
+    }
+    if(vfill) {
+        yoptions |= GTK_FILL;
+    }
     
     int colspan = ct->layout.colspan > 0 ? ct->layout.colspan : 1;
     int rowspan = ct->layout.rowspan > 0 ? ct->layout.rowspan : 1;
@@ -407,7 +480,7 @@
     current->container->add(current->container, widget, TRUE);
     
     UiObject *newobj = uic_object_new(obj, grid);
-    newobj->container = ui_grid_container(obj, grid);
+    newobj->container = ui_grid_container(obj, grid, args.def_hexpand, args.def_vexpand, args.def_hfill, args.def_vfill);
     uic_obj_add(obj, newobj);
     
     return widget;
@@ -766,7 +839,7 @@
         }
         case UI_CONTAINER_GRID: {
             sub = ui_create_grid_widget(data->columnspacing, data->rowspacing);
-            newobj->container = ui_grid_container(newobj, sub);
+            newobj->container = ui_grid_container(newobj, sub, FALSE, FALSE, FALSE, FALSE);
             break;
         }
     }
@@ -1175,6 +1248,11 @@
     ct->layout.vfill = fill;
 }
 
+UIEXPORT void ui_layout_override_defaults(UiObject *obj, UiBool d) {
+    UiContainer *ct = uic_get_current_container(obj);
+    ct->layout.override_defaults = d;
+}
+
 void ui_layout_colspan(UiObject* obj, int cols) {
     UiContainer* ct = uic_get_current_container(obj);
     ct->layout.colspan = cols;
--- a/ui/gtk/container.h	Sun Feb 16 20:57:04 2025 +0100
+++ b/ui/gtk/container.h	Sun Feb 16 21:15:17 2025 +0100
@@ -66,6 +66,7 @@
     UiBool       vexpand;
     UiBool       hfill;
     UiBool       vfill;
+    UiBool       override_defaults;
     int          width;
     int          colspan;
     int          rowspan;
@@ -90,6 +91,10 @@
 
 typedef struct UiGridContainer {
     UiContainer container;
+    UiBool def_hexpand;
+    UiBool def_vexpand;
+    UiBool def_hfill;
+    UiBool def_vfill;
     int x;
     int y;
 #ifdef UI_GTK2
@@ -171,7 +176,13 @@
 void ui_box_container_add(UiContainer *ct, GtkWidget *widget, UiBool fill);
 
 GtkWidget* ui_create_grid_widget(int colspacing, int rowspacing);
-UiContainer* ui_grid_container(UiObject *obj, GtkWidget *grid);
+UiContainer* ui_grid_container(
+        UiObject *obj,
+        GtkWidget *grid,
+        UiBool def_hexpand,
+        UiBool def_vexpand,
+        UiBool def_hfill,
+        UiBool def_vfill);
 void ui_grid_container_add(UiContainer *ct, GtkWidget *widget, UiBool fill);
 
 UiContainer* ui_frame_container(UiObject *obj, GtkWidget *frame);
--- a/ui/ui/container.h	Sun Feb 16 20:57:04 2025 +0100
+++ b/ui/ui/container.h	Sun Feb 16 21:15:17 2025 +0100
@@ -86,6 +86,10 @@
     int spacing;
     int columnspacing;
     int rowspacing;
+    UiBool def_hfill;
+    UiBool def_vfill;
+    UiBool def_hexpand;
+    UiBool def_vexpand;
 } UiContainerArgs;
 
 typedef struct UiFrameArgs {
@@ -314,6 +318,7 @@
 UIEXPORT void ui_layout_vexpand(UiObject *obj, UiBool expand);
 UIEXPORT void ui_layout_hfill(UiObject *obj, UiBool fill);
 UIEXPORT void ui_layout_vfill(UiObject *obj, UiBool fill);
+UIEXPORT void ui_layout_override_defaults(UiObject *obj, UiBool d);
 UIEXPORT void ui_layout_width(UiObject *obj, int width);
 UIEXPORT void ui_layout_height(UiObject* obj, int width);
 UIEXPORT void ui_layout_colspan(UiObject *obj, int cols);
@@ -347,6 +352,7 @@
     if(args.vexpand) ui_layout_vexpand(obj, 1); \
     if(args.hfill) ui_layout_hfill(obj, 1); \
     if(args.vfill) ui_layout_vfill(obj, 1); \
+    if(args.override_defaults) ui_layout_override_defaults(obj, 1); \
     if(args.colspan > 0) ui_layout_colspan(obj, args.colspan); \
     if(args.rowspan > 0) ui_layout_rowspan(obj, args.rowspan); \
     /*force caller to add ';'*/(void)0
--- a/ui/ui/text.h	Sun Feb 16 20:57:04 2025 +0100
+++ b/ui/ui/text.h	Sun Feb 16 21:15:17 2025 +0100
@@ -96,6 +96,7 @@
     UiBool vexpand;
     UiBool hfill;
     UiBool vfill;
+    UiBool override_defaults;
     int colspan;
     int rowspan;
     const char *name;
--- a/ui/ui/tree.h	Sun Feb 16 20:57:04 2025 +0100
+++ b/ui/ui/tree.h	Sun Feb 16 21:15:17 2025 +0100
@@ -174,6 +174,7 @@
     UiBool vexpand;
     UiBool hfill;
     UiBool vfill;
+    UiBool override_defaults;
     int colspan;
     int rowspan;
     const char *name;

mercurial