]>
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/msgdlg.h"
31 #include "wx/valtext.h"
37 #if !USE_SHARED_LIBRARY
38 IMPLEMENT_DYNAMIC_CLASS(wxTextValidator
, wxValidator
)
40 BEGIN_EVENT_TABLE(wxTextValidator
, wxValidator
)
41 EVT_CHAR(wxTextValidator::OnChar
)
45 wxTextValidator::wxTextValidator(long style
, wxString
*val
)
47 m_validatorStyle
= style
;
50 m_refData = new wxVTextRefData;
52 M_VTEXTDATA->m_validatorStyle = style ;
53 M_VTEXTDATA->m_stringValue = val ;
57 wxTextValidator::wxTextValidator(const wxTextValidator
& val
)
62 bool wxTextValidator::Copy(const wxTextValidator
& val
)
64 wxValidator::Copy(val
);
66 m_validatorStyle
= val
.m_validatorStyle
;
67 m_stringValue
= val
.m_stringValue
;
69 wxNode
*node
= val
.m_includeList
.First() ;
72 char *s
= (char *)node
->Data();
76 node
= val
.m_excludeList
.First() ;
79 char *s
= (char *)node
->Data();
86 wxTextValidator::~wxTextValidator()
90 static bool wxIsAlpha(const wxString
& val
)
93 for ( i
= 0; i
< (int)val
.Length(); i
++)
101 static bool wxIsAlphaNumeric(const wxString
& val
)
104 for ( i
= 0; i
< (int)val
.Length(); i
++)
106 if (!isalnum(val
[i
]))
112 // Called when the value in the window must be validated.
113 // This function can pop up an error message.
114 bool wxTextValidator::Validate(wxWindow
*parent
)
116 if ( !m_validatorWindow
)
118 if ( !m_validatorWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)) )
120 if ( !m_stringValue
)
123 wxTextCtrl
*control
= (wxTextCtrl
*) m_validatorWindow
;
125 // If window is disabled, don't validate
126 if ( !control
->Enabled() )
129 wxString
val(control
->GetValue());
131 if ( m_validatorStyle
& wxFILTER_INCLUDE_LIST
)
133 if ( !m_includeList
.Member(val
) )
136 sprintf(buf
, _("%s is invalid."), (const char *)val
);
137 wxMessageBox(buf
, _("Validation conflict"), wxOK
| wxICON_EXCLAMATION
, parent
);
141 if ( m_validatorStyle
& wxFILTER_EXCLUDE_LIST
)
143 if ( m_excludeList
.Member(val
) )
146 sprintf(buf
, _("%s is invalid."), (const char *)val
);
147 wxMessageBox(buf
, _("Validation conflict"), wxOK
| wxICON_EXCLAMATION
, parent
);
151 if ( (m_validatorStyle
& wxFILTER_ASCII
) && !val
.IsAscii() )
154 sprintf(buf
, _("%s should only contain ASCII characters."), (const char *)val
);
155 wxMessageBox(buf
, _("Validation conflict"), wxOK
| wxICON_EXCLAMATION
, parent
);
158 if ( (m_validatorStyle
& wxFILTER_ALPHA
) && !wxIsAlpha(val
) )
161 sprintf(buf
, _("%s should only contain alphabetic characters."), (const char *)val
);
162 wxMessageBox(buf
, _("Validation conflict"), wxOK
| wxICON_EXCLAMATION
, parent
);
165 if ( (m_validatorStyle
& wxFILTER_ALPHANUMERIC
) && !wxIsAlphaNumeric(val
))
168 sprintf(buf
, _("%s should only contain alphabetic or numeric characters."), (const char *)val
);
169 wxMessageBox(buf
,_("Validation conflict"), wxOK
| wxICON_EXCLAMATION
, parent
);
172 if ( (m_validatorStyle
& wxFILTER_NUMERIC
) && !val
.IsNumber())
175 sprintf(buf
, _("%s should be numeric."), (const char *)val
);
176 wxMessageBox(buf
, _("Validation conflict"), wxOK
| wxICON_EXCLAMATION
, parent
);
183 // Called to transfer data to the window
184 bool wxTextValidator::TransferToWindow(void)
186 if ( !m_validatorWindow
)
188 if ( !m_validatorWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)) )
190 if ( !m_stringValue
)
193 wxTextCtrl
*control
= (wxTextCtrl
*) m_validatorWindow
;
194 control
->SetValue(* m_stringValue
) ;
199 // Called to transfer data to the window
200 bool wxTextValidator::TransferFromWindow(void)
202 if ( !m_validatorWindow
)
204 if ( !m_validatorWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)) )
206 if ( !m_stringValue
)
209 wxTextCtrl
*control
= (wxTextCtrl
*) m_validatorWindow
;
210 * m_stringValue
= control
->GetValue() ;
215 void wxTextValidator::SetIncludeList(const wxStringList
& list
)
222 m_includeList
.Clear();
223 // TODO: replace with =
224 wxNode
*node
= list
.First() ;
227 char *s
= (char *)node
->Data();
228 m_includeList
.Add(s
);
233 void wxTextValidator::SetExcludeList(const wxStringList
& list
)
240 m_excludeList
.Clear();
241 // TODO: replace with =
242 wxNode
*node
= list
.First() ;
245 char *s
= (char *)node
->Data();
246 m_excludeList
.Add(s
);
251 void wxTextValidator::OnChar(wxKeyEvent
& event
)
258 if ( !m_validatorWindow
)
261 wxTextCtrl
*textCtrl
= (wxTextCtrl
*)m_validatorWindow
;
263 int keyCode
= event
.KeyCode();
264 if ( keyCode
== WXK_DELETE
|| keyCode
== WXK_RETURN
|| keyCode
== WXK_BACK
)
266 textCtrl
->wxTextCtrl::OnChar(event
);
270 if ( (m_validatorStyle
& wxFILTER_ASCII
) && !isascii(keyCode
) )
275 if ( (m_validatorStyle
& wxFILTER_ALPHA
) && !isalpha(keyCode
) )
280 if ( (m_validatorStyle
& wxFILTER_ALPHANUMERIC
) && !isalnum(keyCode
) )
285 if ( (m_validatorStyle
& wxFILTER_NUMERIC
) && !isdigit(keyCode
) && keyCode
!= '.' )
291 textCtrl
->wxTextCtrl::OnChar(event
);