ui/motif/Grid.c

changeset 115
e57ca2747782
parent 110
c00e968d018b
--- a/ui/motif/Grid.c	Sun Dec 07 20:00:33 2025 +0100
+++ b/ui/motif/Grid.c	Sat Dec 13 15:58:58 2025 +0100
@@ -60,7 +60,7 @@
         XmRDimension,
         sizeof (Dimension),
         XtOffsetOf( GridRec,
-                   mywidget.columnspacing),
+                   grid.columnspacing),
         XmRImmediate,
         (XtPointer) 0
     },
@@ -70,17 +70,47 @@
         XmRDimension,
         sizeof (Dimension),
         XtOffsetOf( GridRec,
-                   mywidget.rowspacing),
+                   grid.rowspacing),
+        XmRImmediate,
+        (XtPointer) 0
+    },
+    {
+        gridPaddingLeft,
+        gridPaddingLeft,
+        XmRDimension,
+        sizeof (Dimension),
+        XtOffsetOf( GridRec,
+                   grid.padding_left),
         XmRImmediate,
         (XtPointer) 0
     },
     {
-        gridMargin,
-        gridMargin,
+        gridPaddingRight,
+        gridPaddingRight,
         XmRDimension,
         sizeof (Dimension),
         XtOffsetOf( GridRec,
-                   mywidget.margin),
+                   grid.padding_right),
+        XmRImmediate,
+        (XtPointer) 0
+    },
+    {
+        gridPaddingTop,
+        gridPaddingTop,
+        XmRDimension,
+        sizeof (Dimension),
+        XtOffsetOf( GridRec,
+                   grid.padding_top),
+        XmRImmediate,
+        (XtPointer) 0
+    },
+    {
+        gridPaddingBottom,
+        gridPaddingBottom,
+        XmRDimension,
+        sizeof (Dimension),
+        XtOffsetOf( GridRec,
+                   grid.padding_bottom),
         XmRImmediate,
         (XtPointer) 0
     }
