2009-08-07 09:21:37 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
# Terminator by Chris Jones <cmsj@tenshu.net>
|
|
|
|
# GPL v2 only
|
|
|
|
"""container.py - classes necessary to contain Terminal widgets"""
|
|
|
|
|
2014-09-19 14:08:08 +00:00
|
|
|
from gi.repository import GObject
|
|
|
|
from gi.repository import Gtk
|
2009-08-09 21:00:43 +00:00
|
|
|
|
2009-12-10 13:20:03 +00:00
|
|
|
from factory import Factory
|
2009-08-19 00:04:53 +00:00
|
|
|
from config import Config
|
2009-11-14 18:54:12 +00:00
|
|
|
from util import dbg, err
|
2009-11-21 18:09:47 +00:00
|
|
|
from translation import _
|
2010-01-18 13:17:35 +00:00
|
|
|
from signalman import Signalman
|
2009-08-19 00:04:53 +00:00
|
|
|
|
2009-08-09 22:54:14 +00:00
|
|
|
# pylint: disable-msg=R0921
|
|
|
|
class Container(object):
|
2009-08-07 09:21:37 +00:00
|
|
|
"""Base class for Terminator Containers"""
|
|
|
|
|
2009-10-08 23:24:58 +00:00
|
|
|
terminator = None
|
2009-08-07 09:21:37 +00:00
|
|
|
immutable = None
|
|
|
|
children = None
|
|
|
|
config = None
|
2009-11-14 22:57:35 +00:00
|
|
|
signals = None
|
2010-01-18 13:17:35 +00:00
|
|
|
signalman = None
|
2009-08-09 21:00:43 +00:00
|
|
|
|
2009-08-19 00:04:53 +00:00
|
|
|
def __init__(self):
|
2009-08-07 09:21:37 +00:00
|
|
|
"""Class initialiser"""
|
|
|
|
self.children = []
|
2009-11-14 22:57:35 +00:00
|
|
|
self.signals = []
|
2010-01-18 13:17:35 +00:00
|
|
|
self.cnxids = Signalman()
|
2009-08-19 00:04:53 +00:00
|
|
|
self.config = Config()
|
2009-08-07 09:21:37 +00:00
|
|
|
|
2009-08-09 22:54:14 +00:00
|
|
|
def register_signals(self, widget):
|
2009-08-09 21:00:43 +00:00
|
|
|
"""Register gobject signals in a way that avoids multiple inheritance"""
|
2014-09-19 14:08:08 +00:00
|
|
|
existing = GObject.signal_list_names(widget)
|
2009-08-09 21:00:43 +00:00
|
|
|
for signal in self.signals:
|
2009-11-08 23:06:26 +00:00
|
|
|
if signal['name'] in existing:
|
|
|
|
dbg('Container:: skipping signal %s for %s, already exists' % (
|
|
|
|
signal['name'], widget))
|
|
|
|
else:
|
2009-11-20 22:52:39 +00:00
|
|
|
dbg('Container:: registering signal for %s on %s' %
|
|
|
|
(signal['name'], widget))
|
2009-11-14 18:54:12 +00:00
|
|
|
try:
|
2014-09-19 14:08:08 +00:00
|
|
|
GObject.signal_new(signal['name'],
|
2009-11-14 18:54:12 +00:00
|
|
|
widget,
|
|
|
|
signal['flags'],
|
|
|
|
signal['return_type'],
|
|
|
|
signal['param_types'])
|
|
|
|
except RuntimeError:
|
|
|
|
err('Container:: registering signal for %s on %s failed' %
|
|
|
|
(signal['name'], widget))
|
2009-08-09 21:00:43 +00:00
|
|
|
|
2010-01-18 13:17:35 +00:00
|
|
|
def connect_child(self, widget, signal, handler, *args):
|
2009-11-14 22:57:35 +00:00
|
|
|
"""Register the requested signal and record its connection ID"""
|
2010-01-18 13:17:35 +00:00
|
|
|
self.cnxids.new(widget, signal, handler, *args)
|
|
|
|
return
|
2009-11-14 22:57:35 +00:00
|
|
|
|
|
|
|
def disconnect_child(self, widget):
|
|
|
|
"""De-register the signals for a child"""
|
2010-01-18 13:17:35 +00:00
|
|
|
self.cnxids.remove_widget(widget)
|
2009-11-14 22:57:35 +00:00
|
|
|
|
2009-08-07 09:21:37 +00:00
|
|
|
def get_offspring(self):
|
2010-01-28 13:41:44 +00:00
|
|
|
"""Return a list of direct child widgets, if any"""
|
2009-08-07 09:21:37 +00:00
|
|
|
return(self.children)
|
|
|
|
|
2011-08-22 20:05:38 +00:00
|
|
|
def get_child_metadata(self, widget):
|
|
|
|
"""Return metadata that would be useful to recreate ourselves after our
|
|
|
|
child is .remove()d and .add()ed"""
|
|
|
|
return None
|
|
|
|
|
2010-03-05 22:44:38 +00:00
|
|
|
def split_horiz(self, widget, cwd=None):
|
2009-08-07 23:31:44 +00:00
|
|
|
"""Split this container horizontally"""
|
2010-03-05 22:44:38 +00:00
|
|
|
return(self.split_axis(widget, True, cwd))
|
2009-08-07 09:21:37 +00:00
|
|
|
|
2010-03-05 22:44:38 +00:00
|
|
|
def split_vert(self, widget, cwd=None):
|
2009-08-07 23:31:44 +00:00
|
|
|
"""Split this container vertically"""
|
2010-03-05 22:44:38 +00:00
|
|
|
return(self.split_axis(widget, False, cwd))
|
2009-08-07 09:21:37 +00:00
|
|
|
|
2010-03-05 22:44:38 +00:00
|
|
|
def split_axis(self, widget, vertical=True, cwd=None, sibling=None, siblinglast=None):
|
2009-08-07 23:31:44 +00:00
|
|
|
"""Default axis splitter. This should be implemented by subclasses"""
|
2009-08-09 22:54:14 +00:00
|
|
|
raise NotImplementedError('split_axis')
|
2009-08-09 17:48:06 +00:00
|
|
|
|
2012-04-04 15:48:29 +00:00
|
|
|
def rotate(self, widget, clockwise):
|
|
|
|
"""Rotate children in this container"""
|
|
|
|
raise NotImplementedError('rotate')
|
|
|
|
|
2011-08-22 20:05:38 +00:00
|
|
|
def add(self, widget, metadata=None):
|
2009-08-27 23:20:22 +00:00
|
|
|
"""Add a widget to the container"""
|
|
|
|
raise NotImplementedError('add')
|
|
|
|
|
2009-08-09 21:00:43 +00:00
|
|
|
def remove(self, widget):
|
|
|
|
"""Remove a widget from the container"""
|
2009-08-09 22:54:14 +00:00
|
|
|
raise NotImplementedError('remove')
|
2009-08-09 21:00:43 +00:00
|
|
|
|
2010-04-12 20:35:24 +00:00
|
|
|
def replace(self, oldwidget, newwidget):
|
|
|
|
"""Replace the child oldwidget with newwidget. This is the bare minimum
|
|
|
|
required for this operation. Containers should override it if they have
|
|
|
|
more complex requirements"""
|
|
|
|
if not oldwidget in self.get_children():
|
|
|
|
err('%s is not a child of %s' % (oldwidget, self))
|
|
|
|
return
|
|
|
|
self.remove(oldwidget)
|
|
|
|
self.add(newwidget)
|
|
|
|
|
2010-03-02 20:37:41 +00:00
|
|
|
def hoover(self):
|
2010-03-02 12:39:47 +00:00
|
|
|
"""Ensure we still have a reason to exist"""
|
|
|
|
raise NotImplementedError('hoover')
|
|
|
|
|
2010-03-10 22:51:33 +00:00
|
|
|
def get_children(self):
|
|
|
|
"""Return an ordered list of the children of this Container"""
|
|
|
|
raise NotImplementedError('get_children')
|
|
|
|
|
2009-08-09 17:48:06 +00:00
|
|
|
def closeterm(self, widget):
|
|
|
|
"""Handle the closure of a terminal"""
|
2009-11-20 05:16:20 +00:00
|
|
|
try:
|
|
|
|
if self.get_property('term_zoomed'):
|
|
|
|
# We're zoomed, so unzoom and then start closing again
|
|
|
|
dbg('Container::closeterm: terminal zoomed, unzooming')
|
|
|
|
self.unzoom(widget)
|
|
|
|
widget.close()
|
|
|
|
return(True)
|
|
|
|
except TypeError:
|
|
|
|
pass
|
2009-08-09 17:48:06 +00:00
|
|
|
|
|
|
|
if not self.remove(widget):
|
2009-12-08 13:57:29 +00:00
|
|
|
dbg('Container::closeterm: self.remove() failed for %s' % widget)
|
2009-08-09 17:48:06 +00:00
|
|
|
return(False)
|
|
|
|
|
2009-10-05 21:15:22 +00:00
|
|
|
self.terminator.deregister_terminal(widget)
|
2009-11-07 01:40:43 +00:00
|
|
|
self.terminator.group_hoover()
|
2009-08-09 17:48:06 +00:00
|
|
|
return(True)
|
|
|
|
|
|
|
|
def resizeterm(self, widget, keyname):
|
|
|
|
"""Handle a keyboard event requesting a terminal resize"""
|
2009-08-09 22:54:14 +00:00
|
|
|
raise NotImplementedError('resizeterm')
|
2009-08-09 17:48:06 +00:00
|
|
|
|
|
|
|
def toggle_zoom(self, widget, fontscale = False):
|
|
|
|
"""Toggle the existing zoom state"""
|
2009-11-20 05:16:20 +00:00
|
|
|
try:
|
|
|
|
if self.get_property('term_zoomed'):
|
|
|
|
self.unzoom(widget)
|
|
|
|
else:
|
|
|
|
self.zoom(widget, fontscale)
|
|
|
|
except TypeError:
|
|
|
|
err('Container::toggle_zoom: %s is unable to handle zooming, for \
|
|
|
|
%s' % (self, widget))
|
2009-08-09 17:48:06 +00:00
|
|
|
|
|
|
|
def zoom(self, widget, fontscale = False):
|
|
|
|
"""Zoom a terminal"""
|
2009-08-09 22:54:14 +00:00
|
|
|
raise NotImplementedError('zoom')
|
2009-08-09 17:48:06 +00:00
|
|
|
|
|
|
|
def unzoom(self, widget):
|
|
|
|
"""Unzoom a terminal"""
|
2009-08-09 22:54:14 +00:00
|
|
|
raise NotImplementedError('unzoom')
|
|
|
|
|
2009-12-09 13:22:34 +00:00
|
|
|
def construct_confirm_close(self, window, reqtype):
|
2009-11-21 18:09:47 +00:00
|
|
|
"""Create a confirmation dialog for closing things"""
|
2011-12-08 13:12:29 +00:00
|
|
|
|
|
|
|
# skip this dialog if applicable
|
|
|
|
if self.config['suppress_multiple_term_dialog']:
|
2014-09-19 14:08:08 +00:00
|
|
|
return Gtk.ResponseType.ACCEPT
|
2011-12-08 13:12:29 +00:00
|
|
|
|
2014-09-19 14:08:08 +00:00
|
|
|
dialog = Gtk.Dialog(_('Close?'), window, Gtk.DialogFlags.MODAL)
|
2009-11-21 18:09:47 +00:00
|
|
|
dialog.set_has_separator(False)
|
|
|
|
dialog.set_resizable(False)
|
|
|
|
|
2014-09-19 14:08:08 +00:00
|
|
|
dialog.add_button(Gtk.STOCK_CANCEL, Gtk.ResponseType.REJECT)
|
|
|
|
c_all = dialog.add_button(Gtk.STOCK_CLOSE, Gtk.ResponseType.ACCEPT)
|
2010-01-22 18:56:12 +00:00
|
|
|
c_all.get_children()[0].get_children()[0].get_children()[1].set_label(
|
|
|
|
_('Close _Terminals'))
|
2009-11-21 18:09:47 +00:00
|
|
|
|
2014-09-19 14:08:08 +00:00
|
|
|
primary = Gtk.Label(label=_('<big><b>Close multiple terminals?</b></big>'))
|
2009-11-21 18:09:47 +00:00
|
|
|
primary.set_use_markup(True)
|
|
|
|
primary.set_alignment(0, 0.5)
|
2014-09-19 14:08:08 +00:00
|
|
|
secondary = Gtk.Label(label=_('This %s has several terminals open. Closing \
|
2009-12-09 13:22:34 +00:00
|
|
|
the %s will also close all terminals within it.') % (reqtype, reqtype))
|
2009-11-21 18:09:47 +00:00
|
|
|
secondary.set_line_wrap(True)
|
2011-12-08 13:12:29 +00:00
|
|
|
|
2014-09-19 14:08:08 +00:00
|
|
|
labels = Gtk.VBox()
|
2009-11-21 18:09:47 +00:00
|
|
|
labels.pack_start(primary, False, False, 6)
|
|
|
|
labels.pack_start(secondary, False, False, 6)
|
|
|
|
|
2014-09-19 14:08:08 +00:00
|
|
|
image = Gtk.Image.new_from_stock(Gtk.STOCK_DIALOG_WARNING,
|
|
|
|
Gtk.IconSize.DIALOG)
|
2009-11-21 18:09:47 +00:00
|
|
|
image.set_alignment(0.5, 0)
|
|
|
|
|
2014-09-19 14:08:08 +00:00
|
|
|
box = Gtk.HBox()
|
2009-11-21 18:09:47 +00:00
|
|
|
box.pack_start(image, False, False, 6)
|
|
|
|
box.pack_start(labels, False, False, 6)
|
|
|
|
dialog.vbox.pack_start(box, False, False, 12)
|
2011-12-08 13:12:29 +00:00
|
|
|
|
2014-09-19 14:08:08 +00:00
|
|
|
checkbox = Gtk.CheckButton(_("Do not show this message next time"))
|
|
|
|
dialog.vbox.pack_end(checkbox, True, True, 0)
|
2009-11-21 18:09:47 +00:00
|
|
|
|
|
|
|
dialog.show_all()
|
2011-12-08 13:12:29 +00:00
|
|
|
|
|
|
|
result = dialog.run()
|
|
|
|
|
|
|
|
# set configuration
|
2013-09-25 22:07:01 +00:00
|
|
|
self.config.base.reload()
|
2011-12-08 13:12:29 +00:00
|
|
|
self.config['suppress_multiple_term_dialog'] = checkbox.get_active()
|
|
|
|
self.config.save()
|
|
|
|
|
|
|
|
dialog.destroy()
|
|
|
|
|
|
|
|
return(result)
|
2009-12-10 13:20:03 +00:00
|
|
|
|
|
|
|
def propagate_title_change(self, widget, title):
|
|
|
|
"""Pass a title change up the widget stack"""
|
|
|
|
maker = Factory()
|
|
|
|
parent = self.get_parent()
|
|
|
|
title = widget.get_window_title()
|
|
|
|
|
|
|
|
if maker.isinstance(self, 'Notebook'):
|
|
|
|
self.update_tab_label_text(widget, title)
|
|
|
|
elif maker.isinstance(self, 'Window'):
|
|
|
|
self.title.set_title(widget, title)
|
|
|
|
|
|
|
|
if maker.isinstance(parent, 'Container'):
|
|
|
|
parent.propagate_title_change(widget, title)
|
|
|
|
|
2010-01-19 13:06:09 +00:00
|
|
|
def get_visible_terminals(self):
|
|
|
|
"""Walk the widget tree to find all of the visible terminals. That is,
|
|
|
|
any terminals which are not hidden in another Notebook pane"""
|
2011-02-17 01:57:30 +00:00
|
|
|
if not hasattr(self, 'cached_maker'):
|
|
|
|
self.cached_maker = Factory()
|
|
|
|
maker = self.cached_maker
|
2010-01-19 13:06:09 +00:00
|
|
|
terminals = {}
|
|
|
|
|
|
|
|
for child in self.get_offspring():
|
2010-07-03 19:42:33 +00:00
|
|
|
if not child:
|
|
|
|
continue
|
2010-01-19 13:06:09 +00:00
|
|
|
if maker.isinstance(child, 'Terminal'):
|
|
|
|
terminals[child] = child.get_allocation()
|
|
|
|
elif maker.isinstance(child, 'Container'):
|
|
|
|
terminals.update(child.get_visible_terminals())
|
|
|
|
else:
|
|
|
|
err('Unknown child type %s' % type(child))
|
|
|
|
|
|
|
|
return(terminals)
|
|
|
|
|
2010-03-11 13:04:01 +00:00
|
|
|
def describe_layout(self, count, parent, global_layout, child_order):
|
2010-02-01 12:11:44 +00:00
|
|
|
"""Describe our current layout"""
|
|
|
|
layout = {}
|
|
|
|
maker = Factory()
|
|
|
|
mytype = maker.type(self)
|
|
|
|
if not mytype:
|
|
|
|
err('unable to detemine own type. %s' % self)
|
|
|
|
return({})
|
|
|
|
|
|
|
|
layout['type'] = mytype
|
2010-02-02 00:39:41 +00:00
|
|
|
layout['parent'] = parent
|
2010-03-11 13:04:01 +00:00
|
|
|
layout['order'] = child_order
|
2010-02-27 13:55:38 +00:00
|
|
|
|
|
|
|
if hasattr(self, 'get_position'):
|
|
|
|
position = self.get_position()
|
|
|
|
if hasattr(position, '__iter__'):
|
|
|
|
position = ':'.join([str(x) for x in position])
|
|
|
|
layout['position'] = position
|
2013-09-02 14:54:07 +00:00
|
|
|
|
2013-09-04 20:59:27 +00:00
|
|
|
if hasattr(self, 'ismaximised'):
|
|
|
|
layout['maximised'] = self.ismaximised
|
|
|
|
|
|
|
|
if hasattr(self, 'isfullscreen'):
|
|
|
|
layout['fullscreen'] = self.isfullscreen
|
2013-11-15 12:21:24 +00:00
|
|
|
|
2013-09-02 14:54:07 +00:00
|
|
|
if hasattr(self, 'ratio'):
|
|
|
|
layout['ratio'] = self.ratio
|
2010-02-27 13:55:38 +00:00
|
|
|
|
2010-02-27 14:18:08 +00:00
|
|
|
if hasattr(self, 'get_size'):
|
|
|
|
layout['size'] = self.get_size()
|
|
|
|
|
2013-06-20 17:48:22 +00:00
|
|
|
if hasattr(self, 'title'):
|
|
|
|
layout['title'] = self.title.text
|
|
|
|
|
2010-07-04 16:22:39 +00:00
|
|
|
if mytype == 'Notebook':
|
2013-11-15 12:21:24 +00:00
|
|
|
labels = []
|
2013-12-18 17:06:59 +00:00
|
|
|
last_active_term = []
|
2010-07-04 16:22:39 +00:00
|
|
|
for tabnum in xrange(0, self.get_n_pages()):
|
|
|
|
page = self.get_nth_page(tabnum)
|
|
|
|
label = self.get_tab_label(page)
|
|
|
|
labels.append(label.get_custom_label())
|
2013-12-18 17:06:59 +00:00
|
|
|
last_active_term.append(self.last_active_term[self.get_nth_page(tabnum)])
|
2010-07-04 16:22:39 +00:00
|
|
|
layout['labels'] = labels
|
2013-12-18 17:06:59 +00:00
|
|
|
layout['last_active_term'] = last_active_term
|
2013-11-15 12:21:24 +00:00
|
|
|
layout['active_page'] = self.get_current_page()
|
2013-11-19 11:46:11 +00:00
|
|
|
else:
|
|
|
|
if hasattr(self, 'last_active_term') and self.last_active_term is not None:
|
|
|
|
layout['last_active_term'] = self.last_active_term
|
2013-12-18 17:06:59 +00:00
|
|
|
|
2013-10-31 17:50:21 +00:00
|
|
|
if mytype == 'Window':
|
|
|
|
if self.uuid == self.terminator.last_active_window:
|
|
|
|
layout['last_active_window'] = True
|
|
|
|
else:
|
|
|
|
layout['last_active_window'] = False
|
|
|
|
|
2010-02-02 00:39:41 +00:00
|
|
|
name = 'child%d' % count
|
|
|
|
count = count + 1
|
|
|
|
|
|
|
|
global_layout[name] = layout
|
|
|
|
|
2010-03-11 13:04:01 +00:00
|
|
|
child_order = 0
|
2010-02-01 12:11:44 +00:00
|
|
|
for child in self.get_children():
|
2010-03-04 11:59:36 +00:00
|
|
|
if hasattr(child, 'describe_layout'):
|
2010-03-11 13:04:01 +00:00
|
|
|
count = child.describe_layout(count, name, global_layout, child_order)
|
|
|
|
child_order = child_order + 1
|
2010-02-01 12:11:44 +00:00
|
|
|
|
2010-02-02 00:39:41 +00:00
|
|
|
return(count)
|
2010-02-01 12:11:44 +00:00
|
|
|
|
|
|
|
def create_layout(self, layout):
|
|
|
|
"""Apply settings for our layout"""
|
|
|
|
raise NotImplementedError('create_layout')
|
|
|
|
|
2012-04-04 15:48:29 +00:00
|
|
|
|
2009-08-09 17:48:06 +00:00
|
|
|
# vim: set expandtab ts=4 sw=4:
|