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;
72 GVariantType *state_type;
92 static guint g_action_signals[NR_SIGNALS];
95 g_action_real_set_state (GAction *action,
98 if (action->priv->state == value)
101 if (!action->priv->state || !g_variant_equal (action->priv->state, value))
103 if (action->priv->state)
104 g_variant_unref (action->priv->state);
106 action->priv->state = g_variant_ref (value);
108 g_object_notify (G_OBJECT (action), "state");
113 g_action_real_get_state_hint (GAction *action)
119 g_action_set_property (GObject *object, guint prop_id,
120 const GValue *value, GParamSpec *pspec)
122 GAction *action = G_ACTION (object);
127 g_assert (action->priv->name == NULL);
128 action->priv->name = g_value_dup_string (value);
131 case PROP_PARAMETER_TYPE:
132 g_assert (action->priv->parameter_type == NULL);
133 action->priv->parameter_type = g_value_dup_boxed (value);
137 g_action_set_enabled (action, g_value_get_boolean (value));
140 case PROP_STATE_TYPE:
141 g_assert (action->priv->state_type == NULL);
142 action->priv->state_type = g_value_dup_boxed (value);
146 if (g_value_get_variant (value))
147 g_action_set_state (action, g_value_get_variant (value));
151 g_assert_not_reached ();
156 g_action_get_property (GObject *object, guint prop_id,
157 GValue *value, GParamSpec *pspec)
159 GAction *action = G_ACTION (object);
164 g_value_set_string (value, g_action_get_name (action));
167 case PROP_PARAMETER_TYPE:
168 g_value_set_boxed (value, g_action_get_parameter_type (action));
172 g_value_set_boolean (value, g_action_get_enabled (action));
175 case PROP_STATE_TYPE:
176 g_value_set_boxed (value, g_action_get_state_type (action));
180 g_value_set_variant (value, g_action_get_state (action));
184 g_assert_not_reached ();
189 g_action_finalize (GObject *object)
191 GAction *action = G_ACTION (object);
193 g_free (action->priv->name);
194 if (action->priv->parameter_type)
195 g_variant_type_free (action->priv->parameter_type);
196 if (action->priv->state_type)
197 g_variant_type_free (action->priv->state_type);
198 if (action->priv->state)
199 g_variant_unref (action->priv->state);
201 G_OBJECT_CLASS (g_action_parent_class)
206 g_action_init (GAction *action)
208 action->priv = G_TYPE_INSTANCE_GET_PRIVATE (action,
214 g_action_class_init (GActionClass *class)
216 GObjectClass *object_class = G_OBJECT_CLASS (class);
218 class->get_state_hint = g_action_real_get_state_hint;
219 class->set_state = g_action_real_set_state;
221 object_class->get_property = g_action_get_property;
222 object_class->set_property = g_action_set_property;
223 object_class->finalize = g_action_finalize;
227 * @action: the #GAction
228 * @parameter: (allow-none): the parameter to the activation
230 * Indicates that the action was just activated.
232 * @parameter will always be of the expected type. In the event that
233 * an incorrect type was given, no signal will be emitted.
237 g_action_signals[SIGNAL_ACTIVATE] =
238 g_signal_new (I_("activate"),
241 G_STRUCT_OFFSET (GActionClass, activate),
243 g_cclosure_marshal_VOID__VARIANT,
250 * The name of the action. This is mostly meaningful for identifying
251 * the action once it has been added to a #GActionGroup.
255 g_object_class_install_property (object_class, PROP_NAME,
256 g_param_spec_string ("name",
258 P_("The name used to invoke the action"),
261 G_PARAM_CONSTRUCT_ONLY |
262 G_PARAM_STATIC_STRINGS));
265 * GAction:parameter-type:
267 * The type of the parameter that must be given when activating the
272 g_object_class_install_property (object_class, PROP_PARAMETER_TYPE,
273 g_param_spec_boxed ("parameter-type",
274 P_("Parameter Type"),
275 P_("The type of GVariant passed to activate()"),
278 G_PARAM_CONSTRUCT_ONLY |
279 G_PARAM_STATIC_STRINGS));
284 * If @action is currently enabled.
286 * If the action is disabled then calls to g_action_activate() and
287 * g_action_set_state() have no effect.
291 g_object_class_install_property (object_class, PROP_ENABLED,
292 g_param_spec_boolean ("enabled",
294 P_("If the action can be activated"),
298 G_PARAM_STATIC_STRINGS));
301 * GAction:state-type:
303 * The #GVariantType of the state that the action has, or %NULL if the
304 * action is stateless.
308 g_object_class_install_property (object_class, PROP_STATE_TYPE,
309 g_param_spec_boxed ("state-type",
311 P_("The type of the state kept by the action"),
314 G_PARAM_CONSTRUCT_ONLY |
315 G_PARAM_STATIC_STRINGS));
320 * The state of the action, or %NULL if the action is stateless.
324 g_object_class_install_property (object_class, PROP_STATE,
325 g_param_spec_variant ("state",
327 P_("The state the action is in"),
332 G_PARAM_STATIC_STRINGS));
334 g_type_class_add_private (class, sizeof (GActionPrivate));
338 * g_action_set_state:
339 * @action: a #GAction
340 * @value: the new state
342 * Request for the state of @action to be changed to @value.
344 * The action must be stateful and @value must be of the correct type.
345 * See g_action_get_state_type().
347 * This call merely requests a change. The action may refuse to change
348 * its state or may change its state to something other than @value.
349 * See g_action_get_state_hint().
354 g_action_set_state (GAction *action,
357 g_return_if_fail (G_IS_ACTION (action));
358 g_return_if_fail ((action->priv->state_type == NULL && value == NULL) ||
359 g_variant_is_of_type (value, action->priv->state_type));
361 g_variant_ref_sink (value);
363 if (action->priv->enabled)
364 G_ACTION_GET_CLASS (action)
365 ->set_state (action, value);
367 g_variant_unref (value);
371 * g_action_get_state:
372 * @action: a #GAction
374 * Queries the current state of @action.
376 * If the action is not stateful then %NULL will be returned. If the
377 * action is stateful then the type of the return value is the type
378 * given by g_action_get_state_type().
380 * The return value (if non-%NULL) should be freed with
381 * g_variant_unref() when it is no longer required.
383 * Returns: (allow-none): the current state of the action
388 g_action_get_state (GAction *action)
390 g_return_val_if_fail (G_IS_ACTION (action), NULL);
392 return action->priv->state;
397 * @action: a #GAction
399 * Queries the name of @action.
401 * Returns: the name of the action
406 g_action_get_name (GAction *action)
408 g_return_val_if_fail (G_IS_ACTION (action), NULL);
410 return action->priv->name;
414 * g_action_get_parameter_type:
415 * @action: a #GAction
417 * Queries the type of the parameter that must be given when activating
420 * When activating the action using g_action_activate(), the #GVariant
421 * given to that function must be of the type returned by this function.
423 * In the case that this function returns %NULL, you must not give any
424 * #GVariant, but %NULL instead.
426 * Returns: (allow-none): the parameter type
431 g_action_get_parameter_type (GAction *action)
433 g_return_val_if_fail (G_IS_ACTION (action), NULL);
435 return action->priv->parameter_type;
439 * g_action_get_state_type:
440 * @action: a #GAction
442 * Queries the type of the state of @action.
444 * If the action is stateful (ie: was created with
445 * g_action_new_stateful()) then this function returns the #GVariantType
446 * of the state. This is the type of the initial value given as the
447 * state. All calls to g_action_set_state() must give a #GVariant of
448 * this type and g_action_get_state() will return a #GVariant of the
451 * If the action is not stateful (ie: created with g_action_new()) then
452 * this function will return %NULL. In that case, g_action_get_state()
453 * will return %NULL and you must not call g_action_set_state().
455 * Returns: (allow-none): the state type, if the action is stateful
460 g_action_get_state_type (GAction *action)
462 g_return_val_if_fail (G_IS_ACTION (action), NULL);
464 return action->priv->state_type;
468 * g_action_get_state_hint:
469 * @action: a #GAction
471 * Requests a hint about the valid range of values for the state of
474 * If %NULL is returned it either means that the action is not stateful
475 * or that there is no hint about the valid range of values for the
476 * state of the action.
478 * If a #GVariant array is returned then each item in the array is a
479 * possible value for the state. If a #GVariant pair (ie: two-tuple) is
480 * returned then the tuple specifies the inclusive lower and upper bound
481 * of valid values for the state.
483 * In any case, the information is merely a hint. It may be possible to
484 * have a state value outside of the hinted range and setting a value
485 * within the range may fail.
487 * The return value (if non-%NULL) should be freed with
488 * g_variant_unref() when it is no longer required.
490 * Returns: (allow-none): the state range hint
495 g_action_get_state_hint (GAction *action)
497 g_return_val_if_fail (G_IS_ACTION (action), NULL);
499 return G_ACTION_GET_CLASS (action)->get_state_hint (action);
503 * g_action_get_enabled:
504 * @action: a #GAction
506 * Checks if @action is currently enabled.
508 * An action must be enabled in order to be activated or in order to
509 * have its state changed from outside callers.
511 * Returns: whether the action is enabled
516 g_action_get_enabled (GAction *action)
518 g_return_val_if_fail (G_IS_ACTION (action), FALSE);
520 return action->priv->enabled;
524 * g_action_set_enabled:
525 * @action: a #GAction
526 * @enabled: whether the action is enabled
528 * Sets the action as enabled or not.
530 * An action must be enabled in order to be activated or in order to
531 * have its state changed from outside callers.
536 g_action_set_enabled (GAction *action,
539 g_return_if_fail (G_IS_ACTION (action));
543 if (action->priv->enabled != enabled)
545 action->priv->enabled = enabled;
546 g_object_notify (G_OBJECT (action), "enabled");
552 * @action: a #GAction
553 * @parameter: the parameter to the activation
555 * Activates the action.
557 * @parameter must be the correct type of parameter for the action (ie:
558 * the parameter type given at construction time). If the parameter
559 * type was %NULL then @parameter must also be %NULL.
564 g_action_activate (GAction *action,
567 g_return_if_fail (G_IS_ACTION (action));
569 g_return_if_fail (action->priv->parameter_type == NULL ?
571 (parameter != NULL &&
572 g_variant_is_of_type (parameter,
573 action->priv->parameter_type)));
575 if (parameter != NULL)
576 g_variant_ref_sink (parameter);
578 if (action->priv->enabled)
579 g_signal_emit (action, g_action_signals[SIGNAL_ACTIVATE], 0, parameter);
581 if (parameter != NULL)
582 g_variant_unref (parameter);
587 * @name: the name of the action
588 * @parameter_type: the type of parameter to the activate function
590 * Creates a new action.
592 * The created action is stateless. See g_action_new_stateful().
594 * Returns: a new #GAction
599 g_action_new (const gchar *name,
600 const GVariantType *parameter_type)
602 return g_object_new (G_TYPE_ACTION,
604 "parameter-type", parameter_type,
609 * g_action_new_stateful:
610 * @name: the name of the action
611 * @parameter_type: the type of the parameter to the activate function
612 * @state: the initial state of the action
614 * Creates a new stateful action.
616 * @state is the initial state of the action. All future state values
617 * must have the same #GVariantType as the initial state.
619 * Returns: a new #GAction
624 g_action_new_stateful (const gchar *name,
625 const GVariantType *parameter_type,
628 return g_object_new (G_TYPE_ACTION,
630 "parameter-type", parameter_type,