Skip to content

Instantly share code, notes, and snippets.

@codebrainz
Created March 22, 2011 01:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codebrainz/880598 to your computer and use it in GitHub Desktop.
Save codebrainz/880598 to your computer and use it in GitHub Desktop.
Test code to try and save and restore tabs inside two notebooks, including which notebook and order.
#!/usr/bin/env python
"""
Demo for storing tabs inside two detachable and re-orderable notebooks
to disk on close and reloading them in the correct notebook and in
order on load.
"""
import gtk
from ConfigParser import SafeConfigParser
DATAFILE = "notebook_session.cfg"
NOTEBOOKS_GROUP = "notebooks_group"
NOTEBOOKS_ID = 100 # could be based on group name
CFG_SPLITTER = "|"
class MainWindow(gtk.Window):
def save_tabs(self):
" Store tabs to a configuration file. "
for nb in [self.nb1, self.nb2]:
nb_name = nb.get_name()
if not self.config.has_section(NOTEBOOKS_GROUP):
self.config.add_section(NOTEBOOKS_GROUP)
for tab in nb.get_children():
self.config.set(NOTEBOOKS_GROUP, nb_name,
CFG_SPLITTER.join([
w.get_data("tab-name") for w in nb.get_children()
if w.get_data("tab-name")]))
self.config.write(open(DATAFILE,"w"))
def load_tabs(self):
" Read tabs from configuration file. "
self.config = SafeConfigParser()
self.config.read([DATAFILE])
if not self.config.has_section(NOTEBOOKS_GROUP):
return
nb1_tabs = self.config.get(NOTEBOOKS_GROUP,
self.nb1.get_name()).split(CFG_SPLITTER)
nb2_tabs = self.config.get(NOTEBOOKS_GROUP,
self.nb2.get_name()).split(CFG_SPLITTER)
for nb, nb_alt, nb_tabs in [ (self.nb1, self.nb2, nb1_tabs),
(self.nb2, self.nb1, nb2_tabs) ]:
for tab_name in nb_tabs:
for tab in nb_alt.get_children():
if tab_name == tab.get_data("tab-name"):
self.reparent_tab(tab, nb)
pos = 0
for tab_name in nb_tabs:
for tab in nb.get_children():
if tab_name == tab.get_data("tab-name"):
nb.reorder_child(tab, pos)
pos += 1
break
def __init__(self):
gtk.Window.__init__(self, gtk.WINDOW_TOPLEVEL)
self.connect("destroy", self.on_destroy)
self.set_default_size(640, 480)
self.nb1 = gtk.Notebook()
self.nb1.set_name("notebook1")
self.nb1.set_group_id(NOTEBOOKS_ID)
self.nb2 = gtk.Notebook()
self.nb2.set_name("notebook2")
self.nb2.set_group_id(NOTEBOOKS_ID)
# add some "registered" tabs
self.add_tabs(self.nb1, 0, 4)
self.add_tabs(self.nb2, 4, 8)
# add a couple "stray" tabs too
self.nb2.append_page(gtk.Label("Dummy Stray #1"), gtk.Label("Stray #1"))
stray = gtk.Label("Dummy Stray #2")
self.nb2.append_page(stray, gtk.Label("Stray #2"))
self.nb2.set_tab_reorderable(stray, True)
hpaned = gtk.HPaned()
hpaned.add1(self.nb1)
hpaned.add2(self.nb2)
self.add(hpaned)
self.load_tabs()
def add_tabs(self, nb, r1, r2):
" Create some dummy tabs to work with. "
for i in range(r1, r2):
content = gtk.Label("Dummy #%d" % i)
name = "tab_page_%d" % i
content.set_data("tab-name", name) # "register" the tab
label = gtk.Label("Tab #%d" % i)
nb.append_page(content, label)
nb.set_tab_reorderable(content, True)
nb.set_tab_detachable(content, True)
def reparent_tab(self, tab, new_nb):
" Move a tab to a new notebook. "
label = tab.get_parent().get_tab_label(tab)
tab.reparent(new_nb)
new_nb.set_tab_label(tab, label)
new_nb.set_tab_reorderable(tab, True)
new_nb.set_tab_detachable(tab, True)
def on_destroy(self, event):
" Save notebook pages and quit. "
self.save_tabs()
gtk.main_quit()
if __name__ == "__main__":
win = MainWindow()
win.show_all()
gtk.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment