]>
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"
27 #include "wx/textctrl.h"
29 #include "wx/msgdlg.h"
33 #include "wx/valtext.h"
43 #if !USE_SHARED_LIBRARY
44 IMPLEMENT_DYNAMIC_CLASS(wxTextValidator
, wxValidator
)
46 BEGIN_EVENT_TABLE(wxTextValidator
, wxValidator
)
47 EVT_CHAR(wxTextValidator::OnChar
)
51 static bool wxIsNumeric(const wxString
& val
);
53 wxTextValidator::wxTextValidator(long style
, wxString
*val
)
55 m_validatorStyle
= style
;
58 m_refData = new wxVTextRefData;
60 M_VTEXTDATA->m_validatorStyle = style ;
61 M_VTEXTDATA->m_stringValue = val ;
65 wxTextValidator::wxTextValidator(const wxTextValidator
& val
)
70 bool wxTextValidator::Copy(const wxTextValidator
& val
)
72 wxValidator::Copy(val
);
74 m_validatorStyle
= val
.m_validatorStyle
;
75 m_stringValue
= val
.m_stringValue
;
77 wxNode
*node
= val
.m_includeList
.First() ;
80 wxChar
*s
= (wxChar
*)node
->Data();
84 node
= val
.m_excludeList
.First() ;
87 wxChar
*s
= (wxChar
*)node
->Data();
94 wxTextValidator::~wxTextValidator()
98 static bool wxIsAlpha(const wxString
& val
)
101 for ( i
= 0; i
< (int)val
.Length(); i
++)
103 if (!isalpha(val
[i
]))
109 static bool wxIsAlphaNumeric(const wxString
& val
)
112 for ( i
= 0; i
< (int)val
.Length(); i
++)
114 if (!isalnum(val
[i
]))
120 // Called when the value in the window must be validated.
121 // This function can pop up an error message.
122 bool wxTextValidator::Validate(wxWindow
*parent
)
124 if ( !m_validatorWindow
)
126 if ( !m_validatorWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)) )
128 if ( !m_stringValue
)
131 wxTextCtrl
*control
= (wxTextCtrl
*) m_validatorWindow
;
133 // If window is disabled, simply return
134 if ( !control
->IsEnabled() )
137 wxString
val(control
->GetValue());
141 // this format string should contian exactly one '%s'
142 const wxChar
*errormsg
= _("'%s' is invalid");
144 if ( m_validatorStyle
& wxFILTER_INCLUDE_LIST
)
146 if ( !m_includeList
.Member(val
) )
151 else if ( m_validatorStyle
& wxFILTER_EXCLUDE_LIST
)
153 if ( m_excludeList
.Member(val
) )
158 else if ( (m_validatorStyle
& wxFILTER_ASCII
) && !val
.IsAscii() )
162 errormsg
= _("'%s' should only contain ASCII characters.");
164 else if ( (m_validatorStyle
& wxFILTER_ALPHA
) && !wxIsAlpha(val
) )
168 errormsg
= _("'%s' should only contain alphabetic characters.");
170 else if ( (m_validatorStyle
& wxFILTER_ALPHANUMERIC
) && !wxIsAlphaNumeric(val
))
174 errormsg
= _("'%s' should only contain alphabetic or numeric characters.");
176 else if ( (m_validatorStyle
& wxFILTER_NUMERIC
) && !wxIsNumeric(val
))
180 errormsg
= _("'%s' should be numeric.");
185 m_validatorWindow
->SetFocus();
188 buf
.Printf(errormsg
, val
.c_str());
190 wxMessageBox(buf
, _("Validation conflict"),
191 wxOK
| wxICON_EXCLAMATION
, parent
);
197 // Called to transfer data to the window
198 bool wxTextValidator::TransferToWindow(void)
200 if ( !m_validatorWindow
)
202 if ( !m_validatorWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)) )
204 if ( !m_stringValue
)
207 wxTextCtrl
*control
= (wxTextCtrl
*) m_validatorWindow
;
208 control
->SetValue(* m_stringValue
) ;
213 // Called to transfer data to the window
214 bool wxTextValidator::TransferFromWindow(void)
216 if ( !m_validatorWindow
)
218 if ( !m_validatorWindow
->IsKindOf(CLASSINFO(wxTextCtrl
)) )
220 if ( !m_stringValue
)
223 wxTextCtrl
*control
= (wxTextCtrl
*) m_validatorWindow
;
224 * m_stringValue
= control
->GetValue() ;
229 void wxTextValidator::SetIncludeList(const wxStringList
& list
)
236 m_includeList
.Clear();
237 // TODO: replace with =
238 wxNode
*node
= list
.First() ;
241 wxChar
*s
= (wxChar
*)node
->Data();
242 m_includeList
.Add(s
);
247 void wxTextValidator::SetExcludeList(const wxStringList
& list
)
254 m_excludeList
.Clear();
255 // TODO: replace with =
256 wxNode
*node
= list
.First() ;
259 wxChar
*s
= (wxChar
*)node
->Data();
260 m_excludeList
.Add(s
);
265 void wxTextValidator::OnChar(wxKeyEvent
& event
)
272 if ( m_validatorWindow
)
274 int keyCode
= event
.KeyCode();
276 // we don't filter special keys and Delete
278 !(keyCode
< WXK_SPACE
|| keyCode
== WXK_DELETE
|| keyCode
> WXK_START
) &&
280 ((m_validatorStyle
& wxFILTER_ASCII
) && !isascii(keyCode
)) ||
281 ((m_validatorStyle
& wxFILTER_ALPHA
) && !isalpha(keyCode
)) ||
282 ((m_validatorStyle
& wxFILTER_ALPHANUMERIC
) && !isalnum(keyCode
)) ||
283 ((m_validatorStyle
& wxFILTER_NUMERIC
) && !isdigit(keyCode
)
284 && keyCode
!= '.' && keyCode
!= ',' && keyCode
!= '-')
288 if ( !wxValidator::IsSilent() )
299 static bool wxIsNumeric(const wxString
& val
)
302 for ( i
= 0; i
< (int)val
.Length(); i
++)
304 // Allow for "," (French) as well as "." -- in future we should
305 // use wxSystemSettings or other to do better localisation
306 if ((!isdigit(val
[i
])) && (val
[i
] != '.') && (val
[i
] != ','))
307 if(!((i
== 0) && (val
[i
] == '-')))