]> git.saurik.com Git - wxWidgets.git/blob - src/common/valtext.cpp
ugly fix for warnings when wxUSE_STL==0 not breaking compilation when wxUSE_STL==1
[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 #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 #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 #ifdef __WXWINCE__
233 inline int isascii(wxChar c) { return (c >= 0) && (c <=127); }
234 #endif
235
236 void wxTextValidator::OnChar(wxKeyEvent& event)
237 {
238 /*
239 if ( !M_VTEXTDATA )
240 return;
241 */
242
243 if ( m_validatorWindow )
244 {
245 int keyCode = event.GetKeyCode();
246
247 // we don't filter special keys and Delete
248 if (
249 !(keyCode < WXK_SPACE || keyCode == WXK_DELETE || keyCode > WXK_START) &&
250 (
251 ((m_validatorStyle & wxFILTER_INCLUDE_CHAR_LIST) && !IsInCharIncludeList(wxString((char) keyCode, 1))) ||
252 ((m_validatorStyle & wxFILTER_EXCLUDE_CHAR_LIST) && !IsNotInCharExcludeList(wxString((char) keyCode, 1))) ||
253 ((m_validatorStyle & wxFILTER_ASCII) && !isascii(keyCode)) ||
254 ((m_validatorStyle & wxFILTER_ALPHA) && !wxIsalpha(keyCode)) ||
255 ((m_validatorStyle & wxFILTER_ALPHANUMERIC) && !wxIsalnum(keyCode)) ||
256 ((m_validatorStyle & wxFILTER_NUMERIC) && !wxIsdigit(keyCode)
257 && keyCode != '.' && keyCode != ',' && keyCode != '-')
258 )
259 )
260 {
261 if ( !wxValidator::IsSilent() )
262 wxBell();
263
264 // eat message
265 return;
266 }
267 }
268
269 event.Skip();
270 }
271
272 static bool wxIsNumeric(const wxString& val)
273 {
274 int i;
275 for ( i = 0; i < (int)val.Length(); i++)
276 {
277 // Allow for "," (French) as well as "." -- in future we should
278 // use wxSystemSettings or other to do better localisation
279 if ((!wxIsdigit(val[i])) && (val[i] != '.') && (val[i] != ',') && (val[i] != wxT('e')) && (val[i] != wxT('E')) && (val[i] != wxT('+')) && (val[i] != wxT('-')))
280 return FALSE;
281 }
282 return TRUE;
283 }
284
285 bool wxTextValidator::IsInCharIncludeList(const wxString& val)
286 {
287 size_t i;
288 for ( i = 0; i < val.Length(); i++)
289 {
290 if (!m_includeList.Member((wxString) val[i]))
291 return FALSE;
292 }
293 return TRUE;
294 }
295
296 bool wxTextValidator::IsNotInCharExcludeList(const wxString& val)
297 {
298 size_t i;
299 for ( i = 0; i < val.Length(); i++)
300 {
301 if (m_excludeList.Member((wxString) val[i]))
302 return FALSE;
303 }
304 return TRUE;
305 }
306
307 #endif
308 // wxUSE_VALIDATORS && wxUSE_TEXTCTRL