wxGrid and right click row info.

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
dkaip
Super wx Problem Solver
Super wx Problem Solver
Posts: 334
Joined: Wed Jan 20, 2010 1:15 pm

wxGrid and right click row info.

Post by dkaip »

Hello. I have a wxGrid and with right click choosing Row message... item i want to take the row and col of cell.
When press right click no cell or row selecting.
With code bellow i take 1378932544. How must tell to give me the row or column?

When i use

Code: Select all

wxMessageBox(wxString::Format(wxT("%i"),evt.GetRow()));
program crashes. What i am doing wrong?
Thanks
Jim.

Code: Select all

void myFrame::on_RClick( wxGridEvent& event )
{
    wxMenu* menu = new wxMenu;
    menu->Append(1, _("&Row message... "));
    menu->Connect(wxEVT_COMMAND_MENU_SELECTED,  wxGridEventHandler(myFrame::OnPopupClick), NULL, this);
    PopupMenu(menu);
}

void myFrame::OnPopupClick(wxGridEvent& evt)
{
    if(evt.GetId()==1)
    {
wxPoint point = evt.GetPosition();
        wxMessageBox(wxString::Format(wxT("%i"),point.y));
}
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7459
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: wxGrid and right click row info.

Post by ONEEYEMAN »

Hi,
You need one or the other.
You 2 handlers does not know about each other and, therefore, wxWidgets generate new event for OnPopupClick().

Thank you.
dkaip
Super wx Problem Solver
Super wx Problem Solver
Posts: 334
Joined: Wed Jan 20, 2010 1:15 pm

Re: wxGrid and right click row info.

Post by dkaip »

When rclick on wxGrid, triggers on_RClick and popup menu it happens.
Ok, then if a choose "Row message..." item then triggering OnPopupClick and we go to evt.GetId()==1 from OnPopupClick().
Here how event evt gives me the row for example of wxGrid, where rclick?
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7459
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: wxGrid and right click row info.

Post by ONEEYEMAN »

Hi,
What is "Row Message..."?

Thank you.
dkaip
Super wx Problem Solver
Super wx Problem Solver
Posts: 334
Joined: Wed Jan 20, 2010 1:15 pm

Re: wxGrid and right click row info.

Post by dkaip »

menu->Append(1, _("&Row message... "));
The popup menu with message..

Thank you.
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7459
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: wxGrid and right click row info.

Post by ONEEYEMAN »

Hi,
You should either have a row selected in the grid or work with the (number_of_rows + 1) row.

Now back to your original problem.
Have a function in you class that you will call in both cases.
Call this function when you do a right click directly. When you react on the menu selection, handle the wxCommandEvent, retrieve the row as approprisate and call the function.

BTW, I'm surprised the code is working at all. The menu selection shouldn't generate wxGridEvent.

Thank you.
dkaip
Super wx Problem Solver
Super wx Problem Solver
Posts: 334
Joined: Wed Jan 20, 2010 1:15 pm

Re: wxGrid and right click row info.

Post by dkaip »

You should either have a row selected in the grid
Sorry, no selected row, because another process happens on LClick.
work with the (number_of_rows + 1) row
i dont know how.
Call this function when you do a right click directly
, but in menu i have menu options, like open file etc.., so i can not have that.

With this code popup menu open ok, and on click an event occurs and OnPopupClick opens and all are ok.

My problem is to get row.

Thank you.
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7459
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: wxGrid and right click row info.

Post by ONEEYEMAN »

Hi,
So which row you want to get when you operate with a menu, if you don't have a current (selected) row?

Anyway do something like this:

Code: Select all

void myFrame::on_RClick( wxGridEvent& event )
{
    operate( 1 );
}

void myFrame::OnPopupClick(wxCommandEvent& evt)
{
    oiperate( 2 );
}

void myFrame::operate(int reason)
{
    swtch( reason )
    {
        case 1:
           wxMenu* menu = new wxMenu;
          menu->Append(1, _("&Row message... "));
          menu->Connect(wxEVT_COMMAND_MENU_SELECTED,  wxGridEventHandler(myFrame::OnPopupClick), NULL, this);
          PopupMenu(menu);
          break;
       case 2:
           int row = m_grid->GetNumberRows() + 1;
           // Do what needs to be done there
    }
}
Once again - selecting a menu is not generating wxGridEvent, but a generic wxCommandEvent.

Do not abuse the API - work with what is available.

BTW, the code you posted originally uses 2 different calls for wxMessageBox(). Which one is correct?

Thank you.
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxGrid and right click row info.

Post by doublemax »

You can get the row from the wxGridEvent. Then use GetPopupMenuSelectionFromUser() instead of PopupMenu(). It's a modal call and returns the ID of the clicked menu item.

http://docs.wxwidgets.org/trunk/classwx ... 682a61a93e
Use the source, Luke!
dkaip
Super wx Problem Solver
Super wx Problem Solver
Posts: 334
Joined: Wed Jan 20, 2010 1:15 pm

Re: wxGrid and right click row info.

Post by dkaip »

Once again - selecting a menu is not generating wxGridEvent, but a generic wxCommandEvent.
But wxGridEventHandler occur event and call OnPopupClick on id==1.
The big problem is that i have not current selection row, so wxPoint must determine the row.
In debug mode if i see event, there is nothing inside to explain the row.
the code you posted originally uses 2 different calls for wxMessageBox(). Which one is correct?
There is no correct, all are. Just trying to find row or column.
Then use GetPopupMenuSelectionFromUser() instead of PopupMenu(). It's a modal call and returns the ID of the clicked menu item.
Unknoun GetPopupMenuSelectionFromUser() function is for me. I just find it, i will try later, thank you.

But with my code i take id==1 ok, so i don't look for id.
Event has nothing inside it, because there is not selected cell.
So am trying ...

Code: Select all

menu->Connect(wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(myFrame::OnPopupClick), NULL, this);
   PopupMenu( menu, point);

}

void myFrame::OnPopupClick(wxCommandEvent& evt)
{
    if(evt.GetId()==1)
    {
    .... if find something ...
    }
    {
I know, is difficult problem...
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7459
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: wxGrid and right click row info.

Post by ONEEYEMAN »

Hi,
It may currently work, but there is no guarantee.
Yes, wxGridEvent is inherited from the wxCommandEvent, but there is a reason grid does not use wxCommandEvent - the latter is too generic.

Normal menu selection will never generate the wxGridEvent, because the menu is not the grid. It will however generate wxCommandEvent which you should handle. Then in the handler get the (new) row by calling GetNumberRows() + 1.

If in doubt - check the documentation.

Thank you.

BTW, if you don't have selected row at the time the user selected the menu - what row should be used? The new one? The old one?
Keep in mind - it is possible to insert a row in the grid in an arbitrary position...
Post Reply