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 void BoxLayout::addItem(const LayoutItemRef& item)
38 return addItem(item, 0);
41 //----------------------------------------------------------------------------
42 void BoxLayout::addItem(const LayoutItemRef& item, int stretch)
44 insertItem(-1, item, stretch);
47 //----------------------------------------------------------------------------
48 void BoxLayout::addStretch(int stretch)
50 insertStretch(-1, stretch);
53 //----------------------------------------------------------------------------
54 void BoxLayout::addSpacing(int size)
56 insertSpacing(-1, size);
59 //----------------------------------------------------------------------------
60 void BoxLayout::insertItem(int index, const LayoutItemRef& item, int stretch)
62 ItemData item_data = {0};
63 item_data.layout_item = item;
64 item_data.stretch = std::max(0, stretch);
66 item->setCanvas(_canvas);
67 item->setParent(this);
70 _layout_items.push_back(item_data);
72 _layout_items.insert(_layout_items.begin() + index, item_data);
77 //----------------------------------------------------------------------------
78 void BoxLayout::insertStretch(int index, int stretch)
80 insertItem(index, LayoutItemRef(new SpacerItem()), stretch);
83 //----------------------------------------------------------------------------
84 void BoxLayout::insertSpacing(int index, int size)
86 SGVec2i size_hint = horiz()
91 insertItem(index, LayoutItemRef(new SpacerItem(size_hint, max_size)));
94 //----------------------------------------------------------------------------
95 void BoxLayout::setStretch(size_t index, int stretch)
97 if( index >= _layout_items.size() )
100 _layout_items.at(index).stretch = std::max(0, stretch);
104 //----------------------------------------------------------------------------
105 int BoxLayout::stretch(size_t index) const
107 if( index >= _layout_items.size() )
110 return _layout_items.at(index).stretch;
113 //----------------------------------------------------------------------------
114 void BoxLayout::setSpacing(int spacing)
116 if( spacing == _padding )
123 //----------------------------------------------------------------------------
124 int BoxLayout::spacing() const
129 //----------------------------------------------------------------------------
130 void BoxLayout::setDirection(Direction dir)
133 _get_layout_coord = &SGVec2i::x;
134 _get_fixed_coord = &SGVec2i::y;
137 std::swap(_get_layout_coord, _get_fixed_coord);
142 //----------------------------------------------------------------------------
143 BoxLayout::Direction BoxLayout::direction() const
148 //----------------------------------------------------------------------------
149 void BoxLayout::setCanvas(const CanvasWeakPtr& canvas)
153 for(size_t i = 0; i < _layout_items.size(); ++i)
154 _layout_items[i].layout_item->setCanvas(canvas);
157 //----------------------------------------------------------------------------
158 bool BoxLayout::horiz() const
160 return (_direction == LeftToRight) || (_direction == RightToLeft);
163 //----------------------------------------------------------------------------
164 void BoxLayout::updateSizeHints() const
166 SGVec2i min_size(0, 0),
170 _layout_data.reset();
171 bool is_first = true;
173 for(size_t i = 0; i < _layout_items.size(); ++i)
175 // TODO check visible
177 ItemData& item_data = _layout_items[i];
178 LayoutItem const& item = *item_data.layout_item;
180 item_data.min_size = (item.minimumSize().*_get_layout_coord)();
181 item_data.max_size = (item.maximumSize().*_get_layout_coord)();
182 item_data.size_hint = (item.sizeHint().*_get_layout_coord)();
184 if( !dynamic_cast<SpacerItem*>(item_data.layout_item.get()) )
188 item_data.padding_orig = 0;
193 item_data.padding_orig = _padding;
194 _layout_data.padding += item_data.padding_orig;
198 // Add sizes of all children in layout direction
199 safeAdd(min_size.x(), item_data.min_size);
200 safeAdd(max_size.x(), item_data.max_size);
201 safeAdd(size_hint.x(), item_data.size_hint);
203 // Take maximum in fixed (non-layouted) direction
204 min_size.y() = std::max( min_size.y(),
205 (item.minimumSize().*_get_fixed_coord)() );
206 max_size.y() = std::max( max_size.y(),
207 (item.maximumSize().*_get_fixed_coord)() );
208 size_hint.y() = std::max( size_hint.y(),
209 (item.sizeHint().*_get_fixed_coord)() );
212 safeAdd(min_size.x(), _layout_data.padding);
213 safeAdd(max_size.x(), _layout_data.padding);
214 safeAdd(size_hint.x(), _layout_data.padding);
216 _layout_data.min_size = min_size.x();
217 _layout_data.max_size = max_size.x();
218 _layout_data.size_hint = size_hint.x();
220 _min_size.x() = (min_size.*_get_layout_coord)();
221 _max_size.x() = (max_size.*_get_layout_coord)();
222 _size_hint.x() = (size_hint.*_get_layout_coord)();
224 _min_size.y() = (min_size.*_get_fixed_coord)();
225 _max_size.y() = (max_size.*_get_fixed_coord)();
226 _size_hint.y() = (size_hint.*_get_fixed_coord)();
228 _flags &= ~SIZE_INFO_DIRTY;
231 //----------------------------------------------------------------------------
232 SGVec2i BoxLayout::sizeHintImpl() const
238 //----------------------------------------------------------------------------
239 SGVec2i BoxLayout::minimumSizeImpl() const
245 //----------------------------------------------------------------------------
246 SGVec2i BoxLayout::maximumSizeImpl() const
252 //----------------------------------------------------------------------------
253 void BoxLayout::doLayout(const SGRecti& geom)
255 if( _flags & SIZE_INFO_DIRTY )
258 _layout_data.size = (geom.size().*_get_layout_coord)();
259 distribute(_layout_items, _layout_data);
261 int fixed_size = (geom.size().*_get_fixed_coord)();
262 SGVec2i cur_pos( (geom.pos().*_get_layout_coord)(),
263 (geom.pos().*_get_fixed_coord)() );
265 bool reverse = (_direction == RightToLeft) || (_direction == BottomToTop);
267 cur_pos.x() += (geom.size().*_get_layout_coord)();
269 // TODO handle reverse layouting (rtl/btt)
270 for(size_t i = 0; i < _layout_items.size(); ++i)
272 ItemData const& data = _layout_items[i];
273 cur_pos.x() += reverse ? -data.padding - data.size : data.padding;
277 std::min( (data.layout_item->maximumSize().*_get_fixed_coord)(),
281 // Center in fixed direction (TODO allow specifying alignment)
282 int offset_fixed = (fixed_size - size.y()) / 2;
283 cur_pos.y() += offset_fixed;
285 data.layout_item->setGeometry(SGRecti(
286 (cur_pos.*_get_layout_coord)(),
287 (cur_pos.*_get_fixed_coord)(),
288 (size.*_get_layout_coord)(),
289 (size.*_get_fixed_coord)()
293 cur_pos.x() += data.size;
294 cur_pos.y() -= offset_fixed;
298 //----------------------------------------------------------------------------
299 HBoxLayout::HBoxLayout():
300 BoxLayout(LeftToRight)
305 //----------------------------------------------------------------------------
306 VBoxLayout::VBoxLayout():
307 BoxLayout(TopToBottom)
312 } // namespace canvas
313 } // namespace simgear