]> git.saurik.com Git - wxWidgets.git/blob - src/common/valtext.cpp
Reorganize wxCollapsiblePane event and layout code under GTK+
[wxWidgets.git] / src / common / valtext.cpp
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 // ----------------------------------------------------------------------------
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
69 if ((!wxIsdigit(val[i])) && (val[i] != wxT('.')) && (val[i] != wxT(',')) && (val[i] != wxT('e')) &&
70 (val[i] != wxT('E')) && (val[i] != wxT('+')) && (val[i] != wxT('-')))
71 return false;
72 }
73 return true;
74 }
75
76
77 // ----------------------------------------------------------------------------
78 // wxTextValidator
79 // ----------------------------------------------------------------------------
80
81 IMPLEMENT_DYNAMIC_CLASS(wxTextValidator, wxValidator)
82 BEGIN_EVENT_TABLE(wxTextValidator, wxValidator)
83 EVT_CHAR(wxTextValidator::OnChar)
84 END_EVENT_TABLE()
85
86
87 #if WXWIN_COMPATIBILITY_2_8
88 wxTextValidator::wxTextValidator(long style, wxString *val)
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)
101 {
102 m_validatorStyle = style;
103 m_stringValue = val;
104 }
105
106 wxTextValidator::wxTextValidator(const wxTextValidator& val)
107 : wxValidator()
108 {
109 Copy(val);
110 }
111
112 bool wxTextValidator::Copy(const wxTextValidator& val)
113 {
114 wxValidator::Copy(val);
115
116 m_validatorStyle = val.m_validatorStyle;
117 m_stringValue = val.m_stringValue;
118
119 m_includes = val.m_includes;
120 m_excludes = val.m_excludes;
121
122 return true;
123 }
124
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
133
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
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 {
152 // If window is disabled, simply return
153 if ( !m_validatorWindow->IsEnabled() )
154 return true;
155
156 wxTextEntry * const text = GetTextEntry();
157 if ( !text )
158 return false;
159
160 wxString val(text->GetValue());
161
162 // NB: this format string should always contain exactly one '%s'
163 wxString errormsg;
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;
190
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;
215 switch (m_validatorStyle)
216 {
217 case wxFILTER_NONE:
218 // nothing to do...
219 break;
220
221 case wxFILTER_INCLUDE_LIST:
222 if ( m_includes.Index(val) == wxNOT_FOUND )
223 errormsg = _("'%s' is invalid");
224 break;
225
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;
255
256 case wxFILTER_EXCLUDE_CHAR_LIST:
257 if ( !IsNotInCharExcludes(val) )
258 errormsg = _("'%s' is invalid");
259 break;
260
261 default:
262 wxFAIL_MSG("invalid text validator style");
263 }
264
265 if (pErr)
266 *pErr = errormsg;
267
268 return errormsg.empty();
269 }
270
271 bool wxTextValidator::IsInCharIncludes(const wxString& val) const
272 {
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...
276 return false;
277
278 // all characters of 'val' are present in m_includes
279 return true;
280 }
281
282 bool wxTextValidator::IsNotInCharExcludes(const wxString& val) const
283 {
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...
287 return false;
288
289 // all characters of 'val' are NOT present in m_excludes
290 return true;
291 }
292
293 void wxTextValidator::OnChar(wxKeyEvent& event)
294 {
295 if (!m_validatorWindow)
296 {
297 event.Skip();
298 return;
299 }
300
301 int keyCode = event.GetKeyCode();
302
303 // we don't filter special keys and delete
304 if (keyCode < WXK_SPACE || keyCode == WXK_DELETE || keyCode >= WXK_START)
305 {
306 event.Skip();
307 return;
308 }
309
310 wxString str((wxUniChar)keyCode, 1);
311 if (!IsValid(str, NULL))
312 {
313 if ( !wxValidator::IsSilent() )
314 wxBell();
315
316 // eat message
317 return;
318 }
319 else
320 event.Skip();
321 }
322
323
324 #endif
325 // wxUSE_VALIDATORS && (wxUSE_TEXTCTRL || wxUSE_COMBOBOX)