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, GAIL_TYPE_CONTAINER,
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));
45 notebook = GTK_NOTEBOOK (widget);
47 accessible->idle_focus_id = 0;
49 focus_page_num = gtk_notebook_get_current_page (notebook);
50 if (focus_page_num == -1)
53 old_focus_page_num = accessible->focus_tab_page;
54 accessible->focus_tab_page = focus_page_num;
55 if (old_focus_page_num != focus_page_num)
59 obj = atk_object_ref_accessible_child (atk_obj, focus_page_num);
60 atk_focus_tracker_notify (obj);
68 focus_cb (GtkWidget *widget,
69 GtkDirectionType type)
71 AtkObject *atk_obj = gtk_widget_get_accessible (widget);
72 GtkNotebookAccessible *accessible = GTK_NOTEBOOK_ACCESSIBLE (atk_obj);
78 if (accessible->idle_focus_id == 0)
79 accessible->idle_focus_id = gdk_threads_add_idle (check_focus_tab, atk_obj);
88 create_notebook_page_accessible (GtkNotebookAccessible *accessible,
89 GtkNotebook *notebook,
95 obj = gtk_notebook_page_accessible_new (accessible, child);
96 g_hash_table_insert (accessible->pages, child, obj);
97 atk_object_set_parent (obj, ATK_OBJECT (accessible));
98 g_signal_emit_by_name (accessible, "children_changed::add", page_num, obj, NULL);
102 page_added_cb (GtkNotebook *notebook,
108 GtkNotebookAccessible *accessible;
110 atk_obj = gtk_widget_get_accessible (GTK_WIDGET (notebook));
111 accessible = GTK_NOTEBOOK_ACCESSIBLE (atk_obj);
112 create_notebook_page_accessible (accessible, notebook, child, page_num);
116 page_removed_cb (GtkNotebook *notebook,
121 GtkNotebookAccessible *accessible;
124 accessible = GTK_NOTEBOOK_ACCESSIBLE (gtk_widget_get_accessible (GTK_WIDGET (notebook)));
126 obj = g_hash_table_lookup (accessible->pages, widget);
127 g_return_if_fail (obj);
128 g_signal_emit_by_name (accessible, "children_changed::remove",
129 page_num, obj, NULL);
130 gtk_notebook_page_accessible_invalidate (GTK_NOTEBOOK_PAGE_ACCESSIBLE (obj));
131 g_hash_table_remove (accessible->pages, widget);
136 accessible_destroyed (gpointer data)
138 GtkNotebookAccessible *accessible = GTK_NOTEBOOK_ACCESSIBLE (data);
140 if (accessible->idle_focus_id)
142 g_source_remove (accessible->idle_focus_id);
143 accessible->idle_focus_id = 0;
147 gtk_notebook_accessible_initialize (AtkObject *obj,
150 GtkNotebookAccessible *accessible;
151 GtkNotebook *notebook;
154 ATK_OBJECT_CLASS (gtk_notebook_accessible_parent_class)->initialize (obj, data);
156 accessible = GTK_NOTEBOOK_ACCESSIBLE (obj);
157 notebook = GTK_NOTEBOOK (data);
158 for (i = 0; i < gtk_notebook_get_n_pages (notebook); i++)
160 create_notebook_page_accessible (accessible,
162 gtk_notebook_get_nth_page (notebook, i),
165 accessible->selected_page = gtk_notebook_get_current_page (notebook);
167 g_signal_connect (notebook, "focus",
168 G_CALLBACK (focus_cb), NULL);
169 g_signal_connect (notebook, "page-added",
170 G_CALLBACK (page_added_cb), NULL);
171 g_signal_connect (notebook, "page-removed",
172 G_CALLBACK (page_removed_cb), NULL);
174 g_object_weak_ref (G_OBJECT (notebook), (GWeakNotify)accessible_destroyed, obj);
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->pages);
186 if (accessible->idle_focus_id)
187 g_source_remove (accessible->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->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->selected_page;
241 page_num = gtk_notebook_get_current_page (notebook);
242 accessible->selected_page = page_num;
243 accessible->focus_tab_page = page_num;
244 old_focus_page_num = accessible->focus_tab_page;
246 if (page_num != old_page_num)
250 if (old_page_num != -1)
252 obj = gtk_notebook_accessible_ref_child (atk_obj, old_page_num);
255 atk_object_notify_state_change (obj, ATK_STATE_SELECTED, FALSE);
256 g_object_unref (obj);
259 obj = gtk_notebook_accessible_ref_child (atk_obj, page_num);
262 atk_object_notify_state_change (obj, ATK_STATE_SELECTED, TRUE);
263 g_object_unref (obj);
265 * The page which is being displayed has changed but there is
266 * no need to tell the focus tracker as the focus page will also
267 * change or a widget in the page will receive focus if the
268 * 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->idle_focus_id)
278 g_source_remove (accessible->idle_focus_id);
279 accessible->idle_focus_id = gdk_threads_add_idle (check_focus_tab, atk_obj);
283 GAIL_WIDGET_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 GailWidgetClass *widget_class = (GailWidgetClass*)klass;
313 GailContainerClass *container_class = (GailContainerClass*)klass;
316 gobject_class->finalize = gtk_notebook_accessible_finalize;
318 class->ref_child = gtk_notebook_accessible_ref_child;
319 class->initialize = gtk_notebook_accessible_initialize;
321 widget_class->notify_gtk = gtk_notebook_accessible_notify_gtk;
323 /* we listen to page-added/-removed, so we don't care about these */
324 container_class->add_gtk = NULL;
325 container_class->remove_gtk = NULL;
329 gtk_notebook_accessible_init (GtkNotebookAccessible *notebook)
331 notebook->pages = g_hash_table_new_full (g_direct_hash,
335 notebook->selected_page = -1;
336 notebook->focus_tab_page = -1;
337 notebook->idle_focus_id = 0;
341 gtk_notebook_accessible_ref_selection (AtkSelection *selection,
344 AtkObject *accessible;
346 GtkNotebook *notebook;
352 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
356 notebook = GTK_NOTEBOOK (widget);
357 pagenum = gtk_notebook_get_current_page (notebook);
360 accessible = gtk_notebook_accessible_ref_child (ATK_OBJECT (selection), pagenum);
365 /* Always return 1 because there can only be one page
366 * selected at any time
369 gtk_notebook_accessible_get_selection_count (AtkSelection *selection)
372 GtkNotebook *notebook;
374 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
378 notebook = GTK_NOTEBOOK (widget);
379 if (notebook == NULL || gtk_notebook_get_current_page (notebook) == -1)
386 gtk_notebook_accessible_is_child_selected (AtkSelection *selection,
390 GtkNotebook *notebook;
393 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
397 notebook = GTK_NOTEBOOK (widget);
398 pagenumber = gtk_notebook_get_current_page(notebook);
407 atk_selection_interface_init (AtkSelectionIface *iface)
409 iface->add_selection = gtk_notebook_accessible_add_selection;
410 iface->ref_selection = gtk_notebook_accessible_ref_selection;
411 iface->get_selection_count = gtk_notebook_accessible_get_selection_count;
412 iface->is_child_selected = gtk_notebook_accessible_is_child_selected;