]> git.saurik.com Git - wxWidgets.git/blame - src/common/valtext.cpp
SetEventData for a few widgets
[wxWidgets.git] / src / common / valtext.cpp
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: 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 and Markus Holzem
9// Licence: wxWindows license
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
13#pragma implementation "valtext.h"
14#endif
15
16// For compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
18
19#ifdef __BORLANDC__
20#pragma hdrstop
21#endif
22
23#ifndef WX_PRECOMP
24#include <stdio.h>
25#include "wx/textctrl.h"
26#include "wx/utils.h"
903f689b
RR
27#include "wx/msgdlg.h"
28#include "wx/intl.h"
c801d85f
KB
29#endif
30
31#include "wx/valtext.h"
32
33#include <ctype.h>
34#include <string.h>
35#include <stdlib.h>
36
37#if !USE_SHARED_LIBRARY
38IMPLEMENT_DYNAMIC_CLASS(wxTextValidator, wxValidator)
39
40BEGIN_EVENT_TABLE(wxTextValidator, wxValidator)
41 EVT_CHAR(wxTextValidator::OnChar)
42END_EVENT_TABLE()
43#endif
44
386af6a2
JS
45static bool wxIsNumeric(const wxString& val);
46
debe6624 47wxTextValidator::wxTextValidator(long style, wxString *val)
c801d85f
KB
48{
49 m_validatorStyle = style ;
50 m_stringValue = val ;
51/*
52 m_refData = new wxVTextRefData;
53
54 M_VTEXTDATA->m_validatorStyle = style ;
55 M_VTEXTDATA->m_stringValue = val ;
56*/
57}
58
59wxTextValidator::wxTextValidator(const wxTextValidator& val)
60{
61 Copy(val);
62}
63
64bool wxTextValidator::Copy(const wxTextValidator& val)
65{
66 wxValidator::Copy(val);
67
68 m_validatorStyle = val.m_validatorStyle ;
69 m_stringValue = val.m_stringValue ;
70
71 wxNode *node = val.m_includeList.First() ;
72 while ( node )
73 {
74 char *s = (char *)node->Data();
75 m_includeList.Add(s);
76 node = node->Next();
77 }
78 node = val.m_excludeList.First() ;
79 while ( node )
80 {
81 char *s = (char *)node->Data();
82 m_excludeList.Add(s);
83 node = node->Next();
84 }
85 return TRUE;
86}
87
88wxTextValidator::~wxTextValidator()
89{
90}
91
92static bool wxIsAlpha(const wxString& val)
93{
94 int i;
95 for ( i = 0; i < (int)val.Length(); i++)
96 {
97 if (!isalpha(val[i]))
98 return FALSE;
99 }
100 return TRUE;
101}
102
103static bool wxIsAlphaNumeric(const wxString& val)
104{
105 int i;
106 for ( i = 0; i < (int)val.Length(); i++)
107 {
108 if (!isalnum(val[i]))
109 return FALSE;
110 }
111 return TRUE;
112}
113
114// Called when the value in the window must be validated.
115// This function can pop up an error message.
116bool wxTextValidator::Validate(wxWindow *parent)
117{
118 if ( !m_validatorWindow )
119 return FALSE;
120 if ( !m_validatorWindow->IsKindOf(CLASSINFO(wxTextCtrl)) )
121 return FALSE;
122 if ( !m_stringValue )
123 return FALSE;
124
125 wxTextCtrl *control = (wxTextCtrl *) m_validatorWindow ;
126
127 // If window is disabled, don't validate
128 if ( !control->Enabled() )
129 return FALSE;
130
131 wxString val(control->GetValue());
132
133 if ( m_validatorStyle & wxFILTER_INCLUDE_LIST )
134 {
135 if ( !m_includeList.Member(val) )
136 {
137 char buf[512];
1a5a8367
DP
138 sprintf(buf, _("%s is invalid."), (const char *)val);
139 wxMessageBox(buf, _("Validation conflict"), wxOK | wxICON_EXCLAMATION, parent);
c801d85f
KB
140 return FALSE;
141 }
142 }
143 if ( m_validatorStyle & wxFILTER_EXCLUDE_LIST )
144 {
145 if ( m_excludeList.Member(val) )
146 {
147 char buf[512];
1a5a8367
DP
148 sprintf(buf, _("%s is invalid."), (const char *)val);
149 wxMessageBox(buf, _("Validation conflict"), wxOK | wxICON_EXCLAMATION, parent);
c801d85f
KB
150 return FALSE;
151 }
152 }
153 if ( (m_validatorStyle & wxFILTER_ASCII) && !val.IsAscii() )
154 {
155 char buf[512];
1a5a8367
DP
156 sprintf(buf, _("%s should only contain ASCII characters."), (const char *)val);
157 wxMessageBox(buf, _("Validation conflict"), wxOK | wxICON_EXCLAMATION, parent);
c801d85f
KB
158 return FALSE;
159 }
160 if ( (m_validatorStyle & wxFILTER_ALPHA) && !wxIsAlpha(val) )
161 {
162 char buf[512];
1a5a8367
DP
163 sprintf(buf, _("%s should only contain alphabetic characters."), (const char *)val);
164 wxMessageBox(buf, _("Validation conflict"), wxOK | wxICON_EXCLAMATION, parent);
c801d85f
KB
165 return FALSE;
166 }
167 if ( (m_validatorStyle & wxFILTER_ALPHANUMERIC) && !wxIsAlphaNumeric(val))
168 {
169 char buf[512];
1a5a8367
DP
170 sprintf(buf, _("%s should only contain alphabetic or numeric characters."), (const char *)val);
171 wxMessageBox(buf,_("Validation conflict"), wxOK | wxICON_EXCLAMATION, parent);
c801d85f
KB
172 return FALSE;
173 }
386af6a2
JS
174 if ( (m_validatorStyle & wxFILTER_NUMERIC) && !wxIsNumeric(val))
175
c801d85f
KB
176 {
177 char buf[512];
1a5a8367
DP
178 sprintf(buf, _("%s should be numeric."), (const char *)val);
179 wxMessageBox(buf, _("Validation conflict"), wxOK | wxICON_EXCLAMATION, parent);
c801d85f
KB
180 return FALSE;
181 }
182
183 return TRUE ;
184}
185
186// Called to transfer data to the window
187bool wxTextValidator::TransferToWindow(void)
188{
189 if ( !m_validatorWindow )
190 return FALSE;
191 if ( !m_validatorWindow->IsKindOf(CLASSINFO(wxTextCtrl)) )
192 return FALSE;
193 if ( !m_stringValue )
194 return FALSE;
195
196 wxTextCtrl *control = (wxTextCtrl *) m_validatorWindow ;
197 control->SetValue(* m_stringValue) ;
198
199 return TRUE;
200}
201
202// Called to transfer data to the window
203bool wxTextValidator::TransferFromWindow(void)
204{
205 if ( !m_validatorWindow )
206 return FALSE;
207 if ( !m_validatorWindow->IsKindOf(CLASSINFO(wxTextCtrl)) )
208 return FALSE;
209 if ( !m_stringValue )
210 return FALSE;
211
212 wxTextCtrl *control = (wxTextCtrl *) m_validatorWindow ;
213 * m_stringValue = control->GetValue() ;
214
215 return TRUE;
216}
217
218void wxTextValidator::SetIncludeList(const wxStringList& list)
219{
220/*
221 if ( !M_VTEXTDATA )
222 return;
223*/
224
225 m_includeList.Clear();
226 // TODO: replace with =
227 wxNode *node = list.First() ;
228 while ( node )
229 {
230 char *s = (char *)node->Data();
231 m_includeList.Add(s);
232 node = node->Next();
233 }
234}
235
236void wxTextValidator::SetExcludeList(const wxStringList& list)
237{
238/*
239 if ( !M_VTEXTDATA )
240 return;
241*/
242
243 m_excludeList.Clear();
244 // TODO: replace with =
245 wxNode *node = list.First() ;
246 while ( node )
247 {
248 char *s = (char *)node->Data();
249 m_excludeList.Add(s);
250 node = node->Next();
251 }
252}
253
254void wxTextValidator::OnChar(wxKeyEvent& event)
255{
256/*
257 if ( !M_VTEXTDATA )
258 return;
259*/
260
261 if ( !m_validatorWindow )
262 return;
263
264 wxTextCtrl *textCtrl = (wxTextCtrl *)m_validatorWindow;
265
266 int keyCode = event.KeyCode();
267 if ( keyCode == WXK_DELETE || keyCode == WXK_RETURN || keyCode == WXK_BACK)
268 {
269 textCtrl->wxTextCtrl::OnChar(event);
270 return ;
271 }
272
273 if ( (m_validatorStyle & wxFILTER_ASCII) && !isascii(keyCode) )
274 {
275 wxBell();
276 return;
277 }
278 if ( (m_validatorStyle & wxFILTER_ALPHA) && !isalpha(keyCode) )
279 {
280 wxBell();
281 return;
282 }
283 if ( (m_validatorStyle & wxFILTER_ALPHANUMERIC) && !isalnum(keyCode) )
284 {
285 wxBell();
286 return;
287 }
288 if ( (m_validatorStyle & wxFILTER_NUMERIC) && !isdigit(keyCode) && keyCode != '.' )
289 {
290 wxBell();
291 return;
292 }
293
294 textCtrl->wxTextCtrl::OnChar(event);
295}
296
386af6a2
JS
297static bool wxIsNumeric(const wxString& val)
298{
299 int i;
300 for ( i = 0; i < (int)val.Length(); i++)
301 {
302 if ((!isdigit(val[i])) && (val[i] != '.'))
303 return FALSE;
304 }
305 return TRUE;
306}
c801d85f 307