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.
This commit is contained in:
Vadim Zeitlin 2021-08-17 22:05:49 +02:00
parent b66656fbec
commit ac1fa83c20
3 changed files with 31 additions and 7 deletions

View File

@ -145,6 +145,9 @@ protected:
// check if the value is in range // check if the value is in range
bool InRange(double n) const { return (n >= m_min) && (n <= m_max); } 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 // ensure that the value is in range wrapping it round if necessary
double AdjustToFitInRange(double value) const; double AdjustToFitInRange(double value) const;

View File

@ -222,11 +222,14 @@ bool wxSpinCtrlGenericBase::Create(wxWindow *parent,
return false; return false;
} }
m_value = initial; m_min = min;
m_min = min; m_max = max;
m_max = max;
m_increment = increment; 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 // the string value overrides the numeric one (for backwards compatibility
// reasons and also because it is simpler to specify the string value which // 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 // comes much sooner in the list of arguments and leave the initial
@ -235,7 +238,7 @@ bool wxSpinCtrlGenericBase::Create(wxWindow *parent,
{ {
double d; double d;
if ( DoTextToValue(value, &d) ) if ( DoTextToValue(value, &d) )
m_value = d; m_value = AdjustAndSnap(d);
} }
m_textCtrl = new wxSpinCtrlTextGeneric(this, DoValueToText(m_value), style); 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 ) if ( val < m_min )
val = m_min; val = m_min;
if ( val > m_max ) 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)); wxString str(DoValueToText(val));
if ((val != m_value) || (str != m_textCtrl->GetValue())) if ((val != m_value) || (str != m_textCtrl->GetValue()))

View File

@ -124,6 +124,17 @@ TEST_CASE_METHOD(SpinCtrlTestCase1, "SpinCtrl::Init4", "[spinctrl]")
CHECK(m_spin->GetValue() == 99); 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]") TEST_CASE_METHOD(SpinCtrlTestCase1, "SpinCtrl::NoEventsInCtor", "[spinctrl]")
{ {
// Verify that creating the control does not generate any events. This is // Verify that creating the control does not generate any events. This is