1 // Basic class for canvas layouts
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
20 #include <simgear/debug/logstream.hxx>
27 //----------------------------------------------------------------------------
30 if( !(_flags & (LAYOUT_DIRTY | SIZE_INFO_DIRTY)) || !isVisible() )
35 _flags &= ~LAYOUT_DIRTY;
38 //----------------------------------------------------------------------------
39 void Layout::invalidate()
41 LayoutItem::invalidate();
42 _flags |= LAYOUT_DIRTY;
45 //----------------------------------------------------------------------------
46 void Layout::setGeometry(const SGRecti& geom)
48 if( geom != _geometry )
51 _flags |= LAYOUT_DIRTY;
57 //----------------------------------------------------------------------------
58 void Layout::removeItem(const LayoutItemRef& item)
61 while( LayoutItemRef child = itemAt(i) )
64 return (void)takeAt(i);
70 //----------------------------------------------------------------------------
77 //----------------------------------------------------------------------------
78 void Layout::ItemData::reset()
93 //----------------------------------------------------------------------------
94 int Layout::ItemData::hfw(int w) const
97 return layout_item->heightForWidth(w);
99 return layout_item->sizeHint().y();
102 //----------------------------------------------------------------------------
103 int Layout::ItemData::mhfw(int w) const
106 return layout_item->minimumHeightForWidth(w);
108 return layout_item->minimumSize().y();
111 //----------------------------------------------------------------------------
112 void Layout::safeAdd(int& a, int b)
114 if( SGLimits<int>::max() - b < a )
115 a = SGLimits<int>::max();
120 //----------------------------------------------------------------------------
121 void Layout::distribute(std::vector<ItemData>& items, const ItemData& space)
123 const int num_children = static_cast<int>(items.size());
128 "Layout::distribute(" << space.size << "px for "
129 << num_children << " items, s.t."
130 << " min=" << space.min_size
131 << ", hint=" << space.size_hint
132 << ", max=" << space.max_size << ")" );
134 if( space.size < space.min_size )
137 SG_LOG( SG_GUI, SG_WARN, "Layout: not enough size (not implemented)");
139 else if( space.size < space.max_size )
144 bool less_then_hint = space.size < space.size_hint;
146 // Give min_size/size_hint to all items
147 _space_left = space.size
148 - (less_then_hint ? space.min_size : space.size_hint);
149 for(int i = 0; i < num_children; ++i)
151 ItemData& d = items[i];
155 d.size = less_then_hint ? d.min_size : d.size_hint;
156 d.padding = d.padding_orig;
157 d.done = d.size >= (less_then_hint ? d.size_hint : d.max_size);
162 i << ") initial=" << d.size
163 << ", min=" << d.min_size
164 << ", hint=" << d.size_hint
165 << ", max=" << d.max_size
174 _sum_stretch += d.stretch;
175 _space_stretch += d.size;
179 // Distribute remaining space to increase the size of each item up to its
180 // size_hint/max_size
181 while( _space_left > 0 )
183 if( _num_not_done <= 0 )
185 SG_LOG(SG_GUI, SG_WARN, "space left, but no more items?");
189 int space_per_element = std::max(1, _space_left / _num_not_done);
191 SG_LOG(SG_GUI, SG_DEBUG, "space/element=" << space_per_element);
193 for(int i = 0; i < num_children; ++i)
195 ItemData& d = items[i];
202 i << ") left=" << _space_left
203 << ", not_done=" << _num_not_done
204 << ", sum=" << _sum_stretch
205 << ", stretch=" << _space_stretch
206 << ", stretch/unit=" << _space_stretch / std::max(1, _sum_stretch)
212 if( _sum_stretch > 0 && d.stretch <= 0 )
217 int max_size = less_then_hint ? d.size_hint : d.max_size;
219 if( _sum_stretch > 0 )
221 target_size = (d.stretch * (_space_left + _space_stretch))
224 // Item would be smaller than minimum size or larger than maximum
225 // size, so just keep bounded size and ignore stretch factor
226 if( target_size <= d.size || target_size >= max_size )
229 _sum_stretch -= d.stretch;
230 _space_stretch -= d.size;
232 if( target_size >= max_size )
233 target_size = max_size;
235 target_size = d.size;
238 _space_stretch += target_size - d.size;
242 // Give space evenly to all remaining elements in this round
243 target_size = d.size + std::min(_space_left, space_per_element);
245 if( target_size >= max_size )
248 target_size = max_size;
252 int old_size = d.size;
253 d.size = target_size;
254 _space_left -= d.size - old_size;
261 if( _sum_stretch <= 0 && d.stretch > 0 )
262 // Distribute remaining space evenly to all non-stretchable items
271 _space_left = space.size - space.max_size;
272 for(int i = 0; i < num_children; ++i)
274 if( items[i].visible )
278 for(int i = 0; i < num_children; ++i)
280 ItemData& d = items[i];
286 // Add superfluous space as padding
287 d.padding = d.padding_orig + _space_left
288 // Padding after last child...
289 / (_num_not_done + 1);
291 _space_left -= d.padding - d.padding_orig;
296 SG_LOG(SG_GUI, SG_DEBUG, "distribute:");
297 for(int i = 0; i < num_children; ++i)
299 ItemData const& d = items[i];
301 SG_LOG(SG_GUI, SG_DEBUG, i << ") pad=" << d.padding
302 << ", size= " << d.size);
304 SG_LOG(SG_GUI, SG_DEBUG, i << ") [hidden]");
308 } // namespace canvas
309 } // namespace simgear