]>
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 | ||
23 | #ifndef WX_PRECOMP | |
ce4169a4 RR |
24 | #include "wx/defs.h" |
25 | #endif | |
26 | ||
27 | #if wxUSE_VALIDATORS | |
28 | ||
29 | #ifndef WX_PRECOMP | |
30 | #include <stdio.h> | |
31 | #include "wx/textctrl.h" | |
32 | #include "wx/utils.h" | |
33 | #include "wx/msgdlg.h" | |
34 | #include "wx/intl.h" | |
c801d85f KB |
35 | #endif |
36 | ||
37 | #include "wx/valtext.h" | |
38 | ||
39 | #include <ctype.h> | |
40 | #include <string.h> | |
41 | #include <stdlib.h> | |
42 | ||
ce3ed50d | 43 | #ifdef __SALFORDC__ |
a994f81b | 44 | #include <clib.h> |
ce3ed50d JS |
45 | #endif |
46 | ||
c801d85f KB |
47 | #if !USE_SHARED_LIBRARY |
48 | IMPLEMENT_DYNAMIC_CLASS(wxTextValidator, wxValidator) | |
49 | ||
50 | BEGIN_EVENT_TABLE(wxTextValidator, wxValidator) | |
a994f81b | 51 | EVT_CHAR(wxTextValidator::OnChar) |
c801d85f KB |
52 | END_EVENT_TABLE() |
53 | #endif | |
54 | ||
386af6a2 JS |
55 | static bool wxIsNumeric(const wxString& val); |
56 | ||
debe6624 | 57 | wxTextValidator::wxTextValidator(long style, wxString *val) |
c801d85f | 58 | { |
a994f81b VZ |
59 | m_validatorStyle = style ; |
60 | m_stringValue = val ; | |
c801d85f KB |
61 | /* |
62 | m_refData = new wxVTextRefData; | |
63 | ||
a994f81b VZ |
64 | M_VTEXTDATA->m_validatorStyle = style ; |
65 | M_VTEXTDATA->m_stringValue = val ; | |
c801d85f KB |
66 | */ |
67 | } | |
68 | ||
69 | wxTextValidator::wxTextValidator(const wxTextValidator& val) | |
70 | { | |
71 | Copy(val); | |
72 | } | |
73 | ||
74 | bool wxTextValidator::Copy(const wxTextValidator& val) | |
75 | { | |
76 | wxValidator::Copy(val); | |
77 | ||
a994f81b VZ |
78 | m_validatorStyle = val.m_validatorStyle ; |
79 | m_stringValue = val.m_stringValue ; | |
80 | ||
81 | wxNode *node = val.m_includeList.First() ; | |
82 | while ( node ) | |
83 | { | |
783b6cfd | 84 | wxChar *s = (wxChar *)node->Data(); |
a994f81b VZ |
85 | m_includeList.Add(s); |
86 | node = node->Next(); | |
87 | } | |
88 | node = val.m_excludeList.First() ; | |
89 | while ( node ) | |
90 | { | |
783b6cfd | 91 | wxChar *s = (wxChar *)node->Data(); |
a994f81b VZ |
92 | m_excludeList.Add(s); |
93 | node = node->Next(); | |
94 | } | |
c801d85f KB |
95 | return TRUE; |
96 | } | |
97 | ||
98 | wxTextValidator::~wxTextValidator() | |
99 | { | |
100 | } | |
101 | ||
102 | static bool wxIsAlpha(const wxString& val) | |
103 | { | |
a994f81b VZ |
104 | int i; |
105 | for ( i = 0; i < (int)val.Length(); i++) | |
106 | { | |
107 | if (!isalpha(val[i])) | |
108 | return FALSE; | |
109 | } | |
110 | return TRUE; | |
c801d85f KB |
111 | } |
112 | ||
113 | static bool wxIsAlphaNumeric(const wxString& val) | |
114 | { | |
a994f81b VZ |
115 | int i; |
116 | for ( i = 0; i < (int)val.Length(); i++) | |
117 | { | |
118 | if (!isalnum(val[i])) | |
119 | return FALSE; | |
120 | } | |
121 | return TRUE; | |
c801d85f KB |
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 | { | |
a994f81b VZ |
128 | if ( !m_validatorWindow ) |
129 | return FALSE; | |
130 | if ( !m_validatorWindow->IsKindOf(CLASSINFO(wxTextCtrl)) ) | |
131 | return FALSE; | |
132 | if ( !m_stringValue ) | |
133 | return FALSE; | |
134 | ||
135 | wxTextCtrl *control = (wxTextCtrl *) m_validatorWindow ; | |
136 | ||
6cd47507 | 137 | // If window is disabled, simply return |
f03fc89f | 138 | if ( !control->IsEnabled() ) |
6cd47507 | 139 | return TRUE; |
a994f81b VZ |
140 | |
141 | wxString val(control->GetValue()); | |
142 | ||
2a47d3c1 | 143 | bool ok = TRUE; |
a994f81b VZ |
144 | |
145 | // this format string should contian exactly one '%s' | |
783b6cfd | 146 | const wxChar *errormsg = _("'%s' is invalid"); |
a994f81b VZ |
147 | |
148 | if ( m_validatorStyle & wxFILTER_INCLUDE_LIST ) | |
149 | { | |
150 | if ( !m_includeList.Member(val) ) | |
151 | { | |
2a47d3c1 | 152 | ok = FALSE; |
a994f81b VZ |
153 | } |
154 | } | |
155 | else if ( m_validatorStyle & wxFILTER_EXCLUDE_LIST ) | |
156 | { | |
157 | if ( m_excludeList.Member(val) ) | |
158 | { | |
2a47d3c1 | 159 | ok = FALSE; |
a994f81b VZ |
160 | } |
161 | } | |
162 | else if ( (m_validatorStyle & wxFILTER_ASCII) && !val.IsAscii() ) | |
163 | { | |
2a47d3c1 | 164 | ok = FALSE; |
a994f81b VZ |
165 | |
166 | errormsg = _("'%s' should only contain ASCII characters."); | |
167 | } | |
168 | else if ( (m_validatorStyle & wxFILTER_ALPHA) && !wxIsAlpha(val) ) | |
169 | { | |
2a47d3c1 | 170 | ok = FALSE; |
a994f81b VZ |
171 | |
172 | errormsg = _("'%s' should only contain alphabetic characters."); | |
173 | } | |
174 | else if ( (m_validatorStyle & wxFILTER_ALPHANUMERIC) && !wxIsAlphaNumeric(val)) | |
175 | { | |
2a47d3c1 | 176 | ok = FALSE; |
a994f81b VZ |
177 | |
178 | errormsg = _("'%s' should only contain alphabetic or numeric characters."); | |
179 | } | |
180 | else if ( (m_validatorStyle & wxFILTER_NUMERIC) && !wxIsNumeric(val)) | |
181 | { | |
2a47d3c1 | 182 | ok = FALSE; |
a994f81b VZ |
183 | |
184 | errormsg = _("'%s' should be numeric."); | |
185 | } | |
186 | ||
187 | if ( !ok ) | |
188 | { | |
189 | m_validatorWindow->SetFocus(); | |
190 | ||
191 | wxString buf; | |
192 | buf.Printf(errormsg, val.c_str()); | |
193 | ||
194 | wxMessageBox(buf, _("Validation conflict"), | |
195 | wxOK | wxICON_EXCLAMATION, parent); | |
196 | } | |
197 | ||
198 | return ok; | |
c801d85f KB |
199 | } |
200 | ||
201 | // Called to transfer data to the window | |
202 | bool wxTextValidator::TransferToWindow(void) | |
203 | { | |
a994f81b VZ |
204 | if ( !m_validatorWindow ) |
205 | return FALSE; | |
206 | if ( !m_validatorWindow->IsKindOf(CLASSINFO(wxTextCtrl)) ) | |
207 | return FALSE; | |
208 | if ( !m_stringValue ) | |
209 | return FALSE; | |
c801d85f | 210 | |
a994f81b VZ |
211 | wxTextCtrl *control = (wxTextCtrl *) m_validatorWindow ; |
212 | control->SetValue(* m_stringValue) ; | |
c801d85f | 213 | |
a994f81b | 214 | return TRUE; |
c801d85f KB |
215 | } |
216 | ||
217 | // Called to transfer data to the window | |
218 | bool wxTextValidator::TransferFromWindow(void) | |
219 | { | |
a994f81b VZ |
220 | if ( !m_validatorWindow ) |
221 | return FALSE; | |
222 | if ( !m_validatorWindow->IsKindOf(CLASSINFO(wxTextCtrl)) ) | |
223 | return FALSE; | |
224 | if ( !m_stringValue ) | |
225 | return FALSE; | |
c801d85f | 226 | |
a994f81b VZ |
227 | wxTextCtrl *control = (wxTextCtrl *) m_validatorWindow ; |
228 | * m_stringValue = control->GetValue() ; | |
c801d85f | 229 | |
a994f81b | 230 | return TRUE; |
c801d85f KB |
231 | } |
232 | ||
233 | void wxTextValidator::SetIncludeList(const wxStringList& list) | |
234 | { | |
235 | /* | |
a994f81b VZ |
236 | if ( !M_VTEXTDATA ) |
237 | return; | |
c801d85f KB |
238 | */ |
239 | ||
a994f81b VZ |
240 | m_includeList.Clear(); |
241 | // TODO: replace with = | |
242 | wxNode *node = list.First() ; | |
243 | while ( node ) | |
244 | { | |
783b6cfd | 245 | wxChar *s = (wxChar *)node->Data(); |
a994f81b VZ |
246 | m_includeList.Add(s); |
247 | node = node->Next(); | |
248 | } | |
c801d85f KB |
249 | } |
250 | ||
251 | void wxTextValidator::SetExcludeList(const wxStringList& list) | |
252 | { | |
253 | /* | |
a994f81b VZ |
254 | if ( !M_VTEXTDATA ) |
255 | return; | |
c801d85f KB |
256 | */ |
257 | ||
a994f81b VZ |
258 | m_excludeList.Clear(); |
259 | // TODO: replace with = | |
260 | wxNode *node = list.First() ; | |
261 | while ( node ) | |
262 | { | |
783b6cfd | 263 | wxChar *s = (wxChar *)node->Data(); |
a994f81b VZ |
264 | m_excludeList.Add(s); |
265 | node = node->Next(); | |
266 | } | |
c801d85f KB |
267 | } |
268 | ||
269 | void wxTextValidator::OnChar(wxKeyEvent& event) | |
270 | { | |
271 | /* | |
a994f81b VZ |
272 | if ( !M_VTEXTDATA ) |
273 | return; | |
c801d85f KB |
274 | */ |
275 | ||
a994f81b VZ |
276 | if ( m_validatorWindow ) |
277 | { | |
278 | int keyCode = event.KeyCode(); | |
279 | ||
280 | // we don't filter special keys and Delete | |
281 | if ( | |
282 | !(keyCode < WXK_SPACE || keyCode == WXK_DELETE || keyCode > WXK_START) && | |
283 | ( | |
284 | ((m_validatorStyle & wxFILTER_ASCII) && !isascii(keyCode)) || | |
285 | ((m_validatorStyle & wxFILTER_ALPHA) && !isalpha(keyCode)) || | |
286 | ((m_validatorStyle & wxFILTER_ALPHANUMERIC) && !isalnum(keyCode)) || | |
287 | ((m_validatorStyle & wxFILTER_NUMERIC) && !isdigit(keyCode) | |
288 | && keyCode != '.' && keyCode != '-') | |
289 | ) | |
290 | ) | |
291 | { | |
292 | if ( !wxValidator::IsSilent() ) | |
293 | wxBell(); | |
294 | ||
295 | // eat message | |
296 | return; | |
297 | } | |
298 | } | |
299 | ||
300 | event.Skip(); | |
c801d85f KB |
301 | } |
302 | ||
386af6a2 JS |
303 | static bool wxIsNumeric(const wxString& val) |
304 | { | |
a994f81b VZ |
305 | int i; |
306 | for ( i = 0; i < (int)val.Length(); i++) | |
307 | { | |
308 | if ((!isdigit(val[i])) && (val[i] != '.')) | |
309 | if(!((i == 0) && (val[i] == '-'))) | |
310 | return FALSE; | |
311 | } | |
312 | return TRUE; | |
386af6a2 | 313 | } |
c801d85f | 314 | |
ce4169a4 RR |
315 | #endif |
316 | // wxUSE_VALIDATORS | |
317 |