[gedit/wip/redesign2: 1/2] Add GeditMenu



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

    Add GeditMenu

 gedit/Makefile.am            |    2 +
 gedit/gedit-menu.c           |  217 ++++++++++++++++++++++++++++++++++++++++++
 gedit/gedit-menu.h           |   67 +++++++++++++
 gedit/gedit-window-private.h |    3 +-
 gedit/gedit-window.c         |   11 ++-
 gedit/gedit-window.h         |    3 +-
 gedit/gedit-window.ui        |    6 +-
 7 files changed, 300 insertions(+), 9 deletions(-)
---
diff --git a/gedit/Makefile.am b/gedit/Makefile.am
index e68240c..8aad6e6 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.h                    \
        gedit-message-bus.h             \
        gedit-message.h                 \
        gedit-panel.h                   \
@@ -189,6 +190,7 @@ 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
new file mode 100644
index 0000000..2cdbba5
--- /dev/null
+++ b/gedit/gedit-menu.c
@@ -0,0 +1,217 @@
+/*
+ * gedit-menu.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.h"
+#include <string.h>
+
+
+typedef struct _GeditMenuPrivate
+{
+       GMenuModel *model;
+       guint last_merge_id;
+} GeditMenuPrivate;
+
+enum
+{
+       PROP_0,
+       PROP_MODEL
+};
+
+G_DEFINE_TYPE_WITH_PRIVATE (GeditMenu, gedit_menu, G_TYPE_OBJECT)
+
+static void
+gedit_menu_dispose (GObject *object)
+{
+       GeditMenu *menu = GEDIT_MENU (object);
+       GeditMenuPrivate *priv = gedit_menu_get_instance_private (menu);
+
+       g_clear_object (&priv->model);
+
+       G_OBJECT_CLASS (gedit_menu_parent_class)->dispose (object);
+}
+
+static void
+gedit_menu_get_property (GObject    *object,
+                         guint       prop_id,
+                         GValue     *value,
+                         GParamSpec *pspec)
+{
+       GeditMenu *menu = GEDIT_MENU (object);
+       GeditMenuPrivate *priv = gedit_menu_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_set_property (GObject     *object,
+                         guint         prop_id,
+                         const GValue *value,
+                         GParamSpec   *pspec)
+{
+       GeditMenu *menu = GEDIT_MENU (object);
+       GeditMenuPrivate *priv = gedit_menu_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_class_init (GeditMenuClass *klass)
+{
+       GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+       object_class->dispose = gedit_menu_dispose;
+       object_class->get_property = gedit_menu_get_property;
+       object_class->set_property = gedit_menu_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_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;
+}
+
+void
+gedit_menu_add_menu_item (GeditMenu   *menu,
+                          GMenuItem   *item,
+                          guint        merge_id,
+                          const gchar *extension_point)
+{
+       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, "mergeid", "u", merge_id);
+                               g_menu_append_item (G_MENU (section), item);
+
+                               g_free (id);
+                               break;
+                       }
+               }
+
+               g_free (id);
+       }
+}
+
+void
+gedit_menu_remove_items (GeditMenu *menu,
+                         guint      merge_id)
+{
+       GeditMenuPrivate *priv;
+       gint i, n_items;
+
+       g_return_if_fail (GEDIT_IS_MENU (menu));
+
+       priv = gedit_menu_get_instance_private (menu);
+       n_items = g_menu_model_get_n_items (priv->model);
+
+       for (i = 0; i < n_items; i++)
+       {
+               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, "mergeid", "u", &id) &&
+                                   id == merge_id)
+                               {
+                                       g_menu_remove (G_MENU (section), j);
+                                       n_items2--;
+                               }
+                               else
+                               {
+                                       j++;
+                               }
+                       }
+               }
+       }
+}
+
+/* ex:set ts=8 noet: */
diff --git a/gedit/gedit-menu.h b/gedit/gedit-menu.h
new file mode 100644
index 0000000..e278154
--- /dev/null
+++ b/gedit/gedit-menu.h
@@ -0,0 +1,67 @@
+/*
+ * gedit-menu.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_H__
+#define __GEDIT_MENU_H__
+
+#include <glib-object.h>
+#include <gio/gio.h>
+
+G_BEGIN_DECLS
+
+#define GEDIT_TYPE_MENU                        (gedit_menu_get_type ())
+#define GEDIT_MENU(obj)                        (G_TYPE_CHECK_INSTANCE_CAST ((obj), GEDIT_TYPE_MENU, 
GeditMenu))
+#define GEDIT_MENU_CONST(obj)          (G_TYPE_CHECK_INSTANCE_CAST ((obj), GEDIT_TYPE_MENU, GeditMenu const))
+#define GEDIT_MENU_CLASS(klass)                (G_TYPE_CHECK_CLASS_CAST ((klass), GEDIT_TYPE_MENU, 
GeditMenuClass))
+#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_GET_CLASS(obj)      (G_TYPE_INSTANCE_GET_CLASS ((obj), GEDIT_TYPE_MENU, GeditMenuClass))
+
+typedef struct _GeditMenu              GeditMenu;
+typedef struct _GeditMenuClass         GeditMenuClass;
+
+struct _GeditMenu
+{
+       GObject parent;
+};
+
+struct _GeditMenuClass
+{
+       GObjectClass parent_class;
+};
+
+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);
+
+G_END_DECLS
+
+#endif /* __GEDIT_MENU_H__ */
diff --git a/gedit/gedit-window-private.h b/gedit/gedit-window-private.h
index a5229f7..b3ff545 100644
--- a/gedit/gedit-window-private.h
+++ b/gedit/gedit-window-private.h
@@ -92,7 +92,8 @@ struct _GeditWindowPrivate
        GtkWidget      *headerbar;
        GtkWidget      *open_button;
        GtkWidget      *open_menu;
-       GMenuModel     *gear_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 00d2108..e4b4055 100644
--- a/gedit/gedit-window.c
+++ b/gedit/gedit-window.c
@@ -237,6 +237,7 @@ 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);
@@ -432,7 +433,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);
@@ -3215,6 +3216,8 @@ 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,11 +4128,11 @@ gedit_window_get_message_bus (GeditWindow *window)
  * 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.
+ * Returns: (transfer none): the #GeditMenu of the gear menu button.
  */
-GMenuModel *
+GeditMenu *
 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..b44f19c 100644
--- a/gedit/gedit-window.h
+++ b/gedit/gedit-window.h
@@ -36,6 +36,7 @@
 
 #include <gedit/gedit-tab.h>
 #include <gedit/gedit-panel.h>
+#include <gedit/gedit-menu.h>
 #include <gedit/gedit-message-bus.h>
 
 G_BEGIN_DECLS
@@ -150,7 +151,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);
+GeditMenu       *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>


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