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, see <http://www.gnu.org/licenses/>.
22 #include "gtknotebookaccessible.h"
23 #include "gtknotebookpageaccessible.h"
26 static void atk_selection_interface_init (AtkSelectionIface *iface);
28 G_DEFINE_TYPE_WITH_CODE (GtkNotebookAccessible, _gtk_notebook_accessible, GTK_TYPE_CONTAINER_ACCESSIBLE,
29 G_IMPLEMENT_INTERFACE (ATK_TYPE_SELECTION, atk_selection_interface_init))
32 check_focus_tab (gpointer data)
36 gint focus_page_num, old_focus_page_num;
37 GtkNotebookAccessible *accessible;
38 GtkNotebook *notebook;
40 atk_obj = ATK_OBJECT (data);
41 accessible = GTK_NOTEBOOK_ACCESSIBLE (atk_obj);
42 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 gtk_notebook_accessible_initialize (AtkObject *obj,
139 GtkNotebookAccessible *accessible;
140 GtkNotebook *notebook;
143 ATK_OBJECT_CLASS (_gtk_notebook_accessible_parent_class)->initialize (obj, data);
145 accessible = GTK_NOTEBOOK_ACCESSIBLE (obj);
146 notebook = GTK_NOTEBOOK (data);
147 for (i = 0; i < gtk_notebook_get_n_pages (notebook); i++)
149 create_notebook_page_accessible (accessible,
151 gtk_notebook_get_nth_page (notebook, i),
154 accessible->selected_page = gtk_notebook_get_current_page (notebook);
156 g_signal_connect (notebook, "focus",
157 G_CALLBACK (focus_cb), NULL);
158 g_signal_connect (notebook, "page-added",
159 G_CALLBACK (page_added_cb), NULL);
160 g_signal_connect (notebook, "page-removed",
161 G_CALLBACK (page_removed_cb), NULL);
163 obj->role = ATK_ROLE_PAGE_TAB_LIST;
167 gtk_notebook_accessible_finalize (GObject *object)
169 GtkNotebookAccessible *accessible = GTK_NOTEBOOK_ACCESSIBLE (object);
171 g_hash_table_destroy (accessible->pages);
173 if (accessible->idle_focus_id)
174 g_source_remove (accessible->idle_focus_id);
176 G_OBJECT_CLASS (_gtk_notebook_accessible_parent_class)->finalize (object);
180 gtk_notebook_accessible_ref_child (AtkObject *obj,
184 GtkNotebookAccessible *accessible;
185 GtkNotebook *notebook;
188 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (obj));
192 accessible = GTK_NOTEBOOK_ACCESSIBLE (obj);
193 notebook = GTK_NOTEBOOK (widget);
195 child = g_hash_table_lookup (accessible->pages,
196 gtk_notebook_get_nth_page (notebook, i));
197 /* can return NULL when i >= n_children */
200 g_object_ref (child);
206 gtk_notebook_accessible_notify_gtk (GObject *obj,
212 widget = GTK_WIDGET (obj);
213 atk_obj = gtk_widget_get_accessible (widget);
215 if (strcmp (pspec->name, "page") == 0)
217 gint page_num, old_page_num;
218 gint focus_page_num = 0;
219 gint old_focus_page_num;
220 GtkNotebookAccessible *accessible;
221 GtkNotebook *notebook;
223 accessible = GTK_NOTEBOOK_ACCESSIBLE (atk_obj);
224 notebook = GTK_NOTEBOOK (widget);
226 /* Notify SELECTED state change for old and new page */
227 old_page_num = accessible->selected_page;
228 page_num = gtk_notebook_get_current_page (notebook);
229 accessible->selected_page = page_num;
230 accessible->focus_tab_page = page_num;
231 old_focus_page_num = accessible->focus_tab_page;
233 if (page_num != old_page_num)
237 if (old_page_num != -1)
239 child = gtk_notebook_accessible_ref_child (atk_obj, old_page_num);
242 atk_object_notify_state_change (child, ATK_STATE_SELECTED, FALSE);
243 g_object_unref (child);
246 child = gtk_notebook_accessible_ref_child (atk_obj, page_num);
249 atk_object_notify_state_change (child, ATK_STATE_SELECTED, TRUE);
250 g_object_unref (child);
252 * The page which is being displayed has changed but there is
253 * no need to tell the focus tracker as the focus page will also
254 * change or a widget in the page will receive focus if the
255 * Notebook does not have tabs.
258 g_signal_emit_by_name (atk_obj, "selection-changed");
259 g_signal_emit_by_name (atk_obj, "visible-data-changed");
261 if (gtk_notebook_get_show_tabs (notebook) &&
262 (focus_page_num != old_focus_page_num))
264 if (accessible->idle_focus_id)
265 g_source_remove (accessible->idle_focus_id);
266 accessible->idle_focus_id = gdk_threads_add_idle (check_focus_tab, atk_obj);
270 GTK_WIDGET_ACCESSIBLE_CLASS (_gtk_notebook_accessible_parent_class)->notify_gtk (obj, pspec);
274 * GtkNotebook only supports the selection of one page at a time.
275 * Selecting a page unselects any previous selection, so this
276 * changes the current selection instead of adding to it.
279 gtk_notebook_accessible_add_selection (AtkSelection *selection,
282 GtkNotebook *notebook;
285 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
289 notebook = GTK_NOTEBOOK (widget);
290 gtk_notebook_set_current_page (notebook, i);
295 _gtk_notebook_accessible_class_init (GtkNotebookAccessibleClass *klass)
297 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
298 AtkObjectClass *class = ATK_OBJECT_CLASS (klass);
299 GtkWidgetAccessibleClass *widget_class = (GtkWidgetAccessibleClass*)klass;
300 GtkContainerAccessibleClass *container_class = (GtkContainerAccessibleClass*)klass;
303 gobject_class->finalize = gtk_notebook_accessible_finalize;
305 class->ref_child = gtk_notebook_accessible_ref_child;
306 class->initialize = gtk_notebook_accessible_initialize;
308 widget_class->notify_gtk = gtk_notebook_accessible_notify_gtk;
310 /* we listen to page-added/-removed, so we don't care about these */
311 container_class->add_gtk = NULL;
312 container_class->remove_gtk = NULL;
316 _gtk_notebook_accessible_init (GtkNotebookAccessible *notebook)
318 notebook->pages = g_hash_table_new_full (g_direct_hash,
322 notebook->selected_page = -1;
323 notebook->focus_tab_page = -1;
324 notebook->idle_focus_id = 0;
328 gtk_notebook_accessible_ref_selection (AtkSelection *selection,
331 AtkObject *accessible;
333 GtkNotebook *notebook;
339 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
343 notebook = GTK_NOTEBOOK (widget);
344 pagenum = gtk_notebook_get_current_page (notebook);
347 accessible = gtk_notebook_accessible_ref_child (ATK_OBJECT (selection), pagenum);
352 /* Always return 1 because there can only be one page
353 * selected at any time
356 gtk_notebook_accessible_get_selection_count (AtkSelection *selection)
359 GtkNotebook *notebook;
361 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
365 notebook = GTK_NOTEBOOK (widget);
366 if (notebook == NULL || gtk_notebook_get_current_page (notebook) == -1)
373 gtk_notebook_accessible_is_child_selected (AtkSelection *selection,
377 GtkNotebook *notebook;
380 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
384 notebook = GTK_NOTEBOOK (widget);
385 pagenumber = gtk_notebook_get_current_page(notebook);
394 atk_selection_interface_init (AtkSelectionIface *iface)
396 iface->add_selection = gtk_notebook_accessible_add_selection;
397 iface->ref_selection = gtk_notebook_accessible_ref_selection;
398 iface->get_selection_count = gtk_notebook_accessible_get_selection_count;
399 iface->is_child_selected = gtk_notebook_accessible_is_child_selected;