]> git.mxchange.org Git - simgear.git/blob - simgear/canvas/layout/LayoutItem.hxx
Doxygen: disable (not working) latex output and update version.
[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    * Holds the four margins for a rectangle.
39    */
40   struct Margins
41   {
42     int l, t, r, b;
43
44     /**
45      * Set all margins to the same value @a m.
46      */
47     explicit Margins(int m = 0);
48
49     /**
50      * Set horizontal and vertical margins to the same values @a h and @a v
51      * respectively.
52      *
53      * @param h Horizontal margins
54      * @param v Vertical margins
55      */
56     Margins(int h, int v);
57
58     /**
59      * Set the margins to the given values.
60      */
61     Margins(int left, int top, int right, int bottom);
62
63     /**
64      * Get the total horizontal margin (sum of left and right margin).
65      */
66     int horiz() const;
67
68     /**
69      * Get the total vertical margin (sum of top and bottom margin).
70      */
71     int vert() const;
72
73     /**
74      * Get total horizontal and vertical margin as vector.
75      */
76     SGVec2i size() const;
77
78     /**
79      * Returns true if all margins are 0.
80      */
81     bool isNull() const;
82   };
83
84   /**
85    * Base class for all layouting elements. Specializations either implement a
86    * layouting algorithm or a widget.
87    */
88   class LayoutItem:
89     public virtual SGVirtualWeakReferenced
90   {
91     public:
92
93       /** Maximum item size (indicating no limit) */
94       static const SGVec2i MAX_SIZE;
95
96       LayoutItem();
97       virtual ~LayoutItem();
98
99       /**
100        * Set the margins to use by the layout system around the item.
101        *
102        * The margins define the size of the clear area around an item. It
103        * increases the size hints and reduces the size of the geometry()
104        * available to child layouts and widgets.
105        *
106        * @see Margins
107        */
108       void setContentsMargins(const Margins& margins);
109
110       /**
111        * Set the individual margins.
112        *
113        * @see setContentsMargins(const Margins&)
114        */
115       void setContentsMargins(int left, int top, int right, int bottom);
116
117       /**
118        * Set all margins to the same value.
119        *
120        * @see setContentsMargins(const Margins&)
121        */
122       void setContentsMargin(int margin);
123
124       /**
125        * Get the currently used margins.
126        *
127        * @see setContentsMargins(const Margins&)
128        * @see Margins
129        */
130       Margins getContentsMargins() const;
131
132       /**
133        * Get the area available to the contents.
134        *
135        * This is equal to the geometry() reduced by the sizes of the margins.
136        *
137        * @see setContentsMargins(const Margins&)
138        * @see geometry()
139        */
140       SGRecti contentsRect() const;
141
142       /**
143        * Get the preferred size of this item.
144        */
145       SGVec2i sizeHint() const;
146
147       /**
148        * Get the minimum amount of the space this item requires.
149        */
150       SGVec2i minimumSize() const;
151
152       /**
153        * Get the maximum amount of space this item can use.
154        */
155       SGVec2i maximumSize() const;
156
157       /**
158        * Returns true if this items preferred and minimum height depend on its
159        * width.
160        *
161        * The default implementation returns false. Reimplement for items
162        * providing height for width.
163        *
164        * @see heightForWidth()
165        * @see minimumHeightForWidth()
166        */
167       virtual bool hasHeightForWidth() const;
168
169       /**
170        * Returns the preferred height for the given width @a w.
171        *
172        * Reimplement heightForWidthImpl() for items providing height for width.
173        *
174        * @see hasHeightForWidth()
175        */
176       int heightForWidth(int w) const;
177
178       /**
179        * Returns the minimum height for the given width @a w.
180        *
181        * Reimplement minimumHeightForWidthImpl() for items providing height for
182        * width.
183        *
184        * @see hasHeightForWidth()
185        */
186       int minimumHeightForWidth(int w) const;
187
188       virtual void setVisible(bool visible);
189       virtual bool isVisible() const;
190
191       bool isExplicitlyHidden() const;
192
193       void show() { setVisible(true); }
194       void hide() { setVisible(false); }
195
196       /**
197        * Mark all cached data as invalid and require it to be recalculated.
198        */
199       virtual void invalidate();
200
201       /**
202        * Mark all cached data of parent item as invalid (if the parent is set).
203        */
204       void invalidateParent();
205
206       /**
207        * Apply any changes not applied yet.
208        */
209       void update();
210
211       /**
212        * Set position and size of this element. For layouts this triggers a
213        * recalculation of the layout.
214        */
215       virtual void setGeometry(const SGRecti& geom);
216
217       /**
218        * Get position and size of this element.
219        */
220       virtual SGRecti geometry() const;
221
222       /**
223        * Set the canvas this item is attached to.
224        */
225       virtual void setCanvas(const CanvasWeakPtr& canvas);
226
227       /**
228        * Get the canvas this item is attached to.
229        */
230       CanvasPtr getCanvas() const;
231
232       /**
233        * Set the parent layout item (usually this is a layout).
234        */
235       void setParent(const LayoutItemWeakRef& parent);
236
237       /**
238        * Get the parent layout.
239        */
240       LayoutItemRef getParent() const;
241
242       /// Called before item is removed from a layout
243       virtual void onRemove() {}
244
245     protected:
246
247       friend class Canvas;
248
249       enum Flags
250       {
251         SIZE_HINT_DIRTY = 1,
252         MINIMUM_SIZE_DIRTY = SIZE_HINT_DIRTY << 1,
253         MAXIMUM_SIZE_DIRTY = MINIMUM_SIZE_DIRTY << 1,
254         SIZE_INFO_DIRTY = SIZE_HINT_DIRTY
255                         | MINIMUM_SIZE_DIRTY
256                         | MAXIMUM_SIZE_DIRTY,
257         EXPLICITLY_HIDDEN = MAXIMUM_SIZE_DIRTY << 1,
258         VISIBLE = EXPLICITLY_HIDDEN << 1,
259         LAYOUT_DIRTY = VISIBLE << 1,
260         LAST_FLAG = LAYOUT_DIRTY
261       };
262
263       CanvasWeakPtr     _canvas;
264       LayoutItemWeakRef _parent;
265
266       SGRecti           _geometry;
267       Margins           _margins;
268
269       mutable uint32_t  _flags;
270       mutable SGVec2i   _size_hint,
271                         _min_size,
272                         _max_size;
273
274       virtual SGVec2i sizeHintImpl() const;
275       virtual SGVec2i minimumSizeImpl() const;
276       virtual SGVec2i maximumSizeImpl() const;
277
278       /**
279        * Returns the preferred height for the given width @a w.
280        *
281        * The default implementation returns -1, indicating that the preferred
282        * height is independent of the given width.
283        *
284        * Reimplement this function for items supporting height for width.
285        *
286        * @note Do not take margins into account, as this is already handled
287        *       before calling this function.
288        *
289        * @see hasHeightForWidth()
290        */
291       virtual int heightForWidthImpl(int w) const;
292
293       /**
294        * Returns the minimum height for the given width @a w.
295        *
296        * The default implementation returns -1, indicating that the minimum
297        * height is independent of the given width.
298        *
299        * Reimplement this function for items supporting height for width.
300        *
301        * @note Do not take margins into account, as this is already handled
302        *       before calling this function.
303        *
304        * @see hasHeightForWidth()
305        */
306       virtual int minimumHeightForWidthImpl(int w) const;
307
308       /**
309        * @return whether the visibility has changed.
310        */
311       void setVisibleInternal(bool visible);
312
313       virtual void contentsRectChanged(const SGRecti& rect) {};
314
315       virtual void visibilityChanged(bool visible) {}
316
317       /**
318        * Allow calling the protected setVisibleImpl from derived classes
319        */
320       static void callSetVisibleInternal(LayoutItem* item, bool visible);
321
322   };
323
324 } // namespace canvas
325 } // namespace simgear
326
327 #endif /* SG_CANVAS_LAYOUT_ITEM_HXX_ */