From: Thomas Geymayer Date: Thu, 17 Jul 2014 13:05:13 +0000 (+0200) Subject: canvas::BoxLayout: fix parent ref on add/remove. X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=e415b08da4424309884e1e9229ff476a03522a86;p=simgear.git canvas::BoxLayout: fix parent ref on add/remove. --- diff --git a/simgear/canvas/layout/BoxLayout.cxx b/simgear/canvas/layout/BoxLayout.cxx index 122fe97f..263878f1 100644 --- a/simgear/canvas/layout/BoxLayout.cxx +++ b/simgear/canvas/layout/BoxLayout.cxx @@ -70,8 +70,12 @@ namespace canvas item_data.layout_item = item; item_data.stretch = std::max(0, stretch); - item->setCanvas(_canvas); - item->setParent(this); + if( SGWeakReferenced::count(this) ) + item->setParent(this); + else + SG_LOG( SG_GUI, + SG_WARN, + "Adding item to expired or non-refcounted layout" ); if( index < 0 ) _layout_items.push_back(item_data); @@ -121,6 +125,7 @@ namespace canvas LayoutItems::iterator it = _layout_items.begin() + index; LayoutItemRef item = it->layout_item; + item->setParent(LayoutItemWeakRef()); item->onRemove(); _layout_items.erase(it); @@ -136,6 +141,7 @@ namespace canvas it != _layout_items.end(); ++it ) { + it->layout_item->setParent(LayoutItemWeakRef()); it->layout_item->onRemove(); } _layout_items.clear(); diff --git a/simgear/canvas/layout/canvas_layout_test.cxx b/simgear/canvas/layout/canvas_layout_test.cxx index 6aa9c79a..5c81a1d8 100644 --- a/simgear/canvas/layout/canvas_layout_test.cxx +++ b/simgear/canvas/layout/canvas_layout_test.cxx @@ -295,35 +295,41 @@ BOOST_AUTO_TEST_CASE( vertical_layout) //------------------------------------------------------------------------------ BOOST_AUTO_TEST_CASE( boxlayout_insert_remove ) { - sc::HBoxLayout hbox; + sc::BoxLayoutRef hbox( new sc::HBoxLayout ); - BOOST_CHECK_EQUAL(hbox.count(), 0); - BOOST_CHECK(!hbox.itemAt(0)); - BOOST_CHECK(!hbox.takeAt(0)); + BOOST_CHECK_EQUAL(hbox->count(), 0); + BOOST_CHECK(!hbox->itemAt(0)); + BOOST_CHECK(!hbox->takeAt(0)); TestWidgetRef w1( new TestWidget( SGVec2i(16, 16), SGVec2i(32, 32), SGVec2i(9999, 32) ) ), w2( new TestWidget(*w1) ); - hbox.addItem(w1); - BOOST_CHECK_EQUAL(hbox.count(), 1); - BOOST_CHECK_EQUAL(hbox.itemAt(0), w1); - - hbox.insertItem(0, w2); - BOOST_CHECK_EQUAL(hbox.count(), 2); - BOOST_CHECK_EQUAL(hbox.itemAt(0), w2); - BOOST_CHECK_EQUAL(hbox.itemAt(1), w1); - - hbox.removeItem(w2); - BOOST_CHECK_EQUAL(hbox.count(), 1); - BOOST_CHECK_EQUAL(hbox.itemAt(0), w1); - - hbox.addItem(w2); - BOOST_CHECK_EQUAL(hbox.count(), 2); - - hbox.clear(); - BOOST_CHECK_EQUAL(hbox.count(), 0); + hbox->addItem(w1); + BOOST_CHECK_EQUAL(hbox->count(), 1); + BOOST_CHECK_EQUAL(hbox->itemAt(0), w1); + BOOST_CHECK_EQUAL(w1->getParent(), hbox); + + hbox->insertItem(0, w2); + BOOST_CHECK_EQUAL(hbox->count(), 2); + BOOST_CHECK_EQUAL(hbox->itemAt(0), w2); + BOOST_CHECK_EQUAL(hbox->itemAt(1), w1); + BOOST_CHECK_EQUAL(w2->getParent(), hbox); + + hbox->removeItem(w2); + BOOST_CHECK_EQUAL(hbox->count(), 1); + BOOST_CHECK_EQUAL(hbox->itemAt(0), w1); + BOOST_CHECK( !w2->getParent() ); + + hbox->addItem(w2); + BOOST_CHECK_EQUAL(hbox->count(), 2); + BOOST_CHECK_EQUAL(w2->getParent(), hbox); + + hbox->clear(); + BOOST_CHECK_EQUAL(hbox->count(), 0); + BOOST_CHECK( !w1->getParent() ); + BOOST_CHECK( !w2->getParent() ); } //------------------------------------------------------------------------------