OSDN Git Service

Replace PangoLayout with ResLayout.
[fukui-no-namari/fukui-no-namari.git] / src / FukuiNoNamari / thread_window.py
index cd5caed..3cc3f32 100644 (file)
@@ -85,38 +85,34 @@ def open_thread(uri, update=False):
 
 
 class HTMLParserToThreadView:
-    def __init__(self, threadview, resnum):
+    def __init__(self, threadview, resnum, left_margin):
         self.threadview = threadview
         self.resnum = resnum
+        self.left_margin = left_margin
         self.initialize()
 
+    def set_left_margin(self, left_margin):
+        self.left_margin = left_margin
+
     def initialize(self):
-        self.buf = ""
-        self.attrlist = pango.AttrList()
-        self.urilist = []
+        self.layout = None
+
+    def on_new_line(self):
+        self.to_thread_view()
+        self.layout = self.threadview.create_res_layout(
+            self.left_margin, self.resnum)
 
     def from_html_parser(self, data, bold, href):
-        data = data.encode("utf8")
-        start = len(self.buf)
-        end = start + len(data)
-        self.buf += data
-        if bold:
-            attr = pango.AttrWeight(pango.WEIGHT_BOLD, start, end)
-            self.attrlist.insert(attr)
-        if href:
-            attr = pango.AttrUnderline(pango.UNDERLINE_SINGLE, start, end)
-            self.attrlist.insert(attr)
-            self.urilist.append((start, end, href))
-
-    def to_thread_view(self, marginleft):
-        layout = self.threadview.create_pango_layout(self.buf)
-        layout.posY = 0
-        layout.resnum = self.resnum
-        layout.marginleft = marginleft
-        layout.set_attributes(self.attrlist)
-        layout.urilist = self.urilist
-        gobject.idle_add(self.threadview.add_layout, layout)
-        self.initialize()
+        if self.layout == None:
+            self.layout = self.threadview.create_res_layout(
+                self.left_margin, self.resnum)
+
+        self.layout.add_text(data, bold, href)
+
+    def to_thread_view(self):
+        if self.layout is not None:
+            gobject.idle_add(self.threadview.add_layout, self.layout)
+            self.initialize()
 
 
 class WinWrap(winwrapbase.WinWrapBase):
@@ -493,8 +489,9 @@ class WinWrap(winwrapbase.WinWrapBase):
             print "maybe syntax error.", self.num, line
 
     def reselems_to_buffer(self, num, name, mail, date, msg):
-        pipe = HTMLParserToThreadView(self.threadview, num)
-        p = barehtmlparser.BareHTMLParser(pipe.from_html_parser)
+        pipe = HTMLParserToThreadView(self.threadview, num, 0)
+        p = barehtmlparser.BareHTMLParser(
+            pipe.from_html_parser, pipe.on_new_line)
 
         # First, create a pango layout for num,name,mail,date
         # 'margin left' is 0
@@ -511,18 +508,19 @@ class WinWrap(winwrapbase.WinWrapBase):
         p.feed(date)
         p.flush()
 
-        pipe.to_thread_view(0)
+        pipe.to_thread_view()
 
 
         # Second, create a pango layout for message
         # 'margin left' is 20
         # msg
+        pipe.set_left_margin(20)
         p.feed(msg.lstrip(" "))
 
         p.feed("<br>")
         p.close()
 
-        pipe.to_thread_view(20)
+        pipe.to_thread_view()
 
     def href_tag(self, href):
         tag = self.textbuffer.create_tag(underline=pango.UNDERLINE_SINGLE)