implement box container (win32)

Wed, 08 Oct 2025 16:09:54 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Wed, 08 Oct 2025 16:09:54 +0200
changeset 818
8185cd761897
parent 817
d09817e6e6a4
child 819
5f6ca885d048

implement box container (win32)

application/main.c file | annotate | diff | comparison | revisions
ui/win32/container.c file | annotate | diff | comparison | revisions
ui/win32/container.h file | annotate | diff | comparison | revisions
ui/win32/grid.c file | annotate | diff | comparison | revisions
ui/win32/grid.h file | annotate | diff | comparison | revisions
ui/win32/window.c file | annotate | diff | comparison | revisions
ui/win32/window.h file | annotate | diff | comparison | revisions
--- a/application/main.c	Wed Oct 08 15:54:33 2025 +0200
+++ b/application/main.c	Wed Oct 08 16:09:54 2025 +0200
@@ -1126,11 +1126,9 @@
 void application_startup(UiEvent *event, void *data) {
 	UiObject *obj = ui_window("Test w32", NULL);
     ui_button(obj, .label = "Test", .hfill = TRUE, .hexpand = TRUE, .colspan = 3, .margin = 10);
-    ui_newline(obj);
-    ui_button(obj, .label = "Test 2-1", .margin_top = 10);
-    ui_button(obj, .label = "Test 2-2", .hfill = TRUE, .hexpand = TRUE, .margin_top = 20);
-    ui_button(obj, .label = "Test 2-3", .margin_top = 30);
-    ui_newline(obj);
+    ui_button(obj, .label = "Test 2-1", .margin_left = 10);
+    ui_button(obj, .label = "Test 2-2", .hfill = TRUE, .hexpand = TRUE, .margin_left = 20);
+    ui_button(obj, .label = "Test 2-3", .margin_left = 30);
     ui_button(obj, .label = "Test 3XX", .colspan = 3, .fill = TRUE);
 	ui_show(obj);
 }
--- a/ui/win32/container.c	Wed Oct 08 15:54:33 2025 +0200
+++ b/ui/win32/container.c	Wed Oct 08 16:09:54 2025 +0200
@@ -69,6 +69,39 @@
     return box_create(obj, args, UI_BOX_HORIZONTAL);
 }
 
+UiContainerX* ui_box_container_create(UiObject *obj, HWND hwnd, UiBoxOrientation orientation, short spacing, GridEdgeInsets padding) {
+    UiBoxContainer *container = cxZalloc(obj->ctx->allocator, sizeof(UiBoxContainer));
+    container->container.hwnd = hwnd;
+    container->container.add = ui_box_container_add;
+    container->layout = ui_grid_layout_create(obj->ctx->allocator, spacing, spacing);
+    container->layout->padding = padding;
+    container->orientation = orientation;
+    return (UiContainerX*)container;
+}
+
+void ui_box_container_add(UiContainerPrivate *ctn, W32Widget *widget, UiLayout *layout) {
+    UiBoxContainer *box = (UiBoxContainer*)ctn;
+    GridLayoutInfo gridLayout = (GridLayoutInfo) {
+        .margin = (GridEdgeInsets) { layout->margin_top, layout->margin_bottom, layout->margin_left, layout->margin_right },
+    };
+    if (box->orientation == UI_BOX_HORIZONTAL) {
+        gridLayout.vexpand = TRUE;
+        gridLayout.vfill = TRUE;
+        gridLayout.hexpand = layout->fill;
+        gridLayout.hfill = layout->fill;
+    } else {
+        gridLayout.hexpand = TRUE;
+        gridLayout.hfill = TRUE;
+        gridLayout.vexpand = layout->fill;
+        gridLayout.vfill = layout->fill;
+    }
+    ui_grid_add_widget(box->layout, box->x, box->y, widget, &gridLayout);
+    if (box->orientation == UI_BOX_HORIZONTAL) {
+        box->x++;
+    } else {
+        box->y++;
+    }
+}
 
 /* ---------------------------- Grid Container ---------------------------- */
 
@@ -80,7 +113,7 @@
     UiGridLayoutContainer *container = cxZalloc(obj->ctx->allocator, sizeof(UiGridLayoutContainer));
     container->container.hwnd = hwnd;
     container->container.add = ui_grid_container_add;
-    container->layout = ui_grid_layout_create(obj->ctx->allocator, hwnd, columnspacing, rowspacing);
+    container->layout = ui_grid_layout_create(obj->ctx->allocator, columnspacing, rowspacing);
     container->layout->padding = padding;
     return (UiContainerX*)container;
 }
