[glib] Add g_simple_action_group_add_entries()



commit 7dd25022eeb1ef3f92b6352ba9ac9204dbb10305
Author: Ryan Lortie <desrt desrt ca>
Date:   Wed Jun 29 13:00:30 2011 +0100

    Add g_simple_action_group_add_entries()
    
    A convenience API for creating lots of actions quickly.

 docs/reference/gio/gio-sections.txt |    4 +
 gio/gio.symbols                     |    1 +
 gio/gsimpleactiongroup.c            |  111 +++++++++++++++++++++++++++++++++++
 gio/gsimpleactiongroup.h            |   23 +++++++
 4 files changed, 139 insertions(+), 0 deletions(-)
---
diff --git a/docs/reference/gio/gio-sections.txt b/docs/reference/gio/gio-sections.txt
index ea027d2..87617a6 100644
--- a/docs/reference/gio/gio-sections.txt
+++ b/docs/reference/gio/gio-sections.txt
@@ -2883,6 +2883,10 @@ g_simple_action_group_lookup
 g_simple_action_group_insert
 g_simple_action_group_remove
 
+<SUBSECTION>
+GActionEntry
+g_simple_action_group_add_entries
+
 <SUBSECTION Standard>
 GSimpleActionGroupClass
 GSimpleActionGroupPrivate
diff --git a/gio/gio.symbols b/gio/gio.symbols
index 0965a29..00d5328 100644
--- a/gio/gio.symbols
+++ b/gio/gio.symbols
@@ -1378,6 +1378,7 @@ g_action_get_state_type
 g_action_get_type
 g_action_change_state
 g_simple_action_group_get_type
+g_simple_action_group_add_entries
 g_simple_action_group_insert
 g_simple_action_group_lookup
 g_simple_action_group_new
diff --git a/gio/gsimpleactiongroup.c b/gio/gsimpleactiongroup.c
index d83feed..14c9714 100644
--- a/gio/gsimpleactiongroup.c
+++ b/gio/gsimpleactiongroup.c
@@ -20,6 +20,8 @@
  */
 
 #include "gsimpleactiongroup.h"
+
+#include "gsimpleaction.h"
 #include "gaction.h"
 
 /**
@@ -378,3 +380,112 @@ g_simple_action_group_remove (GSimpleActionGroup *simple,
       g_hash_table_remove (simple->priv->table, action_name);
     }
 }
+
+/**
+ * GActionEntry:
+ * @name: the name of the action
+ * @activate: the callback to connect to the "activate" signal of the
+ *            action
+ * @parameter_type: the type of the parameter that must be passed to the
+ *                  activate function for this action, given as a single
+ *                  GVariant type string (or %NULL for no parameter)
+ * @state: the initial state for this action, given in GVariant text
+ *         format.  The state is parsed with no extra type information,
+ *         so type tags must be added to the string if they are
+ *         necessary.
+ *
+ * This struct defines a single action.  It is for use with
+ * g_simple_action_group_add_entries().
+ *
+ * The order of the items in the structure are intended to reflect
+ * frequency of use.  It is permissible to use an incomplete initialiser
+ * in order to leave some of the later values as %NULL.  All values
+ * after @name are optional.  Additional optional fields may be added in
+ * the future.
+ **/
+
+/**
+ * g_simple_action_group_add_entries:
+ * @simple: a #GSimpleActionGroup
+ * @entries: a pointer to the first item in an array of #GActionEntry
+ *           structs
+ * @n_entries: the length of @entries, or -1
+ * @user_data: the user data for signal connections
+ *
+ * A convenience function for creating multiple #GSimpleAction instances
+ * and adding them to the action group.
+ *
+ * Each action is constructed as per one #GActionEntry.
+ *
+ * Since: 2.30
+ **/
+void
+g_simple_action_group_add_entries (GSimpleActionGroup *simple,
+                                   const GActionEntry *entries,
+                                   gint                n_entries,
+                                   gpointer            user_data)
+{
+  gint i;
+
+  g_return_if_fail (G_IS_SIMPLE_ACTION_GROUP (simple));
+  g_return_if_fail (entries != NULL || n_entries == 0);
+
+  for (i = 0; n_entries == -1 ? entries[i].name != NULL : i < n_entries; i++)
+    {
+      const GActionEntry *entry = &entries[i];
+      const GVariantType *parameter_type;
+      GSimpleAction *action;
+
+      if (entry->parameter_type)
+        {
+          if (!g_variant_type_string_is_valid (entry->parameter_type))
+            {
+              g_critical ("g_simple_action_group_add_entries: the type "
+                          "string '%s' given as the parameter type for "
+                          "action '%s' is not a valid GVariant type "
+                          "string.  This action will not be added.",
+                          entry->parameter_type, entry->name);
+              return;
+            }
+
+          parameter_type = G_VARIANT_TYPE (entry->parameter_type);
+        }
+      else
+        parameter_type = NULL;
+
+      if (entry->state)
+        {
+          GError *error = NULL;
+          GVariant *state;
+
+          state = g_variant_parse (NULL, entry->state, NULL, NULL, &error);
+          if (state == NULL)
+            {
+              g_critical ("g_simple_action_group_add_entries: GVariant could "
+                          "not parse the state value given for action '%s' "
+                          "('%s'): %s.  This action will not be added.",
+                          entry->name, entry->state, error->message);
+              g_error_free (error);
+              continue;
+            }
+
+          action = g_simple_action_new_stateful (entry->name,
+                                                 parameter_type,
+                                                 state);
+
+          g_variant_unref (state);
+        }
+      else
+        {
+          action = g_simple_action_new (entry->name,
+                                        parameter_type);
+        }
+
+      if (entry->activate != NULL)
+        g_signal_connect (action, "activate",
+                          G_CALLBACK (entry->activate), user_data);
+
+      g_simple_action_group_insert (simple, G_ACTION (action));
+      g_object_unref (action);
+    }
+}
diff --git a/gio/gsimpleactiongroup.h b/gio/gsimpleactiongroup.h
index a526062..8486808 100644
--- a/gio/gsimpleactiongroup.h
+++ b/gio/gsimpleactiongroup.h
@@ -45,6 +45,8 @@ G_BEGIN_DECLS
 typedef struct _GSimpleActionGroupPrivate                   GSimpleActionGroupPrivate;
 typedef struct _GSimpleActionGroupClass                     GSimpleActionGroupClass;
 
+typedef struct _GActionEntry                                GActionEntry;
+
 /**
  * GSimpleActionGroup:
  *
@@ -82,6 +84,27 @@ void                    g_simple_action_group_insert                    (GSimple
 void                    g_simple_action_group_remove                    (GSimpleActionGroup *simple,
                                                                          const gchar        *action_name);
 
+struct _GActionEntry
+{
+  const gchar *name;
+
+  void (* activate) (GSimpleAction *action,
+                     GVariant      *parameter,
+                     gpointer       user_data);
+
+  const gchar *parameter_type;
+
+  const gchar *state;
+
+  /*< private >*/
+  gsize padding[4];
+};
+
+void                    g_simple_action_group_add_entries               (GSimpleActionGroup *simple,
+                                                                         const GActionEntry *entries,
+                                                                         gint                n_entries,
+                                                                         gpointer            user_data);
+
 G_END_DECLS
 
 #endif /* __G_SIMPLE_ACTION_GROUP_H__ */



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