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