2 /// Basic element for layouting canvas elements.
4 // Copyright (C) 2014 Thomas Geymayer <tomgey@gmail.com>
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Library General Public
8 // License as published by the Free Software Foundation; either
9 // version 2 of the License, or (at your option) any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Library General Public License for more details.
16 // You should have received a copy of the GNU Library General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #ifndef SG_CANVAS_LAYOUT_ITEM_HXX_
21 #define SG_CANVAS_LAYOUT_ITEM_HXX_
23 #include <simgear/canvas/canvas_fwd.hxx>
24 #include <simgear/math/SGMath.hxx>
25 #include <simgear/math/SGRect.hxx>
26 #include <simgear/misc/stdint.hxx>
27 #include <simgear/structure/SGWeakReferenced.hxx>
28 #include <simgear/structure/SGSharedPtr.hxx>
35 typedef SGSharedPtr<LayoutItem> LayoutItemRef;
36 typedef SGWeakPtr<LayoutItem> LayoutItemWeakRef;
39 * Holds the four margins for a rectangle.
46 * Set all margins to the same value @a m.
48 explicit Margins(int m = 0);
51 * Set horizontal and vertical margins to the same values @a h and @a v
54 * @param h Horizontal margins
55 * @param v Vertical margins
57 Margins(int h, int v);
60 * Set the margins to the given values.
62 Margins(int left, int top, int right, int bottom);
65 * Get the total horizontal margin (sum of left and right margin).
70 * Get the total vertical margin (sum of top and bottom margin).
75 * Get total horizontal and vertical margin as vector.
80 * Returns true if all margins are 0.
86 * Flags for LayoutItem alignment inside Layouts.
88 * @note You can only use one horizontal and one vertical flag at the same.
92 AlignFill = 0, //!< Use all available space
94 AlignLeft = 0x01, //!< Align with left edge
95 AlignRight = 0x02, //!< Align with right edge
96 AlignHCenter = 0x04, //!< Center horizontally in available space
98 AlignTop = 0x20, //!< Align with top edge
99 AlignBottom = 0x40, //!< Align with bottom edge
100 AlignVCenter = 0x80, //!< Center vertically in available space
102 AlignCenter = AlignVCenter //!< Center both vertically and horizontally
105 AlignHorizontal_Mask = AlignLeft
108 AlignVertical_Mask = AlignTop
114 * Base class for all layouting elements. Specializations either implement a
115 * layouting algorithm or a widget.
118 public virtual SGVirtualWeakReferenced
122 /** Maximum item size (indicating no limit) */
123 static const SGVec2i MAX_SIZE;
126 virtual ~LayoutItem();
129 * Set the margins to use by the layout system around the item.
131 * The margins define the size of the clear area around an item. It
132 * increases the size hints and reduces the size of the geometry()
133 * available to child layouts and widgets.
137 void setContentsMargins(const Margins& margins);
140 * Set the individual margins.
142 * @see setContentsMargins(const Margins&)
144 void setContentsMargins(int left, int top, int right, int bottom);
147 * Set all margins to the same value.
149 * @see setContentsMargins(const Margins&)
151 void setContentsMargin(int margin);
154 * Get the currently used margins.
156 * @see setContentsMargins(const Margins&)
159 Margins getContentsMargins() const;
162 * Get the area available to the contents.
164 * This is equal to the geometry() reduced by the sizes of the margins.
166 * @see setContentsMargins(const Margins&)
169 SGRecti contentsRect() const;
172 * Get the preferred size of this item.
174 SGVec2i sizeHint() const;
177 * Get the minimum amount of the space this item requires.
179 SGVec2i minimumSize() const;
182 * Get the maximum amount of space this item can use.
184 SGVec2i maximumSize() const;
187 * Returns true if this items preferred and minimum height depend on its
190 * The default implementation returns false. Reimplement for items
191 * providing height for width.
193 * @see heightForWidth()
194 * @see minimumHeightForWidth()
196 virtual bool hasHeightForWidth() const;
199 * Returns the preferred height for the given width @a w.
201 * Reimplement heightForWidthImpl() for items providing height for width.
203 * @see hasHeightForWidth()
205 int heightForWidth(int w) const;
208 * Returns the minimum height for the given width @a w.
210 * Reimplement minimumHeightForWidthImpl() for items providing height for
213 * @see hasHeightForWidth()
215 int minimumHeightForWidth(int w) const;
218 * Set alignment of item within @link{Layout Layouts}.
220 * @param alignment Bitwise combination of vertical and horizontal
224 void setAlignment(uint8_t alignment);
227 * Get all alignment flags.
231 uint8_t alignment() const;
233 virtual void setVisible(bool visible);
234 virtual bool isVisible() const;
236 bool isExplicitlyHidden() const;
238 void show() { setVisible(true); }
239 void hide() { setVisible(false); }
242 * Mark all cached data as invalid and require it to be recalculated.
244 virtual void invalidate();
247 * Mark all cached data of parent item as invalid (if the parent is set).
249 void invalidateParent();
252 * Apply any changes not applied yet.
257 * Set position and size of this element. For layouts this triggers a
258 * recalculation of the layout.
260 virtual void setGeometry(const SGRecti& geom);
263 * Get position and size of this element.
265 virtual SGRecti geometry() const;
268 * Get the actual geometry of this item given the rectangle \a geom
269 * taking into account the alignment flags and size hints.
271 * @param geom Area available to this item.
272 * @return The resulting geometry for this item.
274 * @see setAlignment()
279 virtual SGRecti alignmentRect(const SGRecti& geom) const;
282 * Set the canvas this item is attached to.
284 virtual void setCanvas(const CanvasWeakPtr& canvas);
287 * Get the canvas this item is attached to.
289 CanvasPtr getCanvas() const;
292 * Set the parent layout item (usually this is a layout).
294 void setParent(const LayoutItemWeakRef& parent);
297 * Get the parent layout.
299 LayoutItemRef getParent() const;
301 /// Called before item is removed from a layout
302 virtual void onRemove() {}
311 MINIMUM_SIZE_DIRTY = SIZE_HINT_DIRTY << 1,
312 MAXIMUM_SIZE_DIRTY = MINIMUM_SIZE_DIRTY << 1,
313 SIZE_INFO_DIRTY = SIZE_HINT_DIRTY
315 | MAXIMUM_SIZE_DIRTY,
316 EXPLICITLY_HIDDEN = MAXIMUM_SIZE_DIRTY << 1,
317 VISIBLE = EXPLICITLY_HIDDEN << 1,
318 LAYOUT_DIRTY = VISIBLE << 1,
319 LAST_FLAG = LAYOUT_DIRTY
322 CanvasWeakPtr _canvas;
323 LayoutItemWeakRef _parent;
329 mutable uint32_t _flags;
330 mutable SGVec2i _size_hint,
334 virtual SGVec2i sizeHintImpl() const;
335 virtual SGVec2i minimumSizeImpl() const;
336 virtual SGVec2i maximumSizeImpl() const;
339 * Returns the preferred height for the given width @a w.
341 * The default implementation returns -1, indicating that the preferred
342 * height is independent of the given width.
344 * Reimplement this function for items supporting height for width.
346 * @note Do not take margins into account, as this is already handled
347 * before calling this function.
349 * @see hasHeightForWidth()
351 virtual int heightForWidthImpl(int w) const;
354 * Returns the minimum height for the given width @a w.
356 * The default implementation returns -1, indicating that the minimum
357 * height is independent of the given width.
359 * Reimplement this function for items supporting height for width.
361 * @note Do not take margins into account, as this is already handled
362 * before calling this function.
364 * @see hasHeightForWidth()
366 virtual int minimumHeightForWidthImpl(int w) const;
369 * @return whether the visibility has changed.
371 void setVisibleInternal(bool visible);
373 virtual void contentsRectChanged(const SGRecti& rect) {};
375 virtual void visibilityChanged(bool visible) {}
378 * Allow calling the protected setVisibleImpl from derived classes
380 static void callSetVisibleInternal(LayoutItem* item, bool visible);
384 } // namespace canvas
385 } // namespace simgear
387 #endif /* SG_CANVAS_LAYOUT_ITEM_HXX_ */