HtmlTextView: minor refactoring

- Move updating tags into HtmlTextView
- Rename some vars
- Remove legacy GTK code
This commit is contained in:
Philipp Hörist 2018-11-16 20:52:19 +01:00
parent eedd0c2a72
commit 80954e9534
2 changed files with 49 additions and 55 deletions

View File

@ -172,7 +172,7 @@ class ConversationTextview(GObject.GObject):
# no need to inherit TextView, use it as atrribute is safer # no need to inherit TextView, use it as atrribute is safer
self.tv = HtmlTextView(account) self.tv = HtmlTextView(account)
self.tv.connect_tooltip(self.query_tooltip) self.tv.connect('query-tooltip', self._query_tooltip)
# set properties # set properties
self.tv.set_border_width(1) self.tv.set_border_width(1)
@ -199,7 +199,7 @@ class ConversationTextview(GObject.GObject):
self.handlers[id_] = self.tv self.handlers[id_] = self.tv
self.account = account self.account = account
self.cursor_changed = False self._cursor_changed = False
self.last_time_printout = 0 self.last_time_printout = 0
self.encryption_enabled = False self.encryption_enabled = False
@ -304,14 +304,12 @@ class ConversationTextview(GObject.GObject):
self.just_cleared = False self.just_cleared = False
def query_tooltip(self, widget, x_pos, y_pos, keyboard_mode, tooltip): def _query_tooltip(self, widget, x_pos, y_pos, keyboard_mode, tooltip):
window = widget.get_window(Gtk.TextWindowType.TEXT) window = widget.get_window(Gtk.TextWindowType.TEXT)
x_pos, y_pos = self.tv.window_to_buffer_coords( x_pos, y_pos = self.tv.window_to_buffer_coords(
Gtk.TextWindowType.TEXT, x_pos, y_pos) Gtk.TextWindowType.TEXT, x_pos, y_pos)
if Gtk.MINOR_VERSION > 18:
iter_ = self.tv.get_iter_at_position(x_pos, y_pos)[1] iter_ = self.tv.get_iter_at_position(x_pos, y_pos)[1]
else:
iter_ = self.tv.get_iter_at_position(x_pos, y_pos)[0]
for tag in iter_.get_tags(): for tag in iter_.get_tags():
tag_name = tag.get_property('name') tag_name = tag.get_property('name')
if tag_name == 'focus-out-line': if tag_name == 'focus-out-line':
@ -327,11 +325,11 @@ class ConversationTextview(GObject.GObject):
text = text[:47] + '' text = text[:47] + ''
tooltip.set_text(text) tooltip.set_text(text)
window.set_cursor(get_cursor('HAND2')) window.set_cursor(get_cursor('HAND2'))
self.cursor_changed = True self._cursor_changed = True
return True return True
if tag_name in ('url', 'mail', 'xmpp', 'sth_at_sth'): if tag_name in ('url', 'mail', 'xmpp', 'sth_at_sth'):
window.set_cursor(get_cursor('HAND2')) window.set_cursor(get_cursor('HAND2'))
self.cursor_changed = True self._cursor_changed = True
return False return False
try: try:
text = self.corrected_text_list[tag_name] text = self.corrected_text_list[tag_name]
@ -339,9 +337,9 @@ class ConversationTextview(GObject.GObject):
return True return True
except KeyError: except KeyError:
pass pass
if self.cursor_changed: if self._cursor_changed:
window.set_cursor(get_cursor('XTERM')) window.set_cursor(get_cursor('XTERM'))
self.cursor_changed = False self._cursor_changed = False
return False return False
def del_handlers(self): def del_handlers(self):
@ -358,11 +356,7 @@ class ConversationTextview(GObject.GObject):
app.css_config.get_value('.gajim-status-message', StyleAttr.COLOR)) app.css_config.get_value('.gajim-status-message', StyleAttr.COLOR))
self.tagMarked.set_property('foreground', self.tagMarked.set_property('foreground',
app.css_config.get_value('.gajim-highlight-message', StyleAttr.COLOR)) app.css_config.get_value('.gajim-highlight-message', StyleAttr.COLOR))
color = app.css_config.get_value('.gajim-url', StyleAttr.COLOR) self.tv.update_tags()
self.tv.tagURL.set_property('foreground', color)
self.tv.tagMail.set_property('foreground', color)
self.tv.tagXMPP.set_property('foreground', color)
self.tv.tagSthAtSth.set_property('foreground', color)
def scroll_to_end(self, force=False): def scroll_to_end(self, force=False):
if self.autoscroll or force: if self.autoscroll or force:

View File

