[gedit/wip/redesign2] Make the GeditMenu part of the activatable extension point



commit cc0cce0cbea2facfe7f214cb4bc119cacebb417d
Author: Ignacio Casal Quinteiro <icq gnome org>
Date:   Thu Jan 2 13:28:48 2014 +0100

    Make the GeditMenu part of the activatable extension point

 gedit/Makefile.am                       |    2 +-
 gedit/gedit-menu.c                      |  130 +++++++++++--------------------
 gedit/gedit-menu.h                      |    9 +--
 gedit/gedit-window-activatable.c        |   37 +++++++++
 gedit/gedit-window-activatable.h        |   13 +++
 gedit/gedit-window-private.h            |    1 -
 gedit/gedit-window.c                    |   13 +--
 gedit/gedit-window.h                    |    3 +-
 plugins/quickopen/quickopen/__init__.py |    8 +--
 9 files changed, 107 insertions(+), 109 deletions(-)
---
diff --git a/gedit/Makefile.am b/gedit/Makefile.am
index 8aad6e6..c0a5a32 100644
--- a/gedit/Makefile.am
+++ b/gedit/Makefile.am
@@ -162,6 +162,7 @@ libgedit_private_la_SOURCES =               \
        gedit-app-activatable.c         \
        gedit-view-activatable.c        \
        gedit-window-activatable.c      \
+       gedit-menu.c                    \
        gedit-resources.c
 
 libgedit_c_files =                     \
@@ -190,7 +191,6 @@ libgedit_c_files =                  \
        gedit-highlight-mode-dialog.c   \
        gedit-history-entry.c           \
        gedit-io-error-info-bar.c       \
-       gedit-menu.c                    \
        gedit-message-bus.c             \
        gedit-message.c                 \
        gedit-multi-notebook.c          \
diff --git a/gedit/gedit-menu.c b/gedit/gedit-menu.c
index 3ccb454..c46eb8d 100644
--- a/gedit/gedit-menu.c
+++ b/gedit/gedit-menu.c
@@ -22,11 +22,14 @@
 #include "gedit-menu.h"
 #include <string.h>
 
+static guint last_merge_id = 0;
+
 
 typedef struct _GeditMenuPrivate
 {
        GMenuModel *model;
-       guint last_merge_id;
+       guint merge_id;
+       gboolean dispose_has_run;
 } GeditMenuPrivate;
 
 enum
@@ -38,11 +41,44 @@ enum
 G_DEFINE_TYPE_WITH_PRIVATE (GeditMenu, gedit_menu, G_TYPE_OBJECT)
 
 static void
