// Filter keystrokes void wxNumericTextValidator::OnChar(wxKeyEvent& event) { if (!m_validatorWindow) return; int keycode = event.GetKeyCode(); // we don't filter special keys and Delete if (keycode < WXK_SPACE || keycode == WXK_DELETE || keycode > WXK_START) { // Don't disable following event handlers in the chain (i.e. use the key) event.Skip(); return; } if (!wxIsNumeric(keycode)) { if (!wxValidator::IsSilent()) wxBell(); // eat message return; } // Don't disable following event handlers in the chain (i.e. use the key) event.Skip(); }
// Called when the value in the window must be validated. // This function can pop up an error message. bool wxNumericTextValidator::Validate(wxWindow *parent) { if (!CheckValidator()) return false; wxTextCtrl* control = dynamic_cast<wxTextCtrl*>(m_validatorWindow); if (!control) return false; // If window is disabled, simply return if ( !control->IsEnabled() ) return true; if (!wxIsNumeric(control->GetValue())) { m_validatorWindow->SetFocus(); wxString buf; buf.Printf(_("'%s' should be numeric."), control->GetValue().c_str()); wxMessageBox(buf, _("Validation conflict"), wxOK | wxICON_EXCLAMATION, parent); return false; } return true; }
wxString wxTextValidator::IsValid(const wxString& val) const { // wxFILTER_EMPTY is checked for in wxTextValidator::Validate if ( HasFlag(wxFILTER_ASCII) && !val.IsAscii() ) return _("'%s' should only contain ASCII characters."); if ( HasFlag(wxFILTER_ALPHA) && !CheckString(wxIsalpha, val) ) return _("'%s' should only contain alphabetic characters."); if ( HasFlag(wxFILTER_ALPHANUMERIC) && !CheckString(wxIsalnum, val) ) return _("'%s' should only contain alphabetic or numeric characters."); if ( HasFlag(wxFILTER_DIGITS) && !CheckString(wxIsdigit, val) ) return _("'%s' should only contain digits."); if ( HasFlag(wxFILTER_NUMERIC) && !wxIsNumeric(val) ) return _("'%s' should be numeric."); if ( HasFlag(wxFILTER_INCLUDE_LIST) && m_includes.Index(val) == wxNOT_FOUND ) return _("'%s' is invalid"); if ( HasFlag(wxFILTER_INCLUDE_CHAR_LIST) && !ContainsOnlyIncludedCharacters(val) ) return _("'%s' is invalid"); if ( HasFlag(wxFILTER_EXCLUDE_LIST) && m_excludes.Index(val) != wxNOT_FOUND ) return _("'%s' is invalid"); if ( HasFlag(wxFILTER_EXCLUDE_CHAR_LIST) && ContainsExcludedCharacters(val) ) return _("'%s' is invalid"); return wxEmptyString; }
static bool wxIsNumeric(const wxString& val) { for (unsigned int i = 0; i < val.Length(); ++i) { // Allow for "," (French) as well as "." -- in future we should // use wxSystemSettings or other to do better localisation if (!wxIsNumeric(val[i])) return false; } return true; }
// Called when the value in the window must be validated. // This function can pop up an error message. bool wxTextValidator::Validate(wxWindow *parent) { if( !CheckValidator() ) return false; wxTextCtrl *control = (wxTextCtrl *) m_validatorWindow; // If window is disabled, simply return if ( !control->IsEnabled() ) return true; wxString val(control->GetValue()); bool ok = true; // NB: this format string should contian exactly one '%s' wxString errormsg; bool includes = (m_validatorStyle & wxFILTER_INCLUDE_LIST) != 0; if ( includes || (m_validatorStyle & wxFILTER_EXCLUDE_LIST) ) { // if includes, it's only ok to have the members of the list, // otherwise it's only ok to have non-members ok = includes == (m_includes.Index(val) != wxNOT_FOUND); if ( !ok ) { errormsg = _("'%s' is invalid"); } } else if ( (m_validatorStyle & wxFILTER_ASCII) && !val.IsAscii() ) { ok = false; errormsg = _("'%s' should only contain ASCII characters."); } else if ( (m_validatorStyle & wxFILTER_ALPHA) && !wxIsAlpha(val) ) { ok = false; errormsg = _("'%s' should only contain alphabetic characters."); } else if ( (m_validatorStyle & wxFILTER_ALPHANUMERIC) && !wxIsAlphaNumeric(val)) { ok = false; errormsg = _("'%s' should only contain alphabetic or numeric characters."); } else if ( (m_validatorStyle & wxFILTER_NUMERIC) && !wxIsNumeric(val)) { ok = false; errormsg = _("'%s' should be numeric."); } else if ( (m_validatorStyle & wxFILTER_INCLUDE_CHAR_LIST) && !IsInCharIncludes(val)) { //it's only ok to have the members of the list errormsg = _("'%s' is invalid"); ok = false; } else if ( (m_validatorStyle & wxFILTER_EXCLUDE_CHAR_LIST) && !IsNotInCharExcludes(val)) { // it's only ok to have non-members of the list errormsg = _("'%s' is invalid"); ok = false; } if ( !ok ) { wxASSERT_MSG( !errormsg.empty(), _T("you forgot to set errormsg") ); m_validatorWindow->SetFocus(); wxString buf; buf.Printf(errormsg, val.c_str()); wxMessageBox(buf, _("Validation conflict"), wxOK | wxICON_EXCLAMATION, parent); } return ok; }