glib/tests: Fix non-debug build of slice test
[glib.git] / gio / gsimpleactiongroup.c
blobce4921cd7cffa5eebbc0dab64caaf7aa072de380
1 /*
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 "config.h"
24 #include "gsimpleactiongroup.h"
26 #include "gsimpleaction.h"
27 #include "gactionmap.h"
28 #include "gaction.h"
30 /**
31 * SECTION:gsimpleactiongroup
32 * @title: GSimpleActionGroup
33 * @short_description: A simple GActionGroup implementation
35 * #GSimpleActionGroup is a hash table filled with #GAction objects,
36 * implementing the #GActionGroup and #GActionMap interfaces.
37 **/
39 struct _GSimpleActionGroupPrivate
41 GHashTable *table; /* string -> GAction */
44 static void g_simple_action_group_iface_init (GActionGroupInterface *);
45 static void g_simple_action_group_map_iface_init (GActionMapInterface *);
46 G_DEFINE_TYPE_WITH_CODE (GSimpleActionGroup,
47 g_simple_action_group, G_TYPE_OBJECT,
48 G_ADD_PRIVATE (GSimpleActionGroup)
49 G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_GROUP,
50 g_simple_action_group_iface_init);
51 G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_MAP,
52 g_simple_action_group_map_iface_init))
54 static gchar **
55 g_simple_action_group_list_actions (GActionGroup *group)
57 GSimpleActionGroup *simple = G_SIMPLE_ACTION_GROUP (group);
58 GHashTableIter iter;
59 gint n, i = 0;
60 gchar **keys;
61 gpointer key;
63 n = g_hash_table_size (simple->priv->table);
64 keys = g_new (gchar *, n + 1);
66 g_hash_table_iter_init (&iter, simple->priv->table);
67 while (g_hash_table_iter_next (&iter, &key, NULL))
68 keys[i++] = g_strdup (key);
69 g_assert_cmpint (i, ==, n);
70 keys[n] = NULL;
72 return keys;
75 static gboolean
76 g_simple_action_group_query_action (GActionGroup *group,
77 const gchar *action_name,
78 gboolean *enabled,
79 const GVariantType **parameter_type,
80 const GVariantType **state_type,
81 GVariant **state_hint,
82 GVariant **state)
84 GSimpleActionGroup *simple = G_SIMPLE_ACTION_GROUP (group);
85 GAction *action;
87 action = g_hash_table_lookup (simple->priv->table, action_name);
89 if (action == NULL)
90 return FALSE;
92 if (enabled)
93 *enabled = g_action_get_enabled (action);
95 if (parameter_type)
96 *parameter_type = g_action_get_parameter_type (action);
98 if (state_type)
99 *state_type = g_action_get_state_type (action);
101 if (state_hint)
102 *state_hint = g_action_get_state_hint (action);
104 if (state)
105 *state = g_action_get_state (action);
107 return TRUE;
110 static void
111 g_simple_action_group_change_state (GActionGroup *group,
112 const gchar *action_name,
113 GVariant *value)
115 GSimpleActionGroup *simple = G_SIMPLE_ACTION_GROUP (group);
116 GAction *action;
118 action = g_hash_table_lookup (simple->priv->table, action_name);
120 if (action == NULL)
121 return;
123 g_action_change_state (action, value);
126 static void
127 g_simple_action_group_activate (GActionGroup *group,
128 const gchar *action_name,
129 GVariant *parameter)
131 GSimpleActionGroup *simple = G_SIMPLE_ACTION_GROUP (group);
132 GAction *action;
134 action = g_hash_table_lookup (simple->priv->table, action_name);
136 if (action == NULL)
137 return;
139 g_action_activate (action, parameter);
142 static void
143 action_enabled_notify (GAction *action,
144 GParamSpec *pspec,
145 gpointer user_data)
147 g_action_group_action_enabled_changed (user_data,
148 g_action_get_name (action),
149 g_action_get_enabled (action));
152 static void
153 action_state_notify (GAction *action,
154 GParamSpec *pspec,
155 gpointer user_data)
157 GVariant *value;
159 value = g_action_get_state (action);
160 g_action_group_action_state_changed (user_data,
161 g_action_get_name (action),
162 value);
163 g_variant_unref (value);
166 static void
167 g_simple_action_group_disconnect (gpointer key,
168 gpointer value,
169 gpointer user_data)
171 g_signal_handlers_disconnect_by_func (value, action_enabled_notify,
172 user_data);
173 g_signal_handlers_disconnect_by_func (value, action_state_notify,
174 user_data);
177 static GAction *
178 g_simple_action_group_lookup_action (GActionMap *action_map,
179 const gchar *action_name)
181 GSimpleActionGroup *simple = G_SIMPLE_ACTION_GROUP (action_map);
183 return g_hash_table_lookup (simple->priv->table, action_name);
186 static void
187 g_simple_action_group_add_action (GActionMap *action_map,
188 GAction *action)
190 GSimpleActionGroup *simple = G_SIMPLE_ACTION_GROUP (action_map);
191 const gchar *action_name;
192 GAction *old_action;
194 action_name = g_action_get_name (action);
195 old_action = g_hash_table_lookup (simple->priv->table, action_name);
197 if (old_action != action)
199 if (old_action != NULL)
201 g_action_group_action_removed (G_ACTION_GROUP (simple),
202 action_name);
203 g_simple_action_group_disconnect (NULL, old_action, simple);
206 g_signal_connect (action, "notify::enabled",
207 G_CALLBACK (action_enabled_notify), simple);
209 if (g_action_get_state_type (action) != NULL)
210 g_signal_connect (action, "notify::state",
211 G_CALLBACK (action_state_notify), simple);
213 g_hash_table_insert (simple->priv->table,
214 g_strdup (action_name),
215 g_object_ref (action));
217 g_action_group_action_added (G_ACTION_GROUP (simple), action_name);
221 static void
222 g_simple_action_group_remove_action (GActionMap *action_map,
223 const gchar *action_name)
225 GSimpleActionGroup *simple = G_SIMPLE_ACTION_GROUP (action_map);
226 GAction *action;
228 action = g_hash_table_lookup (simple->priv->table, action_name);
230 if (action != NULL)
232 g_action_group_action_removed (G_ACTION_GROUP (simple), action_name);
233 g_simple_action_group_disconnect (NULL, action, simple);
234 g_hash_table_remove (simple->priv->table, action_name);
238 static void
239 g_simple_action_group_finalize (GObject *object)
241 GSimpleActionGroup *simple = G_SIMPLE_ACTION_GROUP (object);
243 g_hash_table_foreach (simple->priv->table,
244 g_simple_action_group_disconnect,
245 simple);
246 g_hash_table_unref (simple->priv->table);
248 G_OBJECT_CLASS (g_simple_action_group_parent_class)
249 ->finalize (object);
252 static void
253 g_simple_action_group_init (GSimpleActionGroup *simple)
255 simple->priv = g_simple_action_group_get_instance_private (simple);
256 simple->priv->table = g_hash_table_new_full (g_str_hash, g_str_equal,
257 g_free, g_object_unref);
260 static void
261 g_simple_action_group_class_init (GSimpleActionGroupClass *class)
263 GObjectClass *object_class = G_OBJECT_CLASS (class);
265 object_class->finalize = g_simple_action_group_finalize;
268 static void
269 g_simple_action_group_iface_init (GActionGroupInterface *iface)
271 iface->list_actions = g_simple_action_group_list_actions;
272 iface->query_action = g_simple_action_group_query_action;
273 iface->change_action_state = g_simple_action_group_change_state;
274 iface->activate_action = g_simple_action_group_activate;
277 static void
278 g_simple_action_group_map_iface_init (GActionMapInterface *iface)
280 iface->add_action = g_simple_action_group_add_action;
281 iface->remove_action = g_simple_action_group_remove_action;
282 iface->lookup_action = g_simple_action_group_lookup_action;
286 * g_simple_action_group_new:
288 * Creates a new, empty, #GSimpleActionGroup.
290 * Returns: a new #GSimpleActionGroup
292 * Since: 2.28
294 GSimpleActionGroup *
295 g_simple_action_group_new (void)
297 return g_object_new (G_TYPE_SIMPLE_ACTION_GROUP, NULL);
301 * g_simple_action_group_lookup:
302 * @simple: a #GSimpleActionGroup
303 * @action_name: the name of an action
305 * Looks up the action with the name @action_name in the group.
307 * If no such action exists, returns %NULL.
309 * Returns: (transfer none): a #GAction, or %NULL
311 * Since: 2.28
313 * Deprecated: 2.38: Use g_action_map_lookup_action()
315 GAction *
316 g_simple_action_group_lookup (GSimpleActionGroup *simple,
317 const gchar *action_name)
319 g_return_val_if_fail (G_IS_SIMPLE_ACTION_GROUP (simple), NULL);
321 return g_action_map_lookup_action (G_ACTION_MAP (simple), action_name);
325 * g_simple_action_group_insert:
326 * @simple: a #GSimpleActionGroup
327 * @action: a #GAction
329 * Adds an action to the action group.
331 * If the action group already contains an action with the same name as
332 * @action then the old action is dropped from the group.
334 * The action group takes its own reference on @action.
336 * Since: 2.28
338 * Deprecated: 2.38: Use g_action_map_add_action()
340 void
341 g_simple_action_group_insert (GSimpleActionGroup *simple,
342 GAction *action)
344 g_return_if_fail (G_IS_SIMPLE_ACTION_GROUP (simple));
346 g_action_map_add_action (G_ACTION_MAP (simple), action);
350 * g_simple_action_group_remove:
351 * @simple: a #GSimpleActionGroup
352 * @action_name: the name of the action
354 * Removes the named action from the action group.
356 * If no action of this name is in the group then nothing happens.
358 * Since: 2.28
360 * Deprecated: 2.38: Use g_action_map_remove_action()
362 void
363 g_simple_action_group_remove (GSimpleActionGroup *simple,
364 const gchar *action_name)
366 g_return_if_fail (G_IS_SIMPLE_ACTION_GROUP (simple));
368 g_action_map_remove_action (G_ACTION_MAP (simple), action_name);
373 * g_simple_action_group_add_entries:
374 * @simple: a #GSimpleActionGroup
375 * @entries: (array length=n_entries): a pointer to the first item in
376 * an array of #GActionEntry structs
377 * @n_entries: the length of @entries, or -1
378 * @user_data: the user data for signal connections
380 * A convenience function for creating multiple #GSimpleAction instances
381 * and adding them to the action group.
383 * Since: 2.30
385 * Deprecated: 2.38: Use g_action_map_add_action_entries()
387 void
388 g_simple_action_group_add_entries (GSimpleActionGroup *simple,
389 const GActionEntry *entries,
390 gint n_entries,
391 gpointer user_data)
393 g_action_map_add_action_entries (G_ACTION_MAP (simple), entries, n_entries, user_data);