]> git.saurik.com Git - wxWidgets.git/blob - src/common/valtext.cpp
added default ctor for wxDirDialog
[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 #if !USE_SHARED_LIBRARY
44 IMPLEMENT_DYNAMIC_CLASS(wxTextValidator, wxValidator)
45
46 BEGIN_EVENT_TABLE(wxTextValidator, wxValidator)
47 EVT_CHAR(wxTextValidator::OnChar)
48 END_EVENT_TABLE()
49 #endif
50
51 static bool wxIsNumeric(const wxString& val);
52
53 wxTextValidator::wxTextValidator(long style, wxString *val)
54 {
55 m_validatorStyle = style ;
56 m_stringValue = val ;
57 /*
58 m_refData = new wxVTextRefData;
59
60 M_VTEXTDATA->m_validatorStyle = style ;
61 M_VTEXTDATA->m_stringValue = val ;
62 */
63 }
64
65 wxTextValidator::wxTextValidator(const wxTextValidator& val)
66 {
67 Copy(val);
68 }
69
70 bool wxTextValidator::Copy(const wxTextValidator& val)
71 {
72 wxValidator::Copy(val);
73
74 m_validatorStyle = val.m_validatorStyle ;
75 m_stringValue = val.m_stringValue ;
76
77 wxNode *node = val.m_includeList.First() ;
78 while ( node )
79 {
80 wxChar *s = (wxChar *)node->Data();
81 m_includeList.Add(s);
82 node = node->Next();
83 }
84 node = val.m_excludeList.First() ;
85 while ( node )
86 {
87 wxChar *s = (wxChar *)node->Data();
88 m_excludeList.Add(s);
89 node = node->Next();
90 }
91 return TRUE;
92 }
93
94 wxTextValidator::~wxTextValidator()
95 {
96 }
97
98 static bool wxIsAlpha(const wxString& val)
99 {
100 int i;
101 for ( i = 0; i < (int)val.Length(); i++)
102 {
103 if (!isalpha(val[i]))
104 return FALSE;
105 }
106 return TRUE;
107 }
108
109 static bool wxIsAlphaNumeric(const wxString& val)
110 {
111 int i;
112 for ( i = 0; i < (int)val.Length(); i++)
113 {
114 if (!isalnum(val[i]))
115 return FALSE;
116 }
117 return TRUE;
118 }
119
120 // Called when the value in the window must be validated.
121 // This function can pop up an error message.
122 bool wxTextValidator::Validate(wxWindow *parent)
123 {
124 if ( !m_validatorWindow )
125 return FALSE;
126 if ( !m_validatorWindow->IsKindOf(CLASSINFO(wxTextCtrl)) )
127 return FALSE;
128 if ( !m_stringValue )
129 return FALSE;
130
131 wxTextCtrl *control = (wxTextCtrl *) m_validatorWindow ;
132
133 // If window is disabled, simply return
134 if ( !control->IsEnabled() )
135 return TRUE;
136
137 wxString val(control->GetValue());
138
139 bool ok = TRUE;
140
141 // this format string should contian exactly one '%s'
142 const wxChar *errormsg = _("'%s' is invalid");
143
144 if ( m_validatorStyle & wxFILTER_INCLUDE_LIST )
145 {
146 if ( !m_includeList.Member(val) )
147 {
148 ok = FALSE;
149 }
150 }
151 else if ( m_validatorStyle & wxFILTER_EXCLUDE_LIST )
152 {
153 if ( m_excludeList.Member(val) )
154 {
155 ok = FALSE;
156 }
157 }
158 else if ( (m_validatorStyle & wxFILTER_ASCII) && !val.IsAscii() )
159 {
160 ok = FALSE;
161
162 errormsg = _("'%s' should only contain ASCII characters.");
163 }
164 else if ( (m_validatorStyle & wxFILTER_ALPHA) && !wxIsAlpha(val) )
165 {
166 ok = FALSE;
167
168 errormsg = _("'%s' should only contain alphabetic characters.");
169 }
170 else if ( (m_validatorStyle & wxFILTER_ALPHANUMERIC) && !wxIsAlphaNumeric(val))
171 {
172 ok = FALSE;
173
174 errormsg = _("'%s' should only contain alphabetic or numeric characters.");
175 }
176 else if ( (m_validatorStyle & wxFILTER_NUMERIC) && !wxIsNumeric(val))
177 {
178 ok = FALSE;
179
180 errormsg = _("'%s' should be numeric.");
181 }
182
183 if ( !ok )
184 {
185 m_validatorWindow->SetFocus();
186
187 wxString buf;
188 buf.Printf(errormsg, val.c_str());
189
190 wxMessageBox(buf, _("Validation conflict"),
191 wxOK | wxICON_EXCLAMATION, parent);
192 }
193
194 return ok;
195 }
196
197 // Called to transfer data to the window
198 bool wxTextValidator::TransferToWindow(void)
199 {
200 if ( !m_validatorWindow )
201 return FALSE;
202 if ( !m_validatorWindow->IsKindOf(CLASSINFO(wxTextCtrl)) )
203 return FALSE;
204 if ( !m_stringValue )
205 return FALSE;
206
207 wxTextCtrl *control = (wxTextCtrl *) m_validatorWindow ;
208 control->SetValue(* m_stringValue) ;
209
210 return TRUE;
211 }
212
213 // Called to transfer data to the window
214 bool wxTextValidator::TransferFromWindow(void)
215 {
216 if ( !m_validatorWindow )
217 return FALSE;
218 if ( !m_validatorWindow->IsKindOf(CLASSINFO(wxTextCtrl)) )
219 return FALSE;
220 if ( !m_stringValue )
221 return FALSE;
222
223 wxTextCtrl *control = (wxTextCtrl *) m_validatorWindow ;
224 * m_stringValue = control->GetValue() ;
225
226 return TRUE;
227 }
228
229 void wxTextValidator::SetIncludeList(const wxStringList& list)
230 {
231 /*
232 if ( !M_VTEXTDATA )
233 return;
234 */
235
236 m_includeList.Clear();
237 // TODO: replace with =
238 wxNode *node = list.First() ;
239 while ( node )
240 {
241 wxChar *s = (wxChar *)node->Data();
242 m_includeList.Add(s);
243 node = node->Next();
244 }
245 }
246
247 void wxTextValidator::SetExcludeList(const wxStringList& list)
248 {
249 /*
250 if ( !M_VTEXTDATA )
251 return;
252 */
253
254 m_excludeList.Clear();
255 // TODO: replace with =
256 wxNode *node = list.First() ;
257 while ( node )
258 {
259 wxChar *s = (wxChar *)node->Data();
260 m_excludeList.Add(s);
261 node = node->Next();
262 }
263 }
264
265 void wxTextValidator::OnChar(wxKeyEvent& event)
266 {
267 /*
268 if ( !M_VTEXTDATA )
269 return;
270 */
271
272 if ( m_validatorWindow )
273 {
274 int keyCode = event.KeyCode();
275
276 // we don't filter special keys and Delete
277 if (
278 !(keyCode < WXK_SPACE || keyCode == WXK_DELETE || keyCode > WXK_START) &&
279 (
280 ((m_validatorStyle & wxFILTER_ASCII) && !isascii(keyCode)) ||
281 ((m_validatorStyle & wxFILTER_ALPHA) && !isalpha(keyCode)) ||
282 ((m_validatorStyle & wxFILTER_ALPHANUMERIC) && !isalnum(keyCode)) ||
283 ((m_validatorStyle & wxFILTER_NUMERIC) && !isdigit(keyCode)
284 && keyCode != '.' && keyCode != ',' && keyCode != '-')
285 )
286 )
287 {
288 if ( !wxValidator::IsSilent() )
289 wxBell();
290
291 // eat message
292 return;
293 }
294 }
295
296 event.Skip();
297 }
298
299 static bool wxIsNumeric(const wxString& val)
300 {
301 int i;
302 for ( i = 0; i < (int)val.Length(); i++)
303 {
304 // Allow for "," (French) as well as "." -- in future we should
305 // use wxSystemSettings or other to do better localisation
306 if ((!isdigit(val[i])) && (val[i] != '.') && (val[i] != ','))
307 if(!((i == 0) && (val[i] == '-')))
308 return FALSE;
309 }
310 return TRUE;
311 }
312
313 #endif
314 // wxUSE_VALIDATORS