1
# -*- coding: utf-8 -*-
3
# __init__.py - Text size plugin
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>
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.
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.
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.
25
from gi.repository import GObject, Gtk, Gdk, Gedit
27
from documenthelper import DocumentHelper
32
gettext.bindtextdomain(GETTEXT_PACKAGE, GP_LOCALEDIR)
33
_ = lambda s: gettext.dgettext(GETTEXT_PACKAGE, s);
37
# UI manager snippet to add menu items to the View menu
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"/>
52
class TextSizePlugin(GObject.Object, Gedit.WindowActivatable):
53
__gtype_name__ = "TextSizePlugin"
55
window = GObject.property(type=Gedit.Window)
58
GObject.Object.__init__(self)
60
def do_activate(self):
66
# Insert document helpers
67
for view in self.window.get_views():
68
self.add_document_helper(view)
70
self.window.connect('tab-added', self.on_tab_added)
71
self.window.connect('tab-removed', self.on_tab_removed)
73
self._accel_group = Gtk.AccelGroup()
74
self.window.add_accel_group(self._accel_group)
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
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)
86
def _install_proxy(self, action):
87
if not isinstance(action, Gtk.Action):
88
action = self._action_group.get_action(str(action))
93
entry = Gtk.AccelMap.lookup_entry(action.get_accel_path())
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
107
if entry[0] in mapping:
108
key = mapping[entry[0]]
111
callback = self._proxy_callback_map[action.get_name()]
113
self._accel_group.connect_group(key, mod, Gtk.ACCEL_LOCKED, callback)
114
self._proxy_mapping[action] = (key, mod)
116
def _init_proxy_accels(self):
117
self._install_proxy('IncreaseFontSizeAction')
118
self._install_proxy('DecreaseFontSizeAction')
119
self._install_proxy('ResetFontSizeAction')
121
def do_deactivate(self):
122
# Remove any installed menu items
125
for view in self.window.get_views():
126
self.remove_document_helper(view)
128
self.window.remove_accel_group(self._accel_group)
130
Gtk.AccelMap.get().disconnect(self._accel_map_handler_id)
132
self._accel_group = None
133
self._action_group = None
135
def _insert_menu(self):
136
# Get the GtkUIManager
137
manager = self.window.get_ui_manager()
139
# Create a new action group
140
self._action_group = Gtk.ActionGroup("GeditTextSizePluginActions")
141
self._action_group.add_actions([("IncreaseFontSizeAction", None, _("_Increase font size"),
143
self.on_increase_font_size_activate),
144
("DecreaseFontSizeAction", None, _("_Decrease font size"),
146
self.on_decrease_font_size_activate),
147
("ResetFontSizeAction", None, _("_Reset font size"),
149
self.on_reset_font_size_activate)])
151
# Insert the action group
152
manager.insert_action_group(self._action_group)
155
self._ui_id = manager.add_ui_from_string(ui_str)
157
def _remove_menu(self):
158
# Get the GtkUIManager
159
manager = self.window.get_ui_manager()
162
manager.remove_ui(self._ui_id)
164
# Remove the action group
165
manager.remove_action_group(self._action_group)
167
# Make sure the manager updates
168
manager.ensure_update()
170
def do_update_state(self):
171
self._action_group.set_sensitive(self.window.get_active_document() != None)
173
def get_helper(self, view):
174
return view.get_data(constants.DOCUMENT_HELPER_KEY)
176
def add_document_helper(self, view):
177
if self.get_helper(view) != None:
182
def remove_document_helper(self, view):
183
helper = self.get_helper(view)
188
def call_helper(self, cb):
189
view = self.window.get_active_view()
192
cb(self.get_helper(view))
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())
198
def on_decrease_font_size_activate(self, action, user_data=None):
199
self.call_helper(lambda helper: helper.decrease_font_size())
201
def on_reset_font_size_activate(self, action, user_data=None):
202
self.call_helper(lambda helper: helper.reset_font_size())
204
def on_increase_font_accel(self, group, accel, key, mod):
205
self.call_helper(lambda helper: helper.increase_font_size())
207
def on_decrease_font_accel(self, group, accel, key, mod):
208
self.call_helper(lambda helper: helper.decrease_font_size())
210
def on_reset_font_accel(self, group, accel, key, mod):
211
self.call_helper(lambda helper: helper.reset_font_size())
213
def on_tab_added(self, window, tab):
214
self.add_document_helper(tab.get_view())
216
def on_tab_removed(self, window, tab):
217
self.remove_document_helper(tab.get_view())
219
def _remap_proxy(self, action):
220
# Remove previous proxy
222
if action in self._proxy_mapping:
223
item = self._proxy_mapping[action]
224
self._accel_group.disconnect_key(item[0], item[1])
226
self._install_proxy(action)
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)