@@ -305,8 +335,8 @@
 void grid_initialize(Widget request, Widget new, ArgList args, Cardinal num_args) {
     Grid mn = (Grid)new;
     
-    mn->mywidget.max_col = 0;
-    mn->mywidget.max_row = 0;
+    mn->grid.max_col = 0;
+    mn->grid.max_row = 0;
     
 }
 void grid_realize(Widget w,XtValueMask *valueMask,XSetWindowAttributes *attributes) {
@@ -364,17 +394,17 @@
 }
 
 void GridChangeManaged(Widget widget) {
-    
+    grid_place_children((Grid)widget);
 }
 
 Boolean ConstraintSetValues(Widget old, Widget request, Widget neww, ArgList args, Cardinal *num_args) {
     GridConstraintRec *constraints = neww->core.constraints;
     Grid grid = (Grid)XtParent(neww);
-    if(constraints->grid.x > grid->mywidget.max_col) {
-        grid->mywidget.max_col = constraints->grid.x;
+    if(constraints->grid.x > grid->grid.max_col) {
+        grid->grid.max_col = constraints->grid.x;
     }
-    if(constraints->grid.y > grid->mywidget.max_row) {
-        grid->mywidget.max_row = constraints->grid.y;
+    if(constraints->grid.y > grid->grid.max_row) {
+        grid->grid.max_row = constraints->grid.y;
     }
 }
 
@@ -389,25 +419,31 @@
     GridConstraintRec *constraints = neww->core.constraints;
     
     Grid grid = (Grid)XtParent(neww);
-    if(constraints->grid.x > grid->mywidget.max_col) {
-        grid->mywidget.max_col = constraints->grid.x;
+    if(constraints->grid.x > grid->grid.max_col) {
+        grid->grid.max_col = constraints->grid.x;
     }
-    if(constraints->grid.y > grid->mywidget.max_row) {
-        grid->mywidget.max_row = constraints->grid.y;
+    if(constraints->grid.y > grid->grid.max_row) {
+        grid->grid.max_row = constraints->grid.y;
     }
     constraints->grid.pref_width = neww->core.width;
     constraints->grid.pref_height = neww->core.height;
 }
 
 void grid_place_children(Grid w) {
-    int ncols = w->mywidget.max_col+1;
-    int nrows = w->mywidget.max_row+1;
+    if(!XtIsRealized((Widget)w)) {
+        return;
+    }
+    
+    int ncols = w->grid.max_col+1;
+    int nrows = w->grid.max_row+1;
     GridDef *cols = calloc(ncols, sizeof(GridDef));
     GridDef *rows = calloc(nrows, sizeof(GridDef));
     int num_cols_expanding = 0;
     int num_rows_expanding = 0;
-    int req_width = 0;
-    int req_height = 0;
+    int req_width = w->grid.padding_left + w->grid.padding_right;
+    int req_height = w->grid.padding_top + w->grid.padding_bottom;
+    int width = w->core.width;
+    int height = w->core.height;
     
     //printf("container width: %d\n", (int)w->core.width);
     
@@ -428,6 +464,12 @@
             if(constraints->grid.pref_width < constraints->grid.min_width) {
                 constraints->grid.pref_width = constraints->grid.min_width;
             }
+            int elm_width = constraints->grid.pref_width + constraints->grid.margin_left + constraints->grid.margin_right;
+            int elm_height = constraints->grid.pref_height + constraints->grid.margin_top + constraints->grid.margin_bottom;
+            if(!XtIsManaged(child)) {
+                elm_width = 0;
+                elm_height = 0;
+            }
             
             if(constraints->grid.colspan > span_max || constraints->grid.rowspan > span_max) {
                 continue;
@@ -489,12 +531,12 @@
                     span_width = last_col->size;
                     
                 }
-                int diff = constraints->grid.pref_width - span_width;
+                int diff = elm_width - span_width;
                 if(diff > 0) {
                     last_col->size += diff; 
                 }
-            } else if(constraints->grid.pref_width > col->size) {
-                col->size = constraints->grid.pref_width;
+            } else if(elm_width > col->size) {
+                col->size = elm_width;
             }
             // row size
             if(constraints->grid.rowspan > 1) {
@@ -505,12 +547,12 @@
                     span_height = last_row->size;
                     
                 }
-                int diff = constraints->grid.pref_height - span_height;
+                int diff = elm_height - span_height;
                 if(diff > 0) {
                     last_row->size += diff; 
                 }
-            } else if(constraints->grid.pref_height > row->size) {
-                row->size = constraints->grid.pref_height;
+            } else if(elm_height > row->size) {
+                row->size = elm_height;
             }
         }
         span_max = 50000; // not sure if this is unreasonable low or high
@@ -530,10 +572,23 @@
         req_height += rows[i].size;
     }
     
+    int total_colspacing = 0;
+    int total_rowspacing = 0;
+    for(int i=0;i+1<ncols;i++) {
+        if(cols[i].size > 0) {
+            total_colspacing += w->grid.columnspacing;
+        }
+    }
+    for(int i=0;i+1<nrows;i++) {
+        if(rows[i].size > 0) {
+            total_rowspacing += w->grid.rowspacing;
+        }
+    }
+    
     if(req_width > 0 && req_height > 0) {
         // add col/row spacing
-        req_width += (ncols-1)*w->mywidget.columnspacing;
-        req_height += (nrows-1)*w->mywidget.rowspacing;
+        req_width += total_colspacing; //(ncols-1)*w->grid.columnspacing;
+        req_height += total_rowspacing; //(nrows-1)*w->grid.rowspacing;
         
         Widget parent = w->core.parent;
         Dimension rwidth = req_width;
@@ -545,9 +600,9 @@
             //rheight = w->core.height;
         }
         
