ui/cocoa/GridLayout.m

changeset 100
d2bd73d28ff1
equal deleted inserted replaced
99:b9767cb5b06b 100:d2bd73d28ff1
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2024 Olaf Wintermann. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #import "GridLayout.h"
30
31
32
33 @implementation GridLayout
34
35 @synthesize label=_label;
36 @synthesize uilayout=_uilayout;
37 @synthesize newline=_newline;
38
39 - (GridLayout*)init {
40 self = [super init];
41 _columnspacing = 0;
42 _rowspacing = 0;
43 _children = cxArrayListCreateSimple(sizeof(GridElm), 32);
44
45 return self;
46 }
47
48 /*
49 - (void) layout {
50 [super layout];
51
52 NSRect r1 = _test.frame;
53 NSSize s1 = _test.intrinsicContentSize;
54 NSEdgeInsets e1 = _test.alignmentRectInsets;
55
56 printf("fuck\n");
57 }
58 */
59
60
61 - (void) layout {
62 int ncols = _cols+1;
63 int nrows = _rows+1;
64
65 GridDef *coldef = calloc(ncols, sizeof(GridDef));
66 GridDef *rowdef = calloc(nrows, sizeof(GridDef));
67
68 NSRect viewFrame = self.frame;
69
70 int colspacing = _columnspacing;
71 int rowspacing = _rowspacing;
72
73 CxIterator i = cxListIterator(_children);
74 cx_foreach(GridElm *, elm, i) {
75 NSSize size = elm->view.intrinsicContentSize;
76 NSEdgeInsets alignment = elm->view.alignmentRectInsets;
77 if(size.width != NSViewNoIntrinsicMetric) {
78 CGFloat width = size.width + alignment.left + alignment.right;
79 if(width > coldef[elm->x].preferred_size) {
80 coldef[elm->x].preferred_size = width;
81 }
82 }
83 if(size.height != NSViewNoIntrinsicMetric) {
84 CGFloat height = size.height + alignment.top + alignment.right;
85 //CGFloat height = size.height;
86 if(height > rowdef[elm->y].preferred_size) {
87 rowdef[elm->y].preferred_size = height;
88 }
89 }
90
91 if(elm->hexpand) {
92 coldef[elm->x].extend = TRUE;
93 }
94 if(elm->vexpand) {
95 rowdef[elm->y].extend = TRUE;
96 }
97 }
98
99 int col_ext = 0;
100 int row_ext = 0;
101
102 int preferred_width = 0;
103 int preferred_height = 0;
104 for(int j=0;j<ncols;j++) {
105 preferred_width += coldef[j].preferred_size + colspacing;
106 if(coldef[j].extend) {
107 col_ext++;
108 }
109 }
110 for(int j=0;j<nrows;j++) {
111 preferred_height += rowdef[j].preferred_size + rowspacing;
112 if(rowdef[j].extend) {
113 row_ext++;
114 }
115 }
116
117 _preferredSize.width = preferred_width;
118 _preferredSize.height = preferred_height;
119
120
121 int hremaining = viewFrame.size.width - preferred_width;
122 int vremaining = viewFrame.size.height - preferred_height;
123 int hext = hremaining/col_ext;
124 int vext = vremaining/row_ext;
125
126 for(int j=0;j<ncols;j++) {
127 GridDef *col = &coldef[j];
128 if(col->extend) {
129 col->size = col->preferred_size + hext;
130 } else {
131 col->size = col->preferred_size;
132 }
133 }
134 for(int j=0;j<nrows;j++) {
135 GridDef *row = &rowdef[j];
136 if(row->extend) {
137 row->size = row->preferred_size + vext;
138 } else {
139 row->size = row->preferred_size;
140 }
141 }
142
143 int pos = 0;
144 for(int j=0;j<ncols;j++) {
145 coldef[j].pos = pos;
146 pos += coldef[j].size + colspacing;
147 }
148 pos = 0;
149 for(int j=0;j<nrows;j++) {
150 rowdef[j].pos = pos;
151 pos += rowdef[j].size + rowspacing;
152 }
153
154 i = cxListIterator(_children);
155 cx_foreach(GridElm *, elm, i) {
156 //NSSize size = elm->view.intrinsicContentSize;
157 GridDef *col = &coldef[elm->x];
158 GridDef *row = &rowdef[elm->y];
159
160 NSEdgeInsets alignment = elm->view.alignmentRectInsets;
161 NSRect frame;
162 frame.size.width = col->size;
163 frame.size.height = row->size;
164 frame.origin.x = col->pos - (alignment.left+alignment.right)/2;
165 frame.origin.y = viewFrame.size.height - row->pos - frame.size.height + ((alignment.top+alignment.right)/2);
166 elm->view.frame = frame;
167 }
168
169 free(coldef);
170 free(rowdef);
171 }
172
173
174 - (NSSize)intrinsicContentSize {
175 return self.preferredSize;
176 }
177
178 - (void) addView:(NSView*)view fill:(BOOL)fill {
179 if(_newline) {
180 _y++;
181 _x = 0;
182 _newline = FALSE;
183 }
184
185 GridElm elm;
186 elm.x = _x;
187 elm.y = _y;
188 elm.margin = 0;
189 elm.colspan = _uilayout.colspan;
190 elm.rowspan = _uilayout.rowspan;
191 elm.hfill = _uilayout.hfill;
192 elm.vfill = _uilayout.vfill;
193 elm.hexpand = _uilayout.hexpand;
194 elm.vexpand = _uilayout.vexpand;
195 elm.view = view;
196 cxListAdd(_children, &elm);
197
198 [self addSubview:view];
199 self.needsLayout = YES;
200
201 if(_x > _cols) {
202 _cols = _x;
203 }
204 if(_y > _rows) {
205 _rows = _y;
206 }
207 _x++;
208 }
209
210 - (void) dealloc {
211 cxListDestroy(_children);
212 }
213
214 @end

mercurial