]> git.mxchange.org Git - simgear.git/blob - simgear/canvas/layout/canvas_layout_test.cxx
canvas::Layout: support for contents margins.
[simgear.git] / simgear / canvas / layout / canvas_layout_test.cxx
1 // Testing canvas layouting system
2 //
3 // Copyright (C) 2014  Thomas Geymayer <tomgey@gmail.com>
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Library General Public
7 // License as published by the Free Software Foundation; either
8 // version 2 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 // Library General Public License for more details.
14 //
15 // You should have received a copy of the GNU Library General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA
18
19 #define BOOST_TEST_MODULE canvas_layout
20 #include <BoostTestTargetConfig.h>
21
22 #include "BoxLayout.hxx"
23 #include "NasalWidget.hxx"
24
25 #include <simgear/debug/logstream.hxx>
26 #include <simgear/nasal/cppbind/NasalContext.hxx>
27
28 #include <cstdlib>
29
30 //------------------------------------------------------------------------------
31 struct SetLogLevelFixture
32 {
33   SetLogLevelFixture()
34   {
35     sglog().set_log_priority(SG_DEBUG);
36   }
37 };
38 BOOST_GLOBAL_FIXTURE(SetLogLevelFixture);
39
40 //------------------------------------------------------------------------------
41 namespace sc = simgear::canvas;
42
43 class TestWidget:
44   public sc::LayoutItem
45 {
46   public:
47     TestWidget( const SGVec2i& min_size,
48                 const SGVec2i& size_hint,
49                 const SGVec2i& max_size = MAX_SIZE )
50     {
51       _size_hint = size_hint;
52       _min_size = min_size;
53       _max_size = max_size;
54     }
55
56     TestWidget(const TestWidget& rhs)
57     {
58       _size_hint = rhs._size_hint;
59       _min_size = rhs._min_size;
60       _max_size = rhs._max_size;
61     }
62
63     void setMinSize(const SGVec2i& size) { _min_size = size; }
64     void setMaxSize(const SGVec2i& size) { _max_size = size; }
65     void setSizeHint(const SGVec2i& size) { _size_hint = size; }
66
67   protected:
68
69     virtual SGVec2i sizeHintImpl() const { return _size_hint; }
70     virtual SGVec2i minimumSizeImpl() const { return _min_size; }
71     virtual SGVec2i maximumSizeImpl() const { return _max_size; }
72
73     virtual void visibilityChanged(bool visible)
74     {
75       if( !visible )
76         _geometry.set(0, 0, 0, 0);
77     }
78 };
79
80 class TestWidgetHFW:
81   public TestWidget
82 {
83   public:
84     TestWidgetHFW( const SGVec2i& min_size,
85                    const SGVec2i& size_hint,
86                    const SGVec2i& max_size = MAX_SIZE ):
87       TestWidget(min_size, size_hint, max_size)
88     {
89
90     }
91
92     virtual bool hasHeightForWidth() const
93     {
94       return true;
95     }
96
97     virtual int heightForWidthImpl(int w) const
98     {
99       return _size_hint.x() * _size_hint.y() / w;
100     }
101
102     virtual int minimumHeightForWidthImpl(int w) const
103     {
104       return _min_size.x() * _min_size.y() / w;
105     }
106 };
107
108 typedef SGSharedPtr<TestWidget> TestWidgetRef;
109
110 //------------------------------------------------------------------------------
111 BOOST_AUTO_TEST_CASE( horizontal_layout )
112 {
113   sc::BoxLayout box_layout(sc::BoxLayout::BottomToTop);
114   box_layout.setSpacing(5);
115
116   BOOST_CHECK_EQUAL(box_layout.direction(), sc::BoxLayout::BottomToTop);
117   BOOST_CHECK_EQUAL(box_layout.spacing(), 5);
118
119   box_layout.setDirection(sc::BoxLayout::LeftToRight);
120   box_layout.setSpacing(9);
121
122   BOOST_CHECK_EQUAL(box_layout.direction(), sc::BoxLayout::LeftToRight);
123   BOOST_CHECK_EQUAL(box_layout.spacing(), 9);
124
125   TestWidgetRef fixed_size_widget( new TestWidget( SGVec2i(16, 16),
126                                                    SGVec2i(16, 16),
127                                                    SGVec2i(16, 16) ) );
128   box_layout.addItem(fixed_size_widget);
129
130   BOOST_CHECK_EQUAL(box_layout.minimumSize(), SGVec2i(16, 16));
131   BOOST_CHECK_EQUAL(box_layout.sizeHint(), SGVec2i(16, 16));
132   BOOST_CHECK_EQUAL(box_layout.maximumSize(), SGVec2i(16, 16));
133
134   TestWidgetRef limited_resize_widget( new TestWidget( SGVec2i(16, 16),
135                                                        SGVec2i(32, 32),
136                                                        SGVec2i(256, 64) ) );
137   box_layout.addItem(limited_resize_widget);
138
139   // Combined sizes of both widget plus the padding between them
140   BOOST_CHECK_EQUAL(box_layout.minimumSize(), SGVec2i(41, 16));
141   BOOST_CHECK_EQUAL(box_layout.sizeHint(), SGVec2i(57, 32));
142   BOOST_CHECK_EQUAL(box_layout.maximumSize(), SGVec2i(281, 64));
143
144   // Test with different spacing/padding
145   box_layout.setSpacing(5);
146
147   BOOST_CHECK_EQUAL(box_layout.minimumSize(), SGVec2i(37, 16));
148   BOOST_CHECK_EQUAL(box_layout.sizeHint(), SGVec2i(53, 32));
149   BOOST_CHECK_EQUAL(box_layout.maximumSize(), SGVec2i(277, 64));
150
151   box_layout.setGeometry(SGRecti(0, 0, 128, 32));
152
153   // Fixed size for first widget and remaining space goes to second widget
154   BOOST_CHECK_EQUAL(fixed_size_widget->geometry(), SGRecti(0, 8, 16, 16));
155   BOOST_CHECK_EQUAL(limited_resize_widget->geometry(), SGRecti(21, 0, 107, 32));
156
157   TestWidgetRef stretch_widget( new TestWidget( SGVec2i(16, 16),
158                                                 SGVec2i(32, 32),
159                                                 SGVec2i(128, 32) ) );
160   box_layout.addItem(stretch_widget, 1);
161   box_layout.update();
162
163   BOOST_CHECK_EQUAL(box_layout.minimumSize(), SGVec2i(58, 16));
164   BOOST_CHECK_EQUAL(box_layout.sizeHint(), SGVec2i(90, 32));
165   BOOST_CHECK_EQUAL(box_layout.maximumSize(), SGVec2i(410, 64));
166
167   // Due to the stretch factor only the last widget gets additional space. All
168   // other widgets get the preferred size.
169   BOOST_CHECK_EQUAL(fixed_size_widget->geometry(), SGRecti(0, 8, 16, 16));
170   BOOST_CHECK_EQUAL(limited_resize_widget->geometry(), SGRecti(21, 0, 32, 32));
171   BOOST_CHECK_EQUAL(stretch_widget->geometry(), SGRecti(58, 0, 70, 32));
172
173   // Test stretch factor
174   TestWidgetRef fast_stretch( new TestWidget(*stretch_widget) );
175   sc::BoxLayout box_layout_stretch(sc::BoxLayout::LeftToRight);
176
177   box_layout_stretch.addItem(stretch_widget, 1);
178   box_layout_stretch.addItem(fast_stretch, 2);
179
180   box_layout_stretch.setGeometry(SGRecti(0,0,128,32));
181
182   BOOST_CHECK_EQUAL(stretch_widget->geometry(), SGRecti(0, 0, 41, 32));
183   BOOST_CHECK_EQUAL(fast_stretch->geometry(), SGRecti(46, 0, 82, 32));
184
185   box_layout_stretch.setGeometry(SGRecti(0,0,256,32));
186
187   BOOST_CHECK_EQUAL(stretch_widget->geometry(), SGRecti(0, 0, 123, 32));
188   BOOST_CHECK_EQUAL(fast_stretch->geometry(), SGRecti(128, 0, 128, 32));
189
190   // Test superflous space to padding
191   box_layout_stretch.setGeometry(SGRecti(0,0,512,32));
192
193   BOOST_CHECK_EQUAL(stretch_widget->geometry(), SGRecti(83, 0, 128, 32));
194   BOOST_CHECK_EQUAL(fast_stretch->geometry(), SGRecti(300, 0, 128, 32));
195
196   // Test more space then preferred, but less than maximum
197   {
198     sc::HBoxLayout hbox;
199     TestWidgetRef w1( new TestWidget( SGVec2i(16,   16),
200                                       SGVec2i(32,   32),
201                                       SGVec2i(9999, 32) ) ),
202                   w2( new TestWidget(*w1) );
203
204     hbox.addItem(w1);
205     hbox.addItem(w2);
206
207     hbox.setGeometry( SGRecti(0, 0, 256, 32) );
208
209     BOOST_CHECK_EQUAL(w1->geometry(), SGRecti(0,   0, 126, 32));
210     BOOST_CHECK_EQUAL(w2->geometry(), SGRecti(131, 0, 125, 32));
211
212     hbox.setStretch(0, 1);
213     hbox.setStretch(1, 1);
214
215     BOOST_CHECK_EQUAL(hbox.stretch(0), 1);
216     BOOST_CHECK_EQUAL(hbox.stretch(1), 1);
217
218     hbox.update();
219
220     BOOST_CHECK_EQUAL(w1->geometry(), SGRecti(0,   0, 125, 32));
221     BOOST_CHECK_EQUAL(w2->geometry(), SGRecti(130, 0, 126, 32));
222
223     BOOST_REQUIRE( hbox.setStretchFactor(w1, 2) );
224     BOOST_REQUIRE( hbox.setStretchFactor(w2, 3) );
225     BOOST_CHECK_EQUAL(hbox.stretch(0), 2);
226     BOOST_CHECK_EQUAL(hbox.stretch(1), 3);
227
228     hbox.removeItem(w1);
229
230     BOOST_CHECK( !hbox.setStretchFactor(w1, 0) );
231   }
232 }
233
234 //------------------------------------------------------------------------------
235 BOOST_AUTO_TEST_CASE( spacer_layouting )
236 {
237   sc::HBoxLayout hbox;
238   TestWidgetRef w1( new TestWidget( SGVec2i(16, 16),
239                                     SGVec2i(32, 32),
240                                     SGVec2i(9999, 9999) ) ),
241                 w2( new TestWidget(*w1) );
242
243   hbox.addItem(w1);
244   hbox.addItem(w2);
245   hbox.addStretch(1);
246
247   BOOST_CHECK_EQUAL(hbox.minimumSize(), SGVec2i(37, 16));
248   BOOST_CHECK_EQUAL(hbox.sizeHint(), SGVec2i(69, 32));
249   BOOST_CHECK_EQUAL(hbox.maximumSize(), sc::LayoutItem::MAX_SIZE);
250
251   hbox.setGeometry(SGRecti(0, 0, 256, 40));
252
253   BOOST_CHECK_EQUAL(w1->geometry(), SGRecti(0,  0, 32, 40));
254   BOOST_CHECK_EQUAL(w2->geometry(), SGRecti(37, 0, 32, 40));
255
256   // now center with increased spacing between both widgets
257   hbox.insertStretch(0, 1);
258   hbox.insertSpacing(2, 10);
259
260   BOOST_CHECK_EQUAL(hbox.minimumSize(), SGVec2i(47, 16));
261   BOOST_CHECK_EQUAL(hbox.sizeHint(), SGVec2i(79, 32));
262   BOOST_CHECK_EQUAL(hbox.maximumSize(), sc::LayoutItem::MAX_SIZE);
263
264   hbox.update();
265
266   BOOST_CHECK_EQUAL(w1->geometry(), SGRecti(88,  0, 32, 40));
267   BOOST_CHECK_EQUAL(w2->geometry(), SGRecti(135, 0, 32, 40));
268 }
269
270 //------------------------------------------------------------------------------
271 BOOST_AUTO_TEST_CASE( vertical_layout)
272 {
273   sc::BoxLayout vbox(sc::BoxLayout::TopToBottom);
274   vbox.setSpacing(7);
275
276   TestWidgetRef fixed_size_widget( new TestWidget( SGVec2i(16, 16),
277                                                    SGVec2i(16, 16),
278                                                    SGVec2i(16, 16) ) );
279   TestWidgetRef limited_resize_widget( new TestWidget( SGVec2i(16, 16),
280                                                        SGVec2i(32, 32),
281                                                        SGVec2i(256, 64) ) );
282
283   vbox.addItem(fixed_size_widget);
284   vbox.addItem(limited_resize_widget);
285
286   BOOST_CHECK_EQUAL(vbox.minimumSize(), SGVec2i(16, 39));
287   BOOST_CHECK_EQUAL(vbox.sizeHint(), SGVec2i(32, 55));
288   BOOST_CHECK_EQUAL(vbox.maximumSize(), SGVec2i(256, 87));
289
290   vbox.setGeometry(SGRecti(10, 20, 16, 55));
291
292   BOOST_CHECK_EQUAL(fixed_size_widget->geometry(), SGRecti(10, 20, 16, 16));
293   BOOST_CHECK_EQUAL(limited_resize_widget->geometry(), SGRecti(10, 43, 16, 32));
294
295   vbox.setDirection(sc::BoxLayout::BottomToTop);
296 }
297
298 //------------------------------------------------------------------------------
299 BOOST_AUTO_TEST_CASE( boxlayout_insert_remove )
300 {
301   sc::BoxLayoutRef hbox( new sc::HBoxLayout );
302
303   BOOST_CHECK_EQUAL(hbox->count(), 0);
304   BOOST_CHECK(!hbox->itemAt(0));
305   BOOST_CHECK(!hbox->takeAt(0));
306
307   TestWidgetRef w1( new TestWidget( SGVec2i(16,   16),
308                                     SGVec2i(32,   32),
309                                     SGVec2i(9999, 32) ) ),
310                 w2( new TestWidget(*w1) );
311
312   hbox->addItem(w1);
313   BOOST_CHECK_EQUAL(hbox->count(), 1);
314   BOOST_CHECK_EQUAL(hbox->itemAt(0), w1);
315   BOOST_CHECK_EQUAL(w1->getParent(), hbox);
316
317   hbox->insertItem(0, w2);
318   BOOST_CHECK_EQUAL(hbox->count(), 2);
319   BOOST_CHECK_EQUAL(hbox->itemAt(0), w2);
320   BOOST_CHECK_EQUAL(hbox->itemAt(1), w1);
321   BOOST_CHECK_EQUAL(w2->getParent(), hbox);
322
323   hbox->removeItem(w2);
324   BOOST_CHECK_EQUAL(hbox->count(), 1);
325   BOOST_CHECK_EQUAL(hbox->itemAt(0), w1);
326   BOOST_CHECK( !w2->getParent() );
327
328   hbox->addItem(w2);
329   BOOST_CHECK_EQUAL(hbox->count(), 2);
330   BOOST_CHECK_EQUAL(w2->getParent(), hbox);
331
332   hbox->clear();
333   BOOST_CHECK_EQUAL(hbox->count(), 0);
334   BOOST_CHECK( !w1->getParent() );
335   BOOST_CHECK( !w2->getParent() );
336 }
337
338 //------------------------------------------------------------------------------
339 BOOST_AUTO_TEST_CASE( boxlayout_visibility )
340 {
341   sc::BoxLayoutRef hbox( new sc::HBoxLayout );
342   TestWidgetRef w1( new TestWidget( SGVec2i(16, 16),
343                                     SGVec2i(32, 32) ) ),
344                 w2( new TestWidget(*w1) ),
345                 w3( new TestWidget(*w1) );
346
347   hbox->addItem(w1);
348   hbox->addItem(w2);
349   hbox->addItem(w3);
350
351   BOOST_REQUIRE_EQUAL(hbox->sizeHint().x(), 3 * 32 + 2 * hbox->spacing());
352
353   hbox->setGeometry(SGRecti(0, 0, 69, 32));
354
355   BOOST_CHECK_EQUAL(w1->geometry(), SGRecti(0,  0, 20, 32));
356   BOOST_CHECK_EQUAL(w2->geometry(), SGRecti(25, 0, 20, 32));
357   BOOST_CHECK_EQUAL(w3->geometry(), SGRecti(50, 0, 19, 32));
358
359   w2->setVisible(false);
360
361   BOOST_REQUIRE(hbox->isVisible());
362   BOOST_REQUIRE(w1->isVisible());
363   BOOST_REQUIRE(!w2->isVisible());
364   BOOST_REQUIRE(w2->isExplicitlyHidden());
365   BOOST_REQUIRE(w3->isVisible());
366
367   BOOST_CHECK_EQUAL(hbox->sizeHint().x(), 2 * 32 + 1 * hbox->spacing());
368
369   hbox->update();
370
371   BOOST_CHECK_EQUAL(w1->geometry(), SGRecti(0,  0, 32, 32));
372   BOOST_CHECK_EQUAL(w2->geometry(), SGRecti(0,  0,  0,  0));
373   BOOST_CHECK_EQUAL(w3->geometry(), SGRecti(37, 0, 32, 32));
374
375   hbox->setVisible(false);
376
377   BOOST_REQUIRE(!hbox->isVisible());
378   BOOST_REQUIRE(hbox->isExplicitlyHidden());
379   BOOST_REQUIRE(!w1->isVisible());
380   BOOST_REQUIRE(!w1->isExplicitlyHidden());
381   BOOST_REQUIRE(!w2->isVisible());
382   BOOST_REQUIRE(w2->isExplicitlyHidden());
383   BOOST_REQUIRE(!w3->isVisible());
384   BOOST_REQUIRE(!w3->isExplicitlyHidden());
385
386   BOOST_CHECK_EQUAL(w1->geometry(), SGRecti(0, 0, 0, 0));
387   BOOST_CHECK_EQUAL(w2->geometry(), SGRecti(0, 0, 0, 0));
388   BOOST_CHECK_EQUAL(w3->geometry(), SGRecti(0, 0, 0, 0));
389
390   w2->setVisible(true);
391
392   BOOST_REQUIRE(!w2->isVisible());
393   BOOST_REQUIRE(!w2->isExplicitlyHidden());
394
395   hbox->setVisible(true);
396
397   BOOST_REQUIRE(hbox->isVisible());
398   BOOST_REQUIRE(w1->isVisible());
399   BOOST_REQUIRE(w2->isVisible());
400   BOOST_REQUIRE(w3->isVisible());
401
402   hbox->update();
403
404   BOOST_CHECK_EQUAL(w1->geometry(), SGRecti(0,  0, 20, 32));
405   BOOST_CHECK_EQUAL(w2->geometry(), SGRecti(25, 0, 20, 32));
406   BOOST_CHECK_EQUAL(w3->geometry(), SGRecti(50, 0, 19, 32));
407 }
408
409 //------------------------------------------------------------------------------
410 BOOST_AUTO_TEST_CASE( boxlayout_contents_margins )
411 {
412   sc::Margins m;
413
414   BOOST_REQUIRE(m.isNull());
415
416   m = sc::Margins(5);
417
418   BOOST_REQUIRE_EQUAL(m.l, 5);
419   BOOST_REQUIRE_EQUAL(m.t, 5);
420   BOOST_REQUIRE_EQUAL(m.r, 5);
421   BOOST_REQUIRE_EQUAL(m.b, 5);
422
423   m = sc::Margins(6, 7);
424
425   BOOST_REQUIRE_EQUAL(m.l, 6);
426   BOOST_REQUIRE_EQUAL(m.t, 7);
427   BOOST_REQUIRE_EQUAL(m.r, 6);
428   BOOST_REQUIRE_EQUAL(m.b, 7);
429
430   BOOST_REQUIRE_EQUAL(m.horiz(), 12);
431   BOOST_REQUIRE_EQUAL(m.vert(), 14);
432   BOOST_REQUIRE(!m.isNull());
433
434   m = sc::Margins(1, 2, 3, 4);
435
436   BOOST_REQUIRE_EQUAL(m.l, 1);
437   BOOST_REQUIRE_EQUAL(m.t, 2);
438   BOOST_REQUIRE_EQUAL(m.r, 3);
439   BOOST_REQUIRE_EQUAL(m.b, 4);
440
441   BOOST_REQUIRE_EQUAL(m.horiz(), 4);
442   BOOST_REQUIRE_EQUAL(m.vert(), 6);
443   BOOST_REQUIRE_EQUAL(m.size(), SGVec2i(4, 6));
444
445   sc::BoxLayoutRef hbox( new sc::HBoxLayout );
446
447   hbox->setContentsMargins(5, 10, 15, 20);
448
449   BOOST_CHECK_EQUAL(hbox->minimumSize(), SGVec2i(20, 30));
450   BOOST_CHECK_EQUAL(hbox->sizeHint(),    SGVec2i(20, 30));
451   BOOST_CHECK_EQUAL(hbox->maximumSize(), SGVec2i(20, 30));
452
453   hbox->setGeometry(SGRecti(0, 0, 30, 40));
454
455   BOOST_CHECK_EQUAL(hbox->contentsRect(), SGRecti(5, 10, 10, 10));
456
457   TestWidgetRef w1( new TestWidget( SGVec2i(16, 16),
458                                     SGVec2i(32, 32) ) ),
459                 w2( new TestWidget(*w1) ),
460                 w3( new TestWidget(*w1) );
461
462   w1->setContentsMargin(5);
463   w2->setContentsMargin(6);
464   w3->setContentsMargin(7);
465
466   BOOST_CHECK_EQUAL(w1->minimumSize(), SGVec2i(26, 26));
467   BOOST_CHECK_EQUAL(w1->sizeHint(),    SGVec2i(42, 42));
468   BOOST_CHECK_EQUAL(w1->maximumSize(), sc::LayoutItem::MAX_SIZE);
469
470   BOOST_CHECK_EQUAL(w2->minimumSize(), SGVec2i(28, 28));
471   BOOST_CHECK_EQUAL(w2->sizeHint(),    SGVec2i(44, 44));
472   BOOST_CHECK_EQUAL(w2->maximumSize(), sc::LayoutItem::MAX_SIZE);
473
474   BOOST_CHECK_EQUAL(w3->minimumSize(), SGVec2i(30, 30));
475   BOOST_CHECK_EQUAL(w3->sizeHint(),    SGVec2i(46, 46));
476   BOOST_CHECK_EQUAL(w3->maximumSize(), sc::LayoutItem::MAX_SIZE);
477
478   hbox->addItem(w1);
479   hbox->addItem(w2);
480   hbox->addItem(w3);
481
482   BOOST_CHECK_EQUAL(hbox->minimumSize(), SGVec2i(114, 60));
483   BOOST_CHECK_EQUAL(hbox->sizeHint(),    SGVec2i(162, 76));
484   BOOST_CHECK_EQUAL(hbox->maximumSize(), sc::LayoutItem::MAX_SIZE);
485
486   hbox->setGeometry(SGRecti(0, 0, hbox->sizeHint().x(), hbox->sizeHint().y()));
487
488   BOOST_CHECK_EQUAL(hbox->contentsRect(), SGRecti(5, 10, 142, 46));
489
490   BOOST_CHECK_EQUAL(w1->geometry(), SGRecti(5,   10, 42, 46));
491   BOOST_CHECK_EQUAL(w2->geometry(), SGRecti(52,  10, 44, 46));
492   BOOST_CHECK_EQUAL(w3->geometry(), SGRecti(101, 10, 46, 46));
493
494   BOOST_CHECK_EQUAL(w1->contentsRect(), SGRecti(10,  15, 32, 36));
495   BOOST_CHECK_EQUAL(w2->contentsRect(), SGRecti(58,  16, 32, 34));
496   BOOST_CHECK_EQUAL(w3->contentsRect(), SGRecti(108, 17, 32, 32));
497 }
498
499 //------------------------------------------------------------------------------
500 BOOST_AUTO_TEST_CASE( boxlayout_hfw )
501 {
502   TestWidgetRef w1( new TestWidgetHFW( SGVec2i(16,   16),
503                                        SGVec2i(32,   32) ) ),
504                 w2( new TestWidgetHFW( SGVec2i(24,   24),
505                                        SGVec2i(48,   48) ) );
506
507   BOOST_CHECK_EQUAL(w1->heightForWidth(16), 64);
508   BOOST_CHECK_EQUAL(w1->minimumHeightForWidth(16), 16);
509   BOOST_CHECK_EQUAL(w2->heightForWidth(24), 96);
510   BOOST_CHECK_EQUAL(w2->minimumHeightForWidth(24), 24);
511
512   TestWidgetRef w_no_hfw( new TestWidget( SGVec2i(16,   16),
513                                           SGVec2i(32,   32) ) );
514   BOOST_CHECK(!w_no_hfw->hasHeightForWidth());
515   BOOST_CHECK_EQUAL(w_no_hfw->heightForWidth(16), -1);
516   BOOST_CHECK_EQUAL(w_no_hfw->minimumHeightForWidth(16), -1);
517
518   // horizontal
519   sc::HBoxLayout hbox;
520   hbox.setSpacing(5);
521   hbox.addItem(w1);
522   hbox.addItem(w2);
523
524   BOOST_CHECK_EQUAL(hbox.heightForWidth(45), w2->heightForWidth(24));
525   BOOST_CHECK_EQUAL(hbox.heightForWidth(85), w2->heightForWidth(48));
526
527   hbox.addItem(w_no_hfw);
528
529   BOOST_CHECK_EQUAL(hbox.heightForWidth(66), 96);
530   BOOST_CHECK_EQUAL(hbox.heightForWidth(122), 48);
531   BOOST_CHECK_EQUAL(hbox.minimumHeightForWidth(66), 24);
532   BOOST_CHECK_EQUAL(hbox.minimumHeightForWidth(122), 16);
533
534   hbox.setGeometry(SGRecti(0, 0, 66, 24));
535
536   BOOST_CHECK_EQUAL(w1->geometry(), SGRecti(0, 0,  16, 24));
537   BOOST_CHECK_EQUAL(w2->geometry(), SGRecti(21, 0, 24, 24));
538   BOOST_CHECK_EQUAL(w_no_hfw->geometry(), SGRecti(50, 0, 16, 24));
539
540   // vertical
541   sc::VBoxLayout vbox;
542   vbox.setSpacing(5);
543   vbox.addItem(w1);
544   vbox.addItem(w2);
545
546   BOOST_CHECK_EQUAL(vbox.heightForWidth(24), 143);
547   BOOST_CHECK_EQUAL(vbox.heightForWidth(48), 74);
548   BOOST_CHECK_EQUAL(vbox.minimumHeightForWidth(24), 39);
549   BOOST_CHECK_EQUAL(vbox.minimumHeightForWidth(48), 22);
550
551   vbox.addItem(w_no_hfw);
552
553   BOOST_CHECK_EQUAL(vbox.heightForWidth(24), 180);
554   BOOST_CHECK_EQUAL(vbox.heightForWidth(48), 111);
555   BOOST_CHECK_EQUAL(vbox.minimumHeightForWidth(24), 60);
556   BOOST_CHECK_EQUAL(vbox.minimumHeightForWidth(48), 43);
557
558   SGVec2i min_size = vbox.minimumSize(),
559           size_hint = vbox.sizeHint();
560
561   BOOST_CHECK_EQUAL(min_size, SGVec2i(24, 66));
562   BOOST_CHECK_EQUAL(size_hint, SGVec2i(48, 122));
563
564   vbox.setGeometry(SGRecti(0, 0, 24, 122));
565
566   BOOST_CHECK_EQUAL(w1->geometry(), SGRecti(0, 0,  24, 33));
567   BOOST_CHECK_EQUAL(w2->geometry(), SGRecti(0, 38, 24, 47));
568   BOOST_CHECK_EQUAL(w_no_hfw->geometry(), SGRecti(0, 90, 24, 32));
569
570   // Vertical layouting modifies size hints, so check if they are correctly
571   // restored
572   BOOST_CHECK_EQUAL(min_size, vbox.minimumSize());
573   BOOST_CHECK_EQUAL(size_hint, vbox.sizeHint());
574
575   vbox.setGeometry(SGRecti(0, 0, 50, 122));
576
577   BOOST_CHECK_EQUAL(w1->geometry(), SGRecti(0, 0,  50, 25));
578   BOOST_CHECK_EQUAL(w2->geometry(), SGRecti(0, 30, 50, 51));
579   BOOST_CHECK_EQUAL(w_no_hfw->geometry(), SGRecti(0, 86, 50, 36));
580
581   // Same geometry as before -> should get same widget geometry
582   // (check internal size hint cache updates correctly)
583   vbox.setGeometry(SGRecti(0, 0, 24, 122));
584
585   BOOST_CHECK_EQUAL(w1->geometry(), SGRecti(0, 0,  24, 33));
586   BOOST_CHECK_EQUAL(w2->geometry(), SGRecti(0, 38, 24, 47));
587   BOOST_CHECK_EQUAL(w_no_hfw->geometry(), SGRecti(0, 90, 24, 32));
588 }
589
590 //------------------------------------------------------------------------------
591 // TODO extend to_nasal_helper for automatic argument conversion
592 static naRef f_Widget_visibilityChanged(nasal::CallContext ctx)
593 {
594   sc::NasalWidget* w = ctx.from_nasal<sc::NasalWidget*>(ctx.me);
595
596   if( !ctx.requireArg<bool>(0) )
597     w->setGeometry(SGRecti(0, 0, -1, -1));
598
599   return naNil();
600 }
601
602 //------------------------------------------------------------------------------
603 BOOST_AUTO_TEST_CASE( nasal_widget )
604 {
605   nasal::Context c;
606   nasal::Hash globals = c.newHash();
607
608   nasal::Object::setupGhost();
609   nasal::Ghost<sc::LayoutItemRef>::init("LayoutItem");
610   sc::NasalWidget::setupGhost(globals);
611
612   nasal::Hash me = c.newHash();
613   me.set("visibilityChanged", &f_Widget_visibilityChanged);
614   sc::NasalWidgetRef w( new sc::NasalWidget(me.get_naRef()) );
615
616   // Default layout sizes (no user set values)
617   BOOST_CHECK_EQUAL(w->minimumSize(), SGVec2i(16, 16));
618   BOOST_CHECK_EQUAL(w->sizeHint(),    SGVec2i(32, 32));
619   BOOST_CHECK_EQUAL(w->maximumSize(), sc::LayoutItem::MAX_SIZE);
620
621   // Changed layout sizes
622   w->setLayoutMinimumSize( SGVec2i(2, 12) );
623   w->setLayoutSizeHint(    SGVec2i(3, 13) );
624   w->setLayoutMaximumSize( SGVec2i(4, 14) );
625
626   BOOST_CHECK_EQUAL(w->minimumSize(), SGVec2i(2, 12));
627   BOOST_CHECK_EQUAL(w->sizeHint(),    SGVec2i(3, 13));
628   BOOST_CHECK_EQUAL(w->maximumSize(), SGVec2i(4, 14));
629
630   // User set values (overwrite layout sizes)
631   w->setMinimumSize( SGVec2i(15, 16) );
632   w->setSizeHint(    SGVec2i(17, 18) );
633   w->setMaximumSize( SGVec2i(19, 20) );
634
635   BOOST_CHECK_EQUAL(w->minimumSize(), SGVec2i(15, 16));
636   BOOST_CHECK_EQUAL(w->sizeHint(),    SGVec2i(17, 18));
637   BOOST_CHECK_EQUAL(w->maximumSize(), SGVec2i(19, 20));
638
639   // Only vertical user set values (layout/default for horizontal hints)
640   w->setMinimumSize( SGVec2i(0, 21) );
641   w->setSizeHint(    SGVec2i(0, 22) );
642   w->setMaximumSize( SGVec2i(SGLimits<int>::max(), 23) );
643
644   BOOST_CHECK_EQUAL(w->minimumSize(), SGVec2i(2, 21));
645   BOOST_CHECK_EQUAL(w->sizeHint(),    SGVec2i(3, 22));
646   BOOST_CHECK_EQUAL(w->maximumSize(), SGVec2i(4, 23));
647
648   w->setVisible(false);
649   BOOST_CHECK_EQUAL(w->geometry(), SGRecti(0, 0, -1, -1));
650 }