3 // This file contains the actual layout engine. It has no dependence
4 // on outside libraries; see layout-props.cxx for the glue code.
6 // Note, property names with leading double-underscores (__bx, etc...)
7 // are debugging information, and can be safely removed.
9 const int DEFAULT_PADDING = 2;
11 int LayoutWidget::UNIT = 5;
13 bool LayoutWidget::eq(const char* a, const char* b)
15 while(*a && (*a == *b)) { a++; b++; }
19 // Normal widgets get a padding of 4 pixels. Layout groups shouldn't
20 // have visible padding by default, except for top-level dialog groups
21 // which need to leave two pixels for the puFrame's border. This
22 // value can, of course, be overriden by the parent groups
23 // <default-padding> property, or per widget with <padding>.
24 int LayoutWidget::padding()
26 int pad = isType("group") ? 0 : 4;
27 if(isType("dialog")) pad = 2;
28 if(hasParent() && parent().hasField("default-padding"))
29 pad = parent().getNum("default-padding");
30 if(hasField("padding"))
31 pad = getNum("padding");
35 void LayoutWidget::calcPrefSize(int* w, int* h)
37 *w = *h = 0; // Ask for nothing by default
39 int legw = stringLength(getStr("legend"));
40 int labw = stringLength(getStr("label"));
42 if(isType("dialog") || isType("group")) {
43 if(!hasField("layout")) {
44 // Legacy support for groups without layout managers.
45 if(hasField("width")) *w = getNum("width");
46 if(hasField("height")) *h = getNum("height");
48 const char* layout = getStr("layout");
49 if (eq(layout, "hbox" )) doHVBox(false, false, w, h);
50 else if(eq(layout, "vbox" )) doHVBox(false, true, w, h);
51 else if(eq(layout, "table")) doTable(false, w, h);
53 } else if (isType("text")) {
55 *h = 4*UNIT; // FIXME: multi line height?
56 } else if (isType("button")) {
57 *w = legw + 6*UNIT + (labw ? labw + UNIT : 0);
59 } else if (isType("checkbox") || isType("radio")) {
60 *w = 3*UNIT + (labw ? (3*UNIT + labw) : 0);
62 } else if (isType("input") || isType("combo") || isType("select")) {
65 } else if (isType("slider")) {
66 if(getBool("vertical")) *w = 3*UNIT;
68 } else if (isType("list") || isType("airport-list") || isType("dial")) {
72 // Throw it all out if the user specified a fixed preference
73 if(hasField("pref-width")) *w = getNum("pref-width");
74 if(hasField("pref-height")) *h = getNum("pref-height");
76 // And finally correct for cell padding
77 int pad = 2*padding();
81 // Store what we calculated
86 // Set up geometry such that the widget lives "inside" the specified
87 void LayoutWidget::layout(int x, int y, int w, int h)
94 // Correct for padding.
101 int prefw = 0, prefh = 0;
102 calcPrefSize(&prefw, &prefh);
106 // "Parent Set" values override widget preferences
107 if(hasField("_psw")) prefw = getNum("_psw");
108 if(hasField("_psh")) prefh = getNum("_psh");
110 bool isGroup = isType("dialog") || isType("group");
112 // Correct our box for alignment. The values above correspond to
113 // a "fill" alignment.
114 const char* halign = isGroup ? "fill" : "center";
115 if(hasField("halign")) halign = getStr("halign");
116 if(eq(halign, "left")) {
118 } else if(eq(halign, "right")) {
121 } else if(eq(halign, "center")) {
125 const char* valign = isGroup ? "fill" : "center";
126 if(hasField("valign")) valign = getStr("valign");
127 if(eq(valign, "bottom")) {
129 } else if(eq(valign, "top")) {
132 } else if(eq(valign, "center")) {
137 // PUI widgets interpret their size differently depending on
138 // type, so diddle the values as needed to fit the widget into
139 // the x/y/w/h box we have calculated.
140 if (isType("text")) {
141 // puText labels are layed out to the right of the box, so
144 } else if (isType("input") || isType("combo") || isType("select")) {
145 // Fix the height to a constant
146 y += (h - 6*UNIT) / 2;
148 } else if (isType("checkbox") || isType("radio")) {
149 // The PUI dimensions are of the check area only. Center it
150 // on the left side of our box.
151 y += (h - 3*UNIT) / 2;
153 } else if (isType("slider")) {
154 // Fix the thickness to a constant
155 if(getBool("vertical")) { x += (w-3*UNIT)/2; w = 3*UNIT; }
156 else { y += (h-3*UNIT)/2; h = 3*UNIT; }
159 // Set out output geometry
165 // Finally, if we are ourselves a layout object, do the actual layout.
166 if(isGroup && hasField("layout")) {
167 const char* layout = getStr("layout");
168 if (eq(layout, "hbox" )) doHVBox(true, false);
169 else if(eq(layout, "vbox" )) doHVBox(true, true);
170 else if(eq(layout, "table")) doTable(true);
174 // Convention: the "A" cooridinate refers to the major axis of the
175 // container (width, for an hbox), "B" is minor.
176 void LayoutWidget::doHVBox(bool doLayout, bool vertical, int* w, int* h)
178 int nc = nChildren();
179 int* prefA = doLayout ? new int[nc] : 0;
180 int i, totalA = 0, maxB = 0, nStretch = 0;
181 int nEq = 0, eqA = 0, eqB = 0, eqTotalA = 0;
182 for(i=0; i<nc; i++) {
183 LayoutWidget child = getChild(i);
185 child.calcPrefSize(vertical ? &b : &a, vertical ? &a : &b);
186 if(doLayout) prefA[i] = a;
188 if(b > maxB) maxB = b;
189 if(child.getBool("stretch")) {
191 } else if(child.getBool("equal")) {
192 int pad = child.padding();
194 eqTotalA += a - 2*pad;
195 if(a-2*pad > eqA) eqA = a - 2*pad;
196 if(b-2*pad > eqB) eqB = b - 2*pad;
199 if(nStretch == 0) nStretch = nc;
200 totalA += nEq * eqA - eqTotalA;
203 if(vertical) { *w = maxB; *h = totalA; }
204 else { *w = totalA; *h = maxB; }
209 int availA = getNum(vertical ? "height" : "width");
210 int availB = getNum(vertical ? "width" : "height");
211 bool stretchAll = nStretch == nc ? true : false;
212 int stretch = availA - totalA;
213 if(stretch < 0) stretch = 0;
214 for(i=0; i<nc; i++) {
215 // Swap the child order for vertical boxes, so we lay out
216 // from top to bottom instead of along the cartesian Y axis.
217 int idx = vertical ? (nc-i-1) : i;
218 LayoutWidget child = getChild(idx);
219 if(child.getBool("equal")) {
220 int pad = child.padding();
221 prefA[idx] = eqA + 2*pad;
222 // Use "parent set" values to communicate the setting to
224 child.setNum(vertical ? "_psh" : "_psw", eqA);
225 child.setNum(vertical ? "_psw" : "_psh", eqB);
227 if(stretchAll || child.getBool("stretch")) {
228 int chunk = stretch / nStretch;
232 child.setNum("__stretch", chunk);
234 if(vertical) child.layout( 0, currA, availB, prefA[idx]);
235 else child.layout(currA, 0, prefA[idx], availB);
245 int w, h, row, col, rspan, cspan;
248 void LayoutWidget::doTable(bool doLayout, int* w, int* h)
250 int i, j, nc = nChildren();
251 TabCell* children = new TabCell[nc];
253 // Pass 1: initialize bookeeping structures
254 int rows = 0, cols = 0;
255 for(i=0; i<nc; i++) {
256 TabCell* cell = &children[i];
257 cell->child = getChild(i);
258 cell->child.calcPrefSize(&cell->w, &cell->h);
259 cell->row = cell->child.getNum("row");
260 cell->col = cell->child.getNum("col");
261 cell->rspan = cell->child.hasField("rowspan") ? cell->child.getNum("rowspan") : 1;
262 cell->cspan = cell->child.hasField("colspan") ? cell->child.getNum("colspan") : 1;
263 if(cell->row + cell->rspan > rows) rows = cell->row + cell->rspan;
264 if(cell->col + cell->cspan > cols) cols = cell->col + cell->cspan;
266 int* rowSizes = new int[rows];
267 int* colSizes = new int[cols];
268 for(i=0; i<rows; i++) rowSizes[i] = 0;
269 for(i=0; i<cols; i++) colSizes[i] = 0;
271 // Pass 1a (hack): we want row zero to be the top, not the
272 // (cartesian: y==0) bottom, so reverse the sense of the row
274 for(i=0; i<nc; i++) {
275 TabCell* cell = &children[i];
276 cell->row = rows - cell->row - cell->rspan;
279 // Pass 2: get sizes for single-cell children
280 for(i=0; i<nc; i++) {
281 TabCell* cell = &children[i];
282 if(cell->rspan < 2 && cell->h > rowSizes[cell->row])
283 rowSizes[cell->row] = cell->h;
284 if(cell->cspan < 2 && cell->w > colSizes[cell->col])
285 colSizes[cell->col] = cell->w;
288 // Pass 3: multi-cell children, make space as needed
289 for(i=0; i<nc; i++) {
290 TabCell* cell = &children[i];
291 if(cell->rspan > 1) {
293 for(j=0; j<cell->rspan; j++)
294 total += rowSizes[cell->row + j];
295 int extra = total - cell->h;
297 for(j=0; j<cell->rspan; j++) {
298 int chunk = extra / (cell->rspan - j);
299 rowSizes[cell->row + j] += chunk;
304 if(cell->cspan > 1) {
306 for(j=0; j<cell->cspan; j++)
307 total += colSizes[cell->col + j];
308 int extra = total - cell->w;
310 for(j=0; j<cell->cspan; j++) {
311 int chunk = extra / (cell->cspan - j);
312 colSizes[cell->col + j] += chunk;
319 // Calculate our preferred sizes, and return if we aren't doing layout
320 int prefw=0, prefh=0;
321 for(i=0; i<cols; i++) prefw += colSizes[i];
322 for(i=0; i<rows; i++) prefh += rowSizes[i];
325 *w = prefw; *h = prefh;
326 delete[] children; delete[] rowSizes; delete[] colSizes;
330 // Allocate extra space
331 int pad = 2*padding();
332 int extra = getNum("height") - pad - prefh;
333 for(i=0; i<rows; i++) {
334 int chunk = extra / (rows - i);
335 rowSizes[i] += chunk;
338 extra = getNum("width") - pad - prefw;
339 for(i=0; i<cols; i++) {
340 int chunk = extra / (cols - i);
341 colSizes[i] += chunk;
345 // Finally, lay out the children (with just two more temporary
346 // arrays for calculating coordinates)
347 int* rowY = new int[rows];
349 for(i=0; i<rows; i++) { rowY[i] = total; total += rowSizes[i]; }
351 int* colX = new int[cols];
353 for(i=0; i<cols; i++) { colX[i] = total; total += colSizes[i]; }
355 for(i=0; i<nc; i++) {
356 TabCell* cell = &children[i];
358 for(j=0; j<cell->rspan; j++) h += rowSizes[cell->row + j];
359 for(j=0; j<cell->cspan; j++) w += colSizes[cell->col + j];
360 int x = colX[cell->col];
361 int y = rowY[cell->row];
362 cell->child.layout(x, y, w, h);