]> git.saurik.com Git - wxWidgets.git/blame - src/common/valtext.cpp
bump subrelease number
[wxWidgets.git] / src / common / valtext.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
264cb7f5 2// Name: src/common/valtext.cpp
c801d85f
KB
3// Purpose: wxTextValidator
4// Author: Julian Smart
5// Modified by:
6// Created: 04/01/98
7// RCS-ID: $Id$
55d99c7a 8// Copyright: (c) Julian Smart
65571936 9// Licence: wxWindows licence
c801d85f
KB
10/////////////////////////////////////////////////////////////////////////////
11
c801d85f
KB
12// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
15#ifdef __BORLANDC__
ce4169a4 16 #pragma hdrstop
c801d85f
KB
17#endif
18
d7260478 19#if wxUSE_VALIDATORS && wxUSE_TEXTCTRL
ce4169a4 20
264cb7f5
WS
21#include "wx/valtext.h"
22
ce4169a4
RR
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"
c801d85f
KB
29#endif
30
c801d85f
KB
31#include <ctype.h>
32#include <string.h>
33#include <stdlib.h>
34
ce3ed50d 35#ifdef __SALFORDC__
a994f81b 36 #include <clib.h>
ce3ed50d
JS
37#endif
38
c801d85f
KB
39IMPLEMENT_DYNAMIC_CLASS(wxTextValidator, wxValidator)
40
41BEGIN_EVENT_TABLE(wxTextValidator, wxValidator)
a994f81b 42 EVT_CHAR(wxTextValidator::OnChar)
c801d85f 43END_EVENT_TABLE()
c801d85f 44
386af6a2
JS
45static bool wxIsNumeric(const wxString& val);
46
debe6624 47wxTextValidator::wxTextValidator(long style, wxString *val)
c801d85f 48{
52cd14b1
VZ
49 m_validatorStyle = style;
50 m_stringValue = val;
c801d85f
KB
51/*
52 m_refData = new wxVTextRefData;
53
52cd14b1
VZ
54 M_VTEXTDATA->m_validatorStyle = style;
55 M_VTEXTDATA->m_stringValue = val;
c801d85f
KB
56*/
57}
58
59wxTextValidator::wxTextValidator(const wxTextValidator& val)
d84afea9 60 : wxValidator()
c801d85f
KB
61{
62 Copy(val);
63}
64
65bool wxTextValidator::Copy(const wxTextValidator& val)
66{
67 wxValidator::Copy(val);
68
52cd14b1
VZ
69 m_validatorStyle = val.m_validatorStyle;
70 m_stringValue = val.m_stringValue;
a994f81b 71
f94a790d
RN
72 m_includes = val.m_includes;
73 m_excludes = val.m_excludes;
52cd14b1 74
cab1a605 75 return true;
c801d85f
KB
76}
77
c801d85f
KB
78static bool wxIsAlpha(const wxString& val)
79{
a994f81b 80 int i;
264cb7f5 81 for ( i = 0; i < (int)val.length(); i++)
a994f81b 82 {
f6bcfd97 83 if (!wxIsalpha(val[i]))
cab1a605 84 return false;
a994f81b 85 }
cab1a605 86 return true;
c801d85f
KB
87}
88
89static bool wxIsAlphaNumeric(const wxString& val)
90{
a994f81b 91 int i;
264cb7f5 92 for ( i = 0; i < (int)val.length(); i++)
a994f81b 93 {
f6bcfd97 94 if (!wxIsalnum(val[i]))
cab1a605 95 return false;
a994f81b 96 }
cab1a605 97 return true;
c801d85f
KB
98}
99
100// Called when the value in the window must be validated.
101// This function can pop up an error message.
102bool wxTextValidator::Validate(wxWindow *parent)
103{
33c5b54b 104 if( !CheckValidator() )
cab1a605 105 return false;
a994f81b 106
52cd14b1 107 wxTextCtrl *control = (wxTextCtrl *) m_validatorWindow;
a994f81b 108
6cd47507 109 // If window is disabled, simply return
f03fc89f 110 if ( !control->IsEnabled() )
cab1a605 111 return true;
a994f81b
VZ
112
113 wxString val(control->GetValue());
114
cab1a605 115 bool ok = true;
a994f81b 116
e9086757
VZ
117 // NB: this format string should contian exactly one '%s'
118 wxString errormsg;
a994f81b 119
f94a790d
RN
120 bool includes = (m_validatorStyle & wxFILTER_INCLUDE_LIST) != 0;
121 if ( includes || (m_validatorStyle & wxFILTER_EXCLUDE_LIST) )
a994f81b 122 {
f94a790d 123 // if includes, it's only ok to have the members of the list,
e9086757 124 // otherwise it's only ok to have non-members
f94a790d 125 ok = includes == (m_includes.Index(val) != wxNOT_FOUND);
e9086757 126 if ( !ok )
a994f81b 127 {
e9086757 128 errormsg = _("'%s' is invalid");
a994f81b
VZ
129 }
130 }
131 else if ( (m_validatorStyle & wxFILTER_ASCII) && !val.IsAscii() )
132 {
cab1a605 133 ok = false;
a994f81b
VZ
134
135 errormsg = _("'%s' should only contain ASCII characters.");
136 }
137 else if ( (m_validatorStyle & wxFILTER_ALPHA) && !wxIsAlpha(val) )
138 {
cab1a605 139 ok = false;
a994f81b
VZ
140
141 errormsg = _("'%s' should only contain alphabetic characters.");
142 }
143 else if ( (m_validatorStyle & wxFILTER_ALPHANUMERIC) && !wxIsAlphaNumeric(val))
144 {
cab1a605 145 ok = false;
a994f81b
VZ
146
147 errormsg = _("'%s' should only contain alphabetic or numeric characters.");
148 }
149 else if ( (m_validatorStyle & wxFILTER_NUMERIC) && !wxIsNumeric(val))
150 {
cab1a605 151 ok = false;
a994f81b
VZ
152
153 errormsg = _("'%s' should be numeric.");
154 }
f94a790d 155 else if ( (m_validatorStyle & wxFILTER_INCLUDE_CHAR_LIST) && !IsInCharIncludes(val))
aaae8296
JS
156 {
157 //it's only ok to have the members of the list
158 errormsg = _("'%s' is invalid");
cab1a605 159 ok = false;
aaae8296 160 }
f94a790d 161 else if ( (m_validatorStyle & wxFILTER_EXCLUDE_CHAR_LIST) && !IsNotInCharExcludes(val))
aaae8296
JS
162 {
163 // it's only ok to have non-members of the list
164 errormsg = _("'%s' is invalid");
cab1a605 165 ok = false;
aaae8296 166 }
a994f81b
VZ
167
168 if ( !ok )
169 {
e9086757
VZ
170 wxASSERT_MSG( !errormsg.empty(), _T("you forgot to set errormsg") );
171
a994f81b
VZ
172 m_validatorWindow->SetFocus();
173
174 wxString buf;
175 buf.Printf(errormsg, val.c_str());
176
177 wxMessageBox(buf, _("Validation conflict"),
178 wxOK | wxICON_EXCLAMATION, parent);
179 }
180
181 return ok;
c801d85f
KB
182}
183
184// Called to transfer data to the window
185bool wxTextValidator::TransferToWindow(void)
186{
33c5b54b 187 if( !CheckValidator() )
cab1a605 188 return false;
c801d85f 189
52cd14b1
VZ
190 if ( m_stringValue )
191 {
192 wxTextCtrl *control = (wxTextCtrl *) m_validatorWindow;
193 control->SetValue(* m_stringValue);
194 }
c801d85f 195
cab1a605 196 return true;
c801d85f
KB
197}
198
199// Called to transfer data to the window
200bool wxTextValidator::TransferFromWindow(void)
201{
33c5b54b 202 if( !CheckValidator() )
cab1a605 203 return false;
c801d85f 204
52cd14b1
VZ
205 if ( m_stringValue )
206 {
207 wxTextCtrl *control = (wxTextCtrl *) m_validatorWindow;
208 *m_stringValue = control->GetValue();
209 }
c801d85f 210
cab1a605 211 return true;
c801d85f
KB
212}
213
f94a790d
RN
214#if WXWIN_COMPATIBILITY_2_4
215
216inline void wxCopyStringListToArrayString(wxArrayString& to, const wxStringList& from)
217{
218 to.Clear();
219
80676593
VZ
220 for ( wxStringList::compatibility_iterator pNode = from.GetFirst();
221 pNode;
222 pNode = pNode->GetNext() )
223 {
f94a790d 224 to.Add(pNode->GetData());
80676593 225 }
f94a790d
RN
226}
227
228inline void wxCopyArrayStringToStringList(wxStringList& to, const wxArrayString& from)
229{
230 to.Clear();
231
232 for(size_t i = 0; i < from.GetCount(); ++i)
233 to.Add(from[i]);
234}
235
236wxStringList& wxTextValidator::GetIncludeList()
237{
238 wxCopyArrayStringToStringList(m_includeList, m_includes);
239 return m_includeList;
240}
241
242wxStringList& wxTextValidator::GetExcludeList()
243{
244 wxCopyArrayStringToStringList(m_excludeList, m_excludes);
245 return m_excludeList;
246}
247
c801d85f
KB
248void wxTextValidator::SetIncludeList(const wxStringList& list)
249{
f94a790d 250 wxCopyStringListToArrayString(m_includes, list);
c801d85f
KB
251}
252
253void wxTextValidator::SetExcludeList(const wxStringList& list)
254{
f94a790d
RN
255 wxCopyStringListToArrayString(m_excludes, list);
256}
257
258bool wxTextValidator::IsInCharIncludeList(const wxString& val)
259{
260 return IsInCharIncludes(val);
261}
262
263bool wxTextValidator::IsNotInCharExcludeList(const wxString& val)
264{
265 return IsNotInCharExcludes(val);
266}
267
268#endif //compat 2.4
269
270
271bool wxTextValidator::IsInCharIncludes(const wxString& val)
272{
273 size_t i;
264cb7f5 274 for ( i = 0; i < val.length(); i++)
f94a790d
RN
275 {
276 if (m_includes.Index((wxString) val[i]) == wxNOT_FOUND)
277 return false;
278 }
279 return true;
280}
281
282bool wxTextValidator::IsNotInCharExcludes(const wxString& val)
283{
284 size_t i;
264cb7f5 285 for ( i = 0; i < val.length(); i++)
f94a790d
RN
286 {
287 if (m_excludes.Index((wxString) val[i]) != wxNOT_FOUND)
288 return false;
289 }
290 return true;
c801d85f
KB
291}
292
293void wxTextValidator::OnChar(wxKeyEvent& event)
294{
295/*
a994f81b
VZ
296 if ( !M_VTEXTDATA )
297 return;
c801d85f
KB
298*/
299
a994f81b
VZ
300 if ( m_validatorWindow )
301 {
12a3f227 302 int keyCode = event.GetKeyCode();
a994f81b
VZ
303
304 // we don't filter special keys and Delete
305 if (
306 !(keyCode < WXK_SPACE || keyCode == WXK_DELETE || keyCode > WXK_START) &&
307 (
f94a790d
RN
308 ((m_validatorStyle & wxFILTER_INCLUDE_CHAR_LIST) && !IsInCharIncludes(wxString((wxChar) keyCode, 1))) ||
309 ((m_validatorStyle & wxFILTER_EXCLUDE_CHAR_LIST) && !IsNotInCharExcludes(wxString((wxChar) keyCode, 1))) ||
b1e4e7cc 310 ((m_validatorStyle & wxFILTER_ASCII) && !isascii(keyCode)) ||
0c6099b7
RN
311 ((m_validatorStyle & wxFILTER_ALPHA) && !wxIsalpha(keyCode)) ||
312 ((m_validatorStyle & wxFILTER_ALPHANUMERIC) && !wxIsalnum(keyCode)) ||
313 ((m_validatorStyle & wxFILTER_NUMERIC) && !wxIsdigit(keyCode)
14d77693 314 && keyCode != wxT('.') && keyCode != wxT(',') && keyCode != wxT('-') && keyCode != wxT('+') && keyCode != wxT('e') && keyCode != wxT('E'))
a994f81b
VZ
315 )
316 )
317 {
318 if ( !wxValidator::IsSilent() )
319 wxBell();
320
321 // eat message
322 return;
323 }
324 }
325
326 event.Skip();
c801d85f
KB
327}
328
386af6a2
JS
329static bool wxIsNumeric(const wxString& val)
330{
a994f81b 331 int i;
264cb7f5 332 for ( i = 0; i < (int)val.length(); i++)
a994f81b 333 {
543f08a6
JS
334 // Allow for "," (French) as well as "." -- in future we should
335 // use wxSystemSettings or other to do better localisation
f7f00d8a 336 if ((!wxIsdigit(val[i])) && (val[i] != wxT('.')) && (val[i] != wxT(',')) && (val[i] != wxT('e')) && (val[i] != wxT('E')) && (val[i] != wxT('+')) && (val[i] != wxT('-')))
cab1a605 337 return false;
a994f81b 338 }
cab1a605 339 return true;
386af6a2 340}
c801d85f 341
aaae8296 342
ce4169a4 343#endif
d7260478 344 // wxUSE_VALIDATORS && wxUSE_TEXTCTRL