]> git.mxchange.org Git - simgear.git/blob - simgear/canvas/layout/Layout.cxx
canvas::Layout: remove/get child 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       return;
50
51     _geometry = geom;
52     _flags |= LAYOUT_DIRTY;
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::ItemData::reset()
72   {
73     layout_item = 0;
74     size_hint   = 0;
75     min_size    = 0;
76     max_size    = 0;
77     padding_orig= 0;
78     padding     = 0;
79     size        = 0;
80     stretch     = 0;
81     done        = false;
82   }
83
84   //----------------------------------------------------------------------------
85   void Layout::safeAdd(int& a, int b)
86   {
87     if( SGLimits<int>::max() - b < a )
88       a = SGLimits<int>::max();
89     else
90       a += b;
91   }
92
93   //----------------------------------------------------------------------------
94   void Layout::distribute(std::vector<ItemData>& items, const ItemData& space)
95   {
96     const int num_children = static_cast<int>(items.size());
97     _num_not_done = num_children;
98
99     SG_LOG( SG_GUI,
100             SG_DEBUG,
101             "Layout::distribute(" << num_children << " items)" );
102
103     if( space.size < space.min_size )
104     {
105       // TODO
106       SG_LOG( SG_GUI, SG_WARN, "Layout: not enough size (not implemented)");
107     }
108     else if( space.size < space.max_size )
109     {
110       _sum_stretch = 0;
111       _space_stretch = 0;
112
113       bool less_then_hint = space.size < space.size_hint;
114
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)
119       {
120         ItemData& d = items[i];
121         d.size = less_then_hint ? d.min_size : d.size_hint;
122         d.padding = d.padding_orig;
123         d.done = d.size >= (less_then_hint ? d.size_hint : d.max_size);
124
125         if( d.done )
126         {
127           _num_not_done -= 1;
128           continue;
129         }
130
131         if( d.stretch > 0 )
132         {
133           _sum_stretch += d.stretch;
134           _space_stretch += d.size;
135         }
136       }
137
138       // Distribute remaining space to increase the size of each item up to its
139       // size_hint/max_size
140       while( _space_left > 0 )
141       {
142         if( _num_not_done <= 0 )
143         {
144           SG_LOG(SG_GUI, SG_WARN, "space left, but no more items?");
145           break;
146         }
147
148         int space_per_element = std::max(1, _space_left / _num_not_done);
149
150         SG_LOG(SG_GUI, SG_DEBUG, "space/element=" << space_per_element);
151
152         for(int i = 0; i < num_children; ++i)
153         {
154           ItemData& d = items[i];
155
156           SG_LOG(
157             SG_GUI,
158             SG_DEBUG,
159             i << ") left=" << _space_left
160               << ", not_done=" << _num_not_done
161               << ", sum=" << _sum_stretch
162               << ", stretch=" << _space_stretch
163               << ", stretch/unit=" << _space_stretch / std::max(1, _sum_stretch)
164           );
165
166           if( d.done )
167             continue;
168
169           if( _sum_stretch > 0 && d.stretch <= 0 )
170             d.done = true;
171           else
172           {
173             int target_size = 0;
174             int max_size = less_then_hint ? d.size_hint : d.max_size;
175
176             if( _sum_stretch > 0 )
177             {
178               target_size = (d.stretch * (_space_left + _space_stretch))
179                           / _sum_stretch;
180
181               // Item would be smaller than minimum size or larger than maximum
182               // size, so just keep bounded size and ignore stretch factor
183               if( target_size <= d.size || target_size >= max_size )
184               {
185                 d.done = true;
186                 _sum_stretch -= d.stretch;
187                 _space_stretch -= d.size;
188
189                 if( target_size >= max_size )
190                   target_size = max_size;
191                 else
192                   target_size = d.size;
193               }
194               else
195                 _space_stretch += target_size - d.size;
196             }
197             else
198             {
199               // Give space evenly to all remaining elements in this round
200               target_size = d.size + std::min(_space_left, space_per_element);
201
202               if( target_size >= max_size )
203               {
204                 d.done = true;
205                 target_size = max_size;
206               }
207             }
208
209             int old_size = d.size;
210             d.size = target_size;
211             _space_left -= d.size - old_size;
212           }
213
214           if( d.done )
215           {
216             _num_not_done -= 1;
217
218             if( _sum_stretch <= 0 && d.stretch > 0 )
219               // Distribute remaining space evenly to all non-stretchable items
220               // in a new round
221               break;
222           }
223         }
224       }
225     }
226     else
227     {
228       _space_left = space.size - space.max_size;
229       for(int i = 0; i < num_children; ++i)
230       {
231         ItemData& d = items[i];
232         d.size = d.max_size;
233
234         // Add superfluous space as padding
235         d.padding = d.padding_orig + _space_left
236                                    // Padding after last child...
237                                    / (_num_not_done + 1);
238
239         _space_left -= d.padding - d.padding_orig;
240         _num_not_done -= 1;
241       }
242     }
243
244     SG_LOG(SG_GUI, SG_DEBUG, "distribute:");
245     for(int i = 0; i < num_children; ++i)
246     {
247       ItemData const& d = items[i];
248       SG_LOG( SG_GUI,
249               SG_DEBUG,
250               i << ") pad=" << d.padding
251                 << ", size = " << d.size );
252     }
253   }
254
255 } // namespace canvas
256 } // namespace simgear