]>
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 // 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.");
176 wxASSERT_MSG( !errormsg
.empty(), _T("you forgot to set errormsg") );
178 m_validatorWindow
->SetFocus();
181 buf
.Printf(errormsg
, val
.c_str());
183 wxMessageBox(buf
, _("Validation conflict"),
184 wxOK
| wxICON_EXCLAMATION
, parent
);
190 // Called to transfer data to the window
191 bool wxTextValidator::TransferToWindow(void)
193 if( !CheckValidator() )
196 wxTextCtrl
*control
= (wxTextCtrl
*) m_validatorWindow
;
197 control
->SetValue(* m_stringValue
) ;
202 // Called to transfer data to the window
203 bool wxTextValidator::TransferFromWindow(void)
205 if( !CheckValidator() )
208 wxTextCtrl
*control
= (wxTextCtrl
*) m_validatorWindow
;
209 * m_stringValue
= control
->GetValue() ;
214 void wxTextValidator::SetIncludeList(const wxStringList
& list
)
221 m_includeList
.Clear();
222 // TODO: replace with =
223 wxNode
*node
= list
.First() ;
226 wxChar
*s
= (wxChar
*)node
->Data();
227 m_includeList
.Add(s
);
232 void wxTextValidator::SetExcludeList(const wxStringList
& list
)
239 m_excludeList
.Clear();
240 // TODO: replace with =
241 wxNode
*node
= list
.First() ;
244 wxChar
*s
= (wxChar
*)node
->Data();
245 m_excludeList
.Add(s
);
250 void wxTextValidator::OnChar(wxKeyEvent
& event
)
257 if ( m_validatorWindow
)
259 int keyCode
= (int)event
.KeyCode();
261 // we don't filter special keys and Delete
263 !(keyCode
< WXK_SPACE
|| keyCode
== WXK_DELETE
|| keyCode
> WXK_START
) &&
265 ((m_validatorStyle
& wxFILTER_ASCII
) && !isascii(keyCode
)) ||
266 ((m_validatorStyle
& wxFILTER_ALPHA
) && !wxIsalpha(keyCode
)) ||
267 ((m_validatorStyle
& wxFILTER_ALPHANUMERIC
) && !wxIsalnum(keyCode
)) ||
268 ((m_validatorStyle
& wxFILTER_NUMERIC
) && !wxIsdigit(keyCode
)
269 && keyCode
!= '.' && keyCode
!= ',' && keyCode
!= '-')
273 if ( !wxValidator::IsSilent() )
284 static bool wxIsNumeric(const wxString
& val
)
287 for ( i
= 0; i
< (int)val
.Length(); i
++)
289 // Allow for "," (French) as well as "." -- in future we should
290 // use wxSystemSettings or other to do better localisation
291 if ((!isdigit(val
[i
])) && (val
[i
] != '.') && (val
[i
] != ','))
292 if(!((i
== 0) && (val
[i
] == '-')))