[balsa/gtk3] More GAction/GtkApplicationWindow helpers



commit 6b0990e37655ebc3236d70e09ac0a04e48ed9021
Author: Peter Bloomfield <PeterBloomfield bellsouth net>
Date:   Sun Jul 28 14:41:04 2013 -0400

    More GAction/GtkApplicationWindow helpers
    
        * libbalsa/application-helpers.c (libbalsa_window_get_menu_bar):
        add callback-data argument;
        (libbalsa_window_add_accelerator): new public method;
        (libbalsa_radio_activated): common callback for the "activated"
        signal of a radio-GAction.
        * libbalsa/application-helpers.h: new API.
        * libbalsa/source-viewer.c (libbalsa_show_message_source): use
        new API.
        * src/main-window.c (bw_set_menus): ditto,

 ChangeLog                      |   12 ++++
 libbalsa/application-helpers.c |  116 +++++++++++++++++++++++++++++++++------
 libbalsa/application-helpers.h |   18 +++++--
 libbalsa/source-viewer.c       |    2 +-
 src/main-window.c              |   17 +-----
 5 files changed, 127 insertions(+), 38 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 7428789..046139b 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,15 @@
+2013-07-28  Peter Bloomfield
+
+       * libbalsa/application-helpers.c (libbalsa_window_get_menu_bar):
+       add callback-data argument;
+       (libbalsa_window_add_accelerator): new public method;
+       (libbalsa_radio_activated): common callback for the "activated"
+       signal of a radio-GAction.
+       * libbalsa/application-helpers.h: new API.
+       * libbalsa/source-viewer.c (libbalsa_show_message_source): use
+       new API.
+       * src/main-window.c (bw_set_menus): ditto,
+
 2013-07-23  Peter Bloomfield
 
        * src/main-window.c (bw_alt_n_cb), (bw_set_alt_bindings),
diff --git a/libbalsa/application-helpers.c b/libbalsa/application-helpers.c
index 923c686..399f585 100644
--- a/libbalsa/application-helpers.c
+++ b/libbalsa/application-helpers.c
@@ -28,22 +28,6 @@
 #include <string.h>
 #include "application-helpers.h"
 
