]> git.mxchange.org Git - flightgear.git/blob - src/GUI/layout.cxx
Merge branch 'maint' into next
[flightgear.git] / src / GUI / layout.cxx
1 #include "layout.hxx"
2
3 // This file contains the actual layout engine.  It has no dependence
4 // on outside libraries; see layout-props.cxx for the glue code.
5
6 // Note, property names with leading double-underscores (__bx, etc...)
7 // are debugging information, and can be safely removed.
8
9 const int DEFAULT_PADDING = 2;
10
11 int LayoutWidget::UNIT = 5;
12
13 bool LayoutWidget::eq(const char* a, const char* b)
14 {
15     while(*a && (*a == *b)) { a++; b++; }
16     return *a == *b;
17 }
18
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()
25 {
26     int pad = (isType("group") || isType("frame")) ? 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");
35     return pad;
36 }
37
38 void LayoutWidget::calcPrefSize(int* w, int* h)
39 {
40     *w = *h = 0; // Ask for nothing by default
41
42     if (!getBool("enabled", true) || isType("nasal"))
43         return;
44
45     int legw = stringLength(getStr("legend"));
46     int labw = stringLength(getStr("label"));
47
48     if(isType("dialog") || isType("group") || isType("frame")) {
49         if(!hasField("layout")) {
50             // Legacy support for groups without layout managers.
51             if(hasField("width")) *w = getNum("width");
52             if(hasField("height")) *h = getNum("height");
53         } else {
54             const char* layout = getStr("layout");
55             if     (eq(layout, "hbox" )) doHVBox(false, false, w, h);
56             else if(eq(layout, "vbox" )) doHVBox(false, true, w, h);
57             else if(eq(layout, "table")) doTable(false, w, h);
58         }
59     } else if (isType("text")) {
60         *w = labw;
61         *h = 3*UNIT; // FIXME: multi line height?
62     } else if (isType("button")) {
63         *w = legw + 6*UNIT + (labw ? labw + UNIT : 0);
64         *h = 6*UNIT;
65     } else if (isType("checkbox") || isType("radio")) {
66         *w = 3*UNIT + (labw ? (3*UNIT + labw) : 0);
67         *h = 3*UNIT;
68     } else if (isType("input") || isType("combo") || isType("select")) {
69         *w = 17*UNIT;
70         *h = 6*UNIT;
71     } else if (isType("slider")) {
72         *w = *h = 17*UNIT;
73         if(getBool("vertical")) *w = 4*UNIT;
74         else                    *h = 4*UNIT;
75     } else if (isType("list") || isType("airport-list")
76             || isType("property-list") || isType("dial")) {
77         *w = *h = 12*UNIT;
78     } else if (isType("hrule")) {
79         *h = 1;
80     } else if (isType("vrule")) {
81         *w = 1;
82     }
83
84     // Throw it all out if the user specified a fixed preference
85     if(hasField("pref-width"))  *w = getNum("pref-width");
86     if(hasField("pref-height")) *h = getNum("pref-height");
87
88     // And finally correct for cell padding
89     int pad = 2*padding();
90     *w += pad;
91     *h += pad;
92
93     // Store what we calculated
94     setNum("__pw", *w);
95     setNum("__ph", *h);
96 }
97
98 // Set up geometry such that the widget lives "inside" the specified 
99 void LayoutWidget::layout(int x, int y, int w, int h)
100 {
101     if (!getBool("enabled", true) || isType("nasal"))
102         return;
103
104     setNum("__bx", x);
105     setNum("__by", y);
106     setNum("__bw", w);
107     setNum("__bh", h);
108
109     // Correct for padding.
110     int pad = padding();
111     x += pad;
112     y += pad;
113     w -= 2*pad;
114     h -= 2*pad;
115
116     int prefw = 0, prefh = 0;
117     calcPrefSize(&prefw, &prefh);
118     prefw -= 2*pad;
119     prefh -= 2*pad;
120
121     // "Parent Set" values override widget preferences
122     if(hasField("_psw")) prefw = getNum("_psw");
123     if(hasField("_psh")) prefh = getNum("_psh");
124
125     bool isGroup = isType("dialog") || isType("group") || isType("frame");
126
127     // Correct our box for alignment.  The values above correspond to
128     // a "fill" alignment.
129     const char* halign = (isGroup || isType("hrule")) ? "fill" : "center";
130     if(hasField("halign")) halign = getStr("halign");
131     if(eq(halign, "left")) {
132         w = prefw;
133     } else if(eq(halign, "right")) {
134         x += w - prefw;
135         w = prefw;
136     } else if(eq(halign, "center")) {
137         x += (w - prefw)/2;
138         w = prefw;
139     }
140     const char* valign = (isGroup || isType("vrule")) ? "fill" : "center";
141     if(hasField("valign")) valign = getStr("valign");
142     if(eq(valign, "bottom")) {
143         h = prefh;
144     } else if(eq(valign, "top")) {
145         y += h - prefh;
146         h = prefh;
147     } else if(eq(valign, "center")) {
148         y += (h - prefh)/2;
149         h = prefh;
150     }
151
152     // PUI widgets interpret their size differently depending on
153     // type, so diddle the values as needed to fit the widget into
154     // the x/y/w/h box we have calculated.
155     if (isType("text")) {
156         // puText labels are layed out to the right of the box, so
157         // zero the width. Also subtract PUSTR_RGAP from the x
158         // coordinate to compensate for the added gap between the
159         // label and its empty puObject.
160         x -= 5;
161         w = 0;
162     } else if (isType("input") || isType("combo") || isType("select")) {
163         // Fix the height to a constant
164         y += (h - 6*UNIT) / 2;
165         h = 6*UNIT;
166     } else if (isType("checkbox") || isType("radio")) {
167         // The PUI dimensions are of the check area only.  Center it
168         // on the left side of our box.
169         y += (h - 3*UNIT) / 2;
170         w = h = 3*UNIT;
171     } else if (isType("slider")) {
172         // Fix the thickness to a constant
173         if(getBool("vertical")) { x += (w-4*UNIT)/2; w = 4*UNIT; }
174         else                    { y += (h-4*UNIT)/2; h = 4*UNIT; }
175     }
176
177     // Set out output geometry
178     setNum("x", x);
179     setNum("y", y);
180     setNum("width", w);
181     setNum("height", h);
182
183     // Finally, if we are ourselves a layout object, do the actual layout.
184     if(isGroup && hasField("layout")) {
185         const char* layout = getStr("layout");
186         if     (eq(layout, "hbox" )) doHVBox(true, false);
187         else if(eq(layout, "vbox" )) doHVBox(true, true);
188         else if(eq(layout, "table")) doTable(true);
189     }
190 }
191
192 // Convention: the "A" cooridinate refers to the major axis of the
193 // container (width, for an hbox), "B" is minor.
194 void LayoutWidget::doHVBox(bool doLayout, bool vertical, int* w, int* h)
195 {
196     int nc = nChildren();
197     int* prefA = doLayout ? new int[nc] : 0;
198     int i, totalA = 0, maxB = 0, nStretch = 0;
199     int nEq = 0, eqA = 0, eqB = 0, eqTotalA = 0;
200     for(i=0; i<nc; i++) {
201         LayoutWidget child = getChild(i);
202         if (!child.getBool("enabled", true))
203             continue;
204
205         int a, b;
206         child.calcPrefSize(vertical ? &b : &a, vertical ? &a : &b);
207         if(doLayout) prefA[i] = a;
208         totalA += a;
209         if(b > maxB) maxB = b;
210         if(child.getBool("stretch")) {
211             nStretch++;
212         } else if(child.getBool("equal")) {
213             int pad = child.padding();
214             nEq++;
215             eqTotalA += a - 2*pad;
216             if(a-2*pad > eqA) eqA = a - 2*pad;
217             if(b-2*pad > eqB) eqB = b - 2*pad;
218         }
219     }
220     if(nStretch == 0) nStretch = nc;
221     totalA += nEq * eqA - eqTotalA; 
222
223     if(!doLayout) {
224         if(vertical) { *w = maxB;   *h = totalA; }
225         else         { *w = totalA; *h = maxB; }
226         return;
227     }
228
229     int currA = 0;
230     int availA = getNum(vertical ? "height" : "width");
231     int availB = getNum(vertical ? "width" : "height");
232     bool stretchAll = nStretch == nc ? true : false;
233     int stretch = availA - totalA;
234     if(stretch < 0) stretch = 0;
235     for(i=0; i<nc; i++) {
236         // Swap the child order for vertical boxes, so we lay out
237         // from top to bottom instead of along the cartesian Y axis.
238         int idx = vertical ? (nc-i-1) : i;
239         LayoutWidget child = getChild(idx);
240         if (!child.getBool("enabled", true))
241             continue;
242
243         if(child.getBool("equal")) {
244             int pad = child.padding();
245             prefA[idx] = eqA + 2*pad;
246             // Use "parent set" values to communicate the setting to
247             // the child.
248             child.setNum(vertical ? "_psh" : "_psw", eqA);
249             child.setNum(vertical ? "_psw" : "_psh", eqB);
250         }
251         if(stretchAll || child.getBool("stretch")) {
252             int chunk = stretch / nStretch;
253             stretch -= chunk;
254             nStretch--;
255             prefA[idx] += chunk;
256             child.setNum("__stretch", chunk);
257         }
258         if(vertical) child.layout(    0, currA,   availB, prefA[idx]);
259         else         child.layout(currA,     0, prefA[idx],   availB);
260         currA += prefA[idx];
261     }
262
263     delete[] prefA;
264 }
265
266 struct TabCell {
267     TabCell() {}
268     LayoutWidget child;
269     int w, h, row, col, rspan, cspan;
270 };
271
272 void LayoutWidget::doTable(bool doLayout, int* w, int* h)
273 {
274     int i, j, nc = nChildren();
275     TabCell* children = new TabCell[nc];
276     
277     // Pass 1: initialize bookeeping structures
278     int rows = 0, cols = 0;
279     for(i=0; i<nc; i++) {
280         TabCell* cell = &children[i];
281         cell->child = getChild(i);
282         cell->child.calcPrefSize(&cell->w, &cell->h);
283         cell->row = cell->child.getNum("row");
284         cell->col = cell->child.getNum("col");
285         cell->rspan = cell->child.hasField("rowspan") ? cell->child.getNum("rowspan") : 1;
286         cell->cspan = cell->child.hasField("colspan") ? cell->child.getNum("colspan") : 1;
287         if(cell->row + cell->rspan > rows) rows = cell->row + cell->rspan;
288         if(cell->col + cell->cspan > cols) cols = cell->col + cell->cspan;
289     }
290     int* rowSizes = new int[rows];
291     int* colSizes = new int[cols];
292     for(i=0; i<rows; i++) rowSizes[i] = 0;
293     for(i=0; i<cols; i++) colSizes[i] = 0;
294
295     // Pass 1a (hack): we want row zero to be the top, not the
296     // (cartesian: y==0) bottom, so reverse the sense of the row
297     // numbers.
298     for(i=0; i<nc; i++) {
299         TabCell* cell = &children[i];
300         cell->row = rows - cell->row - cell->rspan;
301     }
302     
303     // Pass 2: get sizes for single-cell children
304     for(i=0; i<nc; i++) {
305         TabCell* cell = &children[i];
306         if(cell->rspan < 2 && cell->h > rowSizes[cell->row])
307             rowSizes[cell->row] = cell->h;
308         if(cell->cspan < 2 && cell->w > colSizes[cell->col])
309             colSizes[cell->col] = cell->w;
310     }
311     
312     // Pass 3: multi-cell children, make space as needed
313     for(i=0; i<nc; i++) {
314         TabCell* cell = &children[i];
315         if(cell->rspan > 1) {
316             int total = 0;
317             for(j=0; j<cell->rspan; j++)
318                 total += rowSizes[cell->row + j];
319             int extra = cell->h - total;
320             if(extra > 0) {
321                 for(j=0; j<cell->rspan; j++) {
322                     int chunk = extra / (cell->rspan - j);
323                     rowSizes[cell->row + j] += chunk;
324                     extra -= chunk;
325                 }
326             }
327         }
328         if(cell->cspan > 1) {
329             int total = 0;
330             for(j=0; j<cell->cspan; j++)
331                 total += colSizes[cell->col + j];
332             int extra = cell->w - total;
333             if(extra > 0) {
334                 for(j=0; j<cell->cspan; j++) {
335                     int chunk = extra / (cell->cspan - j);
336                     colSizes[cell->col + j] += chunk;
337                     extra -= chunk;
338                 }
339             }
340         }
341     }
342
343     // Calculate our preferred sizes, and return if we aren't doing layout
344     int prefw=0, prefh=0;
345     for(i=0; i<cols; i++) prefw += colSizes[i];
346     for(i=0; i<rows; i++) prefh += rowSizes[i];
347
348     if(!doLayout) {
349         *w = prefw; *h = prefh;
350         delete[] children; delete[] rowSizes; delete[] colSizes;
351         return;
352     }
353
354     // Allocate extra space
355     int pad = 2*padding();
356     int extra = getNum("height") - pad - prefh;
357     for(i=0; i<rows; i++) {
358         int chunk = extra / (rows - i);
359         rowSizes[i] += chunk;
360         extra -= chunk;
361     }
362     extra = getNum("width") - pad - prefw;
363     for(i=0; i<cols; i++) {
364         int chunk = extra / (cols - i);
365         colSizes[i] += chunk;
366         extra -= chunk;
367     }
368
369     // Finally, lay out the children (with just two more temporary
370     // arrays for calculating coordinates)
371     int* rowY = new int[rows];
372     int total = 0;
373     for(i=0; i<rows; i++) { rowY[i] = total; total += rowSizes[i]; }
374
375     int* colX = new int[cols];
376     total = 0;
377     for(i=0; i<cols; i++) { colX[i] = total; total += colSizes[i]; }
378
379     for(i=0; i<nc; i++) {
380         TabCell* cell = &children[i];
381         int w = 0, h = 0;
382         for(j=0; j<cell->rspan; j++) h += rowSizes[cell->row + j];
383         for(j=0; j<cell->cspan; j++) w += colSizes[cell->col + j];
384         int x = colX[cell->col];
385         int y = rowY[cell->row];
386         cell->child.layout(x, y, w, h);
387     }    
388
389     // Clean up
390     delete[] children;
391     delete[] rowSizes;
392     delete[] colSizes;
393     delete[] rowY;
394     delete[] colX;
395 }