1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxTextValidator
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
19 #if wxUSE_VALIDATORS && wxUSE_TEXTCTRL
23 #include "wx/textctrl.h"
25 #include "wx/msgdlg.h"
29 #include "wx/valtext.h"
39 IMPLEMENT_DYNAMIC_CLASS(wxTextValidator
, wxValidator
)
41 BEGIN_EVENT_TABLE(wxTextValidator
, wxValidator
)
42 EVT_CHAR(wxTextValidator::OnChar
)
45 static bool wxIsNumeric(const wxString
& val
);
47 wxTextValidator::wxTextValidator(long style
, wxString
*val
)
49 m_validatorStyle
= style
;
52 m_refData = new wxVTextRefData;
54 M_VTEXTDATA->m_validatorStyle = style;
55 M_VTEXTDATA->m_stringValue = val;
59 wxTextValidator::wxTextValidator(const wxTextValidator
& val
)
65 bool wxTextValidator::Copy(const wxTextValidator
& val
)
67 wxValidator::Copy(val
);
69 m_validatorStyle
= val
.m_validatorStyle
;
70 m_stringValue
= val
.m_stringValue
;
72 m_includes
= val
.m_includes
;
73 m_excludes
= val
.m_excludes
;
78 static bool wxIsAlpha(const wxString
& val
)
81 for ( i
= 0; i
< (int)val
.Length(); i
++)
83 if (!wxIsalpha(val
[i
]))
89 static bool wxIsAlphaNumeric(const wxString
& val
)
92 for ( i
= 0; i
< (int)val
.Length(); i
++)
94 if (!wxIsalnum(val
[i
]))
100 // Called when the value in the window must be validated.
101 // This function can pop up an error message.
102 bool wxTextValidator::Validate(wxWindow
*parent
)
104 if( !CheckValidator() )
107 wxTextCtrl
*control
= (wxTextCtrl
*) m_validatorWindow
;
109 // If window is disabled, simply return
110 if ( !control
->IsEnabled() )
113 wxString
val(control
->GetValue());
117 // NB: this format string should contian exactly one '%s'
120 bool includes
= (m_validatorStyle
& wxFILTER_INCLUDE_LIST
) != 0;
121 if ( includes
|| (m_validatorStyle
& wxFILTER_EXCLUDE_LIST
) )
123 // if includes, it's only ok to have the members of the list,
124 // otherwise it's only ok to have non-members
125 ok
= includes
== (m_includes
.Index(val
) != wxNOT_FOUND
);
128 errormsg
= _("'%s' is invalid");
131 else if ( (m_validatorStyle
& wxFILTER_ASCII
) && !val
.IsAscii() )
135 errormsg
= _("'%s' should only contain ASCII characters.");
137 else if ( (m_validatorStyle
& wxFILTER_ALPHA
) && !wxIsAlpha(val
) )
141 errormsg
= _("'%s' should only contain alphabetic characters.");
143 else if ( (m_validatorStyle
& wxFILTER_ALPHANUMERIC
) && !wxIsAlphaNumeric(val
))
147 errormsg
= _("'%s' should only contain alphabetic or numeric characters.");
149 else if ( (m_validatorStyle
& wxFILTER_NUMERIC
) && !wxIsNumeric(val
))
153 errormsg
= _("'%s' should be numeric.");
155 else if ( (m_validatorStyle
& wxFILTER_INCLUDE_CHAR_LIST
) && !IsInCharIncludes(val
))
157 //it's only ok to have the members of the list
158 errormsg
= _("'%s' is invalid");
161 else if ( (m_validatorStyle
& wxFILTER_EXCLUDE_CHAR_LIST
) && !IsNotInCharExcludes(val
))
163 // it's only ok to have non-members of the list
164 errormsg
= _("'%s' is invalid");
170 wxASSERT_MSG( !errormsg
.empty(), _T("you forgot to set errormsg") );
172 m_validatorWindow
->SetFocus();
175 buf
.Printf(errormsg
, val
.c_str());
177 wxMessageBox(buf
, _("Validation conflict"),
178 wxOK
| wxICON_EXCLAMATION
, parent
);
184 // Called to transfer data to the window
185 bool wxTextValidator::TransferToWindow(void)
187 if( !CheckValidator() )
192 wxTextCtrl
*control
= (wxTextCtrl
*) m_validatorWindow
;
193 control
->SetValue(* m_stringValue
);
199 // Called to transfer data to the window
200 bool wxTextValidator::TransferFromWindow(void)
202 if( !CheckValidator() )
207 wxTextCtrl
*control
= (wxTextCtrl
*) m_validatorWindow
;
208 *m_stringValue
= control
->GetValue();
214 #if WXWIN_COMPATIBILITY_2_4
216 inline void wxCopyStringListToArrayString(wxArrayString
& to
, const wxStringList
& from
)
220 for ( wxStringList::compatibility_iterator pNode
= from
.GetFirst();
222 pNode
= pNode
->GetNext() )
224 to
.Add(pNode
->GetData());
228 inline void wxCopyArrayStringToStringList(wxStringList
& to
, const wxArrayString
& from
)
232 for(size_t i
= 0; i
< from
.GetCount(); ++i
)
236 wxStringList
& wxTextValidator::GetIncludeList()
238 wxCopyArrayStringToStringList(m_includeList
, m_includes
);
239 return m_includeList
;
242 wxStringList
& wxTextValidator::GetExcludeList()
244 wxCopyArrayStringToStringList(m_excludeList
, m_excludes
);
245 return m_excludeList
;
248 void wxTextValidator::SetIncludeList(const wxStringList
& list
)
250 wxCopyStringListToArrayString(m_includes
, list
);
253 void wxTextValidator::SetExcludeList(const wxStringList
& list
)
255 wxCopyStringListToArrayString(m_excludes
, list
);
258 bool wxTextValidator::IsInCharIncludeList(const wxString
& val
)
260 return IsInCharIncludes(val
);
263 bool wxTextValidator::IsNotInCharExcludeList(const wxString
& val
)
265 return IsNotInCharExcludes(val
);
271 bool wxTextValidator::IsInCharIncludes(const wxString
& val
)
274 for ( i
= 0; i
< val
.Length(); i
++)
276 if (m_includes
.Index((wxString
) val
[i
]) == wxNOT_FOUND
)
282 bool wxTextValidator::IsNotInCharExcludes(const wxString
& val
)
285 for ( i
= 0; i
< val
.Length(); i
++)
287 if (m_excludes
.Index((wxString
) val
[i
]) != wxNOT_FOUND
)
293 void wxTextValidator::OnChar(wxKeyEvent
& event
)
300 if ( m_validatorWindow
)
302 int keyCode
= event
.GetKeyCode();
304 // we don't filter special keys and Delete
306 !(keyCode
< WXK_SPACE
|| keyCode
== WXK_DELETE
|| keyCode
> WXK_START
) &&
308 ((m_validatorStyle
& wxFILTER_INCLUDE_CHAR_LIST
) && !IsInCharIncludes(wxString((wxChar
) keyCode
, 1))) ||
309 ((m_validatorStyle
& wxFILTER_EXCLUDE_CHAR_LIST
) && !IsNotInCharExcludes(wxString((wxChar
) keyCode
, 1))) ||
310 ((m_validatorStyle
& wxFILTER_ASCII
) && !isascii(keyCode
)) ||
311 ((m_validatorStyle
& wxFILTER_ALPHA
) && !wxIsalpha(keyCode
)) ||
312 ((m_validatorStyle
& wxFILTER_ALPHANUMERIC
) && !wxIsalnum(keyCode
)) ||
313 ((m_validatorStyle
& wxFILTER_NUMERIC
) && !wxIsdigit(keyCode
)
314 && keyCode
!= wxT('.') && keyCode
!= wxT(',') && keyCode
!= wxT('-') && keyCode
!= wxT('+') && keyCode
!= wxT('e') && keyCode
!= wxT('E'))
318 if ( !wxValidator::IsSilent() )
329 static bool wxIsNumeric(const wxString
& val
)
332 for ( i
= 0; i
< (int)val
.Length(); i
++)
334 // Allow for "," (French) as well as "." -- in future we should
335 // use wxSystemSettings or other to do better localisation
336 if ((!wxIsdigit(val
[i
])) && (val
[i
] != wxT('.')) && (val
[i
] != wxT(',')) && (val
[i
] != wxT('e')) && (val
[i
] != wxT('E')) && (val
[i
] != wxT('+')) && (val
[i
] != wxT('-')))
344 // wxUSE_VALIDATORS && wxUSE_TEXTCTRL