]> git.mxchange.org Git - simgear.git/blob - simgear/canvas/layout/BoxLayout.hxx
Canvas/Layout: tweak the way elements are exposed to Nasal.
[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       /**
49        * Set the stretch factor of the item at position @a index to @a stretch.
50        */
51       void setStretch(size_t index, int stretch);
52
53       /**
54        * Get the stretch factor of the item at position @a index
55        */
56       int stretch(size_t index) const;
57
58       virtual void setSpacing(int spacing);
59       virtual int spacing() const;
60
61       void setDirection(Direction dir);
62       Direction direction() const;
63
64       virtual void setCanvas(const CanvasWeakPtr& canvas);
65
66     protected:
67
68       typedef const int& (SGVec2i::*CoordGetter)() const;
69       CoordGetter _get_layout_coord,    //<! getter for coordinate in layout
70                                         //   direction
71                   _get_fixed_coord;     //<! getter for coordinate in secondary
72                                         //   (fixed) direction
73
74       int _padding;
75       bool _reverse; //<! if true, right-to-left/bottom-to-top layouting
76
77       mutable std::vector<ItemData> _layout_items;
78       mutable ItemData _layout_data;
79
80       void updateSizeHints() const;
81
82       virtual SGVec2i sizeHintImpl() const;
83       virtual SGVec2i minimumSizeImpl() const;
84       virtual SGVec2i maximumSizeImpl() const;
85
86       virtual void doLayout(const SGRecti& geom);
87   };
88
89   /**
90    * Shortcut for creating a horizontal box layout
91    */
92   class HBoxLayout:
93     public BoxLayout
94   {
95     public:
96       HBoxLayout();
97   };
98
99   /**
100    * Shortcut for creating a vertical box layout
101    */
102   class VBoxLayout:
103     public BoxLayout
104   {
105     public:
106       VBoxLayout();
107   };
108
109 } // namespace canvas
110 } // namespace simgear
111
112
113 #endif /* SG_CANVAS_BOX_LAYOUT_HXX_ */
114