[gedit-plugins] Port to the new menu extension API



commit 8f6965b4abcfb415e0ea8331e42c3bbe74c2b2df
Author: Paolo Borelli <pborelli gnome org>
Date:   Wed Feb 5 10:01:13 2014 +0100

    Port to the new menu extension API

 .../bookmarks/gedit-bookmarks-app-activatable.c    |   28 +++-
 plugins/bookmarks/gedit-bookmarks-plugin.c         |   25 +---
 plugins/charmap/charmap/__init__.py                |    8 +-
 plugins/colorpicker/colorpicker.py                 |   48 +++---
 plugins/colorschemer/schemer/__init__.py           |   20 ++-
 plugins/commander/Makefile.am                      |    1 +
 plugins/commander/commander/__init__.py            |   32 +----
 plugins/commander/commander/appactivatable.py      |   59 +++++++
 plugins/commander/commander/windowactivatable.py   |   18 +--
 plugins/drawspaces/gedit-drawspaces-plugin.c       |  178 +++++++++-----------
 plugins/multiedit/Makefile.am                      |    1 +
 plugins/multiedit/multiedit/__init__.py            |   16 +--
 plugins/multiedit/multiedit/appactivatable.py      |   43 +++++
 plugins/multiedit/multiedit/windowactivatable.py   |   16 +--
 plugins/textsize/textsize/__init__.py              |   29 ++--
 15 files changed, 266 insertions(+), 256 deletions(-)
---
diff --git a/plugins/bookmarks/gedit-bookmarks-app-activatable.c 
b/plugins/bookmarks/gedit-bookmarks-app-activatable.c
index 10a4fc1..102c99c 100644
--- a/plugins/bookmarks/gedit-bookmarks-app-activatable.c
+++ b/plugins/bookmarks/gedit-bookmarks-app-activatable.c
@@ -18,16 +18,20 @@
  * along with gedit. If not, see <http://www.gnu.org/licenses/>.
  */
 
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
 
-#include "gedit-bookmarks-app-activatable.h"
-#include <gedit/gedit-app-activatable.h>
+#include <glib/gi18n-lib.h>
 #include <gedit/gedit-app.h>
+#include <gedit/gedit-app-activatable.h>
 #include <libpeas/peas-object-module.h>
-
+#include "gedit-bookmarks-app-activatable.h"
 
 typedef struct _GeditBookmarksAppActivatablePrivate
 {
        GeditApp *app;
+       GeditMenuExtension *menu_ext;
 } GeditBookmarksAppActivatablePrivate;
 
 enum
@@ -53,6 +57,7 @@ gedit_bookmarks_app_activatable_dispose (GObject *object)
        GeditBookmarksAppActivatablePrivate *priv = gedit_bookmarks_app_activatable_get_instance_private 
(activatable);
 
        g_clear_object (&priv->app);
+       g_clear_object (&priv->menu_ext);
 
        G_OBJECT_CLASS (gedit_bookmarks_app_activatable_parent_class)->dispose (object);
 }
@@ -126,10 +131,25 @@ gedit_bookmarks_app_activatable_activate (GeditAppActivatable *activatable)
 {
        GeditBookmarksAppActivatable *app_activatable = GEDIT_BOOKMARKS_APP_ACTIVATABLE (activatable);
        GeditBookmarksAppActivatablePrivate *priv = gedit_bookmarks_app_activatable_get_instance_private 
(app_activatable);
+       GMenuItem *item;
 
        gtk_application_add_accelerator (GTK_APPLICATION (priv->app), "<Primary><Alt>B", 
"win.bookmark-toggle", NULL);
        gtk_application_add_accelerator (GTK_APPLICATION (priv->app), "<Primary>B", "win.bookmark-next", 
NULL);
        gtk_application_add_accelerator (GTK_APPLICATION (priv->app), "<Primary><Shift>B", 
"win.bookmark-prev", NULL);
+
+       priv->menu_ext = gedit_app_activatable_extend_menu (activatable,
+                                                           "ext3");
+       item = g_menu_item_new (_("Toggle Bookmark"), "win.bookmark-toggle");
+       gedit_menu_extension_append_menu_item (priv->menu_ext, item);
+       g_object_unref (item);
+
+       item = g_menu_item_new (_("Goto Next Bookmark"), "win.bookmark-next");
+       gedit_menu_extension_append_menu_item (priv->menu_ext, item);
+       g_object_unref (item);
+
+       item = g_menu_item_new (_("Goto Previous Bookmark"), "win.bookmark-prev");
+       gedit_menu_extension_append_menu_item (priv->menu_ext, item);
+       g_object_unref (item);
 }
 
 static void
@@ -141,6 +161,8 @@ gedit_bookmarks_app_activatable_deactivate (GeditAppActivatable *activatable)
        gtk_application_remove_accelerator (GTK_APPLICATION (priv->app), "win.bookmark-toggle", NULL);
        gtk_application_remove_accelerator (GTK_APPLICATION (priv->app), "win.bookmark-next", NULL);
        gtk_application_remove_accelerator (GTK_APPLICATION (priv->app), "win.bookmark-prev", NULL);
+
+       g_clear_object (&priv->menu_ext);
 }
 
 static void
