treetips.py :  » Installer » Zero-Install » zeroinstall-injector-0.47 » zeroinstall » gtkui » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Installer » Zero Install 
Zero Install » zeroinstall injector 0.47 » zeroinstall » gtkui » treetips.py
"""Add tooltips to a TreeView."""
# Copyright (C) 2009, Thomas Leonard
# See the README file for details, or visit http://0install.net.

from zeroinstall import _
import time, gobject, gtk

class TreeTips:
  """This object allows you to set location-dependent tooltips on a TreeView.
  Connect your TreeView's leave-notify-event to the L{hide} method.
  In your motion-notify-event handler, call L{prime} when the pointer moves
  to an area with a new message. The message will be shown after a delay.
  If calculation of the message is expensive, override L{get_tooltip_text}
  instead.
  """
  timeout = None
  widget = None
  item = None
  time = 0

  def show(self, parent):
    if self.timeout:
      gobject.source_remove(self.timeout)
      self.timeout = None

    if self.widget:
      self.widget.destroy()
      self.widget = None

    if self.item is None:
      return

    text = self.get_tooltip_text()
    if not text:
      return

    self.widget = gtk.Window(gtk.WINDOW_POPUP)
    self.widget.set_app_paintable(True)
    self.widget.set_name('gtk-tooltips')

    self.widget.connect('expose-event', self.tooltip_draw)

    label = gtk.Label(text)
    label.set_line_wrap(True)
    label.set_padding(4, 2)
    self.widget.add(label)
    label.show()

    w, h = self.widget.size_request()
    if hasattr(parent, 'get_screen'):
      screen = parent.get_screen()
      root = screen.get_root_window()
    else:
      root = gtk.gdk.get_default_root_window()
    px, py, mask = gtk.gdk.Window.get_pointer(root)

    #m = gtk.gdk.screen_get_default().get_monitor_at_point(px, py)
    
    x = px - w / 2
    y = py + 12

    # Test if pointer is over the tooltip window
    if py >= y and py <= y + h:
      y = py - h - 2
    self.widget.move(x, y)
    self.widget.show()

    self.widget.connect('destroy', self.tooltip_destroyed)
    self.time = time.time()
  
  def prime(self, parent, item):
    """Call this whenever the pointer moves to an area with a different
    tooltip.
    @param parent: the TreeView widget
    @param item: the text to display
    @see L{get_tooltip_text}"""
    self.hide()
    assert self.timeout is None
    self.item = item
  
    now = time.time()
    if now - self.time > 2:
      delay = 1000
    else:
      delay = 100

    self.timeout = gobject.timeout_add(delay, lambda: self.show(parent))

  def tooltip_draw(self, widget, ev):
    widget.window.draw_rectangle(widget.style.fg_gc[widget.state],
          False, 0, 0,
          widget.allocation.width - 1,
          widget.allocation.height - 1)

  def tooltip_destroyed(self, widget):
    pass
  
  def hide(self):
    """Hide the tooltip, if any.
    Sets L{item} to None."""
    self.item = None
    self.show(None)
  
  def get_tooltip_text(self):
    """"Converts the object passed to L{prime} to a string for display.
    The default implementation just calls C{str}, but subclasses can override it.
    @return: the tooltip message"""
    return str(self.item)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.