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
This commit is contained in:
Francesco Montorsi
2009-01-30 17:22:05 +00:00
parent 253b70f1aa
commit 10b0f4894b
2 changed files with 102 additions and 97 deletions

View File

@@ -2,7 +2,7 @@
// Name: valtext.h // Name: valtext.h
// Purpose: wxTextValidator class // Purpose: wxTextValidator class
// Author: Julian Smart // Author: Julian Smart
// Modified by: // Modified by: Francesco Montorsi
// Created: 29/01/98 // Created: 29/01/98
// RCS-ID: $Id$ // RCS-ID: $Id$
// Copyright: (c) 1998 Julian Smart // Copyright: (c) 1998 Julian Smart
@@ -61,6 +61,9 @@ public:
// Called to transfer data from the window // Called to transfer data from the window
virtual bool TransferFromWindow(); virtual bool TransferFromWindow();
// Filter keystrokes
void OnChar(wxKeyEvent& event);
// ACCESSORS // ACCESSORS
inline wxTextValidatorStyle GetStyle() const { return m_validatorStyle; } inline wxTextValidatorStyle GetStyle() const { return m_validatorStyle; }
inline void SetStyle(wxTextValidatorStyle style) { m_validatorStyle = style; } inline void SetStyle(wxTextValidatorStyle style) { m_validatorStyle = style; }
@@ -76,11 +79,16 @@ public:
void SetExcludes(const wxArrayString& excludes) { m_excludes = excludes; } void SetExcludes(const wxArrayString& excludes) { m_excludes = excludes; }
inline wxArrayString& GetExcludes() { return m_excludes; } inline wxArrayString& GetExcludes() { return m_excludes; }
bool IsInCharIncludes(const wxString& val); protected:
bool IsNotInCharExcludes(const wxString& val);
// Filter keystrokes // returns true if all characters of the given string are present in m_includes
void OnChar(wxKeyEvent& event); bool IsInCharIncludes(const wxString& val) const;
// 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: protected:
wxTextValidatorStyle m_validatorStyle; wxTextValidatorStyle m_validatorStyle;

View File

@@ -2,7 +2,7 @@
// Name: src/common/valtext.cpp // Name: src/common/valtext.cpp
// Purpose: wxTextValidator // Purpose: wxTextValidator
// Author: Julian Smart // Author: Julian Smart
// Modified by: // Modified by: Francesco Montorsi
// Created: 04/01/98 // Created: 04/01/98
// RCS-ID: $Id$ // RCS-ID: $Id$
// Copyright: (c) Julian Smart // Copyright: (c) Julian Smart
@@ -66,7 +66,7 @@ static bool wxIsNumeric(const wxString& val)
{ {
// Allow for "," (French) as well as "." -- in future we should // Allow for "," (French) as well as "." -- in future we should
// use wxSystemSettings or other to do better localisation // 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('-'))) (val[i] != wxT('E')) && (val[i] != wxT('+')) && (val[i] != wxT('-')))
return false; return false;
} }
@@ -101,12 +101,6 @@ wxTextValidator::wxTextValidator(wxTextValidatorStyle style, wxString *val)
{ {
m_validatorStyle = style; m_validatorStyle = style;
m_stringValue = val; m_stringValue = val;
/*
m_refData = new wxVTextRefData;
M_VTEXTDATA->m_validatorStyle = style;
M_VTEXTDATA->m_stringValue = val;
*/
} }
wxTextValidator::wxTextValidator(const wxTextValidator& 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' // NB: this format string should always contain exactly one '%s'
wxString errormsg; 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) switch (m_validatorStyle)
{ {
case wxFILTER_NONE: case wxFILTER_NONE:
@@ -218,109 +262,62 @@ bool wxTextValidator::Validate(wxWindow *parent)
wxFAIL_MSG("invalid text validator style"); wxFAIL_MSG("invalid text validator style");
} }
if ( !errormsg.empty() ) if (pErr)
{ *pErr = errormsg;
m_validatorWindow->SetFocus();
wxString buf; return errormsg.empty();
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::IsInCharIncludes(const wxString& val) const
bool wxTextValidator::TransferToWindow(void)
{ {
if ( m_stringValue ) for (size_t i = 0; i < val.length(); i++)
{
wxTextEntry * const text = GetTextEntry();
if ( !text )
return false;
text->SetValue(*m_stringValue);
}
return true;
}
// Called to transfer data to the window
bool wxTextValidator::TransferFromWindow(void)
{
if ( m_stringValue )
{
wxTextEntry * const text = GetTextEntry();
if ( !text )
return false;
*m_stringValue = text->GetValue();
}
return true;
}
bool wxTextValidator::IsInCharIncludes(const wxString& val)
{
size_t i;
for ( i = 0; i < val.length(); i++)
{
if (m_includes.Index((wxString) val[i]) == wxNOT_FOUND) if (m_includes.Index((wxString) val[i]) == wxNOT_FOUND)
// one character of 'val' is NOT present in m_includes...
return false; return false;
}
// all characters of 'val' are present in m_includes
return true; return true;
} }
bool wxTextValidator::IsNotInCharExcludes(const wxString& val) bool wxTextValidator::IsNotInCharExcludes(const wxString& val) const
{ {
size_t i; for (size_t i = 0; i < val.length(); i++)
for ( i = 0; i < val.length(); i++) if (m_excludes.Index((wxString) val[i]) != wxNOT_FOUND)
{ // one character of 'val' is present in m_excludes...
if (m_excludes.Index((wxString) val[i]) != wxNOT_FOUND)
return false; return false;
}
// all characters of 'val' are NOT present in m_excludes
return true; return true;
} }
void wxTextValidator::OnChar(wxKeyEvent& event) void wxTextValidator::OnChar(wxKeyEvent& event)
{ {
/* if (!m_validatorWindow)
if ( !M_VTEXTDATA )
return;
*/
if ( m_validatorWindow )
{ {
int keyCode = event.GetKeyCode(); event.Skip();
return;
// 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;
}
} }
event.Skip(); int keyCode = event.GetKeyCode();
// we don't filter special keys and delete
if (keyCode < WXK_SPACE || keyCode == WXK_DELETE || keyCode >= WXK_START)
{
event.Skip();
return;
}
wxString str((wxUniChar)keyCode, 1);
if (!IsValid(str, NULL))
{
if ( !wxValidator::IsSilent() )
wxBell();
// eat message
return;
}
else
event.Skip();
} }