diff --git a/plugins/bookmarks/gedit-bookmarks-plugin.c b/plugins/bookmarks/gedit-bookmarks-plugin.c
index b73d9e4..a15d7f0 100644
--- a/plugins/bookmarks/gedit-bookmarks-plugin.c
+++ b/plugins/bookmarks/gedit-bookmarks-plugin.c
@@ -118,7 +118,6 @@ struct _GeditBookmarksPluginPrivate
        GSimpleAction *action_toggle;
        GSimpleAction *action_next;
        GSimpleAction *action_prev;
-       GeditMenuExtension *menu;
 };
 
 enum
@@ -146,7 +145,6 @@ gedit_bookmarks_plugin_dispose (GObject *object)
        g_clear_object (&priv->action_toggle);
        g_clear_object (&priv->action_next);
        g_clear_object (&priv->action_prev);
-       g_clear_object (&priv->menu);
        g_clear_object (&priv->window);
 
        G_OBJECT_CLASS (gedit_bookmarks_plugin_parent_class)->dispose (object);
@@ -199,10 +197,9 @@ free_insert_data (InsertData *data)
 }
 
 static void
-install_menu (GeditBookmarksPlugin *plugin)
+install_actions (GeditBookmarksPlugin *plugin)
 {
        GeditBookmarksPluginPrivate *priv;
-       GMenuItem *item;
 
        priv = plugin->priv;
 
@@ -223,24 +220,10 @@ install_menu (GeditBookmarksPlugin *plugin)
                          G_CALLBACK (on_previous_bookmark_activate), plugin);
        g_action_map_add_action (G_ACTION_MAP (priv->window),
                                 G_ACTION (priv->action_prev));
-
-       priv->menu = gedit_window_activatable_extend_menu (GEDIT_WINDOW_ACTIVATABLE (plugin),
-                                                               "ext3");
-       item = g_menu_item_new (_("Toggle Bookmark"), "win.bookmark-toggle");
-       gedit_menu_extension_append_menu_item (priv->menu, item);
-       g_object_unref (item);
-
-       item = g_menu_item_new (_("Goto Next Bookmark"), "win.bookmark-next");
-       gedit_menu_extension_append_menu_item (priv->menu, item);
-       g_object_unref (item);
-
-       item = g_menu_item_new (_("Goto Previous Bookmark"), "win.bookmark-prev");
-       gedit_menu_extension_append_menu_item (priv->menu, item);
-       g_object_unref (item);
 }
 
 static void
-uninstall_menu (GeditBookmarksPlugin *plugin)
+uninstall_actions (GeditBookmarksPlugin *plugin)
 {
        GeditBookmarksPluginPrivate *priv;
 
@@ -756,7 +739,7 @@ gedit_bookmarks_plugin_activate (GeditWindowActivatable *activatable)
        g_signal_connect (priv->window, "tab-removed",
                          G_CALLBACK (on_tab_removed), activatable);
 
-       install_menu (GEDIT_BOOKMARKS_PLUGIN (activatable));
+       install_actions (GEDIT_BOOKMARKS_PLUGIN (activatable));
        install_messages (priv->window);
 }
 
@@ -834,7 +817,7 @@ gedit_bookmarks_plugin_deactivate (GeditWindowActivatable *activatable)
 
        priv = GEDIT_BOOKMARKS_PLUGIN (activatable)->priv;
 
-       uninstall_menu (GEDIT_BOOKMARKS_PLUGIN (activatable));
+       uninstall_actions (GEDIT_BOOKMARKS_PLUGIN (activatable));
        uninstall_messages (priv->window);
 
        views = gedit_window_get_views (priv->window);
diff --git a/plugins/charmap/charmap/__init__.py b/plugins/charmap/charmap/__init__.py
index 026ab9f..194c016 100644
--- a/plugins/charmap/charmap/__init__.py
+++ b/plugins/charmap/charmap/__init__.py
@@ -44,18 +44,16 @@ class CharmapPlugin(GObject.Object, Gedit.WindowActivatable):
         self.system_settings = Gio.Settings.new("org.gnome.desktop.interface")
         self.system_settings.connect("changed::monospace-font-name", self.font_changed)
 
-        panel = self.window.get_side_panel()
-        image = Gtk.Image.new_from_icon_name("accessories-character-map-symbolic", Gtk.IconSize.MENU)
-
         self.create_charmap_panel()
-        panel.add_item(self.panel, "GeditCharmapPanel", _("Character Map"), image)
+        panel = self.window.get_side_panel()
+        panel.add_titled(self.panel, "GeditCharmapPanel", _("Character Map"))
 
         statusbar = self.window.get_statusbar()
         self.context_id = statusbar.get_context_id("Character Description")
 
     def do_deactivate(self):
         panel = self.window.get_side_panel()
-        panel.remove_item(self.panel)
+        panel.remove(self.panel)
 
     def do_update_state(self):
         self.panel.set_sensitive(len(self.window.get_documents()) >= 1)
diff --git a/plugins/colorpicker/colorpicker.py b/plugins/colorpicker/colorpicker.py
index cb6e80d..b4840e1 100644
--- a/plugins/colorpicker/colorpicker.py
+++ b/plugins/colorpicker/colorpicker.py
@@ -31,18 +31,6 @@ try:
 except:
     _ = lambda s: s
 
