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