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