]> git.mxchange.org Git - simgear.git/blob - simgear/canvas/layout/NasalWidget.cxx
canvas::BoxLayout: add custom additional whitespace (spacer/stretch)
[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     Object(impl)
32   {
33
34   }
35
36   //----------------------------------------------------------------------------
37   void NasalWidget::setGeometry(const SGRect<int>& geom)
38   {
39     if( _geometry == geom )
40       return;
41     _geometry = geom;
42
43     if( !_set_geometry )
44       return;
45
46     naContext c = naNewContext();
47     try
48     {
49       _set_geometry(nasal::to_nasal(c, this), geom);
50     }
51     catch( std::exception const& ex )
52     {
53       SG_LOG(
54         SG_GUI,
55         SG_WARN,
56         "NasalWidget::setGeometry: callback error: '" << ex.what() << "'"
57       );
58     }
59     naFreeContext(c);
60   }
61
62   //----------------------------------------------------------------------------
63   void NasalWidget::setSetGeometryFunc(const SetGeometryFunc& func)
64   {
65     _set_geometry = func;
66   }
67
68   //----------------------------------------------------------------------------
69   void NasalWidget::setSizeHint(const SGVec2i& s)
70   {
71     if( _size_hint == s )
72       return;
73
74     _size_hint = s;
75
76     // TODO just invalidate size_hint? Probably not a performance issue...
77     invalidateParent();
78   }
79
80   //----------------------------------------------------------------------------
81   void NasalWidget::setMinimumSize(const SGVec2i& s)
82   {
83     if( _min_size == s )
84       return;
85
86     _min_size = s;
87     invalidateParent();
88   }
89
90   //----------------------------------------------------------------------------
91   void NasalWidget::setMaximumSize(const SGVec2i& s)
92   {
93     if( _max_size == s )
94       return;
95
96     _max_size = s;
97     invalidateParent();
98   }
99
100   //----------------------------------------------------------------------------
101   static naRef f_makeNasalWidget(const nasal::CallContext& ctx)
102   {
103     return ctx.to_nasal(NasalWidgetRef(
104       new NasalWidget( ctx.requireArg<naRef>(0) )
105     ));
106   }
107
108   //----------------------------------------------------------------------------
109   void NasalWidget::setupGhost(nasal::Hash& ns)
110   {
111     nasal::Ghost<NasalWidgetRef>::init("canvas.Widget")
112       .bases<LayoutItemRef>()
113       .bases<nasal::ObjectRef>()
114       .method("setSetGeometryFunc", &NasalWidget::setSetGeometryFunc)
115       .method("setSizeHint", &NasalWidget::setSizeHint)
116       .method("setMinimumSize", &NasalWidget::setMinimumSize)
117       .method("setMaximumSize", &NasalWidget::setMaximumSize);
118
119     nasal::Hash widget_hash = ns.createHash("Widget");
120     widget_hash.set("new", &f_makeNasalWidget);
121   }
122
123   //----------------------------------------------------------------------------
124   SGVec2i NasalWidget::sizeHintImpl() const
125   {
126     return _size_hint;
127   }
128
129   //----------------------------------------------------------------------------
130   SGVec2i NasalWidget::minimumSizeImpl() const
131   {
132     return _min_size;
133   }
134
135   //----------------------------------------------------------------------------
136   SGVec2i NasalWidget::maximumSizeImpl() const
137   {
138     return _max_size;
139   }
140
141 } // namespace canvas
142 } // namespace simgear