1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/valtext.cpp
3 // Purpose: wxTextValidator
4 // Author: Julian Smart
5 // Modified by: Francesco Montorsi
7 // Copyright: (c) Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
18 #if wxUSE_VALIDATORS && (wxUSE_TEXTCTRL || wxUSE_COMBOBOX)
20 #include "wx/valtext.h"
24 #include "wx/textctrl.h"
25 #include "wx/combobox.h"
27 #include "wx/msgdlg.h"
37 // ----------------------------------------------------------------------------
39 // ----------------------------------------------------------------------------
41 static bool wxIsNumeric(const wxString
& val
)
43 for ( wxString::const_iterator i
= val
.begin(); i
!= val
.end(); ++i
)
45 // Allow for "," (French) as well as "." -- in future we should
46 // use wxSystemSettings or other to do better localisation
47 if ((!wxIsdigit(*i
)) &&
48 (*i
!= wxS('.')) && (*i
!= wxS(',')) && (*i
!= wxS('e')) &&
49 (*i
!= wxS('E')) && (*i
!= wxS('+')) && (*i
!= wxS('-')))
55 // ----------------------------------------------------------------------------
57 // ----------------------------------------------------------------------------
59 IMPLEMENT_DYNAMIC_CLASS(wxTextValidator
, wxValidator
)
60 BEGIN_EVENT_TABLE(wxTextValidator
, wxValidator
)
61 EVT_CHAR(wxTextValidator::OnChar
)
64 wxTextValidator::wxTextValidator(long style
, wxString
*val
)
70 wxTextValidator::wxTextValidator(const wxTextValidator
& val
)
76 void wxTextValidator::SetStyle(long style
)
78 m_validatorStyle
= style
;
82 check
= (int)HasFlag(wxFILTER_ALPHA
) + (int)HasFlag(wxFILTER_ALPHANUMERIC
) +
83 (int)HasFlag(wxFILTER_DIGITS
) + (int)HasFlag(wxFILTER_NUMERIC
);
84 wxASSERT_MSG(check
<= 1,
85 "It makes sense to use only one of the wxFILTER_ALPHA/wxFILTER_ALPHANUMERIC/"
86 "wxFILTER_SIMPLE_NUMBER/wxFILTER_NUMERIC styles");
88 wxASSERT_MSG(((int)HasFlag(wxFILTER_INCLUDE_LIST
) + (int)HasFlag(wxFILTER_INCLUDE_CHAR_LIST
) <= 1) &&
89 ((int)HasFlag(wxFILTER_EXCLUDE_LIST
) + (int)HasFlag(wxFILTER_EXCLUDE_CHAR_LIST
) <= 1),
90 "Using both wxFILTER_[IN|EX]CLUDE_LIST _and_ wxFILTER_[IN|EX]CLUDE_CHAR_LIST "
91 "doesn't work since wxTextValidator internally uses the same array for both");
93 check
= (int)HasFlag(wxFILTER_INCLUDE_LIST
) + (int)HasFlag(wxFILTER_INCLUDE_CHAR_LIST
) +
94 (int)HasFlag(wxFILTER_EXCLUDE_LIST
) + (int)HasFlag(wxFILTER_EXCLUDE_CHAR_LIST
);
95 wxASSERT_MSG(check
<= 1,
96 "Using both an include/exclude list may lead to unexpected results");
97 #endif // wxDEBUG_LEVEL
100 bool wxTextValidator::Copy(const wxTextValidator
& val
)
102 wxValidator::Copy(val
);
104 m_validatorStyle
= val
.m_validatorStyle
;
105 m_stringValue
= val
.m_stringValue
;
107 m_includes
= val
.m_includes
;
108 m_excludes
= val
.m_excludes
;
113 wxTextEntry
*wxTextValidator::GetTextEntry()
116 if (wxDynamicCast(m_validatorWindow
, wxTextCtrl
))
118 return (wxTextCtrl
*)m_validatorWindow
;
123 if (wxDynamicCast(m_validatorWindow
, wxComboBox
))
125 return (wxComboBox
*)m_validatorWindow
;
130 if (wxDynamicCast(m_validatorWindow
, wxComboCtrl
))
132 return (wxComboCtrl
*)m_validatorWindow
;
137 "wxTextValidator can only be used with wxTextCtrl, wxComboBox, "
144 // Called when the value in the window must be validated.
145 // This function can pop up an error message.
146 bool wxTextValidator::Validate(wxWindow
*parent
)
148 // If window is disabled, simply return
149 if ( !m_validatorWindow
->IsEnabled() )
152 wxTextEntry
* const text
= GetTextEntry();
156 wxString
val(text
->GetValue());
159 if ( HasFlag(wxFILTER_EMPTY
) && val
.empty() )
161 errormsg
= _("Required information entry is empty.");
163 else if ( !(errormsg
= IsValid(val
)).empty() )
165 // NB: this format string should always contain exactly one '%s'
167 buf
.Printf(errormsg
, val
.c_str());
171 if ( !errormsg
.empty() )
173 m_validatorWindow
->SetFocus();
174 wxMessageBox(errormsg
, _("Validation conflict"),
175 wxOK
| wxICON_EXCLAMATION
, parent
);
183 // Called to transfer data to the window
184 bool wxTextValidator::TransferToWindow()
188 wxTextEntry
* const text
= GetTextEntry();
192 text
->SetValue(*m_stringValue
);
198 // Called to transfer data to the window
199 bool wxTextValidator::TransferFromWindow()
203 wxTextEntry
* const text
= GetTextEntry();
207 *m_stringValue
= text
->GetValue();
213 // IRIX mipsPro refuses to compile wxStringCheck<func>() if func is inline so
214 // let's work around this by using this non-template function instead of
215 // wxStringCheck(). And while this might be fractionally less efficient because
216 // the function call won't be inlined like this, we don't care enough about
217 // this to add extra #ifs for non-IRIX case.
221 bool CheckString(bool (*func
)(const wxUniChar
&), const wxString
& str
)
223 for ( wxString::const_iterator i
= str
.begin(); i
!= str
.end(); ++i
)
232 } // anonymous namespace
234 wxString
wxTextValidator::IsValid(const wxString
& val
) const
236 // wxFILTER_EMPTY is checked for in wxTextValidator::Validate
238 if ( HasFlag(wxFILTER_ASCII
) && !val
.IsAscii() )
239 return _("'%s' should only contain ASCII characters.");
240 if ( HasFlag(wxFILTER_ALPHA
) && !CheckString(wxIsalpha
, val
) )
241 return _("'%s' should only contain alphabetic characters.");
242 if ( HasFlag(wxFILTER_ALPHANUMERIC
) && !CheckString(wxIsalnum
, val
) )
243 return _("'%s' should only contain alphabetic or numeric characters.");
244 if ( HasFlag(wxFILTER_DIGITS
) && !CheckString(wxIsdigit
, val
) )
245 return _("'%s' should only contain digits.");
246 if ( HasFlag(wxFILTER_NUMERIC
) && !wxIsNumeric(val
) )
247 return _("'%s' should be numeric.");
248 if ( HasFlag(wxFILTER_INCLUDE_LIST
) && m_includes
.Index(val
) == wxNOT_FOUND
)
249 return _("'%s' is invalid");
250 if ( HasFlag(wxFILTER_INCLUDE_CHAR_LIST
) && !ContainsOnlyIncludedCharacters(val
) )
251 return _("'%s' is invalid");
252 if ( HasFlag(wxFILTER_EXCLUDE_LIST
) && m_excludes
.Index(val
) != wxNOT_FOUND
)
253 return _("'%s' is invalid");
254 if ( HasFlag(wxFILTER_EXCLUDE_CHAR_LIST
) && ContainsExcludedCharacters(val
) )
255 return _("'%s' is invalid");
257 return wxEmptyString
;
260 bool wxTextValidator::ContainsOnlyIncludedCharacters(const wxString
& val
) const
262 for ( wxString::const_iterator i
= val
.begin(); i
!= val
.end(); ++i
)
263 if (m_includes
.Index((wxString
) *i
) == wxNOT_FOUND
)
264 // one character of 'val' is NOT present in m_includes...
267 // all characters of 'val' are present in m_includes
271 bool wxTextValidator::ContainsExcludedCharacters(const wxString
& val
) const
273 for ( wxString::const_iterator i
= val
.begin(); i
!= val
.end(); ++i
)
274 if (m_excludes
.Index((wxString
) *i
) != wxNOT_FOUND
)
275 // one character of 'val' is present in m_excludes...
278 // all characters of 'val' are NOT present in m_excludes
282 void wxTextValidator::SetCharIncludes(const wxString
& chars
)
286 for ( wxString::const_iterator i
= chars
.begin(); i
!= chars
.end(); ++i
)
292 void wxTextValidator::SetCharExcludes(const wxString
& chars
)
296 for ( wxString::const_iterator i
= chars
.begin(); i
!= chars
.end(); ++i
)
302 void wxTextValidator::OnChar(wxKeyEvent
& event
)
304 if (!m_validatorWindow
)
310 int keyCode
= event
.GetKeyCode();
312 // we don't filter special keys and delete
313 if (keyCode
< WXK_SPACE
|| keyCode
== WXK_DELETE
|| keyCode
>= WXK_START
)
319 wxString
str((wxUniChar
)keyCode
, 1);
320 if (!IsValid(str
).empty())
322 if ( !wxValidator::IsSilent() )
334 // wxUSE_VALIDATORS && (wxUSE_TEXTCTRL || wxUSE_COMBOBOX)