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"
40 IMPLEMENT_DYNAMIC_CLASS(wxTextValidator
, wxValidator
)
42 BEGIN_EVENT_TABLE(wxTextValidator
, wxValidator
)
43 EVT_CHAR(wxTextValidator::OnChar
)
46 static bool wxIsNumeric(const wxString
& val
);
48 wxTextValidator::wxTextValidator(long style
, wxString
*val
)
50 m_validatorStyle
= style
;
53 m_refData = new wxVTextRefData;
55 M_VTEXTDATA->m_validatorStyle = style;
56 M_VTEXTDATA->m_stringValue = val;
60 wxTextValidator::wxTextValidator(const wxTextValidator
& val
)
66 bool wxTextValidator::Copy(const wxTextValidator
& val
)
68 wxValidator::Copy(val
);
70 m_validatorStyle
= val
.m_validatorStyle
;
71 m_stringValue
= val
.m_stringValue
;
73 m_includes
= val
.m_includes
;
74 m_excludes
= val
.m_excludes
;
79 wxTextEntry
*wxTextValidator::GetTextEntry()
82 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)))
84 return (wxTextCtrl
*)m_validatorWindow
;
89 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxComboBox
)))
91 return (wxComboBox
*)m_validatorWindow
;
96 _T("wxTextValidator can only be used with wxTextCtrl or wxComboBox")
102 static bool wxIsAlpha(const wxString
& val
)
105 for ( i
= 0; i
< (int)val
.length(); i
++)
107 if (!wxIsalpha(val
[i
]))
113 static bool wxIsAlphaNumeric(const wxString
& val
)
116 for ( i
= 0; i
< (int)val
.length(); i
++)
118 if (!wxIsalnum(val
[i
]))
124 // Called when the value in the window must be validated.
125 // This function can pop up an error message.
126 bool wxTextValidator::Validate(wxWindow
*parent
)
128 // If window is disabled, simply return
129 if ( !m_validatorWindow
->IsEnabled() )
132 wxTextEntry
* const text
= GetTextEntry();
136 wxString
val(text
->GetValue());
140 // NB: this format string should contian exactly one '%s'
143 bool includes
= (m_validatorStyle
& wxFILTER_INCLUDE_LIST
) != 0;
144 if ( includes
|| (m_validatorStyle
& wxFILTER_EXCLUDE_LIST
) )
146 // if includes, it's only ok to have the members of the list,
147 // otherwise it's only ok to have non-members
148 ok
= includes
== (m_includes
.Index(val
) != wxNOT_FOUND
);
151 errormsg
= _("'%s' is invalid");
154 else if ( (m_validatorStyle
& wxFILTER_ASCII
) && !val
.IsAscii() )
158 errormsg
= _("'%s' should only contain ASCII characters.");
160 else if ( (m_validatorStyle
& wxFILTER_ALPHA
) && !wxIsAlpha(val
) )
164 errormsg
= _("'%s' should only contain alphabetic characters.");
166 else if ( (m_validatorStyle
& wxFILTER_ALPHANUMERIC
) && !wxIsAlphaNumeric(val
))
170 errormsg
= _("'%s' should only contain alphabetic or numeric characters.");
172 else if ( (m_validatorStyle
& wxFILTER_NUMERIC
) && !wxIsNumeric(val
))
176 errormsg
= _("'%s' should be numeric.");
178 else if ( (m_validatorStyle
& wxFILTER_INCLUDE_CHAR_LIST
) && !IsInCharIncludes(val
))
180 //it's only ok to have the members of the list
181 errormsg
= _("'%s' is invalid");
184 else if ( (m_validatorStyle
& wxFILTER_EXCLUDE_CHAR_LIST
) && !IsNotInCharExcludes(val
))
186 // it's only ok to have non-members of the list
187 errormsg
= _("'%s' is invalid");
193 wxASSERT_MSG( !errormsg
.empty(), _T("you forgot to set errormsg") );
195 m_validatorWindow
->SetFocus();
198 buf
.Printf(errormsg
, val
.c_str());
200 wxMessageBox(buf
, _("Validation conflict"),
201 wxOK
| wxICON_EXCLAMATION
, parent
);
207 // Called to transfer data to the window
208 bool wxTextValidator::TransferToWindow(void)
212 wxTextEntry
* const text
= GetTextEntry();
216 text
->SetValue(*m_stringValue
);
222 // Called to transfer data to the window
223 bool wxTextValidator::TransferFromWindow(void)
227 wxTextEntry
* const text
= GetTextEntry();
231 *m_stringValue
= text
->GetValue();
237 bool wxTextValidator::IsInCharIncludes(const wxString
& val
)
240 for ( i
= 0; i
< val
.length(); i
++)
242 if (m_includes
.Index((wxString
) val
[i
]) == wxNOT_FOUND
)
248 bool wxTextValidator::IsNotInCharExcludes(const wxString
& val
)
251 for ( i
= 0; i
< val
.length(); i
++)
253 if (m_excludes
.Index((wxString
) val
[i
]) != wxNOT_FOUND
)
259 void wxTextValidator::OnChar(wxKeyEvent
& event
)
266 if ( m_validatorWindow
)
268 int keyCode
= event
.GetKeyCode();
270 // we don't filter special keys and Delete
272 !(keyCode
< WXK_SPACE
|| keyCode
== WXK_DELETE
|| keyCode
> WXK_START
) &&
274 ((m_validatorStyle
& wxFILTER_INCLUDE_CHAR_LIST
) && !IsInCharIncludes(wxString((wxChar
) keyCode
, 1))) ||
275 ((m_validatorStyle
& wxFILTER_EXCLUDE_CHAR_LIST
) && !IsNotInCharExcludes(wxString((wxChar
) keyCode
, 1))) ||
276 ((m_validatorStyle
& wxFILTER_ASCII
) && !isascii(keyCode
)) ||
277 ((m_validatorStyle
& wxFILTER_ALPHA
) && !wxIsalpha(keyCode
)) ||
278 ((m_validatorStyle
& wxFILTER_ALPHANUMERIC
) && !wxIsalnum(keyCode
)) ||
279 ((m_validatorStyle
& wxFILTER_NUMERIC
) && !wxIsdigit(keyCode
)
280 && keyCode
!= wxT('.') && keyCode
!= wxT(',') && keyCode
!= wxT('-') && keyCode
!= wxT('+') && keyCode
!= wxT('e') && keyCode
!= wxT('E'))
284 if ( !wxValidator::IsSilent() )
295 static bool wxIsNumeric(const wxString
& val
)
298 for ( i
= 0; i
< (int)val
.length(); i
++)
300 // Allow for "," (French) as well as "." -- in future we should
301 // use wxSystemSettings or other to do better localisation
302 if ((!wxIsdigit(val
[i
])) && (val
[i
] != wxT('.')) && (val
[i
] != wxT(',')) && (val
[i
] != wxT('e')) && (val
[i
] != wxT('E')) && (val
[i
] != wxT('+')) && (val
[i
] != wxT('-')))
310 // wxUSE_VALIDATORS && (wxUSE_TEXTCTRL || wxUSE_COMBOBOX)