1 /* GTK+ - accessibility implementations
2 * Copyright 2001, 2002, 2003 Sun Microsystems Inc.
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.
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 Public
15 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
22 #include "gtknotebookaccessible.h"
23 #include "gtknotebookpageaccessible.h"
25 struct _GtkNotebookAccessiblePrivate
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
39 static void atk_selection_interface_init (AtkSelectionIface *iface);
41 G_DEFINE_TYPE_WITH_CODE (GtkNotebookAccessible, gtk_notebook_accessible, GTK_TYPE_CONTAINER_ACCESSIBLE,
42 G_IMPLEMENT_INTERFACE (ATK_TYPE_SELECTION, atk_selection_interface_init))
45 check_focus_tab (gpointer data)
49 gint focus_page_num, old_focus_page_num;
50 GtkNotebookAccessible *accessible;
51 GtkNotebook *notebook;
53 atk_obj = ATK_OBJECT (data);
54 accessible = GTK_NOTEBOOK_ACCESSIBLE (atk_obj);
55 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (atk_obj));
58 notebook = GTK_NOTEBOOK (widget);
60 accessible->priv->idle_focus_id = 0;
62 focus_page_num = gtk_notebook_get_current_page (notebook);
63 if (focus_page_num == -1)
66 old_focus_page_num = accessible->priv->focus_tab_page;
67 accessible->priv->focus_tab_page = focus_page_num;
68 if (old_focus_page_num != focus_page_num)
72 obj = atk_object_ref_accessible_child (atk_obj, focus_page_num);
73 atk_focus_tracker_notify (obj);
81 focus_cb (GtkWidget *widget,
82 GtkDirectionType type)
84 AtkObject *atk_obj = gtk_widget_get_accessible (widget);
85 GtkNotebookAccessible *accessible = GTK_NOTEBOOK_ACCESSIBLE (atk_obj);
91 if (accessible->priv->idle_focus_id == 0)
92 accessible->priv->idle_focus_id = gdk_threads_add_idle (check_focus_tab, atk_obj);
101 create_notebook_page_accessible (GtkNotebookAccessible *accessible,
102 GtkNotebook *notebook,
108 obj = gtk_notebook_page_accessible_new (accessible, child);
109 g_hash_table_insert (accessible->priv->pages, child, obj);
110 atk_object_set_parent (obj, ATK_OBJECT (accessible));
111 g_signal_emit_by_name (accessible, "children-changed::add", page_num, obj, NULL);
115 page_added_cb (GtkNotebook *notebook,
121 GtkNotebookAccessible *accessible;
123 atk_obj = gtk_widget_get_accessible (GTK_WIDGET (notebook));
124 accessible = GTK_NOTEBOOK_ACCESSIBLE (atk_obj);
125 create_notebook_page_accessible (accessible, notebook, child, page_num);
129 page_removed_cb (GtkNotebook *notebook,
134 GtkNotebookAccessible *accessible;
137 accessible = GTK_NOTEBOOK_ACCESSIBLE (gtk_widget_get_accessible (GTK_WIDGET (notebook)));
139 obj = g_hash_table_lookup (accessible->priv->pages, widget);
140 g_return_if_fail (obj);
141 g_signal_emit_by_name (accessible, "children-changed::remove",
142 page_num, obj, NULL);
143 gtk_notebook_page_accessible_invalidate (GTK_NOTEBOOK_PAGE_ACCESSIBLE (obj));
144 g_hash_table_remove (accessible->priv->pages, widget);
149 gtk_notebook_accessible_initialize (AtkObject *obj,
152 GtkNotebookAccessible *accessible;
153 GtkNotebook *notebook;
156 ATK_OBJECT_CLASS (gtk_notebook_accessible_parent_class)->initialize (obj, data);
158 accessible = GTK_NOTEBOOK_ACCESSIBLE (obj);
159 notebook = GTK_NOTEBOOK (data);
160 for (i = 0; i < gtk_notebook_get_n_pages (notebook); i++)
162 create_notebook_page_accessible (accessible,
164 gtk_notebook_get_nth_page (notebook, i),
167 accessible->priv->selected_page = gtk_notebook_get_current_page (notebook);
169 g_signal_connect (notebook, "focus",
170 G_CALLBACK (focus_cb), NULL);
171 g_signal_connect (notebook, "page-added",
172 G_CALLBACK (page_added_cb), NULL);
173 g_signal_connect (notebook, "page-removed",
174 G_CALLBACK (page_removed_cb), NULL);
176 obj->role = ATK_ROLE_PAGE_TAB_LIST;
180 gtk_notebook_accessible_finalize (GObject *object)
182 GtkNotebookAccessible *accessible = GTK_NOTEBOOK_ACCESSIBLE (object);
184 g_hash_table_destroy (accessible->priv->pages);
186 if (accessible->priv->idle_focus_id)
187 g_source_remove (accessible->priv->idle_focus_id);
189 G_OBJECT_CLASS (gtk_notebook_accessible_parent_class)->finalize (object);
193 gtk_notebook_accessible_ref_child (AtkObject *obj,
197 GtkNotebookAccessible *accessible;
198 GtkNotebook *notebook;
201 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (obj));
205 accessible = GTK_NOTEBOOK_ACCESSIBLE (obj);
206 notebook = GTK_NOTEBOOK (widget);
208 child = g_hash_table_lookup (accessible->priv->pages,
209 gtk_notebook_get_nth_page (notebook, i));
210 /* can return NULL when i >= n_children */
213 g_object_ref (child);
219 gtk_notebook_accessible_notify_gtk (GObject *obj,
225 widget = GTK_WIDGET (obj);
226 atk_obj = gtk_widget_get_accessible (widget);
228 if (strcmp (pspec->name, "page") == 0)
230 gint page_num, old_page_num;
231 gint focus_page_num = 0;
232 gint old_focus_page_num;
233 GtkNotebookAccessible *accessible;
234 GtkNotebook *notebook;
236 accessible = GTK_NOTEBOOK_ACCESSIBLE (atk_obj);
237 notebook = GTK_NOTEBOOK (widget);
239 /* Notify SELECTED state change for old and new page */
240 old_page_num = accessible->priv->selected_page;
241 page_num = gtk_notebook_get_current_page (notebook);
242 accessible->priv->selected_page = page_num;
243 accessible->priv->focus_tab_page = page_num;
244 old_focus_page_num = accessible->priv->focus_tab_page;
246 if (page_num != old_page_num)
250 if (old_page_num != -1)
252 child = gtk_notebook_accessible_ref_child (atk_obj, old_page_num);
255 atk_object_notify_state_change (child, ATK_STATE_SELECTED, FALSE);
256 g_object_unref (child);
259 child = gtk_notebook_accessible_ref_child (atk_obj, page_num);
262 atk_object_notify_state_change (child, ATK_STATE_SELECTED, TRUE);
263 g_object_unref (child);
265 * The page which is being displayed has changed but there
266 * is no need to tell the focus tracker as the focus page
267 * will also change or a widget in the page will receive
268 * focus if the notebook does not have tabs.
271 g_signal_emit_by_name (atk_obj, "selection-changed");
272 g_signal_emit_by_name (atk_obj, "visible-data-changed");
274 if (gtk_notebook_get_show_tabs (notebook) &&
275 (focus_page_num != old_focus_page_num))
277 if (accessible->priv->idle_focus_id)
278 g_source_remove (accessible->priv->idle_focus_id);
279 accessible->priv->idle_focus_id = gdk_threads_add_idle (check_focus_tab, atk_obj);
283 GTK_WIDGET_ACCESSIBLE_CLASS (gtk_notebook_accessible_parent_class)->notify_gtk (obj, pspec);
287 * GtkNotebook only supports the selection of one page at a time.
288 * Selecting a page unselects any previous selection, so this
289 * changes the current selection instead of adding to it.
292 gtk_notebook_accessible_add_selection (AtkSelection *selection,
295 GtkNotebook *notebook;
298 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
302 notebook = GTK_NOTEBOOK (widget);
303 gtk_notebook_set_current_page (notebook, i);
308 gtk_notebook_accessible_class_init (GtkNotebookAccessibleClass *klass)
310 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
311 AtkObjectClass *class = ATK_OBJECT_CLASS (klass);
312 GtkWidgetAccessibleClass *widget_class = (GtkWidgetAccessibleClass*)klass;
313 GtkContainerAccessibleClass *container_class = (GtkContainerAccessibleClass*)klass;
315 gobject_class->finalize = gtk_notebook_accessible_finalize;
317 class->ref_child = gtk_notebook_accessible_ref_child;
318 class->initialize = gtk_notebook_accessible_initialize;
320 widget_class->notify_gtk = gtk_notebook_accessible_notify_gtk;
322 /* we listen to page-added/-removed, so we don't care about these */
323 container_class->add_gtk = NULL;
324 container_class->remove_gtk = NULL;
326 g_type_class_add_private (klass, sizeof (GtkNotebookAccessiblePrivate));
330 gtk_notebook_accessible_init (GtkNotebookAccessible *notebook)
332 notebook->priv = G_TYPE_INSTANCE_GET_PRIVATE (notebook,
333 GTK_TYPE_NOTEBOOK_ACCESSIBLE,
334 GtkNotebookAccessiblePrivate);
335 notebook->priv->pages = g_hash_table_new_full (g_direct_hash,
339 notebook->priv->selected_page = -1;
340 notebook->priv->focus_tab_page = -1;
341 notebook->priv->idle_focus_id = 0;
345 gtk_notebook_accessible_ref_selection (AtkSelection *selection,
348 AtkObject *accessible;
350 GtkNotebook *notebook;
356 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
360 notebook = GTK_NOTEBOOK (widget);
361 pagenum = gtk_notebook_get_current_page (notebook);
364 accessible = gtk_notebook_accessible_ref_child (ATK_OBJECT (selection), pagenum);
369 /* Always return 1 because there can only be one page
370 * selected at any time
373 gtk_notebook_accessible_get_selection_count (AtkSelection *selection)
376 GtkNotebook *notebook;
378 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
382 notebook = GTK_NOTEBOOK (widget);
383 if (notebook == NULL || gtk_notebook_get_current_page (notebook) == -1)
390 gtk_notebook_accessible_is_child_selected (AtkSelection *selection,
394 GtkNotebook *notebook;
397 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
401 notebook = GTK_NOTEBOOK (widget);
402 pagenumber = gtk_notebook_get_current_page(notebook);
411 atk_selection_interface_init (AtkSelectionIface *iface)
413 iface->add_selection = gtk_notebook_accessible_add_selection;
414 iface->ref_selection = gtk_notebook_accessible_ref_selection;
415 iface->get_selection_count = gtk_notebook_accessible_get_selection_count;
416 iface->is_child_selected = gtk_notebook_accessible_is_child_selected;