]> git.mxchange.org Git - simgear.git/blob - simgear/canvas/layout/BoxLayout.hxx
canvas::BoxLayout: add custom additional whitespace (spacer/stretch)
[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
44       virtual void addItem(const LayoutItemRef& item);
45
46       void addItem(const LayoutItemRef& item, int stretch);
47
48       void addStretch(int stretch = 0);
49
50       void addSpacing(int size);
51
52       void insertItem(int index, const LayoutItemRef& item, int stretch = 0);
53
54       void insertStretch(int index, int stretch = 0);
55
56       void insertSpacing(int index, int size);
57
58       /**
59        * Set the stretch factor of the item at position @a index to @a stretch.
60        */
61       void setStretch(size_t index, int stretch);
62
63       /**
64        * Get the stretch factor of the item at position @a index
65        */
66       int stretch(size_t index) const;
67
68       virtual void setSpacing(int spacing);
69       virtual int spacing() const;
70
71       void setDirection(Direction dir);
72       Direction direction() const;
73
74       virtual void setCanvas(const CanvasWeakPtr& canvas);
75
76       bool horiz() const;
77
78     protected:
79
80       typedef const int& (SGVec2i::*CoordGetter)() const;
81       CoordGetter _get_layout_coord,    //<! getter for coordinate in layout
82                                         //   direction
83                   _get_fixed_coord;     //<! getter for coordinate in secondary
84                                         //   (fixed) direction
85
86       int _padding;
87       Direction _direction;
88
89       mutable std::vector<ItemData> _layout_items;
90       mutable ItemData _layout_data;
91
92       void updateSizeHints() const;
93
94       virtual SGVec2i sizeHintImpl() const;
95       virtual SGVec2i minimumSizeImpl() const;
96       virtual SGVec2i maximumSizeImpl() const;
97
98       virtual void doLayout(const SGRecti& geom);
99   };
100
101   /**
102    * Shortcut for creating a horizontal box layout
103    */
104   class HBoxLayout:
105     public BoxLayout
106   {
107     public:
108       HBoxLayout();
109   };
110
111   /**
112    * Shortcut for creating a vertical box layout
113    */
114   class VBoxLayout:
115     public BoxLayout
116   {
117     public:
118       VBoxLayout();
119   };
120
121   typedef SGSharedPtr<BoxLayout> BoxLayoutRef;
122
123 } // namespace canvas
124 } // namespace simgear
125
126
127 #endif /* SG_CANVAS_BOX_LAYOUT_HXX_ */
128