[gedit-latex/pietro-on-jimmy-3-14: 1/3] Fix for 3.12.



commit c4e3a6596c68c5746b7f4af8a2bdfbb3f2741d9c
Author: jimmytheneutrino <aleksei lissitsin gmail com>
Date:   Tue Sep 2 16:36:48 2014 +0300

    Fix for 3.12.

 latex/action.py            |   10 +++++-
 latex/appactivatable.py    |   74 ++++++++++++++++++++++++++++++++++++++++++-
 latex/config.py            |    2 +
 latex/tools/__init__.py    |    4 ++-
 latex/windowactivatable.py |   38 ++++++++++++++++------
 5 files changed, 113 insertions(+), 15 deletions(-)
---
diff --git a/latex/action.py b/latex/action.py
index 25113df..f9a8b56 100644
--- a/latex/action.py
+++ b/latex/action.py
@@ -19,7 +19,7 @@
 # Street, Fifth Floor, Boston, MA  02110-1301, USA
 
 from uuid import uuid1
-from gi.repository import Gtk, GdkPixbuf
+from gi.repository import Gtk, GdkPixbuf, Gio
 
 from .file import File
 from .resources import Resources
@@ -59,7 +59,13 @@ class Action(object):
         self._internal_action = action_clazz(self.__class__.__name__, self.label, self.tooltip, 
self.stock_id)
         self._handler = self._internal_action.connect("activate", lambda gtk_action, action: 
action.activate(window_context), self)
         action_group.add_action_with_accel(self._internal_action, self.accelerator)
-
+    
+    # Hooks a Gio.SimpleAction to a given window.
+    def simplehook(self, window, window_context):
+        self._simple_internal_action = Gio.SimpleAction(name=self.__class__.__name__)
+        self._simplehandler = self._simple_internal_action.connect("activate", lambda action, param: 
self.activate(window_context))
+        window.add_action(self._simple_internal_action)
+        
     @property
     def label(self):
         raise NotImplementedError
diff --git a/latex/appactivatable.py b/latex/appactivatable.py
index 02e9abf..18ad709 100644
--- a/latex/appactivatable.py
+++ b/latex/appactivatable.py
@@ -18,14 +18,19 @@
 import os.path
 import platform
 
-from gi.repository import GLib, Gedit, GObject
+from gi.repository import GLib, Gedit, GObject, Gio, Gtk
 from .resources import Resources
 
+from .config import MENUACTIONS
+
+from .preferences.tools import ToolPreferences
+from .tools import ToolAction
+
 class LaTeXAppActivatable(GObject.Object, Gedit.AppActivatable):
     __gtype_name__ = "GeditLaTeXAppActivatable"
 
     app = GObject.property(type=Gedit.App)
-
+    
     def __init__(self):
         GObject.Object.__init__(self)
 
@@ -43,5 +48,70 @@ class LaTeXAppActivatable(GObject.Object, Gedit.AppActivatable):
             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
