]>
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 | |
9 | // Licence: wxWindows license | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #ifdef __GNUG__ | |
13 | #pragma implementation "valtext.h" | |
14 | #endif | |
15 | ||
16 | // For compilers that support precompilation, includes "wx.h". | |
17 | #include "wx/wxprec.h" | |
18 | ||
19 | #ifdef __BORLANDC__ | |
20 | #pragma hdrstop | |
21 | #endif | |
22 | ||
23 | #ifndef WX_PRECOMP | |
24 | #include <stdio.h> | |
25 | #include "wx/textctrl.h" | |
26 | #include "wx/utils.h" | |
903f689b RR |
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 JS |
37 | #ifdef __SALFORDC__ |
38 | #include <clib.h> | |
39 | #endif | |
40 | ||
c801d85f KB |
41 | #if !USE_SHARED_LIBRARY |
42 | IMPLEMENT_DYNAMIC_CLASS(wxTextValidator, wxValidator) | |
43 | ||
44 | BEGIN_EVENT_TABLE(wxTextValidator, wxValidator) | |
45 | EVT_CHAR(wxTextValidator::OnChar) | |
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 KB |
52 | { |
53 | m_validatorStyle = style ; | |
54 | m_stringValue = val ; | |
55 | /* | |
56 | m_refData = new wxVTextRefData; | |
57 | ||
58 | M_VTEXTDATA->m_validatorStyle = style ; | |
59 | M_VTEXTDATA->m_stringValue = val ; | |
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 | ||
72 | m_validatorStyle = val.m_validatorStyle ; | |
73 | m_stringValue = val.m_stringValue ; | |
74 | ||
75 | wxNode *node = val.m_includeList.First() ; | |
76 | while ( node ) | |
77 | { | |
78 | char *s = (char *)node->Data(); | |
79 | m_includeList.Add(s); | |
80 | node = node->Next(); | |
81 | } | |
82 | node = val.m_excludeList.First() ; | |
83 | while ( node ) | |
84 | { | |
85 | char *s = (char *)node->Data(); | |
86 | m_excludeList.Add(s); | |
87 | node = node->Next(); | |
88 | } | |
89 | return TRUE; | |
90 | } | |
91 | ||
92 | wxTextValidator::~wxTextValidator() | |
93 | { | |
94 | } | |
95 | ||
96 | static bool wxIsAlpha(const wxString& val) | |
97 | { | |
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; | |
105 | } | |
106 | ||
107 | static bool wxIsAlphaNumeric(const wxString& val) | |
108 | { | |
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; | |
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 | { | |
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 | ||
131 | // If window is disabled, don't validate | |
132 | if ( !control->Enabled() ) | |
133 | return FALSE; | |
134 | ||
135 | wxString val(control->GetValue()); | |
136 | ||
137 | if ( m_validatorStyle & wxFILTER_INCLUDE_LIST ) | |
138 | { | |
139 | if ( !m_includeList.Member(val) ) | |
140 | { | |
87d1e11f | 141 | m_validatorWindow->SetFocus(); |
c801d85f | 142 | char buf[512]; |
1a5a8367 DP |
143 | sprintf(buf, _("%s is invalid."), (const char *)val); |
144 | wxMessageBox(buf, _("Validation conflict"), wxOK | wxICON_EXCLAMATION, parent); | |
c801d85f KB |
145 | return FALSE; |
146 | } | |
147 | } | |
148 | if ( m_validatorStyle & wxFILTER_EXCLUDE_LIST ) | |
149 | { | |
150 | if ( m_excludeList.Member(val) ) | |
151 | { | |
87d1e11f | 152 | m_validatorWindow->SetFocus(); |
c801d85f | 153 | char buf[512]; |
1a5a8367 DP |
154 | sprintf(buf, _("%s is invalid."), (const char *)val); |
155 | wxMessageBox(buf, _("Validation conflict"), wxOK | wxICON_EXCLAMATION, parent); | |
c801d85f KB |
156 | return FALSE; |
157 | } | |
158 | } | |
159 | if ( (m_validatorStyle & wxFILTER_ASCII) && !val.IsAscii() ) | |
160 | { | |
87d1e11f | 161 | m_validatorWindow->SetFocus(); |
c801d85f | 162 | char buf[512]; |
1a5a8367 DP |
163 | sprintf(buf, _("%s should only contain ASCII characters."), (const char *)val); |
164 | wxMessageBox(buf, _("Validation conflict"), wxOK | wxICON_EXCLAMATION, parent); | |
c801d85f KB |
165 | return FALSE; |
166 | } | |
167 | if ( (m_validatorStyle & wxFILTER_ALPHA) && !wxIsAlpha(val) ) | |
168 | { | |
87d1e11f | 169 | m_validatorWindow->SetFocus(); |
c801d85f | 170 | char buf[512]; |
1a5a8367 DP |
171 | sprintf(buf, _("%s should only contain alphabetic characters."), (const char *)val); |
172 | wxMessageBox(buf, _("Validation conflict"), wxOK | wxICON_EXCLAMATION, parent); | |
c801d85f KB |
173 | return FALSE; |
174 | } | |
175 | if ( (m_validatorStyle & wxFILTER_ALPHANUMERIC) && !wxIsAlphaNumeric(val)) | |
176 | { | |
87d1e11f | 177 | m_validatorWindow->SetFocus(); |
c801d85f | 178 | char buf[512]; |
1a5a8367 DP |
179 | sprintf(buf, _("%s should only contain alphabetic or numeric characters."), (const char *)val); |
180 | wxMessageBox(buf,_("Validation conflict"), wxOK | wxICON_EXCLAMATION, parent); | |
c801d85f KB |
181 | return FALSE; |
182 | } | |
386af6a2 JS |
183 | if ( (m_validatorStyle & wxFILTER_NUMERIC) && !wxIsNumeric(val)) |
184 | ||
c801d85f | 185 | { |
87d1e11f | 186 | m_validatorWindow->SetFocus(); |
c801d85f | 187 | char buf[512]; |
1a5a8367 DP |
188 | sprintf(buf, _("%s should be numeric."), (const char *)val); |
189 | wxMessageBox(buf, _("Validation conflict"), wxOK | wxICON_EXCLAMATION, parent); | |
c801d85f KB |
190 | return FALSE; |
191 | } | |
192 | ||
193 | return TRUE ; | |
194 | } | |
195 | ||
196 | // Called to transfer data to the window | |
197 | bool wxTextValidator::TransferToWindow(void) | |
198 | { | |
199 | if ( !m_validatorWindow ) | |
200 | return FALSE; | |
201 | if ( !m_validatorWindow->IsKindOf(CLASSINFO(wxTextCtrl)) ) | |
202 | return FALSE; | |
203 | if ( !m_stringValue ) | |
204 | return FALSE; | |
205 | ||
206 | wxTextCtrl *control = (wxTextCtrl *) m_validatorWindow ; | |
207 | control->SetValue(* m_stringValue) ; | |
208 | ||
209 | return TRUE; | |
210 | } | |
211 | ||
212 | // Called to transfer data to the window | |
213 | bool wxTextValidator::TransferFromWindow(void) | |
214 | { | |
215 | if ( !m_validatorWindow ) | |
216 | return FALSE; | |
217 | if ( !m_validatorWindow->IsKindOf(CLASSINFO(wxTextCtrl)) ) | |
218 | return FALSE; | |
219 | if ( !m_stringValue ) | |
220 | return FALSE; | |
221 | ||
222 | wxTextCtrl *control = (wxTextCtrl *) m_validatorWindow ; | |
223 | * m_stringValue = control->GetValue() ; | |
224 | ||
225 | return TRUE; | |
226 | } | |
227 | ||
228 | void wxTextValidator::SetIncludeList(const wxStringList& list) | |
229 | { | |
230 | /* | |
231 | if ( !M_VTEXTDATA ) | |
232 | return; | |
233 | */ | |
234 | ||
235 | m_includeList.Clear(); | |
236 | // TODO: replace with = | |
237 | wxNode *node = list.First() ; | |
238 | while ( node ) | |
239 | { | |
240 | char *s = (char *)node->Data(); | |
241 | m_includeList.Add(s); | |
242 | node = node->Next(); | |
243 | } | |
244 | } | |
245 | ||
246 | void wxTextValidator::SetExcludeList(const wxStringList& list) | |
247 | { | |
248 | /* | |
249 | if ( !M_VTEXTDATA ) | |
250 | return; | |
251 | */ | |
252 | ||
253 | m_excludeList.Clear(); | |
254 | // TODO: replace with = | |
255 | wxNode *node = list.First() ; | |
256 | while ( node ) | |
257 | { | |
258 | char *s = (char *)node->Data(); | |
259 | m_excludeList.Add(s); | |
260 | node = node->Next(); | |
261 | } | |
262 | } | |
263 | ||
264 | void wxTextValidator::OnChar(wxKeyEvent& event) | |
265 | { | |
266 | /* | |
267 | if ( !M_VTEXTDATA ) | |
268 | return; | |
269 | */ | |
270 | ||
271 | if ( !m_validatorWindow ) | |
272 | return; | |
273 | ||
274 | wxTextCtrl *textCtrl = (wxTextCtrl *)m_validatorWindow; | |
275 | ||
276 | int keyCode = event.KeyCode(); | |
1ecc4d80 RR |
277 | if (keyCode == WXK_DELETE || keyCode == WXK_RETURN || keyCode == WXK_BACK || |
278 | keyCode == WXK_HOME || keyCode == WXK_LEFT || keyCode == WXK_UP || | |
279 | keyCode == WXK_RIGHT || keyCode == WXK_DOWN || keyCode == WXK_PRIOR || | |
280 | keyCode == WXK_NEXT || keyCode == WXK_END || keyCode == WXK_HOME) | |
c801d85f KB |
281 | { |
282 | textCtrl->wxTextCtrl::OnChar(event); | |
283 | return ; | |
284 | } | |
285 | ||
286 | if ( (m_validatorStyle & wxFILTER_ASCII) && !isascii(keyCode) ) | |
287 | { | |
288 | wxBell(); | |
289 | return; | |
290 | } | |
291 | if ( (m_validatorStyle & wxFILTER_ALPHA) && !isalpha(keyCode) ) | |
292 | { | |
293 | wxBell(); | |
294 | return; | |
295 | } | |
296 | if ( (m_validatorStyle & wxFILTER_ALPHANUMERIC) && !isalnum(keyCode) ) | |
297 | { | |
298 | wxBell(); | |
299 | return; | |
300 | } | |
5885ba76 | 301 | if ( (m_validatorStyle & wxFILTER_NUMERIC) && !isdigit(keyCode) && keyCode != '.' && keyCode != '-') |
c801d85f KB |
302 | { |
303 | wxBell(); | |
304 | return; | |
305 | } | |
306 | ||
307 | textCtrl->wxTextCtrl::OnChar(event); | |
308 | } | |
309 | ||
386af6a2 JS |
310 | static bool wxIsNumeric(const wxString& val) |
311 | { | |
312 | int i; | |
313 | for ( i = 0; i < (int)val.Length(); i++) | |
314 | { | |
315 | if ((!isdigit(val[i])) && (val[i] != '.')) | |
0ded21d1 | 316 | if(!((i == 0) && (val[i] == '-'))) |
386af6a2 JS |
317 | return FALSE; |
318 | } | |
319 | return TRUE; | |
320 | } | |
c801d85f | 321 |