wxDataViewListCtrl : Using HitTest() to get row

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
BlakJak888
In need of some credit
In need of some credit
Posts: 9
Joined: Tue Mar 02, 2021 12:54 pm

wxDataViewListCtrl : Using HitTest() to get row

Post by BlakJak888 »

wxWidgets version 3.1.5#4 on Visual Studio vcpkg

Windows 10 Platform

C++20 development.

I am trying to get the row that a right-click context menu was clicked on. The row is not necessarily selected so using GetSelectedRow() is not an option.

My idea is to use HitTest() to get the item that was clicked and then use ItemToRow() to get the row.

My code below has an exception at the call to HitTest().

Code: Select all

       wxDataViewItem      item;
        wxDataViewColumn*   colPtr = nullptr;

        wxDataViewListCtrl* dv = static_cast<wxDataViewListCtrl*>(e.GetEventObject());
        wxPoint             scrP = wxGetMousePosition();
        wxPoint             dvP  = dv->ScreenToClient(wxGetMousePosition());
        dv->HitTest(dvP, item, colPtr);
        int row = dv->ItemToRow(item);
I am getting a Read Access Violation from the internal call to wxControl::MSWFindItem(long id, HWND__ *hWnd). This function is directly called by HitTest().

Another thing that is strange is that the two points scrP and dvP return the exact same coordinates which does not seem right at all.

It makes me feel that the dv pointer is not really a true wxDataViewListCtrl object, however I use the exact same code in other handlers to read the values from selected rows so it does seem to work.

Looking for some help or advice.
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxDataViewListCtrl : Using HitTest() to get row

Post by doublemax »

Can you show the code where you connect the event handler?
Use the source, Luke!
BlakJak888
In need of some credit
In need of some credit
Posts: 9
Joined: Tue Mar 02, 2021 12:54 pm

Re: wxDataViewListCtrl : Using HitTest() to get row

Post by BlakJak888 »

I eventually figured it out. I was calling the HitTest() from the event triggered by the Popup MenuItem being clicked and not the actual "right-click" event which pops up the menu in the first place.

The solution is to save the row info at the time the right-click event occurs and then use that info later in the events that get called when the menu items are selected.
BlakJak888
In need of some credit
In need of some credit
Posts: 9
Joined: Tue Mar 02, 2021 12:54 pm

Re: wxDataViewListCtrl : Using HitTest() to get row

Post by BlakJak888 »

... and I should add that the exception was caused by exactly what I thought. The event object returned from GetEventObject() in the code above does not actually return a DataViewListCtrl. Frankly I am not sure what object is passed with the event when a Popup menu is called. Perhaps someone could clarify that for me.

Anyway, the code above works when it is called from the correct event. #-o
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxDataViewListCtrl : Using HitTest() to get row

Post by doublemax »

Tip: Instead of calling PopupMenu(), you can also call GetPopupMenuSelectionFromUser() which is a modal call and returns the ID of the selected menu entry. That way you don't need to store the row number somewhere, as everything happens in the same method.
Use the source, Luke!
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7459
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: wxDataViewListCtrl : Using HitTest() to get row

Post by ONEEYEMAN »

Hi,
Out of curiosity - what event you are trying to catch that you need to call HitTest()?
wxDataViewCtrl has its own events that you can handle and you don't need to use HitTest() all the info is provided in the event...

Thank you.
BlakJak888
In need of some credit
In need of some credit
Posts: 9
Joined: Tue Mar 02, 2021 12:54 pm

Re: wxDataViewListCtrl : Using HitTest() to get row

Post by BlakJak888 »

@doublemax - thanks for pointing that out. I will read up on it tomorrow morning.

@oneeyeman - this is the click of the menuitem on the richt-click context menu for a DataViewListCtrl. Some actions on the menu are tied specifically to the row that the user right clicked on. The problem is that the usual function GetSelectedRow() does not function unless the user has actually selected the row first. Using HitTest() you can get the row where the mouse was even if the row is not selected.
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7459
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: wxDataViewListCtrl : Using HitTest() to get row

Post by ONEEYEMAN »

Hi,
What I'm saying is that you should handle EVT_DATAVIEW_ITEM_CONTEXT_MENU. Are you?
And then you simply use event.GetItem() to get the item where the right click occur.

Thank you.
BlakJak888
In need of some credit
In need of some credit
Posts: 9
Joined: Tue Mar 02, 2021 12:54 pm

Re: wxDataViewListCtrl : Using HitTest() to get row

Post by BlakJak888 »

@oneeyeman - Thanks for that tip. I was not aware I could get the item directly from the event so that does mean the HitTest() call is not required.
Post Reply