1 // Align items horizontally or vertically in a box
3 // Copyright (C) 2014 Thomas Geymayer <tomgey@gmail.com>
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.
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.
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
19 #include "BoxLayout.hxx"
20 #include "SpacerItem.hxx"
21 #include <simgear/canvas/Canvas.hxx>
28 //----------------------------------------------------------------------------
29 BoxLayout::BoxLayout(Direction dir):
35 //----------------------------------------------------------------------------
36 BoxLayout::~BoxLayout()
38 _parent.reset(); // No need to invalidate parent again...
42 //----------------------------------------------------------------------------
43 void BoxLayout::addItem(const LayoutItemRef& item)
45 return addItem(item, 0);
48 //----------------------------------------------------------------------------
49 void BoxLayout::addItem(const LayoutItemRef& item, int stretch)
51 insertItem(-1, item, stretch);
54 //----------------------------------------------------------------------------
55 void BoxLayout::addStretch(int stretch)
57 insertStretch(-1, stretch);
60 //----------------------------------------------------------------------------
61 void BoxLayout::addSpacing(int size)
63 insertSpacing(-1, size);
66 //----------------------------------------------------------------------------
67 void BoxLayout::insertItem(int index, const LayoutItemRef& item, int stretch)
69 ItemData item_data = {0};
70 item_data.layout_item = item;
71 item_data.stretch = std::max(0, stretch);
73 item->setCanvas(_canvas);
74 item->setParent(this);
77 _layout_items.push_back(item_data);
79 _layout_items.insert(_layout_items.begin() + index, item_data);
84 //----------------------------------------------------------------------------
85 void BoxLayout::insertStretch(int index, int stretch)
87 insertItem(index, LayoutItemRef(new SpacerItem()), stretch);
90 //----------------------------------------------------------------------------
91 void BoxLayout::insertSpacing(int index, int size)
93 SGVec2i size_hint = horiz()
98 insertItem(index, LayoutItemRef(new SpacerItem(size_hint, max_size)));
101 //----------------------------------------------------------------------------
102 size_t BoxLayout::count() const
104 return _layout_items.size();
107 //----------------------------------------------------------------------------
108 LayoutItemRef BoxLayout::itemAt(size_t index)
110 if( index >= _layout_items.size() )
111 return LayoutItemRef();
113 return _layout_items[index].layout_item;
116 //----------------------------------------------------------------------------
117 LayoutItemRef BoxLayout::takeAt(size_t index)
119 if( index >= _layout_items.size() )
120 return LayoutItemRef();
122 LayoutItems::iterator it = _layout_items.begin() + index;
123 LayoutItemRef item = it->layout_item;
125 _layout_items.erase(it);
132 //----------------------------------------------------------------------------
133 void BoxLayout::clear()
135 for( LayoutItems::iterator it = _layout_items.begin();
136 it != _layout_items.end();
139 it->layout_item->onRemove();
141 _layout_items.clear();
145 //----------------------------------------------------------------------------
146 void BoxLayout::setStretch(size_t index, int stretch)
148 if( index >= _layout_items.size() )
151 _layout_items.at(index).stretch = std::max(0, stretch);
155 //----------------------------------------------------------------------------
156 int BoxLayout::stretch(size_t index) const
158 if( index >= _layout_items.size() )
161 return _layout_items.at(index).stretch;
164 //----------------------------------------------------------------------------
165 void BoxLayout::setSpacing(int spacing)
167 if( spacing == _padding )
174 //----------------------------------------------------------------------------
175 int BoxLayout::spacing() const
180 //----------------------------------------------------------------------------
181 void BoxLayout::setDirection(Direction dir)
184 _get_layout_coord = &SGVec2i::x;
185 _get_fixed_coord = &SGVec2i::y;
188 std::swap(_get_layout_coord, _get_fixed_coord);
193 //----------------------------------------------------------------------------
194 BoxLayout::Direction BoxLayout::direction() const
199 //----------------------------------------------------------------------------
200 bool BoxLayout::hasHeightForWidth() const
202 if( _flags & SIZE_INFO_DIRTY )
205 return _layout_data.has_hfw;
208 //----------------------------------------------------------------------------
209 int BoxLayout::heightForWidth(int w) const
211 if( !hasHeightForWidth() )
218 //----------------------------------------------------------------------------
219 int BoxLayout::minimumHeightForWidth(int w) const
221 if( !hasHeightForWidth() )
225 return _hfw_min_height;
228 //----------------------------------------------------------------------------
229 void BoxLayout::setCanvas(const CanvasWeakPtr& canvas)
233 for(size_t i = 0; i < _layout_items.size(); ++i)
234 _layout_items[i].layout_item->setCanvas(canvas);
237 //----------------------------------------------------------------------------
238 bool BoxLayout::horiz() const
240 return (_direction == LeftToRight) || (_direction == RightToLeft);
243 //----------------------------------------------------------------------------
244 void BoxLayout::updateSizeHints() const
246 SGVec2i min_size(0, 0),
250 _layout_data.reset();
251 _hfw_width = _hfw_height = _hfw_min_height = -1;
253 bool is_first = true;
255 for(size_t i = 0; i < _layout_items.size(); ++i)
257 // TODO check visible
259 ItemData& item_data = _layout_items[i];
260 LayoutItem const& item = *item_data.layout_item;
262 item_data.min_size = (item.minimumSize().*_get_layout_coord)();
263 item_data.max_size = (item.maximumSize().*_get_layout_coord)();
264 item_data.size_hint = (item.sizeHint().*_get_layout_coord)();
265 item_data.has_hfw = item.hasHeightForWidth();
267 if( !dynamic_cast<SpacerItem*>(item_data.layout_item.get()) )
271 item_data.padding_orig = 0;
276 item_data.padding_orig = _padding;
277 _layout_data.padding += item_data.padding_orig;
281 // Add sizes of all children in layout direction
282 safeAdd(min_size.x(), item_data.min_size);
283 safeAdd(max_size.x(), item_data.max_size);
284 safeAdd(size_hint.x(), item_data.size_hint);
286 // Take maximum in fixed (non-layouted) direction
287 min_size.y() = std::max( min_size.y(),
288 (item.minimumSize().*_get_fixed_coord)() );
289 max_size.y() = std::max( max_size.y(),
290 (item.maximumSize().*_get_fixed_coord)() );
291 size_hint.y() = std::max( size_hint.y(),
292 (item.sizeHint().*_get_fixed_coord)() );
294 _layout_data.has_hfw = _layout_data.has_hfw || item.hasHeightForWidth();
297 safeAdd(min_size.x(), _layout_data.padding);
298 safeAdd(max_size.x(), _layout_data.padding);
299 safeAdd(size_hint.x(), _layout_data.padding);
301 _layout_data.min_size = min_size.x();
302 _layout_data.max_size = max_size.x();
303 _layout_data.size_hint = size_hint.x();
305 _min_size.x() = (min_size.*_get_layout_coord)();
306 _max_size.x() = (max_size.*_get_layout_coord)();
307 _size_hint.x() = (size_hint.*_get_layout_coord)();
309 _min_size.y() = (min_size.*_get_fixed_coord)();
310 _max_size.y() = (max_size.*_get_fixed_coord)();
311 _size_hint.y() = (size_hint.*_get_fixed_coord)();
313 _flags &= ~SIZE_INFO_DIRTY;
316 //----------------------------------------------------------------------------
317 void BoxLayout::updateWFHCache(int w) const
319 if( w == _hfw_width )
327 _layout_data.size = w;
328 const_cast<BoxLayout*>(this)->distribute(_layout_items, _layout_data);
330 for(size_t i = 0; i < _layout_items.size(); ++i)
332 ItemData const& data = _layout_items[i];
333 _hfw_height = std::max(_hfw_height, data.hfw(data.size));
334 _hfw_min_height = std::max(_hfw_min_height, data.mhfw(data.size));
339 for(size_t i = 0; i < _layout_items.size(); ++i)
341 ItemData const& data = _layout_items[i];
342 _hfw_height += data.hfw(w) + data.padding_orig;
343 _hfw_min_height += data.mhfw(w) + data.padding_orig;
350 //----------------------------------------------------------------------------
351 SGVec2i BoxLayout::sizeHintImpl() const
357 //----------------------------------------------------------------------------
358 SGVec2i BoxLayout::minimumSizeImpl() const
364 //----------------------------------------------------------------------------
365 SGVec2i BoxLayout::maximumSizeImpl() const
371 //----------------------------------------------------------------------------
372 void BoxLayout::doLayout(const SGRecti& geom)
374 if( _flags & SIZE_INFO_DIRTY )
377 // Store current size hints because vertical layouts containing
378 // height-for-width items the size hints are update for the actual width of
380 int min_size_save = _layout_data.min_size,
381 size_hint_save = _layout_data.size_hint;
383 _layout_data.size = (geom.size().*_get_layout_coord)();
385 // update width dependent data for layouting of vertical layouts
386 if( _layout_data.has_hfw && !horiz() )
388 for(size_t i = 0; i < _layout_items.size(); ++i)
390 ItemData& data = _layout_items[i];
393 int w = SGMisc<int>::clip( geom.width(),
394 data.layout_item->minimumSize().x(),
395 data.layout_item->maximumSize().x() );
397 data.min_size = data.mhfw(w);
398 data.size_hint = data.hfw(w);
400 // Update size hints for layouting with difference to size hints
401 // calculated by using the size hints provided (without trading
403 _layout_data.min_size += data.min_size
404 - data.layout_item->minimumSize().y();
405 _layout_data.size_hint += data.size_hint
406 - data.layout_item->sizeHint().y();
411 // now do the actual layouting
412 distribute(_layout_items, _layout_data);
414 // Restore size hints possibly changed by vertical layouting
415 _layout_data.min_size = min_size_save;
416 _layout_data.size_hint = size_hint_save;
418 // and finally set the layouted geometry for each item
419 int fixed_size = (geom.size().*_get_fixed_coord)();
420 SGVec2i cur_pos( (geom.pos().*_get_layout_coord)(),
421 (geom.pos().*_get_fixed_coord)() );
423 bool reverse = (_direction == RightToLeft) || (_direction == BottomToTop);
425 cur_pos.x() += (geom.size().*_get_layout_coord)();
427 for(size_t i = 0; i < _layout_items.size(); ++i)
429 ItemData const& data = _layout_items[i];
430 cur_pos.x() += reverse ? -data.padding - data.size : data.padding;
434 std::min( (data.layout_item->maximumSize().*_get_fixed_coord)(),
438 // Center in fixed direction (TODO allow specifying alignment)
439 int offset_fixed = (fixed_size - size.y()) / 2;
440 cur_pos.y() += offset_fixed;
442 data.layout_item->setGeometry(SGRecti(
443 (cur_pos.*_get_layout_coord)(),
444 (cur_pos.*_get_fixed_coord)(),
445 (size.*_get_layout_coord)(),
446 (size.*_get_fixed_coord)()
450 cur_pos.x() += data.size;
451 cur_pos.y() -= offset_fixed;
455 //----------------------------------------------------------------------------
456 HBoxLayout::HBoxLayout():
457 BoxLayout(LeftToRight)
462 //----------------------------------------------------------------------------
463 VBoxLayout::VBoxLayout():
464 BoxLayout(TopToBottom)
469 } // namespace canvas
470 } // namespace simgear