]>
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"
25 #include "wx/textctrl.h"
27 #include "wx/msgbxdlg.h"
30 #include "wx/valtext.h"
36 #if !USE_SHARED_LIBRARY
37 IMPLEMENT_DYNAMIC_CLASS(wxTextValidator
, wxValidator
)
39 BEGIN_EVENT_TABLE(wxTextValidator
, wxValidator
)
40 EVT_CHAR(wxTextValidator::OnChar
)
44 wxTextValidator::wxTextValidator(long style
, wxString
*val
)
46 m_validatorStyle
= style
;
49 m_refData = new wxVTextRefData;
51 M_VTEXTDATA->m_validatorStyle = style ;
52 M_VTEXTDATA->m_stringValue = val ;
56 wxTextValidator::wxTextValidator(const wxTextValidator
& val
)
61 bool wxTextValidator::Copy(const wxTextValidator
& val
)
63 wxValidator::Copy(val
);
65 m_validatorStyle
= val
.m_validatorStyle
;
66 m_stringValue
= val
.m_stringValue
;
68 wxNode
*node
= val
.m_includeList
.First() ;
71 char *s
= (char *)node
->Data();
75 node
= val
.m_excludeList
.First() ;
78 char *s
= (char *)node
->Data();
85 wxTextValidator::~wxTextValidator()
89 static bool wxIsAlpha(const wxString
& val
)
92 for ( i
= 0; i
< (int)val
.Length(); i
++)
100 static bool wxIsAlphaNumeric(const wxString
& val
)
103 for ( i
= 0; i
< (int)val
.Length(); i
++)
105 if (!isalnum(val
[i
]))
111 // Called when the value in the window must be validated.
112 // This function can pop up an error message.
113 bool wxTextValidator::Validate(wxWindow
*parent
)
115 if ( !m_validatorWindow
)
117 if ( !m_validatorWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)) )
119 if ( !m_stringValue
)
122 wxTextCtrl
*control
= (wxTextCtrl
*) m_validatorWindow
;
124 // If window is disabled, don't validate
125 if ( !control
->Enabled() )
128 wxString
val(control
->GetValue());
130 if ( m_validatorStyle
& wxFILTER_INCLUDE_LIST
)
132 if ( !m_includeList
.Member(val
) )
135 sprintf(buf
, _("%s is invalid."), (const char *)val
);
136 wxMessageBox(buf
, _("Validation conflict"), wxOK
| wxICON_EXCLAMATION
, parent
);
140 if ( m_validatorStyle
& wxFILTER_EXCLUDE_LIST
)
142 if ( m_excludeList
.Member(val
) )
145 sprintf(buf
, _("%s is invalid."), (const char *)val
);
146 wxMessageBox(buf
, _("Validation conflict"), wxOK
| wxICON_EXCLAMATION
, parent
);
150 if ( (m_validatorStyle
& wxFILTER_ASCII
) && !val
.IsAscii() )
153 sprintf(buf
, _("%s should only contain ASCII characters."), (const char *)val
);
154 wxMessageBox(buf
, _("Validation conflict"), wxOK
| wxICON_EXCLAMATION
, parent
);
157 if ( (m_validatorStyle
& wxFILTER_ALPHA
) && !wxIsAlpha(val
) )
160 sprintf(buf
, _("%s should only contain alphabetic characters."), (const char *)val
);
161 wxMessageBox(buf
, _("Validation conflict"), wxOK
| wxICON_EXCLAMATION
, parent
);
164 if ( (m_validatorStyle
& wxFILTER_ALPHANUMERIC
) && !wxIsAlphaNumeric(val
))
167 sprintf(buf
, _("%s should only contain alphabetic or numeric characters."), (const char *)val
);
168 wxMessageBox(buf
,_("Validation conflict"), wxOK
| wxICON_EXCLAMATION
, parent
);
171 if ( (m_validatorStyle
& wxFILTER_NUMERIC
) && !val
.IsNumber())
174 sprintf(buf
, _("%s should be numeric."), (const char *)val
);
175 wxMessageBox(buf
, _("Validation conflict"), wxOK
| wxICON_EXCLAMATION
, parent
);
182 // Called to transfer data to the window
183 bool wxTextValidator::TransferToWindow(void)
185 if ( !m_validatorWindow
)
187 if ( !m_validatorWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)) )
189 if ( !m_stringValue
)
192 wxTextCtrl
*control
= (wxTextCtrl
*) m_validatorWindow
;
193 control
->SetValue(* m_stringValue
) ;
198 // Called to transfer data to the window
199 bool wxTextValidator::TransferFromWindow(void)
201 if ( !m_validatorWindow
)
203 if ( !m_validatorWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)) )
205 if ( !m_stringValue
)
208 wxTextCtrl
*control
= (wxTextCtrl
*) m_validatorWindow
;
209 * m_stringValue
= control
->GetValue() ;
214 void wxTextValidator::SetIncludeList(const wxStringList
& list
)
221 m_includeList
.Clear();
222 // TODO: replace with =
223 wxNode
*node
= list
.First() ;
226 char *s
= (char *)node
->Data();
227 m_includeList
.Add(s
);
232 void wxTextValidator::SetExcludeList(const wxStringList
& list
)
239 m_excludeList
.Clear();
240 // TODO: replace with =
241 wxNode
*node
= list
.First() ;
244 char *s
= (char *)node
->Data();
245 m_excludeList
.Add(s
);
250 void wxTextValidator::OnChar(wxKeyEvent
& event
)
257 if ( !m_validatorWindow
)
260 wxTextCtrl
*textCtrl
= (wxTextCtrl
*)m_validatorWindow
;
262 int keyCode
= event
.KeyCode();
263 if ( keyCode
== WXK_DELETE
|| keyCode
== WXK_RETURN
|| keyCode
== WXK_BACK
)
265 textCtrl
->wxTextCtrl::OnChar(event
);
269 if ( (m_validatorStyle
& wxFILTER_ASCII
) && !isascii(keyCode
) )
274 if ( (m_validatorStyle
& wxFILTER_ALPHA
) && !isalpha(keyCode
) )
279 if ( (m_validatorStyle
& wxFILTER_ALPHANUMERIC
) && !isalnum(keyCode
) )
284 if ( (m_validatorStyle
& wxFILTER_NUMERIC
) && !isdigit(keyCode
) && keyCode
!= '.' )
290 textCtrl
->wxTextCtrl::OnChar(event
);