]> git.mxchange.org Git - simgear.git/blob - simgear/canvas/layout/Layout.hxx
canvas::Layout: add clear method to remove all items.
[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       /**
44        * Get the number of items.
45        */
46       virtual size_t count() const = 0;
47
48       /**
49        * Get the item at position @a index.
50        *
51        * If there is no such item the function must do nothing and return an
52        * empty reference.
53        */
54       virtual LayoutItemRef itemAt(size_t index) = 0;
55
56       /**
57        * Remove and get the item at position @a index.
58        *
59        * If there is no such item the function must do nothing and return an
60        * empty reference.
61        */
62       virtual LayoutItemRef takeAt(size_t index) = 0;
63
64       /**
65        * Remove the given @a item from the layout.
66        */
67       void removeItem(const LayoutItemRef& item);
68
69       /**
70        * Remove all items.
71        */
72       virtual void clear();
73
74     protected:
75       enum LayoutFlags
76       {
77         LAYOUT_DIRTY = LayoutItem::LAST_FLAG << 1,
78         LAST_FLAG = LAYOUT_DIRTY
79       };
80
81       struct ItemData
82       {
83         LayoutItemRef layout_item;
84         int     size_hint,
85                 min_size,
86                 max_size,
87                 padding_orig, //<! original padding as specified by the user
88                 padding,      //<! padding before element (layouted)
89                 size,         //<! layouted size
90                 stretch;      //<! stretch factor
91         bool    has_hfw : 1,  //<! height for width
92                 done : 1;     //<! layouting done
93
94         /** Clear values (reset to default/empty state) */
95         void reset();
96
97         int hfw(int w) const;
98         int mhfw(int w) const;
99       };
100
101       /**
102        * Override to implement the actual layouting
103        */
104       virtual void doLayout(const SGRecti& geom) = 0;
105
106       /**
107        * Add two integers taking care of overflow (limit to INT_MAX)
108        */
109       static void safeAdd(int& a, int b);
110
111       /**
112        * Distribute the available @a space to all @a items
113        */
114       void distribute(std::vector<ItemData>& items, const ItemData& space);
115
116     private:
117
118       int _num_not_done, //<! number of children not layouted yet
119           _sum_stretch,  //<! sum of stretch factors of all not yet layouted
120                          //   children
121           _space_stretch,//<! space currently assigned to all not yet layouted
122                          //   stretchable children
123           _space_left;   //<! remaining space not used by any child yet
124
125   };
126
127   typedef SGSharedPtr<Layout> LayoutRef;
128
129 } // namespace canvas
130 } // namespace simgear
131
132
133 #endif /* SG_CANVAS_LAYOUT_HXX_ */