]>
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 VZ |
134 | |
135 | // this format string should contian exactly one '%s' | |
783b6cfd | 136 | const wxChar *errormsg = _("'%s' is invalid"); |
a994f81b VZ |
137 | |
138 | if ( m_validatorStyle & wxFILTER_INCLUDE_LIST ) | |
139 | { | |
140 | if ( !m_includeList.Member(val) ) | |
141 | { | |
2a47d3c1 | 142 | ok = FALSE; |
a994f81b VZ |
143 | } |
144 | } | |
145 | else if ( m_validatorStyle & wxFILTER_EXCLUDE_LIST ) | |
146 | { | |
147 | if ( m_excludeList.Member(val) ) | |
148 | { | |
2a47d3c1 | 149 | ok = FALSE; |
a994f81b VZ |
150 | } |
151 | } | |
152 | else if ( (m_validatorStyle & wxFILTER_ASCII) && !val.IsAscii() ) | |
153 | { | |
2a47d3c1 | 154 | ok = FALSE; |
a994f81b VZ |
155 | |
156 | errormsg = _("'%s' should only contain ASCII characters."); | |
157 | } | |
158 | else if ( (m_validatorStyle & wxFILTER_ALPHA) && !wxIsAlpha(val) ) | |
159 | { | |
2a47d3c1 | 160 | ok = FALSE; |
a994f81b VZ |
161 | |
162 | errormsg = _("'%s' should only contain alphabetic characters."); | |
163 | } | |
164 | else if ( (m_validatorStyle & wxFILTER_ALPHANUMERIC) && !wxIsAlphaNumeric(val)) | |
165 | { | |
2a47d3c1 | 166 | ok = FALSE; |
a994f81b VZ |
167 | |
168 | errormsg = _("'%s' should only contain alphabetic or numeric characters."); | |
169 | } | |
170 | else if ( (m_validatorStyle & wxFILTER_NUMERIC) && !wxIsNumeric(val)) | |
171 | { | |
2a47d3c1 | 172 | ok = FALSE; |
a994f81b VZ |
173 | |
174 | errormsg = _("'%s' should be numeric."); | |
175 | } | |
176 | ||
177 | if ( !ok ) | |
178 | { | |
179 | m_validatorWindow->SetFocus(); | |
180 | ||
181 | wxString buf; | |
182 | buf.Printf(errormsg, val.c_str()); | |
183 | ||
184 | wxMessageBox(buf, _("Validation conflict"), | |
185 | wxOK | wxICON_EXCLAMATION, parent); | |
186 | } | |
187 | ||
188 | return ok; | |
c801d85f KB |
189 | } |
190 | ||
191 | // Called to transfer data to the window | |
192 | bool wxTextValidator::TransferToWindow(void) | |
193 | { | |
33c5b54b | 194 | if( !CheckValidator() ) |
a994f81b | 195 | return FALSE; |
c801d85f | 196 | |
a994f81b VZ |
197 | wxTextCtrl *control = (wxTextCtrl *) m_validatorWindow ; |
198 | control->SetValue(* m_stringValue) ; | |
c801d85f | 199 | |
a994f81b | 200 | return TRUE; |
c801d85f KB |
201 | } |
202 | ||
203 | // Called to transfer data to the window | |
204 | bool wxTextValidator::TransferFromWindow(void) | |
205 | { | |
33c5b54b | 206 | if( !CheckValidator() ) |
a994f81b | 207 | return FALSE; |
c801d85f | 208 | |
a994f81b VZ |
209 | wxTextCtrl *control = (wxTextCtrl *) m_validatorWindow ; |
210 | * m_stringValue = control->GetValue() ; | |
c801d85f | 211 | |
a994f81b | 212 | return TRUE; |
c801d85f KB |
213 | } |
214 | ||
215 | void wxTextValidator::SetIncludeList(const wxStringList& list) | |
216 | { | |
217 | /* | |
a994f81b VZ |
218 | if ( !M_VTEXTDATA ) |
219 | return; | |
c801d85f KB |
220 | */ |
221 | ||
a994f81b VZ |
222 | m_includeList.Clear(); |
223 | // TODO: replace with = | |
224 | wxNode *node = list.First() ; | |
225 | while ( node ) | |
226 | { | |
783b6cfd | 227 | wxChar *s = (wxChar *)node->Data(); |
a994f81b VZ |
228 | m_includeList.Add(s); |
229 | node = node->Next(); | |
230 | } | |
c801d85f KB |
231 | } |
232 | ||
233 | void wxTextValidator::SetExcludeList(const wxStringList& list) | |
234 | { | |
235 | /* | |
a994f81b VZ |
236 | if ( !M_VTEXTDATA ) |
237 | return; | |
c801d85f KB |
238 | */ |
239 | ||
a994f81b VZ |
240 | m_excludeList.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_excludeList.Add(s); |
247 | node = node->Next(); | |
248 | } | |
c801d85f KB |
249 | } |
250 | ||
251 | void wxTextValidator::OnChar(wxKeyEvent& event) | |
252 | { | |
253 | /* | |
a994f81b VZ |
254 | if ( !M_VTEXTDATA ) |
255 | return; | |
c801d85f KB |
256 | */ |
257 | ||
a994f81b VZ |
258 | if ( m_validatorWindow ) |
259 | { | |
6f2a55e3 | 260 | int keyCode = (int)event.KeyCode(); |
a994f81b VZ |
261 | |
262 | // we don't filter special keys and Delete | |
263 | if ( | |
264 | !(keyCode < WXK_SPACE || keyCode == WXK_DELETE || keyCode > WXK_START) && | |
265 | ( | |
266 | ((m_validatorStyle & wxFILTER_ASCII) && !isascii(keyCode)) || | |
f6bcfd97 BP |
267 | ((m_validatorStyle & wxFILTER_ALPHA) && !wxIsalpha(keyCode)) || |
268 | ((m_validatorStyle & wxFILTER_ALPHANUMERIC) && !wxIsalnum(keyCode)) || | |
269 | ((m_validatorStyle & wxFILTER_NUMERIC) && !wxIsdigit(keyCode) | |
543f08a6 | 270 | && keyCode != '.' && keyCode != ',' && keyCode != '-') |
a994f81b VZ |
271 | ) |
272 | ) | |
273 | { | |
274 | if ( !wxValidator::IsSilent() ) | |
275 | wxBell(); | |
276 | ||
277 | // eat message | |
278 | return; | |
279 | } | |
280 | } | |
281 | ||
282 | event.Skip(); | |
c801d85f KB |
283 | } |
284 | ||
386af6a2 JS |
285 | static bool wxIsNumeric(const wxString& val) |
286 | { | |
a994f81b VZ |
287 | int i; |
288 | for ( i = 0; i < (int)val.Length(); i++) | |
289 | { | |
543f08a6 JS |
290 | // Allow for "," (French) as well as "." -- in future we should |
291 | // use wxSystemSettings or other to do better localisation | |
292 | if ((!isdigit(val[i])) && (val[i] != '.') && (val[i] != ',')) | |
a994f81b VZ |
293 | if(!((i == 0) && (val[i] == '-'))) |
294 | return FALSE; | |
295 | } | |
296 | return TRUE; | |
386af6a2 | 297 | } |
c801d85f | 298 | |
ce4169a4 RR |
299 | #endif |
300 | // wxUSE_VALIDATORS |