]> git.mxchange.org Git - simgear.git/blob - simgear/canvas/layout/NasalWidget.cxx
Canvas: basic layouting system.
[simgear.git] / simgear / canvas / layout / NasalWidget.cxx
1 // Glue for GUI layout items implemented in Nasal space
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 "NasalWidget.hxx"
20
21 #include <simgear/canvas/Canvas.hxx>
22 #include <simgear/nasal/cppbind/Ghost.hxx>
23
24 namespace simgear
25 {
26 namespace canvas
27 {
28
29   //----------------------------------------------------------------------------
30   NasalWidget::NasalWidget(naRef impl):
31     _nasal_impl(impl)
32   {
33     assert( naIsHash(_nasal_impl.get_naRef()) );
34   }
35
36   //----------------------------------------------------------------------------
37   void NasalWidget::setGeometry(const SGRect<int>& geom)
38   {
39     if( _geometry == geom )
40       return;
41
42     _geometry = geom;
43
44     if( _set_geometry )
45       _set_geometry(_nasal_impl.get_naRef(), geom);
46   }
47
48   //----------------------------------------------------------------------------
49   void NasalWidget::setSetGeometryFunc(const SetGeometryFunc& func)
50   {
51     _set_geometry = func;
52   }
53
54   //----------------------------------------------------------------------------
55   void NasalWidget::setImpl(naRef obj)
56   {
57     _nasal_impl.reset(obj);
58   }
59
60   //----------------------------------------------------------------------------
61   naRef NasalWidget::getImpl() const
62   {
63     return _nasal_impl.get_naRef();
64   }
65
66   //----------------------------------------------------------------------------
67   void NasalWidget::setSizeHint(const SGVec2i& s)
68   {
69     if( _size_hint == s )
70       return;
71
72     _size_hint = s;
73
74     // TODO just invalidate size_hint? Probably not a performance issue...
75     invalidateParent();
76   }
77
78   //----------------------------------------------------------------------------
79   void NasalWidget::setMinimumSize(const SGVec2i& s)
80   {
81     if( _min_size == s )
82       return;
83
84     _min_size = s;
85     invalidateParent();
86   }
87
88   //----------------------------------------------------------------------------
89   void NasalWidget::setMaximumSize(const SGVec2i& s)
90   {
91     if( _max_size == s )
92       return;
93
94     _max_size = s;
95     invalidateParent();
96   }
97
98   //----------------------------------------------------------------------------
99   bool NasalWidget::_set(naContext c, const std::string& key, naRef val)
100   {
101     if( !_nasal_impl.valid() )
102       return false;
103
104     nasal::Hash(_nasal_impl.get_naRef(), c).set(key, val);
105     return true;
106   }
107
108   //----------------------------------------------------------------------------
109   static naRef f_makeNasalWidget(const nasal::CallContext& ctx)
110   {
111     return ctx.to_nasal(NasalWidgetRef(
112       new NasalWidget( ctx.requireArg<naRef>(0) )
113     ));
114   }
115
116   //----------------------------------------------------------------------------
117   void NasalWidget::setupGhost(nasal::Hash& ns)
118   {
119     nasal::Ghost<NasalWidgetRef>::init("canvas.Widget")
120       .bases<LayoutItemRef>()
121       ._set(&NasalWidget::_set)
122       .member("parents", &NasalWidget::getParents)
123       .method("setSetGeometryFunc", &NasalWidget::setSetGeometryFunc)
124       .method("setSizeHint", &NasalWidget::setSizeHint)
125       .method("setMinimumSize", &NasalWidget::setMinimumSize)
126       .method("setMaximumSize", &NasalWidget::setMaximumSize);
127
128     nasal::Hash widget_hash = ns.createHash("Widget");
129     widget_hash.set("new", &f_makeNasalWidget);
130   }
131
132   //----------------------------------------------------------------------------
133   naRef NasalWidget::getParents(NasalWidget& w, naContext c)
134   {
135     naRef parents[] = { w._nasal_impl.get_naRef() };
136     return nasal::to_nasal(c, parents);
137   }
138
139   //----------------------------------------------------------------------------
140   SGVec2i NasalWidget::sizeHintImpl() const
141   {
142     return _size_hint;
143   }
144
145   //----------------------------------------------------------------------------
146   SGVec2i NasalWidget::minimumSizeImpl() const
147   {
148     return _min_size;
149   }
150
151   //----------------------------------------------------------------------------
152   SGVec2i NasalWidget::maximumSizeImpl() const
153   {
154     return _max_size;
155   }
156
157 } // namespace canvas
158 } // namespace simgear