]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/common/valtext.cpp
Correct erasing of background behind controls in a toolbar in wxMSW.
[wxWidgets.git] / src / common / valtext.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/common/valtext.cpp
3// Purpose: wxTextValidator
4// Author: Julian Smart
5// Modified by: Francesco Montorsi
6// Created: 04/01/98
7// Copyright: (c) Julian Smart
8// Licence: wxWindows licence
9/////////////////////////////////////////////////////////////////////////////
10
11// For compilers that support precompilation, includes "wx.h".
12#include "wx/wxprec.h"
13
14#ifdef __BORLANDC__
15 #pragma hdrstop
16#endif
17
18#if wxUSE_VALIDATORS && (wxUSE_TEXTCTRL || wxUSE_COMBOBOX)
19
20#include "wx/valtext.h"
21
22#ifndef WX_PRECOMP
23 #include <stdio.h>
24 #include "wx/textctrl.h"
25 #include "wx/combobox.h"
26 #include "wx/utils.h"
27 #include "wx/msgdlg.h"
28 #include "wx/intl.h"
29#endif
30
31#include <ctype.h>
32#include <string.h>
33#include <stdlib.h>
34
35#include "wx/combo.h"
36
37// ----------------------------------------------------------------------------
38// global helpers
39// ----------------------------------------------------------------------------
40
41static bool wxIsNumeric(const wxString& val)
42{
43 for ( wxString::const_iterator i = val.begin(); i != val.end(); ++i )
44 {
45 // Allow for "," (French) as well as "." -- in future we should
46 // use wxSystemSettings or other to do better localisation
47 if ((!wxIsdigit(*i)) &&
48 (*i != wxS('.')) && (*i != wxS(',')) && (*i != wxS('e')) &&
49 (*i != wxS('E')) && (*i != wxS('+')) && (*i != wxS('-')))
50 return false;
51 }
52 return true;
53}
54
55// ----------------------------------------------------------------------------
56// wxTextValidator
57// ----------------------------------------------------------------------------
58
59IMPLEMENT_DYNAMIC_CLASS(wxTextValidator, wxValidator)
60BEGIN_EVENT_TABLE(wxTextValidator, wxValidator)
61 EVT_CHAR(wxTextValidator::OnChar)
62END_EVENT_TABLE()
63
64wxTextValidator::wxTextValidator(long style, wxString *val)
65{
66 m_stringValue = val;
67 SetStyle(style);
68}
69
70wxTextValidator::wxTextValidator(const wxTextValidator& val)
71 : wxValidator()
72{
73 Copy(val);
74}
75
76void wxTextValidator::SetStyle(long style)
77{
78 m_validatorStyle = style;
79
80#if wxDEBUG_LEVEL
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");
97#endif // wxDEBUG_LEVEL
98}
99
100bool wxTextValidator::Copy(const wxTextValidator& val)
101{
102 wxValidator::Copy(val);
103
104 m_validatorStyle = val.m_validatorStyle;
105 m_stringValue = val.m_stringValue;
106
107 m_includes = val.m_includes;
108 m_excludes = val.m_excludes;
109
110 return true;
111}
112
113wxTextEntry *wxTextValidator::GetTextEntry()
114{
115#if wxUSE_TEXTCTRL
116 if (wxDynamicCast(m_validatorWindow, wxTextCtrl))
117 {
118 return (wxTextCtrl*)m_validatorWindow;
119 }
120#endif
121
122#if wxUSE_COMBOBOX
123 if (wxDynamicCast(m_validatorWindow, wxComboBox))
124 {
125 return (wxComboBox*)m_validatorWindow;
126 }
127#endif
128
129#if wxUSE_COMBOCTRL
130 if (wxDynamicCast(m_validatorWindow, wxComboCtrl))
131 {
132 return (wxComboCtrl*)m_validatorWindow;
133 }
134#endif
135
136 wxFAIL_MSG(
137 "wxTextValidator can only be used with wxTextCtrl, wxComboBox, "
138 "or wxComboCtrl"
139 );
140
141 return NULL;
142}
143
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{
148 // If window is disabled, simply return
149 if ( !m_validatorWindow->IsEnabled() )
150 return true;
151
152 wxTextEntry * const text = GetTextEntry();
153 if ( !text )
154 return false;
155
156 wxString val(text->GetValue());
157
158 wxString errormsg;
159 if ( HasFlag(wxFILTER_EMPTY) && val.empty() )
160 {
161 errormsg = _("Required information entry is empty.");
162 }
163 else if ( !(errormsg = IsValid(val)).empty() )
164 {
165 // NB: this format string should always contain exactly one '%s'
166 wxString buf;
167 buf.Printf(errormsg, val.c_str());
168 errormsg = buf;
169 }
170
171 if ( !errormsg.empty() )
172 {
173 m_validatorWindow->SetFocus();
174 wxMessageBox(errormsg, _("Validation conflict"),
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;
191
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
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
234wxString wxTextValidator::IsValid(const wxString& val) const
235{
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.");
240 if ( HasFlag(wxFILTER_ALPHA) && !CheckString(wxIsalpha, val) )
241 return _("'%s' should only contain alphabetic characters.");
242 if ( HasFlag(wxFILTER_ALPHANUMERIC) && !CheckString(wxIsalnum, val) )
243 return _("'%s' should only contain alphabetic or numeric characters.");
244 if ( HasFlag(wxFILTER_DIGITS) && !CheckString(wxIsdigit, val) )
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;
258}
259
260bool wxTextValidator::ContainsOnlyIncludedCharacters(const wxString& val) const
261{
262 for ( wxString::const_iterator i = val.begin(); i != val.end(); ++i )
263 if (m_includes.Index((wxString) *i) == wxNOT_FOUND)
264 // one character of 'val' is NOT present in m_includes...
265 return false;
266
267 // all characters of 'val' are present in m_includes
268 return true;
269}
270
271bool wxTextValidator::ContainsExcludedCharacters(const wxString& val) const
272{
273 for ( wxString::const_iterator i = val.begin(); i != val.end(); ++i )
274 if (m_excludes.Index((wxString) *i) != wxNOT_FOUND)
275 // one character of 'val' is present in m_excludes...
276 return true;
277
278 // all characters of 'val' are NOT present in m_excludes
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);
300}
301
302void wxTextValidator::OnChar(wxKeyEvent& event)
303{
304 if (!m_validatorWindow)
305 {
306 event.Skip();
307 return;
308 }
309
310 int keyCode = event.GetKeyCode();
311
312 // we don't filter special keys and delete
313 if (keyCode < WXK_SPACE || keyCode == WXK_DELETE || keyCode >= WXK_START)
314 {
315 event.Skip();
316 return;
317 }
318
319 wxString str((wxUniChar)keyCode, 1);
320 if (!IsValid(str).empty())
321 {
322 if ( !wxValidator::IsSilent() )
323 wxBell();
324
325 // eat message
326 return;
327 }
328 else
329 event.Skip();
330}
331
332
333#endif
334 // wxUSE_VALIDATORS && (wxUSE_TEXTCTRL || wxUSE_COMBOBOX)