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"
29 static void g_simple_action_iface_init (GActionInterface *iface);
30 G_DEFINE_TYPE_WITH_CODE (GSimpleAction, g_simple_action, G_TYPE_OBJECT,
31 G_IMPLEMENT_INTERFACE (G_TYPE_ACTION, g_simple_action_iface_init))
34 * SECTION:gsimpleaction
35 * @title: GSimpleAction
36 * @short_description: a simple #GSimpleAction
38 * A #GSimpleAction is the obvious simple implementation of the #GSimpleAction
39 * interface. This is the easiest way to create an action for purposes of
40 * adding it to a #GSimpleActionGroup.
42 * See also #GtkAction.
45 struct _GSimpleActionPrivate
48 GVariantType *parameter_type;
70 static guint g_simple_action_signals[NR_SIGNALS];
73 g_simple_action_get_name (GAction *action)
75 GSimpleAction *simple = G_SIMPLE_ACTION (action);
77 return simple->priv->name;
81 g_simple_action_get_parameter_type (GAction *action)
83 GSimpleAction *simple = G_SIMPLE_ACTION (action);
85 return simple->priv->parameter_type;
88 static const GVariantType *
89 g_simple_action_get_state_type (GAction *action)
91 GSimpleAction *simple = G_SIMPLE_ACTION (action);
93 if (simple->priv->state != NULL)
94 return g_variant_get_type (simple->priv->state);
100 g_simple_action_get_state_hint (GAction *action)
106 g_simple_action_get_enabled (GAction *action)
108 GSimpleAction *simple = G_SIMPLE_ACTION (action);
110 return simple->priv->enabled;
114 g_simple_action_set_state (GAction *action,
117 GSimpleAction *simple = G_SIMPLE_ACTION (action);
119 g_return_if_fail (value != NULL);
122 const GVariantType *state_type;
124 state_type = simple->priv->state ?
125 g_variant_get_type (simple->priv->state) : NULL;
126 g_return_if_fail (state_type != NULL);
127 g_return_if_fail (g_variant_is_of_type (value, state_type));
130 g_variant_ref_sink (value);
132 if (!g_variant_equal (simple->priv->state, value))
134 if (simple->priv->state)
135 g_variant_unref (simple->priv->state);
137 simple->priv->state = g_variant_ref (value);
139 g_object_notify (G_OBJECT (simple), "state");
142 g_variant_unref (value);
146 g_simple_action_get_state (GAction *action)
148 GSimpleAction *simple = G_SIMPLE_ACTION (action);
150 return simple->priv->state ? g_variant_ref (simple->priv->state) : NULL;
154 g_simple_action_activate (GAction *action,
157 GSimpleAction *simple = G_SIMPLE_ACTION (action);
159 g_return_if_fail (simple->priv->parameter_type == NULL ?
161 (parameter != NULL &&
162 g_variant_is_of_type (parameter,
163 simple->priv->parameter_type)));
165 if (parameter != NULL)
166 g_variant_ref_sink (parameter);
168 if (simple->priv->enabled)
169 g_signal_emit (simple, g_simple_action_signals[SIGNAL_ACTIVATE], 0, parameter);
171 if (parameter != NULL)
172 g_variant_unref (parameter);
176 g_simple_action_set_property (GObject *object,
181 GSimpleAction *simple = G_SIMPLE_ACTION (object);
186 g_assert (simple->priv->name == NULL);
187 simple->priv->name = g_value_dup_string (value);
190 case PROP_PARAMETER_TYPE:
191 g_assert (simple->priv->parameter_type == NULL);
192 simple->priv->parameter_type = g_value_dup_boxed (value);
196 g_simple_action_set_enabled (simple, g_value_get_boolean (value));
200 /* PROP_STATE is marked as G_PARAM_CONSTRUCT so we always get a
201 * call during object construction, even if it is NULL. We treat
202 * that first call differently, for a number of reasons.
204 * First, we don't want the value to be rejected by the
205 * possibly-overridden .set_state() function. Second, we don't
206 * want to be tripped by the assertions in g_simple_action_set_state()
207 * that would enforce the catch22 that we only provide a value of
208 * the same type as the existing value (when there is not yet an
211 if (simple->priv->state_set)
212 g_simple_action_set_state (G_ACTION (simple),
213 g_value_get_variant (value));
215 else /* this is the special case */
217 /* only do it the first time. */
218 simple->priv->state_set = TRUE;
220 /* blindly set it. */
221 simple->priv->state = g_value_dup_variant (value);
226 g_assert_not_reached ();
231 g_simple_action_get_property (GObject *object,
236 GAction *action = G_ACTION (object);
241 g_value_set_string (value, g_simple_action_get_name (action));
244 case PROP_PARAMETER_TYPE:
245 g_value_set_boxed (value, g_simple_action_get_parameter_type (action));
249 g_value_set_boolean (value, g_simple_action_get_enabled (action));
252 case PROP_STATE_TYPE:
253 g_value_set_boxed (value, g_simple_action_get_state_type (action));
257 g_value_take_variant (value, g_simple_action_get_state (action));
261 g_assert_not_reached ();
266 g_simple_action_finalize (GObject *object)
268 GSimpleAction *simple = G_SIMPLE_ACTION (object);
270 g_free (simple->priv->name);
271 if (simple->priv->parameter_type)
272 g_variant_type_free (simple->priv->parameter_type);
273 if (simple->priv->state)
274 g_variant_unref (simple->priv->state);
276 G_OBJECT_CLASS (g_simple_action_parent_class)
281 g_simple_action_init (GSimpleAction *simple)
283 simple->priv = G_TYPE_INSTANCE_GET_PRIVATE (simple,
284 G_TYPE_SIMPLE_ACTION,
285 GSimpleActionPrivate);
289 g_simple_action_iface_init (GActionInterface *iface)
291 iface->get_name = g_simple_action_get_name;
292 iface->get_parameter_type = g_simple_action_get_parameter_type;
293 iface->get_state_type = g_simple_action_get_state_type;
294 iface->get_state_hint = g_simple_action_get_state_hint;
295 iface->get_enabled = g_simple_action_get_enabled;
296 iface->get_state = g_simple_action_get_state;
297 iface->set_state = g_simple_action_set_state;
298 iface->activate = g_simple_action_activate;
302 g_simple_action_class_init (GSimpleActionClass *class)
304 GObjectClass *object_class = G_OBJECT_CLASS (class);
307 object_class->get_property = g_simple_action_get_property;
308 object_class->set_property = g_simple_action_set_property;
309 object_class->finalize = g_simple_action_finalize;
312 * GSimpleAction::activate:
313 * @simple: the #GSimpleAction
314 * @parameter: (allow-none): the parameter to the activation
316 * Indicates that the action was just activated.
318 * @parameter will always be of the expected type. In the event that
319 * an incorrect type was given, no signal will be emitted.
323 g_simple_action_signals[SIGNAL_ACTIVATE] =
324 g_signal_new (I_("activate"),
325 G_TYPE_SIMPLE_ACTION,
327 G_STRUCT_OFFSET (GSimpleActionClass, activate),
329 g_cclosure_marshal_VOID__VARIANT,
334 * GSimpleAction:name:
336 * The name of the action. This is mostly meaningful for identifying
337 * the action once it has been added to a #GSimpleActionGroup.
341 g_object_class_install_property (object_class, PROP_NAME,
342 g_param_spec_string ("name",
344 P_("The name used to invoke the action"),
347 G_PARAM_CONSTRUCT_ONLY |
348 G_PARAM_STATIC_STRINGS));
351 * GSimpleAction:parameter-type:
353 * The type of the parameter that must be given when activating the
358 g_object_class_install_property (object_class, PROP_PARAMETER_TYPE,
359 g_param_spec_boxed ("parameter-type",
360 P_("Parameter Type"),
361 P_("The type of GVariant passed to activate()"),
364 G_PARAM_CONSTRUCT_ONLY |
365 G_PARAM_STATIC_STRINGS));
368 * GSimpleAction:enabled:
370 * If @action is currently enabled.
372 * If the action is disabled then calls to g_simple_action_activate() and
373 * g_simple_action_set_state() have no effect.
377 g_object_class_install_property (object_class, PROP_ENABLED,
378 g_param_spec_boolean ("enabled",
380 P_("If the action can be activated"),
384 G_PARAM_STATIC_STRINGS));
387 * GSimpleAction:state-type:
389 * The #GVariantType of the state that the action has, or %NULL if the
390 * action is stateless.
394 g_object_class_install_property (object_class, PROP_STATE_TYPE,
395 g_param_spec_boxed ("state-type",
397 P_("The type of the state kept by the action"),
400 G_PARAM_STATIC_STRINGS));
403 * GSimpleAction:state:
405 * The state of the action, or %NULL if the action is stateless.
409 g_object_class_install_property (object_class, PROP_STATE,
410 g_param_spec_variant ("state",
412 P_("The state the action is in"),
417 G_PARAM_STATIC_STRINGS));
419 g_type_class_add_private (class, sizeof (GSimpleActionPrivate));
423 * g_simple_action_set_enabled:
424 * @simple: a #GSimpleAction
425 * @enabled: whether the action is enabled
427 * Sets the action as enabled or not.
429 * An action must be enabled in order to be activated or in order to
430 * have its state changed from outside callers.
435 g_simple_action_set_enabled (GSimpleAction *simple,
438 g_return_if_fail (G_IS_SIMPLE_ACTION (simple));
442 if (simple->priv->enabled != enabled)
444 simple->priv->enabled = enabled;
445 g_object_notify (G_OBJECT (simple), "enabled");
449 * g_simple_action_new:
450 * @name: the name of the action
451 * @parameter_type: (allow-none): the type of parameter to the activate function
453 * Creates a new action.
455 * The created action is stateless. See g_simple_action_new_stateful().
457 * Returns: a new #GSimpleAction
462 g_simple_action_new (const gchar *name,
463 const GVariantType *parameter_type)
465 return g_object_new (G_TYPE_SIMPLE_ACTION,
467 "parameter-type", parameter_type,
472 * g_simple_action_new_stateful:
473 * @name: the name of the action
474 * @parameter_type: (allow-none): the type of the parameter to the activate function
475 * @state: the initial state of the action
477 * Creates a new stateful action.
479 * @state is the initial state of the action. All future state values
480 * must have the same #GVariantType as the initial state.
482 * If the @state GVariant is floating, it is consumed.
484 * Returns: a new #GSimpleAction
489 g_simple_action_new_stateful (const gchar *name,
490 const GVariantType *parameter_type,
493 return g_object_new (G_TYPE_SIMPLE_ACTION,
495 "parameter-type", parameter_type,