From bfcd51cb6a51269fdae6d210b13b5ba5474f3e5f Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 7 Jan 2018 01:25:10 +0100 Subject: [PATCH] Make wxValidator::SetWindow() virtual Allow overriding the method called when the validator is associated with the window, this can be convenient to perform some initialization on the validator instance actually used as it can't be done on the initially created object itself because it will be cloned by SetValidator(), creating a new instance. Also change SetWindow() to take wxWindow instead of wxWindowBase, this still requires the cast in wxWindow::SetValidator(), but it's better to have it there rather than in wxValidator and use the simpler type in the public function signature. --- include/wx/validate.h | 9 ++++++--- interface/wx/validate.h | 11 +++++++---- src/common/wincmn.cpp | 4 ++-- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/include/wx/validate.h b/include/wx/validate.h index 477af1019a..0fcc58e476 100644 --- a/include/wx/validate.h +++ b/include/wx/validate.h @@ -62,9 +62,12 @@ public: // Called to transfer data from the window virtual bool TransferFromWindow() { return false; } + // Called when the validator is associated with a window, may be useful to + // override if it needs to somehow initialize the window. + virtual void SetWindow(wxWindow *win) { m_validatorWindow = win; } + // accessors - wxWindow *GetWindow() const { return (wxWindow *)m_validatorWindow; } - void SetWindow(wxWindowBase *win) { m_validatorWindow = win; } + wxWindow *GetWindow() const { return m_validatorWindow; } // validators beep by default if invalid key is pressed, this function // allows to change this @@ -85,7 +88,7 @@ public: #endif protected: - wxWindowBase *m_validatorWindow; + wxWindow *m_validatorWindow; private: static bool ms_isSilent; diff --git a/interface/wx/validate.h b/interface/wx/validate.h index 5a1c4e81d9..1096077019 100644 --- a/interface/wx/validate.h +++ b/interface/wx/validate.h @@ -85,8 +85,11 @@ public: /** Associates a window with the validator. - This function is automatically called by wxWidgets when creating a wxWindow-derived - class instance which takes a wxValidator reference. + This function is automatically called by wxWidgets when creating a + wxWindow-derived class instance which takes a wxValidator reference. + Since wxWidgets 3.1.1, it can be overridden in custom validators in + order to perform any one-time initialization or checks of the window + when the validator is associated with it. E.g. @code @@ -94,9 +97,9 @@ public: wxTextValidator(wxFILTER_ALPHA, &g_data.m_string)); @endcode will automatically link the wxTextValidator instance with the wxTextCtrl - instance. + instance and call SetWindow() method on the wxTextValidator object. */ - void SetWindow(wxWindow* window); + virtual void SetWindow(wxWindow* window); /** This overridable function is called when the value in the window must diff --git a/src/common/wincmn.cpp b/src/common/wincmn.cpp index f07acd465d..b0e542a171 100644 --- a/src/common/wincmn.cpp +++ b/src/common/wincmn.cpp @@ -1782,10 +1782,10 @@ void wxWindowBase::SetValidator(const wxValidator& validator) if ( m_windowValidator ) delete m_windowValidator; - m_windowValidator = (wxValidator *)validator.Clone(); + m_windowValidator = static_cast(validator.Clone()); if ( m_windowValidator ) - m_windowValidator->SetWindow(this); + m_windowValidator->SetWindow(static_cast(this)); } #endif // wxUSE_VALIDATORS