class NoteBookController(object):

    def __init__(self, notebook):
        self.notebook = notebook
        self.tab_buttons = self.getTabButtons()
        # Fix tab button states on initialization.
        self.fixTabButtonStates()

    def getTabButtons(self):
        tab_buttons = []
        # We use this idiom because pygtk2 doesn't have
        # GtkNotebook.get_n_pages() (it's a 2.2 API).
        i = 0
        while 1:
            page = self.notebook.get_nth_page(i)
            if page is None:
                break
            tab = self.notebook.get_tab_label(page)
            buttons = [x for x in tab.children() if isinstance(x, gtk.Button)]
            tab_buttons.extend(buttons)
            i += 1
        return tab_buttons
    
    def on_switch_page(self, *ignore):
        # Fix tab button states after every page switch.
        # We need the idle because the PRELIGHTing doesn't happen until
        # after the page has actually switched.
        gtk.idle_add(self.fixTabButtonStates)

    def fixTabButtonStates(self, *ignore):
        for button in self.tab_buttons:
            button.set_state(gtk.STATE_NORMAL)
