]> git.mxchange.org Git - simgear.git/blob - simgear/canvas/layout/LayoutItem.cxx
Canvas: basic layouting system.
[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
27   //----------------------------------------------------------------------------
28   LayoutItem::LayoutItem():
29     _flags(0)
30   {
31     invalidate();
32   }
33
34   //----------------------------------------------------------------------------
35   LayoutItem::~LayoutItem()
36   {
37
38   }
39
40   //----------------------------------------------------------------------------
41   SGVec2i LayoutItem::sizeHint() const
42   {
43     if( _flags & SIZE_HINT_DIRTY )
44     {
45       _size_hint = sizeHintImpl();
46       _flags &= ~SIZE_HINT_DIRTY;
47     }
48
49     return _size_hint;
50   }
51
52   //----------------------------------------------------------------------------
53   SGVec2i LayoutItem::minimumSize() const
54   {
55     if( _flags & MINIMUM_SIZE_DIRTY )
56     {
57       _min_size = minimumSizeImpl();
58       _flags &= ~MINIMUM_SIZE_DIRTY;
59     }
60
61     return _min_size;
62   }
63
64   //----------------------------------------------------------------------------
65   SGVec2i LayoutItem::maximumSize() const
66   {
67     if( _flags & MAXIMUM_SIZE_DIRTY )
68     {
69       _max_size = maximumSizeImpl();
70       _flags &= ~MAXIMUM_SIZE_DIRTY;
71     }
72
73     return _max_size;
74   }
75
76   //----------------------------------------------------------------------------
77   void LayoutItem::invalidate()
78   {
79     _flags |= SIZE_HINT_DIRTY
80            |  MINIMUM_SIZE_DIRTY
81            |  MAXIMUM_SIZE_DIRTY;
82
83     invalidateParent();
84   }
85
86   //----------------------------------------------------------------------------
87   void LayoutItem::invalidateParent()
88   {
89     LayoutItemRef parent = _parent.lock();
90     if( parent )
91       parent->invalidate();
92   }
93
94   //----------------------------------------------------------------------------
95   void LayoutItem::setGeometry(const SGRecti& geom)
96   {
97     _geometry = geom;
98   }
99
100   //----------------------------------------------------------------------------
101   SGRecti LayoutItem::geometry() const
102   {
103     return _geometry;
104   }
105
106   //----------------------------------------------------------------------------
107   void LayoutItem::setCanvas(const CanvasWeakPtr& canvas)
108   {
109     _canvas = canvas;
110   }
111
112   //----------------------------------------------------------------------------
113   CanvasPtr LayoutItem::getCanvas() const
114   {
115     return _canvas.lock();
116   }
117
118   //----------------------------------------------------------------------------
119   void LayoutItem::setParent(const LayoutItemWeakRef& parent)
120   {
121     _parent = parent;
122     LayoutItemRef parent_ref = parent.lock();
123     setCanvas(parent_ref ? parent_ref->_canvas : CanvasWeakPtr());
124   }
125
126   //----------------------------------------------------------------------------
127   LayoutItemRef LayoutItem::getParent() const
128   {
129     return _parent.lock();
130   }
131
132   //----------------------------------------------------------------------------
133   SGVec2i LayoutItem::sizeHintImpl() const
134   {
135     return SGVec2i(16, 16);
136   }
137
138   //----------------------------------------------------------------------------
139   SGVec2i LayoutItem::minimumSizeImpl() const
140   {
141     return SGVec2i(0, 0);
142   }
143
144   //----------------------------------------------------------------------------
145   SGVec2i LayoutItem::maximumSizeImpl() const
146   {
147     return SGVec2i(SGLimits<int>::max(), SGLimits<int>::max());
148   }
149
150 } // namespace canvas
151 } // namespace simgear