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

« back to all changes in this revision

Viewing changes to plugins/textsize/textsize/__init__.py

  • Committer: Bazaar Package Importer
  • Author(s): Michael Biebl
  • Date: 2011-07-22 20:45:45 UTC
  • mfrom: (1.1.26 upstream)
  • mto: (7.3.1 sid) (1.4.8)
  • mto: This revision was merged to the branch mainline in revision 44.
  • Revision ID: james.westby@ubuntu.com-20110722204545-15t6eui58z5tdb6l
Tags: upstream-3.0.5
ImportĀ upstreamĀ versionĀ 3.0.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
#
 
3
#  __init__.py - Text size plugin
 
4
#
 
5
#  Copyright (C) 2008 - Konstantin Mikhaylov <jtraub.devel@gmail.com>
 
6
#  Copyright (C) 2009 - Wouter Bolsterlee <wbolster@gnome.org>
 
7
#  Copyright (C) 2010 - Ignacio Casal Quinteiro <icq@gnome.org>
 
8
#  Copyright (C) 2010 - Jesse van den Kieboom <jessevdk@gnome.org>
 
9
#
 
10
#  This program is free software; you can redistribute it and/or modify
 
11
#  it under the terms of the GNU General Public License as published by
 
12
#  the Free Software Foundation; either version 2 of the License, or
 
13
#  (at your option) any later version.
 
14
#
 
15
#  This program is distributed in the hope that it will be useful,
 
16
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
17
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
18
#  GNU General Public License for more details.
 
19
#
 
20
#  You should have received a copy of the GNU General Public License
 
21
#  along with this program; if not, write to the Free Software
 
22
#  Foundation, Inc., 59 Temple Place, Suite 330,
 
23
#  Boston, MA 02111-1307, USA.
 
24
 
 
25
from gi.repository import GObject, Gtk, Gdk, Gedit
 
26
import constants
 
27
from documenthelper import DocumentHelper
 
28
import gettext
 
29
from gpdefs import *
 
30
 
 
31
try:
 
32
    gettext.bindtextdomain(GETTEXT_PACKAGE, GP_LOCALEDIR)
 
33
    _ = lambda s: gettext.dgettext(GETTEXT_PACKAGE, s);
 
34
except:
 
35
    _ = lambda s: s
 
36
 
 
37
# UI manager snippet to add menu items to the View menu
 
38
ui_str = """
 
39
<ui>
 
40
  <menubar name="MenuBar">
 
41
    <menu name="ViewMenu" action="View">
 
42
      <placeholder name="ViewOps_2">
 
43
        <menuitem name="IncreaseFontSize" action="IncreaseFontSizeAction"/>
 
44
        <menuitem name="DecreaseFontSize" action="DecreaseFontSizeAction"/>
 
45
        <menuitem name="ResetFontSize" action="ResetFontSizeAction"/>
 
46
      </placeholder>
 
47
    </menu>
 
48
  </menubar>
 
49
</ui>
 
50
"""
 
51
 
 
52
class TextSizePlugin(GObject.Object, Gedit.WindowActivatable):
 
53
    __gtype_name__ = "TextSizePlugin"
 
54
 
 
55
    window = GObject.property(type=Gedit.Window)
 
56
 
 
57
    def __init__(self):
 
58
        GObject.Object.__init__(self)
 
59
 
 
60
    def do_activate(self):
 
61
        self._views  = {}
 
62
 
 
63
        # Insert menu items
 
64
        self._insert_menu()
 
65
 
 
66
        # Insert document helpers
 
67
        for view in self.window.get_views():
 
68
            self.add_document_helper(view)
 
69
 
 
70
        self.window.connect('tab-added', self.on_tab_added)
 
71
        self.window.connect('tab-removed', self.on_tab_removed)
 
72
 
 
73
        self._accel_group = Gtk.AccelGroup()
 
74
        self.window.add_accel_group(self._accel_group)
 
75
 
 
76
        self._proxy_callback_map = {
 
77
            'IncreaseFontSizeAction': self.on_increase_font_accel,
 
78
            'DecreaseFontSizeAction': self.on_decrease_font_accel,
 
79
            'ResetFontSizeAction': self.on_reset_font_accel
 
80
        }
 
81
 
 
82
        self._proxy_mapping = {}
 
83
        self._init_proxy_accels()
 
84
        self._accel_map_handler_id = Gtk.AccelMap.get().connect('changed', self.on_accel_map_changed)
 
85
 
 
86
    def _install_proxy(self, action):
 
87
        if not isinstance(action, Gtk.Action):
 
88
            action = self._action_group.get_action(str(action))
 
89
 
 
90
        if not action:
 
91
            return
 
92
 
 
93
        entry = Gtk.AccelMap.lookup_entry(action.get_accel_path())
 
94
 
 
95
        if not entry:
 
96
            return
 
97
 
 
98
        mapping = {
 
99
            Gdk.KEY_plus: Gdk.KEY_KP_Add,
 
100
            Gdk.KEY_KP_Add: Gdk.KEY_plus,
 
101
            Gdk.KEY_minus: Gdk.KEY_KP_Subtract,
 
102
            Gdk.KEY_KP_Subtract: Gdk.KEY_minus,
 
103
            Gdk.KEY_0: Gdk.KEY_KP_0,
 
104
            Gdk.KEY_KP_0: Gdk.KEY_0
 
105
        }
 
106
 
 
107
        if entry[0] in mapping:
 
