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