[gedit/wip/redesign2: 1/19] Add GeditMenuHelper



commit e8b6993b8488e784f7daeb5a8fe4635c5ae3d65f
Author: Ignacio Casal Quinteiro <icq gnome org>
Date:   Wed Jan 1 19:43:44 2014 +0100

    Add GeditMenuHelper

 gedit/Makefile.am                       |    2 +
 gedit/gedit-menu-extension.c            |  200 +++++++++++++++++++++++++++++++
 gedit/gedit-menu-extension.h            |   65 ++++++++++
 gedit/gedit-window-activatable.c        |   38 ++++++
 gedit/gedit-window-activatable.h        |   13 ++
 gedit/gedit-window.c                    |    6 +-
 gedit/gedit-window.h                    |    2 +-
 plugins/quickopen/quickopen/__init__.py |   28 +----
 8 files changed, 327 insertions(+), 27 deletions(-)
---
diff --git a/gedit/Makefile.am b/gedit/Makefile.am
index e68240c..dbf8bd7 100644
--- a/gedit/Makefile.am
+++ b/gedit/Makefile.am
@@ -133,6 +133,7 @@ INST_H_FILES =                              \
        gedit-document.h                \
        gedit-encodings.h               \
        gedit-encodings-combo-box.h     \
+       gedit-menu-extension.h          \
        gedit-message-bus.h             \
        gedit-message.h                 \
        gedit-panel.h                   \
@@ -161,6 +162,7 @@ libgedit_private_la_SOURCES =               \
        gedit-app-activatable.c         \
        gedit-view-activatable.c        \
        gedit-window-activatable.c      \
+       gedit-menu-extension.c          \
        gedit-resources.c
 
 libgedit_c_files =                     \
