]> git.saurik.com Git - wxWidgets.git/blame - src/common/valtext.cpp
fixing osx_cocoa
[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
10b0f489 5// Modified by: Francesco Montorsi
c801d85f
KB
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
fda62793
JS
36#include "wx/combo.h"
37
40ae9600
FM
38// ----------------------------------------------------------------------------
39// global helpers
40// ----------------------------------------------------------------------------
41
40ae9600
FM
42static bool wxIsNumeric(const wxString& val)
43{
1406dc01 44 for ( wxString::const_iterator i = val.begin(); i != val.end(); ++i )
40ae9600
FM
45 {
46 // Allow for "," (French) as well as "." -- in future we should
47 // use wxSystemSettings or other to do better localisation
58fa61db
FM
48 if ((!wxIsdigit(*i)) &&
49 (*i != wxS('.')) && (*i != wxS(',')) && (*i != wxS('e')) &&
50 (*i != wxS('E')) && (*i != wxS('+')) && (*i != wxS('-')))
40ae9600
FM
51 return false;
52 }
53 return true;
54}
55
40ae9600
FM
56// ----------------------------------------------------------------------------
57// wxTextValidator
58// ----------------------------------------------------------------------------
59
60IMPLEMENT_DYNAMIC_CLASS(wxTextValidator, wxValidator)
c801d85f 61BEGIN_EVENT_TABLE(wxTextValidator, wxValidator)
a994f81b 62 EVT_CHAR(wxTextValidator::OnChar)
c801d85f 63END_EVENT_TABLE()
c801d85f 64
debe6624 65wxTextValidator::wxTextValidator(long style, wxString *val)
40ae9600 66{
40ae9600 67 m_stringValue = val;
58fa61db 68 SetStyle(style);
40ae9600
FM
69}
70
58fa61db
FM
71wxTextValidator::wxTextValidator(const wxTextValidator& val)
72 : wxValidator()
40ae9600 73{
58fa61db 74 Copy(val);
40ae9600 75}
40ae9600 76
58fa61db 77void wxTextValidator::SetStyle(long style)
c801d85f 78{
52cd14b1 79 m_validatorStyle = style;
c801d85f 80
4b6a582b 81#if wxDEBUG_LEVEL
58fa61db
FM
82 int check;
83 check = (int)HasFlag(wxFILTER_ALPHA) + (int)HasFlag(wxFILTER_ALPHANUMERIC) +
84 (int)HasFlag(wxFILTER_DIGITS) + (int)HasFlag(wxFILTER_NUMERIC);
85 wxASSERT_MSG(check <= 1,
86 "It makes sense to use only one of the wxFILTER_ALPHA/wxFILTER_ALPHANUMERIC/"
87 "wxFILTER_SIMPLE_NUMBER/wxFILTER_NUMERIC styles");
88
89 wxASSERT_MSG(((int)HasFlag(wxFILTER_INCLUDE_LIST) + (int)HasFlag(wxFILTER_INCLUDE_CHAR_LIST) <= 1) &&
90 ((int)HasFlag(wxFILTER_EXCLUDE_LIST) + (int)HasFlag(wxFILTER_EXCLUDE_CHAR_LIST) <= 1),
91 "Using both wxFILTER_[IN|EX]CLUDE_LIST _and_ wxFILTER_[IN|EX]CLUDE_CHAR_LIST "
92 "doesn't work since wxTextValidator internally uses the same array for both");
93
94 check = (int)HasFlag(wxFILTER_INCLUDE_LIST) + (int)HasFlag(wxFILTER_INCLUDE_CHAR_LIST) +
95 (int)HasFlag(wxFILTER_EXCLUDE_LIST) + (int)HasFlag(wxFILTER_EXCLUDE_CHAR_LIST);
96 wxASSERT_MSG(check <= 1,
97 "Using both an include/exclude list may lead to unexpected results");
4b6a582b 98#endif // wxDEBUG_LEVEL
c801d85f
KB
99}
100
101bool wxTextValidator::Copy(const wxTextValidator& val)
102{
103 wxValidator::Copy(val);
104
52cd14b1
VZ
105 m_validatorStyle = val.m_validatorStyle;
106 m_stringValue = val.m_stringValue;
a994f81b 107
f94a790d
RN
108 m_includes = val.m_includes;
109 m_excludes = val.m_excludes;
52cd14b1 110
cab1a605 111 return true;
c801d85f
KB
112}
113
472eec8a
VZ
114wxTextEntry *wxTextValidator::GetTextEntry()
115{
116#if wxUSE_TEXTCTRL
117 if (m_validatorWindow->IsKindOf(CLASSINFO(wxTextCtrl)))
118 {
119 return (wxTextCtrl*)m_validatorWindow;
120 }
121#endif
7b235dce 122
472eec8a
VZ
123#if wxUSE_COMBOBOX
124 if (m_validatorWindow->IsKindOf(CLASSINFO(wxComboBox)))
125 {
126 return (wxComboBox*)m_validatorWindow;
127 }
128#endif
129
fda62793
JS
130#if wxUSE_COMBOCTRL
131 if (m_validatorWindow->IsKindOf(CLASSINFO(wxComboCtrl)))
132 {
133 return (wxComboCtrl*)m_validatorWindow;
134 }
135#endif
136
472eec8a 137 wxFAIL_MSG(
fda62793
JS
138 "wxTextValidator can only be used with wxTextCtrl, wxComboBox, "
139 "or wxComboCtrl"
472eec8a
VZ
140 );
141
142 return NULL;
143}
144
c801d85f
KB
145// Called when the value in the window must be validated.
146// This function can pop up an error message.
147bool wxTextValidator::Validate(wxWindow *parent)
148{
6cd47507 149 // If window is disabled, simply return
472eec8a 150 if ( !m_validatorWindow->IsEnabled() )
cab1a605 151 return true;
a994f81b 152
472eec8a
VZ
153 wxTextEntry * const text = GetTextEntry();
154 if ( !text )
155 return false;
156
157 wxString val(text->GetValue());
a994f81b 158
e9086757 159 wxString errormsg;
58fa61db 160 if ( HasFlag(wxFILTER_EMPTY) && val.empty() )
10b0f489 161 {
af59b3fc 162 errormsg = _("Required information entry is empty.");
58fa61db
FM
163 }
164 else if ( !(errormsg = IsValid(val)).empty() )
165 {
166 // NB: this format string should always contain exactly one '%s'
10b0f489
FM
167 wxString buf;
168 buf.Printf(errormsg, val.c_str());
58fa61db
FM
169 errormsg = buf;
170 }
10b0f489 171
58fa61db
FM
172 if ( !errormsg.empty() )
173 {
174 m_validatorWindow->SetFocus();
175 wxMessageBox(errormsg, _("Validation conflict"),
10b0f489
FM
176 wxOK | wxICON_EXCLAMATION, parent);
177
178 return false;
179 }
180
181 return true;
182}
183
184// Called to transfer data to the window
185bool wxTextValidator::TransferToWindow()
186{
187 if ( m_stringValue )
188 {
189 wxTextEntry * const text = GetTextEntry();
190 if ( !text )
191 return false;
a994f81b 192
10b0f489
FM
193 text->SetValue(*m_stringValue);
194 }
195
196 return true;
197}
198
199// Called to transfer data to the window
200bool wxTextValidator::TransferFromWindow()
201{
202 if ( m_stringValue )
203 {
204 wxTextEntry * const text = GetTextEntry();
205 if ( !text )
206 return false;
207
208 *m_stringValue = text->GetValue();
209 }
210
211 return true;
212}
213
3e026ad2
VZ
214// IRIX mipsPro refuses to compile wxStringCheck<func>() if func is inline so
215// let's work around this by using this non-template function instead of
216// wxStringCheck(). And while this might be fractionally less efficient because
217// the function call won't be inlined like this, we don't care enough about
218// this to add extra #ifs for non-IRIX case.
219namespace
220{
221
222bool CheckString(bool (*func)(const wxUniChar&), const wxString& str)
223{
224 for ( wxString::const_iterator i = str.begin(); i != str.end(); ++i )
225 {
226 if ( !func(*i) )
227 return false;
228 }
229
230 return true;
231}
232
233} // anonymous namespace
234
58fa61db 235wxString wxTextValidator::IsValid(const wxString& val) const
10b0f489 236{
58fa61db
FM
237 // wxFILTER_EMPTY is checked for in wxTextValidator::Validate
238
239 if ( HasFlag(wxFILTER_ASCII) && !val.IsAscii() )
240 return _("'%s' should only contain ASCII characters.");
3e026ad2 241 if ( HasFlag(wxFILTER_ALPHA) && !CheckString(wxIsalpha, val) )
58fa61db 242 return _("'%s' should only contain alphabetic characters.");
3e026ad2 243 if ( HasFlag(wxFILTER_ALPHANUMERIC) && !CheckString(wxIsalnum, val) )
58fa61db 244 return _("'%s' should only contain alphabetic or numeric characters.");
90497db9 245 if ( HasFlag(wxFILTER_DIGITS) && !CheckString(wxIsdigit, val) )
58fa61db
FM
246 return _("'%s' should only contain digits.");
247 if ( HasFlag(wxFILTER_NUMERIC) && !wxIsNumeric(val) )
248 return _("'%s' should be numeric.");
249 if ( HasFlag(wxFILTER_INCLUDE_LIST) && m_includes.Index(val) == wxNOT_FOUND )
250 return _("'%s' is invalid");
251 if ( HasFlag(wxFILTER_INCLUDE_CHAR_LIST) && !ContainsOnlyIncludedCharacters(val) )
252 return _("'%s' is invalid");
253 if ( HasFlag(wxFILTER_EXCLUDE_LIST) && m_excludes.Index(val) != wxNOT_FOUND )
254 return _("'%s' is invalid");
255 if ( HasFlag(wxFILTER_EXCLUDE_CHAR_LIST) && ContainsExcludedCharacters(val) )
256 return _("'%s' is invalid");
257
258 return wxEmptyString;
c801d85f
KB
259}
260
1406dc01 261bool wxTextValidator::ContainsOnlyIncludedCharacters(const wxString& val) const
c801d85f 262{
fcd209b6
FM
263 for ( wxString::const_iterator i = val.begin(); i != val.end(); ++i )
264 if (m_includes.Index((wxString) *i) == wxNOT_FOUND)
10b0f489 265 // one character of 'val' is NOT present in m_includes...
472eec8a
VZ
266 return false;
267
10b0f489 268 // all characters of 'val' are present in m_includes
cab1a605 269 return true;
c801d85f
KB
270}
271
1406dc01 272bool wxTextValidator::ContainsExcludedCharacters(const wxString& val) const
c801d85f 273{
fcd209b6
FM
274 for ( wxString::const_iterator i = val.begin(); i != val.end(); ++i )
275 if (m_excludes.Index((wxString) *i) != wxNOT_FOUND)
10b0f489 276 // one character of 'val' is present in m_excludes...
fcd209b6 277 return true;
472eec8a 278
10b0f489 279 // all characters of 'val' are NOT present in m_excludes
fcd209b6
FM
280 return false;
281}
282
283void wxTextValidator::SetCharIncludes(const wxString& chars)
284{
285 wxArrayString arr;
286
287 for ( wxString::const_iterator i = chars.begin(); i != chars.end(); ++i )
288 arr.Add(*i);
289
290 SetIncludes(arr);
291}
292
293void wxTextValidator::SetCharExcludes(const wxString& chars)
294{
295 wxArrayString arr;
296
297 for ( wxString::const_iterator i = chars.begin(); i != chars.end(); ++i )
298 arr.Add(*i);
299
300 SetExcludes(arr);
c801d85f
KB
301}
302
10b0f489 303void wxTextValidator::OnChar(wxKeyEvent& event)
f94a790d 304{
10b0f489 305 if (!m_validatorWindow)
f94a790d 306 {
10b0f489
FM
307 event.Skip();
308 return;
f94a790d 309 }
f94a790d 310
10b0f489 311 int keyCode = event.GetKeyCode();
c801d85f 312
10b0f489
FM
313 // we don't filter special keys and delete
314 if (keyCode < WXK_SPACE || keyCode == WXK_DELETE || keyCode >= WXK_START)
315 {
316 event.Skip();
a994f81b 317 return;
10b0f489 318 }
c801d85f 319
10b0f489 320 wxString str((wxUniChar)keyCode, 1);
58fa61db 321 if (!IsValid(str).empty())
a994f81b 322 {
10b0f489
FM
323 if ( !wxValidator::IsSilent() )
324 wxBell();
a994f81b 325
10b0f489
FM
326 // eat message
327 return;
328 }
329 else
330 event.Skip();
c801d85f
KB
331}
332
aaae8296 333
ce4169a4 334#endif
472eec8a 335 // wxUSE_VALIDATORS && (wxUSE_TEXTCTRL || wxUSE_COMBOBOX)