wxGrid and EVT_CONTEXT_MENU

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
silverstorm82
Knows some wx things
Knows some wx things
Posts: 32
Joined: Wed Aug 17, 2011 5:13 pm

wxGrid and EVT_CONTEXT_MENU

Post by silverstorm82 »

Hello folks,

did anyone got wxGrid to respond to the EVT_CONTEXT_MENU event-handler? I am using a derived class from wxGrid, declared an event table and used EVT_CONTEXT_MENU in it but the handler function is not called:

Code: Select all

class CMyGrid : public wxGrid
{
...
protected:
    void OnContextMenu(wxContextMenuEvent& event);
    DECLARE_EVENT_TABLE();
...
};



BEGIN_EVENT_TABLE(CMyGrid, wxGrid)
    EVT_CONTEXT_MENU(OnContextMenu);
END_EVENT_TABLE



void CMyGrid::OnContextMenu(wxContextMenuEvent& event)
{
    wxMessageBox(wxT("TEST"));
}
"TEST" is never displayed when clicking right on the grid.

Any ideas about this? There must be an error somewhere but I was not able to locate it :-( I also checked this forum but didn't find a solution for the problem.

Thanks in advance,
Jens
catalin
Moderator
Moderator
Posts: 1618
Joined: Wed Nov 12, 2008 7:23 am
Location: Romania

Re: wxGrid and EVT_CONTEXT_MENU

Post by catalin »

I think this is because the events are "eaten" by the wxGrid's components, while wxGrid emits its own events for most of the corresponding "normal" ones. Among the emitted events there is none for menu, but IIRC using wxEVT_GRID_CELL_RIGHT_CLICK and wxEVT_GRID_LABEL_RIGHT_CLICK will work around that limitation. It's not perfect as it will not work for the MSW's context menu button, but may be good enough.
silverstorm82
Knows some wx things
Knows some wx things
Posts: 32
Joined: Wed Aug 17, 2011 5:13 pm

Re: wxGrid and EVT_CONTEXT_MENU

Post by silverstorm82 »

Hi,

that was my first approach but I thought there may be another one as I read in the documentation that handling a wxContextMenuEvent is the better approach as there are different ways of how context menus are handled on the various platforms.

Greetings,
Jens
Post Reply