diff --git a/gedit/gedit-menu-extension.c b/gedit/gedit-menu-extension.c
new file mode 100644
index 0000000..d7f1c5d
--- /dev/null
+++ b/gedit/gedit-menu-extension.c
@@ -0,0 +1,200 @@
+/*
+ * gedit-menu-extension.c
+ * This file is part of gedit
+ *
+ * Copyright (C) 2014 - Ignacio Casal Quinteiro
+ *
+ * gedit 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.
+ *
+ * gedit 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 gedit. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+
+#include "gedit-menu-extension.h"
+#include <string.h>
+
+static guint last_merge_id = 0;
+
+
+typedef struct _GeditMenuExtensionPrivate
+{
+       GMenu *menu;
+       guint merge_id;
+       gboolean dispose_has_run;
+} GeditMenuExtensionPrivate;
+
+enum
+{
+       PROP_0,
+       PROP_MENU
+};
+
+G_DEFINE_TYPE_WITH_PRIVATE (GeditMenuExtension, gedit_menu_extension, G_TYPE_OBJECT)
+
+static void
+gedit_menu_extension_dispose (GObject *object)
+{
+       GeditMenuExtension *menu = GEDIT_MENU_EXTENSION (object);
+       GeditMenuExtensionPrivate *priv = gedit_menu_extension_get_instance_private (menu);
+
+       if (!priv->dispose_has_run)
+       {
+               gedit_menu_extension_remove_items (menu);
+               priv->dispose_has_run = TRUE;
+       }
+
+       g_clear_object (&priv->menu);
+
+       G_OBJECT_CLASS (gedit_menu_extension_parent_class)->dispose (object);
+}
+
+static void
+gedit_menu_extension_get_property (GObject    *object,
+                                   guint       prop_id,
+                                   GValue     *value,
+                                   GParamSpec *pspec)
+{
+       GeditMenuExtension *menu = GEDIT_MENU_EXTENSION (object);
+       GeditMenuExtensionPrivate *priv = gedit_menu_extension_get_instance_private (menu);
+
+       switch (prop_id)
+       {
+               case PROP_MENU:
+                       g_value_set_object (value, priv->menu);
+                       break;
+               default:
+                       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+                       break;
+       }
+}
+
+static void
+gedit_menu_extension_set_property (GObject     *object,
+                                   guint         prop_id,
+                                   const GValue *value,
+                                   GParamSpec   *pspec)
+{
+       GeditMenuExtension *menu = GEDIT_MENU_EXTENSION (object);
+       GeditMenuExtensionPrivate *priv = gedit_menu_extension_get_instance_private (menu);
+
+       switch (prop_id)
+       {
+               case PROP_MENU:
+                       priv->menu = g_value_dup_object (value);
+                       break;
+               default:
+                       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+                       break;
+       }
+}
+
+static void
+gedit_menu_extension_class_init (GeditMenuExtensionClass *klass)
+{
+       GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+       object_class->dispose = gedit_menu_extension_dispose;
+       object_class->get_property = gedit_menu_extension_get_property;
+       object_class->set_property = gedit_menu_extension_set_property;
+
+       g_object_class_install_property (object_class,
+                                        PROP_MENU,
+                                        g_param_spec_object ("menu",
+                                                             "Menu",
+                                                             "The main menu",
+                                                             G_TYPE_MENU,
+                                                             G_PARAM_READWRITE |
+                                                             G_PARAM_CONSTRUCT_ONLY |
+                                                             G_PARAM_STATIC_STRINGS));
+}
+
+static void
+gedit_menu_extension_init (GeditMenuExtension *menu)
+{
+       GeditMenuExtensionPrivate *priv;
+
+       priv = gedit_menu_extension_get_instance_private (menu);
+
+       priv->merge_id = ++last_merge_id;
+}
+
+GeditMenuExtension *
+_gedit_menu_extension_new (GMenu *menu)
+{
+       return g_object_new (GEDIT_TYPE_MENU, "menu", menu, NULL);
+}
+
+void
+gedit_menu_extension_append_menu_item (GeditMenuExtension *menu,
+                                       GMenuItem       *item)
+{
+       GeditMenuExtensionPrivate *priv;
+
+       g_return_if_fail (GEDIT_IS_MENU (menu));
+       g_return_if_fail (G_IS_MENU_ITEM (item));
+
+       priv = gedit_menu_extension_get_instance_private (menu);
+
+       if (priv->menu != NULL)
+       {
+               g_menu_item_set_attribute (item, "gedit-merge-id", "u", priv->merge_id);
+               g_menu_append_item (priv->menu, item);
+       }
+}
+
+void
+gedit_menu_extension_prepend_menu_item (GeditMenuExtension *menu,
+                                        GMenuItem       *item)
+{
+       GeditMenuExtensionPrivate *priv;
+
+       g_return_if_fail (GEDIT_IS_MENU (menu));
+       g_return_if_fail (G_IS_MENU_ITEM (item));
+
+       priv = gedit_menu_extension_get_instance_private (menu);
+
+       if (priv->menu != NULL)
+       {
+               g_menu_item_set_attribute (item, "gedit-merge-id", "u", priv->merge_id);
+               g_menu_prepend_item (priv->menu, item);
+       }
+}
+
+void
+gedit_menu_extension_remove_items (GeditMenuExtension *menu)
+{
+       GeditMenuExtensionPrivate *priv;
+       gint i, n_items;
+
+       priv = gedit_menu_extension_get_instance_private (menu);
+
+       n_items = g_menu_model_get_n_items (G_MENU_MODEL (priv->menu));
+       i = 0;
+       while (i < n_items)
+       {
+               guint id = 0;
+
+               if (g_menu_model_get_item_attribute (G_MENU_MODEL (priv->menu),
+                                                    i, "gedit-merge-id", "u", &id) &&
+                   id == priv->merge_id)
+               {
+                       g_menu_remove (priv->menu, i);
+                       n_items--;
+               }
+               else
+               {
+                       i++;
+               }
+       }
+}
+
+/* ex:set ts=8 noet: */
diff --git a/gedit/gedit-menu-extension.h b/gedit/gedit-menu-extension.h
new file mode 100644
index 0000000..039c965
--- /dev/null
+++ b/gedit/gedit-menu-extension.h
@@ -0,0 +1,65 @@
+/*
+ * gedit-menu-extension.h
+ * This file is part of gedit
+ *
+ * Copyright (C) 2014 - Ignacio Casal Quinteiro
+ *
+ * gedit 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.
+ *
+ * gedit 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 gedit. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+
+#ifndef __GEDIT_MENU_EXTENSION_H__
+#define __GEDIT_MENU_EXTENSION_H__
+
+#include <glib-object.h>
+#include <gio/gio.h>
+
+G_BEGIN_DECLS
+
+#define GEDIT_TYPE_MENU                                (gedit_menu_extension_get_type ())
+#define GEDIT_MENU_EXTENSION(obj)              (G_TYPE_CHECK_INSTANCE_CAST ((obj), GEDIT_TYPE_MENU, 
GeditMenuExtension))
+#define GEDIT_MENU_EXTENSION_CONST(obj)                (G_TYPE_CHECK_INSTANCE_CAST ((obj), GEDIT_TYPE_MENU, 
GeditMenuExtension const))
+#define GEDIT_MENU_EXTENSION_CLASS(klass)      (G_TYPE_CHECK_CLASS_CAST ((klass), GEDIT_TYPE_MENU, 
GeditMenuExtensionClass))
+#define GEDIT_IS_MENU(obj)                     (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GEDIT_TYPE_MENU))
+#define GEDIT_IS_MENU_CLASS(klass)             (G_TYPE_CHECK_CLASS_TYPE ((klass), GEDIT_TYPE_MENU))
+#define GEDIT_MENU_EXTENSION_GET_CLASS(obj)    (G_TYPE_INSTANCE_GET_CLASS ((obj), GEDIT_TYPE_MENU, 
GeditMenuExtensionClass))
+
+typedef struct _GeditMenuExtension      GeditMenuExtension;
+typedef struct _GeditMenuExtensionClass         GeditMenuExtensionClass;
+
+struct _GeditMenuExtension
+{
+       GObject parent;
+};
+
+struct _GeditMenuExtensionClass
+{
+       GObjectClass parent_class;
+};
+
+GType                     gedit_menu_extension_get_type            (void) G_GNUC_CONST;
+
+GeditMenuExtension      *_gedit_menu_extension_new                 (GMenu                *menu);
+
+void                      gedit_menu_extension_append_menu_item    (GeditMenuExtension   *menu,
+                                                                    GMenuItem            *item);
+
+void                      gedit_menu_extension_prepend_menu_item   (GeditMenuExtension   *menu,
+                                                                    GMenuItem            *item);
+
+void                      gedit_menu_extension_remove_items        (GeditMenuExtension   *menu);
+
+G_END_DECLS
+
+#endif /* __GEDIT_MENU_EXTENSION_H__ */
diff --git a/gedit/gedit-window-activatable.c b/gedit/gedit-window-activatable.c
index ce7ee41..416c401 100644
--- a/gedit/gedit-window-activatable.c
+++ b/gedit/gedit-window-activatable.c
@@ -25,6 +25,7 @@
 
 #include "gedit-window-activatable.h"
 #include "gedit-window.h"
