OSDN Git Service

Selected text colors follow the focus in ThreadView.
[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         if drawingarea.get_property("has-focus"):
222             selection_fg = drawingarea.style.text[gtk.STATE_SELECTED]
223             selection_bg = drawingarea.style.base[gtk.STATE_SELECTED]
224         else:
225             selection_fg = drawingarea.style.text[gtk.STATE_ACTIVE]
226             selection_bg = drawingarea.style.base[gtk.STATE_ACTIVE]
227
228         for line in self.line_list:
229
230             text = self.text[line.start_index:line.end_index]
231             u_text = text.encode("utf8")
232             gc = drawingarea.window.new_gc()
233             gc.set_foreground(drawingarea.style.text[gtk.STATE_NORMAL])
234             gc.set_background(drawingarea.style.base[gtk.STATE_NORMAL])
235             attrs = self._get_attrs()
236             if selection:
237
238                 s = start_index - line.start_index
239                 s = max(s, 0)
240                 s = min(s, line.end_index - line.start_index)
241                 s = len(text[:s].encode("utf8"))
242
243                 e = end_index - line.start_index
244                 e = min(e, line.end_index - line.start_index)
245                 e = max(e, 0)
246                 e = len(text[:e].encode("utf8"))
247                     
248                 selection_all_attr_fg = pango.AttrForeground(
249                     selection_fg.red, selection_fg.green, selection_fg.blue,
250                     s, e)
251                 selection_all_attr_bg= pango.AttrBackground(
252                     selection_bg.red, selection_bg.green, selection_bg.blue,
253                     s, e)
254                 attrs.insert(selection_all_attr_fg)
255                 attrs.insert(selection_all_attr_bg)
256
257             pango_layout.set_text(u_text)
258             pango_layout.set_attributes(attrs)
259             drawingarea.window.draw_layout(
260                 gc, int(line.rectangle.x), line.rectangle.y + y_offset,
261                 pango_layout)
262
263
264 class ElementBoldText(ElementText):
265
266     def _get_attrs(self):
267         attrlist = pango.AttrList()
268         attr = pango.AttrWeight(pango.WEIGHT_BOLD,
269                                    end_index=0xffffff)
270         attrlist.insert(attr)
271         return attrlist
272
273     def recalc_char_widths(self):
274         attrlist = self._get_attrs()
275         self.widths = thread_view_extend.get_char_width(
276             self.pango_layout.get_context(), self.text, attrlist)
277
278
279 class ElementLink(ElementText):
280     
281     def __init__(self, text, href, pango_layout):
282         self.href = href
283         ElementText.__init__(self, text, pango_layout)
284
285     def _get_attrs(self):
286         attrlist = pango.AttrList()
287         attr = pango.AttrUnderline(pango.UNDERLINE_SINGLE,
288                                    end_index=0xffffff)
289         attrlist.insert(attr)
290         return attrlist
291
292
293 class ResLayout:
294 # represent one line
295
296     def __init__(self, left_margin, resnum, pango_layout):
297         self.element_list = [ElementEmpty(pango_layout)]
298         self.width = 0
299         self.height = 0
300         self.pango_layout = pango_layout
301         self.left_margin = left_margin
302         self.resnum = resnum
303         self.posY = 0
304         self.list_index = 0
305
306     def add_text(self, text, bold, href):
307         if isinstance(self.element_list[0], ElementEmpty):
308             self.element_list = []
309
310         if href:
311             element = ElementLink(text, href, self.pango_layout)
312             self.element_list.append(element)
313         elif bold:
314             element = ElementBoldText(text, self.pango_layout)
315             self.element_list.append(element)
316         else:
317             element = ElementText(text, self.pango_layout)
318             self.element_list.append(element)
319
320     def get_element_from_xy(self, x, y):
321         for element in self.element_list:
322             if element.is_on_xy(x, y):
323                 return element
324         return None
325
326     def get_close_element_from_xy(self, x, y):
327         x= max(x, self.left_margin)
328         element = self.get_element_from_xy(x, y)
329         if element is None and len(self.element_list) != 0:
330             element = self.element_list[len(self.element_list) - 1]
331         return element
332
333     def recalc_char_widths(self):
334         for element in self.element_list:
335             element.recalc_char_widths()
336
337     def set_width(self, width):
338
339         self.width = width
340         
341         current_x = self.left_margin
342         current_y = 0
343
344         for element in self.element_list:
345             current_x, current_y = element.build_line_list(
346                 current_x, current_y, width, self.left_margin)
347
348         self.height = current_y + get_approximate_char_height(self.pango_layout.get_context())
349
350     def get_pixel_size(self):
351         return self.width, self.height
352
353     def get_text(self, selection_start, selection_end):
354         s_s = selection_start
355         e_s = selection_end
356         s_l, s_e, s_i = selection_start
357         e_l, e_e, e_i = selection_end
358
359         text = ""
360
361         if (s_l is None or s_e is None or s_i is None or
362             e_l is None or e_e is None or e_i is None or
363             self.posY < s_l.posY or self.posY > e_l.posY):
364
365             # nothing to do
366             pass
367
368         elif self.posY > s_s[0].posY and self.posY < e_s[0].posY:
369
370             for element in self.element_list:
371                 text += element.get_text(selection=True)
372
373         elif self == s_s[0] and self == e_s[0]:
374
375             selection = False
376
377             for element in self.element_list:
378                 if s_e == element:
379                     selection = True
380                     start = s_i
381                     end = 0xffffff
382                     if e_e == element:
383                         end = e_i
384                         selection = False
385                     text += element.get_text(selection=True, start_index=start,
386                                              end_index=end)
387                 elif e_e == element:
388                     end = e_i
389                     selection = False
390                     text += element.get_text(
391                         selection=True, end_index=end)
392                 elif selection:
393                     text += element.get_text(selection=True)
394
395         elif self == s_s[0]:
396
397             selection = False
398
399             for element in self.element_list:
400                 if s_e == element:
401                     selection = True
402                     start = s_i
403                     text += element.get_text(selection=True, start_index=start)
404                 elif selection:
405                     text += element.get_text(selection=True)
406
407         elif self == e_s[0]:
408
409             selection = True
410
411             for element in self.element_list:
412                 if e_e == element:
413                     end = e_i
414                     text += element.get_text(selection=True, end_index=e_i)
415                     selection = False
416                 elif selection:
417                     text += element.get_text(selection=True)
418
419         else:
420             # nothing to do
421             pass
422
423         return text
424
425
426     def draw(self, drawingarea, x_offset, y_offset,
427              start_selection, end_selection):
428
429         s_s = start_selection
430         e_s = end_selection
431
432         s_l, s_e, s_i = s_s
433         e_l, e_e, e_i = e_s
434
435         if (s_l is None or s_e is None or s_i is None or
436             e_l is None or e_e is None or e_i is None or
437             self.posY < s_l.posY or self.posY > e_l.posY):
438
439             for element in self.element_list:
440                 element.draw(drawingarea, y_offset, self.pango_layout)
441
442         elif self.posY > s_s[0].posY and self.posY < e_s[0].posY:
443
444             for element in self.element_list:
445                 element.draw(drawingarea, y_offset, self.pango_layout,
446                              selection=True)
447
448         elif self == s_s[0] and self == e_s[0]:
449
450             selection = False
451
452             for element in self.element_list:
453                 if s_e == element:
454                     selection = True
455                     start = s_i
456                     end = 0xffffff
457                     if e_e == element:
458                         end = e_i
459                         selection = False
460                     element.draw(drawingarea, y_offset, self.pango_layout,
461                                  selection=True,
462                                  start_index=start,
463                                  end_index=end)
464                 elif e_e == element:
465                     end = e_i
466                     selection = False
467                     element.draw(drawingarea, y_offset, self.pango_layout,
468                                  selection=True, end_index=end)
469                 else:
470                     element.draw(drawingarea, y_offset, self.pango_layout,
471                                  selection=selection)
472
473         elif self == s_s[0]:
474
475             selection = False
476
477             for element in self.element_list:
478                 if s_e == element:
479                     selection = True
480                     start = s_i
481                     element.draw(drawingarea, y_offset, self.pango_layout,
482                                  selection=selection, start_index = start)
483                 else:
484                     element.draw(drawingarea, y_offset, self.pango_layout,
485                                  selection=selection)
486
487         elif self == e_s[0]:
488
489             selection = True
490
491             for element in self.element_list:
492                 if e_e == element:
493                     end = e_i
494                     element.draw(drawingarea, y_offset, self.pango_layout,
495                                  selection=selection, end_index=e_i)
496                     selection = False
497                 else:
498                     element.draw(drawingarea, y_offset, self.pango_layout,
499                                  selection=selection)
500
501         else:
502             for element in self.element_list:
503                 element.draw(drawingarea, y_offset, self.pango_layout)
504
505     def clone(self):
506         import copy
507         layout = ResLayout(self.left_margin, self.resnum, self.pango_layout)
508         layout.element_list = []
509         for element in self.element_list:
510             layout.element_list.append(copy.copy(element))
511         return layout
512
513
514 class ThreadView(gtk.HBox):
515     __gsignals__ = {
516         "cursor-over-link-event":
517         (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (object, object, )),
518         "uri-clicked-event":
519         (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (object, ))
520         }
521
522     hand_cursor = gtk.gdk.Cursor(gtk.gdk.HAND2)
523     regular_cursor = gtk.gdk.Cursor(gtk.gdk.XTERM)
524     arrow_cursor = gtk.gdk.Cursor(gtk.gdk.LEFT_PTR)
525
526     def __init__(self):
527         gtk.HBox.__init__(self, False, 0)
528         self.drawingarea = gtk.DrawingArea()
529         self.drawingarea.set_property("can_focus", True)
530
531         self.vscrollbar = gtk.VScrollbar()
532         self.pack_start(self.drawingarea)
533         self.pack_start(self.vscrollbar, expand=False)
534         self.adjustment  = self.vscrollbar.get_adjustment()
535
536         self.drawingarea.add_events(
537             gtk.gdk.KEY_PRESS_MASK |
538             gtk.gdk.SCROLL_MASK |
539             gtk.gdk.POINTER_MOTION_MASK |
540             gtk.gdk.BUTTON_PRESS_MASK |
541             gtk.gdk.BUTTON_RELEASE_MASK)
542
543         self.drawingarea_prev_width = 0
544
545         self.drawingarea.connect(
546             "expose-event", self.on_drawingarea_expose_event)
547         self.drawingarea.connect(
548             "configure-event", self.on_drawingarea_configure_event)
549         self.drawingarea.connect(
550             "scroll-event", self.on_drawingarea_scroll_event)
551         self.drawingarea.connect(
552             "motion-notify-event", self.on_drawingrarea_motion_notify_event)
553         self.drawingarea.connect(
554             "button-press-event", self.on_drawingarea_button_press_event)
555         self.drawingarea.connect(
556             "button-release-event", self.on_drawingarea_button_release_event)
557         self.drawingarea.connect(
558             "style-set", self.on_drawingarea_style_set)
559         self.drawingarea.connect(
560             "key-press-event", self.on_drawingarea_key_press_event)
561         self.vscrollbar.connect(
562             "value-changed", self.on_vscrollbar_value_changed)
563
564         self.pango_layout = self.drawingarea.create_pango_layout("")
565
566         self.initialize_buffer()
567
568         self.button1_pressed = False
569         self.current_pressed_uri = None
570             
571         self.popupmenu = None
572         self.menu_openuri = None
573         self.menu_copylinkaddress = None
574         self.menu_separator_link = None
575         self.menu_copyselection = None
576         self.menu_openasuri = None
577         self.menu_separator_selection = None
578
579         self.menud_uri = None
580
581         # for selection
582         self.button_pressed_pt = (None, None, None)
583         self.button_moving_pt = (None, None, None)
584
585     def initialize_buffer(self):
586         self.res_layout_list = []
587
588     def add_layout(self, res_layout):
589         if (len(self.res_layout_list) != 0):
590             last = self.res_layout_list[len(self.res_layout_list)-1]
591             x, y = last.get_pixel_size()
592             res_layout.posY = last.posY + y
593         self.set_layout_width(res_layout)
594         res_layout.list_index = len(self.res_layout_list)
595         self.res_layout_list.append(res_layout)
596
597         x, y = res_layout.get_pixel_size()
598         self.adjustment.upper = res_layout.posY + y
599         # do not use this method in a loop because expensive.
600         # self.redraw()
601         self.change_vscrollbar_visible()
602
603     def create_res_layout(self, left_margin, resnum):
604         return ResLayout(left_margin, resnum, self.pango_layout)
605
606     def set_layout_width(self, layout):
607         width = self.drawingarea.allocation.width
608         layout.set_width(width)
609         
610     def redraw(self):
611         self.drawingarea.queue_draw()
612
613     def wrap_relayout(self):
614         # before relayout, find top layout on gdkwindow
615         top_layout = self.get_layout_on_y(self.adjustment.value)
616         delta = 0
617
618         if top_layout is not None:
619             delta = top_layout.posY - self.vscrollbar.get_value()
620
621         self.relayout()
622         self.drawingarea_prev_width = self.drawingarea.allocation.width
623
624         # after relayout, set vscrollbar.value to top layout's posY
625         if top_layout is not None:
626             self.vscrollbar.set_value(top_layout.posY - delta)
627
628     def relayout(self):
629         width = self.drawingarea.allocation.width
630         sum_height = 0
631         for layout in self.res_layout_list:
632             layout.set_width(width)
633             layout.posY = sum_height
634             x, y = layout.get_pixel_size()
635             sum_height += y
636
637         self.vscrollbar.set_range(0, sum_height)
638         self.change_vscrollbar_visible()
639
640     def change_vscrollbar_visible(self):
641         if self.adjustment.upper <= self.adjustment.page_size:
642             self.vscrollbar.hide()
643         else:
644             self.vscrollbar.show()
645
646     def jump(self, value):
647         self.vscrollbar.set_value(value)
648
649     def jump_to_layout(self, layout):
650         self.jump(layout.posY)
651         
652     def jump_to_the_end(self):
653         value = self.adjustment.upper - self.adjustment.page_size
654         self.vscrollbar.set_value(value)
655
656     def jump_to_res(self, resnum):
657         for layout in self.res_layout_list:
658             if layout.resnum == resnum:
659                 self.jump_to_layout(layout)
660                 return True
661         return False
662
663
664     def _get_selection_start_end(self):
665         pressed_layout, pressed_element, pressed_index = self.button_pressed_pt
666         moving_layout, moving_element, moving_index = self.button_moving_pt
667
668         if (pressed_layout is None or pressed_element is None or
669             pressed_index is None or moving_layout is None or
670             moving_element is None or moving_index is None):
671             return (None, None, None), (None, None, None)
672         
673         if pressed_layout == moving_layout:
674             if pressed_element == moving_element:
675                 if moving_index < pressed_index:
676                     return self.button_moving_pt, self.button_pressed_pt
677             else:
678                 pressed_element_index = pressed_layout.element_list.index(
679                     pressed_element)
680                 moving_element_index = moving_layout.element_list.index(
681                     moving_element)
682                 if moving_element_index < pressed_element_index:
683                     return self.button_moving_pt, self.button_pressed_pt
684         elif moving_layout.posY < pressed_layout.posY:
685             return self.button_moving_pt, self.button_pressed_pt
686
687         return self.button_pressed_pt, self.button_moving_pt
688
689     def draw_viewport(self, area):
690         view_y = self.vscrollbar.get_value()
691         self.drawingarea.window.draw_rectangle(
692             self.drawingarea.style.base_gc[0],
693             True, area.x, area.y, area.width, area.height)
694
695         selection_start, selection_end = self._get_selection_start_end()
696
697         top_layout = self.get_layout_on_y(view_y)
698         if top_layout is None:
699             return
700         #area_top = view_y + area.y
701         area_bottom = view_y + area.y + area.height
702
703         iter = range(top_layout.list_index, len(self.res_layout_list))
704         iter = itertools.imap(lambda index: self.res_layout_list[index], iter)
705         iter = itertools.takewhile(lambda lay: lay.posY <= area_bottom, iter)
706         for layout in iter:
707             layout.draw(self.drawingarea, 0, layout.posY - int(view_y),
708                 selection_start, selection_end)
709
710     def transform_coordinate_gdk_to_adj(self, y):
711         return y + self.vscrollbar.get_value()
712
713     def transform_coordinate_adj_to_layout(self, x, y, layout):
714         return x, y - layout.posY
715
716     def transform_coordinate_gdk_to_layout(self, x, y, layout):
717         return self.transform_coordinate_adj_to_layout(
718             x, self.transform_coordinate_gdk_to_adj(y), layout)
719
720     def get_layout_on_y(self, y):
721
722         def binary_search(lst, start, end, func):
723
724             if end - start <= 0:
725                 return None
726
727             m = (start + end) / 2
728             ret = func(lst[m])
729
730             if ret == 0:
731                 return m
732             if ret > 0:
733                 return binary_search(lst, start, m, func)
734             return binary_search(lst, m+1, end, func)
735
736         def on_y(layout, _y):
737             top = layout.posY
738             width, height = layout.get_pixel_size()
739             bottom = top + height
740             if _y >= top and _y < bottom:
741                 return 0
742             if _y < top:
743                 return 1
744             return -1
745
746         ret = binary_search(
747             self.res_layout_list, 0, len(self.res_layout_list),
748             lambda x: on_y(x, y))
749         if ret is not None:
750             return self.res_layout_list[ret]
751         return None
752
753     def ptrpos_to_layout(self, x, y):
754         # transform coordinate, GdkWindow -> adjustment
755         adj_y = self.transform_coordinate_gdk_to_adj(y)
756         return self.get_layout_on_y(adj_y)
757
758     def ptrpos_to_uri(self,  x, y):
759         # x, y is GdkWindow coordinate
760
761         layout = self.ptrpos_to_layout(x, y)
762
763         if layout is None:
764             return None, None, None
765
766         # transform coordinate, GdkWindow -> res_layout_list
767         lay_x, lay_y = self.transform_coordinate_gdk_to_layout(x, y, layout)
768
769         # xy -> element
770         element = layout.get_element_from_xy(lay_x, lay_y)
771         if isinstance(element, ElementLink):
772             return element.href, layout, element
773
774         return None, layout, None
775
776     def get_selected_text(self):
777         selection_start, selection_end = self._get_selection_start_end()
778         s_l, s_e, s_i = selection_start
779         e_l, e_e, e_i = selection_end
780
781         if (s_l is None or s_e is None or s_i is None or
782             e_l is None or e_e is None or e_i is None):
783             return ""
784
785         text = ""
786         index = s_l.list_index
787         end = e_l.list_index
788
789         while index <= end:
790             layout = self.res_layout_list[index]
791
792             text += layout.get_text(selection_start, selection_end)
793             if index != end:
794                 text += "\n"
795
796             index += 1
797
798         return text
799
800     def _set_button_pressed_pt(self, pt):
801         self.button_pressed_pt = (None, None, None)
802         if pt == None:
803             return
804
805         x, y = pt
806         layout = self.ptrpos_to_layout(x, y)
807         if layout is None:
808             return
809
810         x, y = self.transform_coordinate_gdk_to_layout(x, y, layout)
811         element = layout.get_element_from_xy(x, y)
812         if element is None:
813             element = layout.get_close_element_from_xy(x, y)
814
815         if element is None:
816             return
817
818         index = element.xy_to_index(x, y)
819         if index is None:
820             return
821
822         self.button_pressed_pt = (layout, element, index)
823
824     def _set_button_moving_pt(self, pt):
825         self.button_moving_pt = (None, None, None)
826         if pt == None:
827             return
828
829         x, y = pt
830         layout = self.ptrpos_to_layout(x, y)
831         if layout is None:
832             return
833
834         x, y = self.transform_coordinate_gdk_to_layout(x, y, layout)
835         element = layout.get_element_from_xy(x, y)
836         if element is None:
837             element = layout.get_close_element_from_xy(x, y)
838
839         if element is None:
840             return
841
842         index = element.xy_to_index(x, y)
843         if index is None:
844             return
845
846         self.button_moving_pt = (layout, element, index)
847
848     def on_drawingarea_expose_event(self, widget, event, data=None):
849         self.draw_viewport(event.area)
850
851     def on_drawingarea_configure_event(self, widget, event, data=None):
852         if event.width != self.drawingarea_prev_width:
853             self.wrap_relayout()
854
855         self.adjustment.page_size = self.drawingarea.allocation.height
856         self.vscrollbar.set_increments(20, self.drawingarea.allocation.height)
857
858         # re-set 'value' for prevent overflow
859         self.vscrollbar.set_value(self.vscrollbar.get_value())
860         self.change_vscrollbar_visible()
861
862     def on_vscrollbar_value_changed(self, widget, data=None):
863         self.drawingarea.queue_draw()
864
865     def on_drawingarea_scroll_event(self, widget, event, data=None):
866         self.vscrollbar.emit("scroll-event", event)
867
868     def on_drawingrarea_motion_notify_event(self, widget, event, data=None):
869         if event.state & gtk.gdk.BUTTON1_MASK != gtk.gdk.BUTTON1_MASK:
870             self.button1_pressed = False
871
872         if self.button1_pressed and self.current_pressed_uri is None:
873             old_lay, old_elem, old_idx = self.button_moving_pt
874             self._set_button_moving_pt((event.x, event.y))
875             new_lay, new_elem, new_idx = self.button_moving_pt
876             if (old_lay != new_lay
877                 or old_elem != new_elem
878                 or old_idx != new_idx):
879                 view_y = self.vscrollbar.get_value()
880                 o_y = old_lay.posY
881                 n_y = new_lay.posY
882                 o_width, o_height = old_lay.get_pixel_size()
883                 n_width, n_height = new_lay.get_pixel_size()
884
885                 y = min(o_y, n_y)
886                 height = max(o_y, n_y) - y
887                 if o_y > n_y: height += o_height
888                 else: height += n_height
889
890                 y -= view_y
891
892                 self.drawingarea.queue_draw_area(0, y, n_width, height+1)
893                 #self.drawingarea.window.process_updates(False)
894
895         cursor = ThreadView.regular_cursor
896
897         uri, layout, element = self.ptrpos_to_uri(event.x, event.y)
898         if layout is None:
899             cursor = ThreadView.arrow_cursor
900         else:
901             if uri is not None and uri != "":
902                 cursor = ThreadView.hand_cursor
903                 self.emit("cursor-over-link-event", event, uri)
904
905         self.drawingarea.window.set_cursor(cursor)
906
907     def on_drawingarea_button_press_event(self, widget, event, data=None):
908         if event.button == 1:
909             self.current_pressed_uri = None
910             self.button1_pressed = True
911             uri, layout, element = self.ptrpos_to_uri(event.x, event.y)
912             if uri is not None and layout is not None and element is not None:
913                 self.current_pressed_uri = (uri, layout, element)
914             else:
915                 self._set_button_moving_pt((event.x, event.y))
916                 self._set_button_pressed_pt((event.x, event.y))
917                 self.drawingarea.queue_draw()
918                 
919         elif event.button == 3:
920             time = event.time
921             uri, layout, element = self.ptrpos_to_uri(event.x, event.y)
922             if uri is not None and layout is not None and element is not None:
923                 self.menu_openuri.show()
924                 self.menu_copylinkaddress.show()
925                 self.menu_separator_link.show()
926                 self.menu_openuri.uri = uri
927                 self.menu_copylinkaddress.uri = uri
928             else:
929                 self.menu_openuri.hide()
930                 self.menu_copylinkaddress.hide()
931                 self.menu_separator_link.hide()
932                 self.menu_openuri.uri = None
933                 self.menu_copylinkaddress.uri = None
934
935             text = self.get_selected_text()
936             if text and len(text) > 0:
937                 self.menu_copyselection.show()
938                 self.menu_openasuri.show()
939                 self.menu_separator_selection.show()
940             else:
941                 self.menu_copyselection.hide()
942                 self.menu_openasuri.hide()
943                 self.menu_separator_selection.hide()
944
945             self.popupmenu.popup(None, None, None, event.button, time)
946             return True
947             
948
949     def on_drawingarea_button_release_event(self, widget, event, data=None):
950         if event.button == 1:
951             button1_pressed = self.button1_pressed
952             self.button1_pressed = False
953
954             if button1_pressed and self.current_pressed_uri is not None:
955                 uri, layout, element = self.ptrpos_to_uri(event.x, event.y)
956                 p_uri, p_layout, p_element = self.current_pressed_uri
957                 self.current_pressed_uri = None
958                 if (uri == p_uri and layout == p_layout and
959                     element == p_element):
960                     self.emit("uri-clicked-event", uri)
961
962     def on_drawingarea_style_set(self, widget, previous_style, data=None):
963         if previous_style is None:
964             return False
965
966         new = widget.style.font_desc.hash()
967         old = previous_style.font_desc.hash()
968         if new != old:
969             for layout in self.res_layout_list:
970                 layout.recalc_char_widths()
971             self.wrap_relayout()
972
973     def on_drawingarea_key_press_event(self, widget, event, data=None):
974         if event.type is not gtk.gdk.KEY_PRESS:
975             return
976
977         if event.keyval in (gtk.keysyms.Up, gtk.keysyms.Down,
978                             gtk.keysyms.Page_Up, gtk.keysyms.Page_Down,
979                             gtk.keysyms.Home):
980             value = self.vscrollbar.get_value()
981             if event.keyval == gtk.keysyms.Up:
982                 step_increment = self.adjustment.get_property("step-increment")
983                 value = value - step_increment
984             elif event.keyval == gtk.keysyms.Down:
985                 step_increment = self.adjustment.get_property("step-increment")
986                 value = value + step_increment
987             elif event.keyval == gtk.keysyms.Page_Up:
988                 step_increment = self.adjustment.get_property("page-increment")
989                 value = value - step_increment
990             elif event.keyval == gtk.keysyms.Page_Down:
991                 step_increment = self.adjustment.get_property("page-increment")
992                 value = value + step_increment
993             elif event.keyval == gtk.keysyms.Home:
994                 value = 0
995             self.jump(value)
996         elif event.keyval == gtk.keysyms.End:
997             self.jump_to_the_end()