]> git.mxchange.org Git - simgear.git/blobdiff - simgear/canvas/layout/NasalWidget.cxx
canvas::Text: add heightForWidth method.
[simgear.git] / simgear / canvas / layout / NasalWidget.cxx
index a8532c9e62c92bce3f524d856ffb3b2457184b07..d512b7fe96d7657db052816531e723b93e819e5b 100644 (file)
@@ -28,9 +28,9 @@ namespace canvas
 
   //----------------------------------------------------------------------------
   NasalWidget::NasalWidget(naRef impl):
-    _nasal_impl(impl)
+    Object(impl)
   {
-    assert( naIsHash(_nasal_impl.get_naRef()) );
+
   }
 
   //----------------------------------------------------------------------------
@@ -38,11 +38,25 @@ namespace canvas
   {
     if( _geometry == geom )
       return;
-
     _geometry = geom;
 
-    if( _set_geometry )
-      _set_geometry(_nasal_impl.get_naRef(), geom);
+    if( !_set_geometry )
+      return;
+
+    naContext c = naNewContext();
+    try
+    {
+      _set_geometry(nasal::to_nasal(c, this), geom);
+    }
+    catch( std::exception const& ex )
+    {
+      SG_LOG(
+        SG_GUI,
+        SG_WARN,
+        "NasalWidget::setGeometry: callback error: '" << ex.what() << "'"
+      );
+    }
+    naFreeContext(c);
   }
 
   //----------------------------------------------------------------------------
@@ -52,15 +66,17 @@ namespace canvas
   }
 
   //----------------------------------------------------------------------------
-  void NasalWidget::setImpl(naRef obj)
+  void NasalWidget::setHeightForWidthFunc(const HeightForWidthFunc& func)
   {
-    _nasal_impl.reset(obj);
+    _height_for_width = func;
+    invalidateParent();
   }
 
   //----------------------------------------------------------------------------
-  naRef NasalWidget::getImpl() const
+  void NasalWidget::setMinimumHeightForWidthFunc(const HeightForWidthFunc& func)
   {
-    return _nasal_impl.get_naRef();
+    _min_height_for_width = func;
+    invalidateParent();
   }
 
   //----------------------------------------------------------------------------
@@ -96,13 +112,25 @@ namespace canvas
   }
 
   //----------------------------------------------------------------------------
-  bool NasalWidget::_set(naContext c, const std::string& key, naRef val)
+  bool NasalWidget::hasHeightForWidth() const
   {
-    if( !_nasal_impl.valid() )
-      return false;
+    return !_height_for_width.empty() || !_min_height_for_width.empty();
+  }
 
-    nasal::Hash(_nasal_impl.get_naRef(), c).set(key, val);
-    return true;
+  //----------------------------------------------------------------------------
+  int NasalWidget::heightForWidth(int w) const
+  {
+    return callHeightForWidthFunc( _height_for_width.empty()
+                                 ? _min_height_for_width
+                                 : _height_for_width, w );
+  }
+
+  //----------------------------------------------------------------------------
+  int NasalWidget::minimumHeightForWidth(int w) const
+  {
+    return callHeightForWidthFunc( _min_height_for_width.empty()
+                                 ? _height_for_width
+                                 : _min_height_for_width, w );
   }
 
   //----------------------------------------------------------------------------
@@ -118,9 +146,11 @@ namespace canvas
   {
     nasal::Ghost<NasalWidgetRef>::init("canvas.Widget")
       .bases<LayoutItemRef>()
-      ._set(&NasalWidget::_set)
-      .member("parents", &NasalWidget::getParents)
+      .bases<nasal::ObjectRef>()
       .method("setSetGeometryFunc", &NasalWidget::setSetGeometryFunc)
+      .method("setMinimumHeightForWidthFunc",
+                                    &NasalWidget::setMinimumHeightForWidthFunc)
+      .method("setHeightForWidthFunc", &NasalWidget::setHeightForWidthFunc)
       .method("setSizeHint", &NasalWidget::setSizeHint)
       .method("setMinimumSize", &NasalWidget::setMinimumSize)
       .method("setMaximumSize", &NasalWidget::setMaximumSize);
@@ -130,10 +160,28 @@ namespace canvas
   }
 
   //----------------------------------------------------------------------------
-  naRef NasalWidget::getParents(NasalWidget& w, naContext c)
+  int NasalWidget::callHeightForWidthFunc( const HeightForWidthFunc& hfw,
+                                           int w ) const
   {
-    naRef parents[] = { w._nasal_impl.get_naRef() };
-    return nasal::to_nasal(c, parents);
+    if( hfw.empty() )
+      return -1;
+
+    naContext c = naNewContext();
+    try
+    {
+      return hfw(nasal::to_nasal(c, const_cast<NasalWidget*>(this)), w);
+    }
+    catch( std::exception const& ex )
+    {
+      SG_LOG(
+        SG_GUI,
+        SG_WARN,
+        "NasalWidget.heightForWidth: callback error: '" << ex.what() << "'"
+      );
+    }
+    naFreeContext(c);
+
+    return -1;
   }
 
   //----------------------------------------------------------------------------