[gedit/wip/statusbar-buttons: 1/2] Port the tab width menu button to using GActions.



commit 13595c57b717da0680676b99ae87591cb35d90bf
Author: Steve Frécinaux <code istique net>
Date:   Tue Feb 11 00:49:23 2014 +0100

    Port the tab width menu button to using GActions.

 gedit/gedit-app.c            |   10 +++
 gedit/gedit-app.h            |    1 +
 gedit/gedit-menu.ui          |   25 +++++++
 gedit/gedit-window-private.h |    2 -
 gedit/gedit-window.c         |  149 ++++--------------------------------------
 5 files changed, 48 insertions(+), 139 deletions(-)
---
diff --git a/gedit/gedit-app.c b/gedit/gedit-app.c
index 0c56adc..7345bba 100644
--- a/gedit/gedit-app.c
+++ b/gedit/gedit-app.c
@@ -79,6 +79,7 @@ struct _GeditAppPrivate
        GSettings         *window_settings;
 
        GMenuModel        *window_menu;
+       GMenuModel        *tab_width_menu;
 
        PeasExtensionSet  *extensions;
        GNetworkMonitor   *monitor;
@@ -502,6 +503,7 @@ gedit_app_startup (GApplication *application)
                {
                        app->priv->window_menu = G_MENU_MODEL (gtk_builder_get_object (builder, 
"gear_menu_noappmenu"));
                }
+               app->priv->tab_width_menu = G_MENU_MODEL (gtk_builder_get_object (builder, "tab_width_menu"));
        }
 
        g_object_unref (builder);
@@ -1658,6 +1660,14 @@ _gedit_app_get_window_menu (GeditApp *app)
        return app->priv->window_menu;
 }
 
+GMenuModel *
+_gedit_app_get_tab_width_menu (GeditApp *app)
+{
+       g_return_val_if_fail (GEDIT_IS_APP (app), NULL);
+
+       return app->priv->tab_width_menu;
+}
+
 GeditMenuExtension *
 _gedit_app_extend_menu (GeditApp    *app,
                        const gchar *extension_point)
diff --git a/gedit/gedit-app.h b/gedit/gedit-app.h
index a44ea79..d4cd72a 100644
--- a/gedit/gedit-app.h
+++ b/gedit/gedit-app.h
@@ -136,6 +136,7 @@ void                         _gedit_app_set_default_print_settings  (GeditApp         
*app,
 GObject                        *_gedit_app_get_settings                (GeditApp  *app);
 
 GMenuModel             *_gedit_app_get_window_menu             (GeditApp  *app);
+GMenuModel             *_gedit_app_get_tab_width_menu          (GeditApp  *app);
 
 GeditMenuExtension     *_gedit_app_extend_menu                 (GeditApp    *app,
                                                                 const gchar *extension_point);
diff --git a/gedit/gedit-menu.ui b/gedit/gedit-menu.ui
index b1e356f..8c22906 100644
--- a/gedit/gedit-menu.ui
+++ b/gedit/gedit-menu.ui
@@ -250,4 +250,29 @@
       </item>
     </section>
   </menu>
+  <menu id="tab_width_menu">
+    <section>
+      <item>
+        <attribute name="label">2</attribute>
+        <attribute name="action">win.tab-width</attribute>
+        <attribute name="target" type="u">2</attribute>
+      </item>
+      <item>
+        <attribute name="label">4</attribute>
+        <attribute name="action">win.tab-width</attribute>
+        <attribute name="target" type="u">4</attribute>
+      </item>
+      <item>
+        <attribute name="label">8</attribute>
+        <attribute name="action">win.tab-width</attribute>
+        <attribute name="target" type="u">8</attribute>
+      </item>
+    </section>
+    <section>
+      <item>
+        <attribute name="label" translatable="yes">Use Spaces</attribute>
+        <attribute name="action">win.use-spaces</attribute>
+      </item>
+    </section>
+  </menu>
 </interface>
diff --git a/gedit/gedit-window-private.h b/gedit/gedit-window-private.h
index 6bc0625..abac703 100644
--- a/gedit/gedit-window-private.h
+++ b/gedit/gedit-window-private.h
@@ -65,13 +65,11 @@ struct _GeditWindowPrivate
        /* statusbar and context ids for statusbar messages */
        GtkWidget      *statusbar;
        GtkWidget      *tab_width_combo;
-       GtkWidget      *tab_width_combo_menu;
        GtkWidget      *language_button;
        GtkWidget      *language_button_label;
        guint           generic_message_cid;
        guint           tip_message_cid;
        guint           bracket_match_message_cid;
-       GBinding       *spaces_instead_of_tabs_binding;
        guint           tab_width_id;
        guint           language_changed_id;
 
diff --git a/gedit/gedit-window.c b/gedit/gedit-window.c
index 8befab6..14a9d21 100644
--- a/gedit/gedit-window.c
+++ b/gedit/gedit-window.c
@@ -815,101 +815,17 @@ setup_headerbar_open_button (GeditWindow *window)
 }
 
 static void
