File: appactivatable.py

package info (click to toggle)
gedit-latex-plugin 3.8.0-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 4,772 kB
  • ctags: 1,793
  • sloc: sh: 11,232; python: 6,892; xml: 1,885; makefile: 410; perl: 260
file content (118 lines) | stat: -rw-r--r-- 4,506 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#    Gedit latex plugin
#    Copyright (C) 2011 Ignacio Casal Quinteiro
#
#    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 of the License, 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
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

import os.path
import platform

from gi.repository import GLib, Gedit, GObject, Gio, Gtk
from .resources import Resources

from .config import MENUACTIONS

from .preferences.tools import ToolPreferences

class LaTeXAppActivatable(GObject.Object, Gedit.AppActivatable):
    __gtype_name__ = "GeditLaTeXAppActivatable"

    app = GObject.property(type=Gedit.App)

    def __init__(self):
        GObject.Object.__init__(self)

    def do_activate(self):
        if platform.platform() == 'Windows':
            userdir = os.path.expanduser('~/gedit/latex')
        else:
            userdir = os.path.join(GLib.get_user_config_dir(), 'gedit/latex')

        #check if running from srcdir and if so, prefer that for all data files
        me = os.path.realpath(os.path.dirname(__file__))
        if os.path.exists(os.path.join(me, "..", "configure.ac")):
            sysdir = os.path.abspath(os.path.join(me, "..", "data"))
        else:
            sysdir = self.plugin_info.get_data_dir()

        Resources().set_dirs(userdir, sysdir)
        
        #The following is needed to support gedit 3.12 new menu api.
        #It adds menus and shortcuts here.
        #Actions and toolbar construction are still done in windowactivatable.py.
        
        self._tool_preferences = ToolPreferences()
        self._tool_preferences.connect("tools-changed", self._on_tools_changed)
        
        self.add_latex_menu()
        self.add_latex_tools_menu()
        self.init_tools()
        
    def add_latex_menu(self):
        self.menu_ext = self.extend_menu("tools-section")
        menu = Gio.MenuItem.new(_("LaTeX"))
        container = Gio.Menu.new()
        menu.set_submenu(container)
        self.menu_ext.append_menu_item(menu)
        
        self._icon_factory = Gtk.IconFactory()
        self._icon_factory.add_default()
        
        for clazz in MENUACTIONS:
            action = clazz(icon_factory=self._icon_factory)
            actionlink = "win." + clazz.__name__
            container.append_item(Gio.MenuItem.new(_(action.label), actionlink))
            if action.accelerator is not None:
                self.app.add_accelerator(action.accelerator, actionlink, None)
                
    def add_latex_tools_menu(self):
        menu = Gio.MenuItem.new(_("LaTeX Tools"))
        container = Gio.Menu.new()
        menu.set_submenu(container)
        self.latex_tools_menu = container
        self.menu_ext.append_menu_item(menu)
    
    def init_tools(self):
      
        #the following is copied from windowactivatable.py and modified as necessary
        
        i = 1                    # counting tool actions
        accel_counter = 1        # counting tool actions without custom accel
        for tool in self._tool_preferences.tools:
            # hopefully unique action name
            name = "Tool%sAction" % i

            actionlink = "win." + name
            self.latex_tools_menu.append_item(Gio.MenuItem.new(_(tool.label), actionlink))

            accelerator = None
            if tool.accelerator and len(tool.accelerator) > 0:
                key,mods = Gtk.accelerator_parse(tool.accelerator)
                if Gtk.accelerator_valid(key,mods):
                    accelerator = tool.accelerator
            if not accelerator:
                accelerator = "<Ctrl><Alt>%s" % accel_counter
                accel_counter += 1
            self.app.add_accelerator(accelerator, actionlink, None)
            i += 1
            
    def _on_tools_changed(self, tools):
        self.latex_tools_menu.remove_all()
        self.init_tools()

    def do_deactivate(self):
        del self.latex_tools_menu
        del self.menu_ext
        
# ex:ts=4:et