]>
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$ | |
8 | // Copyright: (c) Julian Smart and Markus Holzem | |
a994f81b | 9 | // Licence: wxWindows license |
c801d85f KB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | #ifdef __GNUG__ | |
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 | ||
ce4169a4 RR |
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" | |
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 | { |
a994f81b VZ |
53 | m_validatorStyle = style ; |
54 | m_stringValue = val ; | |
c801d85f KB |
55 | /* |
56 | m_refData = new wxVTextRefData; | |
57 | ||
a994f81b VZ |
58 | M_VTEXTDATA->m_validatorStyle = style ; |
59 | M_VTEXTDATA->m_stringValue = val ; | |
c801d85f KB |
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 | ||
a994f81b VZ |
72 | m_validatorStyle = val.m_validatorStyle ; |
73 | m_stringValue = val.m_stringValue ; | |
74 | ||
75 | wxNode *node = val.m_includeList.First() ; | |
76 | while ( node ) | |
77 | { | |
783b6cfd | 78 | wxChar *s = (wxChar *)node->Data(); |
a994f81b VZ |
79 | m_includeList.Add(s); |
80 | node = node->Next(); | |
81 | } | |
82 | node = val.m_excludeList.First() ; | |
83 | while ( node ) | |
84 | { | |
783b6cfd | 85 | wxChar *s = (wxChar *)node->Data(); |
a994f81b VZ |
86 | m_excludeList.Add(s); |
87 | node = node->Next(); | |
88 | } | |
c801d85f KB |
89 | return TRUE; |
90 | } | |
91 | ||
92 | wxTextValidator::~wxTextValidator() | |
93 | { | |
94 | } | |
95 | ||
96 | static bool wxIsAlpha(const wxString& val) | |
97 | { | |
a994f81b VZ |
98 | int i; |
99 | for ( i = 0; i < (int)val.Length(); i++) | |
100 | { | |
f6bcfd97 | 101 | if (!wxIsalpha(val[i])) |
a994f81b VZ |
102 | return FALSE; |
103 | } | |
104 | return TRUE; | |
c801d85f KB |
105 | } |
106 | ||
107 | static bool wxIsAlphaNumeric(const wxString& val) | |
108 | { | |
a994f81b VZ |
109 | int i; |
110 | for ( i = 0; i < (int)val.Length(); i++) | |
111 | { | |
f6bcfd97 | 112 | if (!wxIsalnum(val[i])) |
a994f81b VZ |
113 | return FALSE; |
114 | } | |
115 | return TRUE; | |
c801d85f KB |
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 | { | |
33c5b54b | 122 | if( !CheckValidator() ) |
a994f81b VZ |
123 | return FALSE; |
124 | ||
125 | wxTextCtrl *control = (wxTextCtrl *) m_validatorWindow ; | |
126 | ||
6cd47507 | 127 | // If window is disabled, simply return |
f03fc89f | 128 | if ( !control->IsEnabled() ) |
6cd47507 | 129 | return TRUE; |
a994f81b VZ |
130 | |
131 | wxString val(control->GetValue()); | |
132 | ||
2a47d3c1 | 133 | bool ok = TRUE; |
a994f81b | 134 | |
e9086757 VZ |
135 | // NB: this format string should contian exactly one '%s' |
136 | wxString errormsg; | |
a994f81b | 137 | |
e9086757 VZ |
138 | bool includeList = (m_validatorStyle & wxFILTER_INCLUDE_LIST) != 0; |
139 | if ( includeList || (m_validatorStyle & wxFILTER_EXCLUDE_LIST) ) | |
a994f81b | 140 | { |
e9086757 VZ |
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 ) | |
a994f81b | 145 | { |
e9086757 | 146 | errormsg = _("'%s' is invalid"); |
a994f81b VZ |
147 | } |
148 | } | |
149 | else if ( (m_validatorStyle & wxFILTER_ASCII) && !val.IsAscii() ) | |
150 | { | |
2a47d3c1 | 151 | ok = FALSE; |
a994f81b VZ |
152 | |
153 | errormsg = _("'%s' should only contain ASCII characters."); | |
154 | } | |
155 | else if ( (m_validatorStyle & wxFILTER_ALPHA) && !wxIsAlpha(val) ) | |
156 | { | |
2a47d3c1 | 157 | ok = FALSE; |
a994f81b VZ |
158 | |
159 | errormsg = _("'%s' should only contain alphabetic characters."); | |
160 | } | |
161 | else if ( (m_validatorStyle & wxFILTER_ALPHANUMERIC) && !wxIsAlphaNumeric(val)) | |
162 | { | |
2a47d3c1 | 163 | ok = FALSE; |
a994f81b VZ |
164 | |
165 | errormsg = _("'%s' should only contain alphabetic or numeric characters."); | |
166 | } | |
167 | else if ( (m_validatorStyle & wxFILTER_NUMERIC) && !wxIsNumeric(val)) | |
168 | { | |
2a47d3c1 | 169 | ok = FALSE; |
a994f81b VZ |
170 | |
171 | errormsg = _("'%s' should be numeric."); | |
172 | } | |
aaae8296 JS |
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 | } | |
a994f81b VZ |
185 | |
186 | if ( !ok ) | |
187 | { | |
e9086757 VZ |
188 | wxASSERT_MSG( !errormsg.empty(), _T("you forgot to set errormsg") ); |
189 | ||
a994f81b VZ |
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; | |
c801d85f KB |
200 | } |
201 | ||
202 | // Called to transfer data to the window | |
203 | bool wxTextValidator::TransferToWindow(void) | |
204 | { | |
33c5b54b | 205 | if( !CheckValidator() ) |
a994f81b | 206 | return FALSE; |
c801d85f | 207 | |
a994f81b VZ |
208 | wxTextCtrl *control = (wxTextCtrl *) m_validatorWindow ; |
209 | control->SetValue(* m_stringValue) ; | |
c801d85f | 210 | |
a994f81b | 211 | return TRUE; |
c801d85f KB |
212 | } |
213 | ||
214 | // Called to transfer data to the window | |
215 | bool wxTextValidator::TransferFromWindow(void) | |
216 | { | |
33c5b54b | 217 | if( !CheckValidator() ) |
a994f81b | 218 | return FALSE; |
c801d85f | 219 | |
a994f81b VZ |
220 | wxTextCtrl *control = (wxTextCtrl *) m_validatorWindow ; |
221 | * m_stringValue = control->GetValue() ; | |
c801d85f | 222 | |
a994f81b | 223 | return TRUE; |
c801d85f KB |
224 | } |
225 | ||
226 | void wxTextValidator::SetIncludeList(const wxStringList& list) | |
227 | { | |
228 | /* | |
a994f81b VZ |
229 | if ( !M_VTEXTDATA ) |
230 | return; | |
c801d85f KB |
231 | */ |
232 | ||
a994f81b VZ |
233 | m_includeList.Clear(); |
234 | // TODO: replace with = | |
235 | wxNode *node = list.First() ; | |
236 | while ( node ) | |
237 | { | |
783b6cfd | 238 | wxChar *s = (wxChar *)node->Data(); |
a994f81b VZ |
239 | m_includeList.Add(s); |
240 | node = node->Next(); | |
241 | } | |
c801d85f KB |
242 | } |
243 | ||
244 | void wxTextValidator::SetExcludeList(const wxStringList& list) | |
245 | { | |
246 | /* | |
a994f81b VZ |
247 | if ( !M_VTEXTDATA ) |
248 | return; | |
c801d85f KB |
249 | */ |
250 | ||
a994f81b VZ |
251 | m_excludeList.Clear(); |
252 | // TODO: replace with = | |
253 | wxNode *node = list.First() ; | |
254 | while ( node ) | |
255 | { | |
783b6cfd | 256 | wxChar *s = (wxChar *)node->Data(); |
a994f81b VZ |
257 | m_excludeList.Add(s); |
258 | node = node->Next(); | |
259 | } | |
c801d85f KB |
260 | } |
261 | ||
262 | void wxTextValidator::OnChar(wxKeyEvent& event) | |
263 | { | |
264 | /* | |
a994f81b VZ |
265 | if ( !M_VTEXTDATA ) |
266 | return; | |
c801d85f KB |
267 | */ |
268 | ||
a994f81b VZ |
269 | if ( m_validatorWindow ) |
270 | { | |
6f2a55e3 | 271 | int keyCode = (int)event.KeyCode(); |
a994f81b VZ |
272 | |
273 | // we don't filter special keys and Delete | |
274 | if ( | |
275 | !(keyCode < WXK_SPACE || keyCode == WXK_DELETE || keyCode > WXK_START) && | |
276 | ( | |
aaae8296 JS |
277 | ((m_validatorStyle & wxFILTER_INCLUDE_CHAR_LIST) && !IsInCharIncludeList(wxString((char) keyCode, 1))) || |
278 | ((m_validatorStyle & wxFILTER_EXCLUDE_CHAR_LIST) && !IsNotInCharExcludeList(wxString((char) keyCode, 1))) || | |
a994f81b | 279 | ((m_validatorStyle & wxFILTER_ASCII) && !isascii(keyCode)) || |
f6bcfd97 BP |
280 | ((m_validatorStyle & wxFILTER_ALPHA) && !wxIsalpha(keyCode)) || |
281 | ((m_validatorStyle & wxFILTER_ALPHANUMERIC) && !wxIsalnum(keyCode)) || | |
282 | ((m_validatorStyle & wxFILTER_NUMERIC) && !wxIsdigit(keyCode) | |
543f08a6 | 283 | && keyCode != '.' && keyCode != ',' && keyCode != '-') |
a994f81b VZ |
284 | ) |
285 | ) | |
286 | { | |
287 | if ( !wxValidator::IsSilent() ) | |
288 | wxBell(); | |
289 | ||
290 | // eat message | |
291 | return; | |
292 | } | |
293 | } | |
294 | ||
295 | event.Skip(); | |
c801d85f KB |
296 | } |
297 | ||
386af6a2 JS |
298 | static bool wxIsNumeric(const wxString& val) |
299 | { | |
a994f81b VZ |
300 | int i; |
301 | for ( i = 0; i < (int)val.Length(); i++) | |
302 | { | |
543f08a6 JS |
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] != ',')) | |
a994f81b VZ |
306 | if(!((i == 0) && (val[i] == '-'))) |
307 | return FALSE; | |
308 | } | |
309 | return TRUE; | |
386af6a2 | 310 | } |
c801d85f | 311 | |
aaae8296 JS |
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 | ||
ce4169a4 RR |
334 | #endif |
335 | // wxUSE_VALIDATORS |