]> git.saurik.com Git - wxWidgets.git/commitdiff
add SetCharIncludes and SetCharExcludes utilities to wxTextValidator; use iterators...
authorFrancesco Montorsi <f18m_cpp217828@yahoo.it>
Sat, 31 Jan 2009 22:41:51 +0000 (22:41 +0000)
committerFrancesco Montorsi <f18m_cpp217828@yahoo.it>
Sat, 31 Jan 2009 22:41:51 +0000 (22:41 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@58573 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

include/wx/valtext.h
interface/wx/valtext.h
samples/validate/validate.cpp
samples/validate/validate.h
src/common/valtext.cpp

index a5fee8fb6f73e0b8f198a784617dc64bf371a7ba..b096aa2c6f9dce22a7aae704b507042e4798f1a8 100644 (file)
@@ -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; }
 
index a6bde76a50f8f67673f0d14fec72908a82629974..17aba3a2adf4788e674590e93d15e7164c78a43d 100644 (file)
@@ -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.
     */
index 3a0166a8ae11d8b2b21afe0d4410141e0fe486b8..5fbf3eed77231a9176d418c3a1ab49fbf0cd5321 100644 (file)
@@ -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);
index dde69217e4888f3c040711e2ab7b87f2aca0d3e6..9f723097cbbe1292793fc1067360b67f8830fbde 100644 (file)
@@ -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,
index 8b79675c222898230705f95ba79c66dfc16377dd..a0b5c2b8c3d7716181785edb1287540d8cd52f54 100644 (file)
@@ -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)