]> git.mxchange.org Git - simgear.git/blobdiff - simgear/canvas/layout/canvas_layout_test.cxx
canvas::Layout: support for alignment.
[simgear.git] / simgear / canvas / layout / canvas_layout_test.cxx
index 6aa9c79a67af42b8f42c5bbb0dd713f1d4fe92a6..d695f2aa74377b761fc9c000e02b936f3fbefb17 100644 (file)
@@ -23,6 +23,8 @@
 #include "NasalWidget.hxx"
 
 #include <simgear/debug/logstream.hxx>
+#include <simgear/nasal/cppbind/NasalContext.hxx>
+
 #include <cstdlib>
 
 //------------------------------------------------------------------------------
@@ -62,16 +64,17 @@ class TestWidget:
     void setMaxSize(const SGVec2i& size) { _max_size = size; }
     void setSizeHint(const SGVec2i& size) { _size_hint = size; }
 
-    virtual void setGeometry(const SGRecti& geom) { _geom = geom; }
-    virtual SGRecti geometry() const { return _geom; }
-
   protected:
 
-    SGRecti _geom;
-
     virtual SGVec2i sizeHintImpl() const { return _size_hint; }
     virtual SGVec2i minimumSizeImpl() const { return _min_size; }
     virtual SGVec2i maximumSizeImpl() const { return _max_size; }
+
+    virtual void visibilityChanged(bool visible)
+    {
+      if( !visible )
+        _geometry.set(0, 0, 0, 0);
+    }
 };
 
 class TestWidgetHFW:
@@ -91,12 +94,12 @@ class TestWidgetHFW:
       return true;
     }
 
-    virtual int heightForWidth(int w) const
+    virtual int heightForWidthImpl(int w) const
     {
       return _size_hint.x() * _size_hint.y() / w;
     }
 
-    virtual int minimumHeightForWidth(int w) const
+    virtual int minimumHeightForWidthImpl(int w) const
     {
       return _min_size.x() * _min_size.y() / w;
     }
