]>
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 static bool wxIsNumeric(const wxString
& val
);
47 wxTextValidator::wxTextValidator(long style
, wxString
*val
)
49 m_validatorStyle
= style
;
52 m_refData = new wxVTextRefData;
54 M_VTEXTDATA->m_validatorStyle = style ;
55 M_VTEXTDATA->m_stringValue = val ;
59 wxTextValidator::wxTextValidator(const wxTextValidator
& val
)
64 bool wxTextValidator::Copy(const wxTextValidator
& val
)
66 wxValidator::Copy(val
);
68 m_validatorStyle
= val
.m_validatorStyle
;
69 m_stringValue
= val
.m_stringValue
;
71 wxNode
*node
= val
.m_includeList
.First() ;
74 char *s
= (char *)node
->Data();
78 node
= val
.m_excludeList
.First() ;
81 char *s
= (char *)node
->Data();
88 wxTextValidator::~wxTextValidator()
92 static bool wxIsAlpha(const wxString
& val
)
95 for ( i
= 0; i
< (int)val
.Length(); i
++)
103 static bool wxIsAlphaNumeric(const wxString
& val
)
106 for ( i
= 0; i
< (int)val
.Length(); i
++)
108 if (!isalnum(val
[i
]))
114 // Called when the value in the window must be validated.
115 // This function can pop up an error message.
116 bool wxTextValidator::Validate(wxWindow
*parent
)
118 if ( !m_validatorWindow
)
120 if ( !m_validatorWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)) )
122 if ( !m_stringValue
)
125 wxTextCtrl
*control
= (wxTextCtrl
*) m_validatorWindow
;
127 // If window is disabled, don't validate
128 if ( !control
->Enabled() )
131 wxString
val(control
->GetValue());
133 if ( m_validatorStyle
& wxFILTER_INCLUDE_LIST
)
135 if ( !m_includeList
.Member(val
) )
137 m_validatorWindow
->SetFocus();
139 sprintf(buf
, _("%s is invalid."), (const char *)val
);
140 wxMessageBox(buf
, _("Validation conflict"), wxOK
| wxICON_EXCLAMATION
, parent
);
144 if ( m_validatorStyle
& wxFILTER_EXCLUDE_LIST
)
146 if ( m_excludeList
.Member(val
) )
148 m_validatorWindow
->SetFocus();
150 sprintf(buf
, _("%s is invalid."), (const char *)val
);
151 wxMessageBox(buf
, _("Validation conflict"), wxOK
| wxICON_EXCLAMATION
, parent
);
155 if ( (m_validatorStyle
& wxFILTER_ASCII
) && !val
.IsAscii() )
157 m_validatorWindow
->SetFocus();
159 sprintf(buf
, _("%s should only contain ASCII characters."), (const char *)val
);
160 wxMessageBox(buf
, _("Validation conflict"), wxOK
| wxICON_EXCLAMATION
, parent
);
163 if ( (m_validatorStyle
& wxFILTER_ALPHA
) && !wxIsAlpha(val
) )
165 m_validatorWindow
->SetFocus();
167 sprintf(buf
, _("%s should only contain alphabetic characters."), (const char *)val
);
168 wxMessageBox(buf
, _("Validation conflict"), wxOK
| wxICON_EXCLAMATION
, parent
);
171 if ( (m_validatorStyle
& wxFILTER_ALPHANUMERIC
) && !wxIsAlphaNumeric(val
))
173 m_validatorWindow
->SetFocus();
175 sprintf(buf
, _("%s should only contain alphabetic or numeric characters."), (const char *)val
);
176 wxMessageBox(buf
,_("Validation conflict"), wxOK
| wxICON_EXCLAMATION
, parent
);
179 if ( (m_validatorStyle
& wxFILTER_NUMERIC
) && !wxIsNumeric(val
))
182 m_validatorWindow
->SetFocus();
184 sprintf(buf
, _("%s should be numeric."), (const char *)val
);
185 wxMessageBox(buf
, _("Validation conflict"), wxOK
| wxICON_EXCLAMATION
, parent
);
192 // Called to transfer data to the window
193 bool wxTextValidator::TransferToWindow(void)
195 if ( !m_validatorWindow
)
197 if ( !m_validatorWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)) )
199 if ( !m_stringValue
)
202 wxTextCtrl
*control
= (wxTextCtrl
*) m_validatorWindow
;
203 control
->SetValue(* m_stringValue
) ;
208 // Called to transfer data to the window
209 bool wxTextValidator::TransferFromWindow(void)
211 if ( !m_validatorWindow
)
213 if ( !m_validatorWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)) )
215 if ( !m_stringValue
)
218 wxTextCtrl
*control
= (wxTextCtrl
*) m_validatorWindow
;
219 * m_stringValue
= control
->GetValue() ;
224 void wxTextValidator::SetIncludeList(const wxStringList
& list
)
231 m_includeList
.Clear();
232 // TODO: replace with =
233 wxNode
*node
= list
.First() ;
236 char *s
= (char *)node
->Data();
237 m_includeList
.Add(s
);
242 void wxTextValidator::SetExcludeList(const wxStringList
& list
)
249 m_excludeList
.Clear();
250 // TODO: replace with =
251 wxNode
*node
= list
.First() ;
254 char *s
= (char *)node
->Data();
255 m_excludeList
.Add(s
);
260 void wxTextValidator::OnChar(wxKeyEvent
& event
)
267 if ( !m_validatorWindow
)
270 wxTextCtrl
*textCtrl
= (wxTextCtrl
*)m_validatorWindow
;
272 int keyCode
= event
.KeyCode();
273 if (keyCode
== WXK_DELETE
|| keyCode
== WXK_RETURN
|| keyCode
== WXK_BACK
||
274 keyCode
== WXK_HOME
|| keyCode
== WXK_LEFT
|| keyCode
== WXK_UP
||
275 keyCode
== WXK_RIGHT
|| keyCode
== WXK_DOWN
|| keyCode
== WXK_PRIOR
||
276 keyCode
== WXK_NEXT
|| keyCode
== WXK_END
|| keyCode
== WXK_HOME
)
278 textCtrl
->wxTextCtrl::OnChar(event
);
282 if ( (m_validatorStyle
& wxFILTER_ASCII
) && !isascii(keyCode
) )
287 if ( (m_validatorStyle
& wxFILTER_ALPHA
) && !isalpha(keyCode
) )
292 if ( (m_validatorStyle
& wxFILTER_ALPHANUMERIC
) && !isalnum(keyCode
) )
297 if ( (m_validatorStyle
& wxFILTER_NUMERIC
) && !isdigit(keyCode
) && keyCode
!= '.' )
303 textCtrl
->wxTextCtrl::OnChar(event
);
306 static bool wxIsNumeric(const wxString
& val
)
309 for ( i
= 0; i
< (int)val
.Length(); i
++)
311 if ((!isdigit(val
[i
])) && (val
[i
] != '.'))