1 ///@file Basic element for layouting canvas elements
3 // Copyright (C) 2014 Thomas Geymayer <tomgey@gmail.com>
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.
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.
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
19 #ifndef SG_CANVAS_LAYOUT_ITEM_HXX_
20 #define SG_CANVAS_LAYOUT_ITEM_HXX_
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>
34 typedef SGSharedPtr<LayoutItem> LayoutItemRef;
35 typedef SGWeakPtr<LayoutItem> LayoutItemWeakRef;
38 * Holds the four margins for a rectangle.
45 * Set all margins to the same value @a m.
47 explicit Margins(int m = 0);
50 * Set horizontal and vertical margins to the same values @a h and @a v
53 * @param h Horizontal margins
54 * @param v Vertical margins
56 Margins(int h, int v);
59 * Set the margins to the given values.
61 Margins(int left, int top, int right, int bottom);
64 * Get the total horizontal margin (sum of left and right margin).
69 * Get the total vertical margin (sum of top and bottom margin).
74 * Get total horizontal and vertical margin as vector.
79 * Returns true if all margins are 0.
85 * Base class for all layouting elements. Specializations either implement a
86 * layouting algorithm or a widget.
89 public virtual SGVirtualWeakReferenced
93 /** Maximum item size (indicating no limit) */
94 static const SGVec2i MAX_SIZE;
97 virtual ~LayoutItem();
100 * Set the margins to use by the layout system around the item.
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.
108 void setContentsMargins(const Margins& margins);
111 * Set the individual margins.
113 * @see setContentsMargins(const Margins&)
115 void setContentsMargins(int left, int top, int right, int bottom);
118 * Set all margins to the same value.
120 * @see setContentsMargins(const Margins&)
122 void setContentsMargin(int margin);
125 * Get the currently used margins.
127 * @see setContentsMargins(const Margins&)
130 Margins getContentsMargins() const;
133 * Get the area available to the contents.
135 * This is equal to the geometry() reduced by the sizes of the margins.
137 * @see setContentsMargins(const Margins&)
140 SGRecti contentsRect() const;
143 * Get the preferred size of this item.
145 SGVec2i sizeHint() const;
148 * Get the minimum amount of the space this item requires.
150 SGVec2i minimumSize() const;
153 * Get the maximum amount of space this item can use.
155 SGVec2i maximumSize() const;
158 * Returns true if this items preferred and minimum height depend on its
161 * The default implementation returns false. Reimplement for items
162 * providing height for width.
164 * @see heightForWidth()
165 * @see minimumHeightForWidth()
167 virtual bool hasHeightForWidth() const;
170 * Returns the preferred height for the given width @q w.
172 * Reimplement heightForWidthImpl() for items providing height for width.
174 * @see hasHeightForWidth()
176 int heightForWidth(int w) const;
179 * Returns the minimum height for the given width @q w.
181 * Reimplement minimumHeightForWidthImpl() for items providing height for
184 * @see hasHeightForWidth()
186 int minimumHeightForWidth(int w) const;
188 virtual void setVisible(bool visible);
189 virtual bool isVisible() const;
191 bool isExplicitlyHidden() const;
193 void show() { setVisible(true); }
194 void hide() { setVisible(false); }
197 * Mark all cached data as invalid and require it to be recalculated.
199 virtual void invalidate();
202 * Mark all cached data of parent item as invalid (if the parent is set).
204 void invalidateParent();
207 * Apply any changes not applied yet.
212 * Set position and size of this element. For layouts this triggers a
213 * recalculation of the layout.
215 virtual void setGeometry(const SGRecti& geom);
218 * Get position and size of this element.
220 virtual SGRecti geometry() const;
223 * Set the canvas this item is attached to.
225 virtual void setCanvas(const CanvasWeakPtr& canvas);
228 * Get the canvas this item is attached to.
230 CanvasPtr getCanvas() const;
233 * Set the parent layout item (usually this is a layout).
235 void setParent(const LayoutItemWeakRef& parent);
238 * Get the parent layout.
240 LayoutItemRef getParent() const;
242 /// Called before item is removed from a layout
243 virtual void onRemove() {}
252 MINIMUM_SIZE_DIRTY = SIZE_HINT_DIRTY << 1,
253 MAXIMUM_SIZE_DIRTY = MINIMUM_SIZE_DIRTY << 1,
254 SIZE_INFO_DIRTY = SIZE_HINT_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
263 CanvasWeakPtr _canvas;
264 LayoutItemWeakRef _parent;
269 mutable uint32_t _flags;
270 mutable SGVec2i _size_hint,
274 virtual SGVec2i sizeHintImpl() const;
275 virtual SGVec2i minimumSizeImpl() const;
276 virtual SGVec2i maximumSizeImpl() const;
279 * Returns the preferred height for the given width @q w.
281 * The default implementation returns -1, indicating that the preferred
282 * height is independent of the given width.
284 * Reimplement this function for items supporting height for width.
286 * @note Do not take margins into account, as this is already handled
287 * before calling this function.
289 * @see hasHeightForWidth()
291 virtual int heightForWidthImpl(int w) const;
294 * Returns the minimum height for the given width @q w.
296 * The default implementation returns -1, indicating that the minimum
297 * height is independent of the given width.
299 * Reimplement this function for items supporting height for width.
301 * @note Do not take margins into account, as this is already handled
302 * before calling this function.
304 * @see hasHeightForWidth()
306 virtual int minimumHeightForWidthImpl(int w) const;
309 * @return whether the visibility has changed.
311 void setVisibleInternal(bool visible);
313 virtual void contentsRectChanged(const SGRecti& rect) {};
315 virtual void visibilityChanged(bool visible) {}
318 * Allow calling the protected setVisibleImpl from derived classes
320 static void callSetVisibleInternal(LayoutItem* item, bool visible);
324 } // namespace canvas
325 } // namespace simgear
327 #endif /* SG_CANVAS_LAYOUT_ITEM_HXX_ */