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 static void g_simple_action_group_iface_init (GActionGroupInterface *);
40 G_DEFINE_TYPE_WITH_CODE (GSimpleActionGroup,
41 g_simple_action_group, G_TYPE_OBJECT,
42 G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_GROUP,
43 g_simple_action_group_iface_init))
46 g_simple_action_group_list_actions (GActionGroup *group)
48 GSimpleActionGroup *simple = G_SIMPLE_ACTION_GROUP (group);
54 n = g_hash_table_size (simple->priv->table);
55 keys = g_new (gchar *, n + 1);
57 g_hash_table_iter_init (&iter, simple->priv->table);
58 while (g_hash_table_iter_next (&iter, &key, NULL))
59 keys[i++] = g_strdup (key);
60 g_assert_cmpint (i, ==, n);
67 g_simple_action_group_has_action (GActionGroup *group,
68 const gchar *action_name)
70 GSimpleActionGroup *simple = G_SIMPLE_ACTION_GROUP (group);
72 return g_hash_table_lookup (simple->priv->table, action_name) != NULL;
75 static const GVariantType *
76 g_simple_action_group_get_parameter_type (GActionGroup *group,
77 const gchar *action_name)
79 GSimpleActionGroup *simple = G_SIMPLE_ACTION_GROUP (group);
82 action = g_hash_table_lookup (simple->priv->table, action_name);
87 return g_action_get_parameter_type (action);
90 static const GVariantType *
91 g_simple_action_group_get_state_type (GActionGroup *group,
92 const gchar *action_name)
94 GSimpleActionGroup *simple = G_SIMPLE_ACTION_GROUP (group);
97 action = g_hash_table_lookup (simple->priv->table, action_name);
102 return g_action_get_state_type (action);
106 g_simple_action_group_get_state_hint (GActionGroup *group,
107 const gchar *action_name)
109 GSimpleActionGroup *simple = G_SIMPLE_ACTION_GROUP (group);
112 action = g_hash_table_lookup (simple->priv->table, action_name);
117 return g_action_get_state_hint (action);
121 g_simple_action_group_get_enabled (GActionGroup *group,
122 const gchar *action_name)
124 GSimpleActionGroup *simple = G_SIMPLE_ACTION_GROUP (group);
127 action = g_hash_table_lookup (simple->priv->table, action_name);
132 return g_action_get_enabled (action);
136 g_simple_action_group_get_state (GActionGroup *group,
137 const gchar *action_name)
139 GSimpleActionGroup *simple = G_SIMPLE_ACTION_GROUP (group);
142 action = g_hash_table_lookup (simple->priv->table, action_name);
147 return g_action_get_state (action);
151 g_simple_action_group_set_state (GActionGroup *group,
152 const gchar *action_name,
155 GSimpleActionGroup *simple = G_SIMPLE_ACTION_GROUP (group);
158 action = g_hash_table_lookup (simple->priv->table, action_name);
163 g_action_set_state (action, value);
167 g_simple_action_group_activate (GActionGroup *group,
168 const gchar *action_name,
171 GSimpleActionGroup *simple = G_SIMPLE_ACTION_GROUP (group);
174 action = g_hash_table_lookup (simple->priv->table, action_name);
179 g_action_activate (action, parameter);
183 action_enabled_notify (GAction *action,
187 g_action_group_action_enabled_changed (user_data,
188 g_action_get_name (action),
189 g_action_get_enabled (action));
193 action_state_notify (GAction *action,
199 value = g_action_get_state (action);
200 g_action_group_action_state_changed (user_data,
201 g_action_get_name (action),
203 g_variant_unref (value);
207 g_simple_action_group_disconnect (gpointer key,
211 g_signal_handlers_disconnect_by_func (value, action_enabled_notify,
213 g_signal_handlers_disconnect_by_func (value, action_state_notify,
218 g_simple_action_group_finalize (GObject *object)
220 GSimpleActionGroup *simple = G_SIMPLE_ACTION_GROUP (object);
222 g_hash_table_foreach (simple->priv->table,
223 g_simple_action_group_disconnect,
225 g_hash_table_unref (simple->priv->table);
227 G_OBJECT_CLASS (g_simple_action_group_parent_class)
232 g_simple_action_group_init (GSimpleActionGroup *simple)
234 simple->priv = G_TYPE_INSTANCE_GET_PRIVATE (simple,
235 G_TYPE_SIMPLE_ACTION_GROUP,
236 GSimpleActionGroupPrivate);
237 simple->priv->table = g_hash_table_new_full (g_str_hash, g_str_equal,
238 g_free, g_object_unref);
242 g_simple_action_group_class_init (GSimpleActionGroupClass *class)
244 GObjectClass *object_class = G_OBJECT_CLASS (class);
246 object_class->finalize = g_simple_action_group_finalize;
248 g_type_class_add_private (class, sizeof (GSimpleActionGroupPrivate));
252 g_simple_action_group_iface_init (GActionGroupInterface *iface)
254 iface->list_actions = g_simple_action_group_list_actions;
255 iface->has_action = g_simple_action_group_has_action;
256 iface->get_action_parameter_type = g_simple_action_group_get_parameter_type;
257 iface->get_action_state_type = g_simple_action_group_get_state_type;
258 iface->get_action_state_hint = g_simple_action_group_get_state_hint;
259 iface->get_action_enabled = g_simple_action_group_get_enabled;
260 iface->get_action_state = g_simple_action_group_get_state;
261 iface->change_action_state = g_simple_action_group_set_state;
262 iface->activate_action = g_simple_action_group_activate;
266 * g_simple_action_group_new:
268 * Creates a new, empty, #GSimpleActionGroup.
270 * Returns: a new #GSimpleActionGroup
275 g_simple_action_group_new (void)
277 return g_object_new (G_TYPE_SIMPLE_ACTION_GROUP, NULL);
281 * g_simple_action_group_lookup:
282 * @simple: a #GSimpleActionGroup
283 * @action_name: the name of an action
285 * Looks up the action with the name @action_name in the group.
287 * If no such action exists, returns %NULL.
289 * Returns: (transfer none): a #GAction, or %NULL
294 g_simple_action_group_lookup (GSimpleActionGroup *simple,
295 const gchar *action_name)
297 g_return_val_if_fail (G_IS_SIMPLE_ACTION_GROUP (simple), NULL);
299 return g_hash_table_lookup (simple->priv->table, action_name);
303 * g_simple_action_group_insert:
304 * @simple: a #GSimpleActionGroup
305 * @action: a #GAction
307 * Adds an action to the action group.
309 * If the action group already contains an action with the same name as
310 * @action then the old action is dropped from the group.
312 * The action group takes its own reference on @action.
317 g_simple_action_group_insert (GSimpleActionGroup *simple,
320 const gchar *action_name;
323 g_return_if_fail (G_IS_SIMPLE_ACTION_GROUP (simple));
324 g_return_if_fail (G_IS_ACTION (action));
326 action_name = g_action_get_name (action);
327 old_action = g_hash_table_lookup (simple->priv->table, action_name);
329 if (old_action != action)
331 if (old_action != NULL)
333 g_action_group_action_removed (G_ACTION_GROUP (simple),
335 g_simple_action_group_disconnect (NULL, old_action, simple);
338 g_signal_connect (action, "notify::enabled",
339 G_CALLBACK (action_enabled_notify), simple);
341 if (g_action_get_state_type (action) != NULL)
342 g_signal_connect (action, "notify::state",
343 G_CALLBACK (action_state_notify), simple);
345 g_hash_table_insert (simple->priv->table,
346 g_strdup (action_name),
347 g_object_ref (action));
349 g_action_group_action_added (G_ACTION_GROUP (simple), action_name);
354 * g_simple_action_group_remove:
355 * @simple: a #GSimpleActionGroup
356 * @action_name: the name of the action
358 * Removes the named action from the action group.
360 * If no action of this name is in the group then nothing happens.
365 g_simple_action_group_remove (GSimpleActionGroup *simple,
366 const gchar *action_name)
370 g_return_if_fail (G_IS_SIMPLE_ACTION_GROUP (simple));
372 action = g_hash_table_lookup (simple->priv->table, action_name);
376 g_action_group_action_removed (G_ACTION_GROUP (simple), action_name);
377 g_simple_action_group_disconnect (NULL, action, simple);
378 g_hash_table_remove (simple->priv->table, action_name);