From 026767c6aee4c61b904fe68b79af186258fd6005 Mon Sep 17 00:00:00 2001 From: Jaakko Salli Date: Fri, 15 Oct 2010 15:15:27 +0000 Subject: [PATCH] Added wxNumericPropertyValidator, which is a custom wxTextValidator with more accurate filtering of inappropriate input for wxIntProperty, wxFloatProperty and wxUIntProperty (fixes #12563). git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@65806 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/propgrid/props.h | 25 +++++++++ src/propgrid/props.cpp | 109 ++++++++++++++++++++++++++++++++++-- 2 files changed, 130 insertions(+), 4 deletions(-) diff --git a/include/wx/propgrid/props.h b/include/wx/propgrid/props.h index 7ea887e3fe..84f7d5191c 100644 --- a/include/wx/propgrid/props.h +++ b/include/wx/propgrid/props.h @@ -143,6 +143,29 @@ enum wxPGNumericValidationConstants // ----------------------------------------------------------------------- +#if wxUSE_VALIDATORS + +/** + A more comprehensive numeric validator class. +*/ +class wxNumericPropertyValidator : public wxTextValidator +{ +public: + enum NumericType + { + Signed = 0, + Unsigned, + Float + }; + + wxNumericPropertyValidator( NumericType numericType, int base = 10 ); + virtual ~wxNumericPropertyValidator() { } + virtual bool Validate(wxWindow* parent); +}; + +#endif // wxUSE_VALIDATORS + + /** @class wxIntProperty @ingroup classes Basic property with integer value. @@ -257,6 +280,7 @@ public: virtual bool DoSetAttribute( const wxString& name, wxVariant& value ); virtual bool ValidateValue( wxVariant& value, wxPGValidationInfo& validationInfo ) const; + virtual wxValidator* DoGetValidator () const; virtual bool IntToValue( wxVariant& variant, int number, int argFlags = 0 ) const; @@ -302,6 +326,7 @@ public: wxPGValidationInfo* pValidationInfo, int mode = wxPG_PROPERTY_VALIDATION_ERROR_MESSAGE ); + static wxValidator* GetClassValidator(); virtual wxValidator* DoGetValidator () const; protected: diff --git a/src/propgrid/props.cpp b/src/propgrid/props.cpp index cdb43bffdf..f8d0857e7c 100644 --- a/src/propgrid/props.cpp +++ b/src/propgrid/props.cpp @@ -142,6 +142,78 @@ bool wxStringProperty::DoSetAttribute( const wxString& name, wxVariant& value ) return true; } +// ----------------------------------------------------------------------- +// wxNumericPropertyValidator +// ----------------------------------------------------------------------- + +#if wxUSE_VALIDATORS + +wxNumericPropertyValidator:: + wxNumericPropertyValidator( NumericType numericType, int base ) + : wxTextValidator(wxFILTER_INCLUDE_CHAR_LIST) +{ + wxArrayString arr; + arr.Add(wxS("0")); + arr.Add(wxS("1")); + arr.Add(wxS("2")); + arr.Add(wxS("3")); + arr.Add(wxS("4")); + arr.Add(wxS("5")); + arr.Add(wxS("6")); + arr.Add(wxS("7")); + + if ( base >= 10 ) + { + arr.Add(wxS("8")); + arr.Add(wxS("9")); + if ( base >= 16 ) + { + arr.Add(wxS("a")); arr.Add(wxS("A")); + arr.Add(wxS("b")); arr.Add(wxS("B")); + arr.Add(wxS("c")); arr.Add(wxS("C")); + arr.Add(wxS("d")); arr.Add(wxS("D")); + arr.Add(wxS("e")); arr.Add(wxS("E")); + arr.Add(wxS("f")); arr.Add(wxS("F")); + } + } + + if ( numericType == Signed ) + { + arr.Add(wxS("+")); + arr.Add(wxS("-")); + } + else if ( numericType == Float ) + { + arr.Add(wxS("+")); + arr.Add(wxS("-")); + arr.Add(wxS(".")); + arr.Add(wxS("e")); + } + + SetIncludes(arr); +} + +bool wxNumericPropertyValidator::Validate(wxWindow* parent) +{ + if ( !wxTextValidator::Validate(parent) ) + return false; + + wxWindow* wnd = GetWindow(); + if ( !wnd->IsKindOf(CLASSINFO(wxTextCtrl)) ) + return true; + + // Do not allow zero-length string + wxTextCtrl* tc = static_cast(wnd); + wxString text = tc->GetValue(); + + if ( !text.length() ) + return false; + + return true; +} + +#endif // wxUSE_VALIDATORS + // ----------------------------------------------------------------------- // wxIntProperty // ----------------------------------------------------------------------- @@ -376,9 +448,8 @@ wxValidator* wxIntProperty::GetClassValidator() #if wxUSE_VALIDATORS WX_PG_DOGETVALIDATOR_ENTRY() - // Atleast wxPython 2.6.2.1 required that the string argument is given - static wxString v; - wxTextValidator* validator = new wxTextValidator(wxFILTER_NUMERIC,&v); + wxValidator* validator = new wxNumericPropertyValidator( + wxNumericPropertyValidator::Signed); WX_PG_DOGETVALIDATOR_EXIT(validator) #else @@ -531,6 +602,21 @@ bool wxUIntProperty::ValidateValue( wxVariant& value, wxPGValidationInfo& valida wxS("%llu")); } +wxValidator* wxUIntProperty::DoGetValidator() const +{ +#if wxUSE_VALIDATORS + WX_PG_DOGETVALIDATOR_ENTRY() + + wxValidator* validator = new wxNumericPropertyValidator( + wxNumericPropertyValidator::Unsigned, + m_realBase); + + WX_PG_DOGETVALIDATOR_EXIT(validator) +#else + return NULL; +#endif +} + bool wxUIntProperty::DoSetAttribute( const wxString& name, wxVariant& value ) { if ( name == wxPG_UINT_BASE ) @@ -701,9 +787,24 @@ bool wxFloatProperty::DoSetAttribute( const wxString& name, wxVariant& value ) return false; } +wxValidator* +wxFloatProperty::GetClassValidator() +{ +#if wxUSE_VALIDATORS + WX_PG_DOGETVALIDATOR_ENTRY() + + wxValidator* validator = new wxNumericPropertyValidator( + wxNumericPropertyValidator::Float); + + WX_PG_DOGETVALIDATOR_EXIT(validator) +#else + return NULL; +#endif +} + wxValidator* wxFloatProperty::DoGetValidator() const { - return wxIntProperty::GetClassValidator(); + return GetClassValidator(); } // ----------------------------------------------------------------------- -- 2.47.2