From ac1fa83c200a126c452c06cd6d1e6289b4aaa380 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Tue, 17 Aug 2021 22:05:49 +0200 Subject: [PATCH] Ensure initial value of generic wxSpinCtrl is always valid Under MSW creating a wxSpinCtrl with a range of, say, 1..10 and the default initial value of 0 sets its initial value to 1 (i.e. the closest valid value) as expected, but the generic version still set it to the invalid value of 0, which was unexpected, inconsistent and not useful. Fix the generic version to follow MSW behaviour now and add a test checking for this. --- include/wx/generic/spinctlg.h | 3 +++ src/generic/spinctlg.cpp | 24 +++++++++++++++++------- tests/controls/spinctrltest.cpp | 11 +++++++++++ 3 files changed, 31 insertions(+), 7 deletions(-) diff --git a/include/wx/generic/spinctlg.h b/include/wx/generic/spinctlg.h index 64d9cb95c5..db7162751a 100644 --- a/include/wx/generic/spinctlg.h +++ b/include/wx/generic/spinctlg.h @@ -145,6 +145,9 @@ protected: // check if the value is in range bool InRange(double n) const { return (n >= m_min) && (n <= m_max); } + // adjust the value to fit the range and snap it to ticks if necessary + double AdjustAndSnap(double value) const; + // ensure that the value is in range wrapping it round if necessary double AdjustToFitInRange(double value) const; diff --git a/src/generic/spinctlg.cpp b/src/generic/spinctlg.cpp index ae47212210..7e87d1937a 100644 --- a/src/generic/spinctlg.cpp +++ b/src/generic/spinctlg.cpp @@ -222,11 +222,14 @@ bool wxSpinCtrlGenericBase::Create(wxWindow *parent, return false; } - m_value = initial; - m_min = min; - m_max = max; + m_min = min; + m_max = max; m_increment = increment; + // Note that AdjustAndSnap() uses the variables set above, so only call it + // after assigning the values to them. + m_value = AdjustAndSnap(initial); + // the string value overrides the numeric one (for backwards compatibility // reasons and also because it is simpler to specify the string value which // comes much sooner in the list of arguments and leave the initial @@ -235,7 +238,7 @@ bool wxSpinCtrlGenericBase::Create(wxWindow *parent, { double d; if ( DoTextToValue(value, &d) ) - m_value = d; + m_value = AdjustAndSnap(d); } m_textCtrl = new wxSpinCtrlTextGeneric(this, DoValueToText(m_value), style); @@ -529,10 +532,8 @@ void wxSpinCtrlGenericBase::SetValue(const wxString& text) } } -bool wxSpinCtrlGenericBase::DoSetValue(double val, SendEvent sendEvent) +double wxSpinCtrlGenericBase::AdjustAndSnap(double val) const { - wxCHECK_MSG( m_textCtrl, false, wxT("invalid call to wxSpinCtrl::SetValue") ); - if ( val < m_min ) val = m_min; if ( val > m_max ) @@ -551,6 +552,15 @@ bool wxSpinCtrlGenericBase::DoSetValue(double val, SendEvent sendEvent) } } + return val; +} + +bool wxSpinCtrlGenericBase::DoSetValue(double val, SendEvent sendEvent) +{ + wxCHECK_MSG( m_textCtrl, false, wxT("invalid call to wxSpinCtrl::SetValue") ); + + val = AdjustAndSnap(val); + wxString str(DoValueToText(val)); if ((val != m_value) || (str != m_textCtrl->GetValue())) diff --git a/tests/controls/spinctrltest.cpp b/tests/controls/spinctrltest.cpp index fa2662deee..e7262e83ed 100644 --- a/tests/controls/spinctrltest.cpp +++ b/tests/controls/spinctrltest.cpp @@ -124,6 +124,17 @@ TEST_CASE_METHOD(SpinCtrlTestCase1, "SpinCtrl::Init4", "[spinctrl]") CHECK(m_spin->GetValue() == 99); } +TEST_CASE_METHOD(SpinCtrlTestCase1, "SpinCtrl::InitOutOfRange", "[spinctrl]") +{ + m_spin->Create(wxTheApp->GetTopWindow(), wxID_ANY, "", + wxDefaultPosition, wxDefaultSize, 0, + 10, 20, 0); + + // Recreate the control with another "initial" outside of the valid range: + // it shouldn't be taken into account. + CHECK(m_spin->GetValue() == 10); +} + TEST_CASE_METHOD(SpinCtrlTestCase1, "SpinCtrl::NoEventsInCtor", "[spinctrl]") { // Verify that creating the control does not generate any events. This is