-ui_str = """
-<ui>
-  <menubar name="MenuBar">
-    <menu name="ToolsMenu" action="Tools">
-      <placeholder name="ToolsOps_2">
-        <menuitem name="ColorPicker" action="ColorPicker"/>
-      </placeholder>
-    </menu>
-  </menubar>
-</ui>
-"""
-
 class ColorHelper:
 
     def scale_color_component(self, component):
@@ -127,6 +115,23 @@ class ColorHelper:
         else:
             return None
 
+
+class ColorPickerAppActivatable(GObject.Object, Gedit.AppActivatable):
+
+    app = GObject.property(type=Gedit.App)
+
+    def __init__(self):
+        GObject.Object.__init__(self)
+ 
+    def do_activate(self):
+        self.menu_ext = self.extend_menu("ext9")
+        item = Gio.MenuItem.new(_("Pick _Color..."), "win.colorpicker")
+        self.menu_ext.prepend_menu_item(item)
+
+    def do_deactivate(self):
+        self.menu_ext = None
+
+
 class ColorPickerWindowActivatable(GObject.Object, Gedit.WindowActivatable):
 
     window = GObject.property(type=Gedit.Window)
@@ -137,11 +142,13 @@ class ColorPickerWindowActivatable(GObject.Object, Gedit.WindowActivatable):
         self._color_helper = ColorHelper()
 
     def do_activate(self):
-        self._insert_menu()
+        action = Gio.SimpleAction(name="colorpicker")
+        action.connect('activate', lambda a, p: self.on_color_picker_activate())
+        self.window.add_action(action)
         self._update()
 
     def do_deactivate(self):
-        self._remove_menu()
+        self.window.remove_action("colorpicker")
 
     def do_update_state(self):
         self._update()
@@ -154,24 +161,11 @@ class ColorPickerWindowActivatable(GObject.Object, Gedit.WindowActivatable):
                 self._dialog.get_transient_for() == self.window:
             self._dialog.response(Gtk.ResponseType.CLOSE)
 
-    def _insert_menu(self):
-        action = Gio.SimpleAction(name="colorpicker")
-        action.connect('activate', lambda a, p: self.on_color_picker_activate())
-        self.window.add_action(action)
-
-        self.menu = self.extend_menu("ext9")
-        item = Gio.MenuItem.new(_("Pick _Color..."), "win.colorpicker")
-        self.menu.prepend_menu_item(item)
-
-    def _remove_menu(self):
-        self.window.remove_action("colorpicker")
-
     # Signal handlers
 
     def on_color_picker_activate(self):
         if not self._dialog:
             self._dialog = Gtk.ColorChooserDialog.new(_('Pick Color'), self.window)
-
             self._dialog.connect_after('response', self.on_dialog_response)
 
         rgba_str = self._color_helper.get_current_color(self.window.get_active_document(), False)
diff --git a/plugins/colorschemer/schemer/__init__.py b/plugins/colorschemer/schemer/__init__.py
index 59e4e78..c5e92d0 100644
--- a/plugins/colorschemer/schemer/__init__.py
+++ b/plugins/colorschemer/schemer/__init__.py
@@ -21,12 +21,12 @@
 
 from gi.repository import GObject, Gio, Gedit, Gtk
 import os
-
 from .schemer import GUI
 
-class WindowActivatable(GObject.Object, Gedit.WindowActivatable):
 
-  window = GObject.property(type=Gedit.Window)
+class AppActivatable(GObject.Object, Gedit.AppActivatable):
+
+  app = GObject.property(type=Gedit.App)
 
   def __init__(self):
     GObject.Object.__init__(self)
@@ -34,14 +34,16 @@ class WindowActivatable(GObject.Object, Gedit.WindowActivatable):
   def do_activate(self):
     action = Gio.SimpleAction(name="schemer")
     action.connect('activate', self.open_dialog)
-    self.window.add_action(action)
+    self.app.add_action(action)
 
-    self.menu = self.extend_menu("ext9")
-    item = Gio.MenuItem.new(_("Color Scheme Editor"), "win.schemer")
-    self.menu.append_menu_item(item)
+    self.menu_ext = self.extend_menu("appmenuext2")
+    item = Gio.MenuItem.new(_("Color Scheme Editor"), "app.schemer")
+    self.menu_ext.append_menu_item(item)
+
+  def do_deactivate(self):
+    self.app.remove_action("schemer")
+    self.menu_ext = None
 
   def open_dialog(self, action, parameter, data=None):
     schemer.GUI(Gedit.App, os.path.join(self.plugin_info.get_data_dir(), 'ui'))
 
-  def do_deactivate(self):
-    self.window.remove_action("schemer")
diff --git a/plugins/commander/Makefile.am b/plugins/commander/Makefile.am
index 61728ad..e0599a7 100644
--- a/plugins/commander/Makefile.am
+++ b/plugins/commander/Makefile.am
@@ -8,6 +8,7 @@ plugins_commander_PYTHON =                                      \
        plugins/commander/commander/modules.py                  \
        plugins/commander/commander/transparentwindow.py        \
        plugins/commander/commander/utils.py                    \
+       plugins/commander/commander/appactivatable.py           \
        plugins/commander/commander/windowactivatable.py
 
 plugins_commander_commandsdir = $(plugindir)/commander/commands
