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 IMPLEMENT_DYNAMIC_CLASS(wxTextValidator
, wxValidator
)
45 BEGIN_EVENT_TABLE(wxTextValidator
, wxValidator
)
46 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 wxChar
*s
= (wxChar
*)node
->Data();
82 node
= val
.m_excludeList
.First() ;
85 wxChar
*s
= (wxChar
*)node
->Data();
92 wxTextValidator::~wxTextValidator()
96 static bool wxIsAlpha(const wxString
& val
)
99 for ( i
= 0; i
< (int)val
.Length(); i
++)
101 if (!wxIsalpha(val
[i
]))
107 static bool wxIsAlphaNumeric(const wxString
& val
)
110 for ( i
= 0; i
< (int)val
.Length(); i
++)
112 if (!wxIsalnum(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( !CheckValidator() )
125 wxTextCtrl
*control
= (wxTextCtrl
*) m_validatorWindow
;
127 // If window is disabled, simply return
128 if ( !control
->IsEnabled() )
131 wxString
val(control
->GetValue());
135 // NB: this format string should contian exactly one '%s'
138 bool includeList
= (m_validatorStyle
& wxFILTER_INCLUDE_LIST
) != 0;
139 if ( includeList
|| (m_validatorStyle
& wxFILTER_EXCLUDE_LIST
) )
141 // if includeList, it's only ok to have the members of the list,
142 // otherwise it's only ok to have non-members
143 ok
= includeList
== m_includeList
.Member(val
);
146 errormsg
= _("'%s' is invalid");
149 else if ( (m_validatorStyle
& wxFILTER_ASCII
) && !val
.IsAscii() )
153 errormsg
= _("'%s' should only contain ASCII characters.");
155 else if ( (m_validatorStyle
& wxFILTER_ALPHA
) && !wxIsAlpha(val
) )
159 errormsg
= _("'%s' should only contain alphabetic characters.");
161 else if ( (m_validatorStyle
& wxFILTER_ALPHANUMERIC
) && !wxIsAlphaNumeric(val
))
165 errormsg
= _("'%s' should only contain alphabetic or numeric characters.");
167 else if ( (m_validatorStyle
& wxFILTER_NUMERIC
) && !wxIsNumeric(val
))
171 errormsg
= _("'%s' should be numeric.");
173 else if ( (m_validatorStyle
& wxFILTER_INCLUDE_CHAR_LIST
) && !IsInCharIncludeList(val
))
175 //it's only ok to have the members of the list
176 errormsg
= _("'%s' is invalid");
179 else if ( (m_validatorStyle
& wxFILTER_EXCLUDE_CHAR_LIST
) && !IsNotInCharExcludeList(val
))
181 // it's only ok to have non-members of the list
182 errormsg
= _("'%s' is invalid");
188 wxASSERT_MSG( !errormsg
.empty(), _T("you forgot to set errormsg") );
190 m_validatorWindow
->SetFocus();
193 buf
.Printf(errormsg
, val
.c_str());
195 wxMessageBox(buf
, _("Validation conflict"),
196 wxOK
| wxICON_EXCLAMATION
, parent
);
202 // Called to transfer data to the window
203 bool wxTextValidator::TransferToWindow(void)
205 if( !CheckValidator() )
208 wxTextCtrl
*control
= (wxTextCtrl
*) m_validatorWindow
;
209 control
->SetValue(* m_stringValue
) ;
214 // Called to transfer data to the window
215 bool wxTextValidator::TransferFromWindow(void)
217 if( !CheckValidator() )
220 wxTextCtrl
*control
= (wxTextCtrl
*) m_validatorWindow
;
221 * m_stringValue
= control
->GetValue() ;
226 void wxTextValidator::SetIncludeList(const wxStringList
& list
)
233 m_includeList
.Clear();
234 // TODO: replace with =
235 wxNode
*node
= list
.First() ;
238 wxChar
*s
= (wxChar
*)node
->Data();
239 m_includeList
.Add(s
);
244 void wxTextValidator::SetExcludeList(const wxStringList
& list
)
251 m_excludeList
.Clear();
252 // TODO: replace with =
253 wxNode
*node
= list
.First() ;
256 wxChar
*s
= (wxChar
*)node
->Data();
257 m_excludeList
.Add(s
);
262 void wxTextValidator::OnChar(wxKeyEvent
& event
)
269 if ( m_validatorWindow
)
271 int keyCode
= (int)event
.KeyCode();
273 // we don't filter special keys and Delete
275 !(keyCode
< WXK_SPACE
|| keyCode
== WXK_DELETE
|| keyCode
> WXK_START
) &&
277 ((m_validatorStyle
& wxFILTER_INCLUDE_CHAR_LIST
) && !IsInCharIncludeList(wxString((char) keyCode
, 1))) ||
278 ((m_validatorStyle
& wxFILTER_EXCLUDE_CHAR_LIST
) && !IsNotInCharExcludeList(wxString((char) keyCode
, 1))) ||
279 ((m_validatorStyle
& wxFILTER_ASCII
) && !isascii(keyCode
)) ||
280 ((m_validatorStyle
& wxFILTER_ALPHA
) && !wxIsalpha(keyCode
)) ||
281 ((m_validatorStyle
& wxFILTER_ALPHANUMERIC
) && !wxIsalnum(keyCode
)) ||
282 ((m_validatorStyle
& wxFILTER_NUMERIC
) && !wxIsdigit(keyCode
)
283 && keyCode
!= '.' && keyCode
!= ',' && keyCode
!= '-')
287 if ( !wxValidator::IsSilent() )
298 static bool wxIsNumeric(const wxString
& val
)
301 for ( i
= 0; i
< (int)val
.Length(); i
++)
303 // Allow for "," (French) as well as "." -- in future we should
304 // use wxSystemSettings or other to do better localisation
305 if ((!isdigit(val
[i
])) && (val
[i
] != '.') && (val
[i
] != ','))
306 if(!((i
== 0) && (val
[i
] == '-')))
312 bool wxTextValidator::IsInCharIncludeList(const wxString
& val
)
315 for ( i
= 0; i
< val
.Length(); i
++)
317 if (!m_includeList
.Member((wxString
) val
[i
]))
323 bool wxTextValidator::IsNotInCharExcludeList(const wxString
& val
)
326 for ( i
= 0; i
< val
.Length(); i
++)
328 if (m_excludeList
.Member((wxString
) val
[i
]))