@ -563,20 +563,20 @@ class HtmlHandler(xml.sax.handler.ContentHandler):
if height: if height:
self._parse_length(height, False, False, 1, 1000, height_cb) self._parse_length(height, False, False, 1, 1000, height_cb)
def set_size(_pixbuf, w, h, dims): def set_size(_pixbuf, width_, height_, dims):
""" """
FIXME: Floats should be relative to the whole textview, and FIXME: Floats should be relative to the whole textview, and
resize with it. This needs new pifbufs for every resize, resize with it. This needs new pifbufs for every resize,
GdkPixbuf.Pixbuf.scale_simple or similar. GdkPixbuf.Pixbuf.scale_simple or similar.
""" """
if isinstance(dims[0], float): if isinstance(dims[0], float):
dims[0] = int(dims[0]*w) dims[0] = int(dims[0] * width_)
elif not dims[0]: elif not dims[0]:
dims[0] = w dims[0] = width_
if isinstance(dims[1], float): if isinstance(dims[1], float):
dims[1] = int(dims[1]*h) dims[1] = int(dims[1] * height_)
if not dims[1]: if not dims[1]:
dims[1] = h dims[1] = height_
loader.set_size(*dims) loader.set_size(*dims)
if width or height: if width or height:
@ -813,54 +813,48 @@ class HtmlHandler(xml.sax.handler.ContentHandler):
class HtmlTextView(Gtk.TextView): class HtmlTextView(Gtk.TextView):
def __init__(self, account=None):
_tags = ['url', 'mail', 'xmpp', 'sth_at_sth']
def __init__(self, account, standalone=False):
Gtk.TextView.__init__(self) Gtk.TextView.__init__(self)
self.set_wrap_mode(Gtk.WrapMode.CHAR) self.set_wrap_mode(Gtk.WrapMode.CHAR)
self.set_editable(False) self.set_editable(False)
self._changed_cursor = False
self.set_has_tooltip(True) self.set_has_tooltip(True)
self.connect('copy-clipboard', self._on_copy_clipboard) self.connect('copy-clipboard', self._on_copy_clipboard)
self.get_buffer().eol_tag = self.get_buffer().create_tag('eol') self.get_buffer().eol_tag = self.get_buffer().create_tag('eol')
self.config = app.config
self.interface = app.interface
self.account = account self.account = account
self.plugin_modified = False self.plugin_modified = False
self._cursor_changed = False
def connect_tooltip(self, func=None): if standalone:
self.connect('query-tooltip', func or self.__query_tooltip) self.connect('query-tooltip', self._query_tooltip)
self.create_tags()
def create_tags(self): def create_tags(self):
buffer_ = self.get_buffer()
self.tagURL = buffer_.create_tag('url')
color = app.css_config.get_value('.gajim-url', StyleAttr.COLOR) color = app.css_config.get_value('.gajim-url', StyleAttr.COLOR)
self.tagURL.set_property('foreground', color)
self.tagURL.set_property('underline', Pango.Underline.SINGLE)
self.tagURL.connect('event', self.hyperlink_handler, 'url')
self.tagMail = buffer_.create_tag('mail') attrs = {'foreground': color,
self.tagMail.set_property('foreground', color) 'underline': Pango.Underline.SINGLE}
self.tagMail.set_property('underline', Pango.Underline.SINGLE)
self.tagMail.connect('event', self.hyperlink_handler, 'mail')
self.tagXMPP = buffer_.create_tag('xmpp') for tag in self._tags:
self.tagXMPP.set_property('foreground', color) tag_ = self.get_buffer().create_tag(tag, **attrs)
self.tagXMPP.set_property('underline', Pango.Underline.SINGLE) tag_.connect('event', self.hyperlink_handler, tag)
self.tagXMPP.connect('event', self.hyperlink_handler, 'xmpp')
self.tagSthAtSth = buffer_.create_tag('sth_at_sth') def update_tags(self):
self.tagSthAtSth.set_property('foreground', color) tag_table = self.get_buffer().get_tag_table()
self.tagSthAtSth.set_property('underline', Pango.Underline.SINGLE) color = app.css_config.get_value('.gajim-url', StyleAttr.COLOR)
self.tagSthAtSth.connect('event', self.hyperlink_handler, 'sth_at_sth')
def __query_tooltip(self, widget, x_pos, y_pos, _keyboard_mode, tooltip): for tag in self._tags:
tag_table.lookup(tag).set_property('foreground', color)
def _query_tooltip(self, widget, x_pos, y_pos, _keyboard_mode, tooltip):
window = widget.get_window(Gtk.TextWindowType.TEXT) window = widget.get_window(Gtk.TextWindowType.TEXT)
x_pos, y_pos = self.window_to_buffer_coords( x_pos, y_pos = self.window_to_buffer_coords(
Gtk.TextWindowType.TEXT, x_pos, y_pos) Gtk.TextWindowType.TEXT, x_pos, y_pos)
if Gtk.MINOR_VERSION > 18:
iter_ = self.get_iter_at_position(x_pos, y_pos)[1] 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(): for tag in iter_.get_tags():
if getattr(tag, 'is_anchor', False): if getattr(tag, 'is_anchor', False):
text = getattr(tag, 'title', False) text = getattr(tag, 'title', False)
@ -868,13 +862,19 @@ class HtmlTextView(Gtk.TextView):
if len(text) > 50: if len(text) > 50:
text = text[:47] + '' text = text[:47] + ''
tooltip.set_text(text) tooltip.set_text(text)
if not self._changed_cursor:
window.set_cursor(get_cursor('HAND2')) window.set_cursor(get_cursor('HAND2'))
self._changed_cursor = True self._cursor_changed = True
return True return True
if self._changed_cursor:
tag_name = tag.get_property('name')
if tag_name in ('url', 'mail', 'xmpp', 'sth_at_sth'):
window.set_cursor(get_cursor('HAND2'))
self._cursor_changed = True
return False
if self._cursor_changed:
window.set_cursor(get_cursor('XTERM')) window.set_cursor(get_cursor('XTERM'))
self._changed_cursor = False self._cursor_changed = False
return False return False
def show_context_menu(self, _event, kind, text): def show_context_menu(self, _event, kind, text):