2009-11-07 01:40:43 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
# Terminator by Chris Jones <cmsj@tenshu.net>
|
|
|
|
# GPL v2 only
|
|
|
|
"""paned.py - a base Paned container class and the vertical/horizontal
|
|
|
|
variants"""
|
|
|
|
|
|
|
|
import gobject
|
|
|
|
import gtk
|
|
|
|
|
2009-11-20 22:53:06 +00:00
|
|
|
from util import dbg, err, get_top_window
|
2010-01-11 20:06:53 +00:00
|
|
|
from terminator import Terminator
|
2009-11-25 00:37:29 +00:00
|
|
|
from factory import Factory
|
2009-11-07 01:40:43 +00:00
|
|
|
from container import Container
|
|
|
|
|
|
|
|
# pylint: disable-msg=R0921
|
2009-11-14 17:30:03 +00:00
|
|
|
# pylint: disable-msg=E1101
|
2009-11-07 01:40:43 +00:00
|
|
|
class Paned(Container):
|
|
|
|
"""Base class for Paned Containers"""
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
"""Class initialiser"""
|
|
|
|
self.terminator = Terminator()
|
2009-11-14 22:58:22 +00:00
|
|
|
Container.__init__(self)
|
2009-11-08 23:06:26 +00:00
|
|
|
self.signals.append({'name': 'resize-term',
|
|
|
|
'flags': gobject.SIGNAL_RUN_LAST,
|
|
|
|
'return_type': gobject.TYPE_NONE,
|
|
|
|
'param_types': (gobject.TYPE_STRING,)})
|
2009-11-07 01:40:43 +00:00
|
|
|
|
|
|
|
|
2009-11-14 17:30:03 +00:00
|
|
|
# pylint: disable-msg=W0613
|
2009-11-08 01:06:31 +00:00
|
|
|
def set_initial_position(self, widget, event):
|
|
|
|
"""Set the initial position of the widget"""
|
|
|
|
if isinstance(self, gtk.VPaned):
|
|
|
|
position = self.allocation.height / 2
|
|
|
|
else:
|
|
|
|
position = self.allocation.width / 2
|
|
|
|
|
2009-11-14 18:55:07 +00:00
|
|
|
dbg("Paned::set_initial_position: Setting position to: %d" % position)
|
2009-11-08 01:06:31 +00:00
|
|
|
self.set_position(position)
|
2010-01-18 13:17:35 +00:00
|
|
|
self.cnxids.remove_signal(self, 'expose-event')
|
2009-11-08 01:06:31 +00:00
|
|
|
|
2009-11-14 17:30:03 +00:00
|
|
|
# pylint: disable-msg=W0613
|
2009-11-17 04:56:55 +00:00
|
|
|
def split_axis(self, widget, vertical=True, sibling=None):
|
2009-11-07 01:40:43 +00:00
|
|
|
"""Default axis splitter. This should be implemented by subclasses"""
|
2009-11-25 00:37:29 +00:00
|
|
|
maker = Factory()
|
|
|
|
|
2009-11-08 01:06:31 +00:00
|
|
|
self.remove(widget)
|
|
|
|
if vertical:
|
|
|
|
container = VPaned()
|
|
|
|
else:
|
|
|
|
container = HPaned()
|
|
|
|
|
2009-11-17 04:56:55 +00:00
|
|
|
if not sibling:
|
2009-11-25 00:37:29 +00:00
|
|
|
sibling = maker.make('terminal')
|
2009-11-08 01:06:31 +00:00
|
|
|
sibling.spawn_child()
|
|
|
|
|
2009-11-14 18:55:07 +00:00
|
|
|
self.add(container)
|
|
|
|
self.show_all()
|
|
|
|
|
2009-11-08 01:06:31 +00:00
|
|
|
container.add(widget)
|
|
|
|
container.add(sibling)
|
|
|
|
|
|
|
|
self.show_all()
|
2009-11-07 01:40:43 +00:00
|
|
|
|
|
|
|
def add(self, widget):
|
|
|
|
"""Add a widget to the container"""
|
2009-11-25 00:37:29 +00:00
|
|
|
maker = Factory()
|
2009-11-07 01:40:43 +00:00
|
|
|
if len(self.children) == 0:
|
|
|
|
self.pack1(widget, True, True)
|
|
|
|
self.children.append(widget)
|
|
|
|
elif len(self.children) == 1:
|
2009-11-08 01:06:31 +00:00
|
|
|
if self.get_child1():
|
|
|
|
self.pack2(widget, True, True)
|
|
|
|
else:
|
|
|
|
self.pack1(widget, True, True)
|
2009-11-07 01:40:43 +00:00
|
|
|
self.children.append(widget)
|
|
|
|
else:
|
2009-11-14 22:58:22 +00:00
|
|
|
raise ValueError('Paned widgets can only have two children')
|
2009-11-07 01:40:43 +00:00
|
|
|
|
2009-11-25 00:37:29 +00:00
|
|
|
if maker.isinstance(widget, 'Terminal'):
|
2009-11-20 22:53:06 +00:00
|
|
|
top_window = get_top_window(self)
|
2009-11-14 22:58:22 +00:00
|
|
|
|
|
|
|
signals = {'close-term': self.wrapcloseterm,
|
|
|
|
'split-horiz': self.split_horiz,
|
|
|
|
'split-vert': self.split_vert,
|
2009-12-10 13:20:03 +00:00
|
|
|
'title-change': self.propagate_title_change,
|
2009-11-14 22:58:22 +00:00
|
|
|
'resize-term': self.resizeterm,
|
2009-11-20 05:16:20 +00:00
|
|
|
'zoom': top_window.zoom}
|
2009-11-14 22:58:22 +00:00
|
|
|
|
|
|
|
for signal in signals:
|
|
|
|
self.connect_child(widget, signal, signals[signal])
|
|
|
|
|
2010-01-18 13:17:35 +00:00
|
|
|
self.connect_child(widget, 'maximise', top_window.zoom, False)
|
2009-11-14 22:58:22 +00:00
|
|
|
|
2009-11-14 23:04:15 +00:00
|
|
|
widget.grab_focus()
|
|
|
|
|
2009-11-08 23:06:26 +00:00
|
|
|
elif isinstance(widget, gtk.Paned):
|
2009-11-14 18:55:07 +00:00
|
|
|
try:
|
2009-11-14 22:58:22 +00:00
|
|
|
self.connect_child(widget, 'resize-term', self.resizeterm)
|
2009-11-14 18:55:07 +00:00
|
|
|
except TypeError:
|
|
|
|
err('Paned::add: %s has no signal resize-term' % widget)
|
2009-11-07 01:40:43 +00:00
|
|
|
|
|
|
|
def remove(self, widget):
|
|
|
|
"""Remove a widget from the container"""
|
|
|
|
gtk.Paned.remove(self, widget)
|
2009-11-14 22:58:22 +00:00
|
|
|
self.disconnect_child(widget)
|
2009-11-07 01:40:43 +00:00
|
|
|
self.children.remove(widget)
|
|
|
|
return(True)
|
|
|
|
|
|
|
|
def wrapcloseterm(self, widget):
|
|
|
|
"""A child terminal has closed, so this container must die"""
|
2009-12-09 13:02:31 +00:00
|
|
|
dbg('Paned::wrapcloseterm: Called on %s' % widget)
|
2009-11-07 01:40:43 +00:00
|
|
|
if self.closeterm(widget):
|
|
|
|
# At this point we only have one child, which is the surviving term
|
|
|
|
sibling = self.children[0]
|
|
|
|
self.remove(sibling)
|
2009-11-25 00:37:29 +00:00
|
|
|
|
|
|
|
parent = self.get_parent()
|
2009-12-09 13:02:31 +00:00
|
|
|
parent.remove(self)
|
|
|
|
parent.add(sibling)
|
2009-11-07 01:40:43 +00:00
|
|
|
del(self)
|
|
|
|
else:
|
2009-11-14 18:55:07 +00:00
|
|
|
dbg("Paned::wrapcloseterm: self.closeterm failed")
|
2009-11-07 01:40:43 +00:00
|
|
|
|
2009-11-20 15:30:50 +00:00
|
|
|
def hoover(self):
|
|
|
|
"""Check that we still have a reason to exist"""
|
|
|
|
if len(self.children) == 1:
|
|
|
|
dbg('Paned::hoover: We only have one child, die')
|
|
|
|
parent = self.get_parent()
|
|
|
|
parent.remove(self)
|
|
|
|
child = self.children[0]
|
|
|
|
self.remove(child)
|
|
|
|
parent.add(child)
|
|
|
|
del(self)
|
|
|
|
|
2009-11-07 01:40:43 +00:00
|
|
|
def resizeterm(self, widget, keyname):
|
|
|
|
"""Handle a keyboard event requesting a terminal resize"""
|
2009-11-25 00:37:29 +00:00
|
|
|
maker = Factory()
|
2009-11-08 23:06:26 +00:00
|
|
|
if keyname in ['up', 'down'] and isinstance(self, gtk.VPaned):
|
|
|
|
# This is a key we can handle
|
|
|
|
position = self.get_position()
|
|
|
|
|
2009-11-25 00:37:29 +00:00
|
|
|
if maker.isinstance(widget, 'Terminal'):
|
2009-11-08 23:06:26 +00:00
|
|
|
fontheight = widget.vte.get_char_height()
|
|
|
|
else:
|
|
|
|
fontheight = 10
|
|
|
|
|
|
|
|
if keyname == 'up':
|
|
|
|
self.set_position(position - fontheight)
|
|
|
|
else:
|
|
|
|
self.set_position(position + fontheight)
|
|
|
|
elif keyname in ['left', 'right'] and isinstance(self, gtk.HPaned):
|
|
|
|
# This is a key we can handle
|
|
|
|
position = self.get_position()
|
|
|
|
|
2009-11-25 00:37:29 +00:00
|
|
|
if maker.isinstance(widget, 'Terminal'):
|
2009-11-08 23:06:26 +00:00
|
|
|
fontwidth = widget.vte.get_char_width()
|
|
|
|
else:
|
|
|
|
fontwidth = 10
|
|
|
|
|
|
|
|
if keyname == 'left':
|
|
|
|
self.set_position(position - fontwidth)
|
|
|
|
else:
|
|
|
|
self.set_position(position + fontwidth)
|
|
|
|
else:
|
|
|
|
# This is not a key we can handle
|
|
|
|
self.emit('resize-term', keyname)
|
2009-11-07 01:40:43 +00:00
|
|
|
|
|
|
|
class HPaned(Paned, gtk.HPaned):
|
|
|
|
"""Merge gtk.HPaned into our base Paned Container"""
|
|
|
|
def __init__(self):
|
|
|
|
"""Class initialiser"""
|
|
|
|
Paned.__init__(self)
|
|
|
|
gtk.HPaned.__init__(self)
|
2009-11-14 18:55:07 +00:00
|
|
|
self.register_signals(HPaned)
|
2010-01-18 13:17:35 +00:00
|
|
|
self.cnxids.new(self, 'expose-event', self.set_initial_position)
|
2009-11-07 01:40:43 +00:00
|
|
|
|
|
|
|
class VPaned(Paned, gtk.VPaned):
|
|
|
|
"""Merge gtk.VPaned into our base Paned Container"""
|
|
|
|
def __init__(self):
|
|
|
|
"""Class initialiser"""
|
|
|
|
Paned.__init__(self)
|
|
|
|
gtk.VPaned.__init__(self)
|
2009-11-14 18:55:07 +00:00
|
|
|
self.register_signals(VPaned)
|
2010-01-18 13:17:35 +00:00
|
|
|
self.cnxids.new(self, 'expose-event', self.set_initial_position)
|
2009-11-07 01:40:43 +00:00
|
|
|
|
|
|
|
gobject.type_register(HPaned)
|
|
|
|
gobject.type_register(VPaned)
|
|
|
|
# vim: set expandtab ts=4 sw=4:
|