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::setGeometry(const SGRect<int>& geom)
39 if( _geometry == geom )
46 naContext c = naNewContext();
49 _set_geometry(nasal::to_nasal(c, this), geom);
51 catch( std::exception const& ex )
56 "NasalWidget::setGeometry: callback error: '" << ex.what() << "'"
62 //----------------------------------------------------------------------------
63 void NasalWidget::setSetGeometryFunc(const SetGeometryFunc& func)
68 //----------------------------------------------------------------------------
69 void NasalWidget::setHeightForWidthFunc(const HeightForWidthFunc& func)
71 _height_for_width = func;
75 //----------------------------------------------------------------------------
76 void NasalWidget::setMinimumHeightForWidthFunc(const HeightForWidthFunc& func)
78 _min_height_for_width = func;
82 //----------------------------------------------------------------------------
83 void NasalWidget::setSizeHint(const SGVec2i& s)
90 // TODO just invalidate size_hint? Probably not a performance issue...
94 //----------------------------------------------------------------------------
95 void NasalWidget::setMinimumSize(const SGVec2i& s)
104 //----------------------------------------------------------------------------
105 void NasalWidget::setMaximumSize(const SGVec2i& s)
114 //----------------------------------------------------------------------------
115 bool NasalWidget::hasHeightForWidth() const
117 return !_height_for_width.empty() || !_min_height_for_width.empty();
120 //----------------------------------------------------------------------------
121 int NasalWidget::heightForWidth(int w) const
123 return callHeightForWidthFunc( _height_for_width.empty()
124 ? _min_height_for_width
125 : _height_for_width, w );
128 //----------------------------------------------------------------------------
129 int NasalWidget::minimumHeightForWidth(int w) const
131 return callHeightForWidthFunc( _min_height_for_width.empty()
133 : _min_height_for_width, w );
136 //----------------------------------------------------------------------------
137 static naRef f_makeNasalWidget(const nasal::CallContext& ctx)
139 return ctx.to_nasal(NasalWidgetRef(
140 new NasalWidget( ctx.requireArg<naRef>(0) )
144 //----------------------------------------------------------------------------
145 void NasalWidget::setupGhost(nasal::Hash& ns)
147 nasal::Ghost<NasalWidgetRef>::init("canvas.Widget")
148 .bases<LayoutItemRef>()
149 .bases<nasal::ObjectRef>()
150 .method("setSetGeometryFunc", &NasalWidget::setSetGeometryFunc)
151 .method("setMinimumHeightForWidthFunc",
152 &NasalWidget::setMinimumHeightForWidthFunc)
153 .method("setHeightForWidthFunc", &NasalWidget::setHeightForWidthFunc)
154 .method("setSizeHint", &NasalWidget::setSizeHint)
155 .method("setMinimumSize", &NasalWidget::setMinimumSize)
156 .method("setMaximumSize", &NasalWidget::setMaximumSize);
158 nasal::Hash widget_hash = ns.createHash("Widget");
159 widget_hash.set("new", &f_makeNasalWidget);
162 //----------------------------------------------------------------------------
163 int NasalWidget::callHeightForWidthFunc( const HeightForWidthFunc& hfw,
169 naContext c = naNewContext();
172 return hfw(nasal::to_nasal(c, const_cast<NasalWidget*>(this)), w);
174 catch( std::exception const& ex )
179 "NasalWidget.heightForWidth: callback error: '" << ex.what() << "'"
187 //----------------------------------------------------------------------------
188 SGVec2i NasalWidget::sizeHintImpl() const
193 //----------------------------------------------------------------------------
194 SGVec2i NasalWidget::minimumSizeImpl() const
199 //----------------------------------------------------------------------------
200 SGVec2i NasalWidget::maximumSizeImpl() const
205 } // namespace canvas
206 } // namespace simgear