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"
24 #include "gmarshal-internal.h"
27 * SECTION:gactiongroup
28 * @title: GActionGroup
29 * @short_description: A group of actions
33 * #GActionGroup represents a group of actions. Actions can be used to
34 * expose functionality in a structured way, either from one part of a
35 * program to another, or to the outside world. Action groups are often
36 * used together with a #GMenuModel that provides additional
37 * representation data for displaying the actions to the user, e.g. in
40 * The main way to interact with the actions in a GActionGroup is to
41 * activate them with g_action_group_activate_action(). Activating an
42 * action may require a #GVariant parameter. The required type of the
43 * parameter can be inquired with g_action_group_get_action_parameter_type().
44 * Actions may be disabled, see g_action_group_get_action_enabled().
45 * Activating a disabled action has no effect.
47 * Actions may optionally have a state in the form of a #GVariant. The
48 * current state of an action can be inquired with
49 * g_action_group_get_action_state(). Activating a stateful action may
50 * change its state, but it is also possible to set the state by calling
51 * g_action_group_change_action_state().
53 * As typical example, consider a text editing application which has an
54 * option to change the current font to 'bold'. A good way to represent
55 * this would be a stateful action, with a boolean state. Activating the
56 * action would toggle the state.
58 * Each action in the group has a unique name (which is a string). All
59 * method calls, except g_action_group_list_actions() take the name of
60 * an action as an argument.
62 * The #GActionGroup API is meant to be the 'public' API to the action
63 * group. The calls here are exactly the interaction that 'external
64 * forces' (eg: UI, incoming D-Bus messages, etc.) are supposed to have
65 * with actions. 'Internal' APIs (ie: ones meant only to be accessed by
66 * the action group implementation) are found on subclasses. This is
67 * why you will find - for example - g_action_group_get_action_enabled()
68 * but not an equivalent set() call.
70 * Signals are emitted on the action group in response to state changes
71 * on individual actions.
73 * Implementations of #GActionGroup should provide implementations for
74 * the virtual functions g_action_group_list_actions() and
75 * g_action_group_query_action(). The other virtual functions should
76 * not be implemented - their "wrappers" are actually implemented with
77 * calls to g_action_group_query_action().
83 * #GActionGroup is an opaque data structure and can only be accessed
84 * using the following functions.
88 * GActionGroupInterface:
89 * @has_action: the virtual function pointer for g_action_group_has_action()
90 * @list_actions: the virtual function pointer for g_action_group_list_actions()
91 * @get_action_parameter_type: the virtual function pointer for g_action_group_get_action_parameter_type()
92 * @get_action_state_type: the virtual function pointer for g_action_group_get_action_state_type()
93 * @get_action_state_hint: the virtual function pointer for g_action_group_get_action_state_hint()
94 * @get_action_enabled: the virtual function pointer for g_action_group_get_action_enabled()
95 * @get_action_state: the virtual function pointer for g_action_group_get_action_state()
96 * @change_action_state: the virtual function pointer for g_action_group_change_action_state()
97 * @query_action: the virtual function pointer for g_action_group_query_action()
98 * @activate_action: the virtual function pointer for g_action_group_activate_action()
99 * @change_action_state: the virtual function pointer for g_action_group_change_action_state()
100 * @action_added: the class closure for the #GActionGroup::action-added signal
101 * @action_removed: the class closure for the #GActionGroup::action-removed signal
102 * @action_enabled_changed: the class closure for the #GActionGroup::action-enabled-changed signal
103 * @action_state_changed: the class closure for the #GActionGroup::action-enabled-changed signal
105 * The virtual function table for #GActionGroup.
110 G_DEFINE_INTERFACE (GActionGroup, g_action_group, G_TYPE_OBJECT)
115 SIGNAL_ACTION_REMOVED,
116 SIGNAL_ACTION_ENABLED_CHANGED,
117 SIGNAL_ACTION_STATE_CHANGED,
121 static guint g_action_group_signals[NR_SIGNALS];
124 g_action_group_real_has_action (GActionGroup *action_group,
125 const gchar *action_name)
127 return g_action_group_query_action (action_group, action_name, NULL, NULL, NULL, NULL, NULL);
131 g_action_group_real_get_action_enabled (GActionGroup *action_group,
132 const gchar *action_name)
134 gboolean enabled = FALSE;
136 g_action_group_query_action (action_group, action_name, &enabled, NULL, NULL, NULL, NULL);
141 static const GVariantType *
142 g_action_group_real_get_action_parameter_type (GActionGroup *action_group,
143 const gchar *action_name)
145 const GVariantType *type = NULL;
147 g_action_group_query_action (action_group, action_name, NULL, &type, NULL, NULL, NULL);
152 static const GVariantType *
153 g_action_group_real_get_action_state_type (GActionGroup *action_group,
154 const gchar *action_name)
156 const GVariantType *type = NULL;
158 g_action_group_query_action (action_group, action_name, NULL, NULL, &type, NULL, NULL);
164 g_action_group_real_get_action_state_hint (GActionGroup *action_group,
165 const gchar *action_name)
167 GVariant *hint = NULL;
169 g_action_group_query_action (action_group, action_name, NULL, NULL, NULL, &hint, NULL);
175 g_action_group_real_get_action_state (GActionGroup *action_group,
176 const gchar *action_name)
178 GVariant *state = NULL;
180 g_action_group_query_action (action_group, action_name, NULL, NULL, NULL, NULL, &state);
186 g_action_group_real_query_action (GActionGroup *action_group,
187 const gchar *action_name,
189 const GVariantType **parameter_type,
190 const GVariantType **state_type,
191 GVariant **state_hint,
194 GActionGroupInterface *iface = G_ACTION_GROUP_GET_IFACE (action_group);
196 /* we expect implementations to override this method, but we also
197 * allow for implementations that existed before this method was
198 * introduced to override the individual accessors instead.
200 * detect the case that neither has happened and report it.
202 if G_UNLIKELY (iface->has_action == g_action_group_real_has_action ||
203 iface->get_action_enabled == g_action_group_real_get_action_enabled ||
204 iface->get_action_parameter_type == g_action_group_real_get_action_parameter_type ||
205 iface->get_action_state_type == g_action_group_real_get_action_state_type ||
206 iface->get_action_state_hint == g_action_group_real_get_action_state_hint ||
207 iface->get_action_state == g_action_group_real_get_action_state)
209 g_critical ("Class '%s' implements GActionGroup interface without overriding "
210 "query_action() method -- bailing out to avoid infinite recursion.",
211 G_OBJECT_TYPE_NAME (action_group));
215 if (!(* iface->has_action) (action_group, action_name))
219 *enabled = (* iface->get_action_enabled) (action_group, action_name);
221 if (parameter_type != NULL)
222 *parameter_type = (* iface->get_action_parameter_type) (action_group, action_name);
224 if (state_type != NULL)
225 *state_type = (* iface->get_action_state_type) (action_group, action_name);
227 if (state_hint != NULL)
228 *state_hint = (* iface->get_action_state_hint) (action_group, action_name);
231 *state = (* iface->get_action_state) (action_group, action_name);
237 g_action_group_default_init (GActionGroupInterface *iface)
239 iface->has_action = g_action_group_real_has_action;
240 iface->get_action_enabled = g_action_group_real_get_action_enabled;
241 iface->get_action_parameter_type = g_action_group_real_get_action_parameter_type;
242 iface->get_action_state_type = g_action_group_real_get_action_state_type;
243 iface->get_action_state_hint = g_action_group_real_get_action_state_hint;
244 iface->get_action_state = g_action_group_real_get_action_state;
245 iface->query_action = g_action_group_real_query_action;
248 * GActionGroup::action-added:
249 * @action_group: the #GActionGroup that changed
250 * @action_name: the name of the action in @action_group
252 * Signals that a new action was just added to the group.
253 * This signal is emitted after the action has been added
254 * and is now visible.
258 g_action_group_signals[SIGNAL_ACTION_ADDED] =
259 g_signal_new (I_("action-added"),
261 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
262 G_STRUCT_OFFSET (GActionGroupInterface, action_added),
269 * GActionGroup::action-removed:
270 * @action_group: the #GActionGroup that changed
271 * @action_name: the name of the action in @action_group
273 * Signals that an action is just about to be removed from the group.
274 * This signal is emitted before the action is removed, so the action
275 * is still visible and can be queried from the signal handler.
279 g_action_group_signals[SIGNAL_ACTION_REMOVED] =
280 g_signal_new (I_("action-removed"),
282 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
283 G_STRUCT_OFFSET (GActionGroupInterface, action_removed),
291 * GActionGroup::action-enabled-changed:
292 * @action_group: the #GActionGroup that changed
293 * @action_name: the name of the action in @action_group
294 * @enabled: whether the action is enabled or not
296 * Signals that the enabled status of the named action has changed.
300 g_action_group_signals[SIGNAL_ACTION_ENABLED_CHANGED] =
301 g_signal_new (I_("action-enabled-changed"),
303 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
304 G_STRUCT_OFFSET (GActionGroupInterface,
305 action_enabled_changed),
307 _g_cclosure_marshal_VOID__STRING_BOOLEAN,
311 g_signal_set_va_marshaller (g_action_group_signals[SIGNAL_ACTION_ENABLED_CHANGED],
312 G_TYPE_FROM_INTERFACE (iface),
313 _g_cclosure_marshal_VOID__STRING_BOOLEANv);
316 * GActionGroup::action-state-changed:
317 * @action_group: the #GActionGroup that changed
318 * @action_name: the name of the action in @action_group
319 * @value: the new value of the state
321 * Signals that the state of the named action has changed.
325 g_action_group_signals[SIGNAL_ACTION_STATE_CHANGED] =
326 g_signal_new (I_("action-state-changed"),
330 G_SIGNAL_MUST_COLLECT,
331 G_STRUCT_OFFSET (GActionGroupInterface,
332 action_state_changed),
334 _g_cclosure_marshal_VOID__STRING_VARIANT,
338 g_signal_set_va_marshaller (g_action_group_signals[SIGNAL_ACTION_STATE_CHANGED],
339 G_TYPE_FROM_INTERFACE (iface),
340 _g_cclosure_marshal_VOID__STRING_VARIANTv);
344 * g_action_group_list_actions:
345 * @action_group: a #GActionGroup
347 * Lists the actions contained within @action_group.
349 * The caller is responsible for freeing the list with g_strfreev() when
350 * it is no longer required.
352 * Returns: (transfer full): a %NULL-terminated array of the names of the
353 * actions in the group
358 g_action_group_list_actions (GActionGroup *action_group)
360 g_return_val_if_fail (G_IS_ACTION_GROUP (action_group), NULL);
362 return G_ACTION_GROUP_GET_IFACE (action_group)
363 ->list_actions (action_group);
367 * g_action_group_has_action:
368 * @action_group: a #GActionGroup
369 * @action_name: the name of the action to check for
371 * Checks if the named action exists within @action_group.
373 * Returns: whether the named action exists
378 g_action_group_has_action (GActionGroup *action_group,
379 const gchar *action_name)
381 g_return_val_if_fail (G_IS_ACTION_GROUP (action_group), FALSE);
383 return G_ACTION_GROUP_GET_IFACE (action_group)
384 ->has_action (action_group, action_name);
388 * g_action_group_get_action_parameter_type:
389 * @action_group: a #GActionGroup
390 * @action_name: the name of the action to query
392 * Queries the type of the parameter that must be given when activating
393 * the named action within @action_group.
395 * When activating the action using g_action_group_activate_action(),
396 * the #GVariant given to that function must be of the type returned
399 * In the case that this function returns %NULL, you must not give any
400 * #GVariant, but %NULL instead.
402 * The parameter type of a particular action will never change but it is
403 * possible for an action to be removed and for a new action to be added
404 * with the same name but a different parameter type.
406 * Returns: (nullable): the parameter type
411 g_action_group_get_action_parameter_type (GActionGroup *action_group,
412 const gchar *action_name)
414 g_return_val_if_fail (G_IS_ACTION_GROUP (action_group), NULL);
416 return G_ACTION_GROUP_GET_IFACE (action_group)
417 ->get_action_parameter_type (action_group, action_name);
421 * g_action_group_get_action_state_type:
422 * @action_group: a #GActionGroup
423 * @action_name: the name of the action to query
425 * Queries the type of the state of the named action within
428 * If the action is stateful then this function returns the
429 * #GVariantType of the state. All calls to
430 * g_action_group_change_action_state() must give a #GVariant of this
431 * type and g_action_group_get_action_state() will return a #GVariant
434 * If the action is not stateful then this function will return %NULL.
435 * In that case, g_action_group_get_action_state() will return %NULL
436 * and you must not call g_action_group_change_action_state().
438 * The state type of a particular action will never change but it is
439 * possible for an action to be removed and for a new action to be added
440 * with the same name but a different state type.
442 * Returns: (nullable): the state type, if the action is stateful
447 g_action_group_get_action_state_type (GActionGroup *action_group,
448 const gchar *action_name)
450 g_return_val_if_fail (G_IS_ACTION_GROUP (action_group), NULL);
452 return G_ACTION_GROUP_GET_IFACE (action_group)
453 ->get_action_state_type (action_group, action_name);
457 * g_action_group_get_action_state_hint:
458 * @action_group: a #GActionGroup
459 * @action_name: the name of the action to query
461 * Requests a hint about the valid range of values for the state of the
462 * named action within @action_group.
464 * If %NULL is returned it either means that the action is not stateful
465 * or that there is no hint about the valid range of values for the
466 * state of the action.
468 * If a #GVariant array is returned then each item in the array is a
469 * possible value for the state. If a #GVariant pair (ie: two-tuple) is
470 * returned then the tuple specifies the inclusive lower and upper bound
471 * of valid values for the state.
473 * In any case, the information is merely a hint. It may be possible to
474 * have a state value outside of the hinted range and setting a value
475 * within the range may fail.
477 * The return value (if non-%NULL) should be freed with
478 * g_variant_unref() when it is no longer required.
480 * Returns: (nullable) (transfer full): the state range hint
485 g_action_group_get_action_state_hint (GActionGroup *action_group,
486 const gchar *action_name)
488 g_return_val_if_fail (G_IS_ACTION_GROUP (action_group), NULL);
490 return G_ACTION_GROUP_GET_IFACE (action_group)
491 ->get_action_state_hint (action_group, action_name);
495 * g_action_group_get_action_enabled:
496 * @action_group: a #GActionGroup
497 * @action_name: the name of the action to query
499 * Checks if the named action within @action_group is currently enabled.
501 * An action must be enabled in order to be activated or in order to
502 * have its state changed from outside callers.
504 * Returns: whether or not the action is currently enabled
509 g_action_group_get_action_enabled (GActionGroup *action_group,
510 const gchar *action_name)
512 g_return_val_if_fail (G_IS_ACTION_GROUP (action_group), FALSE);
514 return G_ACTION_GROUP_GET_IFACE (action_group)
515 ->get_action_enabled (action_group, action_name);
519 * g_action_group_get_action_state:
520 * @action_group: a #GActionGroup
521 * @action_name: the name of the action to query
523 * Queries the current state of the named action within @action_group.
525 * If the action is not stateful then %NULL will be returned. If the
526 * action is stateful then the type of the return value is the type
527 * given by g_action_group_get_action_state_type().
529 * The return value (if non-%NULL) should be freed with
530 * g_variant_unref() when it is no longer required.
532 * Returns: (nullable) (transfer full): the current state of the action
537 g_action_group_get_action_state (GActionGroup *action_group,
538 const gchar *action_name)
540 g_return_val_if_fail (G_IS_ACTION_GROUP (action_group), NULL);
542 return G_ACTION_GROUP_GET_IFACE (action_group)
543 ->get_action_state (action_group, action_name);
547 * g_action_group_change_action_state:
548 * @action_group: a #GActionGroup
549 * @action_name: the name of the action to request the change on
550 * @value: the new state
552 * Request for the state of the named action within @action_group to be
555 * The action must be stateful and @value must be of the correct type.
556 * See g_action_group_get_action_state_type().
558 * This call merely requests a change. The action may refuse to change
559 * its state or may change its state to something other than @value.
560 * See g_action_group_get_action_state_hint().
562 * If the @value GVariant is floating, it is consumed.
567 g_action_group_change_action_state (GActionGroup *action_group,
568 const gchar *action_name,
571 g_return_if_fail (G_IS_ACTION_GROUP (action_group));
572 g_return_if_fail (action_name != NULL);
573 g_return_if_fail (value != NULL);
575 G_ACTION_GROUP_GET_IFACE (action_group)
576 ->change_action_state (action_group, action_name, value);
580 * g_action_group_activate_action:
581 * @action_group: a #GActionGroup
582 * @action_name: the name of the action to activate
583 * @parameter: (nullable): parameters to the activation
585 * Activate the named action within @action_group.
587 * If the action is expecting a parameter, then the correct type of
588 * parameter must be given as @parameter. If the action is expecting no
589 * parameters then @parameter must be %NULL. See
590 * g_action_group_get_action_parameter_type().
595 g_action_group_activate_action (GActionGroup *action_group,
596 const gchar *action_name,
599 g_return_if_fail (G_IS_ACTION_GROUP (action_group));
600 g_return_if_fail (action_name != NULL);
602 G_ACTION_GROUP_GET_IFACE (action_group)
603 ->activate_action (action_group, action_name, parameter);
607 * g_action_group_action_added:
608 * @action_group: a #GActionGroup
609 * @action_name: the name of an action in the group
611 * Emits the #GActionGroup::action-added signal on @action_group.
613 * This function should only be called by #GActionGroup implementations.
618 g_action_group_action_added (GActionGroup *action_group,
619 const gchar *action_name)
621 g_return_if_fail (G_IS_ACTION_GROUP (action_group));
622 g_return_if_fail (action_name != NULL);
624 g_signal_emit (action_group,
625 g_action_group_signals[SIGNAL_ACTION_ADDED],
626 g_quark_try_string (action_name),
631 * g_action_group_action_removed:
632 * @action_group: a #GActionGroup
633 * @action_name: the name of an action in the group
635 * Emits the #GActionGroup::action-removed signal on @action_group.
637 * This function should only be called by #GActionGroup implementations.
642 g_action_group_action_removed (GActionGroup *action_group,
643 const gchar *action_name)
645 g_return_if_fail (G_IS_ACTION_GROUP (action_group));
646 g_return_if_fail (action_name != NULL);
648 g_signal_emit (action_group,
649 g_action_group_signals[SIGNAL_ACTION_REMOVED],
650 g_quark_try_string (action_name),
655 * g_action_group_action_enabled_changed:
656 * @action_group: a #GActionGroup
657 * @action_name: the name of an action in the group
658 * @enabled: whether or not the action is now enabled
660 * Emits the #GActionGroup::action-enabled-changed signal on @action_group.
662 * This function should only be called by #GActionGroup implementations.
667 g_action_group_action_enabled_changed (GActionGroup *action_group,
668 const gchar *action_name,
671 g_return_if_fail (G_IS_ACTION_GROUP (action_group));
672 g_return_if_fail (action_name != NULL);
676 g_signal_emit (action_group,
677 g_action_group_signals[SIGNAL_ACTION_ENABLED_CHANGED],
678 g_quark_try_string (action_name),
684 * g_action_group_action_state_changed:
685 * @action_group: a #GActionGroup
686 * @action_name: the name of an action in the group
687 * @state: the new state of the named action
689 * Emits the #GActionGroup::action-state-changed signal on @action_group.
691 * This function should only be called by #GActionGroup implementations.
696 g_action_group_action_state_changed (GActionGroup *action_group,
697 const gchar *action_name,
700 g_return_if_fail (G_IS_ACTION_GROUP (action_group));
701 g_return_if_fail (action_name != NULL);
703 g_signal_emit (action_group,
704 g_action_group_signals[SIGNAL_ACTION_STATE_CHANGED],
705 g_quark_try_string (action_name),
711 * g_action_group_query_action:
712 * @action_group: a #GActionGroup
713 * @action_name: the name of an action in the group
714 * @enabled: (out): if the action is presently enabled
715 * @parameter_type: (out) (optional): the parameter type, or %NULL if none needed
716 * @state_type: (out) (optional): the state type, or %NULL if stateless
717 * @state_hint: (out) (optional): the state hint, or %NULL if none
718 * @state: (out) (optional): the current state, or %NULL if stateless
720 * Queries all aspects of the named action within an @action_group.
722 * This function acquires the information available from
723 * g_action_group_has_action(), g_action_group_get_action_enabled(),
724 * g_action_group_get_action_parameter_type(),
725 * g_action_group_get_action_state_type(),
726 * g_action_group_get_action_state_hint() and
727 * g_action_group_get_action_state() with a single function call.
729 * This provides two main benefits.
731 * The first is the improvement in efficiency that comes with not having
732 * to perform repeated lookups of the action in order to discover
733 * different things about it. The second is that implementing
734 * #GActionGroup can now be done by only overriding this one virtual
737 * The interface provides a default implementation of this function that
738 * calls the individual functions, as required, to fetch the
739 * information. The interface also provides default implementations of
740 * those functions that call this function. All implementations,
741 * therefore, must override either this function or all of the others.
743 * If the action exists, %TRUE is returned and any of the requested
744 * fields (as indicated by having a non-%NULL reference passed in) are
745 * filled. If the action doesn't exist, %FALSE is returned and the
746 * fields may or may not have been modified.
748 * Returns: %TRUE if the action exists, else %FALSE
753 g_action_group_query_action (GActionGroup *action_group,
754 const gchar *action_name,
756 const GVariantType **parameter_type,
757 const GVariantType **state_type,
758 GVariant **state_hint,
761 return G_ACTION_GROUP_GET_IFACE (action_group)
762 ->query_action (action_group, action_name, enabled, parameter_type, state_type, state_hint, state);