-/*
- * libbalsa_window_get_menu_bar
- *
- * Construct a menu-bar for a GtkApplicationWindow that does not use the
- * GApplication's menubar
- *
- * window       the GtkApplicationWindow
- * entries      array of GActionEntry structures
- * n_entries    length of the array
- * ui_file      filename for GtkBuilder input defining a menu named
- *              "menubar"
- * error        GError for returning error information
- *
- * returns:     the GtkMenuBar
- */
-
 typedef struct {
     GAction  *action;
     GVariant *parameter;
@@ -155,22 +139,41 @@ get_accel_group(GMenuModel * model,
     return accel_group;
 }
 
+/*
+ * libbalsa_window_get_menu_bar
+ *
+ * Construct a menu-bar for a GtkApplicationWindow that does not use the
+ * GApplication's menubar
+ *
+ * window       the GtkApplicationWindow
+ * entries      array of GActionEntry structures
+ * n_entries    length of the array
+ * ui_file      filename for GtkBuilder input defining a menu named
+ *              "menubar"
+ * error        GError for returning error information
+ * cb_data      user data for GAction callbacks
+ *
+ * returns:     the GtkMenuBar
+ */
+
 GtkWidget *
 libbalsa_window_get_menu_bar(GtkApplicationWindow * window,
                              const GActionEntry   * entries,
                              gint                   n_entries,
                              const gchar          * ui_file,
-                             GError              ** error)
+                             GError              ** error,
+                             gpointer               cb_data)
 {
     GActionMap *map = G_ACTION_MAP(window);
     GtkBuilder *builder;
     GtkWidget *menu_bar = NULL;
 
-    g_action_map_add_action_entries(map, entries, n_entries, window);
+    g_action_map_add_action_entries(map, entries, n_entries, cb_data);
 
     builder = gtk_builder_new();
     if (gtk_builder_add_from_file(builder, ui_file, error)) {
         GMenuModel *menu_model;
+        GSList *accel_groups;
         GtkAccelGroup *accel_group;
 
         menu_model =
@@ -178,6 +181,12 @@ libbalsa_window_get_menu_bar(GtkApplicationWindow * window,
 
         menu_bar = gtk_menu_bar_new_from_model(menu_model);
 
+        /* Remove main-window accelerators: */
+        accel_groups = gtk_accel_groups_from_object(G_OBJECT(window));
+        if (accel_groups)
+            /* Last is first... */
+            gtk_window_remove_accel_group(GTK_WINDOW(window),
+                                          accel_groups->data);
         accel_group = get_accel_group(menu_model, map);
         gtk_window_add_accel_group(GTK_WINDOW(window), accel_group);
         gtk_application_window_set_show_menubar(window, FALSE);
@@ -188,6 +197,58 @@ libbalsa_window_get_menu_bar(GtkApplicationWindow * window,
 }
 
 /*
+ * libbalsa_window_add_accelerator
+ *
+ * Add an accelerator key combination for an action
+ *
+ * window       the GtkApplicationWindow
+ * accel        the accelerator string
+ * action_name  name of the GAction
+ */
+
+void
+libbalsa_window_add_accelerator(GtkApplicationWindow * window,
+                                const gchar          * accel,
+                                const gchar          * action_name)
+{
+    GActionMap *action_map = G_ACTION_MAP(window);
+    guint accel_key;
+    GdkModifierType accel_mods;
+    const gchar *basename;
+    GAction *action;
+    AccelInfo *info;
+    GClosure *closure;
+    GtkAccelGroup *accel_group;
+
+    gtk_accelerator_parse(accel, &accel_key, &accel_mods);
+    if (!accel_key) {
+        g_print("%s: could not parse accelerator \"%s\"\n", __func__,
+                accel);
+        return;
+    }
+
+    basename = strchr(action_name, '.');
+    basename = basename ? basename + 1 : action_name;
+    action = g_action_map_lookup_action(action_map, basename);
+    if (!action) {
+        g_print("%s: could not lookup action \"%s\"\n", __func__,
+                action_name);
+        return;
+    }
+
+    info = g_new(AccelInfo, 1);
+    info->action = action;
+    info->parameter = NULL;
+    closure = g_cclosure_new(G_CALLBACK(accel_activate), info,
+                             (GClosureNotify) accel_info_free);
+
+    accel_group = gtk_accel_group_new();
+    gtk_accel_group_connect(accel_group, accel_key, accel_mods, 0,
+                            closure);
+    gtk_window_add_accel_group(GTK_WINDOW(window), accel_group);
+}
+
+/*
  * libbalsa_toggle_activated
  *
  * Callback for the "activated" signal of GAction with a boolean state
@@ -211,3 +272,22 @@ libbalsa_toggle_activated(GSimpleAction * action,
     g_action_change_state(G_ACTION(action), g_variant_new_boolean(!state));
     g_variant_unref(action_state);
 }
+
+/*
+ * libbalsa_radio_activated
+ *
+ * Callback for the "activated" signal of GAction with a string state
+ * Store the new state
+ *
+ * action       the GAction
+ * parameter    the new state
+ * user_data    not used
+ */
+
+void
+libbalsa_radio_activated(GSimpleAction * action,
+                         GVariant      * parameter,
+                         gpointer        user_data)
+{
+    g_action_change_state(G_ACTION(action), parameter);
+}
diff --git a/libbalsa/application-helpers.h b/libbalsa/application-helpers.h
index 3c5889c..b5da159 100644
--- a/libbalsa/application-helpers.h
+++ b/libbalsa/application-helpers.h
@@ -35,9 +35,19 @@ GtkWidget *libbalsa_window_get_menu_bar(GtkApplicationWindow * window,
                                         const GActionEntry   * entries,
                                         gint                   n_entries,
                                         const gchar          * ui_file,
-                                        GError              ** error);
-void libbalsa_toggle_activated(GSimpleAction * action,
-                               GVariant      * parameter,
-                               gpointer        user_data);
+                                        GError              ** error,
+                                        gpointer               cb_data);
+
+void libbalsa_window_add_accelerator   (GtkApplicationWindow * window,
+                                        const gchar          * accel,
+                                        const gchar          * action_name);
+
+void libbalsa_toggle_activated         (GSimpleAction        * action,
+                                        GVariant             * parameter,
+                                        gpointer               user_data);
+
+void libbalsa_radio_activated          (GSimpleAction        * action,
+                                        GVariant             * parameter,
+                                        gpointer               user_data);
 
 #endif                         /* __LIBBALSA_APPLICATION_HELPERS_H__ */
diff --git a/libbalsa/source-viewer.c b/libbalsa/source-viewer.c
index 5aff165..7c3bf2b 100644
--- a/libbalsa/source-viewer.c
+++ b/libbalsa/source-viewer.c
@@ -230,7 +230,7 @@ libbalsa_show_message_source(GtkApplication  * application,
     menu_bar = libbalsa_window_get_menu_bar(GTK_APPLICATION_WINDOW(window),
                                             win_entries,
                                             G_N_ELEMENTS(win_entries),
-                                            ui_file, &err);
+                                            ui_file, &err, window);
     if (!menu_bar) {
         libbalsa_information(LIBBALSA_INFORMATION_WARNING,
                              _("Error adding from %s: %s\n"), ui_file,
diff --git a/src/main-window.c b/src/main-window.c
index d2010ad..2ca27cb 100644
--- a/src/main-window.c
+++ b/src/main-window.c
@@ -861,19 +861,6 @@ bw_actions_set_enabled(BalsaWindow         * window,
  */
 
 /*
- * Common callback for the "activated" signal of a radio action
- */
-
-static void
-radio_activated(GSimpleAction * action,
-                GVariant      * parameter,
-                gpointer        user_data)
-{
-    g_action_change_state(G_ACTION(action), parameter);
-}
-
-
-/*
  * Helper for some show/hide actions
  */
 
@@ -1988,9 +1975,9 @@ bw_set_menus(BalsaWindow * window)
                                   show_sos_bar_change_state},
         {"wrap",                  libbalsa_toggle_activated, NULL, "false",
                                   wrap_change_state},
-        {"headers",               radio_activated, "s", "'none'",
+        {"headers",               libbalsa_radio_activated, "s", "'none'",
                                   header_change_state},
-        {"threading",             radio_activated, "s", "'flat'",
+        {"threading",             libbalsa_radio_activated, "s", "'flat'",
                                   threading_change_state},
         {"expand-all",            expand_all_activated},
         {"collapse-all",          collapse_all_activated},


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