1 /* GAIL - The GNOME Accessibility Implementation Library
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, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
24 #include "gtknotebookaccessible.h"
25 #include "gtknotebookpageaccessible.h"
28 static void atk_selection_interface_init (AtkSelectionIface *iface);
30 G_DEFINE_TYPE_WITH_CODE (GtkNotebookAccessible, _gtk_notebook_accessible, GTK_TYPE_CONTAINER_ACCESSIBLE,
31 G_IMPLEMENT_INTERFACE (ATK_TYPE_SELECTION, atk_selection_interface_init))
34 check_focus_tab (gpointer data)
38 gint focus_page_num, old_focus_page_num;
39 GtkNotebookAccessible *accessible;
40 GtkNotebook *notebook;
42 atk_obj = ATK_OBJECT (data);
43 accessible = GTK_NOTEBOOK_ACCESSIBLE (atk_obj);
44 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (atk_obj));
47 notebook = GTK_NOTEBOOK (widget);
49 accessible->idle_focus_id = 0;
51 focus_page_num = gtk_notebook_get_current_page (notebook);
52 if (focus_page_num == -1)
55 old_focus_page_num = accessible->focus_tab_page;
56 accessible->focus_tab_page = focus_page_num;
57 if (old_focus_page_num != focus_page_num)
61 obj = atk_object_ref_accessible_child (atk_obj, focus_page_num);
62 atk_focus_tracker_notify (obj);
70 focus_cb (GtkWidget *widget,
71 GtkDirectionType type)
73 AtkObject *atk_obj = gtk_widget_get_accessible (widget);
74 GtkNotebookAccessible *accessible = GTK_NOTEBOOK_ACCESSIBLE (atk_obj);
80 if (accessible->idle_focus_id == 0)
81 accessible->idle_focus_id = gdk_threads_add_idle (check_focus_tab, atk_obj);
90 create_notebook_page_accessible (GtkNotebookAccessible *accessible,
91 GtkNotebook *notebook,
97 obj = _gtk_notebook_page_accessible_new (accessible, child);
98 g_hash_table_insert (accessible->pages, child, obj);
99 atk_object_set_parent (obj, ATK_OBJECT (accessible));
100 g_signal_emit_by_name (accessible, "children-changed::add", page_num, obj, NULL);
104 page_added_cb (GtkNotebook *notebook,
110 GtkNotebookAccessible *accessible;
112 atk_obj = gtk_widget_get_accessible (GTK_WIDGET (notebook));
113 accessible = GTK_NOTEBOOK_ACCESSIBLE (atk_obj);
114 create_notebook_page_accessible (accessible, notebook, child, page_num);
118 page_removed_cb (GtkNotebook *notebook,
123 GtkNotebookAccessible *accessible;
126 accessible = GTK_NOTEBOOK_ACCESSIBLE (gtk_widget_get_accessible (GTK_WIDGET (notebook)));
128 obj = g_hash_table_lookup (accessible->pages, widget);
129 g_return_if_fail (obj);
130 g_signal_emit_by_name (accessible, "children-changed::remove",
131 page_num, obj, NULL);
132 _gtk_notebook_page_accessible_invalidate (GTK_NOTEBOOK_PAGE_ACCESSIBLE (obj));
133 g_hash_table_remove (accessible->pages, widget);
138 gtk_notebook_accessible_initialize (AtkObject *obj,
141 GtkNotebookAccessible *accessible;
142 GtkNotebook *notebook;
145 ATK_OBJECT_CLASS (_gtk_notebook_accessible_parent_class)->initialize (obj, data);
147 accessible = GTK_NOTEBOOK_ACCESSIBLE (obj);
148 notebook = GTK_NOTEBOOK (data);
149 for (i = 0; i < gtk_notebook_get_n_pages (notebook); i++)
151 create_notebook_page_accessible (accessible,
153 gtk_notebook_get_nth_page (notebook, i),
156 accessible->selected_page = gtk_notebook_get_current_page (notebook);
158 g_signal_connect (notebook, "focus",
159 G_CALLBACK (focus_cb), NULL);
160 g_signal_connect (notebook, "page-added",
161 G_CALLBACK (page_added_cb), NULL);
162 g_signal_connect (notebook, "page-removed",
163 G_CALLBACK (page_removed_cb), NULL);
165 obj->role = ATK_ROLE_PAGE_TAB_LIST;
169 gtk_notebook_accessible_finalize (GObject *object)
171 GtkNotebookAccessible *accessible = GTK_NOTEBOOK_ACCESSIBLE (object);
173 g_hash_table_destroy (accessible->pages);
175 if (accessible->idle_focus_id)
176 g_source_remove (accessible->idle_focus_id);
178 G_OBJECT_CLASS (_gtk_notebook_accessible_parent_class)->finalize (object);
182 gtk_notebook_accessible_ref_child (AtkObject *obj,
186 GtkNotebookAccessible *accessible;
187 GtkNotebook *notebook;
190 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (obj));
194 accessible = GTK_NOTEBOOK_ACCESSIBLE (obj);
195 notebook = GTK_NOTEBOOK (widget);
197 child = g_hash_table_lookup (accessible->pages,
198 gtk_notebook_get_nth_page (notebook, i));
199 /* can return NULL when i >= n_children */
202 g_object_ref (child);
208 gtk_notebook_accessible_notify_gtk (GObject *obj,
214 widget = GTK_WIDGET (obj);
215 atk_obj = gtk_widget_get_accessible (widget);
217 if (strcmp (pspec->name, "page") == 0)
219 gint page_num, old_page_num;
220 gint focus_page_num = 0;
221 gint old_focus_page_num;
222 GtkNotebookAccessible *accessible;
223 GtkNotebook *notebook;
225 accessible = GTK_NOTEBOOK_ACCESSIBLE (atk_obj);
226 notebook = GTK_NOTEBOOK (widget);
228 /* Notify SELECTED state change for old and new page */
229 old_page_num = accessible->selected_page;
230 page_num = gtk_notebook_get_current_page (notebook);
231 accessible->selected_page = page_num;
232 accessible->focus_tab_page = page_num;
233 old_focus_page_num = accessible->focus_tab_page;
235 if (page_num != old_page_num)
239 if (old_page_num != -1)
241 child = gtk_notebook_accessible_ref_child (atk_obj, old_page_num);
244 atk_object_notify_state_change (child, ATK_STATE_SELECTED, FALSE);
245 g_object_unref (child);
248 child = gtk_notebook_accessible_ref_child (atk_obj, page_num);
251 atk_object_notify_state_change (child, ATK_STATE_SELECTED, TRUE);
252 g_object_unref (child);
254 * The page which is being displayed has changed but there is
255 * no need to tell the focus tracker as the focus page will also
256 * change or a widget in the page will receive focus if the
257 * Notebook does not have tabs.
260 g_signal_emit_by_name (atk_obj, "selection-changed");
261 g_signal_emit_by_name (atk_obj, "visible-data-changed");
263 if (gtk_notebook_get_show_tabs (notebook) &&
264 (focus_page_num != old_focus_page_num))
266 if (accessible->idle_focus_id)
267 g_source_remove (accessible->idle_focus_id);
268 accessible->idle_focus_id = gdk_threads_add_idle (check_focus_tab, atk_obj);
272 GTK_WIDGET_ACCESSIBLE_CLASS (_gtk_notebook_accessible_parent_class)->notify_gtk (obj, pspec);
276 * GtkNotebook only supports the selection of one page at a time.
277 * Selecting a page unselects any previous selection, so this
278 * changes the current selection instead of adding to it.
281 gtk_notebook_accessible_add_selection (AtkSelection *selection,
284 GtkNotebook *notebook;
287 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
291 notebook = GTK_NOTEBOOK (widget);
292 gtk_notebook_set_current_page (notebook, i);
297 _gtk_notebook_accessible_class_init (GtkNotebookAccessibleClass *klass)
299 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
300 AtkObjectClass *class = ATK_OBJECT_CLASS (klass);
301 GtkWidgetAccessibleClass *widget_class = (GtkWidgetAccessibleClass*)klass;
302 GtkContainerAccessibleClass *container_class = (GtkContainerAccessibleClass*)klass;
305 gobject_class->finalize = gtk_notebook_accessible_finalize;
307 class->ref_child = gtk_notebook_accessible_ref_child;
308 class->initialize = gtk_notebook_accessible_initialize;
310 widget_class->notify_gtk = gtk_notebook_accessible_notify_gtk;
312 /* we listen to page-added/-removed, so we don't care about these */
313 container_class->add_gtk = NULL;
314 container_class->remove_gtk = NULL;
318 _gtk_notebook_accessible_init (GtkNotebookAccessible *notebook)
320 notebook->pages = g_hash_table_new_full (g_direct_hash,
324 notebook->selected_page = -1;
325 notebook->focus_tab_page = -1;
326 notebook->idle_focus_id = 0;
330 gtk_notebook_accessible_ref_selection (AtkSelection *selection,
333 AtkObject *accessible;
335 GtkNotebook *notebook;
341 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
345 notebook = GTK_NOTEBOOK (widget);
346 pagenum = gtk_notebook_get_current_page (notebook);
349 accessible = gtk_notebook_accessible_ref_child (ATK_OBJECT (selection), pagenum);
354 /* Always return 1 because there can only be one page
355 * selected at any time
358 gtk_notebook_accessible_get_selection_count (AtkSelection *selection)
361 GtkNotebook *notebook;
363 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
367 notebook = GTK_NOTEBOOK (widget);
368 if (notebook == NULL || gtk_notebook_get_current_page (notebook) == -1)
375 gtk_notebook_accessible_is_child_selected (AtkSelection *selection,
379 GtkNotebook *notebook;
382 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
386 notebook = GTK_NOTEBOOK (widget);
387 pagenumber = gtk_notebook_get_current_page(notebook);
396 atk_selection_interface_init (AtkSelectionIface *iface)
398 iface->add_selection = gtk_notebook_accessible_add_selection;
399 iface->ref_selection = gtk_notebook_accessible_ref_selection;
400 iface->get_selection_count = gtk_notebook_accessible_get_selection_count;
401 iface->is_child_selected = gtk_notebook_accessible_is_child_selected;