From 52416931a4c682d5de92ec165b99fa09d56d6980 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Tue, 7 Jan 2020 16:44:00 +0100 Subject: [PATCH] Send wxEVT_CONTEXT_MENU to the main window of composite controls This is important to allow catching the context menu events from the composite control children at the main window level using the main window ID: previously, these events used the (typically auto-generated internally) ID of the child window, which was an implementation detail and prevented the code binding to these events using the ID of e.g. wxListCtrl itself from working under the other platforms, where wxListCtrl is a generic composite window, even if it worked under MSW, where wxListCtrl is native. --- include/wx/window.h | 3 +++ src/common/wincmn.cpp | 13 ++++++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/include/wx/window.h b/include/wx/window.h index f07c92d05d..972ef41d58 100644 --- a/include/wx/window.h +++ b/include/wx/window.h @@ -1514,6 +1514,9 @@ public: virtual bool SendIdleEvents(wxIdleEvent& event); // Send wxContextMenuEvent and return true if it was processed. + // + // Note that the event may end up being sent to a different window, if this + // window is part of a composite control. bool WXSendContextMenuEvent(const wxPoint& posInScreenCoords); // get the handle of the window for the underlying window system: this diff --git a/src/common/wincmn.cpp b/src/common/wincmn.cpp index 828b805ff4..0b744892fd 100644 --- a/src/common/wincmn.cpp +++ b/src/common/wincmn.cpp @@ -3071,9 +3071,16 @@ wxWindowBase::DoGetPopupMenuSelectionFromUser(wxMenu& menu, int x, int y) bool wxWindowBase::WXSendContextMenuEvent(const wxPoint& posInScreenCoords) { - wxContextMenuEvent evtCtx(wxEVT_CONTEXT_MENU, GetId(), posInScreenCoords); - evtCtx.SetEventObject(this); - return HandleWindowEvent(evtCtx); + // When the mouse click happens in a subwindow of a composite control, + // the user-visible event should seem to originate from the main window + // and, notably, use its ID and not the (usually auto-generated and so + // not very useful) ID of the subwindow. + wxWindow* const mainWin = GetMainWindowOfCompositeControl(); + + wxContextMenuEvent + evtCtx(wxEVT_CONTEXT_MENU, mainWin->GetId(), posInScreenCoords); + evtCtx.SetEventObject(mainWin); + return mainWin->HandleWindowEvent(evtCtx); } // methods for drawing the sizers in a visible way: this is currently only