1 // Basic class for canvas layouts
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_HXX_
20 #define SG_CANVAS_LAYOUT_HXX_
22 #include "LayoutItem.hxx"
36 virtual void invalidate();
37 virtual void setGeometry(const SGRecti& geom);
39 virtual void addItem(const LayoutItemRef& item) = 0;
40 virtual void setSpacing(int spacing) = 0;
41 virtual int spacing() const = 0;
44 * Get the number of items.
46 virtual size_t count() const = 0;
49 * Get the item at position @a index.
51 * If there is no such item the function must do nothing and return an
54 virtual LayoutItemRef itemAt(size_t index) = 0;
57 * Remove and get the item at position @a index.
59 * If there is no such item the function must do nothing and return an
62 virtual LayoutItemRef takeAt(size_t index) = 0;
65 * Remove the given @a item from the layout.
67 void removeItem(const LayoutItemRef& item);
77 LAYOUT_DIRTY = LayoutItem::LAST_FLAG << 1,
78 LAST_FLAG = LAYOUT_DIRTY
83 LayoutItemRef layout_item;
87 padding_orig, //<! original padding as specified by the user
88 padding, //<! padding before element (layouted)
89 size, //<! layouted size
90 stretch; //<! stretch factor
91 bool has_hfw : 1, //<! height for width
92 done : 1; //<! layouting done
94 /** Clear values (reset to default/empty state) */
98 int mhfw(int w) const;
102 * Override to implement the actual layouting
104 virtual void doLayout(const SGRecti& geom) = 0;
107 * Add two integers taking care of overflow (limit to INT_MAX)
109 static void safeAdd(int& a, int b);
112 * Distribute the available @a space to all @a items
114 void distribute(std::vector<ItemData>& items, const ItemData& space);
118 int _num_not_done, //<! number of children not layouted yet
119 _sum_stretch, //<! sum of stretch factors of all not yet layouted
121 _space_stretch,//<! space currently assigned to all not yet layouted
122 // stretchable children
123 _space_left; //<! remaining space not used by any child yet
127 typedef SGSharedPtr<Layout> LayoutRef;
129 } // namespace canvas
130 } // namespace simgear
133 #endif /* SG_CANVAS_LAYOUT_HXX_ */