Incorrect popup menu position in wxGrid inside notebook Topic is solved

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
cookielau
Knows some wx things
Knows some wx things
Posts: 35
Joined: Mon May 22, 2023 9:21 am

Incorrect popup menu position in wxGrid inside notebook

Post by cookielau »

Hi all:

I'm writing an application using wxGrid inside a notebook in a frame, the code is like this:

Code: Select all

#include <wx/wx.h>
#include <wx/grid.h>
#include <wx/notebook.h>

class MyFrame : public wxFrame
{
public:
    MyFrame(wxWindow* parent = nullptr) : wxFrame(parent, wxID_ANY, "Test")
    {
        wxNotebook* notebook = new wxNotebook(this, wxID_ANY);

        wxPanel* page = new wxPanel(notebook);
        wxGrid* grid = new wxGrid(page, wxID_ANY);
        grid_ = grid;
        grid->CreateGrid(50, 50);
        notebook->AddPage(page, wxString::Format("Page 1"), true);
        page->Bind(wxEVT_SIZE, [page, grid](wxSizeEvent& e)
            {
                grid->SetSize(page->GetClientSize());
                e.Skip();
            });
        page->Bind(wxEVT_GRID_CELL_RIGHT_CLICK, [grid, this](wxGridEvent& event)
            {
                wxMenu popup_menu;
                popup_menu.SetTitle("Popup Menu");
                popup_menu.AppendSeparator();
                const int rc = GetPopupMenuSelectionFromUser(popup_menu, ScreenToClient(event.GetPosition()));
                event.Skip();
            });
    }
private:
    wxGrid* grid_;
};

class MyApp : public wxApp
{
public:
    bool OnInit() override
    {
        (new MyFrame())->Show();
        return true;
    }
}; wxIMPLEMENT_APP(MyApp);
I wanna setup a popup menu when right click on the cell inside the grid to trigger some event, but finding the popup menu's left top position is not where my mouse is. I tried to find some experience from the wxWidgets sample and really find a function `ScreenToClient` in the sample/menu. But it is still not correct for me, could you please help me solve this problem? and I also wonder why this function works well in the sample but not in my app.

Thanks in advance.
Windows 10, wxWidgits v3.2.2.1
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Incorrect popup menu position in wxGrid inside notebook

Post by doublemax »

If you don't pass any position, it should work correctly.

Otherwise you need to call grid->ScreenToClient(event.GetPosition()), because the coordinates in the event object are relative to the grid. If you just call ScreenToClient(), it will be called for the frame, and the coordinates will be offset.
Use the source, Luke!
cookielau
Knows some wx things
Knows some wx things
Posts: 35
Joined: Mon May 22, 2023 9:21 am

Re: Incorrect popup menu position in wxGrid inside notebook

Post by cookielau »

doublemax wrote: Tue Jun 06, 2023 5:33 am If you don't pass any position, it should work correctly.

Otherwise you need to call grid->ScreenToClient(event.GetPosition()), because the coordinates in the event object are relative to the grid. If you just call ScreenToClient(), it will be called for the frame, and the coordinates will be offset.
The first approaching which uses the default parameter works well!
While the second solution seems not working.

Anyway I'll leave the parameter blank in my use.

thanks doublemax, you made my day again!
Windows 10, wxWidgits v3.2.2.1
User avatar
doublemax@work
Super wx Problem Solver
Super wx Problem Solver
Posts: 474
Joined: Wed Jul 29, 2020 6:06 pm
Location: NRW, Germany

Re: Incorrect popup menu position in wxGrid inside notebook

Post by doublemax@work »

cookielau wrote: Tue Jun 06, 2023 6:16 am While the second solution seems not working
Sorry, i didn't read it thoroughly enough.

First of all ScreenToClient() would need to be ClientToScreen(), but then again that would only work when the method GetPopupMenuSelectionFromUser() would take screen coordinates, which it doesn't.
cookielau
Knows some wx things
Knows some wx things
Posts: 35
Joined: Mon May 22, 2023 9:21 am

Re: Incorrect popup menu position in wxGrid inside notebook

Post by cookielau »

doublemax@work wrote: Tue Jun 06, 2023 7:25 am First of all ScreenToClient() would need to be ClientToScreen(), but then again that would only work when the method GetPopupMenuSelectionFromUser() would take screen coordinates, which it doesn't.
Thanks for your prompt reply again, the following code works as well, you are my hero!

Code: Select all

GetPopupMenuSelectionFromUser(popup_menu, ScreenToClient(grid->ClientToScreen(event.GetPosition())));
Windows 10, wxWidgits v3.2.2.1
User avatar
doublemax@work
Super wx Problem Solver
Super wx Problem Solver
Posts: 474
Joined: Wed Jul 29, 2020 6:06 pm
Location: NRW, Germany

Re: Incorrect popup menu position in wxGrid inside notebook

Post by doublemax@work »

If you want to pass explicit coordinates, this should be the easiest way:

Code: Select all

grid->GetPopupMenuSelectionFromUser(popup_menu, event.GetPosition());
cookielau
Knows some wx things
Knows some wx things
Posts: 35
Joined: Mon May 22, 2023 9:21 am

Re: Incorrect popup menu position in wxGrid inside notebook

Post by cookielau »

doublemax@work wrote: Tue Jun 06, 2023 9:24 am If you want to pass explicit coordinates, this should be the easiest way:

Code: Select all

grid->GetPopupMenuSelectionFromUser(popup_menu, event.GetPosition());
Thank you again for your brilliant brain!
Windows 10, wxWidgits v3.2.2.1
Post Reply