+from FukuiNoNamariExt import thread_view_extend
+
+
+class Line:
+
+ HEIGHT = 15
+
+ def __init__(self, start_index, end_index, rectangle):
+ self.start_index = start_index
+ self.end_index = end_index
+ self.rectangle = rectangle
+
+ def is_on_xy(self, x, y):
+ left = self.rectangle.x
+ right = left + self.rectangle.width
+ top = self.rectangle.y
+ bottom = top + self.rectangle.height
+ return x >= left and x < right and y >= top and y < bottom
+
+
+class ElementText:
+
+ def __init__(self, text, pango_layout):
+ self.text = text
+
+ attrlist = self._get_attrs()
+ self.widths = thread_view_extend.get_char_width(
+ pango_layout.get_context(), text, attrlist)
+
+ self.line_list = []
+
+ def _get_attrs(self):
+ attrs = pango.AttrList()
+ return attrs
+
+ def is_on_xy(self, x, y):
+ for line in self.line_list:
+ if line.is_on_xy(x, y):
+ return True
+ return False
+
+ def build_line_list(self, x, y, width, left_margin):
+ self.line_list = []
+
+ current_line_start_index = 0
+ current_line_x = x
+ current_line_y = y
+ current_line_width = 0
+
+ for index, ch in enumerate(self.text):
+ ch_w = self.widths[index]
+ ch_h = Line.HEIGHT
+ if current_line_x + current_line_width + ch_w > width:
+ line = Line(
+ current_line_start_index, index,
+ gtk.gdk.Rectangle(
+ current_line_x, current_line_y,
+ current_line_width, Line.HEIGHT))
+ self.line_list.append(line)
+
+ current_line_start_index = index
+ current_line_x = left_margin
+ current_line_y += Line.HEIGHT
+ current_line_width = ch_w
+ else:
+ current_line_width += ch_w
+
+ if current_line_start_index < len(self.text):
+ line = Line(current_line_start_index, len(self.text),
+ gtk.gdk.Rectangle(current_line_x,
+ current_line_y,
+ current_line_width,
+ Line.HEIGHT))
+ self.line_list.append(line)
+
+ current_line_x += current_line_width
+
+ return current_line_x, current_line_y
+
+ def draw(self, drawable, y_offset, pango_layout):
+
+ for line in self.line_list:
+ text = self.text[line.start_index:line.end_index]
+ gc = drawable.new_gc()
+ pango_layout.set_text(text)
+ attrs = self._get_attrs()
+ if attrs:
+ pango_layout.set_attributes(attrs)
+ drawable.draw_layout(gc,
+ line.rectangle.x, line.rectangle.y + y_offset,
+ pango_layout)
+
+
+class ElementBoldText(ElementText):
+
+ def _get_attrs(self):
+ attrlist = pango.AttrList()
+ attr = pango.AttrWeight(pango.WEIGHT_BOLD,
+ end_index=0xffffff)
+ attrlist.insert(attr)
+ return attrlist
+
+
+class ElementLink(ElementText):
+
+ def __init__(self, text, href, pango_layout):
+ self.href = href
+ ElementText.__init__(self, text, pango_layout)
+
+ def _get_attrs(self):
+ attrlist = pango.AttrList()
+ attr = pango.AttrUnderline(pango.UNDERLINE_SINGLE,
+ end_index=0xffffff)
+ attrlist.insert(attr)
+ return attrlist
+
+
+class ResLayout:
+# represent one line
+
+ def __init__(self, left_margin, resnum, pango_layout):
+ self.element_list = []
+ self.width = 0
+ self.height = 0
+ self.pango_layout = pango_layout
+ self.left_margin = left_margin
+ self.resnum = resnum
+ self.posY = 0
+
+ def add_text(self, text, bold, href):
+ if href:
+ element = ElementLink(text, href, self.pango_layout)
+ self.element_list.append(element)
+ elif bold:
+ element = ElementBoldText(text, self.pango_layout)
+ self.element_list.append(element)
+ else:
+ element = ElementText(text, self.pango_layout)
+ self.element_list.append(element)
+
+ def get_element_from_xy(self, x, y):
+ for element in self.element_list:
+ if element.is_on_xy(x, y):
+ return element
+ return None
+
+ def set_width(self, width):
+
+ self.width = width
+
+ current_x = self.left_margin
+ current_y = 0
+
+ for element in self.element_list:
+ current_x, current_y = element.build_line_list(
+ current_x, current_y, width, self.left_margin)
+
+ self.height = current_y + Line.HEIGHT
+
+ def get_pixel_size(self):
+ return self.width, self.height
+
+ def draw(self, drawable, x_offset, y_offset):
+
+ for element in self.element_list:
+ element.draw(drawable, y_offset, self.pango_layout)