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")) {
67 if(getBool("vertical")) *w = 4*UNIT;
69 } else if (isType("list") || isType("airport-list") || isType("dial")) {
73 // Throw it all out if the user specified a fixed preference
74 if(hasField("pref-width")) *w = getNum("pref-width");
75 if(hasField("pref-height")) *h = getNum("pref-height");
77 // And finally correct for cell padding
78 int pad = 2*padding();
82 // Store what we calculated
87 // Set up geometry such that the widget lives "inside" the specified
88 void LayoutWidget::layout(int x, int y, int w, int h)
95 // Correct for padding.
102 int prefw = 0, prefh = 0;
103 calcPrefSize(&prefw, &prefh);
107 // "Parent Set" values override widget preferences
108 if(hasField("_psw")) prefw = getNum("_psw");
109 if(hasField("_psh")) prefh = getNum("_psh");
111 bool isGroup = isType("dialog") || isType("group");
113 // Correct our box for alignment. The values above correspond to
114 // a "fill" alignment.
115 const char* halign = isGroup ? "fill" : "center";
116 if(hasField("halign")) halign = getStr("halign");
117 if(eq(halign, "left")) {
119 } else if(eq(halign, "right")) {
122 } else if(eq(halign, "center")) {
126 const char* valign = isGroup ? "fill" : "center";
127 if(hasField("valign")) valign = getStr("valign");
128 if(eq(valign, "bottom")) {
130 } else if(eq(valign, "top")) {
133 } else if(eq(valign, "center")) {
138 // PUI widgets interpret their size differently depending on
139 // type, so diddle the values as needed to fit the widget into
140 // the x/y/w/h box we have calculated.
141 if (isType("text")) {
142 // puText labels are layed out to the right of the box, so
145 } else if (isType("input") || isType("combo") || isType("select")) {
146 // Fix the height to a constant
147 y += (h - 6*UNIT) / 2;
149 } else if (isType("checkbox") || isType("radio")) {
150 // The PUI dimensions are of the check area only. Center it
151 // on the left side of our box.
152 y += (h - 3*UNIT) / 2;
154 } else if (isType("slider")) {
155 // Fix the thickness to a constant
156 if(getBool("vertical")) { x += (w-4*UNIT)/2; w = 4*UNIT; }
157 else { y += (h-4*UNIT)/2; h = 4*UNIT; }
160 // Set out output geometry
166 // Finally, if we are ourselves a layout object, do the actual layout.
167 if(isGroup && hasField("layout")) {
168 const char* layout = getStr("layout");
169 if (eq(layout, "hbox" )) doHVBox(true, false);
170 else if(eq(layout, "vbox" )) doHVBox(true, true);
171 else if(eq(layout, "table")) doTable(true);
175 // Convention: the "A" cooridinate refers to the major axis of the
176 // container (width, for an hbox), "B" is minor.
177 void LayoutWidget::doHVBox(bool doLayout, bool vertical, int* w, int* h)
179 int nc = nChildren();
180 int* prefA = doLayout ? new int[nc] : 0;
181 int i, totalA = 0, maxB = 0, nStretch = 0;
182 int nEq = 0, eqA = 0, eqB = 0, eqTotalA = 0;
183 for(i=0; i<nc; i++) {
184 LayoutWidget child = getChild(i);
186 child.calcPrefSize(vertical ? &b : &a, vertical ? &a : &b);
187 if(doLayout) prefA[i] = a;
189 if(b > maxB) maxB = b;
190 if(child.getBool("stretch")) {
192 } else if(child.getBool("equal")) {
193 int pad = child.padding();
195 eqTotalA += a - 2*pad;
196 if(a-2*pad > eqA) eqA = a - 2*pad;
197 if(b-2*pad > eqB) eqB = b - 2*pad;
200 if(nStretch == 0) nStretch = nc;
201 totalA += nEq * eqA - eqTotalA;
204 if(vertical) { *w = maxB; *h = totalA; }
205 else { *w = totalA; *h = maxB; }
210 int availA = getNum(vertical ? "height" : "width");
211 int availB = getNum(vertical ? "width" : "height");
212 bool stretchAll = nStretch == nc ? true : false;
213 int stretch = availA - totalA;
214 if(stretch < 0) stretch = 0;
215 for(i=0; i<nc; i++) {
216 // Swap the child order for vertical boxes, so we lay out
217 // from top to bottom instead of along the cartesian Y axis.
218 int idx = vertical ? (nc-i-1) : i;
219 LayoutWidget child = getChild(idx);
220 if(child.getBool("equal")) {
221 int pad = child.padding();
222 prefA[idx] = eqA + 2*pad;
223 // Use "parent set" values to communicate the setting to
225 child.setNum(vertical ? "_psh" : "_psw", eqA);
226 child.setNum(vertical ? "_psw" : "_psh", eqB);
228 if(stretchAll || child.getBool("stretch")) {
229 int chunk = stretch / nStretch;
233 child.setNum("__stretch", chunk);
235 if(vertical) child.layout( 0, currA, availB, prefA[idx]);
236 else child.layout(currA, 0, prefA[idx], availB);
246 int w, h, row, col, rspan, cspan;
249 void LayoutWidget::doTable(bool doLayout, int* w, int* h)
251 int i, j, nc = nChildren();
252 TabCell* children = new TabCell[nc];
254 // Pass 1: initialize bookeeping structures
255 int rows = 0, cols = 0;
256 for(i=0; i<nc; i++) {
257 TabCell* cell = &children[i];
258 cell->child = getChild(i);
259 cell->child.calcPrefSize(&cell->w, &cell->h);
260 cell->row = cell->child.getNum("row");
261 cell->col = cell->child.getNum("col");
262 cell->rspan = cell->child.hasField("rowspan") ? cell->child.getNum("rowspan") : 1;
263 cell->cspan = cell->child.hasField("colspan") ? cell->child.getNum("colspan") : 1;
264 if(cell->row + cell->rspan > rows) rows = cell->row + cell->rspan;
265 if(cell->col + cell->cspan > cols) cols = cell->col + cell->cspan;
267 int* rowSizes = new int[rows];
268 int* colSizes = new int[cols];
269 for(i=0; i<rows; i++) rowSizes[i] = 0;
270 for(i=0; i<cols; i++) colSizes[i] = 0;
272 // Pass 1a (hack): we want row zero to be the top, not the
273 // (cartesian: y==0) bottom, so reverse the sense of the row
275 for(i=0; i<nc; i++) {
276 TabCell* cell = &children[i];
277 cell->row = rows - cell->row - cell->rspan;
280 // Pass 2: get sizes for single-cell children
281 for(i=0; i<nc; i++) {
282 TabCell* cell = &children[i];
283 if(cell->rspan < 2 && cell->h > rowSizes[cell->row])
284 rowSizes[cell->row] = cell->h;
285 if(cell->cspan < 2 && cell->w > colSizes[cell->col])
286 colSizes[cell->col] = cell->w;
289 // Pass 3: multi-cell children, make space as needed
290 for(i=0; i<nc; i++) {
291 TabCell* cell = &children[i];
292 if(cell->rspan > 1) {
294 for(j=0; j<cell->rspan; j++)
295 total += rowSizes[cell->row + j];
296 int extra = total - cell->h;
298 for(j=0; j<cell->rspan; j++) {
299 int chunk = extra / (cell->rspan - j);
300 rowSizes[cell->row + j] += chunk;
305 if(cell->cspan > 1) {
307 for(j=0; j<cell->cspan; j++)
308 total += colSizes[cell->col + j];
309 int extra = total - cell->w;
311 for(j=0; j<cell->cspan; j++) {
312 int chunk = extra / (cell->cspan - j);
313 colSizes[cell->col + j] += chunk;
320 // Calculate our preferred sizes, and return if we aren't doing layout
321 int prefw=0, prefh=0;
322 for(i=0; i<cols; i++) prefw += colSizes[i];
323 for(i=0; i<rows; i++) prefh += rowSizes[i];
326 *w = prefw; *h = prefh;
327 delete[] children; delete[] rowSizes; delete[] colSizes;
331 // Allocate extra space
332 int pad = 2*padding();
333 int extra = getNum("height") - pad - prefh;
334 for(i=0; i<rows; i++) {
335 int chunk = extra / (rows - i);
336 rowSizes[i] += chunk;
339 extra = getNum("width") - pad - prefw;
340 for(i=0; i<cols; i++) {
341 int chunk = extra / (cols - i);
342 colSizes[i] += chunk;
346 // Finally, lay out the children (with just two more temporary
347 // arrays for calculating coordinates)
348 int* rowY = new int[rows];
350 for(i=0; i<rows; i++) { rowY[i] = total; total += rowSizes[i]; }
352 int* colX = new int[cols];
354 for(i=0; i<cols; i++) { colX[i] = total; total += colSizes[i]; }
356 for(i=0; i<nc; i++) {
357 TabCell* cell = &children[i];
359 for(j=0; j<cell->rspan; j++) h += rowSizes[cell->row + j];
360 for(j=0; j<cell->cspan; j++) w += colSizes[cell->col + j];
361 int x = colX[cell->col];
362 int y = rowY[cell->row];
363 cell->child.layout(x, y, w, h);