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 #include "gsimpleaction.h"
30 * SECTION:gsimpleaction
31 * @title: GSimpleAction
32 * @short_description: A simple GAction implementation
34 * A #GSimpleAction is the obvious simple implementation of the #GAction
35 * interface. This is the easiest way to create an action for purposes of
36 * adding it to a #GSimpleActionGroup.
38 * See also #GtkAction.
42 GObject parent_instance;
45 GVariantType *parameter_type;
48 gboolean state_set_already;
51 typedef GObjectClass GSimpleActionClass;
53 static void g_simple_action_iface_init (GActionInterface *iface);
54 G_DEFINE_TYPE_WITH_CODE (GSimpleAction, g_simple_action, G_TYPE_OBJECT,
55 G_IMPLEMENT_INTERFACE (G_TYPE_ACTION, g_simple_action_iface_init))
74 static guint g_simple_action_signals[NR_SIGNALS];
77 g_simple_action_get_name (GAction *action)
79 GSimpleAction *simple = G_SIMPLE_ACTION (action);
84 static const GVariantType *
85 g_simple_action_get_parameter_type (GAction *action)
87 GSimpleAction *simple = G_SIMPLE_ACTION (action);
89 return simple->parameter_type;
92 static const GVariantType *
93 g_simple_action_get_state_type (GAction *action)
95 GSimpleAction *simple = G_SIMPLE_ACTION (action);
97 if (simple->state != NULL)
98 return g_variant_get_type (simple->state);
104 g_simple_action_get_state_hint (GAction *action)
110 g_simple_action_get_enabled (GAction *action)
112 GSimpleAction *simple = G_SIMPLE_ACTION (action);
114 return simple->enabled;
118 g_simple_action_change_state (GAction *action,
121 GSimpleAction *simple = G_SIMPLE_ACTION (action);
123 /* If the user connected a signal handler then they are responsible
124 * for handling state changes.
126 if (g_signal_has_handler_pending (action, g_simple_action_signals[SIGNAL_CHANGE_STATE], 0, TRUE))
127 g_signal_emit (action, g_simple_action_signals[SIGNAL_CHANGE_STATE], 0, value);
129 /* If not, then the default behaviour is to just set the state. */
131 g_simple_action_set_state (simple, value);
135 * g_simple_action_set_state:
136 * @simple: a #GSimpleAction
137 * @value: the new #GVariant for the state
139 * Sets the state of the action.
141 * This directly updates the 'state' property to the given value.
143 * This should only be called by the implementor of the action. Users
144 * of the action should not attempt to directly modify the 'state'
145 * property. Instead, they should call g_action_change_state() to
146 * request the change.
148 * If the @value GVariant is floating, it is consumed.
153 g_simple_action_set_state (GSimpleAction *simple,
156 g_return_if_fail (G_IS_SIMPLE_ACTION (simple));
157 g_return_if_fail (value != NULL);
160 const GVariantType *state_type;
162 state_type = simple->state ?
163 g_variant_get_type (simple->state) : NULL;
164 g_return_if_fail (state_type != NULL);
165 g_return_if_fail (g_variant_is_of_type (value, state_type));
168 g_variant_ref_sink (value);
170 if (!simple->state || !g_variant_equal (simple->state, value))
173 g_variant_unref (simple->state);
175 simple->state = g_variant_ref (value);
177 g_object_notify (G_OBJECT (simple), "state");
180 g_variant_unref (value);
184 g_simple_action_get_state (GAction *action)
186 GSimpleAction *simple = G_SIMPLE_ACTION (action);
188 return simple->state ? g_variant_ref (simple->state) : NULL;
192 g_simple_action_activate (GAction *action,
195 GSimpleAction *simple = G_SIMPLE_ACTION (action);
197 g_return_if_fail (simple->parameter_type == NULL ?
199 (parameter != NULL &&
200 g_variant_is_of_type (parameter,
201 simple->parameter_type)));
203 if (parameter != NULL)
204 g_variant_ref_sink (parameter);
207 g_signal_emit (simple, g_simple_action_signals[SIGNAL_ACTIVATE], 0, parameter);
209 if (parameter != NULL)
210 g_variant_unref (parameter);
214 g_simple_action_set_property (GObject *object,
219 GSimpleAction *action = G_SIMPLE_ACTION (object);
224 action->name = g_strdup (g_value_get_string (value));
227 case PROP_PARAMETER_TYPE:
228 action->parameter_type = g_value_dup_boxed (value);
232 action->enabled = g_value_get_boolean (value);
236 /* The first time we see this (during construct) we should just
237 * take the state as it was handed to us.
239 * After that, we should make sure we go through the same checks
242 if (!action->state_set_already)
244 action->state = g_value_dup_variant (value);
245 action->state_set_already = TRUE;
248 g_simple_action_set_state (action, g_value_get_variant (value));
253 g_assert_not_reached ();
258 g_simple_action_get_property (GObject *object,
263 GAction *action = G_ACTION (object);
268 g_value_set_string (value, g_simple_action_get_name (action));
271 case PROP_PARAMETER_TYPE:
272 g_value_set_boxed (value, g_simple_action_get_parameter_type (action));
276 g_value_set_boolean (value, g_simple_action_get_enabled (action));
279 case PROP_STATE_TYPE:
280 g_value_set_boxed (value, g_simple_action_get_state_type (action));
284 g_value_take_variant (value, g_simple_action_get_state (action));
288 g_assert_not_reached ();
293 g_simple_action_finalize (GObject *object)
295 GSimpleAction *simple = G_SIMPLE_ACTION (object);
297 g_free (simple->name);
298 if (simple->parameter_type)
299 g_variant_type_free (simple->parameter_type);
301 g_variant_unref (simple->state);
303 G_OBJECT_CLASS (g_simple_action_parent_class)
308 g_simple_action_init (GSimpleAction *simple)
310 simple->enabled = TRUE;
314 g_simple_action_iface_init (GActionInterface *iface)
316 iface->get_name = g_simple_action_get_name;
317 iface->get_parameter_type = g_simple_action_get_parameter_type;
318 iface->get_state_type = g_simple_action_get_state_type;
319 iface->get_state_hint = g_simple_action_get_state_hint;
320 iface->get_enabled = g_simple_action_get_enabled;
321 iface->get_state = g_simple_action_get_state;
322 iface->change_state = g_simple_action_change_state;
323 iface->activate = g_simple_action_activate;
327 g_simple_action_class_init (GSimpleActionClass *class)
329 GObjectClass *object_class = G_OBJECT_CLASS (class);
331 object_class->set_property = g_simple_action_set_property;
332 object_class->get_property = g_simple_action_get_property;
333 object_class->finalize = g_simple_action_finalize;
336 * GSimpleAction::activate:
337 * @simple: the #GSimpleAction
338 * @parameter: (allow-none): the parameter to the activation
340 * Indicates that the action was just activated.
342 * @parameter will always be of the expected type. In the event that
343 * an incorrect type was given, no signal will be emitted.
347 g_simple_action_signals[SIGNAL_ACTIVATE] =
348 g_signal_new (I_("activate"),
349 G_TYPE_SIMPLE_ACTION,
350 G_SIGNAL_RUN_LAST | G_SIGNAL_MUST_COLLECT,
352 g_cclosure_marshal_VOID__VARIANT,
357 * GSimpleAction::change-state:
358 * @simple: the #GSimpleAction
359 * @value: (allow-none): the requested value for the state
361 * Indicates that the action just received a request to change its
364 * @value will always be of the correct state type. In the event that
365 * an incorrect type was given, no signal will be emitted.
367 * If no handler is connected to this signal then the default
368 * behaviour is to call g_simple_action_set_state() to set the state
369 * to the requested value. If you connect a signal handler then no
370 * default action is taken. If the state should change then you must
371 * call g_simple_action_set_state() from the handler.
374 * <title>Example 'change-state' handler</title>
377 * change_volume_state (GSimpleAction *action,
379 * gpointer user_data)
383 * requested = g_variant_get_int32 (value);
385 * // Volume only goes from 0 to 10
386 * if (0 <= requested && requested <= 10)
387 * g_simple_action_set_state (action, value);
392 * The handler need not set the state to the requested value. It
393 * could set it to any value at all, or take some other action.
397 g_simple_action_signals[SIGNAL_CHANGE_STATE] =
398 g_signal_new (I_("change-state"),
399 G_TYPE_SIMPLE_ACTION,
400 G_SIGNAL_RUN_LAST | G_SIGNAL_MUST_COLLECT,
402 g_cclosure_marshal_VOID__VARIANT,
407 * GSimpleAction:name:
409 * The name of the action. This is mostly meaningful for identifying
410 * the action once it has been added to a #GSimpleActionGroup.
414 g_object_class_install_property (object_class, PROP_NAME,
415 g_param_spec_string ("name",
417 P_("The name used to invoke the action"),
420 G_PARAM_CONSTRUCT_ONLY |
421 G_PARAM_STATIC_STRINGS));
424 * GSimpleAction:parameter-type:
426 * The type of the parameter that must be given when activating the
431 g_object_class_install_property (object_class, PROP_PARAMETER_TYPE,
432 g_param_spec_boxed ("parameter-type",
433 P_("Parameter Type"),
434 P_("The type of GVariant passed to activate()"),
437 G_PARAM_CONSTRUCT_ONLY |
438 G_PARAM_STATIC_STRINGS));
441 * GSimpleAction:enabled:
443 * If @action is currently enabled.
445 * If the action is disabled then calls to g_action_activate() and
446 * g_action_change_state() have no effect.
450 g_object_class_install_property (object_class, PROP_ENABLED,
451 g_param_spec_boolean ("enabled",
453 P_("If the action can be activated"),
456 G_PARAM_STATIC_STRINGS));
459 * GSimpleAction:state-type:
461 * The #GVariantType of the state that the action has, or %NULL if the
462 * action is stateless.
466 g_object_class_install_property (object_class, PROP_STATE_TYPE,
467 g_param_spec_boxed ("state-type",
469 P_("The type of the state kept by the action"),
472 G_PARAM_STATIC_STRINGS));
475 * GSimpleAction:state:
477 * The state of the action, or %NULL if the action is stateless.
481 g_object_class_install_property (object_class, PROP_STATE,
482 g_param_spec_variant ("state",
484 P_("The state the action is in"),
487 G_PARAM_READWRITE | G_PARAM_CONSTRUCT |
488 G_PARAM_STATIC_STRINGS));
492 * g_simple_action_set_enabled:
493 * @simple: a #GSimpleAction
494 * @enabled: whether the action is enabled
496 * Sets the action as enabled or not.
498 * An action must be enabled in order to be activated or in order to
499 * have its state changed from outside callers.
501 * This should only be called by the implementor of the action. Users
502 * of the action should not attempt to modify its enabled flag.
507 g_simple_action_set_enabled (GSimpleAction *simple,
510 g_return_if_fail (G_IS_SIMPLE_ACTION (simple));
514 if (simple->enabled != enabled)
516 simple->enabled = enabled;
517 g_object_notify (G_OBJECT (simple), "enabled");
521 * g_simple_action_new:
522 * @name: the name of the action
523 * @parameter_type: (allow-none): the type of parameter to the activate function
525 * Creates a new action.
527 * The created action is stateless. See g_simple_action_new_stateful().
529 * Returns: a new #GSimpleAction
534 g_simple_action_new (const gchar *name,
535 const GVariantType *parameter_type)
537 return g_object_new (G_TYPE_SIMPLE_ACTION,
539 "parameter-type", parameter_type,
544 * g_simple_action_new_stateful:
545 * @name: the name of the action
546 * @parameter_type: (allow-none): the type of the parameter to the activate function
547 * @state: the initial state of the action
549 * Creates a new stateful action.
551 * @state is the initial state of the action. All future state values
552 * must have the same #GVariantType as the initial state.
554 * If the @state GVariant is floating, it is consumed.
556 * Returns: a new #GSimpleAction
561 g_simple_action_new_stateful (const gchar *name,
562 const GVariantType *parameter_type,
565 return g_object_new (G_TYPE_SIMPLE_ACTION,
567 "parameter-type", parameter_type,