]> git.saurik.com Git - wxWidgets.git/blame - src/common/valtext.cpp
wxStdButtonSizer Finalise --> Realize
[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 8// Copyright: (c) Julian Smart
65571936 9// Licence: wxWindows licence
c801d85f
KB
10/////////////////////////////////////////////////////////////////////////////
11
14f355c2 12#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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{
52cd14b1
VZ
53 m_validatorStyle = style;
54 m_stringValue = val;
c801d85f
KB
55/*
56 m_refData = new wxVTextRefData;
57
52cd14b1
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
52cd14b1
VZ
73 m_validatorStyle = val.m_validatorStyle;
74 m_stringValue = val.m_stringValue;
a994f81b 75
f94a790d
RN
76 m_includes = val.m_includes;
77 m_excludes = val.m_excludes;
52cd14b1 78
cab1a605 79 return true;
c801d85f
KB
80}
81
c801d85f
KB
82static bool wxIsAlpha(const wxString& val)
83{
a994f81b
VZ
84 int i;
85 for ( i = 0; i < (int)val.Length(); i++)
86 {
f6bcfd97 87 if (!wxIsalpha(val[i]))
cab1a605 88 return false;
a994f81b 89 }
cab1a605 90 return true;
c801d85f
KB
91}
92
93static bool wxIsAlphaNumeric(const wxString& val)
94{
a994f81b
VZ
95 int i;
96 for ( i = 0; i < (int)val.Length(); i++)
97 {
f6bcfd97 98 if (!wxIsalnum(val[i]))
cab1a605 99 return false;
a994f81b 100 }
cab1a605 101 return true;
c801d85f
KB
102}
103
104// Called when the value in the window must be validated.
105// This function can pop up an error message.
106bool wxTextValidator::Validate(wxWindow *parent)
107{
33c5b54b 108 if( !CheckValidator() )
cab1a605 109 return false;
a994f81b 110
52cd14b1 111 wxTextCtrl *control = (wxTextCtrl *) m_validatorWindow;
a994f81b 112
6cd47507 113 // If window is disabled, simply return
f03fc89f 114 if ( !control->IsEnabled() )
cab1a605 115 return true;
a994f81b
VZ
116
117 wxString val(control->GetValue());
118
cab1a605 119 bool ok = true;
a994f81b 120
e9086757
VZ
121 // NB: this format string should contian exactly one '%s'
122 wxString errormsg;
a994f81b 123
f94a790d
RN
124 bool includes = (m_validatorStyle & wxFILTER_INCLUDE_LIST) != 0;
125 if ( includes || (m_validatorStyle & wxFILTER_EXCLUDE_LIST) )
a994f81b 126 {
f94a790d 127 // if includes, it's only ok to have the members of the list,
e9086757 128 // otherwise it's only ok to have non-members
f94a790d 129 ok = includes == (m_includes.Index(val) != wxNOT_FOUND);
e9086757 130 if ( !ok )
a994f81b 131 {
e9086757 132 errormsg = _("'%s' is invalid");
a994f81b
VZ
133 }
134 }
135 else if ( (m_validatorStyle & wxFILTER_ASCII) && !val.IsAscii() )
136 {
cab1a605 137 ok = false;
a994f81b
VZ
138
139 errormsg = _("'%s' should only contain ASCII characters.");
140 }
141 else if ( (m_validatorStyle & wxFILTER_ALPHA) && !wxIsAlpha(val) )
142 {
cab1a605 143 ok = false;
a994f81b
VZ
144
145 errormsg = _("'%s' should only contain alphabetic characters.");
146 }
147 else if ( (m_validatorStyle & wxFILTER_ALPHANUMERIC) && !wxIsAlphaNumeric(val))
148 {
cab1a605 149 ok = false;
a994f81b
VZ
150
151 errormsg = _("'%s' should only contain alphabetic or numeric characters.");
152 }
153 else if ( (m_validatorStyle & wxFILTER_NUMERIC) && !wxIsNumeric(val))
154 {
cab1a605 155 ok = false;
a994f81b
VZ
156
157 errormsg = _("'%s' should be numeric.");
158 }
f94a790d 159 else if ( (m_validatorStyle & wxFILTER_INCLUDE_CHAR_LIST) && !IsInCharIncludes(val))
aaae8296
JS
160 {
161 //it's only ok to have the members of the list
162 errormsg = _("'%s' is invalid");
cab1a605 163 ok = false;
aaae8296 164 }
f94a790d 165 else if ( (m_validatorStyle & wxFILTER_EXCLUDE_CHAR_LIST) && !IsNotInCharExcludes(val))
aaae8296
JS
166 {
167 // it's only ok to have non-members of the list
168 errormsg = _("'%s' is invalid");
cab1a605 169 ok = false;
aaae8296 170 }
a994f81b
VZ
171
172 if ( !ok )
173 {
e9086757
VZ
174 wxASSERT_MSG( !errormsg.empty(), _T("you forgot to set errormsg") );
175
a994f81b
VZ
176 m_validatorWindow->SetFocus();
177
178 wxString buf;
179 buf.Printf(errormsg, val.c_str());
180
181 wxMessageBox(buf, _("Validation conflict"),
182 wxOK | wxICON_EXCLAMATION, parent);
183 }
184
185 return ok;
c801d85f
KB
186}
187
188// Called to transfer data to the window
189bool wxTextValidator::TransferToWindow(void)
190{
33c5b54b 191 if( !CheckValidator() )
cab1a605 192 return false;
c801d85f 193
52cd14b1
VZ
194 if ( m_stringValue )
195 {
196 wxTextCtrl *control = (wxTextCtrl *) m_validatorWindow;
197 control->SetValue(* m_stringValue);
198 }
c801d85f 199
cab1a605 200 return true;
c801d85f
KB
201}
202
203// Called to transfer data to the window
204bool wxTextValidator::TransferFromWindow(void)
205{
33c5b54b 206 if( !CheckValidator() )
cab1a605 207 return false;
c801d85f 208
52cd14b1
VZ
209 if ( m_stringValue )
210 {
211 wxTextCtrl *control = (wxTextCtrl *) m_validatorWindow;
212 *m_stringValue = control->GetValue();
213 }
c801d85f 214
cab1a605 215 return true;
c801d85f
KB
216}
217
f94a790d
RN
218#if WXWIN_COMPATIBILITY_2_4
219
220inline void wxCopyStringListToArrayString(wxArrayString& to, const wxStringList& from)
221{
222 to.Clear();
223
224 for(wxStringList::Node* pNode = from.GetFirst(); pNode; pNode = pNode->GetNext())
225 to.Add(pNode->GetData());
226}
227
228inline void wxCopyArrayStringToStringList(wxStringList& to, const wxArrayString& from)
229{
230 to.Clear();
231
232 for(size_t i = 0; i < from.GetCount(); ++i)
233 to.Add(from[i]);
234}
235
236wxStringList& wxTextValidator::GetIncludeList()
237{
238 wxCopyArrayStringToStringList(m_includeList, m_includes);
239 return m_includeList;
240}
241
242wxStringList& wxTextValidator::GetExcludeList()
243{
244 wxCopyArrayStringToStringList(m_excludeList, m_excludes);
245 return m_excludeList;
246}
247
c801d85f
KB
248void wxTextValidator::SetIncludeList(const wxStringList& list)
249{
f94a790d 250 wxCopyStringListToArrayString(m_includes, list);
c801d85f
KB
251}
252
253void wxTextValidator::SetExcludeList(const wxStringList& list)
254{
f94a790d
RN
255 wxCopyStringListToArrayString(m_excludes, list);
256}
257
258bool wxTextValidator::IsInCharIncludeList(const wxString& val)
259{
260 return IsInCharIncludes(val);
261}
262
263bool wxTextValidator::IsNotInCharExcludeList(const wxString& val)
264{
265 return IsNotInCharExcludes(val);
266}
267
268#endif //compat 2.4
269
270
271bool wxTextValidator::IsInCharIncludes(const wxString& val)
272{
273 size_t i;
274 for ( i = 0; i < val.Length(); i++)
275 {
276 if (m_includes.Index((wxString) val[i]) == wxNOT_FOUND)
277 return false;
278 }
279 return true;
280}
281
282bool wxTextValidator::IsNotInCharExcludes(const wxString& val)
283{
284 size_t i;
285 for ( i = 0; i < val.Length(); i++)
286 {
287 if (m_excludes.Index((wxString) val[i]) != wxNOT_FOUND)
288 return false;
289 }
290 return true;
c801d85f
KB
291}
292
293void wxTextValidator::OnChar(wxKeyEvent& event)
294{
295/*
a994f81b
VZ
296 if ( !M_VTEXTDATA )
297 return;
c801d85f
KB
298*/
299
a994f81b
VZ
300 if ( m_validatorWindow )
301 {
12a3f227 302 int keyCode = event.GetKeyCode();
a994f81b
VZ
303
304 // we don't filter special keys and Delete
305 if (
306 !(keyCode < WXK_SPACE || keyCode == WXK_DELETE || keyCode > WXK_START) &&
307 (
f94a790d
RN
308 ((m_validatorStyle & wxFILTER_INCLUDE_CHAR_LIST) && !IsInCharIncludes(wxString((wxChar) keyCode, 1))) ||
309 ((m_validatorStyle & wxFILTER_EXCLUDE_CHAR_LIST) && !IsNotInCharExcludes(wxString((wxChar) keyCode, 1))) ||
b1e4e7cc 310 ((m_validatorStyle & wxFILTER_ASCII) && !isascii(keyCode)) ||
0c6099b7
RN
311 ((m_validatorStyle & wxFILTER_ALPHA) && !wxIsalpha(keyCode)) ||
312 ((m_validatorStyle & wxFILTER_ALPHANUMERIC) && !wxIsalnum(keyCode)) ||
313 ((m_validatorStyle & wxFILTER_NUMERIC) && !wxIsdigit(keyCode)
543f08a6 314 && keyCode != '.' && keyCode != ',' && keyCode != '-')
a994f81b
VZ
315 )
316 )
317 {
318 if ( !wxValidator::IsSilent() )
319 wxBell();
320
321 // eat message
322 return;
323 }
324 }
325
326 event.Skip();
c801d85f
KB
327}
328
386af6a2
JS
329static bool wxIsNumeric(const wxString& val)
330{
a994f81b
VZ
331 int i;
332 for ( i = 0; i < (int)val.Length(); i++)
333 {
543f08a6
JS
334 // Allow for "," (French) as well as "." -- in future we should
335 // use wxSystemSettings or other to do better localisation
1c193821 336 if ((!wxIsdigit(val[i])) && (val[i] != '.') && (val[i] != ',') && (val[i] != wxT('e')) && (val[i] != wxT('E')) && (val[i] != wxT('+')) && (val[i] != wxT('-')))
cab1a605 337 return false;
a994f81b 338 }
cab1a605 339 return true;
386af6a2 340}
c801d85f 341
aaae8296 342
ce4169a4 343#endif
d7260478 344 // wxUSE_VALIDATORS && wxUSE_TEXTCTRL