]>
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 | } | |
173 | ||
174 | if ( !ok ) | |
175 | { | |
e9086757 VZ |
176 | wxASSERT_MSG( !errormsg.empty(), _T("you forgot to set errormsg") ); |
177 | ||
a994f81b VZ |
178 | m_validatorWindow->SetFocus(); |
179 | ||
180 | wxString buf; | |
181 | buf.Printf(errormsg, val.c_str()); | |
182 | ||
183 | wxMessageBox(buf, _("Validation conflict"), | |
184 | wxOK | wxICON_EXCLAMATION, parent); | |
185 | } | |
186 | ||
187 | return ok; | |
c801d85f KB |
188 | } |
189 | ||
190 | // Called to transfer data to the window | |
191 | bool wxTextValidator::TransferToWindow(void) | |
192 | { | |
33c5b54b | 193 | if( !CheckValidator() ) |
a994f81b | 194 | return FALSE; |
c801d85f | 195 | |
a994f81b VZ |
196 | wxTextCtrl *control = (wxTextCtrl *) m_validatorWindow ; |
197 | control->SetValue(* m_stringValue) ; | |
c801d85f | 198 | |
a994f81b | 199 | return TRUE; |
c801d85f KB |
200 | } |
201 | ||
202 | // Called to transfer data to the window | |
203 | bool wxTextValidator::TransferFromWindow(void) | |
204 | { | |
33c5b54b | 205 | if( !CheckValidator() ) |
a994f81b | 206 | return FALSE; |
c801d85f | 207 | |
a994f81b VZ |
208 | wxTextCtrl *control = (wxTextCtrl *) m_validatorWindow ; |
209 | * m_stringValue = control->GetValue() ; | |
c801d85f | 210 | |
a994f81b | 211 | return TRUE; |
c801d85f KB |
212 | } |
213 | ||
214 | void wxTextValidator::SetIncludeList(const wxStringList& list) | |
215 | { | |
216 | /* | |
a994f81b VZ |
217 | if ( !M_VTEXTDATA ) |
218 | return; | |
c801d85f KB |
219 | */ |
220 | ||
a994f81b VZ |
221 | m_includeList.Clear(); |
222 | // TODO: replace with = | |
223 | wxNode *node = list.First() ; | |
224 | while ( node ) | |
225 | { | |
783b6cfd | 226 | wxChar *s = (wxChar *)node->Data(); |
a994f81b VZ |
227 | m_includeList.Add(s); |
228 | node = node->Next(); | |
229 | } | |
c801d85f KB |
230 | } |
231 | ||
232 | void wxTextValidator::SetExcludeList(const wxStringList& list) | |
233 | { | |
234 | /* | |
a994f81b VZ |
235 | if ( !M_VTEXTDATA ) |
236 | return; | |
c801d85f KB |
237 | */ |
238 | ||
a994f81b VZ |
239 | m_excludeList.Clear(); |
240 | // TODO: replace with = | |
241 | wxNode *node = list.First() ; | |
242 | while ( node ) | |
243 | { | |
783b6cfd | 244 | wxChar *s = (wxChar *)node->Data(); |
a994f81b VZ |
245 | m_excludeList.Add(s); |
246 | node = node->Next(); | |
247 | } | |
c801d85f KB |
248 | } |
249 | ||
250 | void wxTextValidator::OnChar(wxKeyEvent& event) | |
251 | { | |
252 | /* | |
a994f81b VZ |
253 | if ( !M_VTEXTDATA ) |
254 | return; | |
c801d85f KB |
255 | */ |
256 | ||
a994f81b VZ |
257 | if ( m_validatorWindow ) |
258 | { | |
6f2a55e3 | 259 | int keyCode = (int)event.KeyCode(); |
a994f81b VZ |
260 | |
261 | // we don't filter special keys and Delete | |
262 | if ( | |
263 | !(keyCode < WXK_SPACE || keyCode == WXK_DELETE || keyCode > WXK_START) && | |
264 | ( | |
265 | ((m_validatorStyle & wxFILTER_ASCII) && !isascii(keyCode)) || | |
f6bcfd97 BP |
266 | ((m_validatorStyle & wxFILTER_ALPHA) && !wxIsalpha(keyCode)) || |
267 | ((m_validatorStyle & wxFILTER_ALPHANUMERIC) && !wxIsalnum(keyCode)) || | |
268 | ((m_validatorStyle & wxFILTER_NUMERIC) && !wxIsdigit(keyCode) | |
543f08a6 | 269 | && keyCode != '.' && keyCode != ',' && keyCode != '-') |
a994f81b VZ |
270 | ) |
271 | ) | |
272 | { | |
273 | if ( !wxValidator::IsSilent() ) | |
274 | wxBell(); | |
275 | ||
276 | // eat message | |
277 | return; | |
278 | } | |
279 | } | |
280 | ||
281 | event.Skip(); | |
c801d85f KB |
282 | } |
283 | ||
386af6a2 JS |
284 | static bool wxIsNumeric(const wxString& val) |
285 | { | |
a994f81b VZ |
286 | int i; |
287 | for ( i = 0; i < (int)val.Length(); i++) | |
288 | { | |
543f08a6 JS |
289 | // Allow for "," (French) as well as "." -- in future we should |
290 | // use wxSystemSettings or other to do better localisation | |
291 | if ((!isdigit(val[i])) && (val[i] != '.') && (val[i] != ',')) | |
a994f81b VZ |
292 | if(!((i == 0) && (val[i] == '-'))) |
293 | return FALSE; | |
294 | } | |
295 | return TRUE; | |
386af6a2 | 296 | } |
c801d85f | 297 | |
ce4169a4 RR |
298 | #endif |
299 | // wxUSE_VALIDATORS |