[gedit/wip/redesign2] Add GeditMenuHelper



commit ffda5d66c967616f6172299a1ca18a3ca145f36a
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-helper.c               |  181 +++++++++++++++++++++++++++++++
 gedit/gedit-menu-helper.h               |   62 +++++++++++
 gedit/gedit-window-activatable.c        |   38 +++++++
 gedit/gedit-window-activatable.h        |   13 +++
 gedit/gedit-window-private.h            |    2 +-
 gedit/gedit-window.c                    |   10 +-
 gedit/gedit-window.h                    |    2 +-
 gedit/gedit-window.ui                   |    6 +-
 plugins/quickopen/quickopen/__init__.py |   28 +----
 10 files changed, 311 insertions(+), 33 deletions(-)
---
diff --git a/gedit/Makefile.am b/gedit/Makefile.am
index e68240c..91a2351 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-helper.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-helper.c             \
        gedit-resources.c
 
 libgedit_c_files =                     \
diff --git a/gedit/gedit-menu-helper.c b/gedit/gedit-menu-helper.c
new file mode 100644
index 0000000..152e746
--- /dev/null
+++ b/gedit/gedit-menu-helper.c
@@ -0,0 +1,181 @@
+/*
+ * gedit-menu-helper.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-helper.h"
+#include <string.h>
+
+static guint last_merge_id = 0;
+
+
+typedef struct _GeditMenuHelperPrivate
+{
+       GMenuModel *model;
+       guint merge_id;
+       gboolean dispose_has_run;
+} GeditMenuHelperPrivate;
+
+enum
+{
+       PROP_0,
+       PROP_MODEL
+};
+
+G_DEFINE_TYPE_WITH_PRIVATE (GeditMenuHelper, gedit_menu_helper, G_TYPE_OBJECT)
+
+static void
+gedit_menu_helper_dispose (GObject *object)
+{
+       GeditMenuHelper *menu = GEDIT_MENU_HELPER (object);
+       GeditMenuHelperPrivate *priv = gedit_menu_helper_get_instance_private (menu);
+
+       if (!priv->dispose_has_run)
+       {
+               gedit_menu_helper_remove_items (menu);
+               priv->dispose_has_run = TRUE;
+       }
+
+       g_clear_object (&priv->model);
+
+       G_OBJECT_CLASS (gedit_menu_helper_parent_class)->dispose (object);
+}
+
+static void
+gedit_menu_helper_get_property (GObject    *object,
+                                guint       prop_id,
+                                GValue     *value,
+                                GParamSpec *pspec)
+{
+       GeditMenuHelper *menu = GEDIT_MENU_HELPER (object);
+       GeditMenuHelperPrivate *priv = gedit_menu_helper_get_instance_private (menu);
+
+       switch (prop_id)
+       {
+               case PROP_MODEL:
+                       g_value_set_object (value, priv->model);
+                       break;
+               default:
+                       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+                       break;
+       }
+}
+
+static void
+gedit_menu_helper_set_property (GObject     *object,
+                                guint         prop_id,
+                                const GValue *value,
+                                GParamSpec   *pspec)
+{
+       GeditMenuHelper *menu = GEDIT_MENU_HELPER (object);
+       GeditMenuHelperPrivate *priv = gedit_menu_helper_get_instance_private (menu);
+
+       switch (prop_id)
+       {
+               case PROP_MODEL:
+                       priv->model = g_value_dup_object (value);
+                       break;
+               default:
+                       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+                       break;
+       }
+}
+
+static void
+gedit_menu_helper_class_init (GeditMenuHelperClass *klass)
+{
+       GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+       object_class->dispose = gedit_menu_helper_dispose;
+       object_class->get_property = gedit_menu_helper_get_property;
+       object_class->set_property = gedit_menu_helper_set_property;
+
+       g_object_class_install_property (object_class,
+                                        PROP_MODEL,
+                                        g_param_spec_object ("model",
+                                                             "Model",
+                                                             "The main menu model",
+                                                             G_TYPE_MENU_MODEL,
+                                                             G_PARAM_READWRITE |
+                                                             G_PARAM_CONSTRUCT_ONLY |
+                                                             G_PARAM_STATIC_STRINGS));
+}
+
+static void
+gedit_menu_helper_init (GeditMenuHelper *menu)
+{
+       GeditMenuHelperPrivate *priv;
+
+       priv = gedit_menu_helper_get_instance_private (menu);
+
+       priv->merge_id = ++last_merge_id;
+}
+
+GeditMenuHelper *
+_gedit_menu_helper_new (GMenuModel *model)
+{
+       return g_object_new (GEDIT_TYPE_MENU, "model", model, NULL);
+}
+
+void
+gedit_menu_helper_add_menu_item (GeditMenuHelper *menu,
+                                 GMenuItem       *item)
+{
+       GeditMenuHelperPrivate *priv;
+
+       g_return_if_fail (GEDIT_IS_MENU (menu));
+       g_return_if_fail (G_IS_MENU_ITEM (item));
+
+       priv = gedit_menu_helper_get_instance_private (menu);
+
+       if (priv->model != NULL)
+       {
+               g_menu_item_set_attribute (item, "gedit-merge-id", "u", priv->merge_id);
+               g_menu_append_item (G_MENU (priv->model), item);
+       }
+}
+
+void
+gedit_menu_helper_remove_items (GeditMenuHelper *menu)
+{
+       GeditMenuHelperPrivate *priv;
+       gint i, n_items;
+
+       priv = gedit_menu_helper_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++;
+               }
+       }
+}
+
+/* ex:set ts=8 noet: */
diff --git a/gedit/gedit-menu-helper.h b/gedit/gedit-menu-helper.h
new file mode 100644
index 0000000..79db8c5
--- /dev/null
+++ b/gedit/gedit-menu-helper.h
@@ -0,0 +1,62 @@
+/*
+ * gedit-menu-helper.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_HELPER_H__
+#define __GEDIT_MENU_HELPER_H__
+
+#include <glib-object.h>
+#include <gio/gio.h>
+
+G_BEGIN_DECLS
+
+#define GEDIT_TYPE_MENU                        (gedit_menu_helper_get_type ())
+#define GEDIT_MENU_HELPER(obj)                 (G_TYPE_CHECK_INSTANCE_CAST ((obj), GEDIT_TYPE_MENU, 
GeditMenuHelper))
+#define GEDIT_MENU_HELPER_CONST(obj)           (G_TYPE_CHECK_INSTANCE_CAST ((obj), GEDIT_TYPE_MENU, 
GeditMenuHelper const))
+#define GEDIT_MENU_HELPER_CLASS(klass)         (G_TYPE_CHECK_CLASS_CAST ((klass), GEDIT_TYPE_MENU, 
GeditMenuHelperClass))
+#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_HELPER_GET_CLASS(obj)       (G_TYPE_INSTANCE_GET_CLASS ((obj), GEDIT_TYPE_MENU, 
GeditMenuHelperClass))
+
+typedef struct _GeditMenuHelper                 GeditMenuHelper;
+typedef struct _GeditMenuHelperClass    GeditMenuHelperClass;
+
+struct _GeditMenuHelper
+{
+       GObject parent;
+};
+
+struct _GeditMenuHelperClass
+{
+       GObjectClass parent_class;
+};
+
+GType                     gedit_menu_helper_get_type            (void) G_GNUC_CONST;
+
+GeditMenuHelper         *_gedit_menu_helper_new                 (GMenuModel        *model);
+
+void                      gedit_menu_helper_add_menu_item       (GeditMenuHelper   *menu,
+                                                                 GMenuItem         *item);
+
+void                      gedit_menu_helper_remove_items        (GeditMenuHelper   *menu);
+
+G_END_DECLS
+
+#endif /* __GEDIT_MENU_HELPER_H__ */
diff --git a/gedit/gedit-window-activatable.c b/gedit/gedit-window-activatable.c
index ce7ee41..142fcda 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)
        }
 }
 
