]>
git.saurik.com Git - wxWidgets.git/blob - src/common/valtext.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxTextValidator
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "valtext.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
27 #include "wx/textctrl.h"
29 #include "wx/msgdlg.h"
33 #include "wx/valtext.h"
43 IMPLEMENT_DYNAMIC_CLASS(wxTextValidator
, wxValidator
)
45 BEGIN_EVENT_TABLE(wxTextValidator
, wxValidator
)
46 EVT_CHAR(wxTextValidator::OnChar
)
49 static bool wxIsNumeric(const wxString
& val
);
51 wxTextValidator::wxTextValidator(long style
, wxString
*val
)
53 m_validatorStyle
= style
;
56 m_refData = new wxVTextRefData;
58 M_VTEXTDATA->m_validatorStyle = style ;
59 M_VTEXTDATA->m_stringValue = val ;
63 wxTextValidator::wxTextValidator(const wxTextValidator
& val
)
68 bool wxTextValidator::Copy(const wxTextValidator
& val
)
70 wxValidator::Copy(val
);
72 m_validatorStyle
= val
.m_validatorStyle
;
73 m_stringValue
= val
.m_stringValue
;
75 wxNode
*node
= val
.m_includeList
.First() ;
78 wxChar
*s
= (wxChar
*)node
->Data();
82 node
= val
.m_excludeList
.First() ;
85 wxChar
*s
= (wxChar
*)node
->Data();
92 wxTextValidator::~wxTextValidator()
96 static bool wxIsAlpha(const wxString
& val
)
99 for ( i
= 0; i
< (int)val
.Length(); i
++)
101 if (!isalpha(val
[i
]))
107 static bool wxIsAlphaNumeric(const wxString
& val
)
110 for ( i
= 0; i
< (int)val
.Length(); i
++)
112 if (!isalnum(val
[i
]))
118 // Called when the value in the window must be validated.
119 // This function can pop up an error message.
120 bool wxTextValidator::Validate(wxWindow
*parent
)
122 if ( !m_validatorWindow
)
124 if ( !m_validatorWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)) )
126 if ( !m_stringValue
)
129 wxTextCtrl
*control
= (wxTextCtrl
*) m_validatorWindow
;
131 // If window is disabled, simply return
132 if ( !control
->IsEnabled() )
135 wxString
val(control
->GetValue());
139 // this format string should contian exactly one '%s'
140 const wxChar
*errormsg
= _("'%s' is invalid");
142 if ( m_validatorStyle
& wxFILTER_INCLUDE_LIST
)
144 if ( !m_includeList
.Member(val
) )
149 else if ( m_validatorStyle
& wxFILTER_EXCLUDE_LIST
)
151 if ( m_excludeList
.Member(val
) )
156 else if ( (m_validatorStyle
& wxFILTER_ASCII
) && !val
.IsAscii() )
160 errormsg
= _("'%s' should only contain ASCII characters.");
162 else if ( (m_validatorStyle
& wxFILTER_ALPHA
) && !wxIsAlpha(val
) )
166 errormsg
= _("'%s' should only contain alphabetic characters.");
168 else if ( (m_validatorStyle
& wxFILTER_ALPHANUMERIC
) && !wxIsAlphaNumeric(val
))
172 errormsg
= _("'%s' should only contain alphabetic or numeric characters.");
174 else if ( (m_validatorStyle
& wxFILTER_NUMERIC
) && !wxIsNumeric(val
))
178 errormsg
= _("'%s' should be numeric.");
183 m_validatorWindow
->SetFocus();
186 buf
.Printf(errormsg
, val
.c_str());
188 wxMessageBox(buf
, _("Validation conflict"),
189 wxOK
| wxICON_EXCLAMATION
, parent
);
195 // Called to transfer data to the window
196 bool wxTextValidator::TransferToWindow(void)
198 if ( !m_validatorWindow
)
200 if ( !m_validatorWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)) )
202 if ( !m_stringValue
)
205 wxTextCtrl
*control
= (wxTextCtrl
*) m_validatorWindow
;
206 control
->SetValue(* m_stringValue
) ;
211 // Called to transfer data to the window
212 bool wxTextValidator::TransferFromWindow(void)
214 if ( !m_validatorWindow
)
216 if ( !m_validatorWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)) )
218 if ( !m_stringValue
)
221 wxTextCtrl
*control
= (wxTextCtrl
*) m_validatorWindow
;
222 * m_stringValue
= control
->GetValue() ;
227 void wxTextValidator::SetIncludeList(const wxStringList
& list
)
234 m_includeList
.Clear();
235 // TODO: replace with =
236 wxNode
*node
= list
.First() ;
239 wxChar
*s
= (wxChar
*)node
->Data();
240 m_includeList
.Add(s
);
245 void wxTextValidator::SetExcludeList(const wxStringList
& list
)
252 m_excludeList
.Clear();
253 // TODO: replace with =
254 wxNode
*node
= list
.First() ;
257 wxChar
*s
= (wxChar
*)node
->Data();
258 m_excludeList
.Add(s
);
263 void wxTextValidator::OnChar(wxKeyEvent
& event
)
270 if ( m_validatorWindow
)
272 long keyCode
= event
.KeyCode();
274 // we don't filter special keys and Delete
276 !(keyCode
< WXK_SPACE
|| keyCode
== WXK_DELETE
|| keyCode
> WXK_START
) &&
278 ((m_validatorStyle
& wxFILTER_ASCII
) && !isascii(keyCode
)) ||
279 ((m_validatorStyle
& wxFILTER_ALPHA
) && !isalpha(keyCode
)) ||
280 ((m_validatorStyle
& wxFILTER_ALPHANUMERIC
) && !isalnum(keyCode
)) ||
281 ((m_validatorStyle
& wxFILTER_NUMERIC
) && !isdigit(keyCode
)
282 && keyCode
!= '.' && keyCode
!= ',' && keyCode
!= '-')
286 if ( !wxValidator::IsSilent() )
297 static bool wxIsNumeric(const wxString
& val
)
300 for ( i
= 0; i
< (int)val
.Length(); i
++)
302 // Allow for "," (French) as well as "." -- in future we should
303 // use wxSystemSettings or other to do better localisation
304 if ((!isdigit(val
[i
])) && (val
[i
] != '.') && (val
[i
] != ','))
305 if(!((i
== 0) && (val
[i
] == '-')))