@@ -107,45 +110,45 @@ typedef SGSharedPtr<TestWidget> TestWidgetRef;
 //------------------------------------------------------------------------------
 BOOST_AUTO_TEST_CASE( horizontal_layout )
 {
-  sc::BoxLayout box_layout(sc::BoxLayout::BottomToTop);
-  box_layout.setSpacing(5);
+  sc::BoxLayoutRef box_layout(new sc::BoxLayout(sc::BoxLayout::BottomToTop));
+  box_layout->setSpacing(5);
 
-  BOOST_CHECK_EQUAL(box_layout.direction(), sc::BoxLayout::BottomToTop);
-  BOOST_CHECK_EQUAL(box_layout.spacing(), 5);
+  BOOST_CHECK_EQUAL(box_layout->direction(), sc::BoxLayout::BottomToTop);
+  BOOST_CHECK_EQUAL(box_layout->spacing(), 5);
 
-  box_layout.setDirection(sc::BoxLayout::LeftToRight);
-  box_layout.setSpacing(9);
+  box_layout->setDirection(sc::BoxLayout::LeftToRight);
+  box_layout->setSpacing(9);
 
-  BOOST_CHECK_EQUAL(box_layout.direction(), sc::BoxLayout::LeftToRight);
-  BOOST_CHECK_EQUAL(box_layout.spacing(), 9);
+  BOOST_CHECK_EQUAL(box_layout->direction(), sc::BoxLayout::LeftToRight);
+  BOOST_CHECK_EQUAL(box_layout->spacing(), 9);
 
   TestWidgetRef fixed_size_widget( new TestWidget( SGVec2i(16, 16),
                                                    SGVec2i(16, 16),
                                                    SGVec2i(16, 16) ) );
-  box_layout.addItem(fixed_size_widget);
+  box_layout->addItem(fixed_size_widget);
 
-  BOOST_CHECK_EQUAL(box_layout.minimumSize(), SGVec2i(16, 16));
-  BOOST_CHECK_EQUAL(box_layout.sizeHint(), SGVec2i(16, 16));
-  BOOST_CHECK_EQUAL(box_layout.maximumSize(), SGVec2i(16, 16));
+  BOOST_CHECK_EQUAL(box_layout->minimumSize(), SGVec2i(16, 16));
+  BOOST_CHECK_EQUAL(box_layout->sizeHint(), SGVec2i(16, 16));
+  BOOST_CHECK_EQUAL(box_layout->maximumSize(), SGVec2i(16, 16));
 
   TestWidgetRef limited_resize_widget( new TestWidget( SGVec2i(16, 16),
                                                        SGVec2i(32, 32),
                                                        SGVec2i(256, 64) ) );
-  box_layout.addItem(limited_resize_widget);
+  box_layout->addItem(limited_resize_widget);
 
   // Combined sizes of both widget plus the padding between them
-  BOOST_CHECK_EQUAL(box_layout.minimumSize(), SGVec2i(41, 16));
-  BOOST_CHECK_EQUAL(box_layout.sizeHint(), SGVec2i(57, 32));
-  BOOST_CHECK_EQUAL(box_layout.maximumSize(), SGVec2i(281, 64));
+  BOOST_CHECK_EQUAL(box_layout->minimumSize(), SGVec2i(41, 16));
+  BOOST_CHECK_EQUAL(box_layout->sizeHint(), SGVec2i(57, 32));
+  BOOST_CHECK_EQUAL(box_layout->maximumSize(), SGVec2i(281, 64));
 
   // Test with different spacing/padding
-  box_layout.setSpacing(5);
+  box_layout->setSpacing(5);
 
-  BOOST_CHECK_EQUAL(box_layout.minimumSize(), SGVec2i(37, 16));
-  BOOST_CHECK_EQUAL(box_layout.sizeHint(), SGVec2i(53, 32));
-  BOOST_CHECK_EQUAL(box_layout.maximumSize(), SGVec2i(277, 64));
+  BOOST_CHECK_EQUAL(box_layout->minimumSize(), SGVec2i(37, 16));
+  BOOST_CHECK_EQUAL(box_layout->sizeHint(), SGVec2i(53, 32));
+  BOOST_CHECK_EQUAL(box_layout->maximumSize(), SGVec2i(277, 64));
 
-  box_layout.setGeometry(SGRecti(0, 0, 128, 32));
+  box_layout->setGeometry(SGRecti(0, 0, 128, 32));
 
   // Fixed size for first widget and remaining space goes to second widget
   BOOST_CHECK_EQUAL(fixed_size_widget->geometry(), SGRecti(0, 8, 16, 16));
@@ -154,12 +157,12 @@ BOOST_AUTO_TEST_CASE( horizontal_layout )
   TestWidgetRef stretch_widget( new TestWidget( SGVec2i(16, 16),
                                                 SGVec2i(32, 32),
                                                 SGVec2i(128, 32) ) );
-  box_layout.addItem(stretch_widget, 1);
-  box_layout.update();
+  box_layout->addItem(stretch_widget, 1);
+  box_layout->update();
 
-  BOOST_CHECK_EQUAL(box_layout.minimumSize(), SGVec2i(58, 16));
-  BOOST_CHECK_EQUAL(box_layout.sizeHint(), SGVec2i(90, 32));
-  BOOST_CHECK_EQUAL(box_layout.maximumSize(), SGVec2i(410, 64));
+  BOOST_CHECK_EQUAL(box_layout->minimumSize(), SGVec2i(58, 16));
+  BOOST_CHECK_EQUAL(box_layout->sizeHint(), SGVec2i(90, 32));
+  BOOST_CHECK_EQUAL(box_layout->maximumSize(), SGVec2i(410, 64));
 
   // Due to the stretch factor only the last widget gets additional space. All
   // other widgets get the preferred size.
@@ -169,63 +172,101 @@ BOOST_AUTO_TEST_CASE( horizontal_layout )
 
   // Test stretch factor
   TestWidgetRef fast_stretch( new TestWidget(*stretch_widget) );
-  sc::BoxLayout box_layout_stretch(sc::BoxLayout::LeftToRight);
+  sc::BoxLayoutRef box_layout_stretch(
+    new sc::BoxLayout(sc::BoxLayout::LeftToRight)
+  );
 
-  box_layout_stretch.addItem(stretch_widget, 1);
-  box_layout_stretch.addItem(fast_stretch, 2);
+  box_layout_stretch->addItem(stretch_widget, 1);
+  box_layout_stretch->addItem(fast_stretch, 2);
 
-  box_layout_stretch.setGeometry(SGRecti(0,0,128,32));
+  box_layout_stretch->setGeometry(SGRecti(0,0,128,32));
 
   BOOST_CHECK_EQUAL(stretch_widget->geometry(), SGRecti(0, 0, 41, 32));
   BOOST_CHECK_EQUAL(fast_stretch->geometry(), SGRecti(46, 0, 82, 32));
 
-  box_layout_stretch.setGeometry(SGRecti(0,0,256,32));
+  box_layout_stretch->setGeometry(SGRecti(0,0,256,32));
 
   BOOST_CHECK_EQUAL(stretch_widget->geometry(), SGRecti(0, 0, 123, 32));
   BOOST_CHECK_EQUAL(fast_stretch->geometry(), SGRecti(128, 0, 128, 32));
 
   // Test superflous space to padding
-  box_layout_stretch.setGeometry(SGRecti(0,0,512,32));
+  box_layout_stretch->setGeometry(SGRecti(0,0,512,32));
 
   BOOST_CHECK_EQUAL(stretch_widget->geometry(), SGRecti(83, 0, 128, 32));
   BOOST_CHECK_EQUAL(fast_stretch->geometry(), SGRecti(300, 0, 128, 32));
 
-  // Test more space then preferred, but less than maximum
-  {
-    sc::HBoxLayout hbox;
-    TestWidgetRef w1( new TestWidget( SGVec2i(16,   16),
-                                      SGVec2i(32,   32),
-                                      SGVec2i(9999, 32) ) ),
-                  w2( new TestWidget(*w1) );
+  // ...and now with alignment
+  //
+  // All widgets without alignment get their maximum space and the remaining
+  // space is equally distributed to the remaining items. All items with
+  // alignment are set to their size hint and positioned according to their
+  // alignment.
+
+  // Left widget: size hint and positioned on the left
+  // Right widget: maximum size and positioned on the right
+  stretch_widget->setAlignment(sc::AlignLeft);
+  box_layout_stretch->update();
+  BOOST_CHECK_EQUAL(stretch_widget->geometry(), SGRecti(0, 0, 32, 32));
+  BOOST_CHECK_EQUAL(fast_stretch->geometry(), SGRecti(384, 0, 128, 32));
+
+  // Left widget: align right
+  stretch_widget->setAlignment(sc::AlignRight);
+  box_layout_stretch->update();
+  BOOST_CHECK_EQUAL(stretch_widget->geometry(), SGRecti(347, 0, 32, 32));
+  BOOST_CHECK_EQUAL(fast_stretch->geometry(), SGRecti(384, 0, 128, 32));
+
+  // Left widget: size hint and positioned on the right
+  // Right widget: size hint and positioned on the left of the right half
+  fast_stretch->setAlignment(sc::AlignLeft);
+  box_layout_stretch->update();
+  BOOST_CHECK_EQUAL(stretch_widget->geometry(), SGRecti(221, 0, 32, 32));
+  BOOST_CHECK_EQUAL(fast_stretch->geometry(), SGRecti(258, 0, 32, 32));
+
+  // Also check vertical alignment
+  stretch_widget->setAlignment(sc::AlignLeft | sc::AlignTop);
+  fast_stretch->setAlignment(sc::AlignLeft | sc::AlignBottom);
+  box_layout_stretch->setGeometry(SGRecti(0,0,512,64));
+  BOOST_CHECK_EQUAL(stretch_widget->geometry(), SGRecti(0, 0, 32, 32));
+  BOOST_CHECK_EQUAL(fast_stretch->geometry(), SGRecti(258, 32, 32, 32));
+}
+
+//------------------------------------------------------------------------------
+// Test more space then preferred, but less than maximum
+BOOST_AUTO_TEST_CASE( hbox_pref_to_max )
+{
+  sc::BoxLayoutRef hbox(new sc::HBoxLayout());
+  TestWidgetRef w1( new TestWidget( SGVec2i(16,   16),
+                                    SGVec2i(32,   32),
+                                    SGVec2i(9999, 32) ) ),
+                w2( new TestWidget(*w1) );
 
-    hbox.addItem(w1);
-    hbox.addItem(w2);
+  hbox->addItem(w1);
+  hbox->addItem(w2);
 
-    hbox.setGeometry( SGRecti(0, 0, 256, 32) );
+  hbox->setGeometry( SGRecti(0, 0, 256, 32) );
 
-    BOOST_CHECK_EQUAL(w1->geometry(), SGRecti(0,   0, 126, 32));
-    BOOST_CHECK_EQUAL(w2->geometry(), SGRecti(131, 0, 125, 32));
+  BOOST_CHECK_EQUAL(w1->geometry(), SGRecti(0,   0, 126, 32));
+  BOOST_CHECK_EQUAL(w2->geometry(), SGRecti(131, 0, 125, 32));
 
-    hbox.setStretch(0, 1);
-    hbox.setStretch(1, 1);
+  hbox->setStretch(0, 1);
+  hbox->setStretch(1, 1);
 
-    BOOST_CHECK_EQUAL(hbox.stretch(0), 1);
-    BOOST_CHECK_EQUAL(hbox.stretch(1), 1);
+  BOOST_CHECK_EQUAL(hbox->stretch(0), 1);
+  BOOST_CHECK_EQUAL(hbox->stretch(1), 1);
 
-    hbox.update();
+  hbox->update();
 
-    BOOST_CHECK_EQUAL(w1->geometry(), SGRecti(0,   0, 125, 32));
-    BOOST_CHECK_EQUAL(w2->geometry(), SGRecti(130, 0, 126, 32));
+  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);
+  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);
+  hbox->removeItem(w1);
 
