ui/qt/container.cpp

changeset 112
c3f2f16fa4b8
parent 109
c3dfcb8f0be7
--- a/ui/qt/container.cpp	Sat Oct 04 14:54:25 2025 +0200
+++ b/ui/qt/container.cpp	Sun Oct 19 21:20:08 2025 +0200
@@ -29,6 +29,7 @@
 #include <stdio.h>
 #include "container.h"
 #include "../common/object.h"
+#include "../common/container.h"
 
 #include <cx/mempool.h>
 
@@ -41,34 +42,55 @@
     delete ct;
 }
 
-void ui_container_add(UiObject *obj, UiContainerPrivate *ct) {
+void ui_obj_add_container(UiObject *obj, UiContainerPrivate *ct) {
     UiContainerX *container = (UiContainerX*)ui_malloc(obj->ctx, sizeof(UiContainerX));
     container->close = 0;
     container->container = ct;
     container->prev = NULL;
     container->next = NULL;
+    ct->container = container;
     cxMempoolRegister(obj->ctx->mp, ct, (cx_destructor_func)delete_container);
     uic_object_push_container(obj, container);
 }
 
+/* ------------------------ margin helper ------------------------ */
+
+QWidget* ui_widget_set_margin(QWidget *w, int left, int right, int top, int bottom) {
+    if((left | right | top | bottom) == 0) {
+        return w;
+    }
+    QWidget* wrapper = new QWidget;
+    QVBoxLayout* inner = new QVBoxLayout(wrapper);
+    inner->setContentsMargins(left, top, right, bottom);
+    inner->addWidget(w);
+    
+    // TODO: handle widget visibility changes
+    
+    return wrapper;
+}
+
 /* -------------------- UiBoxContainer -------------------- */
 
 UiBoxContainer::UiBoxContainer(QBoxLayout* box) {
     this->box = box;
+    this->direction = box->direction();
     box->setContentsMargins(QMargins(0,0,0,0));
     box->setSpacing(0);
-    
-    ui_reset_layout(layout);
 }
 
-void UiBoxContainer::add(QWidget* widget) {
+void UiBoxContainer::add(QWidget* widget, UiLayout& layout) {
     bool fill = layout.fill;
     if(hasStretchedWidget && fill) {
         fill = false;
         fprintf(stderr, "UiError: container has 2 filled widgets");
     }
     
+    uic_layout_setup_margin(&layout);
+    widget = ui_widget_set_margin(widget, layout.margin_left, layout.margin_right, layout.margin_top, layout.margin_bottom);
     box->addWidget(widget);
+    if(direction == Qt::LeftToRight) {
+        box->setAlignment(widget, Qt::AlignTop);
+    }
     
     if(!hasStretchedWidget) {
         QSpacerItem *newspace = new QSpacerItem(0, 0, QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
@@ -80,20 +102,19 @@
     if(fill) {
         hasStretchedWidget = true;
     }
-    ui_reset_layout(layout);
     current = widget;
 }
 
 UIWIDGET ui_box(UiObject *obj, UiContainerArgs *args, QBoxLayout::Direction dir) {
     UiContainerPrivate *ctn = (UiContainerPrivate*)ui_obj_container(obj);
-    UI_APPLY_LAYOUT(ctn->layout, args);
+    UiLayout layout = UI_ARGS2LAYOUT(args);
     
     QWidget *widget = new QWidget();
     QBoxLayout *box = new QBoxLayout(dir);
     widget->setLayout(box);
-    ctn->add(widget);
+    ctn->add(widget, layout);
     
-    ui_container_add(obj, new UiBoxContainer(box));
+    ui_obj_add_container(obj, new UiBoxContainer(box));
     
     return widget;
 }
@@ -128,77 +149,44 @@
     grid->setContentsMargins(QMargins(margin, margin, margin, margin));
     grid->setHorizontalSpacing(columnspacing);
     grid->setVerticalSpacing(rowspacing);
-    ui_reset_layout(layout);
 }
 
-void UiGridContainer::add(QWidget* widget) {
-    if(layout.newline) {
+void UiGridContainer::add(QWidget* widget, UiLayout& layout) {
+    if(container->newline) {
         x = 0;
         y++;
+        container->newline = false;
     }
     
-    bool fill = layout.fill;
-    bool hexpand = false;
-    bool vexpand = false;
-    bool hfill = false;
-    bool vfill = false;
-    if(!layout.override_defaults) {
-        if(def_hexpand) {
-            hexpand = true;
-            hfill = true;
-        } else if(def_hfill) {
-            hfill = true;
-        }
-        if(def_vexpand) {
-            vexpand = true;
-            vfill = true;
-        } else if(def_vfill) {
-            vfill = true;
-        }
+    uic_layout_setup_expand_fill(&layout, def_hexpand, def_vexpand, def_hfill, def_vfill);
+    uic_layout_setup_margin(&layout);
+    
+    if(layout.hexpand) {
+        col_expanding = true;
+    }
+    if(layout.vexpand) {
+        row_expanding = true;
     }
     
     if(layout.hexpand) {
-        hexpand = true;
-        //hfill = true;
-    } else if(layout.hfill) {
-        hfill = true;
+        grid->setColumnStretch(x, 1);
     }
     if(layout.vexpand) {
-        vexpand = true;
-        //vfill = true;
-    } else if(layout.vfill) {
-        vfill = true;
-    }
-    if(fill) {
-        hfill = true;
-        vfill = true;
-    }
-    
-    if(hexpand) {
-        col_expanding = true;
-    }
-    if(vexpand) {
-        row_expanding = true;
-    }
-    
-    if(hexpand) {
-        grid->setColumnStretch(x, 1);
-    }
-    if(vexpand) {
         grid->setRowStretch(y, 1);
     }
     
     Qt::Alignment alignment = 0;
-    if(!hfill) {
+    if(!layout.hfill) {
         alignment = Qt::AlignLeft;
     }
-    if(!vfill) {
+    if(!layout.vfill) {
         alignment = Qt::AlignTop;
     }
     
     int colspan = layout.colspan > 0 ? layout.colspan : 1;
     int rowspan = layout.rowspan > 0 ? layout.rowspan : 1;
     
+    widget = ui_widget_set_margin(widget, layout.margin_left, layout.margin_right, layout.margin_top, layout.margin_bottom);
     grid->addWidget(widget, y, x, rowspan, colspan, alignment);
     
     if(x > max_x) {
@@ -210,7 +198,6 @@
     
     x += colspan;
     
-    ui_reset_layout(layout);
     current = widget;
 }
 
@@ -231,14 +218,14 @@
 
 UIWIDGET ui_grid_create(UiObject *obj, UiContainerArgs *args) {
     UiContainerPrivate *ctn = (UiContainerPrivate*)ui_obj_container(obj);
-    UI_APPLY_LAYOUT(ctn->layout, args);
+    UiLayout layout = UI_ARGS2LAYOUT(args);
     
     QWidget *widget = new QWidget();
     QGridLayout *grid = new QGridLayout();
     widget->setLayout(grid);
-    ctn->add(widget);
+    ctn->add(widget, layout);
     
-    ui_container_add(obj, new UiGridContainer(
+    ui_obj_add_container(obj, new UiGridContainer(
             grid,
             args->margin,
             args->columnspacing,
@@ -267,7 +254,7 @@
     widget->setLayout(box);
     dock->setWidget(widget);
     
-    ui_container_add(obj, new UiBoxContainer(box));
+    ui_obj_add_container(obj, new UiBoxContainer(box));
     
     return dock;
 }
@@ -288,15 +275,3 @@
     return 1;
 }
 
-
-/*
- * -------------------- Layout Functions --------------------
- * 
- * functions for setting layout attributes for the current container
- *
- */
-
-void ui_newline(UiObject *obj) {
-    UiContainerPrivate *ct = ui_obj_container(obj);
-    ct->layout.newline = TRUE;
-}

mercurial