~ubuntu-branches/ubuntu/vivid/gedit-plugins/vivid-proposed

« back to all changes in this revision

Viewing changes to plugins/terminal/terminal.py

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Holbach
  • Date: 2006-07-10 11:50:19 UTC
  • Revision ID: james.westby@ubuntu.com-20060710115019-ohx55807af5f36bq
Tags: upstream-2.15.3
ImportĀ upstreamĀ versionĀ 2.15.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf8 -*-
 
2
 
 
3
# terminal.py - Embeded VTE terminal for gedit
 
4
# This file is part of gedit
 
5
#
 
6
# Copyright (C) 2005-2006 - Paolo Borelli
 
7
#
 
8
# gedit is free software; you can redistribute it and/or modify
 
9
# it under the terms of the GNU General Public License as published by
 
10
# the Free Software Foundation; either version 2 of the License, or
 
11
# (at your option) any later version.
 
12
#
 
13
# gedit is distributed in the hope that it will be useful,
 
14
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
# GNU General Public License for more details.
 
17
#
 
18
# You should have received a copy of the GNU General Public License
 
19
# along with gedit; if not, write to the Free Software
 
20
# Foundation, Inc., 51 Franklin St, Fifth Floor, 
 
21
# Boston, MA  02110-1301  USA
 
22
 
 
23
import gedit
 
24
import pango
 
25
import gtk
 
26
import vte
 
27
import gconf
 
28
 
 
29
class GeditTerminal(gtk.HBox):
 
30
    """VTE terminal which follows gnome-terminal default profile options"""
 
31
 
 
32
    GCONF_PROFILE_DIR = "/apps/gnome-terminal/profiles/Default"
 
33
    
 
34
    defaults = {
 
35
        'allow_bold'            : True,
 
36
        'audible_bell'          : False,
 
37
        'background'            : None,
 
38
        'background_color'      : '#000000',
 
39
        'backspace_binding'     : 'ascii-del',
 
40
        'cursor_blinks'         : False,
 
41
        'emulation'             : 'xterm',
 
42
        'font_name'             : 'Monospace 10',
 
43
        'foreground_color'      : '#AAAAAA',
 
44
        'scroll_on_keystroke'   : False,
 
45
        'scroll_on_output'      : False,
 
46
        'scrollback_lines'      : 100,
 
47
        'visible_bell'          : False
 
48
    }
 
49
 
 
50
    def __init__(self):
 
51
        gtk.HBox.__init__(self, False, 4)
 
52
 
 
53
        gconf_client.add_dir(self.GCONF_PROFILE_DIR,
 
54
                             gconf.CLIENT_PRELOAD_RECURSIVE)
 
55
 
 
56
        self._vte = vte.Terminal()
 
57
        self.reconfigure_vte()
 
58
        self._vte.set_size(30, 5)
 
59
        self._vte.set_size_request(200, 50)
 
60
        self._vte.show()
 
61
        self.pack_start(self._vte)
 
62
        
 
63
        self._scrollbar = gtk.VScrollbar(self._vte.get_adjustment())
 
64
        self._scrollbar.show()
 
65
        self.pack_start(self._scrollbar, False, False, 0)
 
66
        
 
67
        gconf_client.notify_add(self.GCONF_PROFILE_DIR,
 
68
                                self.on_gconf_notification)
 
69
        
 
70
        self._vte.connect("button-press-event", self.on_vte_button_press)
 
71
        self._vte.connect("popup-menu", self.on_vte_popup_menu)
 
72
        self._vte.connect("child-exited", lambda term: term.fork_command())
 
73
 
 
74
        self._vte.fork_command()
 
75
 
 
76
    def reconfigure_vte(self):
 
77
        # Fonts
 
78
        if gconf_get_bool(self.GCONF_PROFILE_DIR + "/use_system_font"):
 
79
            font_name = gconf_get_str("/desktop/gnome/interface/monospace_font",
 
80
                                      self.defaults['font_name'])
 
81
        else:
 
82
            font_name = gconf_get_str(self.GCONF_PROFILE_DIR + "/font",
 
83
                                      self.defaults['font_name'])
 
84
 
 
85
        try:
 
86
            self._vte.set_font(pango.FontDescription(font_name))
 
87
        except:
 
88
            pass
 
89
 
 
90
        # colors
 
91
        fg_color = gconf_get_str(self.GCONF_PROFILE_DIR + "/foreground_color",
 
92
                                 self.defaults['foreground_color'])
 
93
        bg_color = gconf_get_str(self.GCONF_PROFILE_DIR + "/background_color",
 
94
                                 self.defaults['background_color'])
 
95
        self._vte.set_colors(gtk.gdk.color_parse (fg_color),
 
96
                             gtk.gdk.color_parse (bg_color),
 
97
                             [])
 
98
 
 
99
        self._vte.set_cursor_blinks(gconf_get_bool(self.GCONF_PROFILE_DIR + "/cursor_blinks",
 
100
                                                   self.defaults['cursor_blinks']))
 
101
 
 
102
        self._vte.set_audible_bell(not gconf_get_bool(self.GCONF_PROFILE_DIR + "/silent_bell",
 
103
                                                      not self.defaults['audible_bell']))
 
