]> git.saurik.com Git - wxWidgets.git/blame - src/common/valtext.cpp
fopen -> wxFopen and such
[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
43#if !USE_SHARED_LIBRARY
44IMPLEMENT_DYNAMIC_CLASS(wxTextValidator, wxValidator)
45
46BEGIN_EVENT_TABLE(wxTextValidator, wxValidator)
a994f81b 47 EVT_CHAR(wxTextValidator::OnChar)
c801d85f
KB
48END_EVENT_TABLE()
49#endif
50
386af6a2
JS
51static bool wxIsNumeric(const wxString& val);
52
debe6624 53wxTextValidator::wxTextValidator(long style, wxString *val)
c801d85f 54{
a994f81b
VZ
55 m_validatorStyle = style ;
56 m_stringValue = val ;
c801d85f
KB
57/*
58 m_refData = new wxVTextRefData;
59
a994f81b
VZ
60 M_VTEXTDATA->m_validatorStyle = style ;
61 M_VTEXTDATA->m_stringValue = val ;
c801d85f
KB
62*/
63}
64
65wxTextValidator::wxTextValidator(const wxTextValidator& val)
66{
67 Copy(val);
68}
69
70bool wxTextValidator::Copy(const wxTextValidator& val)
71{
72 wxValidator::Copy(val);
73
a994f81b
VZ
74 m_validatorStyle = val.m_validatorStyle ;
75 m_stringValue = val.m_stringValue ;
76
77 wxNode *node = val.m_includeList.First() ;
78 while ( node )
79 {
783b6cfd 80 wxChar *s = (wxChar *)node->Data();
a994f81b
VZ
81 m_includeList.Add(s);
82 node = node->Next();
83 }
84 node = val.m_excludeList.First() ;
85 while ( node )
86 {
783b6cfd 87 wxChar *s = (wxChar *)node->Data();
a994f81b
VZ
88 m_excludeList.Add(s);
89 node = node->Next();
90 }
c801d85f
KB
91 return TRUE;
92}
93
94wxTextValidator::~wxTextValidator()
95{
96}
97
98static bool wxIsAlpha(const wxString& val)
99{
a994f81b
VZ
100 int i;
101 for ( i = 0; i < (int)val.Length(); i++)
102 {
103 if (!isalpha(val[i]))
104 return FALSE;
105 }
106 return TRUE;
c801d85f
KB
107}
108
109static bool wxIsAlphaNumeric(const wxString& val)
110{
a994f81b
VZ
111 int i;
112 for ( i = 0; i < (int)val.Length(); i++)
113 {
114 if (!isalnum(val[i]))
115 return FALSE;
116 }
117 return TRUE;
c801d85f
KB
118}
119
120// Called when the value in the window must be validated.
121// This function can pop up an error message.
122bool wxTextValidator::Validate(wxWindow *parent)
123{
a994f81b
VZ
124 if ( !m_validatorWindow )
125 return FALSE;
126 if ( !m_validatorWindow->IsKindOf(CLASSINFO(wxTextCtrl)) )
127 return FALSE;
128 if ( !m_stringValue )
129 return FALSE;
130
131 wxTextCtrl *control = (wxTextCtrl *) m_validatorWindow ;
132
6cd47507 133 // If window is disabled, simply return
f03fc89f 134 if ( !control->IsEnabled() )
6cd47507 135 return TRUE;
a994f81b
VZ
136
137 wxString val(control->GetValue());
138
2a47d3c1 139 bool ok = TRUE;
a994f81b
VZ
140
141 // this format string should contian exactly one '%s'
783b6cfd 142 const wxChar *errormsg = _("'%s' is invalid");
a994f81b
VZ
143
144 if ( m_validatorStyle & wxFILTER_INCLUDE_LIST )
145 {
146 if ( !m_includeList.Member(val) )
147 {
2a47d3c1 148 ok = FALSE;
a994f81b
VZ
149 }
150 }
151 else if ( m_validatorStyle & wxFILTER_EXCLUDE_LIST )
152 {
153 if ( m_excludeList.Member(val) )
154 {
2a47d3c1 155 ok = FALSE;
a994f81b
VZ
156 }
157 }
158 else if ( (m_validatorStyle & wxFILTER_ASCII) && !val.IsAscii() )
159 {
2a47d3c1 160 ok = FALSE;
a994f81b
VZ
161
162 errormsg = _("'%s' should only contain ASCII characters.");
163 }
164 else if ( (m_validatorStyle & wxFILTER_ALPHA) && !wxIsAlpha(val) )
165 {
2a47d3c1 166 ok = FALSE;
a994f81b
VZ
167
168 errormsg = _("'%s' should only contain alphabetic characters.");
169 }
170 else if ( (m_validatorStyle & wxFILTER_ALPHANUMERIC) && !wxIsAlphaNumeric(val))
171 {
2a47d3c1 172 ok = FALSE;
a994f81b
VZ
173
174 errormsg = _("'%s' should only contain alphabetic or numeric characters.");
175 }
176 else if ( (m_validatorStyle & wxFILTER_NUMERIC) && !wxIsNumeric(val))
177 {
2a47d3c1 178 ok = FALSE;
a994f81b
VZ
179
180 errormsg = _("'%s' should be numeric.");
181 }
182
183 if ( !ok )
184 {
185 m_validatorWindow->SetFocus();
186
187 wxString buf;
188 buf.Printf(errormsg, val.c_str());
189
190 wxMessageBox(buf, _("Validation conflict"),
191 wxOK | wxICON_EXCLAMATION, parent);
192 }
193
194 return ok;
c801d85f
KB
195}
196
197// Called to transfer data to the window
198bool wxTextValidator::TransferToWindow(void)
199{
a994f81b
VZ
200 if ( !m_validatorWindow )
201 return FALSE;
202 if ( !m_validatorWindow->IsKindOf(CLASSINFO(wxTextCtrl)) )
203 return FALSE;
204 if ( !m_stringValue )
205 return FALSE;
c801d85f 206
a994f81b
VZ
207 wxTextCtrl *control = (wxTextCtrl *) m_validatorWindow ;
208 control->SetValue(* m_stringValue) ;
c801d85f 209
a994f81b 210 return TRUE;
c801d85f
KB
211}
212
213// Called to transfer data to the window
214bool wxTextValidator::TransferFromWindow(void)
215{
a994f81b
VZ
216 if ( !m_validatorWindow )
217 return FALSE;
218 if ( !m_validatorWindow->IsKindOf(CLASSINFO(wxTextCtrl)) )
219 return FALSE;
220 if ( !m_stringValue )
221 return FALSE;
c801d85f 222
a994f81b
VZ
223 wxTextCtrl *control = (wxTextCtrl *) m_validatorWindow ;
224 * m_stringValue = control->GetValue() ;
c801d85f 225
a994f81b 226 return TRUE;
c801d85f
KB
227}
228
229void wxTextValidator::SetIncludeList(const wxStringList& list)
230{
231/*
a994f81b
VZ
232 if ( !M_VTEXTDATA )
233 return;
c801d85f
KB
234*/
235
a994f81b
VZ
236 m_includeList.Clear();
237 // TODO: replace with =
238 wxNode *node = list.First() ;
239 while ( node )
240 {
783b6cfd 241 wxChar *s = (wxChar *)node->Data();
a994f81b
VZ
242 m_includeList.Add(s);
243 node = node->Next();
244 }
c801d85f
KB
245}
246
247void wxTextValidator::SetExcludeList(const wxStringList& list)
248{
249/*
a994f81b
VZ
250 if ( !M_VTEXTDATA )
251 return;
c801d85f
KB
252*/
253
a994f81b
VZ
254 m_excludeList.Clear();
255 // TODO: replace with =
256 wxNode *node = list.First() ;
257 while ( node )
258 {
783b6cfd 259 wxChar *s = (wxChar *)node->Data();
a994f81b
VZ
260 m_excludeList.Add(s);
261 node = node->Next();
262 }
c801d85f
KB
263}
264
265void wxTextValidator::OnChar(wxKeyEvent& event)
266{
267/*
a994f81b
VZ
268 if ( !M_VTEXTDATA )
269 return;
c801d85f
KB
270*/
271
a994f81b
VZ
272 if ( m_validatorWindow )
273 {
274 int keyCode = event.KeyCode();
275
276 // we don't filter special keys and Delete
277 if (
278 !(keyCode < WXK_SPACE || keyCode == WXK_DELETE || keyCode > WXK_START) &&
279 (
280 ((m_validatorStyle & wxFILTER_ASCII) && !isascii(keyCode)) ||
281 ((m_validatorStyle & wxFILTER_ALPHA) && !isalpha(keyCode)) ||
282 ((m_validatorStyle & wxFILTER_ALPHANUMERIC) && !isalnum(keyCode)) ||
283 ((m_validatorStyle & wxFILTER_NUMERIC) && !isdigit(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
ce4169a4
RR
313#endif
314 // wxUSE_VALIDATORS