[gedit/wip/tabs] Add items in an idle



commit 36ef6c20ba7ad1c03b4e532b0b8134f3e5903c85
Author: Ignacio Casal Quinteiro <ignacio casal nice-software com>
Date:   Thu Jul 11 16:04:28 2013 +0200

    Add items in an idle

 gedit/gedit-notebook.c |   71 ++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 69 insertions(+), 2 deletions(-)
---
diff --git a/gedit/gedit-notebook.c b/gedit/gedit-notebook.c
index 47e5ea1..f0b0128 100644
--- a/gedit/gedit-notebook.c
+++ b/gedit/gedit-notebook.c
@@ -62,6 +62,7 @@ struct _GeditNotebookPrivate
 
        GtkWidget     *documents_button;
        GMenu         *documents_menu;
+       GSimpleActionGroup *documents_action_group;
 
        GList         *focused_pages;
 
@@ -165,9 +166,11 @@ gedit_notebook_set_property (GObject      *object,
 static void
 gedit_notebook_dispose (GObject *object)
 {
-       GeditNotebook *notebook = GEDIT_NOTEBOOK (object);
+       GeditNotebookPrivate *priv = GEDIT_NOTEBOOK (object)->priv;
 
-       g_clear_object (&notebook->priv->ui_settings);
+       g_clear_object (&priv->ui_settings);
+       g_clear_object (&priv->documents_menu);
+       g_clear_object (&priv->documents_action_group);
 
        G_OBJECT_CLASS (gedit_notebook_parent_class)->dispose (object);
 }
@@ -436,6 +439,12 @@ gedit_notebook_page_removed (GtkNotebook *notebook,
                /* If there is no tabs, calling this is pointless */
                update_tabs_visibility (nb, FALSE);
        }
+       else
+       {
+               /* Unset the menu model from the button to make it insensitive */
+               gtk_menu_button_set_menu_model (GTK_MENU_BUTTON (nb->priv->documents_button),
+                                               NULL);
+       }
 }
 
 static void
@@ -459,6 +468,12 @@ gedit_notebook_page_added (GtkNotebook *notebook,
                          nb);
 
        update_tabs_visibility (GEDIT_NOTEBOOK (notebook), FALSE);
+
+       if (!gtk_menu_button_get_menu_model (GTK_MENU_BUTTON (nb->priv->documents_button)))
+       {
+               gtk_menu_button_set_menu_model (GTK_MENU_BUTTON (nb->priv->documents_button),
+                                               G_MENU_MODEL (nb->priv->documents_menu));
+       }
 }
 
 static void
@@ -556,10 +571,51 @@ gedit_notebook_new (void)
        return GTK_WIDGET (g_object_new (GEDIT_TYPE_NOTEBOOK, NULL));
 }
 
+static gboolean
+populate_menu (GeditNotebook *notebook)
+{
+       GeditNotebookPrivate *priv = notebook->priv;
+       GList *l, *children;
+       gint i;
+
+       g_menu_remove_all (priv->documents_menu);
+
+       children = gtk_container_get_children (GTK_CONTAINER (notebook));
+       for (l = children, i = 0; l != NULL; l = g_list_next (l), i++)
+       {
+               GeditTab *tab = GEDIT_TAB (l->data);
+               GMenuItem *item;
+               gchar *tab_name;
+               gchar *name;
+
+               tab_name = _gedit_tab_get_name (GEDIT_TAB (tab));
+               name = gedit_utils_escape_underscores (tab_name, -1);
+               g_free (tab_name);
+
+               item = g_menu_item_new (name, NULL);
+               g_free (name);
+               g_menu_item_set_action_and_target (item, "notebook.page", "i", i);
+
+               g_menu_append_item (priv->documents_menu, item);
+       }
+
+       g_list_free (children);
+
+       return G_SOURCE_REMOVE;
+}
+
+static void
+on_documents_button_toggled (GtkToggleButton *button,
+                             GeditNotebook   *notebook)
+{
+       g_idle_add ((GSourceFunc)populate_menu, notebook);
+}
+
 static void
 gedit_notebook_init (GeditNotebook *notebook)
 {
        GeditNotebookPrivate *priv;
+       GPropertyAction *action;
 
        notebook->priv = GEDIT_NOTEBOOK_GET_PRIVATE (notebook);
        priv = notebook->priv;
@@ -569,6 +625,14 @@ gedit_notebook_init (GeditNotebook *notebook)
        priv->ui_settings = g_settings_new ("org.gnome.gedit.preferences.ui");
 
        priv->documents_menu = g_menu_new ();
+       priv->documents_action_group = g_simple_action_group_new ();
+       action = g_property_action_new ("page", notebook, "page");
+       g_simple_action_group_insert (priv->documents_action_group, G_ACTION (action));
+
+       gtk_widget_insert_action_group (GTK_WIDGET (notebook),
+                                       "notebook",
+                                       G_ACTION_GROUP (priv->documents_action_group));
+       g_object_unref (action);
 
        priv->show_tabs_mode = GEDIT_NOTEBOOK_SHOW_TABS_ALWAYS;
        priv->close_buttons_sensitive = TRUE;
@@ -585,6 +649,9 @@ gedit_notebook_init (GeditNotebook *notebook)
                         notebook,
                         "show-tabs-mode",
                         G_SETTINGS_BIND_GET | G_SETTINGS_BIND_SET);
+
+       g_signal_connect (priv->documents_button, "toggled",
+                         G_CALLBACK (on_documents_button_toggled), notebook);
 }
 
 static GtkWidget *


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