Detecting a URL in a textctrl and changing cursor on GTK

There seem to be some differences between how a textctrl
(style=wx.TE_MULTILINE|wx.TE_RICH2|wx.TE_AUTO_URL) handles a URL
between GTK and Windows.

The native Windows text widget automatically changes the cursor from
an ibeam to a hand when hovering over the URL while on GTK this does
not happen natively (at least on several Ubuntu systems I've tested
on).

Also, Windows does not appear to send an EVT_TEXT_URL event until the
first mousedown over the URL while GTK sends the events when you hover
over the URL before any mousedown events although this difference
should be helpful in programmatically getting GTK to display a hand
when hovering over a URL, which is my question: what is the best way
to mirror the behaviour on GTK that takes place natively on Windows
where hovering over a URL changes the cursor from an ibeam to a hand.

The way to do this would seem to involve either EVT_SET_CURSOR or
EVT_MOTION, which are produced as you move the mouse within the
textctrl's window. An approach would seem to be that only within the
textctrl (since there are other controls on the page) you'd detect
EVT_SET_CURSOR and if it is accompanied by EVT_TEXT_URL then change
the cursor to the hand and if it's not leave it alone/change it back
to the ibeam. However, I am not sure how to detect an EVT_TEXT_URL
event when trapping first for an EVT_SET_CURSOR or EVT_MOTION event.

I hope that makes sense and any help would be appreciated.

Steve

Steve Zatz wrote:

There seem to be some differences between how a textctrl
(style=wx.TE_MULTILINE|wx.TE_RICH2|wx.TE_AUTO_URL) handles a URL
between GTK and Windows.

The native Windows text widget automatically changes the cursor from
an ibeam to a hand when hovering over the URL while on GTK this does
not happen natively (at least on several Ubuntu systems I've tested
on).

Also, Windows does not appear to send an EVT_TEXT_URL event until the
first mousedown over the URL while GTK sends the events when you hover
over the URL before any mousedown events although this difference
should be helpful in programmatically getting GTK to display a hand
when hovering over a URL, which is my question: what is the best way
to mirror the behaviour on GTK that takes place natively on Windows
where hovering over a URL changes the cursor from an ibeam to a hand.

The event object passed for EVT_TEXT_URL has a GetMouseEvent method that you can use to see what kind of mouse event triggered this URL event. IOW, you can look at event.GetMouseEvent().GetEventType() to figure out what happened and when to do things like change the cursor or launch the browser.

···

--
Robin Dunn
Software Craftsman

The event object passed for EVT_TEXT_URL has a GetMouseEvent method that
you can use to see what kind of mouse event triggered this URL event.
IOW, you can look at event.GetMouseEvent().GetEventType() to figure out
what happened and when to do things like change the cursor or launch the
browser.

So yes, using the MouseEvent of EVT_TEXT_URL allows you to change the
cursor to a hand when over a URL and to catch the mouseclick and open
a web browser, and catching EVT_MOTION allows you to turn the cursor
back to an ibeam when not over a URL. Thanks.