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, see <http://www.gnu.org/licenses/>.
17 * Authors: Ryan Lortie <desrt@desrt.ca>
22 #include "gsimpleaction.h"
28 * SECTION:gsimpleaction
29 * @title: GSimpleAction
30 * @short_description: A simple GAction implementation
33 * A #GSimpleAction is the obvious simple implementation of the #GAction
34 * interface. This is the easiest way to create an action for purposes of
35 * adding it to a #GSimpleActionGroup.
37 * See also #GtkAction.
41 GObject parent_instance;
44 GVariantType *parameter_type;
47 gboolean state_set_already;
50 typedef GObjectClass GSimpleActionClass;
52 static void g_simple_action_iface_init (GActionInterface *iface);
53 G_DEFINE_TYPE_WITH_CODE (GSimpleAction, g_simple_action, G_TYPE_OBJECT,
54 G_IMPLEMENT_INTERFACE (G_TYPE_ACTION, g_simple_action_iface_init))
73 static guint g_simple_action_signals[NR_SIGNALS];
76 g_simple_action_get_name (GAction *action)
78 GSimpleAction *simple = G_SIMPLE_ACTION (action);
83 static const GVariantType *
84 g_simple_action_get_parameter_type (GAction *action)
86 GSimpleAction *simple = G_SIMPLE_ACTION (action);
88 return simple->parameter_type;
91 static const GVariantType *
92 g_simple_action_get_state_type (GAction *action)
94 GSimpleAction *simple = G_SIMPLE_ACTION (action);
96 if (simple->state != NULL)
97 return g_variant_get_type (simple->state);
103 g_simple_action_get_state_hint (GAction *action)
109 g_simple_action_get_enabled (GAction *action)
111 GSimpleAction *simple = G_SIMPLE_ACTION (action);
113 return simple->enabled;
117 g_simple_action_change_state (GAction *action,
120 GSimpleAction *simple = G_SIMPLE_ACTION (action);
122 /* If the user connected a signal handler then they are responsible
123 * for handling state changes.
125 if (g_signal_has_handler_pending (action, g_simple_action_signals[SIGNAL_CHANGE_STATE], 0, TRUE))
126 g_signal_emit (action, g_simple_action_signals[SIGNAL_CHANGE_STATE], 0, value);
128 /* If not, then the default behaviour is to just set the state. */
130 g_simple_action_set_state (simple, value);
134 * g_simple_action_set_state:
135 * @simple: a #GSimpleAction
136 * @value: the new #GVariant for the state
138 * Sets the state of the action.
140 * This directly updates the 'state' property to the given value.
142 * This should only be called by the implementor of the action. Users
143 * of the action should not attempt to directly modify the 'state'
144 * property. Instead, they should call g_action_change_state() to
145 * request the change.
147 * If the @value GVariant is floating, it is consumed.
152 g_simple_action_set_state (GSimpleAction *simple,
155 g_return_if_fail (G_IS_SIMPLE_ACTION (simple));
156 g_return_if_fail (value != NULL);
159 const GVariantType *state_type;
161 state_type = simple->state ?
162 g_variant_get_type (simple->state) : NULL;
163 g_return_if_fail (state_type != NULL);
164 g_return_if_fail (g_variant_is_of_type (value, state_type));
167 g_variant_ref_sink (value);
169 if (!simple->state || !g_variant_equal (simple->state, value))
172 g_variant_unref (simple->state);
174 simple->state = g_variant_ref (value);
176 g_object_notify (G_OBJECT (simple), "state");
179 g_variant_unref (value);
183 g_simple_action_get_state (GAction *action)
185 GSimpleAction *simple = G_SIMPLE_ACTION (action);
187 return simple->state ? g_variant_ref (simple->state) : NULL;
191 g_simple_action_activate (GAction *action,
194 GSimpleAction *simple = G_SIMPLE_ACTION (action);
196 g_return_if_fail (simple->parameter_type == NULL ?
198 (parameter != NULL &&
199 g_variant_is_of_type (parameter,
200 simple->parameter_type)));
202 if (parameter != NULL)
203 g_variant_ref_sink (parameter);
207 /* If the user connected a signal handler then they are responsible
208 * for handling activation.
210 if (g_signal_has_handler_pending (action, g_simple_action_signals[SIGNAL_ACTIVATE], 0, TRUE))
211 g_signal_emit (action, g_simple_action_signals[SIGNAL_ACTIVATE], 0, parameter);
213 /* If not, do some reasonable defaults for stateful actions. */
214 else if (simple->state)
216 /* If we have no parameter and this is a boolean action, toggle. */
217 if (parameter == NULL && g_variant_is_of_type (simple->state, G_VARIANT_TYPE_BOOLEAN))
219 gboolean was_enabled = g_variant_get_boolean (simple->state);
220 g_simple_action_change_state (action, g_variant_new_boolean (!was_enabled));
223 /* else, if the parameter and state type are the same, do a change-state */
224 else if (g_variant_is_of_type (simple->state, g_variant_get_type (parameter)))
225 g_simple_action_change_state (action, parameter);
229 if (parameter != NULL)
230 g_variant_unref (parameter);
234 g_simple_action_set_property (GObject *object,
239 GSimpleAction *action = G_SIMPLE_ACTION (object);
244 action->name = g_strdup (g_value_get_string (value));
247 case PROP_PARAMETER_TYPE:
248 action->parameter_type = g_value_dup_boxed (value);
252 action->enabled = g_value_get_boolean (value);
256 /* The first time we see this (during construct) we should just
257 * take the state as it was handed to us.
259 * After that, we should make sure we go through the same checks
262 if (!action->state_set_already)
264 action->state = g_value_dup_variant (value);
265 action->state_set_already = TRUE;
268 g_simple_action_set_state (action, g_value_get_variant (value));
273 g_assert_not_reached ();
278 g_simple_action_get_property (GObject *object,
283 GAction *action = G_ACTION (object);
288 g_value_set_string (value, g_simple_action_get_name (action));
291 case PROP_PARAMETER_TYPE:
292 g_value_set_boxed (value, g_simple_action_get_parameter_type (action));
296 g_value_set_boolean (value, g_simple_action_get_enabled (action));
299 case PROP_STATE_TYPE:
300 g_value_set_boxed (value, g_simple_action_get_state_type (action));
304 g_value_take_variant (value, g_simple_action_get_state (action));
308 g_assert_not_reached ();
313 g_simple_action_finalize (GObject *object)
315 GSimpleAction *simple = G_SIMPLE_ACTION (object);
317 g_free (simple->name);
318 if (simple->parameter_type)
319 g_variant_type_free (simple->parameter_type);
321 g_variant_unref (simple->state);
323 G_OBJECT_CLASS (g_simple_action_parent_class)
328 g_simple_action_init (GSimpleAction *simple)
330 simple->enabled = TRUE;
334 g_simple_action_iface_init (GActionInterface *iface)
336 iface->get_name = g_simple_action_get_name;
337 iface->get_parameter_type = g_simple_action_get_parameter_type;
338 iface->get_state_type = g_simple_action_get_state_type;
339 iface->get_state_hint = g_simple_action_get_state_hint;
340 iface->get_enabled = g_simple_action_get_enabled;
341 iface->get_state = g_simple_action_get_state;
342 iface->change_state = g_simple_action_change_state;
343 iface->activate = g_simple_action_activate;
347 g_simple_action_class_init (GSimpleActionClass *class)
349 GObjectClass *object_class = G_OBJECT_CLASS (class);
351 object_class->set_property = g_simple_action_set_property;
352 object_class->get_property = g_simple_action_get_property;
353 object_class->finalize = g_simple_action_finalize;
356 * GSimpleAction::activate:
357 * @simple: the #GSimpleAction
358 * @parameter: (allow-none): the parameter to the activation
360 * Indicates that the action was just activated.
362 * @parameter will always be of the expected type. In the event that
363 * an incorrect type was given, no signal will be emitted.
365 * Since GLib 2.40, if no handler is connected to this signal then the
366 * default behaviour for boolean-stated actions with a %NULL parameter
367 * type is to toggle them via the #GSimpleAction::change-state signal.
368 * For stateful actions where the state type is equal to the parameter
369 * type, the default is to forward them directly to
370 * #GSimpleAction::change-state. This should allow almost all users
371 * of #GSimpleAction to connect only one handler or the other.
375 g_simple_action_signals[SIGNAL_ACTIVATE] =
376 g_signal_new (I_("activate"),
377 G_TYPE_SIMPLE_ACTION,
378 G_SIGNAL_RUN_LAST | G_SIGNAL_MUST_COLLECT,
380 g_cclosure_marshal_VOID__VARIANT,
385 * GSimpleAction::change-state:
386 * @simple: the #GSimpleAction
387 * @value: (allow-none): the requested value for the state
389 * Indicates that the action just received a request to change its
392 * @value will always be of the correct state type. In the event that
393 * an incorrect type was given, no signal will be emitted.
395 * If no handler is connected to this signal then the default
396 * behaviour is to call g_simple_action_set_state() to set the state
397 * to the requested value. If you connect a signal handler then no
398 * default action is taken. If the state should change then you must
399 * call g_simple_action_set_state() from the handler.
401 * An example of a 'change-state' handler:
402 * |[<!-- language="C" -->
404 * change_volume_state (GSimpleAction *action,
406 * gpointer user_data)
410 * requested = g_variant_get_int32 (value);
412 * // Volume only goes from 0 to 10
413 * if (0 <= requested && requested <= 10)
414 * g_simple_action_set_state (action, value);
418 * The handler need not set the state to the requested value.
419 * It could set it to any value at all, or take some other action.
423 g_simple_action_signals[SIGNAL_CHANGE_STATE] =
424 g_signal_new (I_("change-state"),
425 G_TYPE_SIMPLE_ACTION,
426 G_SIGNAL_RUN_LAST | G_SIGNAL_MUST_COLLECT,
428 g_cclosure_marshal_VOID__VARIANT,
433 * GSimpleAction:name:
435 * The name of the action. This is mostly meaningful for identifying
436 * the action once it has been added to a #GSimpleActionGroup.
440 g_object_class_install_property (object_class, PROP_NAME,
441 g_param_spec_string ("name",
443 P_("The name used to invoke the action"),
446 G_PARAM_CONSTRUCT_ONLY |
447 G_PARAM_STATIC_STRINGS));
450 * GSimpleAction:parameter-type:
452 * The type of the parameter that must be given when activating the
457 g_object_class_install_property (object_class, PROP_PARAMETER_TYPE,
458 g_param_spec_boxed ("parameter-type",
459 P_("Parameter Type"),
460 P_("The type of GVariant passed to activate()"),
463 G_PARAM_CONSTRUCT_ONLY |
464 G_PARAM_STATIC_STRINGS));
467 * GSimpleAction:enabled:
469 * If @action is currently enabled.
471 * If the action is disabled then calls to g_action_activate() and
472 * g_action_change_state() have no effect.
476 g_object_class_install_property (object_class, PROP_ENABLED,
477 g_param_spec_boolean ("enabled",
479 P_("If the action can be activated"),
482 G_PARAM_STATIC_STRINGS));
485 * GSimpleAction:state-type:
487 * The #GVariantType of the state that the action has, or %NULL if the
488 * action is stateless.
492 g_object_class_install_property (object_class, PROP_STATE_TYPE,
493 g_param_spec_boxed ("state-type",
495 P_("The type of the state kept by the action"),
498 G_PARAM_STATIC_STRINGS));
501 * GSimpleAction:state:
503 * The state of the action, or %NULL if the action is stateless.
507 g_object_class_install_property (object_class, PROP_STATE,
508 g_param_spec_variant ("state",
510 P_("The state the action is in"),
513 G_PARAM_READWRITE | G_PARAM_CONSTRUCT |
514 G_PARAM_STATIC_STRINGS));
518 * g_simple_action_set_enabled:
519 * @simple: a #GSimpleAction
520 * @enabled: whether the action is enabled
522 * Sets the action as enabled or not.
524 * An action must be enabled in order to be activated or in order to
525 * have its state changed from outside callers.
527 * This should only be called by the implementor of the action. Users
528 * of the action should not attempt to modify its enabled flag.
533 g_simple_action_set_enabled (GSimpleAction *simple,
536 g_return_if_fail (G_IS_SIMPLE_ACTION (simple));
540 if (simple->enabled != enabled)
542 simple->enabled = enabled;
543 g_object_notify (G_OBJECT (simple), "enabled");
547 * g_simple_action_new:
548 * @name: the name of the action
549 * @parameter_type: (allow-none): the type of parameter to the activate function
551 * Creates a new action.
553 * The created action is stateless. See g_simple_action_new_stateful().
555 * Returns: a new #GSimpleAction
560 g_simple_action_new (const gchar *name,
561 const GVariantType *parameter_type)
563 return g_object_new (G_TYPE_SIMPLE_ACTION,
565 "parameter-type", parameter_type,
570 * g_simple_action_new_stateful:
571 * @name: the name of the action
572 * @parameter_type: (allow-none): the type of the parameter to the activate function
573 * @state: the initial state of the action
575 * Creates a new stateful action.
577 * @state is the initial state of the action. All future state values
578 * must have the same #GVariantType as the initial state.
580 * If the @state GVariant is floating, it is consumed.
582 * Returns: a new #GSimpleAction
587 g_simple_action_new_stateful (const gchar *name,
588 const GVariantType *parameter_type,
591 return g_object_new (G_TYPE_SIMPLE_ACTION,
593 "parameter-type", parameter_type,