]> git.saurik.com Git - wxWidgets.git/blob - src/common/valtext.cpp
added wxPersistentRegisterAndRestore() function to work around VC6 brokenness in...
[wxWidgets.git] / src / common / valtext.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/valtext.cpp
3 // Purpose: wxTextValidator
4 // Author: Julian Smart
5 // Modified by: Francesco Montorsi
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #if wxUSE_VALIDATORS && (wxUSE_TEXTCTRL || wxUSE_COMBOBOX)
20
21 #include "wx/valtext.h"
22
23 #ifndef WX_PRECOMP
24 #include <stdio.h>
25 #include "wx/textctrl.h"
26 #include "wx/combobox.h"
27 #include "wx/utils.h"
28 #include "wx/msgdlg.h"
29 #include "wx/intl.h"
30 #endif
31
32 #include <ctype.h>
33 #include <string.h>
34 #include <stdlib.h>
35
36 // ----------------------------------------------------------------------------
37 // global helpers
38 // ----------------------------------------------------------------------------
39
40 static bool wxIsNumeric(const wxString& val)
41 {
42 for ( wxString::const_iterator i = val.begin(); i != val.end(); ++i )
43 {
44 // Allow for "," (French) as well as "." -- in future we should
45 // use wxSystemSettings or other to do better localisation
46 if ((!wxIsdigit(*i)) && (*i != wxT('.')) && (*i != wxT(',')) && (*i != wxT('e')) &&
47 (*i != wxT('E')) && (*i != wxT('+')) && (*i != wxT('-')))
48 return false;
49 }
50 return true;
51 }
52
53 // ----------------------------------------------------------------------------
54 // wxTextValidator
55 // ----------------------------------------------------------------------------
56
57 IMPLEMENT_DYNAMIC_CLASS(wxTextValidator, wxValidator)
58 BEGIN_EVENT_TABLE(wxTextValidator, wxValidator)
59 EVT_CHAR(wxTextValidator::OnChar)
60 END_EVENT_TABLE()
61
62
63 #if WXWIN_COMPATIBILITY_2_8
64 wxTextValidator::wxTextValidator(long style, wxString *val)
65 {
66 m_validatorStyle = (wxTextValidatorStyle)style;
67 m_stringValue = val;
68 }
69
70 void wxTextValidator::SetStyle(long style)
71 {
72 SetStyle((wxTextValidatorStyle)style);
73 }
74 #endif
75
76 wxTextValidator::wxTextValidator(wxTextValidatorStyle style, wxString *val)
77 {
78 m_validatorStyle = style;
79 m_stringValue = val;
80 }
81
82 wxTextValidator::wxTextValidator(const wxTextValidator& val)
83 : wxValidator()
84 {
85 Copy(val);
86 }
87
88 bool wxTextValidator::Copy(const wxTextValidator& val)
89 {
90 wxValidator::Copy(val);
91
92 m_validatorStyle = val.m_validatorStyle;
93 m_stringValue = val.m_stringValue;
94
95 m_includes = val.m_includes;
96 m_excludes = val.m_excludes;
97
98 return true;
99 }
100
101 wxTextEntry *wxTextValidator::GetTextEntry()
102 {
103 #if wxUSE_TEXTCTRL
104 if (m_validatorWindow->IsKindOf(CLASSINFO(wxTextCtrl)))
105 {
106 return (wxTextCtrl*)m_validatorWindow;
107 }
108 #endif
109
110 #if wxUSE_COMBOBOX
111 if (m_validatorWindow->IsKindOf(CLASSINFO(wxComboBox)))
112 {
113 return (wxComboBox*)m_validatorWindow;
114 }
115 #endif
116
117 wxFAIL_MSG(
118 _T("wxTextValidator can only be used with wxTextCtrl or wxComboBox")
119 );
120
121 return NULL;
122 }
123
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)
127 {
128 // If window is disabled, simply return
129 if ( !m_validatorWindow->IsEnabled() )
130 return true;
131
132 wxTextEntry * const text = GetTextEntry();
133 if ( !text )
134 return false;
135
136 wxString val(text->GetValue());
137
138 // NB: this format string should always contain exactly one '%s'
139 wxString errormsg;
140 if (!IsValid(val, &errormsg))
141 {
142 wxASSERT(!errormsg.empty());
143
144 m_validatorWindow->SetFocus();
145
146 wxString buf;
147 buf.Printf(errormsg, val.c_str());
148
149 wxMessageBox(buf, _("Validation conflict"),
150 wxOK | wxICON_EXCLAMATION, parent);
151
152 return false;
153 }
154
155 return true;
156 }
157
158 // Called to transfer data to the window
159 bool wxTextValidator::TransferToWindow()
160 {
161 if ( m_stringValue )
162 {
163 wxTextEntry * const text = GetTextEntry();
164 if ( !text )
165 return false;
166
167 text->SetValue(*m_stringValue);
168 }
169
170 return true;
171 }
172
173 // Called to transfer data to the window
174 bool wxTextValidator::TransferFromWindow()
175 {
176 if ( m_stringValue )
177 {
178 wxTextEntry * const text = GetTextEntry();
179 if ( !text )
180 return false;
181
182 *m_stringValue = text->GetValue();
183 }
184
185 return true;
186 }
187
188 bool wxTextValidator::IsValid(const wxString& val, wxString* pErr) const
189 {
190 wxString errormsg;
191 switch (m_validatorStyle)
192 {
193 case wxFILTER_NONE:
194 // nothing to do...
195 break;
196
197 case wxFILTER_INCLUDE_LIST:
198 if ( m_includes.Index(val) == wxNOT_FOUND )
199 errormsg = _("'%s' is invalid");
200 break;
201
202 case wxFILTER_EXCLUDE_LIST:
203 if ( m_excludes.Index(val) != wxNOT_FOUND )
204 errormsg = _("'%s' is invalid");
205 break;
206
207 case wxFILTER_ASCII:
208 if ( !val.IsAscii() )
209 errormsg = _("'%s' should only contain ASCII characters.");
210 break;
211
212 case wxFILTER_ALPHA:
213 if ( !wxStringCheck<wxIsalpha>(val) )
214 errormsg = _("'%s' should only contain alphabetic characters.");
215 break;
216
217 case wxFILTER_ALPHANUMERIC:
218 if ( !wxStringCheck<wxIsalnum>(val) )
219 errormsg = _("'%s' should only contain alphabetic or numeric characters.");
220 break;
221
222 case wxFILTER_SIMPLE_NUMBER:
223 if ( !wxStringCheck<wxIsdigit>(val) )
224 errormsg = _("'%s' should be numeric.");
225 break;
226
227 case wxFILTER_NUMERIC:
228 if ( !wxIsNumeric(val) )
229 errormsg = _("'%s' should be numeric.");
230 break;
231
232 case wxFILTER_INCLUDE_CHAR_LIST:
233 if ( !ContainsOnlyIncludedCharacters(val) )
234 errormsg = _("'%s' is invalid");
235 break;
236
237 case wxFILTER_EXCLUDE_CHAR_LIST:
238 if ( ContainsExcludedCharacters(val) )
239 errormsg = _("'%s' is invalid");
240 break;
241
242 default:
243 wxFAIL_MSG("invalid text validator style");
244 }
245
246 if (pErr)
247 *pErr = errormsg;
248
249 return errormsg.empty();
250 }
251
252 bool wxTextValidator::ContainsOnlyIncludedCharacters(const wxString& val) const
253 {
254 for ( wxString::const_iterator i = val.begin(); i != val.end(); ++i )
255 if (m_includes.Index((wxString) *i) == wxNOT_FOUND)
256 // one character of 'val' is NOT present in m_includes...
257 return false;
258
259 // all characters of 'val' are present in m_includes
260 return true;
261 }
262
263 bool wxTextValidator::ContainsExcludedCharacters(const wxString& val) const
264 {
265 for ( wxString::const_iterator i = val.begin(); i != val.end(); ++i )
266 if (m_excludes.Index((wxString) *i) != wxNOT_FOUND)
267 // one character of 'val' is present in m_excludes...
268 return true;
269
270 // all characters of 'val' are NOT present in m_excludes
271 return false;
272 }
273
274 void wxTextValidator::SetCharIncludes(const wxString& chars)
275 {
276 wxArrayString arr;
277
278 for ( wxString::const_iterator i = chars.begin(); i != chars.end(); ++i )
279 arr.Add(*i);
280
281 SetIncludes(arr);
282 }
283
284 void wxTextValidator::SetCharExcludes(const wxString& chars)
285 {
286 wxArrayString arr;
287
288 for ( wxString::const_iterator i = chars.begin(); i != chars.end(); ++i )
289 arr.Add(*i);
290
291 SetExcludes(arr);
292 }
293
294 void wxTextValidator::OnChar(wxKeyEvent& event)
295 {
296 if (!m_validatorWindow)
297 {
298 event.Skip();
299 return;
300 }
301
302 int keyCode = event.GetKeyCode();
303
304 // we don't filter special keys and delete
305 if (keyCode < WXK_SPACE || keyCode == WXK_DELETE || keyCode >= WXK_START)
306 {
307 event.Skip();
308 return;
309 }
310
311 wxString str((wxUniChar)keyCode, 1);
312 if (!IsValid(str, NULL))
313 {
314 if ( !wxValidator::IsSilent() )
315 wxBell();
316
317 // eat message
318 return;
319 }
320 else
321 event.Skip();
322 }
323
324
325 #endif
326 // wxUSE_VALIDATORS && (wxUSE_TEXTCTRL || wxUSE_COMBOBOX)