home *** CD-ROM | disk | FTP | other *** search
/ tusportal.tus.k12.pa.us / tusportal.tus.k12.pa.us.tar / tusportal.tus.k12.pa.us / Wyse / latest-image.raw / 0.img / usr / lib / pygtk / 2.0 / demos / tooltip.py < prev    next >
Text File  |  2010-05-11  |  9KB  |  258 lines

  1. #!/usr/bin/env python
  2. '''Tooltip
  3.  
  4. This is a test of the new gtk tooltip system.  It is a
  5. fairly straight forward port of the example distributed with gtk.
  6. '''
  7.  
  8. import gtk
  9. import cairo
  10. import gobject
  11. import pango
  12.  
  13. rects = [
  14.     {"x":10, "y":10, "r":0.0, "g":0.0, "b":0.9, "tooltip":"Blue box!"},
  15.     {"x":200, "y":170, "r":1.0, "g":0.0, "b":0.0, "tooltip":"Red thing"},
  16.     {"x":100, "y":50, "r":0.8, "g":0.8, "b":0.0, "tooltip":"Yellow thing"}
  17.     ]
  18.  
  19. class TooltipDemo(gtk.Window):
  20.     def __init__(self, parent=None):
  21.         gtk.Window.__init__(self)
  22.         try:
  23.             self.set_screen(parent.get_screen())
  24.         except AttributeError:
  25.             self.connect('destroy', lambda *w: gtk.main_quit())
  26.         self.set_title(self.__class__.__name__)
  27.         
  28.         self.set_border_width(10)
  29.         
  30.         box = gtk.VBox(False, 3)
  31.         self.add(box)
  32.         
  33.         # A check button using the tooltip-markup property
  34.         button = gtk.CheckButton("This one uses the tooltip-markup property")
  35.         button.set_tooltip_text("Hello, I am a static tooltip.")
  36.         box.pack_start(button, False, False, 0)
  37.         
  38.         # A check button using the query-tooltip signal
  39.         button = gtk.CheckButton("I use the query-tooltip signal")
  40.         button.props.has_tooltip = True
  41.         button.connect("query-tooltip", self.query_tooltip_cb)
  42.         box.pack_start(button, False, False, 0)
  43.         
  44.         # A label
  45.         label = gtk.Label("I am just a label")
  46.         label.set_selectable(False)
  47.         label.set_tooltip_text("Label & and tooltip")
  48.         box.pack_start(label, False, False, 0)
  49.         
  50.         # A selectable label
  51.         label = gtk.Label("I am a selectable label")
  52.         label.set_selectable(True)
  53.         label.set_tooltip_markup("<b>Another</b> Label tooltip")
  54.         box.pack_start(label, False, False, 0)
  55.         
  56.         # Another one, with a custom tooltip window
  57.         button = gtk.CheckButton("This one has a custom tooltip window!")
  58.         box.pack_start(button, False, False, 0)
  59.         
  60.         tooltip_window = gtk.Window(gtk.WINDOW_POPUP)
  61.         tooltip_button = gtk.Label("blaat!")
  62.         tooltip_window.add(tooltip_button)
  63.         tooltip_button.show()
  64.         
  65.         button.set_tooltip_window(tooltip_window)
  66.         button.connect("query-tooltip", self.query_tooltip_custom_cb)
  67.         button.props.has_tooltip = True
  68.         
  69.         # An insensitive button
  70.         button = gtk.Button("This one is insensitive")
  71.         button.set_sensitive(False)
  72.         button.props.tooltip_text = "Insensitive!"
  73.         box.pack_start(button, False, False, 0)
  74.         
  75.         # Testcases from Kris without a tree view don't exist
  76.         tree_view = gtk.TreeView(self.create_model())
  77.         tree_view.set_size_request(200, 240)
  78.         
  79.         tree_view.insert_column_with_attributes(0, "Test",
  80.                                                  gtk.CellRendererText(),
  81.                                                  text = 0)
  82.         
  83.         tree_view.props.has_tooltip = True
  84.         tree_view.connect("query-tooltip", self.query_tooltip_tree_view_cb)
  85.         tree_view.get_selection().connect("changed",
  86.                                           self.selection_changed_cb, tree_view)
  87.         
  88.         # We cannot get the button on the treeview column directly
  89.         # so we have to use a ugly hack to get it.
  90.         column = tree_view.get_column(0)
  91.         column.set_clickable(True)
  92.         label = gtk.Label("Test")
  93.         column.set_widget(label)
  94.         label.show()
  95.         button = label.get_parent()
  96.         button.props.tooltip_text = "Header"
  97.  
  98.         box.pack_start(tree_view, False, False, 2)
  99.  
  100.         # Add an IconView for some more testing
  101.         iconview = gtk.IconView()
  102.         iconview.props.has_tooltip = True
  103.         iconview.connect("query-tooltip", self.query_tooltip_icon_view_cb)
  104.         
  105.         model = gtk.ListStore(str, gtk.gdk.Pixbuf)
  106.         iconview.set_model(model)
  107.         iconview.set_text_column(0)
  108.         iconview.set_pixbuf_column(1)
  109.         
  110.         pixbuf1 = iconview.render_icon(gtk.STOCK_APPLY,gtk.ICON_SIZE_BUTTON)
  111.         model.append(['Apply', pixbuf1])
  112.         
  113.         pixbuf2 = iconview.render_icon(gtk.STOCK_CANCEL,gtk.ICON_SIZE_BUTTON)
  114.         model.append(['Cancel', pixbuf2])
  115.         
  116.         box.pack_start(iconview, False, False, 2)
  117.         
  118.         # And a text view for Matthias
  119.         buffer = gtk.TextBuffer()
  120.         
  121.         iter = buffer.get_end_iter()
  122.         buffer.insert(iter, "Hello, the text ", -1)
  123.         
  124.         tag = buffer.create_tag("bold")
  125.         tag.props.weight = pango.WEIGHT_BOLD
  126.         
  127.         iter = buffer.get_end_iter()
  128.         buffer.insert_with_tags(iter, "in bold", tag)
  129.         
  130.         iter = buffer.get_end_iter()
  131.         buffer.insert(iter, " has a tooltip!", -1)
  132.         
  133.         text_view = gtk.TextView(buffer)
  134.         text_view.set_size_request(200, 50)
  135.         
  136.         text_view.props.has_tooltip = True
  137.         text_view.connect("query-tooltip", self.query_tooltip_text_view_cb, tag)
  138.         
  139.         box.pack_start(text_view, False, False, 2)
  140.         
  141.         # Drawing area
  142.         drawing_area = gtk.DrawingArea()
  143.         drawing_area.set_size_request(320, 240)
  144.         drawing_area.props.has_tooltip = True
  145.         drawing_area.connect("expose_event", self.drawing_area_expose)
  146.         drawing_area.connect("query-tooltip",
  147.                              self.query_tooltip_drawing_area_cb)
  148.         box.pack_start(drawing_area, False, False, 2)
  149.         
  150.         # Done!
  151.         self.show_all()
  152.     
  153.     def query_tooltip_cb(self, widget, x, y, keyboard_tip, tooltip):
  154.         tooltip.set_markup(widget.get_label())
  155.         tooltip.set_icon_from_stock(gtk.STOCK_DELETE, gtk.ICON_SIZE_MENU)
  156.     
  157.         return True
  158.     
  159.     def query_tooltip_custom_cb(self, widget, x, y, keyboard_tip, tooltip):
  160.         color = gtk.gdk.Color(0, 65535, 0)
  161.         window = widget.get_tooltip_window()
  162.     
  163.         window.modify_bg(gtk.STATE_NORMAL, color)
  164.     
  165.         return True
  166.     
  167.     def query_tooltip_text_view_cb(self, widget, x, y,
  168.                                    keyboard_tip, tooltip, data):
  169.         if keyboard_tip:
  170.             offset= widget.props.buffer.cursor_position
  171.             iter = widget.props.buffer.get_iter_at_offset(offset)
  172.         else:
  173.             coords = widget.window_to_buffer_coords(gtk.TEXT_WINDOW_TEXT, x, y)
  174.             ret =widget.get_iter_at_position(coords[0], coords[1])
  175.     
  176.         if ret[0].has_tag(data):
  177.             tooltip.set_text("Tooltip on text tag")
  178.         else:
  179.             return False
  180.     
  181.         return True
  182.     
  183.     def query_tooltip_tree_view_cb(self, widget, x, y, keyboard_tip, tooltip):
  184.         if not widget.get_tooltip_context(x, y, keyboard_tip):
  185.             return False
  186.         else:
  187.             model, path, iter = widget.get_tooltip_context(x, y, keyboard_tip)
  188.  
  189.             value = model.get(iter, 0)
  190.             tooltip.set_markup("<b>Path %s:</b> %s" %(path[0], value[0]))
  191.             widget.set_tooltip_row(tooltip, path)
  192.             return True
  193.  
  194.     def query_tooltip_icon_view_cb(self, widget, x, y, keyboard_tip, tooltip):
  195.         if not widget.get_tooltip_context(x, y, keyboard_tip):
  196.             return False
  197.         else:
  198.             model, path, iter = widget.get_tooltip_context(x, y, keyboard_tip)
  199.     
  200.             value = model.get(iter, 0)
  201.             tooltip.set_markup("<b>Path %s:</b> %s" %(path[0], value[0]))
  202.             widget.set_tooltip_item(tooltip, path)
  203.             return True
  204.  
  205.     def query_tooltip_drawing_area_cb(self, widget, x, y, keyboard_tip,
  206.                                       tooltip, data=None):
  207.         if keyboard_tip:
  208.             return False
  209.         
  210.         for i in range(len(rects)):
  211.             if(rects[i]["x"] < x and x < rects[i]["x"] + 50 \
  212.                     and rects[i]["y"] < y and y < rects[i]["y"] + 50):
  213.                 tooltip.set_markup(rects[i]["tooltip"])
  214.                 return True;
  215.         return False
  216.     
  217.     def selection_changed_cb(self, selection, tree_view):
  218.         tree_view.trigger_tooltip_query()
  219.     
  220.     def create_model(self):    
  221.         store = gtk.TreeStore(gobject.TYPE_STRING);
  222.         
  223.         # A tree store with some random words ...
  224.         store.append(None, ("File Manager",))
  225.         store.append(None, ("Gossip",))
  226.         store.append(None, ("System Settings",))
  227.         store.append(None, ("The GIMP",))
  228.         store.append(None, ("Terminal",))
  229.         store.append(None, ("Word Processor",))
  230.         
  231.         return(store)
  232.     
  233.     def drawing_area_expose(self, drawing_area, event, data=None):
  234.         cr = drawing_area.window.cairo_create()
  235.         
  236.         cr.rectangle(0, 0,
  237.                      drawing_area.allocation.width,
  238.                      drawing_area.allocation.height)
  239.         cr.set_source_rgb(1.0, 1.0, 1.0)
  240.         cr.fill()
  241.         
  242.         for i in range(len(rects)):
  243.             cr.rectangle(rects[i]["x"], rects[i]["y"], 50, 50)
  244.             cr.set_source_rgb(rects[i]["r"], rects[i]["g"], rects[i]["b"])
  245.             cr.stroke()
  246.         
  247.             cr.rectangle(rects[i]["x"], rects[i]["y"], 50, 50)
  248.             cr.set_source_rgba(rects[i]["r"], rects[i]["g"], rects[i]["b"], 0.5)
  249.             cr.fill()
  250.     
  251.         return False
  252.  
  253. def main():
  254.     TooltipDemo()
  255.     gtk.main()
  256.  
  257. if __name__ == '__main__':
  258.     main()