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