]> git.saurik.com Git - wxWidgets.git/blob - src/common/valtext.cpp
Add support for dynamic event tables
[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/msgbxdlg.h"
28 #endif
29
30 #include "wx/valtext.h"
31
32 #include <ctype.h>
33 #include <string.h>
34 #include <stdlib.h>
35
36 #if !USE_SHARED_LIBRARY
37 IMPLEMENT_DYNAMIC_CLASS(wxTextValidator, wxValidator)
38
39 BEGIN_EVENT_TABLE(wxTextValidator, wxValidator)
40 EVT_CHAR(wxTextValidator::OnChar)
41 END_EVENT_TABLE()
42 #endif
43
44 wxTextValidator::wxTextValidator(const long style, wxString *val)
45 {
46 m_validatorStyle = style ;
47 m_stringValue = val ;
48 /*
49 m_refData = new wxVTextRefData;
50
51 M_VTEXTDATA->m_validatorStyle = style ;
52 M_VTEXTDATA->m_stringValue = val ;
53 */
54 }
55
56 wxTextValidator::wxTextValidator(const wxTextValidator& val)
57 {
58 Copy(val);
59 }
60
61 bool wxTextValidator::Copy(const wxTextValidator& val)
62 {
63 wxValidator::Copy(val);
64
65 m_validatorStyle = val.m_validatorStyle ;
66 m_stringValue = val.m_stringValue ;
67
68 wxNode *node = val.m_includeList.First() ;
69 while ( node )
70 {
71 char *s = (char *)node->Data();
72 m_includeList.Add(s);
73 node = node->Next();
74 }
75 node = val.m_excludeList.First() ;
76 while ( node )
77 {
78 char *s = (char *)node->Data();
79 m_excludeList.Add(s);
80 node = node->Next();
81 }
82 return TRUE;
83 }
84
85 wxTextValidator::~wxTextValidator()
86 {
87 }
88
89 static bool wxIsAlpha(const wxString& val)
90 {
91 int i;
92 for ( i = 0; i < (int)val.Length(); i++)
93 {
94 if (!isalpha(val[i]))
95 return FALSE;
96 }
97 return TRUE;
98 }
99
100 static bool wxIsAlphaNumeric(const wxString& val)
101 {
102 int i;
103 for ( i = 0; i < (int)val.Length(); i++)
104 {
105 if (!isalnum(val[i]))
106 return FALSE;
107 }
108 return TRUE;
109 }
110
111 // Called when the value in the window must be validated.
112 // This function can pop up an error message.
113 bool wxTextValidator::Validate(wxWindow *parent)
114 {
115 if ( !m_validatorWindow )
116 return FALSE;
117 if ( !m_validatorWindow->IsKindOf(CLASSINFO(wxTextCtrl)) )
118 return FALSE;
119 if ( !m_stringValue )
120 return FALSE;
121
122 wxTextCtrl *control = (wxTextCtrl *) m_validatorWindow ;
123
124 // If window is disabled, don't validate
125 if ( !control->Enabled() )
126 return FALSE;
127
128 wxString val(control->GetValue());
129
130 if ( m_validatorStyle & wxFILTER_INCLUDE_LIST )
131 {
132 if ( !m_includeList.Member(val) )
133 {
134 char buf[512];
135 sprintf(buf, "%s is invalid.", (const char *)val);
136 wxMessageBox(buf, "Validation conflict", wxOK | wxICON_EXCLAMATION, parent);
137 return FALSE;
138 }
139 }
140 if ( m_validatorStyle & wxFILTER_EXCLUDE_LIST )
141 {
142 if ( m_excludeList.Member(val) )
143 {
144 char buf[512];
145 sprintf(buf, "%s is invalid.", (const char *)val);
146 wxMessageBox(buf, "Validation conflict", wxOK | wxICON_EXCLAMATION, parent);
147 return FALSE;
148 }
149 }
150 if ( (m_validatorStyle & wxFILTER_ASCII) && !val.IsAscii() )
151 {
152 char buf[512];
153 sprintf(buf, "%s should only contain ASCII characters.", (const char *)val);
154 wxMessageBox(buf, "Validation conflict", wxOK | wxICON_EXCLAMATION, parent);
155 return FALSE;
156 }
157 if ( (m_validatorStyle & wxFILTER_ALPHA) && !wxIsAlpha(val) )
158 {
159 char buf[512];
160 sprintf(buf, "%s should only contain alphabetic characters.", (const char *)val);
161 wxMessageBox(buf, "Validation conflict", wxOK | wxICON_EXCLAMATION, parent);
162 return FALSE;
163 }
164 if ( (m_validatorStyle & wxFILTER_ALPHANUMERIC) && !wxIsAlphaNumeric(val))
165 {
166 char buf[512];
167 sprintf(buf, "%s should only contain alphabetic or numeric characters.", (const char *)val);
168 wxMessageBox(buf, "Validation conflict", wxOK | wxICON_EXCLAMATION, parent);
169 return FALSE;
170 }
171 if ( (m_validatorStyle & wxFILTER_NUMERIC) && !val.IsNumber())
172 {
173 char buf[512];
174 sprintf(buf, "%s should be numeric.", (const char *)val);
175 wxMessageBox(buf, "Validation conflict", wxOK | wxICON_EXCLAMATION, parent);
176 return FALSE;
177 }
178
179 return TRUE ;
180 }
181
182 // Called to transfer data to the window
183 bool wxTextValidator::TransferToWindow(void)
184 {
185 if ( !m_validatorWindow )
186 return FALSE;
187 if ( !m_validatorWindow->IsKindOf(CLASSINFO(wxTextCtrl)) )
188 return FALSE;
189 if ( !m_stringValue )
190 return FALSE;
191
192 wxTextCtrl *control = (wxTextCtrl *) m_validatorWindow ;
193 control->SetValue(* m_stringValue) ;
194
195 return TRUE;
196 }
197
198 // Called to transfer data to the window
199 bool wxTextValidator::TransferFromWindow(void)
200 {
201 if ( !m_validatorWindow )
202 return FALSE;
203 if ( !m_validatorWindow->IsKindOf(CLASSINFO(wxTextCtrl)) )
204 return FALSE;
205 if ( !m_stringValue )
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 char *s = (char *)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 char *s = (char *)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 return;
259
260 wxTextCtrl *textCtrl = (wxTextCtrl *)m_validatorWindow;
261
262 int keyCode = event.KeyCode();
263 if ( keyCode == WXK_DELETE || keyCode == WXK_RETURN || keyCode == WXK_BACK)
264 {
265 textCtrl->wxTextCtrl::OnChar(event);
266 return ;
267 }
268
269 if ( (m_validatorStyle & wxFILTER_ASCII) && !isascii(keyCode) )
270 {
271 wxBell();
272 return;
273 }
274 if ( (m_validatorStyle & wxFILTER_ALPHA) && !isalpha(keyCode) )
275 {
276 wxBell();
277 return;
278 }
279 if ( (m_validatorStyle & wxFILTER_ALPHANUMERIC) && !isalnum(keyCode) )
280 {
281 wxBell();
282 return;
283 }
284 if ( (m_validatorStyle & wxFILTER_NUMERIC) && !isdigit(keyCode) && keyCode != '.' )
285 {
286 wxBell();
287 return;
288 }
289
290 textCtrl->wxTextCtrl::OnChar(event);
291 }
292
293