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 * Base class for all layouting elements. Specializations either implement a
87 * layouting algorithm or a widget.
90 public virtual SGVirtualWeakReferenced
94 /** Maximum item size (indicating no limit) */
95 static const SGVec2i MAX_SIZE;
98 virtual ~LayoutItem();
101 * Set the margins to use by the layout system around the item.
103 * The margins define the size of the clear area around an item. It
104 * increases the size hints and reduces the size of the geometry()
105 * available to child layouts and widgets.
109 void setContentsMargins(const Margins& margins);
112 * Set the individual margins.
114 * @see setContentsMargins(const Margins&)
116 void setContentsMargins(int left, int top, int right, int bottom);
119 * Set all margins to the same value.
121 * @see setContentsMargins(const Margins&)
123 void setContentsMargin(int margin);
126 * Get the currently used margins.
128 * @see setContentsMargins(const Margins&)
131 Margins getContentsMargins() const;
134 * Get the area available to the contents.
136 * This is equal to the geometry() reduced by the sizes of the margins.
138 * @see setContentsMargins(const Margins&)
141 SGRecti contentsRect() const;
144 * Get the preferred size of this item.
146 SGVec2i sizeHint() const;
149 * Get the minimum amount of the space this item requires.
151 SGVec2i minimumSize() const;
154 * Get the maximum amount of space this item can use.
156 SGVec2i maximumSize() const;
159 * Returns true if this items preferred and minimum height depend on its
162 * The default implementation returns false. Reimplement for items
163 * providing height for width.
165 * @see heightForWidth()
166 * @see minimumHeightForWidth()
168 virtual bool hasHeightForWidth() const;
171 * Returns the preferred height for the given width @a w.
173 * Reimplement heightForWidthImpl() for items providing height for width.
175 * @see hasHeightForWidth()
177 int heightForWidth(int w) const;
180 * Returns the minimum height for the given width @a w.
182 * Reimplement minimumHeightForWidthImpl() for items providing height for
185 * @see hasHeightForWidth()
187 int minimumHeightForWidth(int w) const;
189 virtual void setVisible(bool visible);
190 virtual bool isVisible() const;
192 bool isExplicitlyHidden() const;
194 void show() { setVisible(true); }
195 void hide() { setVisible(false); }
198 * Mark all cached data as invalid and require it to be recalculated.
200 virtual void invalidate();
203 * Mark all cached data of parent item as invalid (if the parent is set).
205 void invalidateParent();
208 * Apply any changes not applied yet.
213 * Set position and size of this element. For layouts this triggers a
214 * recalculation of the layout.
216 virtual void setGeometry(const SGRecti& geom);
219 * Get position and size of this element.
221 virtual SGRecti geometry() const;
224 * Set the canvas this item is attached to.
226 virtual void setCanvas(const CanvasWeakPtr& canvas);
229 * Get the canvas this item is attached to.
231 CanvasPtr getCanvas() const;
234 * Set the parent layout item (usually this is a layout).
236 void setParent(const LayoutItemWeakRef& parent);
239 * Get the parent layout.
241 LayoutItemRef getParent() const;
243 /// Called before item is removed from a layout
244 virtual void onRemove() {}
253 MINIMUM_SIZE_DIRTY = SIZE_HINT_DIRTY << 1,
254 MAXIMUM_SIZE_DIRTY = MINIMUM_SIZE_DIRTY << 1,
255 SIZE_INFO_DIRTY = SIZE_HINT_DIRTY
257 | MAXIMUM_SIZE_DIRTY,
258 EXPLICITLY_HIDDEN = MAXIMUM_SIZE_DIRTY << 1,
259 VISIBLE = EXPLICITLY_HIDDEN << 1,
260 LAYOUT_DIRTY = VISIBLE << 1,
261 LAST_FLAG = LAYOUT_DIRTY
264 CanvasWeakPtr _canvas;
265 LayoutItemWeakRef _parent;
270 mutable uint32_t _flags;
271 mutable SGVec2i _size_hint,
275 virtual SGVec2i sizeHintImpl() const;
276 virtual SGVec2i minimumSizeImpl() const;
277 virtual SGVec2i maximumSizeImpl() const;
280 * Returns the preferred height for the given width @a w.
282 * The default implementation returns -1, indicating that the preferred
283 * height is independent of the given width.
285 * Reimplement this function for items supporting height for width.
287 * @note Do not take margins into account, as this is already handled
288 * before calling this function.
290 * @see hasHeightForWidth()
292 virtual int heightForWidthImpl(int w) const;
295 * Returns the minimum height for the given width @a w.
297 * The default implementation returns -1, indicating that the minimum
298 * height is independent of the given width.
300 * Reimplement this function for items supporting height for width.
302 * @note Do not take margins into account, as this is already handled
303 * before calling this function.
305 * @see hasHeightForWidth()
307 virtual int minimumHeightForWidthImpl(int w) const;
310 * @return whether the visibility has changed.
312 void setVisibleInternal(bool visible);
314 virtual void contentsRectChanged(const SGRecti& rect) {};
316 virtual void visibilityChanged(bool visible) {}
319 * Allow calling the protected setVisibleImpl from derived classes
321 static void callSetVisibleInternal(LayoutItem* item, bool visible);
325 } // namespace canvas
326 } // namespace simgear
328 #endif /* SG_CANVAS_LAYOUT_ITEM_HXX_ */