Improvde #include order consistency
[glib.git] / gio / gsimpleaction.c
blob64dacfd9954b2934b4b2b33523d1fc6ce6776ce7
1 /*
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>
22 #include "config.h"
24 #include "gsimpleaction.h"
26 #include "gaction.h"
27 #include "glibintl.h"
29 /**
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.
40 struct _GSimpleAction
42 GObject parent_instance;
44 gchar *name;
45 GVariantType *parameter_type;
46 gboolean enabled;
47 GVariant *state;
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))
56 enum
58 PROP_NONE,
59 PROP_NAME,
60 PROP_PARAMETER_TYPE,
61 PROP_ENABLED,
62 PROP_STATE_TYPE,
63 PROP_STATE
66 enum
68 SIGNAL_CHANGE_STATE,
69 SIGNAL_ACTIVATE,
70 NR_SIGNALS
73 static guint g_simple_action_signals[NR_SIGNALS];
75 static const gchar *
76 g_simple_action_get_name (GAction *action)
78 GSimpleAction *simple = G_SIMPLE_ACTION (action);
80 return simple->name;
83 static const GVariantType *
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);
98 else
99 return NULL;
102 static GVariant *
103 g_simple_action_get_state_hint (GAction *action)
105 return NULL;
108 static gboolean
109 g_simple_action_get_enabled (GAction *action)
111 GSimpleAction *simple = G_SIMPLE_ACTION (action);
113 return simple->enabled;
116 static void
117 g_simple_action_change_state (GAction *action,
118 GVariant *value)
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. */
129 else
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.
147 * Since: 2.30
149 void
150 g_simple_action_set_state (GSimpleAction *simple,
151 GVariant *value)
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))
169 if (simple->state)
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);
180 static GVariant *
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;
188 static void
189 g_simple_action_activate (GAction *action,
190 GVariant *parameter)
192 GSimpleAction *simple = G_SIMPLE_ACTION (action);
194 g_return_if_fail (simple->parameter_type == NULL ?
195 parameter == 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);
203 if (simple->enabled)
204 g_signal_emit (simple, g_simple_action_signals[SIGNAL_ACTIVATE], 0, parameter);
206 if (parameter != NULL)
207 g_variant_unref (parameter);
210 static void
211 g_simple_action_set_property (GObject *object,
212 guint prop_id,
213 const GValue *value,
214 GParamSpec *pspec)
216 GSimpleAction *action = G_SIMPLE_ACTION (object);
218 switch (prop_id)
220 case PROP_NAME:
221 action->name = g_strdup (g_value_get_string (value));
222 break;
224 case PROP_PARAMETER_TYPE:
225 action->parameter_type = g_value_dup_boxed (value);
226 break;
228 case PROP_ENABLED:
229 action->enabled = g_value_get_boolean (value);
230 break;
232 case PROP_STATE:
233 action->state = g_value_dup_variant (value);
234 break;
236 default:
237 g_assert_not_reached ();
241 static void
242 g_simple_action_get_property (GObject *object,
243 guint prop_id,
244 GValue *value,
245 GParamSpec *pspec)
247 GAction *action = G_ACTION (object);
249 switch (prop_id)
251 case PROP_NAME:
252 g_value_set_string (value, g_simple_action_get_name (action));
253 break;
255 case PROP_PARAMETER_TYPE:
256 g_value_set_boxed (value, g_simple_action_get_parameter_type (action));
257 break;
259 case PROP_ENABLED:
260 g_value_set_boolean (value, g_simple_action_get_enabled (action));
261 break;
263 case PROP_STATE_TYPE:
264 g_value_set_boxed (value, g_simple_action_get_state_type (action));
265 break;
267 case PROP_STATE:
268 g_value_take_variant (value, g_simple_action_get_state (action));
269 break;
271 default:
272 g_assert_not_reached ();
276 static void
277 g_simple_action_finalize (GObject *object)
279 GSimpleAction *simple = G_SIMPLE_ACTION (object);
281 g_free (simple->name);
282 if (simple->parameter_type)
283 g_variant_type_free (simple->parameter_type);
284 if (simple->state)
285 g_variant_unref (simple->state);
287 G_OBJECT_CLASS (g_simple_action_parent_class)
288 ->finalize (object);
291 void
292 g_simple_action_init (GSimpleAction *simple)
294 simple->enabled = TRUE;
297 void
298 g_simple_action_iface_init (GActionInterface *iface)
300 iface->get_name = g_simple_action_get_name;
301 iface->get_parameter_type = g_simple_action_get_parameter_type;
302 iface->get_state_type = g_simple_action_get_state_type;
303 iface->get_state_hint = g_simple_action_get_state_hint;
304 iface->get_enabled = g_simple_action_get_enabled;
305 iface->get_state = g_simple_action_get_state;
306 iface->change_state = g_simple_action_change_state;
307 iface->activate = g_simple_action_activate;
310 void
311 g_simple_action_class_init (GSimpleActionClass *class)
313 GObjectClass *object_class = G_OBJECT_CLASS (class);
315 object_class->set_property = g_simple_action_set_property;
316 object_class->get_property = g_simple_action_get_property;
317 object_class->finalize = g_simple_action_finalize;
320 * GSimpleAction::activate:
321 * @simple: the #GSimpleAction
322 * @parameter: (allow-none): the parameter to the activation
324 * Indicates that the action was just activated.
326 * @parameter will always be of the expected type. In the event that
327 * an incorrect type was given, no signal will be emitted.
329 * Since: 2.28
331 g_simple_action_signals[SIGNAL_ACTIVATE] =
332 g_signal_new (I_("activate"),
333 G_TYPE_SIMPLE_ACTION,
334 G_SIGNAL_RUN_LAST | G_SIGNAL_MUST_COLLECT,
335 0, NULL, NULL,
336 g_cclosure_marshal_VOID__VARIANT,
337 G_TYPE_NONE, 1,
338 G_TYPE_VARIANT);
341 * GSimpleAction::change-state:
342 * @simple: the #GSimpleAction
343 * @value: (allow-none): the requested value for the state
345 * Indicates that the action just received a request to change its
346 * state.
348 * @value will always be of the correct state type. In the event that
349 * an incorrect type was given, no signal will be emitted.
351 * If no handler is connected to this signal then the default
352 * behaviour is to call g_simple_action_set_state() to set the state
353 * to the requested value. If you connect a signal handler then no
354 * default action is taken. If the state should change then you must
355 * call g_simple_action_set_state() from the handler.
357 * <example>
358 * <title>Example 'change-state' handler</title>
359 * <programlisting>
360 * static void
361 * change_volume_state (GSimpleAction *action,
362 * GVariant *value,
363 * gpointer user_data)
365 * gint requested;
367 * requested = g_variant_get_int32 (value);
369 * // Volume only goes from 0 to 10
370 * if (0 <= requested && requested <= 10)
371 * g_simple_action_set_state (action, value);
373 * </programlisting>
374 * </example>
376 * The handler need not set the state to the requested value. It
377 * could set it to any value at all, or take some other action.
379 * Since: 2.30
381 g_simple_action_signals[SIGNAL_CHANGE_STATE] =
382 g_signal_new (I_("change-state"),
383 G_TYPE_SIMPLE_ACTION,
384 G_SIGNAL_RUN_LAST | G_SIGNAL_MUST_COLLECT,
385 0, NULL, NULL,
386 g_cclosure_marshal_VOID__VARIANT,
387 G_TYPE_NONE, 1,
388 G_TYPE_VARIANT);
391 * GSimpleAction:name:
393 * The name of the action. This is mostly meaningful for identifying
394 * the action once it has been added to a #GSimpleActionGroup.
396 * Since: 2.28
398 g_object_class_install_property (object_class, PROP_NAME,
399 g_param_spec_string ("name",
400 P_("Action Name"),
401 P_("The name used to invoke the action"),
402 NULL,
403 G_PARAM_READWRITE |
404 G_PARAM_CONSTRUCT_ONLY |
405 G_PARAM_STATIC_STRINGS));
408 * GSimpleAction:parameter-type:
410 * The type of the parameter that must be given when activating the
411 * action.
413 * Since: 2.28
415 g_object_class_install_property (object_class, PROP_PARAMETER_TYPE,
416 g_param_spec_boxed ("parameter-type",
417 P_("Parameter Type"),
418 P_("The type of GVariant passed to activate()"),
419 G_TYPE_VARIANT_TYPE,
420 G_PARAM_READWRITE |
421 G_PARAM_CONSTRUCT_ONLY |
422 G_PARAM_STATIC_STRINGS));
425 * GSimpleAction:enabled:
427 * If @action is currently enabled.
429 * If the action is disabled then calls to g_action_activate() and
430 * g_action_change_state() have no effect.
432 * Since: 2.28
434 g_object_class_install_property (object_class, PROP_ENABLED,
435 g_param_spec_boolean ("enabled",
436 P_("Enabled"),
437 P_("If the action can be activated"),
438 TRUE,
439 G_PARAM_READWRITE |
440 G_PARAM_STATIC_STRINGS));
443 * GSimpleAction:state-type:
445 * The #GVariantType of the state that the action has, or %NULL if the
446 * action is stateless.
448 * Since: 2.28
450 g_object_class_install_property (object_class, PROP_STATE_TYPE,
451 g_param_spec_boxed ("state-type",
452 P_("State Type"),
453 P_("The type of the state kept by the action"),
454 G_TYPE_VARIANT_TYPE,
455 G_PARAM_READABLE |
456 G_PARAM_STATIC_STRINGS));
459 * GSimpleAction:state:
461 * The state of the action, or %NULL if the action is stateless.
463 * Since: 2.28
465 g_object_class_install_property (object_class, PROP_STATE,
466 g_param_spec_variant ("state",
467 P_("State"),
468 P_("The state the action is in"),
469 G_VARIANT_TYPE_ANY,
470 NULL,
471 G_PARAM_READWRITE |
472 G_PARAM_STATIC_STRINGS));
476 * g_simple_action_set_enabled:
477 * @simple: a #GSimpleAction
478 * @enabled: whether the action is enabled
480 * Sets the action as enabled or not.
482 * An action must be enabled in order to be activated or in order to
483 * have its state changed from outside callers.
485 * This should only be called by the implementor of the action. Users
486 * of the action should not attempt to modify its enabled flag.
488 * Since: 2.28
490 void
491 g_simple_action_set_enabled (GSimpleAction *simple,
492 gboolean enabled)
494 g_return_if_fail (G_IS_SIMPLE_ACTION (simple));
496 enabled = !!enabled;
498 if (simple->enabled != enabled)
500 simple->enabled = enabled;
501 g_object_notify (G_OBJECT (simple), "enabled");
505 * g_simple_action_new:
506 * @name: the name of the action
507 * @parameter_type: (allow-none): the type of parameter to the activate function
509 * Creates a new action.
511 * The created action is stateless. See g_simple_action_new_stateful().
513 * Returns: a new #GSimpleAction
515 * Since: 2.28
517 GSimpleAction *
518 g_simple_action_new (const gchar *name,
519 const GVariantType *parameter_type)
521 return g_object_new (G_TYPE_SIMPLE_ACTION,
522 "name", name,
523 "parameter-type", parameter_type,
524 NULL);
528 * g_simple_action_new_stateful:
529 * @name: the name of the action
530 * @parameter_type: (allow-none): the type of the parameter to the activate function
531 * @state: the initial state of the action
533 * Creates a new stateful action.
535 * @state is the initial state of the action. All future state values
536 * must have the same #GVariantType as the initial state.
538 * If the @state GVariant is floating, it is consumed.
540 * Returns: a new #GSimpleAction
542 * Since: 2.28
544 GSimpleAction *
545 g_simple_action_new_stateful (const gchar *name,
546 const GVariantType *parameter_type,
547 GVariant *state)
549 return g_object_new (G_TYPE_SIMPLE_ACTION,
550 "name", name,
551 "parameter-type", parameter_type,
552 "state", state,
553 NULL);