+
+            # create action
+            action = ToolAction(tool)
+            actionlink = "win." + name
+            self.latex_tools_menu.append_item(Gio.MenuItem.new(_(action.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()
+        
 # ex:ts=4:et
diff --git a/latex/config.py b/latex/config.py
index 02cd041..7a545a0 100644
--- a/latex/config.py
+++ b/latex/config.py
@@ -52,6 +52,8 @@ ACTIONS = [LaTeXMenuAction, LaTeXNewAction, LaTeXChooseMasterAction,
         LaTeXBuildAction, LaTeXBuildMenuAction,
         BibTeXMenuAction, BibTeXNewEntryAction]
 
+MENUACTIONS = [LaTeXNewAction, LaTeXChooseMasterAction, LaTeXCloseEnvironmentAction, BibTeXNewEntryAction] 
+
 # views
 from .views import IssueView
 from .latex.views import LaTeXSymbolMapView, LaTeXOutlineView
diff --git a/latex/tools/__init__.py b/latex/tools/__init__.py
index 389c449..c990633 100644
--- a/latex/tools/__init__.py
+++ b/latex/tools/__init__.py
@@ -123,7 +123,9 @@ class ToolAction(Action):
             decorator = context._window_decorator
             doc = decorator.window.get_active_document()
             self.saving_id = doc.connect("saved",self.run_tool,context,doc)
-            decorator.save_file()
+            #FIXME Better would be to trigger a dialog if needed as was done before like this:
+            #decorator.save_file()
+            doc.save(0)
         else:
             LOG.error("tool activate: no active editor")
 
diff --git a/latex/windowactivatable.py b/latex/windowactivatable.py
index 17ef0c9..cb1a762 100644
--- a/latex/windowactivatable.py
+++ b/latex/windowactivatable.py
@@ -24,6 +24,7 @@ This is searched by gedit for a class extending gedit.Plugin
 
 import logging
 import string
+from traceback import print_exc
 
 from gi.repository import Gedit, GObject, Gio, Gtk, PeasGtk
 
@@ -74,6 +75,7 @@ class LaTeXWindowActivatable(GObject.Object, Gedit.WindowActivatable, PeasGtk.Co
         """
         Called when the window extension is activated
         """
+
         self._preferences = Preferences()
         self._tool_preferences = ToolPreferences()
         self._tool_preferences.connect("tools-changed", self._on_tools_changed)
@@ -90,7 +92,7 @@ class LaTeXWindowActivatable(GObject.Object, Gedit.WindowActivatable, PeasGtk.Co
         self._init_tab_decorators()
 
         # FIXME: find another way to save a document
-        self._save_action = self._ui_manager.get_action("/MenuBar/FileMenu/FileSaveMenu")
+        #self._save_action = self._ui_manager.get_action("/MenuBar/FileMenu/FileSaveMenu")
 
         #
         # listen to tab signals
@@ -161,7 +163,7 @@ class LaTeXWindowActivatable(GObject.Object, Gedit.WindowActivatable, PeasGtk.Co
         """
         Merge the plugin's UI definition with the one of Gedit and hook the actions
         """
-        self._ui_manager = self.window.get_ui_manager()
+        self._ui_manager = Gtk.UIManager()
         self._action_group = Gtk.ActionGroup("LaTeXWindowActivatableActions")
         self._icon_factory = Gtk.IconFactory()
         self._icon_factory.add_default()
@@ -175,6 +177,7 @@ class LaTeXWindowActivatable(GObject.Object, Gedit.WindowActivatable, PeasGtk.Co
         for clazz in ACTIONS:
             action = clazz(icon_factory=self._icon_factory)
             action.hook(self._action_group, self._window_context)
+            action.simplehook(self.window, self._window_context)
 
             self._action_objects[clazz.__name__] = action
 
@@ -184,8 +187,10 @@ class LaTeXWindowActivatable(GObject.Object, Gedit.WindowActivatable, PeasGtk.Co
                 else:
                     self._action_extensions[extension] = [clazz.__name__]
 
-        toolbar_mode = self._preferences.get("toolbar-mode")
-
+        #toolbar_mode = self._preferences.get("toolbar-mode")
+        # force normal mode
+        toolbar_mode = "normal"
+        
         # merge ui
         self._ui_manager.insert_action_group(self._action_group, -1)
         self._ui_id = self._ui_manager.add_ui_from_file(Resources().get_ui_file("ui-toolbar-%s.builder" % 
toolbar_mode))
@@ -195,9 +200,17 @@ class LaTeXWindowActivatable(GObject.Object, Gedit.WindowActivatable, PeasGtk.Co
             self._toolbar_name = "LaTeXToolbar"
             self._toolbar.set_style(Gtk.ToolbarStyle.BOTH_HORIZ)
             # FIXME: Adding a new toolbar to gedit is not really public API
-            self._main_box = self.window.get_children()[0]
-            self._main_box.pack_start(self._toolbar, False, True, 0)
-            self._main_box.reorder_child(self._toolbar, 2)
+            try:
+                # We assume the spot below exists
+                self._main_box = 
self.window.get_children()[0].get_children()[0].get_children()[0].get_children()[1]
+                self._main_box.pack_start(self._toolbar, False, True, 0)
+                self._main_box.reorder_child(self._toolbar, 1)
+            except (IndexError, AttributeError, TypeError) as e:
+                print_exc()
+                print()
+                print("Could not find place for the toolbar. Disabling toolbar.")
+                self_toolbar = None
+                self._toolbar_name = ""
         elif toolbar_mode == "combined":
             self._toolbar = None
             self._toolbar_name = "ToolBar"
@@ -205,7 +218,7 @@ class LaTeXWindowActivatable(GObject.Object, Gedit.WindowActivatable, PeasGtk.Co
             self._toolbar = None
             self._toolbar_name = ""
             LOG.info("Toolbar disabled")    
-
+            
     def _init_tab_decorators(self):
         """
         Look for already open tabs and create decorators for them
@@ -258,6 +271,11 @@ class LaTeXWindowActivatable(GObject.Object, Gedit.WindowActivatable, PeasGtk.Co
             action = ToolAction(tool)
             gtk_action = Gtk.Action(name, action.label, action.tooltip, action.stock_id)
             self._action_handlers[gtk_action] = gtk_action.connect("activate", lambda gtk_action, action: 
action.activate(self._window_context), action)
+            
+            # create simple actions to be used by menu (created in appactivatable.py)
+            simpleaction = Gio.SimpleAction(name=name)
+            simpleaction.connect("activate", lambda _a, _b, action: action.activate(self._window_context), 
action)
+            self.window.add_action(simpleaction)
 
             accelerator = None
             if tool.accelerator and len(tool.accelerator) > 0:
@@ -449,7 +467,7 @@ class LaTeXWindowActivatable(GObject.Object, Gedit.WindowActivatable, PeasGtk.Co
             if view in self._side_views:
                 view.show()
             else:
-                self.window.get_side_panel().add_item(view, "after_side_view_id" + str(i), view.get_label(), 
view.get_icon())
+                self.window.get_side_panel().add_titled(view, "after_side_view_id" + str(i), 
view.get_label())
                 self._side_views.append(view)
                 i += 1
 
@@ -458,7 +476,7 @@ class LaTeXWindowActivatable(GObject.Object, Gedit.WindowActivatable, PeasGtk.Co
             if view in self._bottom_views:
                 view.show()
             else:
-                self.window.get_bottom_panel().add_item(view, "bottom_view_id" + str(i), view.get_label(), 
view.get_icon())
+                self.window.get_bottom_panel().add_titled(view, "bottom_view_id" + str(i), view.get_label())
                 self._bottom_views.append(view)
                 i += 1
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]