From 7c3d540c7073f4e14df950f09f76309746bb7a06 Mon Sep 17 00:00:00 2001 From: Artur Wieczorek Date: Thu, 11 Jun 2020 15:28:38 +0200 Subject: [PATCH] Validate input in generic wxSpinCtrl and wxSpinCtrlDouble Use respective validator to control what is typed in the text field. Closes #17882. --- include/wx/generic/spinctlg.h | 4 ++++ src/generic/spinctlg.cpp | 37 +++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/include/wx/generic/spinctlg.h b/include/wx/generic/spinctlg.h index a252dc0c14..b0d458ffda 100644 --- a/include/wx/generic/spinctlg.h +++ b/include/wx/generic/spinctlg.h @@ -147,6 +147,8 @@ protected: // ensure that the value is in range wrapping it round if necessary double AdjustToFitInRange(double value) const; + // Assign validator with current parameters + virtual void ResetTextValidator() = 0; double m_value; double m_min; @@ -331,6 +333,7 @@ protected: virtual bool DoTextToValue(const wxString& text, double *val); virtual wxString DoValueToText(double val); + virtual void ResetTextValidator() wxOVERRIDE; private: // Common part of all ctors. @@ -411,6 +414,7 @@ protected: virtual bool DoTextToValue(const wxString& text, double *val) wxOVERRIDE; virtual wxString DoValueToText(double val) wxOVERRIDE; + virtual void ResetTextValidator() wxOVERRIDE; void DetermineDigits(double inc); unsigned m_digits; diff --git a/src/generic/spinctlg.cpp b/src/generic/spinctlg.cpp index 438ed14da8..848a8bb8f7 100644 --- a/src/generic/spinctlg.cpp +++ b/src/generic/spinctlg.cpp @@ -44,6 +44,9 @@ wxIMPLEMENT_DYNAMIC_CLASS(wxSpinDoubleEvent, wxNotifyEvent); #if wxUSE_SPINBTN +#include "wx/valnum.h" +#include "wx/valtext.h" + // ---------------------------------------------------------------------------- // constants // ---------------------------------------------------------------------------- @@ -250,6 +253,8 @@ bool wxSpinCtrlGenericBase::Create(wxWindow *parent, m_spinButton->SetToolTip(GetToolTipText()); #endif // wxUSE_TOOLTIPS + ResetTextValidator(); + m_spin_value = m_spinButton->GetValue(); SetInitialSize(size); @@ -593,6 +598,8 @@ void wxSpinCtrlGenericBase::DoSetRange(double min, double max) m_max = max; if ( m_value > m_max ) DoSetValue(m_max, SendEvent_None); + + ResetTextValidator(); } void wxSpinCtrlGenericBase::DoSetIncrement(double inc) @@ -636,6 +643,8 @@ bool wxSpinCtrl::SetBase(int base) m_base = base; + ResetTextValidator(); + // ... but DoValueToText() after doing it. if ( hasValidVal ) m_textCtrl->ChangeValue(DoValueToText(val)); @@ -679,6 +688,24 @@ wxString wxSpinCtrl::DoValueToText(double val) } } +void wxSpinCtrl::ResetTextValidator() +{ +#if wxUSE_VALIDATORS + if ( GetBase() == 10 ) + { + wxIntegerValidator 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 //----------------------------------------------------------------------------- @@ -719,11 +746,21 @@ void wxSpinCtrlDouble::SetDigits(unsigned digits) m_format.Printf(wxT("%%0.%ulf"), digits); + ResetTextValidator(); m_textCtrl->InvalidateBestSize(); DoSetValue(m_value, SendEvent_None); } +void wxSpinCtrlDouble::ResetTextValidator() +{ +#if wxUSE_VALIDATORS + wxFloatingPointValidator validator(m_digits); + validator.SetRange(m_min, m_max); + m_textCtrl->SetValidator(validator); +#endif // wxUSE_VALIDATORS +} + void wxSpinCtrlDouble::DetermineDigits(double inc) { inc = fabs(inc);