From: Francesco Montorsi Date: Sat, 31 Jan 2009 22:41:51 +0000 (+0000) Subject: add SetCharIncludes and SetCharExcludes utilities to wxTextValidator; use iterators... X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/fcd209b6a20b41e7ddf9acf837311ac6779ea41f add SetCharIncludes and SetCharExcludes utilities to wxTextValidator; use iterators when scanning wxStrings; fix typo in ContainsExcludedCharacters (reversed return values); modify the sample to show wxTextValidator with wxFILTER_EXCLUDE_CHAR_LIST git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@58573 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/include/wx/valtext.h b/include/wx/valtext.h index a5fee8fb6f..b096aa2c6f 100644 --- a/include/wx/valtext.h +++ b/include/wx/valtext.h @@ -74,9 +74,11 @@ public: wxTextEntry *GetTextEntry(); + void SetCharIncludes(const wxString& chars); void SetIncludes(const wxArrayString& includes) { m_includes = includes; } inline wxArrayString& GetIncludes() { return m_includes; } + void SetCharExcludes(const wxString& chars); void SetExcludes(const wxArrayString& excludes) { m_excludes = excludes; } inline wxArrayString& GetExcludes() { return m_excludes; } diff --git a/interface/wx/valtext.h b/interface/wx/valtext.h index a6bde76a50..17aba3a2ad 100644 --- a/interface/wx/valtext.h +++ b/interface/wx/valtext.h @@ -49,12 +49,12 @@ enum wxTextValidatorStyle /// Use an include list. The validator checks if each input character is /// in the list (one character per list element), complaining if not. - /// See wxTextValidator::SetIncludes(). + /// See wxTextValidator::SetCharIncludes(). wxFILTER_INCLUDE_CHAR_LIST, - /// Use an include list. The validator checks if each input character is + /// Use an exclude list. The validator checks if each input character is /// in the list (one character per list element), complaining if it is. - /// See wxTextValidator::SetExcludes(). + /// See wxTextValidator::SetCharExcludes(). wxFILTER_EXCLUDE_CHAR_LIST }; @@ -123,11 +123,29 @@ public: */ void SetExcludes(const wxArrayString& stringList); + /** + Breaks the given @a chars strings in single characters and sets the + internal wxArrayString used to store the "excluded" characters + (see SetExcludes()). + + This function is mostly useful when @c wxFILTER_EXCLUDE_CHAR_LIST was used. + */ + void SetCharExcludes(const wxString& chars); + /** Sets the include list (valid values for the user input). */ void SetIncludes(const wxArrayString& stringList); + /** + Breaks the given @a chars strings in single characters and sets the + internal wxArrayString used to store the "included" characters + (see SetIncludes()). + + This function is mostly useful when @c wxFILTER_INCLUDE_CHAR_LIST was used. + */ + void SetCharIncludes(const wxString& chars); + /** Sets the validator style. */ diff --git a/samples/validate/validate.cpp b/samples/validate/validate.cpp index 3a0166a8ae..5fbf3eed77 100644 --- a/samples/validate/validate.cpp +++ b/samples/validate/validate.cpp @@ -61,10 +61,10 @@ MyData::MyData() // will complain because spaces aren't alpha. Note that validation // is performed only when 'OK' is pressed. m_string = wxT("Spaces are invalid here"); + m_string2 = "Valid text"; m_listbox_choices.Add(0); } - // ---------------------------------------------------------------------------- // MyComboBoxValidator // ---------------------------------------------------------------------------- @@ -162,7 +162,7 @@ MyFrame::MyFrame(wxFrame *frame, const wxString&title, int x, int y, int w, int wxMenu *file_menu = new wxMenu; - file_menu->Append(VALIDATE_TEST_DIALOG, wxT("&Test"), wxT("Demonstrate validators")); + file_menu->Append(VALIDATE_TEST_DIALOG, wxT("&Test dialog..."), wxT("Demonstrate validators")); file_menu->AppendCheckItem(VALIDATE_TOGGLE_BELL, wxT("&Bell on error"), wxT("Toggle bell on error")); file_menu->AppendSeparator(); file_menu->Append(wxID_EXIT, wxT("E&xit")); @@ -203,6 +203,8 @@ void MyFrame::OnTestDialog(wxCommandEvent& WXUNUSED(event)) // when we created the validators. m_listbox->Clear(); m_listbox->Append(wxString(_T("string: ")) + g_data.m_string); + m_listbox->Append(wxString(_T("string #2: ")) + g_data.m_string2); + for(unsigned int i = 0; i < g_data.m_listbox_choices.GetCount(); ++i) { int j = g_data.m_listbox_choices[i]; @@ -234,7 +236,7 @@ MyDialog::MyDialog( wxWindow *parent, const wxString& title, // setup the flex grid sizer // ------------------------- - wxFlexGridSizer *flexgridsizer = new wxFlexGridSizer(2, 2, 5, 5); + wxFlexGridSizer *flexgridsizer = new wxFlexGridSizer(3, 2, 5, 5); // Create and add controls to sizers. Note that a member variable // of g_data is bound to each control upon construction. There is @@ -248,14 +250,13 @@ MyDialog::MyDialog( wxWindow *parent, const wxString& title, wxTextValidator(wxFILTER_ALPHA, &g_data.m_string)); flexgridsizer->Add(m_text, 1, wxGROW); - // This wxCheckBox* doesn't need to be assigned to any pointer - // because we don't use it elsewhere--it can be anonymous. - // We don't need any such pointer to query its state, which - // can be gotten directly from g_data. - flexgridsizer->Add(new wxCheckBox(this, VALIDATE_CHECK, wxT("Sample checkbox"), - wxPoint(130, 10), wxSize(120, wxDefaultCoord), 0, - wxGenericValidator(&g_data.m_checkbox_state)), - 1, wxALIGN_CENTER); + + // Now set a wxTextValidator with an explicit list of characters NOT allowed: + wxTextValidator textVal(wxFILTER_EXCLUDE_CHAR_LIST, &g_data.m_string2); + textVal.SetCharExcludes("bcwyz"); + flexgridsizer->Add(new wxTextCtrl(this, VALIDATE_TEXT2, wxEmptyString, + wxDefaultPosition, wxDefaultSize, 0, textVal), + 1, wxGROW); flexgridsizer->Add(new wxListBox((wxWindow*)this, VALIDATE_LIST, wxPoint(10, 30), wxSize(120, wxDefaultCoord), @@ -269,6 +270,15 @@ MyDialog::MyDialog( wxWindow *parent, const wxString& title, MyComboBoxValidator(&g_data.m_combobox_choice)); flexgridsizer->Add(m_combobox, 1, wxALIGN_CENTER); + // This wxCheckBox* doesn't need to be assigned to any pointer + // because we don't use it elsewhere--it can be anonymous. + // We don't need any such pointer to query its state, which + // can be gotten directly from g_data. + flexgridsizer->Add(new wxCheckBox(this, VALIDATE_CHECK, wxT("Sample checkbox"), + wxPoint(130, 10), wxSize(120, wxDefaultCoord), 0, + wxGenericValidator(&g_data.m_checkbox_state)), + 1, wxALIGN_CENTER); + flexgridsizer->AddGrowableCol(0); flexgridsizer->AddGrowableCol(1); flexgridsizer->AddGrowableRow(1); diff --git a/samples/validate/validate.h b/samples/validate/validate.h index dde69217e4..9f723097cb 100644 --- a/samples/validate/validate.h +++ b/samples/validate/validate.h @@ -62,7 +62,7 @@ public: // These data members are designed for transfer to and from // controls, via validators. For instance, a text control's // transferred value is a string: - wxString m_string; + wxString m_string, m_string2; // Listboxes may permit multiple selections, so their state // is transferred to an integer-array class. @@ -103,6 +103,7 @@ enum VALIDATE_TOGGLE_BELL, VALIDATE_TEXT, + VALIDATE_TEXT2, VALIDATE_LIST, VALIDATE_CHECK, VALIDATE_COMBO, diff --git a/src/common/valtext.cpp b/src/common/valtext.cpp index 8b79675c22..a0b5c2b8c3 100644 --- a/src/common/valtext.cpp +++ b/src/common/valtext.cpp @@ -251,8 +251,8 @@ bool wxTextValidator::IsValid(const wxString& val, wxString* pErr) const bool wxTextValidator::ContainsOnlyIncludedCharacters(const wxString& val) const { - for (size_t i = 0; i < val.length(); i++) - if (m_includes.Index((wxString) val[i]) == wxNOT_FOUND) + for ( wxString::const_iterator i = val.begin(); i != val.end(); ++i ) + if (m_includes.Index((wxString) *i) == wxNOT_FOUND) // one character of 'val' is NOT present in m_includes... return false; @@ -262,13 +262,33 @@ bool wxTextValidator::ContainsOnlyIncludedCharacters(const wxString& val) const bool wxTextValidator::ContainsExcludedCharacters(const wxString& val) const { - for (size_t i = 0; i < val.length(); i++) - if (m_excludes.Index((wxString) val[i]) != wxNOT_FOUND) + for ( wxString::const_iterator i = val.begin(); i != val.end(); ++i ) + if (m_excludes.Index((wxString) *i) != wxNOT_FOUND) // one character of 'val' is present in m_excludes... - return false; + return true; // all characters of 'val' are NOT present in m_excludes - return true; + return false; +} + +void wxTextValidator::SetCharIncludes(const wxString& chars) +{ + wxArrayString arr; + + for ( wxString::const_iterator i = chars.begin(); i != chars.end(); ++i ) + arr.Add(*i); + + SetIncludes(arr); +} + +void wxTextValidator::SetCharExcludes(const wxString& chars) +{ + wxArrayString arr; + + for ( wxString::const_iterator i = chars.begin(); i != chars.end(); ++i ) + arr.Add(*i); + + SetExcludes(arr); } void wxTextValidator::OnChar(wxKeyEvent& event)