~ubuntu-branches/ubuntu/saucy/terminator/saucy

« back to all changes in this revision

Viewing changes to terminatorlib/titlebar.py

  • Committer: Bazaar Package Importer
  • Author(s): Nicolas Valcárcel Scerpella (Canonical)
  • Date: 2010-04-07 17:10:31 UTC
  • mfrom: (1.1.7 upstream)
  • Revision ID: james.westby@ubuntu.com-20100407171031-35nsuj0tmbub0bj5
Tags: 0.92-0ubuntu1
* New upstream release
* Remove python-xdg from Recommends. (Closes: #567967)
* Downgrade python-gnome2 to Recommends.
* Update python-gtk2 dependency to (>= 2.14.0)
* Add python-keybinder to Recommends

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
# Terminator by Chris Jones <cmsj@tenshu.net>
 
3
# GPL v2 only
 
4
"""titlebar.py - classes necessary to provide a terminal title bar"""
 
5
 
 
6
import gtk
 
7
import gobject
 
8
 
 
9
from version import APP_NAME
 
10
from util import dbg
 
11
from terminator import Terminator
 
12
from editablelabel import EditableLabel
 
13
 
 
14
# pylint: disable-msg=R0904
 
15
# pylint: disable-msg=W0613
 
16
class Titlebar(gtk.EventBox):
 
17
    """Class implementing the Titlebar widget"""
 
18
 
 
19
    terminator = None
 
20
    terminal = None
 
21
    config = None
 
22
    oldtitle = None
 
23
    termtext = None
 
24
    sizetext = None
 
25
    label = None
 
26
    ebox = None
 
27
    groupicon = None
 
28
    grouplabel = None
 
29
    groupentry = None
 
30
    bellicon = None
 
31
 
 
32
    __gsignals__ = {
 
33
            'clicked': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, ()),
 
34
            'edit-done': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, ()),
 
35
            'create-group': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE,
 
36
                (gobject.TYPE_STRING,)),
 
37
    }
 
38
 
 
39
    def __init__(self, terminal):
 
40
        """Class initialiser"""
 
41
        gtk.EventBox.__init__(self)
 
42
        self.__gobject_init__()
 
43
 
 
44
        self.terminator = Terminator()
 
45
        self.terminal = terminal
 
46
        self.config = self.terminal.config
 
47
 
 
48
        self.label = EditableLabel()
 
49
        self.label.connect('edit-done', self.on_edit_done)
 
50
        self.ebox = gtk.EventBox()
 
51
        grouphbox = gtk.HBox()
 
52
        self.grouplabel = gtk.Label()
 
53
        self.groupicon = gtk.Image()
 
54
        self.bellicon = gtk.Image()
 
55
        self.bellicon.set_no_show_all(True)
 
56
 
 
57
        self.groupentry = gtk.Entry()
 
58
        self.groupentry.set_no_show_all(True)
 
59
        self.groupentry.connect('focus-out-event', self.groupentry_cancel)
 
60
        self.groupentry.connect('activate', self.groupentry_activate)
 
61
        self.groupentry.connect('key-press-event', self.groupentry_keypress)
 
62
 
 
63
        groupsend_type = self.terminator.groupsend_type
 
64
        if self.terminator.groupsend == groupsend_type['all']:
 
65
            icon_name = 'all'
 
66
        elif self.terminator.groupsend == groupsend_type['group']:
 
67
            icon_name = 'group'
 
68
        elif self.terminator.groupsend == groupsend_type['off']:
 
69
            icon_name = 'off'
 
70
        self.set_from_icon_name('_active_broadcast_%s' % icon_name, 
 
71
                gtk.ICON_SIZE_MENU)
 
72
 
 
73
        grouphbox.pack_start(self.groupicon, False, True, 2)
 
74
        grouphbox.pack_start(self.grouplabel, False, True, 2)
 
75
        grouphbox.pack_start(self.groupentry, False, True, 2)
 
