1 // Glue for GUI layout items implemented in Nasal space
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 #include "NasalWidget.hxx"
21 #include <simgear/canvas/Canvas.hxx>
22 #include <simgear/nasal/cppbind/Ghost.hxx>
29 //----------------------------------------------------------------------------
30 NasalWidget::NasalWidget(naRef impl):
36 //----------------------------------------------------------------------------
37 void NasalWidget::invalidate()
39 LayoutItem::invalidate();
40 _flags |= LAYOUT_DIRTY;
43 //----------------------------------------------------------------------------
44 void NasalWidget::setGeometry(const SGRect<int>& geom)
46 if( _geometry != geom )
48 else if( !(_flags & LAYOUT_DIRTY) || !_set_geometry )
51 naContext c = naNewContext();
54 _set_geometry(nasal::to_nasal(c, this), geom);
55 _flags &= ~LAYOUT_DIRTY;
57 catch( std::exception const& ex )
62 "NasalWidget::setGeometry: callback error: '" << ex.what() << "'"
68 //----------------------------------------------------------------------------
69 void NasalWidget::onRemove()
71 if( !_nasal_impl.valid() )
74 typedef boost::function<void(nasal::Me)> Deleter;
76 naContext c = naNewContext();
80 nasal::get_member<Deleter>(c, _nasal_impl.get_naRef(), "onRemove");
82 del(_nasal_impl.get_naRef());
84 catch( std::exception const& ex )
89 "NasalWidget::onRemove: callback error: '" << ex.what() << "'"
95 //----------------------------------------------------------------------------
96 void NasalWidget::setSetGeometryFunc(const SetGeometryFunc& func)
101 //----------------------------------------------------------------------------
102 void NasalWidget::setHeightForWidthFunc(const HeightForWidthFunc& func)
104 _height_for_width = func;
108 //----------------------------------------------------------------------------
109 void NasalWidget::setMinimumHeightForWidthFunc(const HeightForWidthFunc& func)
111 _min_height_for_width = func;
115 //----------------------------------------------------------------------------
116 void NasalWidget::setSizeHint(const SGVec2i& s)
118 if( _size_hint == s )
123 // TODO just invalidate size_hint? Probably not a performance issue...
127 //----------------------------------------------------------------------------
128 void NasalWidget::setMinimumSize(const SGVec2i& s)
137 //----------------------------------------------------------------------------
138 void NasalWidget::setMaximumSize(const SGVec2i& s)
147 //----------------------------------------------------------------------------
148 bool NasalWidget::hasHeightForWidth() const
150 return !_height_for_width.empty() || !_min_height_for_width.empty();
153 //----------------------------------------------------------------------------
154 int NasalWidget::heightForWidth(int w) const
156 return callHeightForWidthFunc( _height_for_width.empty()
157 ? _min_height_for_width
158 : _height_for_width, w );
161 //----------------------------------------------------------------------------
162 int NasalWidget::minimumHeightForWidth(int w) const
164 return callHeightForWidthFunc( _min_height_for_width.empty()
166 : _min_height_for_width, w );
169 //----------------------------------------------------------------------------
170 static naRef f_makeNasalWidget(const nasal::CallContext& ctx)
172 return ctx.to_nasal(NasalWidgetRef(
173 new NasalWidget( ctx.requireArg<naRef>(0) )
177 //----------------------------------------------------------------------------
178 void NasalWidget::setupGhost(nasal::Hash& ns)
180 nasal::Ghost<NasalWidgetRef>::init("canvas.Widget")
181 .bases<LayoutItemRef>()
182 .bases<nasal::ObjectRef>()
183 .method("setSetGeometryFunc", &NasalWidget::setSetGeometryFunc)
184 .method("setMinimumHeightForWidthFunc",
185 &NasalWidget::setMinimumHeightForWidthFunc)
186 .method("setHeightForWidthFunc", &NasalWidget::setHeightForWidthFunc)
187 .method("setSizeHint", &NasalWidget::setSizeHint)
188 .method("setMinimumSize", &NasalWidget::setMinimumSize)
189 .method("setMaximumSize", &NasalWidget::setMaximumSize);
191 nasal::Hash widget_hash = ns.createHash("Widget");
192 widget_hash.set("new", &f_makeNasalWidget);
195 //----------------------------------------------------------------------------
196 int NasalWidget::callHeightForWidthFunc( const HeightForWidthFunc& hfw,
202 naContext c = naNewContext();
205 return hfw(nasal::to_nasal(c, const_cast<NasalWidget*>(this)), w);
207 catch( std::exception const& ex )
212 "NasalWidget.heightForWidth: callback error: '" << ex.what() << "'"
220 //----------------------------------------------------------------------------
221 SGVec2i NasalWidget::sizeHintImpl() const
226 //----------------------------------------------------------------------------
227 SGVec2i NasalWidget::minimumSizeImpl() const
232 //----------------------------------------------------------------------------
233 SGVec2i NasalWidget::maximumSizeImpl() const
238 } // namespace canvas
239 } // namespace simgear