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