From e4c903b2ea42fe104ef50d0ea6028f14d8309dfa Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Tue, 8 Feb 2011 13:36:13 +0000 Subject: [PATCH] Fix wxWrapSizer minimal size calculation. Add a unit test checking that wxWrapSizer::CalcMin() returns the expected results. Closes #12464. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@66862 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- docs/changes.txt | 1 + include/wx/sizer.h | 4 ++ include/wx/window.h | 7 +- src/common/wincmn.cpp | 10 +++ tests/Makefile.in | 4 ++ tests/makefile.bcc | 28 ++++---- tests/makefile.gcc | 28 ++++---- tests/makefile.vc | 28 ++++---- tests/makefile.wat | 28 ++++---- tests/sizers/wrapsizer.cpp | 128 +++++++++++++++++++++++++++++++++ tests/test.bkl | 1 + tests/test_test_gui.dsp | 4 ++ tests/test_vc7_test_gui.vcproj | 3 + tests/test_vc8_test_gui.vcproj | 4 ++ tests/test_vc9_test_gui.vcproj | 4 ++ 15 files changed, 228 insertions(+), 54 deletions(-) create mode 100644 tests/sizers/wrapsizer.cpp diff --git a/docs/changes.txt b/docs/changes.txt index 2d77036063..bb79b65387 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -461,6 +461,7 @@ All (GUI): - wxPropertyGrid: Added "HasAlpha" attribute for wxColourProperty. - Added support for saving PNG files with palette (troelsk). - Added support for saving as GIF and animated GIF (troelsk). +- Fix wxWrapSizer minimal size calculation (Catalin Raceanu). GTK: diff --git a/include/wx/sizer.h b/include/wx/sizer.h index 6d414fd09d..39044e31e8 100644 --- a/include/wx/sizer.h +++ b/include/wx/sizer.h @@ -665,6 +665,10 @@ public: m_position = pos; m_size = size; Layout(); + + // This call is required for wxWrapSizer to be able to calculate its + // minimal size correctly. + InformFirstDirection(wxHORIZONTAL, size.x, size.y); } void SetDimension(int x, int y, int width, int height) { SetDimension(wxPoint(x, y), wxSize(width, height)); } diff --git a/include/wx/window.h b/include/wx/window.h index 1527a1e0de..0fd842426a 100644 --- a/include/wx/window.h +++ b/include/wx/window.h @@ -525,12 +525,7 @@ public: // tells the item how much more space there is available in the opposite // direction (-1 if unknown). virtual bool - InformFirstDirection(int WXUNUSED(direction), - int WXUNUSED(size), - int WXUNUSED(availableOtherDir)) - { - return false; - } + InformFirstDirection(int direction, int size, int availableOtherDir); // sends a size event to the window using its current size -- this has an // effect of refreshing the window layout diff --git a/src/common/wincmn.cpp b/src/common/wincmn.cpp index 76aca9086c..2c58ce1ec1 100644 --- a/src/common/wincmn.cpp +++ b/src/common/wincmn.cpp @@ -853,6 +853,16 @@ wxSize wxWindowBase::GetWindowBorderSize() const return size*2; } +bool +wxWindowBase::InformFirstDirection(int direction, + int size, + int availableOtherDir) +{ + return GetSizer() && GetSizer()->InformFirstDirection(direction, + size, + availableOtherDir); +} + wxSize wxWindowBase::GetEffectiveMinSize() const { // merge the best size with the min size, giving priority to the min size diff --git a/tests/Makefile.in b/tests/Makefile.in index 9a1f9abb78..9c57cd3d22 100644 --- a/tests/Makefile.in +++ b/tests/Makefile.in @@ -213,6 +213,7 @@ TEST_GUI_OBJECTS = \ test_gui_settings.o \ test_gui_socket.o \ test_gui_boxsizer.o \ + test_gui_wrapsizer.o \ test_gui_valnum.o \ test_gui_clientsize.o \ test_gui_setsize.o \ @@ -868,6 +869,9 @@ test_gui_socket.o: $(srcdir)/net/socket.cpp $(TEST_GUI_ODEP) test_gui_boxsizer.o: $(srcdir)/sizers/boxsizer.cpp $(TEST_GUI_ODEP) $(CXXC) -c -o $@ $(TEST_GUI_CXXFLAGS) $(srcdir)/sizers/boxsizer.cpp +test_gui_wrapsizer.o: $(srcdir)/sizers/wrapsizer.cpp $(TEST_GUI_ODEP) + $(CXXC) -c -o $@ $(TEST_GUI_CXXFLAGS) $(srcdir)/sizers/wrapsizer.cpp + test_gui_valnum.o: $(srcdir)/validators/valnum.cpp $(TEST_GUI_ODEP) $(CXXC) -c -o $@ $(TEST_GUI_CXXFLAGS) $(srcdir)/validators/valnum.cpp diff --git a/tests/makefile.bcc b/tests/makefile.bcc index 5b9060338d..f6a49b636b 100644 --- a/tests/makefile.bcc +++ b/tests/makefile.bcc @@ -195,12 +195,13 @@ TEST_GUI_OBJECTS = \ $(OBJS)\test_gui_guifuncs.obj \ $(OBJS)\test_gui_selstoretest.obj \ $(OBJS)\test_gui_garbage.obj \ - $(OBJS)\test_gui_settings.obj \ - $(OBJS)\test_gui_socket.obj \ - $(OBJS)\test_gui_boxsizer.obj \ - $(OBJS)\test_gui_valnum.obj \ - $(OBJS)\test_gui_clientsize.obj \ - $(OBJS)\test_gui_setsize.obj \ + $(OBJS)\test_gui_settings.obj \ + $(OBJS)\test_gui_socket.obj \ + $(OBJS)\test_gui_boxsizer.obj \ + $(OBJS)\test_gui_wrapsizer.obj \ + $(OBJS)\test_gui_valnum.obj \ + $(OBJS)\test_gui_clientsize.obj \ + $(OBJS)\test_gui_setsize.obj \ $(OBJS)\test_gui_xrctest.obj ### Conditionally set variables: ### @@ -913,12 +914,15 @@ $(OBJS)\test_gui_settings.obj: .\misc\settings.cpp $(OBJS)\test_gui_socket.obj: .\net\socket.cpp $(CXX) -q -c -P -o$@ $(TEST_GUI_CXXFLAGS) .\net\socket.cpp -$(OBJS)\test_gui_boxsizer.obj: .\sizers\boxsizer.cpp - $(CXX) -q -c -P -o$@ $(TEST_GUI_CXXFLAGS) .\sizers\boxsizer.cpp - -$(OBJS)\test_gui_valnum.obj: .\validators\valnum.cpp - $(CXX) -q -c -P -o$@ $(TEST_GUI_CXXFLAGS) .\validators\valnum.cpp - +$(OBJS)\test_gui_boxsizer.obj: .\sizers\boxsizer.cpp + $(CXX) -q -c -P -o$@ $(TEST_GUI_CXXFLAGS) .\sizers\boxsizer.cpp + +$(OBJS)\test_gui_wrapsizer.obj: .\sizers\wrapsizer.cpp + $(CXX) -q -c -P -o$@ $(TEST_GUI_CXXFLAGS) .\sizers\wrapsizer.cpp + +$(OBJS)\test_gui_valnum.obj: .\validators\valnum.cpp + $(CXX) -q -c -P -o$@ $(TEST_GUI_CXXFLAGS) .\validators\valnum.cpp + $(OBJS)\test_gui_clientsize.obj: .\window\clientsize.cpp $(CXX) -q -c -P -o$@ $(TEST_GUI_CXXFLAGS) .\window\clientsize.cpp diff --git a/tests/makefile.gcc b/tests/makefile.gcc index f7829a341b..52490dd62f 100644 --- a/tests/makefile.gcc +++ b/tests/makefile.gcc @@ -188,12 +188,13 @@ TEST_GUI_OBJECTS = \ $(OBJS)\test_gui_guifuncs.o \ $(OBJS)\test_gui_selstoretest.o \ $(OBJS)\test_gui_garbage.o \ - $(OBJS)\test_gui_settings.o \ - $(OBJS)\test_gui_socket.o \ - $(OBJS)\test_gui_boxsizer.o \ - $(OBJS)\test_gui_valnum.o \ - $(OBJS)\test_gui_clientsize.o \ - $(OBJS)\test_gui_setsize.o \ + $(OBJS)\test_gui_settings.o \ + $(OBJS)\test_gui_socket.o \ + $(OBJS)\test_gui_boxsizer.o \ + $(OBJS)\test_gui_wrapsizer.o \ + $(OBJS)\test_gui_valnum.o \ + $(OBJS)\test_gui_clientsize.o \ + $(OBJS)\test_gui_setsize.o \ $(OBJS)\test_gui_xrctest.o ### Conditionally set variables: ### @@ -894,12 +895,15 @@ $(OBJS)\test_gui_settings.o: ./misc/settings.cpp $(OBJS)\test_gui_socket.o: ./net/socket.cpp $(CXX) -c -o $@ $(TEST_GUI_CXXFLAGS) $(CPPDEPS) $< -$(OBJS)\test_gui_boxsizer.o: ./sizers/boxsizer.cpp - $(CXX) -c -o $@ $(TEST_GUI_CXXFLAGS) $(CPPDEPS) $< - -$(OBJS)\test_gui_valnum.o: ./validators/valnum.cpp - $(CXX) -c -o $@ $(TEST_GUI_CXXFLAGS) $(CPPDEPS) $< - +$(OBJS)\test_gui_boxsizer.o: ./sizers/boxsizer.cpp + $(CXX) -c -o $@ $(TEST_GUI_CXXFLAGS) $(CPPDEPS) $< + +$(OBJS)\test_gui_wrapsizer.o: ./sizers/wrapsizer.cpp + $(CXX) -c -o $@ $(TEST_GUI_CXXFLAGS) $(CPPDEPS) $< + +$(OBJS)\test_gui_valnum.o: ./validators/valnum.cpp + $(CXX) -c -o $@ $(TEST_GUI_CXXFLAGS) $(CPPDEPS) $< + $(OBJS)\test_gui_clientsize.o: ./window/clientsize.cpp $(CXX) -c -o $@ $(TEST_GUI_CXXFLAGS) $(CPPDEPS) $< diff --git a/tests/makefile.vc b/tests/makefile.vc index 2af1e2cfa5..19f365c0ad 100644 --- a/tests/makefile.vc +++ b/tests/makefile.vc @@ -190,12 +190,13 @@ TEST_GUI_OBJECTS = \ $(OBJS)\test_gui_guifuncs.obj \ $(OBJS)\test_gui_selstoretest.obj \ $(OBJS)\test_gui_garbage.obj \ - $(OBJS)\test_gui_settings.obj \ - $(OBJS)\test_gui_socket.obj \ - $(OBJS)\test_gui_boxsizer.obj \ - $(OBJS)\test_gui_valnum.obj \ - $(OBJS)\test_gui_clientsize.obj \ - $(OBJS)\test_gui_setsize.obj \ + $(OBJS)\test_gui_settings.obj \ + $(OBJS)\test_gui_socket.obj \ + $(OBJS)\test_gui_boxsizer.obj \ + $(OBJS)\test_gui_wrapsizer.obj \ + $(OBJS)\test_gui_valnum.obj \ + $(OBJS)\test_gui_clientsize.obj \ + $(OBJS)\test_gui_setsize.obj \ $(OBJS)\test_gui_xrctest.obj TEST_GUI_RESOURCES = \ $(OBJS)\test_gui_sample.res @@ -1039,12 +1040,15 @@ $(OBJS)\test_gui_settings.obj: .\misc\settings.cpp $(OBJS)\test_gui_socket.obj: .\net\socket.cpp $(CXX) /c /nologo /TP /Fo$@ $(TEST_GUI_CXXFLAGS) .\net\socket.cpp -$(OBJS)\test_gui_boxsizer.obj: .\sizers\boxsizer.cpp - $(CXX) /c /nologo /TP /Fo$@ $(TEST_GUI_CXXFLAGS) .\sizers\boxsizer.cpp - -$(OBJS)\test_gui_valnum.obj: .\validators\valnum.cpp - $(CXX) /c /nologo /TP /Fo$@ $(TEST_GUI_CXXFLAGS) .\validators\valnum.cpp - +$(OBJS)\test_gui_boxsizer.obj: .\sizers\boxsizer.cpp + $(CXX) /c /nologo /TP /Fo$@ $(TEST_GUI_CXXFLAGS) .\sizers\boxsizer.cpp + +$(OBJS)\test_gui_wrapsizer.obj: .\sizers\wrapsizer.cpp + $(CXX) /c /nologo /TP /Fo$@ $(TEST_GUI_CXXFLAGS) .\sizers\wrapsizer.cpp + +$(OBJS)\test_gui_valnum.obj: .\validators\valnum.cpp + $(CXX) /c /nologo /TP /Fo$@ $(TEST_GUI_CXXFLAGS) .\validators\valnum.cpp + $(OBJS)\test_gui_clientsize.obj: .\window\clientsize.cpp $(CXX) /c /nologo /TP /Fo$@ $(TEST_GUI_CXXFLAGS) .\window\clientsize.cpp diff --git a/tests/makefile.wat b/tests/makefile.wat index ba5ae182f5..6500836f23 100644 --- a/tests/makefile.wat +++ b/tests/makefile.wat @@ -434,12 +434,13 @@ TEST_GUI_OBJECTS = & $(OBJS)\test_gui_guifuncs.obj & $(OBJS)\test_gui_selstoretest.obj & $(OBJS)\test_gui_garbage.obj & - $(OBJS)\test_gui_settings.obj & - $(OBJS)\test_gui_socket.obj & - $(OBJS)\test_gui_boxsizer.obj & - $(OBJS)\test_gui_valnum.obj & - $(OBJS)\test_gui_clientsize.obj & - $(OBJS)\test_gui_setsize.obj & + $(OBJS)\test_gui_settings.obj & + $(OBJS)\test_gui_socket.obj & + $(OBJS)\test_gui_boxsizer.obj & + $(OBJS)\test_gui_wrapsizer.obj & + $(OBJS)\test_gui_valnum.obj & + $(OBJS)\test_gui_clientsize.obj & + $(OBJS)\test_gui_setsize.obj & $(OBJS)\test_gui_xrctest.obj @@ -953,12 +954,15 @@ $(OBJS)\test_gui_settings.obj : .AUTODEPEND .\misc\settings.cpp $(OBJS)\test_gui_socket.obj : .AUTODEPEND .\net\socket.cpp $(CXX) -bt=nt -zq -fo=$^@ $(TEST_GUI_CXXFLAGS) $< -$(OBJS)\test_gui_boxsizer.obj : .AUTODEPEND .\sizers\boxsizer.cpp - $(CXX) -bt=nt -zq -fo=$^@ $(TEST_GUI_CXXFLAGS) $< - -$(OBJS)\test_gui_valnum.obj : .AUTODEPEND .\validators\valnum.cpp - $(CXX) -bt=nt -zq -fo=$^@ $(TEST_GUI_CXXFLAGS) $< - +$(OBJS)\test_gui_boxsizer.obj : .AUTODEPEND .\sizers\boxsizer.cpp + $(CXX) -bt=nt -zq -fo=$^@ $(TEST_GUI_CXXFLAGS) $< + +$(OBJS)\test_gui_wrapsizer.obj : .AUTODEPEND .\sizers\wrapsizer.cpp + $(CXX) -bt=nt -zq -fo=$^@ $(TEST_GUI_CXXFLAGS) $< + +$(OBJS)\test_gui_valnum.obj : .AUTODEPEND .\validators\valnum.cpp + $(CXX) -bt=nt -zq -fo=$^@ $(TEST_GUI_CXXFLAGS) $< + $(OBJS)\test_gui_clientsize.obj : .AUTODEPEND .\window\clientsize.cpp $(CXX) -bt=nt -zq -fo=$^@ $(TEST_GUI_CXXFLAGS) $< diff --git a/tests/sizers/wrapsizer.cpp b/tests/sizers/wrapsizer.cpp new file mode 100644 index 0000000000..a5d857527d --- /dev/null +++ b/tests/sizers/wrapsizer.cpp @@ -0,0 +1,128 @@ +/////////////////////////////////////////////////////////////////////////////// +// Name: tests/sizers/wrapsizer.cpp +// Purpose: Unit tests for wxWrapSizer +// Author: Catalin Raceanu +// Created: 2010-10-23 +// RCS-ID: $Id:$ +// Copyright: (c) 2010 wxWidgets development team +/////////////////////////////////////////////////////////////////////////////// + +// ---------------------------------------------------------------------------- +// headers +// ---------------------------------------------------------------------------- + +#include "testprec.h" + +#ifdef __BORLANDC__ + #pragma hdrstop +#endif + +#ifndef WX_PRECOMP + #include "wx/app.h" +#endif // WX_PRECOMP + +#include "wx/wrapsizer.h" + +#include "asserthelper.h" + +// ---------------------------------------------------------------------------- +// test class +// ---------------------------------------------------------------------------- + +class WrapSizerTestCase : public CppUnit::TestCase +{ +public: + WrapSizerTestCase() { } + + virtual void setUp(); + virtual void tearDown(); + +private: + CPPUNIT_TEST_SUITE( WrapSizerTestCase ); + CPPUNIT_TEST( CalcMin ); + CPPUNIT_TEST_SUITE_END(); + + void CalcMin(); + + wxWindow *m_win; + wxSizer *m_sizer; + + DECLARE_NO_COPY_CLASS(WrapSizerTestCase) +}; + +// register in the unnamed registry so that these tests are run by default +CPPUNIT_TEST_SUITE_REGISTRATION( WrapSizerTestCase ); + +// also include in it's own registry so that these tests can be run alone +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( WrapSizerTestCase, "WrapSizerTestCase" ); + +// ---------------------------------------------------------------------------- +// test initialization +// ---------------------------------------------------------------------------- + +void WrapSizerTestCase::setUp() +{ + m_win = new wxWindow(wxTheApp->GetTopWindow(), wxID_ANY); + m_win->SetClientSize(180, 240); + + m_sizer = new wxWrapSizer(wxHORIZONTAL); + m_win->SetSizer(m_sizer); +} + +void WrapSizerTestCase::tearDown() +{ + delete m_win; + m_win = NULL; + + m_sizer = NULL; +} + +// ---------------------------------------------------------------------------- +// tests themselves +// ---------------------------------------------------------------------------- + +void WrapSizerTestCase::CalcMin() +{ + const wxSize sizeTotal = m_win->GetClientSize(); + wxSize sizeMinExpected; + + // With a single child the min size must be the same as child size. + const wxSize sizeChild1 = wxSize(sizeTotal.x/2 - 10, sizeTotal.y/4); + sizeMinExpected = sizeChild1; + + wxWindow * const + child1 = new wxWindow(m_win, wxID_ANY, wxDefaultPosition, sizeChild1); + child1->SetBackgroundColour(*wxRED); + m_sizer->Add(child1); + m_win->Layout(); + + CPPUNIT_ASSERT_EQUAL( sizeMinExpected, m_sizer->CalcMin() ); + + // If both children can fit in the same row, the minimal size of the sizer + // is determined by the sum of their minimal horizontal dimensions and + // the maximum of their minimal vertical dimensions. + const wxSize sizeChild2 = wxSize(sizeTotal.x/2 + 10, sizeTotal.y/3); + sizeMinExpected.x += sizeChild2.x; + sizeMinExpected.y = wxMax(sizeChild1.y, sizeChild2.y); + + wxWindow * const + child2 = new wxWindow(m_win, wxID_ANY, wxDefaultPosition, sizeChild2); + child2->SetBackgroundColour(*wxYELLOW); + m_sizer->Add(child2); + m_win->Layout(); + + CPPUNIT_ASSERT_EQUAL( sizeMinExpected, m_sizer->CalcMin() ); + + // Three children will take at least two rows so the minimal size in + // vertical direction must increase. + const wxSize sizeChild3 = wxSize(sizeTotal.x/2, sizeTotal.y/5); + sizeMinExpected.y += sizeChild3.y; + + wxWindow * const + child3 = new wxWindow(m_win, wxID_ANY, wxDefaultPosition, sizeChild3); + child3->SetBackgroundColour(*wxGREEN); + m_sizer->Add(child3); + m_win->Layout(); + + CPPUNIT_ASSERT_EQUAL( sizeMinExpected, m_sizer->CalcMin() ); +} diff --git a/tests/test.bkl b/tests/test.bkl index 33d577ece2..bea939d1ae 100644 --- a/tests/test.bkl +++ b/tests/test.bkl @@ -199,6 +199,7 @@ --> net/socket.cpp sizers/boxsizer.cpp + sizers/wrapsizer.cpp validators/valnum.cpp window/clientsize.cpp window/setsize.cpp diff --git a/tests/test_test_gui.dsp b/tests/test_test_gui.dsp index 9d31a564d0..382ecd8890 100644 --- a/tests/test_test_gui.dsp +++ b/tests/test_test_gui.dsp @@ -529,6 +529,10 @@ SOURCE=.\controls\windowtest.cpp # End Source File # Begin Source File +SOURCE=.\sizers\wrapsizer.cpp +# End Source File +# Begin Source File + SOURCE=.\xml\xrctest.cpp # End Source File # End Group diff --git a/tests/test_vc7_test_gui.vcproj b/tests/test_vc7_test_gui.vcproj index 9f23cd81e4..d2bd4c2d73 100644 --- a/tests/test_vc7_test_gui.vcproj +++ b/tests/test_vc7_test_gui.vcproj @@ -830,6 +830,9 @@ RelativePath=".\controls\windowtest.cpp"> + + diff --git a/tests/test_vc8_test_gui.vcproj b/tests/test_vc8_test_gui.vcproj index d4d9c62f0f..8099a9a808 100644 --- a/tests/test_vc8_test_gui.vcproj +++ b/tests/test_vc8_test_gui.vcproj @@ -1180,6 +1180,10 @@ > + + diff --git a/tests/test_vc9_test_gui.vcproj b/tests/test_vc9_test_gui.vcproj index f349958217..c11080f593 100644 --- a/tests/test_vc9_test_gui.vcproj +++ b/tests/test_vc9_test_gui.vcproj @@ -1152,6 +1152,10 @@ > + + -- 2.45.2