+gedit_menu_remove_items (GeditMenu *menu)
+{
+       GeditMenuPrivate *priv;
+       gint i, n_items;
+
+       priv = gedit_menu_get_instance_private (menu);
+
+       n_items = g_menu_model_get_n_items (priv->model);
+       i = 0;
+       while (i < n_items)
+       {
+               guint id = 0;
+
+               if (g_menu_model_get_item_attribute (priv->model, i, "gedit-merge-id", "u", &id) &&
+                   id == priv->merge_id)
+               {
+                       g_menu_remove (G_MENU (priv->model), i);
+                       n_items--;
+               }
+               else
+               {
+                       i++;
+               }
+       }
+}
+
+static void
 gedit_menu_dispose (GObject *object)
 {
        GeditMenu *menu = GEDIT_MENU (object);
        GeditMenuPrivate *priv = gedit_menu_get_instance_private (menu);
 
+       if (!priv->dispose_has_run)
+       {
+               gedit_menu_remove_items (menu);
+               priv->dispose_has_run = TRUE;
+       }
+
        g_clear_object (&priv->model);
 
        G_OBJECT_CLASS (gedit_menu_parent_class)->dispose (object);
@@ -111,106 +147,34 @@ gedit_menu_class_init (GeditMenuClass *klass)
 static void
 gedit_menu_init (GeditMenu *menu)
 {
-}
-
-GeditMenu *
-_gedit_menu_new (GMenuModel *model)
-{
-       return g_object_new (GEDIT_TYPE_MENU, "model", model, NULL);
-}
-
-guint
-gedit_menu_new_merge_id (GeditMenu *menu)
-{
        GeditMenuPrivate *priv;
 
-       g_return_val_if_fail (GEDIT_IS_MENU (menu), 0);
-
        priv = gedit_menu_get_instance_private (menu);
-       priv->last_merge_id++;
 
-       return priv->last_merge_id;
+       priv->merge_id = ++last_merge_id;
 }
 
-void
-gedit_menu_add_menu_item (GeditMenu   *menu,
-                          GMenuItem   *item,
-                          guint        merge_id,
-                          const gchar *extension_point)
+GeditMenu *
+_gedit_menu_new (GMenuModel *model)
 {
-       GeditMenuPrivate *priv;
-       gint i, n_items;
-
-       g_return_if_fail (GEDIT_IS_MENU (menu));
-       g_return_if_fail (G_IS_MENU_ITEM (item));
-       g_return_if_fail (extension_point != NULL);
-
-       priv = gedit_menu_get_instance_private (menu);
-       n_items = g_menu_model_get_n_items (priv->model);
-
-       for (i = 0; i < n_items; i++)
-       {
-               gchar *id = NULL;
-
-               if (g_menu_model_get_item_attribute (priv->model, i, "id", "s", &id) &&
-                   strcmp (id, extension_point) == 0)
-               {
-                       GMenuModel *section;
-
-                       section = g_menu_model_get_item_link (priv->model, i, G_MENU_LINK_SECTION);
-                       if (section != NULL)
-                       {
-                               g_menu_item_set_attribute (item, "gedit-merge-id", "u", merge_id);
-                               g_menu_append_item (G_MENU (section), item);
-
-                               g_free (id);
-                               break;
-                       }
-               }
-
-               g_free (id);
-       }
+       return g_object_new (GEDIT_TYPE_MENU, "model", model, NULL);
 }
 
 void
-gedit_menu_remove_items (GeditMenu *menu,
-                         guint      merge_id)
+gedit_menu_add_menu_item (GeditMenu *menu,
+                          GMenuItem *item)
 {
        GeditMenuPrivate *priv;
-       gint i, n_items;
 
        g_return_if_fail (GEDIT_IS_MENU (menu));
+       g_return_if_fail (G_IS_MENU_ITEM (item));
 
        priv = gedit_menu_get_instance_private (menu);
-       n_items = g_menu_model_get_n_items (priv->model);
 
-       for (i = 0; i < n_items; i++)
+       if (priv->model != NULL)
        {
-               GMenuModel *section;
-
-               section = g_menu_model_get_item_link (priv->model, i, G_MENU_LINK_SECTION);
-               if (section != NULL)
-               {
-                       gint j, n_items2;
-
-                       n_items2 = g_menu_model_get_n_items (section);
-                       j = 0;
-                       while (j < n_items2)
-                       {
-                               guint id = 0;
-
-                               if (g_menu_model_get_item_attribute (section, j, "gedit-merge-id", "u", &id) 
&&
-                                   id == merge_id)
-                               {
-                                       g_menu_remove (G_MENU (section), j);
-                                       n_items2--;
-                               }
-                               else
-                               {
-                                       j++;
-                               }
-                       }
-               }
+               g_menu_item_set_attribute (item, "gedit-merge-id", "u", priv->merge_id);
+               g_menu_append_item (G_MENU (priv->model), item);
        }
 }
 
diff --git a/gedit/gedit-menu.h b/gedit/gedit-menu.h
index e278154..0952af2 100644
--- a/gedit/gedit-menu.h
+++ b/gedit/gedit-menu.h
@@ -52,15 +52,8 @@ GType                     gedit_menu_get_type            (void) G_GNUC_CONST;
 
 GeditMenu                *_gedit_menu_new                (GMenuModel  *model);
 
-guint                     gedit_menu_new_merge_id        (GeditMenu   *menu);
-
 void                      gedit_menu_add_menu_item       (GeditMenu   *menu,
-                                                          GMenuItem   *item,
-                                                          guint        merge_id,
-                                                          const gchar *extension_point);
-
-void                      gedit_menu_remove_items        (GeditMenu   *menu,
-                                                          guint        merge_id);
+                                                          GMenuItem   *item);
 
 G_END_DECLS
 
diff --git a/gedit/gedit-window-activatable.c b/gedit/gedit-window-activatable.c
index ce7ee41..655b98c 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,39 @@ gedit_window_activatable_update_state (GeditWindowActivatable *activatable)
        }
 }
 
+GeditMenu *
+gedit_window_activatable_get_menu_ext (GeditWindowActivatable *activatable,
+                                       const gchar            *extension_point)
+{
+       GeditMenu *menu = NULL;
+       GeditWindow *window;
+       GMenuModel *model;
+       gint i, n_items;
+
+       g_return_val_if_fail (GEDIT_IS_WINDOW_ACTIVATABLE (activatable), 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_new (section);
+               }
+
+               g_free (id);
+       }
+
+       return menu;
+}
+
diff --git a/gedit/gedit-window-activatable.h b/gedit/gedit-window-activatable.h
index b55509e..de5e962 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.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_get_menu_ext:
+ * @activatable: A #GeditWindowActivatable.
+ * @extension_point: the extension point section of the menu to get.
+ *
+ * Gets the GeditMenu for @extension_point.
+ *
+ * Returns: (transfer full): a #GeditMenu for the specific section or %NULL if not found.
+ */
+GeditMenu *gedit_window_activatable_get_menu_ext (GeditWindowActivatable *activatable,
+                                                  const gchar            *extension_point);
+
 G_END_DECLS
 
 #endif /* __GEDIT_WINDOW_ACTIVATABLE_H__ */
