evt.GetPosition() in wx.ScrolledWindow - windows vs. gtk

Hi all,
I just stumbled on a possible cross platform inconsistency regarding
evt.GetPosition() (wx.EVT_LEFT_UP).
I need to get the position, where the image (placed in a
ScrolledWindow) was clicked on.
on windows (XPp SP3) I used the obvious evt.GetPosition(), but on
Linux (Kubuntu 8.04) the scrolled offset isn't taken into account.
(wxpython 2.8.10.1).

The attached file should demonstrate the problem. After scrolling the
image a bit and clicking on it, the clicked position relative to the
image is shown in title bar.
On windows both values are the same, on linux the second one is
relative to the visible rectangle (even on if the image is scrolled
away from the initial position).

A replacement:
evt.GetEventObject().ScreenToClient(wx.GetMousePosition())
seems to be working identically on both platforms.

Did I used the event method wrongly or is it a known issue?
Aren't there any drawbacks of the workaround, which I am missing now?

Thanks in advance,
       Vlasta

ScrolledWindow_evt_position.py (794 Bytes)

···

##############################################

#! Python
# -*- coding: utf-8 -*-

import wx

def test_mouse_pos(evt):
    bmp_mouse_pos = evt.GetEventObject().ScreenToClient(wx.GetMousePosition())
    evt_pos = evt.GetPosition()
    frm.SetTitle("mouse-pos: " + str(bmp_mouse_pos)+ " - evt-pos: " +
str(evt_pos))
    evt.Skip()

appl = wx.App(redirect=False)
frm = wx.Frame(None, -1, "ShowPosition - test")
scr_win = wx.ScrolledWindow(frm)
scr_win.SetScrollbars(1, 1, 2000, 2000, 0, 0)
static_bmp = wx.StaticBitmap(scr_win, -1,
wx.ArtProvider.GetBitmap(wx.ART_NORMAL_FILE, size=(1820,1800)))
scr_win.SetScrollbars(1, 1, 2000, 2000, 0, 0)
img_sizer = wx.BoxSizer(wx.VERTICAL)
img_sizer.Add(static_bmp)
scr_win.SetSizer(img_sizer)
static_bmp.Bind(wx.EVT_LEFT_UP, test_mouse_pos)
frm.Show()
appl.MainLoop()

##############################################

Actually, it's probably Windows that is wrong here. Mouse events are always in device coordinantes so to convert to the logical coordinates (which take the scrolling into account.) You can convert using the scrolled window's CalcUnscrolledPosition method.

···

On 10/22/09 8:42 AM, Vlastimil Brom wrote:

Hi all,
I just stumbled on a possible cross platform inconsistency regarding
evt.GetPosition() (wx.EVT_LEFT_UP).
I need to get the position, where the image (placed in a
ScrolledWindow) was clicked on.
on windows (XPp SP3) I used the obvious evt.GetPosition(), but on
Linux (Kubuntu 8.04) the scrolled offset isn't taken into account.
(wxpython 2.8.10.1).

--
Robin Dunn
Software Craftsman

Thanks for the answer Robin,
I thought, that the event position would be reported with respect to
the event object (StaticBitmap), not just the currently visible part
of it. But it doesn't really matter for me, which implementation is
correct; I rather wanted to find out, whether there can be any
problems with using the converted mouse position
evt.GetEventObject().ScreenToClient(wx.GetMousePosition())

I'd prefer to use the event object directly, as the event handler has
direct access to it. Its ScreenToClient method apparently reflects the
scrolled position on both platforms.

regards,
   Vlasta

···

2009/10/23 Robin Dunn <robin@alldunn.com>:

On 10/22/09 8:42 AM, Vlastimil Brom wrote:

Hi all,
I just stumbled on a possible cross platform inconsistency regarding
evt.GetPosition() (wx.EVT_LEFT_UP).
I need to get the position, where the image (placed in a
ScrolledWindow) was clicked on.
on windows (XPp SP3) I used the obvious evt.GetPosition(), but on
Linux (Kubuntu 8.04) the scrolled offset isn't taken into account.
(wxpython 2.8.10.1).

Actually, it's probably Windows that is wrong here. Mouse events are
always in device coordinantes so to convert to the logical coordinates
(which take the scrolling into account.) You can convert using the
scrolled window's CalcUnscrolledPosition method.

--
Robin Dunn
Software Craftsman
http://wxPython.org

>

Oops, sorry, I wasn't thinking about the StaticBitmap there. The reason you are seeing a difference is because on wxGTK the static bitmap is not a real widget, so the mouse event is really coming from the scrolled window. If you use the generic class in wx.lib.statbmp instead then you won't see any difference across platforms.

···

On 10/23/09 3:54 AM, Vlastimil Brom wrote:

2009/10/23 Robin Dunn<robin@alldunn.com>:

On 10/22/09 8:42 AM, Vlastimil Brom wrote:

Hi all,
I just stumbled on a possible cross platform inconsistency regarding
evt.GetPosition() (wx.EVT_LEFT_UP).
I need to get the position, where the image (placed in a
ScrolledWindow) was clicked on.
on windows (XPp SP3) I used the obvious evt.GetPosition(), but on
Linux (Kubuntu 8.04) the scrolled offset isn't taken into account.
(wxpython 2.8.10.1).

Actually, it's probably Windows that is wrong here. Mouse events are
always in device coordinantes so to convert to the logical coordinates
(which take the scrolling into account.) You can convert using the
scrolled window's CalcUnscrolledPosition method.

Thanks for the answer Robin,
I thought, that the event position would be reported with respect to
the event object (StaticBitmap), not just the currently visible part
of it.

--
Robin Dunn
Software Craftsman

Ok, thank you very much for the clarification;
greetings,
  Vlasta

···

2009/10/26 Robin Dunn <robin@alldunn.com>:

On 10/23/09 3:54 AM, Vlastimil Brom wrote:

2009/10/23 Robin Dunn<robin@alldunn.com>:

On 10/22/09 8:42 AM, Vlastimil Brom wrote:

Hi all,
I just stumbled on a possible cross platform inconsistency regarding
evt.GetPosition() (wx.EVT_LEFT_UP).
I need to get the position, where the image (placed in a
ScrolledWindow) was clicked on.
on windows (XPp SP3) I used the obvious evt.GetPosition(), but on
Linux (Kubuntu 8.04) the scrolled offset isn't taken into account.
(wxpython 2.8.10.1).

Actually, it's probably Windows that is wrong here. Mouse events are
always in device coordinantes so to convert to the logical coordinates
(which take the scrolling into account.) You can convert using the
scrolled window's CalcUnscrolledPosition method.

Thanks for the answer Robin,
I thought, that the event position would be reported with respect to
the event object (StaticBitmap), not just the currently visible part
of it.

Oops, sorry, I wasn't thinking about the StaticBitmap there. The reason
you are seeing a difference is because on wxGTK the static bitmap is not
a real widget, so the mouse event is really coming from the scrolled
window. If you use the generic class in wx.lib.statbmp instead then you
won't see any difference across platforms.

--
Robin Dunn
Software Craftsman
http://wxPython.org