Hello,
I have a class MyGrid
derived from wx.grid.Grid
that through standard layout managers is inside a wx.ScrolledWindow
.
I would like MyGrid not to react to mouse wheel events and instead propagate them up to the ScrolledWindow.
Currently I achieve this by catching the event in MyGrid self.Bind(wx.EVT_MOUSEWHEEL, self._on_mouse_wheel)
and then sending appropriate commands to the ScrolledWindow:
def _on_mouse_wheel(self, event):
''' Event listener on mouse scroll when the grid is embedded inside a Scrolled Window. It
makes the scroll window scroll, instead of the grid itself '''
delta = event.GetWheelDelta()
rotation = event.GetWheelRotation()
linesPer = event.GetLinesPerAction()
wheelAxis = 0 if event.GetWheelAxis() == wx.MOUSE_WHEEL_HORIZONTAL else 1
mws = self._mouseWheelScroll[wheelAxis]
mws = mws + rotation
lines = mws / delta
mws = mws - lines * delta
self._mouseWheelScroll[wheelAxis] = mws
if lines != 0:
lines = lines * linesPer
viewStart = self.scrollWin.GetViewStart()
if wheelAxis == 0: # notice the different sign in horizontal and vertical logic
self.scrollWin.Scroll(int(viewStart[wheelAxis] + lines), -1)
else:
self.scrollWin.Scroll(-1, int(viewStart[wheelAxis] - lines))
This works but I feel it would be cleaner to implement _on_mouse_wheel
in a way that it just forwarded the received mouse wheel event (or an equivalent event) to the scrolled window instead. But I have not succeeded.
I have tried self.scrollWin.AddPendingEvent(event)
but it does nothing. I have tried passing event.Clone()
instead of the original event as well as wx.PostEvent
but none of them does anything.
What is the right way to forward the event to the ancestor window? Thank you