]> git.saurik.com Git - wxWidgets.git/blame - src/common/valtext.cpp
Fix for listbox problem, when created on invisible
[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
222ed1d6
MB
76 m_includeList = val.m_includeList;
77 m_excludeList = val.m_excludeList;
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
e9086757
VZ
124 bool includeList = (m_validatorStyle & wxFILTER_INCLUDE_LIST) != 0;
125 if ( includeList || (m_validatorStyle & wxFILTER_EXCLUDE_LIST) )
a994f81b 126 {
e9086757
VZ
127 // if includeList, it's only ok to have the members of the list,
128 // otherwise it's only ok to have non-members
129 ok = includeList == m_includeList.Member(val);
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 }
aaae8296
JS
159 else if ( (m_validatorStyle & wxFILTER_INCLUDE_CHAR_LIST) && !IsInCharIncludeList(val))
160 {
161 //it's only ok to have the members of the list
162 errormsg = _("'%s' is invalid");
cab1a605 163 ok = false;
aaae8296
JS
164 }
165 else if ( (m_validatorStyle & wxFILTER_EXCLUDE_CHAR_LIST) && !IsNotInCharExcludeList(val))
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
218void wxTextValidator::SetIncludeList(const wxStringList& list)
219{
222ed1d6 220 m_includeList = list;
c801d85f
KB
221}
222
223void wxTextValidator::SetExcludeList(const wxStringList& list)
224{
222ed1d6 225 m_excludeList = list;
c801d85f
KB
226}
227
228void wxTextValidator::OnChar(wxKeyEvent& event)
229{
230/*
a994f81b
VZ
231 if ( !M_VTEXTDATA )
232 return;
c801d85f
KB
233*/
234
a994f81b
VZ
235 if ( m_validatorWindow )
236 {
12a3f227 237 int keyCode = event.GetKeyCode();
a994f81b
VZ
238
239 // we don't filter special keys and Delete
240 if (
241 !(keyCode < WXK_SPACE || keyCode == WXK_DELETE || keyCode > WXK_START) &&
242 (
7df07b10
MB
243 ((m_validatorStyle & wxFILTER_INCLUDE_CHAR_LIST) && !IsInCharIncludeList(wxString((wxChar) keyCode, 1))) ||
244 ((m_validatorStyle & wxFILTER_EXCLUDE_CHAR_LIST) && !IsNotInCharExcludeList(wxString((wxChar) keyCode, 1))) ||
b1e4e7cc 245 ((m_validatorStyle & wxFILTER_ASCII) && !isascii(keyCode)) ||
0c6099b7
RN
246 ((m_validatorStyle & wxFILTER_ALPHA) && !wxIsalpha(keyCode)) ||
247 ((m_validatorStyle & wxFILTER_ALPHANUMERIC) && !wxIsalnum(keyCode)) ||
248 ((m_validatorStyle & wxFILTER_NUMERIC) && !wxIsdigit(keyCode)
543f08a6 249 && keyCode != '.' && keyCode != ',' && keyCode != '-')
a994f81b
VZ
250 )
251 )
252 {
253 if ( !wxValidator::IsSilent() )
254 wxBell();
255
256 // eat message
257 return;
258 }
259 }
260
261 event.Skip();
c801d85f
KB
262}
263
386af6a2
JS
264static bool wxIsNumeric(const wxString& val)
265{
a994f81b
VZ
266 int i;
267 for ( i = 0; i < (int)val.Length(); i++)
268 {
543f08a6
JS
269 // Allow for "," (French) as well as "." -- in future we should
270 // use wxSystemSettings or other to do better localisation
1c193821 271 if ((!wxIsdigit(val[i])) && (val[i] != '.') && (val[i] != ',') && (val[i] != wxT('e')) && (val[i] != wxT('E')) && (val[i] != wxT('+')) && (val[i] != wxT('-')))
cab1a605 272 return false;
a994f81b 273 }
cab1a605 274 return true;
386af6a2 275}
c801d85f 276
aaae8296
JS
277bool wxTextValidator::IsInCharIncludeList(const wxString& val)
278{
279 size_t i;
280 for ( i = 0; i < val.Length(); i++)
281 {
282 if (!m_includeList.Member((wxString) val[i]))
cab1a605 283 return false;
aaae8296 284 }
cab1a605 285 return true;
aaae8296
JS
286}
287
288bool wxTextValidator::IsNotInCharExcludeList(const wxString& val)
289{
290 size_t i;
291 for ( i = 0; i < val.Length(); i++)
292 {
293 if (m_excludeList.Member((wxString) val[i]))
cab1a605 294 return false;
aaae8296 295 }
cab1a605 296 return true;
aaae8296
JS
297}
298
ce4169a4 299#endif
d7260478 300 // wxUSE_VALIDATORS && wxUSE_TEXTCTRL