1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxTextValidator
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "valtext.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
23 #if wxUSE_VALIDATORS && wxUSE_TEXTCTRL
27 #include "wx/textctrl.h"
29 #include "wx/msgdlg.h"
33 #include "wx/valtext.h"
43 IMPLEMENT_DYNAMIC_CLASS(wxTextValidator
, wxValidator
)
45 BEGIN_EVENT_TABLE(wxTextValidator
, wxValidator
)
46 EVT_CHAR(wxTextValidator::OnChar
)
49 static bool wxIsNumeric(const wxString
& val
);
51 wxTextValidator::wxTextValidator(long style
, wxString
*val
)
53 m_validatorStyle
= style
;
56 m_refData = new wxVTextRefData;
58 M_VTEXTDATA->m_validatorStyle = style;
59 M_VTEXTDATA->m_stringValue = val;
63 wxTextValidator::wxTextValidator(const wxTextValidator
& val
)
69 bool wxTextValidator::Copy(const wxTextValidator
& val
)
71 wxValidator::Copy(val
);
73 m_validatorStyle
= val
.m_validatorStyle
;
74 m_stringValue
= val
.m_stringValue
;
76 m_includeList
= val
.m_includeList
;
77 m_excludeList
= val
.m_excludeList
;
82 wxTextValidator::~wxTextValidator()
86 static bool wxIsAlpha(const wxString
& val
)
89 for ( i
= 0; i
< (int)val
.Length(); i
++)
91 if (!wxIsalpha(val
[i
]))
97 static bool wxIsAlphaNumeric(const wxString
& val
)
100 for ( i
= 0; i
< (int)val
.Length(); i
++)
102 if (!wxIsalnum(val
[i
]))
108 // Called when the value in the window must be validated.
109 // This function can pop up an error message.
110 bool wxTextValidator::Validate(wxWindow
*parent
)
112 if( !CheckValidator() )
115 wxTextCtrl
*control
= (wxTextCtrl
*) m_validatorWindow
;
117 // If window is disabled, simply return
118 if ( !control
->IsEnabled() )
121 wxString
val(control
->GetValue());
125 // NB: this format string should contian exactly one '%s'
128 bool includeList
= (m_validatorStyle
& wxFILTER_INCLUDE_LIST
) != 0;
129 if ( includeList
|| (m_validatorStyle
& wxFILTER_EXCLUDE_LIST
) )
131 // if includeList, it's only ok to have the members of the list,
132 // otherwise it's only ok to have non-members
133 ok
= includeList
== m_includeList
.Member(val
);
136 errormsg
= _("'%s' is invalid");
139 else if ( (m_validatorStyle
& wxFILTER_ASCII
) && !val
.IsAscii() )
143 errormsg
= _("'%s' should only contain ASCII characters.");
145 else if ( (m_validatorStyle
& wxFILTER_ALPHA
) && !wxIsAlpha(val
) )
149 errormsg
= _("'%s' should only contain alphabetic characters.");
151 else if ( (m_validatorStyle
& wxFILTER_ALPHANUMERIC
) && !wxIsAlphaNumeric(val
))
155 errormsg
= _("'%s' should only contain alphabetic or numeric characters.");
157 else if ( (m_validatorStyle
& wxFILTER_NUMERIC
) && !wxIsNumeric(val
))
161 errormsg
= _("'%s' should be numeric.");
163 else if ( (m_validatorStyle
& wxFILTER_INCLUDE_CHAR_LIST
) && !IsInCharIncludeList(val
))
165 //it's only ok to have the members of the list
166 errormsg
= _("'%s' is invalid");
169 else if ( (m_validatorStyle
& wxFILTER_EXCLUDE_CHAR_LIST
) && !IsNotInCharExcludeList(val
))
171 // it's only ok to have non-members of the list
172 errormsg
= _("'%s' is invalid");
178 wxASSERT_MSG( !errormsg
.empty(), _T("you forgot to set errormsg") );
180 m_validatorWindow
->SetFocus();
183 buf
.Printf(errormsg
, val
.c_str());
185 wxMessageBox(buf
, _("Validation conflict"),
186 wxOK
| wxICON_EXCLAMATION
, parent
);
192 // Called to transfer data to the window
193 bool wxTextValidator::TransferToWindow(void)
195 if( !CheckValidator() )
200 wxTextCtrl
*control
= (wxTextCtrl
*) m_validatorWindow
;
201 control
->SetValue(* m_stringValue
);
207 // Called to transfer data to the window
208 bool wxTextValidator::TransferFromWindow(void)
210 if( !CheckValidator() )
215 wxTextCtrl
*control
= (wxTextCtrl
*) m_validatorWindow
;
216 *m_stringValue
= control
->GetValue();
222 void wxTextValidator::SetIncludeList(const wxStringList
& list
)
224 m_includeList
= list
;
227 void wxTextValidator::SetExcludeList(const wxStringList
& list
)
229 m_excludeList
= list
;
233 inline int isascii(wxChar c
) { return (c
>= 0) && (c
<=127); }
236 void wxTextValidator::OnChar(wxKeyEvent
& event
)
243 if ( m_validatorWindow
)
245 int keyCode
= event
.GetKeyCode();
247 // we don't filter special keys and Delete
249 !(keyCode
< WXK_SPACE
|| keyCode
== WXK_DELETE
|| keyCode
> WXK_START
) &&
251 ((m_validatorStyle
& wxFILTER_INCLUDE_CHAR_LIST
) && !IsInCharIncludeList(wxString((char) keyCode
, 1))) ||
252 ((m_validatorStyle
& wxFILTER_EXCLUDE_CHAR_LIST
) && !IsNotInCharExcludeList(wxString((char) keyCode
, 1))) ||
253 ((m_validatorStyle
& wxFILTER_ASCII
) && !isascii(keyCode
)) ||
254 ((m_validatorStyle
& wxFILTER_ALPHA
) && !wxIsalpha(keyCode
)) ||
255 ((m_validatorStyle
& wxFILTER_ALPHANUMERIC
) && !wxIsalnum(keyCode
)) ||
256 ((m_validatorStyle
& wxFILTER_NUMERIC
) && !wxIsdigit(keyCode
)
257 && keyCode
!= '.' && keyCode
!= ',' && keyCode
!= '-')
261 if ( !wxValidator::IsSilent() )
272 static bool wxIsNumeric(const wxString
& val
)
275 for ( i
= 0; i
< (int)val
.Length(); i
++)
277 // Allow for "," (French) as well as "." -- in future we should
278 // use wxSystemSettings or other to do better localisation
279 if ((!wxIsdigit(val
[i
])) && (val
[i
] != '.') && (val
[i
] != ',') && (val
[i
] != wxT('e')) && (val
[i
] != wxT('E')) && (val
[i
] != wxT('+')) && (val
[i
] != wxT('-')))
285 bool wxTextValidator::IsInCharIncludeList(const wxString
& val
)
288 for ( i
= 0; i
< val
.Length(); i
++)
290 if (!m_includeList
.Member((wxString
) val
[i
]))
296 bool wxTextValidator::IsNotInCharExcludeList(const wxString
& val
)
299 for ( i
= 0; i
< val
.Length(); i
++)
301 if (m_excludeList
.Member((wxString
) val
[i
]))
308 // wxUSE_VALIDATORS && wxUSE_TEXTCTRL