OSDN Git Service

not relayout pango layout list when adding pango layout.
[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
23
24 class ThreadView(gtk.HBox):
25
26     def __init__(self):
27         gtk.HBox.__init__(self, False, 0)
28         self.drawingarea = gtk.DrawingArea()
29         self.vscrollbar = gtk.VScrollbar()
30         self.pack_start(self.drawingarea)
31         self.pack_start(self.vscrollbar, expand=False)
32         self.adjustment  = self.vscrollbar.get_adjustment()
33
34         self.drawingarea.add_events(gtk.gdk.SCROLL_MASK)
35
36         self.adjustment.step_increment = 20
37         self.drawingarea_prev_width = 0
38
39         self.drawingarea.connect(
40             "expose-event", self.on_drawingarea_expose_event)
41         self.drawingarea.connect(
42             "size-allocate", self.on_drawingarea_size_allocate)
43         self.drawingarea.connect(
44             "button-press-event", self.on_drawingarea_button_press_event)
45         self.drawingarea.connect(
46             "scroll-event", self.on_drawingarea_scroll_event)
47         self.vscrollbar.connect(
48             "value-changed", self.on_vscrollbar_value_changed)
49
50         self.initialize_buffer()
51
52     def initialize_buffer(self):
53         self.pangolayout = []
54
55     def add_layout(self, pangolayout):
56         if (len(self.pangolayout) != 0):
57             last = self.pangolayout[len(self.pangolayout)-1]
58             x, y = last.get_pixel_size()
59             pangolayout.posY = last.posY + y
60         self.set_layout_width(pangolayout)
61         self.pangolayout.append(pangolayout)
62         x, y = pangolayout.get_pixel_size()
63         self.adjustment.upper = pangolayout.posY + y
64         self.redraw()
65
66     def create_pango_layout(self, text):
67         return self.drawingarea.create_pango_layout(text)
68
69     def set_layout_width(self, layout):
70         width = self.drawingarea.allocation.width
71         layout.set_width((width - layout.marginleft) * pango.SCALE)
72         
73     def redraw(self):
74         self.drawingarea.queue_draw()
75
76     def relayout(self):
77         width = self.drawingarea.allocation.width
78         sum_height = 0
79         for layout in self.pangolayout:
80             layout.set_width((width - layout.marginleft) * pango.SCALE)
81             layout.posY = sum_height
82             x, y = layout.get_pixel_size()
83             sum_height += y
84         self.adjustment.upper = sum_height
85
86     def jump(self, value):
87         if value > self.adjustment.upper - self.adjustment.page_size:
88             value = self.adjustment.upper - self.adjustment.page_size
89         self.adjustment.set_value(value)
90
91     def jump_to_layout(self, layout):
92         self.jump(layout.posY)
93         
94     def jump_to_the_end(self):
95         value = self.adjustment.upper - self.adjustment.page_size
96         self.adjustment.set_value(value)
97
98     def jump_to_res(self, resnum):
99         for layout in self.pangolayout:
100             if layout.resnum == resnum:
101                 self.jump_to_layout(layout)
102                 return True
103         return False
104
105     def draw_viewport(self):
106         view_y = self.adjustment.get_value()
107         self.drawingarea.window.draw_rectangle(
108             self.drawingarea.style.base_gc[0],
109             True, 0, 0,
110             self.drawingarea.allocation.width,
111             self.drawingarea.allocation.height)
112
113         gc = self.drawingarea.window.new_gc()
114         for layout in self.pangolayout:
115             w, h = layout.get_pixel_size()
116             layout_top = layout.posY
117             layout_bottom = layout.posY + h
118             area_top = view_y
119             area_bottom = view_y + self.drawingarea.allocation.height
120             if layout_top <= area_bottom and layout_bottom >= area_top:
121                 self.drawingarea.window.draw_layout(
122                     gc, layout.marginleft, layout.posY - int(view_y), layout)
123
124     def on_drawingarea_expose_event(self, widget, event, data=None):
125         self.draw_viewport()
126
127     def on_drawingarea_size_allocate(self, widget, allocation, data=None):
128         if allocation.width != self.drawingarea_prev_width:
129             self.relayout()
130             self.drawingarea_prev_width = allocation.width
131         self.adjustment.page_size = self.drawingarea.allocation.height
132         self.adjustment.page_increment = self.drawingarea.allocation.height
133
134     def on_drawingarea_button_press_event(self, widget, event, data=None):
135         self.drawingarea.queue_draw()
136
137     def on_vscrollbar_value_changed(self, widget, data=None):
138         self.drawingarea.queue_draw()
139
140     def on_drawingarea_scroll_event(self, widget, event, data=None):
141         if event.direction == gtk.gdk.SCROLL_UP:
142             self.adjustment.value -= 66.476200804
143             if self.adjustment.value < self.adjustment.lower:
144                 self.adjustment.value = self.adjustment.lower
145         if event.direction == gtk.gdk.SCROLL_DOWN:
146             self.adjustment.value += 66.476200804
147             max_value = self.adjustment.upper - self.adjustment.page_size
148             if self.adjustment.value > max_value:
149                 self.adjustment.value = max_value