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))
72 static guint g_simple_action_signals[NR_SIGNALS];
75 g_simple_action_get_name (GAction *action)
77 GSimpleAction *simple = G_SIMPLE_ACTION (action);
83 g_simple_action_get_parameter_type (GAction *action)
85 GSimpleAction *simple = G_SIMPLE_ACTION (action);
87 return simple->parameter_type;
90 static const GVariantType *
91 g_simple_action_get_state_type (GAction *action)
93 GSimpleAction *simple = G_SIMPLE_ACTION (action);
95 if (simple->state != NULL)
96 return g_variant_get_type (simple->state);
102 g_simple_action_get_state_hint (GAction *action)
108 g_simple_action_get_enabled (GAction *action)
110 GSimpleAction *simple = G_SIMPLE_ACTION (action);
112 return simple->enabled;
116 g_simple_action_change_state (GAction *action,
119 GSimpleAction *simple = G_SIMPLE_ACTION (action);
121 g_simple_action_set_state (simple, value);
125 * g_simple_action_set_state:
126 * @simple: a #GSimpleAction
127 * @value: the new #GVariant for the state
129 * Sets the state of the action.
131 * This directly updates the 'state' property to the given value.
133 * This should only be called by the implementor of the action. Users
134 * of the action should not attempt to directly modify the 'state'
135 * property. Instead, they should call g_action_change_state() to
136 * request the change.
141 g_simple_action_set_state (GSimpleAction *simple,
144 g_return_if_fail (G_IS_SIMPLE_ACTION (simple));
145 g_return_if_fail (value != NULL);
148 const GVariantType *state_type;
150 state_type = simple->state ?
151 g_variant_get_type (simple->state) : NULL;
152 g_return_if_fail (state_type != NULL);
153 g_return_if_fail (g_variant_is_of_type (value, state_type));
156 g_variant_ref_sink (value);
158 if (!simple->state || !g_variant_equal (simple->state, value))
161 g_variant_unref (simple->state);
163 simple->state = g_variant_ref (value);
165 g_object_notify (G_OBJECT (simple), "state");
168 g_variant_unref (value);
172 g_simple_action_get_state (GAction *action)
174 GSimpleAction *simple = G_SIMPLE_ACTION (action);
176 return simple->state ? g_variant_ref (simple->state) : NULL;
180 g_simple_action_activate (GAction *action,
183 GSimpleAction *simple = G_SIMPLE_ACTION (action);
185 g_return_if_fail (simple->parameter_type == NULL ?
187 (parameter != NULL &&
188 g_variant_is_of_type (parameter,
189 simple->parameter_type)));
191 if (parameter != NULL)
192 g_variant_ref_sink (parameter);
195 g_signal_emit (simple, g_simple_action_signals[SIGNAL_ACTIVATE], 0, parameter);
197 if (parameter != NULL)
198 g_variant_unref (parameter);
202 g_simple_action_set_property (GObject *object,
207 GSimpleAction *simple = G_SIMPLE_ACTION (object);
212 g_assert (simple->name == NULL);
213 simple->name = g_value_dup_string (value);
216 case PROP_PARAMETER_TYPE:
217 g_assert (simple->parameter_type == NULL);
218 simple->parameter_type = g_value_dup_boxed (value);
222 g_simple_action_set_enabled (simple, g_value_get_boolean (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->name);
271 if (simple->parameter_type)
272 g_variant_type_free (simple->parameter_type);
274 g_variant_unref (simple->state);
276 G_OBJECT_CLASS (g_simple_action_parent_class)
281 g_simple_action_init (GSimpleAction *simple)
286 g_simple_action_iface_init (GActionInterface *iface)
288 iface->get_name = g_simple_action_get_name;
289 iface->get_parameter_type = g_simple_action_get_parameter_type;
290 iface->get_state_type = g_simple_action_get_state_type;
291 iface->get_state_hint = g_simple_action_get_state_hint;
292 iface->get_enabled = g_simple_action_get_enabled;
293 iface->get_state = g_simple_action_get_state;
294 iface->change_state = g_simple_action_change_state;
295 iface->activate = g_simple_action_activate;
299 g_simple_action_class_init (GSimpleActionClass *class)
301 GObjectClass *object_class = G_OBJECT_CLASS (class);
303 object_class->get_property = g_simple_action_get_property;
304 object_class->set_property = g_simple_action_set_property;
305 object_class->finalize = g_simple_action_finalize;
308 * GSimpleAction::activate:
309 * @simple: the #GSimpleAction
310 * @parameter: (allow-none): the parameter to the activation
312 * Indicates that the action was just activated.
314 * @parameter will always be of the expected type. In the event that
315 * an incorrect type was given, no signal will be emitted.
319 g_simple_action_signals[SIGNAL_ACTIVATE] =
320 g_signal_new (I_("activate"),
321 G_TYPE_SIMPLE_ACTION,
322 G_SIGNAL_RUN_LAST | G_SIGNAL_MUST_COLLECT,
324 g_cclosure_marshal_VOID__VARIANT,
329 * GSimpleAction:name:
331 * The name of the action. This is mostly meaningful for identifying
332 * the action once it has been added to a #GSimpleActionGroup.
336 g_object_class_install_property (object_class, PROP_NAME,
337 g_param_spec_string ("name",
339 P_("The name used to invoke the action"),
342 G_PARAM_CONSTRUCT_ONLY |
343 G_PARAM_STATIC_STRINGS));
346 * GSimpleAction:parameter-type:
348 * The type of the parameter that must be given when activating the
353 g_object_class_install_property (object_class, PROP_PARAMETER_TYPE,
354 g_param_spec_boxed ("parameter-type",
355 P_("Parameter Type"),
356 P_("The type of GVariant passed to activate()"),
359 G_PARAM_CONSTRUCT_ONLY |
360 G_PARAM_STATIC_STRINGS));
363 * GSimpleAction:enabled:
365 * If @action is currently enabled.
367 * If the action is disabled then calls to g_simple_action_activate() and
368 * g_simple_action_change_state() have no effect.
372 g_object_class_install_property (object_class, PROP_ENABLED,
373 g_param_spec_boolean ("enabled",
375 P_("If the action can be activated"),
379 G_PARAM_STATIC_STRINGS));
382 * GSimpleAction:state-type:
384 * The #GVariantType of the state that the action has, or %NULL if the
385 * action is stateless.
389 g_object_class_install_property (object_class, PROP_STATE_TYPE,
390 g_param_spec_boxed ("state-type",
392 P_("The type of the state kept by the action"),
395 G_PARAM_STATIC_STRINGS));
398 * GSimpleAction:state:
400 * The state of the action, or %NULL if the action is stateless.
404 g_object_class_install_property (object_class, PROP_STATE,
405 g_param_spec_variant ("state",
407 P_("The state the action is in"),
411 G_PARAM_STATIC_STRINGS));
415 * g_simple_action_set_enabled:
416 * @simple: a #GSimpleAction
417 * @enabled: whether the action is enabled
419 * Sets the action as enabled or not.
421 * An action must be enabled in order to be activated or in order to
422 * have its state changed from outside callers.
424 * This should only be called by the implementor of the action. Users
425 * of the action should not attempt to modify its enabled flag.
430 g_simple_action_set_enabled (GSimpleAction *simple,
433 g_return_if_fail (G_IS_SIMPLE_ACTION (simple));
437 if (simple->enabled != enabled)
439 simple->enabled = enabled;
440 g_object_notify (G_OBJECT (simple), "enabled");
444 * g_simple_action_new:
445 * @name: the name of the action
446 * @parameter_type: (allow-none): the type of parameter to the activate function
448 * Creates a new action.
450 * The created action is stateless. See g_simple_action_new_stateful().
452 * Returns: a new #GSimpleAction
457 g_simple_action_new (const gchar *name,
458 const GVariantType *parameter_type)
460 return g_object_new (G_TYPE_SIMPLE_ACTION,
462 "parameter-type", parameter_type,
467 * g_simple_action_new_stateful:
468 * @name: the name of the action
469 * @parameter_type: (allow-none): the type of the parameter to the activate function
470 * @state: the initial state of the action
472 * Creates a new stateful action.
474 * @state is the initial state of the action. All future state values
475 * must have the same #GVariantType as the initial state.
477 * If the @state GVariant is floating, it is consumed.
479 * Returns: a new #GSimpleAction
484 g_simple_action_new_stateful (const gchar *name,
485 const GVariantType *parameter_type,
488 GSimpleAction *simple;
490 simple = g_object_new (G_TYPE_SIMPLE_ACTION,
492 "parameter-type", parameter_type,
496 simple->state = g_variant_ref_sink (state);