]> git.mxchange.org Git - simgear.git/blob - simgear/canvas/layout/Layout.cxx
canvas::Layout: proper cleanup/update on removing items.
[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)) )
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     has_hfw     = false;
89     done        = false;
90   }
91
92   //----------------------------------------------------------------------------
93   int Layout::ItemData::hfw(int w) const
94   {
95     if( has_hfw )
96       return layout_item->heightForWidth(w);
97     else
98       return layout_item->sizeHint().y();
99   }
100
101   //----------------------------------------------------------------------------
102   int Layout::ItemData::mhfw(int w) const
103   {
104     if( has_hfw )
105       return layout_item->minimumHeightForWidth(w);
106     else
107       return layout_item->minimumSize().y();
108   }
109
110   //----------------------------------------------------------------------------
111   void Layout::safeAdd(int& a, int b)
112   {
113     if( SGLimits<int>::max() - b < a )
114       a = SGLimits<int>::max();
115     else
116       a += b;
117   }
118
119   //----------------------------------------------------------------------------
120   void Layout::distribute(std::vector<ItemData>& items, const ItemData& space)
121   {
122     const int num_children = static_cast<int>(items.size());
123     _num_not_done = num_children;
124
125     SG_LOG( SG_GUI,
126             SG_DEBUG,
127             "Layout::distribute(" << num_children << " items)" );
128
129     if( space.size < space.min_size )
130     {
131       // TODO
132       SG_LOG( SG_GUI, SG_WARN, "Layout: not enough size (not implemented)");
133     }
134     else if( space.size < space.max_size )
135     {
136       _sum_stretch = 0;
137       _space_stretch = 0;
138
139       bool less_then_hint = space.size < space.size_hint;
140
141       // Give min_size/size_hint to all items
142       _space_left = space.size
143                   - (less_then_hint ? space.min_size : space.size_hint);
144       for(int i = 0; i < num_children; ++i)
145       {
146         ItemData& d = items[i];
147         d.size = less_then_hint ? d.min_size : d.size_hint;
148         d.padding = d.padding_orig;
149         d.done = d.size >= (less_then_hint ? d.size_hint : d.max_size);
150
151         SG_LOG(
152           SG_GUI,
153           SG_DEBUG,
154           i << ") initial=" << d.size
155             << ", min=" << d.min_size
156             << ", hint=" << d.size_hint
157             << ", max=" << d.max_size
158         );
159
160         if( d.done )
161         {
162           _num_not_done -= 1;
163           continue;
164         }
165
166         if( d.stretch > 0 )
167         {
168           _sum_stretch += d.stretch;
169           _space_stretch += d.size;
170         }
171       }
172
173       // Distribute remaining space to increase the size of each item up to its
174       // size_hint/max_size
175       while( _space_left > 0 )
176       {
177         if( _num_not_done <= 0 )
178         {
179           SG_LOG(SG_GUI, SG_WARN, "space left, but no more items?");
180           break;
181         }
182
183         int space_per_element = std::max(1, _space_left / _num_not_done);
184
185         SG_LOG(SG_GUI, SG_DEBUG, "space/element=" << space_per_element);
186
187         for(int i = 0; i < num_children; ++i)
188         {
189           ItemData& d = items[i];
190
191           SG_LOG(
192             SG_GUI,
193             SG_DEBUG,
194             i << ") left=" << _space_left
195               << ", not_done=" << _num_not_done
196               << ", sum=" << _sum_stretch
197               << ", stretch=" << _space_stretch
198               << ", stretch/unit=" << _space_stretch / std::max(1, _sum_stretch)
199           );
200
201           if( d.done )
202             continue;
203
204           if( _sum_stretch > 0 && d.stretch <= 0 )
205             d.done = true;
206           else
207           {
208             int target_size = 0;
209             int max_size = less_then_hint ? d.size_hint : d.max_size;
210
211             if( _sum_stretch > 0 )
212             {
213               target_size = (d.stretch * (_space_left + _space_stretch))
214                           / _sum_stretch;
215
216               // Item would be smaller than minimum size or larger than maximum
217               // size, so just keep bounded size and ignore stretch factor
218               if( target_size <= d.size || target_size >= max_size )
219               {
220                 d.done = true;
221                 _sum_stretch -= d.stretch;
222                 _space_stretch -= d.size;
223
224                 if( target_size >= max_size )
225                   target_size = max_size;
226                 else
227                   target_size = d.size;
228               }
229               else
230                 _space_stretch += target_size - d.size;
231             }
232             else
233             {
234               // Give space evenly to all remaining elements in this round
235               target_size = d.size + std::min(_space_left, space_per_element);
236
237               if( target_size >= max_size )
238               {
239                 d.done = true;
240                 target_size = max_size;
241               }
242             }
243
244             int old_size = d.size;
245             d.size = target_size;
246             _space_left -= d.size - old_size;
247           }
248
249           if( d.done )
250           {
251             _num_not_done -= 1;
252
253             if( _sum_stretch <= 0 && d.stretch > 0 )
254               // Distribute remaining space evenly to all non-stretchable items
255               // in a new round
256               break;
257           }
258         }
259       }
260     }
261     else
262     {
263       _space_left = space.size - space.max_size;
264       for(int i = 0; i < num_children; ++i)
265       {
266         ItemData& d = items[i];
267         d.size = d.max_size;
268
269         // Add superfluous space as padding
270         d.padding = d.padding_orig + _space_left
271                                    // Padding after last child...
272                                    / (_num_not_done + 1);
273
274         _space_left -= d.padding - d.padding_orig;
275         _num_not_done -= 1;
276       }
277     }
278
279     SG_LOG(SG_GUI, SG_DEBUG, "distribute:");
280     for(int i = 0; i < num_children; ++i)
281     {
282       ItemData const& d = items[i];
283       SG_LOG( SG_GUI,
284               SG_DEBUG,
285               i << ") pad=" << d.padding
286                 << ", size = " << d.size );
287     }
288   }
289
290 } // namespace canvas
291 } // namespace simgear