-        if(!w->mywidget.sizerequest) {
+        if(!w->grid.sizerequest) {
             Dimension actual_width, actual_height;
-            w->mywidget.sizerequest = TRUE;
+            w->grid.sizerequest = TRUE;
             
             //printf("sizerequest: %d x %d\n", (int)req_width, (int)req_height);
             
@@ -558,7 +613,7 @@
             //XtGeometryResult result = XtMakeGeometryRequest((Widget)w, &request, &reply);
             
             XtMakeResizeRequest((Widget)w, req_width, req_height, &actual_width, &actual_height);
-            w->mywidget.sizerequest = FALSE;
+            w->grid.sizerequest = FALSE;
             //printf("size request: %d %d\n", (int)actual_width, (int)actual_height);
         }
         
@@ -568,37 +623,41 @@
     
     // how much space can we add to each expanding col/row
     int hexpand = 0;
-    int width_diff = (int)w->core.width - req_width;
+    int width_diff = width - req_width;
     int hexpand2 = 0;
     if(width_diff > 0 && num_cols_expanding > 0) {
         hexpand = width_diff / num_cols_expanding;
         hexpand2 = width_diff-hexpand*num_cols_expanding;
     }
-    int x = 0;
+    int x = w->grid.padding_left;
     for(int i=0;i<ncols;i++) {
         cols[i].pos = x;
         if(cols[i].expand) {
             cols[i].size += hexpand + hexpand2;
         }
-        x += cols[i].size + w->mywidget.columnspacing;
+        if(cols[i].size > 0) {
+            x += cols[i].size + w->grid.columnspacing;
+        }
         
         hexpand2 = 0;
     }
     
     int vexpand = 0;
-    int height_diff = (int)w->core.height - req_height;
+    int height_diff = height - req_height;
     int vexpand2 = 0;
     if(height_diff > 0 && num_rows_expanding > 0) {
         vexpand = height_diff / num_rows_expanding;
         vexpand2 = height_diff-vexpand*num_rows_expanding;
     }
-    int y = 0;
+    int y = w->grid.padding_bottom;
     for(int i=0;i<nrows;i++) {
         rows[i].pos = y;
         if(rows[i].expand) {
             rows[i].size += vexpand + vexpand2;
         }
-        y += rows[i].size += w->mywidget.rowspacing;
+        if(rows[i].size > 0) {
+            y += rows[i].size += w->grid.rowspacing;
+        }
         
         vexpand2 = 0;
     }
@@ -608,8 +667,8 @@
         GridConstraintRec *constraints = child->core.constraints;
         GridDef c = cols[constraints->grid.x];
         GridDef r = rows[constraints->grid.y];
-        int x = c.pos;
-        int y = r.pos;
+        int x = c.pos + constraints->grid.margin_left;
+        int y = r.pos + constraints->grid.margin_top;
         int width = constraints->grid.pref_width;
         int height = constraints->grid.pref_height;
         if(constraints->grid.hfill) {
@@ -617,12 +676,12 @@
                 Dimension cwidth = 0;
                 for(int j=0;j<constraints->grid.colspan;j++) {
                     if(constraints->grid.x+j < ncols) {
-                        cwidth += cols[constraints->grid.x+j].size + (j > 0 ? w->mywidget.columnspacing : 0);
+                        cwidth += cols[constraints->grid.x+j].size + (j > 0 ? w->grid.columnspacing : 0);
                     }
                 }
                 width = cwidth;
             } else {
-                width = c.size - w->mywidget.columnspacing;
+                width = c.size - w->grid.columnspacing - constraints->grid.margin_left - constraints->grid.margin_right;
             }
         }
         if(constraints->grid.vfill) {
@@ -630,15 +689,15 @@
                 Dimension cheight = 0;
                 for(int j=0;j<constraints->grid.rowspan;j++) {
                     if(constraints->grid.y+j < nrows) {
-                        cheight += rows[constraints->grid.y+j].size + (j > 0 ? w->mywidget.rowspacing : 0);
+                        cheight += rows[constraints->grid.y+j].size + (j > 0 ? w->grid.rowspacing : 0);
                     }
                 }
                 height = cheight;
             } else {
-                height = r.size - w->mywidget.rowspacing;
+                height = r.size - w->grid.rowspacing - constraints->grid.margin_top - constraints->grid.margin_bottom;
             }
         }
-        
+              
         if(width > 0 && height > 0) {
             XtConfigureWidget(child, x, y, width, height, child->core.border_width);
         }

mercurial