OSDN Git Service

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