diff --git a/gedit/gedit-window-private.h b/gedit/gedit-window-private.h
index b3ff545..ac2a9d6 100644
--- a/gedit/gedit-window-private.h
+++ b/gedit/gedit-window-private.h
@@ -93,7 +93,6 @@ struct _GeditWindowPrivate
        GtkWidget      *open_button;
        GtkWidget      *open_menu;
        GMenuModel     *gear_menu_model;
-       GeditMenu      *gear_menu;
 
        /* recent files */
        guint           update_documents_list_menu_id;
diff --git a/gedit/gedit-window.c b/gedit/gedit-window.c
index e4b4055..40ba4a0 100644
--- a/gedit/gedit-window.c
+++ b/gedit/gedit-window.c
@@ -237,7 +237,6 @@ gedit_window_dispose (GObject *object)
                window->priv->update_documents_list_menu_id = 0;
        }
 
-       g_clear_object (&window->priv->gear_menu);
        g_clear_object (&window->priv->manager);
        g_clear_object (&window->priv->message_bus);
        g_clear_object (&window->priv->window_group);
@@ -3216,8 +3215,6 @@ gedit_window_init (GeditWindow *window)
                                         G_N_ELEMENTS (win_entries),
                                         window);
 
-       window->priv->gear_menu = _gedit_menu_new (window->priv->gear_menu_model);
-
        window->priv->window_group = gtk_window_group_new ();
        gtk_window_group_add_window (window->priv->window_group, GTK_WINDOW (window));
 
@@ -4125,19 +4122,19 @@ gedit_window_get_message_bus (GeditWindow *window)
 }
 
 /**
- * gedit_window_get_gear_menu:
+ * _gedit_window_get_gear_menu:
  * @window: a #GeditWindow.
  *
  * Gets the gear menu.
  *
- * Returns: (transfer none): the #GeditMenu of the gear menu button.
+ * Returns: (transfer none): the #GMenuModel of the gear menu button.
  */
-GeditMenu *
-gedit_window_get_gear_menu (GeditWindow *window)
+GMenuModel *
+_gedit_window_get_gear_menu (GeditWindow *window)
 {
        g_return_val_if_fail (GEDIT_IS_WINDOW (window), NULL);
 
-       return window->priv->gear_menu;
+       return window->priv->gear_menu_model;
 }
 
 /* ex:set ts=8 noet: */
diff --git a/gedit/gedit-window.h b/gedit/gedit-window.h
index b44f19c..6dba3ef 100644
--- a/gedit/gedit-window.h
+++ b/gedit/gedit-window.h
@@ -36,7 +36,6 @@
 
 #include <gedit/gedit-tab.h>
 #include <gedit/gedit-panel.h>
-#include <gedit/gedit-menu.h>
 #include <gedit/gedit-message-bus.h>
 
 G_BEGIN_DECLS
@@ -151,7 +150,7 @@ GeditTab        *gedit_window_get_tab_from_location (GeditWindow         *window
 /* Message bus */
 GeditMessageBus        *gedit_window_get_message_bus           (GeditWindow         *window);
 
-GeditMenu       *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 f5ba158..4cedcd3 100644
--- a/plugins/quickopen/quickopen/__init__.py
+++ b/plugins/quickopen/quickopen/__init__.py
@@ -58,9 +58,6 @@ class QuickOpenPlugin(GObject.Object, Gedit.WindowActivatable):
         self._popup_size = size
 
     def _uninstall_menu(self):
-        gear_menu = self.window.get_gear_menu()
-        gear_menu.remove_items(self.merge_id)
-
         self.window.remove_action("quickopen")
 
     def _install_menu(self):
@@ -71,9 +68,8 @@ class QuickOpenPlugin(GObject.Object, Gedit.WindowActivatable):
         item = Gio.MenuItem.new(_("Quick Open..."), "win.quickopen")
         item.set_attribute_value("accel", GLib.Variant.new_string("<Primary><Alt>O"))
 
-        gear_menu = self.window.get_gear_menu()
-        self.merge_id = gear_menu.new_merge_id()
-        gear_menu.add_menu_item (item, self.merge_id, "ext2")
+        self.menu = self.get_menu_ext("ext2")
+        self.menu.add_menu_item(item)
 
     def _create_popup(self):
         paths = []


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