]>
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"
31 #include "wx/textctrl.h"
33 #include "wx/msgdlg.h"
37 #include "wx/valtext.h"
47 #if !USE_SHARED_LIBRARY
48 IMPLEMENT_DYNAMIC_CLASS(wxTextValidator
, wxValidator
)
50 BEGIN_EVENT_TABLE(wxTextValidator
, wxValidator
)
51 EVT_CHAR(wxTextValidator::OnChar
)
55 static bool wxIsNumeric(const wxString
& val
);
57 wxTextValidator::wxTextValidator(long style
, wxString
*val
)
59 m_validatorStyle
= style
;
62 m_refData = new wxVTextRefData;
64 M_VTEXTDATA->m_validatorStyle = style ;
65 M_VTEXTDATA->m_stringValue = val ;
69 wxTextValidator::wxTextValidator(const wxTextValidator
& val
)
74 bool wxTextValidator::Copy(const wxTextValidator
& val
)
76 wxValidator::Copy(val
);
78 m_validatorStyle
= val
.m_validatorStyle
;
79 m_stringValue
= val
.m_stringValue
;
81 wxNode
*node
= val
.m_includeList
.First() ;
84 wxChar
*s
= (wxChar
*)node
->Data();
88 node
= val
.m_excludeList
.First() ;
91 wxChar
*s
= (wxChar
*)node
->Data();
98 wxTextValidator::~wxTextValidator()
102 static bool wxIsAlpha(const wxString
& val
)
105 for ( i
= 0; i
< (int)val
.Length(); i
++)
107 if (!isalpha(val
[i
]))
113 static bool wxIsAlphaNumeric(const wxString
& val
)
116 for ( i
= 0; i
< (int)val
.Length(); i
++)
118 if (!isalnum(val
[i
]))
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
)
128 if ( !m_validatorWindow
)
130 if ( !m_validatorWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)) )
132 if ( !m_stringValue
)
135 wxTextCtrl
*control
= (wxTextCtrl
*) m_validatorWindow
;
137 // If window is disabled, simply return
138 if ( !control
->IsEnabled() )
141 wxString
val(control
->GetValue());
145 // this format string should contian exactly one '%s'
146 const wxChar
*errormsg
= _("'%s' is invalid");
148 if ( m_validatorStyle
& wxFILTER_INCLUDE_LIST
)
150 if ( !m_includeList
.Member(val
) )
155 else if ( m_validatorStyle
& wxFILTER_EXCLUDE_LIST
)
157 if ( m_excludeList
.Member(val
) )
162 else if ( (m_validatorStyle
& wxFILTER_ASCII
) && !val
.IsAscii() )
166 errormsg
= _("'%s' should only contain ASCII characters.");
168 else if ( (m_validatorStyle
& wxFILTER_ALPHA
) && !wxIsAlpha(val
) )
172 errormsg
= _("'%s' should only contain alphabetic characters.");
174 else if ( (m_validatorStyle
& wxFILTER_ALPHANUMERIC
) && !wxIsAlphaNumeric(val
))
178 errormsg
= _("'%s' should only contain alphabetic or numeric characters.");
180 else if ( (m_validatorStyle
& wxFILTER_NUMERIC
) && !wxIsNumeric(val
))
184 errormsg
= _("'%s' should be numeric.");
189 m_validatorWindow
->SetFocus();
192 buf
.Printf(errormsg
, val
.c_str());
194 wxMessageBox(buf
, _("Validation conflict"),
195 wxOK
| wxICON_EXCLAMATION
, parent
);
201 // Called to transfer data to the window
202 bool wxTextValidator::TransferToWindow(void)
204 if ( !m_validatorWindow
)
206 if ( !m_validatorWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)) )
208 if ( !m_stringValue
)
211 wxTextCtrl
*control
= (wxTextCtrl
*) m_validatorWindow
;
212 control
->SetValue(* m_stringValue
) ;
217 // Called to transfer data to the window
218 bool wxTextValidator::TransferFromWindow(void)
220 if ( !m_validatorWindow
)
222 if ( !m_validatorWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)) )
224 if ( !m_stringValue
)
227 wxTextCtrl
*control
= (wxTextCtrl
*) m_validatorWindow
;
228 * m_stringValue
= control
->GetValue() ;
233 void wxTextValidator::SetIncludeList(const wxStringList
& list
)
240 m_includeList
.Clear();
241 // TODO: replace with =
242 wxNode
*node
= list
.First() ;
245 wxChar
*s
= (wxChar
*)node
->Data();
246 m_includeList
.Add(s
);
251 void wxTextValidator::SetExcludeList(const wxStringList
& list
)
258 m_excludeList
.Clear();
259 // TODO: replace with =
260 wxNode
*node
= list
.First() ;
263 wxChar
*s
= (wxChar
*)node
->Data();
264 m_excludeList
.Add(s
);
269 void wxTextValidator::OnChar(wxKeyEvent
& event
)
276 if ( m_validatorWindow
)
278 int keyCode
= event
.KeyCode();
280 // we don't filter special keys and Delete
282 !(keyCode
< WXK_SPACE
|| keyCode
== WXK_DELETE
|| keyCode
> WXK_START
) &&
284 ((m_validatorStyle
& wxFILTER_ASCII
) && !isascii(keyCode
)) ||
285 ((m_validatorStyle
& wxFILTER_ALPHA
) && !isalpha(keyCode
)) ||
286 ((m_validatorStyle
& wxFILTER_ALPHANUMERIC
) && !isalnum(keyCode
)) ||
287 ((m_validatorStyle
& wxFILTER_NUMERIC
) && !isdigit(keyCode
)
288 && keyCode
!= '.' && keyCode
!= '-')
292 if ( !wxValidator::IsSilent() )
303 static bool wxIsNumeric(const wxString
& val
)
306 for ( i
= 0; i
< (int)val
.Length(); i
++)
308 if ((!isdigit(val
[i
])) && (val
[i
] != '.'))
309 if(!((i
== 0) && (val
[i
] == '-')))