OSDN Git Service

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