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