1 """Add tooltips to a TreeView."""
2 # Copyright (C) 2009, Thomas Leonard
3 # See the README file for details, or visit http://0install.net.
5 from zeroinstall
import _
6 import time
, gobject
, gtk
9 """This object allows you to set location-dependent tooltips on a TreeView.
10 Connect your TreeView's leave-notify-event to the L{hide} method.
11 In your motion-notify-event handler, call L{prime} when the pointer moves
12 to an area with a new message. The message will be shown after a delay.
13 If calculation of the message is expensive, override L{get_tooltip_text}
21 def show(self
, parent
):
23 gobject
.source_remove(self
.timeout
)
33 text
= self
.get_tooltip_text()
37 self
.widget
= gtk
.Window(gtk
.WINDOW_POPUP
)
38 self
.widget
.set_app_paintable(True)
39 self
.widget
.set_name('gtk-tooltips')
41 self
.widget
.connect('expose-event', self
.tooltip_draw
)
43 label
= gtk
.Label(text
)
44 label
.set_line_wrap(True)
45 label
.set_padding(4, 2)
46 self
.widget
.add(label
)
49 w
, h
= self
.widget
.size_request()
50 if hasattr(parent
, 'get_screen'):
51 screen
= parent
.get_screen()
52 root
= screen
.get_root_window()
54 root
= gtk
.gdk
.get_default_root_window()
55 px
, py
, mask
= gtk
.gdk
.Window
.get_pointer(root
)
57 #m = gtk.gdk.screen_get_default().get_monitor_at_point(px, py)
62 # Test if pointer is over the tooltip window
63 if py
>= y
and py
<= y
+ h
:
65 self
.widget
.move(x
, y
)
68 self
.widget
.connect('destroy', self
.tooltip_destroyed
)
69 self
.time
= time
.time()
71 def prime(self
, parent
, item
):
72 """Call this whenever the pointer moves to an area with a different
74 @param parent: the TreeView widget
75 @param item: the text to display
76 @see L{get_tooltip_text}"""
78 assert self
.timeout
is None
82 if now
- self
.time
> 2:
87 self
.timeout
= gobject
.timeout_add(delay
, lambda: self
.show(parent
))
89 def tooltip_draw(self
, widget
, ev
):
90 widget
.window
.draw_rectangle(widget
.style
.fg_gc
[widget
.state
],
92 widget
.allocation
.width
- 1,
93 widget
.allocation
.height
- 1)
95 def tooltip_destroyed(self
, widget
):
99 """Hide the tooltip, if any.
100 Sets L{item} to None."""
104 def get_tooltip_text(self
):
105 """"Converts the object passed to L{prime} to a string for display.
106 The default implementation just calls C{str}, but subclasses can override it.
107 @return: the tooltip message"""
108 return str(self
.item
)