2 * Copyright © 2010 Codethink Limited
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published
6 * by the Free Software Foundation; either version 2 of the licence or (at
7 * your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General
15 * Public License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
17 * Boston, MA 02111-1307, USA.
19 * Authors: Ryan Lortie <desrt@desrt.ca>
24 #include "gsimpleaction.h"
25 #include "gactiongroup.h"
26 #include "gactionmap.h"
32 * @short_description: Interface for action containers
34 * The GActionMap interface is implemented by #GActionGroup
35 * implementations that operate by containing a number of
36 * named #GAction instances, such as #GSimpleActionGroup.
38 * One useful application of this interface is to map the
39 * names of actions from various action groups to unique,
40 * prefixed names (e.g. by prepending "app." or "win.").
41 * This is the motivation for the 'Map' part of the interface
48 * GActionMapInterface:
49 * @lookup_action: the virtual function pointer for g_action_map_lookup_action()
50 * @add_action: the virtual function pointer for g_action_map_add_action()
51 * @remove_action: the virtual function pointer for g_action_map_remove_action()
53 * The virtual function table for #GActionMap.
58 G_DEFINE_INTERFACE (GActionMap, g_action_map, G_TYPE_ACTION_GROUP)
61 g_action_map_default_init (GActionMapInterface *iface)
66 * g_action_map_lookup_action:
67 * @action_map: a #GActionMap
68 * @action_name: the name of an action
70 * Looks up the action with the name @action_name in @action_map.
72 * If no such action exists, returns %NULL.
74 * Returns: (transfer none): a #GAction, or %NULL
79 g_action_map_lookup_action (GActionMap *action_map,
80 const gchar *action_name)
82 return G_ACTION_MAP_GET_IFACE (action_map)
83 ->lookup_action (action_map, action_name);
87 * g_action_map_add_action:
88 * @action_map: a #GActionMap
91 * Adds an action to the @action_map.
93 * If the action map already contains an action with the same name
94 * as @action then the old action is dropped from the action map.
96 * The action map takes its own reference on @action.
101 g_action_map_add_action (GActionMap *action_map,
104 G_ACTION_MAP_GET_IFACE (action_map)->add_action (action_map, action);
108 * g_action_map_remove_action:
109 * @action_map: a #GActionMap
110 * @action_name: the name of the action
112 * Removes the named action from the action map.
114 * If no action of this name is in the map then nothing happens.
119 g_action_map_remove_action (GActionMap *action_map,
120 const gchar *action_name)
122 G_ACTION_MAP_GET_IFACE (action_map)->remove_action (action_map, action_name);
127 * @name: the name of the action
128 * @activate: the callback to connect to the "activate" signal of the
130 * @parameter_type: the type of the parameter that must be passed to the
131 * activate function for this action, given as a single
132 * GVariant type string (or %NULL for no parameter)
133 * @state: the initial state for this action, given in GVariant text
134 * format. The state is parsed with no extra type information,
135 * so type tags must be added to the string if they are
137 * @change_state: the callback to connect to the "change-state" signal
140 * This struct defines a single action. It is for use with
141 * g_action_map_add_action_entries().
143 * The order of the items in the structure are intended to reflect
144 * frequency of use. It is permissible to use an incomplete initialiser
145 * in order to leave some of the later values as %NULL. All values
146 * after @name are optional. Additional optional fields may be added in
149 * See g_action_map_add_action_entries() for an example.
153 * g_action_map_add_action_entries:
154 * @action_map: a #GActionMap
155 * @entries: a pointer to the first item in an array of #GActionEntry
157 * @n_entries: the length of @entries, or -1 if @entries is %NULL-terminated
158 * @user_data: the user data for signal connections
160 * A convenience function for creating multiple #GSimpleAction instances
161 * and adding them to a #GActionMap.
163 * Each action is constructed as per one #GActionEntry.
166 * <title>Using g_action_map_add_action_entries()</title>
169 * activate_quit (GSimpleAction *simple,
170 * GVariant *parameter,
171 * gpointer user_data)
177 * activate_print_string (GSimpleAction *simple,
178 * GVariant *parameter,
179 * gpointer user_data)
181 * g_print ("%s\n", g_variant_get_string (parameter, NULL));
184 * static GActionGroup *
185 * create_action_group (void)
187 * const GActionEntry entries[] = {
188 * { "quit", activate_quit },
189 * { "print-string", activate_print_string, "s" }
191 * GSimpleActionGroup *group;
193 * group = g_simple_action_group_new ();
194 * g_action_map_add_action_entries (G_ACTION_MAP (group), entries, G_N_ELEMENTS (entries), NULL);
196 * return G_ACTION_GROUP (group);
204 g_action_map_add_action_entries (GActionMap *action_map,
205 const GActionEntry *entries,
211 g_return_if_fail (G_IS_ACTION_MAP (action_map));
212 g_return_if_fail (entries != NULL || n_entries == 0);
214 for (i = 0; n_entries == -1 ? entries[i].name != NULL : i < n_entries; i++)
216 const GActionEntry *entry = &entries[i];
217 const GVariantType *parameter_type;
218 GSimpleAction *action;
220 if (entry->parameter_type)
222 if (!g_variant_type_string_is_valid (entry->parameter_type))
224 g_critical ("g_action_map_add_entries: the type "
225 "string '%s' given as the parameter type for "
226 "action '%s' is not a valid GVariant type "
227 "string. This action will not be added.",
228 entry->parameter_type, entry->name);
232 parameter_type = G_VARIANT_TYPE (entry->parameter_type);
235 parameter_type = NULL;
239 GError *error = NULL;
242 state = g_variant_parse (NULL, entry->state, NULL, NULL, &error);
245 g_critical ("g_action_map_add_entries: GVariant could "
246 "not parse the state value given for action '%s' "
247 "('%s'): %s. This action will not be added.",
248 entry->name, entry->state, error->message);
249 g_error_free (error);
253 action = g_simple_action_new_stateful (entry->name,
257 g_variant_unref (state);
261 action = g_simple_action_new (entry->name,
265 if (entry->activate != NULL)
266 g_signal_connect (action, "activate",
267 G_CALLBACK (entry->activate), user_data);
269 if (entry->change_state != NULL)
270 g_signal_connect (action, "change-state",
271 G_CALLBACK (entry->change_state), user_data);
273 g_action_map_add_action (action_map, G_ACTION (action));
274 g_object_unref (action);