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>
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
35 * #GSimpleActionGroup is a hash table filled with #GAction objects,
36 * implementing the #GActionGroup and #GActionMap interfaces.
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
))
55 g_simple_action_group_list_actions (GActionGroup
*group
)
57 GSimpleActionGroup
*simple
= G_SIMPLE_ACTION_GROUP (group
);
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
);
76 g_simple_action_group_query_action (GActionGroup
*group
,
77 const gchar
*action_name
,
79 const GVariantType
**parameter_type
,
80 const GVariantType
**state_type
,
81 GVariant
**state_hint
,
84 GSimpleActionGroup
*simple
= G_SIMPLE_ACTION_GROUP (group
);
87 action
= g_hash_table_lookup (simple
->priv
->table
, action_name
);
93 *enabled
= g_action_get_enabled (action
);
96 *parameter_type
= g_action_get_parameter_type (action
);
99 *state_type
= g_action_get_state_type (action
);
102 *state_hint
= g_action_get_state_hint (action
);
105 *state
= g_action_get_state (action
);
111 g_simple_action_group_change_state (GActionGroup
*group
,
112 const gchar
*action_name
,
115 GSimpleActionGroup
*simple
= G_SIMPLE_ACTION_GROUP (group
);
118 action
= g_hash_table_lookup (simple
->priv
->table
, action_name
);
123 g_action_change_state (action
, value
);
127 g_simple_action_group_activate (GActionGroup
*group
,
128 const gchar
*action_name
,
131 GSimpleActionGroup
*simple
= G_SIMPLE_ACTION_GROUP (group
);
134 action
= g_hash_table_lookup (simple
->priv
->table
, action_name
);
139 g_action_activate (action
, parameter
);
143 action_enabled_notify (GAction
*action
,
147 g_action_group_action_enabled_changed (user_data
,
148 g_action_get_name (action
),
149 g_action_get_enabled (action
));
153 action_state_notify (GAction
*action
,
159 value
= g_action_get_state (action
);
160 g_action_group_action_state_changed (user_data
,
161 g_action_get_name (action
),
163 g_variant_unref (value
);
167 g_simple_action_group_disconnect (gpointer key
,
171 g_signal_handlers_disconnect_by_func (value
, action_enabled_notify
,
173 g_signal_handlers_disconnect_by_func (value
, action_state_notify
,
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
);
187 g_simple_action_group_add_action (GActionMap
*action_map
,
190 GSimpleActionGroup
*simple
= G_SIMPLE_ACTION_GROUP (action_map
);
191 const gchar
*action_name
;
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
),
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
);
222 g_simple_action_group_remove_action (GActionMap
*action_map
,
223 const gchar
*action_name
)
225 GSimpleActionGroup
*simple
= G_SIMPLE_ACTION_GROUP (action_map
);
228 action
= g_hash_table_lookup (simple
->priv
->table
, action_name
);
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
);
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
,
246 g_hash_table_unref (simple
->priv
->table
);
248 G_OBJECT_CLASS (g_simple_action_group_parent_class
)
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
);
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
;
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
;
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
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
313 * Deprecated: 2.38: Use g_action_map_lookup_action()
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.
338 * Deprecated: 2.38: Use g_action_map_add_action()
341 g_simple_action_group_insert (GSimpleActionGroup
*simple
,
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.
360 * Deprecated: 2.38: Use g_action_map_remove_action()
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.
385 * Deprecated: 2.38: Use g_action_map_add_action_entries()
388 g_simple_action_group_add_entries (GSimpleActionGroup
*simple
,
389 const GActionEntry
*entries
,
393 g_action_map_add_action_entries (G_ACTION_MAP (simple
), entries
, n_entries
, user_data
);