]> git.saurik.com Git - wxWidgets.git/blame - src/common/valtext.cpp
compilation fix after argv changes
[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
472eec8a 19#if wxUSE_VALIDATORS && (wxUSE_TEXTCTRL || wxUSE_COMBOBOX)
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"
92646f5a 26 #include "wx/combobox.h"
ce4169a4
RR
27 #include "wx/utils.h"
28 #include "wx/msgdlg.h"
29 #include "wx/intl.h"
c801d85f
KB
30#endif
31
c801d85f
KB
32#include <ctype.h>
33#include <string.h>
34#include <stdlib.h>
35
ce3ed50d 36#ifdef __SALFORDC__
a994f81b 37 #include <clib.h>
ce3ed50d
JS
38#endif
39
c801d85f
KB
40IMPLEMENT_DYNAMIC_CLASS(wxTextValidator, wxValidator)
41
42BEGIN_EVENT_TABLE(wxTextValidator, wxValidator)
a994f81b 43 EVT_CHAR(wxTextValidator::OnChar)
c801d85f 44END_EVENT_TABLE()
c801d85f 45
386af6a2
JS
46static bool wxIsNumeric(const wxString& val);
47
debe6624 48wxTextValidator::wxTextValidator(long style, wxString *val)
c801d85f 49{
52cd14b1
VZ
50 m_validatorStyle = style;
51 m_stringValue = val;
c801d85f
KB
52/*
53 m_refData = new wxVTextRefData;
54
52cd14b1
VZ
55 M_VTEXTDATA->m_validatorStyle = style;
56 M_VTEXTDATA->m_stringValue = val;
c801d85f
KB
57*/
58}
59
60wxTextValidator::wxTextValidator(const wxTextValidator& val)
d84afea9 61 : wxValidator()
c801d85f
KB
62{
63 Copy(val);
64}
65
66bool wxTextValidator::Copy(const wxTextValidator& val)
67{
68 wxValidator::Copy(val);
69
52cd14b1
VZ
70 m_validatorStyle = val.m_validatorStyle;
71 m_stringValue = val.m_stringValue;
a994f81b 72
f94a790d
RN
73 m_includes = val.m_includes;
74 m_excludes = val.m_excludes;
52cd14b1 75
cab1a605 76 return true;
c801d85f
KB
77}
78
472eec8a
VZ
79wxTextEntry *wxTextValidator::GetTextEntry()
80{
81#if wxUSE_TEXTCTRL
82 if (m_validatorWindow->IsKindOf(CLASSINFO(wxTextCtrl)))
83 {
84 return (wxTextCtrl*)m_validatorWindow;
85 }
86#endif
7b235dce 87
472eec8a
VZ
88#if wxUSE_COMBOBOX
89 if (m_validatorWindow->IsKindOf(CLASSINFO(wxComboBox)))
90 {
91 return (wxComboBox*)m_validatorWindow;
92 }
93#endif
94
95 wxFAIL_MSG(
96 _T("wxTextValidator can only be used with wxTextCtrl or wxComboBox")
97 );
98
99 return NULL;
100}
101
c801d85f
KB
102static bool wxIsAlpha(const wxString& val)
103{
a994f81b 104 int i;
264cb7f5 105 for ( i = 0; i < (int)val.length(); i++)
a994f81b 106 {
f6bcfd97 107 if (!wxIsalpha(val[i]))
cab1a605 108 return false;
a994f81b 109 }
cab1a605 110 return true;
c801d85f
KB
111}
112
113static bool wxIsAlphaNumeric(const wxString& val)
114{
a994f81b 115 int i;
264cb7f5 116 for ( i = 0; i < (int)val.length(); i++)
a994f81b 117 {
f6bcfd97 118 if (!wxIsalnum(val[i]))
cab1a605 119 return false;
a994f81b 120 }
cab1a605 121 return true;
c801d85f
KB
122}
123
124// Called when the value in the window must be validated.
125// This function can pop up an error message.
126bool wxTextValidator::Validate(wxWindow *parent)
127{
6cd47507 128 // If window is disabled, simply return
472eec8a 129 if ( !m_validatorWindow->IsEnabled() )
cab1a605 130 return true;
a994f81b 131
472eec8a
VZ
132 wxTextEntry * const text = GetTextEntry();
133 if ( !text )
134 return false;
135
136 wxString val(text->GetValue());
a994f81b 137
cab1a605 138 bool ok = true;
a994f81b 139
e9086757
VZ
140 // NB: this format string should contian exactly one '%s'
141 wxString errormsg;
a994f81b 142
f94a790d
RN
143 bool includes = (m_validatorStyle & wxFILTER_INCLUDE_LIST) != 0;
144 if ( includes || (m_validatorStyle & wxFILTER_EXCLUDE_LIST) )
a994f81b 145 {
f94a790d 146 // if includes, it's only ok to have the members of the list,
e9086757 147 // otherwise it's only ok to have non-members
f94a790d 148 ok = includes == (m_includes.Index(val) != wxNOT_FOUND);
e9086757 149 if ( !ok )
a994f81b 150 {
e9086757 151 errormsg = _("'%s' is invalid");
a994f81b
VZ
152 }
153 }
154 else if ( (m_validatorStyle & wxFILTER_ASCII) && !val.IsAscii() )
155 {
cab1a605 156 ok = false;
a994f81b
VZ
157
158 errormsg = _("'%s' should only contain ASCII characters.");
159 }
160 else if ( (m_validatorStyle & wxFILTER_ALPHA) && !wxIsAlpha(val) )
161 {
cab1a605 162 ok = false;
a994f81b
VZ
163
164 errormsg = _("'%s' should only contain alphabetic characters.");
165 }
166 else if ( (m_validatorStyle & wxFILTER_ALPHANUMERIC) && !wxIsAlphaNumeric(val))
167 {
cab1a605 168 ok = false;
a994f81b
VZ
169
170 errormsg = _("'%s' should only contain alphabetic or numeric characters.");
171 }
172 else if ( (m_validatorStyle & wxFILTER_NUMERIC) && !wxIsNumeric(val))
173 {
cab1a605 174 ok = false;
a994f81b
VZ
175
176 errormsg = _("'%s' should be numeric.");
177 }
f94a790d 178 else if ( (m_validatorStyle & wxFILTER_INCLUDE_CHAR_LIST) && !IsInCharIncludes(val))
aaae8296
JS
179 {
180 //it's only ok to have the members of the list
181 errormsg = _("'%s' is invalid");
cab1a605 182 ok = false;
aaae8296 183 }
f94a790d 184 else if ( (m_validatorStyle & wxFILTER_EXCLUDE_CHAR_LIST) && !IsNotInCharExcludes(val))
aaae8296
JS
185 {
186 // it's only ok to have non-members of the list
187 errormsg = _("'%s' is invalid");
cab1a605 188 ok = false;
aaae8296 189 }
a994f81b
VZ
190
191 if ( !ok )
192 {
e9086757
VZ
193 wxASSERT_MSG( !errormsg.empty(), _T("you forgot to set errormsg") );
194
a994f81b
VZ
195 m_validatorWindow->SetFocus();
196
197 wxString buf;
198 buf.Printf(errormsg, val.c_str());
199
200 wxMessageBox(buf, _("Validation conflict"),
201 wxOK | wxICON_EXCLAMATION, parent);
202 }
203
204 return ok;
c801d85f
KB
205}
206
207// Called to transfer data to the window
208bool wxTextValidator::TransferToWindow(void)
209{
52cd14b1
VZ
210 if ( m_stringValue )
211 {
472eec8a
VZ
212 wxTextEntry * const text = GetTextEntry();
213 if ( !text )
214 return false;
215
216 text->SetValue(*m_stringValue);
52cd14b1 217 }
c801d85f 218
cab1a605 219 return true;
c801d85f
KB
220}
221
222// Called to transfer data to the window
223bool wxTextValidator::TransferFromWindow(void)
224{
52cd14b1
VZ
225 if ( m_stringValue )
226 {
472eec8a
VZ
227 wxTextEntry * const text = GetTextEntry();
228 if ( !text )
229 return false;
230
231 *m_stringValue = text->GetValue();
52cd14b1 232 }
c801d85f 233
cab1a605 234 return true;
c801d85f
KB
235}
236
f94a790d
RN
237bool wxTextValidator::IsInCharIncludes(const wxString& val)
238{
239 size_t i;
264cb7f5 240 for ( i = 0; i < val.length(); i++)
f94a790d
RN
241 {
242 if (m_includes.Index((wxString) val[i]) == wxNOT_FOUND)
243 return false;
244 }
245 return true;
246}
247
248bool wxTextValidator::IsNotInCharExcludes(const wxString& val)
249{
250 size_t i;
264cb7f5 251 for ( i = 0; i < val.length(); i++)
f94a790d
RN
252 {
253 if (m_excludes.Index((wxString) val[i]) != wxNOT_FOUND)
254 return false;
255 }
256 return true;
c801d85f
KB
257}
258
259void wxTextValidator::OnChar(wxKeyEvent& event)
260{
261/*
a994f81b
VZ
262 if ( !M_VTEXTDATA )
263 return;
c801d85f
KB
264*/
265
a994f81b
VZ
266 if ( m_validatorWindow )
267 {
12a3f227 268 int keyCode = event.GetKeyCode();
a994f81b
VZ
269
270 // we don't filter special keys and Delete
271 if (
272 !(keyCode < WXK_SPACE || keyCode == WXK_DELETE || keyCode > WXK_START) &&
273 (
f94a790d
RN
274 ((m_validatorStyle & wxFILTER_INCLUDE_CHAR_LIST) && !IsInCharIncludes(wxString((wxChar) keyCode, 1))) ||
275 ((m_validatorStyle & wxFILTER_EXCLUDE_CHAR_LIST) && !IsNotInCharExcludes(wxString((wxChar) keyCode, 1))) ||
b1e4e7cc 276 ((m_validatorStyle & wxFILTER_ASCII) && !isascii(keyCode)) ||
0c6099b7
RN
277 ((m_validatorStyle & wxFILTER_ALPHA) && !wxIsalpha(keyCode)) ||
278 ((m_validatorStyle & wxFILTER_ALPHANUMERIC) && !wxIsalnum(keyCode)) ||
279 ((m_validatorStyle & wxFILTER_NUMERIC) && !wxIsdigit(keyCode)
14d77693 280 && keyCode != wxT('.') && keyCode != wxT(',') && keyCode != wxT('-') && keyCode != wxT('+') && keyCode != wxT('e') && keyCode != wxT('E'))
a994f81b
VZ
281 )
282 )
283 {
284 if ( !wxValidator::IsSilent() )
285 wxBell();
286
287 // eat message
288 return;
289 }
290 }
291
292 event.Skip();
c801d85f
KB
293}
294
386af6a2
JS
295static bool wxIsNumeric(const wxString& val)
296{
a994f81b 297 int i;
264cb7f5 298 for ( i = 0; i < (int)val.length(); i++)
a994f81b 299 {
543f08a6
JS
300 // Allow for "," (French) as well as "." -- in future we should
301 // use wxSystemSettings or other to do better localisation
f7f00d8a 302 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 303 return false;
a994f81b 304 }
cab1a605 305 return true;
386af6a2 306}
c801d85f 307
aaae8296 308
ce4169a4 309#endif
472eec8a 310 // wxUSE_VALIDATORS && (wxUSE_TEXTCTRL || wxUSE_COMBOBOX)