1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/valtext.cpp
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 || wxUSE_COMBOBOX)
21 #include "wx/valtext.h"
25 #include "wx/textctrl.h"
27 #include "wx/msgdlg.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 wxTextEntry
*wxTextValidator::GetTextEntry()
81 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)))
83 return (wxTextCtrl
*)m_validatorWindow
;
87 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxComboBox
)))
89 return (wxComboBox
*)m_validatorWindow
;
94 _T("wxTextValidator can only be used with wxTextCtrl or wxComboBox")
100 static bool wxIsAlpha(const wxString
& val
)
103 for ( i
= 0; i
< (int)val
.length(); i
++)
105 if (!wxIsalpha(val
[i
]))
111 static bool wxIsAlphaNumeric(const wxString
& val
)
114 for ( i
= 0; i
< (int)val
.length(); i
++)
116 if (!wxIsalnum(val
[i
]))
122 // Called when the value in the window must be validated.
123 // This function can pop up an error message.
124 bool wxTextValidator::Validate(wxWindow
*parent
)
126 // If window is disabled, simply return
127 if ( !m_validatorWindow
->IsEnabled() )
130 wxTextEntry
* const text
= GetTextEntry();
134 wxString
val(text
->GetValue());
138 // NB: this format string should contian exactly one '%s'
141 bool includes
= (m_validatorStyle
& wxFILTER_INCLUDE_LIST
) != 0;
142 if ( includes
|| (m_validatorStyle
& wxFILTER_EXCLUDE_LIST
) )
144 // if includes, it's only ok to have the members of the list,
145 // otherwise it's only ok to have non-members
146 ok
= includes
== (m_includes
.Index(val
) != wxNOT_FOUND
);
149 errormsg
= _("'%s' is invalid");
152 else if ( (m_validatorStyle
& wxFILTER_ASCII
) && !val
.IsAscii() )
156 errormsg
= _("'%s' should only contain ASCII characters.");
158 else if ( (m_validatorStyle
& wxFILTER_ALPHA
) && !wxIsAlpha(val
) )
162 errormsg
= _("'%s' should only contain alphabetic characters.");
164 else if ( (m_validatorStyle
& wxFILTER_ALPHANUMERIC
) && !wxIsAlphaNumeric(val
))
168 errormsg
= _("'%s' should only contain alphabetic or numeric characters.");
170 else if ( (m_validatorStyle
& wxFILTER_NUMERIC
) && !wxIsNumeric(val
))
174 errormsg
= _("'%s' should be numeric.");
176 else if ( (m_validatorStyle
& wxFILTER_INCLUDE_CHAR_LIST
) && !IsInCharIncludes(val
))
178 //it's only ok to have the members of the list
179 errormsg
= _("'%s' is invalid");
182 else if ( (m_validatorStyle
& wxFILTER_EXCLUDE_CHAR_LIST
) && !IsNotInCharExcludes(val
))
184 // it's only ok to have non-members of the list
185 errormsg
= _("'%s' is invalid");
191 wxASSERT_MSG( !errormsg
.empty(), _T("you forgot to set errormsg") );
193 m_validatorWindow
->SetFocus();
196 buf
.Printf(errormsg
, val
.c_str());
198 wxMessageBox(buf
, _("Validation conflict"),
199 wxOK
| wxICON_EXCLAMATION
, parent
);
205 // Called to transfer data to the window
206 bool wxTextValidator::TransferToWindow(void)
210 wxTextEntry
* const text
= GetTextEntry();
214 text
->SetValue(*m_stringValue
);
220 // Called to transfer data to the window
221 bool wxTextValidator::TransferFromWindow(void)
225 wxTextEntry
* const text
= GetTextEntry();
229 *m_stringValue
= text
->GetValue();
235 bool wxTextValidator::IsInCharIncludes(const wxString
& val
)
238 for ( i
= 0; i
< val
.length(); i
++)
240 if (m_includes
.Index((wxString
) val
[i
]) == wxNOT_FOUND
)
246 bool wxTextValidator::IsNotInCharExcludes(const wxString
& val
)
249 for ( i
= 0; i
< val
.length(); i
++)
251 if (m_excludes
.Index((wxString
) val
[i
]) != wxNOT_FOUND
)
257 void wxTextValidator::OnChar(wxKeyEvent
& event
)
264 if ( m_validatorWindow
)
266 int keyCode
= event
.GetKeyCode();
268 // we don't filter special keys and Delete
270 !(keyCode
< WXK_SPACE
|| keyCode
== WXK_DELETE
|| keyCode
> WXK_START
) &&
272 ((m_validatorStyle
& wxFILTER_INCLUDE_CHAR_LIST
) && !IsInCharIncludes(wxString((wxChar
) keyCode
, 1))) ||
273 ((m_validatorStyle
& wxFILTER_EXCLUDE_CHAR_LIST
) && !IsNotInCharExcludes(wxString((wxChar
) keyCode
, 1))) ||
274 ((m_validatorStyle
& wxFILTER_ASCII
) && !isascii(keyCode
)) ||
275 ((m_validatorStyle
& wxFILTER_ALPHA
) && !wxIsalpha(keyCode
)) ||
276 ((m_validatorStyle
& wxFILTER_ALPHANUMERIC
) && !wxIsalnum(keyCode
)) ||
277 ((m_validatorStyle
& wxFILTER_NUMERIC
) && !wxIsdigit(keyCode
)
278 && keyCode
!= wxT('.') && keyCode
!= wxT(',') && keyCode
!= wxT('-') && keyCode
!= wxT('+') && keyCode
!= wxT('e') && keyCode
!= wxT('E'))
282 if ( !wxValidator::IsSilent() )
293 static bool wxIsNumeric(const wxString
& val
)
296 for ( i
= 0; i
< (int)val
.length(); i
++)
298 // Allow for "," (French) as well as "." -- in future we should
299 // use wxSystemSettings or other to do better localisation
300 if ((!wxIsdigit(val
[i
])) && (val
[i
] != wxT('.')) && (val
[i
] != wxT(',')) && (val
[i
] != wxT('e')) && (val
[i
] != wxT('E')) && (val
[i
] != wxT('+')) && (val
[i
] != wxT('-')))
308 // wxUSE_VALIDATORS && (wxUSE_TEXTCTRL || wxUSE_COMBOBOX)