--- a/ui/win32/container.h	Wed Oct 08 15:54:33 2025 +0200
+++ b/ui/win32/container.h	Wed Oct 08 16:09:54 2025 +0200
@@ -37,8 +37,9 @@
 extern "C" {
 #endif
 
-
+typedef struct UiContainerPrivate    UiContainerPrivate;
 typedef struct UiGridLayoutContainer UiGridLayoutContainer;
+typedef struct UiBoxContainer        UiBoxContainer;
 
 enum UiBoxOrientation {
     UI_BOX_VERTICAL = 0,
@@ -52,8 +53,6 @@
 };
 typedef enum UiContainerType UiContainerType;
 
-typedef struct UiContainerPrivate UiContainerPrivate;
-
 typedef struct UiRect {
     int x;
     int y;
@@ -70,6 +69,14 @@
     HWND            hwnd;
 };
 
+struct UiBoxContainer {
+    UiContainerPrivate container;
+    UiGridLayout *layout;
+    UiBoxOrientation orientation;
+    int x;
+    int y;
+};
+
 struct UiGridLayoutContainer {
     UiContainerPrivate container;
     UiGridLayout *layout;
@@ -85,6 +92,9 @@
 HWND ui_container_get_parent(UiContainerPrivate *ctn);
 void ui_container_add(UiContainerPrivate *ctn, W32Widget *widget, UiLayout *layout);
 
+UiContainerX* ui_box_container_create(UiObject *obj, HWND hwnd, UiBoxOrientation orientation, short spacing, GridEdgeInsets padding);
+void ui_box_container_add(UiContainerPrivate *ctn, W32Widget *widget, UiLayout *layout);
+
 UiContainerX* ui_grid_container_create(
     UiObject *obj,
     HWND hwnd,
--- a/ui/win32/grid.c	Wed Oct 08 15:54:33 2025 +0200
+++ b/ui/win32/grid.c	Wed Oct 08 16:09:54 2025 +0200
@@ -34,9 +34,8 @@
 #include <stdio.h>
 #include <stdlib.h>
 
-UiGridLayout* ui_grid_layout_create(const CxAllocator *a, HWND control, short columnspacing, short rowspacing) {
+UiGridLayout* ui_grid_layout_create(const CxAllocator *a, short columnspacing, short rowspacing) {
     UiGridLayout *grid = cxZalloc(a, sizeof(UiGridLayout));
-    grid->hwnd = control;
     grid->widgets = cxArrayListCreate(a, NULL, sizeof(GridElm), 32);
     grid->columnspacing = columnspacing;
     grid->rowspacing = rowspacing;
--- a/ui/win32/grid.h	Wed Oct 08 15:54:33 2025 +0200
+++ b/ui/win32/grid.h	Wed Oct 08 16:09:54 2025 +0200
@@ -62,8 +62,6 @@
 } GridElm;
 
 typedef struct UiGridLayout {
-    HWND hwnd;
-
     GridEdgeInsets padding;
     short columnspacing;
     short rowspacing;
@@ -78,7 +76,6 @@
 
     int max_column;
     int max_row;
-
 } UiGridLayout;
 
 typedef struct GridDef {
@@ -88,7 +85,7 @@
     BOOLEAN expand;
 } GridDef;
 
-UiGridLayout* ui_grid_layout_create(const CxAllocator *a, HWND control, short columnspacing, short rowspacing);
+UiGridLayout* ui_grid_layout_create(const CxAllocator *a, short columnspacing, short rowspacing);
 
 void ui_grid_add_widget(
     UiGridLayout *grid,
--- a/ui/win32/window.c	Wed Oct 08 15:54:33 2025 +0200
+++ b/ui/win32/window.c	Wed Oct 08 16:09:54 2025 +0200
@@ -103,13 +103,12 @@
 
     UpdateWindow(hwnd);
 
-	// TODO: switch to box container
-	UiContainerX *container = ui_grid_container_create(obj, hwnd, 0, 0, INSETS_ZERO);
+	UiContainerX *container = ui_box_container_create(obj, hwnd, UI_BOX_VERTICAL, 0, INSETS_ZERO);
 	uic_object_push_container(obj, container);
 
 	UiWindow *widget = w32_widget_create(&w32_toplevel_widget_class, hwnd, sizeof(UiWindow));
 	widget->obj = obj;
-	widget->container = (UiGridLayoutContainer *)container;
+	widget->container = (UiBoxContainer *)container;
 	obj->widget = (W32Widget*)widget;
 	obj->ref = 1;
 
--- a/ui/win32/window.h	Wed Oct 08 15:54:33 2025 +0200
+++ b/ui/win32/window.h	Wed Oct 08 16:09:54 2025 +0200
@@ -44,7 +44,7 @@
 typedef struct UiWindow {
     W32Widget widget;
     UiObject *obj;
-    UiGridLayoutContainer *container;
+    UiBoxContainer *container;
 } UiWindow;
 
 void ui_window_init(void);

mercurial