]> git.saurik.com Git - wxWidgets.git/blob - src/common/valtext.cpp
added wxTextEntry::AutoComplete() and implemented it for wxGTK
[wxWidgets.git] / src / common / valtext.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/valtext.cpp
3 // Purpose: wxTextValidator
4 // Author: Julian Smart
5 // Modified by:
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 #ifdef __SALFORDC__
37 #include <clib.h>
38 #endif
39
40 IMPLEMENT_DYNAMIC_CLASS(wxTextValidator, wxValidator)
41
42 BEGIN_EVENT_TABLE(wxTextValidator, wxValidator)
43 EVT_CHAR(wxTextValidator::OnChar)
44 END_EVENT_TABLE()
45
46 static bool wxIsNumeric(const wxString& val);
47
48 wxTextValidator::wxTextValidator(long style, wxString *val)
49 {
50 m_validatorStyle = style;
51 m_stringValue = val;
52 /*
53 m_refData = new wxVTextRefData;
54
55 M_VTEXTDATA->m_validatorStyle = style;
56 M_VTEXTDATA->m_stringValue = val;
57 */
58 }
59
60 wxTextValidator::wxTextValidator(const wxTextValidator& val)
61 : wxValidator()
62 {
63 Copy(val);
64 }
65
66 bool wxTextValidator::Copy(const wxTextValidator& val)
67 {
68 wxValidator::Copy(val);
69
70 m_validatorStyle = val.m_validatorStyle;
71 m_stringValue = val.m_stringValue;
72
73 m_includes = val.m_includes;
74 m_excludes = val.m_excludes;
75
76 return true;
77 }
78
79 wxTextEntry *wxTextValidator::GetTextEntry()
80 {
81 #if wxUSE_TEXTCTRL
82 if (m_validatorWindow->IsKindOf(CLASSINFO(wxTextCtrl)))
83 {
84 return (wxTextCtrl*)m_validatorWindow;
85 }
86 #endif
87 #if wxUSE_COMBOBOX
88 if (m_validatorWindow->IsKindOf(CLASSINFO(wxComboBox)))
89 {
90 return (wxComboBox*)m_validatorWindow;
91 }
92 #endif
93
94 wxFAIL_MSG(
95 _T("wxTextValidator can only be used with wxTextCtrl or wxComboBox")
96 );
97
98 return NULL;
99 }
100
101 static bool wxIsAlpha(const wxString& val)
102 {
103 int i;
104 for ( i = 0; i < (int)val.length(); i++)
105 {
106 if (!wxIsalpha(val[i]))
107 return false;
108 }
109 return true;
110 }
111
112 static bool wxIsAlphaNumeric(const wxString& val)
113 {
114 int i;
115 for ( i = 0; i < (int)val.length(); i++)
116 {
117 if (!wxIsalnum(val[i]))
118 return false;
119 }
120 return true;
121 }
122
123 // Called when the value in the window must be validated.
124 // This function can pop up an error message.
125 bool wxTextValidator::Validate(wxWindow *parent)
126 {
127 // If window is disabled, simply return
128 if ( !m_validatorWindow->IsEnabled() )
129 return true;
130
131 wxTextEntry * const text = GetTextEntry();
132 if ( !text )
133 return false;
134
135 wxString val(text->GetValue());
136
137 bool ok = true;
138
139 // NB: this format string should contian exactly one '%s'
140 wxString errormsg;
141
142 bool includes = (m_validatorStyle & wxFILTER_INCLUDE_LIST) != 0;
143 if ( includes || (m_validatorStyle & wxFILTER_EXCLUDE_LIST) )
144 {
145 // if includes, it's only ok to have the members of the list,
146 // otherwise it's only ok to have non-members
147 ok = includes == (m_includes.Index(val) != wxNOT_FOUND);
148 if ( !ok )
149 {
150 errormsg = _("'%s' is invalid");
151 }
152 }
153 else if ( (m_validatorStyle & wxFILTER_ASCII) && !val.IsAscii() )
154 {
155 ok = false;
156
157 errormsg = _("'%s' should only contain ASCII characters.");
158 }
159 else if ( (m_validatorStyle & wxFILTER_ALPHA) && !wxIsAlpha(val) )
160 {
161 ok = false;
162
163 errormsg = _("'%s' should only contain alphabetic characters.");
164 }
165 else if ( (m_validatorStyle & wxFILTER_ALPHANUMERIC) && !wxIsAlphaNumeric(val))
166 {
167 ok = false;
168
169 errormsg = _("'%s' should only contain alphabetic or numeric characters.");
170 }
171 else if ( (m_validatorStyle & wxFILTER_NUMERIC) && !wxIsNumeric(val))
172 {
173 ok = false;
174
175 errormsg = _("'%s' should be numeric.");
176 }
177 else if ( (m_validatorStyle & wxFILTER_INCLUDE_CHAR_LIST) && !IsInCharIncludes(val))
178 {
179 //it's only ok to have the members of the list
180 errormsg = _("'%s' is invalid");
181 ok = false;
182 }
183 else if ( (m_validatorStyle & wxFILTER_EXCLUDE_CHAR_LIST) && !IsNotInCharExcludes(val))
184 {
185 // it's only ok to have non-members of the list
186 errormsg = _("'%s' is invalid");
187 ok = false;
188 }
189
190 if ( !ok )
191 {
192 wxASSERT_MSG( !errormsg.empty(), _T("you forgot to set errormsg") );
193
194 m_validatorWindow->SetFocus();
195
196 wxString buf;
197 buf.Printf(errormsg, val.c_str());
198
199 wxMessageBox(buf, _("Validation conflict"),
200 wxOK | wxICON_EXCLAMATION, parent);
201 }
202
203 return ok;
204 }
205
206 // Called to transfer data to the window
207 bool wxTextValidator::TransferToWindow(void)
208 {
209 if ( m_stringValue )
210 {
211 wxTextEntry * const text = GetTextEntry();
212 if ( !text )
213 return false;
214
215 text->SetValue(*m_stringValue);
216 }
217
218 return true;
219 }
220
221 // Called to transfer data to the window
222 bool wxTextValidator::TransferFromWindow(void)
223 {
224 if ( m_stringValue )
225 {
226 wxTextEntry * const text = GetTextEntry();
227 if ( !text )
228 return false;
229
230 *m_stringValue = text->GetValue();
231 }
232
233 return true;
234 }
235
236 bool wxTextValidator::IsInCharIncludes(const wxString& val)
237 {
238 size_t i;
239 for ( i = 0; i < val.length(); i++)
240 {
241 if (m_includes.Index((wxString) val[i]) == wxNOT_FOUND)
242 return false;
243 }
244 return true;
245 }
246
247 bool wxTextValidator::IsNotInCharExcludes(const wxString& val)
248 {
249 size_t i;
250 for ( i = 0; i < val.length(); i++)
251 {
252 if (m_excludes.Index((wxString) val[i]) != wxNOT_FOUND)
253 return false;
254 }
255 return true;
256 }
257
258 void wxTextValidator::OnChar(wxKeyEvent& event)
259 {
260 /*
261 if ( !M_VTEXTDATA )
262 return;
263 */
264
265 if ( m_validatorWindow )
266 {
267 int keyCode = event.GetKeyCode();
268
269 // we don't filter special keys and Delete
270 if (
271 !(keyCode < WXK_SPACE || keyCode == WXK_DELETE || keyCode > WXK_START) &&
272 (
273 ((m_validatorStyle & wxFILTER_INCLUDE_CHAR_LIST) && !IsInCharIncludes(wxString((wxChar) keyCode, 1))) ||
274 ((m_validatorStyle & wxFILTER_EXCLUDE_CHAR_LIST) && !IsNotInCharExcludes(wxString((wxChar) keyCode, 1))) ||
275 ((m_validatorStyle & wxFILTER_ASCII) && !isascii(keyCode)) ||
276 ((m_validatorStyle & wxFILTER_ALPHA) && !wxIsalpha(keyCode)) ||
277 ((m_validatorStyle & wxFILTER_ALPHANUMERIC) && !wxIsalnum(keyCode)) ||
278 ((m_validatorStyle & wxFILTER_NUMERIC) && !wxIsdigit(keyCode)
279 && keyCode != wxT('.') && keyCode != wxT(',') && keyCode != wxT('-') && keyCode != wxT('+') && keyCode != wxT('e') && keyCode != wxT('E'))
280 )
281 )
282 {
283 if ( !wxValidator::IsSilent() )
284 wxBell();
285
286 // eat message
287 return;
288 }
289 }
290
291 event.Skip();
292 }
293
294 static bool wxIsNumeric(const wxString& val)
295 {
296 int i;
297 for ( i = 0; i < (int)val.length(); i++)
298 {
299 // Allow for "," (French) as well as "." -- in future we should
300 // use wxSystemSettings or other to do better localisation
301 if ((!wxIsdigit(val[i])) && (val[i] != wxT('.')) && (val[i] != wxT(',')) && (val[i] != wxT('e')) && (val[i] != wxT('E')) && (val[i] != wxT('+')) && (val[i] != wxT('-')))
302 return false;
303 }
304 return true;
305 }
306
307
308 #endif
309 // wxUSE_VALIDATORS && (wxUSE_TEXTCTRL || wxUSE_COMBOBOX)