]>
Commit | Line | Data |
---|---|---|
c801d85f KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: valtext.cpp | |
3 | // Purpose: wxTextValidator | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
55d99c7a | 8 | // Copyright: (c) Julian Smart |
65571936 | 9 | // Licence: wxWindows licence |
c801d85f KB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
14f355c2 | 12 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
ce4169a4 | 13 | #pragma implementation "valtext.h" |
c801d85f KB |
14 | #endif |
15 | ||
16 | // For compilers that support precompilation, includes "wx.h". | |
17 | #include "wx/wxprec.h" | |
18 | ||
19 | #ifdef __BORLANDC__ | |
ce4169a4 | 20 | #pragma hdrstop |
c801d85f KB |
21 | #endif |
22 | ||
d7260478 | 23 | #if wxUSE_VALIDATORS && wxUSE_TEXTCTRL |
ce4169a4 RR |
24 | |
25 | #ifndef WX_PRECOMP | |
26 | #include <stdio.h> | |
27 | #include "wx/textctrl.h" | |
28 | #include "wx/utils.h" | |
29 | #include "wx/msgdlg.h" | |
30 | #include "wx/intl.h" | |
c801d85f KB |
31 | #endif |
32 | ||
33 | #include "wx/valtext.h" | |
34 | ||
35 | #include <ctype.h> | |
36 | #include <string.h> | |
37 | #include <stdlib.h> | |
38 | ||
ce3ed50d | 39 | #ifdef __SALFORDC__ |
a994f81b | 40 | #include <clib.h> |
ce3ed50d JS |
41 | #endif |
42 | ||
c801d85f KB |
43 | IMPLEMENT_DYNAMIC_CLASS(wxTextValidator, wxValidator) |
44 | ||
45 | BEGIN_EVENT_TABLE(wxTextValidator, wxValidator) | |
a994f81b | 46 | EVT_CHAR(wxTextValidator::OnChar) |
c801d85f | 47 | END_EVENT_TABLE() |
c801d85f | 48 | |
386af6a2 JS |
49 | static bool wxIsNumeric(const wxString& val); |
50 | ||
debe6624 | 51 | wxTextValidator::wxTextValidator(long style, wxString *val) |
c801d85f | 52 | { |
52cd14b1 VZ |
53 | m_validatorStyle = style; |
54 | m_stringValue = val; | |
c801d85f KB |
55 | /* |
56 | m_refData = new wxVTextRefData; | |
57 | ||
52cd14b1 VZ |
58 | M_VTEXTDATA->m_validatorStyle = style; |
59 | M_VTEXTDATA->m_stringValue = val; | |
c801d85f KB |
60 | */ |
61 | } | |
62 | ||
63 | wxTextValidator::wxTextValidator(const wxTextValidator& val) | |
d84afea9 | 64 | : wxValidator() |
c801d85f KB |
65 | { |
66 | Copy(val); | |
67 | } | |
68 | ||
69 | bool wxTextValidator::Copy(const wxTextValidator& val) | |
70 | { | |
71 | wxValidator::Copy(val); | |
72 | ||
52cd14b1 VZ |
73 | m_validatorStyle = val.m_validatorStyle; |
74 | m_stringValue = val.m_stringValue; | |
a994f81b | 75 | |
f94a790d RN |
76 | m_includes = val.m_includes; |
77 | m_excludes = val.m_excludes; | |
52cd14b1 | 78 | |
cab1a605 | 79 | return true; |
c801d85f KB |
80 | } |
81 | ||
c801d85f KB |
82 | static bool wxIsAlpha(const wxString& val) |
83 | { | |
a994f81b VZ |
84 | int i; |
85 | for ( i = 0; i < (int)val.Length(); i++) | |
86 | { | |
f6bcfd97 | 87 | if (!wxIsalpha(val[i])) |
cab1a605 | 88 | return false; |
a994f81b | 89 | } |
cab1a605 | 90 | return true; |
c801d85f KB |
91 | } |
92 | ||
93 | static bool wxIsAlphaNumeric(const wxString& val) | |
94 | { | |
a994f81b VZ |
95 | int i; |
96 | for ( i = 0; i < (int)val.Length(); i++) | |
97 | { | |
f6bcfd97 | 98 | if (!wxIsalnum(val[i])) |
cab1a605 | 99 | return false; |
a994f81b | 100 | } |
cab1a605 | 101 | return true; |
c801d85f KB |
102 | } |
103 | ||
104 | // Called when the value in the window must be validated. | |
105 | // This function can pop up an error message. | |
106 | bool wxTextValidator::Validate(wxWindow *parent) | |
107 | { | |
33c5b54b | 108 | if( !CheckValidator() ) |
cab1a605 | 109 | return false; |
a994f81b | 110 | |
52cd14b1 | 111 | wxTextCtrl *control = (wxTextCtrl *) m_validatorWindow; |
a994f81b | 112 | |
6cd47507 | 113 | // If window is disabled, simply return |
f03fc89f | 114 | if ( !control->IsEnabled() ) |
cab1a605 | 115 | return true; |
a994f81b VZ |
116 | |
117 | wxString val(control->GetValue()); | |
118 | ||
cab1a605 | 119 | bool ok = true; |
a994f81b | 120 | |
e9086757 VZ |
121 | // NB: this format string should contian exactly one '%s' |
122 | wxString errormsg; | |
a994f81b | 123 | |
f94a790d RN |
124 | bool includes = (m_validatorStyle & wxFILTER_INCLUDE_LIST) != 0; |
125 | if ( includes || (m_validatorStyle & wxFILTER_EXCLUDE_LIST) ) | |
a994f81b | 126 | { |
f94a790d | 127 | // if includes, it's only ok to have the members of the list, |
e9086757 | 128 | // otherwise it's only ok to have non-members |
f94a790d | 129 | ok = includes == (m_includes.Index(val) != wxNOT_FOUND); |
e9086757 | 130 | if ( !ok ) |
a994f81b | 131 | { |
e9086757 | 132 | errormsg = _("'%s' is invalid"); |
a994f81b VZ |
133 | } |
134 | } | |
135 | else if ( (m_validatorStyle & wxFILTER_ASCII) && !val.IsAscii() ) | |
136 | { | |
cab1a605 | 137 | ok = false; |
a994f81b VZ |
138 | |
139 | errormsg = _("'%s' should only contain ASCII characters."); | |
140 | } | |
141 | else if ( (m_validatorStyle & wxFILTER_ALPHA) && !wxIsAlpha(val) ) | |
142 | { | |
cab1a605 | 143 | ok = false; |
a994f81b VZ |
144 | |
145 | errormsg = _("'%s' should only contain alphabetic characters."); | |
146 | } | |
147 | else if ( (m_validatorStyle & wxFILTER_ALPHANUMERIC) && !wxIsAlphaNumeric(val)) | |
148 | { | |
cab1a605 | 149 | ok = false; |
a994f81b VZ |
150 | |
151 | errormsg = _("'%s' should only contain alphabetic or numeric characters."); | |
152 | } | |
153 | else if ( (m_validatorStyle & wxFILTER_NUMERIC) && !wxIsNumeric(val)) | |
154 | { | |
cab1a605 | 155 | ok = false; |
a994f81b VZ |
156 | |
157 | errormsg = _("'%s' should be numeric."); | |
158 | } | |
f94a790d | 159 | else if ( (m_validatorStyle & wxFILTER_INCLUDE_CHAR_LIST) && !IsInCharIncludes(val)) |
aaae8296 JS |
160 | { |
161 | //it's only ok to have the members of the list | |
162 | errormsg = _("'%s' is invalid"); | |
cab1a605 | 163 | ok = false; |
aaae8296 | 164 | } |
f94a790d | 165 | else if ( (m_validatorStyle & wxFILTER_EXCLUDE_CHAR_LIST) && !IsNotInCharExcludes(val)) |
aaae8296 JS |
166 | { |
167 | // it's only ok to have non-members of the list | |
168 | errormsg = _("'%s' is invalid"); | |
cab1a605 | 169 | ok = false; |
aaae8296 | 170 | } |
a994f81b VZ |
171 | |
172 | if ( !ok ) | |
173 | { | |
e9086757 VZ |
174 | wxASSERT_MSG( !errormsg.empty(), _T("you forgot to set errormsg") ); |
175 | ||
a994f81b VZ |
176 | m_validatorWindow->SetFocus(); |
177 | ||
178 | wxString buf; | |
179 | buf.Printf(errormsg, val.c_str()); | |
180 | ||
181 | wxMessageBox(buf, _("Validation conflict"), | |
182 | wxOK | wxICON_EXCLAMATION, parent); | |
183 | } | |
184 | ||
185 | return ok; | |
c801d85f KB |
186 | } |
187 | ||
188 | // Called to transfer data to the window | |
189 | bool wxTextValidator::TransferToWindow(void) | |
190 | { | |
33c5b54b | 191 | if( !CheckValidator() ) |
cab1a605 | 192 | return false; |
c801d85f | 193 | |
52cd14b1 VZ |
194 | if ( m_stringValue ) |
195 | { | |
196 | wxTextCtrl *control = (wxTextCtrl *) m_validatorWindow; | |
197 | control->SetValue(* m_stringValue); | |
198 | } | |
c801d85f | 199 | |
cab1a605 | 200 | return true; |
c801d85f KB |
201 | } |
202 | ||
203 | // Called to transfer data to the window | |
204 | bool wxTextValidator::TransferFromWindow(void) | |
205 | { | |
33c5b54b | 206 | if( !CheckValidator() ) |
cab1a605 | 207 | return false; |
c801d85f | 208 | |
52cd14b1 VZ |
209 | if ( m_stringValue ) |
210 | { | |
211 | wxTextCtrl *control = (wxTextCtrl *) m_validatorWindow; | |
212 | *m_stringValue = control->GetValue(); | |
213 | } | |
c801d85f | 214 | |
cab1a605 | 215 | return true; |
c801d85f KB |
216 | } |
217 | ||
f94a790d RN |
218 | #if WXWIN_COMPATIBILITY_2_4 |
219 | ||
220 | inline void wxCopyStringListToArrayString(wxArrayString& to, const wxStringList& from) | |
221 | { | |
222 | to.Clear(); | |
223 | ||
80676593 VZ |
224 | for ( wxStringList::compatibility_iterator pNode = from.GetFirst(); |
225 | pNode; | |
226 | pNode = pNode->GetNext() ) | |
227 | { | |
f94a790d | 228 | to.Add(pNode->GetData()); |
80676593 | 229 | } |
f94a790d RN |
230 | } |
231 | ||
232 | inline void wxCopyArrayStringToStringList(wxStringList& to, const wxArrayString& from) | |
233 | { | |
234 | to.Clear(); | |
235 | ||
236 | for(size_t i = 0; i < from.GetCount(); ++i) | |
237 | to.Add(from[i]); | |
238 | } | |
239 | ||
240 | wxStringList& wxTextValidator::GetIncludeList() | |
241 | { | |
242 | wxCopyArrayStringToStringList(m_includeList, m_includes); | |
243 | return m_includeList; | |
244 | } | |
245 | ||
246 | wxStringList& wxTextValidator::GetExcludeList() | |
247 | { | |
248 | wxCopyArrayStringToStringList(m_excludeList, m_excludes); | |
249 | return m_excludeList; | |
250 | } | |
251 | ||
c801d85f KB |
252 | void wxTextValidator::SetIncludeList(const wxStringList& list) |
253 | { | |
f94a790d | 254 | wxCopyStringListToArrayString(m_includes, list); |
c801d85f KB |
255 | } |
256 | ||
257 | void wxTextValidator::SetExcludeList(const wxStringList& list) | |
258 | { | |
f94a790d RN |
259 | wxCopyStringListToArrayString(m_excludes, list); |
260 | } | |
261 | ||
262 | bool wxTextValidator::IsInCharIncludeList(const wxString& val) | |
263 | { | |
264 | return IsInCharIncludes(val); | |
265 | } | |
266 | ||
267 | bool wxTextValidator::IsNotInCharExcludeList(const wxString& val) | |
268 | { | |
269 | return IsNotInCharExcludes(val); | |
270 | } | |
271 | ||
272 | #endif //compat 2.4 | |
273 | ||
274 | ||
275 | bool wxTextValidator::IsInCharIncludes(const wxString& val) | |
276 | { | |
277 | size_t i; | |
278 | for ( i = 0; i < val.Length(); i++) | |
279 | { | |
280 | if (m_includes.Index((wxString) val[i]) == wxNOT_FOUND) | |
281 | return false; | |
282 | } | |
283 | return true; | |
284 | } | |
285 | ||
286 | bool wxTextValidator::IsNotInCharExcludes(const wxString& val) | |
287 | { | |
288 | size_t i; | |
289 | for ( i = 0; i < val.Length(); i++) | |
290 | { | |
291 | if (m_excludes.Index((wxString) val[i]) != wxNOT_FOUND) | |
292 | return false; | |
293 | } | |
294 | return true; | |
c801d85f KB |
295 | } |
296 | ||
297 | void wxTextValidator::OnChar(wxKeyEvent& event) | |
298 | { | |
299 | /* | |
a994f81b VZ |
300 | if ( !M_VTEXTDATA ) |
301 | return; | |
c801d85f KB |
302 | */ |
303 | ||
a994f81b VZ |
304 | if ( m_validatorWindow ) |
305 | { | |
12a3f227 | 306 | int keyCode = event.GetKeyCode(); |
a994f81b VZ |
307 | |
308 | // we don't filter special keys and Delete | |
309 | if ( | |
310 | !(keyCode < WXK_SPACE || keyCode == WXK_DELETE || keyCode > WXK_START) && | |
311 | ( | |
f94a790d RN |
312 | ((m_validatorStyle & wxFILTER_INCLUDE_CHAR_LIST) && !IsInCharIncludes(wxString((wxChar) keyCode, 1))) || |
313 | ((m_validatorStyle & wxFILTER_EXCLUDE_CHAR_LIST) && !IsNotInCharExcludes(wxString((wxChar) keyCode, 1))) || | |
b1e4e7cc | 314 | ((m_validatorStyle & wxFILTER_ASCII) && !isascii(keyCode)) || |
0c6099b7 RN |
315 | ((m_validatorStyle & wxFILTER_ALPHA) && !wxIsalpha(keyCode)) || |
316 | ((m_validatorStyle & wxFILTER_ALPHANUMERIC) && !wxIsalnum(keyCode)) || | |
317 | ((m_validatorStyle & wxFILTER_NUMERIC) && !wxIsdigit(keyCode) | |
543f08a6 | 318 | && keyCode != '.' && keyCode != ',' && keyCode != '-') |
a994f81b VZ |
319 | ) |
320 | ) | |
321 | { | |
322 | if ( !wxValidator::IsSilent() ) | |
323 | wxBell(); | |
324 | ||
325 | // eat message | |
326 | return; | |
327 | } | |
328 | } | |
329 | ||
330 | event.Skip(); | |
c801d85f KB |
331 | } |
332 | ||
386af6a2 JS |
333 | static bool wxIsNumeric(const wxString& val) |
334 | { | |
a994f81b VZ |
335 | int i; |
336 | for ( i = 0; i < (int)val.Length(); i++) | |
337 | { | |
543f08a6 JS |
338 | // Allow for "," (French) as well as "." -- in future we should |
339 | // use wxSystemSettings or other to do better localisation | |
1c193821 | 340 | if ((!wxIsdigit(val[i])) && (val[i] != '.') && (val[i] != ',') && (val[i] != wxT('e')) && (val[i] != wxT('E')) && (val[i] != wxT('+')) && (val[i] != wxT('-'))) |
cab1a605 | 341 | return false; |
a994f81b | 342 | } |
cab1a605 | 343 | return true; |
386af6a2 | 344 | } |
c801d85f | 345 | |
aaae8296 | 346 | |
ce4169a4 | 347 | #endif |
d7260478 | 348 | // wxUSE_VALIDATORS && wxUSE_TEXTCTRL |