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>
24 G_DEFINE_TYPE (GAction, g_action, G_TYPE_OBJECT)
29 * @short_description: an action
31 * #GAction represents a single named action.
33 * The main interface to an action is that it can be activated with
34 * g_action_activate(). This results in the 'activate' signal being
35 * emitted. An activation has a #GVariant parameter (which may be
36 * %NULL). The correct type for the parameter is determined by a static
37 * parameter type (which is given at construction time).
39 * An action may optionally have a state, in which case the state may be
40 * set with g_action_set_state(). This call takes a #GVariant. The
41 * correct type for the state is determined by a static state type
42 * (which is given at construction time).
44 * The state may have a hint associated with it, specifying its valid
47 * #GAction is intended to be used both as a simple action class and as
48 * a base class for more complicated action types. The base class
49 * itself supports activation and state. Not supported are state hints
50 * and filtering requests to set the state based on the requested value.
51 * You should subclass if you require either of these abilities.
53 * In all cases, the base class is responsible for storing the name of
54 * the action, the parameter type, the enabled state, the optional state
55 * type and the state and emitting the appropriate signals when these
56 * change. The base class is also responsbile for filtering calls to
57 * g_action_activate() and g_action_set_state() for type safety and for
58 * the state being enabled.
60 * Probably the only useful thing to do with a #GAction is to put it
61 * inside of a #GSimpleActionGroup.
64 struct _GActionPrivate
67 GVariantType *parameter_type;
70 GVariantType *state_type;
90 static guint g_action_signals[NR_SIGNALS];
93 g_action_real_set_state (GAction *action,
96 if (action->priv->state == value)
99 if (!action->priv->state || !g_variant_equal (action->priv->state, value))
101 if (action->priv->state)
102 g_variant_unref (action->priv->state);
104 action->priv->state = g_variant_ref (value);
106 g_object_notify (G_OBJECT (action), "state");
111 g_action_real_get_state_hint (GAction *action)
117 g_action_set_property (GObject *object, guint prop_id,
118 const GValue *value, GParamSpec *pspec)
120 GAction *action = G_ACTION (object);
125 g_assert (action->priv->name == NULL);
126 action->priv->name = g_value_dup_string (value);
129 case PROP_PARAMETER_TYPE:
130 g_assert (action->priv->parameter_type == NULL);
131 action->priv->parameter_type = g_value_dup_boxed (value);
135 g_action_set_enabled (action, g_value_get_boolean (value));
138 case PROP_STATE_TYPE:
139 g_assert (action->priv->state_type == NULL);
140 action->priv->state_type = g_value_dup_boxed (value);
144 if (g_value_get_variant (value))
145 g_action_set_state (action, g_value_get_variant (value));
149 g_assert_not_reached ();
154 g_action_get_property (GObject *object, guint prop_id,
155 GValue *value, GParamSpec *pspec)
157 GAction *action = G_ACTION (object);
162 g_value_set_string (value, g_action_get_name (action));
165 case PROP_PARAMETER_TYPE:
166 g_value_set_boxed (value, g_action_get_parameter_type (action));
170 g_value_set_boolean (value, g_action_get_enabled (action));
173 case PROP_STATE_TYPE:
174 g_value_set_boxed (value, g_action_get_state_type (action));
178 g_value_set_variant (value, g_action_get_state (action));
182 g_assert_not_reached ();
187 g_action_finalize (GObject *object)
189 GAction *action = G_ACTION (object);
191 g_free (action->priv->name);
192 if (action->priv->parameter_type)
193 g_variant_type_free (action->priv->parameter_type);
194 if (action->priv->state_type)
195 g_variant_type_free (action->priv->state_type);
196 if (action->priv->state)
197 g_variant_unref (action->priv->state);
199 G_OBJECT_CLASS (g_action_parent_class)
204 g_action_init (GAction *action)
206 action->priv = G_TYPE_INSTANCE_GET_PRIVATE (action,
212 g_action_class_init (GActionClass *class)
214 GObjectClass *object_class = G_OBJECT_CLASS (class);
216 class->get_state_hint = g_action_real_get_state_hint;
217 class->set_state = g_action_real_set_state;
219 object_class->get_property = g_action_get_property;
220 object_class->set_property = g_action_set_property;
221 object_class->finalize = g_action_finalize;
225 * @action: the #GAction
226 * @parameter: (allow-none): the parameter to the activation
228 * Indicates that the action was just activated.
230 * @parameter will always be of the expected type. In the event that
231 * an incorrect type was given, no signal will be emitted.
233 g_action_signals[SIGNAL_ACTIVATE] =
234 g_signal_new ("activate", G_TYPE_ACTION, G_SIGNAL_RUN_LAST,
235 G_STRUCT_OFFSET (GActionClass, activate),
236 NULL, NULL, g_cclosure_marshal_VOID__VARIANT,
237 G_TYPE_NONE, 1, G_TYPE_VARIANT);
242 * The name of the action. This is mostly meaningful for identifying
243 * the action once it has been added to a #GActionGroup.
245 g_object_class_install_property (object_class, PROP_NAME,
246 g_param_spec_string ("name", "action name",
247 "the name used to invoke the action",
248 NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
249 G_PARAM_STATIC_STRINGS));
252 * GAction:parameter-type:
254 * The type of the parameter that must be given when activating the
257 g_object_class_install_property (object_class, PROP_PARAMETER_TYPE,
258 g_param_spec_boxed ("parameter-type", "parameter type",
259 "the type of GVariant passed to activate()",
260 G_TYPE_VARIANT_TYPE, G_PARAM_READWRITE |
261 G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
266 * If @action is currently enabled.
268 * If the action is disabled then calls to g_action_activate() and
269 * g_action_set_state() have no effect.
271 g_object_class_install_property (object_class, PROP_ENABLED,
272 g_param_spec_boolean ("enabled", "enabled",
273 "if the action can be activated", TRUE,
274 G_PARAM_CONSTRUCT | G_PARAM_READWRITE |
275 G_PARAM_STATIC_STRINGS));
278 * GAction:state-type:
280 * The #GVariantType of the state that the action has, or %NULL if the
281 * action is stateless.
283 g_object_class_install_property (object_class, PROP_STATE_TYPE,
284 g_param_spec_boxed ("state-type", "state type",
285 "the type of the state kept by the action",
286 G_TYPE_VARIANT_TYPE, G_PARAM_READWRITE |
287 G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
292 * The state of the action, or %NULL if the action is stateless.
294 g_object_class_install_property (object_class, PROP_STATE,
295 g_param_spec_variant ("state", "state", "the state the action is in",
296 G_VARIANT_TYPE_ANY, NULL,
297 G_PARAM_CONSTRUCT | G_PARAM_READWRITE |
298 G_PARAM_STATIC_STRINGS));
300 g_type_class_add_private (class, sizeof (GActionPrivate));
304 * g_action_set_state:
305 * @action: a #GAction
306 * @value: the new state
308 * Request for the state of @action to be changed to @value.
310 * The action must be stateful and @value must be of the correct type.
311 * See g_action_get_state_type().
313 * This call merely requests a change. The action may refuse to change
314 * its state or may change its state to something other than @value.
315 * See g_action_get_state_hint().
320 g_action_set_state (GAction *action,
323 g_return_if_fail (G_IS_ACTION (action));
324 g_return_if_fail ((action->priv->state_type == NULL && value == NULL) ||
325 g_variant_is_of_type (value, action->priv->state_type));
327 g_variant_ref_sink (value);
329 if (action->priv->enabled)
330 G_ACTION_GET_CLASS (action)
331 ->set_state (action, value);
333 g_variant_unref (value);
337 * g_action_get_state:
338 * @action: a #GAction
340 * Queries the current state of @action.
342 * If the action is not stateful then %NULL will be returned. If the
343 * action is stateful then the type of the return value is the type
344 * given by g_action_get_state_type().
346 * The return value (if non-%NULL) should be freed with
347 * g_variant_unref() when it is no longer required.
349 * Returns: (allow-none): the current state of the action
354 g_action_get_state (GAction *action)
356 g_return_val_if_fail (G_IS_ACTION (action), NULL);
358 return action->priv->state;
363 * @action: a #GAction
365 * Queries the name of @action.
367 * Returns: the name of the action
372 g_action_get_name (GAction *action)
374 g_return_val_if_fail (G_IS_ACTION (action), NULL);
376 return action->priv->name;
380 * g_action_get_parameter_type:
381 * @action: a #GAction
383 * Queries the type of the parameter that must be given when activating
386 * When activating the action using g_action_activate(), the #GVariant
387 * given to that function must be of the type returned by this function.
389 * In the case that this function returns %NULL, you must not give any
390 * #GVariant, but %NULL instead.
392 * Returns: (allow-none): the parameter type
397 g_action_get_parameter_type (GAction *action)
399 g_return_val_if_fail (G_IS_ACTION (action), NULL);
401 return action->priv->parameter_type;
405 * g_action_get_state_type:
406 * @action: a #GAction
408 * Queries the type of the state of @action.
410 * If the action is stateful (ie: was created with
411 * g_action_new_stateful()) then this function returns the #GVariantType
412 * of the state. This is the type of the initial value given as the
413 * state. All calls to g_action_set_state() must give a #GVariant of
414 * this type and g_action_get_state() will return a #GVariant of the
417 * If the action is not stateful (ie: created with g_action_new()) then
418 * this function will return %NULL. In that case, g_action_get_state()
419 * will return %NULL and you must not call g_action_set_state().
421 * Returns: (allow-none): the state type, if the action is stateful
426 g_action_get_state_type (GAction *action)
428 g_return_val_if_fail (G_IS_ACTION (action), NULL);
430 return action->priv->state_type;
434 * g_action_get_state_hint:
435 * @action: a #GAction
437 * Requests a hint about the valid range of values for the state of
440 * If %NULL is returned it either means that the action is not stateful
441 * or that there is no hint about the valid range of values for the
442 * state of the action.
444 * If a #GVariant array is returned then each item in the array is a
445 * possible value for the state. If a #GVariant pair (ie: two-tuple) is
446 * returned then the tuple specifies the inclusive lower and upper bound
447 * of valid values for the state.
449 * In any case, the information is merely a hint. It may be possible to
450 * have a state value outside of the hinted range and setting a value
451 * within the range may fail.
453 * The return value (if non-%NULL) should be freed with
454 * g_variant_unref() when it is no longer required.
456 * Returns: (allow-none): the state range hint
461 g_action_get_state_hint (GAction *action)
463 g_return_val_if_fail (G_IS_ACTION (action), NULL);
465 return G_ACTION_GET_CLASS (action)
466 ->get_state_hint (action);
470 * g_action_get_enabled:
471 * @action: a #GAction
473 * Checks if @action is currently enabled.
475 * An action must be enabled in order to be activated or in order to
476 * have its state changed from outside callers.
478 * Returns: whether the action is enabled
483 g_action_get_enabled (GAction *action)
485 g_return_val_if_fail (G_IS_ACTION (action), FALSE);
487 return action->priv->enabled;
491 * g_action_set_enabled:
492 * @action: a #GAction
493 * @enabled: whether the action is enabled
495 * Sets the action as enabled or not.
497 * An action must be enabled in order to be activated or in order to
498 * have its state changed from outside callers.
503 g_action_set_enabled (GAction *action,
506 g_return_if_fail (G_IS_ACTION (action));
510 if (action->priv->enabled != enabled)
512 action->priv->enabled = enabled;
513 g_object_notify (G_OBJECT (action), "enabled");
519 * @action: a #GAction
520 * @parameter: the parameter to the activation
522 * Activates the action.
524 * @parameter must be the correct type of parameter for the action (ie:
525 * the parameter type given at construction time). If the parameter
526 * type was %NULL then @parameter must also be %NULL.
531 g_action_activate (GAction *action,
534 g_return_if_fail (G_IS_ACTION (action));
536 g_return_if_fail (action->priv->parameter_type == NULL ?
538 (parameter != NULL &&
539 g_variant_is_of_type (parameter,
540 action->priv->parameter_type)));
543 g_variant_ref_sink (parameter);
545 if (action->priv->enabled)
546 g_signal_emit (action, g_action_signals[SIGNAL_ACTIVATE], 0, parameter);
549 g_variant_unref (parameter);
554 * @name: the name of the action
555 * @parameter_type: the type of parameter to the activate function
557 * Creates a new action.
559 * The created action is stateless. See g_action_new_stateful().
561 * Returns: a new #GAction
566 g_action_new (const gchar *name,
567 const GVariantType *parameter_type)
569 return g_object_new (G_TYPE_ACTION,
571 "parameter-type", parameter_type,
576 * g_action_new_stateful:
577 * @name: the name of the action
578 * @parameter_type: the type of the parameter to the activate function
579 * @state: the initial state of the action
581 * Creates a new stateful action.
583 * @state is the initial state of the action. All future state values
584 * must have the same #GVariantType as the initial state.
586 * Returns: a new #GAction
591 g_action_new_stateful (const gchar *name,
592 const GVariantType *parameter_type,
595 return g_object_new (G_TYPE_ACTION,
597 "parameter-type", parameter_type,