[pygtk] URLs in treeviews

Tony Nelson tonynelson at georgeanelson.com
Sat Sep 9 23:39:14 WST 2006


At 11:24 AM +0300 9/9/06, Osmo Salomaa wrote:
>pe, 2006-09-08 kello 15:36 -0400, hvastani at vt.edu kirjoitti:
>  > but how do I make the cursor change to the shape of a hand ( like in
>  > web browsers ) when I mouse over this text.
>
>You can connect to the text view's motion-notify-event. Then, when the
>mouse pointer moves, you can get the pointer's coordinates, get the
>gtk.TextIter at that location and get the gtk.TextTags at that iter.
>Based on what those tags are, you can change the cursor.
>
>----------------------------------------
>def _on_text_view_motion_notify_event(self, text_view, event):
>
>     x, y = text_view.get_pointer()
>     x, y = text_view.window_to_buffer_coords(gtk.TEXT_WINDOW_TEXT, x, y)
>     tags = text_view.get_iter_at_location(x, y).get_tags()
>     window = text_view.get_window(gtk.TEXT_WINDOW_TEXT)
>     for tag in tags:
>         if tag in self._url_tags:
>             window.set_cursor(HAND_CURSOR)
>             return
>     window.set_cursor(NORMAL_CURSOR)
>----------------------------------------
>
>However, this is painfully slow! Maybe someone knows a better way?

When I do something similar, it is fast.  The main difference is that 
I give the tag a property name so I know it is a tag without having 
to look it up in my collection.  However, "tag in self._url_tags" 
should be fast enough if _url_tags is a dictionary and not a list.

Note that the event already has the pointer coords in event.x and 
event.y.  Use text_view.window_to_buffer_coords( 
gtk.TEXT_WINDOW_WIDGET, int(event.x), int(event.y) ) to convert them.

When text_view.get_iter_at_position() is available, I use it, as it 
does a better job at staying on the link.  This is especially 
apparent when there is a TAB character before the link.

As you are looking for tags within a larger widget, enter and leave 
won't work.  Motion is the correct way.
-- 
____________________________________________________________________
TonyN.:'                       <mailto:tonynelson at georgeanelson.com>
       '                              <http://www.georgeanelson.com/>


More information about the pygtk mailing list