]>
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__ | |
a994f81b | 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__ | |
a994f81b | 20 | #pragma hdrstop |
c801d85f KB |
21 | #endif |
22 | ||
23 | #ifndef WX_PRECOMP | |
a994f81b VZ |
24 | #include <stdio.h> |
25 | #include "wx/textctrl.h" | |
26 | #include "wx/utils.h" | |
27 | #include "wx/msgdlg.h" | |
28 | #include "wx/intl.h" | |
c801d85f KB |
29 | #endif |
30 | ||
31 | #include "wx/valtext.h" | |
32 | ||
33 | #include <ctype.h> | |
34 | #include <string.h> | |
35 | #include <stdlib.h> | |
36 | ||
ce3ed50d | 37 | #ifdef __SALFORDC__ |
a994f81b | 38 | #include <clib.h> |
ce3ed50d JS |
39 | #endif |
40 | ||
c801d85f KB |
41 | #if !USE_SHARED_LIBRARY |
42 | IMPLEMENT_DYNAMIC_CLASS(wxTextValidator, wxValidator) | |
43 | ||
44 | BEGIN_EVENT_TABLE(wxTextValidator, wxValidator) | |
a994f81b | 45 | EVT_CHAR(wxTextValidator::OnChar) |
c801d85f KB |
46 | END_EVENT_TABLE() |
47 | #endif | |
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 | { | |
101 | if (!isalpha(val[i])) | |
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 | { | |
112 | if (!isalnum(val[i])) | |
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 | { | |
a994f81b VZ |
122 | if ( !m_validatorWindow ) |
123 | return FALSE; | |
124 | if ( !m_validatorWindow->IsKindOf(CLASSINFO(wxTextCtrl)) ) | |
125 | return FALSE; | |
126 | if ( !m_stringValue ) | |
127 | return FALSE; | |
128 | ||
129 | wxTextCtrl *control = (wxTextCtrl *) m_validatorWindow ; | |
130 | ||
6cd47507 | 131 | // If window is disabled, simply return |
a994f81b | 132 | if ( !control->Enabled() ) |
6cd47507 | 133 | return TRUE; |
a994f81b VZ |
134 | |
135 | wxString val(control->GetValue()); | |
136 | ||
2a47d3c1 | 137 | bool ok = TRUE; |
a994f81b VZ |
138 | |
139 | // this format string should contian exactly one '%s' | |
783b6cfd | 140 | const wxChar *errormsg = _("'%s' is invalid"); |
a994f81b VZ |
141 | |
142 | if ( m_validatorStyle & wxFILTER_INCLUDE_LIST ) | |
143 | { | |
144 | if ( !m_includeList.Member(val) ) | |
145 | { | |
2a47d3c1 | 146 | ok = FALSE; |
a994f81b VZ |
147 | } |
148 | } | |
149 | else if ( m_validatorStyle & wxFILTER_EXCLUDE_LIST ) | |
150 | { | |
151 | if ( m_excludeList.Member(val) ) | |
152 | { | |
2a47d3c1 | 153 | ok = FALSE; |
a994f81b VZ |
154 | } |
155 | } | |
156 | else if ( (m_validatorStyle & wxFILTER_ASCII) && !val.IsAscii() ) | |
157 | { | |
2a47d3c1 | 158 | ok = FALSE; |
a994f81b VZ |
159 | |
160 | errormsg = _("'%s' should only contain ASCII characters."); | |
161 | } | |
162 | else if ( (m_validatorStyle & wxFILTER_ALPHA) && !wxIsAlpha(val) ) | |
163 | { | |
2a47d3c1 | 164 | ok = FALSE; |
a994f81b VZ |
165 | |
166 | errormsg = _("'%s' should only contain alphabetic characters."); | |
167 | } | |
168 | else if ( (m_validatorStyle & wxFILTER_ALPHANUMERIC) && !wxIsAlphaNumeric(val)) | |
169 | { | |
2a47d3c1 | 170 | ok = FALSE; |
a994f81b VZ |
171 | |
172 | errormsg = _("'%s' should only contain alphabetic or numeric characters."); | |
173 | } | |
174 | else if ( (m_validatorStyle & wxFILTER_NUMERIC) && !wxIsNumeric(val)) | |
175 | { | |
2a47d3c1 | 176 | ok = FALSE; |
a994f81b VZ |
177 | |
178 | errormsg = _("'%s' should be numeric."); | |
179 | } | |
180 | ||
181 | if ( !ok ) | |
182 | { | |
183 | m_validatorWindow->SetFocus(); | |
184 | ||
185 | wxString buf; | |
186 | buf.Printf(errormsg, val.c_str()); | |
187 | ||
188 | wxMessageBox(buf, _("Validation conflict"), | |
189 | wxOK | wxICON_EXCLAMATION, parent); | |
190 | } | |
191 | ||
192 | return ok; | |
c801d85f KB |
193 | } |
194 | ||
195 | // Called to transfer data to the window | |
196 | bool wxTextValidator::TransferToWindow(void) | |
197 | { | |
a994f81b VZ |
198 | if ( !m_validatorWindow ) |
199 | return FALSE; | |
200 | if ( !m_validatorWindow->IsKindOf(CLASSINFO(wxTextCtrl)) ) | |
201 | return FALSE; | |
202 | if ( !m_stringValue ) | |
203 | return FALSE; | |
c801d85f | 204 | |
a994f81b VZ |
205 | wxTextCtrl *control = (wxTextCtrl *) m_validatorWindow ; |
206 | control->SetValue(* m_stringValue) ; | |
c801d85f | 207 | |
a994f81b | 208 | return TRUE; |
c801d85f KB |
209 | } |
210 | ||
211 | // Called to transfer data to the window | |
212 | bool wxTextValidator::TransferFromWindow(void) | |
213 | { | |
a994f81b VZ |
214 | if ( !m_validatorWindow ) |
215 | return FALSE; | |
216 | if ( !m_validatorWindow->IsKindOf(CLASSINFO(wxTextCtrl)) ) | |
217 | return FALSE; | |
218 | if ( !m_stringValue ) | |
219 | return FALSE; | |
c801d85f | 220 | |
a994f81b VZ |
221 | wxTextCtrl *control = (wxTextCtrl *) m_validatorWindow ; |
222 | * m_stringValue = control->GetValue() ; | |
c801d85f | 223 | |
a994f81b | 224 | return TRUE; |
c801d85f KB |
225 | } |
226 | ||
227 | void wxTextValidator::SetIncludeList(const wxStringList& list) | |
228 | { | |
229 | /* | |
a994f81b VZ |
230 | if ( !M_VTEXTDATA ) |
231 | return; | |
c801d85f KB |
232 | */ |
233 | ||
a994f81b VZ |
234 | m_includeList.Clear(); |
235 | // TODO: replace with = | |
236 | wxNode *node = list.First() ; | |
237 | while ( node ) | |
238 | { | |
783b6cfd | 239 | wxChar *s = (wxChar *)node->Data(); |
a994f81b VZ |
240 | m_includeList.Add(s); |
241 | node = node->Next(); | |
242 | } | |
c801d85f KB |
243 | } |
244 | ||
245 | void wxTextValidator::SetExcludeList(const wxStringList& list) | |
246 | { | |
247 | /* | |
a994f81b VZ |
248 | if ( !M_VTEXTDATA ) |
249 | return; | |
c801d85f KB |
250 | */ |
251 | ||
a994f81b VZ |
252 | m_excludeList.Clear(); |
253 | // TODO: replace with = | |
254 | wxNode *node = list.First() ; | |
255 | while ( node ) | |
256 | { | |
783b6cfd | 257 | wxChar *s = (wxChar *)node->Data(); |
a994f81b VZ |
258 | m_excludeList.Add(s); |
259 | node = node->Next(); | |
260 | } | |
c801d85f KB |
261 | } |
262 | ||
263 | void wxTextValidator::OnChar(wxKeyEvent& event) | |
264 | { | |
265 | /* | |
a994f81b VZ |
266 | if ( !M_VTEXTDATA ) |
267 | return; | |
c801d85f KB |
268 | */ |
269 | ||
a994f81b VZ |
270 | if ( m_validatorWindow ) |
271 | { | |
272 | int keyCode = event.KeyCode(); | |
273 | ||
274 | // we don't filter special keys and Delete | |
275 | if ( | |
276 | !(keyCode < WXK_SPACE || keyCode == WXK_DELETE || keyCode > WXK_START) && | |
277 | ( | |
278 | ((m_validatorStyle & wxFILTER_ASCII) && !isascii(keyCode)) || | |
279 | ((m_validatorStyle & wxFILTER_ALPHA) && !isalpha(keyCode)) || | |
280 | ((m_validatorStyle & wxFILTER_ALPHANUMERIC) && !isalnum(keyCode)) || | |
281 | ((m_validatorStyle & wxFILTER_NUMERIC) && !isdigit(keyCode) | |
282 | && keyCode != '.' && keyCode != '-') | |
283 | ) | |
284 | ) | |
285 | { | |
286 | if ( !wxValidator::IsSilent() ) | |
287 | wxBell(); | |
288 | ||
289 | // eat message | |
290 | return; | |
291 | } | |
292 | } | |
293 | ||
294 | event.Skip(); | |
c801d85f KB |
295 | } |
296 | ||
386af6a2 JS |
297 | static bool wxIsNumeric(const wxString& val) |
298 | { | |
a994f81b VZ |
299 | int i; |
300 | for ( i = 0; i < (int)val.Length(); i++) | |
301 | { | |
302 | if ((!isdigit(val[i])) && (val[i] != '.')) | |
303 | if(!((i == 0) && (val[i] == '-'))) | |
304 | return FALSE; | |
305 | } | |
306 | return TRUE; | |
386af6a2 | 307 | } |
c801d85f | 308 |