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
This commit is contained in:
Francesco Montorsi
2009-01-31 22:41:51 +00:00
parent c27dce1818
commit fcd209b6a2
5 changed files with 72 additions and 21 deletions

View File

@@ -74,9 +74,11 @@ public:
wxTextEntry *GetTextEntry(); wxTextEntry *GetTextEntry();
void SetCharIncludes(const wxString& chars);
void SetIncludes(const wxArrayString& includes) { m_includes = includes; } void SetIncludes(const wxArrayString& includes) { m_includes = includes; }
inline wxArrayString& GetIncludes() { return m_includes; } inline wxArrayString& GetIncludes() { return m_includes; }
void SetCharExcludes(const wxString& chars);
void SetExcludes(const wxArrayString& excludes) { m_excludes = excludes; } void SetExcludes(const wxArrayString& excludes) { m_excludes = excludes; }
inline wxArrayString& GetExcludes() { return m_excludes; } inline wxArrayString& GetExcludes() { return m_excludes; }

View File

@@ -49,12 +49,12 @@ enum wxTextValidatorStyle
/// Use an include list. The validator checks if each input character is /// Use an include list. The validator checks if each input character is
/// in the list (one character per list element), complaining if not. /// in the list (one character per list element), complaining if not.
/// See wxTextValidator::SetIncludes(). /// See wxTextValidator::SetCharIncludes().
wxFILTER_INCLUDE_CHAR_LIST, 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. /// in the list (one character per list element), complaining if it is.
/// See wxTextValidator::SetExcludes(). /// See wxTextValidator::SetCharExcludes().
wxFILTER_EXCLUDE_CHAR_LIST wxFILTER_EXCLUDE_CHAR_LIST
}; };
@@ -123,11 +123,29 @@ public:
*/ */
void SetExcludes(const wxArrayString& stringList); 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). Sets the include list (valid values for the user input).
*/ */
void SetIncludes(const wxArrayString& stringList); 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. Sets the validator style.
*/ */

View File

