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/NasalContext.hxx>
23 #include <simgear/nasal/cppbind/Ghost.hxx>
30 //----------------------------------------------------------------------------
31 NasalWidget::NasalWidget(naRef impl):
33 _layout_size_hint(32, 32),
34 _layout_min_size(16, 16),
35 _layout_max_size(MAX_SIZE),
36 _user_size_hint(0, 0),
38 _user_max_size(MAX_SIZE)
43 //----------------------------------------------------------------------------
44 NasalWidget::~NasalWidget()
49 //----------------------------------------------------------------------------
50 void NasalWidget::invalidate()
52 LayoutItem::invalidate();
53 _flags |= LAYOUT_DIRTY;
56 //----------------------------------------------------------------------------
57 void NasalWidget::setGeometry(const SGRect<int>& geom)
59 if( _geometry != geom )
62 _flags |= LAYOUT_DIRTY;
65 if( !_set_geometry || !(_flags & LAYOUT_DIRTY) )
71 _set_geometry(nasal::to_nasal(c, this), geom);
72 _flags &= ~LAYOUT_DIRTY;
74 catch( std::exception const& ex )
79 "NasalWidget::setGeometry: callback error: '" << ex.what() << "'"
84 //----------------------------------------------------------------------------
85 void NasalWidget::onRemove()
87 if( !_nasal_impl.valid() )
90 typedef boost::function<void(nasal::Me)> Deleter;
92 naContext c = naNewContext();
96 nasal::get_member<Deleter>(c, _nasal_impl.get_naRef(), "onRemove");
98 del(nasal::to_nasal(c, this));
100 catch( std::exception const& ex )
105 "NasalWidget::onRemove: callback error: '" << ex.what() << "'"
111 //----------------------------------------------------------------------------
112 void NasalWidget::setSetGeometryFunc(const SetGeometryFunc& func)
114 _set_geometry = func;
117 //----------------------------------------------------------------------------
118 void NasalWidget::setHeightForWidthFunc(const HeightForWidthFunc& func)
120 _height_for_width = func;
124 //----------------------------------------------------------------------------
125 void NasalWidget::setMinimumHeightForWidthFunc(const HeightForWidthFunc& func)
127 _min_height_for_width = func;
131 //----------------------------------------------------------------------------
132 void NasalWidget::setSizeHint(const SGVec2i& s)
134 if( _user_size_hint == s )
139 // TODO just invalidate size_hint? Probably not a performance issue...
143 //----------------------------------------------------------------------------
144 void NasalWidget::setMinimumSize(const SGVec2i& s)
146 if( _user_min_size == s )
153 //----------------------------------------------------------------------------
154 void NasalWidget::setMaximumSize(const SGVec2i& s)
156 if( _user_max_size == s )
163 //----------------------------------------------------------------------------
164 void NasalWidget::setLayoutSizeHint(const SGVec2i& s)
166 if( _layout_size_hint == s )
169 _layout_size_hint = s;
173 //----------------------------------------------------------------------------
174 void NasalWidget::setLayoutMinimumSize(const SGVec2i& s)
176 if( _layout_min_size == s )
179 _layout_min_size = s;
183 //----------------------------------------------------------------------------
184 void NasalWidget::setLayoutMaximumSize(const SGVec2i& s)
186 if( _layout_max_size == s )
189 _layout_max_size = s;
193 //----------------------------------------------------------------------------
194 bool NasalWidget::hasHeightForWidth() const
196 return !_height_for_width.empty() || !_min_height_for_width.empty();
199 //----------------------------------------------------------------------------
200 int NasalWidget::heightForWidth(int w) const
202 return callHeightForWidthFunc( _height_for_width.empty()
203 ? _min_height_for_width
204 : _height_for_width, w );
207 //----------------------------------------------------------------------------
208 int NasalWidget::minimumHeightForWidth(int w) const
210 return callHeightForWidthFunc( _min_height_for_width.empty()
212 : _min_height_for_width, w );
215 //----------------------------------------------------------------------------
216 static naRef f_makeNasalWidget(const nasal::CallContext& ctx)
218 return ctx.to_nasal(NasalWidgetRef(
219 new NasalWidget( ctx.requireArg<naRef>(0) )
223 //----------------------------------------------------------------------------
224 void NasalWidget::setupGhost(nasal::Hash& ns)
226 nasal::Ghost<NasalWidgetRef>::init("canvas.Widget")
227 .bases<LayoutItemRef>()
228 .bases<nasal::ObjectRef>()
229 .method("setSetGeometryFunc", &NasalWidget::setSetGeometryFunc)
230 .method("setMinimumHeightForWidthFunc",
231 &NasalWidget::setMinimumHeightForWidthFunc)
232 .method("setHeightForWidthFunc", &NasalWidget::setHeightForWidthFunc)
233 .method("setSizeHint", &NasalWidget::setSizeHint)
234 .method("setMinimumSize", &NasalWidget::setMinimumSize)
235 .method("setMaximumSize", &NasalWidget::setMaximumSize)
236 .method("setLayoutSizeHint", &NasalWidget::setLayoutSizeHint)
237 .method("setLayoutMinimumSize", &NasalWidget::setLayoutMinimumSize)
238 .method("setLayoutMaximumSize", &NasalWidget::setLayoutMaximumSize);
240 nasal::Hash widget_hash = ns.createHash("Widget");
241 widget_hash.set("new", &f_makeNasalWidget);
244 //----------------------------------------------------------------------------
245 int NasalWidget::callHeightForWidthFunc( const HeightForWidthFunc& hfw,
251 naContext c = naNewContext();
254 return hfw(nasal::to_nasal(c, const_cast<NasalWidget*>(this)), w);
256 catch( std::exception const& ex )
261 "NasalWidget.heightForWidth: callback error: '" << ex.what() << "'"
269 //----------------------------------------------------------------------------
270 SGVec2i NasalWidget::sizeHintImpl() const
273 _user_size_hint.x() > 0 ? _user_size_hint.x() : _layout_size_hint.x(),
274 _user_size_hint.y() > 0 ? _user_size_hint.y() : _layout_size_hint.y()
278 //----------------------------------------------------------------------------
279 SGVec2i NasalWidget::minimumSizeImpl() const
282 _user_min_size.x() > 0 ? _user_min_size.x() : _layout_min_size.x(),
283 _user_min_size.y() > 0 ? _user_min_size.y() : _layout_min_size.y()
287 //----------------------------------------------------------------------------
288 SGVec2i NasalWidget::maximumSizeImpl() const
291 _user_max_size.x() < MAX_SIZE.x() ? _user_max_size.x()
292 : _layout_max_size.x(),
293 _user_max_size.y() < MAX_SIZE.y() ? _user_max_size.y()
294 : _layout_max_size.y()
298 } // namespace canvas
299 } // namespace simgear