#!/usr/bin/env python # # Middle Click Tab Close - Epiphany Extension # Copyright (C) 2006 Stefan Stuhr # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License along # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. import gtk import epiphany def attach_window(window): notebook = window.get_notebook() signal = notebook.connect("scroll-event", lambda x, y: True) window._mctc_notebook_scroll_sig = signal def detach_window(window): if hasattr(window, "_mctc_notebook_scroll_sig"): notebook = window.get_notebook() notebook.disconnect(window._mctc_notebook_scroll_sig) delattr(window, "_mctc_notebook_scroll_sig") def mctc_button_press_cb(widget, event): return (event.button == 2) def mctc_motion_notify_cb(widget, event): if event.is_hint: x, y, state = event.window.get_pointer() else: state = event.state return (state & gtk.gdk.BUTTON2_MASK) def mctc_button_release_cb(widget, event, window, tab): if event.button != 2: return width, height = widget.allocation.width, widget.allocation.height if not (event.x >= 0 and event.x < width and event.y >= 0 and event.y < height): return if tab != window.get_active_tab(): tab._other_extensions_i_didnt_have_focus = True window.remove_tab(tab) def attach_tab(window, tab): notebook = window.get_notebook() widget = notebook.get_tab_label(tab) signal = widget.connect("button-press-event", mctc_button_press_cb) tab._mctc_tab_button_press_sig = signal signal = widget.connect("motion-notify-event", mctc_motion_notify_cb) tab._mctc_tab_motion_notify_sig = signal signal = widget.connect("button-release-event", mctc_button_release_cb, window, tab) tab._mctc_tab_button_release_sig = signal def detach_tab(window, tab): notebook = window.get_notebook() widget = notebook.get_tab_label(tab) if hasattr(tab, "_other_extensions_i_didnt_have_focus"): delattr(tab, "_other_extensions_i_didnt_have_focus") if not widget: return if hasattr(tab, "_mctc_tab_button_press_sig"): widget.disconnect(tab._mctc_tab_button_press_sig) delattr(tab, "_mctc_tab_button_press_sig") if hasattr(tab, "_mctc_tab_motion_notify_sig"): widget.disconnect(tab._mctc_tab_motion_notify_sig) delattr(tab, "_mctc_tab_motion_notify_sig") if hasattr(tab, "_mctc_tab_button_release_sig"): widget.disconnect(tab._mctc_tab_button_release_sig) delattr(tab, "_mctc_tab_button_release_sig")