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>
21 #include "gactiongroup.h"
26 * SECTION:gactiongroup
27 * @title: GActionGroup
28 * @short_description: A group of actions
32 * #GActionGroup represents a group of actions. Actions can be used to
33 * expose functionality in a structured way, either from one part of a
34 * program to another, or to the outside world. Action groups are often
35 * used together with a #GMenuModel that provides additional
36 * representation data for displaying the actions to the user, e.g. in
39 * The main way to interact with the actions in a GActionGroup is to
40 * activate them with g_action_group_activate_action(). Activating an
41 * action may require a #GVariant parameter. The required type of the
42 * parameter can be inquired with g_action_group_get_action_parameter_type().
43 * Actions may be disabled, see g_action_group_get_action_enabled().
44 * Activating a disabled action has no effect.
46 * Actions may optionally have a state in the form of a #GVariant. The
47 * current state of an action can be inquired with
48 * g_action_group_get_action_state(). Activating a stateful action may
49 * change its state, but it is also possible to set the state by calling
50 * g_action_group_change_action_state().
52 * As typical example, consider a text editing application which has an
53 * option to change the current font to 'bold'. A good way to represent
54 * this would be a stateful action, with a boolean state. Activating the
55 * action would toggle the state.
57 * Each action in the group has a unique name (which is a string). All
58 * method calls, except g_action_group_list_actions() take the name of
59 * an action as an argument.
61 * The #GActionGroup API is meant to be the 'public' API to the action
62 * group. The calls here are exactly the interaction that 'external
63 * forces' (eg: UI, incoming D-Bus messages, etc.) are supposed to have
64 * with actions. 'Internal' APIs (ie: ones meant only to be accessed by
65 * the action group implementation) are found on subclasses. This is
66 * why you will find - for example - g_action_group_get_action_enabled()
67 * but not an equivalent set() call.
69 * Signals are emitted on the action group in response to state changes
70 * on individual actions.
72 * Implementations of #GActionGroup should provide implementations for
73 * the virtual functions g_action_group_list_actions() and
74 * g_action_group_query_action(). The other virtual functions should
75 * not be implemented - their "wrappers" are actually implemented with
76 * calls to g_action_group_query_action().
82 * #GActionGroup is an opaque data structure and can only be accessed
83 * using the following functions.
87 * GActionGroupInterface:
88 * @has_action: the virtual function pointer for g_action_group_has_action()
89 * @list_actions: the virtual function pointer for g_action_group_list_actions()
90 * @get_action_parameter_type: the virtual function pointer for g_action_group_get_action_parameter_type()
91 * @get_action_state_type: the virtual function pointer for g_action_group_get_action_state_type()
92 * @get_action_state_hint: the virtual function pointer for g_action_group_get_action_state_hint()
93 * @get_action_enabled: the virtual function pointer for g_action_group_get_action_enabled()
94 * @get_action_state: the virtual function pointer for g_action_group_get_action_state()
95 * @change_action_state: the virtual function pointer for g_action_group_change_action_state()
96 * @query_action: the virtual function pointer for g_action_group_query_action()
97 * @activate_action: the virtual function pointer for g_action_group_activate_action()
98 * @change_action_state: the virtual function pointer for g_action_group_change_action_state()
99 * @action_added: the class closure for the #GActionGroup::action-added signal
100 * @action_removed: the class closure for the #GActionGroup::action-removed signal
101 * @action_enabled_changed: the class closure for the #GActionGroup::action-enabled-changed signal
102 * @action_state_changed: the class closure for the #GActionGroup::action-enabled-changed signal
104 * The virtual function table for #GActionGroup.
109 G_DEFINE_INTERFACE (GActionGroup
, g_action_group
, G_TYPE_OBJECT
)
114 SIGNAL_ACTION_REMOVED
,
115 SIGNAL_ACTION_ENABLED_CHANGED
,
116 SIGNAL_ACTION_STATE_CHANGED
,
120 static guint g_action_group_signals
[NR_SIGNALS
];
123 g_action_group_real_has_action (GActionGroup
*action_group
,
124 const gchar
*action_name
)
126 return g_action_group_query_action (action_group
, action_name
, NULL
, NULL
, NULL
, NULL
, NULL
);
130 g_action_group_real_get_action_enabled (GActionGroup
*action_group
,
131 const gchar
*action_name
)
133 gboolean enabled
= FALSE
;
135 g_action_group_query_action (action_group
, action_name
, &enabled
, NULL
, NULL
, NULL
, NULL
);
140 static const GVariantType
*
141 g_action_group_real_get_action_parameter_type (GActionGroup
*action_group
,
142 const gchar
*action_name
)
144 const GVariantType
*type
= NULL
;
146 g_action_group_query_action (action_group
, action_name
, NULL
, &type
, NULL
, NULL
, NULL
);
151 static const GVariantType
*
152 g_action_group_real_get_action_state_type (GActionGroup
*action_group
,
153 const gchar
*action_name
)
155 const GVariantType
*type
= NULL
;
157 g_action_group_query_action (action_group
, action_name
, NULL
, NULL
, &type
, NULL
, NULL
);
163 g_action_group_real_get_action_state_hint (GActionGroup
*action_group
,
164 const gchar
*action_name
)
166 GVariant
*hint
= NULL
;
168 g_action_group_query_action (action_group
, action_name
, NULL
, NULL
, NULL
, &hint
, NULL
);
174 g_action_group_real_get_action_state (GActionGroup
*action_group
,
175 const gchar
*action_name
)
177 GVariant
*state
= NULL
;
179 g_action_group_query_action (action_group
, action_name
, NULL
, NULL
, NULL
, NULL
, &state
);
185 g_action_group_real_query_action (GActionGroup
*action_group
,
186 const gchar
*action_name
,
188 const GVariantType
**parameter_type
,
189 const GVariantType
**state_type
,
190 GVariant
**state_hint
,
193 GActionGroupInterface
*iface
= G_ACTION_GROUP_GET_IFACE (action_group
);
195 /* we expect implementations to override this method, but we also
196 * allow for implementations that existed before this method was
197 * introduced to override the individual accessors instead.
199 * detect the case that neither has happened and report it.
201 if G_UNLIKELY (iface
->has_action
== g_action_group_real_has_action
||
202 iface
->get_action_enabled
== g_action_group_real_get_action_enabled
||
203 iface
->get_action_parameter_type
== g_action_group_real_get_action_parameter_type
||
204 iface
->get_action_state_type
== g_action_group_real_get_action_state_type
||
205 iface
->get_action_state_hint
== g_action_group_real_get_action_state_hint
||
206 iface
->get_action_state
== g_action_group_real_get_action_state
)
208 g_critical ("Class '%s' implements GActionGroup interface without overriding "
209 "query_action() method -- bailing out to avoid infinite recursion.",
210 G_OBJECT_TYPE_NAME (action_group
));
214 if (!(* iface
->has_action
) (action_group
, action_name
))
218 *enabled
= (* iface
->get_action_enabled
) (action_group
, action_name
);
220 if (parameter_type
!= NULL
)
221 *parameter_type
= (* iface
->get_action_parameter_type
) (action_group
, action_name
);
223 if (state_type
!= NULL
)
224 *state_type
= (* iface
->get_action_state_type
) (action_group
, action_name
);
226 if (state_hint
!= NULL
)
227 *state_hint
= (* iface
->get_action_state_hint
) (action_group
, action_name
);
230 *state
= (* iface
->get_action_state
) (action_group
, action_name
);
236 g_action_group_default_init (GActionGroupInterface
*iface
)
238 iface
->has_action
= g_action_group_real_has_action
;
239 iface
->get_action_enabled
= g_action_group_real_get_action_enabled
;
240 iface
->get_action_parameter_type
= g_action_group_real_get_action_parameter_type
;
241 iface
->get_action_state_type
= g_action_group_real_get_action_state_type
;
242 iface
->get_action_state_hint
= g_action_group_real_get_action_state_hint
;
243 iface
->get_action_state
= g_action_group_real_get_action_state
;
244 iface
->query_action
= g_action_group_real_query_action
;
247 * GActionGroup::action-added:
248 * @action_group: the #GActionGroup that changed
249 * @action_name: the name of the action in @action_group
251 * Signals that a new action was just added to the group.
252 * This signal is emitted after the action has been added
253 * and is now visible.
257 g_action_group_signals
[SIGNAL_ACTION_ADDED
] =
258 g_signal_new (I_("action-added"),
260 G_SIGNAL_RUN_LAST
| G_SIGNAL_DETAILED
,
261 G_STRUCT_OFFSET (GActionGroupInterface
, action_added
),
263 g_cclosure_marshal_VOID__STRING
,
268 * GActionGroup::action-removed:
269 * @action_group: the #GActionGroup that changed
270 * @action_name: the name of the action in @action_group
272 * Signals that an action is just about to be removed from the group.
273 * This signal is emitted before the action is removed, so the action
274 * is still visible and can be queried from the signal handler.
278 g_action_group_signals
[SIGNAL_ACTION_REMOVED
] =
279 g_signal_new (I_("action-removed"),
281 G_SIGNAL_RUN_LAST
| G_SIGNAL_DETAILED
,
282 G_STRUCT_OFFSET (GActionGroupInterface
, action_removed
),
284 g_cclosure_marshal_VOID__STRING
,
290 * GActionGroup::action-enabled-changed:
291 * @action_group: the #GActionGroup that changed
292 * @action_name: the name of the action in @action_group
293 * @enabled: whether the action is enabled or not
295 * Signals that the enabled status of the named action has changed.
299 g_action_group_signals
[SIGNAL_ACTION_ENABLED_CHANGED
] =
300 g_signal_new (I_("action-enabled-changed"),
302 G_SIGNAL_RUN_LAST
| G_SIGNAL_DETAILED
,
303 G_STRUCT_OFFSET (GActionGroupInterface
,
304 action_enabled_changed
),
312 * GActionGroup::action-state-changed:
313 * @action_group: the #GActionGroup that changed
314 * @action_name: the name of the action in @action_group
315 * @value: the new value of the state
317 * Signals that the state of the named action has changed.
321 g_action_group_signals
[SIGNAL_ACTION_STATE_CHANGED
] =
322 g_signal_new (I_("action-state-changed"),
326 G_SIGNAL_MUST_COLLECT
,
327 G_STRUCT_OFFSET (GActionGroupInterface
,
328 action_state_changed
),
337 * g_action_group_list_actions:
338 * @action_group: a #GActionGroup
340 * Lists the actions contained within @action_group.
342 * The caller is responsible for freeing the list with g_strfreev() when
343 * it is no longer required.
345 * Returns: (transfer full): a %NULL-terminated array of the names of the
346 * actions in the group
351 g_action_group_list_actions (GActionGroup
*action_group
)
353 g_return_val_if_fail (G_IS_ACTION_GROUP (action_group
), NULL
);
355 return G_ACTION_GROUP_GET_IFACE (action_group
)
356 ->list_actions (action_group
);
360 * g_action_group_has_action:
361 * @action_group: a #GActionGroup
362 * @action_name: the name of the action to check for
364 * Checks if the named action exists within @action_group.
366 * Returns: whether the named action exists
371 g_action_group_has_action (GActionGroup
*action_group
,
372 const gchar
*action_name
)
374 g_return_val_if_fail (G_IS_ACTION_GROUP (action_group
), FALSE
);
376 return G_ACTION_GROUP_GET_IFACE (action_group
)
377 ->has_action (action_group
, action_name
);
381 * g_action_group_get_action_parameter_type:
382 * @action_group: a #GActionGroup
383 * @action_name: the name of the action to query
385 * Queries the type of the parameter that must be given when activating
386 * the named action within @action_group.
388 * When activating the action using g_action_group_activate_action(),
389 * the #GVariant given to that function must be of the type returned
392 * In the case that this function returns %NULL, you must not give any
393 * #GVariant, but %NULL instead.
395 * The parameter type of a particular action will never change but it is
396 * possible for an action to be removed and for a new action to be added
397 * with the same name but a different parameter type.
399 * Returns: (nullable): the parameter type
404 g_action_group_get_action_parameter_type (GActionGroup
*action_group
,
405 const gchar
*action_name
)
407 g_return_val_if_fail (G_IS_ACTION_GROUP (action_group
), NULL
);
409 return G_ACTION_GROUP_GET_IFACE (action_group
)
410 ->get_action_parameter_type (action_group
, action_name
);
414 * g_action_group_get_action_state_type:
415 * @action_group: a #GActionGroup
416 * @action_name: the name of the action to query
418 * Queries the type of the state of the named action within
421 * If the action is stateful then this function returns the
422 * #GVariantType of the state. All calls to
423 * g_action_group_change_action_state() must give a #GVariant of this
424 * type and g_action_group_get_action_state() will return a #GVariant
427 * If the action is not stateful then this function will return %NULL.
428 * In that case, g_action_group_get_action_state() will return %NULL
429 * and you must not call g_action_group_change_action_state().
431 * The state type of a particular action will never change but it is
432 * possible for an action to be removed and for a new action to be added
433 * with the same name but a different state type.
435 * Returns: (nullable): the state type, if the action is stateful
440 g_action_group_get_action_state_type (GActionGroup
*action_group
,
441 const gchar
*action_name
)
443 g_return_val_if_fail (G_IS_ACTION_GROUP (action_group
), NULL
);
445 return G_ACTION_GROUP_GET_IFACE (action_group
)
446 ->get_action_state_type (action_group
, action_name
);
450 * g_action_group_get_action_state_hint:
451 * @action_group: a #GActionGroup
452 * @action_name: the name of the action to query
454 * Requests a hint about the valid range of values for the state of the
455 * named action within @action_group.
457 * If %NULL is returned it either means that the action is not stateful
458 * or that there is no hint about the valid range of values for the
459 * state of the action.
461 * If a #GVariant array is returned then each item in the array is a
462 * possible value for the state. If a #GVariant pair (ie: two-tuple) is
463 * returned then the tuple specifies the inclusive lower and upper bound
464 * of valid values for the state.
466 * In any case, the information is merely a hint. It may be possible to
467 * have a state value outside of the hinted range and setting a value
468 * within the range may fail.
470 * The return value (if non-%NULL) should be freed with
471 * g_variant_unref() when it is no longer required.
473 * Returns: (nullable) (transfer full): the state range hint
478 g_action_group_get_action_state_hint (GActionGroup
*action_group
,
479 const gchar
*action_name
)
481 g_return_val_if_fail (G_IS_ACTION_GROUP (action_group
), NULL
);
483 return G_ACTION_GROUP_GET_IFACE (action_group
)
484 ->get_action_state_hint (action_group
, action_name
);
488 * g_action_group_get_action_enabled:
489 * @action_group: a #GActionGroup
490 * @action_name: the name of the action to query
492 * Checks if the named action within @action_group is currently enabled.
494 * An action must be enabled in order to be activated or in order to
495 * have its state changed from outside callers.
497 * Returns: whether or not the action is currently enabled
502 g_action_group_get_action_enabled (GActionGroup
*action_group
,
503 const gchar
*action_name
)
505 g_return_val_if_fail (G_IS_ACTION_GROUP (action_group
), FALSE
);
507 return G_ACTION_GROUP_GET_IFACE (action_group
)
508 ->get_action_enabled (action_group
, action_name
);
512 * g_action_group_get_action_state:
513 * @action_group: a #GActionGroup
514 * @action_name: the name of the action to query
516 * Queries the current state of the named action within @action_group.
518 * If the action is not stateful then %NULL will be returned. If the
519 * action is stateful then the type of the return value is the type
520 * given by g_action_group_get_action_state_type().
522 * The return value (if non-%NULL) should be freed with
523 * g_variant_unref() when it is no longer required.
525 * Returns: (nullable): the current state of the action
530 g_action_group_get_action_state (GActionGroup
*action_group
,
531 const gchar
*action_name
)
533 g_return_val_if_fail (G_IS_ACTION_GROUP (action_group
), NULL
);
535 return G_ACTION_GROUP_GET_IFACE (action_group
)
536 ->get_action_state (action_group
, action_name
);
540 * g_action_group_change_action_state:
541 * @action_group: a #GActionGroup
542 * @action_name: the name of the action to request the change on
543 * @value: the new state
545 * Request for the state of the named action within @action_group to be
548 * The action must be stateful and @value must be of the correct type.
549 * See g_action_group_get_action_state_type().
551 * This call merely requests a change. The action may refuse to change
552 * its state or may change its state to something other than @value.
553 * See g_action_group_get_action_state_hint().
555 * If the @value GVariant is floating, it is consumed.
560 g_action_group_change_action_state (GActionGroup
*action_group
,
561 const gchar
*action_name
,
564 g_return_if_fail (G_IS_ACTION_GROUP (action_group
));
565 g_return_if_fail (action_name
!= NULL
);
566 g_return_if_fail (value
!= NULL
);
568 G_ACTION_GROUP_GET_IFACE (action_group
)
569 ->change_action_state (action_group
, action_name
, value
);
573 * g_action_group_activate_action:
574 * @action_group: a #GActionGroup
575 * @action_name: the name of the action to activate
576 * @parameter: (nullable): parameters to the activation
578 * Activate the named action within @action_group.
580 * If the action is expecting a parameter, then the correct type of
581 * parameter must be given as @parameter. If the action is expecting no
582 * parameters then @parameter must be %NULL. See
583 * g_action_group_get_action_parameter_type().
588 g_action_group_activate_action (GActionGroup
*action_group
,
589 const gchar
*action_name
,
592 g_return_if_fail (G_IS_ACTION_GROUP (action_group
));
593 g_return_if_fail (action_name
!= NULL
);
595 G_ACTION_GROUP_GET_IFACE (action_group
)
596 ->activate_action (action_group
, action_name
, parameter
);
600 * g_action_group_action_added:
601 * @action_group: a #GActionGroup
602 * @action_name: the name of an action in the group
604 * Emits the #GActionGroup::action-added signal on @action_group.
606 * This function should only be called by #GActionGroup implementations.
611 g_action_group_action_added (GActionGroup
*action_group
,
612 const gchar
*action_name
)
614 g_return_if_fail (G_IS_ACTION_GROUP (action_group
));
615 g_return_if_fail (action_name
!= NULL
);
617 g_signal_emit (action_group
,
618 g_action_group_signals
[SIGNAL_ACTION_ADDED
],
619 g_quark_try_string (action_name
),
624 * g_action_group_action_removed:
625 * @action_group: a #GActionGroup
626 * @action_name: the name of an action in the group
628 * Emits the #GActionGroup::action-removed signal on @action_group.
630 * This function should only be called by #GActionGroup implementations.
635 g_action_group_action_removed (GActionGroup
*action_group
,
636 const gchar
*action_name
)
638 g_return_if_fail (G_IS_ACTION_GROUP (action_group
));
639 g_return_if_fail (action_name
!= NULL
);
641 g_signal_emit (action_group
,
642 g_action_group_signals
[SIGNAL_ACTION_REMOVED
],
643 g_quark_try_string (action_name
),
648 * g_action_group_action_enabled_changed:
649 * @action_group: a #GActionGroup
650 * @action_name: the name of an action in the group
651 * @enabled: whether or not the action is now enabled
653 * Emits the #GActionGroup::action-enabled-changed signal on @action_group.
655 * This function should only be called by #GActionGroup implementations.
660 g_action_group_action_enabled_changed (GActionGroup
*action_group
,
661 const gchar
*action_name
,
664 g_return_if_fail (G_IS_ACTION_GROUP (action_group
));
665 g_return_if_fail (action_name
!= NULL
);
669 g_signal_emit (action_group
,
670 g_action_group_signals
[SIGNAL_ACTION_ENABLED_CHANGED
],
671 g_quark_try_string (action_name
),
677 * g_action_group_action_state_changed:
678 * @action_group: a #GActionGroup
679 * @action_name: the name of an action in the group
680 * @state: the new state of the named action
682 * Emits the #GActionGroup::action-state-changed signal on @action_group.
684 * This function should only be called by #GActionGroup implementations.
689 g_action_group_action_state_changed (GActionGroup
*action_group
,
690 const gchar
*action_name
,
693 g_return_if_fail (G_IS_ACTION_GROUP (action_group
));
694 g_return_if_fail (action_name
!= NULL
);
696 g_signal_emit (action_group
,
697 g_action_group_signals
[SIGNAL_ACTION_STATE_CHANGED
],
698 g_quark_try_string (action_name
),
704 * g_action_group_query_action:
705 * @action_group: a #GActionGroup
706 * @action_name: the name of an action in the group
707 * @enabled: (out): if the action is presently enabled
708 * @parameter_type: (out) (optional): the parameter type, or %NULL if none needed
709 * @state_type: (out) (optional): the state type, or %NULL if stateless
710 * @state_hint: (out) (optional): the state hint, or %NULL if none
711 * @state: (out) (optional): the current state, or %NULL if stateless
713 * Queries all aspects of the named action within an @action_group.
715 * This function acquires the information available from
716 * g_action_group_has_action(), g_action_group_get_action_enabled(),
717 * g_action_group_get_action_parameter_type(),
718 * g_action_group_get_action_state_type(),
719 * g_action_group_get_action_state_hint() and
720 * g_action_group_get_action_state() with a single function call.
722 * This provides two main benefits.
724 * The first is the improvement in efficiency that comes with not having
725 * to perform repeated lookups of the action in order to discover
726 * different things about it. The second is that implementing
727 * #GActionGroup can now be done by only overriding this one virtual
730 * The interface provides a default implementation of this function that
731 * calls the individual functions, as required, to fetch the
732 * information. The interface also provides default implementations of
733 * those functions that call this function. All implementations,
734 * therefore, must override either this function or all of the others.
736 * If the action exists, %TRUE is returned and any of the requested
737 * fields (as indicated by having a non-%NULL reference passed in) are
738 * filled. If the action doesn't exist, %FALSE is returned and the
739 * fields may or may not have been modified.
741 * Returns: %TRUE if the action exists, else %FALSE
746 g_action_group_query_action (GActionGroup
*action_group
,
747 const gchar
*action_name
,
749 const GVariantType
**parameter_type
,
750 const GVariantType
**state_type
,
751 GVariant
**state_hint
,
754 return G_ACTION_GROUP_GET_IFACE (action_group
)
755 ->query_action (action_group
, action_name
, enabled
, parameter_type
, state_type
, state_hint
, state
);