]> git.saurik.com Git - wxWidgets.git/blob - src/common/valtext.cpp
Some doc tweaks
[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 #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"
31 #endif
32
33 #include "wx/valtext.h"
34
35 #include <ctype.h>
36 #include <string.h>
37 #include <stdlib.h>
38
39 #ifdef __SALFORDC__
40 #include <clib.h>
41 #endif
42
43 IMPLEMENT_DYNAMIC_CLASS(wxTextValidator, wxValidator)
44
45 BEGIN_EVENT_TABLE(wxTextValidator, wxValidator)
46 EVT_CHAR(wxTextValidator::OnChar)
47 END_EVENT_TABLE()
48
49 static bool wxIsNumeric(const wxString& val);
50
51 wxTextValidator::wxTextValidator(long style, wxString *val)
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 wxChar *s = (wxChar *)node->Data();
79 m_includeList.Add(s);
80 node = node->Next();
81 }
82 node = val.m_excludeList.First() ;
83 while ( node )
84 {
85 wxChar *s = (wxChar *)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 (!wxIsalpha(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 (!wxIsalnum(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( !CheckValidator() )
123 return FALSE;
124
125 wxTextCtrl *control = (wxTextCtrl *) m_validatorWindow ;
126
127 // If window is disabled, simply return
128 if ( !control->IsEnabled() )
129 return TRUE;
130
131 wxString val(control->GetValue());
132
133 bool ok = TRUE;
134
135 // this format string should contian exactly one '%s'
136 const wxChar *errormsg = _("'%s' is invalid");
137
138 if ( m_validatorStyle & wxFILTER_INCLUDE_LIST )
139 {
140 if ( !m_includeList.Member(val) )
141 {
142 ok = FALSE;
143 }
144 }
145 else if ( m_validatorStyle & wxFILTER_EXCLUDE_LIST )
146 {
147 if ( m_excludeList.Member(val) )
148 {
149 ok = FALSE;
150 }
151 }
152 else if ( (m_validatorStyle & wxFILTER_ASCII) && !val.IsAscii() )
153 {
154 ok = FALSE;
155
156 errormsg = _("'%s' should only contain ASCII characters.");
157 }
158 else if ( (m_validatorStyle & wxFILTER_ALPHA) && !wxIsAlpha(val) )
159 {
160 ok = FALSE;
161
162 errormsg = _("'%s' should only contain alphabetic characters.");
163 }
164 else if ( (m_validatorStyle & wxFILTER_ALPHANUMERIC) && !wxIsAlphaNumeric(val))
165 {
166 ok = FALSE;
167
168 errormsg = _("'%s' should only contain alphabetic or numeric characters.");
169 }
170 else if ( (m_validatorStyle & wxFILTER_NUMERIC) && !wxIsNumeric(val))
171 {
172 ok = FALSE;
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;
189 }
190
191 // Called to transfer data to the window
192 bool wxTextValidator::TransferToWindow(void)
193 {
194 if( !CheckValidator() )
195 return FALSE;
196
197 wxTextCtrl *control = (wxTextCtrl *) m_validatorWindow ;
198 control->SetValue(* m_stringValue) ;
199
200 return TRUE;
201 }
202
203 // Called to transfer data to the window
204 bool wxTextValidator::TransferFromWindow(void)
205 {
206 if( !CheckValidator() )
207 return FALSE;
208
209 wxTextCtrl *control = (wxTextCtrl *) m_validatorWindow ;
210 * m_stringValue = control->GetValue() ;
211
212 return TRUE;
213 }
214
215 void wxTextValidator::SetIncludeList(const wxStringList& list)
216 {
217 /*
218 if ( !M_VTEXTDATA )
219 return;
220 */
221
222 m_includeList.Clear();
223 // TODO: replace with =
224 wxNode *node = list.First() ;
225 while ( node )
226 {
227 wxChar *s = (wxChar *)node->Data();
228 m_includeList.Add(s);
229 node = node->Next();
230 }
231 }
232
233 void wxTextValidator::SetExcludeList(const wxStringList& list)
234 {
235 /*
236 if ( !M_VTEXTDATA )
237 return;
238 */
239
240 m_excludeList.Clear();
241 // TODO: replace with =
242 wxNode *node = list.First() ;
243 while ( node )
244 {
245 wxChar *s = (wxChar *)node->Data();
246 m_excludeList.Add(s);
247 node = node->Next();
248 }
249 }
250
251 void wxTextValidator::OnChar(wxKeyEvent& event)
252 {
253 /*
254 if ( !M_VTEXTDATA )
255 return;
256 */
257
258 if ( m_validatorWindow )
259 {
260 int keyCode = (int)event.KeyCode();
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)) ||
267 ((m_validatorStyle & wxFILTER_ALPHA) && !wxIsalpha(keyCode)) ||
268 ((m_validatorStyle & wxFILTER_ALPHANUMERIC) && !wxIsalnum(keyCode)) ||
269 ((m_validatorStyle & wxFILTER_NUMERIC) && !wxIsdigit(keyCode)
270 && keyCode != '.' && keyCode != ',' && keyCode != '-')
271 )
272 )
273 {
274 if ( !wxValidator::IsSilent() )
275 wxBell();
276
277 // eat message
278 return;
279 }
280 }
281
282 event.Skip();
283 }
284
285 static bool wxIsNumeric(const wxString& val)
286 {
287 int i;
288 for ( i = 0; i < (int)val.Length(); i++)
289 {
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] != ','))
293 if(!((i == 0) && (val[i] == '-')))
294 return FALSE;
295 }
296 return TRUE;
297 }
298
299 #endif
300 // wxUSE_VALIDATORS