fix grid spacing (Cocoa)

Sun, 05 Oct 2025 12:34:42 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 05 Oct 2025 12:34:42 +0200
changeset 799
f8ff8df41713
parent 798
504c45926386
child 800
814d374fb689

fix grid spacing (Cocoa)

make/xcode/toolkit/toolkit/main.m file | annotate | diff | comparison | revisions
ui/cocoa/BoxContainer.m file | annotate | diff | comparison | revisions
ui/cocoa/GridLayout.h file | annotate | diff | comparison | revisions
ui/cocoa/GridLayout.m file | annotate | diff | comparison | revisions
ui/cocoa/container.h file | annotate | diff | comparison | revisions
ui/cocoa/container.m file | annotate | diff | comparison | revisions
--- a/make/xcode/toolkit/toolkit/main.m	Sat Oct 04 18:33:58 2025 +0200
+++ b/make/xcode/toolkit/toolkit/main.m	Sun Oct 05 12:34:42 2025 +0200
@@ -133,17 +133,20 @@
     
     ui_button(obj, .label = "HBox Button 1");
     ui_button(obj, .label = "HBox Button 2");
-    ui_grid(obj, .fill = TRUE) {
-        ui_hbox(obj, .spacing = 0, .colspan = 2) {
+    ui_grid(obj, .fill = TRUE, .columnspacing = 20, .rowspacing = 10) {
+        ui_hbox(obj, .spacing = 8, .colspan = 1) {
             ui_button(obj, .label = "HBox Button 1");
             ui_button(obj, .label = "HBox Button 2");
             ui_button(obj, .label = "HBox Button 3");
+            ui_button(obj, .label = "HBox Button 4");
         }
         ui_newline(obj);
         
         ui_spinbox(obj, .width = 300, .varname = "number", .digits = 2, .step = 0.1);
+        ui_button(obj, .label = "spinbox", .hfill = TRUE);
         ui_newline(obj);
         ui_textfield(obj, .width = 300);
+        ui_button(obj, .label = "textfield", .hfill = TRUE);
     }
     ui_button(obj, .label = "HBox Button 3");
     
--- a/ui/cocoa/BoxContainer.m	Sat Oct 04 18:33:58 2025 +0200
+++ b/ui/cocoa/BoxContainer.m	Sun Oct 05 12:34:42 2025 +0200
@@ -13,7 +13,7 @@
     return self;
 }
 
-- (void) addView:(NSView*)view {
+- (void) addView:(NSView*)view  margin:(NSEdgeInsets)margin {
     UiLayout layout = self.uilayout;
     if(_orientation == NSUserInterfaceLayoutOrientationVertical) {
         layout.hexpand = TRUE;
@@ -24,7 +24,7 @@
         self.newline = FALSE;
     }
     self.uilayout = layout;
-    [super addView:view];
+    [super addView:view margin:margin];
     if(_orientation == NSUserInterfaceLayoutOrientationVertical) {
         self.newline = TRUE;
     }
--- a/ui/cocoa/GridLayout.h	Sat Oct 04 18:33:58 2025 +0200
+++ b/ui/cocoa/GridLayout.h	Sun Oct 05 12:34:42 2025 +0200
@@ -34,7 +34,7 @@
 
 typedef struct GridElm {
     NSView *view;
-    int margin;
+    NSEdgeInsets margin;
     int x;
     int y;
     int colspan;
--- a/ui/cocoa/GridLayout.m	Sat Oct 04 18:33:58 2025 +0200
+++ b/ui/cocoa/GridLayout.m	Sun Oct 05 12:34:42 2025 +0200
@@ -184,17 +184,23 @@
     int preferred_width = 0;
     int preferred_height = 0;
     for(int j=0;j<ncols;j++) {
-        preferred_width += cols[j].preferred_size + colspacing;
+        preferred_width += cols[j].preferred_size;
         if(cols[j].expand) {
             col_ext++;
         }
     }
     for(int j=0;j<nrows;j++) {
-        preferred_height += rows[j].preferred_size + rowspacing;
+        preferred_height += rows[j].preferred_size;
         if(rows[j].expand) {
             row_ext++;
         }
     }
+    if(ncols > 0) {
+        preferred_width += (ncols-1) * colspacing;
+    }
+    if(nrows > 0) {
+        preferred_height += (nrows-1) * rowspacing;
+    }
     
     _preferredSize.width = preferred_width;
     _preferredSize.height = preferred_height;
@@ -292,7 +298,7 @@
     return self.preferredSize;
 }
 
-- (void) addView:(NSView*)view {
+- (void) addView:(NSView*)view margin:(NSEdgeInsets)margin {
     _preferredSize.width = -1;
     _preferredSize.height = -1;
     
@@ -305,7 +311,7 @@
     GridElm elm;
     elm.x = _x;
     elm.y = _y;
-    elm.margin = 0;
+    elm.margin = margin;
     elm.colspan = _uilayout.colspan;
     elm.rowspan = _uilayout.rowspan;
     if(_uilayout.fill) {
--- a/ui/cocoa/container.h	Sat Oct 04 18:33:58 2025 +0200
+++ b/ui/cocoa/container.h	Sun Oct 05 12:34:42 2025 +0200
@@ -71,7 +71,7 @@
 @property const char *label;
 @property UiBool newline;
 
-- (void) addView:(NSView*)view;
+- (void) addView:(NSView*)view margin:(NSEdgeInsets)margin;
 
 @end
 
--- a/ui/cocoa/container.m	Sat Oct 04 18:33:58 2025 +0200
+++ b/ui/cocoa/container.m	Sun Oct 05 12:34:42 2025 +0200
@@ -120,6 +120,8 @@
 UIWIDGET ui_grid_create(UiObject *obj, UiContainerArgs *args) {
     GridLayout *grid = [[GridLayout alloc] init];
     grid.translatesAutoresizingMaskIntoConstraints = false;
+    grid.columnspacing = args->columnspacing;
+    grid.rowspacing = args->rowspacing;
     
     // add box to the parent
     UiLayout layout = UI_INIT_LAYOUT(args);
@@ -160,7 +162,8 @@
     UiContainerX *ctn = obj->container_end;
     id<Container> container = (__bridge id<Container>)ctn->container;
     container.uilayout = *layout;
-    [container addView:view];
+    NSEdgeInsets margin = {0}; // TODO
+    [container addView:view margin:margin];
 }
 
 /* ---------------------- public layout functions ----------------------- */

mercurial