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"
26 * SECTION:gsimpleactiongroup
27 * @title: GSimpleActionGroup
28 * @short_description: a simple #GActionGroup implementation
30 * #GSimpleActionGroup is a hash table filled with #GAction objects,
31 * implementing the #GActionGroup interface.
34 struct _GSimpleActionGroupPrivate
36 GHashTable *table; /* string -> GAction */
39 G_DEFINE_TYPE (GSimpleActionGroup, g_simple_action_group, G_TYPE_ACTION_GROUP)
42 g_simple_action_group_list_actions (GActionGroup *group)
44 GSimpleActionGroup *simple = G_SIMPLE_ACTION_GROUP (group);
50 n = g_hash_table_size (simple->priv->table);
51 keys = g_new (gchar *, n + 1);
53 g_hash_table_iter_init (&iter, simple->priv->table);
54 while (g_hash_table_iter_next (&iter, &key, NULL))
55 keys[i++] = g_strdup (key);
56 g_assert_cmpint (i, ==, n);
63 g_simple_action_group_has_action (GActionGroup *group,
64 const gchar *action_name)
66 GSimpleActionGroup *simple = G_SIMPLE_ACTION_GROUP (group);
68 return g_hash_table_lookup (simple->priv->table, action_name) != NULL;
71 static const GVariantType *
72 g_simple_action_group_get_parameter_type (GActionGroup *group,
73 const gchar *action_name)
75 GSimpleActionGroup *simple = G_SIMPLE_ACTION_GROUP (group);
78 action = g_hash_table_lookup (simple->priv->table, action_name);
83 return g_action_get_parameter_type (action);
86 static const GVariantType *
87 g_simple_action_group_get_state_type (GActionGroup *group,
88 const gchar *action_name)
90 GSimpleActionGroup *simple = G_SIMPLE_ACTION_GROUP (group);
93 action = g_hash_table_lookup (simple->priv->table, action_name);
98 return g_action_get_state_type (action);
102 g_simple_action_group_get_state_hint (GActionGroup *group,
103 const gchar *action_name)
105 GSimpleActionGroup *simple = G_SIMPLE_ACTION_GROUP (group);
108 action = g_hash_table_lookup (simple->priv->table, action_name);
113 return g_action_get_state_hint (action);
117 g_simple_action_group_get_enabled (GActionGroup *group,
118 const gchar *action_name)
120 GSimpleActionGroup *simple = G_SIMPLE_ACTION_GROUP (group);
123 action = g_hash_table_lookup (simple->priv->table, action_name);
128 return g_action_get_enabled (action);
132 g_simple_action_group_get_state (GActionGroup *group,
133 const gchar *action_name)
135 GSimpleActionGroup *simple = G_SIMPLE_ACTION_GROUP (group);
138 action = g_hash_table_lookup (simple->priv->table, action_name);
143 return g_action_get_state (action);
147 g_simple_action_group_set_state (GActionGroup *group,
148 const gchar *action_name,
151 GSimpleActionGroup *simple = G_SIMPLE_ACTION_GROUP (group);
154 action = g_hash_table_lookup (simple->priv->table, action_name);
159 return g_action_set_state (action, value);
163 g_simple_action_group_activate (GActionGroup *group,
164 const gchar *action_name,
167 GSimpleActionGroup *simple = G_SIMPLE_ACTION_GROUP (group);
170 action = g_hash_table_lookup (simple->priv->table, action_name);
175 return g_action_activate (action, parameter);
179 action_enabled_changed (GAction *action,
183 g_action_group_action_enabled_changed (user_data,
184 g_action_get_name (action),
189 action_state_changed (GAction *action,
193 g_action_group_action_state_changed (user_data,
194 g_action_get_name (action),
199 g_simple_action_group_disconnect (gpointer key,
203 g_signal_handlers_disconnect_by_func (value, action_enabled_changed,
205 g_signal_handlers_disconnect_by_func (value, action_state_changed,
210 g_simple_action_group_finalize (GObject *object)
212 GSimpleActionGroup *simple = G_SIMPLE_ACTION_GROUP (object);
214 g_hash_table_foreach (simple->priv->table,
215 g_simple_action_group_disconnect,
217 g_hash_table_unref (simple->priv->table);
219 G_OBJECT_CLASS (g_simple_action_group_parent_class)
224 g_simple_action_group_init (GSimpleActionGroup *simple)
226 simple->priv = G_TYPE_INSTANCE_GET_PRIVATE (simple,
227 G_TYPE_SIMPLE_ACTION_GROUP,
228 GSimpleActionGroupPrivate);
229 simple->priv->table = g_hash_table_new_full (g_str_hash, g_str_equal,
230 g_free, g_object_unref);
234 g_simple_action_group_class_init (GSimpleActionGroupClass *class)
236 GActionGroupClass *group_class = G_ACTION_GROUP_CLASS (class);
237 GObjectClass *object_class = G_OBJECT_CLASS (class);
239 object_class->finalize = g_simple_action_group_finalize;
241 group_class->list_actions = g_simple_action_group_list_actions;
242 group_class->has_action = g_simple_action_group_has_action;
243 group_class->get_parameter_type = g_simple_action_group_get_parameter_type;
244 group_class->get_state_type = g_simple_action_group_get_state_type;
245 group_class->get_state_hint = g_simple_action_group_get_state_hint;
246 group_class->get_enabled = g_simple_action_group_get_enabled;
247 group_class->get_state = g_simple_action_group_get_state;
248 group_class->set_state = g_simple_action_group_set_state;
249 group_class->activate = g_simple_action_group_activate;
251 g_type_class_add_private (class, sizeof (GSimpleActionGroupPrivate));
255 * g_simple_action_group_new:
257 * Creates a new, empty, #GSimpleActionGroup.
259 * Returns: a new #GSimpleActionGroup
264 g_simple_action_group_new (void)
266 return g_object_new (G_TYPE_SIMPLE_ACTION_GROUP, NULL);
270 * g_simple_action_group_lookup:
271 * @simple: a #GSimpleActionGroup
272 * @action_name: the name of an action
274 * Looks up the action with the name @action_name in the group.
276 * If no such action exists, returns %NULL.
278 * Returns: (transfer none): a #GAction, or %NULL
283 g_simple_action_group_lookup (GSimpleActionGroup *simple,
284 const gchar *action_name)
286 g_return_val_if_fail (G_IS_SIMPLE_ACTION_GROUP (simple), NULL);
288 return g_hash_table_lookup (simple->priv->table, action_name);
292 * g_simple_action_group_insert:
293 * @simple: a #GSimpleActionGroup
294 * @action: a #GAction
296 * Adds an action to the action group.
298 * If the action group already contains an action with the same name as
299 * @action then the old action is dropped from the group.
301 * The action group takes its own reference on @action.
306 g_simple_action_group_insert (GSimpleActionGroup *simple,
309 const gchar *action_name;
312 g_return_if_fail (G_IS_SIMPLE_ACTION_GROUP (simple));
313 g_return_if_fail (G_IS_ACTION (action));
315 action_name = g_action_get_name (action);
316 old_action = g_hash_table_lookup (simple->priv->table, action_name);
318 if (old_action != action)
320 if (old_action != NULL)
322 g_action_group_action_removed (G_ACTION_GROUP (simple),
324 g_simple_action_group_disconnect (NULL, old_action, simple);
327 g_signal_connect (action, "notify::enabled",
328 G_CALLBACK (action_enabled_changed), simple);
330 if (g_action_get_state_type (action) != NULL)
331 g_signal_connect (action, "notify::state",
332 G_CALLBACK (action_state_changed), simple);
334 g_hash_table_insert (simple->priv->table,
335 g_strdup (action_name),
336 g_object_ref (action));
338 g_action_group_action_added (G_ACTION_GROUP (simple), action_name);
343 * g_simple_action_group_remove:
344 * @simple: a #GSimpleActionGroup
345 * @action_name: the name of the action
347 * Removes the named action from the action group.
349 * If no action of this name is in the group then nothing happens.
354 g_simple_action_group_remove (GSimpleActionGroup *simple,
355 const gchar *action_name)
359 g_return_if_fail (G_IS_SIMPLE_ACTION_GROUP (simple));
361 action = g_hash_table_lookup (simple->priv->table, action_name);
365 g_action_group_action_removed (G_ACTION_GROUP (simple), action_name);
366 g_simple_action_group_disconnect (NULL, action, simple);
367 g_hash_table_remove (simple->priv->table, action_name);
372 * g_simple_action_group_set_enabled:
373 * @simple: a #GSimpleActionGroup
374 * @action_name: the name of an action
375 * @enabled: if the action should be enabled
377 * Sets an action in the group as being enabled or not.
379 * This is a convenience function, equivalent to calling
380 * g_simple_action_group_lookup() and using g_action_set_enabled() on
383 * If no action named @action_name exists then this function does
389 g_simple_action_group_set_enabled (GSimpleActionGroup *simple,
390 const gchar *action_name,
395 g_return_if_fail (G_IS_SIMPLE_ACTION_GROUP (simple));
397 action = g_hash_table_lookup (simple->priv->table, action_name);
400 g_action_set_enabled (action, enabled);