]> git.saurik.com Git - wxWidgets.git/blob - src/common/valtext.cpp
(hopefully) final touches to wxArtProvider
[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 // NB: this format string should contian exactly one '%s'
136 wxString errormsg;
137
138 bool includeList = (m_validatorStyle & wxFILTER_INCLUDE_LIST) != 0;
139 if ( includeList || (m_validatorStyle & wxFILTER_EXCLUDE_LIST) )
140 {
141 // if includeList, it's only ok to have the members of the list,
142 // otherwise it's only ok to have non-members
143 ok = includeList == m_includeList.Member(val);
144 if ( !ok )
145 {
146 errormsg = _("'%s' is invalid");
147 }
148 }
149 else if ( (m_validatorStyle & wxFILTER_ASCII) && !val.IsAscii() )
150 {
151 ok = FALSE;
152
153 errormsg = _("'%s' should only contain ASCII characters.");
154 }
155 else if ( (m_validatorStyle & wxFILTER_ALPHA) && !wxIsAlpha(val) )
156 {
157 ok = FALSE;
158
159 errormsg = _("'%s' should only contain alphabetic characters.");
160 }
161 else if ( (m_validatorStyle & wxFILTER_ALPHANUMERIC) && !wxIsAlphaNumeric(val))
162 {
163 ok = FALSE;
164
165 errormsg = _("'%s' should only contain alphabetic or numeric characters.");
166 }
167 else if ( (m_validatorStyle & wxFILTER_NUMERIC) && !wxIsNumeric(val))
168 {
169 ok = FALSE;
170
171 errormsg = _("'%s' should be numeric.");
172 }
173
174 if ( !ok )
175 {
176 wxASSERT_MSG( !errormsg.empty(), _T("you forgot to set errormsg") );
177
178 m_validatorWindow->SetFocus();
179
180 wxString buf;
181 buf.Printf(errormsg, val.c_str());
182
183 wxMessageBox(buf, _("Validation conflict"),
184 wxOK | wxICON_EXCLAMATION, parent);
185 }
186
187 return ok;
188 }
189
190 // Called to transfer data to the window
191 bool wxTextValidator::TransferToWindow(void)
192 {
193 if( !CheckValidator() )
194 return FALSE;
195
196 wxTextCtrl *control = (wxTextCtrl *) m_validatorWindow ;
197 control->SetValue(* m_stringValue) ;
198
199 return TRUE;
200 }
201
202 // Called to transfer data to the window
203 bool wxTextValidator::TransferFromWindow(void)
204 {
205 if( !CheckValidator() )
206 return FALSE;
207
208 wxTextCtrl *control = (wxTextCtrl *) m_validatorWindow ;
209 * m_stringValue = control->GetValue() ;
210
211 return TRUE;
212 }
213
214 void wxTextValidator::SetIncludeList(const wxStringList& list)
215 {
216 /*
217 if ( !M_VTEXTDATA )
218 return;
219 */
220
221 m_includeList.Clear();
222 // TODO: replace with =
223 wxNode *node = list.First() ;
224 while ( node )
225 {
226 wxChar *s = (wxChar *)node->Data();
227 m_includeList.Add(s);
228 node = node->Next();
229 }
230 }
231
232 void wxTextValidator::SetExcludeList(const wxStringList& list)
233 {
234 /*
235 if ( !M_VTEXTDATA )
236 return;
237 */
238
239 m_excludeList.Clear();
240 // TODO: replace with =
241 wxNode *node = list.First() ;
242 while ( node )
243 {
244 wxChar *s = (wxChar *)node->Data();
245 m_excludeList.Add(s);
246 node = node->Next();
247 }
248 }
249
250 void wxTextValidator::OnChar(wxKeyEvent& event)
251 {
252 /*
253 if ( !M_VTEXTDATA )
254 return;
255 */
256
257 if ( m_validatorWindow )
258 {
259 int keyCode = (int)event.KeyCode();
260
261 // we don't filter special keys and Delete
262 if (
263 !(keyCode < WXK_SPACE || keyCode == WXK_DELETE || keyCode > WXK_START) &&
264 (
265 ((m_validatorStyle & wxFILTER_ASCII) && !isascii(keyCode)) ||
266 ((m_validatorStyle & wxFILTER_ALPHA) && !wxIsalpha(keyCode)) ||
267 ((m_validatorStyle & wxFILTER_ALPHANUMERIC) && !wxIsalnum(keyCode)) ||
268 ((m_validatorStyle & wxFILTER_NUMERIC) && !wxIsdigit(keyCode)
269 && keyCode != '.' && keyCode != ',' && keyCode != '-')
270 )
271 )
272 {
273 if ( !wxValidator::IsSilent() )
274 wxBell();
275
276 // eat message
277 return;
278 }
279 }
280
281 event.Skip();
282 }
283
284 static bool wxIsNumeric(const wxString& val)
285 {
286 int i;
287 for ( i = 0; i < (int)val.Length(); i++)
288 {
289 // Allow for "," (French) as well as "." -- in future we should
290 // use wxSystemSettings or other to do better localisation
291 if ((!isdigit(val[i])) && (val[i] != '.') && (val[i] != ','))
292 if(!((i == 0) && (val[i] == '-')))
293 return FALSE;
294 }
295 return TRUE;
296 }
297
298 #endif
299 // wxUSE_VALIDATORS