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 //----------------------------------------------------------------------------
28 void Layout::removeItem(const LayoutItemRef& item)
31 while( LayoutItemRef child = itemAt(i) )
34 return (void)takeAt(i);
40 //----------------------------------------------------------------------------
47 //----------------------------------------------------------------------------
48 void Layout::ItemData::reset()
63 //----------------------------------------------------------------------------
64 int Layout::ItemData::hfw(int w) const
67 return layout_item->heightForWidth(w);
69 return layout_item->sizeHint().y();
72 //----------------------------------------------------------------------------
73 int Layout::ItemData::mhfw(int w) const
76 return layout_item->minimumHeightForWidth(w);
78 return layout_item->minimumSize().y();
81 //----------------------------------------------------------------------------
82 void Layout::contentsRectChanged(const SGRecti& rect)
86 _flags &= ~LAYOUT_DIRTY;
89 //----------------------------------------------------------------------------
90 void Layout::distribute(std::vector<ItemData>& items, const ItemData& space)
92 const int num_children = static_cast<int>(items.size());
97 "Layout::distribute(" << space.size << "px for "
98 << num_children << " items, s.t."
99 << " min=" << space.min_size
100 << ", hint=" << space.size_hint
101 << ", max=" << space.max_size << ")" );
103 if( space.size < space.min_size )
106 SG_LOG( SG_GUI, SG_WARN, "Layout: not enough size (not implemented)");
108 else if( space.size < space.max_size )
113 bool less_then_hint = space.size < space.size_hint;
115 // Give min_size/size_hint to all items
116 _space_left = space.size
117 - (less_then_hint ? space.min_size : space.size_hint);
118 for(int i = 0; i < num_children; ++i)
120 ItemData& d = items[i];
124 d.size = less_then_hint ? d.min_size : d.size_hint;
125 d.padding = d.padding_orig;
126 d.done = d.size >= (less_then_hint ? d.size_hint : d.max_size);
131 i << ") initial=" << d.size
132 << ", min=" << d.min_size
133 << ", hint=" << d.size_hint
134 << ", max=" << d.max_size
143 _sum_stretch += d.stretch;
144 _space_stretch += d.size;
148 // Distribute remaining space to increase the size of each item up to its
149 // size_hint/max_size
150 while( _space_left > 0 )
152 if( _num_not_done <= 0 )
154 SG_LOG(SG_GUI, SG_WARN, "space left, but no more items?");
158 int space_per_element = std::max(1, _space_left / _num_not_done);
160 SG_LOG(SG_GUI, SG_DEBUG, "space/element=" << space_per_element);
162 for(int i = 0; i < num_children; ++i)
164 ItemData& d = items[i];
171 i << ") left=" << _space_left
172 << ", not_done=" << _num_not_done
173 << ", sum=" << _sum_stretch
174 << ", stretch=" << _space_stretch
175 << ", stretch/unit=" << _space_stretch / std::max(1, _sum_stretch)
181 if( _sum_stretch > 0 && d.stretch <= 0 )
186 int max_size = less_then_hint ? d.size_hint : d.max_size;
188 if( _sum_stretch > 0 )
190 target_size = (d.stretch * (_space_left + _space_stretch))
193 // Item would be smaller than minimum size or larger than maximum
194 // size, so just keep bounded size and ignore stretch factor
195 if( target_size <= d.size || target_size >= max_size )
198 _sum_stretch -= d.stretch;
199 _space_stretch -= d.size;
201 if( target_size >= max_size )
202 target_size = max_size;
204 target_size = d.size;
207 _space_stretch += target_size - d.size;
211 // Give space evenly to all remaining elements in this round
212 target_size = d.size + std::min(_space_left, space_per_element);
214 if( target_size >= max_size )
217 target_size = max_size;
221 int old_size = d.size;
222 d.size = target_size;
223 _space_left -= d.size - old_size;
230 if( _sum_stretch <= 0 && d.stretch > 0 )
231 // Distribute remaining space evenly to all non-stretchable items
240 _space_left = space.size - space.max_size;
241 for(int i = 0; i < num_children; ++i)
243 if( items[i].visible )
247 for(int i = 0; i < num_children; ++i)
249 ItemData& d = items[i];
255 // Add superfluous space as padding
256 d.padding = d.padding_orig + _space_left
257 // Padding after last child...
258 / (_num_not_done + 1);
260 _space_left -= d.padding - d.padding_orig;
265 SG_LOG(SG_GUI, SG_DEBUG, "distribute:");
266 for(int i = 0; i < num_children; ++i)
268 ItemData const& d = items[i];
270 SG_LOG(SG_GUI, SG_DEBUG, i << ") pad=" << d.padding
271 << ", size= " << d.size);
273 SG_LOG(SG_GUI, SG_DEBUG, i << ") [hidden]");
277 } // namespace canvas
278 } // namespace simgear