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 IMPLEMENT_DYNAMIC_CLASS(wxTextValidator
, wxValidator
)
38 BEGIN_EVENT_TABLE(wxTextValidator
, wxValidator
)
39 EVT_CHAR(wxTextValidator::OnChar
)
42 static bool wxIsNumeric(const wxString
& val
);
44 wxTextValidator::wxTextValidator(long style
, wxString
*val
)
46 m_validatorStyle
= style
;
49 m_refData = new wxVTextRefData;
51 M_VTEXTDATA->m_validatorStyle = style;
52 M_VTEXTDATA->m_stringValue = val;
56 wxTextValidator::wxTextValidator(const wxTextValidator
& val
)
62 bool wxTextValidator::Copy(const wxTextValidator
& val
)
64 wxValidator::Copy(val
);
66 m_validatorStyle
= val
.m_validatorStyle
;
67 m_stringValue
= val
.m_stringValue
;
69 m_includes
= val
.m_includes
;
70 m_excludes
= val
.m_excludes
;
75 wxTextEntry
*wxTextValidator::GetTextEntry()
78 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)))
80 return (wxTextCtrl
*)m_validatorWindow
;
85 if (m_validatorWindow
->IsKindOf(CLASSINFO(wxComboBox
)))
87 return (wxComboBox
*)m_validatorWindow
;
92 _T("wxTextValidator can only be used with wxTextCtrl or wxComboBox")
98 static bool wxIsAlpha(const wxString
& val
)
101 for ( i
= 0; i
< (int)val
.length(); i
++)
103 if (!wxIsalpha(val
[i
]))
109 static bool wxIsAlphaNumeric(const wxString
& val
)
112 for ( i
= 0; i
< (int)val
.length(); i
++)
114 if (!wxIsalnum(val
[i
]))
120 // Called when the value in the window must be validated.
121 // This function can pop up an error message.
122 bool wxTextValidator::Validate(wxWindow
*parent
)
124 // If window is disabled, simply return
125 if ( !m_validatorWindow
->IsEnabled() )
128 wxTextEntry
* const text
= GetTextEntry();
132 wxString
val(text
->GetValue());
136 // NB: this format string should contian exactly one '%s'
139 bool includes
= (m_validatorStyle
& wxFILTER_INCLUDE_LIST
) != 0;
140 if ( includes
|| (m_validatorStyle
& wxFILTER_EXCLUDE_LIST
) )
142 // if includes, it's only ok to have the members of the list,
143 // otherwise it's only ok to have non-members
144 ok
= includes
== (m_includes
.Index(val
) != wxNOT_FOUND
);
147 errormsg
= _("'%s' is invalid");
150 else if ( (m_validatorStyle
& wxFILTER_ASCII
) && !val
.IsAscii() )
154 errormsg
= _("'%s' should only contain ASCII characters.");
156 else if ( (m_validatorStyle
& wxFILTER_ALPHA
) && !wxIsAlpha(val
) )
160 errormsg
= _("'%s' should only contain alphabetic characters.");
162 else if ( (m_validatorStyle
& wxFILTER_ALPHANUMERIC
) && !wxIsAlphaNumeric(val
))
166 errormsg
= _("'%s' should only contain alphabetic or numeric characters.");
168 else if ( (m_validatorStyle
& wxFILTER_NUMERIC
) && !wxIsNumeric(val
))
172 errormsg
= _("'%s' should be numeric.");
174 else if ( (m_validatorStyle
& wxFILTER_INCLUDE_CHAR_LIST
) && !IsInCharIncludes(val
))
176 //it's only ok to have the members of the list
177 errormsg
= _("'%s' is invalid");
180 else if ( (m_validatorStyle
& wxFILTER_EXCLUDE_CHAR_LIST
) && !IsNotInCharExcludes(val
))
182 // it's only ok to have non-members of the list
183 errormsg
= _("'%s' is invalid");
189 wxASSERT_MSG( !errormsg
.empty(), _T("you forgot to set errormsg") );
191 m_validatorWindow
->SetFocus();
194 buf
.Printf(errormsg
, val
.c_str());
196 wxMessageBox(buf
, _("Validation conflict"),
197 wxOK
| wxICON_EXCLAMATION
, parent
);
203 // Called to transfer data to the window
204 bool wxTextValidator::TransferToWindow(void)
208 wxTextEntry
* const text
= GetTextEntry();
212 text
->SetValue(*m_stringValue
);
218 // Called to transfer data to the window
219 bool wxTextValidator::TransferFromWindow(void)
223 wxTextEntry
* const text
= GetTextEntry();
227 *m_stringValue
= text
->GetValue();
233 bool wxTextValidator::IsInCharIncludes(const wxString
& val
)
236 for ( i
= 0; i
< val
.length(); i
++)
238 if (m_includes
.Index((wxString
) val
[i
]) == wxNOT_FOUND
)
244 bool wxTextValidator::IsNotInCharExcludes(const wxString
& val
)
247 for ( i
= 0; i
< val
.length(); i
++)
249 if (m_excludes
.Index((wxString
) val
[i
]) != wxNOT_FOUND
)
255 void wxTextValidator::OnChar(wxKeyEvent
& event
)
262 if ( m_validatorWindow
)
264 int keyCode
= event
.GetKeyCode();
266 // we don't filter special keys and Delete
268 !(keyCode
< WXK_SPACE
|| keyCode
== WXK_DELETE
|| keyCode
> WXK_START
) &&
270 ((m_validatorStyle
& wxFILTER_INCLUDE_CHAR_LIST
) && !IsInCharIncludes(wxString((wxChar
) keyCode
, 1))) ||
271 ((m_validatorStyle
& wxFILTER_EXCLUDE_CHAR_LIST
) && !IsNotInCharExcludes(wxString((wxChar
) keyCode
, 1))) ||
272 ((m_validatorStyle
& wxFILTER_ASCII
) && !isascii(keyCode
)) ||
273 ((m_validatorStyle
& wxFILTER_ALPHA
) && !wxIsalpha(keyCode
)) ||
274 ((m_validatorStyle
& wxFILTER_ALPHANUMERIC
) && !wxIsalnum(keyCode
)) ||
275 ((m_validatorStyle
& wxFILTER_NUMERIC
) && !wxIsdigit(keyCode
)
276 && keyCode
!= wxT('.') && keyCode
!= wxT(',') && keyCode
!= wxT('-') && keyCode
!= wxT('+') && keyCode
!= wxT('e') && keyCode
!= wxT('E'))
280 if ( !wxValidator::IsSilent() )
291 static bool wxIsNumeric(const wxString
& val
)
294 for ( i
= 0; i
< (int)val
.length(); i
++)
296 // Allow for "," (French) as well as "." -- in future we should
297 // use wxSystemSettings or other to do better localisation
298 if ((!wxIsdigit(val
[i
])) && (val
[i
] != wxT('.')) && (val
[i
] != wxT(',')) && (val
[i
] != wxT('e')) && (val
[i
] != wxT('E')) && (val
[i
] != wxT('+')) && (val
[i
] != wxT('-')))
306 // wxUSE_VALIDATORS && (wxUSE_TEXTCTRL || wxUSE_COMBOBOX)