From 10b0f4894bcfc158dc406700365887da74ddbf15 Mon Sep 17 00:00:00 2001 From: Francesco Montorsi Date: Fri, 30 Jan 2009 17:22:05 +0000 Subject: [PATCH] cleanup code and reorganize it to reuse the same switch() for both OnChar() and Validate() git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@58524 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/valtext.h | 18 +++-- src/common/valtext.cpp | 171 ++++++++++++++++++++--------------------- 2 files changed, 97 insertions(+), 92 deletions(-) diff --git a/include/wx/valtext.h b/include/wx/valtext.h index 8aa0ea295c..1408a09eff 100644 --- a/include/wx/valtext.h +++ b/include/wx/valtext.h @@ -2,7 +2,7 @@ // Name: valtext.h // Purpose: wxTextValidator class // Author: Julian Smart -// Modified by: +// Modified by: Francesco Montorsi // Created: 29/01/98 // RCS-ID: $Id$ // Copyright: (c) 1998 Julian Smart @@ -61,6 +61,9 @@ public: // Called to transfer data from the window virtual bool TransferFromWindow(); + // Filter keystrokes + void OnChar(wxKeyEvent& event); + // ACCESSORS inline wxTextValidatorStyle GetStyle() const { return m_validatorStyle; } inline void SetStyle(wxTextValidatorStyle style) { m_validatorStyle = style; } @@ -76,11 +79,16 @@ public: void SetExcludes(const wxArrayString& excludes) { m_excludes = excludes; } inline wxArrayString& GetExcludes() { return m_excludes; } - bool IsInCharIncludes(const wxString& val); - bool IsNotInCharExcludes(const wxString& val); +protected: + + // returns true if all characters of the given string are present in m_includes + bool IsInCharIncludes(const wxString& val) const; - // Filter keystrokes - void OnChar(wxKeyEvent& event); + // returns true if all characters of the given string are NOT present in m_excludes + bool IsNotInCharExcludes(const wxString& val) const; + + // returns true if the contents of 'val' are valid for the current validation style + bool IsValid(const wxString& val, wxString* errormsg) const; protected: wxTextValidatorStyle m_validatorStyle; diff --git a/src/common/valtext.cpp b/src/common/valtext.cpp index a2f5c621ef..d150671737 100644 --- a/src/common/valtext.cpp +++ b/src/common/valtext.cpp @@ -2,7 +2,7 @@ // Name: src/common/valtext.cpp // Purpose: wxTextValidator // Author: Julian Smart -// Modified by: +// Modified by: Francesco Montorsi // Created: 04/01/98 // RCS-ID: $Id$ // Copyright: (c) Julian Smart @@ -66,7 +66,7 @@ static bool wxIsNumeric(const wxString& val) { // Allow for "," (French) as well as "." -- in future we should // use wxSystemSettings or other to do better localisation - if ((!wxIsdigit(val[i])) && (val[i] != wxT('.')) && (val[i] != wxT(',')) && (val[i] != wxT('e')) && + if ((!wxIsdigit(val[i])) && (val[i] != wxT('.')) && (val[i] != wxT(',')) && (val[i] != wxT('e')) && (val[i] != wxT('E')) && (val[i] != wxT('+')) && (val[i] != wxT('-'))) return false; } @@ -101,12 +101,6 @@ wxTextValidator::wxTextValidator(wxTextValidatorStyle style, wxString *val) { m_validatorStyle = style; m_stringValue = val; -/* - m_refData = new wxVTextRefData; - - M_VTEXTDATA->m_validatorStyle = style; - M_VTEXTDATA->m_stringValue = val; -*/ } wxTextValidator::wxTextValidator(const wxTextValidator& val) @@ -167,7 +161,57 @@ bool wxTextValidator::Validate(wxWindow *parent) // NB: this format string should always contain exactly one '%s' wxString errormsg; + if (!IsValid(val, &errormsg)) + { + wxASSERT(!errormsg.empty()); + + m_validatorWindow->SetFocus(); + + wxString buf; + buf.Printf(errormsg, val.c_str()); + + wxMessageBox(buf, _("Validation conflict"), + wxOK | wxICON_EXCLAMATION, parent); + + return false; + } + + return true; +} + +// Called to transfer data to the window +bool wxTextValidator::TransferToWindow() +{ + if ( m_stringValue ) + { + wxTextEntry * const text = GetTextEntry(); + if ( !text ) + return false; + text->SetValue(*m_stringValue); + } + + return true; +} + +// Called to transfer data to the window +bool wxTextValidator::TransferFromWindow() +{ + if ( m_stringValue ) + { + wxTextEntry * const text = GetTextEntry(); + if ( !text ) + return false; + + *m_stringValue = text->GetValue(); + } + + return true; +} + +bool wxTextValidator::IsValid(const wxString& val, wxString* pErr) const +{ + wxString errormsg; switch (m_validatorStyle) { case wxFILTER_NONE: @@ -218,109 +262,62 @@ bool wxTextValidator::Validate(wxWindow *parent) wxFAIL_MSG("invalid text validator style"); } - if ( !errormsg.empty() ) - { - m_validatorWindow->SetFocus(); - - wxString buf; - buf.Printf(errormsg, val.c_str()); - - wxMessageBox(buf, _("Validation conflict"), - wxOK | wxICON_EXCLAMATION, parent); - - return false; - } + if (pErr) + *pErr = errormsg; - return true; + return errormsg.empty(); } -// Called to transfer data to the window -bool wxTextValidator::TransferToWindow(void) +bool wxTextValidator::IsInCharIncludes(const wxString& val) const { - if ( m_stringValue ) - { - wxTextEntry * const text = GetTextEntry(); - if ( !text ) + for (size_t i = 0; i < val.length(); i++) + if (m_includes.Index((wxString) val[i]) == wxNOT_FOUND) + // one character of 'val' is NOT present in m_includes... return false; - text->SetValue(*m_stringValue); - } - + // all characters of 'val' are present in m_includes return true; } -// Called to transfer data to the window -bool wxTextValidator::TransferFromWindow(void) +bool wxTextValidator::IsNotInCharExcludes(const wxString& val) const { - if ( m_stringValue ) - { - wxTextEntry * const text = GetTextEntry(); - if ( !text ) + for (size_t i = 0; i < val.length(); i++) + if (m_excludes.Index((wxString) val[i]) != wxNOT_FOUND) + // one character of 'val' is present in m_excludes... return false; - *m_stringValue = text->GetValue(); - } - + // all characters of 'val' are NOT present in m_excludes return true; } -bool wxTextValidator::IsInCharIncludes(const wxString& val) +void wxTextValidator::OnChar(wxKeyEvent& event) { - size_t i; - for ( i = 0; i < val.length(); i++) + if (!m_validatorWindow) { - if (m_includes.Index((wxString) val[i]) == wxNOT_FOUND) - return false; + event.Skip(); + return; } - return true; -} -bool wxTextValidator::IsNotInCharExcludes(const wxString& val) -{ - size_t i; - for ( i = 0; i < val.length(); i++) - { - if (m_excludes.Index((wxString) val[i]) != wxNOT_FOUND) - return false; - } - return true; -} + int keyCode = event.GetKeyCode(); -void wxTextValidator::OnChar(wxKeyEvent& event) -{ -/* - if ( !M_VTEXTDATA ) + // we don't filter special keys and delete + if (keyCode < WXK_SPACE || keyCode == WXK_DELETE || keyCode >= WXK_START) + { + event.Skip(); return; -*/ + } - if ( m_validatorWindow ) + wxString str((wxUniChar)keyCode, 1); + if (!IsValid(str, NULL)) { - int keyCode = event.GetKeyCode(); - - // we don't filter special keys and Delete - if ( - !(keyCode < WXK_SPACE || keyCode == WXK_DELETE || keyCode > WXK_START) && - ( - ((m_validatorStyle == wxFILTER_INCLUDE_CHAR_LIST) && !IsInCharIncludes(wxString((wxChar) keyCode, 1))) || - ((m_validatorStyle == wxFILTER_EXCLUDE_CHAR_LIST) && !IsNotInCharExcludes(wxString((wxChar) keyCode, 1))) || - ((m_validatorStyle == wxFILTER_ASCII) && !isascii(keyCode)) || - ((m_validatorStyle == wxFILTER_ALPHA) && !wxIsalpha(keyCode)) || - ((m_validatorStyle == wxFILTER_ALPHANUMERIC) && !wxIsalnum(keyCode)) || - ((m_validatorStyle == wxFILTER_NUMERIC) && !wxIsdigit(keyCode) - && keyCode != wxT('.') && keyCode != wxT(',') && keyCode != wxT('-') && keyCode != wxT('+') - && keyCode != wxT('e') && keyCode != wxT('E')) - ) - ) - { - if ( !wxValidator::IsSilent() ) - wxBell(); - - // eat message - return; - } - } + if ( !wxValidator::IsSilent() ) + wxBell(); - event.Skip(); + // eat message + return; + } + else + event.Skip(); } -- 2.47.2