OSDN Git Service

selection thread_view
[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 from FukuiNoNamariExt import thread_view_extend
23
24
25 class Line:
26
27     HEIGHT = 15
28
29     def __init__(self, start_index, end_index, rectangle):
30         self.start_index = start_index
31         self.end_index = end_index
32         self.rectangle = rectangle
33
34     def is_on_xy(self, x, y):
35         left = self.rectangle.x
36         right = left + self.rectangle.width
37         top = self.rectangle.y
38         bottom = top + self.rectangle.height
39         return x >= left and x < right and y >= top and y < bottom
40         
41
42 class ElementEmpty:
43
44     def __init__(self):
45         self.initialize()
46
47     def initialize(self):
48         self.line_list = []
49
50     def is_on_xy(self, x, y):
51         for line in self.line_list:
52             if line.is_on_xy(x, y):
53                 return True
54         return False
55
56     def xy_to_index(self, x, y):
57         return 0
58
59     def build_line_list(self, x, y, width, left_margin):
60         self.initialize()
61
62         line = Line(0, 0, gtk.gdk.Rectangle(x, y, width - x, Line.HEIGHT))
63         self.line_list.append(line)
64
65         return width, y
66
67     def draw(self, drawingarea, y_offset, pango_layout,
68              selection=False, start_index=0, end_index=0xffffff):
69         pass
70
71     
72 class ElementText:
73
74     def __init__(self, text, pango_layout):
75         self.text = text
76
77         attrlist = self._get_attrs()
78         self.widths = thread_view_extend.get_char_width(
79             pango_layout.get_context(), text, attrlist)
80
81         self.line_list = []
82
83     def _get_attrs(self):
84         attrs = pango.AttrList()
85         return attrs
86
87     def is_on_xy(self, x, y):
88         for line in self.line_list:
89             if line.is_on_xy(x, y):
90                 return True
91         return False
92
93     def xy_to_index(self, x, y):
94         for line in self.line_list:
95             top = line.rectangle.y
96             bottom = top + line.rectangle.height
97             if y >= top and y < bottom:
98                 sum_of_widths = line.rectangle.x
99                 index = line.start_index
100                 for width in self.widths[line.start_index:line.end_index]:
101                     if sum_of_widths + width/2 > x:
102                         break
103                     sum_of_widths += width
104                     index += 1
105                 return index
106         
107     def build_line_list(self, x, y, width, left_margin):
108         self.line_list = []
109
110         current_line_start_index = 0
111         current_line_x = x
112         current_line_y = y
113         current_line_width = 0
114
115         for index, ch in enumerate(self.text):
116             ch_w = self.widths[index]
117             ch_h = Line.HEIGHT
118             if current_line_x + current_line_width + ch_w > width:
119                 line = Line(
120                     current_line_start_index, index,
121                     gtk.gdk.Rectangle(
122                     current_line_x, current_line_y,
123                     current_line_width, Line.HEIGHT))
124                 self.line_list.append(line)
125
126                 current_line_start_index = index
127                 current_line_x = left_margin
128                 current_line_y += Line.HEIGHT
129                 current_line_width = ch_w
130             else:
131                 current_line_width += ch_w
132
133         if current_line_start_index < len(self.text):
134             line = Line(current_line_start_index, len(self.text),
135                         gtk.gdk.Rectangle(current_line_x,
136                                           current_line_y,
137                                           current_line_width,
138                                           Line.HEIGHT))
139             self.line_list.append(line)
140
141             current_line_x += current_line_width
142
143         return current_line_x, current_line_y
144
145     def draw(self, drawingarea, y_offset, pango_layout,
146              selection=False, start_index=0, end_index=0xffffff):
147
148         selection_fg = drawingarea.style.fg[3]
149         selection_bg = drawingarea.style.bg[3]
150
151         for line in self.line_list:
152
153             text = self.text[line.start_index:line.end_index]
154             u_text = text.encode("utf8")
155             gc = drawingarea.window.new_gc()
156             attrs = self._get_attrs()
157             if selection:
158
159                 s = start_index - line.start_index
160                 s = max(s, 0)
161                 s = min(s, line.end_index - line.start_index)
162                 s = len(text[:s].encode("utf8"))
163
164                 e = end_index - line.start_index
165                 e = min(e, line.end_index - line.start_index)
166                 e = max(e, 0)
167                 e = len(text[:e].encode("utf8"))
168                     
169                 selection_all_attr_fg = pango.AttrForeground(
170                     selection_fg.red, selection_fg.green, selection_fg.blue,
171                     s, e)
172                 selection_all_attr_bg= pango.AttrBackground(
173                     selection_bg.red, selection_bg.green, selection_bg.blue,
174                     s, e)
175                 attrs.insert(selection_all_attr_fg)
176                 attrs.insert(selection_all_attr_bg)
177
178             pango_layout.set_text(u_text)
179             pango_layout.set_attributes(attrs)
180             drawingarea.window.draw_layout(
181                 gc, line.rectangle.x, line.rectangle.y + y_offset,
182                 pango_layout)
183
184
185 class ElementBoldText(ElementText):
186
187     def _get_attrs(self):
188         attrlist = pango.AttrList()
189         attr = pango.AttrWeight(pango.WEIGHT_BOLD,
190                                    end_index=0xffffff)
191         attrlist.insert(attr)
192         return attrlist
193
194
195 class ElementLink(ElementText):
196     
197     def __init__(self, text, href, pango_layout):
198         self.href = href
199         ElementText.__init__(self, text, pango_layout)
200
201     def _get_attrs(self):
202         attrlist = pango.AttrList()
203         attr = pango.AttrUnderline(pango.UNDERLINE_SINGLE,
204                                    end_index=0xffffff)
205         attrlist.insert(attr)
206         return attrlist
207
208
209 class ResLayout:
210 # represent one line
211
212     def __init__(self, left_margin, resnum, pango_layout):
213         self.element_list = [ElementEmpty()]
214         self.width = 0
215         self.height = 0
216         self.pango_layout = pango_layout
217         self.left_margin = left_margin
218         self.resnum = resnum
219         self.posY = 0
220
221     def add_text(self, text, bold, href):
222         if isinstance(self.element_list[0], ElementEmpty):
223             self.element_list = []
224
225         if href:
226             element = ElementLink(text, href, self.pango_layout)
227             self.element_list.append(element)
228         elif bold:
229             element = ElementBoldText(text, self.pango_layout)
230             self.element_list.append(element)
231         else:
232             element = ElementText(text, self.pango_layout)
233             self.element_list.append(element)
234
235     def get_element_from_xy(self, x, y):
236         for element in self.element_list:
237             if element.is_on_xy(x, y):
238                 return element
239         return None
240
241     def get_close_element_from_xy(self, x, y):
242         x= max(x, self.left_margin)
243         element = self.get_element_from_xy(x, y)
244         if element is None and len(self.element_list) != 0:
245             element = self.element_list[len(self.element_list) - 1]
246         return element
247
248     def set_width(self, width):
249
250         self.width = width
251         
252         current_x = self.left_margin
253         current_y = 0
254
255         for element in self.element_list:
256             current_x, current_y = element.build_line_list(
257                 current_x, current_y, width, self.left_margin)
258
259         self.height = current_y + Line.HEIGHT
260
261     def get_pixel_size(self):
262         return self.width, self.height
263
264     def draw(self, drawingarea, x_offset, y_offset,
265              start_selection, end_selection):
266
267         s_s = start_selection
268         e_s = end_selection
269
270         s_l, s_e, s_i = s_s
271         e_l, e_e, e_i = e_s
272
273         if (s_l is None or s_e is None or s_i is None or
274             e_l is None or e_e is None or e_i is None or
275             self.posY < s_l.posY or self.posY > e_l.posY):
276
277             for element in self.element_list:
278                 element.draw(drawingarea, y_offset, self.pango_layout)
279
280         elif self.posY > s_s[0].posY and self.posY < e_s[0].posY:
281
282             for element in self.element_list:
283                 element.draw(drawingarea, y_offset, self.pango_layout,
284                              selection=True)
285
286         elif self == s_s[0] and self == e_s[0]:
287
288             selection = False
289
290             for element in self.element_list:
291                 if s_e == element:
292                     selection = True
293                     start = s_i
294                     end = 0xffffff
295                     if e_e == element:
296                         end = e_i
297                         selection = False
298                     element.draw(drawingarea, y_offset, self.pango_layout,
299                                  selection=True,
300                                  start_index=start,
301                                  end_index=end)
302                 elif e_e == element:
303                     end = e_i
304                     selection = False
305                     element.draw(drawingarea, y_offset, self.pango_layout,
306                                  selection=True, end_index=end)
307                 else:
308                     element.draw(drawingarea, y_offset, self.pango_layout,
309                                  selection=selection)
310
311         elif self == s_s[0]:
312
313             selection = False
314
315             for element in self.element_list:
316                 if s_e == element:
317                     selection = True
318                     start = s_i
319                     element.draw(drawingarea, y_offset, self.pango_layout,
320                                  selection=selection, start_index = start)
321                 else:
322                     element.draw(drawingarea, y_offset, self.pango_layout,
323                                  selection=selection)
324
325         elif self == e_s[0]:
326
327             selection = True
328
329             for element in self.element_list:
330                 if e_e == element:
331                     end = e_i
332                     element.draw(drawingarea, y_offset, self.pango_layout,
333                                  selection=selection, end_index=e_i)
334                     selection = False
335                 else:
336                     element.draw(drawingarea, y_offset, self.pango_layout,
337                                  selection=selection)
338
339         else:
340             for element in self.element_list:
341                 element.draw(drawingarea, y_offset, self.pango_layout)
342                 
343
344
345 class ThreadView(gtk.HBox):
346     hand_cursor = gtk.gdk.Cursor(gtk.gdk.HAND2)
347     regular_cursor = gtk.gdk.Cursor(gtk.gdk.XTERM)
348     arrow_cursor = gtk.gdk.Cursor(gtk.gdk.LEFT_PTR)
349
350     def __init__(self):
351         gtk.HBox.__init__(self, False, 0)
352         self.drawingarea = gtk.DrawingArea()
353         self.vscrollbar = gtk.VScrollbar()
354         self.pack_start(self.drawingarea)
355         self.pack_start(self.vscrollbar, expand=False)
356         self.adjustment  = self.vscrollbar.get_adjustment()
357
358         self.drawingarea.add_events(
359             gtk.gdk.SCROLL_MASK |
360             gtk.gdk.POINTER_MOTION_MASK |
361             gtk.gdk.BUTTON_PRESS_MASK |
362             gtk.gdk.BUTTON_RELEASE_MASK)
363
364         self.drawingarea_prev_width = 0
365
366         self.drawingarea.connect(
367             "expose-event", self.on_drawingarea_expose_event)
368         self.drawingarea.connect(
369             "configure-event", self.on_drawingarea_configure_event)
370         self.drawingarea.connect(
371             "scroll-event", self.on_drawingarea_scroll_event)
372         self.drawingarea.connect(
373             "motion-notify-event", self.on_drawingrarea_motion_notify_event)
374         self.drawingarea.connect(
375             "button-press-event", self.on_drawingarea_button_press_event)
376         self.drawingarea.connect(
377             "button-release-event", self.on_drawingarea_button_release_event)
378         self.vscrollbar.connect(
379             "value-changed", self.on_vscrollbar_value_changed)
380
381         self.pango_layout = self.drawingarea.create_pango_layout("")
382
383         self.initialize_buffer()
384
385         self.on_uri_clicked = self._on_uri_clicked
386
387         self.button1_pressed = False
388         self.current_pressed_uri = None
389             
390         self.popupmenu = None
391         self.menu_openuri = None
392         self.menu_copylinkaddress = None
393         self.menu_separator_link = None
394         self.menu_copyselection = None
395         self.menu_openasuri = None
396         self.menu_separator_selection = None
397
398         self.menud_uri = None
399
400         # for selection
401         self.button_pressed_pt = (None, None, None)
402         self.button_moving_pt = (None, None, None)
403
404     def _on_uri_clicked(self, uri):
405         print uri, "clicked!!!!"
406
407     def initialize_buffer(self):
408         self.res_layout_list = []
409
410     def add_layout(self, res_layout):
411         if (len(self.res_layout_list) != 0):
412             last = self.res_layout_list[len(self.res_layout_list)-1]
413             x, y = last.get_pixel_size()
414             res_layout.posY = last.posY + y
415         self.set_layout_width(res_layout)
416         self.res_layout_list.append(res_layout)
417         x, y = res_layout.get_pixel_size()
418         self.adjustment.upper = res_layout.posY + y
419         self.redraw()
420         self.change_vscrollbar_visible()
421
422     def create_res_layout(self, left_margin, resnum):
423         return ResLayout(left_margin, resnum, self.pango_layout)
424
425     def set_layout_width(self, layout):
426         width = self.drawingarea.allocation.width
427         layout.set_width(width)
428         
429     def redraw(self):
430         self.drawingarea.queue_draw()
431
432     def relayout(self):
433         width = self.drawingarea.allocation.width
434         sum_height = 0
435         for layout in self.res_layout_list:
436             layout.set_width(width)
437             layout.posY = sum_height
438             x, y = layout.get_pixel_size()
439             sum_height += y
440         self.vscrollbar.set_range(0, sum_height)
441         self.change_vscrollbar_visible()
442
443     def change_vscrollbar_visible(self):
444         if self.adjustment.upper < self.adjustment.page_size:
445             self.vscrollbar.hide()
446         else:
447             self.vscrollbar.show()
448
449     def jump(self, value):
450         self.vscrollbar.set_value(value)
451
452     def jump_to_layout(self, layout):
453         self.jump(layout.posY)
454         
455     def jump_to_the_end(self):
456         value = self.adjustment.upper - self.adjustment.page_size
457         self.vscrollbar.set_value(value)
458
459     def jump_to_res(self, resnum):
460         for layout in self.res_layout_list:
461             if layout.resnum == resnum:
462                 self.jump_to_layout(layout)
463                 return True
464         return False
465
466
467     def _get_selection_start_end(self):
468         pressed_layout, pressed_element, pressed_index = self.button_pressed_pt
469         moving_layout, moving_element, moving_index = self.button_moving_pt
470
471         if (pressed_layout is None or pressed_element is None or
472             pressed_index is None or moving_layout is None or
473             moving_element is None or moving_index is None):
474             return (None, None, None), (None, None, None)
475         
476         if pressed_layout == moving_layout:
477             if pressed_element == moving_element:
478                 if moving_index < pressed_index:
479                     return self.button_moving_pt, self.button_pressed_pt
480             else:
481                 pressed_element_index = pressed_layout.element_list.index(
482                     pressed_element)
483                 moving_element_index = moving_layout.element_list.index(
484                     moving_element)
485                 if moving_element_index < pressed_element_index:
486                     return self.button_moving_pt, self.button_pressed_pt
487         elif moving_layout.posY < pressed_layout.posY:
488             return self.button_moving_pt, self.button_pressed_pt
489
490         return self.button_pressed_pt, self.button_moving_pt
491
492     def draw_viewport(self):
493         view_y = self.vscrollbar.get_value()
494         self.drawingarea.window.draw_rectangle(
495             self.drawingarea.style.base_gc[0],
496             True, 0, 0,
497             self.drawingarea.allocation.width,
498             self.drawingarea.allocation.height)
499
500         selection_start, selection_end = self._get_selection_start_end()
501
502         for layout in self.res_layout_list:
503             w, h = layout.get_pixel_size()
504             layout_top = layout.posY
505             layout_bottom = layout.posY + h
506             area_top = view_y
507             area_bottom = view_y + self.drawingarea.allocation.height
508             if layout_top <= area_bottom and layout_bottom >= area_top:
509                 layout.draw(self.drawingarea,
510                             0, layout.posY - int(view_y),
511                             selection_start, selection_end)
512
513     def transform_coordinate_gdk_to_adj(self, y):
514         return y + self.vscrollbar.get_value()
515
516     def transform_coordinate_adj_to_layout(self, x, y, layout):
517         return x, y - layout.posY
518
519     def transform_coordinate_gdk_to_layout(self, x, y, layout):
520         return self.transform_coordinate_adj_to_layout(
521             x, self.transform_coordinate_gdk_to_adj(y), layout)
522
523     def ptrpos_to_layout(self, x, y):
524         # transform coordinate, GdkWindow -> adjustment
525         adj_x = x
526         adj_y = self.transform_coordinate_gdk_to_adj(y)
527         for lay in self.res_layout_list:
528             width, height = lay.get_pixel_size()
529             if (adj_y >= lay.posY and adj_y < lay.posY + height and
530                 adj_x >= 0):
531                 return lay
532         return None
533
534     def ptrpos_to_uri(self,  x, y):
535         # x, y is GdkWindow coordinate
536
537         layout = self.ptrpos_to_layout(x, y)
538
539         if layout is None:
540             return None, None, None
541
542         # transform coordinate, GdkWindow -> res_layout_list
543         lay_x, lay_y = self.transform_coordinate_gdk_to_layout(x, y, layout)
544
545         # xy -> element
546         element = layout.get_element_from_xy(lay_x, lay_y)
547         if isinstance(element, ElementLink):
548             return element.href, layout, element
549
550         return None, layout, None
551
552
553     def _set_button_pressed_pt(self, pt):
554         self.button_pressed_pt = (None, None, None)
555         if pt == None:
556             return
557
558         x, y = pt
559         layout = self.ptrpos_to_layout(x, y)
560         if layout is None:
561             return
562
563         x, y = self.transform_coordinate_gdk_to_layout(x, y, layout)
564         element = layout.get_element_from_xy(x, y)
565         if element is None:
566             element = layout.get_close_element_from_xy(x, y)
567
568         if element is None:
569             return
570
571         index = element.xy_to_index(x, y)
572         if index is None:
573             return
574
575         self.button_pressed_pt = (layout, element, index)
576
577     def _set_button_moving_pt(self, pt):
578         self.button_moving_pt = (None, None, None)
579         if pt == None:
580             return
581
582         x, y = pt
583         layout = self.ptrpos_to_layout(x, y)
584         if layout is None:
585             return
586
587         x, y = self.transform_coordinate_gdk_to_layout(x, y, layout)
588         element = layout.get_element_from_xy(x, y)
589         if element is None:
590             element = layout.get_close_element_from_xy(x, y)
591
592         if element is None:
593             return
594
595         index = element.xy_to_index(x, y)
596         if index is None:
597             return
598
599         self.button_moving_pt = (layout, element, index)
600
601     def on_drawingarea_expose_event(self, widget, event, data=None):
602         self.draw_viewport()
603
604     def on_drawingarea_configure_event(self, widget, event, data=None):
605         if event.width != self.drawingarea_prev_width:
606
607             # before relayout, find top layout on gdkwindow
608             top_layout = None
609             delta = 0
610             for lay in self.res_layout_list:
611                 if lay.posY > self.adjustment.value:
612                     break
613                 top_layout = lay
614
615             if top_layout is not None:
616                 delta = top_layout.posY - self.vscrollbar.get_value()
617
618             self.relayout()
619             self.drawingarea_prev_width = event.width
620
621             # after relayout, set vscrollbar.value to top layout's posY
622             if top_layout is not None:
623                 self.vscrollbar.set_value(top_layout.posY - delta)
624
625         self.adjustment.page_size = self.drawingarea.allocation.height
626         self.vscrollbar.set_increments(20, self.drawingarea.allocation.height)
627
628         # re-set 'value' for prevent overflow
629         self.vscrollbar.set_value(self.vscrollbar.get_value())
630         self.change_vscrollbar_visible()
631
632     def on_vscrollbar_value_changed(self, widget, data=None):
633         self.drawingarea.queue_draw()
634
635     def on_drawingarea_scroll_event(self, widget, event, data=None):
636         self.vscrollbar.emit("scroll-event", event)
637
638     def on_drawingrarea_motion_notify_event(self, widget, event, data=None):
639         if event.state & gtk.gdk.BUTTON1_MASK != gtk.gdk.BUTTON1_MASK:
640             self.button1_pressed = False
641
642         if self.button1_pressed:
643             old_lay, old_elem, old_idx = self.button_moving_pt
644             self._set_button_moving_pt((event.x, event.y))
645             new_lay, new_elem, new_idx = self.button_moving_pt
646             if (old_lay != new_lay
647                 or old_elem != new_elem
648                 or old_idx != new_idx):
649                 self.drawingarea.queue_draw()
650                 #self.drawingarea.window.process_updates(False)
651
652         cursor = ThreadView.regular_cursor
653
654         uri, layout, element = self.ptrpos_to_uri(event.x, event.y)
655         if layout is None:
656             cursor = ThreadView.arrow_cursor
657         else:
658             if uri is not None and uri != "":
659                 cursor = ThreadView.hand_cursor
660
661         self.drawingarea.window.set_cursor(cursor)
662
663     def on_drawingarea_button_press_event(self, widget, event, data=None):
664         if event.button == 1:
665             self.current_pressed_uri = None
666             self.button1_pressed = True
667             uri, layout, element = self.ptrpos_to_uri(event.x, event.y)
668             if uri is not None and layout is not None and element is not None:
669                 self.current_pressed_uri = (uri, layout, element)
670             else:
671                 self._set_button_moving_pt(None)
672                 self._set_button_pressed_pt((event.x, event.y))
673                 self.drawingarea.queue_draw()
674                 
675         elif event.button == 3:
676             time = event.time
677             uri, layout, element = self.ptrpos_to_uri(event.x, event.y)
678             if uri is not None and layout is not None and element is not None:
679                 self.menu_openuri.show()
680                 self.menu_copylinkaddress.show()
681                 self.menu_separator_link.show()
682                 self.menu_openuri.uri = uri
683                 self.menu_copylinkaddress.uri = uri
684             else:
685                 self.menu_openuri.hide()
686                 self.menu_copylinkaddress.hide()
687                 self.menu_separator_link.hide()
688                 self.menu_openuri.uri = None
689                 self.menu_copylinkaddress.uri = None
690
691             self.menu_copyselection.hide()
692             self.menu_openasuri.hide()
693             self.menu_separator_selection.hide()
694
695             self.popupmenu.popup(None, None, None, event.button, time)
696             return True
697             
698
699     def on_drawingarea_button_release_event(self, widget, event, data=None):
700         if event.button == 1:
701             button1_pressed = self.button1_pressed
702             self.button1_pressed = False
703
704             if button1_pressed and self.current_pressed_uri is not None:
705                 uri, layout, element = self.ptrpos_to_uri(event.x, event.y)
706                 p_uri, p_layout, p_element = self.current_pressed_uri
707                 self.current_preesed_uri = None
708                 if (uri == p_uri and layout == p_layout and
709                     element == p_element):
710                     self.on_uri_clicked(uri)