]> git.saurik.com Git - wxWidgets.git/blame - src/common/valtext.cpp
Rework the wxCusor ctor taking wx stock number to provide as many cursors as
[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
214bool wxTextValidator::IsInCharIncludes(const wxString& val)
215{
216 size_t i;
264cb7f5 217 for ( i = 0; i < val.length(); i++)
f94a790d
RN
218 {
219 if (m_includes.Index((wxString) val[i]) == wxNOT_FOUND)
220 return false;
221 }
222 return true;
223}
224
225bool wxTextValidator::IsNotInCharExcludes(const wxString& val)
226{
227 size_t i;
264cb7f5 228 for ( i = 0; i < val.length(); i++)
f94a790d
RN
229 {
230 if (m_excludes.Index((wxString) val[i]) != wxNOT_FOUND)
231 return false;
232 }
233 return true;
c801d85f
KB
234}
235
236void wxTextValidator::OnChar(wxKeyEvent& event)
237{
238/*
a994f81b
VZ
239 if ( !M_VTEXTDATA )
240 return;
c801d85f
KB
241*/
242
a994f81b
VZ
243 if ( m_validatorWindow )
244 {
12a3f227 245 int keyCode = event.GetKeyCode();
a994f81b
VZ
246
247 // we don't filter special keys and Delete
248 if (
249 !(keyCode < WXK_SPACE || keyCode == WXK_DELETE || keyCode > WXK_START) &&
250 (
f94a790d
RN
251 ((m_validatorStyle & wxFILTER_INCLUDE_CHAR_LIST) && !IsInCharIncludes(wxString((wxChar) keyCode, 1))) ||
252 ((m_validatorStyle & wxFILTER_EXCLUDE_CHAR_LIST) && !IsNotInCharExcludes(wxString((wxChar) keyCode, 1))) ||
b1e4e7cc 253 ((m_validatorStyle & wxFILTER_ASCII) && !isascii(keyCode)) ||
0c6099b7
RN
254 ((m_validatorStyle & wxFILTER_ALPHA) && !wxIsalpha(keyCode)) ||
255 ((m_validatorStyle & wxFILTER_ALPHANUMERIC) && !wxIsalnum(keyCode)) ||
256 ((m_validatorStyle & wxFILTER_NUMERIC) && !wxIsdigit(keyCode)
14d77693 257 && keyCode != wxT('.') && keyCode != wxT(',') && keyCode != wxT('-') && keyCode != wxT('+') && keyCode != wxT('e') && keyCode != wxT('E'))
a994f81b
VZ
258 )
259 )
260 {
261 if ( !wxValidator::IsSilent() )
262 wxBell();
263
264 // eat message
265 return;
266 }
267 }
268
269 event.Skip();
c801d85f
KB
270}
271
386af6a2
JS
272static bool wxIsNumeric(const wxString& val)
273{
a994f81b 274 int i;
264cb7f5 275 for ( i = 0; i < (int)val.length(); i++)
a994f81b 276 {
543f08a6
JS
277 // Allow for "," (French) as well as "." -- in future we should
278 // use wxSystemSettings or other to do better localisation
f7f00d8a 279 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 280 return false;
a994f81b 281 }
cab1a605 282 return true;
386af6a2 283}
c801d85f 284
aaae8296 285
ce4169a4 286#endif
d7260478 287 // wxUSE_VALIDATORS && wxUSE_TEXTCTRL