+#include <string.h>
 
 /**
  * SECTION:gedit-window-activatable
@@ -124,3 +125,40 @@ gedit_window_activatable_update_state (GeditWindowActivatable *activatable)
        }
 }
 
+GeditMenuExtension *
+gedit_window_activatable_extend_gear_menu (GeditWindowActivatable *activatable,
+                                           const gchar            *extension_point)
+{
+       GeditMenuExtension *menu = NULL;
+       GeditWindow *window;
+       GMenuModel *model;
+       gint i, n_items;
+
+       g_return_val_if_fail (GEDIT_IS_WINDOW_ACTIVATABLE (activatable), NULL);
+       g_return_val_if_fail (extension_point != NULL, NULL);
+
+       g_object_get (G_OBJECT (activatable), "window", &window, NULL);
+       model = _gedit_window_get_gear_menu (window);
+       g_object_unref (window);
+
+       n_items = g_menu_model_get_n_items (model);
+
+       for (i = 0; i < n_items; i++)
+       {
+               gchar *id = NULL;
+
+               if (g_menu_model_get_item_attribute (model, i, "id", "s", &id) &&
+                   strcmp (id, extension_point) == 0)
+               {
+                       GMenuModel *section;
+
+                       section = g_menu_model_get_item_link (model, i, G_MENU_LINK_SECTION);
+                       menu = _gedit_menu_extension_new (G_MENU (section));
+               }
+
+               g_free (id);
+       }
+
+       return menu;
+}
+
diff --git a/gedit/gedit-window-activatable.h b/gedit/gedit-window-activatable.h
index b55509e..440a8c7 100644
--- a/gedit/gedit-window-activatable.h
+++ b/gedit/gedit-window-activatable.h
@@ -23,6 +23,7 @@
 #define __GEDIT_WINDOW_ACTIVATABLE_H__
 
 #include <glib-object.h>
+#include <gedit/gedit-menu-extension.h>
 
 G_BEGIN_DECLS
 
@@ -57,6 +58,18 @@ void  gedit_window_activatable_activate      (GeditWindowActivatable *activatable);
 void    gedit_window_activatable_deactivate    (GeditWindowActivatable *activatable);
 void    gedit_window_activatable_update_state  (GeditWindowActivatable *activatable);
 
+/**
+ * gedit_window_activatable_extend_gear_menu:
+ * @activatable: A #GeditWindowActivatable.
+ * @extension_point: the extension point section of the menu to get.
+ *
+ * Gets the #GeditMenuExtension for the gear menu @extension_point.
+ *
+ * Returns: (transfer full): a #GeditMenuExtension for the specific section or %NULL if not found.
+ */
+GeditMenuExtension *gedit_window_activatable_extend_gear_menu (GeditWindowActivatable *activatable,
+                                                                const gchar            *extension_point);
+
 G_END_DECLS
 
 #endif /* __GEDIT_WINDOW_ACTIVATABLE_H__ */
