]> git.saurik.com Git - wxWidgets.git/blame - src/common/valtext.cpp
make string at least empty (instead of containing garbage) if malloc() failed
[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
b1d4dd7a 76 wxStringList::Node *node = val.m_includeList.GetFirst() ;
a994f81b
VZ
77 while ( node )
78 {
b1d4dd7a 79 wxChar *s = node->GetData();
a994f81b 80 m_includeList.Add(s);
b1d4dd7a 81 node = node->GetNext();
a994f81b 82 }
b1d4dd7a 83 node = val.m_excludeList.GetFirst() ;
a994f81b
VZ
84 while ( node )
85 {
b1d4dd7a 86 wxChar *s = node->GetData();
a994f81b 87 m_excludeList.Add(s);
b1d4dd7a 88 node = node->GetNext();
a994f81b 89 }
dc259b79 90 return TRUE;
c801d85f
KB
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]))
dc259b79 103 return FALSE;
a994f81b 104 }
dc259b79 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]))
dc259b79 114 return FALSE;
a994f81b 115 }
dc259b79 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() )
dc259b79 124 return FALSE;
a994f81b
VZ
125
126 wxTextCtrl *control = (wxTextCtrl *) m_validatorWindow ;
127
6cd47507 128 // If window is disabled, simply return
f03fc89f 129 if ( !control->IsEnabled() )
dc259b79 130 return TRUE;
a994f81b
VZ
131
132 wxString val(control->GetValue());
133
dc259b79 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 {
dc259b79 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 {
dc259b79 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 {
dc259b79 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 {
dc259b79 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");
dc259b79 178 ok = FALSE;
aaae8296
JS
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");
dc259b79 184 ok = FALSE;
aaae8296 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() )
dc259b79 207 return FALSE;
c801d85f 208
7f03dc28
JS
209 if (!m_stringValue)
210 return TRUE;
211
a994f81b
VZ
212 wxTextCtrl *control = (wxTextCtrl *) m_validatorWindow ;
213 control->SetValue(* m_stringValue) ;
c801d85f 214
dc259b79 215 return TRUE;
c801d85f
KB
216}
217
218// Called to transfer data to the window
219bool wxTextValidator::TransferFromWindow(void)
220{
33c5b54b 221 if( !CheckValidator() )
dc259b79 222 return FALSE;
c801d85f 223
7f03dc28
JS
224 if (!m_stringValue)
225 return TRUE;
226
a994f81b
VZ
227 wxTextCtrl *control = (wxTextCtrl *) m_validatorWindow ;
228 * m_stringValue = control->GetValue() ;
c801d85f 229
dc259b79 230 return TRUE;
c801d85f
KB
231}
232
233void wxTextValidator::SetIncludeList(const wxStringList& list)
234{
235/*
a994f81b
VZ
236 if ( !M_VTEXTDATA )
237 return;
c801d85f
KB
238*/
239
a994f81b
VZ
240 m_includeList.Clear();
241 // TODO: replace with =
b1d4dd7a 242 wxStringList::Node *node = list.GetFirst();
a994f81b
VZ
243 while ( node )
244 {
b1d4dd7a 245 wxChar *s = node->GetData();
a994f81b 246 m_includeList.Add(s);
b1d4dd7a 247 node = node->GetNext();
a994f81b 248 }
c801d85f
KB
249}
250
251void wxTextValidator::SetExcludeList(const wxStringList& list)
252{
253/*
a994f81b
VZ
254 if ( !M_VTEXTDATA )
255 return;
c801d85f
KB
256*/
257
a994f81b
VZ
258 m_excludeList.Clear();
259 // TODO: replace with =
b1d4dd7a 260 wxStringList::Node *node = list.GetFirst() ;
a994f81b
VZ
261 while ( node )
262 {
b1d4dd7a 263 wxChar *s = node->GetData();
a994f81b 264 m_excludeList.Add(s);
b1d4dd7a 265 node = node->GetNext();
a994f81b 266 }
c801d85f
KB
267}
268
269void wxTextValidator::OnChar(wxKeyEvent& event)
270{
271/*
a994f81b
VZ
272 if ( !M_VTEXTDATA )
273 return;
c801d85f
KB
274*/
275
a994f81b
VZ
276 if ( m_validatorWindow )
277 {
12a3f227 278 int keyCode = event.GetKeyCode();
a994f81b
VZ
279
280 // we don't filter special keys and Delete
281 if (
282 !(keyCode < WXK_SPACE || keyCode == WXK_DELETE || keyCode > WXK_START) &&
283 (
b1d4dd7a 284 ((m_validatorStyle & wxFILTER_INCLUDE_CHAR_LIST) && !IsInCharIncludeList(wxString((char) keyCode, 1))) ||
aaae8296 285 ((m_validatorStyle & wxFILTER_EXCLUDE_CHAR_LIST) && !IsNotInCharExcludeList(wxString((char) keyCode, 1))) ||
a994f81b 286 ((m_validatorStyle & wxFILTER_ASCII) && !isascii(keyCode)) ||
f6bcfd97
BP
287 ((m_validatorStyle & wxFILTER_ALPHA) && !wxIsalpha(keyCode)) ||
288 ((m_validatorStyle & wxFILTER_ALPHANUMERIC) && !wxIsalnum(keyCode)) ||
289 ((m_validatorStyle & wxFILTER_NUMERIC) && !wxIsdigit(keyCode)
543f08a6 290 && keyCode != '.' && keyCode != ',' && keyCode != '-')
a994f81b
VZ
291 )
292 )
293 {
294 if ( !wxValidator::IsSilent() )
295 wxBell();
296
297 // eat message
298 return;
299 }
300 }
301
302 event.Skip();
c801d85f
KB
303}
304
386af6a2
JS
305static bool wxIsNumeric(const wxString& val)
306{
a994f81b
VZ
307 int i;
308 for ( i = 0; i < (int)val.Length(); i++)
309 {
543f08a6
JS
310 // Allow for "," (French) as well as "." -- in future we should
311 // use wxSystemSettings or other to do better localisation
00013f54 312 if ((!isdigit(val[i])) && (val[i] != '.') && (val[i] != ',') && (val[i] != wxT('e')) && (val[i] != wxT('E')) && (val[i] != wxT('+')) && (val[i] != wxT('-')))
dc259b79 313 return FALSE;
a994f81b 314 }
dc259b79 315 return TRUE;
386af6a2 316}
c801d85f 317
aaae8296
JS
318bool wxTextValidator::IsInCharIncludeList(const wxString& val)
319{
320 size_t i;
321 for ( i = 0; i < val.Length(); i++)
322 {
323 if (!m_includeList.Member((wxString) val[i]))
dc259b79 324 return FALSE;
aaae8296 325 }
dc259b79 326 return TRUE;
aaae8296
JS
327}
328
329bool wxTextValidator::IsNotInCharExcludeList(const wxString& val)
330{
331 size_t i;
332 for ( i = 0; i < val.Length(); i++)
333 {
334 if (m_excludeList.Member((wxString) val[i]))
dc259b79 335 return FALSE;
aaae8296 336 }
dc259b79 337 return TRUE;
aaae8296
JS
338}
339
ce4169a4 340#endif
d7260478 341 // wxUSE_VALIDATORS && wxUSE_TEXTCTRL