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 GSimpleAction
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;
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);
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.
150 g_simple_action_set_state (GSimpleAction *simple,
153 g_return_if_fail (G_IS_SIMPLE_ACTION (simple));
154 g_return_if_fail (value != NULL);
157 const GVariantType *state_type;
159 state_type = simple->state ?
160 g_variant_get_type (simple->state) : NULL;
161 g_return_if_fail (state_type != NULL);
162 g_return_if_fail (g_variant_is_of_type (value, state_type));
165 g_variant_ref_sink (value);
167 if (!simple->state || !g_variant_equal (simple->state, value))
170 g_variant_unref (simple->state);
172 simple->state = g_variant_ref (value);
174 g_object_notify (G_OBJECT (simple), "state");
177 g_variant_unref (value);
181 g_simple_action_get_state (GAction *action)
183 GSimpleAction *simple = G_SIMPLE_ACTION (action);
185 return simple->state ? g_variant_ref (simple->state) : NULL;
189 g_simple_action_activate (GAction *action,
192 GSimpleAction *simple = G_SIMPLE_ACTION (action);
194 g_return_if_fail (simple->parameter_type == NULL ?
196 (parameter != NULL &&
197 g_variant_is_of_type (parameter,
198 simple->parameter_type)));
200 if (parameter != NULL)
201 g_variant_ref_sink (parameter);
204 g_signal_emit (simple, g_simple_action_signals[SIGNAL_ACTIVATE], 0, parameter);
206 if (parameter != NULL)
207 g_variant_unref (parameter);
211 g_simple_action_set_property (GObject *object,
216 GSimpleAction *simple = G_SIMPLE_ACTION (object);
221 g_assert (simple->name == NULL);
222 simple->name = g_value_dup_string (value);
225 case PROP_PARAMETER_TYPE:
226 g_assert (simple->parameter_type == NULL);
227 simple->parameter_type = g_value_dup_boxed (value);
231 g_simple_action_set_enabled (simple, g_value_get_boolean (value));
235 g_assert_not_reached ();
240 g_simple_action_get_property (GObject *object,
245 GAction *action = G_ACTION (object);
250 g_value_set_string (value, g_simple_action_get_name (action));
253 case PROP_PARAMETER_TYPE:
254 g_value_set_boxed (value, g_simple_action_get_parameter_type (action));
258 g_value_set_boolean (value, g_simple_action_get_enabled (action));
261 case PROP_STATE_TYPE:
262 g_value_set_boxed (value, g_simple_action_get_state_type (action));
266 g_value_take_variant (value, g_simple_action_get_state (action));
270 g_assert_not_reached ();
275 g_simple_action_finalize (GObject *object)
277 GSimpleAction *simple = G_SIMPLE_ACTION (object);
279 g_free (simple->name);
280 if (simple->parameter_type)
281 g_variant_type_free (simple->parameter_type);
283 g_variant_unref (simple->state);
285 G_OBJECT_CLASS (g_simple_action_parent_class)
290 g_simple_action_init (GSimpleAction *simple)
295 g_simple_action_iface_init (GActionInterface *iface)
297 iface->get_name = g_simple_action_get_name;
298 iface->get_parameter_type = g_simple_action_get_parameter_type;
299 iface->get_state_type = g_simple_action_get_state_type;
300 iface->get_state_hint = g_simple_action_get_state_hint;
301 iface->get_enabled = g_simple_action_get_enabled;
302 iface->get_state = g_simple_action_get_state;
303 iface->change_state = g_simple_action_change_state;
304 iface->activate = g_simple_action_activate;
308 g_simple_action_class_init (GSimpleActionClass *class)
310 GObjectClass *object_class = G_OBJECT_CLASS (class);
312 object_class->get_property = g_simple_action_get_property;
313 object_class->set_property = g_simple_action_set_property;
314 object_class->finalize = g_simple_action_finalize;
317 * GSimpleAction::activate:
318 * @simple: the #GSimpleAction
319 * @parameter: (allow-none): the parameter to the activation
321 * Indicates that the action was just activated.
323 * @parameter will always be of the expected type. In the event that
324 * an incorrect type was given, no signal will be emitted.
328 g_simple_action_signals[SIGNAL_ACTIVATE] =
329 g_signal_new (I_("activate"),
330 G_TYPE_SIMPLE_ACTION,
331 G_SIGNAL_RUN_LAST | G_SIGNAL_MUST_COLLECT,
333 g_cclosure_marshal_VOID__VARIANT,
338 * GSimpleAction::change-state:
339 * @simple: the #GSimpleAction
340 * @value: (allow-none): the requested value for the state
342 * Indicates that the action just received a request to change its
345 * @value will always be of the correct state type. In the event that
346 * an incorrect type was given, no signal will be emitted.
348 * If no handler is connected to this signal then the default
349 * behaviour is to call g_simple_action_set_state() to set the state
350 * to the requested value. If you connect a signal handler then no
351 * default action is taken. If the state should change then you must
352 * call g_simple_action_set_state() from the handler.
355 * <title>Example 'change-state' handler</title>
358 * change_volume_state (GSimpleAction *action,
360 * gpointer user_data)
364 * requested = g_variant_get_int32 (value);
366 * // Volume only goes from 0 to 10
367 * if (0 <= requested && requested <= 10)
368 * g_simple_action_set_state (action, value);
373 * The handler need not set the state to the requested value. It
374 * could set it to any value at all, or take some other action.
378 g_simple_action_signals[SIGNAL_CHANGE_STATE] =
379 g_signal_new (I_("change-state"),
380 G_TYPE_SIMPLE_ACTION,
381 G_SIGNAL_RUN_LAST | G_SIGNAL_MUST_COLLECT,
383 g_cclosure_marshal_VOID__VARIANT,
388 * GSimpleAction:name:
390 * The name of the action. This is mostly meaningful for identifying
391 * the action once it has been added to a #GSimpleActionGroup.
395 g_object_class_install_property (object_class, PROP_NAME,
396 g_param_spec_string ("name",
398 P_("The name used to invoke the action"),
401 G_PARAM_CONSTRUCT_ONLY |
402 G_PARAM_STATIC_STRINGS));
405 * GSimpleAction:parameter-type:
407 * The type of the parameter that must be given when activating the
412 g_object_class_install_property (object_class, PROP_PARAMETER_TYPE,
413 g_param_spec_boxed ("parameter-type",
414 P_("Parameter Type"),
415 P_("The type of GVariant passed to activate()"),
418 G_PARAM_CONSTRUCT_ONLY |
419 G_PARAM_STATIC_STRINGS));
422 * GSimpleAction:enabled:
424 * If @action is currently enabled.
426 * If the action is disabled then calls to g_simple_action_activate() and
427 * g_simple_action_change_state() have no effect.
431 g_object_class_install_property (object_class, PROP_ENABLED,
432 g_param_spec_boolean ("enabled",
434 P_("If the action can be activated"),
438 G_PARAM_STATIC_STRINGS));
441 * GSimpleAction:state-type:
443 * The #GVariantType of the state that the action has, or %NULL if the
444 * action is stateless.
448 g_object_class_install_property (object_class, PROP_STATE_TYPE,
449 g_param_spec_boxed ("state-type",
451 P_("The type of the state kept by the action"),
454 G_PARAM_STATIC_STRINGS));
457 * GSimpleAction:state:
459 * The state of the action, or %NULL if the action is stateless.
463 g_object_class_install_property (object_class, PROP_STATE,
464 g_param_spec_variant ("state",
466 P_("The state the action is in"),
470 G_PARAM_STATIC_STRINGS));
474 * g_simple_action_set_enabled:
475 * @simple: a #GSimpleAction
476 * @enabled: whether the action is enabled
478 * Sets the action as enabled or not.
480 * An action must be enabled in order to be activated or in order to
481 * have its state changed from outside callers.
483 * This should only be called by the implementor of the action. Users
484 * of the action should not attempt to modify its enabled flag.
489 g_simple_action_set_enabled (GSimpleAction *simple,
492 g_return_if_fail (G_IS_SIMPLE_ACTION (simple));
496 if (simple->enabled != enabled)
498 simple->enabled = enabled;
499 g_object_notify (G_OBJECT (simple), "enabled");
503 * g_simple_action_new:
504 * @name: the name of the action
505 * @parameter_type: (allow-none): the type of parameter to the activate function
507 * Creates a new action.
509 * The created action is stateless. See g_simple_action_new_stateful().
511 * Returns: a new #GSimpleAction
516 g_simple_action_new (const gchar *name,
517 const GVariantType *parameter_type)
519 return g_object_new (G_TYPE_SIMPLE_ACTION,
521 "parameter-type", parameter_type,
526 * g_simple_action_new_stateful:
527 * @name: the name of the action
528 * @parameter_type: (allow-none): the type of the parameter to the activate function
529 * @state: the initial state of the action
531 * Creates a new stateful action.
533 * @state is the initial state of the action. All future state values
534 * must have the same #GVariantType as the initial state.
536 * If the @state GVariant is floating, it is consumed.
538 * Returns: a new #GSimpleAction
543 g_simple_action_new_stateful (const gchar *name,
544 const GVariantType *parameter_type,
547 GSimpleAction *simple;
549 simple = g_object_new (G_TYPE_SIMPLE_ACTION,
551 "parameter-type", parameter_type,
555 simple->state = g_variant_ref_sink (state);