-tab_width_combo_item_activated (GtkMenuItem *item,
-                               GeditWindow *window)
-{
-       GeditView *view;
-       guint width_data = 0;
-
-       view = gedit_window_get_active_view (window);
-       if (!view)
-               return;
-
-       width_data = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (item), TAB_WIDTH_DATA));
-       if (width_data == 0)
-               return;
-
-       gtk_source_view_set_tab_width (GTK_SOURCE_VIEW (view), width_data);
-}
-
-static void
-use_spaces_toggled (GtkCheckMenuItem *item,
-                   GeditWindow      *window)
-{
-       GeditView *view;
-       gboolean active;
-
-       view = gedit_window_get_active_view (window);
-       if (!view)
-               return;
-
-       active = gtk_check_menu_item_get_active (item);
-
-       gtk_source_view_set_insert_spaces_instead_of_tabs (GTK_SOURCE_VIEW (view),
-                                                          active);
-}
-
-static void
 create_tab_width_combo (GeditWindow *window)
 {
-       static guint tab_widths[] = { 2, 4, 8 };
-
-       guint i = 0;
-       GtkWidget *item;
-
        window->priv->tab_width_combo = gedit_status_menu_button_new ();
-       window->priv->tab_width_combo_menu = gtk_menu_new ();
-       gtk_menu_button_set_popup (GTK_MENU_BUTTON (window->priv->tab_width_combo),
-                                  window->priv->tab_width_combo_menu);
+       gtk_menu_button_set_menu_model (GTK_MENU_BUTTON (window->priv->tab_width_combo),
+                                       _gedit_app_get_tab_width_menu (GEDIT_APP (g_application_get_default 
())));
        gtk_widget_show (window->priv->tab_width_combo);
        gtk_box_pack_end (GTK_BOX (window->priv->statusbar),
                          window->priv->tab_width_combo,
                          FALSE,
                          TRUE,
                          0);
-
-       for (i = 0; i < G_N_ELEMENTS (tab_widths); i++)
-       {
-               gchar *label = g_strdup_printf ("%u", tab_widths[i]);
-               item = gtk_menu_item_new_with_label (label);
-               g_free (label);
-
-               g_object_set_data (G_OBJECT (item), TAB_WIDTH_DATA, GINT_TO_POINTER (tab_widths[i]));
-
-               g_signal_connect (item,
-                                 "activate",
-                                 G_CALLBACK (tab_width_combo_item_activated),
-                                 window);
-
-               gtk_menu_shell_append (GTK_MENU_SHELL (window->priv->tab_width_combo_menu),
-                                      GTK_WIDGET (item));
-               gtk_widget_show (item);
-       }
-
-       /* Add a hidden item for custom values */
-       item = gtk_menu_item_new ();
-       gtk_menu_shell_append (GTK_MENU_SHELL (window->priv->tab_width_combo_menu),
-                              GTK_WIDGET (item));
-
-       g_signal_connect (item,
-                         "activate",
-                         G_CALLBACK (tab_width_combo_item_activated),
-                         window);
-
-       item = gtk_separator_menu_item_new ();
-       gtk_menu_shell_append (GTK_MENU_SHELL (window->priv->tab_width_combo_menu),
-                              GTK_WIDGET (item));
-       gtk_widget_show (item);
-
-       item = gtk_check_menu_item_new_with_label (_("Use Spaces"));
-       gtk_menu_shell_append (GTK_MENU_SHELL (window->priv->tab_width_combo_menu),
-                              GTK_WIDGET (item));
-       gtk_widget_show (item);
-
-       g_signal_connect (item,
-                         "toggled",
-                         G_CALLBACK (use_spaces_toggled),
-                         window);
 }
 
 static void
