From 5591a2009394c9bae43bcd2fa08f2fb51391cbc1 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Wed, 18 Nov 2015 02:08:34 +0100 Subject: [PATCH] Detect attempts to catch wxEVT_TEXT_ENTER without wxTE_PROCESS_ENTER This is never going to work, so complain about trying to do it to help with catching this bug. This is possible thanks to the new OnDynamicBind() method invoked whenever a dynamic event handler is bound to a control, so this doesn't detect all possible occurrences of the bug (such as specifying the handler in a static event table or in a validator), but it's still better than nothing. In the future OnDynamicBind() should be extended for other invalid calls, e.g. binding a handler for wxEVT_TEXT_ENTER to a non-text control shouldn't work neither, ideally. --- include/wx/event.h | 9 +++++++++ include/wx/textctrl.h | 5 +++++ src/common/event.cpp | 7 +++++++ src/common/textcmn.cpp | 15 +++++++++++++++ 4 files changed, 36 insertions(+) diff --git a/include/wx/event.h b/include/wx/event.h index 0666f3c8f4..c70f159f6e 100644 --- a/include/wx/event.h +++ b/include/wx/event.h @@ -3671,6 +3671,15 @@ protected: virtual bool TryParent(wxEvent& event), return DoTryApp(event); ) #endif // WXWIN_COMPATIBILITY_2_8 + // Overriding this method allows filtering the event handlers dynamically + // connected to this object. If this method returns false, the handler is + // not connected at all. If it returns true, it is connected using the + // possibly modified fields of the given entry. + virtual bool OnDynamicBind(wxDynamicEventTableEntry& WXUNUSED(entry)) + { + return true; + } + static const wxEventTable sm_eventTable; virtual const wxEventTable *GetEventTable() const; diff --git a/include/wx/textctrl.h b/include/wx/textctrl.h index e83221f48a..cd7a815a6e 100644 --- a/include/wx/textctrl.h +++ b/include/wx/textctrl.h @@ -743,6 +743,11 @@ public: } protected: + // Override wxEvtHandler method to check for a common problem of binding + // wxEVT_TEXT_ENTER to a control without wxTE_PROCESS_ENTER style, which is + // never going to work. + virtual bool OnDynamicBind(wxDynamicEventTableEntry& entry); + // override streambuf method #if wxHAS_TEXT_WINDOW_STREAM int overflow(int i) wxOVERRIDE; diff --git a/src/common/event.cpp b/src/common/event.cpp index 3e7f76f614..2faf29971c 100644 --- a/src/common/event.cpp +++ b/src/common/event.cpp @@ -1693,6 +1693,13 @@ void wxEvtHandler::DoBind(int id, wxDynamicEventTableEntry *entry = new wxDynamicEventTableEntry(eventType, id, lastId, func, userData); + // Check if the derived class allows binding such event handlers. + if ( !OnDynamicBind(*entry) ) + { + delete entry; + return; + } + if (!m_dynamicEvents) m_dynamicEvents = new DynamicEvents; diff --git a/src/common/textcmn.cpp b/src/common/textcmn.cpp index baefa1c8c6..2774635bbe 100644 --- a/src/common/textcmn.cpp +++ b/src/common/textcmn.cpp @@ -1194,6 +1194,21 @@ void wxTextCtrlBase::DoUpdateWindowUI(wxUpdateUIEvent& event) } } +bool wxTextCtrlBase::OnDynamicBind(wxDynamicEventTableEntry& entry) +{ + if ( entry.m_eventType == wxEVT_TEXT_ENTER ) + { + wxCHECK_MSG + ( + HasFlag(wxTE_PROCESS_ENTER), + false, + wxS("Must have wxTE_PROCESS_ENTER for wxEVT_TEXT_ENTER to work") + ); + } + + return wxControl::OnDynamicBind(entry); +} + // ---------------------------------------------------------------------------- // hit testing // ----------------------------------------------------------------------------