2009-08-07 23:31:44 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
# Terminator by Chris Jones <cmsj@tenshu.net>
|
|
|
|
# GPL v2 only
|
|
|
|
"""window.py - class for the main Terminator window"""
|
|
|
|
|
|
|
|
import pygtk
|
|
|
|
pygtk.require('2.0')
|
|
|
|
import gobject
|
|
|
|
import gtk
|
2010-01-19 20:03:05 +00:00
|
|
|
import glib
|
2009-08-07 23:31:44 +00:00
|
|
|
|
2009-08-09 23:11:31 +00:00
|
|
|
from util import dbg, err
|
2009-11-21 18:47:38 +00:00
|
|
|
from translation import _
|
2009-08-10 22:09:49 +00:00
|
|
|
from version import APP_NAME
|
2009-08-07 23:31:44 +00:00
|
|
|
from container import Container
|
2009-11-25 00:37:29 +00:00
|
|
|
from factory import Factory
|
2010-01-11 20:06:53 +00:00
|
|
|
from terminator import Terminator
|
2009-08-07 23:31:44 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
import deskbar.core.keybinder as bindkey
|
|
|
|
except ImportError:
|
|
|
|
err('Unable to find python bindings for deskbar, "hide_window" is not' \
|
|
|
|
'available.')
|
|
|
|
|
2009-08-18 12:57:35 +00:00
|
|
|
# pylint: disable-msg=R0904
|
2009-08-07 23:31:44 +00:00
|
|
|
class Window(Container, gtk.Window):
|
|
|
|
"""Class implementing a top-level Terminator window"""
|
|
|
|
|
2009-08-10 23:15:31 +00:00
|
|
|
terminator = None
|
2009-08-07 23:31:44 +00:00
|
|
|
title = None
|
|
|
|
isfullscreen = None
|
2009-08-09 23:07:40 +00:00
|
|
|
ismaximised = None
|
2009-08-07 23:31:44 +00:00
|
|
|
hidebound = None
|
2009-08-09 22:25:53 +00:00
|
|
|
hidefunc = None
|
2009-08-07 23:31:44 +00:00
|
|
|
|
2009-11-14 18:56:50 +00:00
|
|
|
zoom_data = None
|
2009-10-06 23:08:13 +00:00
|
|
|
term_zoomed = gobject.property(type=bool, default=False)
|
|
|
|
|
2010-01-11 20:56:30 +00:00
|
|
|
def __init__(self):
|
2009-08-07 23:31:44 +00:00
|
|
|
"""Class initialiser"""
|
2009-08-10 23:15:31 +00:00
|
|
|
self.terminator = Terminator()
|
2009-11-17 03:58:12 +00:00
|
|
|
self.terminator.register_window(self)
|
2009-08-10 23:15:31 +00:00
|
|
|
|
2009-08-19 00:06:07 +00:00
|
|
|
Container.__init__(self)
|
2009-08-07 23:31:44 +00:00
|
|
|
gtk.Window.__init__(self)
|
2009-08-09 21:00:43 +00:00
|
|
|
gobject.type_register(Window)
|
|
|
|
self.register_signals(Window)
|
2009-08-07 23:31:44 +00:00
|
|
|
|
|
|
|
self.set_property('allow-shrink', True)
|
2009-08-10 22:09:49 +00:00
|
|
|
self.apply_icon()
|
|
|
|
|
2009-08-07 23:31:44 +00:00
|
|
|
self.register_callbacks()
|
|
|
|
self.apply_config()
|
|
|
|
|
2009-09-02 20:10:28 +00:00
|
|
|
self.title = WindowTitle(self)
|
|
|
|
self.title.update()
|
|
|
|
|
2010-01-11 20:56:30 +00:00
|
|
|
options = self.config.options_get()
|
|
|
|
if options:
|
|
|
|
if options.forcedtitle is not None:
|
|
|
|
self.title.force_title(options.forcedtitle)
|
2010-01-05 12:55:05 +00:00
|
|
|
|
2010-01-11 20:56:30 +00:00
|
|
|
if options.role is not None:
|
|
|
|
self.set_role(options.role)
|
|
|
|
|
|
|
|
if options.geometry is not None:
|
|
|
|
if not self.parse_geometry(options.geometry):
|
|
|
|
err('Window::__init__: Unable to parse geometry: %s' %
|
|
|
|
options.geometry)
|
2010-01-05 12:58:05 +00:00
|
|
|
|
2009-08-07 23:31:44 +00:00
|
|
|
def register_callbacks(self):
|
|
|
|
"""Connect the GTK+ signals we care about"""
|
|
|
|
self.connect('key-press-event', self.on_key_press)
|
|
|
|
self.connect('delete_event', self.on_delete_event)
|
|
|
|
self.connect('destroy', self.on_destroy_event)
|
|
|
|
self.connect('window-state-event', self.on_window_state_changed)
|
|
|
|
|
2009-08-09 22:25:53 +00:00
|
|
|
# Attempt to grab a global hotkey for hiding the window.
|
|
|
|
# If we fail, we'll never hide the window, iconifying instead.
|
2009-08-09 23:07:40 +00:00
|
|
|
try:
|
|
|
|
self.hidebound = bindkey.tomboy_keybinder_bind(
|
|
|
|
self.config['keybindings']['hide_window'],
|
|
|
|
self.on_hide_window)
|
2010-01-19 11:14:01 +00:00
|
|
|
except (KeyError, NameError):
|
2009-08-09 23:07:40 +00:00
|
|
|
pass
|
|
|
|
|
2009-08-09 22:25:53 +00:00
|
|
|
if not self.hidebound:
|
2009-08-07 23:31:44 +00:00
|
|
|
dbg('Unable to bind hide_window key, another instance has it.')
|
2009-08-09 22:25:53 +00:00
|
|
|
self.hidefunc = self.iconify
|
|
|
|
else:
|
|
|
|
self.hidefunc = self.hide
|
2009-08-07 23:31:44 +00:00
|
|
|
|
|
|
|
def apply_config(self):
|
|
|
|
"""Apply various configuration options"""
|
2010-01-11 20:56:30 +00:00
|
|
|
options = self.config.options_get()
|
|
|
|
maximise = self.config['window_state'] == 'maximise'
|
|
|
|
fullscreen = self.config['window_state'] == 'fullscreen'
|
|
|
|
hidden = self.config['window_state'] == 'hidden'
|
|
|
|
borderless = self.config['borderless']
|
|
|
|
|
|
|
|
if options:
|
|
|
|
if options.maximise:
|
|
|
|
maximise = True
|
|
|
|
if options.fullscreen:
|
|
|
|
fullscreen = True
|
|
|
|
if options.hidden:
|
|
|
|
hidden = True
|
|
|
|
if options.borderless:
|
|
|
|
borderless = True
|
|
|
|
|
|
|
|
self.set_fullscreen(fullscreen)
|
|
|
|
self.set_maximised(maximise)
|
|
|
|
self.set_borderless(borderless)
|
2009-11-21 18:47:38 +00:00
|
|
|
self.set_real_transparency()
|
2009-08-07 23:31:44 +00:00
|
|
|
if self.hidebound:
|
2010-01-11 20:56:30 +00:00
|
|
|
self.set_hidden(hidden)
|
2009-08-07 23:31:44 +00:00
|
|
|
else:
|
2010-01-11 20:56:30 +00:00
|
|
|
self.set_iconified(hidden)
|
2009-08-07 23:31:44 +00:00
|
|
|
|
2009-08-10 22:09:49 +00:00
|
|
|
def apply_icon(self):
|
|
|
|
"""Set the window icon"""
|
|
|
|
icon_theme = gtk.IconTheme()
|
|
|
|
|
|
|
|
try:
|
|
|
|
icon = icon_theme.load_icon(APP_NAME, 48, 0)
|
2010-01-19 20:03:05 +00:00
|
|
|
except (NameError, glib.GError):
|
2009-08-10 22:09:49 +00:00
|
|
|
dbg('Unable to load 48px Terminator icon')
|
|
|
|
icon = self.render_icon(gtk.STOCK_DIALOG_INFO, gtk.ICON_SIZE_BUTTON)
|
|
|
|
|
|
|
|
self.set_icon(icon)
|
|
|
|
|
2009-08-07 23:31:44 +00:00
|
|
|
def on_key_press(self, window, event):
|
2009-08-09 23:07:40 +00:00
|
|
|
"""Handle a keyboard event"""
|
2009-11-25 00:37:29 +00:00
|
|
|
maker = Factory()
|
2009-11-21 18:47:38 +00:00
|
|
|
# FIXME: We probably want to cancel window urgency here
|
|
|
|
|
|
|
|
mapping = self.terminator.keybindings.lookup(event)
|
|
|
|
|
|
|
|
if mapping:
|
|
|
|
dbg('Window::on_key_press: looked up %r' % mapping)
|
|
|
|
if mapping == 'full_screen':
|
|
|
|
self.set_fullscreen(not self.isfullscreen)
|
|
|
|
elif mapping == 'close_window':
|
|
|
|
if not self.on_delete_event(window,
|
|
|
|
gtk.gdk.Event(gtk.gdk.DELETE)):
|
|
|
|
self.on_destroy_event(window,
|
|
|
|
gtk.gdk.Event(gtk.gdk.DESTROY))
|
2009-11-22 04:28:39 +00:00
|
|
|
elif mapping == 'new_tab':
|
2009-12-08 13:01:13 +00:00
|
|
|
self.tab_new()
|
2009-11-21 18:47:38 +00:00
|
|
|
else:
|
|
|
|
return(False)
|
|
|
|
return(True)
|
2009-08-07 23:31:44 +00:00
|
|
|
|
2009-12-08 13:01:13 +00:00
|
|
|
def tab_new(self):
|
|
|
|
"""Make a new tab"""
|
|
|
|
maker = Factory()
|
|
|
|
if not maker.isinstance(self.get_child(), 'Notebook'):
|
2010-01-05 12:49:57 +00:00
|
|
|
notebook = maker.make('Notebook', window=self)
|
2009-12-08 13:01:13 +00:00
|
|
|
self.get_child().newtab()
|
|
|
|
|
2009-08-07 23:31:44 +00:00
|
|
|
def on_delete_event(self, window, event, data=None):
|
2009-08-09 23:07:40 +00:00
|
|
|
"""Handle a window close request"""
|
2009-11-25 00:37:29 +00:00
|
|
|
maker = Factory()
|
|
|
|
if maker.isinstance(self.get_child(), 'Terminal'):
|
2009-11-21 18:47:38 +00:00
|
|
|
dbg('Window::on_delete_event: Only one child, closing is fine')
|
|
|
|
return(False)
|
|
|
|
return(self.confirm_close(window, _('window')))
|
|
|
|
|
|
|
|
def confirm_close(self, window, type):
|
|
|
|
"""Display a confirmation dialog when the user is closing multiple
|
|
|
|
terminals in one window"""
|
|
|
|
dialog = self.construct_confirm_close(window, type)
|
|
|
|
result = dialog.run()
|
|
|
|
dialog.destroy()
|
|
|
|
return(not (result == gtk.RESPONSE_ACCEPT))
|
2009-08-07 23:31:44 +00:00
|
|
|
|
|
|
|
def on_destroy_event(self, widget, data=None):
|
2009-08-09 23:07:40 +00:00
|
|
|
"""Handle window descruction"""
|
2009-11-17 03:58:12 +00:00
|
|
|
self.terminator.deregister_window(self)
|
2009-11-21 18:47:38 +00:00
|
|
|
self.destroy()
|
|
|
|
del(self)
|
2009-08-09 23:07:40 +00:00
|
|
|
|
|
|
|
def on_hide_window(self, data):
|
|
|
|
"""Handle a request to hide/show the window"""
|
2009-08-07 23:31:44 +00:00
|
|
|
pass
|
|
|
|
|
2009-08-18 12:57:35 +00:00
|
|
|
# pylint: disable-msg=W0613
|
2009-08-07 23:31:44 +00:00
|
|
|
def on_window_state_changed(self, window, event):
|
2009-08-09 23:07:40 +00:00
|
|
|
"""Handle the state of the window changing"""
|
2009-08-07 23:31:44 +00:00
|
|
|
self.isfullscreen = bool(event.new_window_state &
|
|
|
|
gtk.gdk.WINDOW_STATE_FULLSCREEN)
|
|
|
|
self.ismaximised = bool(event.new_window_state &
|
|
|
|
gtk.gdk.WINDOW_STATE_MAXIMIZED)
|
2009-11-17 03:58:12 +00:00
|
|
|
dbg('Window::on_window_state_changed: fullscreen=%s, maximised=%s' %
|
2009-08-07 23:31:44 +00:00
|
|
|
(self.isfullscreen, self.ismaximised))
|
2009-08-09 23:07:40 +00:00
|
|
|
|
2009-08-07 23:31:44 +00:00
|
|
|
return(False)
|
|
|
|
|
|
|
|
def set_maximised(self, value):
|
2009-08-09 23:07:40 +00:00
|
|
|
"""Set the maximised state of the window from the supplied value"""
|
2009-08-07 23:31:44 +00:00
|
|
|
if value == True:
|
|
|
|
self.maximize()
|
|
|
|
else:
|
|
|
|
self.unmaximize()
|
|
|
|
|
|
|
|
def set_fullscreen(self, value):
|
2009-08-09 23:07:40 +00:00
|
|
|
"""Set the fullscreen state of the window from the supplied value"""
|
2009-08-07 23:31:44 +00:00
|
|
|
if value == True:
|
|
|
|
self.fullscreen()
|
|
|
|
else:
|
|
|
|
self.unfullscreen()
|
|
|
|
|
|
|
|
def set_borderless(self, value):
|
2009-08-09 23:07:40 +00:00
|
|
|
"""Set the state of the window border from the supplied value"""
|
2009-08-07 23:31:44 +00:00
|
|
|
self.set_decorated (not value)
|
|
|
|
|
|
|
|
def set_hidden(self, value):
|
2009-08-09 23:07:40 +00:00
|
|
|
"""Set the visibility of the window from the supplied value"""
|
2009-08-07 23:31:44 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
def set_iconified(self, value):
|
2009-08-09 23:07:40 +00:00
|
|
|
"""Set the minimised state of the window from the value"""
|
2009-08-07 23:31:44 +00:00
|
|
|
pass
|
|
|
|
|
2009-11-21 18:47:38 +00:00
|
|
|
def set_real_transparency(self, value=True):
|
2009-08-09 23:07:40 +00:00
|
|
|
"""Enable RGBA if supported on the current screen"""
|
2009-08-07 23:31:44 +00:00
|
|
|
screen = self.get_screen()
|
|
|
|
if value:
|
|
|
|
colormap = screen.get_rgba_colormap()
|
|
|
|
else:
|
|
|
|
colormap = screen.get_rgb_colormap()
|
|
|
|
|
|
|
|
if colormap:
|
|
|
|
self.set_colormap(colormap)
|
|
|
|
|
2009-08-27 23:20:22 +00:00
|
|
|
def add(self, widget):
|
|
|
|
"""Add a widget to the window by way of gtk.Window.add()"""
|
2009-11-25 00:37:29 +00:00
|
|
|
maker = Factory()
|
2009-11-14 23:04:15 +00:00
|
|
|
gtk.Window.add(self, widget)
|
2009-11-25 00:37:29 +00:00
|
|
|
if maker.isinstance(widget, 'Terminal'):
|
2009-11-14 22:58:42 +00:00
|
|
|
signals = {'close-term': self.closeterm,
|
|
|
|
'title-change': self.title.set_title,
|
|
|
|
'split-horiz': self.split_horiz,
|
|
|
|
'split-vert': self.split_vert,
|
2009-11-20 05:16:20 +00:00
|
|
|
'unzoom': self.unzoom}
|
2009-11-14 22:58:42 +00:00
|
|
|
|
|
|
|
for signal in signals:
|
|
|
|
self.connect_child(widget, signal, signals[signal])
|
|
|
|
|
2009-11-14 23:04:15 +00:00
|
|
|
widget.grab_focus()
|
2009-08-27 23:20:22 +00:00
|
|
|
|
|
|
|
def remove(self, widget):
|
|
|
|
"""Remove our child widget by way of gtk.Window.remove()"""
|
|
|
|
gtk.Window.remove(self, widget)
|
2009-11-14 22:58:42 +00:00
|
|
|
self.disconnect_child(widget)
|
|
|
|
return(True)
|
2009-10-05 21:16:28 +00:00
|
|
|
|
2009-11-17 04:56:55 +00:00
|
|
|
def split_axis(self, widget, vertical=True, sibling=None):
|
2009-10-05 21:16:28 +00:00
|
|
|
"""Split the window"""
|
2009-11-25 00:37:29 +00:00
|
|
|
maker = Factory()
|
2009-10-05 21:16:28 +00:00
|
|
|
self.remove(widget)
|
|
|
|
|
|
|
|
if vertical:
|
2009-11-25 00:37:29 +00:00
|
|
|
container = maker.make('VPaned')
|
2009-10-05 21:16:28 +00:00
|
|
|
else:
|
2009-11-25 00:37:29 +00:00
|
|
|
container = maker.make('HPaned')
|
2009-10-05 21:16:28 +00:00
|
|
|
|
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-14 18:56:50 +00:00
|
|
|
self.add(container)
|
|
|
|
container.show_all()
|
2009-10-05 21:16:28 +00:00
|
|
|
|
2009-11-07 01:40:43 +00:00
|
|
|
for term in [widget, sibling]:
|
|
|
|
container.add(term)
|
|
|
|
container.show_all()
|
2009-10-05 21:16:28 +00:00
|
|
|
|
2009-10-27 23:23:59 +00:00
|
|
|
sibling.spawn_child()
|
2009-08-27 23:20:22 +00:00
|
|
|
|
2009-11-20 05:16:20 +00:00
|
|
|
def zoom(self, widget, font_scale=True):
|
2009-11-14 18:56:50 +00:00
|
|
|
"""Zoom a terminal widget"""
|
|
|
|
children = self.get_children()
|
|
|
|
|
|
|
|
if widget in children:
|
|
|
|
# This widget is a direct child of ours and we're a Window
|
|
|
|
# so zooming is a no-op
|
|
|
|
return
|
|
|
|
|
|
|
|
self.zoom_data = widget.get_zoom_data()
|
|
|
|
self.zoom_data['widget'] = widget
|
|
|
|
self.zoom_data['old_child'] = children[0]
|
|
|
|
self.zoom_data['font_scale'] = font_scale
|
|
|
|
|
|
|
|
self.remove(self.zoom_data['old_child'])
|
|
|
|
self.zoom_data['old_parent'].remove(widget)
|
|
|
|
self.add(widget)
|
|
|
|
self.set_property('term_zoomed', True)
|
|
|
|
|
|
|
|
if font_scale:
|
2010-01-18 19:48:24 +00:00
|
|
|
widget.cnxids.new(widget, 'size-allocate',
|
2009-11-14 18:56:50 +00:00
|
|
|
widget.zoom_scale, self.zoom_data)
|
|
|
|
|
|
|
|
widget.grab_focus()
|
|
|
|
|
2009-11-20 05:16:20 +00:00
|
|
|
def unzoom(self, widget):
|
2009-11-14 18:56:50 +00:00
|
|
|
"""Restore normal terminal layout"""
|
|
|
|
if not self.get_property('term_zoomed'):
|
|
|
|
# We're not zoomed anyway
|
2009-11-20 05:16:20 +00:00
|
|
|
dbg('Window::unzoom: not zoomed, no-op')
|
2009-11-14 18:56:50 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
widget = self.zoom_data['widget']
|
|
|
|
if self.zoom_data['font_scale']:
|
|
|
|
widget.vte.set_font(self.zoom_data['old_font'])
|
|
|
|
|
|
|
|
self.remove(widget)
|
|
|
|
self.add(self.zoom_data['old_child'])
|
|
|
|
self.zoom_data['old_parent'].add(widget)
|
|
|
|
widget.grab_focus()
|
|
|
|
self.zoom_data = None
|
|
|
|
self.set_property('term_zoomed', False)
|
|
|
|
|
2010-01-19 13:06:09 +00:00
|
|
|
def get_visible_terminals(self):
|
|
|
|
"""Walk down the widget tree to find all of the visible terminals.
|
|
|
|
Mostly using Container::get_visible_terminals()"""
|
|
|
|
maker = Factory()
|
|
|
|
child = self.get_child()
|
|
|
|
terminals = {}
|
|
|
|
|
|
|
|
# If our child is a Notebook, reset to work from its visible child
|
|
|
|
if maker.isinstance(child, 'Notebook'):
|
|
|
|
pagenum = child.get_current_page()
|
|
|
|
child = child.get_nth_page(pagenum)
|
|
|
|
|
|
|
|
if maker.isinstance(child, 'Container'):
|
|
|
|
terminals.update(child.get_visible_terminals())
|
|
|
|
elif maker.isnstance(child, 'Terminal'):
|
|
|
|
# This branch is only possible if we started out as a Notebook. We
|
|
|
|
# wouldn't have been called if there was really only one Terminal
|
|
|
|
# child.
|
|
|
|
terminals[child] = child.get_allocation()
|
|
|
|
else:
|
|
|
|
err('Unknown child type %s' % type(child))
|
|
|
|
|
|
|
|
return(terminals)
|
|
|
|
|
2009-08-10 22:04:39 +00:00
|
|
|
class WindowTitle(object):
|
|
|
|
"""Class to handle the setting of the window title"""
|
|
|
|
|
|
|
|
window = None
|
|
|
|
text = None
|
|
|
|
forced = None
|
|
|
|
|
|
|
|
def __init__(self, window):
|
|
|
|
"""Class initialiser"""
|
|
|
|
self.window = window
|
|
|
|
self.forced = False
|
|
|
|
|
2009-09-02 20:10:28 +00:00
|
|
|
def set_title(self, widget, text):
|
2009-08-10 22:04:39 +00:00
|
|
|
"""Set the title"""
|
|
|
|
if not self.forced:
|
2009-09-02 20:10:28 +00:00
|
|
|
self.text = text
|
2009-08-10 22:04:39 +00:00
|
|
|
self.update()
|
|
|
|
|
|
|
|
def force_title(self, newtext):
|
|
|
|
"""Force a specific title"""
|
|
|
|
if newtext:
|
2010-01-05 12:49:57 +00:00
|
|
|
self.set_title(None, newtext)
|
2009-08-10 22:04:39 +00:00
|
|
|
self.forced = True
|
|
|
|
else:
|
|
|
|
self.forced = False
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
"""Update the title automatically"""
|
|
|
|
title = None
|
|
|
|
|
|
|
|
# FIXME: What the hell is this for?!
|
|
|
|
if self.forced:
|
|
|
|
title = self.text
|
|
|
|
else:
|
|
|
|
title = "%s" % self.text
|
|
|
|
|
|
|
|
self.window.set_title(title)
|
|
|
|
|
2009-08-07 23:31:44 +00:00
|
|
|
# vim: set expandtab ts=4 sw=4:
|