1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/valtext.cpp
3 // Purpose: wxTextValidator
4 // Author: Julian Smart
5 // Modified by: Francesco Montorsi
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"
26 #include "wx/combobox.h"
28 #include "wx/msgdlg.h"
36 // ----------------------------------------------------------------------------
38 // ----------------------------------------------------------------------------
40 static bool wxIsAlpha(const wxString
& val
)
43 for ( i
= 0; i
< (int)val
.length(); i
++)
45 if (!wxIsalpha(val
[i
]))
51 static bool wxIsAlphaNumeric(const wxString
& val
)
54 for ( i
= 0; i
< (int)val
.length(); i
++)
56 if (!wxIsalnum(val
[i
]))
62 static bool wxIsNumeric(const wxString
& val
)
65 for ( i
= 0; i
< (int)val
.length(); i
++)
67 // Allow for "," (French) as well as "." -- in future we should
68 // use wxSystemSettings or other to do better localisation
69 if ((!wxIsdigit(val
[i
])) && (val
[i
] != wxT('.')) && (val
[i
] != wxT(',')) && (val
[i
] != wxT('e')) &&
70 (val
[i
] != wxT('E')) && (val
[i
] != wxT('+')) && (val
[i
] != wxT('-')))
77 // ----------------------------------------------------------------------------
79 // ----------------------------------------------------------------------------
81 IMPLEMENT_DYNAMIC_CLASS(wxTextValidator
, wxValidator
)
82 BEGIN_EVENT_TABLE(wxTextValidator
, wxValidator
)
83 EVT_CHAR(wxTextValidator::OnChar
)
87 #if WXWIN_COMPATIBILITY_2_8
88 wxTextValidator::wxTextValidator(long style
, wxString
*val
)
90 m_validatorStyle
= (wxTextValidatorStyle
)style
;
94 void wxTextValidator::SetStyle(long style
)
96 SetStyle((wxTextValidatorStyle
)style
);
100 wxTextValidator::wxTextValidator(wxTextValidatorStyle style
, wxString
*val
)
102 m_validatorStyle
= style
;
106 wxTextValidator::wxTextValidator(const wxTextValidator
& val
)
112 bool wxTextValidator::Copy(const wxTextValidator
& val
)
114 wxValidator::Copy(val
);
116 m_validatorStyle
= val
.m_validatorStyle
;
117 m_stringValue
= val
.m_stringValue
;
119 m_includes
= val
.m_includes
;
120 m_excludes
= val
.m_excludes
;
125 wxTextEntry
*wxTextValidator::GetTextEntry()
128 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)))
130 return (wxTextCtrl
*)m_validatorWindow
;
135 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxComboBox
)))
137 return (wxComboBox
*)m_validatorWindow
;
142 _T("wxTextValidator can only be used with wxTextCtrl or wxComboBox")
148 // Called when the value in the window must be validated.
149 // This function can pop up an error message.
150 bool wxTextValidator::Validate(wxWindow
*parent
)
152 // If window is disabled, simply return
153 if ( !m_validatorWindow
->IsEnabled() )
156 wxTextEntry
* const text
= GetTextEntry();
160 wxString
val(text
->GetValue());
162 // NB: this format string should always contain exactly one '%s'
164 if (!IsValid(val
, &errormsg
))
166 wxASSERT(!errormsg
.empty());
168 m_validatorWindow
->SetFocus();
171 buf
.Printf(errormsg
, val
.c_str());
173 wxMessageBox(buf
, _("Validation conflict"),
174 wxOK
| wxICON_EXCLAMATION
, parent
);
182 // Called to transfer data to the window
183 bool wxTextValidator::TransferToWindow()
187 wxTextEntry
* const text
= GetTextEntry();
191 text
->SetValue(*m_stringValue
);
197 // Called to transfer data to the window
198 bool wxTextValidator::TransferFromWindow()
202 wxTextEntry
* const text
= GetTextEntry();
206 *m_stringValue
= text
->GetValue();
212 bool wxTextValidator::IsValid(const wxString
& val
, wxString
* pErr
) const
215 switch (m_validatorStyle
)
221 case wxFILTER_INCLUDE_LIST
:
222 if ( m_includes
.Index(val
) == wxNOT_FOUND
)
223 errormsg
= _("'%s' is invalid");
226 case wxFILTER_EXCLUDE_LIST
:
227 if ( m_excludes
.Index(val
) != wxNOT_FOUND
)
228 errormsg
= _("'%s' is invalid");
232 if ( !val
.IsAscii() )
233 errormsg
= _("'%s' should only contain ASCII characters.");
237 if ( !wxIsAlpha(val
) )
238 errormsg
= _("'%s' should only contain alphabetic characters.");
241 case wxFILTER_ALPHANUMERIC
:
242 if ( !wxIsAlphaNumeric(val
) )
243 errormsg
= _("'%s' should only contain alphabetic or numeric characters.");
246 case wxFILTER_NUMERIC
:
247 if ( !wxIsNumeric(val
) )
248 errormsg
= _("'%s' should be numeric.");
251 case wxFILTER_INCLUDE_CHAR_LIST
:
252 if ( !IsInCharIncludes(val
) )
253 errormsg
= _("'%s' is invalid");
256 case wxFILTER_EXCLUDE_CHAR_LIST
:
257 if ( !IsNotInCharExcludes(val
) )
258 errormsg
= _("'%s' is invalid");
262 wxFAIL_MSG("invalid text validator style");
268 return errormsg
.empty();
271 bool wxTextValidator::IsInCharIncludes(const wxString
& val
) const
273 for (size_t i
= 0; i
< val
.length(); i
++)
274 if (m_includes
.Index((wxString
) val
[i
]) == wxNOT_FOUND
)
275 // one character of 'val' is NOT present in m_includes...
278 // all characters of 'val' are present in m_includes
282 bool wxTextValidator::IsNotInCharExcludes(const wxString
& val
) const
284 for (size_t i
= 0; i
< val
.length(); i
++)
285 if (m_excludes
.Index((wxString
) val
[i
]) != wxNOT_FOUND
)
286 // one character of 'val' is present in m_excludes...
289 // all characters of 'val' are NOT present in m_excludes
293 void wxTextValidator::OnChar(wxKeyEvent
& event
)
295 if (!m_validatorWindow
)
301 int keyCode
= event
.GetKeyCode();
303 // we don't filter special keys and delete
304 if (keyCode
< WXK_SPACE
|| keyCode
== WXK_DELETE
|| keyCode
>= WXK_START
)
310 wxString
str((wxUniChar
)keyCode
, 1);
311 if (!IsValid(str
, NULL
))
313 if ( !wxValidator::IsSilent() )
325 // wxUSE_VALIDATORS && (wxUSE_TEXTCTRL || wxUSE_COMBOBOX)