Armed with a text editor

mu's views on program and recipe! design

#!/usr/bin/env python
#
#   Expand Four Tabs - Epiphany Extension
#   Copyright (C) 2006  Stefan Stuhr, Michael Urman
#
#   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.
#

import gtk
import epiphany

def hbox_style_set_cb(hbox, previous_style):
    hbox._expand4tabs_original_width = hbox.get_size_request()[0]
    hbox.set_size_request(16, -1)

def attach_tab(window, tab):
    notebook = window.get_notebook()

    # if there are fewer than 4 pages, leave normal, but otherwise
    # switch the new (and existing if necessary) to expand mode
    if notebook.get_n_pages() == 4:
        pages = map(notebook.get_nth_page, range(notebook.get_n_pages()))
    elif notebook.get_n_pages() > 4:
        pages = [tab]
    else:
        return

    for page in pages:
        hbox = notebook.get_tab_label(page)
        if not isinstance(hbox, gtk.HBox): continue

        hbox._expand4tabs_original_width = hbox.get_size_request()[0]
        hbox.set_size_request(16, -1)
        hbox._expand4tabs_hbox_style_set_sig = hbox.connect("style-set",
                hbox_style_set_cb)

        notebook.child_set(page, "tab-expand", True)

def detach_tab(window, tab):
    notebook = window.get_notebook()

    # always switch the detached one back, however if there are now
    # fewer than 4 pages, switch all back to non-expand mode
    if notebook.get_n_pages() == 3:
        pages = map(notebook.get_nth_page, range(notebook.get_n_pages()))
        if tab not in pages: pages.append(tab)
    else:
        pages = [tab]

    for page in pages:
        hbox = notebook.get_tab_label(page)

        if page.get_parent() == notebook:
            notebook.child_set(page, "tab-expand", False)

        if not isinstance(hbox, gtk.HBox): continue

        try:
            sig = hbox._expand4tabs_hbox_style_set_sig
        except AttributeError:
            pass
        else:
            hbox.disconnect(sig)
            del hbox._expand4tabs_hbox_style_set_sig

        try:
            width = hbox._expand4tabs_original_width
        except AttributeError:
            pass
        else:
            window.set_geometry_hints(hbox, -1)
            hbox.set_size_request(width, -1)
            del hbox._expand4tabs_original_width