OSDN Git Service

The module "thread_view" is divided to the package "ThreadViewBase". (#16539)
[fukui-no-namari/fukui-no-namari.git] / src / FukuiNoNamari / thread_view.py
1 # Copyright (C) 2007, 2009 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 gobject
22 from ThreadViewBase.drawable import Drawable
23
24
25 class ThreadView(Drawable):
26
27     __gsignals__ = {
28         "cursor-over-link-event":
29         (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (object, object, )),
30         "populate-popup": (gobject.SIGNAL_RUN_LAST,
31                            gobject.TYPE_NONE,
32                            (gtk.Menu, )),
33         "button-press-event": "override",
34           }
35
36     def __init__(self):
37         super(ThreadView, self).__init__()
38         self.set_property("can_focus", True)
39
40         self.add_events(
41             gtk.gdk.KEY_PRESS_MASK |
42             gtk.gdk.SCROLL_MASK |
43             gtk.gdk.POINTER_MOTION_MASK |
44             gtk.gdk.BUTTON_PRESS_MASK |
45             gtk.gdk.BUTTON_RELEASE_MASK)
46
47     def jump_to_layout(self, layout):
48         self.jump(layout.posY)
49
50     def jump_to_res(self, resnum):
51         for layout in self.res_layout_list:
52             if layout.resnum == resnum:
53                 self.jump_to_layout(layout)
54                 return True
55         return False
56
57     def get_selected_text(self):
58         selection_start, selection_end = self._get_selection_start_end()
59         s_l, s_e, s_i = selection_start
60         e_l, e_e, e_i = selection_end
61
62         if (s_l is None or s_e is None or s_i is None or
63             e_l is None or e_e is None or e_i is None):
64             return ""
65
66         text = ""
67         index = s_l.list_index
68         end = e_l.list_index
69
70         while index <= end:
71             layout = self.res_layout_list[index]
72
73             text += layout.get_text(selection_start, selection_end)
74             if index != end:
75                 text += "\n"
76
77             index += 1
78
79         return text
80
81     def _copy_text_to_clipboard(self, text):
82         if text and len(text) > 0:
83             clip = gtk.Clipboard()
84             text = text.encode("utf8")
85             clip.set_text(text, len(text))
86
87     def _do_popup(self, x, y, button, time):
88         selection = self.get_selected_text()
89         uri, layout, element = self.ptrpos_to_uri(x, y)
90
91         menu = gtk.Menu()
92         if uri is not None and len(uri) > 0:
93             menuitem = gtk.MenuItem("Open _Link")
94             menuitem.connect("activate", self.on_open_uri_activated, uri)
95             menu.append(menuitem)
96
97             menuitem = gtk.MenuItem("Copy Link _Address")
98             menuitem.connect("activate", self.on_copy_uri_activated, uri)
99             menu.append(menuitem)
100
101             menu.append(gtk.SeparatorMenuItem())
102
103         if selection is not None and len(selection) > 0:
104             menuitem = gtk.ImageMenuItem(gtk.STOCK_COPY)
105             menuitem.connect("activate",
106                              self.on_copy_selection_activated, selection)
107             menu.append(menuitem)
108
109             menuitem = gtk.MenuItem("Open _Selected as URL")
110             menuitem.connect("activate",
111                              self.on_open_as_uri_activated, selection)
112             menu.append(menuitem)
113
114             menu.append(gtk.SeparatorMenuItem())
115
116         self.emit("populate-popup", menu)
117         menu.show_all()
118         menu.popup(None, None, None, button, time)
119
120     def do_button_press_event(self, event):
121         Drawable.do_button_press_event(self, event)
122         if event.button == 3:
123             self._do_popup(event.x, event.y, event.button, event.time)
124             return True
125
126     def on_copy_uri_activated(self, widget, uri):
127         self._copy_text_to_clipboard(uri)
128
129     def on_open_uri_activated(self, widget, uri):
130         self.emit("uri-clicked-event", uri)
131
132     def on_copy_selection_activated(self, widget, selection):
133         self._copy_text_to_clipboard(selection)
134
135     def on_open_as_uri_activated(self, widget, selection):
136         if not selection.startswith("http://"):
137             selection = "http://" + selection
138         self.emit("uri-clicked-event", selection)
139
140
141 class ThreadViewScrollbar(gtk.VScrollbar):
142
143     def __init__(self, adjustment=None):
144         super(ThreadViewScrollbar, self).__init__(adjustment)
145         self.set_property("no-show-all", True)
146         adjustment.connect("changed", self.on_adjustment_changed)
147
148     def change_vscrollbar_visible(self):
149         adjustment = self.get_adjustment()
150         if adjustment.upper <= adjustment.page_size:
151             self.hide()
152         else:
153             self.show()
154
155     def on_adjustment_changed(self, widget, data=None):
156         self.change_vscrollbar_visible()
157
158
159 class ThreadViewContainer(gtk.HBox):
160
161     def __init__(self, child):
162         super(ThreadViewContainer, self).__init__(False, 2)
163         adjustment = gtk.Adjustment()
164         self.drawingarea = child
165         child.set_scroll_adjustments(None, adjustment)
166         self.vscrollbar = ThreadViewScrollbar(adjustment)
167         self.pack_start(child)
168         self.pack_start(self.vscrollbar, expand=False)
169
170         child.connect("scroll-event", self.on_child_scroll_event)
171
172     def on_child_scroll_event(self, widget, event, data=None):
173         self.vscrollbar.emit("scroll-event", event)