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