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()
89 callMethod<void>("onRemove");
91 catch( std::exception const& ex )
96 "NasalWidget::onRemove: callback error: '" << ex.what() << "'"
101 //----------------------------------------------------------------------------
102 void NasalWidget::setSetGeometryFunc(const SetGeometryFunc& func)
104 _set_geometry = func;
107 //----------------------------------------------------------------------------
108 void NasalWidget::setHeightForWidthFunc(const HeightForWidthFunc& func)
110 _height_for_width = func;
114 //----------------------------------------------------------------------------
115 void NasalWidget::setMinimumHeightForWidthFunc(const HeightForWidthFunc& func)
117 _min_height_for_width = func;
121 //----------------------------------------------------------------------------
122 void NasalWidget::setSizeHint(const SGVec2i& s)
124 if( _user_size_hint == s )
129 // TODO just invalidate size_hint? Probably not a performance issue...
133 //----------------------------------------------------------------------------
134 void NasalWidget::setMinimumSize(const SGVec2i& s)
136 if( _user_min_size == s )
143 //----------------------------------------------------------------------------
144 void NasalWidget::setMaximumSize(const SGVec2i& s)
146 if( _user_max_size == s )
153 //----------------------------------------------------------------------------
154 void NasalWidget::setLayoutSizeHint(const SGVec2i& s)
156 if( _layout_size_hint == s )
159 _layout_size_hint = s;
163 //----------------------------------------------------------------------------
164 void NasalWidget::setLayoutMinimumSize(const SGVec2i& s)
166 if( _layout_min_size == s )
169 _layout_min_size = s;
173 //----------------------------------------------------------------------------
174 void NasalWidget::setLayoutMaximumSize(const SGVec2i& s)
176 if( _layout_max_size == s )
179 _layout_max_size = s;
183 //----------------------------------------------------------------------------
184 bool NasalWidget::hasHeightForWidth() const
186 return !_height_for_width.empty() || !_min_height_for_width.empty();
189 //----------------------------------------------------------------------------
190 int NasalWidget::heightForWidth(int w) const
192 return callHeightForWidthFunc( _height_for_width.empty()
193 ? _min_height_for_width
194 : _height_for_width, w );
197 //----------------------------------------------------------------------------
198 int NasalWidget::minimumHeightForWidth(int w) const
200 return callHeightForWidthFunc( _min_height_for_width.empty()
202 : _min_height_for_width, w );
205 //----------------------------------------------------------------------------
206 static naRef f_makeNasalWidget(const nasal::CallContext& ctx)
208 return ctx.to_nasal(NasalWidgetRef(
209 new NasalWidget( ctx.requireArg<naRef>(0) )
213 //----------------------------------------------------------------------------
214 void NasalWidget::setupGhost(nasal::Hash& ns)
216 nasal::Ghost<NasalWidgetRef>::init("canvas.Widget")
217 .bases<LayoutItemRef>()
218 .bases<nasal::ObjectRef>()
219 .method("setSetGeometryFunc", &NasalWidget::setSetGeometryFunc)
220 .method("setMinimumHeightForWidthFunc",
221 &NasalWidget::setMinimumHeightForWidthFunc)
222 .method("setHeightForWidthFunc", &NasalWidget::setHeightForWidthFunc)
223 .method("setSizeHint", &NasalWidget::setSizeHint)
224 .method("setMinimumSize", &NasalWidget::setMinimumSize)
225 .method("setMaximumSize", &NasalWidget::setMaximumSize)
226 .method("setLayoutSizeHint", &NasalWidget::setLayoutSizeHint)
227 .method("setLayoutMinimumSize", &NasalWidget::setLayoutMinimumSize)
228 .method("setLayoutMaximumSize", &NasalWidget::setLayoutMaximumSize);
230 nasal::Hash widget_hash = ns.createHash("Widget");
231 widget_hash.set("new", &f_makeNasalWidget);
234 //----------------------------------------------------------------------------
235 int NasalWidget::callHeightForWidthFunc( const HeightForWidthFunc& hfw,
241 naContext c = naNewContext();
244 return hfw(nasal::to_nasal(c, const_cast<NasalWidget*>(this)), w);
246 catch( std::exception const& ex )
251 "NasalWidget.heightForWidth: callback error: '" << ex.what() << "'"
259 //----------------------------------------------------------------------------
260 SGVec2i NasalWidget::sizeHintImpl() const
263 _user_size_hint.x() > 0 ? _user_size_hint.x() : _layout_size_hint.x(),
264 _user_size_hint.y() > 0 ? _user_size_hint.y() : _layout_size_hint.y()
268 //----------------------------------------------------------------------------
269 SGVec2i NasalWidget::minimumSizeImpl() const
272 _user_min_size.x() > 0 ? _user_min_size.x() : _layout_min_size.x(),
273 _user_min_size.y() > 0 ? _user_min_size.y() : _layout_min_size.y()
277 //----------------------------------------------------------------------------
278 SGVec2i NasalWidget::maximumSizeImpl() const
281 _user_max_size.x() < MAX_SIZE.x() ? _user_max_size.x()
282 : _layout_max_size.x(),
283 _user_max_size.y() < MAX_SIZE.y() ? _user_max_size.y()
284 : _layout_max_size.y()
288 //----------------------------------------------------------------------------
289 void NasalWidget::visibilityChanged(bool visible)
293 callMethod<void>("visibilityChanged", visible);
295 catch( std::exception const& ex )
300 "NasalWidget::visibilityChanged: callback error: '" << ex.what() << "'"
305 } // namespace canvas
306 } // namespace simgear