76
 
 
77
        self.ebox.add(grouphbox)
 
78
        self.ebox.show_all()
 
79
 
 
80
        self.bellicon.set_from_icon_name('terminal-bell', gtk.ICON_SIZE_MENU)
 
81
        hbox = gtk.HBox()
 
82
        hbox.pack_start(self.ebox, False, True, 0)
 
83
        hbox.pack_start(gtk.VSeparator(), False, True, 0)
 
84
        hbox.pack_start(self.label, True, True)
 
85
        hbox.pack_end(self.bellicon, False, False, 2)
 
86
 
 
87
        self.add(hbox)
 
88
        hbox.show_all()
 
89
        self.set_no_show_all(True)
 
90
        self.show()
 
91
 
 
92
        self.connect('button-press-event', self.on_clicked)
 
93
 
 
94
    def connect_icon(self, func):
 
95
        """Connect the supplied function to clicking on the group icon"""
 
96
        self.ebox.connect('button-release-event', func)
 
97
 
 
98
    def update(self, other=None):
 
99
        """Update our contents"""
 
100
        default_bg = False
 
101
        self.label.set_text("%s %s" % (self.termtext, self.sizetext))
 
102
 
 
103
        if other:
 
104
            term = self.terminal
 
105
            terminator = self.terminator
 
106
            if term != other and term.group and term.group == other.group:
 
107
                if terminator.groupsend == terminator.groupsend_type['off']:
 
108
                    title_fg = self.config['title_inactive_fg_color']
 
109
                    title_bg = self.config['title_inactive_bg_color']
 
110
                    icon = '_receive_off'
 
111
                    default_bg = True
 
112
                else:
 
113
                    title_fg = self.config['title_receive_fg_color']
 
114
                    title_bg = self.config['title_receive_bg_color']
 
115
                    icon = '_receive_on'
 
116
                group_fg = self.config['title_receive_fg_color']
 
117
                group_bg = self.config['title_receive_bg_color']
 
118
            elif term != other and not term.group or term.group != other.group:
 
119
                if terminator.groupsend == terminator.groupsend_type['all']:
 
120
                    title_fg = self.config['title_receive_fg_color']
 
121
                    title_bg = self.config['title_receive_bg_color']
 
122
                    icon = '_receive_on'
 
123
                else:
 
124
                    title_fg = self.config['title_inactive_fg_color']
 
125
                    title_bg = self.config['title_inactive_bg_color']
 
126
                    icon = '_receive_off'
 
127
                    default_bg = True
 
128
                group_fg = self.config['title_inactive_fg_color']
 
129
                group_bg = self.config['title_inactive_bg_color']
 
130
            else:
 
131
                title_fg = self.config['title_transmit_fg_color']
 
132
                title_bg = self.config['title_transmit_bg_color']
 
133
                if terminator.groupsend == terminator.groupsend_type['all']:
 
134
                    icon = '_active_broadcast_all'
 
135
                elif terminator.groupsend == terminator.groupsend_type['group']:
 
136
                    icon = '_active_broadcast_group'
 
137
                else:
 
138
                    icon = '_active_broadcast_off'
 
139
                group_fg = self.config['title_transmit_fg_color']
 
140
                group_bg = self.config['title_transmit_bg_color']
 
141
 
 
142
            self.label.modify_fg(gtk.STATE_NORMAL,
 
143
                    gtk.gdk.color_parse(title_fg))
 
144
            self.grouplabel.modify_fg(gtk.STATE_NORMAL,
 
145
                    gtk.gdk.color_parse(group_fg))
 
146
            self.modify_bg(gtk.STATE_NORMAL, 
 
147
                    gtk.gdk.color_parse(title_bg))
 
148
            if not self.get_desired_visibility():
 
149
                if default_bg == True:
 
150
                    color = term.get_style().bg[gtk.STATE_NORMAL]
 
151
                else:
 
152
                    color = gtk.gdk.color_parse(title_bg)
 
