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

@@ -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()))