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>
26 G_DEFINE_INTERFACE (GAction, g_action, G_TYPE_OBJECT)
31 * @short_description: an action
33 * #GAction represents a single named action.
35 * The main interface to an action is that it can be activated with
36 * g_action_activate(). This results in the 'activate' signal being
37 * emitted. An activation has a #GVariant parameter (which may be
38 * %NULL). The correct type for the parameter is determined by a static
39 * parameter type (which is given at construction time).
41 * An action may optionally have a state, in which case the state may be
42 * set with g_action_set_state(). This call takes a #GVariant. The
43 * correct type for the state is determined by a static state type
44 * (which is given at construction time).
46 * The state may have a hint associated with it, specifying its valid
49 * #GAction is merely the interface to the concept of an action, as
50 * described above. Various implementations of actions exist, including
51 * #GSimpleAction and #GtkAction.
53 * In all cases, the implementing class is responsible for storing the
54 * name of the action, the parameter type, the enabled state, the
55 * optional state type and the state and emitting the appropriate
56 * signals when these change. The implementor responsible for filtering
57 * calls to g_action_activate() and g_action_set_state() for type safety
58 * and for the state being enabled.
60 * Probably the only useful thing to do with a #GAction is to put it
61 * inside of a #GSimpleActionGroup.
65 g_action_default_init (GActionInterface *iface)
70 * The name of the action. This is mostly meaningful for identifying
71 * the action once it has been added to a #GActionGroup.
75 g_object_interface_install_property (iface,
76 g_param_spec_string ("name",
78 P_("The name used to invoke the action"),
81 G_PARAM_STATIC_STRINGS));
84 * GAction:parameter-type:
86 * The type of the parameter that must be given when activating the
91 g_object_interface_install_property (iface,
92 g_param_spec_boxed ("parameter-type",
94 P_("The type of GVariant passed to activate()"),
97 G_PARAM_STATIC_STRINGS));
102 * If @action is currently enabled.
104 * If the action is disabled then calls to g_action_activate() and
105 * g_action_set_state() have no effect.
109 g_object_interface_install_property (iface,
110 g_param_spec_boolean ("enabled",
112 P_("If the action can be activated"),
115 G_PARAM_STATIC_STRINGS));
118 * GAction:state-type:
120 * The #GVariantType of the state that the action has, or %NULL if the
121 * action is stateless.
125 g_object_interface_install_property (iface,
126 g_param_spec_boxed ("state-type",
128 P_("The type of the state kept by the action"),
131 G_PARAM_STATIC_STRINGS));
136 * The state of the action, or %NULL if the action is stateless.
140 g_object_interface_install_property (iface,
141 g_param_spec_variant ("state",
143 P_("The state the action is in"),
148 G_PARAM_STATIC_STRINGS));
152 * g_action_set_state:
153 * @action: a #GAction
154 * @value: the new state
156 * Request for the state of @action to be changed to @value.
158 * The action must be stateful and @value must be of the correct type.
159 * See g_action_get_state_type().
161 * This call merely requests a change. The action may refuse to change
162 * its state or may change its state to something other than @value.
163 * See g_action_get_state_hint().
165 * If the @value GVariant is floating, it is consumed.
170 g_action_set_state (GAction *action,
173 const GVariantType *state_type;
175 g_return_if_fail (G_IS_ACTION (action));
176 g_return_if_fail (value != NULL);
177 state_type = g_action_get_state_type (action);
178 g_return_if_fail (state_type != NULL);
179 g_return_if_fail (g_variant_is_of_type (value, state_type));
181 g_variant_ref_sink (value);
183 G_ACTION_GET_IFACE (action)
184 ->set_state (action, value);
186 g_variant_unref (value);
190 * g_action_get_state:
191 * @action: a #GAction
193 * Queries the current state of @action.
195 * If the action is not stateful then %NULL will be returned. If the
196 * action is stateful then the type of the return value is the type
197 * given by g_action_get_state_type().
199 * The return value (if non-%NULL) should be freed with
200 * g_variant_unref() when it is no longer required.
202 * Returns: (allow-none): the current state of the action
207 g_action_get_state (GAction *action)
209 g_return_val_if_fail (G_IS_ACTION (action), NULL);
211 return G_ACTION_GET_IFACE (action)
212 ->get_state (action);
217 * @action: a #GAction
219 * Queries the name of @action.
221 * Returns: the name of the action
226 g_action_get_name (GAction *action)
228 g_return_val_if_fail (G_IS_ACTION (action), NULL);
230 return G_ACTION_GET_IFACE (action)
235 * g_action_get_parameter_type:
236 * @action: a #GAction
238 * Queries the type of the parameter that must be given when activating
241 * When activating the action using g_action_activate(), the #GVariant
242 * given to that function must be of the type returned by this function.
244 * In the case that this function returns %NULL, you must not give any
245 * #GVariant, but %NULL instead.
247 * Returns: (allow-none): the parameter type
252 g_action_get_parameter_type (GAction *action)
254 g_return_val_if_fail (G_IS_ACTION (action), NULL);
256 return G_ACTION_GET_IFACE (action)
257 ->get_parameter_type (action);
261 * g_action_get_state_type:
262 * @action: a #GAction
264 * Queries the type of the state of @action.
266 * If the action is stateful (ie: was created with
267 * g_action_new_stateful()) then this function returns the #GVariantType
268 * of the state. This is the type of the initial value given as the
269 * state. All calls to g_action_set_state() must give a #GVariant of
270 * this type and g_action_get_state() will return a #GVariant of the
273 * If the action is not stateful (ie: created with g_action_new()) then
274 * this function will return %NULL. In that case, g_action_get_state()
275 * will return %NULL and you must not call g_action_set_state().
277 * Returns: (allow-none): the state type, if the action is stateful
282 g_action_get_state_type (GAction *action)
284 g_return_val_if_fail (G_IS_ACTION (action), NULL);
286 return G_ACTION_GET_IFACE (action)
287 ->get_state_type (action);
291 * g_action_get_state_hint:
292 * @action: a #GAction
294 * Requests a hint about the valid range of values for the state of
297 * If %NULL is returned it either means that the action is not stateful
298 * or that there is no hint about the valid range of values for the
299 * state of the action.
301 * If a #GVariant array is returned then each item in the array is a
302 * possible value for the state. If a #GVariant pair (ie: two-tuple) is
303 * returned then the tuple specifies the inclusive lower and upper bound
304 * of valid values for the state.
306 * In any case, the information is merely a hint. It may be possible to
307 * have a state value outside of the hinted range and setting a value
308 * within the range may fail.
310 * The return value (if non-%NULL) should be freed with
311 * g_variant_unref() when it is no longer required.
313 * Returns: (allow-none): the state range hint
318 g_action_get_state_hint (GAction *action)
320 g_return_val_if_fail (G_IS_ACTION (action), NULL);
322 return G_ACTION_GET_IFACE (action)
323 ->get_state_hint (action);
327 * g_action_get_enabled:
328 * @action: a #GAction
330 * Checks if @action is currently enabled.
332 * An action must be enabled in order to be activated or in order to
333 * have its state changed from outside callers.
335 * Returns: whether the action is enabled
340 g_action_get_enabled (GAction *action)
342 g_return_val_if_fail (G_IS_ACTION (action), FALSE);
344 return G_ACTION_GET_IFACE (action)
345 ->get_enabled (action);
350 * @action: a #GAction
351 * @parameter: (allow-none): the parameter to the activation
353 * Activates the action.
355 * @parameter must be the correct type of parameter for the action (ie:
356 * the parameter type given at construction time). If the parameter
357 * type was %NULL then @parameter must also be %NULL.
362 g_action_activate (GAction *action,
365 g_return_if_fail (G_IS_ACTION (action));
367 if (parameter != NULL)
368 g_variant_ref_sink (parameter);
370 G_ACTION_GET_IFACE (action)
371 ->activate (action, parameter);
373 if (parameter != NULL)
374 g_variant_unref (parameter);