]> git.mxchange.org Git - simgear.git/blob - simgear/canvas/layout/Layout.hxx
Off-by-one error in the OSG_VERSION_LESS_THAN macro
[simgear.git] / simgear / canvas / layout / Layout.hxx
1 /// @file
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   /**
31    * Base class for all Canvas layouts.
32    */
33   class Layout:
34     public LayoutItem
35   {
36     public:
37       virtual void addItem(const LayoutItemRef& item) = 0;
38       virtual void setSpacing(int spacing) = 0;
39       virtual int spacing() const = 0;
40
41       /**
42        * Get the number of items.
43        */
44       virtual size_t count() const = 0;
45
46       /**
47        * Get the item at position @a index.
48        *
49        * If there is no such item the function must do nothing and return an
50        * empty reference.
51        */
52       virtual LayoutItemRef itemAt(size_t index) = 0;
53
54       /**
55        * Remove and get the item at position @a index.
56        *
57        * If there is no such item the function must do nothing and return an
58        * empty reference.
59        */
60       virtual LayoutItemRef takeAt(size_t index) = 0;
61
62       /**
63        * Remove the given @a item from the layout.
64        */
65       void removeItem(const LayoutItemRef& item);
66
67       /**
68        * Remove all items.
69        */
70       virtual void clear();
71
72       /**
73        * Get the actual geometry of this layout given the rectangle \a geom
74        * taking into account the alignment flags and size hints. For layouts,
75        * if no alignment (different to AlignFill) is set, the whole area is
76        * used. Excess space is distributed by the layouting algorithm and
77        * handled by the individual children.
78        *
79        * @param geom    Area available to this layout.
80        * @return The resulting geometry for this layout.
81        */
82       virtual SGRecti alignmentRect(const SGRecti& geom) const;
83
84     protected:
85       enum LayoutFlags
86       {
87         LAST_FLAG = LayoutItem::LAST_FLAG
88       };
89
90       struct ItemData
91       {
92         LayoutItemRef layout_item;
93         int     size_hint,
94                 min_size,
95                 max_size,
96                 padding_orig, //!< original padding as specified by the user
97                 padding,      //!< padding before element (layouted)
98                 size,         //!< layouted size
99                 stretch;      //!< stretch factor
100         bool    visible : 1,
101                 has_align: 1, //!< Has alignment factor set (!= AlignFill)
102                 has_hfw : 1,  //!< height for width
103                 done : 1;     //!< layouting done
104
105         /** Clear values (reset to default/empty state) */
106         void reset();
107
108         int hfw(int w) const;
109         int mhfw(int w) const;
110       };
111
112       Layout();
113
114       virtual void contentsRectChanged(const SGRecti& rect);
115
116       /**
117        * Override to implement the actual layouting
118        */
119       virtual void doLayout(const SGRecti& geom) = 0;
120
121       /**
122        * Distribute the available @a space to all @a items
123        */
124       void distribute(std::vector<ItemData>& items, const ItemData& space);
125
126     private:
127
128       int _num_not_done, //!< number of children not layouted yet
129           _sum_stretch,  //!< sum of stretch factors of all not yet layouted
130                          //   children
131           _space_stretch,//!< space currently assigned to all not yet layouted
132                          //   stretchable children
133           _space_left;   //!< remaining space not used by any child yet
134
135   };
136
137   typedef SGSharedPtr<Layout> LayoutRef;
138
139 } // namespace canvas
140 } // namespace simgear
141
142
143 #endif /* SG_CANVAS_LAYOUT_HXX_ */