OSDN Git Service

ThreadView, character widths are cached. This is expected to improve performance.
[fukui-no-namari/fukui-no-namari.git] / src / FukuiNoNamari / thread_view.py
1 # Copyright (C) 2007 by Aiwota Programmer
2 # aiwotaprog@tetteke.tk
3 #
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
18 import pygtk
19 pygtk.require('2.0')
20 import gtk
21 import pango
22 import gobject
23 import itertools
24 from FukuiNoNamariExt import thread_view_extend
25
26
27 def get_approximate_char_height(pango_context):
28     desc = pango_context.get_font_description()
29     font = pango_context.load_font(desc)
30     ink, log = font.get_glyph_extents(0)
31     return log[3] / pango.SCALE + 2
32
33
34 class Rectangle:
35     def __init__(self, x, y, width, height):
36         self.x = x
37         self.y = y
38         self.width = width
39         self.height = height
40
41
42 class Line:
43
44     HEIGHT = 15
45
46     def __init__(self, start_index, end_index, rectangle):
47         self.start_index = start_index
48         self.end_index = end_index
49         self.rectangle = rectangle
50
51     def is_on_xy(self, x, y):
52         left = self.rectangle.x
53         right = left + self.rectangle.width
54         top = self.rectangle.y
55         bottom = top + self.rectangle.height
56         return x >= left and x < right and y >= top and y < bottom
57         
58
59 class ElementEmpty:
60
61     def __init__(self, pango_layout):
62         self.pango_layout = pango_layout
63         self.initialize()
64
65     def recalc_char_widths(self):
66         pass
67         
68     def initialize(self):
69         self.line_list = []
70
71     def is_on_xy(self, x, y):
72         for line in self.line_list:
73             if line.is_on_xy(x, y):
74                 return True
75         return False
76
77     def xy_to_index(self, x, y):
78         return 0
79
80     def build_line_list(self, x, y, width, left_margin):
81         self.initialize()
82
83         line = Line(0, 0, Rectangle(
84             x, y, width - x,
85             get_approximate_char_height(self.pango_layout.get_context())))
86         self.line_list.append(line)
87
88         return width, y
89
90     def get_text(self, selection=False, start_index=0, end_index=0xffffff):
91         return ""
92
93     def draw(self, drawingarea, y_offset, pango_layout,
94              selection=False, start_index=0, end_index=0xffffff):
95         pass
96
97     
98 class ElementText:
99
100     ch_width_dict = {}   # key: char, value: width
101
102     def __init__(self, text, pango_layout):
103         self.text = text
104         self.pango_layout = pango_layout
105
106         self.recalc_char_widths()
107
108         self.line_list = []
109
110     def recalc_char_widths(self):
111         self.widths = [i for i in itertools.repeat(0, len(self.text))]
112
113         dict = self._get_ch_width_dict()
114         need_to_get = False
115         for index, ch in enumerate(self.text):
116             if ch not in dict:
117                 need_to_get = True
118                 break
119             else:
120                 width = dict[ch]
121                 self.widths[index] = width
122
123         if need_to_get:
124             attrlist = self._get_attrs()
125             self.widths = thread_view_extend.get_char_width(
126                 self.pango_layout.get_context(), self.text, attrlist)
127             for index, width in enumerate(self.widths):
128                 dict[self.text[index]] = self.widths[index]
129
130     def _get_ch_width_dict(self):
131         return ElementText.ch_width_dict
132
133     def _get_attrs(self):
134         attrs = pango.AttrList()
135         return attrs
136
137     def is_on_xy(self, x, y):
138         for line in self.line_list:
139             if line.is_on_xy(x, y):
140                 return True
141         return False
142
143     def xy_to_index(self, x, y):
144         for line in self.line_list:
145             top = line.rectangle.y
146             bottom = top + line.rectangle.height
147             if y >= top and y < bottom:
148                 sum_of_widths = line.rectangle.x
149                 index = line.start_index
150                 for width in self.widths[line.start_index:line.end_index]:
151                     if sum_of_widths + width/2 > x:
152                         break
153                     sum_of_widths += width
154                     index += 1
155                 return index
156         
157     def build_line_list(self, x, y, width, left_margin):
158         self.line_list = []
159
160         current_line_start_index = 0
161         current_line_x = x
162         current_line_y = y
163         current_line_width = 0
164
165         ch_h = get_approximate_char_height(self.pango_layout.get_context())
166
167         for index, ch in enumerate(self.text):
168             ch_w = self.widths[index]
169             if current_line_x + current_line_width + ch_w > width:
170                 line = Line(
171                     current_line_start_index, index,
172                     Rectangle(
173                     current_line_x, current_line_y,
174                     current_line_width, ch_h))
175                 self.line_list.append(line)
176
177                 current_line_start_index = index
178                 current_line_x = left_margin
179                 current_line_y += ch_h
180                 current_line_width = ch_w
181             else:
182                 current_line_width += ch_w
183
184         if current_line_start_index < len(self.text):
185             line = Line(current_line_start_index, len(self.text),
186                         Rectangle(current_line_x,
187                                           current_line_y,
188                                           current_line_width,
189                                           ch_h))
190             self.line_list.append(line)
191
192             current_line_x += current_line_width
193
194         return current_line_x, current_line_y
195
196     def get_text(self, selection=False, start_index=0, end_index=0xffffff):
197
198         text = ""
199
200         for line in self.line_list:
201
202             t = self.text[line.start_index:line.end_index]
203             if selection:
204                 s = start_index - line.start_index
205                 s = max(s, 0)
206                 s = min(s, line.end_index - line.start_index)
207
208                 e = end_index - line.start_index
209                 e = min(e, line.end_index - line.start_index)
210                 e = max(e, 0)
211
212                 t = t[s:e]
213
214             text += t
215
216         return text
217
218     def draw(self, drawingarea, y_offset, pango_layout,
219              selection=False, start_index=0, end_index=0xffffff):
220
221         selection_fg = drawingarea.style.fg[3]
222         selection_bg = drawingarea.style.bg[3]
223
224         for line in self.line_list:
225
226             text = self.text[line.start_index:line.end_index]
227             u_text = text.encode("utf8")
228             gc = drawingarea.window.new_gc()
229             attrs = self._get_attrs()
230             if selection:
231
232                 s = start_index - line.start_index
233                 s = max(s, 0)
234                 s = min(s, line.end_index - line.start_index)
235                 s = len(text[:s].encode("utf8"))
236
237                 e = end_index - line.start_index
238                 e = min(e, line.end_index - line.start_index)
239                 e = max(e, 0)
240                 e = len(text[:e].encode("utf8"))
241                     
242                 selection_all_attr_fg = pango.AttrForeground(
243                     selection_fg.red, selection_fg.green, selection_fg.blue,
244                     s, e)
245                 selection_all_attr_bg= pango.AttrBackground(
246                     selection_bg.red, selection_bg.green, selection_bg.blue,
247                     s, e)
248                 attrs.insert(selection_all_attr_fg)
249                 attrs.insert(selection_all_attr_bg)
250
251             pango_layout.set_text(u_text)
252             pango_layout.set_attributes(attrs)
253             drawingarea.window.draw_layout(
254                 gc, int(line.rectangle.x), line.rectangle.y + y_offset,
255                 pango_layout)
256
257
258 class ElementBoldText(ElementText):
259
260     def _get_attrs(self):
261         attrlist = pango.AttrList()
262         attr = pango.AttrWeight(pango.WEIGHT_BOLD,
263                                    end_index=0xffffff)
264         attrlist.insert(attr)
265         return attrlist
266
267     def recalc_char_widths(self):
268         attrlist = self._get_attrs()
269         self.widths = thread_view_extend.get_char_width(
270             self.pango_layout.get_context(), self.text, attrlist)
271
272
273 class ElementLink(ElementText):
274     
275     def __init__(self, text, href, pango_layout):
276         self.href = href
277         ElementText.__init__(self, text, pango_layout)
278
279     def _get_attrs(self):
280         attrlist = pango.AttrList()
281         attr = pango.AttrUnderline(pango.UNDERLINE_SINGLE,
282                                    end_index=0xffffff)
283         attrlist.insert(attr)
284         return attrlist
285
286
287 class ResLayout:
288 # represent one line
289
290     def __init__(self, left_margin, resnum, pango_layout):
291         self.element_list = [ElementEmpty(pango_layout)]
292         self.width = 0
293         self.height = 0
294         self.pango_layout = pango_layout
295         self.left_margin = left_margin
296         self.resnum = resnum
297         self.posY = 0
298         self.list_index = 0
299
300     def add_text(self, text, bold, href):
301         if isinstance(self.element_list[0], ElementEmpty):
302             self.element_list = []
303
304         if href:
305             element = ElementLink(text, href, self.pango_layout)
306             self.element_list.append(element)
307         elif bold:
308             element = ElementBoldText(text, self.pango_layout)
309             self.element_list.append(element)
310         else:
311             element = ElementText(text, self.pango_layout)
312             self.element_list.append(element)
313
314     def get_element_from_xy(self, x, y):
315         for element in self.element_list:
316             if element.is_on_xy(x, y):
317                 return element
318         return None
319
320     def get_close_element_from_xy(self, x, y):
321         x= max(x, self.left_margin)
322         element = self.get_element_from_xy(x, y)
323         if element is None and len(self.element_list) != 0:
324             element = self.element_list[len(self.element_list) - 1]
325         return element
326
327     def recalc_char_widths(self):
328         for element in self.element_list:
329             element.recalc_char_widths()
330
331     def set_width(self, width):
332
333         self.width = width
334         
335         current_x = self.left_margin
336         current_y = 0
337
338         for element in self.element_list:
339             current_x, current_y = element.build_line_list(
340                 current_x, current_y, width, self.left_margin)
341
342         self.height = current_y + get_approximate_char_height(self.pango_layout.get_context())
343
344     def get_pixel_size(self):
345         return self.width, self.height
346
347     def get_text(self, selection_start, selection_end):
348         s_s = selection_start
349         e_s = selection_end
350         s_l, s_e, s_i = selection_start
351         e_l, e_e, e_i = selection_end
352
353         text = ""
354
355         if (s_l is None or s_e is None or s_i is None or
356             e_l is None or e_e is None or e_i is None or
357             self.posY < s_l.posY or self.posY > e_l.posY):
358
359             # nothing to do
360             pass
361
362         elif self.posY > s_s[0].posY and self.posY < e_s[0].posY:
363
364             for element in self.element_list:
365                 text += element.get_text(selection=True)
366
367         elif self == s_s[0] and self == e_s[0]:
368
369             selection = False
370
371             for element in self.element_list:
372                 if s_e == element:
373                     selection = True
374                     start = s_i
375                     end = 0xffffff
376                     if e_e == element:
377                         end = e_i
378                         selection = False
379                     text += element.get_text(selection=True, start_index=start,
380                                              end_index=end)
381                 elif e_e == element:
382                     end = e_i
383                     selection = False
384                     text += element.get_text(
385                         selection=True, end_index=end)
386                 elif selection:
387                     text += element.get_text(selection=True)
388
389         elif self == s_s[0]:
390
391             selection = False
392
393             for element in self.element_list:
394                 if s_e == element:
395                     selection = True
396                     start = s_i
397                     text += element.get_text(selection=True, start_index=start)
398                 elif selection:
399                     text += element.get_text(selection=True)
400
401         elif self == e_s[0]:
402
403             selection = True
404
405             for element in self.element_list:
406                 if e_e == element:
407                     end = e_i
408                     text += element.get_text(selection=True, end_index=e_i)
409                     selection = False
410                 elif selection:
411                     text += element.get_text(selection=True)
412
413         else:
414             # nothing to do
415             pass
416
417         return text
418
419
420     def draw(self, drawingarea, x_offset, y_offset,
421              start_selection, end_selection):
422
423         s_s = start_selection
424         e_s = end_selection
425
426         s_l, s_e, s_i = s_s
427         e_l, e_e, e_i = e_s
428
429         if (s_l is None or s_e is None or s_i is None or
430             e_l is None or e_e is None or e_i is None or
431             self.posY < s_l.posY or self.posY > e_l.posY):
432
433             for element in self.element_list:
434                 element.draw(drawingarea, y_offset, self.pango_layout)
435
436         elif self.posY > s_s[0].posY and self.posY < e_s[0].posY:
437
438             for element in self.element_list:
439                 element.draw(drawingarea, y_offset, self.pango_layout,
440                              selection=True)
441
442         elif self == s_s[0] and self == e_s[0]:
443
444             selection = False
445
446             for element in self.element_list:
447                 if s_e == element:
448                     selection = True
449                     start = s_i
450                     end = 0xffffff
451                     if e_e == element:
452                         end = e_i
453                         selection = False
454                     element.draw(drawingarea, y_offset, self.pango_layout,
455                                  selection=True,
456                                  start_index=start,
457                                  end_index=end)
458                 elif e_e == element:
459                     end = e_i
460                     selection = False
461                     element.draw(drawingarea, y_offset, self.pango_layout,
462                                  selection=True, end_index=end)
463                 else:
464                     element.draw(drawingarea, y_offset, self.pango_layout,
465                                  selection=selection)
466
467         elif self == s_s[0]:
468
469             selection = False
470
471             for element in self.element_list:
472                 if s_e == element:
473                     selection = True
474                     start = s_i
475                     element.draw(drawingarea, y_offset, self.pango_layout,
476                                  selection=selection, start_index = start)
477                 else:
478                     element.draw(drawingarea, y_offset, self.pango_layout,
479                                  selection=selection)
480
481         elif self == e_s[0]:
482
483             selection = True
484
485             for element in self.element_list:
486                 if e_e == element:
487                     end = e_i
488                     element.draw(drawingarea, y_offset, self.pango_layout,
489                                  selection=selection, end_index=e_i)
490                     selection = False
491                 else:
492                     element.draw(drawingarea, y_offset, self.pango_layout,
493                                  selection=selection)
494
495         else:
496             for element in self.element_list:
497                 element.draw(drawingarea, y_offset, self.pango_layout)
498
499     def clone(self):
500         import copy
501         layout = ResLayout(self.left_margin, self.resnum, self.pango_layout)
502         layout.element_list = []
503         for element in self.element_list:
504             layout.element_list.append(copy.copy(element))
505         return layout
506
507
508 class ThreadView(gtk.HBox):
509     __gsignals__ = {
510         "cursor-over-link-event":
511         (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (object, object, )),
512         "uri-clicked-event":
513         (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (object, ))
514         }
515
516     hand_cursor = gtk.gdk.Cursor(gtk.gdk.HAND2)
517     regular_cursor = gtk.gdk.Cursor(gtk.gdk.XTERM)
518     arrow_cursor = gtk.gdk.Cursor(gtk.gdk.LEFT_PTR)
519
520     def __init__(self):
521         gtk.HBox.__init__(self, False, 0)
522         self.drawingarea = gtk.DrawingArea()
523         self.drawingarea.set_property("can_focus", True)
524
525         self.vscrollbar = gtk.VScrollbar()
526         self.pack_start(self.drawingarea)
527         self.pack_start(self.vscrollbar, expand=False)
528         self.adjustment  = self.vscrollbar.get_adjustment()
529
530         self.drawingarea.add_events(
531             gtk.gdk.KEY_PRESS_MASK |
532             gtk.gdk.SCROLL_MASK |
533             gtk.gdk.POINTER_MOTION_MASK |
534             gtk.gdk.BUTTON_PRESS_MASK |
535             gtk.gdk.BUTTON_RELEASE_MASK)
536
537         self.drawingarea_prev_width = 0
538
539         self.drawingarea.connect(
540             "expose-event", self.on_drawingarea_expose_event)
541         self.drawingarea.connect(
542             "configure-event", self.on_drawingarea_configure_event)
543         self.drawingarea.connect(
544             "scroll-event", self.on_drawingarea_scroll_event)
545         self.drawingarea.connect(
546             "motion-notify-event", self.on_drawingrarea_motion_notify_event)
547         self.drawingarea.connect(
548             "button-press-event", self.on_drawingarea_button_press_event)
549         self.drawingarea.connect(
550             "button-release-event", self.on_drawingarea_button_release_event)
551         self.drawingarea.connect(
552             "style-set", self.on_drawingarea_style_set)
553         self.drawingarea.connect(
554             "key-press-event", self.on_drawingarea_key_press_event)
555         self.vscrollbar.connect(
556             "value-changed", self.on_vscrollbar_value_changed)
557
558         self.pango_layout = self.drawingarea.create_pango_layout("")
559
560         self.initialize_buffer()
561
562         self.button1_pressed = False
563         self.current_pressed_uri = None
564             
565         self.popupmenu = None
566         self.menu_openuri = None
567         self.menu_copylinkaddress = None
568         self.menu_separator_link = None
569         self.menu_copyselection = None
570         self.menu_openasuri = None
571         self.menu_separator_selection = None
572
573         self.menud_uri = None
574
575         # for selection
576         self.button_pressed_pt = (None, None, None)
577         self.button_moving_pt = (None, None, None)
578
579     def initialize_buffer(self):
580         self.res_layout_list = []
581
582     def add_layout(self, res_layout):
583         if (len(self.res_layout_list) != 0):
584             last = self.res_layout_list[len(self.res_layout_list)-1]
585             x, y = last.get_pixel_size()
586             res_layout.posY = last.posY + y
587         self.set_layout_width(res_layout)
588         res_layout.list_index = len(self.res_layout_list)
589         self.res_layout_list.append(res_layout)
590
591         x, y = res_layout.get_pixel_size()
592         self.adjustment.upper = res_layout.posY + y
593         # do not use this method in a loop because expensive.
594         # self.redraw()
595         self.change_vscrollbar_visible()
596
597     def create_res_layout(self, left_margin, resnum):
598         return ResLayout(left_margin, resnum, self.pango_layout)
599
600     def set_layout_width(self, layout):
601         width = self.drawingarea.allocation.width
602         layout.set_width(width)
603         
604     def redraw(self):
605         self.drawingarea.queue_draw()
606
607     def wrap_relayout(self):
608         # before relayout, find top layout on gdkwindow
609         top_layout = self.get_layout_on_y(self.adjustment.value)
610         delta = 0
611
612         if top_layout is not None:
613             delta = top_layout.posY - self.vscrollbar.get_value()
614
615         self.relayout()
616         self.drawingarea_prev_width = self.drawingarea.allocation.width
617
618         # after relayout, set vscrollbar.value to top layout's posY
619         if top_layout is not None:
620             self.vscrollbar.set_value(top_layout.posY - delta)
621
622     def relayout(self):
623         width = self.drawingarea.allocation.width
624         sum_height = 0
625         for layout in self.res_layout_list:
626             layout.set_width(width)
627             layout.posY = sum_height
628             x, y = layout.get_pixel_size()
629             sum_height += y
630
631         self.vscrollbar.set_range(0, sum_height)
632         self.change_vscrollbar_visible()
633
634     def change_vscrollbar_visible(self):
635         if self.adjustment.upper <= self.adjustment.page_size:
636             self.vscrollbar.hide()
637         else:
638             self.vscrollbar.show()
639
640     def jump(self, value):
641         self.vscrollbar.set_value(value)
642
643     def jump_to_layout(self, layout):
644         self.jump(layout.posY)
645         
646     def jump_to_the_end(self):
647         value = self.adjustment.upper - self.adjustment.page_size
648         self.vscrollbar.set_value(value)
649
650     def jump_to_res(self, resnum):
651         for layout in self.res_layout_list:
652             if layout.resnum == resnum:
653                 self.jump_to_layout(layout)
654                 return True
655         return False
656
657
658     def _get_selection_start_end(self):
659         pressed_layout, pressed_element, pressed_index = self.button_pressed_pt
660         moving_layout, moving_element, moving_index = self.button_moving_pt
661
662         if (pressed_layout is None or pressed_element is None or
663             pressed_index is None or moving_layout is None or
664             moving_element is None or moving_index is None):
665             return (None, None, None), (None, None, None)
666         
667         if pressed_layout == moving_layout:
668             if pressed_element == moving_element:
669                 if moving_index < pressed_index:
670                     return self.button_moving_pt, self.button_pressed_pt
671             else:
672                 pressed_element_index = pressed_layout.element_list.index(
673                     pressed_element)
674                 moving_element_index = moving_layout.element_list.index(
675                     moving_element)
676                 if moving_element_index < pressed_element_index:
677                     return self.button_moving_pt, self.button_pressed_pt
678         elif moving_layout.posY < pressed_layout.posY:
679             return self.button_moving_pt, self.button_pressed_pt
680
681         return self.button_pressed_pt, self.button_moving_pt
682
683     def draw_viewport(self, area):
684         view_y = self.vscrollbar.get_value()
685         self.drawingarea.window.draw_rectangle(
686             self.drawingarea.style.base_gc[0],
687             True, area.x, area.y, area.width, area.height)
688
689         selection_start, selection_end = self._get_selection_start_end()
690
691         top_layout = self.get_layout_on_y(view_y)
692         if top_layout is None:
693             return
694         #area_top = view_y + area.y
695         area_bottom = view_y + area.y + area.height
696
697         iter = range(top_layout.list_index, len(self.res_layout_list))
698         iter = itertools.imap(lambda index: self.res_layout_list[index], iter)
699         iter = itertools.takewhile(lambda lay: lay.posY <= area_bottom, iter)
700         for layout in iter:
701             layout.draw(self.drawingarea, 0, layout.posY - int(view_y),
702                 selection_start, selection_end)
703
704     def transform_coordinate_gdk_to_adj(self, y):
705         return y + self.vscrollbar.get_value()
706
707     def transform_coordinate_adj_to_layout(self, x, y, layout):
708         return x, y - layout.posY
709
710     def transform_coordinate_gdk_to_layout(self, x, y, layout):
711         return self.transform_coordinate_adj_to_layout(
712             x, self.transform_coordinate_gdk_to_adj(y), layout)
713
714     def get_layout_on_y(self, y):
715
716         def binary_search(lst, start, end, func):
717
718             if end - start <= 0:
719                 return None
720
721             m = (start + end) / 2
722             ret = func(lst[m])
723
724             if ret == 0:
725                 return m
726             if ret > 0:
727                 return binary_search(lst, start, m, func)
728             return binary_search(lst, m+1, end, func)
729
730         def on_y(layout, _y):
731             top = layout.posY
732             width, height = layout.get_pixel_size()
733             bottom = top + height
734             if _y >= top and _y < bottom:
735                 return 0
736             if _y < top:
737                 return 1
738             return -1
739
740         ret = binary_search(
741             self.res_layout_list, 0, len(self.res_layout_list),
742             lambda x: on_y(x, y))
743         if ret is not None:
744             return self.res_layout_list[ret]
745         return None
746
747     def ptrpos_to_layout(self, x, y):
748         # transform coordinate, GdkWindow -> adjustment
749         adj_y = self.transform_coordinate_gdk_to_adj(y)
750         return self.get_layout_on_y(adj_y)
751
752     def ptrpos_to_uri(self,  x, y):
753         # x, y is GdkWindow coordinate
754
755         layout = self.ptrpos_to_layout(x, y)
756
757         if layout is None:
758             return None, None, None
759
760         # transform coordinate, GdkWindow -> res_layout_list
761         lay_x, lay_y = self.transform_coordinate_gdk_to_layout(x, y, layout)
762
763         # xy -> element
764         element = layout.get_element_from_xy(lay_x, lay_y)
765         if isinstance(element, ElementLink):
766             return element.href, layout, element
767
768         return None, layout, None
769
770     def get_selected_text(self):
771         selection_start, selection_end = self._get_selection_start_end()
772         s_l, s_e, s_i = selection_start
773         e_l, e_e, e_i = selection_end
774
775         if (s_l is None or s_e is None or s_i is None or
776             e_l is None or e_e is None or e_i is None):
777             return ""
778
779         text = ""
780         index = s_l.list_index
781         end = e_l.list_index
782
783         while index <= end:
784             layout = self.res_layout_list[index]
785
786             text += layout.get_text(selection_start, selection_end)
787             if index != end:
788                 text += "\n"
789
790             index += 1
791
792         return text
793
794     def _set_button_pressed_pt(self, pt):
795         self.button_pressed_pt = (None, None, None)
796         if pt == None:
797             return
798
799         x, y = pt
800         layout = self.ptrpos_to_layout(x, y)
801         if layout is None:
802             return
803
804         x, y = self.transform_coordinate_gdk_to_layout(x, y, layout)
805         element = layout.get_element_from_xy(x, y)
806         if element is None:
807             element = layout.get_close_element_from_xy(x, y)
808
809         if element is None:
810             return
811
812         index = element.xy_to_index(x, y)
813         if index is None:
814             return
815
816         self.button_pressed_pt = (layout, element, index)
817
818     def _set_button_moving_pt(self, pt):
819         self.button_moving_pt = (None, None, None)
820         if pt == None:
821             return
822
823         x, y = pt
824         layout = self.ptrpos_to_layout(x, y)
825         if layout is None:
826             return
827
828         x, y = self.transform_coordinate_gdk_to_layout(x, y, layout)
829         element = layout.get_element_from_xy(x, y)
830         if element is None:
831             element = layout.get_close_element_from_xy(x, y)
832
833         if element is None:
834             return
835
836         index = element.xy_to_index(x, y)
837         if index is None:
838             return
839
840         self.button_moving_pt = (layout, element, index)
841
842     def on_drawingarea_expose_event(self, widget, event, data=None):
843         self.draw_viewport(event.area)
844
845     def on_drawingarea_configure_event(self, widget, event, data=None):
846         if event.width != self.drawingarea_prev_width:
847             self.wrap_relayout()
848
849         self.adjustment.page_size = self.drawingarea.allocation.height
850         self.vscrollbar.set_increments(20, self.drawingarea.allocation.height)
851
852         # re-set 'value' for prevent overflow
853         self.vscrollbar.set_value(self.vscrollbar.get_value())
854         self.change_vscrollbar_visible()
855
856     def on_vscrollbar_value_changed(self, widget, data=None):
857         self.drawingarea.queue_draw()
858
859     def on_drawingarea_scroll_event(self, widget, event, data=None):
860         self.vscrollbar.emit("scroll-event", event)
861
862     def on_drawingrarea_motion_notify_event(self, widget, event, data=None):
863         if event.state & gtk.gdk.BUTTON1_MASK != gtk.gdk.BUTTON1_MASK:
864             self.button1_pressed = False
865
866         if self.button1_pressed and self.current_pressed_uri is None:
867             old_lay, old_elem, old_idx = self.button_moving_pt
868             self._set_button_moving_pt((event.x, event.y))
869             new_lay, new_elem, new_idx = self.button_moving_pt
870             if (old_lay != new_lay
871                 or old_elem != new_elem
872                 or old_idx != new_idx):
873                 view_y = self.vscrollbar.get_value()
874                 o_y = old_lay.posY
875                 n_y = new_lay.posY
876                 o_width, o_height = old_lay.get_pixel_size()
877                 n_width, n_height = new_lay.get_pixel_size()
878
879                 y = min(o_y, n_y)
880                 height = max(o_y, n_y) - y
881                 if o_y > n_y: height += o_height
882                 else: height += n_height
883
884                 y -= view_y
885
886                 self.drawingarea.queue_draw_area(0, y, n_width, height+1)
887                 #self.drawingarea.window.process_updates(False)
888
889         cursor = ThreadView.regular_cursor
890
891         uri, layout, element = self.ptrpos_to_uri(event.x, event.y)
892         if layout is None:
893             cursor = ThreadView.arrow_cursor
894         else:
895             if uri is not None and uri != "":
896                 cursor = ThreadView.hand_cursor
897                 self.emit("cursor-over-link-event", event, uri)
898
899         self.drawingarea.window.set_cursor(cursor)
900
901     def on_drawingarea_button_press_event(self, widget, event, data=None):
902         if event.button == 1:
903             self.current_pressed_uri = None
904             self.button1_pressed = True
905             uri, layout, element = self.ptrpos_to_uri(event.x, event.y)
906             if uri is not None and layout is not None and element is not None:
907                 self.current_pressed_uri = (uri, layout, element)
908             else:
909                 self._set_button_moving_pt((event.x, event.y))
910                 self._set_button_pressed_pt((event.x, event.y))
911                 self.drawingarea.queue_draw()
912                 
913         elif event.button == 3:
914             time = event.time
915             uri, layout, element = self.ptrpos_to_uri(event.x, event.y)
916             if uri is not None and layout is not None and element is not None:
917                 self.menu_openuri.show()
918                 self.menu_copylinkaddress.show()
919                 self.menu_separator_link.show()
920                 self.menu_openuri.uri = uri
921                 self.menu_copylinkaddress.uri = uri
922             else:
923                 self.menu_openuri.hide()
924                 self.menu_copylinkaddress.hide()
925                 self.menu_separator_link.hide()
926                 self.menu_openuri.uri = None
927                 self.menu_copylinkaddress.uri = None
928
929             text = self.get_selected_text()
930             if text and len(text) > 0:
931                 self.menu_copyselection.show()
932                 self.menu_openasuri.show()
933                 self.menu_separator_selection.show()
934             else:
935                 self.menu_copyselection.hide()
936                 self.menu_openasuri.hide()
937                 self.menu_separator_selection.hide()
938
939             self.popupmenu.popup(None, None, None, event.button, time)
940             return True
941             
942
943     def on_drawingarea_button_release_event(self, widget, event, data=None):
944         if event.button == 1:
945             button1_pressed = self.button1_pressed
946             self.button1_pressed = False
947
948             if button1_pressed and self.current_pressed_uri is not None:
949                 uri, layout, element = self.ptrpos_to_uri(event.x, event.y)
950                 p_uri, p_layout, p_element = self.current_pressed_uri
951                 self.current_pressed_uri = None
952                 if (uri == p_uri and layout == p_layout and
953                     element == p_element):
954                     self.emit("uri-clicked-event", uri)
955
956     def on_drawingarea_style_set(self, widget, previous_style, data=None):
957         if previous_style is None:
958             return False
959
960         new = widget.style.font_desc.hash()
961         old = previous_style.font_desc.hash()
962         if new != old:
963             for layout in self.res_layout_list:
964                 layout.recalc_char_widths()
965             self.wrap_relayout()
966
967     def on_drawingarea_key_press_event(self, widget, event, data=None):
968         if event.type is not gtk.gdk.KEY_PRESS:
969             return
970
971         if event.keyval in (gtk.keysyms.Up, gtk.keysyms.Down,
972                             gtk.keysyms.Page_Up, gtk.keysyms.Page_Down,
973                             gtk.keysyms.Home):
974             value = self.vscrollbar.get_value()
975             if event.keyval == gtk.keysyms.Up:
976                 step_increment = self.adjustment.get_property("step-increment")
977                 value = value - step_increment
978             elif event.keyval == gtk.keysyms.Down:
979                 step_increment = self.adjustment.get_property("step-increment")
980                 value = value + step_increment
981             elif event.keyval == gtk.keysyms.Page_Up:
982                 step_increment = self.adjustment.get_property("page-increment")
983                 value = value - step_increment
984             elif event.keyval == gtk.keysyms.Page_Down:
985                 step_increment = self.adjustment.get_property("page-increment")
986                 value = value + step_increment
987             elif event.keyval == gtk.keysyms.Home:
988                 value = 0
989             self.jump(value)
990         elif event.keyval == gtk.keysyms.End:
991             self.jump_to_the_end()