]> git.mxchange.org Git - simgear.git/commitdiff
canvas::BoxLayout: set stretch factor by item.
authorThomas Geymayer <tomgey@gmail.com>
Wed, 16 Jul 2014 17:24:41 +0000 (19:24 +0200)
committerThomas Geymayer <tomgey@gmail.com>
Wed, 16 Jul 2014 17:24:41 +0000 (19:24 +0200)
simgear/canvas/layout/BoxLayout.cxx
simgear/canvas/layout/BoxLayout.hxx
simgear/canvas/layout/canvas_layout_test.cxx

index e0155c229a408f86c1e27776208027bb92efb2d8..122fe97fbe428a704e5c925a96838b7a08974bd4 100644 (file)
@@ -152,6 +152,24 @@ namespace canvas
     invalidate();
   }
 
+  //----------------------------------------------------------------------------
+  bool BoxLayout::setStretchFactor(const LayoutItemRef& item, int stretch)
+  {
+    for( LayoutItems::iterator it = _layout_items.begin();
+                               it != _layout_items.end();
+                             ++it )
+    {
+      if( item == it->layout_item )
+      {
+        it->stretch = std::max(0, stretch);
+        invalidate();
+        return true;
+      }
+    }
+
+    return false;
+  }
+
   //----------------------------------------------------------------------------
   int BoxLayout::stretch(size_t index) const
   {
index 8982aeafa8501891308ae08baa0f35111bf43b28..59988a722a2b17813ee64ff7659027271a17bc8b 100644 (file)
@@ -66,6 +66,14 @@ namespace canvas
        */
       void setStretch(size_t index, int stretch);
 
+      /**
+       * Set the stretch factor of the given @a item to @a stretch, if it exists
+       * in this layout.
+       *
+       * @return true, if the @a item was found in the layout
+       */
+      bool setStretchFactor(const LayoutItemRef& item, int stretch);
+
       /**
        * Get the stretch factor of the item at position @a index
        */
index 0b59daca71256133a781efd446d0fb70b5f889fb..6aa9c79a67af42b8f42c5bbb0dd713f1d4fe92a6 100644 (file)
@@ -216,6 +216,15 @@ BOOST_AUTO_TEST_CASE( horizontal_layout )
 
     BOOST_CHECK_EQUAL(w1->geometry(), SGRecti(0,   0, 125, 32));
     BOOST_CHECK_EQUAL(w2->geometry(), SGRecti(130, 0, 126, 32));
+
+    BOOST_REQUIRE( hbox.setStretchFactor(w1, 2) );
+    BOOST_REQUIRE( hbox.setStretchFactor(w2, 3) );
+    BOOST_CHECK_EQUAL(hbox.stretch(0), 2);
+    BOOST_CHECK_EQUAL(hbox.stretch(1), 3);
+
+    hbox.removeItem(w1);
+
+    BOOST_CHECK( !hbox.setStretchFactor(w1, 0) );
   }
 }