diff --git a/plugins/commander/commander/__init__.py b/plugins/commander/commander/__init__.py
index d925a61..57fddd3 100644
--- a/plugins/commander/commander/__init__.py
+++ b/plugins/commander/commander/__init__.py
@@ -27,37 +27,7 @@ path = os.path.dirname(__file__)
 if not path in sys.path:
     sys.path.insert(0, path)
 
+from appactivatable import CommanderAppActivatable
 from windowactivatable import CommanderWindowActivatable
-import commander.commands as commands
-from gi.repository import GObject, GLib, Gedit
-
-class CommanderPlugin(GObject.Object, Gedit.AppActivatable):
-    __gtype_name__ = "CommanderPlugin"
-
-    app = GObject.property(type=Gedit.App)
-
-    def __init__(self):
-        GObject.Object.__init__(self)
-
-    def do_activate(self):
-        self._path = os.path.dirname(__file__)
-
-        if not self._path in sys.path:
-            sys.path.insert(0, self._path)
-
-        commands.Commands().set_dirs([
-            os.path.join(GLib.get_user_config_dir(), 'gedit/commander/modules'),
-            os.path.join(self.plugin_info.get_data_dir(), 'modules')
-        ])
-
-        self.app.add_accelerator("<Primary>period", "win.commander", None)
-
-    def deactivate(self):
-        commands.Commands().stop()
-
-        if self._path in sys.path:
-            sys.path.remove(self._path)
-
-        self.app.remove_accelerator("win.commander", None)
 
 # vi:ex:ts=4:et
diff --git a/plugins/commander/commander/appactivatable.py b/plugins/commander/commander/appactivatable.py
new file mode 100644
index 0000000..6f60d38
--- /dev/null
+++ b/plugins/commander/commander/appactivatable.py
@@ -0,0 +1,59 @@
+# -*- coding: utf-8 -*-
+#
+#  __init__.py - commander
+#
+#  Copyright (C) 2010 - Jesse van den Kieboom
+#
+#  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 Street, Fifth Floor,
+#  Boston, MA 02110-1301, USA.
+
+from gi.repository import GLib, GObject, Gio, Gedit
+import sys, os
+import commander.commands as commands
+
+class CommanderAppActivatable(GObject.Object, Gedit.AppActivatable):
+
+    app = GObject.property(type=Gedit.App)
+
+    def __init__(self):
+        GObject.Object.__init__(self)
+
+    def do_activate(self):
+        self._path = os.path.dirname(__file__)
+
+        if not self._path in sys.path:
+            sys.path.insert(0, self._path)
+
+        commands.Commands().set_dirs([
+            os.path.join(GLib.get_user_config_dir(), 'gedit/commander/modules'),
+            os.path.join(self.plugin_info.get_data_dir(), 'modules')
+        ])
+
+        self.app.add_accelerator("<Primary>period", "win.commander", None)
+
+        self.menu_ext = self.extend_menu("ext9")
+        item = Gio.MenuItem.new(_('Commander Mode'), "win.commander")
+        self.menu_ext.append_menu_item(item)
+
+    def deactivate(self):
+        commands.Commands().stop()
+
+        if self._path in sys.path:
+            sys.path.remove(self._path)
+
+        self.app.remove_accelerator("win.commander", None)
+        self.menu_ext = None
+
+# vi:ex:ts=4:et
diff --git a/plugins/commander/commander/windowactivatable.py 
b/plugins/commander/commander/windowactivatable.py
index 8dc4ee5..a064cf9 100644
--- a/plugins/commander/commander/windowactivatable.py
+++ b/plugins/commander/commander/windowactivatable.py
@@ -41,27 +41,17 @@ class CommanderWindowActivatable(GObject.Object, Gedit.WindowActivatable):
         self._entry = None
         self._view = None
 
-        self.install_ui()
-
-    def do_deactivate(self):
-        self.uninstall_ui()
-
-    def do_update_state(self):
-        pass
-
-    def install_ui(self):
         action = Gio.SimpleAction.new_stateful("commander", None, GLib.Variant.new_boolean(False))
         action.connect('activate', self.activate_toggle)
         action.connect('change-state', self.commander_mode)
         self.window.add_action(action)
 
-        self.menu = self.extend_menu("ext9")
-        item = Gio.MenuItem.new(_('Commander Mode'), "win.commander")
-        self.menu.append_menu_item(item)
-
-    def uninstall_ui(self):
+    def do_deactivate(self):
         self.window.remove_action("commander")
 
+    def do_update_state(self):
+        pass
+
     def activate_toggle(self, action, parameter):
         state = action.get_state()
         action.change_state(GLib.Variant.new_boolean(not state.get_boolean()))
diff --git a/plugins/drawspaces/gedit-drawspaces-plugin.c b/plugins/drawspaces/gedit-drawspaces-plugin.c
index 49d97fe..8d2d6f9 100644
--- a/plugins/drawspaces/gedit-drawspaces-plugin.c
+++ b/plugins/drawspaces/gedit-drawspaces-plugin.c
@@ -26,6 +26,8 @@
 #include <gedit/gedit-debug.h>
 #include <gedit/gedit-view.h>
 #include <gedit/gedit-tab.h>
