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
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.
43 GObject parent_instance;
46 GVariantType *parameter_type;
49 gboolean state_set_already;
52 typedef GObjectClass GSimpleActionClass;
54 static void g_simple_action_iface_init (GActionInterface *iface);
55 G_DEFINE_TYPE_WITH_CODE (GSimpleAction, g_simple_action, G_TYPE_OBJECT,
56 G_IMPLEMENT_INTERFACE (G_TYPE_ACTION, g_simple_action_iface_init))
75 static guint g_simple_action_signals[NR_SIGNALS];
78 g_simple_action_get_name (GAction *action)
80 GSimpleAction *simple = G_SIMPLE_ACTION (action);
85 static const GVariantType *
86 g_simple_action_get_parameter_type (GAction *action)
88 GSimpleAction *simple = G_SIMPLE_ACTION (action);
90 return simple->parameter_type;
93 static const GVariantType *
94 g_simple_action_get_state_type (GAction *action)
96 GSimpleAction *simple = G_SIMPLE_ACTION (action);
98 if (simple->state != NULL)
99 return g_variant_get_type (simple->state);
105 g_simple_action_get_state_hint (GAction *action)
111 g_simple_action_get_enabled (GAction *action)
113 GSimpleAction *simple = G_SIMPLE_ACTION (action);
115 return simple->enabled;
119 g_simple_action_change_state (GAction *action,
122 GSimpleAction *simple = G_SIMPLE_ACTION (action);
124 /* If the user connected a signal handler then they are responsible
125 * for handling state changes.
127 if (g_signal_has_handler_pending (action, g_simple_action_signals[SIGNAL_CHANGE_STATE], 0, TRUE))
128 g_signal_emit (action, g_simple_action_signals[SIGNAL_CHANGE_STATE], 0, value);
130 /* If not, then the default behaviour is to just set the state. */
132 g_simple_action_set_state (simple, value);
136 * g_simple_action_set_state:
137 * @simple: a #GSimpleAction
138 * @value: the new #GVariant for the state
140 * Sets the state of the action.
142 * This directly updates the 'state' property to the given value.
144 * This should only be called by the implementor of the action. Users
145 * of the action should not attempt to directly modify the 'state'
146 * property. Instead, they should call g_action_change_state() to
147 * request the change.
149 * If the @value GVariant is floating, it is consumed.
154 g_simple_action_set_state (GSimpleAction *simple,
157 g_return_if_fail (G_IS_SIMPLE_ACTION (simple));
158 g_return_if_fail (value != NULL);
161 const GVariantType *state_type;
163 state_type = simple->state ?
164 g_variant_get_type (simple->state) : NULL;
165 g_return_if_fail (state_type != NULL);
166 g_return_if_fail (g_variant_is_of_type (value, state_type));
169 g_variant_ref_sink (value);
171 if (!simple->state || !g_variant_equal (simple->state, value))
174 g_variant_unref (simple->state);
176 simple->state = g_variant_ref (value);
178 g_object_notify (G_OBJECT (simple), "state");
181 g_variant_unref (value);
185 g_simple_action_get_state (GAction *action)
187 GSimpleAction *simple = G_SIMPLE_ACTION (action);
189 return simple->state ? g_variant_ref (simple->state) : NULL;
193 g_simple_action_activate (GAction *action,
196 GSimpleAction *simple = G_SIMPLE_ACTION (action);
198 g_return_if_fail (simple->parameter_type == NULL ?
200 (parameter != NULL &&
201 g_variant_is_of_type (parameter,
202 simple->parameter_type)));
204 if (parameter != NULL)
205 g_variant_ref_sink (parameter);
208 g_signal_emit (simple, g_simple_action_signals[SIGNAL_ACTIVATE], 0, parameter);
210 if (parameter != NULL)
211 g_variant_unref (parameter);
215 g_simple_action_set_property (GObject *object,
220 GSimpleAction *action = G_SIMPLE_ACTION (object);
225 action->name = g_strdup (g_value_get_string (value));
228 case PROP_PARAMETER_TYPE:
229 action->parameter_type = g_value_dup_boxed (value);
233 action->enabled = g_value_get_boolean (value);
237 /* The first time we see this (during construct) we should just
238 * take the state as it was handed to us.
240 * After that, we should make sure we go through the same checks
243 if (!action->state_set_already)
245 action->state = g_value_dup_variant (value);
246 action->state_set_already = TRUE;
249 g_simple_action_set_state (action, g_value_get_variant (value));
254 g_assert_not_reached ();
259 g_simple_action_get_property (GObject *object,
264 GAction *action = G_ACTION (object);
269 g_value_set_string (value, g_simple_action_get_name (action));
272 case PROP_PARAMETER_TYPE:
273 g_value_set_boxed (value, g_simple_action_get_parameter_type (action));
277 g_value_set_boolean (value, g_simple_action_get_enabled (action));
280 case PROP_STATE_TYPE:
281 g_value_set_boxed (value, g_simple_action_get_state_type (action));
285 g_value_take_variant (value, g_simple_action_get_state (action));
289 g_assert_not_reached ();
294 g_simple_action_finalize (GObject *object)
296 GSimpleAction *simple = G_SIMPLE_ACTION (object);
298 g_free (simple->name);
299 if (simple->parameter_type)
300 g_variant_type_free (simple->parameter_type);
302 g_variant_unref (simple->state);
304 G_OBJECT_CLASS (g_simple_action_parent_class)
309 g_simple_action_init (GSimpleAction *simple)
311 simple->enabled = TRUE;
315 g_simple_action_iface_init (GActionInterface *iface)
317 iface->get_name = g_simple_action_get_name;
318 iface->get_parameter_type = g_simple_action_get_parameter_type;
319 iface->get_state_type = g_simple_action_get_state_type;
320 iface->get_state_hint = g_simple_action_get_state_hint;
321 iface->get_enabled = g_simple_action_get_enabled;
322 iface->get_state = g_simple_action_get_state;
323 iface->change_state = g_simple_action_change_state;
324 iface->activate = g_simple_action_activate;
328 g_simple_action_class_init (GSimpleActionClass *class)
330 GObjectClass *object_class = G_OBJECT_CLASS (class);
332 object_class->set_property = g_simple_action_set_property;
333 object_class->get_property = g_simple_action_get_property;
334 object_class->finalize = g_simple_action_finalize;
337 * GSimpleAction::activate:
338 * @simple: the #GSimpleAction
339 * @parameter: (allow-none): the parameter to the activation
341 * Indicates that the action was just activated.
343 * @parameter will always be of the expected type. In the event that
344 * an incorrect type was given, no signal will be emitted.
348 g_simple_action_signals[SIGNAL_ACTIVATE] =
349 g_signal_new (I_("activate"),
350 G_TYPE_SIMPLE_ACTION,
351 G_SIGNAL_RUN_LAST | G_SIGNAL_MUST_COLLECT,
353 g_cclosure_marshal_VOID__VARIANT,
358 * GSimpleAction::change-state:
359 * @simple: the #GSimpleAction
360 * @value: (allow-none): the requested value for the state
362 * Indicates that the action just received a request to change its
365 * @value will always be of the correct state type. In the event that
366 * an incorrect type was given, no signal will be emitted.
368 * If no handler is connected to this signal then the default
369 * behaviour is to call g_simple_action_set_state() to set the state
370 * to the requested value. If you connect a signal handler then no
371 * default action is taken. If the state should change then you must
372 * call g_simple_action_set_state() from the handler.
375 * <title>Example 'change-state' handler</title>
378 * change_volume_state (GSimpleAction *action,
380 * gpointer user_data)
384 * requested = g_variant_get_int32 (value);
386 * // Volume only goes from 0 to 10
387 * if (0 <= requested && requested <= 10)
388 * g_simple_action_set_state (action, value);
393 * The handler need not set the state to the requested value. It
394 * could set it to any value at all, or take some other action.
398 g_simple_action_signals[SIGNAL_CHANGE_STATE] =
399 g_signal_new (I_("change-state"),
400 G_TYPE_SIMPLE_ACTION,
401 G_SIGNAL_RUN_LAST | G_SIGNAL_MUST_COLLECT,
403 g_cclosure_marshal_VOID__VARIANT,
408 * GSimpleAction:name:
410 * The name of the action. This is mostly meaningful for identifying
411 * the action once it has been added to a #GSimpleActionGroup.
415 g_object_class_install_property (object_class, PROP_NAME,
416 g_param_spec_string ("name",
418 P_("The name used to invoke the action"),
421 G_PARAM_CONSTRUCT_ONLY |
422 G_PARAM_STATIC_STRINGS));
425 * GSimpleAction:parameter-type:
427 * The type of the parameter that must be given when activating the
432 g_object_class_install_property (object_class, PROP_PARAMETER_TYPE,
433 g_param_spec_boxed ("parameter-type",
434 P_("Parameter Type"),
435 P_("The type of GVariant passed to activate()"),
438 G_PARAM_CONSTRUCT_ONLY |
439 G_PARAM_STATIC_STRINGS));
442 * GSimpleAction:enabled:
444 * If @action is currently enabled.
446 * If the action is disabled then calls to g_action_activate() and
447 * g_action_change_state() have no effect.
451 g_object_class_install_property (object_class, PROP_ENABLED,
452 g_param_spec_boolean ("enabled",
454 P_("If the action can be activated"),
457 G_PARAM_STATIC_STRINGS));
460 * GSimpleAction:state-type:
462 * The #GVariantType of the state that the action has, or %NULL if the
463 * action is stateless.
467 g_object_class_install_property (object_class, PROP_STATE_TYPE,
468 g_param_spec_boxed ("state-type",
470 P_("The type of the state kept by the action"),
473 G_PARAM_STATIC_STRINGS));
476 * GSimpleAction:state:
478 * The state of the action, or %NULL if the action is stateless.
482 g_object_class_install_property (object_class, PROP_STATE,
483 g_param_spec_variant ("state",
485 P_("The state the action is in"),
488 G_PARAM_READWRITE | G_PARAM_CONSTRUCT |
489 G_PARAM_STATIC_STRINGS));
493 * g_simple_action_set_enabled:
494 * @simple: a #GSimpleAction
495 * @enabled: whether the action is enabled
497 * Sets the action as enabled or not.
499 * An action must be enabled in order to be activated or in order to
500 * have its state changed from outside callers.
502 * This should only be called by the implementor of the action. Users
503 * of the action should not attempt to modify its enabled flag.
508 g_simple_action_set_enabled (GSimpleAction *simple,
511 g_return_if_fail (G_IS_SIMPLE_ACTION (simple));
515 if (simple->enabled != enabled)
517 simple->enabled = enabled;
518 g_object_notify (G_OBJECT (simple), "enabled");
522 * g_simple_action_new:
523 * @name: the name of the action
524 * @parameter_type: (allow-none): the type of parameter to the activate function
526 * Creates a new action.
528 * The created action is stateless. See g_simple_action_new_stateful().
530 * Returns: a new #GSimpleAction
535 g_simple_action_new (const gchar *name,
536 const GVariantType *parameter_type)
538 return g_object_new (G_TYPE_SIMPLE_ACTION,
540 "parameter-type", parameter_type,
545 * g_simple_action_new_stateful:
546 * @name: the name of the action
547 * @parameter_type: (allow-none): the type of the parameter to the activate function
548 * @state: the initial state of the action
550 * Creates a new stateful action.
552 * @state is the initial state of the action. All future state values
553 * must have the same #GVariantType as the initial state.
555 * If the @state GVariant is floating, it is consumed.
557 * Returns: a new #GSimpleAction
562 g_simple_action_new_stateful (const gchar *name,
563 const GVariantType *parameter_type,
566 return g_object_new (G_TYPE_SIMPLE_ACTION,
568 "parameter-type", parameter_type,