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