Debian Codegraph @

clutter/cally/cally-root.c

raw
1 /* CALLY - The Clutter Accessibility Implementation Library
2 *
3 * Copyright (C) 2008 Igalia, S.L.
4 *
5 * Author: Alejandro PiƱeiro Iglesias <[email protected]>
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
21 */
22
23 /**
24 * SECTION:cally-root
25 * @short_description: Root object for the Cally toolkit
26 * @see_also: #ClutterStage
27 *
28 * #CallyRoot is the root object of the accessibility tree-like
29 * hierarchy, exposing the application level.
30 *
31 * Somewhat equivalent to #GailTopLevel. We consider that this class
32 * expose the a11y information of the #ClutterStageManager, as the
33 * children of this object are the different ClutterStage managed (so
34 * the #GObject used in the atk_object_initialize() is the
35 * #ClutterStageManager).
36 */
37
38 #include "config.h"
39
40 #include "cally-root.h"
41
42 #include "clutter-actor.h"
43 #include "clutter-stage-private.h"
44 #include "clutter-stage-manager.h"
45
46
47 /* GObject */
48 static void cally_root_finalize (GObject *object);
49
50 /* AtkObject.h */
51 static void cally_root_initialize (AtkObject *accessible,
52 gpointer data);
53 static gint cally_root_get_n_children (AtkObject *obj);
54 static AtkObject * cally_root_ref_child (AtkObject *obj,
55 gint i);
56 static AtkObject * cally_root_get_parent (AtkObject *obj);
57 static const char * cally_root_get_name (AtkObject *obj);
58
59 /* Private */
60 static void cally_util_stage_added_cb (ClutterStageManager *stage_manager,
61 ClutterStage *stage,
62 gpointer data);
63 static void cally_util_stage_removed_cb (ClutterStageManager *stage_manager,
64 ClutterStage *stage,
65 gpointer data);
66
67 struct _CallyRootPrivate
68 {
69 /* We save the CallyStage objects. Other option could save the stage
70 * list, and then just get the a11y object on the ref_child, etc. But
71 * the ref_child is more common that the init and the stage-add,
72 * stage-remove, so we avoid getting the accessible object
73 * constantly
74 */
75 GSList *stage_list;
76
77 /* signals id */
78 guint stage_added_id;
79 guint stage_removed_id;
80 };
81
82 G_DEFINE_TYPE_WITH_PRIVATE (CallyRoot, cally_root, ATK_TYPE_GOBJECT_ACCESSIBLE)
83
84 static void
85 cally_root_class_init (CallyRootClass *klass)
86 {
87 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
88 AtkObjectClass *class = ATK_OBJECT_CLASS (klass);
89
90 gobject_class->finalize = cally_root_finalize;
91
92 /* AtkObject */
93 class->get_n_children = cally_root_get_n_children;
94 class->ref_child = cally_root_ref_child;
95 class->get_parent = cally_root_get_parent;
96 class->initialize = cally_root_initialize;
97 class->get_name = cally_root_get_name;
98 }
99
100 static void
101 cally_root_init (CallyRoot *root)
102 {
103 root->priv = cally_root_get_instance_private (root);
104
105 root->priv->stage_list = NULL;
106 root->priv->stage_added_id = 0;
107 root->priv->stage_removed_id = 0;
108 }
109
110 /**
111 * cally_root_new:
112 *
113 * Creates a new #CallyRoot object.
114 *
115 * Return value: the newly created #AtkObject
116 *
117 * Since: 1.4
118 */
119 AtkObject*
120 cally_root_new (void)
121 {
122 GObject *object = NULL;
123 AtkObject *accessible = NULL;
124 ClutterStageManager *stage_manager = NULL;
125
126 object = g_object_new (CALLY_TYPE_ROOT, NULL);
127
128 accessible = ATK_OBJECT (object);
129 stage_manager = clutter_stage_manager_get_default ();
130
131 atk_object_initialize (accessible, stage_manager);
132
133 return accessible;
134 }
135
136 static void
137 cally_root_finalize (GObject *object)
138 {
139 CallyRoot *root = CALLY_ROOT (object);
140 GObject *stage_manager = NULL;
141
142 g_return_if_fail (CALLY_IS_ROOT (object));
143
144 if (root->priv->stage_list)
145 {
146 g_slist_free (root->priv->stage_list);
147 root->priv->stage_list = NULL;
148 }
149
150 stage_manager = atk_gobject_accessible_get_object (ATK_GOBJECT_ACCESSIBLE (root));
151
152 g_signal_handler_disconnect (stage_manager,
153 root->priv->stage_added_id);
154
155 g_signal_handler_disconnect (stage_manager,
156 root->priv->stage_added_id);
157
158 G_OBJECT_CLASS (cally_root_parent_class)->finalize (object);
159 }
160
161 /* AtkObject.h */
162 static void
163 cally_root_initialize (AtkObject *accessible,
164 gpointer data)
165 {
166 ClutterStageManager *stage_manager = NULL;
167 const GSList *iter = NULL;
168 const GSList *stage_list = NULL;
169 ClutterStage *clutter_stage = NULL;
170 AtkObject *cally_stage = NULL;
171 CallyRoot *root = NULL;
172
173 accessible->role = ATK_ROLE_APPLICATION;
174 accessible->accessible_parent = NULL;
175
176 /* children initialization */
177 root = CALLY_ROOT (accessible);
178 stage_manager = CLUTTER_STAGE_MANAGER (data);
179 stage_list = clutter_stage_manager_peek_stages (stage_manager);
180
181 for (iter = stage_list; iter != NULL; iter = g_slist_next (iter))
182 {
183 clutter_stage = CLUTTER_STAGE (iter->data);
184 cally_stage = clutter_actor_get_accessible (CLUTTER_ACTOR (clutter_stage));
185
186 atk_object_set_parent (cally_stage, ATK_OBJECT (root));
187
188 root->priv->stage_list = g_slist_append (root->priv->stage_list,
189 cally_stage);
190 }
191
192 root->priv->stage_added_id =
193 g_signal_connect (G_OBJECT (stage_manager), "stage-added",
194 G_CALLBACK (cally_util_stage_added_cb), root);
195
196 root->priv->stage_removed_id =
197 g_signal_connect (G_OBJECT (stage_manager), "stage-removed",
198 G_CALLBACK (cally_util_stage_removed_cb), root);
199
200 ATK_OBJECT_CLASS (cally_root_parent_class)->initialize (accessible, data);
201 }
202
203
204 static gint
205 cally_root_get_n_children (AtkObject *obj)
206 {
207 CallyRoot *root = CALLY_ROOT (obj);
208
209 return g_slist_length (root->priv->stage_list);
210 }
211
212 static AtkObject*
213 cally_root_ref_child (AtkObject *obj,
214 gint i)
215 {
216 CallyRoot *cally_root = NULL;
217 GSList *stage_list = NULL;
218 gint num = 0;
219 AtkObject *item = NULL;
220
221 cally_root = CALLY_ROOT (obj);
222 stage_list = cally_root->priv->stage_list;
223 num = g_slist_length (stage_list);
224
225 g_return_val_if_fail ((i < num)&&(i >= 0), NULL);
226
227 item = g_slist_nth_data (stage_list, i);
228 if (!item)
229 {
230 return NULL;
231 }
232
233 g_object_ref (item);
234
235 return item;
236 }
237
238 static AtkObject*
239 cally_root_get_parent (AtkObject *obj)
240 {
241 return NULL;
242 }
243
244 static const char *
245 cally_root_get_name (AtkObject *obj)
246 {
247 return g_get_prgname ();
248 }
249
250 /* -------------------------------- PRIVATE --------------------------------- */
251
252 static void
253 cally_util_stage_added_cb (ClutterStageManager *stage_manager,
254 ClutterStage *stage,
255 gpointer data)
256 {
257 CallyRoot *root = CALLY_ROOT (data);
258 AtkObject *cally_stage = NULL;
259 gint index = -1;
260
261 cally_stage = clutter_actor_get_accessible (CLUTTER_ACTOR (stage));
262
263 atk_object_set_parent (cally_stage, ATK_OBJECT (root));
264
265 root->priv->stage_list = g_slist_append (root->priv->stage_list,
266 cally_stage);
267
268 index = g_slist_index (root->priv->stage_list, cally_stage);
269 g_signal_emit_by_name (root, "children_changed::add",
270 index, cally_stage, NULL);
271 g_signal_emit_by_name (cally_stage, "create", 0);
272 }
273
274 static void
275 cally_util_stage_removed_cb (ClutterStageManager *stage_manager,
276 ClutterStage *stage,
277 gpointer data)
278 {
279 CallyRoot *root = CALLY_ROOT (data);
280 AtkObject *cally_stage = NULL;
281 gint index = -1;
282
283 cally_stage = clutter_actor_get_accessible (CLUTTER_ACTOR (stage));
284
285 index = g_slist_index (root->priv->stage_list, cally_stage);
286
287 root->priv->stage_list = g_slist_remove (root->priv->stage_list,
288 cally_stage);
289
290 index = g_slist_index (root->priv->stage_list, cally_stage);
291 g_signal_emit_by_name (root, "children_changed::remove",
292 index, cally_stage, NULL);
293 g_signal_emit_by_name (cally_stage, "destroy", 0);
294 }