@@ -61,10 +61,10 @@ MyData::MyData()
// will complain because spaces aren't alpha. Note that validation // will complain because spaces aren't alpha. Note that validation
// is performed only when 'OK' is pressed. // is performed only when 'OK' is pressed.
m_string = wxT("Spaces are invalid here"); m_string = wxT("Spaces are invalid here");
m_string2 = "Valid text";
m_listbox_choices.Add(0); m_listbox_choices.Add(0);
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// MyComboBoxValidator // MyComboBoxValidator
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@@ -162,7 +162,7 @@ MyFrame::MyFrame(wxFrame *frame, const wxString&title, int x, int y, int w, int
wxMenu *file_menu = new wxMenu; 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->AppendCheckItem(VALIDATE_TOGGLE_BELL, wxT("&Bell on error"), wxT("Toggle bell on error"));
file_menu->AppendSeparator(); file_menu->AppendSeparator();
file_menu->Append(wxID_EXIT, wxT("E&xit")); file_menu->Append(wxID_EXIT, wxT("E&xit"));
@@ -203,6 +203,8 @@ void MyFrame::OnTestDialog(wxCommandEvent& WXUNUSED(event))
// when we created the validators. // when we created the validators.
m_listbox->Clear(); m_listbox->Clear();
m_listbox->Append(wxString(_T("string: ")) + g_data.m_string); 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) for(unsigned int i = 0; i < g_data.m_listbox_choices.GetCount(); ++i)
{ {
int j = g_data.m_listbox_choices[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 // 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 // Create and add controls to sizers. Note that a member variable
// of g_data is bound to each control upon construction. There is // 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)); wxTextValidator(wxFILTER_ALPHA, &g_data.m_string));
flexgridsizer->Add(m_text, 1, wxGROW); 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. // Now set a wxTextValidator with an explicit list of characters NOT allowed:
// We don't need any such pointer to query its state, which wxTextValidator textVal(wxFILTER_EXCLUDE_CHAR_LIST, &g_data.m_string2);
// can be gotten directly from g_data. textVal.SetCharExcludes("bcwyz");
flexgridsizer->Add(new wxCheckBox(this, VALIDATE_CHECK, wxT("Sample checkbox"), flexgridsizer->Add(new wxTextCtrl(this, VALIDATE_TEXT2, wxEmptyString,
wxPoint(130, 10), wxSize(120, wxDefaultCoord), 0, wxDefaultPosition, wxDefaultSize, 0, textVal),
wxGenericValidator(&g_data.m_checkbox_state)), 1, wxGROW);
1, wxALIGN_CENTER);
flexgridsizer->Add(new wxListBox((wxWindow*)this, VALIDATE_LIST, flexgridsizer->Add(new wxListBox((wxWindow*)this, VALIDATE_LIST,
wxPoint(10, 30), wxSize(120, wxDefaultCoord), wxPoint(10, 30), wxSize(120, wxDefaultCoord),
@@ -269,6 +270,15 @@ MyDialog::MyDialog( wxWindow *parent, const wxString& title,
MyComboBoxValidator(&g_data.m_combobox_choice)); MyComboBoxValidator(&g_data.m_combobox_choice));
flexgridsizer->Add(m_combobox, 1, wxALIGN_CENTER); 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(0);
flexgridsizer->AddGrowableCol(1); flexgridsizer->AddGrowableCol(1);
flexgridsizer->AddGrowableRow(1); flexgridsizer->AddGrowableRow(1);

View File

@@ -62,7 +62,7 @@ public:
// These data members are designed for transfer to and from // These data members are designed for transfer to and from
// controls, via validators. For instance, a text control's // controls, via validators. For instance, a text control's
// transferred value is a string: // transferred value is a string:
wxString m_string; wxString m_string, m_string2;
// Listboxes may permit multiple selections, so their state // Listboxes may permit multiple selections, so their state
// is transferred to an integer-array class. // is transferred to an integer-array class.
@@ -103,6 +103,7 @@ enum
VALIDATE_TOGGLE_BELL, VALIDATE_TOGGLE_BELL,
VALIDATE_TEXT, VALIDATE_TEXT,
VALIDATE_TEXT2,
VALIDATE_LIST, VALIDATE_LIST,
VALIDATE_CHECK, VALIDATE_CHECK,
VALIDATE_COMBO, VALIDATE_COMBO,

View File

@@ -251,8 +251,8 @@ bool wxTextValidator::IsValid(const wxString& val, wxString* pErr) const
bool wxTextValidator::ContainsOnlyIncludedCharacters(const wxString& val) const bool wxTextValidator::ContainsOnlyIncludedCharacters(const wxString& val) const
{ {
for (size_t i = 0; i < val.length(); i++) for ( wxString::const_iterator i = val.begin(); i != val.end(); ++i )
if (m_includes.Index((wxString) val[i]) == wxNOT_FOUND) if (m_includes.Index((wxString) *i) == wxNOT_FOUND)
// one character of 'val' is NOT present in m_includes... // one character of 'val' is NOT present in m_includes...
return false; return false;
@@ -262,13 +262,33 @@ bool wxTextValidator::ContainsOnlyIncludedCharacters(const wxString& val) const
bool wxTextValidator::ContainsExcludedCharacters(const wxString& val) const bool wxTextValidator::ContainsExcludedCharacters(const wxString& val) const
{ {
for (size_t i = 0; i < val.length(); i++) for ( wxString::const_iterator i = val.begin(); i != val.end(); ++i )
if (m_excludes.Index((wxString) val[i]) != wxNOT_FOUND) if (m_excludes.Index((wxString) *i) != wxNOT_FOUND)
// one character of 'val' is present in m_excludes... // one character of 'val' is present in m_excludes...
return false; return true;
// all characters of 'val' are NOT present in m_excludes // 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) void wxTextValidator::OnChar(wxKeyEvent& event)