1# -*- coding: utf-8 -*-
2
3# __init__.py -- plugin object
4#
5# Copyright (C) 2006 - Steve Frécinaux
6#
7# This program is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation; either version 2, or (at your option)
10# any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16#
17# 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/>.
19
20# Parts from "Interactive Python-GTK Console" (stolen from epiphany's console.py)
21#     Copyright (C), 1998 James Henstridge <james@daa.com.au>
22#     Copyright (C), 2005 Adam Hooper <adamh@densi.com>
23# Bits from gedit Python Console Plugin
24#     Copyrignt (C), 2005 Raphaël Slinckx
25
26import gi
27gi.require_version('Gedit', '3.0')
28gi.require_version('Peas', '1.0')
29gi.require_version('PeasGtk', '1.0')
30gi.require_version('Gtk', '3.0')
31
32from gi.repository import GObject, Gtk, Gedit, Peas, PeasGtk
33from .console import PythonConsole
34from .config import PythonConsoleConfigWidget
35
36try:
37    import gettext
38    gettext.bindtextdomain('gedit')
39    gettext.textdomain('gedit')
40    _ = gettext.gettext
41except:
42    _ = lambda s: s
43
44class PythonConsolePlugin(GObject.Object, Gedit.WindowActivatable, PeasGtk.Configurable):
45    __gtype_name__ = "PythonConsolePlugin"
46
47    window = GObject.Property(type=Gedit.Window)
48
49    def __init__(self):
50        GObject.Object.__init__(self)
51
52    def do_activate(self):
53        self._console = PythonConsole(namespace = {'__builtins__' : __builtins__,
54                                                   'gedit' : Gedit,
55                                                   'window' : self.window})
56        self._console.eval('print("You can access the main window through ' \
57                           '\'window\' :\\n%s" % window)', False)
58        bottom = self.window.get_bottom_panel()
59        self._console.show_all()
60        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    def do_update_state(self):
68        pass
69
70    def do_create_configure_widget(self):
71        config_widget = PythonConsoleConfigWidget(self.plugin_info.get_data_dir())
72
73        return config_widget.configure_widget()
74
75# ex:et:ts=4:
76