canvas::BoxLayout: fix parent ref on add/remove.
This commit is contained in:
@@ -70,8 +70,12 @@ namespace canvas
|
|||||||
item_data.layout_item = item;
|
item_data.layout_item = item;
|
||||||
item_data.stretch = std::max(0, stretch);
|
item_data.stretch = std::max(0, stretch);
|
||||||
|
|
||||||
item->setCanvas(_canvas);
|
if( SGWeakReferenced::count(this) )
|
||||||
item->setParent(this);
|
item->setParent(this);
|
||||||
|
else
|
||||||
|
SG_LOG( SG_GUI,
|
||||||
|
SG_WARN,
|
||||||
|
"Adding item to expired or non-refcounted layout" );
|
||||||
|
|
||||||
if( index < 0 )
|
if( index < 0 )
|
||||||
_layout_items.push_back(item_data);
|
_layout_items.push_back(item_data);
|
||||||
@@ -121,6 +125,7 @@ namespace canvas
|
|||||||
|
|
||||||
LayoutItems::iterator it = _layout_items.begin() + index;
|
LayoutItems::iterator it = _layout_items.begin() + index;
|
||||||
LayoutItemRef item = it->layout_item;
|
LayoutItemRef item = it->layout_item;
|
||||||
|
item->setParent(LayoutItemWeakRef());
|
||||||
item->onRemove();
|
item->onRemove();
|
||||||
_layout_items.erase(it);
|
_layout_items.erase(it);
|
||||||
|
|
||||||
@@ -136,6 +141,7 @@ namespace canvas
|
|||||||
it != _layout_items.end();
|
it != _layout_items.end();
|
||||||
++it )
|
++it )
|
||||||
{
|
{
|
||||||
|
it->layout_item->setParent(LayoutItemWeakRef());
|
||||||
it->layout_item->onRemove();
|
it->layout_item->onRemove();
|
||||||
}
|
}
|
||||||
_layout_items.clear();
|
_layout_items.clear();
|
||||||
|
|||||||
@@ -295,35 +295,41 @@ BOOST_AUTO_TEST_CASE( vertical_layout)
|
|||||||
//------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------
|
||||||
BOOST_AUTO_TEST_CASE( boxlayout_insert_remove )
|
BOOST_AUTO_TEST_CASE( boxlayout_insert_remove )
|
||||||
{
|
{
|
||||||
sc::HBoxLayout hbox;
|
sc::BoxLayoutRef hbox( new sc::HBoxLayout );
|
||||||
|
|
||||||
BOOST_CHECK_EQUAL(hbox.count(), 0);
|
BOOST_CHECK_EQUAL(hbox->count(), 0);
|
||||||
BOOST_CHECK(!hbox.itemAt(0));
|
BOOST_CHECK(!hbox->itemAt(0));
|
||||||
BOOST_CHECK(!hbox.takeAt(0));
|
BOOST_CHECK(!hbox->takeAt(0));
|
||||||
|
|
||||||
TestWidgetRef w1( new TestWidget( SGVec2i(16, 16),
|
TestWidgetRef w1( new TestWidget( SGVec2i(16, 16),
|
||||||
SGVec2i(32, 32),
|
SGVec2i(32, 32),
|
||||||
SGVec2i(9999, 32) ) ),
|
SGVec2i(9999, 32) ) ),
|
||||||
w2( new TestWidget(*w1) );
|
w2( new TestWidget(*w1) );
|
||||||
|
|
||||||
hbox.addItem(w1);
|
hbox->addItem(w1);
|
||||||
BOOST_CHECK_EQUAL(hbox.count(), 1);
|
BOOST_CHECK_EQUAL(hbox->count(), 1);
|
||||||
BOOST_CHECK_EQUAL(hbox.itemAt(0), w1);
|
BOOST_CHECK_EQUAL(hbox->itemAt(0), w1);
|
||||||
|
BOOST_CHECK_EQUAL(w1->getParent(), hbox);
|
||||||
|
|
||||||
hbox.insertItem(0, w2);
|
hbox->insertItem(0, w2);
|
||||||
BOOST_CHECK_EQUAL(hbox.count(), 2);
|
BOOST_CHECK_EQUAL(hbox->count(), 2);
|
||||||
BOOST_CHECK_EQUAL(hbox.itemAt(0), w2);
|
BOOST_CHECK_EQUAL(hbox->itemAt(0), w2);
|
||||||
BOOST_CHECK_EQUAL(hbox.itemAt(1), w1);
|
BOOST_CHECK_EQUAL(hbox->itemAt(1), w1);
|
||||||
|
BOOST_CHECK_EQUAL(w2->getParent(), hbox);
|
||||||
|
|
||||||
hbox.removeItem(w2);
|
hbox->removeItem(w2);
|
||||||
BOOST_CHECK_EQUAL(hbox.count(), 1);
|
BOOST_CHECK_EQUAL(hbox->count(), 1);
|
||||||
BOOST_CHECK_EQUAL(hbox.itemAt(0), w1);
|
BOOST_CHECK_EQUAL(hbox->itemAt(0), w1);
|
||||||
|
BOOST_CHECK( !w2->getParent() );
|
||||||
|
|
||||||
hbox.addItem(w2);
|
hbox->addItem(w2);
|
||||||
BOOST_CHECK_EQUAL(hbox.count(), 2);
|
BOOST_CHECK_EQUAL(hbox->count(), 2);
|
||||||
|
BOOST_CHECK_EQUAL(w2->getParent(), hbox);
|
||||||
|
|
||||||
hbox.clear();
|
hbox->clear();
|
||||||
BOOST_CHECK_EQUAL(hbox.count(), 0);
|
BOOST_CHECK_EQUAL(hbox->count(), 0);
|
||||||
|
BOOST_CHECK( !w1->getParent() );
|
||||||
|
BOOST_CHECK( !w2->getParent() );
|
||||||
}
|
}
|
||||||
|
|
||||||
//------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------
|
||||||
|
|||||||
Reference in New Issue
Block a user