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
;
88 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxComboBox
)))
90 return (wxComboBox
*)m_validatorWindow
;
95 _T("wxTextValidator can only be used with wxTextCtrl or wxComboBox")
101 static bool wxIsAlpha(const wxString
& val
)
104 for ( i
= 0; i
< (int)val
.length(); i
++)
106 if (!wxIsalpha(val
[i
]))
112 static bool wxIsAlphaNumeric(const wxString
& val
)
115 for ( i
= 0; i
< (int)val
.length(); i
++)
117 if (!wxIsalnum(val
[i
]))
123 // Called when the value in the window must be validated.
124 // This function can pop up an error message.
125 bool wxTextValidator::Validate(wxWindow
*parent
)
127 // If window is disabled, simply return
128 if ( !m_validatorWindow
->IsEnabled() )
131 wxTextEntry
* const text
= GetTextEntry();
135 wxString
val(text
->GetValue());
139 // NB: this format string should contian exactly one '%s'
142 bool includes
= (m_validatorStyle
& wxFILTER_INCLUDE_LIST
) != 0;
143 if ( includes
|| (m_validatorStyle
& wxFILTER_EXCLUDE_LIST
) )
145 // if includes, it's only ok to have the members of the list,
146 // otherwise it's only ok to have non-members
147 ok
= includes
== (m_includes
.Index(val
) != wxNOT_FOUND
);
150 errormsg
= _("'%s' is invalid");
153 else if ( (m_validatorStyle
& wxFILTER_ASCII
) && !val
.IsAscii() )
157 errormsg
= _("'%s' should only contain ASCII characters.");
159 else if ( (m_validatorStyle
& wxFILTER_ALPHA
) && !wxIsAlpha(val
) )
163 errormsg
= _("'%s' should only contain alphabetic characters.");
165 else if ( (m_validatorStyle
& wxFILTER_ALPHANUMERIC
) && !wxIsAlphaNumeric(val
))
169 errormsg
= _("'%s' should only contain alphabetic or numeric characters.");
171 else if ( (m_validatorStyle
& wxFILTER_NUMERIC
) && !wxIsNumeric(val
))
175 errormsg
= _("'%s' should be numeric.");
177 else if ( (m_validatorStyle
& wxFILTER_INCLUDE_CHAR_LIST
) && !IsInCharIncludes(val
))
179 //it's only ok to have the members of the list
180 errormsg
= _("'%s' is invalid");
183 else if ( (m_validatorStyle
& wxFILTER_EXCLUDE_CHAR_LIST
) && !IsNotInCharExcludes(val
))
185 // it's only ok to have non-members of the list
186 errormsg
= _("'%s' is invalid");
192 wxASSERT_MSG( !errormsg
.empty(), _T("you forgot to set errormsg") );
194 m_validatorWindow
->SetFocus();
197 buf
.Printf(errormsg
, val
.c_str());
199 wxMessageBox(buf
, _("Validation conflict"),
200 wxOK
| wxICON_EXCLAMATION
, parent
);
206 // Called to transfer data to the window
207 bool wxTextValidator::TransferToWindow(void)
211 wxTextEntry
* const text
= GetTextEntry();
215 text
->SetValue(*m_stringValue
);
221 // Called to transfer data to the window
222 bool wxTextValidator::TransferFromWindow(void)
226 wxTextEntry
* const text
= GetTextEntry();
230 *m_stringValue
= text
->GetValue();
236 bool wxTextValidator::IsInCharIncludes(const wxString
& val
)
239 for ( i
= 0; i
< val
.length(); i
++)
241 if (m_includes
.Index((wxString
) val
[i
]) == wxNOT_FOUND
)
247 bool wxTextValidator::IsNotInCharExcludes(const wxString
& val
)
250 for ( i
= 0; i
< val
.length(); i
++)
252 if (m_excludes
.Index((wxString
) val
[i
]) != wxNOT_FOUND
)
258 void wxTextValidator::OnChar(wxKeyEvent
& event
)
265 if ( m_validatorWindow
)
267 int keyCode
= event
.GetKeyCode();
269 // we don't filter special keys and Delete
271 !(keyCode
< WXK_SPACE
|| keyCode
== WXK_DELETE
|| keyCode
> WXK_START
) &&
273 ((m_validatorStyle
& wxFILTER_INCLUDE_CHAR_LIST
) && !IsInCharIncludes(wxString((wxChar
) keyCode
, 1))) ||
274 ((m_validatorStyle
& wxFILTER_EXCLUDE_CHAR_LIST
) && !IsNotInCharExcludes(wxString((wxChar
) keyCode
, 1))) ||
275 ((m_validatorStyle
& wxFILTER_ASCII
) && !isascii(keyCode
)) ||
276 ((m_validatorStyle
& wxFILTER_ALPHA
) && !wxIsalpha(keyCode
)) ||
277 ((m_validatorStyle
& wxFILTER_ALPHANUMERIC
) && !wxIsalnum(keyCode
)) ||
278 ((m_validatorStyle
& wxFILTER_NUMERIC
) && !wxIsdigit(keyCode
)
279 && keyCode
!= wxT('.') && keyCode
!= wxT(',') && keyCode
!= wxT('-') && keyCode
!= wxT('+') && keyCode
!= wxT('e') && keyCode
!= wxT('E'))
283 if ( !wxValidator::IsSilent() )
294 static bool wxIsNumeric(const wxString
& val
)
297 for ( i
= 0; i
< (int)val
.length(); i
++)
299 // Allow for "," (French) as well as "." -- in future we should
300 // use wxSystemSettings or other to do better localisation
301 if ((!wxIsdigit(val
[i
])) && (val
[i
] != wxT('.')) && (val
[i
] != wxT(',')) && (val
[i
] != wxT('e')) && (val
[i
] != wxT('E')) && (val
[i
] != wxT('+')) && (val
[i
] != wxT('-')))
309 // wxUSE_VALIDATORS && (wxUSE_TEXTCTRL || wxUSE_COMBOBOX)