]> git.mxchange.org Git - simgear.git/commitdiff
canvas::BoxLayout: fix parent ref on add/remove.
authorThomas Geymayer <tomgey@gmail.com>
Thu, 17 Jul 2014 13:05:13 +0000 (15:05 +0200)
committerThomas Geymayer <tomgey@gmail.com>
Thu, 17 Jul 2014 13:10:35 +0000 (15:10 +0200)
simgear/canvas/layout/BoxLayout.cxx
simgear/canvas/layout/canvas_layout_test.cxx

index 122fe97fbe428a704e5c925a96838b7a08974bd4..263878f1b38b55de598d5408e51f3dd02f277609 100644 (file)
@@ -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();
index 6aa9c79a67af42b8f42c5bbb0dd713f1d4fe92a6..5c81a1d89308855a64661d3d96ba3034fe6d3be1 100644 (file)
@@ -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() );
 }
 
 //------------------------------------------------------------------------------