From 61738c91b68439e8a0e4de02962f377e3469171d Mon Sep 17 00:00:00 2001 From: Benny Malengier Date: Mon, 20 Aug 2012 15:37:57 +0000 Subject: [PATCH] GTK3: forgotten util functions hex to rgb from previous commit, correct link color in notes svn: r20235 --- src/gui/utils.py | 19 +++++++++++++++++++ src/gui/widgets/styledtextbuffer.py | 4 +++- src/gui/widgets/styledtexteditor.py | 14 +++++++++++--- 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/src/gui/utils.py b/src/gui/utils.py index 6a92c267e..2fe80afed 100644 --- a/src/gui/utils.py +++ b/src/gui/utils.py @@ -409,3 +409,22 @@ def is_right_click(event): if event.button == 3: return True + +def hex_to_rgb(value): + """ + Convert a hexademical value #FF00FF to rgb. Returns tuple of integers + """ + value = value.lstrip('#') + lenv = len(value) + return tuple(int(value[i:i+lenv/3], 16) for i in range(0, lenv, lenv/3)) + +def rgb_to_hex(rgb): + """ + Convert a tuple of integer or float rgb values to its hex value + """ + if type(rgb[0]) == int: + return '#%02x%02x%02x' % rgb + else: + rgbint = (int(rgb[0] * 255), int(rgb[1] * 255), int(rgb[2] * 255)) + return '#%02x%02x%02x' % rgbint + diff --git a/src/gui/widgets/styledtextbuffer.py b/src/gui/widgets/styledtextbuffer.py index b5d6ca44a..3803439c1 100644 --- a/src/gui/widgets/styledtextbuffer.py +++ b/src/gui/widgets/styledtextbuffer.py @@ -294,6 +294,8 @@ class StyledTextBuffer(UndoableBuffer): self.connect_after('insert-text', self.after_insert_text) self.connect_after('delete-range', self.after_delete_range) + self.linkcolor = 'blue' + # init gtkspell "state machine" self.gtkspell_state = GtkSpellState(self) @@ -560,7 +562,7 @@ class StyledTextBuffer(UndoableBuffer): for s_tag in s_tags: if s_tag.name == _('Link'): g_tag = LinkTag(self, s_tag.value, - foreground="blue", + foreground=self.linkcolor, underline=UNDERLINE_SINGLE) else: g_tag = self._find_tag_by_name(int(s_tag.name), s_tag.value) diff --git a/src/gui/widgets/styledtexteditor.py b/src/gui/widgets/styledtexteditor.py index b5df28ae2..82e1bd2dc 100644 --- a/src/gui/widgets/styledtexteditor.py +++ b/src/gui/widgets/styledtexteditor.py @@ -61,7 +61,7 @@ from gui.widgets.toolcomboentry import ToolComboEntry from gui.widgets.springseparator import SpringSeparatorAction from gui.spell import Spell from gui.display import display_url -from gui.utils import SystemFonts +from gui.utils import SystemFonts, rgb_to_hex from gen.config import config from gen.constfunc import has_display @@ -183,6 +183,14 @@ class StyledTextEditor(Gtk.TextView): self.textbuffer.connect('changed', self._on_buffer_changed) GObject.GObject.__init__(self, buffer=self.textbuffer) + st_cont = self.get_style_context() + col = st_cont.lookup_color('link_color') + if col[0]: + self.linkcolor = rgb_to_hex((col[1].red, col[1].green, col[1].blue)) + else: + self.linkcolor = 'blue' + self.textbuffer.linkcolor = self.linkcolor + self.match = None self.last_match = None self._init_url_match() @@ -541,7 +549,7 @@ class StyledTextEditor(Gtk.TextView): """Setup regexp matching for URL match.""" self.textbuffer.create_tag('hyperlink', underline=Pango.Underline.SINGLE, - foreground='blue') + foreground=self.linkcolor) self.textbuffer.match_add(SCHEME + "//(" + USER + "@)?[" + HOSTCHARS + ".]+" + "(:[0-9]+)?(" + URLPATH + ")?/?", GENURL) @@ -621,7 +629,7 @@ class StyledTextEditor(Gtk.TextView): tag = LinkTag(self.textbuffer, data=uri, underline=Pango.Underline.SINGLE, - foreground="blue") + foreground=self.linkcolor) selection_bounds = self.textbuffer.get_selection_bounds() self.textbuffer.apply_tag(tag, selection_bounds[0],