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