[gobject] set all properties before constructed()
[glib.git] / gio / gsimpleactiongroup.c
blob4ff6e004f310c14ad91fdad679762c2e268e3a5e
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 "gsimpleactiongroup.h"
24 #include "gsimpleaction.h"
25 #include "gactionmap.h"
26 #include "gaction.h"
28 /**
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.
35 **/
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))
51 static gchar **
52 g_simple_action_group_list_actions (GActionGroup *group)
54 GSimpleActionGroup *simple = G_SIMPLE_ACTION_GROUP (group);
55 GHashTableIter iter;
56 gint n, i = 0;
57 gchar **keys;
58 gpointer key;
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);
67 keys[n] = NULL;
69 return keys;
72 static gboolean
73 g_simple_action_group_query_action (GActionGroup *group,
74 const gchar *action_name,
75 gboolean *enabled,
76 const GVariantType **parameter_type,
77 const GVariantType **state_type,
78 GVariant **state_hint,
79 GVariant **state)
81 GSimpleActionGroup *simple = G_SIMPLE_ACTION_GROUP (group);
82 GAction *action;
84 action = g_hash_table_lookup (simple->priv->table, action_name);
86 if (action == NULL)
87 return FALSE;
89 if (enabled)
90 *enabled = g_action_get_enabled (action);
92 if (parameter_type)
93 *parameter_type = g_action_get_parameter_type (action);
95 if (state_type)
96 *state_type = g_action_get_state_type (action);
98 if (state_hint)
99 *state_hint = g_action_get_state_hint (action);
101 if (state)
102 *state = g_action_get_state (action);
104 return TRUE;
107 static void
108 g_simple_action_group_change_state (GActionGroup *group,
109 const gchar *action_name,
110 GVariant *value)
112 GSimpleActionGroup *simple = G_SIMPLE_ACTION_GROUP (group);
113 GAction *action;
115 action = g_hash_table_lookup (simple->priv->table, action_name);
117 if (action == NULL)
118 return;
120 g_action_change_state (action, value);
123 static void
124 g_simple_action_group_activate (GActionGroup *group,
125 const gchar *action_name,
126 GVariant *parameter)
128 GSimpleActionGroup *simple = G_SIMPLE_ACTION_GROUP (group);
129 GAction *action;
131 action = g_hash_table_lookup (simple->priv->table, action_name);
133 if (action == NULL)
134 return;
136 g_action_activate (action, parameter);
139 static void
140 action_enabled_notify (GAction *action,
141 GParamSpec *pspec,
142 gpointer user_data)
144 g_action_group_action_enabled_changed (user_data,
145 g_action_get_name (action),
146 g_action_get_enabled (action));
149 static void
150 action_state_notify (GAction *action,
151 GParamSpec *pspec,
152 gpointer user_data)
154 GVariant *value;
156 value = g_action_get_state (action);
157 g_action_group_action_state_changed (user_data,
158 g_action_get_name (action),
159 value);
160 g_variant_unref (value);
163 static void
164 g_simple_action_group_disconnect (gpointer key,
165 gpointer value,
166 gpointer user_data)
168 g_signal_handlers_disconnect_by_func (value, action_enabled_notify,
169 user_data);
170 g_signal_handlers_disconnect_by_func (value, action_state_notify,
171 user_data);
174 static GAction *
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);
183 static void
184 g_simple_action_group_add_action (GActionMap *action_map,
185 GAction *action)
187 GSimpleActionGroup *simple = G_SIMPLE_ACTION_GROUP (action_map);
188 const gchar *action_name;
189 GAction *old_action;
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),
199 action_name);
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);
218 static void
219 g_simple_action_group_remove_action (GActionMap *action_map,
220 const gchar *action_name)
222 GSimpleActionGroup *simple = G_SIMPLE_ACTION_GROUP (action_map);
223 GAction *action;
225 action = g_hash_table_lookup (simple->priv->table, action_name);
227 if (action != NULL)
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);
235 static void
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,
242 simple);
243 g_hash_table_unref (simple->priv->table);
245 G_OBJECT_CLASS (g_simple_action_group_parent_class)
246 ->finalize (object);
249 static void
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);
259 static void
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));
269 static void
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;
278 static void
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
293 * Since: 2.28
295 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
312 * Since: 2.28
314 GAction *
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.
335 * Since: 2.28
337 void
338 g_simple_action_group_insert (GSimpleActionGroup *simple,
339 GAction *action)
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.
355 * Since: 2.28
357 void
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: (array length=n_entries): a pointer to the first item in
371 * an array of #GActionEntry structs
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.
378 * Since: 2.30
380 void
381 g_simple_action_group_add_entries (GSimpleActionGroup *simple,
382 const GActionEntry *entries,
383 gint n_entries,
384 gpointer user_data)
386 g_action_map_add_action_entries (G_ACTION_MAP (simple), entries, n_entries, user_data);