-    BOOST_CHECK( !hbox.setStretchFactor(w1, 0) );
-  }
+  BOOST_CHECK( !hbox->setStretchFactor(w1, 0) );
 }
 
 //------------------------------------------------------------------------------
@@ -295,35 +336,203 @@ 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->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() );
+}
+
+//------------------------------------------------------------------------------
+BOOST_AUTO_TEST_CASE( boxlayout_visibility )
+{
+  sc::BoxLayoutRef hbox( new sc::HBoxLayout );
+  TestWidgetRef w1( new TestWidget( SGVec2i(16, 16),
+                                    SGVec2i(32, 32) ) ),
+                w2( new TestWidget(*w1) ),
+                w3( new TestWidget(*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->addItem(w1);
+  hbox->addItem(w2);
+  hbox->addItem(w3);
 
-  hbox.removeItem(w2);
-  BOOST_CHECK_EQUAL(hbox.count(), 1);
-  BOOST_CHECK_EQUAL(hbox.itemAt(0), w1);
+  BOOST_REQUIRE_EQUAL(hbox->sizeHint().x(), 3 * 32 + 2 * hbox->spacing());
 
-  hbox.addItem(w2);
-  BOOST_CHECK_EQUAL(hbox.count(), 2);
+  hbox->setGeometry(SGRecti(0, 0, 69, 32));
 
-  hbox.clear();
-  BOOST_CHECK_EQUAL(hbox.count(), 0);
+  BOOST_CHECK_EQUAL(w1->geometry(), SGRecti(0,  0, 20, 32));
+  BOOST_CHECK_EQUAL(w2->geometry(), SGRecti(25, 0, 20, 32));
+  BOOST_CHECK_EQUAL(w3->geometry(), SGRecti(50, 0, 19, 32));
+
+  w2->setVisible(false);
+
+  BOOST_REQUIRE(hbox->isVisible());
+  BOOST_REQUIRE(w1->isVisible());
+  BOOST_REQUIRE(!w2->isVisible());
+  BOOST_REQUIRE(w2->isExplicitlyHidden());
+  BOOST_REQUIRE(w3->isVisible());
+
+  BOOST_CHECK_EQUAL(hbox->sizeHint().x(), 2 * 32 + 1 * hbox->spacing());
+
+  hbox->update();
+
+  BOOST_CHECK_EQUAL(w1->geometry(), SGRecti(0,  0, 32, 32));
+  BOOST_CHECK_EQUAL(w2->geometry(), SGRecti(0,  0,  0,  0));
+  BOOST_CHECK_EQUAL(w3->geometry(), SGRecti(37, 0, 32, 32));
+
+  hbox->setVisible(false);
+
+  BOOST_REQUIRE(!hbox->isVisible());
+  BOOST_REQUIRE(hbox->isExplicitlyHidden());
+  BOOST_REQUIRE(!w1->isVisible());
+  BOOST_REQUIRE(!w1->isExplicitlyHidden());
+  BOOST_REQUIRE(!w2->isVisible());
+  BOOST_REQUIRE(w2->isExplicitlyHidden());
+  BOOST_REQUIRE(!w3->isVisible());
+  BOOST_REQUIRE(!w3->isExplicitlyHidden());
+
+  BOOST_CHECK_EQUAL(w1->geometry(), SGRecti(0, 0, 0, 0));
+  BOOST_CHECK_EQUAL(w2->geometry(), SGRecti(0, 0, 0, 0));
+  BOOST_CHECK_EQUAL(w3->geometry(), SGRecti(0, 0, 0, 0));
+
+  w2->setVisible(true);
+
+  BOOST_REQUIRE(!w2->isVisible());
+  BOOST_REQUIRE(!w2->isExplicitlyHidden());
+
+  hbox->setVisible(true);
+
+  BOOST_REQUIRE(hbox->isVisible());
+  BOOST_REQUIRE(w1->isVisible());
+  BOOST_REQUIRE(w2->isVisible());
+  BOOST_REQUIRE(w3->isVisible());
+
+  hbox->update();
+
+  BOOST_CHECK_EQUAL(w1->geometry(), SGRecti(0,  0, 20, 32));
+  BOOST_CHECK_EQUAL(w2->geometry(), SGRecti(25, 0, 20, 32));
+  BOOST_CHECK_EQUAL(w3->geometry(), SGRecti(50, 0, 19, 32));
+}
+
+//------------------------------------------------------------------------------
+BOOST_AUTO_TEST_CASE( boxlayout_contents_margins )
+{
+  sc::Margins m;
+
+  BOOST_REQUIRE(m.isNull());
+
+  m = sc::Margins(5);
+
+  BOOST_REQUIRE_EQUAL(m.l, 5);
+  BOOST_REQUIRE_EQUAL(m.t, 5);
+  BOOST_REQUIRE_EQUAL(m.r, 5);
+  BOOST_REQUIRE_EQUAL(m.b, 5);
+
+  m = sc::Margins(6, 7);
+
+  BOOST_REQUIRE_EQUAL(m.l, 6);
+  BOOST_REQUIRE_EQUAL(m.t, 7);
+  BOOST_REQUIRE_EQUAL(m.r, 6);
+  BOOST_REQUIRE_EQUAL(m.b, 7);
+
+  BOOST_REQUIRE_EQUAL(m.horiz(), 12);
+  BOOST_REQUIRE_EQUAL(m.vert(), 14);
+  BOOST_REQUIRE(!m.isNull());
+
+  m = sc::Margins(1, 2, 3, 4);
+
+  BOOST_REQUIRE_EQUAL(m.l, 1);
+  BOOST_REQUIRE_EQUAL(m.t, 2);
+  BOOST_REQUIRE_EQUAL(m.r, 3);
+  BOOST_REQUIRE_EQUAL(m.b, 4);
+
+  BOOST_REQUIRE_EQUAL(m.horiz(), 4);
+  BOOST_REQUIRE_EQUAL(m.vert(), 6);
+  BOOST_REQUIRE_EQUAL(m.size(), SGVec2i(4, 6));
+
+  sc::BoxLayoutRef hbox( new sc::HBoxLayout );
+
+  hbox->setContentsMargins(5, 10, 15, 20);
+
+  BOOST_CHECK_EQUAL(hbox->minimumSize(), SGVec2i(20, 30));
+  BOOST_CHECK_EQUAL(hbox->sizeHint(),    SGVec2i(20, 30));
+  BOOST_CHECK_EQUAL(hbox->maximumSize(), SGVec2i(20, 30));
+
+  hbox->setGeometry(SGRecti(0, 0, 30, 40));
+
+  BOOST_CHECK_EQUAL(hbox->geometry(), SGRecti(0, 0, 30, 40));
+  BOOST_CHECK_EQUAL(hbox->contentsRect(), SGRecti(5, 10, 10, 10));
+
+  TestWidgetRef w1( new TestWidget( SGVec2i(16, 16),
+                                    SGVec2i(32, 32) ) ),
+                w2( new TestWidget(*w1) ),
+                w3( new TestWidget(*w1) );
+
+  w1->setContentsMargin(5);
+  w2->setContentsMargin(6);
+  w3->setContentsMargin(7);
+
+  BOOST_CHECK_EQUAL(w1->minimumSize(), SGVec2i(26, 26));
+  BOOST_CHECK_EQUAL(w1->sizeHint(),    SGVec2i(42, 42));
+  BOOST_CHECK_EQUAL(w1->maximumSize(), sc::LayoutItem::MAX_SIZE);
+
+  BOOST_CHECK_EQUAL(w2->minimumSize(), SGVec2i(28, 28));
+  BOOST_CHECK_EQUAL(w2->sizeHint(),    SGVec2i(44, 44));
+  BOOST_CHECK_EQUAL(w2->maximumSize(), sc::LayoutItem::MAX_SIZE);
+
+  BOOST_CHECK_EQUAL(w3->minimumSize(), SGVec2i(30, 30));
+  BOOST_CHECK_EQUAL(w3->sizeHint(),    SGVec2i(46, 46));
+  BOOST_CHECK_EQUAL(w3->maximumSize(), sc::LayoutItem::MAX_SIZE);
+
+  hbox->addItem(w1);
+  hbox->addItem(w2);
+  hbox->addItem(w3);
+
+  BOOST_CHECK_EQUAL(hbox->minimumSize(), SGVec2i(114, 60));
+  BOOST_CHECK_EQUAL(hbox->sizeHint(),    SGVec2i(162, 76));
+  BOOST_CHECK_EQUAL(hbox->maximumSize(), sc::LayoutItem::MAX_SIZE);
+
+  hbox->setGeometry(SGRecti(0, 0, hbox->sizeHint().x(), hbox->sizeHint().y()));
+
+  BOOST_CHECK_EQUAL(hbox->contentsRect(), SGRecti(5, 10, 142, 46));
+
+  BOOST_CHECK_EQUAL(w1->geometry(), SGRecti(5,   10, 42, 46));
+  BOOST_CHECK_EQUAL(w2->geometry(), SGRecti(52,  10, 44, 46));
+  BOOST_CHECK_EQUAL(w3->geometry(), SGRecti(101, 10, 46, 46));
+
+  BOOST_CHECK_EQUAL(w1->contentsRect(), SGRecti(10,  15, 32, 36));
+  BOOST_CHECK_EQUAL(w2->contentsRect(), SGRecti(58,  16, 32, 34));
+  BOOST_CHECK_EQUAL(w3->contentsRect(), SGRecti(108, 17, 32, 32));
 }
 
 //------------------------------------------------------------------------------
@@ -417,13 +626,77 @@ BOOST_AUTO_TEST_CASE( boxlayout_hfw )
   BOOST_CHECK_EQUAL(w_no_hfw->geometry(), SGRecti(0, 90, 24, 32));
 }
 
+//------------------------------------------------------------------------------
+BOOST_AUTO_TEST_CASE( item_alignment_rect )
+{
+  TestWidgetRef w1( new TestWidget( SGVec2i(16, 16),
+                                    SGVec2i(32, 32) ) );
+
+  const SGRecti r(10, 10, 64, 64);
+
+  // Default: AlignFill -> fill up to maximum size
+  BOOST_CHECK_EQUAL(w1->alignmentRect(r), r);
+
+  // Horizontal
+
+  // AlignLeft -> width from size hint, positioned on the left
+  w1->setAlignment(sc::AlignLeft);
+  BOOST_CHECK_EQUAL(w1->alignmentRect(r), SGRecti(10, 10, 32, 64));
+
+  // AlignRight -> width from size hint, positioned on the left
+  w1->setAlignment(sc::AlignRight);
+  BOOST_CHECK_EQUAL(w1->alignmentRect(r), SGRecti(42, 10, 32, 64));
+
+  // AlignHCenter -> width from size hint, positioned in the center
+  w1->setAlignment(sc::AlignHCenter);
+  BOOST_CHECK_EQUAL(w1->alignmentRect(r), SGRecti(26, 10, 32, 64));
+
+
+  // Vertical
+
+  // AlignTop -> height from size hint, positioned on the top
+  w1->setAlignment(sc::AlignTop);
+  BOOST_CHECK_EQUAL(w1->alignmentRect(r), SGRecti(10, 10, 64, 32));
+
+  // AlignBottom -> height from size hint, positioned on the bottom
+  w1->setAlignment(sc::AlignBottom);
+  BOOST_CHECK_EQUAL(w1->alignmentRect(r), SGRecti(10, 42, 64, 32));
+
+  // AlignVCenter -> height from size hint, positioned in the center
+  w1->setAlignment(sc::AlignVCenter);
+  BOOST_CHECK_EQUAL(w1->alignmentRect(r), SGRecti(10, 26, 64, 32));
+
+
+  // Vertical + Horizontal
+  w1->setAlignment(sc::AlignCenter);
+  BOOST_CHECK_EQUAL(w1->alignmentRect(r), SGRecti(26, 26, 32, 32));
+}
+
+//------------------------------------------------------------------------------
+// TODO extend to_nasal_helper for automatic argument conversion
+static naRef f_Widget_visibilityChanged(nasal::CallContext ctx)
+{
+  sc::NasalWidget* w = ctx.from_nasal<sc::NasalWidget*>(ctx.me);
+
+  if( !ctx.requireArg<bool>(0) )
+    w->setGeometry(SGRecti(0, 0, -1, -1));
+
+  return naNil();
+}
+
 //------------------------------------------------------------------------------
 BOOST_AUTO_TEST_CASE( nasal_widget )
 {
-  naContext c = naNewContext();
-  naRef me = naNewHash(c);
+  nasal::Context c;
+  nasal::Hash globals = c.newHash();
+
+  nasal::Object::setupGhost();
+  nasal::Ghost<sc::LayoutItemRef>::init("LayoutItem");
+  sc::NasalWidget::setupGhost(globals);
 
-  sc::NasalWidgetRef w( new sc::NasalWidget(me) );
+  nasal::Hash me = c.newHash();
+  me.set("visibilityChanged", &f_Widget_visibilityChanged);
+  sc::NasalWidgetRef w( new sc::NasalWidget(me.get_naRef()) );
 
   // Default layout sizes (no user set values)
   BOOST_CHECK_EQUAL(w->minimumSize(), SGVec2i(16, 16));
@@ -457,5 +730,6 @@ BOOST_AUTO_TEST_CASE( nasal_widget )
   BOOST_CHECK_EQUAL(w->sizeHint(),    SGVec2i(3, 22));
   BOOST_CHECK_EQUAL(w->maximumSize(), SGVec2i(4, 23));
 
-  naFreeContext(c);
+  w->setVisible(false);
+  BOOST_CHECK_EQUAL(w->geometry(), SGRecti(0, 0, -1, -1));
 }