]> Pileus Git - ~andy/gtk/blob - gtk/a11y/gtknotebookaccessible.c
Convert GailNotebookPage to GtkNotebookPageAccessible
[~andy/gtk] / gtk / a11y / gtknotebookaccessible.c
1 /* GAIL - The GNOME Accessibility Implementation Library
2  * Copyright 2001, 2002, 2003 Sun Microsystems Inc.
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 #include "config.h"
21
22 #include <string.h>
23 #include <gtk/gtk.h>
24 #include "gtknotebookaccessible.h"
25 #include "gtknotebookpageaccessible.h"
26
27
28 static void atk_selection_interface_init (AtkSelectionIface *iface);
29
30 G_DEFINE_TYPE_WITH_CODE (GtkNotebookAccessible, gtk_notebook_accessible, GAIL_TYPE_CONTAINER,
31                          G_IMPLEMENT_INTERFACE (ATK_TYPE_SELECTION, atk_selection_interface_init))
32
33 static gboolean
34 check_focus_tab (gpointer data)
35 {
36   GtkWidget *widget;
37   AtkObject *atk_obj;
38   gint focus_page_num, old_focus_page_num;
39   GtkNotebookAccessible *accessible;
40   GtkNotebook *notebook;
41
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);
46
47   accessible->idle_focus_id = 0;
48
49   focus_page_num = gtk_notebook_get_current_page (notebook);
50   if (focus_page_num == -1)
51     return FALSE;
52
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)
56     {
57       AtkObject *obj;
58
59       obj = atk_object_ref_accessible_child (atk_obj, focus_page_num);
60       atk_focus_tracker_notify (obj);
61       g_object_unref (obj);
62     }
63
64   return FALSE;
65 }
66
67 static gboolean
68 focus_cb (GtkWidget        *widget,
69           GtkDirectionType  type)
70 {
71   AtkObject *atk_obj = gtk_widget_get_accessible (widget);
72   GtkNotebookAccessible *accessible = GTK_NOTEBOOK_ACCESSIBLE (atk_obj);
73
74   switch (type)
75     {
76     case GTK_DIR_LEFT:
77     case GTK_DIR_RIGHT:
78       if (accessible->idle_focus_id == 0)
79         accessible->idle_focus_id = gdk_threads_add_idle (check_focus_tab, atk_obj);
80       break;
81     default:
82       break;
83     }
84   return FALSE;
85 }
86
87 static void
88 create_notebook_page_accessible (GtkNotebookAccessible *accessible,
89                                  GtkNotebook           *notebook,
90                                  GtkWidget             *child,
91                                  gint                   page_num)
92 {
93   AtkObject *obj;
94
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);
99 }
100
101 static void
102 page_added_cb (GtkNotebook *notebook,
103                GtkWidget   *child,
104                guint        page_num,
105                gpointer     data)
106 {
107   AtkObject *atk_obj;
108   GtkNotebookAccessible *accessible;
109
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);
113 }
114
115 static void
116 page_removed_cb (GtkNotebook *notebook,
117                  GtkWidget   *widget,
118                  guint        page_num,
119                  gpointer     data)
120 {
121   GtkNotebookAccessible *accessible;
122   AtkObject *obj;
123
124   accessible = GTK_NOTEBOOK_ACCESSIBLE (gtk_widget_get_accessible (GTK_WIDGET (notebook)));
125
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);
132 }
133
134
135 static void
136 accessible_destroyed (gpointer data)
137 {
138   GtkNotebookAccessible *accessible = GTK_NOTEBOOK_ACCESSIBLE (data);
139
140   if (accessible->idle_focus_id)
141     {
142       g_source_remove (accessible->idle_focus_id);
143       accessible->idle_focus_id = 0;
144     }
145 }
146 static void
147 gtk_notebook_accessible_initialize (AtkObject *obj,
148                                     gpointer   data)
149 {
150   GtkNotebookAccessible *accessible;
151   GtkNotebook *notebook;
152   gint i;
153
154   ATK_OBJECT_CLASS (gtk_notebook_accessible_parent_class)->initialize (obj, data);
155
156   accessible = GTK_NOTEBOOK_ACCESSIBLE (obj);
157   notebook = GTK_NOTEBOOK (data);
158   for (i = 0; i < gtk_notebook_get_n_pages (notebook); i++)
159     {
160       create_notebook_page_accessible (accessible,
161                                        notebook,
162                                        gtk_notebook_get_nth_page (notebook, i),
163                                        i);
164     }
165   accessible->selected_page = gtk_notebook_get_current_page (notebook);
166
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);
173
174   g_object_weak_ref (G_OBJECT (notebook), (GWeakNotify)accessible_destroyed, obj);
175
176   obj->role = ATK_ROLE_PAGE_TAB_LIST;
177 }
178
179 static void
180 gtk_notebook_accessible_finalize (GObject *object)
181 {
182   GtkNotebookAccessible *accessible = GTK_NOTEBOOK_ACCESSIBLE (object);
183
184   g_hash_table_destroy (accessible->pages);
185
186   if (accessible->idle_focus_id)
187     g_source_remove (accessible->idle_focus_id);
188
189   G_OBJECT_CLASS (gtk_notebook_accessible_parent_class)->finalize (object);
190 }
191
192 static AtkObject *
193 gtk_notebook_accessible_ref_child (AtkObject *obj,
194                                    gint       i)
195 {
196   AtkObject *child;
197   GtkNotebookAccessible *accessible;
198   GtkNotebook *notebook;
199   GtkWidget *widget;
200  
201   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (obj));
202   if (widget == NULL)
203     return NULL;
204
205   accessible = GTK_NOTEBOOK_ACCESSIBLE (obj);
206   notebook = GTK_NOTEBOOK (widget);
207
208   child = g_hash_table_lookup (accessible->pages,
209                                gtk_notebook_get_nth_page (notebook, i));
210   /* can return NULL when i >= n_children */
211
212   if (child)
213     g_object_ref (child);
214
215   return child;
216 }
217
218 static void
219 gtk_notebook_accessible_notify_gtk (GObject    *obj,
220                                     GParamSpec *pspec)
221 {
222   GtkWidget *widget;
223   AtkObject* atk_obj;
224
225   widget = GTK_WIDGET (obj);
226   atk_obj = gtk_widget_get_accessible (widget);
227
228   if (strcmp (pspec->name, "page") == 0)
229     {
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;
235
236       accessible = GTK_NOTEBOOK_ACCESSIBLE (atk_obj);
237       notebook = GTK_NOTEBOOK (widget);
238
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;
245
246       if (page_num != old_page_num)
247         {
248           AtkObject *obj;
249
250           if (old_page_num != -1)
251             {
252               obj = gtk_notebook_accessible_ref_child (atk_obj, old_page_num);
253               if (obj)
254                 {
255                   atk_object_notify_state_change (obj, ATK_STATE_SELECTED, FALSE);
256                   g_object_unref (obj);
257                 }
258             }
259           obj = gtk_notebook_accessible_ref_child (atk_obj, page_num);
260           if (obj)
261             {
262               atk_object_notify_state_change (obj, ATK_STATE_SELECTED, TRUE);
263               g_object_unref (obj);
264               /*
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.
269                */
270             }
271           g_signal_emit_by_name (atk_obj, "selection_changed");
272           g_signal_emit_by_name (atk_obj, "visible_data_changed");
273         }
274       if (gtk_notebook_get_show_tabs (notebook) &&
275          (focus_page_num != old_focus_page_num))
276         {
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);
280         }
281     }
282   else
283     GAIL_WIDGET_CLASS (gtk_notebook_accessible_parent_class)->notify_gtk (obj, pspec);
284 }
285
286 /*
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.
290  */
291 static gboolean
292 gtk_notebook_accessible_add_selection (AtkSelection *selection,
293                                        gint          i)
294 {
295   GtkNotebook *notebook;
296   GtkWidget *widget;
297
298   widget =  gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
299   if (widget == NULL)
300     return FALSE;
301
302   notebook = GTK_NOTEBOOK (widget);
303   gtk_notebook_set_current_page (notebook, i);
304   return TRUE;
305 }
306
307 static void
308 gtk_notebook_accessible_class_init (GtkNotebookAccessibleClass *klass)
309 {
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;
314
315
316   gobject_class->finalize = gtk_notebook_accessible_finalize;
317
318   class->ref_child = gtk_notebook_accessible_ref_child;
319   class->initialize = gtk_notebook_accessible_initialize;
320
321   widget_class->notify_gtk = gtk_notebook_accessible_notify_gtk;
322
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;
326 }
327
328 static void
329 gtk_notebook_accessible_init (GtkNotebookAccessible *notebook)
330 {
331   notebook->pages = g_hash_table_new_full (g_direct_hash,
332                                            g_direct_equal,
333                                            NULL,
334                                            g_object_unref);
335   notebook->selected_page = -1;
336   notebook->focus_tab_page = -1;
337   notebook->idle_focus_id = 0;
338 }
339
340 static AtkObject *
341 gtk_notebook_accessible_ref_selection (AtkSelection *selection,
342                                        gint          i)
343 {
344   AtkObject *accessible;
345   GtkWidget *widget;
346   GtkNotebook *notebook;
347   gint pagenum;
348
349   if (i != 0)
350     return NULL;
351
352   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
353   if (widget == NULL)
354     return NULL;
355
356   notebook = GTK_NOTEBOOK (widget);
357   pagenum = gtk_notebook_get_current_page (notebook);
358   if (pagenum == -1)
359     return NULL;
360   accessible = gtk_notebook_accessible_ref_child (ATK_OBJECT (selection), pagenum);
361
362   return accessible;
363 }
364
365 /* Always return 1 because there can only be one page
366  * selected at any time
367  */
368 static gint
369 gtk_notebook_accessible_get_selection_count (AtkSelection *selection)
370 {
371   GtkWidget *widget;
372   GtkNotebook *notebook;
373
374   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
375   if (widget == NULL)
376     return 0;
377
378   notebook = GTK_NOTEBOOK (widget);
379   if (notebook == NULL || gtk_notebook_get_current_page (notebook) == -1)
380     return 0;
381
382   return 1;
383 }
384
385 static gboolean
386 gtk_notebook_accessible_is_child_selected (AtkSelection *selection,
387                                            gint          i)
388 {
389   GtkWidget *widget;
390   GtkNotebook *notebook;
391   gint pagenumber;
392
393   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
394   if (widget == NULL)
395     return FALSE;
396
397   notebook = GTK_NOTEBOOK (widget);
398   pagenumber = gtk_notebook_get_current_page(notebook);
399
400   if (pagenumber == i)
401     return TRUE;
402
403   return FALSE;
404 }
405
406 static void
407 atk_selection_interface_init (AtkSelectionIface *iface)
408 {
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;
413 }