OSDN Git Service

Selected text colors follow the focus in ThreadView.
[fukui-no-namari/fukui-no-namari.git] / src / FukuiNoNamari / thread_view.py
index ebecefe..d7b0b43 100644 (file)
@@ -97,20 +97,38 @@ class ElementEmpty:
     
 class ElementText:
 
+    ch_width_dict = {}   # key: char, value: width
+
     def __init__(self, text, pango_layout):
         self.text = text
         self.pango_layout = pango_layout
 
-        attrlist = self._get_attrs()
-        self.widths = thread_view_extend.get_char_width(
-            pango_layout.get_context(), text, attrlist)
+        self.recalc_char_widths()
 
         self.line_list = []
 
     def recalc_char_widths(self):
-        attrlist = self._get_attrs()
-        self.widths = thread_view_extend.get_char_width(
-            self.pango_layout.get_context(), self.text, attrlist)
+        self.widths = [i for i in itertools.repeat(0, len(self.text))]
+
+        dict = self._get_ch_width_dict()
+        need_to_get = False
+        for index, ch in enumerate(self.text):
+            if ch not in dict:
+                need_to_get = True
+                break
+            else:
+                width = dict[ch]
+                self.widths[index] = width
+
+        if need_to_get:
+            attrlist = self._get_attrs()
+            self.widths = thread_view_extend.get_char_width(
+                self.pango_layout.get_context(), self.text, attrlist)
+            for index, width in enumerate(self.widths):
+                dict[self.text[index]] = self.widths[index]
+
+    def _get_ch_width_dict(self):
+        return ElementText.ch_width_dict
 
     def _get_attrs(self):
         attrs = pango.AttrList()
@@ -200,14 +218,20 @@ class ElementText:
     def draw(self, drawingarea, y_offset, pango_layout,
              selection=False, start_index=0, end_index=0xffffff):
 
-        selection_fg = drawingarea.style.fg[3]
-        selection_bg = drawingarea.style.bg[3]
+        if drawingarea.get_property("has-focus"):
+            selection_fg = drawingarea.style.text[gtk.STATE_SELECTED]
+            selection_bg = drawingarea.style.base[gtk.STATE_SELECTED]
+        else:
+            selection_fg = drawingarea.style.text[gtk.STATE_ACTIVE]
+            selection_bg = drawingarea.style.base[gtk.STATE_ACTIVE]
 
         for line in self.line_list:
 
             text = self.text[line.start_index:line.end_index]
             u_text = text.encode("utf8")
             gc = drawingarea.window.new_gc()
+            gc.set_foreground(drawingarea.style.text[gtk.STATE_NORMAL])
+            gc.set_background(drawingarea.style.base[gtk.STATE_NORMAL])
             attrs = self._get_attrs()
             if selection:
 
@@ -246,6 +270,11 @@ class ElementBoldText(ElementText):
         attrlist.insert(attr)
         return attrlist
 
+    def recalc_char_widths(self):
+        attrlist = self._get_attrs()
+        self.widths = thread_view_extend.get_char_width(
+            self.pango_layout.get_context(), self.text, attrlist)
+
 
 class ElementLink(ElementText):