7 weeks ago
implement grid hfill/vfill (Cocoa)
--- a/make/xcode/toolkit/toolkit/main.m Wed Jan 22 21:27:11 2025 +0100 +++ b/make/xcode/toolkit/toolkit/main.m Wed Jan 22 22:06:10 2025 +0100 @@ -38,13 +38,13 @@ UiObject *obj = ui_window("My Window", NULL); - ui_button(obj, .label = "Button X1 -------------------------------------------------- END", .onclick = action_button); + ui_button(obj, .label = "Button X1 -------------------------------------------------- END", .onclick = action_button, .hfill = 1); ui_newline(obj); - ui_button(obj, .label = "Button X2"); - ui_button(obj, .label = "Button X3", .hexpand = TRUE); - ui_button(obj, .label = "Button X4"); + ui_button(obj, .label = "Button X2", .hfill = 1); + ui_button(obj, .label = "Button X3", .hexpand = TRUE, .hfill = 1); + ui_button(obj, .label = "Button X4", .hfill = 1); ui_newline(obj); - ui_button(obj, .label = "Button X5"); + ui_button(obj, .label = "Button X5", .hfill = 0); ui_show(obj);
--- a/ui/cocoa/GridLayout.h Wed Jan 22 21:27:11 2025 +0100 +++ b/ui/cocoa/GridLayout.h Wed Jan 22 22:06:10 2025 +0100 @@ -39,6 +39,8 @@ int y; int colspan; int rowspan; + int preferred_width; + int preferred_height; BOOL hexpand; BOOL vexpand; BOOL hfill; @@ -49,7 +51,7 @@ int size; int pos; int preferred_size; - BOOL extend; + BOOL expand; } GridDef; @interface GridLayout : NSView<Container>
--- a/ui/cocoa/GridLayout.m Wed Jan 22 21:27:11 2025 +0100 +++ b/ui/cocoa/GridLayout.m Wed Jan 22 22:06:10 2025 +0100 @@ -62,8 +62,8 @@ int ncols = _cols+1; int nrows = _rows+1; - GridDef *coldef = calloc(ncols, sizeof(GridDef)); - GridDef *rowdef = calloc(nrows, sizeof(GridDef)); + GridDef *cols = calloc(ncols, sizeof(GridDef)); + GridDef *rows = calloc(nrows, sizeof(GridDef)); NSRect viewFrame = self.frame; @@ -76,23 +76,25 @@ NSEdgeInsets alignment = elm->view.alignmentRectInsets; if(size.width != NSViewNoIntrinsicMetric) { CGFloat width = size.width + alignment.left + alignment.right; - if(width > coldef[elm->x].preferred_size) { - coldef[elm->x].preferred_size = width; + if(width > cols[elm->x].preferred_size) { + cols[elm->x].preferred_size = width; } + elm->preferred_width = width; } if(size.height != NSViewNoIntrinsicMetric) { CGFloat height = size.height + alignment.top + alignment.right; //CGFloat height = size.height; - if(height > rowdef[elm->y].preferred_size) { - rowdef[elm->y].preferred_size = height; + if(height > rows[elm->y].preferred_size) { + rows[elm->y].preferred_size = height; } + elm->preferred_height = height; } if(elm->hexpand) { - coldef[elm->x].extend = TRUE; + cols[elm->x].expand = TRUE; } if(elm->vexpand) { - rowdef[elm->y].extend = TRUE; + rows[elm->y].expand = TRUE; } } @@ -102,14 +104,14 @@ int preferred_width = 0; int preferred_height = 0; for(int j=0;j<ncols;j++) { - preferred_width += coldef[j].preferred_size + colspacing; - if(coldef[j].extend) { + preferred_width += cols[j].preferred_size + colspacing; + if(cols[j].expand) { col_ext++; } } for(int j=0;j<nrows;j++) { - preferred_height += rowdef[j].preferred_size + rowspacing; - if(rowdef[j].extend) { + preferred_height += rows[j].preferred_size + rowspacing; + if(rows[j].expand) { row_ext++; } } @@ -124,16 +126,16 @@ int vext = vremaining/row_ext; for(int j=0;j<ncols;j++) { - GridDef *col = &coldef[j]; - if(col->extend) { + GridDef *col = &cols[j]; + if(col->expand) { col->size = col->preferred_size + hext; } else { col->size = col->preferred_size; } } for(int j=0;j<nrows;j++) { - GridDef *row = &rowdef[j]; - if(row->extend) { + GridDef *row = &rows[j]; + if(row->expand) { row->size = row->preferred_size + vext; } else { row->size = row->preferred_size; @@ -142,32 +144,40 @@ int pos = 0; for(int j=0;j<ncols;j++) { - coldef[j].pos = pos; - pos += coldef[j].size + colspacing; + cols[j].pos = pos; + pos += cols[j].size + colspacing; } pos = 0; for(int j=0;j<nrows;j++) { - rowdef[j].pos = pos; - pos += rowdef[j].size + rowspacing; + rows[j].pos = pos; + pos += rows[j].size + rowspacing; } i = cxListIterator(_children); cx_foreach(GridElm *, elm, i) { //NSSize size = elm->view.intrinsicContentSize; - GridDef *col = &coldef[elm->x]; - GridDef *row = &rowdef[elm->y]; + GridDef *col = &cols[elm->x]; + GridDef *row = &rows[elm->y]; NSEdgeInsets alignment = elm->view.alignmentRectInsets; NSRect frame; - frame.size.width = col->size; - frame.size.height = row->size; + if(elm->hfill) { + frame.size.width = col->size; + } else { + frame.size.width = elm->preferred_width; + } + if(elm->vfill) { + frame.size.height = row->size; + } 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); elm->view.frame = frame; } - free(coldef); - free(rowdef); + free(cols); + free(rows); }