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"
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
;
105 m_refData = new wxVTextRefData;
107 M_VTEXTDATA->m_validatorStyle = style;
108 M_VTEXTDATA->m_stringValue = val;
112 wxTextValidator::wxTextValidator(const wxTextValidator
& val
)
118 bool wxTextValidator::Copy(const wxTextValidator
& val
)
120 wxValidator::Copy(val
);
122 m_validatorStyle
= val
.m_validatorStyle
;
123 m_stringValue
= val
.m_stringValue
;
125 m_includes
= val
.m_includes
;
126 m_excludes
= val
.m_excludes
;
131 wxTextEntry
*wxTextValidator::GetTextEntry()
134 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)))
136 return (wxTextCtrl
*)m_validatorWindow
;
141 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxComboBox
)))
143 return (wxComboBox
*)m_validatorWindow
;
148 _T("wxTextValidator can only be used with wxTextCtrl or wxComboBox")
154 // Called when the value in the window must be validated.
155 // This function can pop up an error message.
156 bool wxTextValidator::Validate(wxWindow
*parent
)
158 // If window is disabled, simply return
159 if ( !m_validatorWindow
->IsEnabled() )
162 wxTextEntry
* const text
= GetTextEntry();
166 wxString
val(text
->GetValue());
168 // NB: this format string should always contain exactly one '%s'
171 switch (m_validatorStyle
)
177 case wxFILTER_INCLUDE_LIST
:
178 if ( m_includes
.Index(val
) == wxNOT_FOUND
)
179 errormsg
= _("'%s' is invalid");
182 case wxFILTER_EXCLUDE_LIST
:
183 if ( m_excludes
.Index(val
) != wxNOT_FOUND
)
184 errormsg
= _("'%s' is invalid");
188 if ( !val
.IsAscii() )
189 errormsg
= _("'%s' should only contain ASCII characters.");
193 if ( !wxIsAlpha(val
) )
194 errormsg
= _("'%s' should only contain alphabetic characters.");
197 case wxFILTER_ALPHANUMERIC
:
198 if ( !wxIsAlphaNumeric(val
) )
199 errormsg
= _("'%s' should only contain alphabetic or numeric characters.");
202 case wxFILTER_NUMERIC
:
203 if ( !wxIsNumeric(val
) )
204 errormsg
= _("'%s' should be numeric.");
207 case wxFILTER_INCLUDE_CHAR_LIST
:
208 if ( !IsInCharIncludes(val
) )
209 errormsg
= _("'%s' is invalid");
212 case wxFILTER_EXCLUDE_CHAR_LIST
:
213 if ( !IsNotInCharExcludes(val
) )
214 errormsg
= _("'%s' is invalid");
218 wxFAIL_MSG("invalid text validator style");
221 if ( !errormsg
.empty() )
223 m_validatorWindow
->SetFocus();
226 buf
.Printf(errormsg
, val
.c_str());
228 wxMessageBox(buf
, _("Validation conflict"),
229 wxOK
| wxICON_EXCLAMATION
, parent
);
237 // Called to transfer data to the window
238 bool wxTextValidator::TransferToWindow(void)
242 wxTextEntry
* const text
= GetTextEntry();
246 text
->SetValue(*m_stringValue
);
252 // Called to transfer data to the window
253 bool wxTextValidator::TransferFromWindow(void)
257 wxTextEntry
* const text
= GetTextEntry();
261 *m_stringValue
= text
->GetValue();
267 bool wxTextValidator::IsInCharIncludes(const wxString
& val
)
270 for ( i
= 0; i
< val
.length(); i
++)
272 if (m_includes
.Index((wxString
) val
[i
]) == wxNOT_FOUND
)
278 bool wxTextValidator::IsNotInCharExcludes(const wxString
& val
)
281 for ( i
= 0; i
< val
.length(); i
++)
283 if (m_excludes
.Index((wxString
) val
[i
]) != wxNOT_FOUND
)
289 void wxTextValidator::OnChar(wxKeyEvent
& event
)
296 if ( m_validatorWindow
)
298 int keyCode
= event
.GetKeyCode();
300 // we don't filter special keys and Delete
302 !(keyCode
< WXK_SPACE
|| keyCode
== WXK_DELETE
|| keyCode
> WXK_START
) &&
304 ((m_validatorStyle
== wxFILTER_INCLUDE_CHAR_LIST
) && !IsInCharIncludes(wxString((wxChar
) keyCode
, 1))) ||
305 ((m_validatorStyle
== wxFILTER_EXCLUDE_CHAR_LIST
) && !IsNotInCharExcludes(wxString((wxChar
) keyCode
, 1))) ||
306 ((m_validatorStyle
== wxFILTER_ASCII
) && !isascii(keyCode
)) ||
307 ((m_validatorStyle
== wxFILTER_ALPHA
) && !wxIsalpha(keyCode
)) ||
308 ((m_validatorStyle
== wxFILTER_ALPHANUMERIC
) && !wxIsalnum(keyCode
)) ||
309 ((m_validatorStyle
== wxFILTER_NUMERIC
) && !wxIsdigit(keyCode
)
310 && keyCode
!= wxT('.') && keyCode
!= wxT(',') && keyCode
!= wxT('-') && keyCode
!= wxT('+')
311 && keyCode
!= wxT('e') && keyCode
!= wxT('E'))
315 if ( !wxValidator::IsSilent() )
328 // wxUSE_VALIDATORS && (wxUSE_TEXTCTRL || wxUSE_COMBOBOX)