153
                self.set_size_request(-1, 2)
 
154
                self.label.hide()
 
155
            else:
 
156
                self.set_size_request(-1, -1)
 
157
                self.label.show()
 
158
            self.ebox.modify_bg(gtk.STATE_NORMAL,
 
159
                    gtk.gdk.color_parse(group_bg))
 
160
            self.set_from_icon_name(icon, gtk.ICON_SIZE_MENU)
 
161
 
 
162
    def get_desired_visibility(self):
 
163
        """Returns True if the titlebar is supposed to be visible. False if
 
164
        not"""
 
165
        if self.editing() == True:
 
166
            return(True)
 
167
        else:
 
168
            return(self.config['show_titlebar'])
 
169
 
 
170
    def set_from_icon_name(self, name, size = gtk.ICON_SIZE_MENU):
 
171
        """Set an icon for the group label"""
 
172
        if not name:
 
173
            self.groupicon.hide()
 
174
            return
 
175
        
 
176
        self.groupicon.set_from_icon_name(APP_NAME + name, size)
 
177
        self.groupicon.show()
 
178
 
 
179
    def update_terminal_size(self, width, height):
 
180
        """Update the displayed terminal size"""
 
181
        self.sizetext = "%sx%s" % (width, height)
 
182
        self.update()
 
183
 
 
184
    def set_terminal_title(self, widget, title):
 
185
        """Update the terminal title"""
 
186
        self.termtext = title
 
187
        self.update()
 
188
        # Return False so we don't interrupt any chains of signal handling
 
189
        return False
 
190
 
 
191
    def set_group_label(self, name):
 
192
        """Set the name of the group"""
 
193
        if name:
 
194
            self.grouplabel.set_text(name)
 
195
            self.grouplabel.show()
 
196
        else:
 
197
            self.grouplabel.hide()
 
198
 
 
199
    def on_clicked(self, widget, event):
 
200
        """Handle a click on the label"""
 
201
        self.set_size_request(-1, -1)
 
202
        self.label.show()
 
203
        self.emit('clicked')
 
204
 
 
205
    def on_edit_done(self, widget):
 
206
        """Re-emit an edit-done signal from an EditableLabel"""
 
207
        self.emit('edit-done')
 
208
 
 
209
    def editing(self):
 
210
        """Determine if we're currently editing a group name or title"""
 
211
        return(self.groupentry.get_property('visible') or self.label.editing())
 
212
 
 
213
    def create_group(self):
 
214
        """Create a new group"""
 
215
        self.groupentry.show()
 
216
        self.groupentry.grab_focus()
 
217
 
 
218
    def groupentry_cancel(self, widget, event):
 
219
        """Hide the group name entry"""
 
220
        self.groupentry.set_text('')
 
221
        self.groupentry.hide()
 
222
        self.get_parent().grab_focus()
 
223
 
 
224
    def groupentry_activate(self, widget):
 
225
        """Actually cause a group to be created"""
 
226
        groupname = self.groupentry.get_text()
 
227
        dbg('Titlebar::groupentry_activate: creating group: %s' % groupname)
 
228
        self.groupentry_cancel(None, None)
 
229
        self.emit('create-group', groupname)
 
230
 
 
231
    def groupentry_keypress(self, widget, event):
 
232
        """Handle keypresses on the entry widget"""
 
233
        key = gtk.gdk.keyval_name(event.keyval)
 
234
        if key == 'Escape':
 
235
            self.groupentry_cancel(None, None)
 
236
 
 
237
    def icon_bell(self):
 
238
        """A bell signal requires we display our bell icon"""
 
239
        self.bellicon.show()
 
240
        gobject.timeout_add(1000, self.icon_bell_hide)
 
241
 
 
242
    def icon_bell_hide(self):
 
243
        """Handle a timeout which means we now hide the bell icon"""
 
244
        self.bellicon.hide()
 
245
        return(False)
 
246
 
 
247
gobject.type_register(Titlebar)