Blame plugins/textsize/textsize/__init__.py

Packit 6978fb
# -*- coding: utf-8 -*-
Packit 6978fb
#
Packit 6978fb
#  __init__.py - Text size plugin
Packit 6978fb
#
Packit 6978fb
#  Copyright (C) 2008 - Konstantin Mikhaylov <jtraub.devel@gmail.com>
Packit 6978fb
#  Copyright (C) 2009 - Wouter Bolsterlee <wbolster@gnome.org>
Packit 6978fb
#  Copyright (C) 2010 - Ignacio Casal Quinteiro <icq@gnome.org>
Packit 6978fb
#  Copyright (C) 2010 - Jesse van den Kieboom <jessevdk@gnome.org>
Packit 6978fb
#
Packit 6978fb
#  This program is free software; you can redistribute it and/or modify
Packit 6978fb
#  it under the terms of the GNU General Public License as published by
Packit 6978fb
#  the Free Software Foundation; either version 2 of the License, or
Packit 6978fb
#  (at your option) any later version.
Packit 6978fb
#
Packit 6978fb
#  This program is distributed in the hope that it will be useful,
Packit 6978fb
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6978fb
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 6978fb
#  GNU General Public License for more details.
Packit 6978fb
#
Packit 6978fb
#  You should have received a copy of the GNU General Public License
Packit 6978fb
#  along with this program; if not, write to the Free Software
Packit 6978fb
#  Foundation, Inc., 51 Franklin Street, Fifth Floor,
Packit 6978fb
#  Boston, MA 02110-1301, USA.
Packit 6978fb
Packit 6978fb
import gi
Packit 6978fb
gi.require_version('Gtk', '3.0')
Packit 6978fb
gi.require_version('Gedit', '3.0')
Packit 6978fb
from gi.repository import GObject, Gio, Gtk, Gdk, Gedit
Packit 6978fb
from .viewactivatable import TextSizeViewActivatable
Packit 6978fb
from gpdefs import *
Packit 6978fb
Packit 6978fb
try:
Packit 6978fb
    import gettext
Packit 6978fb
    gettext.bindtextdomain('gedit-plugins')
Packit 6978fb
    gettext.textdomain('gedit-plugins')
Packit 6978fb
    _ = gettext.gettext
Packit 6978fb
except:
Packit 6978fb
    _ = lambda s: s
Packit 6978fb
Packit 6978fb
Packit 6978fb
class TextSizeAppActivatable(GObject.Object, Gedit.AppActivatable):
Packit 6978fb
Packit 6978fb
    app = GObject.Property(type=Gedit.App)
Packit 6978fb
Packit 6978fb
    def __init__(self):
Packit 6978fb
        GObject.Object.__init__(self)
Packit 6978fb
Packit 6978fb
    def do_activate(self):
Packit 6978fb
        self.app.set_accels_for_action("win.text-larger", ["<Primary>plus", "<Primary>KP_Add"])
Packit 6978fb
        self.app.set_accels_for_action("win.text-smaller", ["<Primary>minus", "<Primary>KP_Subtract"])
Packit 6978fb
        self.app.set_accels_for_action("win.text-normal", ["<Primary>0", "<Primary>KP_0"])
Packit 6978fb
Packit 6978fb
        self.menu_ext = self.extend_menu("view-section-2")
Packit 6978fb
        item = Gio.MenuItem.new(_("_Normal size"), "win.text-normal")
Packit 6978fb
        self.menu_ext.prepend_menu_item(item)
Packit 6978fb
        item = Gio.MenuItem.new(_("S_maller Text"), "win.text-smaller")
Packit 6978fb
        self.menu_ext.prepend_menu_item(item)
Packit 6978fb
        item = Gio.MenuItem.new(_("_Larger Text"), "win.text-larger")
Packit 6978fb
        self.menu_ext.prepend_menu_item(item)
Packit 6978fb
Packit 6978fb
    def do_deactivate(self):
Packit 6978fb
        self.app.set_accels_for_action("win.text-larger", [])
Packit 6978fb
        self.app.set_accels_for_action("win.text-smaller", [])
Packit 6978fb
        self.app.set_accels_for_action("win.text-normal", [])
Packit 6978fb
        self.menu_ext = None
Packit 6978fb
Packit 6978fb
Packit 6978fb
class TextSizeWindowActivatable(GObject.Object, Gedit.WindowActivatable):
Packit 6978fb
Packit 6978fb
    window = GObject.Property(type=Gedit.Window)
Packit 6978fb
Packit 6978fb
    def __init__(self):
Packit 6978fb
        GObject.Object.__init__(self)
Packit 6978fb
Packit 6978fb
    def do_activate(self):
Packit 6978fb
        action = Gio.SimpleAction(name="text-larger")
Packit 6978fb
        action.connect('activate', self.on_larger_text_activate)
Packit 6978fb
        self.window.add_action(action)
Packit 6978fb
Packit 6978fb
        action = Gio.SimpleAction(name="text-smaller")
Packit 6978fb
        action.connect('activate', self.on_smaller_text_activate)
Packit 6978fb
        self.window.add_action(action)
Packit 6978fb
Packit 6978fb
        action = Gio.SimpleAction(name="text-normal")
Packit 6978fb
        action.connect('activate', self.on_normal_size_activate)
Packit 6978fb
        self.window.add_action(action)
Packit 6978fb
Packit 6978fb
    def do_deactivate(self):
Packit 6978fb
        self.window.remove_action("text-larger")
Packit 6978fb
        self.window.remove_action("text-smaller")
Packit 6978fb
        self.window.remove_action("text-normal")
Packit 6978fb
Packit 6978fb
    def do_update_state(self):
Packit 6978fb
        self.window.lookup_action("text-larger").set_enabled(self.window.get_active_document() != None)
Packit 6978fb
        self.window.lookup_action("text-smaller").set_enabled(self.window.get_active_document() != None)
Packit 6978fb
        self.window.lookup_action("text-normal").set_enabled(self.window.get_active_document() != None)
Packit 6978fb
Packit 6978fb
    def get_view_activatable(self, view):
Packit 6978fb
        if not hasattr(view, "textsize_view_activatable"):
Packit 6978fb
            return None
Packit 6978fb
        return view.textsize_view_activatable
Packit 6978fb
Packit 6978fb
    def call_view_activatable(self, cb):
Packit 6978fb
        view = self.window.get_active_view()
Packit 6978fb
Packit 6978fb
        if view:
Packit 6978fb
            cb(self.get_view_activatable(view))
Packit 6978fb
Packit 6978fb
    # Menu activate handlers
Packit 6978fb
    def on_larger_text_activate(self, action, parameter, user_data=None):
Packit 6978fb
        self.call_view_activatable(lambda va: va.larger_text())
Packit 6978fb
Packit 6978fb
    def on_smaller_text_activate(self, action, parameter, user_data=None):
Packit 6978fb
        self.call_view_activatable(lambda va: va.smaller_text())
Packit 6978fb
Packit 6978fb
    def on_normal_size_activate(self, action, parameter, user_data=None):
Packit 6978fb
        self.call_view_activatable(lambda va: va.normal_size())
Packit 6978fb
Packit 6978fb
# ex:ts=4:et: