ui/cocoa/GridLayout.m

changeset 112
c3f2f16fa4b8
parent 110
c00e968d018b
equal deleted inserted replaced
111:81c4f73236a4 112:c3f2f16fa4b8
30 30
31 31
32 32
33 @implementation GridLayout 33 @implementation GridLayout
34 34
35 @synthesize label=_label; 35 @synthesize container = _container;
36 @synthesize uilayout=_uilayout;
37 @synthesize newline=_newline;
38 36
39 - (GridLayout*)init { 37 - (GridLayout*)init {
40 self = [super init]; 38 self = [super init];
41 _columnspacing = 0; 39 _columnspacing = 0;
42 _rowspacing = 0; 40 _rowspacing = 0;
56 NSEdgeInsets e1 = _test.alignmentRectInsets; 54 NSEdgeInsets e1 = _test.alignmentRectInsets;
57 55
58 } 56 }
59 */ 57 */
60 58
59 - (BOOL)isFlipped {
60 return YES;
61 }
61 62
62 - (void) layout { 63 - (void) layout {
63 int ncols = _cols+1; 64 int ncols = _cols+1;
64 int nrows = _rows+1; 65 int nrows = _rows+1;
65 66
66 GridDef *cols = calloc(ncols, sizeof(GridDef)); 67 GridDef *cols = calloc(ncols, sizeof(GridDef));
67 GridDef *rows = calloc(nrows, sizeof(GridDef)); 68 GridDef *rows = calloc(nrows, sizeof(GridDef));
68 69
69 NSRect viewFrame = self.frame; 70 //NSRect viewFrame = self.frame;
71 NSRect viewFrame = self.bounds;
70 72
71 int colspacing = _columnspacing; 73 int colspacing = _columnspacing;
72 int rowspacing = _rowspacing; 74 int rowspacing = _rowspacing;
73 75
74 int span_max = 1; 76 int span_max = 1;
87 } 89 }
88 if(size.height == NSViewNoIntrinsicMetric) { 90 if(size.height == NSViewNoIntrinsicMetric) {
89 size.height = size2.height; 91 size.height = size2.height;
90 } 92 }
91 if(size.width != NSViewNoIntrinsicMetric) { 93 if(size.width != NSViewNoIntrinsicMetric) {
92 CGFloat width = size.width; 94 CGFloat width = size.width + elm->margin.left + elm->margin.right;
93 if(width > cols[elm->x].preferred_size && elm->colspan <= 1 && span_max == 1) { 95 if(width > cols[elm->x].preferred_size && elm->colspan <= 1 && span_max == 1) {
94 cols[elm->x].preferred_size = width; 96 cols[elm->x].preferred_size = width;
95 } 97 }
96 elm->preferred_width = width; 98 elm->preferred_width = width;
97 } 99 }
98 if(size.height != NSViewNoIntrinsicMetric) { 100 if(size.height != NSViewNoIntrinsicMetric) {
99 CGFloat height = size.height; 101 CGFloat height = size.height + elm->margin.top + elm->margin.bottom;
100 //CGFloat height = size.height;
101 if(height > rows[elm->y].preferred_size && elm->rowspan <= 1 && span_max == 1) { 102 if(height > rows[elm->y].preferred_size && elm->rowspan <= 1 && span_max == 1) {
102 rows[elm->y].preferred_size = height; 103 rows[elm->y].preferred_size = height;
103 } 104 }
104 elm->preferred_height = height; 105 elm->preferred_height = height;
105 } 106 }
107
106 108
107 if(elm->rowspan > span_max || elm->colspan > span_max) { 109 if(elm->rowspan > span_max || elm->colspan > span_max) {
108 continue; 110 continue;
109 } 111 }
110 112
182 int row_ext = 0; 184 int row_ext = 0;
183 185
184 int preferred_width = 0; 186 int preferred_width = 0;
185 int preferred_height = 0; 187 int preferred_height = 0;
186 for(int j=0;j<ncols;j++) { 188 for(int j=0;j<ncols;j++) {
187 preferred_width += cols[j].preferred_size + colspacing; 189 preferred_width += cols[j].preferred_size;
188 if(cols[j].expand) { 190 if(cols[j].expand) {
189 col_ext++; 191 col_ext++;
190 } 192 }
191 } 193 }
192 for(int j=0;j<nrows;j++) { 194 for(int j=0;j<nrows;j++) {
193 preferred_height += rows[j].preferred_size + rowspacing; 195 preferred_height += rows[j].preferred_size;
194 if(rows[j].expand) { 196 if(rows[j].expand) {
195 row_ext++; 197 row_ext++;
196 } 198 }
199 }
200 if(ncols > 0) {
201 preferred_width += (ncols-1) * colspacing;
202 }
203 if(nrows > 0) {
204 preferred_height += (nrows-1) * rowspacing;
197 } 205 }
198 206
199 _preferredSize.width = preferred_width; 207 _preferredSize.width = preferred_width;
200 _preferredSize.height = preferred_height; 208 _preferredSize.height = preferred_height;
201 209
237 cx_foreach(GridElm *, elm, i) { 245 cx_foreach(GridElm *, elm, i) {
238 //NSSize size = elm->view.intrinsicContentSize; 246 //NSSize size = elm->view.intrinsicContentSize;
239 GridDef *col = &cols[elm->x]; 247 GridDef *col = &cols[elm->x];
240 GridDef *row = &rows[elm->y]; 248 GridDef *row = &rows[elm->y];
241 249
242 NSEdgeInsets alignment = elm->view.alignmentRectInsets;
243 NSRect frame; 250 NSRect frame;
244 if(elm->hfill) { 251 if(elm->hfill) {
245 if(elm->colspan > 1) { 252 if(elm->colspan > 1) {
246 int cwidth = 0; 253 int cwidth = 0;
247 int end_col = elm->x + elm->colspan; 254 int end_col = elm->x + elm->colspan;
248 if(end_col > ncols) { 255 if(end_col > ncols) {
249 end_col = ncols; 256 end_col = ncols;
250 } 257 }
258 int real_span = 0;
251 for(int c=elm->x;c<end_col;c++) { 259 for(int c=elm->x;c<end_col;c++) {
252 cwidth += cols[c].size; 260 cwidth += cols[c].size;
253 } 261 real_span++;
254 frame.size.width = cwidth + + alignment.left + alignment.right; 262 }
263 if(real_span > 0) {
264 cwidth += (real_span-1) * colspacing;
265 }
266 frame.size.width = cwidth;
255 } else { 267 } else {
256 frame.size.width = col->size + alignment.left + alignment.right; 268 frame.size.width = col->size;
257 } 269 }
258 } else { 270 } else {
259 frame.size.width = elm->preferred_width + alignment.left + alignment.right; 271 frame.size.width = elm->preferred_width;
260 } 272 }
273 frame.size.width -= elm->margin.left + elm->margin.right;
261 if(elm->vfill) { 274 if(elm->vfill) {
262 if(elm->rowspan > 1) { 275 if(elm->rowspan > 1) {
263 int rheight = 0; 276 int rheight = 0;
264 int end_row = elm->y + elm->rowspan; 277 int end_row = elm->y + elm->rowspan;
265 if(end_row > nrows) { 278 if(end_row > nrows) {
266 end_row = nrows; 279 end_row = nrows;
267 } 280 }
281 int real_span = 0;
268 for(int r=elm->y;r<end_row;r++) { 282 for(int r=elm->y;r<end_row;r++) {
269 rheight += rows[r].size; 283 rheight += rows[r].size;
284 real_span++;
285 }
286 if(real_span > 0) {
287 rheight += (real_span-1) * rowspacing;
270 } 288 }
271 frame.size.height = rheight; 289 frame.size.height = rheight;
272 } 290 }
273 frame.size.height = row->size; 291 frame.size.height = row->size;
274 } else { 292 } else {
275 frame.size.height = elm->preferred_height; 293 frame.size.height = elm->preferred_height;
276 } 294 }
277 frame.origin.x = col->pos - (alignment.left+alignment.right)/2; 295 frame.size.height -= elm->margin.top + elm->margin.bottom;
278 //frame.origin.y = viewFrame.size.height - row->pos - frame.size.height + ((alignment.top+alignment.right)/2); 296
279 frame.origin.y = viewFrame.size.height - row->pos - frame.size.height; 297 frame.origin.x = col->pos + elm->margin.left;
280 elm->view.frame = frame; 298 frame.origin.y = row->pos + elm->margin.top;
299 NSRect viewFrame = [elm->view frameForAlignmentRect:frame];
300 elm->view.frame = viewFrame;
281 } 301 }
282 302
283 free(cols); 303 free(cols);
284 free(rows); 304 free(rows);
285 } 305 }
290 [self layout]; 310 [self layout];
291 } 311 }
292 return self.preferredSize; 312 return self.preferredSize;
293 } 313 }
294 314
295 - (void) addView:(NSView*)view { 315 - (void) addView:(NSView*)view layout:(UiLayout*)layout {
296 _preferredSize.width = -1; 316 _preferredSize.width = -1;
297 _preferredSize.height = -1; 317 _preferredSize.height = -1;
298 318
299 if(_newline) { 319 if(self.container != nil && self.container->newline) {
300 _y++; 320 _y++;
301 _x = 0; 321 _x = 0;
302 _newline = FALSE; 322 self.container->newline = FALSE;
303 } 323 }
304 324
305 GridElm elm; 325 GridElm elm;
306 elm.x = _x; 326 elm.x = _x;
307 elm.y = _y; 327 elm.y = _y;
308 elm.margin = 0; 328 elm.margin = NSEdgeInsetsMake(layout->margin_top, layout->margin_left, layout->margin_bottom, layout->margin_right);
309 elm.colspan = _uilayout.colspan; 329 elm.colspan = layout->colspan;
310 elm.rowspan = _uilayout.rowspan; 330 elm.rowspan = layout->rowspan;
311 if(_uilayout.fill) { 331 if(layout->fill) {
312 elm.hfill = TRUE; 332 elm.hfill = TRUE;
313 elm.vfill = TRUE; 333 elm.vfill = TRUE;
314 elm.hexpand = TRUE; 334 elm.hexpand = TRUE;
315 elm.vexpand = TRUE; 335 elm.vexpand = TRUE;
316 } else { 336 } else {
317 elm.hfill = _uilayout.hfill; 337 elm.hfill = layout->hfill;
318 elm.vfill = _uilayout.vfill; 338 elm.vfill = layout->vfill;
319 elm.hexpand = _uilayout.hexpand; 339 elm.hexpand = layout->hexpand;
320 elm.vexpand = _uilayout.vexpand; 340 elm.vexpand = layout->vexpand;
321 } 341 }
322 elm.view = view; 342 elm.view = view;
323 cxListAdd(_children, &elm); 343 cxListAdd(_children, &elm);
324 344
325 [self addSubview:view]; 345 [self addSubview:view];

mercurial