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