Docs: Convert examples to |[ ]|
[platform/upstream/glib.git] / gio / gactionmap.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, see <http://www.gnu.org/licenses/>.
16  *
17  * Authors: Ryan Lortie <desrt@desrt.ca>
18  */
19
20 #include "config.h"
21
22 #include "gsimpleaction.h"
23 #include "gactionmap.h"
24 #include "gaction.h"
25
26 /**
27  * SECTION:gactionmap
28  * @title: GActionMap
29  * @include: gio/gio.h
30  * @short_description: Interface for action containers
31  *
32  * The GActionMap interface is implemented by #GActionGroup
33  * implementations that operate by containing a number of
34  * named #GAction instances, such as #GSimpleActionGroup.
35  *
36  * One useful application of this interface is to map the
37  * names of actions from various action groups to unique,
38  * prefixed names (e.g. by prepending "app." or "win.").
39  * This is the motivation for the 'Map' part of the interface
40  * name.
41  *
42  * Since: 2.32
43  **/
44
45 /**
46  * GActionMapInterface:
47  * @lookup_action: the virtual function pointer for g_action_map_lookup_action()
48  * @add_action: the virtual function pointer for g_action_map_add_action()
49  * @remove_action: the virtual function pointer for g_action_map_remove_action()
50  *
51  * The virtual function table for #GActionMap.
52  *
53  * Since: 2.32
54  **/
55
56 G_DEFINE_INTERFACE (GActionMap, g_action_map, G_TYPE_OBJECT)
57
58 static void
59 g_action_map_default_init (GActionMapInterface *iface)
60 {
61 }
62
63 /**
64  * g_action_map_lookup_action:
65  * @action_map: a #GActionMap
66  * @action_name: the name of an action
67  *
68  * Looks up the action with the name @action_name in @action_map.
69  *
70  * If no such action exists, returns %NULL.
71  *
72  * Returns: (transfer none): a #GAction, or %NULL
73  *
74  * Since: 2.32
75  */
76 GAction *
77 g_action_map_lookup_action (GActionMap  *action_map,
78                             const gchar *action_name)
79 {
80   return G_ACTION_MAP_GET_IFACE (action_map)
81     ->lookup_action (action_map, action_name);
82 }
83
84 /**
85  * g_action_map_add_action:
86  * @action_map: a #GActionMap
87  * @action: a #GAction
88  *
89  * Adds an action to the @action_map.
90  *
91  * If the action map already contains an action with the same name
92  * as @action then the old action is dropped from the action map.
93  *
94  * The action map takes its own reference on @action.
95  *
96  * Since: 2.32
97  */
98 void
99 g_action_map_add_action (GActionMap *action_map,
100                          GAction    *action)
101 {
102   G_ACTION_MAP_GET_IFACE (action_map)->add_action (action_map, action);
103 }
104
105 /**
106  * g_action_map_remove_action:
107  * @action_map: a #GActionMap
108  * @action_name: the name of the action
109  *
110  * Removes the named action from the action map.
111  *
112  * If no action of this name is in the map then nothing happens.
113  *
114  * Since: 2.32
115  */
116 void
117 g_action_map_remove_action (GActionMap  *action_map,
118                             const gchar *action_name)
119 {
120   G_ACTION_MAP_GET_IFACE (action_map)->remove_action (action_map, action_name);
121 }
122
123 /**
124  * GActionEntry:
125  * @name: the name of the action
126  * @activate: the callback to connect to the "activate" signal of the
127  *            action
128  * @parameter_type: the type of the parameter that must be passed to the
129  *                  activate function for this action, given as a single
130  *                  GVariant type string (or %NULL for no parameter)
131  * @state: the initial state for this action, given in GVariant text
132  *         format.  The state is parsed with no extra type information,
133  *         so type tags must be added to the string if they are
134  *         necessary.
135  * @change_state: the callback to connect to the "change-state" signal
136  *                of the action
137  *
138  * This struct defines a single action.  It is for use with
139  * g_action_map_add_action_entries().
140  *
141  * The order of the items in the structure are intended to reflect
142  * frequency of use.  It is permissible to use an incomplete initialiser
143  * in order to leave some of the later values as %NULL.  All values
144  * after @name are optional.  Additional optional fields may be added in
145  * the future.
146  *
147  * See g_action_map_add_action_entries() for an example.
148  **/
149
150 /**
151  * g_action_map_add_action_entries:
152  * @action_map: a #GActionMap
153  * @entries: (array length=n_entries) (element-type GActionEntry): a pointer to
154  *           the first item in an array of #GActionEntry structs
155  * @n_entries: the length of @entries, or -1 if @entries is %NULL-terminated
156  * @user_data: the user data for signal connections
157  *
158  * A convenience function for creating multiple #GSimpleAction instances
159  * and adding them to a #GActionMap.
160  *
161  * Each action is constructed as per one #GActionEntry.
162  *
163  * |[
164  * static void
165  * activate_quit (GSimpleAction *simple,
166  *                GVariant      *parameter,
167  *                gpointer       user_data)
168  * {
169  *   exit (0);
170  * }
171  *
172  * static void
173  * activate_print_string (GSimpleAction *simple,
174  *                        GVariant      *parameter,
175  *                        gpointer       user_data)
176  * {
177  *   g_print ("%s\n", g_variant_get_string (parameter, NULL));
178  * }
179  *
180  * static GActionGroup *
181  * create_action_group (void)
182  * {
183  *   const GActionEntry entries[] = {
184  *     { "quit",         activate_quit              },
185  *     { "print-string", activate_print_string, "s" }
186  *   };
187  *   GSimpleActionGroup *group;
188  *
189  *   group = g_simple_action_group_new ();
190  *   g_action_map_add_action_entries (G_ACTION_MAP (group), entries, G_N_ELEMENTS (entries), NULL);
191  *
192  *   return G_ACTION_GROUP (group);
193  * }
194  * ]|
195  *
196  * Since: 2.32
197  */
198 void
199 g_action_map_add_action_entries (GActionMap         *action_map,
200                                  const GActionEntry *entries,
201                                  gint                n_entries,
202                                  gpointer            user_data)
203 {
204   gint i;
205
206   g_return_if_fail (G_IS_ACTION_MAP (action_map));
207   g_return_if_fail (entries != NULL || n_entries == 0);
208
209   for (i = 0; n_entries == -1 ? entries[i].name != NULL : i < n_entries; i++)
210     {
211       const GActionEntry *entry = &entries[i];
212       const GVariantType *parameter_type;
213       GSimpleAction *action;
214
215       if (entry->parameter_type)
216         {
217           if (!g_variant_type_string_is_valid (entry->parameter_type))
218             {
219               g_critical ("g_action_map_add_entries: the type "
220                           "string '%s' given as the parameter type for "
221                           "action '%s' is not a valid GVariant type "
222                           "string.  This action will not be added.",
223                           entry->parameter_type, entry->name);
224               return;
225             }
226
227           parameter_type = G_VARIANT_TYPE (entry->parameter_type);
228         }
229       else
230         parameter_type = NULL;
231
232       if (entry->state)
233         {
234           GError *error = NULL;
235           GVariant *state;
236
237           state = g_variant_parse (NULL, entry->state, NULL, NULL, &error);
238           if (state == NULL)
239             {
240               g_critical ("g_action_map_add_entries: GVariant could "
241                           "not parse the state value given for action '%s' "
242                           "('%s'): %s.  This action will not be added.",
243                           entry->name, entry->state, error->message);
244               g_error_free (error);
245               continue;
246             }
247
248           action = g_simple_action_new_stateful (entry->name,
249                                                  parameter_type,
250                                                  state);
251
252           g_variant_unref (state);
253         }
254       else
255         {
256           action = g_simple_action_new (entry->name,
257                                         parameter_type);
258         }
259
260       if (entry->activate != NULL)
261         g_signal_connect (action, "activate",
262                           G_CALLBACK (entry->activate), user_data);
263
264       if (entry->change_state != NULL)
265         g_signal_connect (action, "change-state",
266                           G_CALLBACK (entry->change_state), user_data);
267
268       g_action_map_add_action (action_map, G_ACTION (action));
269       g_object_unref (action);
270     }
271 }