OSDN Git Service

separate DrawingArea
[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         self.pangolayout.append(pangolayout)
57
58     def create_pango_layout(self, text):
59         return self.drawingarea.create_pango_layout(text)
60
61     def redraw(self):
62         self.drawingarea.queue_draw()
63
64     def relayout(self):
65         width = self.drawingarea.allocation.width
66         sum_height = 0
67         for layout in self.pangolayout:
68             layout.set_width((width - layout.marginleft) * pango.SCALE)
69             layout.posY = sum_height
70             x, y = layout.get_pixel_size()
71             sum_height += y
72         self.adjustment.upper = sum_height
73
74     def jump(self, value):
75         if value > self.adjustment.upper - self.adjustment.page_size:
76             value = self.adjustment.upper - self.adjustment.page_size
77         self.adjustment.set_value(value)
78
79     def jump_to_layout(self, layout):
80         self.jump(layout.posY)
81         
82     def jump_to_the_end(self):
83         value = self.adjustment.upper - self.adjustment.page_size
84         self.adjustment.set_value(value)
85
86     def jump_to_res(self, resnum):
87         for layout in self.pangolayout:
88             if layout.resnum == resnum:
89                 self.jump_to_layout(layout)
90                 return True
91         return False
92
93     def draw_viewport(self):
94         view_y = self.adjustment.get_value()
95         self.drawingarea.window.draw_rectangle(
96             self.drawingarea.style.base_gc[0],
97             True, 0, 0,
98             self.drawingarea.allocation.width,
99             self.drawingarea.allocation.height)
100
101         gc = self.drawingarea.window.new_gc()
102         for layout in self.pangolayout:
103             w, h = layout.get_pixel_size()
104             layout_top = layout.posY
105             layout_bottom = layout.posY + h
106             area_top = view_y
107             area_bottom = view_y + self.drawingarea.allocation.height
108             if layout_top <= area_bottom and layout_bottom >= area_top:
109                 self.drawingarea.window.draw_layout(
110                     gc, layout.marginleft, layout.posY - int(view_y), layout)
111
112     def on_drawingarea_expose_event(self, widget, event, data=None):
113         self.draw_viewport()
114
115     def on_drawingarea_size_allocate(self, widget, allocation, data=None):
116         if allocation.width != self.drawingarea_prev_width:
117             self.relayout()
118             self.drawingarea_prev_width = allocation.width
119         self.adjustment.page_size = self.drawingarea.allocation.height
120         self.adjustment.page_increment = self.drawingarea.allocation.height
121
122     def on_drawingarea_button_press_event(self, widget, event, data=None):
123         self.drawingarea.queue_draw()
124
125     def on_vscrollbar_value_changed(self, widget, data=None):
126         self.drawingarea.queue_draw()
127
128     def on_drawingarea_scroll_event(self, widget, event, data=None):
129         if event.direction == gtk.gdk.SCROLL_UP:
130             self.adjustment.value -= 66.476200804
131             if self.adjustment.value < self.adjustment.lower:
132                 self.adjustment.value = self.adjustment.lower
133         if event.direction == gtk.gdk.SCROLL_DOWN:
134             self.adjustment.value += 66.476200804
135             max_value = self.adjustment.upper - self.adjustment.page_size
136             if self.adjustment.value > max_value:
137                 self.adjustment.value = max_value