2 * Copyright © 2010 Codethink Limited
4 * SPDX-License-Identifier: LGPL-2.1-or-later
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General
17 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 * Authors: Ryan Lortie <desrt@desrt.ca>
24 #include "gsimpleactiongroup.h"
26 #include "gsimpleaction.h"
27 #include "gactionmap.h"
31 * SECTION:gsimpleactiongroup
32 * @title: GSimpleActionGroup
33 * @short_description: A simple GActionGroup implementation
36 * #GSimpleActionGroup is a hash table filled with #GAction objects,
37 * implementing the #GActionGroup and #GActionMap interfaces.
40 struct _GSimpleActionGroupPrivate
42 GHashTable *table; /* string -> GAction */
45 static void g_simple_action_group_iface_init (GActionGroupInterface *);
46 static void g_simple_action_group_map_iface_init (GActionMapInterface *);
47 G_DEFINE_TYPE_WITH_CODE (GSimpleActionGroup,
48 g_simple_action_group, G_TYPE_OBJECT,
49 G_ADD_PRIVATE (GSimpleActionGroup)
50 G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_GROUP,
51 g_simple_action_group_iface_init);
52 G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_MAP,
53 g_simple_action_group_map_iface_init))
56 g_simple_action_group_list_actions (GActionGroup *group)
58 GSimpleActionGroup *simple = G_SIMPLE_ACTION_GROUP (group);
64 n = g_hash_table_size (simple->priv->table);
65 keys = g_new (gchar *, n + 1);
67 g_hash_table_iter_init (&iter, simple->priv->table);
68 while (g_hash_table_iter_next (&iter, &key, NULL))
69 keys[i++] = g_strdup (key);
70 g_assert_cmpint (i, ==, n);
77 g_simple_action_group_query_action (GActionGroup *group,
78 const gchar *action_name,
80 const GVariantType **parameter_type,
81 const GVariantType **state_type,
82 GVariant **state_hint,
85 GSimpleActionGroup *simple = G_SIMPLE_ACTION_GROUP (group);
88 action = g_hash_table_lookup (simple->priv->table, action_name);
94 *enabled = g_action_get_enabled (action);
97 *parameter_type = g_action_get_parameter_type (action);
100 *state_type = g_action_get_state_type (action);
103 *state_hint = g_action_get_state_hint (action);
106 *state = g_action_get_state (action);
112 g_simple_action_group_change_state (GActionGroup *group,
113 const gchar *action_name,
116 GSimpleActionGroup *simple = G_SIMPLE_ACTION_GROUP (group);
119 action = g_hash_table_lookup (simple->priv->table, action_name);
124 g_action_change_state (action, value);
128 g_simple_action_group_activate (GActionGroup *group,
129 const gchar *action_name,
132 GSimpleActionGroup *simple = G_SIMPLE_ACTION_GROUP (group);
135 action = g_hash_table_lookup (simple->priv->table, action_name);
140 g_action_activate (action, parameter);
144 action_enabled_notify (GAction *action,
148 g_action_group_action_enabled_changed (user_data,
149 g_action_get_name (action),
150 g_action_get_enabled (action));
154 action_state_notify (GAction *action,
160 value = g_action_get_state (action);
161 g_action_group_action_state_changed (user_data,
162 g_action_get_name (action),
164 g_variant_unref (value);
168 g_simple_action_group_disconnect (gpointer key,
172 g_signal_handlers_disconnect_by_func (value, action_enabled_notify,
174 g_signal_handlers_disconnect_by_func (value, action_state_notify,
179 g_simple_action_group_lookup_action (GActionMap *action_map,
180 const gchar *action_name)
182 GSimpleActionGroup *simple = G_SIMPLE_ACTION_GROUP (action_map);
184 return g_hash_table_lookup (simple->priv->table, action_name);
188 g_simple_action_group_add_action (GActionMap *action_map,
191 GSimpleActionGroup *simple = G_SIMPLE_ACTION_GROUP (action_map);
192 const gchar *action_name;
195 action_name = g_action_get_name (action);
196 if (action_name == NULL)
198 g_critical ("The supplied action has no name. You must set the "
199 "GAction:name property when creating an action.");
203 old_action = g_hash_table_lookup (simple->priv->table, action_name);
205 if (old_action != action)
207 if (old_action != NULL)
209 g_action_group_action_removed (G_ACTION_GROUP (simple),
211 g_simple_action_group_disconnect (NULL, old_action, simple);
214 g_signal_connect (action, "notify::enabled",
215 G_CALLBACK (action_enabled_notify), simple);
217 if (g_action_get_state_type (action) != NULL)
218 g_signal_connect (action, "notify::state",
219 G_CALLBACK (action_state_notify), simple);
221 g_hash_table_insert (simple->priv->table,
222 g_strdup (action_name),
223 g_object_ref (action));
225 g_action_group_action_added (G_ACTION_GROUP (simple), action_name);
230 g_simple_action_group_remove_action (GActionMap *action_map,
231 const gchar *action_name)
233 GSimpleActionGroup *simple = G_SIMPLE_ACTION_GROUP (action_map);
236 action = g_hash_table_lookup (simple->priv->table, action_name);
240 g_action_group_action_removed (G_ACTION_GROUP (simple), action_name);
241 g_simple_action_group_disconnect (NULL, action, simple);
242 g_hash_table_remove (simple->priv->table, action_name);
247 g_simple_action_group_finalize (GObject *object)
249 GSimpleActionGroup *simple = G_SIMPLE_ACTION_GROUP (object);
251 g_hash_table_foreach (simple->priv->table,
252 g_simple_action_group_disconnect,
254 g_hash_table_unref (simple->priv->table);
256 G_OBJECT_CLASS (g_simple_action_group_parent_class)
261 g_simple_action_group_init (GSimpleActionGroup *simple)
263 simple->priv = g_simple_action_group_get_instance_private (simple);
264 simple->priv->table = g_hash_table_new_full (g_str_hash, g_str_equal,
265 g_free, g_object_unref);
269 g_simple_action_group_class_init (GSimpleActionGroupClass *class)
271 GObjectClass *object_class = G_OBJECT_CLASS (class);
273 object_class->finalize = g_simple_action_group_finalize;
277 g_simple_action_group_iface_init (GActionGroupInterface *iface)
279 iface->list_actions = g_simple_action_group_list_actions;
280 iface->query_action = g_simple_action_group_query_action;
281 iface->change_action_state = g_simple_action_group_change_state;
282 iface->activate_action = g_simple_action_group_activate;
286 g_simple_action_group_map_iface_init (GActionMapInterface *iface)
288 iface->add_action = g_simple_action_group_add_action;
289 iface->remove_action = g_simple_action_group_remove_action;
290 iface->lookup_action = g_simple_action_group_lookup_action;
294 * g_simple_action_group_new:
296 * Creates a new, empty, #GSimpleActionGroup.
298 * Returns: a new #GSimpleActionGroup
303 g_simple_action_group_new (void)
305 return g_object_new (G_TYPE_SIMPLE_ACTION_GROUP, NULL);
309 * g_simple_action_group_lookup:
310 * @simple: a #GSimpleActionGroup
311 * @action_name: the name of an action
313 * Looks up the action with the name @action_name in the group.
315 * If no such action exists, returns %NULL.
317 * Returns: (transfer none): a #GAction, or %NULL
321 * Deprecated: 2.38: Use g_action_map_lookup_action()
324 g_simple_action_group_lookup (GSimpleActionGroup *simple,
325 const gchar *action_name)
327 g_return_val_if_fail (G_IS_SIMPLE_ACTION_GROUP (simple), NULL);
329 return g_action_map_lookup_action (G_ACTION_MAP (simple), action_name);
333 * g_simple_action_group_insert:
334 * @simple: a #GSimpleActionGroup
335 * @action: a #GAction
337 * Adds an action to the action group.
339 * If the action group already contains an action with the same name as
340 * @action then the old action is dropped from the group.
342 * The action group takes its own reference on @action.
346 * Deprecated: 2.38: Use g_action_map_add_action()
349 g_simple_action_group_insert (GSimpleActionGroup *simple,
352 g_return_if_fail (G_IS_SIMPLE_ACTION_GROUP (simple));
354 g_action_map_add_action (G_ACTION_MAP (simple), action);
358 * g_simple_action_group_remove:
359 * @simple: a #GSimpleActionGroup
360 * @action_name: the name of the action
362 * Removes the named action from the action group.
364 * If no action of this name is in the group then nothing happens.
368 * Deprecated: 2.38: Use g_action_map_remove_action()
371 g_simple_action_group_remove (GSimpleActionGroup *simple,
372 const gchar *action_name)
374 g_return_if_fail (G_IS_SIMPLE_ACTION_GROUP (simple));
376 g_action_map_remove_action (G_ACTION_MAP (simple), action_name);
381 * g_simple_action_group_add_entries:
382 * @simple: a #GSimpleActionGroup
383 * @entries: (array length=n_entries): a pointer to the first item in
384 * an array of #GActionEntry structs
385 * @n_entries: the length of @entries, or -1
386 * @user_data: the user data for signal connections
388 * A convenience function for creating multiple #GSimpleAction instances
389 * and adding them to the action group.
393 * Deprecated: 2.38: Use g_action_map_add_action_entries()
396 g_simple_action_group_add_entries (GSimpleActionGroup *simple,
397 const GActionEntry *entries,
401 g_action_map_add_action_entries (G_ACTION_MAP (simple), entries, n_entries, user_data);