]> git.saurik.com Git - wxWidgets.git/blame - src/common/valtext.cpp
Further WinCE mods
[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$
55d99c7a
JS
8// Copyright: (c) Julian Smart
9// Licence: wxWindows licence
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
d7260478 23#if wxUSE_VALIDATORS && wxUSE_TEXTCTRL
ce4169a4
RR
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
222ed1d6
MB
76 m_includeList = val.m_includeList;
77 m_excludeList = val.m_excludeList;
78
dc259b79 79 return TRUE;
c801d85f
KB
80}
81
82wxTextValidator::~wxTextValidator()
83{
84}
85
86static bool wxIsAlpha(const wxString& val)
87{
a994f81b
VZ
88 int i;
89 for ( i = 0; i < (int)val.Length(); i++)
90 {
f6bcfd97 91 if (!wxIsalpha(val[i]))
dc259b79 92 return FALSE;
a994f81b 93 }
dc259b79 94 return TRUE;
c801d85f
KB
95}
96
97static bool wxIsAlphaNumeric(const wxString& val)
98{
a994f81b
VZ
99 int i;
100 for ( i = 0; i < (int)val.Length(); i++)
101 {
f6bcfd97 102 if (!wxIsalnum(val[i]))
dc259b79 103 return FALSE;
a994f81b 104 }
dc259b79 105 return TRUE;
c801d85f
KB
106}
107
108// Called when the value in the window must be validated.
109// This function can pop up an error message.
110bool wxTextValidator::Validate(wxWindow *parent)
111{
33c5b54b 112 if( !CheckValidator() )
dc259b79 113 return FALSE;
a994f81b
VZ
114
115 wxTextCtrl *control = (wxTextCtrl *) m_validatorWindow ;
116
6cd47507 117 // If window is disabled, simply return
f03fc89f 118 if ( !control->IsEnabled() )
dc259b79 119 return TRUE;
a994f81b
VZ
120
121 wxString val(control->GetValue());
122
dc259b79 123 bool ok = TRUE;
a994f81b 124
e9086757
VZ
125 // NB: this format string should contian exactly one '%s'
126 wxString errormsg;
a994f81b 127
e9086757
VZ
128 bool includeList = (m_validatorStyle & wxFILTER_INCLUDE_LIST) != 0;
129 if ( includeList || (m_validatorStyle & wxFILTER_EXCLUDE_LIST) )
a994f81b 130 {
e9086757
VZ
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 )
a994f81b 135 {
e9086757 136 errormsg = _("'%s' is invalid");
a994f81b
VZ
137 }
138 }
139 else if ( (m_validatorStyle & wxFILTER_ASCII) && !val.IsAscii() )
140 {
dc259b79 141 ok = FALSE;
a994f81b
VZ
142
143 errormsg = _("'%s' should only contain ASCII characters.");
144 }
145 else if ( (m_validatorStyle & wxFILTER_ALPHA) && !wxIsAlpha(val) )
146 {
dc259b79 147 ok = FALSE;
a994f81b
VZ
148
149 errormsg = _("'%s' should only contain alphabetic characters.");
150 }
151 else if ( (m_validatorStyle & wxFILTER_ALPHANUMERIC) && !wxIsAlphaNumeric(val))
152 {
dc259b79 153 ok = FALSE;
a994f81b
VZ
154
155 errormsg = _("'%s' should only contain alphabetic or numeric characters.");
156 }
157 else if ( (m_validatorStyle & wxFILTER_NUMERIC) && !wxIsNumeric(val))
158 {
dc259b79 159 ok = FALSE;
a994f81b
VZ
160
161 errormsg = _("'%s' should be numeric.");
162 }
aaae8296
JS
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");
dc259b79 167 ok = FALSE;
aaae8296
JS
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");
dc259b79 173 ok = FALSE;
aaae8296 174 }
a994f81b
VZ
175
176 if ( !ok )
177 {
e9086757
VZ
178 wxASSERT_MSG( !errormsg.empty(), _T("you forgot to set errormsg") );
179
a994f81b
VZ
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;
c801d85f
KB
190}
191
192// Called to transfer data to the window
193bool wxTextValidator::TransferToWindow(void)
194{
33c5b54b 195 if( !CheckValidator() )
dc259b79 196 return FALSE;
c801d85f 197
7f03dc28
JS
198 if (!m_stringValue)
199 return TRUE;
200
a994f81b
VZ
201 wxTextCtrl *control = (wxTextCtrl *) m_validatorWindow ;
202 control->SetValue(* m_stringValue) ;
c801d85f 203
dc259b79 204 return TRUE;
c801d85f
KB
205}
206
207// Called to transfer data to the window
208bool wxTextValidator::TransferFromWindow(void)
209{
33c5b54b 210 if( !CheckValidator() )
dc259b79 211 return FALSE;
c801d85f 212
7f03dc28
JS
213 if (!m_stringValue)
214 return TRUE;
215
a994f81b
VZ
216 wxTextCtrl *control = (wxTextCtrl *) m_validatorWindow ;
217 * m_stringValue = control->GetValue() ;
c801d85f 218
dc259b79 219 return TRUE;
c801d85f
KB
220}
221
222void wxTextValidator::SetIncludeList(const wxStringList& list)
223{
222ed1d6 224 m_includeList = list;
c801d85f
KB
225}
226
227void wxTextValidator::SetExcludeList(const wxStringList& list)
228{
222ed1d6 229 m_excludeList = list;
c801d85f
KB
230}
231
232void wxTextValidator::OnChar(wxKeyEvent& event)
233{
234/*
a994f81b
VZ
235 if ( !M_VTEXTDATA )
236 return;
c801d85f
KB
237*/
238
a994f81b
VZ
239 if ( m_validatorWindow )
240 {
12a3f227 241 int keyCode = event.GetKeyCode();
a994f81b
VZ
242
243 // we don't filter special keys and Delete
244 if (
245 !(keyCode < WXK_SPACE || keyCode == WXK_DELETE || keyCode > WXK_START) &&
246 (
b1d4dd7a 247 ((m_validatorStyle & wxFILTER_INCLUDE_CHAR_LIST) && !IsInCharIncludeList(wxString((char) keyCode, 1))) ||
aaae8296 248 ((m_validatorStyle & wxFILTER_EXCLUDE_CHAR_LIST) && !IsNotInCharExcludeList(wxString((char) keyCode, 1))) ||
a994f81b 249 ((m_validatorStyle & wxFILTER_ASCII) && !isascii(keyCode)) ||
f6bcfd97
BP
250 ((m_validatorStyle & wxFILTER_ALPHA) && !wxIsalpha(keyCode)) ||
251 ((m_validatorStyle & wxFILTER_ALPHANUMERIC) && !wxIsalnum(keyCode)) ||
252 ((m_validatorStyle & wxFILTER_NUMERIC) && !wxIsdigit(keyCode)
543f08a6 253 && keyCode != '.' && keyCode != ',' && keyCode != '-')
a994f81b
VZ
254 )
255 )
256 {
257 if ( !wxValidator::IsSilent() )
258 wxBell();
259
260 // eat message
261 return;
262 }
263 }
264
265 event.Skip();
c801d85f
KB
266}
267
386af6a2
JS
268static bool wxIsNumeric(const wxString& val)
269{
a994f81b
VZ
270 int i;
271 for ( i = 0; i < (int)val.Length(); i++)
272 {
543f08a6
JS
273 // Allow for "," (French) as well as "." -- in future we should
274 // use wxSystemSettings or other to do better localisation
00013f54 275 if ((!isdigit(val[i])) && (val[i] != '.') && (val[i] != ',') && (val[i] != wxT('e')) && (val[i] != wxT('E')) && (val[i] != wxT('+')) && (val[i] != wxT('-')))
dc259b79 276 return FALSE;
a994f81b 277 }
dc259b79 278 return TRUE;
386af6a2 279}
c801d85f 280
aaae8296
JS
281bool 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]))
dc259b79 287 return FALSE;
aaae8296 288 }
dc259b79 289 return TRUE;
aaae8296
JS
290}
291
292bool 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]))
dc259b79 298 return FALSE;
aaae8296 299 }
dc259b79 300 return TRUE;
aaae8296
JS
301}
302
ce4169a4 303#endif
d7260478 304 // wxUSE_VALIDATORS && wxUSE_TEXTCTRL