def button_press_event(self, text_view, event): """callback for mouse click events""" # we only react on left or right mouse clicks if event.button != 1 and event.button != 3: return False # try to get a selection try: (start, end) = self.buffer.get_selection_bounds() except ValueError: pass else: if start.get_offset() != end.get_offset(): return False # get the iter at the mouse position (x, y) = self.window_to_buffer_coords(Gtk.TextWindowType.WIDGET, int(event.x), int(event.y)) iter = self.get_iter_at_location(x, y) # call open_url or menu.popup if an URL is assigned to the iter tags = iter.get_tags() for tag in tags: if hasattr(tag, "url"): if event.button == 1: open_url(tag.url) break if event.button == 3: self.create_context_menu(tag.url) self.menu.popup(None, None, None, None, event.button, event.time) return True
def handle_context_menu(self, menuitem, action, url): """Handle activate event for the links' context menu""" if action == "open": open_url(url) if action == "copy": # the following two lines used to be enough - then gtk3/pygi # came along ... #cb = Gtk.Clipboard() #cb.set_text(url) display = Gdk.Display.get_default() selection = Gdk.Atom.intern("CLIPBOARD", False) cb = Gtk.Clipboard.get_for_display(display, selection) cb.set_text(url, -1) cb.store()