]>
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 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 // this format string should contian exactly one '%s'
136 const wxChar
*errormsg
= _("'%s' is invalid");
138 if ( m_validatorStyle
& wxFILTER_INCLUDE_LIST
)
140 if ( !m_includeList
.Member(val
) )
145 else if ( m_validatorStyle
& wxFILTER_EXCLUDE_LIST
)
147 if ( m_excludeList
.Member(val
) )
152 else if ( (m_validatorStyle
& wxFILTER_ASCII
) && !val
.IsAscii() )
156 errormsg
= _("'%s' should only contain ASCII characters.");
158 else if ( (m_validatorStyle
& wxFILTER_ALPHA
) && !wxIsAlpha(val
) )
162 errormsg
= _("'%s' should only contain alphabetic characters.");
164 else if ( (m_validatorStyle
& wxFILTER_ALPHANUMERIC
) && !wxIsAlphaNumeric(val
))
168 errormsg
= _("'%s' should only contain alphabetic or numeric characters.");
170 else if ( (m_validatorStyle
& wxFILTER_NUMERIC
) && !wxIsNumeric(val
))
174 errormsg
= _("'%s' should be numeric.");
179 m_validatorWindow
->SetFocus();
182 buf
.Printf(errormsg
, val
.c_str());
184 wxMessageBox(buf
, _("Validation conflict"),
185 wxOK
| wxICON_EXCLAMATION
, parent
);
191 // Called to transfer data to the window
192 bool wxTextValidator::TransferToWindow(void)
194 if( !CheckValidator() )
197 wxTextCtrl
*control
= (wxTextCtrl
*) m_validatorWindow
;
198 control
->SetValue(* m_stringValue
) ;
203 // Called to transfer data to the window
204 bool wxTextValidator::TransferFromWindow(void)
206 if( !CheckValidator() )
209 wxTextCtrl
*control
= (wxTextCtrl
*) m_validatorWindow
;
210 * m_stringValue
= control
->GetValue() ;
215 void wxTextValidator::SetIncludeList(const wxStringList
& list
)
222 m_includeList
.Clear();
223 // TODO: replace with =
224 wxNode
*node
= list
.First() ;
227 wxChar
*s
= (wxChar
*)node
->Data();
228 m_includeList
.Add(s
);
233 void wxTextValidator::SetExcludeList(const wxStringList
& list
)
240 m_excludeList
.Clear();
241 // TODO: replace with =
242 wxNode
*node
= list
.First() ;
245 wxChar
*s
= (wxChar
*)node
->Data();
246 m_excludeList
.Add(s
);
251 void wxTextValidator::OnChar(wxKeyEvent
& event
)
258 if ( m_validatorWindow
)
260 int keyCode
= (int)event
.KeyCode();
262 // we don't filter special keys and Delete
264 !(keyCode
< WXK_SPACE
|| keyCode
== WXK_DELETE
|| keyCode
> WXK_START
) &&
266 ((m_validatorStyle
& wxFILTER_ASCII
) && !isascii(keyCode
)) ||
267 ((m_validatorStyle
& wxFILTER_ALPHA
) && !wxIsalpha(keyCode
)) ||
268 ((m_validatorStyle
& wxFILTER_ALPHANUMERIC
) && !wxIsalnum(keyCode
)) ||
269 ((m_validatorStyle
& wxFILTER_NUMERIC
) && !wxIsdigit(keyCode
)
270 && keyCode
!= '.' && keyCode
!= ',' && keyCode
!= '-')
274 if ( !wxValidator::IsSilent() )
285 static bool wxIsNumeric(const wxString
& val
)
288 for ( i
= 0; i
< (int)val
.Length(); i
++)
290 // Allow for "," (French) as well as "." -- in future we should
291 // use wxSystemSettings or other to do better localisation
292 if ((!isdigit(val
[i
])) && (val
[i
] != '.') && (val
[i
] != ','))
293 if(!((i
== 0) && (val
[i
] == '-')))