DataViewCtrl: how to get the item under the mouse cursor? (hit test)

I’m trying to compute the DataViewItem that is under the mouse cursor: in ListCtrl that is done through the HitTest method, but the corresponding method for DataViewCtrl seems to behave differently, and I haven’t understood how to use it. The following script, using a DataViewListCtrl, is as far as I’ve managed to get, can anybody help me? Thank you.

···

#########################################
import wx
import wx.dataview

def test():
pos = view.ScreenToClient(wx.GetMousePosition())
item = view.RowToItem(0)
col = view.GetColumn(0)
print(col.class)
# How to get the item under the mouse cursor?
print(view.HitTest(pos, item, col))
#timer.Restart()

app = wx.App()
frame = wx.Frame(None)
view = wx.dataview.DataViewListCtrl(frame, style=wx.dataview.DV_MULTIPLE)
column = view.AppendTextColumn(“Text”, 0)
view.AppendItem((‘Test1’, ))
view.AppendItem((‘Test2’, ))
view.AppendItem((‘Test3’, ))
timer = wx.CallLater(3000, test)
frame.Centre()
frame.Show()
app.MainLoop()
#########################################

Hi,

I'm trying to compute the DataViewItem that is under the mouse cursor: in ListCtrl that is done through the HitTest method, but the corresponding method for DataViewCtrl seems to behave differently, and I haven't understood how to use it. The following script, using a DataViewListCtrl, is as far as I've managed to get, can anybody help me? Thank you.

I gave it a shot and with 2.9.5 I get this exception:

File "h:\devProjectsT\aaTests\aaMisc\dariogiova\dvchitest.py", line 26, in <module>
   app.MainLoop()
File "c:\Python27\Lib\site-packages\wx-2.9.5-msw\wx\_core.py", line 8660, in MainLoop
   wx.PyApp.MainLoop(self)
File "c:\Python27\Lib\site-packages\wx-2.9.5-msw\wx\_core.py", line 7952, in MainLoop
   return _core_.PyApp_MainLoop(*args, **kwargs)
File "c:\Python27\Lib\site-packages\wx-2.9.5-msw\wx\_misc.py", line 1367, in Notify
   self.notify()
File "c:\Python27\Lib\site-packages\wx-2.9.5-msw\wx\_core.py", line 16870, in Notify
   self.result = self.callable(*self.args, **self.kwargs)
File "h:\devProjectsT\aaTests\aaMisc\dariogiova\dvchitest.py", line 12, in test
   print(view.HitTest(pos, item, col))
File "c:\Python27\Lib\site-packages\wx-2.9.5-msw\wx\dataview.py", line 1817, in HitTest
   return _dataview.DataViewCtrl_HitTest(*args, **kwargs)

TypeError: in method 'DataViewCtrl_HitTest', expected argument 4 of type 'wxDataViewColumn *&'

even though 'col' at that time is an instance of 'wx.dataview.DataViewColumn

If I run it against Phoenix (3.0.1.dev75864) I get:

File "h:\devProjectsT\aaTests\aaMisc\dariogiova\dvchitest.py", line 28, in <module>
   app.MainLoop()
File "c:\Python27\Lib\site-packages\wx-3.0.1-msw-phoenix\wx\core.py", line 1878, in MainLoop
   rv = wx.PyApp.MainLoop(self)
File "c:\Python27\Lib\site-packages\wx-3.0.1-msw-phoenix\wx\core.py", line 1940, in Notify
   self.notify()
File "c:\Python27\Lib\site-packages\wx-3.0.1-msw-phoenix\wx\core.py", line 3017, in Notify
   self.result = self.callable(*self.args, **self.kwargs)
File "h:\devProjectsT\aaTests\aaMisc\dariogiova\dvchitest.py", line 14, in test
   print(view.HitTest(pos, item, col))

TypeError: DataViewCtrl.HitTest(): too many arguments

So, looks like there is a problem with HitTest.

Could you use instead 'EVT_DATAVIEW_ITEM_ACTIVATED'?

Werner

···

On 05/03/2014 16:12, dariogiova@gmail.com wrote:

Sorry, I should have mentioned that I’m using 3.0.0, which raises the same exception as 2.9.5. Is it a known bug? The only related report I’ve found is wxTrac has been migrated to GitHub Issues - wxWidgets which seems farily old, though still open.

Should ‘EVT_DATAVIEW_ITEM_ACTIVATED’ be trieggered when the mouse hovers over an item? The documentation says “This event is triggered by float clicking an item or pressing some special key (usually “Enter”) when it is focused”: I don’t know what “float clicking” is, but the only way I can trigger it seems to be by double-clicking the item, which is very far from what I need. What I’m trying to do is actually temporarily work around another bug, wxTrac has been migrated to GitHub Issues - wxWidgets while waiting for 3.0.1 to be released.

···

On Wednesday, March 5, 2014 11:45:37 PM UTC+8, werner wrote:

Hi,

On 05/03/2014 16:12, dario...@gmail.com wrote:

