1 # Copyright (C) 2007, 2009 by Aiwota Programmer
2 # aiwotaprog@tetteke.tk
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.
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.
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
22 from ThreadViewBase.drawable import Drawable
25 class ThreadView(Drawable):
28 "cursor-over-link-event":
29 (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (object, object, )),
30 "populate-popup": (gobject.SIGNAL_RUN_LAST,
33 "button-press-event": "override",
37 super(ThreadView, self).__init__()
38 self.set_property("can_focus", True)
41 gtk.gdk.KEY_PRESS_MASK |
43 gtk.gdk.POINTER_MOTION_MASK |
44 gtk.gdk.BUTTON_PRESS_MASK |
45 gtk.gdk.BUTTON_RELEASE_MASK)
47 def jump_to_layout(self, layout):
48 self.jump(layout.posY)
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)
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
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):
67 index = s_l.list_index
71 layout = self.res_layout_list[index]
73 text += layout.get_text(selection_start, selection_end)
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))
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)
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)
97 menuitem = gtk.MenuItem("Copy Link _Address")
98 menuitem.connect("activate", self.on_copy_uri_activated, uri)
101 menu.append(gtk.SeparatorMenuItem())
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)
109 menuitem = gtk.MenuItem("Open _Selected as URL")
110 menuitem.connect("activate",
111 self.on_open_as_uri_activated, selection)
112 menu.append(menuitem)
114 menu.append(gtk.SeparatorMenuItem())
116 self.emit("populate-popup", menu)
118 menu.popup(None, None, None, button, time)
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)
126 def on_copy_uri_activated(self, widget, uri):
127 self._copy_text_to_clipboard(uri)
129 def on_open_uri_activated(self, widget, uri):
130 self.emit("uri-clicked-event", uri)
132 def on_copy_selection_activated(self, widget, selection):
133 self._copy_text_to_clipboard(selection)
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)
141 class ThreadViewScrollbar(gtk.VScrollbar):
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)
148 def change_vscrollbar_visible(self):
149 adjustment = self.get_adjustment()
150 if adjustment.upper <= adjustment.page_size:
155 def on_adjustment_changed(self, widget, data=None):
156 self.change_vscrollbar_visible()
159 class ThreadViewContainer(gtk.HBox):
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)
170 child.connect("scroll-event", self.on_child_scroll_event)
172 def on_child_scroll_event(self, widget, event, data=None):
173 self.vscrollbar.emit("scroll-event", event)