]> git.mxchange.org Git - simgear.git/blob - simgear/canvas/layout/BoxLayout.hxx
Off-by-one error in the OSG_VERSION_LESS_THAN macro
[simgear.git] / simgear / canvas / layout / BoxLayout.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_BOX_LAYOUT_HXX_
20 #define SG_CANVAS_BOX_LAYOUT_HXX_
21
22 #include "Layout.hxx"
23
24 namespace simgear
25 {
26 namespace canvas
27 {
28
29   /**
30    * Align LayoutItems horizontally or vertically in a box.
31    *
32    * @see http://qt-project.org/doc/qt-4.8/qboxlayout.html#details
33    */
34   class BoxLayout:
35     public Layout
36   {
37     public:
38
39       enum Direction
40       {
41         LeftToRight,
42         RightToLeft,
43         TopToBottom,
44         BottomToTop
45       };
46
47       BoxLayout(Direction dir);
48       ~BoxLayout();
49
50       virtual void addItem(const LayoutItemRef& item);
51
52       void addItem( const LayoutItemRef& item,
53                     int stretch,
54                     uint8_t alignment = 0 );
55
56       void addStretch(int stretch = 0);
57
58       void addSpacing(int size);
59
60       void insertItem( int index,
61                        const LayoutItemRef& item,
62                        int stretch = 0,
63                        uint8_t alignment = 0 );
64
65       void insertStretch(int index, int stretch = 0);
66
67       void insertSpacing(int index, int size);
68
69       virtual size_t count() const;
70       virtual LayoutItemRef itemAt(size_t index);
71       virtual LayoutItemRef takeAt(size_t index);
72       virtual void clear();
73
74       /**
75        * Set the stretch factor of the item at position @a index to @a stretch.
76        */
77       void setStretch(size_t index, int stretch);
78
79       /**
80        * Set the stretch factor of the given @a item to @a stretch, if it exists
81        * in this layout.
82        *
83        * @return true, if the @a item was found in the layout
84        */
85       bool setStretchFactor(const LayoutItemRef& item, int stretch);
86
87       /**
88        * Get the stretch factor of the item at position @a index
89        */
90       int stretch(size_t index) const;
91
92       virtual void setSpacing(int spacing);
93       virtual int spacing() const;
94
95       void setDirection(Direction dir);
96       Direction direction() const;
97
98       virtual bool hasHeightForWidth() const;
99
100       virtual void setCanvas(const CanvasWeakPtr& canvas);
101
102       bool horiz() const;
103
104     protected:
105
106       typedef const int& (SGVec2i::*CoordGetter)() const;
107       CoordGetter _get_layout_coord,    //!< getter for coordinate in layout
108                                         //   direction
109                   _get_fixed_coord;     //!< getter for coordinate in secondary
110                                         //   (fixed) direction
111
112       int _padding;
113       Direction _direction;
114
115       typedef std::vector<ItemData> LayoutItems;
116
117       mutable LayoutItems _layout_items;
118       mutable ItemData _layout_data;
119
120       // Cache for last height-for-width query
121       mutable int _hfw_width,
122                   _hfw_height,
123                   _hfw_min_height;
124
125       void updateSizeHints() const;
126       void updateWFHCache(int w) const;
127
128       virtual SGVec2i sizeHintImpl() const;
129       virtual SGVec2i minimumSizeImpl() const;
130       virtual SGVec2i maximumSizeImpl() const;
131
132       virtual int heightForWidthImpl(int w) const;
133       virtual int minimumHeightForWidthImpl(int w) const;
134
135       virtual void doLayout(const SGRecti& geom);
136
137       virtual void visibilityChanged(bool visible);
138   };
139
140   /**
141    * Shortcut for creating a horizontal box layout
142    */
143   class HBoxLayout:
144     public BoxLayout
145   {
146     public:
147       HBoxLayout();
148   };
149
150   /**
151    * Shortcut for creating a vertical box layout
152    */
153   class VBoxLayout:
154     public BoxLayout
155   {
156     public:
157       VBoxLayout();
158   };
159
160   typedef SGSharedPtr<BoxLayout> BoxLayoutRef;
161
162 } // namespace canvas
163 } // namespace simgear
164
165
166 #endif /* SG_CANVAS_BOX_LAYOUT_HXX_ */
167