[tepl] Amtk utils: add function to bind GAction to GtkAction



commit 52b9be0ddbb5d8c93bad73e1ebda6cbdcd543620
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Mon Aug 14 06:58:12 2017 +0200

    Amtk utils: add function to bind GAction to GtkAction
    
    To port an application to GAction gradually.

 amtk/amtk-utils.c                    |   68 ++++++++++++++++++++++++++++++++++
 amtk/amtk-utils.h                    |    9 ++++-
 docs/reference/tepl-3.0-sections.txt |    1 +
 3 files changed, 77 insertions(+), 1 deletions(-)
---
diff --git a/amtk/amtk-utils.c b/amtk/amtk-utils.c
index c5d1eaa..589b8fb 100644
--- a/amtk/amtk-utils.c
+++ b/amtk/amtk-utils.c
@@ -182,3 +182,71 @@ amtk_utils_recent_chooser_menu_get_item_uri (GtkRecentChooserMenu *menu,
        g_strfreev (all_uris);
        return item_uri;
 }
+
+G_GNUC_BEGIN_IGNORE_DEPRECATIONS
+static void
+gtk_action_activate_cb (GtkAction *gtk_action,
+                       GAction   *g_action)
+{
+       g_action_activate (g_action, NULL);
+}
+
+/**
+ * amtk_utils_bind_g_action_to_gtk_action:
+ * @g_action_map: a #GActionMap.
+ * @g_action_name: a #GAction name present in @g_action_map.
+ * @gtk_action_group: a #GtkActionGroup.
+ * @gtk_action_name: a #GtkAction name present in @gtk_action_group.
+ *
+ * Utility function to be able to port an application gradually to #GAction,
+ * when #GtkUIManager and #GtkAction are still used. Porting to #GAction should
+ * be the first step.
+ *
+ * This function:
+ * - Calls g_action_activate() (with a %NULL #GVariant parameter) when the
+ *   #GtkAction #GtkAction::activate signal is emitted.
+ * - Binds the #GAction #GAction:enabled property to the #GtkAction
+ *   #GtkAction:sensitive property. The binding is done with the
+ *   %G_BINDING_BIDIRECTIONAL and %G_BINDING_SYNC_CREATE flags, the source is
+ *   the #GAction and the target is the #GtkAction.
+ *
+ * When using this function, you should set the callback to %NULL in the
+ * corresponding #GtkActionEntry.
+ *
+ * Since: 3.0
+ */
+void
+amtk_utils_bind_g_action_to_gtk_action (GActionMap     *g_action_map,
+                                       const gchar    *g_action_name,
+                                       GtkActionGroup *gtk_action_group,
+                                       const gchar    *gtk_action_name)
+{
+       GAction *g_action;
+       const GVariantType *param_type;
+       GtkAction *gtk_action;
+
+       g_return_if_fail (G_IS_ACTION_MAP (g_action_map));
+       g_return_if_fail (g_action_name != NULL);
+       g_return_if_fail (GTK_IS_ACTION_GROUP (gtk_action_group));
+       g_return_if_fail (gtk_action_name != NULL);
+
+       g_action = g_action_map_lookup_action (g_action_map, g_action_name);
+       g_return_if_fail (g_action != NULL);
+
+       param_type = g_action_get_parameter_type (g_action);
+       g_return_if_fail (param_type == NULL);
+
+       gtk_action = gtk_action_group_get_action (gtk_action_group, gtk_action_name);
+       g_return_if_fail (gtk_action != NULL);
+
+       g_signal_connect_object (gtk_action,
+                                "activate",
+                                G_CALLBACK (gtk_action_activate_cb),
+                                g_action,
+                                0);
+
+       g_object_bind_property (g_action, "enabled",
+                               gtk_action, "sensitive",
+                               G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE);
+}
+G_GNUC_END_IGNORE_DEPRECATIONS
diff --git a/amtk/amtk-utils.h b/amtk/amtk-utils.h
index 393bdb2..d3ac23b 100644
--- a/amtk/amtk-utils.h
+++ b/amtk/amtk-utils.h
@@ -38,11 +38,18 @@ gchar *             _amtk_utils_replace_home_dir_with_tilde         (const gchar 
*filename);
 G_GNUC_INTERNAL
 gchar **       _amtk_utils_strv_copy                           (const gchar * const *strv);
 
-/* Widget utilities */
+/* GTK+ utilities */
 
 gchar *                amtk_utils_recent_chooser_menu_get_item_uri     (GtkRecentChooserMenu *menu,
                                                                 GtkMenuItem          *item);
 
+G_GNUC_BEGIN_IGNORE_DEPRECATIONS
+void           amtk_utils_bind_g_action_to_gtk_action          (GActionMap     *g_action_map,
+                                                                const gchar    *g_action_name,
+                                                                GtkActionGroup *gtk_action_group,
+                                                                const gchar    *gtk_action_name);
+G_GNUC_END_IGNORE_DEPRECATIONS
+
 G_END_DECLS
 
 #endif /* AMTK_UTILS_H */
diff --git a/docs/reference/tepl-3.0-sections.txt b/docs/reference/tepl-3.0-sections.txt
index 7d1b597..51c6b83 100644
--- a/docs/reference/tepl-3.0-sections.txt
+++ b/docs/reference/tepl-3.0-sections.txt
@@ -156,6 +156,7 @@ amtk_menu_shell_get_type
 <SECTION>
 <FILE>amtk-utils</FILE>
 amtk_utils_recent_chooser_menu_get_item_uri
+amtk_utils_bind_g_action_to_gtk_action
 </SECTION>
 
 <INCLUDE>tepl/tepl.h</INCLUDE>


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