@@ -1241,49 +1157,14 @@ tab_width_changed (GObject     *object,
                   GParamSpec  *pspec,
                   GeditWindow *window)
 {
-       GList *items;
-       GList *item;
        guint new_tab_width;
        gchar *label;
-       gboolean found = FALSE;
-
-       items = gtk_container_get_children (GTK_CONTAINER (window->priv->tab_width_combo_menu));
 
        new_tab_width = gtk_source_view_get_tab_width (GTK_SOURCE_VIEW (object));
 
        label = g_strdup_printf (_("Tab Width: %u"), new_tab_width);
        gedit_status_menu_button_set_label (GEDIT_STATUS_MENU_BUTTON (window->priv->tab_width_combo), label);
        g_free (label);
-
-       for (item = items; item; item = item->next)
-       {
-               guint tab_width = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (item->data), TAB_WIDTH_DATA));
-
-               if (tab_width == new_tab_width)
-               {
-                       found = TRUE;
-               }
-
-               if (GTK_IS_SEPARATOR_MENU_ITEM (item->next->data))
-               {
-                       if (!found)
-                       {
-                               /* Show the last menu item with a custom value */
-                               label = g_strdup_printf ("%u", new_tab_width);
-                               gtk_menu_item_set_label (GTK_MENU_ITEM (item->data), label);
-                               g_free (label);
-                               gtk_widget_show (GTK_WIDGET (item->data));
-                       }
-                       else
-                       {
-                               gtk_widget_hide (GTK_WIDGET (item->data));
-                       }
-
-                       break;
-               }
-       }
-
-       g_list_free (items);
 }
 
 static void
@@ -1314,13 +1195,10 @@ update_statusbar (GeditWindow *window,
                  GeditView   *new_view)
 {
        GeditDocument *doc;
-       GList *items;
-       GtkCheckMenuItem *item;
+       GPropertyAction *action;
 
        if (old_view)
        {
-               g_clear_object (&window->priv->spaces_instead_of_tabs_binding);
-
                if (window->priv->tab_width_id)
                {
                        g_signal_handler_disconnect (old_view,
@@ -1336,6 +1214,9 @@ update_statusbar (GeditWindow *window,
 
                        window->priv->language_changed_id = 0;
                }
+
+               g_action_map_remove_action (G_ACTION_MAP (window), "tab-width");
+               g_action_map_remove_action (G_ACTION_MAP (window), "use-spaces");
        }
 
        if (new_view == NULL)
@@ -1353,17 +1234,13 @@ update_statusbar (GeditWindow *window,
        gtk_widget_show (window->priv->tab_width_combo);
        gtk_widget_show (window->priv->language_button);
 
-       /* find the use spaces item */
-       items = gtk_container_get_children (GTK_CONTAINER (window->priv->tab_width_combo_menu));
-       item = GTK_CHECK_MENU_ITEM (g_list_last (items)->data);
-       g_list_free (items);
+       action = g_property_action_new ("tab-width", new_view, "tab-width");
+       g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (action));
+       g_object_unref (action);
 
-       window->priv->spaces_instead_of_tabs_binding =
-               g_object_bind_property (new_view,
-                                       "insert-spaces-instead-of-tabs",
-                                       item,
-                                       "active",
-                                        G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE);
+       action = g_property_action_new ("use-spaces", new_view, "insert-spaces-instead-of-tabs");
+       g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (action));
+       g_object_unref (action);
 
        window->priv->tab_width_id = g_signal_connect (new_view,
                                                       "notify::tab-width",
@@ -2386,8 +2263,6 @@ on_tab_removed (GeditMultiNotebook *multi,
 
        if (tab == gedit_multi_notebook_get_active_tab (multi))
        {
-               g_clear_object (&window->priv->spaces_instead_of_tabs_binding);
-
                if (window->priv->tab_width_id)
                {
                        g_signal_handler_disconnect (view, window->priv->tab_width_id);


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