ui/cocoa/GridLayout.m

changeset 112
c3f2f16fa4b8
parent 110
c00e968d018b
--- a/ui/cocoa/GridLayout.m	Sat Oct 04 14:54:25 2025 +0200
+++ b/ui/cocoa/GridLayout.m	Sun Oct 19 21:20:08 2025 +0200
@@ -32,9 +32,7 @@
 
 @implementation GridLayout
 
-@synthesize label=_label;
-@synthesize uilayout=_uilayout;
-@synthesize newline=_newline;
+@synthesize container = _container;
 
 - (GridLayout*)init {
     self = [super init];
@@ -58,6 +56,9 @@
 }
  */
 
+- (BOOL)isFlipped {
+    return YES;
+}
 
 - (void) layout {
     int ncols = _cols+1;
@@ -66,7 +67,8 @@
     GridDef *cols = calloc(ncols, sizeof(GridDef));
     GridDef *rows = calloc(nrows, sizeof(GridDef));
     
-    NSRect viewFrame = self.frame;
+    //NSRect viewFrame = self.frame;
+    NSRect viewFrame = self.bounds;
     
     int colspacing = _columnspacing;
     int rowspacing = _rowspacing;
@@ -89,21 +91,21 @@
                 size.height = size2.height;
             }
             if(size.width != NSViewNoIntrinsicMetric) {
-                CGFloat width = size.width;
+                CGFloat width = size.width + elm->margin.left + elm->margin.right;
                 if(width > cols[elm->x].preferred_size && elm->colspan <= 1 && span_max == 1) {
                     cols[elm->x].preferred_size = width;
                 }
                 elm->preferred_width = width;
             }
             if(size.height != NSViewNoIntrinsicMetric) {
-                CGFloat height = size.height;
-                //CGFloat height = size.height;
+                CGFloat height = size.height + elm->margin.top + elm->margin.bottom;
                 if(height > rows[elm->y].preferred_size && elm->rowspan <= 1 && span_max == 1) {
                     rows[elm->y].preferred_size = height;
                 }
                 elm->preferred_height = height;
             }
             
+            
             if(elm->rowspan > span_max || elm->colspan > span_max) {
                 continue;
             }
@@ -184,17 +186,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;
@@ -239,7 +247,6 @@
         GridDef *col = &cols[elm->x];
         GridDef *row = &rows[elm->y];
         
-        NSEdgeInsets alignment = elm->view.alignmentRectInsets;
         NSRect frame;
         if(elm->hfill) {
             if(elm->colspan > 1) {
@@ -248,16 +255,22 @@
                 if(end_col > ncols) {
                     end_col = ncols;
                 }
+                int real_span = 0;
                 for(int c=elm->x;c<end_col;c++) {
                     cwidth += cols[c].size;
+                    real_span++;
                 }
-                frame.size.width = cwidth + + alignment.left + alignment.right;
+                if(real_span > 0) {
+                    cwidth += (real_span-1) * colspacing;
+                }
+                frame.size.width = cwidth;
             } else {
-                frame.size.width = col->size + alignment.left + alignment.right;
+                frame.size.width = col->size;
             }
         } else {
-            frame.size.width = elm->preferred_width + alignment.left + alignment.right;
+            frame.size.width = elm->preferred_width;
         }
+        frame.size.width -= elm->margin.left + elm->margin.right;
         if(elm->vfill) {
             if(elm->rowspan > 1) {
                 int rheight = 0;
@@ -265,8 +278,13 @@
                 if(end_row > nrows) {
                     end_row = nrows;
                 }
+                int real_span = 0;
                 for(int r=elm->y;r<end_row;r++) {
                     rheight += rows[r].size;
+                    real_span++;
+                }
+                if(real_span > 0) {
+                    rheight += (real_span-1) * rowspacing;
                 }
                 frame.size.height = rheight;
             }
@@ -274,10 +292,12 @@
         } else {
             frame.size.height = elm->preferred_height;
         }
-        frame.origin.x = col->pos - (alignment.left+alignment.right)/2;
-        //frame.origin.y = viewFrame.size.height - row->pos - frame.size.height + ((alignment.top+alignment.right)/2);
-        frame.origin.y = viewFrame.size.height - row->pos - frame.size.height;
-        elm->view.frame = frame;
+        frame.size.height -= elm->margin.top + elm->margin.bottom;
+        
+        frame.origin.x = col->pos + elm->margin.left;
+        frame.origin.y = row->pos + elm->margin.top;
+        NSRect viewFrame = [elm->view frameForAlignmentRect:frame];
+        elm->view.frame = viewFrame;
     }
     
     free(cols);
@@ -292,32 +312,32 @@
     return self.preferredSize;
 }
 
-- (void) addView:(NSView*)view {
+- (void) addView:(NSView*)view layout:(UiLayout*)layout {
     _preferredSize.width = -1;
     _preferredSize.height = -1;
     
-    if(_newline) {
+    if(self.container != nil && self.container->newline) {
         _y++;
         _x = 0;
-        _newline = FALSE;
+        self.container->newline = FALSE;
     }
     
     GridElm elm;
     elm.x = _x;
     elm.y = _y;
-    elm.margin = 0;
-    elm.colspan = _uilayout.colspan;
-    elm.rowspan = _uilayout.rowspan;
-    if(_uilayout.fill) {
+    elm.margin = NSEdgeInsetsMake(layout->margin_top, layout->margin_left, layout->margin_bottom, layout->margin_right);
+    elm.colspan = layout->colspan;
+    elm.rowspan = layout->rowspan;
+    if(layout->fill) {
         elm.hfill = TRUE;
         elm.vfill = TRUE;
         elm.hexpand = TRUE;
         elm.vexpand = TRUE;
     } else {
-        elm.hfill = _uilayout.hfill;
-        elm.vfill = _uilayout.vfill;
-        elm.hexpand = _uilayout.hexpand;
-        elm.vexpand = _uilayout.vexpand;
+        elm.hfill = layout->hfill;
+        elm.vfill = layout->vfill;
+        elm.hexpand = layout->hexpand;
+        elm.vexpand = layout->vexpand;
     }
     elm.view = view;
     cxListAdd(_children, &elm);

mercurial