]> git.saurik.com Git - wxWidgets.git/blob - src/common/valtext.cpp
deTABified
[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
9 // Licence: wxWindows licence
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 && wxUSE_TEXTCTRL
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 : wxValidator()
65 {
66 Copy(val);
67 }
68
69 bool wxTextValidator::Copy(const wxTextValidator& val)
70 {
71 wxValidator::Copy(val);
72
73 m_validatorStyle = val.m_validatorStyle ;
74 m_stringValue = val.m_stringValue ;
75
76 wxStringList::Node *node = val.m_includeList.GetFirst() ;
77 while ( node )
78 {
79 wxChar *s = node->GetData();
80 m_includeList.Add(s);
81 node = node->GetNext();
82 }
83 node = val.m_excludeList.GetFirst() ;
84 while ( node )
85 {
86 wxChar *s = node->GetData();
87 m_excludeList.Add(s);
88 node = node->GetNext();
89 }
90 return TRUE;
91 }
92
93 wxTextValidator::~wxTextValidator()
94 {
95 }
96
97 static bool wxIsAlpha(const wxString& val)
98 {
99 int i;
100 for ( i = 0; i < (int)val.Length(); i++)
101 {
102 if (!wxIsalpha(val[i]))
103 return FALSE;
104 }
105 return TRUE;
106 }
107
108 static bool wxIsAlphaNumeric(const wxString& val)
109 {
110 int i;
111 for ( i = 0; i < (int)val.Length(); i++)
112 {
113 if (!wxIsalnum(val[i]))
114 return FALSE;
115 }
116 return TRUE;
117 }
118
119 // Called when the value in the window must be validated.
120 // This function can pop up an error message.
121 bool wxTextValidator::Validate(wxWindow *parent)
122 {
123 if( !CheckValidator() )
124 return FALSE;
125
126 wxTextCtrl *control = (wxTextCtrl *) m_validatorWindow ;
127
128 // If window is disabled, simply return
129 if ( !control->IsEnabled() )
130 return TRUE;
131
132 wxString val(control->GetValue());
133
134 bool ok = TRUE;
135
136 // NB: this format string should contian exactly one '%s'
137 wxString errormsg;
138
139 bool includeList = (m_validatorStyle & wxFILTER_INCLUDE_LIST) != 0;
140 if ( includeList || (m_validatorStyle & wxFILTER_EXCLUDE_LIST) )
141 {
142 // if includeList, it's only ok to have the members of the list,
143 // otherwise it's only ok to have non-members
144 ok = includeList == m_includeList.Member(val);
145 if ( !ok )
146 {
147 errormsg = _("'%s' is invalid");
148 }
149 }
150 else if ( (m_validatorStyle & wxFILTER_ASCII) && !val.IsAscii() )
151 {
152 ok = FALSE;
153
154 errormsg = _("'%s' should only contain ASCII characters.");
155 }
156 else if ( (m_validatorStyle & wxFILTER_ALPHA) && !wxIsAlpha(val) )
157 {
158 ok = FALSE;
159
160 errormsg = _("'%s' should only contain alphabetic characters.");
161 }
162 else if ( (m_validatorStyle & wxFILTER_ALPHANUMERIC) && !wxIsAlphaNumeric(val))
163 {
164 ok = FALSE;
165
166 errormsg = _("'%s' should only contain alphabetic or numeric characters.");
167 }
168 else if ( (m_validatorStyle & wxFILTER_NUMERIC) && !wxIsNumeric(val))
169 {
170 ok = FALSE;
171
172 errormsg = _("'%s' should be numeric.");
173 }
174 else if ( (m_validatorStyle & wxFILTER_INCLUDE_CHAR_LIST) && !IsInCharIncludeList(val))
175 {
176 //it's only ok to have the members of the list
177 errormsg = _("'%s' is invalid");
178 ok = FALSE;
179 }
180 else if ( (m_validatorStyle & wxFILTER_EXCLUDE_CHAR_LIST) && !IsNotInCharExcludeList(val))
181 {
182 // it's only ok to have non-members of the list
183 errormsg = _("'%s' is invalid");
184 ok = FALSE;
185 }
186
187 if ( !ok )
188 {
189 wxASSERT_MSG( !errormsg.empty(), _T("you forgot to set errormsg") );
190
191 m_validatorWindow->SetFocus();
192
193 wxString buf;
194 buf.Printf(errormsg, val.c_str());
195
196 wxMessageBox(buf, _("Validation conflict"),
197 wxOK | wxICON_EXCLAMATION, parent);
198 }
199
200 return ok;
201 }
202
203 // Called to transfer data to the window
204 bool wxTextValidator::TransferToWindow(void)
205 {
206 if( !CheckValidator() )
207 return FALSE;
208
209 if (!m_stringValue)
210 return TRUE;
211
212 wxTextCtrl *control = (wxTextCtrl *) m_validatorWindow ;
213 control->SetValue(* m_stringValue) ;
214
215 return TRUE;
216 }
217
218 // Called to transfer data to the window
219 bool wxTextValidator::TransferFromWindow(void)
220 {
221 if( !CheckValidator() )
222 return FALSE;
223
224 if (!m_stringValue)
225 return TRUE;
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 wxStringList::Node *node = list.GetFirst();
243 while ( node )
244 {
245 wxChar *s = node->GetData();
246 m_includeList.Add(s);
247 node = node->GetNext();
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 wxStringList::Node *node = list.GetFirst() ;
261 while ( node )
262 {
263 wxChar *s = node->GetData();
264 m_excludeList.Add(s);
265 node = node->GetNext();
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.GetKeyCode();
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_INCLUDE_CHAR_LIST) && !IsInCharIncludeList(wxString((char) keyCode, 1))) ||
285 ((m_validatorStyle & wxFILTER_EXCLUDE_CHAR_LIST) && !IsNotInCharExcludeList(wxString((char) keyCode, 1))) ||
286 ((m_validatorStyle & wxFILTER_ASCII) && !isascii(keyCode)) ||
287 ((m_validatorStyle & wxFILTER_ALPHA) && !wxIsalpha(keyCode)) ||
288 ((m_validatorStyle & wxFILTER_ALPHANUMERIC) && !wxIsalnum(keyCode)) ||
289 ((m_validatorStyle & wxFILTER_NUMERIC) && !wxIsdigit(keyCode)
290 && keyCode != '.' && keyCode != ',' && keyCode != '-')
291 )
292 )
293 {
294 if ( !wxValidator::IsSilent() )
295 wxBell();
296
297 // eat message
298 return;
299 }
300 }
301
302 event.Skip();
303 }
304
305 static bool wxIsNumeric(const wxString& val)
306 {
307 int i;
308 for ( i = 0; i < (int)val.Length(); i++)
309 {
310 // Allow for "," (French) as well as "." -- in future we should
311 // use wxSystemSettings or other to do better localisation
312 if ((!isdigit(val[i])) && (val[i] != '.') && (val[i] != ',') && (val[i] != wxT('e')) && (val[i] != wxT('E')) && (val[i] != wxT('+')) && (val[i] != wxT('-')))
313 return FALSE;
314 }
315 return TRUE;
316 }
317
318 bool wxTextValidator::IsInCharIncludeList(const wxString& val)
319 {
320 size_t i;
321 for ( i = 0; i < val.Length(); i++)
322 {
323 if (!m_includeList.Member((wxString) val[i]))
324 return FALSE;
325 }
326 return TRUE;
327 }
328
329 bool wxTextValidator::IsNotInCharExcludeList(const wxString& val)
330 {
331 size_t i;
332 for ( i = 0; i < val.Length(); i++)
333 {
334 if (m_excludeList.Member((wxString) val[i]))
335 return FALSE;
336 }
337 return TRUE;
338 }
339
340 #endif
341 // wxUSE_VALIDATORS && wxUSE_TEXTCTRL