]>
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 and Markus Holzem | |
9 | // Licence: wxWindows license | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #ifdef __GNUG__ | |
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 | |
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 | { | |
65 | Copy(val); | |
66 | } | |
67 | ||
68 | bool wxTextValidator::Copy(const wxTextValidator& val) | |
69 | { | |
70 | wxValidator::Copy(val); | |
71 | ||
72 | m_validatorStyle = val.m_validatorStyle ; | |
73 | m_stringValue = val.m_stringValue ; | |
74 | ||
75 | wxNode *node = val.m_includeList.First() ; | |
76 | while ( node ) | |
77 | { | |
78 | wxChar *s = (wxChar *)node->Data(); | |
79 | m_includeList.Add(s); | |
80 | node = node->Next(); | |
81 | } | |
82 | node = val.m_excludeList.First() ; | |
83 | while ( node ) | |
84 | { | |
85 | wxChar *s = (wxChar *)node->Data(); | |
86 | m_excludeList.Add(s); | |
87 | node = node->Next(); | |
88 | } | |
89 | return TRUE; | |
90 | } | |
91 | ||
92 | wxTextValidator::~wxTextValidator() | |
93 | { | |
94 | } | |
95 | ||
96 | static bool wxIsAlpha(const wxString& val) | |
97 | { | |
98 | int i; | |
99 | for ( i = 0; i < (int)val.Length(); i++) | |
100 | { | |
101 | if (!wxIsalpha(val[i])) | |
102 | return FALSE; | |
103 | } | |
104 | return TRUE; | |
105 | } | |
106 | ||
107 | static bool wxIsAlphaNumeric(const wxString& val) | |
108 | { | |
109 | int i; | |
110 | for ( i = 0; i < (int)val.Length(); i++) | |
111 | { | |
112 | if (!wxIsalnum(val[i])) | |
113 | return FALSE; | |
114 | } | |
115 | return TRUE; | |
116 | } | |
117 | ||
118 | // Called when the value in the window must be validated. | |
119 | // This function can pop up an error message. | |
120 | bool wxTextValidator::Validate(wxWindow *parent) | |
121 | { | |
122 | if( !CheckValidator() ) | |
123 | return FALSE; | |
124 | ||
125 | wxTextCtrl *control = (wxTextCtrl *) m_validatorWindow ; | |
126 | ||
127 | // If window is disabled, simply return | |
128 | if ( !control->IsEnabled() ) | |
129 | return TRUE; | |
130 | ||
131 | wxString val(control->GetValue()); | |
132 | ||
133 | bool ok = TRUE; | |
134 | ||
135 | // NB: this format string should contian exactly one '%s' | |
136 | wxString errormsg; | |
137 | ||
138 | bool includeList = (m_validatorStyle & wxFILTER_INCLUDE_LIST) != 0; | |
139 | if ( includeList || (m_validatorStyle & wxFILTER_EXCLUDE_LIST) ) | |
140 | { | |
141 | // if includeList, it's only ok to have the members of the list, | |
142 | // otherwise it's only ok to have non-members | |
143 | ok = includeList == m_includeList.Member(val); | |
144 | if ( !ok ) | |
145 | { | |
146 | errormsg = _("'%s' is invalid"); | |
147 | } | |
148 | } | |
149 | else if ( (m_validatorStyle & wxFILTER_ASCII) && !val.IsAscii() ) | |
150 | { | |
151 | ok = FALSE; | |
152 | ||
153 | errormsg = _("'%s' should only contain ASCII characters."); | |
154 | } | |
155 | else if ( (m_validatorStyle & wxFILTER_ALPHA) && !wxIsAlpha(val) ) | |
156 | { | |
157 | ok = FALSE; | |
158 | ||
159 | errormsg = _("'%s' should only contain alphabetic characters."); | |
160 | } | |
161 | else if ( (m_validatorStyle & wxFILTER_ALPHANUMERIC) && !wxIsAlphaNumeric(val)) | |
162 | { | |
163 | ok = FALSE; | |
164 | ||
165 | errormsg = _("'%s' should only contain alphabetic or numeric characters."); | |
166 | } | |
167 | else if ( (m_validatorStyle & wxFILTER_NUMERIC) && !wxIsNumeric(val)) | |
168 | { | |
169 | ok = FALSE; | |
170 | ||
171 | errormsg = _("'%s' should be numeric."); | |
172 | } | |
173 | else if ( (m_validatorStyle & wxFILTER_INCLUDE_CHAR_LIST) && !IsInCharIncludeList(val)) | |
174 | { | |
175 | //it's only ok to have the members of the list | |
176 | errormsg = _("'%s' is invalid"); | |
177 | ok = FALSE; | |
178 | } | |
179 | else if ( (m_validatorStyle & wxFILTER_EXCLUDE_CHAR_LIST) && !IsNotInCharExcludeList(val)) | |
180 | { | |
181 | // it's only ok to have non-members of the list | |
182 | errormsg = _("'%s' is invalid"); | |
183 | ok = FALSE; | |
184 | } | |
185 | ||
186 | if ( !ok ) | |
187 | { | |
188 | wxASSERT_MSG( !errormsg.empty(), _T("you forgot to set errormsg") ); | |
189 | ||
190 | m_validatorWindow->SetFocus(); | |
191 | ||
192 | wxString buf; | |
193 | buf.Printf(errormsg, val.c_str()); | |
194 | ||
195 | wxMessageBox(buf, _("Validation conflict"), | |
196 | wxOK | wxICON_EXCLAMATION, parent); | |
197 | } | |
198 | ||
199 | return ok; | |
200 | } | |
201 | ||
202 | // Called to transfer data to the window | |
203 | bool wxTextValidator::TransferToWindow(void) | |
204 | { | |
205 | if( !CheckValidator() ) | |
206 | return FALSE; | |
207 | ||
208 | wxTextCtrl *control = (wxTextCtrl *) m_validatorWindow ; | |
209 | control->SetValue(* m_stringValue) ; | |
210 | ||
211 | return TRUE; | |
212 | } | |
213 | ||
214 | // Called to transfer data to the window | |
215 | bool wxTextValidator::TransferFromWindow(void) | |
216 | { | |
217 | if( !CheckValidator() ) | |
218 | return FALSE; | |
219 | ||
220 | wxTextCtrl *control = (wxTextCtrl *) m_validatorWindow ; | |
221 | * m_stringValue = control->GetValue() ; | |
222 | ||
223 | return TRUE; | |
224 | } | |
225 | ||
226 | void wxTextValidator::SetIncludeList(const wxStringList& list) | |
227 | { | |
228 | /* | |
229 | if ( !M_VTEXTDATA ) | |
230 | return; | |
231 | */ | |
232 | ||
233 | m_includeList.Clear(); | |
234 | // TODO: replace with = | |
235 | wxNode *node = list.First() ; | |
236 | while ( node ) | |
237 | { | |
238 | wxChar *s = (wxChar *)node->Data(); | |
239 | m_includeList.Add(s); | |
240 | node = node->Next(); | |
241 | } | |
242 | } | |
243 | ||
244 | void wxTextValidator::SetExcludeList(const wxStringList& list) | |
245 | { | |
246 | /* | |
247 | if ( !M_VTEXTDATA ) | |
248 | return; | |
249 | */ | |
250 | ||
251 | m_excludeList.Clear(); | |
252 | // TODO: replace with = | |
253 | wxNode *node = list.First() ; | |
254 | while ( node ) | |
255 | { | |
256 | wxChar *s = (wxChar *)node->Data(); | |
257 | m_excludeList.Add(s); | |
258 | node = node->Next(); | |
259 | } | |
260 | } | |
261 | ||
262 | void wxTextValidator::OnChar(wxKeyEvent& event) | |
263 | { | |
264 | /* | |
265 | if ( !M_VTEXTDATA ) | |
266 | return; | |
267 | */ | |
268 | ||
269 | if ( m_validatorWindow ) | |
270 | { | |
271 | int keyCode = (int)event.KeyCode(); | |
272 | ||
273 | // we don't filter special keys and Delete | |
274 | if ( | |
275 | !(keyCode < WXK_SPACE || keyCode == WXK_DELETE || keyCode > WXK_START) && | |
276 | ( | |
277 | ((m_validatorStyle & wxFILTER_INCLUDE_CHAR_LIST) && !IsInCharIncludeList(wxString((char) keyCode, 1))) || | |
278 | ((m_validatorStyle & wxFILTER_EXCLUDE_CHAR_LIST) && !IsNotInCharExcludeList(wxString((char) keyCode, 1))) || | |
279 | ((m_validatorStyle & wxFILTER_ASCII) && !isascii(keyCode)) || | |
280 | ((m_validatorStyle & wxFILTER_ALPHA) && !wxIsalpha(keyCode)) || | |
281 | ((m_validatorStyle & wxFILTER_ALPHANUMERIC) && !wxIsalnum(keyCode)) || | |
282 | ((m_validatorStyle & wxFILTER_NUMERIC) && !wxIsdigit(keyCode) | |
283 | && keyCode != '.' && keyCode != ',' && keyCode != '-') | |
284 | ) | |
285 | ) | |
286 | { | |
287 | if ( !wxValidator::IsSilent() ) | |
288 | wxBell(); | |
289 | ||
290 | // eat message | |
291 | return; | |
292 | } | |
293 | } | |
294 | ||
295 | event.Skip(); | |
296 | } | |
297 | ||
298 | static bool wxIsNumeric(const wxString& val) | |
299 | { | |
300 | int i; | |
301 | for ( i = 0; i < (int)val.Length(); i++) | |
302 | { | |
303 | // Allow for "," (French) as well as "." -- in future we should | |
304 | // use wxSystemSettings or other to do better localisation | |
305 | if ((!isdigit(val[i])) && (val[i] != '.') && (val[i] != ',')) | |
306 | if(!((i == 0) && (val[i] == '-'))) | |
307 | return FALSE; | |
308 | } | |
309 | return TRUE; | |
310 | } | |
311 | ||
312 | bool wxTextValidator::IsInCharIncludeList(const wxString& val) | |
313 | { | |
314 | size_t i; | |
315 | for ( i = 0; i < val.Length(); i++) | |
316 | { | |
317 | if (!m_includeList.Member((wxString) val[i])) | |
318 | return FALSE; | |
319 | } | |
320 | return TRUE; | |
321 | } | |
322 | ||
323 | bool wxTextValidator::IsNotInCharExcludeList(const wxString& val) | |
324 | { | |
325 | size_t i; | |
326 | for ( i = 0; i < val.Length(); i++) | |
327 | { | |
328 | if (m_excludeList.Member((wxString) val[i])) | |
329 | return FALSE; | |
330 | } | |
331 | return TRUE; | |
332 | } | |
333 | ||
334 | #endif | |
335 | // wxUSE_VALIDATORS |