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.
This commit is contained in:
Vadim Zeitlin 2020-01-07 16:44:00 +01:00
parent 3a09975465
commit 52416931a4
2 changed files with 13 additions and 3 deletions

View File

@ -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

View File

@ -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