Refactor showing tooltip in HtmlTextView

The 'query-tooltip' event does essentially the same as the
'motion-notify-event' but supplys us with coordinates,
which lets us get rid of deprecated get_client_pointer()

Add a connect_tooltip() method that allows other classes
that use HtmlTextView to provide their own tooltip method
This commit is contained in:
Philipp Hörist 2017-03-07 18:56:41 +01:00
parent 205f5f3044
commit 6de3120d43
2 changed files with 28 additions and 59 deletions

View File

@ -841,21 +841,21 @@ class HtmlTextView(Gtk.TextView):
self.set_wrap_mode(Gtk.WrapMode.CHAR)
self.set_editable(False)
self._changed_cursor = False
self.connect('destroy', self.__destroy_event)
self.connect('motion-notify-event', self.__motion_notify_event)
self.set_has_tooltip(True)
self.connect('leave-notify-event', self.__leave_event)
self.connect('enter-notify-event', self.__motion_notify_event)
self.connect('realize', self.on_html_text_view_realized)
self.connect('unrealize', self.on_html_text_view_unrealized)
self.connect('copy-clipboard', self.on_html_text_view_copy_clipboard)
self.id_ = self.connect('button-release-event',
self.on_left_mouse_button_release)
self.get_buffer().eol_tag = self.get_buffer().create_tag('eol')
self.tooltip = tooltips.BaseTooltip()
self.config = gajim.config
self.interface = gajim.interface
# end big hack
def connect_tooltip(self, func=None):
self.connect('query-tooltip', func or self.__query_tooltip)
def create_tags(self):
buffer_ = self.get_buffer()
@ -880,9 +880,29 @@ class HtmlTextView(Gtk.TextView):
self.tagSthAtSth.set_property('underline', Pango.Underline.SINGLE)
self.tagSthAtSth.connect('event', self.hyperlink_handler, 'sth_at_sth')
def __destroy_event(self, widget):
if self.tooltip.timeout != 0 or self.tooltip.shown:
self.tooltip.hide_tooltip()
def __query_tooltip(self, widget, x_pos, y_pos, keyboard_mode, tooltip):
window = widget.get_window(Gtk.TextWindowType.TEXT)
x_pos, y_pos = self.window_to_buffer_coords(
Gtk.TextWindowType.TEXT, x_pos, y_pos)
if Gtk.MINOR_VERSION > 18:
iter_ = self.get_iter_at_position(x_pos, y_pos)[1]
else:
iter_ = self.get_iter_at_position(x_pos, y_pos)[0]
for tag in iter_.get_tags():
if getattr(tag, 'is_anchor', False):
text = getattr(tag, 'title', False)
if text:
if len(text) > 50:
text = text[:47] + ''
tooltip.set_text(text)
if not self._changed_cursor:
window.set_cursor(Gdk.Cursor.new(Gdk.CursorType.HAND2))
self._changed_cursor = True
return True
if self._changed_cursor:
window.set_cursor(Gdk.Cursor.new(Gdk.CursorType.XTERM))
self._changed_cursor = False
return False
def __leave_event(self, widget, event):
if self._changed_cursor:
@ -890,58 +910,6 @@ class HtmlTextView(Gtk.TextView):
window.set_cursor(Gdk.Cursor.new(Gdk.CursorType.XTERM))
self._changed_cursor = False
def show_tooltip(self, tag):
self.tooltip.timeout = 0
if not self.tooltip.win:
# check if the current pointer is still over the line
w = self.get_window(Gtk.TextWindowType.TEXT)
device = w.get_display().get_device_manager().get_client_pointer()
pointer = w.get_device_position(device)
x = pointer[1]
y = pointer[2]
iter_ = self.get_iter_at_location(x, y)
if isinstance(iter_, tuple):
iter_ = iter_[1]
tags = iter_.get_tags()
is_over_anchor = False
for tag_ in tags:
if getattr(tag_, 'is_anchor', False):
is_over_anchor = True
break
if not is_over_anchor:
return
text = getattr(tag, 'title', False)
if text:
if len(text) > 50:
text = text[:47] + ''
position = w.get_origin()[1:]
self.tooltip.show_tooltip(text, 8, position[1] + y)
def __motion_notify_event(self, widget, event):
w = widget.get_window(Gtk.TextWindowType.TEXT)
device = w.get_display().get_device_manager().get_client_pointer()
pointer = w.get_device_position(device)
x = pointer[1]
y = pointer[2]
iter_ = widget.get_iter_at_location(x, y)
if isinstance(iter_, tuple):
iter_ = iter_[1]
tags = iter_.get_tags()
anchor_tags = [tag for tag in tags if getattr(tag, 'is_anchor', False)]
if self.tooltip.timeout != 0 or self.tooltip.shown:
# Check if we should hide the line tooltip
if not anchor_tags:
self.tooltip.hide_tooltip()
if not self._changed_cursor and anchor_tags:
w.set_cursor(Gdk.Cursor.new(Gdk.CursorType.HAND2))
self._changed_cursor = True
self.tooltip.timeout = GLib.timeout_add(500, self.show_tooltip,
anchor_tags[0])
elif self._changed_cursor and not anchor_tags:
w.set_cursor(Gdk.Cursor.new(Gdk.CursorType.XTERM))
self._changed_cursor = False
return False
def on_open_link_activate(self, widget, kind, text):
helpers.launch_browser_mailer(kind, text)

View File

@ -70,6 +70,7 @@ class PluginsWindow(object):
setattr(self, widget_name, self.xml.get_object(widget_name))
self.plugin_description_textview = HtmlTextView()
self.plugin_description_textview.connect_tooltip()
self.plugin_description_textview.set_wrap_mode(Gtk.WrapMode.WORD)
sw = self.xml.get_object('scrolledwindow2')
sw.add(self.plugin_description_textview)