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>
23 #include "gactiongroup.h"
25 #include "gio-marshal.h"
29 * SECTION:gactiongroup
30 * @title: GActionGroup
31 * @short_description: A group of actions
33 * #GActionGroup represents a group of actions.
35 * Each action in the group has a unique name (which is a string). All
36 * method calls, except g_action_group_list_actions() take the name of
37 * an action as an argument.
39 * The #GActionGroup API is meant to be the 'public' API to the action
40 * group. The calls here are exactly the interaction that 'external
41 * forces' (eg: UI, incoming D-Bus messages, etc.) are supposed to have
42 * with actions. 'Internal' APIs (ie: ones meant only to be accessed by
43 * the action group implementation) are found on subclasses. This is
44 * why you will find -- for example -- g_action_group_get_action_enabled()
45 * but not an equivalent <function>set()</function> call.
47 * Signals are emitted on the action group in response to state changes
48 * on individual actions.
51 G_DEFINE_INTERFACE (GActionGroup, g_action_group, G_TYPE_OBJECT)
56 SIGNAL_ACTION_REMOVED,
57 SIGNAL_ACTION_ENABLED_CHANGED,
58 SIGNAL_ACTION_STATE_CHANGED,
62 static guint g_action_group_signals[NR_SIGNALS];
65 g_action_group_default_init (GActionGroupInterface *class)
68 * GActionGroup::action-added:
69 * @action_group: the #GActionGroup that changed
70 * @action_name: the name of the action in @action_group
72 * Signals that a new action was just added to the group.
73 * This signal is emitted after the action has been added
78 g_action_group_signals[SIGNAL_ACTION_ADDED] =
79 g_signal_new (I_("action-added"),
81 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
82 G_STRUCT_OFFSET (GActionGroupInterface, action_added),
84 g_cclosure_marshal_VOID__STRING,
89 * GActionGroup::action-removed:
90 * @action_group: the #GActionGroup that changed
91 * @action_name: the name of the action in @action_group
93 * Signals that an action is just about to be removed from the group.
94 * This signal is emitted before the action is removed, so the action
95 * is still visible and can be queried from the signal handler.
99 g_action_group_signals[SIGNAL_ACTION_REMOVED] =
100 g_signal_new (I_("action-removed"),
102 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
103 G_STRUCT_OFFSET (GActionGroupInterface, action_removed),
105 g_cclosure_marshal_VOID__STRING,
111 * GActionGroup::action-enabled-changed:
112 * @action_group: the #GActionGroup that changed
113 * @action_name: the name of the action in @action_group
114 * @enabled: whether the action is enabled or not
116 * Signals that the enabled status of the named action has changed.
120 g_action_group_signals[SIGNAL_ACTION_ENABLED_CHANGED] =
121 g_signal_new (I_("action-enabled-changed"),
123 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
124 G_STRUCT_OFFSET (GActionGroupInterface,
125 action_enabled_changed),
127 _gio_marshal_VOID__STRING_BOOLEAN,
133 * GActionGroup::action-state-changed:
134 * @action_group: the #GActionGroup that changed
135 * @action_name: the name of the action in @action_group
136 * @value: the new value of the state
138 * Signals that the state of the named action has changed.
142 g_action_group_signals[SIGNAL_ACTION_STATE_CHANGED] =
143 g_signal_new (I_("action-state-changed"),
147 G_SIGNAL_MUST_COLLECT,
148 G_STRUCT_OFFSET (GActionGroupInterface,
149 action_state_changed),
151 _gio_marshal_VOID__STRING_VARIANT,
158 * g_action_group_list_actions:
159 * @action_group: a #GActionGroup
161 * Lists the actions contained within @action_group.
163 * The caller is responsible for freeing the list with g_strfreev() when
164 * it is no longer required.
166 * Returns: (transfer full): a %NULL-terminated array of the names of the
167 * actions in the groupb
172 g_action_group_list_actions (GActionGroup *action_group)
174 g_return_val_if_fail (G_IS_ACTION_GROUP (action_group), NULL);
176 return G_ACTION_GROUP_GET_IFACE (action_group)
177 ->list_actions (action_group);
181 * g_action_group_has_action:
182 * @action_group: a #GActionGroup
183 * @action_name: the name of the action to check for
185 * Checks if the named action exists within @action_group.
187 * Returns: whether the named action exists
192 g_action_group_has_action (GActionGroup *action_group,
193 const gchar *action_name)
195 g_return_val_if_fail (G_IS_ACTION_GROUP (action_group), FALSE);
197 return G_ACTION_GROUP_GET_IFACE (action_group)
198 ->has_action (action_group, action_name);
202 * g_action_group_get_action_parameter_type:
203 * @action_group: a #GActionGroup
204 * @action_name: the name of the action to query
206 * Queries the type of the parameter that must be given when activating
207 * the named action within @action_group.
209 * When activating the action using g_action_group_activate_action(),
210 * the #GVariant given to that function must be of the type returned
213 * In the case that this function returns %NULL, you must not give any
214 * #GVariant, but %NULL instead.
216 * The parameter type of a particular action will never change but it is
217 * possible for an action to be removed and for a new action to be added
218 * with the same name but a different parameter type.
220 * Return value: the parameter type
225 g_action_group_get_action_parameter_type (GActionGroup *action_group,
226 const gchar *action_name)
228 g_return_val_if_fail (G_IS_ACTION_GROUP (action_group), NULL);
230 return G_ACTION_GROUP_GET_IFACE (action_group)
231 ->get_action_parameter_type (action_group, action_name);
235 * g_action_group_get_action_state_type:
236 * @action_group: a #GActionGroup
237 * @action_name: the name of the action to query
239 * Queries the type of the state of the named action within
242 * If the action is stateful then this function returns the
243 * #GVariantType of the state. All calls to
244 * g_action_group_change_action_state() must give a #GVariant of this
245 * type and g_action_group_get_action_state() will return a #GVariant
248 * If the action is not stateful then this function will return %NULL.
249 * In that case, g_action_group_get_action_state() will return %NULL
250 * and you must not call g_action_group_change_action_state().
252 * The state type of a particular action will never change but it is
253 * possible for an action to be removed and for a new action to be added
254 * with the same name but a different state type.
256 * Returns: (transfer full): the state type, if the action is stateful
261 g_action_group_get_action_state_type (GActionGroup *action_group,
262 const gchar *action_name)
264 g_return_val_if_fail (G_IS_ACTION_GROUP (action_group), NULL);
266 return G_ACTION_GROUP_GET_IFACE (action_group)
267 ->get_action_state_type (action_group, action_name);
271 * g_action_group_get_action_state_hint:
272 * @action_group: a #GActionGroup
273 * @action_name: the name of the action to query
275 * Requests a hint about the valid range of values for the state of the
276 * named action within @action_group.
278 * If %NULL is returned it either means that the action is not stateful
279 * or that there is no hint about the valid range of values for the
280 * state of the action.
282 * If a #GVariant array is returned then each item in the array is a
283 * possible value for the state. If a #GVariant pair (ie: two-tuple) is
284 * returned then the tuple specifies the inclusive lower and upper bound
285 * of valid values for the state.
287 * In any case, the information is merely a hint. It may be possible to
288 * have a state value outside of the hinted range and setting a value
289 * within the range may fail.
291 * The return value (if non-%NULL) should be freed with
292 * g_variant_unref() when it is no longer required.
294 * Return value: (transfer full): the state range hint
299 g_action_group_get_action_state_hint (GActionGroup *action_group,
300 const gchar *action_name)
302 g_return_val_if_fail (G_IS_ACTION_GROUP (action_group), NULL);
304 return G_ACTION_GROUP_GET_IFACE (action_group)
305 ->get_action_state_hint (action_group, action_name);
309 * g_action_group_get_action_enabled:
310 * @action_group: a #GActionGroup
311 * @action_name: the name of the action to query
313 * Checks if the named action within @action_group is currently enabled.
315 * An action must be enabled in order to be activated or in order to
316 * have its state changed from outside callers.
318 * Return value: whether or not the action is currently enabled
323 g_action_group_get_action_enabled (GActionGroup *action_group,
324 const gchar *action_name)
326 g_return_val_if_fail (G_IS_ACTION_GROUP (action_group), FALSE);
328 return G_ACTION_GROUP_GET_IFACE (action_group)
329 ->get_action_enabled (action_group, action_name);
333 * g_action_group_get_action_state:
334 * @action_group: a #GActionGroup
335 * @action_name: the name of the action to query
337 * Queries the current state of the named action within @action_group.
339 * If the action is not stateful then %NULL will be returned. If the
340 * action is stateful then the type of the return value is the type
341 * given by g_action_group_get_action_state_type().
343 * The return value (if non-%NULL) should be freed with
344 * g_variant_unref() when it is no longer required.
346 * Return value: (allow-none): the current state of the action
351 g_action_group_get_action_state (GActionGroup *action_group,
352 const gchar *action_name)
354 g_return_val_if_fail (G_IS_ACTION_GROUP (action_group), NULL);
356 return G_ACTION_GROUP_GET_IFACE (action_group)
357 ->get_action_state (action_group, action_name);
361 * g_action_group_change_action_state:
362 * @action_group: a #GActionGroup
363 * @action_name: the name of the action to request the change on
364 * @value: the new state
366 * Request for the state of the named action within @action_group to be
369 * The action must be stateful and @value must be of the correct type.
370 * See g_action_group_get_action_state_type().
372 * This call merely requests a change. The action may refuse to change
373 * its state or may change its state to something other than @value.
374 * See g_action_group_get_action_state_hint().
376 * If the @value GVariant is floating, it is consumed.
381 g_action_group_change_action_state (GActionGroup *action_group,
382 const gchar *action_name,
385 g_return_if_fail (G_IS_ACTION_GROUP (action_group));
386 g_return_if_fail (action_name != NULL);
387 g_return_if_fail (value != NULL);
389 G_ACTION_GROUP_GET_IFACE (action_group)
390 ->change_action_state (action_group, action_name, value);
394 * g_action_group_activate_action:
395 * @action_group: a #GActionGroup
396 * @action_name: the name of the action to activate
397 * @parameter: (allow-none): parameters to the activation
399 * Activate the named action within @action_group.
401 * If the action is expecting a parameter, then the correct type of
402 * parameter must be given as @parameter. If the action is expecting no
403 * parameters then @parameter must be %NULL. See
404 * g_action_group_get_action_parameter_type().
409 g_action_group_activate_action (GActionGroup *action_group,
410 const gchar *action_name,
413 g_return_if_fail (G_IS_ACTION_GROUP (action_group));
414 g_return_if_fail (action_name != NULL);
416 G_ACTION_GROUP_GET_IFACE (action_group)
417 ->activate_action (action_group, action_name, parameter);
421 * g_action_group_action_added:
422 * @action_group: a #GActionGroup
423 * @action_name: the name of an action in the group
425 * Emits the #GActionGroup::action-added signal on @action_group.
427 * This function should only be called by #GActionGroup implementations.
432 g_action_group_action_added (GActionGroup *action_group,
433 const gchar *action_name)
435 g_return_if_fail (G_IS_ACTION_GROUP (action_group));
436 g_return_if_fail (action_name != NULL);
438 g_signal_emit (action_group,
439 g_action_group_signals[SIGNAL_ACTION_ADDED],
440 g_quark_try_string (action_name),
445 * g_action_group_action_removed:
446 * @action_group: a #GActionGroup
447 * @action_name: the name of an action in the group
449 * Emits the #GActionGroup::action-removed signal on @action_group.
451 * This function should only be called by #GActionGroup implementations.
456 g_action_group_action_removed (GActionGroup *action_group,
457 const gchar *action_name)
459 g_return_if_fail (G_IS_ACTION_GROUP (action_group));
460 g_return_if_fail (action_name != NULL);
462 g_signal_emit (action_group,
463 g_action_group_signals[SIGNAL_ACTION_REMOVED],
464 g_quark_try_string (action_name),
469 * g_action_group_action_enabled_changed:
470 * @action_group: a #GActionGroup
471 * @action_name: the name of an action in the group
472 * @enabled: whether or not the action is now enabled
474 * Emits the #GActionGroup::action-enabled-changed signal on @action_group.
476 * This function should only be called by #GActionGroup implementations.
481 g_action_group_action_enabled_changed (GActionGroup *action_group,
482 const gchar *action_name,
485 g_return_if_fail (G_IS_ACTION_GROUP (action_group));
486 g_return_if_fail (action_name != NULL);
490 g_signal_emit (action_group,
491 g_action_group_signals[SIGNAL_ACTION_ENABLED_CHANGED],
492 g_quark_try_string (action_name),
498 * g_action_group_action_state_changed:
499 * @action_group: a #GActionGroup
500 * @action_name: the name of an action in the group
501 * @state: the new state of the named action
503 * Emits the #GActionGroup::action-state-changed signal on @action_group.
505 * This function should only be called by #GActionGroup implementations.
510 g_action_group_action_state_changed (GActionGroup *action_group,
511 const gchar *action_name,
514 g_return_if_fail (G_IS_ACTION_GROUP (action_group));
515 g_return_if_fail (action_name != NULL);
517 g_signal_emit (action_group,
518 g_action_group_signals[SIGNAL_ACTION_STATE_CHANGED],
519 g_quark_try_string (action_name),