Refactor some code from Notebook into a standalone function that finds all descendant widgets of a given container that are Containers or Terminals and returns lists of them, and use this when closing a tab. This function will form the basis of layout enumeration

This commit is contained in:
Chris Jones 2010-01-28 13:41:44 +00:00
parent 0817d2651d
commit 789092bb07
4 changed files with 42 additions and 17 deletions

View File

@ -60,7 +60,7 @@ class Container(object):
self.cnxids.remove_widget(widget) self.cnxids.remove_widget(widget)
def get_offspring(self): def get_offspring(self):
"""Return a list of child widgets, if any""" """Return a list of direct child widgets, if any"""
return(self.children) return(self.children)
def split_horiz(self, widget): def split_horiz(self, widget):

View File

@ -12,7 +12,7 @@ from factory import Factory
from container import Container from container import Container
from editablelabel import EditableLabel from editablelabel import EditableLabel
from translation import _ from translation import _
from util import err, dbg, get_top_window from util import err, dbg, get_top_window, enumerate_descendants
class Notebook(Container, gtk.Notebook): class Notebook(Container, gtk.Notebook):
"""Class implementing a gtk.Notebook container""" """Class implementing a gtk.Notebook container"""
@ -179,21 +179,9 @@ class Notebook(Container, gtk.Notebook):
dialog.destroy() dialog.destroy()
if result == gtk.RESPONSE_ACCEPT: if result == gtk.RESPONSE_ACCEPT:
containers = [] containers = None
objects = [] objects = None
for descendant in child.get_children(): containers, objects = enumerate_descendants(child)
if maker.isinstance(descendant, 'Container'):
containers.append(descendant)
elif maker.isinstance(descendant, 'Terminal'):
objects.append(descendant)
while len(containers) > 0:
child = containers.pop()
for descendant in child.get_children():
if maker.isinstance(descendant, 'Container'):
containers.append(descendant)
elif maker.isinstance(descendant, 'Terminal'):
objects.append(descendant)
while len(objects) > 0: while len(objects) > 0:
descendant = objects.pop() descendant = objects.pop()

View File

@ -61,6 +61,7 @@ class Terminator(Borg):
self.windows.remove(window) self.windows.remove(window)
if len(self.windows) == 0: if len(self.windows) == 0:
# We have no windows left, we should exit # We have no windows left, we should exit
dbg('no windows remain, quitting')
gtk.main_quit() gtk.main_quit()
def register_terminal(self, terminal): def register_terminal(self, terminal):
@ -79,6 +80,7 @@ class Terminator(Borg):
self.terminals.remove(terminal) self.terminals.remove(terminal)
if len(self.terminals) == 0: if len(self.terminals) == 0:
dbg('no terminals remain, destroying all windows')
for window in self.windows: for window in self.windows:
window.destroy() window.destroy()
else: else:

View File

@ -229,3 +229,38 @@ def get_nav_tiebreak(direction, cursor_x, cursor_y, rect):
else: else:
raise ValueError('Unknown direction: %s' % direction) raise ValueError('Unknown direction: %s' % direction)
def enumerate_descendants(parent):
"""Walk all our children and build up a list of containers and
terminals"""
# FIXME: Does having to import this here mean we should move this function
# back to Container?
from factory import Factory
containerstmp = []
containers = []
terminals = []
maker = Factory()
if parent is None:
err('no parent widget specified')
return
for descendant in parent.get_children():
if maker.isinstance(descendant, 'Container'):
containerstmp.append(descendant)
elif maker.isinstance(descendant, 'Terminal'):
terminals.append(descendant)
while len(containerstmp) > 0:
child = containerstmp.pop()
for descendant in child.get_children():
if maker.isinstance(descendant, 'Container'):
containerstmp.append(descendant)
elif maker.isinstance(descendant, 'Terminal'):
terminals.append(descendant)
containers.append(child)
dbg('%d containers and %d terminals fall beneath %s' % (len(containers),
len(terminals), parent))
return(containers, terminals)