104
 
 
105
        self._vte.set_scrollback_lines(gconf_get_int(self.GCONF_PROFILE_DIR + "/scrollback_lines",
 
106
                                                     self.defaults['scrollback_lines']))
 
107
        
 
108
        self._vte.set_allow_bold(gconf_get_bool(self.GCONF_PROFILE_DIR + "/allow_bold",
 
109
                                                self.defaults['allow_bold']))
 
110
 
 
111
        self._vte.set_scroll_on_keystroke(gconf_get_bool(self.GCONF_PROFILE_DIR + "/scroll_on_keystroke",
 
112
                                                         self.defaults['scroll_on_keystroke']))
 
113
 
 
114
        self._vte.set_scroll_on_output(gconf_get_bool(self.GCONF_PROFILE_DIR + "/scroll_on_output",
 
115
                                                      self.defaults['scroll_on_output']))
 
116
 
 
117
        self._vte.set_emulation(self.defaults['emulation'])
 
118
        self._vte.set_visible_bell(self.defaults['visible_bell'])
 
119
 
 
120
    def on_gconf_notification(self, client, cnxn_id, entry, what):
 
121
        self.reconfigure_vte()
 
122
 
 
123
    def on_vte_button_press(self, term, event):
 
124
        if event.button == 3:
 
125
            self.do_popup(event)
 
126
            return True
 
127
 
 
128
    def on_vte_popup_menu(self, term):
 
129
        self.do_popup()
 
130
 
 
131
    def create_popup_menu(self):
 
132
        menu = gtk.Menu()
 
133
 
 
134
        item = gtk.ImageMenuItem(gtk.STOCK_COPY)
 
135
        item.connect("activate", lambda menu_item: self._vte.copy_clipboard())
 
136
        menu.append(item)
 
137
 
 
138
        item = gtk.ImageMenuItem(gtk.STOCK_PASTE)
 
139
        item.connect("activate", lambda menu_item: self._vte.paste_clipboard())
 
140
        menu.append(item)
 
141
        
 
142
        menu.show_all()
 
143
        return menu
 
144
 
 
145
    def do_popup(self, event = None):
 
146
        menu = self.create_popup_menu()
 
147
   
 
148
        if event is not None:
 
149
                menu.popup(None, None, None, event.button, event.time)
 
150
        else:
 
151
            menu.popup(None, None,
 
152
                       lambda m: gedit.utils.menu_position_under_widget(m, self),
 
153
                       0, gtk.get_current_event_time())
 
154
            menu.select_first(False)        
 
155
 
 
156
class TerminalWindowHelper(object):
 
157
    def __init__(self, window):
 
158
        self._window = window
 
159
 
 
160
        self._panel = GeditTerminal()
 
161
        self._panel.show()
 
162
 
 
163
        image = gtk.Image()
 
164
        image.set_from_icon_name("utilities-terminal", gtk.ICON_SIZE_MENU)
 
165
 
 
166
        bottom = window.get_bottom_panel()
 
167
        bottom.add_item(self._panel, "Terminal", image)
 
168
 
 
169
    def deactivate(self):
 
170
        bottom = window.get_bottom_panel()
 
171
        bottom.remove_item(self._panel)
 
172
    
 
173
    def update_ui(self):
 
174
        pass
 
175
 
 
176
class TerminalPlugin(gedit.Plugin):
 
177
    WINDOW_DATA_KEY = "TerminalPluginWindowData"
 
178
 
 
179
    def __init__(self):
 
180
        gedit.Plugin.__init__(self)
 
181
 
 
182
    def activate(self, window):
 
183
        helper = TerminalWindowHelper(window)
 
184
        window.set_data(self.WINDOW_DATA_KEY, helper)
 
185
 
 
186
    def deactivate(self, window):
 
187
        window.get_data(self.WINDOW_DATA_KEY).deactivate()
 
188
        window.set_data(self.WINDOW_DATA_KEY, None)
 
189
 
 
190
    def update_ui(self, window):
 
191
        window.get_data(self.WINDOW_DATA_KEY).update_ui()
 
192
 
 
193
gconf_client = gconf.client_get_default()
 
194
def gconf_get_bool(key, default = False):
 
195
    val = gconf_client.get(key)
 
196
    
 
197
    if val is not None and val.type == gconf.VALUE_BOOL:
 
198
        return val.get_bool()
 
199
    else:
 
200
        return default
 
201
 
 
202
def gconf_get_str(key, default = ""):
 
203
    val = gconf_client.get(key)
 
204
    
 
205
    if val is not None and val.type == gconf.VALUE_STRING:
 
206
        return val.get_string()
 
207
    else:
 
208
        return default
 
209
 
 
210
def gconf_get_int(key, default = 0):
 
211
    val = gconf_client.get(key)
 
212
    
 
213
    if val is not None and val.type == gconf.VALUE_INT:
 
214
        return val.get_int()
 
215
    else:
 
216
        return default
 
217
 
 
218
 
 
219
# ex:ts=4:et: Let's conform to PEP8