2 * Copyright © 2010 Codethink Limited
4 * SPDX-License-Identifier: LGPL-2.1-or-later
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General
17 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 * Authors: Ryan Lortie <desrt@desrt.ca>
24 #include "gsimpleaction.h"
30 * SECTION:gsimpleaction
31 * @title: GSimpleAction
32 * @short_description: A simple GAction implementation
35 * A #GSimpleAction is the obvious simple implementation of the #GAction
36 * interface. This is the easiest way to create an action for purposes of
37 * adding it to a #GSimpleActionGroup.
39 * See also #GtkAction.
45 * #GSimpleAction is an opaque data structure and can only be accessed
46 * using the following functions.
51 GObject parent_instance;
54 GVariantType *parameter_type;
58 gboolean state_set_already;
61 typedef GObjectClass GSimpleActionClass;
63 static void g_simple_action_iface_init (GActionInterface *iface);
64 G_DEFINE_TYPE_WITH_CODE (GSimpleAction, g_simple_action, G_TYPE_OBJECT,
65 G_IMPLEMENT_INTERFACE (G_TYPE_ACTION, g_simple_action_iface_init))
84 static guint g_simple_action_signals[NR_SIGNALS];
87 g_simple_action_get_name (GAction *action)
89 GSimpleAction *simple = G_SIMPLE_ACTION (action);
94 static const GVariantType *
95 g_simple_action_get_parameter_type (GAction *action)
97 GSimpleAction *simple = G_SIMPLE_ACTION (action);
99 return simple->parameter_type;
102 static const GVariantType *
103 g_simple_action_get_state_type (GAction *action)
105 GSimpleAction *simple = G_SIMPLE_ACTION (action);
107 if (simple->state != NULL)
108 return g_variant_get_type (simple->state);
114 g_simple_action_get_state_hint (GAction *action)
116 GSimpleAction *simple = G_SIMPLE_ACTION (action);
118 if (simple->state_hint != NULL)
119 return g_variant_ref (simple->state_hint);
125 g_simple_action_get_enabled (GAction *action)
127 GSimpleAction *simple = G_SIMPLE_ACTION (action);
129 return simple->enabled;
133 g_simple_action_change_state (GAction *action,
136 GSimpleAction *simple = G_SIMPLE_ACTION (action);
138 /* If the user connected a signal handler then they are responsible
139 * for handling state changes.
141 if (g_signal_has_handler_pending (action, g_simple_action_signals[SIGNAL_CHANGE_STATE], 0, TRUE))
142 g_signal_emit (action, g_simple_action_signals[SIGNAL_CHANGE_STATE], 0, value);
144 /* If not, then the default behaviour is to just set the state. */
146 g_simple_action_set_state (simple, value);
150 * g_simple_action_set_state:
151 * @simple: a #GSimpleAction
152 * @value: the new #GVariant for the state
154 * Sets the state of the action.
156 * This directly updates the 'state' property to the given value.
158 * This should only be called by the implementor of the action. Users
159 * of the action should not attempt to directly modify the 'state'
160 * property. Instead, they should call g_action_change_state() to
161 * request the change.
163 * If the @value GVariant is floating, it is consumed.
168 g_simple_action_set_state (GSimpleAction *simple,
171 g_return_if_fail (G_IS_SIMPLE_ACTION (simple));
172 g_return_if_fail (value != NULL);
175 const GVariantType *state_type;
177 state_type = simple->state ?
178 g_variant_get_type (simple->state) : NULL;
179 g_return_if_fail (state_type != NULL);
180 g_return_if_fail (g_variant_is_of_type (value, state_type));
183 g_variant_ref_sink (value);
185 if (!simple->state || !g_variant_equal (simple->state, value))
188 g_variant_unref (simple->state);
190 simple->state = g_variant_ref (value);
192 g_object_notify (G_OBJECT (simple), "state");
195 g_variant_unref (value);
199 g_simple_action_get_state (GAction *action)
201 GSimpleAction *simple = G_SIMPLE_ACTION (action);
203 return simple->state ? g_variant_ref (simple->state) : NULL;
207 g_simple_action_activate (GAction *action,
210 GSimpleAction *simple = G_SIMPLE_ACTION (action);
212 g_return_if_fail (simple->parameter_type == NULL ?
214 (parameter != NULL &&
215 g_variant_is_of_type (parameter,
216 simple->parameter_type)));
218 if (parameter != NULL)
219 g_variant_ref_sink (parameter);
223 /* If the user connected a signal handler then they are responsible
224 * for handling activation.
226 if (g_signal_has_handler_pending (action, g_simple_action_signals[SIGNAL_ACTIVATE], 0, TRUE))
227 g_signal_emit (action, g_simple_action_signals[SIGNAL_ACTIVATE], 0, parameter);
229 /* If not, do some reasonable defaults for stateful actions. */
230 else if (simple->state)
232 /* If we have no parameter and this is a boolean action, toggle. */
233 if (parameter == NULL && g_variant_is_of_type (simple->state, G_VARIANT_TYPE_BOOLEAN))
235 gboolean was_enabled = g_variant_get_boolean (simple->state);
236 g_simple_action_change_state (action, g_variant_new_boolean (!was_enabled));
239 /* else, if the parameter and state type are the same, do a change-state */
240 else if (g_variant_is_of_type (simple->state, g_variant_get_type (parameter)))
241 g_simple_action_change_state (action, parameter);
245 if (parameter != NULL)
246 g_variant_unref (parameter);
250 g_simple_action_set_property (GObject *object,
255 GSimpleAction *action = G_SIMPLE_ACTION (object);
260 action->name = g_strdup (g_value_get_string (value));
263 case PROP_PARAMETER_TYPE:
264 action->parameter_type = g_value_dup_boxed (value);
268 action->enabled = g_value_get_boolean (value);
272 /* The first time we see this (during construct) we should just
273 * take the state as it was handed to us.
275 * After that, we should make sure we go through the same checks
278 if (!action->state_set_already)
280 action->state = g_value_dup_variant (value);
281 action->state_set_already = TRUE;
284 g_simple_action_set_state (action, g_value_get_variant (value));
289 g_assert_not_reached ();
294 g_simple_action_get_property (GObject *object,
299 GAction *action = G_ACTION (object);
304 g_value_set_string (value, g_simple_action_get_name (action));
307 case PROP_PARAMETER_TYPE:
308 g_value_set_boxed (value, g_simple_action_get_parameter_type (action));
312 g_value_set_boolean (value, g_simple_action_get_enabled (action));
315 case PROP_STATE_TYPE:
316 g_value_set_boxed (value, g_simple_action_get_state_type (action));
320 g_value_take_variant (value, g_simple_action_get_state (action));
324 g_assert_not_reached ();
329 g_simple_action_finalize (GObject *object)
331 GSimpleAction *simple = G_SIMPLE_ACTION (object);
333 g_free (simple->name);
334 if (simple->parameter_type)
335 g_variant_type_free (simple->parameter_type);
337 g_variant_unref (simple->state);
338 if (simple->state_hint)
339 g_variant_unref (simple->state_hint);
341 G_OBJECT_CLASS (g_simple_action_parent_class)
346 g_simple_action_init (GSimpleAction *simple)
348 simple->enabled = TRUE;
352 g_simple_action_iface_init (GActionInterface *iface)
354 iface->get_name = g_simple_action_get_name;
355 iface->get_parameter_type = g_simple_action_get_parameter_type;
356 iface->get_state_type = g_simple_action_get_state_type;
357 iface->get_state_hint = g_simple_action_get_state_hint;
358 iface->get_enabled = g_simple_action_get_enabled;
359 iface->get_state = g_simple_action_get_state;
360 iface->change_state = g_simple_action_change_state;
361 iface->activate = g_simple_action_activate;
365 g_simple_action_class_init (GSimpleActionClass *class)
367 GObjectClass *object_class = G_OBJECT_CLASS (class);
369 object_class->set_property = g_simple_action_set_property;
370 object_class->get_property = g_simple_action_get_property;
371 object_class->finalize = g_simple_action_finalize;
374 * GSimpleAction::activate:
375 * @simple: the #GSimpleAction
376 * @parameter: (nullable): the parameter to the activation, or %NULL if it has
379 * Indicates that the action was just activated.
381 * @parameter will always be of the expected type, i.e. the parameter type
382 * specified when the action was created. If an incorrect type is given when
383 * activating the action, this signal is not emitted.
385 * Since GLib 2.40, if no handler is connected to this signal then the
386 * default behaviour for boolean-stated actions with a %NULL parameter
387 * type is to toggle them via the #GSimpleAction::change-state signal.
388 * For stateful actions where the state type is equal to the parameter
389 * type, the default is to forward them directly to
390 * #GSimpleAction::change-state. This should allow almost all users
391 * of #GSimpleAction to connect only one handler or the other.
395 g_simple_action_signals[SIGNAL_ACTIVATE] =
396 g_signal_new (I_("activate"),
397 G_TYPE_SIMPLE_ACTION,
398 G_SIGNAL_RUN_LAST | G_SIGNAL_MUST_COLLECT,
405 * GSimpleAction::change-state:
406 * @simple: the #GSimpleAction
407 * @value: (nullable): the requested value for the state
409 * Indicates that the action just received a request to change its
412 * @value will always be of the correct state type, i.e. the type of the
413 * initial state passed to g_simple_action_new_stateful(). If an incorrect
414 * type is given when requesting to change the state, this signal is not
417 * If no handler is connected to this signal then the default
418 * behaviour is to call g_simple_action_set_state() to set the state
419 * to the requested value. If you connect a signal handler then no
420 * default action is taken. If the state should change then you must
421 * call g_simple_action_set_state() from the handler.
423 * An example of a 'change-state' handler:
424 * |[<!-- language="C" -->
426 * change_volume_state (GSimpleAction *action,
428 * gpointer user_data)
432 * requested = g_variant_get_int32 (value);
434 * // Volume only goes from 0 to 10
435 * if (0 <= requested && requested <= 10)
436 * g_simple_action_set_state (action, value);
440 * The handler need not set the state to the requested value.
441 * It could set it to any value at all, or take some other action.
445 g_simple_action_signals[SIGNAL_CHANGE_STATE] =
446 g_signal_new (I_("change-state"),
447 G_TYPE_SIMPLE_ACTION,
448 G_SIGNAL_RUN_LAST | G_SIGNAL_MUST_COLLECT,
455 * GSimpleAction:name:
457 * The name of the action. This is mostly meaningful for identifying
458 * the action once it has been added to a #GSimpleActionGroup.
462 g_object_class_install_property (object_class, PROP_NAME,
463 g_param_spec_string ("name",
465 P_("The name used to invoke the action"),
468 G_PARAM_CONSTRUCT_ONLY |
469 G_PARAM_STATIC_STRINGS));
472 * GSimpleAction:parameter-type:
474 * The type of the parameter that must be given when activating the
479 g_object_class_install_property (object_class, PROP_PARAMETER_TYPE,
480 g_param_spec_boxed ("parameter-type",
481 P_("Parameter Type"),
482 P_("The type of GVariant passed to activate()"),
485 G_PARAM_CONSTRUCT_ONLY |
486 G_PARAM_STATIC_STRINGS));
489 * GSimpleAction:enabled:
491 * If @action is currently enabled.
493 * If the action is disabled then calls to g_action_activate() and
494 * g_action_change_state() have no effect.
498 g_object_class_install_property (object_class, PROP_ENABLED,
499 g_param_spec_boolean ("enabled",
501 P_("If the action can be activated"),
504 G_PARAM_STATIC_STRINGS));
507 * GSimpleAction:state-type:
509 * The #GVariantType of the state that the action has, or %NULL if the
510 * action is stateless.
514 g_object_class_install_property (object_class, PROP_STATE_TYPE,
515 g_param_spec_boxed ("state-type",
517 P_("The type of the state kept by the action"),
520 G_PARAM_STATIC_STRINGS));
523 * GSimpleAction:state:
525 * The state of the action, or %NULL if the action is stateless.
529 g_object_class_install_property (object_class, PROP_STATE,
530 g_param_spec_variant ("state",
532 P_("The state the action is in"),
535 G_PARAM_READWRITE | G_PARAM_CONSTRUCT |
536 G_PARAM_STATIC_STRINGS));
540 * g_simple_action_set_enabled:
541 * @simple: a #GSimpleAction
542 * @enabled: whether the action is enabled
544 * Sets the action as enabled or not.
546 * An action must be enabled in order to be activated or in order to
547 * have its state changed from outside callers.
549 * This should only be called by the implementor of the action. Users
550 * of the action should not attempt to modify its enabled flag.
555 g_simple_action_set_enabled (GSimpleAction *simple,
558 g_return_if_fail (G_IS_SIMPLE_ACTION (simple));
562 if (simple->enabled != enabled)
564 simple->enabled = enabled;
565 g_object_notify (G_OBJECT (simple), "enabled");
570 * g_simple_action_set_state_hint:
571 * @simple: a #GSimpleAction
572 * @state_hint: (nullable): a #GVariant representing the state hint
574 * Sets the state hint for the action.
576 * See g_action_get_state_hint() for more information about
577 * action state hints.
582 g_simple_action_set_state_hint (GSimpleAction *simple,
583 GVariant *state_hint)
585 g_return_if_fail (G_IS_SIMPLE_ACTION (simple));
587 if (simple->state_hint != NULL)
589 g_variant_unref (simple->state_hint);
590 simple->state_hint = NULL;
593 if (state_hint != NULL)
594 simple->state_hint = g_variant_ref (state_hint);
598 * g_simple_action_new:
599 * @name: the name of the action
600 * @parameter_type: (nullable): the type of parameter that will be passed to
601 * handlers for the #GSimpleAction::activate signal, or %NULL for no parameter
603 * Creates a new action.
605 * The created action is stateless. See g_simple_action_new_stateful() to create
606 * an action that has state.
608 * Returns: a new #GSimpleAction
613 g_simple_action_new (const gchar *name,
614 const GVariantType *parameter_type)
616 g_return_val_if_fail (name != NULL, NULL);
618 return g_object_new (G_TYPE_SIMPLE_ACTION,
620 "parameter-type", parameter_type,
625 * g_simple_action_new_stateful:
626 * @name: the name of the action
627 * @parameter_type: (nullable): the type of the parameter that will be passed to
628 * handlers for the #GSimpleAction::activate signal, or %NULL for no parameter
629 * @state: the initial state of the action
631 * Creates a new stateful action.
633 * All future state values must have the same #GVariantType as the initial
636 * If the @state #GVariant is floating, it is consumed.
638 * Returns: a new #GSimpleAction
643 g_simple_action_new_stateful (const gchar *name,
644 const GVariantType *parameter_type,
647 return g_object_new (G_TYPE_SIMPLE_ACTION,
649 "parameter-type", parameter_type,