diff --git a/gedit/gedit-window.c b/gedit/gedit-window.c
index 00d2108..c683097 100644
--- a/gedit/gedit-window.c
+++ b/gedit/gedit-window.c
@@ -4122,15 +4122,15 @@ gedit_window_get_message_bus (GeditWindow *window)
 }
 
 /**
- * gedit_window_get_gear_menu:
+ * _gedit_window_get_gear_menu:
  * @window: a #GeditWindow.
  *
- * Gets the gear menu model.
+ * Gets the gear menu.
  *
  * Returns: (transfer none): the #GMenuModel of the gear menu button.
  */
 GMenuModel *
-gedit_window_get_gear_menu (GeditWindow *window)
+_gedit_window_get_gear_menu (GeditWindow *window)
 {
        g_return_val_if_fail (GEDIT_IS_WINDOW (window), NULL);
 
diff --git a/gedit/gedit-window.h b/gedit/gedit-window.h
index 9e4ac6c..6dba3ef 100644
--- a/gedit/gedit-window.h
+++ b/gedit/gedit-window.h
@@ -150,7 +150,7 @@ GeditTab        *gedit_window_get_tab_from_location (GeditWindow         *window
 /* Message bus */
 GeditMessageBus        *gedit_window_get_message_bus           (GeditWindow         *window);
 
-GMenuModel      *gedit_window_get_gear_menu             (GeditWindow         *window);
+GMenuModel       *_gedit_window_get_gear_menu           (GeditWindow         *window);
 
 /*
  * Non exported functions
diff --git a/plugins/quickopen/quickopen/__init__.py b/plugins/quickopen/quickopen/__init__.py
index d2d37b6..efab0d4 100644
--- a/plugins/quickopen/quickopen/__init__.py
+++ b/plugins/quickopen/quickopen/__init__.py
@@ -58,18 +58,6 @@ class QuickOpenPlugin(GObject.Object, Gedit.WindowActivatable):
         self._popup_size = size
 
     def _uninstall_menu(self):
-        gear_menu = self.window.get_gear_menu()
-        for i in range(gear_menu.get_n_items()):
-            id_attr = gear_menu.get_item_attribute_value(i, "id", GLib.VariantType.new("s"))
-            if id_attr and id_attr.get_string() == "ext2":
-                model = gear_menu.get_item_link(i, "section")
-                for j in range(model.get_n_items()):
-                    attr = model.get_item_attribute_value(j, "id", GLib.VariantType.new("s"))
-                    if attr and attr.get_string() == "quickopen":
-                        model.remove(j)
-                        break
-                break
-
         self.window.remove_action("quickopen")
 
     def _install_menu(self):
@@ -77,17 +65,11 @@ class QuickOpenPlugin(GObject.Object, Gedit.WindowActivatable):
         action.connect('activate', self.on_quick_open_activate)
         self.window.add_action(action)
 
-        gear_menu = self.window.get_gear_menu()
-        for i in  range(gear_menu.get_n_items()):
-            id_attr = gear_menu.get_item_attribute_value(i, "id", GLib.VariantType.new("s"))
-            if id_attr and id_attr.get_string() == "ext2":
-                model = gear_menu.get_item_link(i, "section")
-
-                item = Gio.MenuItem.new(_("Quick Open..."), "win.quickopen")
-                item.set_attribute_value("id", GLib.Variant.new_string("quickopen"))
-                item.set_attribute_value("accel", GLib.Variant.new_string("<Primary><Alt>O"))
-                model.append_item(item)
-                break
+        item = Gio.MenuItem.new(_("Quick Open..."), "win.quickopen")
+        item.set_attribute_value("accel", GLib.Variant.new_string("<Primary><Alt>O"))
+
+        self.menu = self.extend_gear_menu("ext2")
+        self.menu.prepend_menu_item(item)
 
     def _create_popup(self):
         paths = []


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