Fixed errors in help text.
[zeroinstall.git] / treetips.py
blobe43d2aadbcf5a113f9dace6c61b53124dd67cf77
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 if item == self.item:
61 return
63 self.hide()
64 assert self.timeout is None
65 self.item = item
67 now = time.time()
68 if now - self.time > 2:
69 delay = 1000
70 else:
71 delay = 100
73 self.timeout = gobject.timeout_add(delay, lambda: self.show(parent))
75 def tooltip_draw(self, widget, ev):
76 widget.window.draw_rectangle(widget.style.fg_gc[widget.state],
77 False, 0, 0,
78 widget.allocation.width - 1,
79 widget.allocation.height - 1)
81 def tooltip_destroyed(self, widget):
82 pass
84 def hide(self):
85 self.item = None
86 self.show(None)