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"
38 // ----------------------------------------------------------------------------
40 // ----------------------------------------------------------------------------
42 static bool wxIsNumeric(const wxString
& val
)
44 for ( wxString::const_iterator i
= val
.begin(); i
!= val
.end(); ++i
)
46 // Allow for "," (French) as well as "." -- in future we should
47 // use wxSystemSettings or other to do better localisation
48 if ((!wxIsdigit(*i
)) &&
49 (*i
!= wxS('.')) && (*i
!= wxS(',')) && (*i
!= wxS('e')) &&
50 (*i
!= wxS('E')) && (*i
!= wxS('+')) && (*i
!= wxS('-')))
56 // ----------------------------------------------------------------------------
58 // ----------------------------------------------------------------------------
60 IMPLEMENT_DYNAMIC_CLASS(wxTextValidator
, wxValidator
)
61 BEGIN_EVENT_TABLE(wxTextValidator
, wxValidator
)
62 EVT_CHAR(wxTextValidator::OnChar
)
65 wxTextValidator::wxTextValidator(long style
, wxString
*val
)
71 wxTextValidator::wxTextValidator(const wxTextValidator
& val
)
77 void wxTextValidator::SetStyle(long style
)
79 m_validatorStyle
= style
;
83 check
= (int)HasFlag(wxFILTER_ALPHA
) + (int)HasFlag(wxFILTER_ALPHANUMERIC
) +
84 (int)HasFlag(wxFILTER_DIGITS
) + (int)HasFlag(wxFILTER_NUMERIC
);
85 wxASSERT_MSG(check
<= 1,
86 "It makes sense to use only one of the wxFILTER_ALPHA/wxFILTER_ALPHANUMERIC/"
87 "wxFILTER_SIMPLE_NUMBER/wxFILTER_NUMERIC styles");
89 wxASSERT_MSG(((int)HasFlag(wxFILTER_INCLUDE_LIST
) + (int)HasFlag(wxFILTER_INCLUDE_CHAR_LIST
) <= 1) &&
90 ((int)HasFlag(wxFILTER_EXCLUDE_LIST
) + (int)HasFlag(wxFILTER_EXCLUDE_CHAR_LIST
) <= 1),
91 "Using both wxFILTER_[IN|EX]CLUDE_LIST _and_ wxFILTER_[IN|EX]CLUDE_CHAR_LIST "
92 "doesn't work since wxTextValidator internally uses the same array for both");
94 check
= (int)HasFlag(wxFILTER_INCLUDE_LIST
) + (int)HasFlag(wxFILTER_INCLUDE_CHAR_LIST
) +
95 (int)HasFlag(wxFILTER_EXCLUDE_LIST
) + (int)HasFlag(wxFILTER_EXCLUDE_CHAR_LIST
);
96 wxASSERT_MSG(check
<= 1,
97 "Using both an include/exclude list may lead to unexpected results");
98 #endif // wxDEBUG_LEVEL
101 bool wxTextValidator::Copy(const wxTextValidator
& val
)
103 wxValidator::Copy(val
);
105 m_validatorStyle
= val
.m_validatorStyle
;
106 m_stringValue
= val
.m_stringValue
;
108 m_includes
= val
.m_includes
;
109 m_excludes
= val
.m_excludes
;
114 wxTextEntry
*wxTextValidator::GetTextEntry()
117 if (wxDynamicCast(m_validatorWindow
, wxTextCtrl
))
119 return (wxTextCtrl
*)m_validatorWindow
;
124 if (wxDynamicCast(m_validatorWindow
, wxComboBox
))
126 return (wxComboBox
*)m_validatorWindow
;
131 if (wxDynamicCast(m_validatorWindow
, wxComboCtrl
))
133 return (wxComboCtrl
*)m_validatorWindow
;
138 "wxTextValidator can only be used with wxTextCtrl, wxComboBox, "
145 // Called when the value in the window must be validated.
146 // This function can pop up an error message.
147 bool wxTextValidator::Validate(wxWindow
*parent
)
149 // If window is disabled, simply return
150 if ( !m_validatorWindow
->IsEnabled() )
153 wxTextEntry
* const text
= GetTextEntry();
157 wxString
val(text
->GetValue());
160 if ( HasFlag(wxFILTER_EMPTY
) && val
.empty() )
162 errormsg
= _("Required information entry is empty.");
164 else if ( !(errormsg
= IsValid(val
)).empty() )
166 // NB: this format string should always contain exactly one '%s'
168 buf
.Printf(errormsg
, val
.c_str());
172 if ( !errormsg
.empty() )
174 m_validatorWindow
->SetFocus();
175 wxMessageBox(errormsg
, _("Validation conflict"),
176 wxOK
| wxICON_EXCLAMATION
, parent
);
184 // Called to transfer data to the window
185 bool wxTextValidator::TransferToWindow()
189 wxTextEntry
* const text
= GetTextEntry();
193 text
->SetValue(*m_stringValue
);
199 // Called to transfer data to the window
200 bool wxTextValidator::TransferFromWindow()
204 wxTextEntry
* const text
= GetTextEntry();
208 *m_stringValue
= text
->GetValue();
214 // IRIX mipsPro refuses to compile wxStringCheck<func>() if func is inline so
215 // let's work around this by using this non-template function instead of
216 // wxStringCheck(). And while this might be fractionally less efficient because
217 // the function call won't be inlined like this, we don't care enough about
218 // this to add extra #ifs for non-IRIX case.
222 bool CheckString(bool (*func
)(const wxUniChar
&), const wxString
& str
)
224 for ( wxString::const_iterator i
= str
.begin(); i
!= str
.end(); ++i
)
233 } // anonymous namespace
235 wxString
wxTextValidator::IsValid(const wxString
& val
) const
237 // wxFILTER_EMPTY is checked for in wxTextValidator::Validate
239 if ( HasFlag(wxFILTER_ASCII
) && !val
.IsAscii() )
240 return _("'%s' should only contain ASCII characters.");
241 if ( HasFlag(wxFILTER_ALPHA
) && !CheckString(wxIsalpha
, val
) )
242 return _("'%s' should only contain alphabetic characters.");
243 if ( HasFlag(wxFILTER_ALPHANUMERIC
) && !CheckString(wxIsalnum
, val
) )
244 return _("'%s' should only contain alphabetic or numeric characters.");
245 if ( HasFlag(wxFILTER_DIGITS
) && !CheckString(wxIsdigit
, val
) )
246 return _("'%s' should only contain digits.");
247 if ( HasFlag(wxFILTER_NUMERIC
) && !wxIsNumeric(val
) )
248 return _("'%s' should be numeric.");
249 if ( HasFlag(wxFILTER_INCLUDE_LIST
) && m_includes
.Index(val
) == wxNOT_FOUND
)
250 return _("'%s' is invalid");
251 if ( HasFlag(wxFILTER_INCLUDE_CHAR_LIST
) && !ContainsOnlyIncludedCharacters(val
) )
252 return _("'%s' is invalid");
253 if ( HasFlag(wxFILTER_EXCLUDE_LIST
) && m_excludes
.Index(val
) != wxNOT_FOUND
)
254 return _("'%s' is invalid");
255 if ( HasFlag(wxFILTER_EXCLUDE_CHAR_LIST
) && ContainsExcludedCharacters(val
) )
256 return _("'%s' is invalid");
258 return wxEmptyString
;
261 bool wxTextValidator::ContainsOnlyIncludedCharacters(const wxString
& val
) const
263 for ( wxString::const_iterator i
= val
.begin(); i
!= val
.end(); ++i
)
264 if (m_includes
.Index((wxString
) *i
) == wxNOT_FOUND
)
265 // one character of 'val' is NOT present in m_includes...
268 // all characters of 'val' are present in m_includes
272 bool wxTextValidator::ContainsExcludedCharacters(const wxString
& val
) const
274 for ( wxString::const_iterator i
= val
.begin(); i
!= val
.end(); ++i
)
275 if (m_excludes
.Index((wxString
) *i
) != wxNOT_FOUND
)
276 // one character of 'val' is present in m_excludes...
279 // all characters of 'val' are NOT present in m_excludes
283 void wxTextValidator::SetCharIncludes(const wxString
& chars
)
287 for ( wxString::const_iterator i
= chars
.begin(); i
!= chars
.end(); ++i
)
293 void wxTextValidator::SetCharExcludes(const wxString
& chars
)
297 for ( wxString::const_iterator i
= chars
.begin(); i
!= chars
.end(); ++i
)
303 void wxTextValidator::OnChar(wxKeyEvent
& event
)
305 if (!m_validatorWindow
)
311 int keyCode
= event
.GetKeyCode();
313 // we don't filter special keys and delete
314 if (keyCode
< WXK_SPACE
|| keyCode
== WXK_DELETE
|| keyCode
>= WXK_START
)
320 wxString
str((wxUniChar
)keyCode
, 1);
321 if (!IsValid(str
).empty())
323 if ( !wxValidator::IsSilent() )
335 // wxUSE_VALIDATORS && (wxUSE_TEXTCTRL || wxUSE_COMBOBOX)