1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxTextValidator
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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_includes
= val
.m_includes
;
77 m_excludes
= val
.m_excludes
;
82 static bool wxIsAlpha(const wxString
& val
)
85 for ( i
= 0; i
< (int)val
.Length(); i
++)
87 if (!wxIsalpha(val
[i
]))
93 static bool wxIsAlphaNumeric(const wxString
& val
)
96 for ( i
= 0; i
< (int)val
.Length(); i
++)
98 if (!wxIsalnum(val
[i
]))
104 // Called when the value in the window must be validated.
105 // This function can pop up an error message.
106 bool wxTextValidator::Validate(wxWindow
*parent
)
108 if( !CheckValidator() )
111 wxTextCtrl
*control
= (wxTextCtrl
*) m_validatorWindow
;
113 // If window is disabled, simply return
114 if ( !control
->IsEnabled() )
117 wxString
val(control
->GetValue());
121 // NB: this format string should contian exactly one '%s'
124 bool includes
= (m_validatorStyle
& wxFILTER_INCLUDE_LIST
) != 0;
125 if ( includes
|| (m_validatorStyle
& wxFILTER_EXCLUDE_LIST
) )
127 // if includes, it's only ok to have the members of the list,
128 // otherwise it's only ok to have non-members
129 ok
= includes
== (m_includes
.Index(val
) != wxNOT_FOUND
);
132 errormsg
= _("'%s' is invalid");
135 else if ( (m_validatorStyle
& wxFILTER_ASCII
) && !val
.IsAscii() )
139 errormsg
= _("'%s' should only contain ASCII characters.");
141 else if ( (m_validatorStyle
& wxFILTER_ALPHA
) && !wxIsAlpha(val
) )
145 errormsg
= _("'%s' should only contain alphabetic characters.");
147 else if ( (m_validatorStyle
& wxFILTER_ALPHANUMERIC
) && !wxIsAlphaNumeric(val
))
151 errormsg
= _("'%s' should only contain alphabetic or numeric characters.");
153 else if ( (m_validatorStyle
& wxFILTER_NUMERIC
) && !wxIsNumeric(val
))
157 errormsg
= _("'%s' should be numeric.");
159 else if ( (m_validatorStyle
& wxFILTER_INCLUDE_CHAR_LIST
) && !IsInCharIncludes(val
))
161 //it's only ok to have the members of the list
162 errormsg
= _("'%s' is invalid");
165 else if ( (m_validatorStyle
& wxFILTER_EXCLUDE_CHAR_LIST
) && !IsNotInCharExcludes(val
))
167 // it's only ok to have non-members of the list
168 errormsg
= _("'%s' is invalid");
174 wxASSERT_MSG( !errormsg
.empty(), _T("you forgot to set errormsg") );
176 m_validatorWindow
->SetFocus();
179 buf
.Printf(errormsg
, val
.c_str());
181 wxMessageBox(buf
, _("Validation conflict"),
182 wxOK
| wxICON_EXCLAMATION
, parent
);
188 // Called to transfer data to the window
189 bool wxTextValidator::TransferToWindow(void)
191 if( !CheckValidator() )
196 wxTextCtrl
*control
= (wxTextCtrl
*) m_validatorWindow
;
197 control
->SetValue(* m_stringValue
);
203 // Called to transfer data to the window
204 bool wxTextValidator::TransferFromWindow(void)
206 if( !CheckValidator() )
211 wxTextCtrl
*control
= (wxTextCtrl
*) m_validatorWindow
;
212 *m_stringValue
= control
->GetValue();
218 #if WXWIN_COMPATIBILITY_2_4
220 inline void wxCopyStringListToArrayString(wxArrayString
& to
, const wxStringList
& from
)
224 for ( wxStringList::compatibility_iterator pNode
= from
.GetFirst();
226 pNode
= pNode
->GetNext() )
228 to
.Add(pNode
->GetData());
232 inline void wxCopyArrayStringToStringList(wxStringList
& to
, const wxArrayString
& from
)
236 for(size_t i
= 0; i
< from
.GetCount(); ++i
)
240 wxStringList
& wxTextValidator::GetIncludeList()
242 wxCopyArrayStringToStringList(m_includeList
, m_includes
);
243 return m_includeList
;
246 wxStringList
& wxTextValidator::GetExcludeList()
248 wxCopyArrayStringToStringList(m_excludeList
, m_excludes
);
249 return m_excludeList
;
252 void wxTextValidator::SetIncludeList(const wxStringList
& list
)
254 wxCopyStringListToArrayString(m_includes
, list
);
257 void wxTextValidator::SetExcludeList(const wxStringList
& list
)
259 wxCopyStringListToArrayString(m_excludes
, list
);
262 bool wxTextValidator::IsInCharIncludeList(const wxString
& val
)
264 return IsInCharIncludes(val
);
267 bool wxTextValidator::IsNotInCharExcludeList(const wxString
& val
)
269 return IsNotInCharExcludes(val
);
275 bool wxTextValidator::IsInCharIncludes(const wxString
& val
)
278 for ( i
= 0; i
< val
.Length(); i
++)
280 if (m_includes
.Index((wxString
) val
[i
]) == wxNOT_FOUND
)
286 bool wxTextValidator::IsNotInCharExcludes(const wxString
& val
)
289 for ( i
= 0; i
< val
.Length(); i
++)
291 if (m_excludes
.Index((wxString
) val
[i
]) != wxNOT_FOUND
)
297 void wxTextValidator::OnChar(wxKeyEvent
& event
)
304 if ( m_validatorWindow
)
306 int keyCode
= event
.GetKeyCode();
308 // we don't filter special keys and Delete
310 !(keyCode
< WXK_SPACE
|| keyCode
== WXK_DELETE
|| keyCode
> WXK_START
) &&
312 ((m_validatorStyle
& wxFILTER_INCLUDE_CHAR_LIST
) && !IsInCharIncludes(wxString((wxChar
) keyCode
, 1))) ||
313 ((m_validatorStyle
& wxFILTER_EXCLUDE_CHAR_LIST
) && !IsNotInCharExcludes(wxString((wxChar
) keyCode
, 1))) ||
314 ((m_validatorStyle
& wxFILTER_ASCII
) && !isascii(keyCode
)) ||
315 ((m_validatorStyle
& wxFILTER_ALPHA
) && !wxIsalpha(keyCode
)) ||
316 ((m_validatorStyle
& wxFILTER_ALPHANUMERIC
) && !wxIsalnum(keyCode
)) ||
317 ((m_validatorStyle
& wxFILTER_NUMERIC
) && !wxIsdigit(keyCode
)
318 && keyCode
!= '.' && keyCode
!= ',' && keyCode
!= '-')
322 if ( !wxValidator::IsSilent() )
333 static bool wxIsNumeric(const wxString
& val
)
336 for ( i
= 0; i
< (int)val
.Length(); i
++)
338 // Allow for "," (French) as well as "." -- in future we should
339 // use wxSystemSettings or other to do better localisation
340 if ((!wxIsdigit(val
[i
])) && (val
[i
] != '.') && (val
[i
] != ',') && (val
[i
] != wxT('e')) && (val
[i
] != wxT('E')) && (val
[i
] != wxT('+')) && (val
[i
] != wxT('-')))
348 // wxUSE_VALIDATORS && wxUSE_TEXTCTRL