terminator/terminatorlib/titlebar.py

260 lines
9.4 KiB
Python
Raw Normal View History

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"""
import gtk
import gobject
from version import APP_NAME
from util import dbg
from terminator import Terminator
2009-09-02 21:38:27 +00:00
from editablelabel import EditableLabel
# pylint: disable-msg=R0904
2009-09-03 09:34:31 +00:00
# pylint: disable-msg=W0613
2009-08-18 12:45:57 +00:00
class Titlebar(gtk.EventBox):
"""Class implementing the Titlebar widget"""
terminator = None
2010-01-18 23:27:22 +00:00
terminal = None
config = None
oldtitle = None
2009-09-02 21:38:27 +00:00
termtext = None
sizetext = None
label = None
ebox = None
groupicon = None
grouplabel = None
groupentry = None
bellicon = None
__gsignals__ = {
'clicked': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, ()),
'edit-done': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, ()),
'create-group': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE,
(gobject.TYPE_STRING,)),
}
2010-01-18 23:27:22 +00:00
def __init__(self, terminal):
2009-08-18 12:45:57 +00:00
"""Class initialiser"""
gtk.EventBox.__init__(self)
self.__gobject_init__()
2009-08-18 12:45:57 +00:00
self.terminator = Terminator()
2010-01-18 23:27:22 +00:00
self.terminal = terminal
self.config = self.terminal.config
2009-09-02 21:38:27 +00:00
self.label = EditableLabel()
self.label.connect('edit-done', self.on_edit_done)
2009-09-02 21:38:27 +00:00
self.ebox = gtk.EventBox()
grouphbox = gtk.HBox()
2009-09-02 21:38:27 +00:00
self.grouplabel = gtk.Label()
self.groupicon = gtk.Image()
self.bellicon = gtk.Image()
self.bellicon.set_no_show_all(True)
2009-09-02 21:38:27 +00:00
self.groupentry = gtk.Entry()
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)
groupsend_type = self.terminator.groupsend_type
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,
gtk.ICON_SIZE_MENU)
2009-09-02 21:38:27 +00:00
grouphbox.pack_start(self.groupicon, False, True, 2)
grouphbox.pack_start(self.grouplabel, False, True, 2)
grouphbox.pack_start(self.groupentry, False, True, 2)
self.ebox.add(grouphbox)
2009-09-02 21:38:27 +00:00
self.ebox.show_all()
self.bellicon.set_from_icon_name('terminal-bell', gtk.ICON_SIZE_MENU)
hbox = gtk.HBox()
hbox.pack_start(self.ebox, False, True, 0)
hbox.pack_start(gtk.VSeparator(), False, True, 0)
hbox.pack_start(self.label, True, True)
hbox.pack_end(self.bellicon, False, False, 2)
2009-09-02 21:38:27 +00:00
self.add(hbox)
hbox.show_all()
self.set_no_show_all(True)
self.show()
2009-08-18 12:45:57 +00:00
self.connect('button-press-event', self.on_clicked)
def connect_icon(self, func):
"""Connect the supplied function to clicking on the group icon"""
self.ebox.connect('button-release-event', func)
2010-01-18 23:27:22 +00:00
def update(self, other=None):
2009-08-28 00:11:13 +00:00
"""Update our contents"""
default_bg = False
2009-09-02 21:38:27 +00:00
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
if term != other and term.group and term.group == other.group:
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'
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'
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:
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']
self.label.modify_fg(gtk.STATE_NORMAL,
gtk.gdk.color_parse(title_fg))
self.grouplabel.modify_fg(gtk.STATE_NORMAL,
gtk.gdk.color_parse(group_fg))
self.modify_bg(gtk.STATE_NORMAL,
gtk.gdk.color_parse(title_bg))
if not self.get_desired_visibility():
if default_bg == True:
color = term.get_style().bg[gtk.STATE_NORMAL]
else:
color = gtk.gdk.color_parse(title_bg)
self.set_size_request(-1, 2)
self.label.hide()
else:
self.set_size_request(-1, -1)
self.label.show()
2010-01-18 23:27:22 +00:00
self.ebox.modify_bg(gtk.STATE_NORMAL,
gtk.gdk.color_parse(group_bg))
self.set_from_icon_name(icon, gtk.ICON_SIZE_MENU)
2009-08-28 00:11:13 +00:00
def get_desired_visibility(self):
"""Returns True if the titlebar is supposed to be visible. False if
not"""
if self.editing() == True:
return(True)
else:
return(self.config['show_titlebar'])
def set_from_icon_name(self, name, size = gtk.ICON_SIZE_MENU):
"""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()
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-09-02 20:18:36 +00:00
def set_terminal_title(self, widget, title):
"""Update the terminal title"""
2009-09-02 21:38:27 +00:00
self.termtext = title
self.update()
# 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:
self.grouplabel.hide()
def on_clicked(self, widget, event):
"""Handle a click on the label"""
self.set_size_request(-1, -1)
self.label.show()
self.emit('clicked')
def on_edit_done(self, widget):
"""Re-emit an edit-done signal from an EditableLabel"""
self.emit('edit-done')
def editing(self):
"""Determine if we're currently editing a group name or title"""
return(self.groupentry.get_property('visible') or self.label.editing())
def create_group(self):
"""Create a new group"""
self.groupentry.show()
self.groupentry.grab_focus()
def groupentry_cancel(self, widget, event):
"""Hide the group name entry"""
self.groupentry.set_text('')
self.groupentry.hide()
self.get_parent().grab_focus()
def groupentry_activate(self, widget):
"""Actually cause a group to be created"""
groupname = self.groupentry.get_text()
dbg('Titlebar::groupentry_activate: creating group: %s' % groupname)
self.groupentry_cancel(None, None)
self.emit('create-group', groupname)
def groupentry_keypress(self, widget, event):
"""Handle keypresses on the entry widget"""
key = gtk.gdk.keyval_name(event.keyval)
if key == 'Escape':
self.groupentry_cancel(None, None)
def icon_bell(self):
"""A bell signal requires we display our bell icon"""
self.bellicon.show()
gobject.timeout_add(1000, self.icon_bell_hide)
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()
2009-08-18 12:45:57 +00:00
gobject.type_register(Titlebar)