]> git.saurik.com Git - wxWidgets.git/blob - src/common/valtext.cpp
1. C++ comments removed from setup.h
[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 #ifdef __SALFORDC__
38 #include <clib.h>
39 #endif
40
41 #if !USE_SHARED_LIBRARY
42 IMPLEMENT_DYNAMIC_CLASS(wxTextValidator, wxValidator)
43
44 BEGIN_EVENT_TABLE(wxTextValidator, wxValidator)
45 EVT_CHAR(wxTextValidator::OnChar)
46 END_EVENT_TABLE()
47 #endif
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 char *s = (char *)node->Data();
79 m_includeList.Add(s);
80 node = node->Next();
81 }
82 node = val.m_excludeList.First() ;
83 while ( node )
84 {
85 char *s = (char *)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 (!isalpha(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 (!isalnum(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 ( !m_validatorWindow )
123 return FALSE;
124 if ( !m_validatorWindow->IsKindOf(CLASSINFO(wxTextCtrl)) )
125 return FALSE;
126 if ( !m_stringValue )
127 return FALSE;
128
129 wxTextCtrl *control = (wxTextCtrl *) m_validatorWindow ;
130
131 // If window is disabled, don't validate
132 if ( !control->Enabled() )
133 return FALSE;
134
135 wxString val(control->GetValue());
136
137 if ( m_validatorStyle & wxFILTER_INCLUDE_LIST )
138 {
139 if ( !m_includeList.Member(val) )
140 {
141 m_validatorWindow->SetFocus();
142 char buf[512];
143 sprintf(buf, _("%s is invalid."), (const char *)val);
144 wxMessageBox(buf, _("Validation conflict"), wxOK | wxICON_EXCLAMATION, parent);
145 return FALSE;
146 }
147 }
148 if ( m_validatorStyle & wxFILTER_EXCLUDE_LIST )
149 {
150 if ( m_excludeList.Member(val) )
151 {
152 m_validatorWindow->SetFocus();
153 char buf[512];
154 sprintf(buf, _("%s is invalid."), (const char *)val);
155 wxMessageBox(buf, _("Validation conflict"), wxOK | wxICON_EXCLAMATION, parent);
156 return FALSE;
157 }
158 }
159 if ( (m_validatorStyle & wxFILTER_ASCII) && !val.IsAscii() )
160 {
161 m_validatorWindow->SetFocus();
162 char buf[512];
163 sprintf(buf, _("%s should only contain ASCII characters."), (const char *)val);
164 wxMessageBox(buf, _("Validation conflict"), wxOK | wxICON_EXCLAMATION, parent);
165 return FALSE;
166 }
167 if ( (m_validatorStyle & wxFILTER_ALPHA) && !wxIsAlpha(val) )
168 {
169 m_validatorWindow->SetFocus();
170 char buf[512];
171 sprintf(buf, _("%s should only contain alphabetic characters."), (const char *)val);
172 wxMessageBox(buf, _("Validation conflict"), wxOK | wxICON_EXCLAMATION, parent);
173 return FALSE;
174 }
175 if ( (m_validatorStyle & wxFILTER_ALPHANUMERIC) && !wxIsAlphaNumeric(val))
176 {
177 m_validatorWindow->SetFocus();
178 char buf[512];
179 sprintf(buf, _("%s should only contain alphabetic or numeric characters."), (const char *)val);
180 wxMessageBox(buf,_("Validation conflict"), wxOK | wxICON_EXCLAMATION, parent);
181 return FALSE;
182 }
183 if ( (m_validatorStyle & wxFILTER_NUMERIC) && !wxIsNumeric(val))
184
185 {
186 m_validatorWindow->SetFocus();
187 char buf[512];
188 sprintf(buf, _("%s should be numeric."), (const char *)val);
189 wxMessageBox(buf, _("Validation conflict"), wxOK | wxICON_EXCLAMATION, parent);
190 return FALSE;
191 }
192
193 return TRUE ;
194 }
195
196 // Called to transfer data to the window
197 bool wxTextValidator::TransferToWindow(void)
198 {
199 if ( !m_validatorWindow )
200 return FALSE;
201 if ( !m_validatorWindow->IsKindOf(CLASSINFO(wxTextCtrl)) )
202 return FALSE;
203 if ( !m_stringValue )
204 return FALSE;
205
206 wxTextCtrl *control = (wxTextCtrl *) m_validatorWindow ;
207 control->SetValue(* m_stringValue) ;
208
209 return TRUE;
210 }
211
212 // Called to transfer data to the window
213 bool wxTextValidator::TransferFromWindow(void)
214 {
215 if ( !m_validatorWindow )
216 return FALSE;
217 if ( !m_validatorWindow->IsKindOf(CLASSINFO(wxTextCtrl)) )
218 return FALSE;
219 if ( !m_stringValue )
220 return FALSE;
221
222 wxTextCtrl *control = (wxTextCtrl *) m_validatorWindow ;
223 * m_stringValue = control->GetValue() ;
224
225 return TRUE;
226 }
227
228 void wxTextValidator::SetIncludeList(const wxStringList& list)
229 {
230 /*
231 if ( !M_VTEXTDATA )
232 return;
233 */
234
235 m_includeList.Clear();
236 // TODO: replace with =
237 wxNode *node = list.First() ;
238 while ( node )
239 {
240 char *s = (char *)node->Data();
241 m_includeList.Add(s);
242 node = node->Next();
243 }
244 }
245
246 void wxTextValidator::SetExcludeList(const wxStringList& list)
247 {
248 /*
249 if ( !M_VTEXTDATA )
250 return;
251 */
252
253 m_excludeList.Clear();
254 // TODO: replace with =
255 wxNode *node = list.First() ;
256 while ( node )
257 {
258 char *s = (char *)node->Data();
259 m_excludeList.Add(s);
260 node = node->Next();
261 }
262 }
263
264 void wxTextValidator::OnChar(wxKeyEvent& event)
265 {
266 /*
267 if ( !M_VTEXTDATA )
268 return;
269 */
270
271 if ( !m_validatorWindow )
272 return;
273
274 wxTextCtrl *textCtrl = (wxTextCtrl *)m_validatorWindow;
275
276 int keyCode = event.KeyCode();
277 if (keyCode == WXK_DELETE || keyCode == WXK_RETURN || keyCode == WXK_BACK ||
278 keyCode == WXK_HOME || keyCode == WXK_LEFT || keyCode == WXK_UP ||
279 keyCode == WXK_RIGHT || keyCode == WXK_DOWN || keyCode == WXK_PRIOR ||
280 keyCode == WXK_NEXT || keyCode == WXK_END || keyCode == WXK_HOME)
281 {
282 textCtrl->wxTextCtrl::OnChar(event);
283 return ;
284 }
285
286 if ( (m_validatorStyle & wxFILTER_ASCII) && !isascii(keyCode) )
287 {
288 wxBell();
289 return;
290 }
291 if ( (m_validatorStyle & wxFILTER_ALPHA) && !isalpha(keyCode) )
292 {
293 wxBell();
294 return;
295 }
296 if ( (m_validatorStyle & wxFILTER_ALPHANUMERIC) && !isalnum(keyCode) )
297 {
298 wxBell();
299 return;
300 }
301 if ( (m_validatorStyle & wxFILTER_NUMERIC) && !isdigit(keyCode) && keyCode != '.' && keyCode != '-')
302 {
303 wxBell();
304 return;
305 }
306
307 textCtrl->wxTextCtrl::OnChar(event);
308 }
309
310 static bool wxIsNumeric(const wxString& val)
311 {
312 int i;
313 for ( i = 0; i < (int)val.Length(); i++)
314 {
315 if ((!isdigit(val[i])) && (val[i] != '.'))
316 if(!((i == 0) && (val[i] == '-')))
317 return FALSE;
318 }
319 return TRUE;
320 }
321