]> git.mxchange.org Git - simgear.git/blob - simgear/canvas/layout/LayoutItem.cxx
canvas::Layout: support for hiding items.
[simgear.git] / simgear / canvas / layout / LayoutItem.cxx
1 // 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 #include "LayoutItem.hxx"
20 #include <simgear/canvas/Canvas.hxx>
21
22 namespace simgear
23 {
24 namespace canvas
25 {
26   const SGVec2i LayoutItem::MAX_SIZE( SGLimits<int>::max(),
27                                       SGLimits<int>::max() );
28
29   //----------------------------------------------------------------------------
30   LayoutItem::LayoutItem():
31     _flags(VISIBLE),
32     _size_hint(0, 0),
33     _min_size(0, 0),
34     _max_size(MAX_SIZE)
35   {
36     invalidate();
37   }
38
39   //----------------------------------------------------------------------------
40   LayoutItem::~LayoutItem()
41   {
42
43   }
44
45   //----------------------------------------------------------------------------
46   SGVec2i LayoutItem::sizeHint() const
47   {
48     if( _flags & SIZE_HINT_DIRTY )
49     {
50       _size_hint = sizeHintImpl();
51       _flags &= ~SIZE_HINT_DIRTY;
52     }
53
54     return _size_hint;
55   }
56
57   //----------------------------------------------------------------------------
58   SGVec2i LayoutItem::minimumSize() const
59   {
60     if( _flags & MINIMUM_SIZE_DIRTY )
61     {
62       _min_size = minimumSizeImpl();
63       _flags &= ~MINIMUM_SIZE_DIRTY;
64     }
65
66     return _min_size;
67   }
68
69   //----------------------------------------------------------------------------
70   SGVec2i LayoutItem::maximumSize() const
71   {
72     if( _flags & MAXIMUM_SIZE_DIRTY )
73     {
74       _max_size = maximumSizeImpl();
75       _flags &= ~MAXIMUM_SIZE_DIRTY;
76     }
77
78     return _max_size;
79   }
80
81   //----------------------------------------------------------------------------
82   bool LayoutItem::hasHeightForWidth() const
83   {
84     return false;
85   }
86
87   //----------------------------------------------------------------------------
88   int LayoutItem::heightForWidth(int w) const
89   {
90     return -1;
91   }
92
93   //------------------------------------------------------------------------------
94   int LayoutItem::minimumHeightForWidth(int w) const
95   {
96     return heightForWidth(w);
97   }
98
99   //----------------------------------------------------------------------------
100   void LayoutItem::setVisible(bool visible)
101   {
102     if( visible )
103       _flags &= ~EXPLICITLY_HIDDEN;
104     else
105       _flags |= EXPLICITLY_HIDDEN;
106
107     setVisibleInternal(visible);
108   }
109
110   //----------------------------------------------------------------------------
111   bool LayoutItem::isVisible() const
112   {
113     return _flags & VISIBLE;
114   }
115
116   //----------------------------------------------------------------------------
117   bool LayoutItem::isExplicitlyHidden() const
118   {
119     return _flags & EXPLICITLY_HIDDEN;
120   }
121
122   //----------------------------------------------------------------------------
123   void LayoutItem::invalidate()
124   {
125     _flags |= SIZE_INFO_DIRTY;
126     invalidateParent();
127   }
128
129   //----------------------------------------------------------------------------
130   void LayoutItem::invalidateParent()
131   {
132     LayoutItemRef parent = _parent.lock();
133     if( parent )
134       parent->invalidate();
135   }
136
137   //----------------------------------------------------------------------------
138   void LayoutItem::setGeometry(const SGRecti& geom)
139   {
140     _geometry = geom;
141   }
142
143   //----------------------------------------------------------------------------
144   SGRecti LayoutItem::geometry() const
145   {
146     return _geometry;
147   }
148
149   //----------------------------------------------------------------------------
150   void LayoutItem::setCanvas(const CanvasWeakPtr& canvas)
151   {
152     _canvas = canvas;
153   }
154
155   //----------------------------------------------------------------------------
156   CanvasPtr LayoutItem::getCanvas() const
157   {
158     return _canvas.lock();
159   }
160
161   //----------------------------------------------------------------------------
162   void LayoutItem::setParent(const LayoutItemWeakRef& parent)
163   {
164     _parent = parent;
165     LayoutItemRef parent_ref = parent.lock();
166     setCanvas(parent_ref ? parent_ref->_canvas : CanvasWeakPtr());
167
168     setVisibleInternal(!parent_ref || parent_ref->isVisible());
169   }
170
171   //----------------------------------------------------------------------------
172   LayoutItemRef LayoutItem::getParent() const
173   {
174     return _parent.lock();
175   }
176
177   //----------------------------------------------------------------------------
178   SGVec2i LayoutItem::sizeHintImpl() const
179   {
180     return _size_hint;
181   }
182
183   //----------------------------------------------------------------------------
184   SGVec2i LayoutItem::minimumSizeImpl() const
185   {
186     return _min_size;
187   }
188
189   //----------------------------------------------------------------------------
190   SGVec2i LayoutItem::maximumSizeImpl() const
191   {
192     return _max_size;
193   }
194
195   //----------------------------------------------------------------------------
196   void LayoutItem::setVisibleInternal(bool visible)
197   {
198     LayoutItemRef parent = getParent();
199     if( isExplicitlyHidden() || (parent && !parent->isVisible()) )
200       visible = false;
201
202     if( isVisible() == visible )
203       return;
204
205     invalidateParent();
206
207     if( visible )
208       _flags |= VISIBLE;
209     else
210       _flags &= ~VISIBLE;
211
212     visibilityChanged(visible);
213   }
214
215   //----------------------------------------------------------------------------
216   void LayoutItem::callSetVisibleInternal(LayoutItem* item, bool visible)
217   {
218     item->setVisibleInternal(visible);
219   }
220
221 } // namespace canvas
222 } // namespace simgear