+#include <gedit/gedit-app.h>
+#include <gedit/gedit-app-activatable.h>
 #include <gedit/gedit-window.h>
 #include <gedit/gedit-window-activatable.h>
 #include <gedit/gedit-utils.h>
@@ -42,6 +44,7 @@
                                GEDIT_TYPE_DRAWSPACES_PLUGIN,           \
                                GeditDrawspacesPluginPrivate))
 
+static void gedit_app_activatable_iface_init (GeditAppActivatableInterface *iface);
 static void gedit_window_activatable_iface_init (GeditWindowActivatableInterface *iface);
 static void peas_gtk_configurable_iface_init (PeasGtkConfigurableInterface *iface);
 
@@ -49,6 +52,8 @@ G_DEFINE_DYNAMIC_TYPE_EXTENDED (GeditDrawspacesPlugin,
                                gedit_drawspaces_plugin,
                                PEAS_TYPE_EXTENSION_BASE,
                                0,
+                               G_IMPLEMENT_INTERFACE_DYNAMIC (GEDIT_TYPE_APP_ACTIVATABLE,
+                                                              gedit_app_activatable_iface_init)
                                G_IMPLEMENT_INTERFACE_DYNAMIC (GEDIT_TYPE_WINDOW_ACTIVATABLE,
                                                               gedit_window_activatable_iface_init)
                                G_IMPLEMENT_INTERFACE_DYNAMIC (PEAS_GTK_TYPE_CONFIGURABLE,
@@ -56,14 +61,13 @@ G_DEFINE_DYNAMIC_TYPE_EXTENDED (GeditDrawspacesPlugin,
 
 struct _GeditDrawspacesPluginPrivate
 {
-       GSettings *settings;
+       GeditApp *app;
+       GeditMenuExtension *menu_ext;
 
+       GSettings *settings;
        GeditWindow *window;
-
        GtkSourceDrawSpacesFlags flags;
 
-       GeditMenuExtension *menu;
-
        guint enable : 1;
 };
 
@@ -94,6 +98,7 @@ enum
 enum
 {
        PROP_0,
+       PROP_APP,
        PROP_WINDOW
 };
 
@@ -147,8 +152,9 @@ gedit_drawspaces_plugin_dispose (GObject *object)
 
        gedit_debug_message (DEBUG_PLUGINS, "GeditDrawspacesPlugin disposing");
 
+       g_clear_object (&plugin->priv->app);
+       g_clear_object (&plugin->priv->menu_ext);
        g_clear_object (&priv->settings);
-       g_clear_object (&priv->menu);
        g_clear_object (&priv->window);
 
        G_OBJECT_CLASS (gedit_drawspaces_plugin_parent_class)->dispose (object);
@@ -164,10 +170,12 @@ gedit_drawspaces_plugin_set_property (GObject      *object,
 
        switch (prop_id)
        {
+               case PROP_APP:
+                       plugin->priv->app = GEDIT_APP (g_value_dup_object (value));
+                       break;
                case PROP_WINDOW:
                        plugin->priv->window = GEDIT_WINDOW (g_value_dup_object (value));
                        break;
-
                default:
                        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
                        break;
@@ -184,10 +192,12 @@ gedit_drawspaces_plugin_get_property (GObject    *object,
 
        switch (prop_id)
        {
+               case PROP_APP:
+                       g_value_set_object (value, plugin->priv->app);
+                       break;
                case PROP_WINDOW:
                        g_value_set_object (value, plugin->priv->window);
                        break;
-
                default:
                        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
                        break;
@@ -241,7 +251,38 @@ get_config_options (GeditDrawspacesPlugin *plugin)
 }
 
 static void
-gedit_drawspaces_plugin_activate (GeditWindowActivatable *activatable)
+gedit_drawspaces_plugin_app_activate (GeditAppActivatable *activatable)
+{
+       GeditDrawspacesPluginPrivate *priv;
+       GMenuItem *item;
+       GAction *action;
+
+       gedit_debug (DEBUG_PLUGINS);
+
+       priv = GEDIT_DRAWSPACES_PLUGIN (activatable)->priv;
+
+       priv->menu_ext = gedit_app_activatable_extend_menu (activatable,
+                                                           "ext5");
+       item = g_menu_item_new (_("Show _White Space"), "win.show-white-space");
+       gedit_menu_extension_append_menu_item (priv->menu_ext, item);
+       g_object_unref (item);
+}
+
+static void
+gedit_drawspaces_plugin_app_deactivate (GeditAppActivatable *activatable)
+{
+       GeditDrawspacesPluginPrivate *priv;
+       GtkUIManager *manager;
+
+       gedit_debug (DEBUG_PLUGINS);
+
+       priv = GEDIT_DRAWSPACES_PLUGIN (activatable)->priv;
+
+       g_clear_object (&priv->menu_ext);
+}
+
+static void
+gedit_drawspaces_plugin_window_activate (GeditWindowActivatable *activatable)
 {
        GeditDrawspacesPluginPrivate *priv;
        GMenuItem *item;
@@ -259,12 +300,6 @@ gedit_drawspaces_plugin_activate (GeditWindowActivatable *activatable)
                                 action);
        g_object_unref (action);
 
-       priv->menu = gedit_window_activatable_extend_menu (activatable,
-                                                               "ext5");
-       item = g_menu_item_new (_("Show _White Space"), "win.show-white-space");
-       gedit_menu_extension_append_menu_item (priv->menu, item);
-       g_object_unref (item);
-
        if (priv->enable)
        {
                draw_spaces (GEDIT_DRAWSPACES_PLUGIN (activatable));
@@ -275,7 +310,7 @@ gedit_drawspaces_plugin_activate (GeditWindowActivatable *activatable)
 }
 
 static void
-gedit_drawspaces_plugin_deactivate (GeditWindowActivatable *activatable)
+gedit_drawspaces_plugin_window_deactivate (GeditWindowActivatable *activatable)
 {
        GeditDrawspacesPluginPrivate *priv;
        GtkUIManager *manager;
@@ -305,129 +340,61 @@ widget_destroyed (GtkWidget *obj, gpointer widget_pointer)
 }
 
 static void
-on_draw_tabs_toggled (GtkToggleButton           *button,
-                     DrawspacesConfigureWidget *widget)
+set_flag (DrawspacesConfigureWidget *widget, GtkSourceDrawSpacesFlags flag, gboolean active)
 {
-       if (gtk_toggle_button_get_active (button))
-       {
-               widget->flags |= GTK_SOURCE_DRAW_SPACES_TAB;
-       }
-       else
-       {
-               widget->flags &= ~GTK_SOURCE_DRAW_SPACES_TAB;
-       }
-
+       widget->flags = active ? widget->flags | flag : widget->flags & ~flag;
        g_settings_set_flags (widget->settings,
                              SETTINGS_KEY_DRAW_SPACES,
                              widget->flags);
 }
 
 static void
+on_draw_tabs_toggled (GtkToggleButton           *button,
+                     DrawspacesConfigureWidget *widget)
+{
+       set_flag (widget, GTK_SOURCE_DRAW_SPACES_TAB, gtk_toggle_button_get_active (button));
+}
+
+static void
 on_draw_spaces_toggled (GtkToggleButton           *button,
                        DrawspacesConfigureWidget *widget)
 {
-       if (gtk_toggle_button_get_active (button))
-       {
-               widget->flags |= GTK_SOURCE_DRAW_SPACES_SPACE;
-       }
-       else
-       {
-               widget->flags &= ~GTK_SOURCE_DRAW_SPACES_SPACE;
-       }
-
-       g_settings_set_flags (widget->settings,
-                             SETTINGS_KEY_DRAW_SPACES,
-                             widget->flags);
+       set_flag (widget, GTK_SOURCE_DRAW_SPACES_SPACE, gtk_toggle_button_get_active (button));
 }
 
 static void
 on_draw_newline_toggled (GtkToggleButton           *button,
                         DrawspacesConfigureWidget *widget)
 {
-       if (gtk_toggle_button_get_active (button))
-       {
-               widget->flags |= GTK_SOURCE_DRAW_SPACES_NEWLINE;
-       }
-       else
-       {
-               widget->flags &= ~GTK_SOURCE_DRAW_SPACES_NEWLINE;
-       }
-
-       g_settings_set_flags (widget->settings,
-                             SETTINGS_KEY_DRAW_SPACES,
-                             widget->flags);
+       set_flag (widget, GTK_SOURCE_DRAW_SPACES_NEWLINE, gtk_toggle_button_get_active (button));
 }
 
 static void
 on_draw_nbsp_toggled (GtkToggleButton           *button,
                      DrawspacesConfigureWidget *widget)
 {
-       if (gtk_toggle_button_get_active (button))
-       {
-               widget->flags |= GTK_SOURCE_DRAW_SPACES_NBSP;
-       }
-       else
-       {
-               widget->flags &= ~GTK_SOURCE_DRAW_SPACES_NBSP;
-       }
-
-       g_settings_set_flags (widget->settings,
-                             SETTINGS_KEY_DRAW_SPACES,
-                             widget->flags);
+       set_flag (widget, GTK_SOURCE_DRAW_SPACES_NBSP, gtk_toggle_button_get_active (button));
 }
 
 static void
 on_draw_leading_toggled (GtkToggleButton           *button,
                         DrawspacesConfigureWidget *widget)
 {
-       if (gtk_toggle_button_get_active (button))
-       {
-               widget->flags |= GTK_SOURCE_DRAW_SPACES_LEADING;
-       }
-       else
-       {
-               widget->flags &= ~GTK_SOURCE_DRAW_SPACES_LEADING;
-       }
-
-       g_settings_set_flags (widget->settings,
-                             SETTINGS_KEY_DRAW_SPACES,
-                             widget->flags);
+       set_flag (widget, GTK_SOURCE_DRAW_SPACES_LEADING, gtk_toggle_button_get_active (button));
 }
 
 static void
 on_draw_text_toggled (GtkToggleButton           *button,
                      DrawspacesConfigureWidget *widget)
 {
-       if (gtk_toggle_button_get_active (button))
-       {
-               widget->flags |= GTK_SOURCE_DRAW_SPACES_TEXT;
-       }
-       else
-       {
-               widget->flags &= ~GTK_SOURCE_DRAW_SPACES_TEXT;
-       }
-
-       g_settings_set_flags (widget->settings,
-                             SETTINGS_KEY_DRAW_SPACES,
-                             widget->flags);
+       set_flag (widget, GTK_SOURCE_DRAW_SPACES_TEXT, gtk_toggle_button_get_active (button));
 }
 
 static void
 on_draw_trailing_toggled (GtkToggleButton           *button,
                          DrawspacesConfigureWidget *widget)
 {
-       if (gtk_toggle_button_get_active (button))
-       {
-               widget->flags |= GTK_SOURCE_DRAW_SPACES_TRAILING;
-       }
-       else
-       {
-               widget->flags &= ~GTK_SOURCE_DRAW_SPACES_TRAILING;
-       }
-
-       g_settings_set_flags (widget->settings,
-                             SETTINGS_KEY_DRAW_SPACES,
-                             widget->flags);
+       set_flag (widget, GTK_SOURCE_DRAW_SPACES_TRAILING, gtk_toggle_button_get_active (button));
 }
 
 static DrawspacesConfigureWidget *
@@ -533,6 +500,7 @@ gedit_drawspaces_plugin_class_init (GeditDrawspacesPluginClass *klass)
        object_class->set_property = gedit_drawspaces_plugin_set_property;
        object_class->get_property = gedit_drawspaces_plugin_get_property;
 
+       g_object_class_override_property (object_class, PROP_APP, "app");
        g_object_class_override_property (object_class, PROP_WINDOW, "window");
 
        g_type_class_add_private (object_class, sizeof (GeditDrawspacesPluginPrivate));
@@ -550,10 +518,17 @@ peas_gtk_configurable_iface_init (PeasGtkConfigurableInterface *iface)
 }
 
 static void
+gedit_app_activatable_iface_init (GeditAppActivatableInterface *iface)
+{
+       iface->activate = gedit_drawspaces_plugin_app_activate;
+       iface->deactivate = gedit_drawspaces_plugin_app_deactivate;
+}
+
+static void
 gedit_window_activatable_iface_init (GeditWindowActivatableInterface *iface)
 {
-       iface->activate = gedit_drawspaces_plugin_activate;
-       iface->deactivate = gedit_drawspaces_plugin_deactivate;
+       iface->activate = gedit_drawspaces_plugin_window_activate;
+       iface->deactivate = gedit_drawspaces_plugin_window_deactivate;
 }
 
 G_MODULE_EXPORT void
@@ -562,6 +537,9 @@ peas_register_types (PeasObjectModule *module)
        gedit_drawspaces_plugin_register_type (G_TYPE_MODULE (module));
 
        peas_object_module_register_extension_type (module,
+                                                   GEDIT_TYPE_APP_ACTIVATABLE,
+                                                   GEDIT_TYPE_DRAWSPACES_PLUGIN);
+       peas_object_module_register_extension_type (module,
                                                    GEDIT_TYPE_WINDOW_ACTIVATABLE,
                                                    GEDIT_TYPE_DRAWSPACES_PLUGIN);
        peas_object_module_register_extension_type (module,
diff --git a/plugins/multiedit/Makefile.am b/plugins/multiedit/Makefile.am
index 220f0bd..d5b4b7b 100644
--- a/plugins/multiedit/Makefile.am
+++ b/plugins/multiedit/Makefile.am
@@ -2,6 +2,7 @@ if ENABLE_PYTHON
 plugins_multieditdir = $(plugindir)/multiedit
 plugins_multiedit_PYTHON =                                     \
        plugins/multiedit/multiedit/__init__.py                 \
+       plugins/multiedit/multiedit/appactivatable.py   \
        plugins/multiedit/multiedit/windowactivatable.py        \
        plugins/multiedit/multiedit/viewactivatable.py          \
        plugins/multiedit/multiedit/signals.py
diff --git a/plugins/multiedit/multiedit/__init__.py b/plugins/multiedit/multiedit/__init__.py
index cf78f21..4a7df2b 100644
--- a/plugins/multiedit/multiedit/__init__.py
+++ b/plugins/multiedit/multiedit/__init__.py
@@ -19,22 +19,8 @@
 #  Foundation, Inc., 51 Franklin Street, Fifth Floor,
 #  Boston, MA 02110-1301, USA.
 
-from gi.repository import GObject, Gedit
-
+from .appactivatable import MultiEditAppActivatable
 from .viewactivatable import MultiEditViewActivatable
 from .windowactivatable import MultiEditWindowActivatable
 
-class MultiEditAppActivatable(GObject.Object, Gedit.AppActivatable):
-
-    app = GObject.property(type=Gedit.App)
-
-    def __init__(self):
-        GObject.Object.__init__(self)
-
-    def do_activate(self):
-        self.app.add_accelerator("<Primary><Shift>C", "win.multiedit", None)
-
-    def do_deactivate(self):
-        self.app.remove_accelerator("win.multiedit", None)
-
 # ex:ts=4:et:
diff --git a/plugins/multiedit/multiedit/appactivatable.py b/plugins/multiedit/multiedit/appactivatable.py
new file mode 100644
index 0000000..1282ad9
--- /dev/null
+++ b/plugins/multiedit/multiedit/appactivatable.py
@@ -0,0 +1,43 @@
+# -*- coding: utf-8 -*-
+#
+#  multiedit.py - Multi Edit
+#
+#  Copyright (C) 2009 - Jesse van den Kieboom
+#
+#  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 Street, Fifth Floor,
+#  Boston, MA 02110-1301, USA.
+
+from gi.repository import GObject, Gedit
+
+
+class MultiEditAppActivatable(GObject.Object, Gedit.AppActivatable):
+
+    app = GObject.property(type=Gedit.App)
+
+    def __init__(self):
+        GObject.Object.__init__(self)
+
+    def do_activate(self):
+        self.app.add_accelerator("<Primary><Shift>C", "win.multiedit", None)
+
+        self.menu_ext = self.extend_menu("ext9")
+        item = Gio.MenuItem.new(_('Multi Edit Mode'), "win.multiedit")
+        self.menu_ext.append_menu_item(item)
+
+    def do_deactivate(self):
+        self.app.remove_accelerator("win.multiedit", None)
+        self.menu_ext = None
+
+# ex:ts=4:et:
diff --git a/plugins/multiedit/multiedit/windowactivatable.py 
b/plugins/multiedit/multiedit/windowactivatable.py
index fdd9a91..e24480d 100644
--- a/plugins/multiedit/multiedit/windowactivatable.py
+++ b/plugins/multiedit/multiedit/windowactivatable.py
@@ -37,26 +37,16 @@ class MultiEditWindowActivatable(GObject.Object, Gedit.WindowActivatable):
     window = GObject.property(type=Gedit.Window)
 
     def do_activate(self):
-        self._insert_menu()
-
-        self.window.multiedit_window_activatable = self
-
-    def do_deactivate(self):
-        self._remove_menu()
-        delattr(self.window, 'multiedit_window_activatable')
-
-    def _insert_menu(self):
         action = Gio.SimpleAction.new_stateful("multiedit", None, GLib.Variant.new_boolean(False))
         action.connect('activate', self.activate_toggle)
         action.connect('change-state', self.multi_edit_mode)
         self.window.add_action(action)
 
-        self.menu = self.extend_menu("ext9")
-        item = Gio.MenuItem.new(_('Multi Edit Mode'), "win.multiedit")
-        self.menu.append_menu_item(item)
+        self.window.multiedit_window_activatable = self
 
-    def _remove_menu(self):
+    def do_deactivate(self):
         self.window.remove_action("multiedit")
+        delattr(self.window, 'multiedit_window_activatable')
 
     def do_update_state(self):
         view = self.get_view_activatable(self.window.get_active_view())
diff --git a/plugins/textsize/textsize/__init__.py b/plugins/textsize/textsize/__init__.py
index 12c3d18..7b101f3 100644
--- a/plugins/textsize/textsize/__init__.py
+++ b/plugins/textsize/textsize/__init__.py
@@ -46,10 +46,20 @@ class TextSizeAppActivatable(GObject.Object, Gedit.AppActivatable):
         self.app.set_accels_for_action("win.text-smaller", ["<Primary>minus", "<Primary>KP_Subtract"])
         self.app.set_accels_for_action("win.text-normal", ["<Primary>0", "<Primary>KP_0"])
 
+        self.menu_ext = self.extend_menu("ext9")
+        item = Gio.MenuItem.new(_("_Normal size"), "win.text-normal")
+        self.menu_ext.prepend_menu_item(item)
+        item = Gio.MenuItem.new(_("S_maller Text"), "win.text-smaller")
+        self.menu_ext.prepend_menu_item(item)
+        item = Gio.MenuItem.new(_("_Larger Text"), "win.text-larger")
+        self.menu_ext.prepend_menu_item(item)
+
     def do_deactivate(self):
         self.app.set_accels_for_action("win.text-larger", [])
         self.app.set_accels_for_action("win.text-smaller", [])
         self.app.set_accels_for_action("win.text-normal", [])
+        self.menu_ext = None
+
 
 class TextSizeWindowActivatable(GObject.Object, Gedit.WindowActivatable):
 
@@ -59,12 +69,6 @@ class TextSizeWindowActivatable(GObject.Object, Gedit.WindowActivatable):
         GObject.Object.__init__(self)
 
     def do_activate(self):
-        self._insert_menu()
-
-    def do_deactivate(self):
-        self._remove_menu()
-
-    def _insert_menu(self):
         action = Gio.SimpleAction(name="text-larger")
         action.connect('activate', self.on_larger_text_activate)
         self.window.add_action(action)
@@ -77,18 +81,7 @@ class TextSizeWindowActivatable(GObject.Object, Gedit.WindowActivatable):
         action.connect('activate', self.on_normal_size_activate)
         self.window.add_action(action)
 
-        self.menu = self.extend_menu("ext9")
-
-        item = Gio.MenuItem.new(_("_Normal size"), "win.text-normal")
-        self.menu.prepend_menu_item(item)
-
-        item = Gio.MenuItem.new(_("S_maller Text"), "win.text-smaller")
-        self.menu.prepend_menu_item(item)
-
-        item = Gio.MenuItem.new(_("_Larger Text"), "win.text-larger")
-        self.menu.prepend_menu_item(item)
-
-    def _remove_menu(self):
+    def do_deactivate(self):
         self.window.remove_action("text-larger")
         self.window.remove_action("text-smaller")
         self.window.remove_action("text-normal")


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