]> git.mxchange.org Git - simgear.git/blob - simgear/canvas/layout/LayoutItem.hxx
canvas::NasalWidget: check for empty setGeometry callback.
[simgear.git] / simgear / canvas / layout / LayoutItem.hxx
1 ///@file Basic element for layouting canvas elements
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_ITEM_HXX_
20 #define SG_CANVAS_LAYOUT_ITEM_HXX_
21
22 #include <simgear/canvas/canvas_fwd.hxx>
23 #include <simgear/math/SGMath.hxx>
24 #include <simgear/math/SGRect.hxx>
25 #include <simgear/misc/stdint.hxx>
26 #include <simgear/structure/SGWeakReferenced.hxx>
27 #include <simgear/structure/SGSharedPtr.hxx>
28
29 namespace simgear
30 {
31 namespace canvas
32 {
33   class LayoutItem;
34   typedef SGSharedPtr<LayoutItem> LayoutItemRef;
35   typedef SGWeakPtr<LayoutItem> LayoutItemWeakRef;
36
37   /**
38    * Base class for all layouting elements. Specializations either implement a
39    * layouting algorithm or a widget.
40    */
41   class LayoutItem:
42     public virtual SGVirtualWeakReferenced
43   {
44     public:
45
46       /** Maximum item size (indicating no limit) */
47       static const SGVec2i MAX_SIZE;
48
49       LayoutItem();
50       virtual ~LayoutItem();
51
52       /**
53        * Get the preferred size of this item.
54        */
55       SGVec2i sizeHint() const;
56
57       /**
58        * Get the minimum amount of the space this item requires.
59        */
60       SGVec2i minimumSize() const;
61
62       /**
63        * Get the maximum amount of space this item can use.
64        */
65       SGVec2i maximumSize() const;
66
67       virtual bool hasHeightForWidth() const;
68       virtual int heightForWidth(int w) const;
69       virtual int minimumHeightForWidth(int w) const;
70
71       /**
72        * Mark all cached data as invalid and require it to be recalculated.
73        */
74       virtual void invalidate();
75
76       /**
77        * Mark all cached data of parent item as invalid (if it is known)
78        */
79       void invalidateParent();
80
81       /**
82        * Set position and size of this element. For layouts this triggers a
83        * recalculation of the layout.
84        */
85       virtual void setGeometry(const SGRecti& geom);
86
87       /**
88        * Get position and size of this element.
89        */
90       virtual SGRecti geometry() const;
91
92       /**
93        * Set the canvas this item is attached to.
94        */
95       virtual void setCanvas(const CanvasWeakPtr& canvas);
96
97       /**
98        * Get the canvas this item is attached to.
99        */
100       CanvasPtr getCanvas() const;
101
102       /**
103        * Set the parent layout item (usually this is a layout).
104        */
105       void setParent(const LayoutItemWeakRef& parent);
106
107       /**
108        * Get the parent layout.
109        */
110       LayoutItemRef getParent() const;
111
112       /// Called before item is removed from a layout
113       virtual void onRemove() {}
114
115     protected:
116
117       friend class Canvas;
118
119       enum Flags
120       {
121         SIZE_HINT_DIRTY = 1,
122         MINIMUM_SIZE_DIRTY = SIZE_HINT_DIRTY << 1,
123         MAXIMUM_SIZE_DIRTY = MINIMUM_SIZE_DIRTY << 1,
124         SIZE_INFO_DIRTY = SIZE_HINT_DIRTY
125                         | MINIMUM_SIZE_DIRTY
126                         | MAXIMUM_SIZE_DIRTY,
127         LAST_FLAG = MAXIMUM_SIZE_DIRTY
128       };
129
130       CanvasWeakPtr     _canvas;
131       LayoutItemWeakRef _parent;
132
133       SGRecti           _geometry;
134
135       mutable uint32_t  _flags;
136       mutable SGVec2i   _size_hint,
137                         _min_size,
138                         _max_size;
139
140       virtual SGVec2i sizeHintImpl() const;
141       virtual SGVec2i minimumSizeImpl() const;
142       virtual SGVec2i maximumSizeImpl() const;
143
144   };
145
146 } // namespace canvas
147 } // namespace simgear
148
149 #endif /* SG_CANVAS_LAYOUT_ITEM_HXX_ */