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>
22 #include "gsimpleactiongroup.h"
24 #include "gsimpleaction.h"
25 #include "gactionmap.h"
29 * SECTION:gsimpleactiongroup
30 * @title: GSimpleActionGroup
31 * @short_description: A simple GActionGroup implementation
33 * #GSimpleActionGroup is a hash table filled with #GAction objects,
34 * implementing the #GActionGroup and #GActionMap interfaces.
37 struct _GSimpleActionGroupPrivate
39 GHashTable *table; /* string -> GAction */
42 static void g_simple_action_group_iface_init (GActionGroupInterface *);
43 static void g_simple_action_group_map_iface_init (GActionMapInterface *);
44 G_DEFINE_TYPE_WITH_CODE (GSimpleActionGroup,
45 g_simple_action_group, G_TYPE_OBJECT,
46 G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_GROUP,
47 g_simple_action_group_iface_init);
48 G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_MAP,
49 g_simple_action_group_map_iface_init))
52 g_simple_action_group_list_actions (GActionGroup *group)
54 GSimpleActionGroup *simple = G_SIMPLE_ACTION_GROUP (group);
60 n = g_hash_table_size (simple->priv->table);
61 keys = g_new (gchar *, n + 1);
63 g_hash_table_iter_init (&iter, simple->priv->table);
64 while (g_hash_table_iter_next (&iter, &key, NULL))
65 keys[i++] = g_strdup (key);
66 g_assert_cmpint (i, ==, n);
73 g_simple_action_group_query_action (GActionGroup *group,
74 const gchar *action_name,
76 const GVariantType **parameter_type,
77 const GVariantType **state_type,
78 GVariant **state_hint,
81 GSimpleActionGroup *simple = G_SIMPLE_ACTION_GROUP (group);
84 action = g_hash_table_lookup (simple->priv->table, action_name);
90 *enabled = g_action_get_enabled (action);
93 *parameter_type = g_action_get_parameter_type (action);
96 *state_type = g_action_get_state_type (action);
99 *state_hint = g_action_get_state_hint (action);
102 *state = g_action_get_state (action);
108 g_simple_action_group_change_state (GActionGroup *group,
109 const gchar *action_name,
112 GSimpleActionGroup *simple = G_SIMPLE_ACTION_GROUP (group);
115 action = g_hash_table_lookup (simple->priv->table, action_name);
120 g_action_change_state (action, value);
124 g_simple_action_group_activate (GActionGroup *group,
125 const gchar *action_name,
128 GSimpleActionGroup *simple = G_SIMPLE_ACTION_GROUP (group);
131 action = g_hash_table_lookup (simple->priv->table, action_name);
136 g_action_activate (action, parameter);
140 action_enabled_notify (GAction *action,
144 g_action_group_action_enabled_changed (user_data,
145 g_action_get_name (action),
146 g_action_get_enabled (action));
150 action_state_notify (GAction *action,
156 value = g_action_get_state (action);
157 g_action_group_action_state_changed (user_data,
158 g_action_get_name (action),
160 g_variant_unref (value);
164 g_simple_action_group_disconnect (gpointer key,
168 g_signal_handlers_disconnect_by_func (value, action_enabled_notify,
170 g_signal_handlers_disconnect_by_func (value, action_state_notify,
175 g_simple_action_group_lookup_action (GActionMap *action_map,
176 const gchar *action_name)
178 GSimpleActionGroup *simple = G_SIMPLE_ACTION_GROUP (action_map);
180 return g_hash_table_lookup (simple->priv->table, action_name);
184 g_simple_action_group_add_action (GActionMap *action_map,
187 GSimpleActionGroup *simple = G_SIMPLE_ACTION_GROUP (action_map);
188 const gchar *action_name;
191 action_name = g_action_get_name (action);
192 old_action = g_hash_table_lookup (simple->priv->table, action_name);
194 if (old_action != action)
196 if (old_action != NULL)
198 g_action_group_action_removed (G_ACTION_GROUP (simple),
200 g_simple_action_group_disconnect (NULL, old_action, simple);
203 g_signal_connect (action, "notify::enabled",
204 G_CALLBACK (action_enabled_notify), simple);
206 if (g_action_get_state_type (action) != NULL)
207 g_signal_connect (action, "notify::state",
208 G_CALLBACK (action_state_notify), simple);
210 g_hash_table_insert (simple->priv->table,
211 g_strdup (action_name),
212 g_object_ref (action));
214 g_action_group_action_added (G_ACTION_GROUP (simple), action_name);
219 g_simple_action_group_remove_action (GActionMap *action_map,
220 const gchar *action_name)
222 GSimpleActionGroup *simple = G_SIMPLE_ACTION_GROUP (action_map);
225 action = g_hash_table_lookup (simple->priv->table, action_name);
229 g_action_group_action_removed (G_ACTION_GROUP (simple), action_name);
230 g_simple_action_group_disconnect (NULL, action, simple);
231 g_hash_table_remove (simple->priv->table, action_name);
236 g_simple_action_group_finalize (GObject *object)
238 GSimpleActionGroup *simple = G_SIMPLE_ACTION_GROUP (object);
240 g_hash_table_foreach (simple->priv->table,
241 g_simple_action_group_disconnect,
243 g_hash_table_unref (simple->priv->table);
245 G_OBJECT_CLASS (g_simple_action_group_parent_class)
250 g_simple_action_group_init (GSimpleActionGroup *simple)
252 simple->priv = G_TYPE_INSTANCE_GET_PRIVATE (simple,
253 G_TYPE_SIMPLE_ACTION_GROUP,
254 GSimpleActionGroupPrivate);
255 simple->priv->table = g_hash_table_new_full (g_str_hash, g_str_equal,
256 g_free, g_object_unref);
260 g_simple_action_group_class_init (GSimpleActionGroupClass *class)
262 GObjectClass *object_class = G_OBJECT_CLASS (class);
264 object_class->finalize = g_simple_action_group_finalize;
266 g_type_class_add_private (class, sizeof (GSimpleActionGroupPrivate));
270 g_simple_action_group_iface_init (GActionGroupInterface *iface)
272 iface->list_actions = g_simple_action_group_list_actions;
273 iface->query_action = g_simple_action_group_query_action;
274 iface->change_action_state = g_simple_action_group_change_state;
275 iface->activate_action = g_simple_action_group_activate;
279 g_simple_action_group_map_iface_init (GActionMapInterface *iface)
281 iface->add_action = g_simple_action_group_add_action;
282 iface->remove_action = g_simple_action_group_remove_action;
283 iface->lookup_action = g_simple_action_group_lookup_action;
287 * g_simple_action_group_new:
289 * Creates a new, empty, #GSimpleActionGroup.
291 * Returns: a new #GSimpleActionGroup
296 g_simple_action_group_new (void)
298 return g_object_new (G_TYPE_SIMPLE_ACTION_GROUP, NULL);
302 * g_simple_action_group_lookup:
303 * @simple: a #GSimpleActionGroup
304 * @action_name: the name of an action
306 * Looks up the action with the name @action_name in the group.
308 * If no such action exists, returns %NULL.
310 * Returns: (transfer none): a #GAction, or %NULL
315 g_simple_action_group_lookup (GSimpleActionGroup *simple,
316 const gchar *action_name)
318 g_return_val_if_fail (G_IS_SIMPLE_ACTION_GROUP (simple), NULL);
320 return g_action_map_lookup_action (G_ACTION_MAP (simple), action_name);
324 * g_simple_action_group_insert:
325 * @simple: a #GSimpleActionGroup
326 * @action: a #GAction
328 * Adds an action to the action group.
330 * If the action group already contains an action with the same name as
331 * @action then the old action is dropped from the group.
333 * The action group takes its own reference on @action.
338 g_simple_action_group_insert (GSimpleActionGroup *simple,
341 g_return_if_fail (G_IS_SIMPLE_ACTION_GROUP (simple));
343 g_action_map_add_action (G_ACTION_MAP (simple), action);
347 * g_simple_action_group_remove:
348 * @simple: a #GSimpleActionGroup
349 * @action_name: the name of the action
351 * Removes the named action from the action group.
353 * If no action of this name is in the group then nothing happens.
358 g_simple_action_group_remove (GSimpleActionGroup *simple,
359 const gchar *action_name)
361 g_return_if_fail (G_IS_SIMPLE_ACTION_GROUP (simple));
363 g_action_map_remove_action (G_ACTION_MAP (simple), action_name);
368 * g_simple_action_group_add_entries:
369 * @simple: a #GSimpleActionGroup
370 * @entries: a pointer to the first item in an array of #GActionEntry
372 * @n_entries: the length of @entries, or -1
373 * @user_data: the user data for signal connections
375 * A convenience function for creating multiple #GSimpleAction instances
376 * and adding them to the action group.
381 g_simple_action_group_add_entries (GSimpleActionGroup *simple,
382 const GActionEntry *entries,
386 g_action_map_add_action_entries (G_ACTION_MAP (simple), entries, n_entries, user_data);