I’m trying to compute the DataViewItem that is under the mouse cursor:
in ListCtrl that is done through the HitTest method, but the
corresponding method for DataViewCtrl seems to behave differently, and
I haven’t understood how to use it. The following script, using a
DataViewListCtrl, is as far as I’ve managed to get, can anybody help
me? Thank you.

I gave it a shot and with 2.9.5 I get this exception:

File “h:\devProjectsT\aaTests\aaMisc\dariogiova\dvchitest.py”, line 26,
in

app.MainLoop()

File “c:\Python27\Lib\site-packages\wx-2.9.5-msw\wx_core.py”, line
8660, in MainLoop

wx.PyApp.MainLoop(self)

File “c:\Python27\Lib\site-packages\wx-2.9.5-msw\wx_core.py”, line
7952, in MainLoop

return core.PyApp_MainLoop(*args, **kwargs)

File “c:\Python27\Lib\site-packages\wx-2.9.5-msw\wx_misc.py”, line
1367, in Notify

self.notify()

File “c:\Python27\Lib\site-packages\wx-2.9.5-msw\wx_core.py”, line
16870, in Notify

self.result = self.callable(*self.args, **self.kwargs)

File “h:\devProjectsT\aaTests\aaMisc\dariogiova\dvchitest.py”, line 12,
in test

print(view.HitTest(pos, item, col))

File “c:\Python27\Lib\site-packages\wx-2.9.5-msw\wx\dataview.py”, line
1817, in HitTest

return _dataview.DataViewCtrl_HitTest(*args, **kwargs)

TypeError: in method ‘DataViewCtrl_HitTest’, expected argument 4 of type
‘wxDataViewColumn *&’

even though ‘col’ at that time is an instance of 'wx.dataview.DataViewColumn

If I run it against Phoenix (3.0.1.dev75864) I get:

File “h:\devProjectsT\aaTests\aaMisc\dariogiova\dvchitest.py”, line 28,
in

app.MainLoop()

File “c:\Python27\Lib\site-packages\wx-3.0.1-msw-phoenix\wx\core.py”,
line 1878, in MainLoop

rv = wx.PyApp.MainLoop(self)

File “c:\Python27\Lib\site-packages\wx-3.0.1-msw-phoenix\wx\core.py”,
line 1940, in Notify

self.notify()

File “c:\Python27\Lib\site-packages\wx-3.0.1-msw-phoenix\wx\core.py”,
line 3017, in Notify

self.result = self.callable(*self.args, **self.kwargs)

File “h:\devProjectsT\aaTests\aaMisc\dariogiova\dvchitest.py”, line 14,
in test

print(view.HitTest(pos, item, col))

TypeError: DataViewCtrl.HitTest(): too many arguments

So, looks like there is a problem with HitTest.

Could you use instead ‘EVT_DATAVIEW_ITEM_ACTIVATED’?

Werner

Hi,

...

Sorry, I should have mentioned that I'm using 3.0.0, which raises the same exception as 2.9.5. Is it a known bug?

It looks like but I am not sure.

Yes, providing the version one uses is always good and if you get an exception you should include it too, as often that is enough to help.

...

Should 'EVT_DATAVIEW_ITEM_ACTIVATED' be trieggered when the mouse hovers over an item? The documentation says "This event is triggered by float clicking an item or pressing some special key (usually “Enter”) when it is focused": I don't know what "float clicking" is, but the only way I can trigger it seems to be by double-clicking the item, which is very far from what I need. What I'm trying to do is actually temporarily work around another bug, wxTrac has been migrated to GitHub Issues - wxWidgets while waiting for 3.0.1 to be released.

No it will not trigger on mouse hover, only when an item is 'selected', most of the time that is when the user would expect something to happen.

Werner

···

On 05/03/2014 17:01, dariogiova@gmail.com wrote:

On Wednesday, March 5, 2014 11:45:37 PM UTC+8, werner wrote:

I see, thanks for your help. I thought it was me doing something wrong. Here’s my exception:

···

On Thursday, March 6, 2014 12:07:25 AM UTC+8, werner wrote:

Hi,

On 05/03/2014 17:01, dario...@gmail.com wrote:

On Wednesday, March 5, 2014 11:45:37 PM UTC+8, werner wrote:

Sorry, I should have mentioned that I’m using 3.0.0, which raises the
same exception as 2.9.5. Is it a known bug?

It looks like but I am not sure.

Yes, providing the version one uses is always good and if you get an
exception you should include it too, as often that is enough to help.

########################
Traceback (most recent call last):
File “/usr/lib/python2.7/site-packages/wx-3.0-gtk2/wx/_misc.py”, line 1367, in Notify
self.notify()
File “/usr/lib/python2.7/site-packages/wx-3.0-gtk2/wx/_core.py”, line 16866, in Notify
self.result = self.callable(*self.args, **self.kwargs)
File “test_dvlistc3.py”, line 10, in test
print(view.HitTest(pos, item, col))
File “/usr/lib/python2.7/site-packages/wx-3.0-gtk2/wx/dataview.py”, line 1817, in HitTest
return _dataview.DataViewCtrl_HitTest(*args, **kwargs)
TypeError: in method ‘DataViewCtrl_HitTest’, expected argument 4 of type ‘wxDataViewColumn *&’
########################

