From 9130f8bfc65e3453b0081640d1c1efeb24e6bb1b Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Mon, 25 Jan 2021 12:58:04 +0100 Subject: [PATCH] Don't change orthogonal alignment in wxSizerFlags::CenterXXX() It was unexpected that using wxSizerFlags().Right().CentreVertical() didn't right-align the item because CentreVertical() reset the alignment in the horizontal direction rather than just setting it in the vertical one, so change this, document the new behaviour explicitly and add a new unit test checking for it. Closes #18989. --- include/wx/sizer.h | 18 +++++++++++++++--- interface/wx/sizer.h | 6 ++++++ tests/sizers/gridsizer.cpp | 25 ++++++++++++++++++++++++- 3 files changed, 45 insertions(+), 4 deletions(-) diff --git a/include/wx/sizer.h b/include/wx/sizer.h index dd15744a64..c1f234734f 100644 --- a/include/wx/sizer.h +++ b/include/wx/sizer.h @@ -69,14 +69,26 @@ public: return *this; } - // some shortcuts for Align() + // this is just a shortcut for Align() wxSizerFlags& Centre() { return Align(wxALIGN_CENTRE); } wxSizerFlags& Center() { return Centre(); } - wxSizerFlags& CentreVertical() { return Align(wxALIGN_CENTRE_VERTICAL); } + // but all the remaining methods turn on the corresponding alignment flag + // without affecting the existing ones + wxSizerFlags& CentreVertical() + { + m_flags = (m_flags & ~wxALIGN_BOTTOM) | wxALIGN_CENTRE_VERTICAL; + return *this; + } + wxSizerFlags& CenterVertical() { return CentreVertical(); } - wxSizerFlags& CentreHorizontal() { return Align(wxALIGN_CENTRE_HORIZONTAL); } + wxSizerFlags& CentreHorizontal() + { + m_flags = (m_flags & ~wxALIGN_RIGHT) | wxALIGN_CENTRE_HORIZONTAL; + return *this; + } + wxSizerFlags& CenterHorizontal() { return CentreHorizontal(); } wxSizerFlags& Top() diff --git a/interface/wx/sizer.h b/interface/wx/sizer.h index ad85a0ccad..9e2e9b6a82 100644 --- a/interface/wx/sizer.h +++ b/interface/wx/sizer.h @@ -1455,6 +1455,9 @@ public: them anyhow. For 2D sizers, centering an item in one direction is quite different from centering it in both directions however. + Note that, unlike Align(), this method doesn't change the vertical + alignment. + @see CentreVertical() @since 3.1.0 @@ -1467,6 +1470,9 @@ public: The remarks in CentreHorizontal() documentation also apply to this function. + Note that, unlike Align(), this method doesn't change the horizontal + alignment. + @since 3.1.0 */ wxSizerFlags& CentreVertical(); diff --git a/tests/sizers/gridsizer.cpp b/tests/sizers/gridsizer.cpp index fc3c130f03..492bf9aab8 100644 --- a/tests/sizers/gridsizer.cpp +++ b/tests/sizers/gridsizer.cpp @@ -85,7 +85,7 @@ void GridSizerTestCase::SetChildren(const wxVector& children, // ---------------------------------------------------------------------------- TEST_CASE_METHOD(GridSizerTestCase, - "wxGridSizer::Expand", + "wxGridSizer::Layout", "[grid-sizer][sizer]") { const wxSize sizeTotal = m_win->GetClientSize(); @@ -144,6 +144,29 @@ TEST_CASE_METHOD(GridSizerTestCase, CHECK( children[2]->GetSize() == wxSize(sizeChild.x, sizeRest.y) ); CHECK( children[3]->GetSize() == wxSize(sizeChild.x, sizeRest.y) ); } + + // Test alignment flag too. + SECTION("Right align") + { + SetChildren(children, wxSizerFlags().Right()); + CHECK( children[0]->GetPosition() == wxPoint( 0, 0) ); + CHECK( children[1]->GetPosition() == wxPoint(sizeRest.x, 0) ); + CHECK( children[2]->GetPosition() == wxPoint( 0, sizeChild.y) ); + CHECK( children[3]->GetPosition() == wxPoint(sizeRest.x, sizeChild.y) ); + } + + // Also test combining centering in one direction and aligning in another. + SECTION("Right align and center vertically") + { + SetChildren(children, wxSizerFlags().Right().CentreVertical()); + + const int yMid = sizeChild.y + (sizeRest.y - sizeChild.y) / 2; + + CHECK( children[0]->GetPosition() == wxPoint( 0, 0) ); + CHECK( children[1]->GetPosition() == wxPoint(sizeRest.x, 0) ); + CHECK( children[2]->GetPosition() == wxPoint( 0, yMid) ); + CHECK( children[3]->GetPosition() == wxPoint(sizeRest.x, yMid) ); + } } TEST_CASE_METHOD(GridSizerTestCase,