]> git.saurik.com Git - wxWidgets.git/blob - src/common/valtext.cpp
cleanup image after application
[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
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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 && wxUSE_TEXTCTRL
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 : wxValidator()
65 {
66 Copy(val);
67 }
68
69 bool wxTextValidator::Copy(const wxTextValidator& val)
70 {
71 wxValidator::Copy(val);
72
73 m_validatorStyle = val.m_validatorStyle;
74 m_stringValue = val.m_stringValue;
75
76 m_includeList = val.m_includeList;
77 m_excludeList = val.m_excludeList;
78
79 return true;
80 }
81
82 wxTextValidator::~wxTextValidator()
83 {
84 }
85
86 static bool wxIsAlpha(const wxString& val)
87 {
88 int i;
89 for ( i = 0; i < (int)val.Length(); i++)
90 {
91 if (!wxIsalpha(val[i]))
92 return false;
93 }
94 return true;
95 }
96
97 static bool wxIsAlphaNumeric(const wxString& val)
98 {
99 int i;
100 for ( i = 0; i < (int)val.Length(); i++)
101 {
102 if (!wxIsalnum(val[i]))
103 return false;
104 }
105 return true;
106 }
107
108 // Called when the value in the window must be validated.
109 // This function can pop up an error message.
110 bool wxTextValidator::Validate(wxWindow *parent)
111 {
112 if( !CheckValidator() )
113 return false;
114
115 wxTextCtrl *control = (wxTextCtrl *) m_validatorWindow;
116
117 // If window is disabled, simply return
118 if ( !control->IsEnabled() )
119 return true;
120
121 wxString val(control->GetValue());
122
123 bool ok = true;
124
125 // NB: this format string should contian exactly one '%s'
126 wxString errormsg;
127
128 bool includeList = (m_validatorStyle & wxFILTER_INCLUDE_LIST) != 0;
129 if ( includeList || (m_validatorStyle & wxFILTER_EXCLUDE_LIST) )
130 {
131 // if includeList, it's only ok to have the members of the list,
132 // otherwise it's only ok to have non-members
133 ok = includeList == m_includeList.Member(val);
134 if ( !ok )
135 {
136 errormsg = _("'%s' is invalid");
137 }
138 }
139 else if ( (m_validatorStyle & wxFILTER_ASCII) && !val.IsAscii() )
140 {
141 ok = false;
142
143 errormsg = _("'%s' should only contain ASCII characters.");
144 }
145 else if ( (m_validatorStyle & wxFILTER_ALPHA) && !wxIsAlpha(val) )
146 {
147 ok = false;
148
149 errormsg = _("'%s' should only contain alphabetic characters.");
150 }
151 else if ( (m_validatorStyle & wxFILTER_ALPHANUMERIC) && !wxIsAlphaNumeric(val))
152 {
153 ok = false;
154
155 errormsg = _("'%s' should only contain alphabetic or numeric characters.");
156 }
157 else if ( (m_validatorStyle & wxFILTER_NUMERIC) && !wxIsNumeric(val))
158 {
159 ok = false;
160
161 errormsg = _("'%s' should be numeric.");
162 }
163 else if ( (m_validatorStyle & wxFILTER_INCLUDE_CHAR_LIST) && !IsInCharIncludeList(val))
164 {
165 //it's only ok to have the members of the list
166 errormsg = _("'%s' is invalid");
167 ok = false;
168 }
169 else if ( (m_validatorStyle & wxFILTER_EXCLUDE_CHAR_LIST) && !IsNotInCharExcludeList(val))
170 {
171 // it's only ok to have non-members of the list
172 errormsg = _("'%s' is invalid");
173 ok = false;
174 }
175
176 if ( !ok )
177 {
178 wxASSERT_MSG( !errormsg.empty(), _T("you forgot to set errormsg") );
179
180 m_validatorWindow->SetFocus();
181
182 wxString buf;
183 buf.Printf(errormsg, val.c_str());
184
185 wxMessageBox(buf, _("Validation conflict"),
186 wxOK | wxICON_EXCLAMATION, parent);
187 }
188
189 return ok;
190 }
191
192 // Called to transfer data to the window
193 bool wxTextValidator::TransferToWindow(void)
194 {
195 if( !CheckValidator() )
196 return false;
197
198 if ( m_stringValue )
199 {
200 wxTextCtrl *control = (wxTextCtrl *) m_validatorWindow;
201 control->SetValue(* m_stringValue);
202 }
203
204 return true;
205 }
206
207 // Called to transfer data to the window
208 bool wxTextValidator::TransferFromWindow(void)
209 {
210 if( !CheckValidator() )
211 return false;
212
213 if ( m_stringValue )
214 {
215 wxTextCtrl *control = (wxTextCtrl *) m_validatorWindow;
216 *m_stringValue = control->GetValue();
217 }
218
219 return true;
220 }
221
222 void wxTextValidator::SetIncludeList(const wxStringList& list)
223 {
224 m_includeList = list;
225 }
226
227 void wxTextValidator::SetExcludeList(const wxStringList& list)
228 {
229 m_excludeList = list;
230 }
231
232 void wxTextValidator::OnChar(wxKeyEvent& event)
233 {
234 /*
235 if ( !M_VTEXTDATA )
236 return;
237 */
238
239 if ( m_validatorWindow )
240 {
241 int keyCode = event.GetKeyCode();
242
243 // we don't filter special keys and Delete
244 if (
245 !(keyCode < WXK_SPACE || keyCode == WXK_DELETE || keyCode > WXK_START) &&
246 (
247 ((m_validatorStyle & wxFILTER_INCLUDE_CHAR_LIST) && !IsInCharIncludeList(wxString((wxChar) keyCode, 1))) ||
248 ((m_validatorStyle & wxFILTER_EXCLUDE_CHAR_LIST) && !IsNotInCharExcludeList(wxString((wxChar) keyCode, 1))) ||
249 ((m_validatorStyle & wxFILTER_ASCII) && !isascii(keyCode)) ||
250 ((m_validatorStyle & wxFILTER_ALPHA) && !wxIsalpha(keyCode)) ||
251 ((m_validatorStyle & wxFILTER_ALPHANUMERIC) && !wxIsalnum(keyCode)) ||
252 ((m_validatorStyle & wxFILTER_NUMERIC) && !wxIsdigit(keyCode)
253 && keyCode != '.' && keyCode != ',' && keyCode != '-')
254 )
255 )
256 {
257 if ( !wxValidator::IsSilent() )
258 wxBell();
259
260 // eat message
261 return;
262 }
263 }
264
265 event.Skip();
266 }
267
268 static bool wxIsNumeric(const wxString& val)
269 {
270 int i;
271 for ( i = 0; i < (int)val.Length(); i++)
272 {
273 // Allow for "," (French) as well as "." -- in future we should
274 // use wxSystemSettings or other to do better localisation
275 if ((!wxIsdigit(val[i])) && (val[i] != '.') && (val[i] != ',') && (val[i] != wxT('e')) && (val[i] != wxT('E')) && (val[i] != wxT('+')) && (val[i] != wxT('-')))
276 return false;
277 }
278 return true;
279 }
280
281 bool wxTextValidator::IsInCharIncludeList(const wxString& val)
282 {
283 size_t i;
284 for ( i = 0; i < val.Length(); i++)
285 {
286 if (!m_includeList.Member((wxString) val[i]))
287 return false;
288 }
289 return true;
290 }
291
292 bool wxTextValidator::IsNotInCharExcludeList(const wxString& val)
293 {
294 size_t i;
295 for ( i = 0; i < val.Length(); i++)
296 {
297 if (m_excludeList.Member((wxString) val[i]))
298 return false;
299 }
300 return true;
301 }
302
303 #endif
304 // wxUSE_VALIDATORS && wxUSE_TEXTCTRL