__init__.py 2.57 KB
Newer Older
Steve Frécinaux's avatar
Steve Frécinaux committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
# -*- coding: utf-8 -*-

# __init__.py -- plugin object
#
# Copyright (C) 2006 - Steve Frécinaux
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
18
# along with this program; if not, see <http://www.gnu.org/licenses/>.
Steve Frécinaux's avatar
Steve Frécinaux committed
19 20 21 22 23 24 25

# Parts from "Interactive Python-GTK Console" (stolen from epiphany's console.py)
#     Copyright (C), 1998 James Henstridge <james@daa.com.au>
#     Copyright (C), 2005 Adam Hooper <adamh@densi.com>
# Bits from gedit Python Console Plugin
#     Copyrignt (C), 2005 Raphaël Slinckx

26 27 28
import gi
gi.require_version('Gedit', '3.0')
gi.require_version('Peas', '1.0')
29 30
gi.require_version('PeasGtk', '1.0')
gi.require_version('Gtk', '3.0')
31

32
from gi.repository import GObject, Gtk, Gedit, Peas, PeasGtk
33 34
from .console import PythonConsole
from .config import PythonConsoleConfigWidget
Steve Frécinaux's avatar
Steve Frécinaux committed
35

36 37 38 39 40 41 42
try:
    import gettext
    gettext.bindtextdomain('gedit')
    gettext.textdomain('gedit')
    _ = gettext.gettext
except:
    _ = lambda s: s
43

44
class PythonConsolePlugin(GObject.Object, Gedit.WindowActivatable, PeasGtk.Configurable):
45 46
    __gtype_name__ = "PythonConsolePlugin"

47
    window = GObject.Property(type=Gedit.Window)
48

49
    def __init__(self):
50
        GObject.Object.__init__(self)
51

52
    def do_activate(self):
53 54
        self._console = PythonConsole(namespace = {'__builtins__' : __builtins__,
                                                   'gedit' : Gedit,
55
                                                   'window' : self.window})
56 57
        self._console.eval('print("You can access the main window through ' \
                           '\'window\' :\\n%s" % window)', False)
58
        bottom = self.window.get_bottom_panel()
59 60
        self._console.show_all()
        bottom.add_titled(self._console, "GeditPythonConsolePanel", _('Python Console'))
61

62
    def do_deactivate(self):
63
        self._console.stop()
64
        bottom = self.window.get_bottom_panel()
65
        bottom.remove(self._console)
66

67 68 69
    def do_update_state(self):
        pass

70 71
    def do_create_configure_widget(self):
        config_widget = PythonConsoleConfigWidget(self.plugin_info.get_data_dir())
72

73
        return config_widget.configure_widget()
74

75
# ex:et:ts=4: