2 * Copyright © 2010 Codethink Limited
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at 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, see <http://www.gnu.org/licenses/>.
17 * Authors: Ryan Lortie <desrt@desrt.ca>
22 #include "gsimpleactiongroup.h"
24 #include "gsimpleaction.h"
25 #include "gactionmap.h"
29 * SECTION:gsimpleactiongroup
30 * @title: GSimpleActionGroup
31 * @short_description: A simple GActionGroup implementation
34 * #GSimpleActionGroup is a hash table filled with #GAction objects,
35 * implementing the #GActionGroup and #GActionMap interfaces.
38 struct _GSimpleActionGroupPrivate
40 GHashTable
*table
; /* string -> GAction */
43 static void g_simple_action_group_iface_init (GActionGroupInterface
*);
44 static void g_simple_action_group_map_iface_init (GActionMapInterface
*);
45 G_DEFINE_TYPE_WITH_CODE (GSimpleActionGroup
,
46 g_simple_action_group
, G_TYPE_OBJECT
,
47 G_ADD_PRIVATE (GSimpleActionGroup
)
48 G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_GROUP
,
49 g_simple_action_group_iface_init
);
50 G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_MAP
,
51 g_simple_action_group_map_iface_init
))
54 g_simple_action_group_list_actions (GActionGroup
*group
)
56 GSimpleActionGroup
*simple
= G_SIMPLE_ACTION_GROUP (group
);
62 n
= g_hash_table_size (simple
->priv
->table
);
63 keys
= g_new (gchar
*, n
+ 1);
65 g_hash_table_iter_init (&iter
, simple
->priv
->table
);
66 while (g_hash_table_iter_next (&iter
, &key
, NULL
))
67 keys
[i
++] = g_strdup (key
);
68 g_assert_cmpint (i
, ==, n
);
75 g_simple_action_group_query_action (GActionGroup
*group
,
76 const gchar
*action_name
,
78 const GVariantType
**parameter_type
,
79 const GVariantType
**state_type
,
80 GVariant
**state_hint
,
83 GSimpleActionGroup
*simple
= G_SIMPLE_ACTION_GROUP (group
);
86 action
= g_hash_table_lookup (simple
->priv
->table
, action_name
);
92 *enabled
= g_action_get_enabled (action
);
95 *parameter_type
= g_action_get_parameter_type (action
);
98 *state_type
= g_action_get_state_type (action
);
101 *state_hint
= g_action_get_state_hint (action
);
104 *state
= g_action_get_state (action
);
110 g_simple_action_group_change_state (GActionGroup
*group
,
111 const gchar
*action_name
,
114 GSimpleActionGroup
*simple
= G_SIMPLE_ACTION_GROUP (group
);
117 action
= g_hash_table_lookup (simple
->priv
->table
, action_name
);
122 g_action_change_state (action
, value
);
126 g_simple_action_group_activate (GActionGroup
*group
,
127 const gchar
*action_name
,
130 GSimpleActionGroup
*simple
= G_SIMPLE_ACTION_GROUP (group
);
133 action
= g_hash_table_lookup (simple
->priv
->table
, action_name
);
138 g_action_activate (action
, parameter
);
142 action_enabled_notify (GAction
*action
,
146 g_action_group_action_enabled_changed (user_data
,
147 g_action_get_name (action
),
148 g_action_get_enabled (action
));
152 action_state_notify (GAction
*action
,
158 value
= g_action_get_state (action
);
159 g_action_group_action_state_changed (user_data
,
160 g_action_get_name (action
),
162 g_variant_unref (value
);
166 g_simple_action_group_disconnect (gpointer key
,
170 g_signal_handlers_disconnect_by_func (value
, action_enabled_notify
,
172 g_signal_handlers_disconnect_by_func (value
, action_state_notify
,
177 g_simple_action_group_lookup_action (GActionMap
*action_map
,
178 const gchar
*action_name
)
180 GSimpleActionGroup
*simple
= G_SIMPLE_ACTION_GROUP (action_map
);
182 return g_hash_table_lookup (simple
->priv
->table
, action_name
);
186 g_simple_action_group_add_action (GActionMap
*action_map
,
189 GSimpleActionGroup
*simple
= G_SIMPLE_ACTION_GROUP (action_map
);
190 const gchar
*action_name
;
193 action_name
= g_action_get_name (action
);
194 if (action_name
== NULL
)
196 g_critical ("The supplied action has no name. You must set the "
197 "GAction:name property when creating an action.");
201 old_action
= g_hash_table_lookup (simple
->priv
->table
, action_name
);
203 if (old_action
!= action
)
205 if (old_action
!= NULL
)
207 g_action_group_action_removed (G_ACTION_GROUP (simple
),
209 g_simple_action_group_disconnect (NULL
, old_action
, simple
);
212 g_signal_connect (action
, "notify::enabled",
213 G_CALLBACK (action_enabled_notify
), simple
);
215 if (g_action_get_state_type (action
) != NULL
)
216 g_signal_connect (action
, "notify::state",
217 G_CALLBACK (action_state_notify
), simple
);
219 g_hash_table_insert (simple
->priv
->table
,
220 g_strdup (action_name
),
221 g_object_ref (action
));
223 g_action_group_action_added (G_ACTION_GROUP (simple
), action_name
);
228 g_simple_action_group_remove_action (GActionMap
*action_map
,
229 const gchar
*action_name
)
231 GSimpleActionGroup
*simple
= G_SIMPLE_ACTION_GROUP (action_map
);
234 action
= g_hash_table_lookup (simple
->priv
->table
, action_name
);
238 g_action_group_action_removed (G_ACTION_GROUP (simple
), action_name
);
239 g_simple_action_group_disconnect (NULL
, action
, simple
);
240 g_hash_table_remove (simple
->priv
->table
, action_name
);
245 g_simple_action_group_finalize (GObject
*object
)
247 GSimpleActionGroup
*simple
= G_SIMPLE_ACTION_GROUP (object
);
249 g_hash_table_foreach (simple
->priv
->table
,
250 g_simple_action_group_disconnect
,
252 g_hash_table_unref (simple
->priv
->table
);
254 G_OBJECT_CLASS (g_simple_action_group_parent_class
)
259 g_simple_action_group_init (GSimpleActionGroup
*simple
)
261 simple
->priv
= g_simple_action_group_get_instance_private (simple
);
262 simple
->priv
->table
= g_hash_table_new_full (g_str_hash
, g_str_equal
,
263 g_free
, g_object_unref
);
267 g_simple_action_group_class_init (GSimpleActionGroupClass
*class)
269 GObjectClass
*object_class
= G_OBJECT_CLASS (class);
271 object_class
->finalize
= g_simple_action_group_finalize
;
275 g_simple_action_group_iface_init (GActionGroupInterface
*iface
)
277 iface
->list_actions
= g_simple_action_group_list_actions
;
278 iface
->query_action
= g_simple_action_group_query_action
;
279 iface
->change_action_state
= g_simple_action_group_change_state
;
280 iface
->activate_action
= g_simple_action_group_activate
;
284 g_simple_action_group_map_iface_init (GActionMapInterface
*iface
)
286 iface
->add_action
= g_simple_action_group_add_action
;
287 iface
->remove_action
= g_simple_action_group_remove_action
;
288 iface
->lookup_action
= g_simple_action_group_lookup_action
;
292 * g_simple_action_group_new:
294 * Creates a new, empty, #GSimpleActionGroup.
296 * Returns: a new #GSimpleActionGroup
301 g_simple_action_group_new (void)
303 return g_object_new (G_TYPE_SIMPLE_ACTION_GROUP
, NULL
);
307 * g_simple_action_group_lookup:
308 * @simple: a #GSimpleActionGroup
309 * @action_name: the name of an action
311 * Looks up the action with the name @action_name in the group.
313 * If no such action exists, returns %NULL.
315 * Returns: (transfer none): a #GAction, or %NULL
319 * Deprecated: 2.38: Use g_action_map_lookup_action()
322 g_simple_action_group_lookup (GSimpleActionGroup
*simple
,
323 const gchar
*action_name
)
325 g_return_val_if_fail (G_IS_SIMPLE_ACTION_GROUP (simple
), NULL
);
327 return g_action_map_lookup_action (G_ACTION_MAP (simple
), action_name
);
331 * g_simple_action_group_insert:
332 * @simple: a #GSimpleActionGroup
333 * @action: a #GAction
335 * Adds an action to the action group.
337 * If the action group already contains an action with the same name as
338 * @action then the old action is dropped from the group.
340 * The action group takes its own reference on @action.
344 * Deprecated: 2.38: Use g_action_map_add_action()
347 g_simple_action_group_insert (GSimpleActionGroup
*simple
,
350 g_return_if_fail (G_IS_SIMPLE_ACTION_GROUP (simple
));
352 g_action_map_add_action (G_ACTION_MAP (simple
), action
);
356 * g_simple_action_group_remove:
357 * @simple: a #GSimpleActionGroup
358 * @action_name: the name of the action
360 * Removes the named action from the action group.
362 * If no action of this name is in the group then nothing happens.
366 * Deprecated: 2.38: Use g_action_map_remove_action()
369 g_simple_action_group_remove (GSimpleActionGroup
*simple
,
370 const gchar
*action_name
)
372 g_return_if_fail (G_IS_SIMPLE_ACTION_GROUP (simple
));
374 g_action_map_remove_action (G_ACTION_MAP (simple
), action_name
);
379 * g_simple_action_group_add_entries:
380 * @simple: a #GSimpleActionGroup
381 * @entries: (array length=n_entries): a pointer to the first item in
382 * an array of #GActionEntry structs
383 * @n_entries: the length of @entries, or -1
384 * @user_data: the user data for signal connections
386 * A convenience function for creating multiple #GSimpleAction instances
387 * and adding them to the action group.
391 * Deprecated: 2.38: Use g_action_map_add_action_entries()
394 g_simple_action_group_add_entries (GSimpleActionGroup
*simple
,
395 const GActionEntry
*entries
,
399 g_action_map_add_action_entries (G_ACTION_MAP (simple
), entries
, n_entries
, user_data
);