OSDN Git Service

implement uri click event
[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     hand_cursor = gtk.gdk.Cursor(gtk.gdk.HAND2)
26     regular_cursor = gtk.gdk.Cursor(gtk.gdk.XTERM)
27     arrow_cursor = gtk.gdk.Cursor(gtk.gdk.LEFT_PTR)
28
29     def __init__(self):
30         gtk.HBox.__init__(self, False, 0)
31         self.drawingarea = gtk.DrawingArea()
32         self.vscrollbar = gtk.VScrollbar()
33         self.pack_start(self.drawingarea)
34         self.pack_start(self.vscrollbar, expand=False)
35         self.adjustment  = self.vscrollbar.get_adjustment()
36
37         self.drawingarea.add_events(
38             gtk.gdk.SCROLL_MASK |
39             gtk.gdk.POINTER_MOTION_MASK |
40             gtk.gdk.BUTTON_PRESS_MASK |
41             gtk.gdk.BUTTON_RELEASE_MASK)
42
43         self.adjustment.step_increment = 20
44         self.drawingarea_prev_width = 0
45
46         self.drawingarea.connect(
47             "expose-event", self.on_drawingarea_expose_event)
48         self.drawingarea.connect(
49             "size-allocate", self.on_drawingarea_size_allocate)
50         self.drawingarea.connect(
51             "button-press-event", self.on_drawingarea_button_press_event)
52         self.drawingarea.connect(
53             "scroll-event", self.on_drawingarea_scroll_event)
54         self.drawingarea.connect(
55             "motion-notify-event", self.on_drawingrarea_motion_notify_event)
56         self.drawingarea.connect(
57             "button-press-event", self.on_drawingrarea_button_press_event)
58         self.drawingarea.connect(
59             "button-release-event", self.on_drawingrarea_button_release_event)
60         self.vscrollbar.connect(
61             "value-changed", self.on_vscrollbar_value_changed)
62
63         self.initialize_buffer()
64
65         self.on_uri_clicked = self._on_uri_clicked
66
67         self.button1_pressed = False
68         self.current_pressed_uri = None
69             
70     def _on_uri_clicked(self, uri):
71         print uri, "clicked!!!!"
72
73     def initialize_buffer(self):
74         self.pangolayout = []
75
76     def add_layout(self, pangolayout):
77         if (len(self.pangolayout) != 0):
78             last = self.pangolayout[len(self.pangolayout)-1]
79             x, y = last.get_pixel_size()
80             pangolayout.posY = last.posY + y
81         self.set_layout_width(pangolayout)
82         self.pangolayout.append(pangolayout)
83         x, y = pangolayout.get_pixel_size()
84         self.adjustment.upper = pangolayout.posY + y
85         self.prevent_adjustment_overflow()
86         self.redraw()
87         self.change_vscrollbar_visible()
88
89     def create_pango_layout(self, text):
90         return self.drawingarea.create_pango_layout(text)
91
92     def set_layout_width(self, layout):
93         width = self.drawingarea.allocation.width
94         layout.set_width((width - layout.marginleft) * pango.SCALE)
95         
96     def redraw(self):
97         self.drawingarea.queue_draw()
98
99     def relayout(self):
100         width = self.drawingarea.allocation.width
101         sum_height = 0
102         for layout in self.pangolayout:
103             layout.set_width((width - layout.marginleft) * pango.SCALE)
104             layout.posY = sum_height
105             x, y = layout.get_pixel_size()
106             sum_height += y
107         self.adjustment.upper = sum_height
108         self.prevent_adjustment_overflow()
109         self.change_vscrollbar_visible()
110
111     def change_vscrollbar_visible(self):
112         if self.adjustment.upper < self.adjustment.page_size:
113             self.vscrollbar.hide()
114         else:
115             self.vscrollbar.show()
116
117     def jump(self, value):
118         if value > self.adjustment.upper - self.adjustment.page_size:
119             value = self.adjustment.upper - self.adjustment.page_size
120         self.adjustment.set_value(value)
121
122     def jump_to_layout(self, layout):
123         self.jump(layout.posY)
124         
125     def jump_to_the_end(self):
126         value = self.adjustment.upper - self.adjustment.page_size
127         self.adjustment.set_value(value)
128
129     def jump_to_res(self, resnum):
130         for layout in self.pangolayout:
131             if layout.resnum == resnum:
132                 self.jump_to_layout(layout)
133                 return True
134         return False
135
136     def draw_viewport(self):
137         view_y = self.adjustment.get_value()
138         self.drawingarea.window.draw_rectangle(
139             self.drawingarea.style.base_gc[0],
140             True, 0, 0,
141             self.drawingarea.allocation.width,
142             self.drawingarea.allocation.height)
143
144         gc = self.drawingarea.window.new_gc()
145         for layout in self.pangolayout:
146             w, h = layout.get_pixel_size()
147             layout_top = layout.posY
148             layout_bottom = layout.posY + h
149             area_top = view_y
150             area_bottom = view_y + self.drawingarea.allocation.height
151             if layout_top <= area_bottom and layout_bottom >= area_top:
152                 self.drawingarea.window.draw_layout(
153                     gc, layout.marginleft, layout.posY - int(view_y), layout)
154
155     def transform_coordinate_gdk_to_adj(self, y):
156         return y + self.adjustment.value
157
158     def transform_coordinate_adj_to_layout(self, x, y, layout):
159         return x - layout.marginleft, y - layout.posY
160
161     def transform_coordinate_gdk_to_layout(self, x, y, layout):
162         return self.transform_coordinate_adj_to_layout(
163             x, self.transform_coordinate_gdk_to_adj(y), layout)
164
165     def ptrpos_to_layout(self, x, y):
166         # transform coordinate, GdkWindow -> adjustment
167         adj_x = x
168         adj_y = self.transform_coordinate_gdk_to_adj(y)
169         for lay in self.pangolayout:
170             width, height = lay.get_pixel_size()
171             if (adj_y >= lay.posY and adj_y < lay.posY + height and
172                 adj_x >= lay.marginleft):
173                 return lay
174         return None
175
176     def ptrpos_to_uri(self,  x, y):
177         # x, y is GdkWindow coordinate
178
179         layout = self.ptrpos_to_layout(x, y)
180
181         if layout is None:
182             return None, None, None
183
184         # transform coordinate, GdkWindow -> pangolayout
185         lay_x, lay_y = self.transform_coordinate_gdk_to_layout(x, y, layout)
186
187         # xy -> index
188         idx, clk = layout.xy_to_index(
189             int(lay_x)*pango.SCALE, int(lay_y)*pango.SCALE)
190
191         x, y, width, height = layout.index_to_pos(idx)
192         x /= pango.SCALE
193         y /= pango.SCALE
194         width /= pango.SCALE
195         height /= pango.SCALE
196         if (lay_x >= x and lay_x < x + width and
197             lay_y >= y and lay_y < y + height):
198
199             for i, (start, end, href) in enumerate(layout.urilist):
200                 if idx >= start and idx < end:
201                     return href, layout, i
202
203         return None, layout, None
204
205     def on_drawingarea_expose_event(self, widget, event, data=None):
206         self.draw_viewport()
207
208     def on_drawingarea_size_allocate(self, widget, allocation, data=None):
209         if allocation.width != self.drawingarea_prev_width:
210             self.relayout()
211             self.drawingarea_prev_width = allocation.width
212         self.adjustment.page_size = self.drawingarea.allocation.height
213         self.adjustment.page_increment = self.drawingarea.allocation.height
214
215         self.prevent_adjustment_overflow()
216         self.change_vscrollbar_visible()
217
218     def prevent_adjustment_overflow(self):
219         if (self.adjustment.value >
220             self.adjustment.upper - self.adjustment.page_size):
221             self.adjustment.value = (self.adjustment.upper -
222                                      self.adjustment.page_size)
223
224         if self.adjustment.value < 0:
225             self.adjustment.value = 0
226
227     def on_drawingarea_button_press_event(self, widget, event, data=None):
228         self.drawingarea.queue_draw()
229
230     def on_vscrollbar_value_changed(self, widget, data=None):
231         self.drawingarea.queue_draw()
232
233     def on_drawingarea_scroll_event(self, widget, event, data=None):
234         if event.direction == gtk.gdk.SCROLL_UP:
235             self.adjustment.value -= 66.476200804
236             if self.adjustment.value < self.adjustment.lower:
237                 self.adjustment.value = self.adjustment.lower
238         if event.direction == gtk.gdk.SCROLL_DOWN:
239             self.adjustment.value += 66.476200804
240             max_value = self.adjustment.upper - self.adjustment.page_size
241             if self.adjustment.value > max_value:
242                 self.adjustment.value = max_value
243         self.prevent_adjustment_overflow()
244
245     def on_drawingrarea_motion_notify_event(self, widget, event, data=None):
246         if event.state & gtk.gdk.BUTTON1_MASK != gtk.gdk.BUTTON1_MASK:
247             self.button1_pressed = False
248
249         cursor = ThreadView.regular_cursor
250
251         uri, layout, index = self.ptrpos_to_uri(event.x, event.y)
252         if layout is None:
253             cursor = ThreadView.arrow_cursor
254         else:
255             if uri is not None and uri != "":
256                 cursor = ThreadView.hand_cursor
257
258         self.drawingarea.window.set_cursor(cursor)
259
260     def on_drawingrarea_button_press_event(self, widget, event, data=None):
261         if event.button == 1:
262             self.current_pressed_uri = None
263             self.button1_pressed = True
264             uri, layout, index = self.ptrpos_to_uri(event.x, event.y)
265             if uri is not None and layout is not None and index is not None:
266                 self.current_pressed_uri = (uri, layout, index)
267
268     def on_drawingrarea_button_release_event(self, widget, event, data=None):
269         if event.button == 1:
270             button1_pressed = self.button1_pressed
271             self.button1_pressed = False
272
273             if button1_pressed and self.current_pressed_uri is not None:
274                 uri, layout, index = self.ptrpos_to_uri(event.x, event.y)
275                 p_uri, p_layout, p_index = self.current_pressed_uri
276                 self.current_preesed_uri = None
277                 if uri == p_uri and layout == p_layout and index == p_index:
278                     self.on_uri_clicked(uri)