Debian Codegraph @

gtk/a11y/gtknotebookaccessible.c

raw
1 /* GTK+ - accessibility implementations
2 * Copyright 2001, 2002, 2003 Sun Microsystems Inc.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at 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 Public
15 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include "config.h"
19
20 #include <string.h>
21 #include <gtk/gtk.h>
22 #include "gtknotebookaccessible.h"
23 #include "gtknotebookpageaccessible.h"
24
25 struct _GtkNotebookAccessiblePrivate
26 {
27 /*
28 * page_cache maintains a list of pre-ref'd Notebook Pages.
29 * This cache is queried by gtk_notebook_accessible_ref_child().
30 * If the page is found in the list then a new page does not
31 * need to be created
32 */
33 GHashTable * pages;
34 gint selected_page;
35 };
36
37 static void atk_selection_interface_init (AtkSelectionIface *iface);
38
39 G_DEFINE_TYPE_WITH_CODE (GtkNotebookAccessible, gtk_notebook_accessible, GTK_TYPE_CONTAINER_ACCESSIBLE,
40 G_ADD_PRIVATE (GtkNotebookAccessible)
41 G_IMPLEMENT_INTERFACE (ATK_TYPE_SELECTION, atk_selection_interface_init))
42
43 static void
44 create_notebook_page_accessible (GtkNotebookAccessible *accessible,
45 GtkNotebook *notebook,
46 GtkWidget *child,
47 gint page_num)
48 {
49 AtkObject *obj;
50
51 obj = gtk_notebook_page_accessible_new (accessible, child);
52 g_hash_table_insert (accessible->priv->pages, child, obj);
53 atk_object_set_parent (obj, ATK_OBJECT (accessible));
54 g_signal_emit_by_name (accessible, "children-changed::add", page_num, obj, NULL);
55 }
56
57 static void
58 page_added_cb (GtkNotebook *notebook,
59 GtkWidget *child,
60 guint page_num,
61 gpointer data)
62 {
63 AtkObject *atk_obj;
64 GtkNotebookAccessible *accessible;
65
66 atk_obj = gtk_widget_get_accessible (GTK_WIDGET (notebook));
67 accessible = GTK_NOTEBOOK_ACCESSIBLE (atk_obj);
68 create_notebook_page_accessible (accessible, notebook, child, page_num);
69 }
70
71 static void
72 page_removed_cb (GtkNotebook *notebook,
73 GtkWidget *widget,
74 guint page_num,
75 gpointer data)
76 {
77 GtkNotebookAccessible *accessible;
78 AtkObject *obj;
79
80 accessible = GTK_NOTEBOOK_ACCESSIBLE (gtk_widget_get_accessible (GTK_WIDGET (notebook)));
81
82 obj = g_hash_table_lookup (accessible->priv->pages, widget);
83 g_return_if_fail (obj);
84 g_signal_emit_by_name (accessible, "children-changed::remove",
85 page_num, obj, NULL);
86 gtk_notebook_page_accessible_invalidate (GTK_NOTEBOOK_PAGE_ACCESSIBLE (obj));
87 g_hash_table_remove (accessible->priv->pages, widget);
88 }
89
90
91 static void
92 gtk_notebook_accessible_initialize (AtkObject *obj,
93 gpointer data)
94 {
95 GtkNotebookAccessible *accessible;
96 GtkNotebook *notebook;
97 gint i;
98
99 ATK_OBJECT_CLASS (gtk_notebook_accessible_parent_class)->initialize (obj, data);
100
101 accessible = GTK_NOTEBOOK_ACCESSIBLE (obj);
102 notebook = GTK_NOTEBOOK (data);
103 for (i = 0; i < gtk_notebook_get_n_pages (notebook); i++)
104 {
105 create_notebook_page_accessible (accessible,
106 notebook,
107 gtk_notebook_get_nth_page (notebook, i),
108 i);
109 }
110 accessible->priv->selected_page = gtk_notebook_get_current_page (notebook);
111
112 g_signal_connect (notebook, "page-added",
113 G_CALLBACK (page_added_cb), NULL);
114 g_signal_connect (notebook, "page-removed",
115 G_CALLBACK (page_removed_cb), NULL);
116
117 obj->role = ATK_ROLE_PAGE_TAB_LIST;
118 }
119
120 static void
121 gtk_notebook_accessible_finalize (GObject *object)
122 {
123 GtkNotebookAccessible *accessible = GTK_NOTEBOOK_ACCESSIBLE (object);
124
125 g_hash_table_destroy (accessible->priv->pages);
126
127 G_OBJECT_CLASS (gtk_notebook_accessible_parent_class)->finalize (object);
128 }
129
130 static AtkObject *
131 gtk_notebook_accessible_ref_child (AtkObject *obj,
132 gint i)
133 {
134 AtkObject *child;
135 GtkNotebookAccessible *accessible;
136 GtkNotebook *notebook;
137 GtkWidget *widget;
138
139 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (obj));
140 if (widget == NULL)
141 return NULL;
142
143 accessible = GTK_NOTEBOOK_ACCESSIBLE (obj);
144 notebook = GTK_NOTEBOOK (widget);
145
146 child = g_hash_table_lookup (accessible->priv->pages,
147 gtk_notebook_get_nth_page (notebook, i));
148 /* can return NULL when i >= n_children */
149
150 if (child)
151 g_object_ref (child);
152
153 return child;
154 }
155
156 static void
157 gtk_notebook_accessible_notify_gtk (GObject *obj,
158 GParamSpec *pspec)
159 {
160 GtkWidget *widget;
161 AtkObject* atk_obj;
162
163 widget = GTK_WIDGET (obj);
164 atk_obj = gtk_widget_get_accessible (widget);
165
166 if (strcmp (pspec->name, "page") == 0)
167 {
168 gint page_num, old_page_num;
169 GtkNotebookAccessible *accessible;
170 GtkNotebook *notebook;
171
172 accessible = GTK_NOTEBOOK_ACCESSIBLE (atk_obj);
173 notebook = GTK_NOTEBOOK (widget);
174
175 /* Notify SELECTED state change for old and new page */
176 old_page_num = accessible->priv->selected_page;
177 page_num = gtk_notebook_get_current_page (notebook);
178 accessible->priv->selected_page = page_num;
179
180 if (page_num != old_page_num)
181 {
182 AtkObject *child;
183
184 if (old_page_num != -1)
185 {
186 child = gtk_notebook_accessible_ref_child (atk_obj, old_page_num);
187 if (child)
188 {
189 atk_object_notify_state_change (child, ATK_STATE_SELECTED, FALSE);
190 g_object_unref (child);
191 }
192 }
193 child = gtk_notebook_accessible_ref_child (atk_obj, page_num);
194 if (child)
195 {
196 atk_object_notify_state_change (child, ATK_STATE_SELECTED, TRUE);
197 g_object_unref (child);
198 }
199 g_signal_emit_by_name (atk_obj, "selection-changed");
200 g_signal_emit_by_name (atk_obj, "visible-data-changed");
201 }
202 }
203 else
204 GTK_WIDGET_ACCESSIBLE_CLASS (gtk_notebook_accessible_parent_class)->notify_gtk (obj, pspec);
205 }
206
207 /*
208 * GtkNotebook only supports the selection of one page at a time.
209 * Selecting a page unselects any previous selection, so this
210 * changes the current selection instead of adding to it.
211 */
212 static gboolean
213 gtk_notebook_accessible_add_selection (AtkSelection *selection,
214 gint i)
215 {
216 GtkNotebook *notebook;
217 GtkWidget *widget;
218
219 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
220 if (widget == NULL)
221 return FALSE;
222
223 notebook = GTK_NOTEBOOK (widget);
224 gtk_notebook_set_current_page (notebook, i);
225 return TRUE;
226 }
227
228 static void
229 gtk_notebook_accessible_class_init (GtkNotebookAccessibleClass *klass)
230 {
231 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
232 AtkObjectClass *class = ATK_OBJECT_CLASS (klass);
233 GtkWidgetAccessibleClass *widget_class = (GtkWidgetAccessibleClass*)klass;
234 GtkContainerAccessibleClass *container_class = (GtkContainerAccessibleClass*)klass;
235
236 gobject_class->finalize = gtk_notebook_accessible_finalize;
237
238 class->ref_child = gtk_notebook_accessible_ref_child;
239 class->initialize = gtk_notebook_accessible_initialize;
240
241 widget_class->notify_gtk = gtk_notebook_accessible_notify_gtk;
242
243 /* we listen to page-added/-removed, so we don't care about these */
244 container_class->add_gtk = NULL;
245 container_class->remove_gtk = NULL;
246 }
247
248 static void
249 gtk_notebook_accessible_init (GtkNotebookAccessible *notebook)
250 {
251 notebook->priv = gtk_notebook_accessible_get_instance_private (notebook);
252 notebook->priv->pages = g_hash_table_new_full (g_direct_hash,
253 g_direct_equal,
254 NULL,
255 g_object_unref);
256 notebook->priv->selected_page = -1;
257 }
258
259 static AtkObject *
260 gtk_notebook_accessible_ref_selection (AtkSelection *selection,
261 gint i)
262 {
263 AtkObject *accessible;
264 GtkWidget *widget;
265 GtkNotebook *notebook;
266 gint pagenum;
267
268 if (i != 0)
269 return NULL;
270
271 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
272 if (widget == NULL)
273 return NULL;
274
275 notebook = GTK_NOTEBOOK (widget);
276 pagenum = gtk_notebook_get_current_page (notebook);
277 if (pagenum == -1)
278 return NULL;
279 accessible = gtk_notebook_accessible_ref_child (ATK_OBJECT (selection), pagenum);
280
281 return accessible;
282 }
283
284 /* Always return 1 because there can only be one page
285 * selected at any time
286 */
287 static gint
288 gtk_notebook_accessible_get_selection_count (AtkSelection *selection)
289 {
290 GtkWidget *widget;
291 GtkNotebook *notebook;
292
293 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
294 if (widget == NULL)
295 return 0;
296
297 notebook = GTK_NOTEBOOK (widget);
298 if (notebook == NULL || gtk_notebook_get_current_page (notebook) == -1)
299 return 0;
300
301 return 1;
302 }
303
304 static gboolean
305 gtk_notebook_accessible_is_child_selected (AtkSelection *selection,
306 gint i)
307 {
308 GtkWidget *widget;
309 GtkNotebook *notebook;
310 gint pagenumber;
311
312 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
313 if (widget == NULL)
314 return FALSE;
315
316 notebook = GTK_NOTEBOOK (widget);
317 pagenumber = gtk_notebook_get_current_page(notebook);
318
319 if (pagenumber == i)
320 return TRUE;
321
322 return FALSE;
323 }
324
325 static void
326 atk_selection_interface_init (AtkSelectionIface *iface)
327 {
328 iface->add_selection = gtk_notebook_accessible_add_selection;
329 iface->ref_selection = gtk_notebook_accessible_ref_selection;
330 iface->get_selection_count = gtk_notebook_accessible_get_selection_count;
331 iface->is_child_selected = gtk_notebook_accessible_is_child_selected;
332 }