Add g_simple_action_group_add_entries()

A convenience API for creating lots of actions quickly.
This commit is contained in:
Ryan Lortie 2011-06-29 13:00:30 +01:00 committed by Javier Jardon
parent 695a9a7993
commit 7dd25022ee
4 changed files with 139 additions and 0 deletions

View File

@ -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

View File

@ -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

View File

@ -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);
}
}

View File

@ -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__ */