Added tooltips to cache explorer.
[zeroinstall.git] / zeroinstall / 0launch-gui / treetips.py
blobe6fde9452f04a5a07d40d108adaaea260f6127b4
1 import time, gobject, gtk
3 class TreeTips:
4 timeout = None
5 widget = None
6 item = None
7 time = 0
9 def show(self, parent):
10 if self.timeout:
11 gobject.source_remove(self.timeout)
12 self.timeout = None
14 if self.widget:
15 self.widget.destroy()
16 self.widget = None
18 if self.item is None:
19 return
21 text = self.get_tooltip_text(self.item)
22 if not text:
23 return
25 self.widget = gtk.Window(gtk.WINDOW_POPUP)
26 self.widget.set_app_paintable(True)
27 self.widget.set_name('gtk-tooltips')
29 self.widget.connect('expose-event', self.tooltip_draw)
31 label = gtk.Label(text)
32 label.set_line_wrap(True)
33 label.set_padding(4, 2)
34 self.widget.add(label)
35 label.show()
37 w, h = self.widget.size_request()
38 if hasattr(parent, 'get_screen'):
39 screen = parent.get_screen()
40 root = screen.get_root_window()
41 else:
42 root = gtk.gdk.get_default_root_window()
43 px, py, mask = gtk.gdk.Window.get_pointer(root)
45 #m = gtk.gdk.screen_get_default().get_monitor_at_point(px, py)
47 x = px - w / 2
48 y = py + 12
50 # Test if pointer is over the tooltip window
51 if py >= y and py <= y + h:
52 y = py - h - 2
53 self.widget.move(x, y)
54 self.widget.show()
56 self.widget.connect('destroy', self.tooltip_destroyed)
57 self.time = time.time()
59 def prime(self, parent, item):
60 self.hide()
61 assert self.timeout is None
62 self.item = item
64 now = time.time()
65 if now - self.time > 2:
66 delay = 1000
67 else:
68 delay = 100
70 self.timeout = gobject.timeout_add(delay, lambda: self.show(parent))
72 def tooltip_draw(self, widget, ev):
73 widget.window.draw_rectangle(widget.style.fg_gc[widget.state],
74 False, 0, 0,
75 widget.allocation.width - 1,
76 widget.allocation.height - 1)
78 def tooltip_destroyed(self, widget):
79 pass
81 def hide(self):
82 self.item = None
83 self.show(None)
85 def get_tooltip_text(self, item):
86 "Override in subclasses."
87 return str(item)