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