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
)
69 bool wxTextValidator::Copy(const wxTextValidator
& val
)
71 wxValidator::Copy(val
);
73 m_validatorStyle
= val
.m_validatorStyle
;
74 m_stringValue
= val
.m_stringValue
;
76 wxStringList::Node
*node
= val
.m_includeList
.GetFirst() ;
79 wxChar
*s
= node
->GetData();
81 node
= node
->GetNext();
83 node
= val
.m_excludeList
.GetFirst() ;
86 wxChar
*s
= node
->GetData();
88 node
= node
->GetNext();
93 wxTextValidator::~wxTextValidator()
97 static bool wxIsAlpha(const wxString
& val
)
100 for ( i
= 0; i
< (int)val
.Length(); i
++)
102 if (!wxIsalpha(val
[i
]))
108 static bool wxIsAlphaNumeric(const wxString
& val
)
111 for ( i
= 0; i
< (int)val
.Length(); i
++)
113 if (!wxIsalnum(val
[i
]))
119 // Called when the value in the window must be validated.
120 // This function can pop up an error message.
121 bool wxTextValidator::Validate(wxWindow
*parent
)
123 if( !CheckValidator() )
126 wxTextCtrl
*control
= (wxTextCtrl
*) m_validatorWindow
;
128 // If window is disabled, simply return
129 if ( !control
->IsEnabled() )
132 wxString
val(control
->GetValue());
136 // NB: this format string should contian exactly one '%s'
139 bool includeList
= (m_validatorStyle
& wxFILTER_INCLUDE_LIST
) != 0;
140 if ( includeList
|| (m_validatorStyle
& wxFILTER_EXCLUDE_LIST
) )
142 // if includeList, it's only ok to have the members of the list,
143 // otherwise it's only ok to have non-members
144 ok
= includeList
== m_includeList
.Member(val
);
147 errormsg
= _("'%s' is invalid");
150 else if ( (m_validatorStyle
& wxFILTER_ASCII
) && !val
.IsAscii() )
154 errormsg
= _("'%s' should only contain ASCII characters.");
156 else if ( (m_validatorStyle
& wxFILTER_ALPHA
) && !wxIsAlpha(val
) )
160 errormsg
= _("'%s' should only contain alphabetic characters.");
162 else if ( (m_validatorStyle
& wxFILTER_ALPHANUMERIC
) && !wxIsAlphaNumeric(val
))
166 errormsg
= _("'%s' should only contain alphabetic or numeric characters.");
168 else if ( (m_validatorStyle
& wxFILTER_NUMERIC
) && !wxIsNumeric(val
))
172 errormsg
= _("'%s' should be numeric.");
174 else if ( (m_validatorStyle
& wxFILTER_INCLUDE_CHAR_LIST
) && !IsInCharIncludeList(val
))
176 //it's only ok to have the members of the list
177 errormsg
= _("'%s' is invalid");
180 else if ( (m_validatorStyle
& wxFILTER_EXCLUDE_CHAR_LIST
) && !IsNotInCharExcludeList(val
))
182 // it's only ok to have non-members of the list
183 errormsg
= _("'%s' is invalid");
189 wxASSERT_MSG( !errormsg
.empty(), _T("you forgot to set errormsg") );
191 m_validatorWindow
->SetFocus();
194 buf
.Printf(errormsg
, val
.c_str());
196 wxMessageBox(buf
, _("Validation conflict"),
197 wxOK
| wxICON_EXCLAMATION
, parent
);
203 // Called to transfer data to the window
204 bool wxTextValidator::TransferToWindow(void)
206 if( !CheckValidator() )
209 wxTextCtrl
*control
= (wxTextCtrl
*) m_validatorWindow
;
210 control
->SetValue(* m_stringValue
) ;
215 // Called to transfer data to the window
216 bool wxTextValidator::TransferFromWindow(void)
218 if( !CheckValidator() )
221 wxTextCtrl
*control
= (wxTextCtrl
*) m_validatorWindow
;
222 * m_stringValue
= control
->GetValue() ;
227 void wxTextValidator::SetIncludeList(const wxStringList
& list
)
234 m_includeList
.Clear();
235 // TODO: replace with =
236 wxStringList::Node
*node
= list
.GetFirst();
239 wxChar
*s
= node
->GetData();
240 m_includeList
.Add(s
);
241 node
= node
->GetNext();
245 void wxTextValidator::SetExcludeList(const wxStringList
& list
)
252 m_excludeList
.Clear();
253 // TODO: replace with =
254 wxStringList::Node
*node
= list
.GetFirst() ;
257 wxChar
*s
= node
->GetData();
258 m_excludeList
.Add(s
);
259 node
= node
->GetNext();
263 void wxTextValidator::OnChar(wxKeyEvent
& event
)
270 if ( m_validatorWindow
)
272 int keyCode
= event
.GetKeyCode();
274 // we don't filter special keys and Delete
276 !(keyCode
< WXK_SPACE
|| keyCode
== WXK_DELETE
|| keyCode
> WXK_START
) &&
278 ((m_validatorStyle
& wxFILTER_INCLUDE_CHAR_LIST
) && !IsInCharIncludeList(wxString((char) keyCode
, 1))) ||
279 ((m_validatorStyle
& wxFILTER_EXCLUDE_CHAR_LIST
) && !IsNotInCharExcludeList(wxString((char) keyCode
, 1))) ||
280 ((m_validatorStyle
& wxFILTER_ASCII
) && !isascii(keyCode
)) ||
281 ((m_validatorStyle
& wxFILTER_ALPHA
) && !wxIsalpha(keyCode
)) ||
282 ((m_validatorStyle
& wxFILTER_ALPHANUMERIC
) && !wxIsalnum(keyCode
)) ||
283 ((m_validatorStyle
& wxFILTER_NUMERIC
) && !wxIsdigit(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
] == '-')))
313 bool wxTextValidator::IsInCharIncludeList(const wxString
& val
)
316 for ( i
= 0; i
< val
.Length(); i
++)
318 if (!m_includeList
.Member((wxString
) val
[i
]))
324 bool wxTextValidator::IsNotInCharExcludeList(const wxString
& val
)
327 for ( i
= 0; i
< val
.Length(); i
++)
329 if (m_excludeList
.Member((wxString
) val
[i
]))