]> git.saurik.com Git - wxWidgets.git/blob - src/common/valtext.cpp
Work on streams of all sorts. More to come.
[wxWidgets.git] / src / common / valtext.cpp
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 "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"
35 #endif
36
37 #include "wx/valtext.h"
38
39 #include <ctype.h>
40 #include <string.h>
41 #include <stdlib.h>
42
43 #ifdef __SALFORDC__
44 #include <clib.h>
45 #endif
46
47 #if !USE_SHARED_LIBRARY
48 IMPLEMENT_DYNAMIC_CLASS(wxTextValidator, wxValidator)
49
50 BEGIN_EVENT_TABLE(wxTextValidator, wxValidator)
51 EVT_CHAR(wxTextValidator::OnChar)
52 END_EVENT_TABLE()
53 #endif
54
55 static bool wxIsNumeric(const wxString& val);
56
57 wxTextValidator::wxTextValidator(long style, wxString *val)
58 {
59 m_validatorStyle = style ;
60 m_stringValue = val ;
61 /*
62 m_refData = new wxVTextRefData;
63
64 M_VTEXTDATA->m_validatorStyle = style ;
65 M_VTEXTDATA->m_stringValue = val ;
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
78 m_validatorStyle = val.m_validatorStyle ;
79 m_stringValue = val.m_stringValue ;
80
81 wxNode *node = val.m_includeList.First() ;
82 while ( node )
83 {
84 wxChar *s = (wxChar *)node->Data();
85 m_includeList.Add(s);
86 node = node->Next();
87 }
88 node = val.m_excludeList.First() ;
89 while ( node )
90 {
91 wxChar *s = (wxChar *)node->Data();
92 m_excludeList.Add(s);
93 node = node->Next();
94 }
95 return TRUE;
96 }
97
98 wxTextValidator::~wxTextValidator()
99 {
100 }
101
102 static bool wxIsAlpha(const wxString& val)
103 {
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;
111 }
112
113 static bool wxIsAlphaNumeric(const wxString& val)
114 {
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;
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 {
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
137 // If window is disabled, simply return
138 if ( !control->IsEnabled() )
139 return TRUE;
140
141 wxString val(control->GetValue());
142
143 bool ok = TRUE;
144
145 // this format string should contian exactly one '%s'
146 const wxChar *errormsg = _("'%s' is invalid");
147
148 if ( m_validatorStyle & wxFILTER_INCLUDE_LIST )
149 {
150 if ( !m_includeList.Member(val) )
151 {
152 ok = FALSE;
153 }
154 }
155 else if ( m_validatorStyle & wxFILTER_EXCLUDE_LIST )
156 {
157 if ( m_excludeList.Member(val) )
158 {
159 ok = FALSE;
160 }
161 }
162 else if ( (m_validatorStyle & wxFILTER_ASCII) && !val.IsAscii() )
163 {
164 ok = FALSE;
165
166 errormsg = _("'%s' should only contain ASCII characters.");
167 }
168 else if ( (m_validatorStyle & wxFILTER_ALPHA) && !wxIsAlpha(val) )
169 {
170 ok = FALSE;
171
172 errormsg = _("'%s' should only contain alphabetic characters.");
173 }
174 else if ( (m_validatorStyle & wxFILTER_ALPHANUMERIC) && !wxIsAlphaNumeric(val))
175 {
176 ok = FALSE;
177
178 errormsg = _("'%s' should only contain alphabetic or numeric characters.");
179 }
180 else if ( (m_validatorStyle & wxFILTER_NUMERIC) && !wxIsNumeric(val))
181 {
182 ok = FALSE;
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;
199 }
200
201 // Called to transfer data to the window
202 bool wxTextValidator::TransferToWindow(void)
203 {
204 if ( !m_validatorWindow )
205 return FALSE;
206 if ( !m_validatorWindow->IsKindOf(CLASSINFO(wxTextCtrl)) )
207 return FALSE;
208 if ( !m_stringValue )
209 return FALSE;
210
211 wxTextCtrl *control = (wxTextCtrl *) m_validatorWindow ;
212 control->SetValue(* m_stringValue) ;
213
214 return TRUE;
215 }
216
217 // Called to transfer data to the window
218 bool wxTextValidator::TransferFromWindow(void)
219 {
220 if ( !m_validatorWindow )
221 return FALSE;
222 if ( !m_validatorWindow->IsKindOf(CLASSINFO(wxTextCtrl)) )
223 return FALSE;
224 if ( !m_stringValue )
225 return FALSE;
226
227 wxTextCtrl *control = (wxTextCtrl *) m_validatorWindow ;
228 * m_stringValue = control->GetValue() ;
229
230 return TRUE;
231 }
232
233 void wxTextValidator::SetIncludeList(const wxStringList& list)
234 {
235 /*
236 if ( !M_VTEXTDATA )
237 return;
238 */
239
240 m_includeList.Clear();
241 // TODO: replace with =
242 wxNode *node = list.First() ;
243 while ( node )
244 {
245 wxChar *s = (wxChar *)node->Data();
246 m_includeList.Add(s);
247 node = node->Next();
248 }
249 }
250
251 void wxTextValidator::SetExcludeList(const wxStringList& list)
252 {
253 /*
254 if ( !M_VTEXTDATA )
255 return;
256 */
257
258 m_excludeList.Clear();
259 // TODO: replace with =
260 wxNode *node = list.First() ;
261 while ( node )
262 {
263 wxChar *s = (wxChar *)node->Data();
264 m_excludeList.Add(s);
265 node = node->Next();
266 }
267 }
268
269 void wxTextValidator::OnChar(wxKeyEvent& event)
270 {
271 /*
272 if ( !M_VTEXTDATA )
273 return;
274 */
275
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();
301 }
302
303 static bool wxIsNumeric(const wxString& val)
304 {
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;
313 }
314
315 #endif
316 // wxUSE_VALIDATORS
317