Validate input in generic wxSpinCtrl and wxSpinCtrlDouble
Use respective validator to control what is typed in the text field. Closes #17882.
This commit is contained in:
@@ -147,6 +147,8 @@ protected:
|
|||||||
// 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;
|
||||||
|
|
||||||
|
// Assign validator with current parameters
|
||||||
|
virtual void ResetTextValidator() = 0;
|
||||||
|
|
||||||
double m_value;
|
double m_value;
|
||||||
double m_min;
|
double m_min;
|
||||||
@@ -331,6 +333,7 @@ protected:
|
|||||||
|
|
||||||
virtual bool DoTextToValue(const wxString& text, double *val);
|
virtual bool DoTextToValue(const wxString& text, double *val);
|
||||||
virtual wxString DoValueToText(double val);
|
virtual wxString DoValueToText(double val);
|
||||||
|
virtual void ResetTextValidator() wxOVERRIDE;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// Common part of all ctors.
|
// Common part of all ctors.
|
||||||
@@ -411,6 +414,7 @@ protected:
|
|||||||
|
|
||||||
virtual bool DoTextToValue(const wxString& text, double *val) wxOVERRIDE;
|
virtual bool DoTextToValue(const wxString& text, double *val) wxOVERRIDE;
|
||||||
virtual wxString DoValueToText(double val) wxOVERRIDE;
|
virtual wxString DoValueToText(double val) wxOVERRIDE;
|
||||||
|
virtual void ResetTextValidator() wxOVERRIDE;
|
||||||
void DetermineDigits(double inc);
|
void DetermineDigits(double inc);
|
||||||
|
|
||||||
unsigned m_digits;
|
unsigned m_digits;
|
||||||
|
@@ -44,6 +44,9 @@ wxIMPLEMENT_DYNAMIC_CLASS(wxSpinDoubleEvent, wxNotifyEvent);
|
|||||||
|
|
||||||
#if wxUSE_SPINBTN
|
#if wxUSE_SPINBTN
|
||||||
|
|
||||||
|
#include "wx/valnum.h"
|
||||||
|
#include "wx/valtext.h"
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
// constants
|
// constants
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
@@ -250,6 +253,8 @@ bool wxSpinCtrlGenericBase::Create(wxWindow *parent,
|
|||||||
m_spinButton->SetToolTip(GetToolTipText());
|
m_spinButton->SetToolTip(GetToolTipText());
|
||||||
#endif // wxUSE_TOOLTIPS
|
#endif // wxUSE_TOOLTIPS
|
||||||
|
|
||||||
|
ResetTextValidator();
|
||||||
|
|
||||||
m_spin_value = m_spinButton->GetValue();
|
m_spin_value = m_spinButton->GetValue();
|
||||||
|
|
||||||
SetInitialSize(size);
|
SetInitialSize(size);
|
||||||
@@ -593,6 +598,8 @@ void wxSpinCtrlGenericBase::DoSetRange(double min, double max)
|
|||||||
m_max = max;
|
m_max = max;
|
||||||
if ( m_value > m_max )
|
if ( m_value > m_max )
|
||||||
DoSetValue(m_max, SendEvent_None);
|
DoSetValue(m_max, SendEvent_None);
|
||||||
|
|
||||||
|
ResetTextValidator();
|
||||||
}
|
}
|
||||||
|
|
||||||
void wxSpinCtrlGenericBase::DoSetIncrement(double inc)
|
void wxSpinCtrlGenericBase::DoSetIncrement(double inc)
|
||||||
@@ -636,6 +643,8 @@ bool wxSpinCtrl::SetBase(int base)
|
|||||||
|
|
||||||
m_base = base;
|
m_base = base;
|
||||||
|
|
||||||
|
ResetTextValidator();
|
||||||
|
|
||||||
// ... but DoValueToText() after doing it.
|
// ... but DoValueToText() after doing it.
|
||||||
if ( hasValidVal )
|
if ( hasValidVal )
|
||||||
m_textCtrl->ChangeValue(DoValueToText(val));
|
m_textCtrl->ChangeValue(DoValueToText(val));
|
||||||
@@ -679,6 +688,24 @@ wxString wxSpinCtrl::DoValueToText(double val)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void wxSpinCtrl::ResetTextValidator()
|
||||||
|
{
|
||||||
|
#if wxUSE_VALIDATORS
|
||||||
|
if ( GetBase() == 10 )
|
||||||
|
{
|
||||||
|
wxIntegerValidator<int> validator;
|
||||||
|
validator.SetRange(GetMin(), GetMax());
|
||||||
|
m_textCtrl->SetValidator(validator);
|
||||||
|
}
|
||||||
|
else // == 16
|
||||||
|
{
|
||||||
|
wxTextValidator validator(wxFILTER_XDIGITS);
|
||||||
|
m_textCtrl->SetValidator(validator);
|
||||||
|
|
||||||
|
}
|
||||||
|
#endif // wxUSE_VALIDATORS
|
||||||
|
}
|
||||||
|
|
||||||
#endif // !wxHAS_NATIVE_SPINCTRL
|
#endif // !wxHAS_NATIVE_SPINCTRL
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
@@ -719,11 +746,21 @@ void wxSpinCtrlDouble::SetDigits(unsigned digits)
|
|||||||
|
|
||||||
m_format.Printf(wxT("%%0.%ulf"), digits);
|
m_format.Printf(wxT("%%0.%ulf"), digits);
|
||||||
|
|
||||||
|
ResetTextValidator();
|
||||||
m_textCtrl->InvalidateBestSize();
|
m_textCtrl->InvalidateBestSize();
|
||||||
|
|
||||||
DoSetValue(m_value, SendEvent_None);
|
DoSetValue(m_value, SendEvent_None);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void wxSpinCtrlDouble::ResetTextValidator()
|
||||||
|
{
|
||||||
|
#if wxUSE_VALIDATORS
|
||||||
|
wxFloatingPointValidator<double> validator(m_digits);
|
||||||
|
validator.SetRange(m_min, m_max);
|
||||||
|
m_textCtrl->SetValidator(validator);
|
||||||
|
#endif // wxUSE_VALIDATORS
|
||||||
|
}
|
||||||
|
|
||||||
void wxSpinCtrlDouble::DetermineDigits(double inc)
|
void wxSpinCtrlDouble::DetermineDigits(double inc)
|
||||||
{
|
{
|
||||||
inc = fabs(inc);
|
inc = fabs(inc);
|
||||||
|
Reference in New Issue
Block a user