2
# Copyright 2007 Red Hat, Inc.
4
# Thomas Woerner <twoerner@redhat.com>
6
# This program is free software; you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation; either version 2 of the License, or
9
# (at your option) any later version.
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
# GNU General Public License for more details.
16
# You should have received a copy of the GNU General Public License
17
# along with this program. If not, see <http://www.gnu.org/licenses/>.
24
##############################################################################
26
class TreeViewTooltips(gobject.GObject):
28
"show-delay": (gobject.TYPE_UINT,
29
"Show Delay (ms)", "Show Delay (ms), 0 for no delay.",
31
gobject.PARAM_READWRITE),
32
"hide-delay": (gobject.TYPE_UINT,
33
"Hide Delay (ms)", "Hide Delay (ms), 0 for unlimited.",
35
gobject.PARAM_READWRITE),
38
def __init__(self, treeview, tooltip_func, *tooltip_func_args):
39
self.__gobject_init__()
40
self.treeview = treeview
41
self.tooltip_func = tooltip_func
42
self.tooltip_func_args = tooltip_func_args
44
self.popup = gtk.Window(gtk.WINDOW_POPUP)
45
self.label = gtk.Label()
46
self.label.set_line_wrap(True)
47
self.label.set_use_markup(True)
48
self.popup.add(self.label)
49
self.popup.set_name('gtk-tooltips')
50
self.popup.set_resizable(False)
51
self.popup.set_border_width(4)
52
self.popup.set_app_paintable(True)
55
self.hide_delay = 5*1000
57
self.show_timer = None
58
self.hide_timer = None
59
self.path = self.col = None
61
treeview.connect("motion-notify-event", self.on_motion_notify)
62
treeview.connect("leave-notify-event", self.on_leave_notify)
63
self.popup.connect("expose-event", self.on_expose_event)
65
def do_get_property(self, property):
66
if property.name == 'show-delay':
67
return self.show_delay
68
elif property.name == 'hide-delay':
69
return self.hide_delay
71
def do_set_property(self, property, value):
72
if property.name == 'show-delay':
73
self.show_delay = value
74
elif property.name == 'hide-delay':
75
self.hide_delay = value
77
def on_motion_notify(self, treeview, event):
78
if event.window != treeview.get_bin_window():
81
path = treeview.get_path_at_pos(int(event.x), int(event.y))
83
path, col, _x, _y = path
84
if self.path != path or self.col != col:
86
gobject.source_remove(self.hide_timer)
87
self.hide_timer = None
92
gobject.source_remove(self.show_timer)
93
self.show_timer = None
94
self.show_timer = gobject.timeout_add(self.show_delay,
100
gobject.source_remove(self.hide_timer)
101
if self.hide_delay > 0:
102
self.hide_timer = gobject.timeout_add(self.hide_delay,
104
elif self.path or self.col:
106
gobject.source_remove(self.show_timer)
107
self.show_timer = None
109
gobject.source_remove(self.hide_timer)
110
self.hide_timer = None
113
def on_leave_notify(self, treeview, event):
115
gobject.source_remove(self.show_timer)
116
self.show_timer = None
118
gobject.source_remove(self.hide_timer)
119
self.hide_timer = None
120
if self.path or self.col:
123
def show_tip(self, path, col, event_x, event_y):
124
text = self.tooltip_func(self.treeview.get_model(), path, col,
125
*self.tooltip_func_args)
126
if not text or len(text) == 0:
128
self.label.set_label(text)
129
(parent_x, parent_y) = self.treeview.get_bin_window().get_origin()
130
(width, height) = self.popup.size_request()
131
screen_width = gtk.gdk.screen_width()
132
screen_height = gtk.gdk.screen_height()
133
x = int(event_x + parent_x)
134
y = int(event_y + parent_y + 10)
135
if x + width> screen_width:
136
x = screen_width - width
137
if y + height > screen_height:
138
y = int(event_y + parent_y - height - 10)
139
self.popup.move(x, y)
140
self.popup.show_all()
141
if self.hide_delay > 0:
142
self.hide_timer = gobject.timeout_add(self.hide_delay,
146
def on_expose_event(self, window, event):
147
(width, height) = window.size_request()
148
window.style.paint_flat_box(window.window, gtk.STATE_NORMAL,
149
gtk.SHADOW_OUT, None, window,
150
'tooltip', 0, 0, width, height)
153
self.path = self.col = None
157
##############################################################################
159
if __name__ == "__main__":
160
def getTooltip(model, path, col, cell):
161
iter = model.get_iter(path)
162
text = "%s" % model.get_value(iter, cell)
165
window = gtk.Window(gtk.WINDOW_TOPLEVEL)
166
window.connect("delete_event", gtk.main_quit)
167
window.set_default_size(200,250)
169
scrolledwin = gtk.ScrolledWindow()
170
scrolledwin.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
171
window.add(scrolledwin)
173
treeview = gtk.TreeView()
174
treeview.get_selection().set_mode(gtk.SELECTION_NONE)
175
store = gtk.ListStore(gobject.TYPE_STRING, gobject.TYPE_STRING,
177
treeview.set_model(store)
178
scrolledwin.add(treeview)
180
column = gtk.TreeViewColumn("Head1", gtk.CellRendererText(), text=0)
181
treeview.append_column(column)
183
column = gtk.TreeViewColumn("Head2", gtk.CellRendererText(), text=1)
184
treeview.append_column(column)
187
cell1 = "cell data %d.1" % i
188
cell2 = "cell data %d.2" % i
189
cell3 = "Tooltip %d" % i
191
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
193
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
195
You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA."""
196
store.append([cell1, cell2, cell3])
198
tips = TreeViewTooltips(treeview, getTooltip, 2)
199
# tips.set_property("hide-delay", 20*1000)