]> git.mxchange.org Git - simgear.git/blob - simgear/canvas/layout/Layout.hxx
canvas::BoxLayout: add custom additional whitespace (spacer/stretch)
[simgear.git] / simgear / canvas / layout / Layout.hxx
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 #ifndef SG_CANVAS_LAYOUT_HXX_
20 #define SG_CANVAS_LAYOUT_HXX_
21
22 #include "LayoutItem.hxx"
23 #include <vector>
24
25 namespace simgear
26 {
27 namespace canvas
28 {
29
30   class Layout:
31     public LayoutItem
32   {
33     public:
34       void update();
35
36       virtual void invalidate();
37       virtual void setGeometry(const SGRecti& geom);
38
39       virtual void addItem(const LayoutItemRef& item) = 0;
40       virtual void setSpacing(int spacing) = 0;
41       virtual int spacing() const = 0;
42
43     protected:
44       enum LayoutFlags
45       {
46         LAYOUT_DIRTY = LayoutItem::LAST_FLAG << 1,
47         LAST_FLAG = LAYOUT_DIRTY
48       };
49
50       struct ItemData
51       {
52         LayoutItemRef layout_item;
53         int     size_hint,
54                 min_size,
55                 max_size,
56                 padding_orig, //<! original padding as specified by the user
57                 padding,      //<! padding before element (layouted)
58                 size,         //<! layouted size
59                 stretch;      //<! stretch factor
60         bool    done;         //<! layouting done
61
62         /** Clear values (reset to default/empty state) */
63         void reset();
64       };
65
66       /**
67        * Override to implement the actual layouting
68        */
69       virtual void doLayout(const SGRecti& geom) = 0;
70
71       /**
72        * Add two integers taking care of overflow (limit to INT_MAX)
73        */
74       static void safeAdd(int& a, int b);
75
76       /**
77        * Distribute the available @a space to all @a items
78        */
79       void distribute(std::vector<ItemData>& items, const ItemData& space);
80
81     private:
82
83       int _num_not_done, //<! number of children not layouted yet
84           _sum_stretch,  //<! sum of stretch factors of all not yet layouted
85                          //   children
86           _space_stretch,//<! space currently assigned to all not yet layouted
87                          //   stretchable children
88           _space_left;   //<! remaining space not used by any child yet
89
90   };
91
92   typedef SGSharedPtr<Layout> LayoutRef;
93
94 } // namespace canvas
95 } // namespace simgear
96
97
98 #endif /* SG_CANVAS_LAYOUT_HXX_ */