]> git.mxchange.org Git - flightgear.git/blob - src/GUI/layout.cxx
GUI layout management and a few visual/eye-candy modifications. See
[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") ? 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");
32     return pad;
33 }
34
35 void LayoutWidget::calcPrefSize(int* w, int* h)
36 {
37     *w = *h = 0; // Ask for nothing by default
38
39     int legw = stringLength(getStr("legend"));
40     int labw = stringLength(getStr("label"));
41
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");
47         } else {
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);
52         }
53     } else if (isType("text")) {
54         *w = labw;
55         *h = 4*UNIT; // FIXME: multi line height?
56     } else if (isType("button")) {
57         *w = legw + 6*UNIT + (labw ? labw + UNIT : 0);
58         *h = 6*UNIT;
59     } else if (isType("checkbox") || isType("radio")) {
60         *w = 3*UNIT + (labw ? (3*UNIT + labw) : 0);
61         *h = 3*UNIT;
62     } else if (isType("input") || isType("combo") || isType("select")) {
63         *w = 17*UNIT;
64         *h = 6*UNIT;
65     } else if (isType("slider")) {
66         if(getBool("vertical")) *w = 3*UNIT;
67         else                    *h = 3*UNIT;
68     } else if (isType("list") || isType("airport-list") || isType("dial")) {
69         *w = *h = 12*UNIT;
70     }
71
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");
75
76     // And finally correct for cell padding
77     int pad = 2*padding();
78     *w += pad;
79     *h += pad;
80
81     // Store what we calculated
82     setNum("__pw", *w);
83     setNum("__ph", *h);
84 }
85
86 // Set up geometry such that the widget lives "inside" the specified 
87 void LayoutWidget::layout(int x, int y, int w, int h)
88 {
89     setNum("__bx", x);
90     setNum("__by", y);
91     setNum("__bw", w);
92     setNum("__bh", h);
93
94     // Correct for padding.
95     int pad = padding();
96     x += pad;
97     y += pad;
98     w -= 2*pad;
99     h -= 2*pad;
100
101     int prefw = 0, prefh = 0;
102     calcPrefSize(&prefw, &prefh);
103     prefw -= 2*pad;
104     prefh -= 2*pad;
105
106     // "Parent Set" values override widget preferences
107     if(hasField("_psw")) prefw = getNum("_psw");
108     if(hasField("_psh")) prefh = getNum("_psh");
109
110     bool isGroup = isType("dialog") || isType("group");
111
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")) {
117         w = prefw;
118     } else if(eq(halign, "right")) {
119         x += w - prefw;
120         w = prefw;
121     } else if(eq(halign, "center")) {
122         x += (w - prefw)/2;
123         w = prefw;
124     }
125     const char* valign = isGroup ? "fill" : "center";
126     if(hasField("valign")) valign = getStr("valign");
127     if(eq(valign, "bottom")) {
128         h = prefh;
129     } else if(eq(valign, "top")) {
130         y += h - prefh;
131         h = prefh;
132     } else if(eq(valign, "center")) {
133         y += (h - prefh)/2;
134         h = prefh;
135     }
136
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
142         // zero the width.
143         w = 0;
144     } else if (isType("input") || isType("combo") || isType("select")) {
145         // Fix the height to a constant
146         y += (h - 6*UNIT) / 2;
147         h = 6*UNIT;
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;
152         w = h = 3*UNIT;
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; }
157     }
158
159     // Set out output geometry
160     setNum("x", x);
161     setNum("y", y);
162     setNum("width", w);
163     setNum("height", h);
164
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);
171     }
172 }
173
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)
177 {
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);
184         int a, b;
185         child.calcPrefSize(vertical ? &b : &a, vertical ? &a : &b);
186         if(doLayout) prefA[i] = a;
187         totalA += a;
188         if(b > maxB) maxB = b;
189         if(child.getBool("stretch")) {
190             nStretch++;
191         } else if(child.getBool("equal")) {
192             int pad = child.padding();
193             nEq++;
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;
197         }
198     }
199     if(nStretch == 0) nStretch = nc;
200     totalA += nEq * eqA - eqTotalA; 
201
202     if(!doLayout) {
203         if(vertical) { *w = maxB;   *h = totalA; }
204         else         { *w = totalA; *h = maxB; }
205         return;
206     }
207
208     int currA = 0;
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
223             // the child.
224             child.setNum(vertical ? "_psh" : "_psw", eqA);
225             child.setNum(vertical ? "_psw" : "_psh", eqB);
226         }
227         if(stretchAll || child.getBool("stretch")) {
228             int chunk = stretch / nStretch;
229             stretch -= chunk;
230             nStretch--;
231             prefA[idx] += chunk;
232             child.setNum("__stretch", chunk);
233         }
234         if(vertical) child.layout(    0, currA,   availB, prefA[idx]);
235         else         child.layout(currA,     0, prefA[idx],   availB);
236         currA += prefA[idx];
237     }
238
239     delete[] prefA;
240 }
241
242 struct TabCell {
243     TabCell() {}
244     LayoutWidget child;
245     int w, h, row, col, rspan, cspan;
246 };
247
248 void LayoutWidget::doTable(bool doLayout, int* w, int* h)
249 {
250     int i, j, nc = nChildren();
251     TabCell* children = new TabCell[nc];
252     
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;
265     }
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;
270
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
273     // numbers.
274     for(i=0; i<nc; i++) {
275         TabCell* cell = &children[i];
276         cell->row = rows - cell->row - cell->rspan;
277     }
278     
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;
286     }
287     
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) {
292             int total = 0;
293             for(j=0; j<cell->rspan; j++)
294                 total += rowSizes[cell->row + j];
295             int extra = total - cell->h;
296             if(extra > 0) {
297                 for(j=0; j<cell->rspan; j++) {
298                     int chunk = extra / (cell->rspan - j);
299                     rowSizes[cell->row + j] += chunk;
300                     extra -= chunk;
301                 }
302             }
303         }
304         if(cell->cspan > 1) {
305             int total = 0;
306             for(j=0; j<cell->cspan; j++)
307                 total += colSizes[cell->col + j];
308             int extra = total - cell->w;
309             if(extra > 0) {
310                 for(j=0; j<cell->cspan; j++) {
311                     int chunk = extra / (cell->cspan - j);
312                     colSizes[cell->col + j] += chunk;
313                     extra -= chunk;
314                 }
315             }
316         }
317     }
318
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];
323
324     if(!doLayout) {
325         *w = prefw; *h = prefh;
326         delete[] children; delete[] rowSizes; delete[] colSizes;
327         return;
328     }
329
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;
336         extra -= chunk;
337     }
338     extra = getNum("width") - pad - prefw;
339     for(i=0; i<cols; i++) {
340         int chunk = extra / (cols - i);
341         colSizes[i] += chunk;
342         extra -= chunk;
343     }
344
345     // Finally, lay out the children (with just two more temporary
346     // arrays for calculating coordinates)
347     int* rowY = new int[rows];
348     int total = 0;
349     for(i=0; i<rows; i++) { rowY[i] = total; total += rowSizes[i]; }
350
351     int* colX = new int[cols];
352     total = 0;
353     for(i=0; i<cols; i++) { colX[i] = total; total += colSizes[i]; }
354
355     for(i=0; i<nc; i++) {
356         TabCell* cell = &children[i];
357         int w = 0, h = 0;
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);
363     }    
364
365     // Clean up
366     delete[] children;
367     delete[] rowSizes;
368     delete[] colSizes;
369     delete[] rowY;
370     delete[] colX;
371 }