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