]> git.mxchange.org Git - simgear.git/blob - simgear/canvas/layout/Layout.cxx
Move overflow protected add helpers to math.
[simgear.git] / simgear / canvas / layout / Layout.cxx
1 // Basic class for canvas layouts
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 "Layout.hxx"
20 #include <simgear/debug/logstream.hxx>
21
22 namespace simgear
23 {
24 namespace canvas
25 {
26
27   //----------------------------------------------------------------------------
28   void Layout::update()
29   {
30     if( !(_flags & (LAYOUT_DIRTY | SIZE_INFO_DIRTY)) || !isVisible() )
31       return;
32
33     doLayout(_geometry);
34
35     _flags &= ~LAYOUT_DIRTY;
36   }
37
38   //----------------------------------------------------------------------------
39   void Layout::invalidate()
40   {
41     LayoutItem::invalidate();
42     _flags |= LAYOUT_DIRTY;
43   }
44
45   //----------------------------------------------------------------------------
46   void Layout::setGeometry(const SGRecti& geom)
47   {
48     if( geom != _geometry )
49     {
50       _geometry = geom;
51       _flags |= LAYOUT_DIRTY;
52     }
53
54     update();
55   }
56
57   //----------------------------------------------------------------------------
58   void Layout::removeItem(const LayoutItemRef& item)
59   {
60     size_t i = 0;
61     while( LayoutItemRef child = itemAt(i) )
62     {
63       if( item == child )
64         return (void)takeAt(i);
65
66       ++i;
67     }
68   }
69
70   //----------------------------------------------------------------------------
71   void Layout::clear()
72   {
73     while( itemAt(0) )
74       takeAt(0);
75   }
76
77   //----------------------------------------------------------------------------
78   void Layout::ItemData::reset()
79   {
80     layout_item = 0;
81     size_hint   = 0;
82     min_size    = 0;
83     max_size    = 0;
84     padding_orig= 0;
85     padding     = 0;
86     size        = 0;
87     stretch     = 0;
88     visible     = false;
89     has_hfw     = false;
90     done        = false;
91   }
92
93   //----------------------------------------------------------------------------
94   int Layout::ItemData::hfw(int w) const
95   {
96     if( has_hfw )
97       return layout_item->heightForWidth(w);
98     else
99       return layout_item->sizeHint().y();
100   }
101
102   //----------------------------------------------------------------------------
103   int Layout::ItemData::mhfw(int w) const
104   {
105     if( has_hfw )
106       return layout_item->minimumHeightForWidth(w);
107     else
108       return layout_item->minimumSize().y();
109   }
110
111   //----------------------------------------------------------------------------
112   void Layout::distribute(std::vector<ItemData>& items, const ItemData& space)
113   {
114     const int num_children = static_cast<int>(items.size());
115     _num_not_done = 0;
116
117     SG_LOG( SG_GUI,
118             SG_DEBUG,
119             "Layout::distribute(" << space.size << "px for "
120                                   << num_children << " items, s.t."
121                                   << " min=" << space.min_size
122                                   << ", hint=" << space.size_hint
123                                   << ", max=" << space.max_size << ")" );
124
125     if( space.size < space.min_size )
126     {
127       // TODO
128       SG_LOG( SG_GUI, SG_WARN, "Layout: not enough size (not implemented)");
129     }
130     else if( space.size < space.max_size )
131     {
132       _sum_stretch = 0;
133       _space_stretch = 0;
134
135       bool less_then_hint = space.size < space.size_hint;
136
137       // Give min_size/size_hint to all items
138       _space_left = space.size
139                   - (less_then_hint ? space.min_size : space.size_hint);
140       for(int i = 0; i < num_children; ++i)
141       {
142         ItemData& d = items[i];
143         if( !d.visible )
144           continue;
145
146         d.size = less_then_hint ? d.min_size : d.size_hint;
147         d.padding = d.padding_orig;
148         d.done = d.size >= (less_then_hint ? d.size_hint : d.max_size);
149
150         SG_LOG(
151           SG_GUI,
152           SG_DEBUG,
153           i << ") initial=" << d.size
154             << ", min=" << d.min_size
155             << ", hint=" << d.size_hint
156             << ", max=" << d.max_size
157         );
158
159         if( d.done )
160           continue;
161         _num_not_done += 1;
162
163         if( d.stretch > 0 )
164         {
165           _sum_stretch += d.stretch;
166           _space_stretch += d.size;
167         }
168       }
169
170       // Distribute remaining space to increase the size of each item up to its
171       // size_hint/max_size
172       while( _space_left > 0 )
173       {
174         if( _num_not_done <= 0 )
175         {
176           SG_LOG(SG_GUI, SG_WARN, "space left, but no more items?");
177           break;
178         }
179
180         int space_per_element = std::max(1, _space_left / _num_not_done);
181
182         SG_LOG(SG_GUI, SG_DEBUG, "space/element=" << space_per_element);
183
184         for(int i = 0; i < num_children; ++i)
185         {
186           ItemData& d = items[i];
187           if( !d.visible )
188             continue;
189
190           SG_LOG(
191             SG_GUI,
192             SG_DEBUG,
193             i << ") left=" << _space_left
194               << ", not_done=" << _num_not_done
195               << ", sum=" << _sum_stretch
196               << ", stretch=" << _space_stretch
197               << ", stretch/unit=" << _space_stretch / std::max(1, _sum_stretch)
198           );
199
200           if( d.done )
201             continue;
202
203           if( _sum_stretch > 0 && d.stretch <= 0 )
204             d.done = true;
205           else
206           {
207             int target_size = 0;
208             int max_size = less_then_hint ? d.size_hint : d.max_size;
209
210             if( _sum_stretch > 0 )
211             {
212               target_size = (d.stretch * (_space_left + _space_stretch))
213                           / _sum_stretch;
214
215               // Item would be smaller than minimum size or larger than maximum
216               // size, so just keep bounded size and ignore stretch factor
217               if( target_size <= d.size || target_size >= max_size )
218               {
219                 d.done = true;
220                 _sum_stretch -= d.stretch;
221                 _space_stretch -= d.size;
222
223                 if( target_size >= max_size )
224                   target_size = max_size;
225                 else
226                   target_size = d.size;
227               }
228               else
229                 _space_stretch += target_size - d.size;
230             }
231             else
232             {
233               // Give space evenly to all remaining elements in this round
234               target_size = d.size + std::min(_space_left, space_per_element);
235
236               if( target_size >= max_size )
237               {
238                 d.done = true;
239                 target_size = max_size;
240               }
241             }
242
243             int old_size = d.size;
244             d.size = target_size;
245             _space_left -= d.size - old_size;
246           }
247
248           if( d.done )
249           {
250             _num_not_done -= 1;
251
252             if( _sum_stretch <= 0 && d.stretch > 0 )
253               // Distribute remaining space evenly to all non-stretchable items
254               // in a new round
255               break;
256           }
257         }
258       }
259     }
260     else
261     {
262       _space_left = space.size - space.max_size;
263       for(int i = 0; i < num_children; ++i)
264       {
265         if( items[i].visible )
266           _num_not_done += 1;
267       }
268
269       for(int i = 0; i < num_children; ++i)
270       {
271         ItemData& d = items[i];
272         if( !d.visible )
273           continue;
274
275         d.size = d.max_size;
276
277         // Add superfluous space as padding
278         d.padding = d.padding_orig + _space_left
279                                    // Padding after last child...
280                                    / (_num_not_done + 1);
281
282         _space_left -= d.padding - d.padding_orig;
283         _num_not_done -= 1;
284       }
285     }
286
287     SG_LOG(SG_GUI, SG_DEBUG, "distribute:");
288     for(int i = 0; i < num_children; ++i)
289     {
290       ItemData const& d = items[i];
291       if( d.visible )
292         SG_LOG(SG_GUI, SG_DEBUG, i << ") pad=" << d.padding
293                                    << ", size= " << d.size);
294       else
295         SG_LOG(SG_GUI, SG_DEBUG, i << ") [hidden]");
296     }
297   }
298
299 } // namespace canvas
300 } // namespace simgear