My system is:

Arch Linux x86_64

GTK+ 2.24.22

wxGTK 3.0.0

wxPython 3.0.0.0

Werner wrote:

Hi,

...

Sorry, I should have mentioned that I'm using 3.0.0, which raises the
same exception as 2.9.5. Is it a known bug?

It looks like but I am not sure.

Sorta. The method wrapper will need to be rewritten (Pythonized) to return the item and col as a tuple instead of expecting to pass by reference through the parameters.

···

On 05/03/2014 17:01, dariogiova@gmail.com wrote:

On Wednesday, March 5, 2014 11:45:37 PM UTC+8, werner wrote:

--
Robin Dunn
Software Craftsman

I am trying to use DataViewCtrl and I am quite happy with it.

Drag and Drop seems to work as well, but now I struggle to find on which DataViewItem the drop target is.

As mentioned above, HitTest throws this error:

TypeError: in method ‘DataViewCtrl_HitTest’, expected argument 4 of type ‘wxDataViewColumn *&’

because this is not wrapped yet.

Can I workaround this by providing a memoryview/buffer/pointer myself until this gets fixed?

With best regards

···

On Saturday, March 15, 2014 5:51:43 AM UTC+1, Robin Dunn wrote:

like but I am not sure.

Sorta. The method wrapper will need to be rewritten (Pythonized) to
return the item and col as a tuple instead of expecting to pass by
reference through the parameters.

hmm, I tried passing col.this, as well as trying int(col.this) with no luck.
:frowning:

···

On Wednesday, March 5, 2014 7:12:23 AM UTC-8, dario...@gmail.com wrote:

I’m trying to compute the DataViewItem that is under the mouse cursor: in ListCtrl that is done through the HitTest method, but the corresponding method for DataViewCtrl seems to behave differently, and I haven’t understood how to use it. The following script, using a DataViewListCtrl, is as far as I’ve managed to get, can anybody help me? Thank you.

#########################################
import wx
import wx.dataview

def test():
pos = view.ScreenToClient(wx.GetMousePosition())
item = view.RowToItem(0)
col = view.GetColumn(0)
print(col.class)
# How to get the item under the mouse cursor?
print(view.HitTest(pos, item, col))
#timer.Restart()

app = wx.App()
frame = wx.Frame(None)
view = wx.dataview.DataViewListCtrl(frame, style=wx.dataview.DV_MULTIPLE)
column = view.AppendTextColumn(“Text”, 0)
view.AppendItem((‘Test1’, ))
view.AppendItem((‘Test2’, ))
view.AppendItem((‘Test3’, ))
timer = wx.CallLater(3000, test)
frame.Centre()
frame.Show()
app.MainLoop()
#########################################

In wxPython/trunk/src/dataview.i in line 2419 there is already the telling comment:
// TODO: Should probably change this to return the item and col as a tuple…)
virtual void HitTest( const wxPoint & point, wxDataViewItem &item, wxDataViewColumn* &column ) const;

``

As I have no idea whatsoever how to tell SWIG how to do that, can you give a short explanation what to change in the SWIG-file to get the behaviour in the comment, or is this very difficult to describe?
With thanks in advance

···

On Saturday, March 15, 2014 5:51:43 AM UTC+1, Robin Dunn wrote:

Sorta. The method wrapper will need to be rewritten (Pythonized) to
return the item and col as a tuple instead of expecting to pass by
reference through the parameters.

nepix32 wrote:

    Sorta. The method wrapper will need to be rewritten (Pythonized) to
    return the item and col as a tuple instead of expecting to pass by
    reference through the parameters.

In wxPython/trunk/src/dataview.i
<wxTrac has been migrated to GitHub Issues - wxWidgets;
in line 2419 there is already the telling comment:
>>
// TODO: Should probably change this to return the item and col as a
tuple...)
virtualvoidHitTest(constwxPoint &point,wxDataViewItem
&item,wxDataViewColumn*&column )const;

As I have no idea whatsoever how to tell SWIG how to do that, can you
give a short explanation what to change in the SWIG-file to get the
behaviour in the comment, or is this very difficult to describe?

Thanks for the reminder. I've just implemented it for the CLassic 3.0.2 release.

···

On Saturday, March 15, 2014 5:51:43 AM UTC+1, Robin Dunn wrote:

--
Robin Dunn
Software Craftsman

Thank you Robin, does that fix wxTrac has been migrated to GitHub Issues - wxWidgets ?
And, if I understand well, would that provide a way to get the current scroll position of the DataViewCtrl by using the top coordinates of that window? (It was the only problem left unsolved in https://groups.google.com/d/msg/wxpython-users/isAboUxaoFs/z9PRJClSPgwJ )

···

On 14/10/14 08:17, Robin Dunn wrote:

Thanks for the reminder. I've just implemented it for the CLassic 3.0.2 release.