From a6d677a0fec04b3aeae0fde2ed038b31b7c9ee0c Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 7 Jan 2018 01:26:53 +0100 Subject: [PATCH] Check for correct window in wxNumValidatorBase::SetWindow() Override SetWindow() to check that the validator is being associated with the window of the correct type, this allows to trigger an assert immediately if this is not the case, making it simpler to find the error as the call to SetValitator() on the wrong window will be in the call stack when this happens, unlike before when the assert would happen only at some later time. --- include/wx/valnum.h | 4 ++++ src/common/valnum.cpp | 19 +++++++++++++++++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/include/wx/valnum.h b/include/wx/valnum.h index f4436cb027..42f7675a76 100644 --- a/include/wx/valnum.h +++ b/include/wx/valnum.h @@ -50,6 +50,10 @@ public: // we don't need this as we do our validation on the fly here. virtual bool Validate(wxWindow * WXUNUSED(parent)) wxOVERRIDE { return true; } + // Override base class method to check that the window is a text control or + // combobox. + virtual void SetWindow(wxWindow *win) wxOVERRIDE; + protected: wxNumValidatorBase(int style) { diff --git a/src/common/valnum.cpp b/src/common/valnum.cpp index bb483502a6..4f11072b1c 100644 --- a/src/common/valnum.cpp +++ b/src/common/valnum.cpp @@ -52,6 +52,23 @@ int wxNumValidatorBase::GetFormatFlags() const return flags; } +void wxNumValidatorBase::SetWindow(wxWindow *win) +{ + wxValidator::SetWindow(win); + +#if wxUSE_TEXTCTRL + if ( wxDynamicCast(m_validatorWindow, wxTextCtrl) ) + return; +#endif // wxUSE_TEXTCTRL + +#if wxUSE_COMBOBOX + if ( wxDynamicCast(m_validatorWindow, wxComboBox) ) + return; +#endif // wxUSE_COMBOBOX + + wxFAIL_MSG("Can only be used with wxTextCtrl or wxComboBox"); +} + wxTextEntry *wxNumValidatorBase::GetTextEntry() const { #if wxUSE_TEXTCTRL @@ -64,8 +81,6 @@ wxTextEntry *wxNumValidatorBase::GetTextEntry() const return combo; #endif // wxUSE_COMBOBOX - wxFAIL_MSG("Can only be used with wxTextCtrl or wxComboBox"); - return NULL; }