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 // As comments above note, this was being set to 2. For some
28 // reason this causes the dialogs to shrink on subsequent pops
29 // so for now we'll make "dialog" padding 0.
30 if(isType("dialog")) pad = 0;
31 if(hasParent() && parent().hasField("default-padding"))
32 pad = parent().getNum("default-padding");
33 if(hasField("padding"))
34 pad = getNum("padding");
38 void LayoutWidget::calcPrefSize(int* w, int* h)
40 *w = *h = 0; // Ask for nothing by default
42 int legw = stringLength(getStr("legend"));
43 int labw = stringLength(getStr("label"));
45 if(isType("dialog") || isType("group")) {
46 if(!hasField("layout")) {
47 // Legacy support for groups without layout managers.
48 if(hasField("width")) *w = getNum("width");
49 if(hasField("height")) *h = getNum("height");
51 const char* layout = getStr("layout");
52 if (eq(layout, "hbox" )) doHVBox(false, false, w, h);
53 else if(eq(layout, "vbox" )) doHVBox(false, true, w, h);
54 else if(eq(layout, "table")) doTable(false, w, h);
56 } else if (isType("text")) {
58 *h = 4*UNIT; // FIXME: multi line height?
59 } else if (isType("button")) {
60 *w = legw + 6*UNIT + (labw ? labw + UNIT : 0);
62 } else if (isType("checkbox") || isType("radio")) {
63 *w = 3*UNIT + (labw ? (3*UNIT + labw) : 0);
65 } else if (isType("input") || isType("combo") || isType("select")) {
68 } else if (isType("slider")) {
70 if(getBool("vertical")) *w = 4*UNIT;
72 } else if (isType("list") || isType("airport-list") || isType("dial")) {
76 // Throw it all out if the user specified a fixed preference
77 if(hasField("pref-width")) *w = getNum("pref-width");
78 if(hasField("pref-height")) *h = getNum("pref-height");
80 // And finally correct for cell padding
81 int pad = 2*padding();
85 // Store what we calculated
90 // Set up geometry such that the widget lives "inside" the specified
91 void LayoutWidget::layout(int x, int y, int w, int h)
98 // Correct for padding.
105 int prefw = 0, prefh = 0;
106 calcPrefSize(&prefw, &prefh);
110 // "Parent Set" values override widget preferences
111 if(hasField("_psw")) prefw = getNum("_psw");
112 if(hasField("_psh")) prefh = getNum("_psh");
114 bool isGroup = isType("dialog") || isType("group");
116 // Correct our box for alignment. The values above correspond to
117 // a "fill" alignment.
118 const char* halign = isGroup ? "fill" : "center";
119 if(hasField("halign")) halign = getStr("halign");
120 if(eq(halign, "left")) {
122 } else if(eq(halign, "right")) {
125 } else if(eq(halign, "center")) {
129 const char* valign = isGroup ? "fill" : "center";
130 if(hasField("valign")) valign = getStr("valign");
131 if(eq(valign, "bottom")) {
133 } else if(eq(valign, "top")) {
136 } else if(eq(valign, "center")) {
141 // PUI widgets interpret their size differently depending on
142 // type, so diddle the values as needed to fit the widget into
143 // the x/y/w/h box we have calculated.
144 if (isType("text")) {
145 // puText labels are layed out to the right of the box, so
148 } else if (isType("input") || isType("combo") || isType("select")) {
149 // Fix the height to a constant
150 y += (h - 6*UNIT) / 2;
152 } else if (isType("checkbox") || isType("radio")) {
153 // The PUI dimensions are of the check area only. Center it
154 // on the left side of our box.
155 y += (h - 3*UNIT) / 2;
157 } else if (isType("slider")) {
158 // Fix the thickness to a constant
159 if(getBool("vertical")) { x += (w-4*UNIT)/2; w = 4*UNIT; }
160 else { y += (h-4*UNIT)/2; h = 4*UNIT; }
163 // Set out output geometry
169 // Finally, if we are ourselves a layout object, do the actual layout.
170 if(isGroup && hasField("layout")) {
171 const char* layout = getStr("layout");
172 if (eq(layout, "hbox" )) doHVBox(true, false);
173 else if(eq(layout, "vbox" )) doHVBox(true, true);
174 else if(eq(layout, "table")) doTable(true);
178 // Convention: the "A" cooridinate refers to the major axis of the
179 // container (width, for an hbox), "B" is minor.
180 void LayoutWidget::doHVBox(bool doLayout, bool vertical, int* w, int* h)
182 int nc = nChildren();
183 int* prefA = doLayout ? new int[nc] : 0;
184 int i, totalA = 0, maxB = 0, nStretch = 0;
185 int nEq = 0, eqA = 0, eqB = 0, eqTotalA = 0;
186 for(i=0; i<nc; i++) {
187 LayoutWidget child = getChild(i);
189 child.calcPrefSize(vertical ? &b : &a, vertical ? &a : &b);
190 if(doLayout) prefA[i] = a;
192 if(b > maxB) maxB = b;
193 if(child.getBool("stretch")) {
195 } else if(child.getBool("equal")) {
196 int pad = child.padding();
198 eqTotalA += a - 2*pad;
199 if(a-2*pad > eqA) eqA = a - 2*pad;
200 if(b-2*pad > eqB) eqB = b - 2*pad;
203 if(nStretch == 0) nStretch = nc;
204 totalA += nEq * eqA - eqTotalA;
207 if(vertical) { *w = maxB; *h = totalA; }
208 else { *w = totalA; *h = maxB; }
213 int availA = getNum(vertical ? "height" : "width");
214 int availB = getNum(vertical ? "width" : "height");
215 bool stretchAll = nStretch == nc ? true : false;
216 int stretch = availA - totalA;
217 if(stretch < 0) stretch = 0;
218 for(i=0; i<nc; i++) {
219 // Swap the child order for vertical boxes, so we lay out
220 // from top to bottom instead of along the cartesian Y axis.
221 int idx = vertical ? (nc-i-1) : i;
222 LayoutWidget child = getChild(idx);
223 if(child.getBool("equal")) {
224 int pad = child.padding();
225 prefA[idx] = eqA + 2*pad;
226 // Use "parent set" values to communicate the setting to
228 child.setNum(vertical ? "_psh" : "_psw", eqA);
229 child.setNum(vertical ? "_psw" : "_psh", eqB);
231 if(stretchAll || child.getBool("stretch")) {
232 int chunk = stretch / nStretch;
236 child.setNum("__stretch", chunk);
238 if(vertical) child.layout( 0, currA, availB, prefA[idx]);
239 else child.layout(currA, 0, prefA[idx], availB);
249 int w, h, row, col, rspan, cspan;
252 void LayoutWidget::doTable(bool doLayout, int* w, int* h)
254 int i, j, nc = nChildren();
255 TabCell* children = new TabCell[nc];
257 // Pass 1: initialize bookeeping structures
258 int rows = 0, cols = 0;
259 for(i=0; i<nc; i++) {
260 TabCell* cell = &children[i];
261 cell->child = getChild(i);
262 cell->child.calcPrefSize(&cell->w, &cell->h);
263 cell->row = cell->child.getNum("row");
264 cell->col = cell->child.getNum("col");
265 cell->rspan = cell->child.hasField("rowspan") ? cell->child.getNum("rowspan") : 1;
266 cell->cspan = cell->child.hasField("colspan") ? cell->child.getNum("colspan") : 1;
267 if(cell->row + cell->rspan > rows) rows = cell->row + cell->rspan;
268 if(cell->col + cell->cspan > cols) cols = cell->col + cell->cspan;
270 int* rowSizes = new int[rows];
271 int* colSizes = new int[cols];
272 for(i=0; i<rows; i++) rowSizes[i] = 0;
273 for(i=0; i<cols; i++) colSizes[i] = 0;
275 // Pass 1a (hack): we want row zero to be the top, not the
276 // (cartesian: y==0) bottom, so reverse the sense of the row
278 for(i=0; i<nc; i++) {
279 TabCell* cell = &children[i];
280 cell->row = rows - cell->row - cell->rspan;
283 // Pass 2: get sizes for single-cell children
284 for(i=0; i<nc; i++) {
285 TabCell* cell = &children[i];
286 if(cell->rspan < 2 && cell->h > rowSizes[cell->row])
287 rowSizes[cell->row] = cell->h;
288 if(cell->cspan < 2 && cell->w > colSizes[cell->col])
289 colSizes[cell->col] = cell->w;
292 // Pass 3: multi-cell children, make space as needed
293 for(i=0; i<nc; i++) {
294 TabCell* cell = &children[i];
295 if(cell->rspan > 1) {
297 for(j=0; j<cell->rspan; j++)
298 total += rowSizes[cell->row + j];
299 int extra = total - cell->h;
301 for(j=0; j<cell->rspan; j++) {
302 int chunk = extra / (cell->rspan - j);
303 rowSizes[cell->row + j] += chunk;
308 if(cell->cspan > 1) {
310 for(j=0; j<cell->cspan; j++)
311 total += colSizes[cell->col + j];
312 int extra = total - cell->w;
314 for(j=0; j<cell->cspan; j++) {
315 int chunk = extra / (cell->cspan - j);
316 colSizes[cell->col + j] += chunk;
323 // Calculate our preferred sizes, and return if we aren't doing layout
324 int prefw=0, prefh=0;
325 for(i=0; i<cols; i++) prefw += colSizes[i];
326 for(i=0; i<rows; i++) prefh += rowSizes[i];
329 *w = prefw; *h = prefh;
330 delete[] children; delete[] rowSizes; delete[] colSizes;
334 // Allocate extra space
335 int pad = 2*padding();
336 int extra = getNum("height") - pad - prefh;
337 for(i=0; i<rows; i++) {
338 int chunk = extra / (rows - i);
339 rowSizes[i] += chunk;
342 extra = getNum("width") - pad - prefw;
343 for(i=0; i<cols; i++) {
344 int chunk = extra / (cols - i);
345 colSizes[i] += chunk;
349 // Finally, lay out the children (with just two more temporary
350 // arrays for calculating coordinates)
351 int* rowY = new int[rows];
353 for(i=0; i<rows; i++) { rowY[i] = total; total += rowSizes[i]; }
355 int* colX = new int[cols];
357 for(i=0; i<cols; i++) { colX[i] = total; total += colSizes[i]; }
359 for(i=0; i<nc; i++) {
360 TabCell* cell = &children[i];
362 for(j=0; j<cell->rspan; j++) h += rowSizes[cell->row + j];
363 for(j=0; j<cell->cspan; j++) w += colSizes[cell->col + j];
364 int x = colX[cell->col];
365 int y = rowY[cell->row];
366 cell->child.layout(x, y, w, h);