Clean up GApplication docs
[platform/upstream/glib.git] / gio / gaction.c
1 /*
2  * Copyright © 2010 Codethink Limited
3  *
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.
8  *
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.
13  *
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.
18  *
19  * Authors: Ryan Lortie <desrt@desrt.ca>
20  */
21
22 #include "config.h"
23 #include "gaction.h"
24 #include "glibintl.h"
25
26 G_DEFINE_INTERFACE (GAction, g_action, G_TYPE_OBJECT)
27
28 /**
29  * SECTION:gaction
30  * @title: GAction
31  * @short_description: An action interface
32  *
33  * #GAction represents a single named action.
34  *
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).
40  *
41  * An action may optionally have a state, in which case the state may be
42  * set with g_action_change_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).
45  *
46  * The state may have a hint associated with it, specifying its valid
47  * range.
48  *
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.
52  *
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_change_state() for type
58  * safety and for the state being enabled.
59  *
60  * Probably the only useful thing to do with a #GAction is to put it
61  * inside of a #GSimpleActionGroup.
62  **/
63
64 /**
65  * GActionInterface:
66  * @get_name: the virtual function pointer for g_action_get_name()
67  * @get_parameter_type: the virtual function pointer for g_action_get_parameter_type()
68  * @get_state_type: the virtual function pointer for g_action_get_state_type()
69  * @get_state_hint: the virtual function pointer for g_action_get_state_hint()
70  * @get_enabled: the virtual function pointer for g_action_get_enabled()
71  * @get_state: the virtual function pointer for g_action_get_state()
72  * @change_state: the virtual function pointer for g_action_change_state()
73  * @activate: the virtual function pointer for g_action_activate().  Note that #GAction does not have an
74  *            'activate' signal but that implementations of it may have one.
75  *
76  * The virtual function table for #GAction.
77  *
78  * Since: 2.28
79  */
80
81 void
82 g_action_default_init (GActionInterface *iface)
83 {
84   /**
85    * GAction:name:
86    *
87    * The name of the action.  This is mostly meaningful for identifying
88    * the action once it has been added to a #GActionGroup.
89    *
90    * Since: 2.28
91    **/
92   g_object_interface_install_property (iface,
93                                        g_param_spec_string ("name",
94                                                             P_("Action Name"),
95                                                             P_("The name used to invoke the action"),
96                                                             NULL,
97                                                             G_PARAM_READWRITE |
98                                                             G_PARAM_CONSTRUCT_ONLY |
99                                                             G_PARAM_STATIC_STRINGS));
100
101   /**
102    * GAction:parameter-type:
103    *
104    * The type of the parameter that must be given when activating the
105    * action.
106    *
107    * Since: 2.28
108    **/
109   g_object_interface_install_property (iface,
110                                        g_param_spec_boxed ("parameter-type",
111                                                            P_("Parameter Type"),
112                                                            P_("The type of GVariant passed to activate()"),
113                                                            G_TYPE_VARIANT_TYPE,
114                                                            G_PARAM_READWRITE |
115                                                            G_PARAM_CONSTRUCT_ONLY |
116                                                            G_PARAM_STATIC_STRINGS));
117
118   /**
119    * GAction:enabled:
120    *
121    * If @action is currently enabled.
122    *
123    * If the action is disabled then calls to g_action_activate() and
124    * g_action_change_state() have no effect.
125    *
126    * Since: 2.28
127    **/
128   g_object_interface_install_property (iface,
129                                        g_param_spec_boolean ("enabled",
130                                                              P_("Enabled"),
131                                                              P_("If the action can be activated"),
132                                                              TRUE,
133                                                              G_PARAM_READWRITE |
134                                                              G_PARAM_CONSTRUCT_ONLY |
135                                                              G_PARAM_STATIC_STRINGS));
136
137   /**
138    * GAction:state-type:
139    *
140    * The #GVariantType of the state that the action has, or %NULL if the
141    * action is stateless.
142    *
143    * Since: 2.28
144    **/
145   g_object_interface_install_property (iface,
146                                        g_param_spec_boxed ("state-type",
147                                                            P_("State Type"),
148                                                            P_("The type of the state kept by the action"),
149                                                            G_TYPE_VARIANT_TYPE,
150                                                            G_PARAM_READABLE |
151                                                            G_PARAM_STATIC_STRINGS));
152
153   /**
154    * GAction:state:
155    *
156    * The state of the action, or %NULL if the action is stateless.
157    *
158    * Since: 2.28
159    **/
160   g_object_interface_install_property (iface,
161                                        g_param_spec_variant ("state",
162                                                              P_("State"),
163                                                              P_("The state the action is in"),
164                                                              G_VARIANT_TYPE_ANY,
165                                                              NULL,
166                                                              G_PARAM_READWRITE |
167                                                              G_PARAM_CONSTRUCT_ONLY |
168                                                              G_PARAM_STATIC_STRINGS));
169 }
170
171 /**
172  * g_action_change_state:
173  * @action: a #GAction
174  * @value: the new state
175  *
176  * Request for the state of @action to be changed to @value.
177  *
178  * The action must be stateful and @value must be of the correct type.
179  * See g_action_get_state_type().
180  *
181  * This call merely requests a change.  The action may refuse to change
182  * its state or may change its state to something other than @value.
183  * See g_action_get_state_hint().
184  *
185  * If the @value GVariant is floating, it is consumed.
186  *
187  * Since: 2.30
188  **/
189 void
190 g_action_change_state (GAction  *action,
191                        GVariant *value)
192 {
193   const GVariantType *state_type;
194
195   g_return_if_fail (G_IS_ACTION (action));
196   g_return_if_fail (value != NULL);
197   state_type = g_action_get_state_type (action);
198   g_return_if_fail (state_type != NULL);
199   g_return_if_fail (g_variant_is_of_type (value, state_type));
200
201   g_variant_ref_sink (value);
202
203   G_ACTION_GET_IFACE (action)
204     ->change_state (action, value);
205
206   g_variant_unref (value);
207 }
208
209 /**
210  * g_action_get_state:
211  * @action: a #GAction
212  *
213  * Queries the current state of @action.
214  *
215  * If the action is not stateful then %NULL will be returned.  If the
216  * action is stateful then the type of the return value is the type
217  * given by g_action_get_state_type().
218  *
219  * The return value (if non-%NULL) should be freed with
220  * g_variant_unref() when it is no longer required.
221  *
222  * Returns: (transfer full): the current state of the action
223  *
224  * Since: 2.28
225  **/
226 GVariant *
227 g_action_get_state (GAction *action)
228 {
229   g_return_val_if_fail (G_IS_ACTION (action), NULL);
230
231   return G_ACTION_GET_IFACE (action)
232     ->get_state (action);
233 }
234
235 /**
236  * g_action_get_name:
237  * @action: a #GAction
238  *
239  * Queries the name of @action.
240  *
241  * Returns: the name of the action
242  *
243  * Since: 2.28
244  **/
245 const gchar *
246 g_action_get_name (GAction *action)
247 {
248   g_return_val_if_fail (G_IS_ACTION (action), NULL);
249
250   return G_ACTION_GET_IFACE (action)
251     ->get_name (action);
252 }
253
254 /**
255  * g_action_get_parameter_type:
256  * @action: a #GAction
257  *
258  * Queries the type of the parameter that must be given when activating
259  * @action.
260  *
261  * When activating the action using g_action_activate(), the #GVariant
262  * given to that function must be of the type returned by this function.
263  *
264  * In the case that this function returns %NULL, you must not give any
265  * #GVariant, but %NULL instead.
266  *
267  * Returns: (allow-none): the parameter type
268  *
269  * Since: 2.28
270  **/
271 const GVariantType *
272 g_action_get_parameter_type (GAction *action)
273 {
274   g_return_val_if_fail (G_IS_ACTION (action), NULL);
275
276   return G_ACTION_GET_IFACE (action)
277     ->get_parameter_type (action);
278 }
279
280 /**
281  * g_action_get_state_type:
282  * @action: a #GAction
283  *
284  * Queries the type of the state of @action.
285  *
286  * If the action is stateful (e.g. created with
287  * g_simple_action_new_stateful()) then this function returns the
288  * #GVariantType of the state.  This is the type of the initial value
289  * given as the state. All calls to g_action_change_state() must give a
290  * #GVariant of this type and g_action_get_state() will return a
291  * #GVariant of the same type.
292  *
293  * If the action is not stateful (e.g. created with g_simple_action_new())
294  * then this function will return %NULL. In that case, g_action_get_state()
295  * will return %NULL and you must not call g_action_change_state().
296  *
297  * Returns: (allow-none): the state type, if the action is stateful
298  *
299  * Since: 2.28
300  **/
301 const GVariantType *
302 g_action_get_state_type (GAction *action)
303 {
304   g_return_val_if_fail (G_IS_ACTION (action), NULL);
305
306   return G_ACTION_GET_IFACE (action)
307     ->get_state_type (action);
308 }
309
310 /**
311  * g_action_get_state_hint:
312  * @action: a #GAction
313  *
314  * Requests a hint about the valid range of values for the state of
315  * @action.
316  *
317  * If %NULL is returned it either means that the action is not stateful
318  * or that there is no hint about the valid range of values for the
319  * state of the action.
320  *
321  * If a #GVariant array is returned then each item in the array is a
322  * possible value for the state.  If a #GVariant pair (ie: two-tuple) is
323  * returned then the tuple specifies the inclusive lower and upper bound
324  * of valid values for the state.
325  *
326  * In any case, the information is merely a hint.  It may be possible to
327  * have a state value outside of the hinted range and setting a value
328  * within the range may fail.
329  *
330  * The return value (if non-%NULL) should be freed with
331  * g_variant_unref() when it is no longer required.
332  *
333  * Returns: (transfer full): the state range hint
334  *
335  * Since: 2.28
336  **/
337 GVariant *
338 g_action_get_state_hint (GAction *action)
339 {
340   g_return_val_if_fail (G_IS_ACTION (action), NULL);
341
342   return G_ACTION_GET_IFACE (action)
343     ->get_state_hint (action);
344 }
345
346 /**
347  * g_action_get_enabled:
348  * @action: a #GAction
349  *
350  * Checks if @action is currently enabled.
351  *
352  * An action must be enabled in order to be activated or in order to
353  * have its state changed from outside callers.
354  *
355  * Returns: whether the action is enabled
356  *
357  * Since: 2.28
358  **/
359 gboolean
360 g_action_get_enabled (GAction *action)
361 {
362   g_return_val_if_fail (G_IS_ACTION (action), FALSE);
363
364   return G_ACTION_GET_IFACE (action)
365     ->get_enabled (action);
366 }
367
368 /**
369  * g_action_activate:
370  * @action: a #GAction
371  * @parameter: (allow-none): the parameter to the activation
372  *
373  * Activates the action.
374  *
375  * @parameter must be the correct type of parameter for the action (ie:
376  * the parameter type given at construction time).  If the parameter
377  * type was %NULL then @parameter must also be %NULL.
378  *
379  * Since: 2.28
380  **/
381 void
382 g_action_activate (GAction  *action,
383                    GVariant *parameter)
384 {
385   g_return_if_fail (G_IS_ACTION (action));
386
387   if (parameter != NULL)
388     g_variant_ref_sink (parameter);
389
390   G_ACTION_GET_IFACE (action)
391     ->activate (action, parameter);
392
393   if (parameter != NULL)
394     g_variant_unref (parameter);
395 }