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