diff --git a/simgear/canvas/layout/BoxLayout.cxx b/simgear/canvas/layout/BoxLayout.cxx index e0155c22..122fe97f 100644 --- a/simgear/canvas/layout/BoxLayout.cxx +++ b/simgear/canvas/layout/BoxLayout.cxx @@ -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 { diff --git a/simgear/canvas/layout/BoxLayout.hxx b/simgear/canvas/layout/BoxLayout.hxx index 8982aeaf..59988a72 100644 --- a/simgear/canvas/layout/BoxLayout.hxx +++ b/simgear/canvas/layout/BoxLayout.hxx @@ -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 */ diff --git a/simgear/canvas/layout/canvas_layout_test.cxx b/simgear/canvas/layout/canvas_layout_test.cxx index 0b59daca..6aa9c79a 100644 --- a/simgear/canvas/layout/canvas_layout_test.cxx +++ b/simgear/canvas/layout/canvas_layout_test.cxx @@ -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) ); } }