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>
26 G_DEFINE_TYPE (GAction, g_action, G_TYPE_OBJECT)
31 * @short_description: an action
33 * #GAction represents a single named action.
35 * The main interface to an action is that it can be activated with
36 * g_action_activate(). This results in the 'activate' signal being
37 * emitted. An activation has a #GVariant parameter (which may be
38 * %NULL). The correct type for the parameter is determined by a static
39 * parameter type (which is given at construction time).
41 * An action may optionally have a state, in which case the state may be
42 * set with g_action_set_state(). This call takes a #GVariant. The
43 * correct type for the state is determined by a static state type
44 * (which is given at construction time).
46 * The state may have a hint associated with it, specifying its valid
49 * #GAction is intended to be used both as a simple action class and as
50 * a base class for more complicated action types. The base class
51 * itself supports activation and state. Not supported are state hints
52 * and filtering requests to set the state based on the requested value.
53 * You should subclass if you require either of these abilities.
55 * In all cases, the base class is responsible for storing the name of
56 * the action, the parameter type, the enabled state, the optional state
57 * type and the state and emitting the appropriate signals when these
58 * change. The base class is also responsbile for filtering calls to
59 * g_action_activate() and g_action_set_state() for type safety and for
60 * the state being enabled.
62 * Probably the only useful thing to do with a #GAction is to put it
63 * inside of a #GSimpleActionGroup.
66 struct _GActionPrivate
69 GVariantType *parameter_type;
91 static guint g_action_signals[NR_SIGNALS];
94 g_action_real_set_state (GAction *action,
97 if (action->priv->state == value)
100 if (!action->priv->state || !g_variant_equal (action->priv->state, value))
102 if (action->priv->state)
103 g_variant_unref (action->priv->state);
105 action->priv->state = g_variant_ref (value);
107 g_object_notify (G_OBJECT (action), "state");
112 g_action_real_get_state_hint (GAction *action)
118 g_action_set_property (GObject *object, guint prop_id,
119 const GValue *value, GParamSpec *pspec)
121 GAction *action = G_ACTION (object);
126 g_assert (action->priv->name == NULL);
127 action->priv->name = g_value_dup_string (value);
130 case PROP_PARAMETER_TYPE:
131 g_assert (action->priv->parameter_type == NULL);
132 action->priv->parameter_type = g_value_dup_boxed (value);
136 g_action_set_enabled (action, g_value_get_boolean (value));
140 /* PROP_STATE is marked as G_PARAM_CONSTRUCT so we always get a
141 * call during object construction, even if it is NULL. We treat
142 * that first call differently, for a number of reasons.
144 * First, we don't want the value to be rejected by the
145 * possibly-overridden .set_state() function. Second, we don't
146 * want to be tripped by the assertions in g_action_set_state()
147 * that would enforce the catch22 that we only provide a value of
148 * the same type as the existing value (when there is not yet an
151 if (action->priv->state_set)
152 g_action_set_state (action, g_value_get_variant (value));
154 else /* this is the special case */
156 /* only do it the first time. */
157 action->priv->state_set = TRUE;
159 /* blindly set it. */
160 action->priv->state = g_value_dup_variant (value);
165 g_assert_not_reached ();
170 g_action_get_property (GObject *object, guint prop_id,
171 GValue *value, GParamSpec *pspec)
173 GAction *action = G_ACTION (object);
178 g_value_set_string (value, g_action_get_name (action));
181 case PROP_PARAMETER_TYPE:
182 g_value_set_boxed (value, g_action_get_parameter_type (action));
186 g_value_set_boolean (value, g_action_get_enabled (action));
189 case PROP_STATE_TYPE:
190 g_value_set_boxed (value, g_action_get_state_type (action));
194 g_value_set_variant (value, g_action_get_state (action));
198 g_assert_not_reached ();
203 g_action_finalize (GObject *object)
205 GAction *action = G_ACTION (object);
207 g_free (action->priv->name);
208 if (action->priv->parameter_type)
209 g_variant_type_free (action->priv->parameter_type);
210 if (action->priv->state)
211 g_variant_unref (action->priv->state);
213 G_OBJECT_CLASS (g_action_parent_class)
218 g_action_init (GAction *action)
220 action->priv = G_TYPE_INSTANCE_GET_PRIVATE (action,
226 g_action_class_init (GActionClass *class)
228 GObjectClass *object_class = G_OBJECT_CLASS (class);
230 class->get_state_hint = g_action_real_get_state_hint;
231 class->set_state = g_action_real_set_state;
233 object_class->get_property = g_action_get_property;
234 object_class->set_property = g_action_set_property;
235 object_class->finalize = g_action_finalize;
239 * @action: the #GAction
240 * @parameter: (allow-none): the parameter to the activation
242 * Indicates that the action was just activated.
244 * @parameter will always be of the expected type. In the event that
245 * an incorrect type was given, no signal will be emitted.
249 g_action_signals[SIGNAL_ACTIVATE] =
250 g_signal_new (I_("activate"),
253 G_STRUCT_OFFSET (GActionClass, activate),
255 g_cclosure_marshal_VOID__VARIANT,
262 * The name of the action. This is mostly meaningful for identifying
263 * the action once it has been added to a #GActionGroup.
267 g_object_class_install_property (object_class, PROP_NAME,
268 g_param_spec_string ("name",
270 P_("The name used to invoke the action"),
273 G_PARAM_CONSTRUCT_ONLY |
274 G_PARAM_STATIC_STRINGS));
277 * GAction:parameter-type:
279 * The type of the parameter that must be given when activating the
284 g_object_class_install_property (object_class, PROP_PARAMETER_TYPE,
285 g_param_spec_boxed ("parameter-type",
286 P_("Parameter Type"),
287 P_("The type of GVariant passed to activate()"),
290 G_PARAM_CONSTRUCT_ONLY |
291 G_PARAM_STATIC_STRINGS));
296 * If @action is currently enabled.
298 * If the action is disabled then calls to g_action_activate() and
299 * g_action_set_state() have no effect.
303 g_object_class_install_property (object_class, PROP_ENABLED,
304 g_param_spec_boolean ("enabled",
306 P_("If the action can be activated"),
310 G_PARAM_STATIC_STRINGS));
313 * GAction:state-type:
315 * The #GVariantType of the state that the action has, or %NULL if the
316 * action is stateless.
320 g_object_class_install_property (object_class, PROP_STATE_TYPE,
321 g_param_spec_boxed ("state-type",
323 P_("The type of the state kept by the action"),
326 G_PARAM_STATIC_STRINGS));
331 * The state of the action, or %NULL if the action is stateless.
335 g_object_class_install_property (object_class, PROP_STATE,
336 g_param_spec_variant ("state",
338 P_("The state the action is in"),
343 G_PARAM_STATIC_STRINGS));
345 g_type_class_add_private (class, sizeof (GActionPrivate));
349 * g_action_set_state:
350 * @action: a #GAction
351 * @value: the new state
353 * Request for the state of @action to be changed to @value.
355 * The action must be stateful and @value must be of the correct type.
356 * See g_action_get_state_type().
358 * This call merely requests a change. The action may refuse to change
359 * its state or may change its state to something other than @value.
360 * See g_action_get_state_hint().
365 g_action_set_state (GAction *action,
368 const GVariantType *state_type;
370 g_return_if_fail (G_IS_ACTION (action));
371 g_return_if_fail (value != NULL);
372 state_type = g_action_get_state_type (action);
373 g_return_if_fail (state_type != NULL);
374 g_return_if_fail (g_variant_is_of_type (value, state_type));
376 g_variant_ref_sink (value);
378 if (action->priv->enabled)
379 G_ACTION_GET_CLASS (action)
380 ->set_state (action, value);
382 g_variant_unref (value);
386 * g_action_get_state:
387 * @action: a #GAction
389 * Queries the current state of @action.
391 * If the action is not stateful then %NULL will be returned. If the
392 * action is stateful then the type of the return value is the type
393 * given by g_action_get_state_type().
395 * Returns: (allow-none) (transfer none): the current state of the action
400 g_action_get_state (GAction *action)
402 g_return_val_if_fail (G_IS_ACTION (action), NULL);
404 return action->priv->state;
409 * @action: a #GAction
411 * Queries the name of @action.
413 * Returns: the name of the action
418 g_action_get_name (GAction *action)
420 g_return_val_if_fail (G_IS_ACTION (action), NULL);
422 return action->priv->name;
426 * g_action_get_parameter_type:
427 * @action: a #GAction
429 * Queries the type of the parameter that must be given when activating
432 * When activating the action using g_action_activate(), the #GVariant
433 * given to that function must be of the type returned by this function.
435 * In the case that this function returns %NULL, you must not give any
436 * #GVariant, but %NULL instead.
438 * Returns: (allow-none): the parameter type
443 g_action_get_parameter_type (GAction *action)
445 g_return_val_if_fail (G_IS_ACTION (action), NULL);
447 return action->priv->parameter_type;
451 * g_action_get_state_type:
452 * @action: a #GAction
454 * Queries the type of the state of @action.
456 * If the action is stateful (ie: was created with
457 * g_action_new_stateful()) then this function returns the #GVariantType
458 * of the state. This is the type of the initial value given as the
459 * state. All calls to g_action_set_state() must give a #GVariant of
460 * this type and g_action_get_state() will return a #GVariant of the
463 * If the action is not stateful (ie: created with g_action_new()) then
464 * this function will return %NULL. In that case, g_action_get_state()
465 * will return %NULL and you must not call g_action_set_state().
467 * Returns: (allow-none): the state type, if the action is stateful
472 g_action_get_state_type (GAction *action)
474 g_return_val_if_fail (G_IS_ACTION (action), NULL);
476 if (action->priv->state != NULL)
477 return g_variant_get_type (action->priv->state);
483 * g_action_get_state_hint:
484 * @action: a #GAction
486 * Requests a hint about the valid range of values for the state of
489 * If %NULL is returned it either means that the action is not stateful
490 * or that there is no hint about the valid range of values for the
491 * state of the action.
493 * If a #GVariant array is returned then each item in the array is a
494 * possible value for the state. If a #GVariant pair (ie: two-tuple) is
495 * returned then the tuple specifies the inclusive lower and upper bound
496 * of valid values for the state.
498 * In any case, the information is merely a hint. It may be possible to
499 * have a state value outside of the hinted range and setting a value
500 * within the range may fail.
502 * The return value (if non-%NULL) should be freed with
503 * g_variant_unref() when it is no longer required.
505 * Returns: (allow-none): the state range hint
510 g_action_get_state_hint (GAction *action)
512 g_return_val_if_fail (G_IS_ACTION (action), NULL);
514 return G_ACTION_GET_CLASS (action)->get_state_hint (action);
518 * g_action_get_enabled:
519 * @action: a #GAction
521 * Checks if @action is currently enabled.
523 * An action must be enabled in order to be activated or in order to
524 * have its state changed from outside callers.
526 * Returns: whether the action is enabled
531 g_action_get_enabled (GAction *action)
533 g_return_val_if_fail (G_IS_ACTION (action), FALSE);
535 return action->priv->enabled;
539 * g_action_set_enabled:
540 * @action: a #GAction
541 * @enabled: whether the action is enabled
543 * Sets the action as enabled or not.
545 * An action must be enabled in order to be activated or in order to
546 * have its state changed from outside callers.
551 g_action_set_enabled (GAction *action,
554 g_return_if_fail (G_IS_ACTION (action));
558 if (action->priv->enabled != enabled)
560 action->priv->enabled = enabled;
561 g_object_notify (G_OBJECT (action), "enabled");
567 * @action: a #GAction
568 * @parameter: the parameter to the activation
570 * Activates the action.
572 * @parameter must be the correct type of parameter for the action (ie:
573 * the parameter type given at construction time). If the parameter
574 * type was %NULL then @parameter must also be %NULL.
579 g_action_activate (GAction *action,
582 g_return_if_fail (G_IS_ACTION (action));
584 g_return_if_fail (action->priv->parameter_type == NULL ?
586 (parameter != NULL &&
587 g_variant_is_of_type (parameter,
588 action->priv->parameter_type)));
590 if (parameter != NULL)
591 g_variant_ref_sink (parameter);
593 if (action->priv->enabled)
594 g_signal_emit (action, g_action_signals[SIGNAL_ACTIVATE], 0, parameter);
596 if (parameter != NULL)
597 g_variant_unref (parameter);
602 * @name: the name of the action
603 * @parameter_type: the type of parameter to the activate function
605 * Creates a new action.
607 * The created action is stateless. See g_action_new_stateful().
609 * Returns: a new #GAction
614 g_action_new (const gchar *name,
615 const GVariantType *parameter_type)
617 return g_object_new (G_TYPE_ACTION,
619 "parameter-type", parameter_type,
624 * g_action_new_stateful:
625 * @name: the name of the action
626 * @parameter_type: the type of the parameter to the activate function
627 * @state: the initial state of the action
629 * Creates a new stateful action.
631 * @state is the initial state of the action. All future state values
632 * must have the same #GVariantType as the initial state.
634 * Returns: a new #GAction
639 g_action_new_stateful (const gchar *name,
640 const GVariantType *parameter_type,
643 return g_object_new (G_TYPE_ACTION,
645 "parameter-type", parameter_type,