2009-08-18 12:45:57 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
# Terminator by Chris Jones <cmsj@tenshu.net>
|
|
|
|
# GPL v2 only
|
|
|
|
"""titlebar.py - classes necessary to provide a terminal title bar"""
|
|
|
|
|
2014-09-19 14:10:43 +00:00
|
|
|
from gi.repository import Gtk, Gdk
|
2014-09-19 14:08:08 +00:00
|
|
|
from gi.repository import GObject
|
2012-09-27 12:56:42 +00:00
|
|
|
import random
|
2012-10-24 13:19:58 +00:00
|
|
|
import itertools
|
2009-08-18 12:45:57 +00:00
|
|
|
|
2009-09-02 22:17:54 +00:00
|
|
|
from version import APP_NAME
|
2009-10-15 13:17:37 +00:00
|
|
|
from util import dbg
|
2010-01-11 20:06:53 +00:00
|
|
|
from terminator import Terminator
|
2009-09-02 21:38:27 +00:00
|
|
|
from editablelabel import EditableLabel
|
|
|
|
|
2009-08-18 12:52:02 +00:00
|
|
|
# pylint: disable-msg=R0904
|
2009-09-03 09:34:31 +00:00
|
|
|
# pylint: disable-msg=W0613
|
2014-09-19 14:08:08 +00:00
|
|
|
class Titlebar(Gtk.EventBox):
|
2009-08-18 12:45:57 +00:00
|
|
|
"""Class implementing the Titlebar widget"""
|
|
|
|
|
2009-09-02 22:17:54 +00:00
|
|
|
terminator = None
|
2010-01-18 23:27:22 +00:00
|
|
|
terminal = None
|
|
|
|
config = None
|
2009-09-02 20:10:28 +00:00
|
|
|
oldtitle = None
|
2009-09-02 21:38:27 +00:00
|
|
|
termtext = None
|
|
|
|
sizetext = None
|
|
|
|
label = None
|
|
|
|
ebox = None
|
|
|
|
groupicon = None
|
|
|
|
grouplabel = None
|
2009-10-15 13:17:37 +00:00
|
|
|
groupentry = None
|
2010-02-11 13:05:12 +00:00
|
|
|
bellicon = None
|
2009-09-02 20:10:28 +00:00
|
|
|
|
2009-09-02 23:52:36 +00:00
|
|
|
__gsignals__ = {
|
2014-09-19 14:08:08 +00:00
|
|
|
'clicked': (GObject.SignalFlags.RUN_LAST, None, ()),
|
|
|
|
'edit-done': (GObject.SignalFlags.RUN_LAST, None, ()),
|
|
|
|
'create-group': (GObject.SignalFlags.RUN_LAST, None,
|
|
|
|
(GObject.TYPE_STRING,)),
|
2009-09-02 23:52:36 +00:00
|
|
|
}
|
|
|
|
|
2010-01-18 23:27:22 +00:00
|
|
|
def __init__(self, terminal):
|
2009-08-18 12:45:57 +00:00
|
|
|
"""Class initialiser"""
|
2014-09-19 14:08:08 +00:00
|
|
|
GObject.GObject.__init__(self)
|
2009-08-18 12:45:57 +00:00
|
|
|
|
2009-09-02 22:17:54 +00:00
|
|
|
self.terminator = Terminator()
|
2010-01-18 23:27:22 +00:00
|
|
|
self.terminal = terminal
|
|
|
|
self.config = self.terminal.config
|
2009-09-02 22:17:54 +00:00
|
|
|
|
2009-09-02 21:38:27 +00:00
|
|
|
self.label = EditableLabel()
|
2009-09-02 23:52:36 +00:00
|
|
|
self.label.connect('edit-done', self.on_edit_done)
|
2014-09-19 14:08:08 +00:00
|
|
|
self.ebox = Gtk.EventBox()
|
|
|
|
grouphbox = Gtk.HBox()
|
2015-02-19 14:30:45 +00:00
|
|
|
self.grouplabel = Gtk.Label(ellipsize='end')
|
2014-09-19 14:08:08 +00:00
|
|
|
self.groupicon = Gtk.Image()
|
|
|
|
self.bellicon = Gtk.Image()
|
2010-02-11 13:05:12 +00:00
|
|
|
self.bellicon.set_no_show_all(True)
|
2009-09-02 21:38:27 +00:00
|
|
|
|
2014-09-19 14:08:08 +00:00
|
|
|
self.groupentry = Gtk.Entry()
|
2009-10-15 13:17:37 +00:00
|
|
|
self.groupentry.set_no_show_all(True)
|
|
|
|
self.groupentry.connect('focus-out-event', self.groupentry_cancel)
|
|
|
|
self.groupentry.connect('activate', self.groupentry_activate)
|
|
|
|
self.groupentry.connect('key-press-event', self.groupentry_keypress)
|
|
|
|
|
2009-09-03 12:59:17 +00:00
|
|
|
groupsend_type = self.terminator.groupsend_type
|
2009-09-02 22:17:54 +00:00
|
|
|
if self.terminator.groupsend == groupsend_type['all']:
|
|
|
|
icon_name = 'all'
|
|
|
|
elif self.terminator.groupsend == groupsend_type['group']:
|
|
|
|
icon_name = 'group'
|
|
|
|
elif self.terminator.groupsend == groupsend_type['off']:
|
|
|
|
icon_name = 'off'
|
|
|
|
self.set_from_icon_name('_active_broadcast_%s' % icon_name,
|
2014-09-19 14:08:08 +00:00
|
|
|
Gtk.IconSize.MENU)
|
2009-09-02 21:38:27 +00:00
|
|
|
|
2009-09-06 22:53:21 +00:00
|
|
|
grouphbox.pack_start(self.groupicon, False, True, 2)
|
|
|
|
grouphbox.pack_start(self.grouplabel, False, True, 2)
|
2009-10-15 13:17:37 +00:00
|
|
|
grouphbox.pack_start(self.groupentry, False, True, 2)
|
|
|
|
|
2009-09-06 22:53:21 +00:00
|
|
|
self.ebox.add(grouphbox)
|
2009-09-02 21:38:27 +00:00
|
|
|
self.ebox.show_all()
|
|
|
|
|
2014-09-19 14:08:08 +00:00
|
|
|
self.bellicon.set_from_icon_name('terminal-bell', Gtk.IconSize.MENU)
|
|
|
|
hbox = Gtk.HBox()
|
2009-09-06 22:53:21 +00:00
|
|
|
hbox.pack_start(self.ebox, False, True, 0)
|
2014-09-19 14:10:43 +00:00
|
|
|
hbox.pack_start(Gtk.VSeparator(), False, True, 0)
|
|
|
|
hbox.pack_start(self.label, True, True, 0)
|
2010-02-11 13:05:12 +00:00
|
|
|
hbox.pack_end(self.bellicon, False, False, 2)
|
2009-09-02 21:38:27 +00:00
|
|
|
|
2015-02-19 14:30:45 +00:00
|
|
|
viewport = Gtk.Viewport(hscroll_policy='natural')
|
|
|
|
viewport.add(hbox)
|
|
|
|
self.add (viewport)
|
|
|
|
viewport.show_all()
|
2010-02-09 13:08:07 +00:00
|
|
|
self.set_no_show_all(True)
|
2010-03-23 20:48:04 +00:00
|
|
|
self.show()
|
2009-08-18 12:45:57 +00:00
|
|
|
|
2009-09-02 23:52:36 +00:00
|
|
|
self.connect('button-press-event', self.on_clicked)
|
|
|
|
|
2009-08-19 00:05:44 +00:00
|
|
|
def connect_icon(self, func):
|
|
|
|
"""Connect the supplied function to clicking on the group icon"""
|
2012-06-24 18:36:39 +00:00
|
|
|
self.ebox.connect('button-press-event', func)
|
2009-08-19 00:05:44 +00:00
|
|
|
|
2010-01-18 23:27:22 +00:00
|
|
|
def update(self, other=None):
|
2009-08-28 00:11:13 +00:00
|
|
|
"""Update our contents"""
|
2010-02-09 00:23:21 +00:00
|
|
|
default_bg = False
|
2011-08-21 00:07:11 +00:00
|
|
|
if self.config['title_hide_sizetext']:
|
|
|
|
self.label.set_text("%s" % self.termtext)
|
|
|
|
else:
|
|
|
|
self.label.set_text("%s %s" % (self.termtext, self.sizetext))
|
2010-01-18 23:27:22 +00:00
|
|
|
|
|
|
|
if other:
|
|
|
|
term = self.terminal
|
|
|
|
terminator = self.terminator
|
2010-08-24 19:44:29 +00:00
|
|
|
if other == 'window-focus-out':
|
|
|
|
title_fg = self.config['title_inactive_fg_color']
|
|
|
|
title_bg = self.config['title_inactive_bg_color']
|
|
|
|
icon = '_receive_off'
|
|
|
|
default_bg = True
|
|
|
|
group_fg = self.config['title_inactive_fg_color']
|
|
|
|
group_bg = self.config['title_inactive_bg_color']
|
|
|
|
elif term != other and term.group and term.group == other.group:
|
2010-01-18 23:27:22 +00:00
|
|
|
if terminator.groupsend == terminator.groupsend_type['off']:
|
|
|
|
title_fg = self.config['title_inactive_fg_color']
|
|
|
|
title_bg = self.config['title_inactive_bg_color']
|
|
|
|
icon = '_receive_off'
|
2010-02-09 00:23:21 +00:00
|
|
|
default_bg = True
|
2010-01-18 23:27:22 +00:00
|
|
|
else:
|
|
|
|
title_fg = self.config['title_receive_fg_color']
|
|
|
|
title_bg = self.config['title_receive_bg_color']
|
|
|
|
icon = '_receive_on'
|
|
|
|
group_fg = self.config['title_receive_fg_color']
|
|
|
|
group_bg = self.config['title_receive_bg_color']
|
|
|
|
elif term != other and not term.group or term.group != other.group:
|
|
|
|
if terminator.groupsend == terminator.groupsend_type['all']:
|
|
|
|
title_fg = self.config['title_receive_fg_color']
|
|
|
|
title_bg = self.config['title_receive_bg_color']
|
|
|
|
icon = '_receive_on'
|
|
|
|
else:
|
|
|
|
title_fg = self.config['title_inactive_fg_color']
|
|
|
|
title_bg = self.config['title_inactive_bg_color']
|
|
|
|
icon = '_receive_off'
|
2010-02-09 00:23:21 +00:00
|
|
|
default_bg = True
|
2010-01-18 23:27:22 +00:00
|
|
|
group_fg = self.config['title_inactive_fg_color']
|
|
|
|
group_bg = self.config['title_inactive_bg_color']
|
|
|
|
else:
|
2010-07-03 19:00:04 +00:00
|
|
|
# We're the active terminal
|
2010-01-18 23:27:22 +00:00
|
|
|
title_fg = self.config['title_transmit_fg_color']
|
|
|
|
title_bg = self.config['title_transmit_bg_color']
|
|
|
|
if terminator.groupsend == terminator.groupsend_type['all']:
|
|
|
|
icon = '_active_broadcast_all'
|
|
|
|
elif terminator.groupsend == terminator.groupsend_type['group']:
|
|
|
|
icon = '_active_broadcast_group'
|
|
|
|
else:
|
|
|
|
icon = '_active_broadcast_off'
|
|
|
|
group_fg = self.config['title_transmit_fg_color']
|
|
|
|
group_bg = self.config['title_transmit_bg_color']
|
|
|
|
|
2014-09-19 14:08:08 +00:00
|
|
|
self.label.modify_fg(Gtk.StateType.NORMAL,
|
|
|
|
Gdk.color_parse(title_fg))
|
|
|
|
self.grouplabel.modify_fg(Gtk.StateType.NORMAL,
|
|
|
|
Gdk.color_parse(group_fg))
|
|
|
|
self.modify_bg(Gtk.StateType.NORMAL,
|
|
|
|
Gdk.color_parse(title_bg))
|
2010-03-23 20:48:04 +00:00
|
|
|
if not self.get_desired_visibility():
|
2010-02-09 00:23:21 +00:00
|
|
|
if default_bg == True:
|
2014-09-19 14:10:43 +00:00
|
|
|
color = term.get_style_context().get_background_color(Gtk.StateType.NORMAL) # VERIFY FOR GTK3
|
2010-02-09 00:23:21 +00:00
|
|
|
else:
|
2014-09-19 14:08:08 +00:00
|
|
|
color = Gdk.color_parse(title_bg)
|
2010-05-15 13:30:13 +00:00
|
|
|
self.update_visibility()
|
2014-09-19 14:08:08 +00:00
|
|
|
self.ebox.modify_bg(Gtk.StateType.NORMAL,
|
|
|
|
Gdk.color_parse(group_bg))
|
|
|
|
self.set_from_icon_name(icon, Gtk.IconSize.MENU)
|
2009-08-28 00:11:13 +00:00
|
|
|
|
2010-05-15 13:30:13 +00:00
|
|
|
def update_visibility(self):
|
|
|
|
"""Make the titlebar be visible or not"""
|
|
|
|
if not self.get_desired_visibility():
|
|
|
|
dbg('hiding titlebar')
|
|
|
|
self.hide()
|
|
|
|
self.label.hide()
|
|
|
|
else:
|
|
|
|
dbg('showing titlebar')
|
|
|
|
self.show()
|
|
|
|
self.label.show()
|
|
|
|
|
2010-03-23 20:48:04 +00:00
|
|
|
def get_desired_visibility(self):
|
|
|
|
"""Returns True if the titlebar is supposed to be visible. False if
|
|
|
|
not"""
|
2010-05-15 13:30:13 +00:00
|
|
|
if self.editing() == True or self.terminal.group:
|
|
|
|
dbg('implicit desired visibility')
|
2010-03-23 20:48:04 +00:00
|
|
|
return(True)
|
|
|
|
else:
|
2010-05-15 13:30:13 +00:00
|
|
|
dbg('configured visibility: %s' % self.config['show_titlebar'])
|
2010-03-23 20:48:04 +00:00
|
|
|
return(self.config['show_titlebar'])
|
|
|
|
|
2014-09-19 14:08:08 +00:00
|
|
|
def set_from_icon_name(self, name, size = Gtk.IconSize.MENU):
|
2009-09-02 22:17:54 +00:00
|
|
|
"""Set an icon for the group label"""
|
|
|
|
if not name:
|
|
|
|
self.groupicon.hide()
|
|
|
|
return
|
|
|
|
|
|
|
|
self.groupicon.set_from_icon_name(APP_NAME + name, size)
|
|
|
|
self.groupicon.show()
|
|
|
|
|
2009-08-19 18:01:11 +00:00
|
|
|
def update_terminal_size(self, width, height):
|
|
|
|
"""Update the displayed terminal size"""
|
2009-09-02 21:38:27 +00:00
|
|
|
self.sizetext = "%sx%s" % (width, height)
|
|
|
|
self.update()
|
2009-08-19 18:01:11 +00:00
|
|
|
|
2009-09-02 20:18:36 +00:00
|
|
|
def set_terminal_title(self, widget, title):
|
2009-09-02 20:10:28 +00:00
|
|
|
"""Update the terminal title"""
|
2009-09-02 21:38:27 +00:00
|
|
|
self.termtext = title
|
|
|
|
self.update()
|
2009-12-10 13:20:03 +00:00
|
|
|
# Return False so we don't interrupt any chains of signal handling
|
|
|
|
return False
|
2009-09-02 21:38:27 +00:00
|
|
|
|
|
|
|
def set_group_label(self, name):
|
|
|
|
"""Set the name of the group"""
|
|
|
|
if name:
|
|
|
|
self.grouplabel.set_text(name)
|
|
|
|
self.grouplabel.show()
|
|
|
|
else:
|
2012-10-24 17:29:19 +00:00
|
|
|
self.grouplabel.set_text('')
|
2009-09-02 21:38:27 +00:00
|
|
|
self.grouplabel.hide()
|
2010-05-15 13:30:13 +00:00
|
|
|
self.update_visibility()
|
2009-09-02 21:38:27 +00:00
|
|
|
|
|
|
|
def on_clicked(self, widget, event):
|
|
|
|
"""Handle a click on the label"""
|
2010-05-15 13:30:13 +00:00
|
|
|
self.show()
|
2010-03-23 20:48:04 +00:00
|
|
|
self.label.show()
|
2009-09-02 23:52:36 +00:00
|
|
|
self.emit('clicked')
|
|
|
|
|
|
|
|
def on_edit_done(self, widget):
|
|
|
|
"""Re-emit an edit-done signal from an EditableLabel"""
|
|
|
|
self.emit('edit-done')
|
2009-09-02 20:10:28 +00:00
|
|
|
|
2010-01-12 01:05:53 +00:00
|
|
|
def editing(self):
|
|
|
|
"""Determine if we're currently editing a group name or title"""
|
|
|
|
return(self.groupentry.get_property('visible') or self.label.editing())
|
2010-01-12 00:58:50 +00:00
|
|
|
|
2009-10-15 13:17:37 +00:00
|
|
|
def create_group(self):
|
|
|
|
"""Create a new group"""
|
2012-10-22 12:25:37 +00:00
|
|
|
if self.terminal.group:
|
|
|
|
self.groupentry.set_text(self.terminal.group)
|
|
|
|
else:
|
2012-10-24 13:19:58 +00:00
|
|
|
defaultmembers=['Alpha','Beta','Gamma','Delta','Epsilon','Zeta','Eta',
|
|
|
|
'Theta','Iota','Kappa','Lambda','Mu','Nu','Xi',
|
2014-05-08 17:27:39 +00:00
|
|
|
'Omicron','Pi','Rho','Sigma','Tau','Upsilon','Phi',
|
2012-10-24 13:19:58 +00:00
|
|
|
'Chi','Psi','Omega']
|
2012-10-22 12:25:37 +00:00
|
|
|
currentgroups=set(self.terminator.groups)
|
2012-10-24 13:19:58 +00:00
|
|
|
for i in range(1,4):
|
|
|
|
defaultgroups=set(map(''.join, list(itertools.product(defaultmembers,repeat=i))))
|
|
|
|
freegroups = list(defaultgroups-currentgroups)
|
|
|
|
if freegroups:
|
2012-10-24 16:49:33 +00:00
|
|
|
self.groupentry.set_text(random.choice(freegroups))
|
2012-10-24 13:19:58 +00:00
|
|
|
break
|
|
|
|
else:
|
|
|
|
self.groupentry.set_text('')
|
2009-10-15 13:17:37 +00:00
|
|
|
self.groupentry.show()
|
2012-06-24 18:22:21 +00:00
|
|
|
self.grouplabel.hide()
|
2009-10-15 13:17:37 +00:00
|
|
|
self.groupentry.grab_focus()
|
2010-05-15 13:30:13 +00:00
|
|
|
self.update_visibility()
|
2009-10-15 13:17:37 +00:00
|
|
|
|
|
|
|
def groupentry_cancel(self, widget, event):
|
|
|
|
"""Hide the group name entry"""
|
|
|
|
self.groupentry.set_text('')
|
|
|
|
self.groupentry.hide()
|
2012-06-24 18:22:21 +00:00
|
|
|
self.grouplabel.show()
|
2009-11-20 05:42:49 +00:00
|
|
|
self.get_parent().grab_focus()
|
2009-10-15 13:17:37 +00:00
|
|
|
|
|
|
|
def groupentry_activate(self, widget):
|
|
|
|
"""Actually cause a group to be created"""
|
2012-10-24 13:21:53 +00:00
|
|
|
groupname = self.groupentry.get_text() or None
|
2009-11-20 05:40:31 +00:00
|
|
|
dbg('Titlebar::groupentry_activate: creating group: %s' % groupname)
|
2009-10-15 13:17:37 +00:00
|
|
|
self.groupentry_cancel(None, None)
|
2012-06-24 18:36:39 +00:00
|
|
|
last_focused_term=self.terminator.last_focused_term
|
|
|
|
if self.terminal.targets_for_new_group:
|
|
|
|
[term.titlebar.emit('create-group', groupname) for term in self.terminal.targets_for_new_group]
|
|
|
|
self.terminal.targets_for_new_group = None
|
|
|
|
else:
|
|
|
|
self.emit('create-group', groupname)
|
|
|
|
last_focused_term.grab_focus()
|
|
|
|
self.terminator.focus_changed(last_focused_term)
|
2009-10-15 13:17:37 +00:00
|
|
|
|
|
|
|
def groupentry_keypress(self, widget, event):
|
|
|
|
"""Handle keypresses on the entry widget"""
|
2014-09-19 14:08:08 +00:00
|
|
|
key = Gdk.keyval_name(event.keyval)
|
2009-10-15 13:17:37 +00:00
|
|
|
if key == 'Escape':
|
|
|
|
self.groupentry_cancel(None, None)
|
|
|
|
|
2010-02-11 13:05:12 +00:00
|
|
|
def icon_bell(self):
|
|
|
|
"""A bell signal requires we display our bell icon"""
|
|
|
|
self.bellicon.show()
|
2014-09-19 14:08:08 +00:00
|
|
|
GObject.timeout_add(1000, self.icon_bell_hide)
|
2010-02-11 13:05:12 +00:00
|
|
|
|
|
|
|
def icon_bell_hide(self):
|
|
|
|
"""Handle a timeout which means we now hide the bell icon"""
|
|
|
|
self.bellicon.hide()
|
|
|
|
return(False)
|
|
|
|
|
2010-04-22 22:14:03 +00:00
|
|
|
def get_custom_string(self):
|
|
|
|
"""If we have a custom string set, return it, otherwise None"""
|
|
|
|
if self.label.is_custom():
|
|
|
|
return(self.label.get_text())
|
|
|
|
else:
|
|
|
|
return(None)
|
|
|
|
|
|
|
|
def set_custom_string(self, string):
|
|
|
|
"""Set a custom string"""
|
|
|
|
self.label.set_text(string)
|
|
|
|
self.label.set_custom()
|
|
|
|
|
2014-09-19 14:08:08 +00:00
|
|
|
GObject.type_register(Titlebar)
|