]> git.mxchange.org Git - simgear.git/blob - simgear/canvas/layout/BoxLayout.cxx
canvas::Text: add heightForWidth method.
[simgear.git] / simgear / canvas / layout / BoxLayout.cxx
1 // Align items horizontally or vertically in a box
2 //
3 // Copyright (C) 2014  Thomas Geymayer <tomgey@gmail.com>
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Library General Public
7 // License as published by the Free Software Foundation; either
8 // version 2 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 // Library General Public License for more details.
14 //
15 // You should have received a copy of the GNU Library General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA
18
19 #include "BoxLayout.hxx"
20 #include "SpacerItem.hxx"
21 #include <simgear/canvas/Canvas.hxx>
22
23 namespace simgear
24 {
25 namespace canvas
26 {
27
28   //----------------------------------------------------------------------------
29   BoxLayout::BoxLayout(Direction dir):
30     _padding(5)
31   {
32     setDirection(dir);
33   }
34
35   //----------------------------------------------------------------------------
36   void BoxLayout::addItem(const LayoutItemRef& item)
37   {
38     return addItem(item, 0);
39   }
40
41   //----------------------------------------------------------------------------
42   void BoxLayout::addItem(const LayoutItemRef& item, int stretch)
43   {
44     insertItem(-1, item, stretch);
45   }
46
47   //----------------------------------------------------------------------------
48   void BoxLayout::addStretch(int stretch)
49   {
50     insertStretch(-1, stretch);
51   }
52
53   //----------------------------------------------------------------------------
54   void BoxLayout::addSpacing(int size)
55   {
56     insertSpacing(-1, size);
57   }
58
59   //----------------------------------------------------------------------------
60   void BoxLayout::insertItem(int index, const LayoutItemRef& item, int stretch)
61   {
62     ItemData item_data = {0};
63     item_data.layout_item = item;
64     item_data.stretch = std::max(0, stretch);
65
66     item->setCanvas(_canvas);
67     item->setParent(this);
68
69     if( index < 0 )
70       _layout_items.push_back(item_data);
71     else
72       _layout_items.insert(_layout_items.begin() + index, item_data);
73
74     invalidate();
75   }
76
77   //----------------------------------------------------------------------------
78   void BoxLayout::insertStretch(int index, int stretch)
79   {
80     insertItem(index, LayoutItemRef(new SpacerItem()), stretch);
81   }
82
83   //----------------------------------------------------------------------------
84   void BoxLayout::insertSpacing(int index, int size)
85   {
86     SGVec2i size_hint = horiz()
87                           ? SGVec2i(size, 0)
88                           : SGVec2i(0, size),
89                 max_size  = size_hint;
90
91     insertItem(index, LayoutItemRef(new SpacerItem(size_hint, max_size)));
92   }
93
94   //----------------------------------------------------------------------------
95   size_t BoxLayout::count() const
96   {
97     return _layout_items.size();
98   }
99
100   //----------------------------------------------------------------------------
101   LayoutItemRef BoxLayout::itemAt(size_t index)
102   {
103     if( index >= _layout_items.size() )
104       return LayoutItemRef();
105
106     return _layout_items[index].layout_item;
107   }
108
109   //----------------------------------------------------------------------------
110   LayoutItemRef BoxLayout::takeAt(size_t index)
111   {
112     if( index >= _layout_items.size() )
113       return LayoutItemRef();
114
115     LayoutItems::iterator it = _layout_items.begin() + index;
116     LayoutItemRef item = it->layout_item;
117     _layout_items.erase(it);
118
119     invalidate();
120
121     return item;
122   }
123
124   //----------------------------------------------------------------------------
125   void BoxLayout::setStretch(size_t index, int stretch)
126   {
127     if( index >= _layout_items.size() )
128       return;
129
130     _layout_items.at(index).stretch = std::max(0, stretch);
131     invalidate();
132   }
133
134   //----------------------------------------------------------------------------
135   int BoxLayout::stretch(size_t index) const
136   {
137     if( index >= _layout_items.size() )
138       return 0;
139
140     return _layout_items.at(index).stretch;
141   }
142
143   //----------------------------------------------------------------------------
144   void BoxLayout::setSpacing(int spacing)
145   {
146     if( spacing == _padding )
147       return;
148
149     _padding = spacing;
150     invalidate();
151   }
152
153   //----------------------------------------------------------------------------
154   int BoxLayout::spacing() const
155   {
156     return _padding;
157   }
158
159   //----------------------------------------------------------------------------
160   void BoxLayout::setDirection(Direction dir)
161   {
162     _direction = dir;
163     _get_layout_coord = &SGVec2i::x;
164     _get_fixed_coord = &SGVec2i::y;
165
166     if( !horiz() )
167       std::swap(_get_layout_coord, _get_fixed_coord);
168
169     invalidate();
170   }
171
172   //----------------------------------------------------------------------------
173   BoxLayout::Direction BoxLayout::direction() const
174   {
175     return _direction;
176   }
177
178   //----------------------------------------------------------------------------
179   bool BoxLayout::hasHeightForWidth() const
180   {
181     if( _flags & SIZE_INFO_DIRTY )
182       updateSizeHints();
183
184     return _layout_data.has_hfw;
185   }
186
187   //----------------------------------------------------------------------------
188   int BoxLayout::heightForWidth(int w) const
189   {
190     if( !hasHeightForWidth() )
191       return -1;
192
193     updateWFHCache(w);
194     return _hfw_height;
195   }
196
197   //----------------------------------------------------------------------------
198   int BoxLayout::minimumHeightForWidth(int w) const
199   {
200     if( !hasHeightForWidth() )
201       return -1;
202
203     updateWFHCache(w);
204     return _hfw_min_height;
205   }
206
207   //----------------------------------------------------------------------------
208   void BoxLayout::setCanvas(const CanvasWeakPtr& canvas)
209   {
210     _canvas = canvas;
211
212     for(size_t i = 0; i < _layout_items.size(); ++i)
213       _layout_items[i].layout_item->setCanvas(canvas);
214   }
215
216   //----------------------------------------------------------------------------
217   bool BoxLayout::horiz() const
218   {
219     return (_direction == LeftToRight) || (_direction == RightToLeft);
220   }
221
222   //----------------------------------------------------------------------------
223   void BoxLayout::updateSizeHints() const
224   {
225     SGVec2i min_size(0, 0),
226             max_size(0, 0),
227             size_hint(0, 0);
228
229     _layout_data.reset();
230     _hfw_width = _hfw_height = _hfw_min_height = -1;
231
232     bool is_first = true;
233
234     for(size_t i = 0; i < _layout_items.size(); ++i)
235     {
236       // TODO check visible
237
238       ItemData& item_data = _layout_items[i];
239       LayoutItem const& item = *item_data.layout_item;
240
241       item_data.min_size  = (item.minimumSize().*_get_layout_coord)();
242       item_data.max_size  = (item.maximumSize().*_get_layout_coord)();
243       item_data.size_hint = (item.sizeHint().*_get_layout_coord)();
244       item_data.has_hfw = item.hasHeightForWidth();
245
246       if( !dynamic_cast<SpacerItem*>(item_data.layout_item.get()) )
247       {
248         if( is_first )
249         {
250           item_data.padding_orig = 0;
251           is_first = false;
252         }
253         else
254         {
255           item_data.padding_orig = _padding;
256           _layout_data.padding += item_data.padding_orig;
257         }
258       }
259
260       // Add sizes of all children in layout direction
261       safeAdd(min_size.x(),  item_data.min_size);
262       safeAdd(max_size.x(),  item_data.max_size);
263       safeAdd(size_hint.x(), item_data.size_hint);
264
265       // Take maximum in fixed (non-layouted) direction
266       min_size.y()  = std::max( min_size.y(),
267                                 (item.minimumSize().*_get_fixed_coord)() );
268       max_size.y()  = std::max( max_size.y(),
269                                 (item.maximumSize().*_get_fixed_coord)() );
270       size_hint.y() = std::max( size_hint.y(),
271                                 (item.sizeHint().*_get_fixed_coord)() );
272
273       _layout_data.has_hfw = _layout_data.has_hfw || item.hasHeightForWidth();
274     }
275
276     safeAdd(min_size.x(),  _layout_data.padding);
277     safeAdd(max_size.x(),  _layout_data.padding);
278     safeAdd(size_hint.x(), _layout_data.padding);
279
280     _layout_data.min_size = min_size.x();
281     _layout_data.max_size = max_size.x();
282     _layout_data.size_hint = size_hint.x();
283
284     _min_size.x()  = (min_size.*_get_layout_coord)();
285     _max_size.x()  = (max_size.*_get_layout_coord)();
286     _size_hint.x() = (size_hint.*_get_layout_coord)();
287
288     _min_size.y()  = (min_size.*_get_fixed_coord)();
289     _max_size.y()  = (max_size.*_get_fixed_coord)();
290     _size_hint.y() = (size_hint.*_get_fixed_coord)();
291
292     _flags &= ~SIZE_INFO_DIRTY;
293   }
294
295   //----------------------------------------------------------------------------
296   void BoxLayout::updateWFHCache(int w) const
297   {
298     if( w == _hfw_width )
299       return;
300
301     _hfw_height = 0;
302     _hfw_min_height = 0;
303
304     if( horiz() )
305     {
306       _layout_data.size = w;
307       const_cast<BoxLayout*>(this)->distribute(_layout_items, _layout_data);
308
309       for(size_t i = 0; i < _layout_items.size(); ++i)
310       {
311         ItemData const& data = _layout_items[i];
312         _hfw_height = std::max(_hfw_height, data.hfw(data.size));
313         _hfw_min_height = std::max(_hfw_min_height, data.mhfw(data.size));
314       }
315     }
316     else
317     {
318       for(size_t i = 0; i < _layout_items.size(); ++i)
319       {
320         ItemData const& data = _layout_items[i];
321         _hfw_height += data.hfw(w) + data.padding_orig;
322         _hfw_min_height += data.mhfw(w) + data.padding_orig;
323       }
324     }
325
326     _hfw_width = w;
327   }
328
329   //----------------------------------------------------------------------------
330   SGVec2i BoxLayout::sizeHintImpl() const
331   {
332     updateSizeHints();
333     return _size_hint;
334   }
335
336   //----------------------------------------------------------------------------
337   SGVec2i BoxLayout::minimumSizeImpl() const
338   {
339     updateSizeHints();
340     return _min_size;
341   }
342
343   //----------------------------------------------------------------------------
344   SGVec2i BoxLayout::maximumSizeImpl() const
345   {
346     updateSizeHints();
347     return _max_size;
348   }
349
350   //----------------------------------------------------------------------------
351   void BoxLayout::doLayout(const SGRecti& geom)
352   {
353     if( _flags & SIZE_INFO_DIRTY )
354       updateSizeHints();
355
356     // Store current size hints because vertical layouts containing
357     // height-for-width items the size hints are update for the actual width of
358     // the layout
359     int min_size_save = _layout_data.min_size,
360         size_hint_save = _layout_data.size_hint;
361
362     _layout_data.size = (geom.size().*_get_layout_coord)();
363
364     // update width dependent data for layouting of vertical layouts
365     if( _layout_data.has_hfw && !horiz() )
366     {
367       for(size_t i = 0; i < _layout_items.size(); ++i)
368       {
369         ItemData& data = _layout_items[i];
370         if( data.has_hfw )
371         {
372           int w = SGMisc<int>::clip( geom.width(),
373                                      data.layout_item->minimumSize().x(),
374                                      data.layout_item->maximumSize().x() );
375
376           int d = data.layout_item->minimumHeightForWidth(w) - data.min_size;
377           data.min_size += d;
378           _layout_data.min_size += d;
379
380           d = data.layout_item->heightForWidth(w) - data.size_hint;
381           data.size_hint += d;
382           _layout_data.size_hint += d;
383         }
384       }
385     }
386
387     // now do the actual layouting
388     distribute(_layout_items, _layout_data);
389
390     // Restore size hints possibly changed by vertical layouting
391     _layout_data.min_size = min_size_save;
392     _layout_data.size_hint = size_hint_save;
393
394     // and finally set the layouted geometry for each item
395     int fixed_size = (geom.size().*_get_fixed_coord)();
396     SGVec2i cur_pos( (geom.pos().*_get_layout_coord)(),
397                      (geom.pos().*_get_fixed_coord)() );
398
399     bool reverse = (_direction == RightToLeft) || (_direction == BottomToTop);
400     if( reverse )
401       cur_pos.x() += (geom.size().*_get_layout_coord)();
402
403     for(size_t i = 0; i < _layout_items.size(); ++i)
404     {
405       ItemData const& data = _layout_items[i];
406       cur_pos.x() += reverse ? -data.padding - data.size : data.padding;
407
408       SGVec2i size(
409         data.size,
410         std::min( (data.layout_item->maximumSize().*_get_fixed_coord)(),
411                   fixed_size )
412       );
413
414       // Center in fixed direction (TODO allow specifying alignment)
415       int offset_fixed = (fixed_size - size.y()) / 2;
416       cur_pos.y() += offset_fixed;
417
418       data.layout_item->setGeometry(SGRecti(
419         (cur_pos.*_get_layout_coord)(),
420         (cur_pos.*_get_fixed_coord)(),
421         (size.*_get_layout_coord)(),
422         (size.*_get_fixed_coord)()
423       ));
424
425       if( !reverse )
426         cur_pos.x() += data.size;
427       cur_pos.y() -= offset_fixed;
428     }
429   }
430
431   //----------------------------------------------------------------------------
432   HBoxLayout::HBoxLayout():
433     BoxLayout(LeftToRight)
434   {
435
436   }
437
438   //----------------------------------------------------------------------------
439   VBoxLayout::VBoxLayout():
440     BoxLayout(TopToBottom)
441   {
442
443   }
444
445 } // namespace canvas
446 } // namespace simgear