+GeditMenuHelper *
+gedit_window_activatable_get_menu_ext (GeditWindowActivatable *activatable,
+                                       const gchar            *extension_point)
+{
+       GeditMenuHelper *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_helper_new (section);
+               }
+
+               g_free (id);
+       }
+
+       return menu;
+}
+
diff --git a/gedit/gedit-window-activatable.h b/gedit/gedit-window-activatable.h
index b55509e..16cbecc 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-helper.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 #GeditMenuHelper for @extension_point.
+ *
+ * Returns: (transfer full): a #GeditMenuHelper for the specific section or %NULL if not found.
+ */
+GeditMenuHelper *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 a5229f7..ac2a9d6 100644
--- a/gedit/gedit-window-private.h
+++ b/gedit/gedit-window-private.h
@@ -92,7 +92,7 @@ struct _GeditWindowPrivate
        GtkWidget      *headerbar;
        GtkWidget      *open_button;
        GtkWidget      *open_menu;
-       GMenuModel     *gear_menu;
+       GMenuModel     *gear_menu_model;
 
        /* recent files */
        guint           update_documents_list_menu_id;
diff --git a/gedit/gedit-window.c b/gedit/gedit-window.c
index 00d2108..40ba4a0 100644
--- a/gedit/gedit-window.c
+++ b/gedit/gedit-window.c
@@ -432,7 +432,7 @@ gedit_window_class_init (GeditWindowClass *klass)
        gtk_widget_class_bind_template_child_private (widget_class, GeditWindow, side_headerbar);
        gtk_widget_class_bind_template_child_private (widget_class, GeditWindow, headerbar);
        gtk_widget_class_bind_template_child_private (widget_class, GeditWindow, open_menu);
-       gtk_widget_class_bind_template_child_private (widget_class, GeditWindow, gear_menu);
+       gtk_widget_class_bind_template_child_private (widget_class, GeditWindow, gear_menu_model);
        gtk_widget_class_bind_template_child_private (widget_class, GeditWindow, hpaned);
        gtk_widget_class_bind_template_child_private (widget_class, GeditWindow, side_panel);
        gtk_widget_class_bind_template_child_private (widget_class, GeditWindow, vpaned);
@@ -4122,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 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);
 
-       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 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/gedit/gedit-window.ui b/gedit/gedit-window.ui
index 0461dfd..f3ece7f 100644
--- a/gedit/gedit-window.ui
+++ b/gedit/gedit-window.ui
@@ -1,7 +1,7 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <interface>
   <!-- interface-requires gtk+ 3.8 -->
-  <menu id="gear_menu">
+  <menu id="gear_menu_model">
     <section>
       <attribute name="id">ext1</attribute>
     </section>
@@ -191,7 +191,7 @@
               <object class="GtkMenuButton" id="gear_button">
                 <property name="visible">True</property>
                 <property name="valign">center</property>
-                <property name="menu_model">gear_menu</property>
+                <property name="menu_model">gear_menu_model</property>
                 <style>
                   <class name="image-button"/>
                 </style>
@@ -391,7 +391,7 @@
           <object class="GtkMenuButton" id="fullscreen_gear_button">
             <property name="visible">True</property>
             <property name="valign">center</property>
-            <property name="menu_model">gear_menu</property>
+            <property name="menu_model">gear_menu_model</property>
             <style>
               <class name="image-button"/>
             </style>
diff --git a/plugins/quickopen/quickopen/__init__.py b/plugins/quickopen/quickopen/__init__.py
index d2d37b6..4cedcd3 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.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]