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