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