108
            key = mapping[entry[0]]
 
109
            mod = entry[1]
 
110
 
 
111
            callback = self._proxy_callback_map[action.get_name()]
 
112
 
 
113
            self._accel_group.connect_group(key, mod, Gtk.ACCEL_LOCKED, callback)
 
114
            self._proxy_mapping[action] = (key, mod)
 
115
 
 
116
    def _init_proxy_accels(self):
 
117
        self._install_proxy('IncreaseFontSizeAction')
 
118
        self._install_proxy('DecreaseFontSizeAction')
 
119
        self._install_proxy('ResetFontSizeAction')
 
120
 
 
121
    def do_deactivate(self):
 
122
        # Remove any installed menu items
 
123
        self._remove_menu()
 
124
 
 
125
        for view in self.window.get_views():
 
126
            self.remove_document_helper(view)
 
127
 
 
128
        self.window.remove_accel_group(self._accel_group)
 
129
 
 
130
        Gtk.AccelMap.get().disconnect(self._accel_map_handler_id)
 
131
 
 
132
        self._accel_group = None
 
133
        self._action_group = None
 
134
 
 
135
    def _insert_menu(self):
 
136
        # Get the GtkUIManager
 
137
        manager = self.window.get_ui_manager()
 
138
 
 
139
        # Create a new action group
 
140
        self._action_group = Gtk.ActionGroup("GeditTextSizePluginActions")
 
141
        self._action_group.add_actions([("IncreaseFontSizeAction", None, _("_Increase font size"),
 
142
                                         "<Ctrl>plus", None,
 
143
                                         self.on_increase_font_size_activate),
 
144
                                         ("DecreaseFontSizeAction", None, _("_Decrease font size"),
 
145
                                         "<Ctrl>minus", None,
 
146
                                         self.on_decrease_font_size_activate),
 
147
                                         ("ResetFontSizeAction", None, _("_Reset font size"),
 
148
                                         "<Ctrl>0", None,
 
149
                                         self.on_reset_font_size_activate)])
 
150
 
 
151
        # Insert the action group
 
152
        manager.insert_action_group(self._action_group)
 
153
 
 
154
        # Merge the UI
 
155
        self._ui_id = manager.add_ui_from_string(ui_str)
 
156
 
 
157
    def _remove_menu(self):
 
158
        # Get the GtkUIManager
 
159
        manager = self.window.get_ui_manager()
 
160
 
 
161
        # Remove the ui
 
162
        manager.remove_ui(self._ui_id)
 
163
 
 
164
        # Remove the action group
 
165
        manager.remove_action_group(self._action_group)
 
166
 
 
167
        # Make sure the manager updates
 
168
        manager.ensure_update()
 
169
 
 
170
    def do_update_state(self):
 
171
        self._action_group.set_sensitive(self.window.get_active_document() != None)
 
172
 
 
173
    def get_helper(self, view):
 
174
        return view.get_data(constants.DOCUMENT_HELPER_KEY)
 
175
 
 
176
    def add_document_helper(self, view):
 
177
        if self.get_helper(view) != None:
 
178
            return
 
179
 
 
180
        DocumentHelper(view)
 
181
 
 
182
    def remove_document_helper(self, view):
 
183
        helper = self.get_helper(view)
 
184
 
 
185
        if helper != None:
 
186
            helper.stop()
 
187
 
 
188
    def call_helper(self, cb):
 
189
        view = self.window.get_active_view()
 
190
 
 
191
        if view:
 
192
            cb(self.get_helper(view))
 
193
 
 
194
    # Menu activate handlers
 
195
    def on_increase_font_size_activate(self, action, user_data=None):
 
196
        self.call_helper(lambda helper: helper.increase_font_size())
 
197
 
 
198
    def on_decrease_font_size_activate(self, action, user_data=None):
 
199
        self.call_helper(lambda helper: helper.decrease_font_size())
 
200
 
 
201
    def on_reset_font_size_activate(self, action, user_data=None):
 
202
        self.call_helper(lambda helper: helper.reset_font_size())
 
203
 
 
204
    def on_increase_font_accel(self, group, accel, key, mod):
 
205
        self.call_helper(lambda helper: helper.increase_font_size())
 
206
 
 
207
    def on_decrease_font_accel(self, group, accel, key, mod):
 
208
        self.call_helper(lambda helper: helper.decrease_font_size())
 
209
 
 
210
    def on_reset_font_accel(self, group, accel, key, mod):
 
211
        self.call_helper(lambda helper: helper.reset_font_size())
 
212
 
 
213
    def on_tab_added(self, window, tab):
 
214
        self.add_document_helper(tab.get_view())
 
215
 
 
216
    def on_tab_removed(self, window, tab):
 
217
        self.remove_document_helper(tab.get_view())
 
218
 
 
219
    def _remap_proxy(self, action):
 
220
        # Remove previous proxy
 
221
 
 
222
        if action in self._proxy_mapping:
 
223
            item = self._proxy_mapping[action]
 
224
            self._accel_group.disconnect_key(item[0], item[1])
 
225
 
 
226
        self._install_proxy(action)
 
227
 
 
228
    def on_accel_map_changed(self, accelmap, path, key, mod):
 
229
        for action in self._action_group.list_actions():
 
230
            if action.get_accel_path() == path:
 
231
                self._remap_proxy(action)
 
232
                return
 
233
 
 
234
# ex:ts=4:et: