2009-11-22 03:53:38 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
# Terminator by Chris Jones <cmsj@tenshu.net>
|
|
|
|
# GPL v2 only
|
2009-11-22 04:28:39 +00:00
|
|
|
"""notebook.py - classes for the notebook widget"""
|
2009-11-22 03:53:38 +00:00
|
|
|
|
|
|
|
import gobject
|
|
|
|
import gtk
|
|
|
|
|
2010-01-11 20:06:53 +00:00
|
|
|
from terminator import Terminator
|
2009-11-23 15:17:33 +00:00
|
|
|
from config import Config
|
2009-11-25 00:37:29 +00:00
|
|
|
from factory import Factory
|
2009-11-23 15:17:33 +00:00
|
|
|
from container import Container
|
|
|
|
from editablelabel import EditableLabel
|
|
|
|
from translation import _
|
2010-01-28 13:41:44 +00:00
|
|
|
from util import err, dbg, get_top_window, enumerate_descendants
|
2009-11-22 03:53:38 +00:00
|
|
|
|
2009-11-22 04:28:39 +00:00
|
|
|
class Notebook(Container, gtk.Notebook):
|
|
|
|
"""Class implementing a gtk.Notebook container"""
|
2009-11-23 15:17:33 +00:00
|
|
|
window = None
|
2009-11-22 03:53:38 +00:00
|
|
|
|
2009-11-22 04:28:39 +00:00
|
|
|
def __init__(self, window):
|
2009-11-22 03:53:38 +00:00
|
|
|
"""Class initialiser"""
|
2009-11-22 04:28:39 +00:00
|
|
|
if isinstance(window.get_child(), gtk.Notebook):
|
|
|
|
err('There is already a Notebook at the top of this window')
|
|
|
|
raise(ValueError)
|
|
|
|
|
|
|
|
Container.__init__(self)
|
|
|
|
gtk.Notebook.__init__(self)
|
|
|
|
self.terminator = Terminator()
|
2009-11-23 15:17:33 +00:00
|
|
|
self.window = window
|
2009-11-22 04:28:39 +00:00
|
|
|
gobject.type_register(Notebook)
|
|
|
|
self.register_signals(Notebook)
|
|
|
|
self.configure()
|
|
|
|
|
|
|
|
child = window.get_child()
|
|
|
|
window.remove(child)
|
|
|
|
window.add(self)
|
|
|
|
self.newtab(child)
|
2009-11-23 15:17:33 +00:00
|
|
|
|
2009-11-22 04:28:39 +00:00
|
|
|
self.show_all()
|
|
|
|
|
|
|
|
def configure(self):
|
|
|
|
"""Apply widget-wide settings"""
|
2010-01-21 12:55:57 +00:00
|
|
|
# FIXME: The old reordered handler updated Terminator.terminals with
|
|
|
|
# the new order of terminals. We probably need to preserve this for
|
|
|
|
# navigation to next/prev terminals.
|
2009-11-22 04:28:39 +00:00
|
|
|
#self.connect('page-reordered', self.on_page_reordered)
|
|
|
|
self.set_property('homogeneous', not self.config['scroll_tabbar'])
|
|
|
|
self.set_scrollable(self.config['scroll_tabbar'])
|
|
|
|
|
|
|
|
pos = getattr(gtk, 'POS_%s' % self.config['tab_position'].upper())
|
|
|
|
self.set_tab_pos(pos)
|
|
|
|
self.set_show_tabs(not self.config['hide_tabbar'])
|
2009-11-22 03:53:38 +00:00
|
|
|
|
2010-02-27 13:04:15 +00:00
|
|
|
def create_layout(self, layout):
|
|
|
|
"""Apply layout configuration"""
|
|
|
|
if not layout.has_key('children'):
|
|
|
|
err('layout specifies no children: %s' % layout)
|
|
|
|
return
|
|
|
|
|
|
|
|
children = layout['children']
|
|
|
|
if len(children) <= 1:
|
|
|
|
#Notebooks should have two or more children
|
|
|
|
err('incorrect number of children for Notebook: %s' % layout)
|
|
|
|
return
|
|
|
|
|
|
|
|
pages = 2 # Notebooks always start with two pages
|
|
|
|
num = 0
|
|
|
|
keys = children.keys()
|
|
|
|
keys.sort()
|
|
|
|
|
|
|
|
for child_key in keys:
|
|
|
|
child = children[child_key]
|
|
|
|
if child['type'] == 'Terminal':
|
|
|
|
continue
|
|
|
|
elif child['type'] == 'VPaned':
|
|
|
|
page = self.get_nth_page(num)
|
|
|
|
self.split_axis(page, True)
|
|
|
|
elif child['type'] == 'HPaned':
|
|
|
|
page = self.get_nth_page(num)
|
|
|
|
self.split_axis(page, False)
|
|
|
|
num = num + 1
|
|
|
|
|
|
|
|
num = 0
|
|
|
|
for child_key in keys:
|
|
|
|
page = self.get_nth_page(num)
|
|
|
|
page.create_layout(children[child_key])
|
|
|
|
num = num + 1
|
|
|
|
|
2010-03-02 12:39:47 +00:00
|
|
|
def split_axis(self, widget, vertical=True, sibling=None, widgetlast=False):
|
2009-12-08 13:57:29 +00:00
|
|
|
"""Split the axis of a terminal inside us"""
|
2010-02-27 12:30:38 +00:00
|
|
|
order = None
|
2009-11-25 00:37:29 +00:00
|
|
|
page_num = self.page_num(widget)
|
|
|
|
if page_num == -1:
|
|
|
|
err('Notebook::split_axis: %s not found in Notebook' % widget)
|
|
|
|
return
|
|
|
|
|
2009-12-08 13:57:29 +00:00
|
|
|
label = self.get_tab_label(widget)
|
|
|
|
self.remove(widget)
|
2009-11-25 00:37:29 +00:00
|
|
|
|
|
|
|
maker = Factory()
|
|
|
|
if vertical:
|
|
|
|
container = maker.make('vpaned')
|
|
|
|
else:
|
|
|
|
container = maker.make('hpaned')
|
|
|
|
|
|
|
|
if not sibling:
|
|
|
|
sibling = maker.make('terminal')
|
2010-02-04 00:59:11 +00:00
|
|
|
sibling.spawn_child()
|
2009-11-25 00:37:29 +00:00
|
|
|
|
|
|
|
self.insert_page(container, None, page_num)
|
2009-12-08 13:57:29 +00:00
|
|
|
self.set_tab_label(container, label)
|
2009-11-25 00:37:29 +00:00
|
|
|
self.show_all()
|
|
|
|
|
2010-03-02 12:39:47 +00:00
|
|
|
order = [sibling, widget]
|
|
|
|
if widgetlast is True:
|
|
|
|
order.reverse()
|
2010-02-27 12:30:38 +00:00
|
|
|
|
|
|
|
for terminal in order:
|
|
|
|
container.add(terminal)
|
2009-11-25 00:37:29 +00:00
|
|
|
self.set_current_page(page_num)
|
|
|
|
|
|
|
|
self.show_all()
|
2009-11-22 03:53:38 +00:00
|
|
|
|
|
|
|
def add(self, widget):
|
|
|
|
"""Add a widget to the container"""
|
2009-12-08 13:57:29 +00:00
|
|
|
self.newtab(widget)
|
2009-11-22 03:53:38 +00:00
|
|
|
|
|
|
|
def remove(self, widget):
|
|
|
|
"""Remove a widget from the container"""
|
2009-11-25 00:37:29 +00:00
|
|
|
page_num = self.page_num(widget)
|
|
|
|
if page_num == -1:
|
2010-01-22 19:03:58 +00:00
|
|
|
err('%s not found in Notebook. Actual parent is: %s' %
|
2009-12-08 13:57:29 +00:00
|
|
|
(widget, widget.get_parent()))
|
2009-11-25 00:37:29 +00:00
|
|
|
return(False)
|
|
|
|
self.remove_page(page_num)
|
2009-12-08 13:57:29 +00:00
|
|
|
self.disconnect_child(widget)
|
2009-11-25 12:51:14 +00:00
|
|
|
return(True)
|
2009-11-22 03:53:38 +00:00
|
|
|
|
2009-11-22 04:28:39 +00:00
|
|
|
def newtab(self, widget=None):
|
|
|
|
"""Add a new tab, optionally supplying a child widget"""
|
2010-01-24 12:55:03 +00:00
|
|
|
maker = Factory()
|
2010-01-21 12:33:42 +00:00
|
|
|
top_window = get_top_window(self)
|
|
|
|
|
2009-11-22 04:28:39 +00:00
|
|
|
if not widget:
|
2010-01-04 13:11:16 +00:00
|
|
|
widget = maker.make('Terminal')
|
2009-11-22 04:28:39 +00:00
|
|
|
widget.spawn_child()
|
|
|
|
|
2009-11-25 12:51:14 +00:00
|
|
|
signals = {'close-term': self.wrapcloseterm,
|
2009-11-25 00:37:29 +00:00
|
|
|
'split-horiz': self.split_horiz,
|
|
|
|
'split-vert': self.split_vert,
|
2009-12-10 13:20:03 +00:00
|
|
|
'title-change': self.propagate_title_change,
|
2010-01-24 12:55:03 +00:00
|
|
|
'unzoom': self.unzoom,
|
|
|
|
'tab-change': top_window.tab_change,
|
|
|
|
'group-all': top_window.group_all,
|
|
|
|
'ungroup-all': top_window.ungroup_all,
|
|
|
|
'group-tab': top_window.group_tab,
|
2010-01-24 22:15:54 +00:00
|
|
|
'ungroup-tab': top_window.ungroup_tab,
|
2010-01-28 12:49:38 +00:00
|
|
|
'move-tab': top_window.move_tab,
|
2010-01-29 13:12:33 +00:00
|
|
|
'tab-new': top_window.tab_new,
|
|
|
|
'navigate': top_window.navigate_terminal}
|
2009-11-25 00:37:29 +00:00
|
|
|
|
2009-11-25 09:07:48 +00:00
|
|
|
if maker.isinstance(widget, 'Terminal'):
|
|
|
|
for signal in signals:
|
|
|
|
self.connect_child(widget, signal, signals[signal])
|
2009-11-25 00:37:29 +00:00
|
|
|
|
2009-11-22 04:28:39 +00:00
|
|
|
self.set_tab_reorderable(widget, True)
|
2009-11-23 15:17:33 +00:00
|
|
|
label = TabLabel(self.window.get_title(), self)
|
2009-12-08 09:10:39 +00:00
|
|
|
label.connect('close-clicked', self.closetab)
|
2009-11-23 15:17:33 +00:00
|
|
|
|
|
|
|
label.show_all()
|
|
|
|
widget.show_all()
|
|
|
|
|
2009-12-07 20:32:47 +00:00
|
|
|
self.append_page(widget, None)
|
2009-11-23 15:17:33 +00:00
|
|
|
self.set_tab_label(widget, label)
|
|
|
|
self.set_tab_label_packing(widget, not self.config['scroll_tabbar'],
|
|
|
|
not self.config['scroll_tabbar'],
|
|
|
|
gtk.PACK_START)
|
|
|
|
|
2009-11-25 00:37:29 +00:00
|
|
|
self.set_current_page(-1)
|
2009-11-22 04:28:39 +00:00
|
|
|
widget.grab_focus()
|
2009-11-25 12:51:14 +00:00
|
|
|
|
|
|
|
def wrapcloseterm(self, widget):
|
|
|
|
"""A child terminal has closed"""
|
2009-12-09 13:02:13 +00:00
|
|
|
dbg('Notebook::wrapcloseterm: called on %s' % widget)
|
2009-11-25 12:51:14 +00:00
|
|
|
if self.closeterm(widget):
|
2009-12-08 13:57:29 +00:00
|
|
|
dbg('Notebook::wrapcloseterm: closeterm succeeded')
|
2010-01-19 22:44:05 +00:00
|
|
|
self.hoover()
|
2009-12-08 13:57:29 +00:00
|
|
|
else:
|
|
|
|
dbg('Notebook::wrapcloseterm: closeterm failed')
|
2009-11-25 12:51:14 +00:00
|
|
|
|
2009-12-08 09:10:39 +00:00
|
|
|
def closetab(self, widget, label):
|
|
|
|
"""Close a tab"""
|
|
|
|
tabnum = None
|
|
|
|
try:
|
|
|
|
nb = widget.notebook
|
|
|
|
except AttributeError:
|
|
|
|
err('TabLabel::closetab: called on non-Notebook: %s' % widget)
|
|
|
|
return
|
|
|
|
|
|
|
|
for i in xrange(0, nb.get_n_pages()):
|
|
|
|
if label == nb.get_tab_label(nb.get_nth_page(i)):
|
|
|
|
tabnum = i
|
|
|
|
break
|
|
|
|
|
|
|
|
if not tabnum:
|
2009-12-09 13:02:13 +00:00
|
|
|
err('TabLabel::closetab: %s not in %s. Bailing.' % (label, nb))
|
2009-12-08 09:10:39 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
maker = Factory()
|
|
|
|
child = nb.get_nth_page(tabnum)
|
|
|
|
|
|
|
|
if maker.isinstance(child, 'Terminal'):
|
2009-12-08 13:57:29 +00:00
|
|
|
dbg('Notebook::closetab: child is a single Terminal')
|
2009-12-08 09:10:39 +00:00
|
|
|
child.close()
|
|
|
|
elif maker.isinstance(child, 'Container'):
|
2009-12-08 13:57:29 +00:00
|
|
|
dbg('Notebook::closetab: child is a Container')
|
|
|
|
dialog = self.construct_confirm_close(self.window, _('tab'))
|
2009-12-08 13:01:13 +00:00
|
|
|
result = dialog.run()
|
|
|
|
dialog.destroy()
|
|
|
|
|
2009-12-08 13:57:29 +00:00
|
|
|
if result == gtk.RESPONSE_ACCEPT:
|
2010-01-28 13:41:44 +00:00
|
|
|
containers = None
|
|
|
|
objects = None
|
|
|
|
containers, objects = enumerate_descendants(child)
|
2009-12-08 13:57:29 +00:00
|
|
|
|
2009-12-09 13:02:13 +00:00
|
|
|
while len(objects) > 0:
|
|
|
|
descendant = objects.pop()
|
2009-12-08 13:57:29 +00:00
|
|
|
descendant.close()
|
|
|
|
while gtk.events_pending():
|
|
|
|
gtk.main_iteration()
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
dbg('Notebook::closetab: user cancelled request')
|
|
|
|
return
|
2009-12-08 09:10:39 +00:00
|
|
|
else:
|
2009-12-08 13:57:29 +00:00
|
|
|
err('Notebook::closetab: child is unknown type %s' % child)
|
|
|
|
return
|
2009-12-08 09:10:39 +00:00
|
|
|
|
|
|
|
nb.remove_page(tabnum)
|
|
|
|
del(label)
|
|
|
|
|
2009-11-22 03:53:38 +00:00
|
|
|
def resizeterm(self, widget, keyname):
|
|
|
|
"""Handle a keyboard event requesting a terminal resize"""
|
|
|
|
raise NotImplementedError('resizeterm')
|
|
|
|
|
|
|
|
def zoom(self, widget, fontscale = False):
|
|
|
|
"""Zoom a terminal"""
|
|
|
|
raise NotImplementedError('zoom')
|
|
|
|
|
|
|
|
def unzoom(self, widget):
|
|
|
|
"""Unzoom a terminal"""
|
|
|
|
raise NotImplementedError('unzoom')
|
|
|
|
|
2009-12-10 23:25:52 +00:00
|
|
|
def find_tab_root(self, widget):
|
|
|
|
"""Look for the tab child which is or ultimately contains the supplied
|
|
|
|
widget"""
|
|
|
|
parent = widget.get_parent()
|
|
|
|
previous = parent
|
|
|
|
|
|
|
|
while parent is not None and parent is not self:
|
|
|
|
previous = parent
|
|
|
|
parent = parent.get_parent()
|
|
|
|
|
|
|
|
if previous == self:
|
|
|
|
return(widget)
|
|
|
|
else:
|
|
|
|
return(previous)
|
|
|
|
|
2009-12-10 13:20:03 +00:00
|
|
|
def update_tab_label_text(self, widget, text):
|
|
|
|
"""Update the text of a tab label"""
|
2009-12-10 23:25:52 +00:00
|
|
|
notebook = self.find_tab_root(widget)
|
|
|
|
label = self.get_tab_label(notebook)
|
2009-12-10 13:20:03 +00:00
|
|
|
if not label:
|
|
|
|
err('Notebook::update_tab_label_text: %s not found' % widget)
|
|
|
|
return
|
|
|
|
|
|
|
|
label.set_label(text)
|
|
|
|
|
2010-01-19 22:44:05 +00:00
|
|
|
def hoover(self):
|
|
|
|
"""Clean up any empty tabs and if we only have one tab left, die"""
|
|
|
|
numpages = self.get_n_pages()
|
|
|
|
while numpages > 0:
|
|
|
|
numpages = numpages - 1
|
|
|
|
page = self.get_nth_page(numpages)
|
|
|
|
if not page:
|
|
|
|
dbg('Removing empty page: %d' % numpages)
|
|
|
|
self.remove_page(numpages)
|
|
|
|
|
|
|
|
if self.get_n_pages() == 1:
|
|
|
|
dbg('Last page, removing self')
|
|
|
|
child = self.get_nth_page(0)
|
|
|
|
self.remove_page(0)
|
|
|
|
parent = self.get_parent()
|
|
|
|
parent.remove(self)
|
2010-01-25 12:55:38 +00:00
|
|
|
self.cnxids.remove_all()
|
2010-01-19 22:44:05 +00:00
|
|
|
parent.add(child)
|
|
|
|
del(self)
|
|
|
|
|
2009-11-23 15:17:33 +00:00
|
|
|
class TabLabel(gtk.HBox):
|
|
|
|
"""Class implementing a label widget for Notebook tabs"""
|
|
|
|
notebook = None
|
|
|
|
terminator = None
|
|
|
|
config = None
|
|
|
|
label = None
|
|
|
|
icon = None
|
|
|
|
button = None
|
|
|
|
|
2009-12-08 09:10:39 +00:00
|
|
|
__gsignals__ = {
|
|
|
|
'close-clicked': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE,
|
|
|
|
(gobject.TYPE_OBJECT,)),
|
|
|
|
}
|
|
|
|
|
2009-11-23 15:17:33 +00:00
|
|
|
def __init__(self, title, notebook):
|
|
|
|
"""Class initialiser"""
|
|
|
|
gtk.HBox.__init__(self)
|
2009-12-08 09:10:39 +00:00
|
|
|
self.__gobject_init__()
|
|
|
|
|
2009-11-23 15:17:33 +00:00
|
|
|
self.notebook = notebook
|
|
|
|
self.terminator = Terminator()
|
|
|
|
self.config = Config()
|
|
|
|
|
|
|
|
self.label = EditableLabel(title)
|
|
|
|
self.update_angle()
|
|
|
|
|
|
|
|
self.pack_start(self.label, True, True)
|
|
|
|
|
|
|
|
self.update_button()
|
|
|
|
self.show_all()
|
|
|
|
|
2009-12-10 13:20:03 +00:00
|
|
|
def set_label(self, text):
|
|
|
|
"""Update the text of our label"""
|
|
|
|
self.label.set_text(text)
|
|
|
|
|
2009-11-23 15:17:33 +00:00
|
|
|
def update_button(self):
|
|
|
|
"""Update the state of our close button"""
|
|
|
|
if not self.config['close_button_on_tab']:
|
|
|
|
if self.button:
|
|
|
|
self.button.remove(self.icon)
|
|
|
|
self.remove(self.button)
|
|
|
|
del(self.button)
|
|
|
|
del(self.icon)
|
|
|
|
self.button = None
|
|
|
|
self.icon = None
|
|
|
|
return
|
|
|
|
|
|
|
|
if not self.button:
|
|
|
|
self.button = gtk.Button()
|
|
|
|
if not self.icon:
|
|
|
|
self.icon = gtk.Image()
|
|
|
|
self.icon.set_from_stock(gtk.STOCK_CLOSE,
|
|
|
|
gtk.ICON_SIZE_MENU)
|
|
|
|
|
|
|
|
self.button.set_focus_on_click(False)
|
|
|
|
self.button.set_relief(gtk.RELIEF_NONE)
|
|
|
|
self.button.add(self.icon)
|
|
|
|
self.button.connect('clicked', self.on_close)
|
|
|
|
self.button.set_name('terminator-tab-close-button')
|
|
|
|
self.button.connect('style-set', self.on_style_set)
|
|
|
|
if hasattr(self.button, 'set_tooltip_text'):
|
|
|
|
self.button.set_tooltip_text(_('Close Tab'))
|
|
|
|
self.pack_start(self.button, False, False)
|
|
|
|
self.show_all()
|
|
|
|
|
|
|
|
def update_angle(self):
|
|
|
|
"""Update the angle of a label"""
|
|
|
|
position = self.notebook.get_tab_pos()
|
|
|
|
if position == gtk.POS_LEFT:
|
|
|
|
self.label.set_angle(90)
|
|
|
|
elif position == gtk.POS_RIGHT:
|
|
|
|
self.label.set_angle(270)
|
|
|
|
else:
|
|
|
|
self.label.set_angle(0)
|
|
|
|
|
|
|
|
def on_style_set(self, widget, prevstyle):
|
|
|
|
"""Style changed, recalculate icon size"""
|
|
|
|
x, y = gtk.icon_size_lookup_for_settings(self.button.get_settings(),
|
|
|
|
gtk.ICON_SIZE_MENU)
|
|
|
|
self.button.set_size_request(x + 2, y + 2)
|
|
|
|
|
2010-01-22 19:03:58 +00:00
|
|
|
def on_close(self, _widget):
|
2009-11-23 15:17:33 +00:00
|
|
|
"""The close button has been clicked. Destroy the tab"""
|
2009-12-08 09:10:39 +00:00
|
|
|
self.emit('close-clicked', self)
|
|
|
|
|
2009-11-22 03:53:38 +00:00
|
|
|
# vim: set expandtab ts=4 sw=4:
|