-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgtk_treeviewtooltips.py
219 lines (188 loc) · 8.57 KB
/
gtk_treeviewtooltips.py
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
#
# Copyright 2007 Red Hat, Inc.
# Authors:
# Thomas Woerner <[email protected]>
#
# 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.
#
# 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.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
import pygtk
import gtk
import gobject
##############################################################################
class TreeViewTooltips(gobject.GObject):
__gproperties__ = {
"show-delay": (gobject.TYPE_UINT,
"Show Delay (ms)", "Show Delay (ms), 0 for no delay.",
0, 10000, 500,
gobject.PARAM_READWRITE),
"hide-delay": (gobject.TYPE_UINT,
"Hide Delay (ms)", "Hide Delay (ms), 0 for unlimited.",
0, 99999999, 5*1000,
gobject.PARAM_READWRITE),
}
def __init__(self, treeview, tooltip_func, *tooltip_func_args):
self.__gobject_init__()
self.treeview = treeview
self.tooltip_func = tooltip_func
self.tooltip_func_args = tooltip_func_args
self.popup = gtk.Window(gtk.WINDOW_POPUP)
self.label = gtk.Label()
self.label.set_line_wrap(True)
self.label.set_use_markup(True)
self.popup.add(self.label)
self.popup.set_name('gtk-tooltips')
self.popup.set_resizable(False)
self.popup.set_border_width(4)
self.popup.set_app_paintable(True)
self.show_delay = 500
self.hide_delay = 5*1000
self.show_timer = None
self.hide_timer = None
self.path = self.col = None
treeview.connect("motion-notify-event", self.on_motion_notify)
treeview.connect("leave-notify-event", self.on_leave_notify)
treeview.connect_after("scroll-event", self.on_scroll)
self.popup.connect("expose-event", self.on_expose_event)
def do_get_property(self, property):
if property.name == 'show-delay':
return self.show_delay
elif property.name == 'hide-delay':
return self.hide_delay
def do_set_property(self, property, value):
if property.name == 'show-delay':
self.show_delay = value
elif property.name == 'hide-delay':
self.hide_delay = value
def on_scroll(self, treeview, event):
self.hide_tip()
def on_motion_notify(self, treeview, event):
if event.window != treeview.get_bin_window():
return
path = treeview.get_path_at_pos(int(event.x), int(event.y))
if path:
path, col, _x, _y = path
if self.path != path or self.col != col:
if self.hide_timer:
gobject.source_remove(self.hide_timer)
self.hide_timer = None
self.hide_tip()
self.path = path
self.col = col
if self.show_timer:
gobject.source_remove(self.show_timer)
self.show_timer = None
self.show_timer = gobject.timeout_add(self.show_delay,
self.show_tip,
path, col,
event.x, event.y)
else:
if self.hide_timer:
gobject.source_remove(self.hide_timer)
if self.hide_delay > 0:
self.hide_timer = gobject.timeout_add(self.hide_delay,
self.hide_tip)
elif self.path or self.col:
if self.show_timer:
gobject.source_remove(self.show_timer)
self.show_timer = None
if self.hide_timer:
gobject.source_remove(self.hide_timer)
self.hide_timer = None
self.hide_tip()
def on_leave_notify(self, treeview, event):
if self.show_timer:
gobject.source_remove(self.show_timer)
self.show_timer = None
if self.hide_timer:
gobject.source_remove(self.hide_timer)
self.hide_timer = None
if self.path or self.col:
self.hide_tip()
# treeview does not have the focus, do not show tooltip (there might
# be an overlapping window)
if not treeview.is_focus():
return
# are we still inside the visible area of the treeview
# (mouse moved into the tooltip)
pos = treeview.widget_to_tree_coords(int(event.x), int(event.y))
visible = treeview.get_visible_rect()
if pos[0] >= visible.x and pos[0] < visible.x + visible.width and \
pos[1] >= visible.y and pos[1] < visible.y + visible.height:
self.on_motion_notify(treeview, event)
def show_tip(self, path, col, event_x, event_y):
text = self.tooltip_func(self.treeview.get_model(), path, col,
*self.tooltip_func_args)
if not text or len(text) == 0:
return False
self.label.set_label(text)
(parent_x, parent_y) = self.treeview.get_bin_window().get_origin()
(width, height) = self.popup.size_request()
screen_width = gtk.gdk.screen_width()
screen_height = gtk.gdk.screen_height()
x = int(event_x + parent_x)
y = int(event_y + parent_y + 10)
if x + width> screen_width:
x = screen_width - width
if y + height > screen_height:
y = int(event_y + parent_y - height - 10)
self.popup.move(x, y)
self.popup.show_all()
if self.hide_delay > 0:
self.hide_timer = gobject.timeout_add(self.hide_delay,
self.hide_tip)
return False
def on_expose_event(self, window, event):
(width, height) = window.size_request()
window.style.paint_flat_box(window.window, gtk.STATE_NORMAL,
gtk.SHADOW_OUT, None, window,
'tooltip', 0, 0, width, height)
def hide_tip(self):
self.path = self.col = None
self.popup.hide()
return False
##############################################################################
if __name__ == "__main__":
def getTooltip(model, path, col, cell):
iter = model.get_iter(path)
text = "%s" % model.get_value(iter, cell)
return text
window = gtk.Window(gtk.WINDOW_TOPLEVEL)
window.connect("delete_event", gtk.main_quit)
window.set_default_size(200,250)
scrolledwin = gtk.ScrolledWindow()
scrolledwin.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
window.add(scrolledwin)
treeview = gtk.TreeView()
treeview.get_selection().set_mode(gtk.SELECTION_NONE)
store = gtk.ListStore(gobject.TYPE_STRING, gobject.TYPE_STRING,
gobject.TYPE_STRING)
treeview.set_model(store)
scrolledwin.add(treeview)
column = gtk.TreeViewColumn("Head1", gtk.CellRendererText(), text=0)
treeview.append_column(column)
column = gtk.TreeViewColumn("Head2", gtk.CellRendererText(), text=1)
treeview.append_column(column)
for i in xrange(10):
cell1 = "cell data %d.1" % i
cell2 = "cell data %d.2" % i
cell3 = "Tooltip %d" % i
cell3 += """
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.
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.
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."""
store.append([cell1, cell2, cell3])
tips = TreeViewTooltips(treeview, getTooltip, 2)
# tips.set_property("hide-delay", 20*1000)
window.show_all()
gtk.main()