From c6dae1699e518cb4bbc6f638b7b68f03b9abc8ab Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Thu, 16 May 2013 14:32:55 +0000 Subject: [PATCH 1/1] Allow associating a validator with wxGridCellTextEditor. Add wxGridCellTextEditor::SetValidator() for finer control over text input in wxGrid. Closes #15176. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@74001 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- docs/changes.txt | 1 + include/wx/generic/grideditors.h | 9 +++++---- interface/wx/grid.h | 7 +++++++ src/generic/grideditors.cpp | 20 ++++++++++++++++++++ 4 files changed, 33 insertions(+), 4 deletions(-) diff --git a/docs/changes.txt b/docs/changes.txt index 11fb2bf55c..b16eb57c18 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -655,6 +655,7 @@ All (GUI): - Add chainable wxWizardPageSimple::Chain() overload. - Add wxTextEntryDialog::SetMaxLength() (derEine). - Fix maximum width support in wxGridCellTextEditor (derEine). +- Allow associating a validator with wxGridCellTextEditor (derEine). - Add more convenient wxFont(wxFontInfo) ctor. - Pass menu events to the handler in the associated wxMenuBar. diff --git a/include/wx/generic/grideditors.h b/include/wx/generic/grideditors.h index 7fc563b427..1167589012 100644 --- a/include/wx/generic/grideditors.h +++ b/include/wx/generic/grideditors.h @@ -75,9 +75,9 @@ public: // parameters string format is "max_width" virtual void SetParameters(const wxString& params); + virtual void SetValidator(const wxValidator& validator); - virtual wxGridCellEditor *Clone() const - { return new wxGridCellTextEditor(m_maxChars); } + virtual wxGridCellEditor *Clone() const; // added GetValue so we can get the value which is in the control virtual wxString GetValue() const; @@ -92,8 +92,9 @@ protected: void DoReset(const wxString& startValue); private: - size_t m_maxChars; // max number of chars allowed - wxString m_value; + size_t m_maxChars; // max number of chars allowed + wxScopedPtr m_validator; + wxString m_value; wxDECLARE_NO_COPY_CLASS(wxGridCellTextEditor); }; diff --git a/interface/wx/grid.h b/interface/wx/grid.h index e55affd187..2bdabe866a 100644 --- a/interface/wx/grid.h +++ b/interface/wx/grid.h @@ -638,6 +638,13 @@ public: the maximum width. */ virtual void SetParameters(const wxString& params); + + /** + Set validator to validate user input. + + @since 2.9.5 + */ + virtual void SetValidator(const wxValidator& validator); }; /** diff --git a/src/generic/grideditors.cpp b/src/generic/grideditors.cpp index aee78e2fe8..2963e77821 100644 --- a/src/generic/grideditors.cpp +++ b/src/generic/grideditors.cpp @@ -416,6 +416,11 @@ void wxGridCellTextEditor::DoCreate(wxWindow* parent, { Text()->SetMaxLength(m_maxChars); } + // validate text in textctrl, if validator is set + if ( m_validator ) + { + Text()->SetValidator(*m_validator); + } wxGridCellEditor::Create(parent, id, evtHandler); } @@ -634,6 +639,21 @@ void wxGridCellTextEditor::SetParameters(const wxString& params) } } +void wxGridCellTextEditor::SetValidator(const wxValidator& validator) +{ + m_validator.reset(static_cast(validator.Clone())); +} + +wxGridCellEditor *wxGridCellTextEditor::Clone() const +{ + wxGridCellTextEditor* editor = new wxGridCellTextEditor(m_maxChars); + if ( m_validator ) + { + editor->SetValidator(*m_validator); + } + return editor; +} + // return the value in the text control wxString wxGridCellTextEditor::GetValue() const { -- 2.45.2