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 "gailnotebook.h"
25 #include "gailnotebookpage.h"
26 #include "gail-private-macros.h"
28 static void gail_notebook_class_init (GailNotebookClass *klass);
29 static void gail_notebook_init (GailNotebook *notebook);
30 static void gail_notebook_finalize (GObject *object);
31 static void gail_notebook_real_initialize (AtkObject *obj,
34 static void gail_notebook_real_notify_gtk (GObject *obj,
37 static AtkObject* gail_notebook_ref_child (AtkObject *obj,
39 static gint gail_notebook_real_remove_gtk (GtkContainer *container,
42 static void atk_selection_interface_init (AtkSelectionIface *iface);
43 static gboolean gail_notebook_add_selection (AtkSelection *selection,
45 static AtkObject* gail_notebook_ref_selection (AtkSelection *selection,
47 static gint gail_notebook_get_selection_count (AtkSelection *selection);
48 static gboolean gail_notebook_is_child_selected (AtkSelection *selection,
50 static AtkObject* find_child_in_list (GList *list,
52 static void check_cache (GailNotebook *gail_notebook,
53 GtkNotebook *notebook);
54 static void reset_cache (GailNotebook *gail_notebook,
56 static void create_notebook_page_accessible (GailNotebook *gail_notebook,
57 GtkNotebook *notebook,
59 gboolean insert_before,
61 static void gail_notebook_child_parent_set (GtkWidget *widget,
62 GtkWidget *old_parent,
64 static gboolean gail_notebook_focus_cb (GtkWidget *widget,
65 GtkDirectionType type);
66 static gboolean gail_notebook_check_focus_tab (gpointer data);
67 static void gail_notebook_destroyed (gpointer data);
70 G_DEFINE_TYPE_WITH_CODE (GailNotebook, gail_notebook, GAIL_TYPE_CONTAINER,
71 G_IMPLEMENT_INTERFACE (ATK_TYPE_SELECTION, atk_selection_interface_init))
74 gail_notebook_class_init (GailNotebookClass *klass)
76 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
77 AtkObjectClass *class = ATK_OBJECT_CLASS (klass);
78 GailWidgetClass *widget_class;
79 GailContainerClass *container_class;
81 widget_class = (GailWidgetClass*)klass;
82 container_class = (GailContainerClass*)klass;
84 gobject_class->finalize = gail_notebook_finalize;
86 widget_class->notify_gtk = gail_notebook_real_notify_gtk;
88 class->ref_child = gail_notebook_ref_child;
89 class->initialize = gail_notebook_real_initialize;
91 * We do not provide an implementation of get_n_children
92 * as the implementation in GailContainer returns the correct
95 container_class->remove_gtk = gail_notebook_real_remove_gtk;
99 gail_notebook_init (GailNotebook *notebook)
101 notebook->page_cache = NULL;
102 notebook->selected_page = -1;
103 notebook->focus_tab_page = -1;
104 notebook->remove_index = -1;
105 notebook->idle_focus_id = 0;
109 gail_notebook_ref_child (AtkObject *obj,
112 AtkObject *accessible = NULL;
113 GailNotebook *gail_notebook;
114 GtkNotebook *gtk_notebook;
117 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (obj));
124 gail_notebook = GAIL_NOTEBOOK (obj);
126 gtk_notebook = GTK_NOTEBOOK (widget);
128 if (gail_notebook->page_count < gtk_notebook_get_n_pages (gtk_notebook))
129 check_cache (gail_notebook, gtk_notebook);
131 accessible = find_child_in_list (gail_notebook->page_cache, i);
133 if (accessible != NULL)
134 g_object_ref (accessible);
140 gail_notebook_page_added (GtkNotebook *gtk_notebook,
146 GailNotebook *notebook;
148 atk_obj = gtk_widget_get_accessible (GTK_WIDGET (gtk_notebook));
149 notebook = GAIL_NOTEBOOK (atk_obj);
150 create_notebook_page_accessible (notebook, gtk_notebook, page_num, FALSE, NULL);
151 notebook->page_count++;
155 gail_notebook_real_initialize (AtkObject *obj,
158 GailNotebook *notebook;
159 GtkNotebook *gtk_notebook;
162 ATK_OBJECT_CLASS (gail_notebook_parent_class)->initialize (obj, data);
164 notebook = GAIL_NOTEBOOK (obj);
165 gtk_notebook = GTK_NOTEBOOK (data);
166 for (i = 0; i < gtk_notebook_get_n_pages (gtk_notebook); i++)
168 create_notebook_page_accessible (notebook, gtk_notebook, i, FALSE, NULL);
170 notebook->page_count = i;
171 notebook->selected_page = gtk_notebook_get_current_page (gtk_notebook);
173 g_signal_connect (gtk_notebook,
175 G_CALLBACK (gail_notebook_focus_cb),
177 g_signal_connect (gtk_notebook,
179 G_CALLBACK (gail_notebook_page_added),
181 g_object_weak_ref (G_OBJECT(gtk_notebook),
182 (GWeakNotify) gail_notebook_destroyed,
185 obj->role = ATK_ROLE_PAGE_TAB_LIST;
189 gail_notebook_real_notify_gtk (GObject *obj,
195 widget = GTK_WIDGET (obj);
196 atk_obj = gtk_widget_get_accessible (widget);
198 if (strcmp (pspec->name, "page") == 0)
200 gint page_num, old_page_num;
201 gint focus_page_num = 0;
202 gint old_focus_page_num;
203 GailNotebook *gail_notebook;
204 GtkNotebook *gtk_notebook;
206 gail_notebook = GAIL_NOTEBOOK (atk_obj);
207 gtk_notebook = GTK_NOTEBOOK (widget);
209 if (gail_notebook->page_count < gtk_notebook_get_n_pages (gtk_notebook))
210 check_cache (gail_notebook, gtk_notebook);
212 * Notify SELECTED state change for old and new page
214 old_page_num = gail_notebook->selected_page;
215 page_num = gtk_notebook_get_current_page (gtk_notebook);
216 gail_notebook->selected_page = page_num;
217 gail_notebook->focus_tab_page = page_num;
218 old_focus_page_num = gail_notebook->focus_tab_page;
220 if (page_num != old_page_num)
224 if (old_page_num != -1)
226 obj = gail_notebook_ref_child (atk_obj, old_page_num);
229 atk_object_notify_state_change (obj,
232 g_object_unref (obj);
235 obj = gail_notebook_ref_child (atk_obj, page_num);
238 atk_object_notify_state_change (obj,
241 g_object_unref (obj);
243 * The page which is being displayed has changed but there is
244 * no need to tell the focus tracker as the focus page will also
245 * change or a widget in the page will receive focus if the
246 * Notebook does not have tabs.
249 g_signal_emit_by_name (atk_obj, "selection_changed");
250 g_signal_emit_by_name (atk_obj, "visible_data_changed");
252 if (gtk_notebook_get_show_tabs (gtk_notebook) &&
253 (focus_page_num != old_focus_page_num))
255 if (gail_notebook->idle_focus_id)
256 g_source_remove (gail_notebook->idle_focus_id);
257 gail_notebook->idle_focus_id = gdk_threads_add_idle (gail_notebook_check_focus_tab, atk_obj);
261 GAIL_WIDGET_CLASS (gail_notebook_parent_class)->notify_gtk (obj, pspec);
265 gail_notebook_finalize (GObject *object)
267 GailNotebook *notebook = GAIL_NOTEBOOK (object);
271 * Get rid of the GailNotebookPage objects which we have cached.
273 list = notebook->page_cache;
278 g_object_unref (list->data);
283 g_list_free (notebook->page_cache);
285 if (notebook->idle_focus_id)
286 g_source_remove (notebook->idle_focus_id);
288 G_OBJECT_CLASS (gail_notebook_parent_class)->finalize (object);
292 atk_selection_interface_init (AtkSelectionIface *iface)
294 iface->add_selection = gail_notebook_add_selection;
295 iface->ref_selection = gail_notebook_ref_selection;
296 iface->get_selection_count = gail_notebook_get_selection_count;
297 iface->is_child_selected = gail_notebook_is_child_selected;
299 * The following don't make any sense for GtkNotebook widgets.
300 * Unsupported AtkSelection interfaces:
302 * remove_selection();
303 * select_all_selection();
308 * GtkNotebook only supports the selection of one page at a time.
309 * Selecting a page unselects any previous selection, so this
310 * changes the current selection instead of adding to it.
313 gail_notebook_add_selection (AtkSelection *selection,
316 GtkNotebook *notebook;
319 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
326 notebook = GTK_NOTEBOOK (widget);
327 gtk_notebook_set_current_page (notebook, i);
332 gail_notebook_ref_selection (AtkSelection *selection,
335 AtkObject *accessible;
337 GtkNotebook *notebook;
341 * A note book can have only one selection.
343 gail_return_val_if_fail (i == 0, NULL);
344 g_return_val_if_fail (GAIL_IS_NOTEBOOK (selection), NULL);
346 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
348 /* State is defunct */
351 notebook = GTK_NOTEBOOK (widget);
352 pagenum = gtk_notebook_get_current_page (notebook);
353 gail_return_val_if_fail (pagenum != -1, NULL);
354 accessible = gail_notebook_ref_child (ATK_OBJECT (selection), pagenum);
360 * Always return 1 because there can only be one page
361 * selected at any time
364 gail_notebook_get_selection_count (AtkSelection *selection)
367 GtkNotebook *notebook;
369 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
376 notebook = GTK_NOTEBOOK (widget);
377 if (notebook == NULL || gtk_notebook_get_current_page (notebook) == -1)
384 gail_notebook_is_child_selected (AtkSelection *selection,
388 GtkNotebook *notebook;
391 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
399 notebook = GTK_NOTEBOOK (widget);
400 pagenumber = gtk_notebook_get_current_page(notebook);
409 find_child_in_list (GList *list,
412 AtkObject *obj = NULL;
416 if (GAIL_NOTEBOOK_PAGE (list->data)->index == index)
418 obj = ATK_OBJECT (list->data);
427 check_cache (GailNotebook *gail_notebook,
428 GtkNotebook *notebook)
434 gtk_list = gtk_container_get_children (GTK_CONTAINER (notebook));
435 gail_list = gail_notebook->page_cache;
442 create_notebook_page_accessible (gail_notebook, notebook, i, FALSE, NULL);
444 else if (GAIL_NOTEBOOK_PAGE (gail_list->data)->page != gtk_list->data)
446 create_notebook_page_accessible (gail_notebook, notebook, i, TRUE, gail_list);
450 gail_list = gail_list->next;
453 gtk_list = gtk_list->next;
455 g_list_free (gtk_list);
457 gail_notebook->page_count = i;
461 reset_cache (GailNotebook *gail_notebook,
466 for (l = gail_notebook->page_cache; l; l = l->next)
468 if (GAIL_NOTEBOOK_PAGE (l->data)->index > index)
469 GAIL_NOTEBOOK_PAGE (l->data)->index -= 1;
474 create_notebook_page_accessible (GailNotebook *gail_notebook,
475 GtkNotebook *notebook,
477 gboolean insert_before,
482 obj = gail_notebook_page_new (notebook, index);
485 gail_notebook->page_cache = g_list_insert_before (gail_notebook->page_cache, list, obj);
487 gail_notebook->page_cache = g_list_append (gail_notebook->page_cache, obj);
488 g_signal_connect (gtk_notebook_get_nth_page (notebook, index),
490 G_CALLBACK (gail_notebook_child_parent_set),
495 gail_notebook_child_parent_set (GtkWidget *widget,
496 GtkWidget *old_parent,
499 GailNotebook *gail_notebook;
501 gail_return_if_fail (old_parent != NULL);
502 gail_notebook = GAIL_NOTEBOOK (gtk_widget_get_accessible (old_parent));
503 gail_notebook->remove_index = GAIL_NOTEBOOK_PAGE (data)->index;
507 gail_notebook_real_remove_gtk (GtkContainer *container,
511 GailNotebook *gail_notebook;
515 g_return_val_if_fail (container != NULL, 1);
516 gail_notebook = GAIL_NOTEBOOK (gtk_widget_get_accessible (GTK_WIDGET (container)));
517 index = gail_notebook->remove_index;
518 gail_notebook->remove_index = -1;
520 obj = find_child_in_list (gail_notebook->page_cache, index);
521 g_return_val_if_fail (obj, 1);
522 gail_notebook->page_cache = g_list_remove (gail_notebook->page_cache, obj);
523 gail_notebook->page_count -= 1;
524 reset_cache (gail_notebook, index);
525 g_signal_emit_by_name (gail_notebook,
526 "children_changed::remove",
527 GAIL_NOTEBOOK_PAGE (obj)->index,
529 g_object_unref (obj);
534 gail_notebook_focus_cb (GtkWidget *widget,
535 GtkDirectionType type)
537 AtkObject *atk_obj = gtk_widget_get_accessible (widget);
538 GailNotebook *gail_notebook = GAIL_NOTEBOOK (atk_obj);
544 if (gail_notebook->idle_focus_id == 0)
545 gail_notebook->idle_focus_id = gdk_threads_add_idle (gail_notebook_check_focus_tab, atk_obj);
554 gail_notebook_check_focus_tab (gpointer data)
558 gint focus_page_num, old_focus_page_num;
559 GailNotebook *gail_notebook;
560 GtkNotebook *gtk_notebook;
562 atk_obj = ATK_OBJECT (data);
563 gail_notebook = GAIL_NOTEBOOK (atk_obj);
564 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (atk_obj));
566 gtk_notebook = GTK_NOTEBOOK (widget);
568 gail_notebook->idle_focus_id = 0;
570 focus_page_num = gtk_notebook_get_current_page (gtk_notebook);
571 if (focus_page_num == -1)
574 old_focus_page_num = gail_notebook->focus_tab_page;
575 gail_notebook->focus_tab_page = focus_page_num;
576 if (old_focus_page_num != focus_page_num)
580 obj = atk_object_ref_accessible_child (atk_obj, focus_page_num);
581 atk_focus_tracker_notify (obj);
582 g_object_unref (obj);
589 gail_notebook_destroyed (gpointer data)
591 GailNotebook *gail_notebook = GAIL_NOTEBOOK (data);
593 if (gail_notebook->idle_focus_id)
595 g_source_remove (gail_notebook->idle_focus_id);
596 gail_notebook->idle_focus_id = 0;