]>
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
) )
138 sprintf(buf
, _("%s is invalid."), (const char *)val
);
139 wxMessageBox(buf
, _("Validation conflict"), wxOK
| wxICON_EXCLAMATION
, parent
);
143 if ( m_validatorStyle
& wxFILTER_EXCLUDE_LIST
)
145 if ( m_excludeList
.Member(val
) )
148 sprintf(buf
, _("%s is invalid."), (const char *)val
);
149 wxMessageBox(buf
, _("Validation conflict"), wxOK
| wxICON_EXCLAMATION
, parent
);
153 if ( (m_validatorStyle
& wxFILTER_ASCII
) && !val
.IsAscii() )
156 sprintf(buf
, _("%s should only contain ASCII characters."), (const char *)val
);
157 wxMessageBox(buf
, _("Validation conflict"), wxOK
| wxICON_EXCLAMATION
, parent
);
160 if ( (m_validatorStyle
& wxFILTER_ALPHA
) && !wxIsAlpha(val
) )
163 sprintf(buf
, _("%s should only contain alphabetic characters."), (const char *)val
);
164 wxMessageBox(buf
, _("Validation conflict"), wxOK
| wxICON_EXCLAMATION
, parent
);
167 if ( (m_validatorStyle
& wxFILTER_ALPHANUMERIC
) && !wxIsAlphaNumeric(val
))
170 sprintf(buf
, _("%s should only contain alphabetic or numeric characters."), (const char *)val
);
171 wxMessageBox(buf
,_("Validation conflict"), wxOK
| wxICON_EXCLAMATION
, parent
);
174 if ( (m_validatorStyle
& wxFILTER_NUMERIC
) && !wxIsNumeric(val
))
178 sprintf(buf
, _("%s should be numeric."), (const char *)val
);
179 wxMessageBox(buf
, _("Validation conflict"), wxOK
| wxICON_EXCLAMATION
, parent
);
186 // Called to transfer data to the window
187 bool wxTextValidator::TransferToWindow(void)
189 if ( !m_validatorWindow
)
191 if ( !m_validatorWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)) )
193 if ( !m_stringValue
)
196 wxTextCtrl
*control
= (wxTextCtrl
*) m_validatorWindow
;
197 control
->SetValue(* m_stringValue
) ;
202 // Called to transfer data to the window
203 bool wxTextValidator::TransferFromWindow(void)
205 if ( !m_validatorWindow
)
207 if ( !m_validatorWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)) )
209 if ( !m_stringValue
)
212 wxTextCtrl
*control
= (wxTextCtrl
*) m_validatorWindow
;
213 * m_stringValue
= control
->GetValue() ;
218 void wxTextValidator::SetIncludeList(const wxStringList
& list
)
225 m_includeList
.Clear();
226 // TODO: replace with =
227 wxNode
*node
= list
.First() ;
230 char *s
= (char *)node
->Data();
231 m_includeList
.Add(s
);
236 void wxTextValidator::SetExcludeList(const wxStringList
& list
)
243 m_excludeList
.Clear();
244 // TODO: replace with =
245 wxNode
*node
= list
.First() ;
248 char *s
= (char *)node
->Data();
249 m_excludeList
.Add(s
);
254 void wxTextValidator::OnChar(wxKeyEvent
& event
)
261 if ( !m_validatorWindow
)
264 wxTextCtrl
*textCtrl
= (wxTextCtrl
*)m_validatorWindow
;
266 int keyCode
= event
.KeyCode();
267 if ( keyCode
== WXK_DELETE
|| keyCode
== WXK_RETURN
|| keyCode
== WXK_BACK
)
269 textCtrl
->wxTextCtrl::OnChar(event
);
273 if ( (m_validatorStyle
& wxFILTER_ASCII
) && !isascii(keyCode
) )
278 if ( (m_validatorStyle
& wxFILTER_ALPHA
) && !isalpha(keyCode
) )
283 if ( (m_validatorStyle
& wxFILTER_ALPHANUMERIC
) && !isalnum(keyCode
) )
288 if ( (m_validatorStyle
& wxFILTER_NUMERIC
) && !isdigit(keyCode
) && keyCode
!= '.' )
294 textCtrl
->wxTextCtrl::OnChar(event
);
297 static bool wxIsNumeric(const wxString
& val
)
300 for ( i
= 0; i
< (int)val
.Length(); i
++)
302 if ((!isdigit(val
[i
])) && (val
[i
] != '.'))