]> 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 62e0b78f6fe137aa7b65814d3cca32000803f7b3..d512b7fe96d7657db052816531e723b93e819e5b 100644 (file)
@@ -65,6 +65,20 @@ namespace canvas
     _set_geometry = func;
   }
 
+  //----------------------------------------------------------------------------
+  void NasalWidget::setHeightForWidthFunc(const HeightForWidthFunc& func)
+  {
+    _height_for_width = func;
+    invalidateParent();
+  }
+
+  //----------------------------------------------------------------------------
+  void NasalWidget::setMinimumHeightForWidthFunc(const HeightForWidthFunc& func)
+  {
+    _min_height_for_width = func;
+    invalidateParent();
+  }
+
   //----------------------------------------------------------------------------
   void NasalWidget::setSizeHint(const SGVec2i& s)
   {
@@ -97,6 +111,28 @@ namespace canvas
     invalidateParent();
   }
 
+  //----------------------------------------------------------------------------
+  bool NasalWidget::hasHeightForWidth() const
+  {
+    return !_height_for_width.empty() || !_min_height_for_width.empty();
+  }
+
+  //----------------------------------------------------------------------------
+  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 );
+  }
+
   //----------------------------------------------------------------------------
   static naRef f_makeNasalWidget(const nasal::CallContext& ctx)
   {
@@ -112,6 +148,9 @@ namespace canvas
       .bases<LayoutItemRef>()
       .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);
@@ -120,6 +159,31 @@ namespace canvas
     widget_hash.set("new", &f_makeNasalWidget);
   }
 
+  //----------------------------------------------------------------------------
+  int NasalWidget::callHeightForWidthFunc( const HeightForWidthFunc& hfw,
+                                           int w ) const
+  {
+    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;
+  }
+
   //----------------------------------------------------------------------------
   SGVec2i NasalWidget::sizeHintImpl() const
   {