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 wxIsNumeric(const wxString
& val
)
42 for ( wxString::const_iterator i
= val
.begin(); i
!= val
.end(); ++i
)
44 // Allow for "," (French) as well as "." -- in future we should
45 // use wxSystemSettings or other to do better localisation
46 if ((!wxIsdigit(*i
)) &&
47 (*i
!= wxS('.')) && (*i
!= wxS(',')) && (*i
!= wxS('e')) &&
48 (*i
!= wxS('E')) && (*i
!= wxS('+')) && (*i
!= wxS('-')))
54 // ----------------------------------------------------------------------------
56 // ----------------------------------------------------------------------------
58 IMPLEMENT_DYNAMIC_CLASS(wxTextValidator
, wxValidator
)
59 BEGIN_EVENT_TABLE(wxTextValidator
, wxValidator
)
60 EVT_CHAR(wxTextValidator::OnChar
)
63 wxTextValidator::wxTextValidator(long style
, wxString
*val
)
69 wxTextValidator::wxTextValidator(const wxTextValidator
& val
)
75 void wxTextValidator::SetStyle(long style
)
77 m_validatorStyle
= style
;
81 check
= (int)HasFlag(wxFILTER_ALPHA
) + (int)HasFlag(wxFILTER_ALPHANUMERIC
) +
82 (int)HasFlag(wxFILTER_DIGITS
) + (int)HasFlag(wxFILTER_NUMERIC
);
83 wxASSERT_MSG(check
<= 1,
84 "It makes sense to use only one of the wxFILTER_ALPHA/wxFILTER_ALPHANUMERIC/"
85 "wxFILTER_SIMPLE_NUMBER/wxFILTER_NUMERIC styles");
87 wxASSERT_MSG(((int)HasFlag(wxFILTER_INCLUDE_LIST
) + (int)HasFlag(wxFILTER_INCLUDE_CHAR_LIST
) <= 1) &&
88 ((int)HasFlag(wxFILTER_EXCLUDE_LIST
) + (int)HasFlag(wxFILTER_EXCLUDE_CHAR_LIST
) <= 1),
89 "Using both wxFILTER_[IN|EX]CLUDE_LIST _and_ wxFILTER_[IN|EX]CLUDE_CHAR_LIST "
90 "doesn't work since wxTextValidator internally uses the same array for both");
92 check
= (int)HasFlag(wxFILTER_INCLUDE_LIST
) + (int)HasFlag(wxFILTER_INCLUDE_CHAR_LIST
) +
93 (int)HasFlag(wxFILTER_EXCLUDE_LIST
) + (int)HasFlag(wxFILTER_EXCLUDE_CHAR_LIST
);
94 wxASSERT_MSG(check
<= 1,
95 "Using both an include/exclude list may lead to unexpected results");
96 #endif // wxDEBUG_LEVEL
99 bool wxTextValidator::Copy(const wxTextValidator
& val
)
101 wxValidator::Copy(val
);
103 m_validatorStyle
= val
.m_validatorStyle
;
104 m_stringValue
= val
.m_stringValue
;
106 m_includes
= val
.m_includes
;
107 m_excludes
= val
.m_excludes
;
112 wxTextEntry
*wxTextValidator::GetTextEntry()
115 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)))
117 return (wxTextCtrl
*)m_validatorWindow
;
122 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxComboBox
)))
124 return (wxComboBox
*)m_validatorWindow
;
129 wxT("wxTextValidator can only be used with wxTextCtrl or wxComboBox")
135 // Called when the value in the window must be validated.
136 // This function can pop up an error message.
137 bool wxTextValidator::Validate(wxWindow
*parent
)
139 // If window is disabled, simply return
140 if ( !m_validatorWindow
->IsEnabled() )
143 wxTextEntry
* const text
= GetTextEntry();
147 wxString
val(text
->GetValue());
150 if ( HasFlag(wxFILTER_EMPTY
) && val
.empty() )
152 errormsg
= _("Required information entry is empty.");
154 else if ( !(errormsg
= IsValid(val
)).empty() )
156 // NB: this format string should always contain exactly one '%s'
158 buf
.Printf(errormsg
, val
.c_str());
162 if ( !errormsg
.empty() )
164 m_validatorWindow
->SetFocus();
165 wxMessageBox(errormsg
, _("Validation conflict"),
166 wxOK
| wxICON_EXCLAMATION
, parent
);
174 // Called to transfer data to the window
175 bool wxTextValidator::TransferToWindow()
179 wxTextEntry
* const text
= GetTextEntry();
183 text
->SetValue(*m_stringValue
);
189 // Called to transfer data to the window
190 bool wxTextValidator::TransferFromWindow()
194 wxTextEntry
* const text
= GetTextEntry();
198 *m_stringValue
= text
->GetValue();
204 // IRIX mipsPro refuses to compile wxStringCheck<func>() if func is inline so
205 // let's work around this by using this non-template function instead of
206 // wxStringCheck(). And while this might be fractionally less efficient because
207 // the function call won't be inlined like this, we don't care enough about
208 // this to add extra #ifs for non-IRIX case.
212 bool CheckString(bool (*func
)(const wxUniChar
&), const wxString
& str
)
214 for ( wxString::const_iterator i
= str
.begin(); i
!= str
.end(); ++i
)
223 } // anonymous namespace
225 wxString
wxTextValidator::IsValid(const wxString
& val
) const
227 // wxFILTER_EMPTY is checked for in wxTextValidator::Validate
229 if ( HasFlag(wxFILTER_ASCII
) && !val
.IsAscii() )
230 return _("'%s' should only contain ASCII characters.");
231 if ( HasFlag(wxFILTER_ALPHA
) && !CheckString(wxIsalpha
, val
) )
232 return _("'%s' should only contain alphabetic characters.");
233 if ( HasFlag(wxFILTER_ALPHANUMERIC
) && !CheckString(wxIsalnum
, val
) )
234 return _("'%s' should only contain alphabetic or numeric characters.");
235 if ( HasFlag(wxFILTER_DIGITS
) && !CheckString(wxIsdigit
, val
) )
236 return _("'%s' should only contain digits.");
237 if ( HasFlag(wxFILTER_NUMERIC
) && !wxIsNumeric(val
) )
238 return _("'%s' should be numeric.");
239 if ( HasFlag(wxFILTER_INCLUDE_LIST
) && m_includes
.Index(val
) == wxNOT_FOUND
)
240 return _("'%s' is invalid");
241 if ( HasFlag(wxFILTER_INCLUDE_CHAR_LIST
) && !ContainsOnlyIncludedCharacters(val
) )
242 return _("'%s' is invalid");
243 if ( HasFlag(wxFILTER_EXCLUDE_LIST
) && m_excludes
.Index(val
) != wxNOT_FOUND
)
244 return _("'%s' is invalid");
245 if ( HasFlag(wxFILTER_EXCLUDE_CHAR_LIST
) && ContainsExcludedCharacters(val
) )
246 return _("'%s' is invalid");
248 return wxEmptyString
;
251 bool wxTextValidator::ContainsOnlyIncludedCharacters(const wxString
& val
) const
253 for ( wxString::const_iterator i
= val
.begin(); i
!= val
.end(); ++i
)
254 if (m_includes
.Index((wxString
) *i
) == wxNOT_FOUND
)
255 // one character of 'val' is NOT present in m_includes...
258 // all characters of 'val' are present in m_includes
262 bool wxTextValidator::ContainsExcludedCharacters(const wxString
& val
) const
264 for ( wxString::const_iterator i
= val
.begin(); i
!= val
.end(); ++i
)
265 if (m_excludes
.Index((wxString
) *i
) != wxNOT_FOUND
)
266 // one character of 'val' is present in m_excludes...
269 // all characters of 'val' are NOT present in m_excludes
273 void wxTextValidator::SetCharIncludes(const wxString
& chars
)
277 for ( wxString::const_iterator i
= chars
.begin(); i
!= chars
.end(); ++i
)
283 void wxTextValidator::SetCharExcludes(const wxString
& chars
)
287 for ( wxString::const_iterator i
= chars
.begin(); i
!= chars
.end(); ++i
)
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
).empty())
313 if ( !wxValidator::IsSilent() )
325 // wxUSE_VALIDATORS && (wxUSE_TEXTCTRL || wxUSE_COMBOBOX)