]>
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"
41 #if !USE_SHARED_LIBRARY
42 IMPLEMENT_DYNAMIC_CLASS(wxTextValidator
, wxValidator
)
44 BEGIN_EVENT_TABLE(wxTextValidator
, wxValidator
)
45 EVT_CHAR(wxTextValidator::OnChar
)
49 static bool wxIsNumeric(const wxString
& val
);
51 wxTextValidator::wxTextValidator(long style
, wxString
*val
)
53 m_validatorStyle
= style
;
56 m_refData = new wxVTextRefData;
58 M_VTEXTDATA->m_validatorStyle = style ;
59 M_VTEXTDATA->m_stringValue = val ;
63 wxTextValidator::wxTextValidator(const wxTextValidator
& val
)
68 bool wxTextValidator::Copy(const wxTextValidator
& val
)
70 wxValidator::Copy(val
);
72 m_validatorStyle
= val
.m_validatorStyle
;
73 m_stringValue
= val
.m_stringValue
;
75 wxNode
*node
= val
.m_includeList
.First() ;
78 char *s
= (char *)node
->Data();
82 node
= val
.m_excludeList
.First() ;
85 char *s
= (char *)node
->Data();
92 wxTextValidator::~wxTextValidator()
96 static bool wxIsAlpha(const wxString
& val
)
99 for ( i
= 0; i
< (int)val
.Length(); i
++)
101 if (!isalpha(val
[i
]))
107 static bool wxIsAlphaNumeric(const wxString
& val
)
110 for ( i
= 0; i
< (int)val
.Length(); i
++)
112 if (!isalnum(val
[i
]))
118 // Called when the value in the window must be validated.
119 // This function can pop up an error message.
120 bool wxTextValidator::Validate(wxWindow
*parent
)
122 if ( !m_validatorWindow
)
124 if ( !m_validatorWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)) )
126 if ( !m_stringValue
)
129 wxTextCtrl
*control
= (wxTextCtrl
*) m_validatorWindow
;
131 // If window is disabled, don't validate
132 if ( !control
->Enabled() )
135 wxString
val(control
->GetValue());
137 if ( m_validatorStyle
& wxFILTER_INCLUDE_LIST
)
139 if ( !m_includeList
.Member(val
) )
141 m_validatorWindow
->SetFocus();
143 sprintf(buf
, _("%s is invalid."), (const char *)val
);
144 wxMessageBox(buf
, _("Validation conflict"), wxOK
| wxICON_EXCLAMATION
, parent
);
148 if ( m_validatorStyle
& wxFILTER_EXCLUDE_LIST
)
150 if ( m_excludeList
.Member(val
) )
152 m_validatorWindow
->SetFocus();
154 sprintf(buf
, _("%s is invalid."), (const char *)val
);
155 wxMessageBox(buf
, _("Validation conflict"), wxOK
| wxICON_EXCLAMATION
, parent
);
159 if ( (m_validatorStyle
& wxFILTER_ASCII
) && !val
.IsAscii() )
161 m_validatorWindow
->SetFocus();
163 sprintf(buf
, _("%s should only contain ASCII characters."), (const char *)val
);
164 wxMessageBox(buf
, _("Validation conflict"), wxOK
| wxICON_EXCLAMATION
, parent
);
167 if ( (m_validatorStyle
& wxFILTER_ALPHA
) && !wxIsAlpha(val
) )
169 m_validatorWindow
->SetFocus();
171 sprintf(buf
, _("%s should only contain alphabetic characters."), (const char *)val
);
172 wxMessageBox(buf
, _("Validation conflict"), wxOK
| wxICON_EXCLAMATION
, parent
);
175 if ( (m_validatorStyle
& wxFILTER_ALPHANUMERIC
) && !wxIsAlphaNumeric(val
))
177 m_validatorWindow
->SetFocus();
179 sprintf(buf
, _("%s should only contain alphabetic or numeric characters."), (const char *)val
);
180 wxMessageBox(buf
,_("Validation conflict"), wxOK
| wxICON_EXCLAMATION
, parent
);
183 if ( (m_validatorStyle
& wxFILTER_NUMERIC
) && !wxIsNumeric(val
))
186 m_validatorWindow
->SetFocus();
188 sprintf(buf
, _("%s should be numeric."), (const char *)val
);
189 wxMessageBox(buf
, _("Validation conflict"), wxOK
| wxICON_EXCLAMATION
, parent
);
196 // Called to transfer data to the window
197 bool wxTextValidator::TransferToWindow(void)
199 if ( !m_validatorWindow
)
201 if ( !m_validatorWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)) )
203 if ( !m_stringValue
)
206 wxTextCtrl
*control
= (wxTextCtrl
*) m_validatorWindow
;
207 control
->SetValue(* m_stringValue
) ;
212 // Called to transfer data to the window
213 bool wxTextValidator::TransferFromWindow(void)
215 if ( !m_validatorWindow
)
217 if ( !m_validatorWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)) )
219 if ( !m_stringValue
)
222 wxTextCtrl
*control
= (wxTextCtrl
*) m_validatorWindow
;
223 * m_stringValue
= control
->GetValue() ;
228 void wxTextValidator::SetIncludeList(const wxStringList
& list
)
235 m_includeList
.Clear();
236 // TODO: replace with =
237 wxNode
*node
= list
.First() ;
240 char *s
= (char *)node
->Data();
241 m_includeList
.Add(s
);
246 void wxTextValidator::SetExcludeList(const wxStringList
& list
)
253 m_excludeList
.Clear();
254 // TODO: replace with =
255 wxNode
*node
= list
.First() ;
258 char *s
= (char *)node
->Data();
259 m_excludeList
.Add(s
);
264 void wxTextValidator::OnChar(wxKeyEvent
& event
)
271 if ( !m_validatorWindow
)
274 wxTextCtrl
*textCtrl
= (wxTextCtrl
*)m_validatorWindow
;
276 int keyCode
= event
.KeyCode();
277 if (keyCode
== WXK_DELETE
|| keyCode
== WXK_RETURN
|| keyCode
== WXK_BACK
||
278 keyCode
== WXK_HOME
|| keyCode
== WXK_LEFT
|| keyCode
== WXK_UP
||
279 keyCode
== WXK_RIGHT
|| keyCode
== WXK_DOWN
|| keyCode
== WXK_PRIOR
||
280 keyCode
== WXK_NEXT
|| keyCode
== WXK_END
|| keyCode
== WXK_HOME
)
282 textCtrl
->wxTextCtrl::OnChar(event
);
286 if ( (m_validatorStyle
& wxFILTER_ASCII
) && !isascii(keyCode
) )
291 if ( (m_validatorStyle
& wxFILTER_ALPHA
) && !isalpha(keyCode
) )
296 if ( (m_validatorStyle
& wxFILTER_ALPHANUMERIC
) && !isalnum(keyCode
) )
301 if ( (m_validatorStyle
& wxFILTER_NUMERIC
) && !isdigit(keyCode
) && keyCode
!= '.' && keyCode
!= '-')
307 textCtrl
->wxTextCtrl::OnChar(event
);
310 static bool wxIsNumeric(const wxString
& val
)
313 for ( i
= 0; i
< (int)val
.Length(); i
++)
315 if ((!isdigit(val
[i
])) && (val
[i
] != '.'))
316 if(!((i
== 0) && (val
[i
] == '-')))