]> Pileus Git - ~andy/gtk/blob - gtk/a11y/gtknotebookaccessible.c
filechooser: Show FUSE mounted locations in shortcuts
[~andy/gtk] / gtk / a11y / gtknotebookaccessible.c
1 /* GTK+ - accessibility implementations
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, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include "config.h"
19
20 #include <string.h>
21 #include <gtk/gtk.h>
22 #include "gtknotebookaccessible.h"
23 #include "gtknotebookpageaccessible.h"
24
25 struct _GtkNotebookAccessiblePrivate
26 {
27   /*
28    * page_cache maintains a list of pre-ref'd Notebook Pages.
29    * This cache is queried by gtk_notebook_accessible_ref_child().
30    * If the page is found in the list then a new page does not
31    * need to be created
32    */
33   GHashTable * pages;
34   gint         selected_page;
35   gint         focus_tab_page;
36   guint        idle_focus_id;
37 };
38
39 static void atk_selection_interface_init (AtkSelectionIface *iface);
40
41 G_DEFINE_TYPE_WITH_CODE (GtkNotebookAccessible, gtk_notebook_accessible, GTK_TYPE_CONTAINER_ACCESSIBLE,
42                          G_IMPLEMENT_INTERFACE (ATK_TYPE_SELECTION, atk_selection_interface_init))
43
44 static gboolean
45 check_focus_tab (gpointer data)
46 {
47   GtkWidget *widget;
48   AtkObject *atk_obj;
49   gint focus_page_num, old_focus_page_num;
50   GtkNotebookAccessible *accessible;
51   GtkNotebook *notebook;
52
53   atk_obj = ATK_OBJECT (data);
54   accessible = GTK_NOTEBOOK_ACCESSIBLE (atk_obj);
55   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (atk_obj));
56   if (widget == NULL)
57     return FALSE;
58   notebook = GTK_NOTEBOOK (widget);
59
60   accessible->priv->idle_focus_id = 0;
61
62   focus_page_num = gtk_notebook_get_current_page (notebook);
63   if (focus_page_num == -1)
64     return FALSE;
65
66   old_focus_page_num = accessible->priv->focus_tab_page;
67   accessible->priv->focus_tab_page = focus_page_num;
68   if (old_focus_page_num != focus_page_num)
69     {
70       AtkObject *obj;
71
72       obj = atk_object_ref_accessible_child (atk_obj, focus_page_num);
73       atk_focus_tracker_notify (obj);
74       g_object_unref (obj);
75     }
76
77   return FALSE;
78 }
79
80 static gboolean
81 focus_cb (GtkWidget        *widget,
82           GtkDirectionType  type)
83 {
84   AtkObject *atk_obj = gtk_widget_get_accessible (widget);
85   GtkNotebookAccessible *accessible = GTK_NOTEBOOK_ACCESSIBLE (atk_obj);
86
87   switch (type)
88     {
89     case GTK_DIR_LEFT:
90     case GTK_DIR_RIGHT:
91       if (accessible->priv->idle_focus_id == 0)
92         accessible->priv->idle_focus_id = gdk_threads_add_idle (check_focus_tab, atk_obj);
93       break;
94     default:
95       break;
96     }
97   return FALSE;
98 }
99
100 static void
101 create_notebook_page_accessible (GtkNotebookAccessible *accessible,
102                                  GtkNotebook           *notebook,
103                                  GtkWidget             *child,
104                                  gint                   page_num)
105 {
106   AtkObject *obj;
107
108   obj = gtk_notebook_page_accessible_new (accessible, child);
109   g_hash_table_insert (accessible->priv->pages, child, obj);
110   atk_object_set_parent (obj, ATK_OBJECT (accessible));
111   g_signal_emit_by_name (accessible, "children-changed::add", page_num, obj, NULL);
112 }
113
114 static void
115 page_added_cb (GtkNotebook *notebook,
116                GtkWidget   *child,
117                guint        page_num,
118                gpointer     data)
119 {
120   AtkObject *atk_obj;
121   GtkNotebookAccessible *accessible;
122
123   atk_obj = gtk_widget_get_accessible (GTK_WIDGET (notebook));
124   accessible = GTK_NOTEBOOK_ACCESSIBLE (atk_obj);
125   create_notebook_page_accessible (accessible, notebook, child, page_num);
126 }
127
128 static void
129 page_removed_cb (GtkNotebook *notebook,
130                  GtkWidget   *widget,
131                  guint        page_num,
132                  gpointer     data)
133 {
134   GtkNotebookAccessible *accessible;
135   AtkObject *obj;
136
137   accessible = GTK_NOTEBOOK_ACCESSIBLE (gtk_widget_get_accessible (GTK_WIDGET (notebook)));
138
139   obj = g_hash_table_lookup (accessible->priv->pages, widget);
140   g_return_if_fail (obj);
141   g_signal_emit_by_name (accessible, "children-changed::remove",
142                          page_num, obj, NULL);
143   gtk_notebook_page_accessible_invalidate (GTK_NOTEBOOK_PAGE_ACCESSIBLE (obj));
144   g_hash_table_remove (accessible->priv->pages, widget);
145 }
146
147
148 static void
149 gtk_notebook_accessible_initialize (AtkObject *obj,
150                                     gpointer   data)
151 {
152   GtkNotebookAccessible *accessible;
153   GtkNotebook *notebook;
154   gint i;
155
156   ATK_OBJECT_CLASS (gtk_notebook_accessible_parent_class)->initialize (obj, data);
157
158   accessible = GTK_NOTEBOOK_ACCESSIBLE (obj);
159   notebook = GTK_NOTEBOOK (data);
160   for (i = 0; i < gtk_notebook_get_n_pages (notebook); i++)
161     {
162       create_notebook_page_accessible (accessible,
163                                        notebook,
164                                        gtk_notebook_get_nth_page (notebook, i),
165                                        i);
166     }
167   accessible->priv->selected_page = gtk_notebook_get_current_page (notebook);
168
169   g_signal_connect (notebook, "focus",
170                     G_CALLBACK (focus_cb), NULL);
171   g_signal_connect (notebook, "page-added",
172                     G_CALLBACK (page_added_cb), NULL);
173   g_signal_connect (notebook, "page-removed",
174                     G_CALLBACK (page_removed_cb), NULL);
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->priv->pages);
185
186   if (accessible->priv->idle_focus_id)
187     g_source_remove (accessible->priv->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->priv->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->priv->selected_page;
241       page_num = gtk_notebook_get_current_page (notebook);
242       accessible->priv->selected_page = page_num;
243       accessible->priv->focus_tab_page = page_num;
244       old_focus_page_num = accessible->priv->focus_tab_page;
245
246       if (page_num != old_page_num)
247         {
248           AtkObject *child;
249
250           if (old_page_num != -1)
251             {
252               child = gtk_notebook_accessible_ref_child (atk_obj, old_page_num);
253               if (child)
254                 {
255                   atk_object_notify_state_change (child, ATK_STATE_SELECTED, FALSE);
256                   g_object_unref (child);
257                 }
258             }
259           child = gtk_notebook_accessible_ref_child (atk_obj, page_num);
260           if (child)
261             {
262               atk_object_notify_state_change (child, ATK_STATE_SELECTED, TRUE);
263               g_object_unref (child);
264               /*
265                * The page which is being displayed has changed but there
266                * is no need to tell the focus tracker as the focus page
267                * will also change or a widget in the page will receive
268                * focus if the 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->priv->idle_focus_id)
278             g_source_remove (accessible->priv->idle_focus_id);
279           accessible->priv->idle_focus_id = gdk_threads_add_idle (check_focus_tab, atk_obj);
280         }
281     }
282   else
283     GTK_WIDGET_ACCESSIBLE_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   GtkWidgetAccessibleClass *widget_class = (GtkWidgetAccessibleClass*)klass;
313   GtkContainerAccessibleClass *container_class = (GtkContainerAccessibleClass*)klass;
314
315   gobject_class->finalize = gtk_notebook_accessible_finalize;
316
317   class->ref_child = gtk_notebook_accessible_ref_child;
318   class->initialize = gtk_notebook_accessible_initialize;
319
320   widget_class->notify_gtk = gtk_notebook_accessible_notify_gtk;
321
322   /* we listen to page-added/-removed, so we don't care about these */
323   container_class->add_gtk = NULL;
324   container_class->remove_gtk = NULL;
325
326   g_type_class_add_private (klass, sizeof (GtkNotebookAccessiblePrivate));
327 }
328
329 static void
330 gtk_notebook_accessible_init (GtkNotebookAccessible *notebook)
331 {
332   notebook->priv = G_TYPE_INSTANCE_GET_PRIVATE (notebook,
333                                                 GTK_TYPE_NOTEBOOK_ACCESSIBLE,
334                                                 GtkNotebookAccessiblePrivate);
335   notebook->priv->pages = g_hash_table_new_full (g_direct_hash,
336                                                  g_direct_equal,
337                                                  NULL,
338                                                  g_object_unref);
339   notebook->priv->selected_page = -1;
340   notebook->priv->focus_tab_page = -1;
341   notebook->priv->idle_focus_id = 0;
342 }
343
344 static AtkObject *
345 gtk_notebook_accessible_ref_selection (AtkSelection *selection,
346                                        gint          i)
347 {
348   AtkObject *accessible;
349   GtkWidget *widget;
350   GtkNotebook *notebook;
351   gint pagenum;
352
353   if (i != 0)
354     return NULL;
355
356   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
357   if (widget == NULL)
358     return NULL;
359
360   notebook = GTK_NOTEBOOK (widget);
361   pagenum = gtk_notebook_get_current_page (notebook);
362   if (pagenum == -1)
363     return NULL;
364   accessible = gtk_notebook_accessible_ref_child (ATK_OBJECT (selection), pagenum);
365
366   return accessible;
367 }
368
369 /* Always return 1 because there can only be one page
370  * selected at any time
371  */
372 static gint
373 gtk_notebook_accessible_get_selection_count (AtkSelection *selection)
374 {
375   GtkWidget *widget;
376   GtkNotebook *notebook;
377
378   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
379   if (widget == NULL)
380     return 0;
381
382   notebook = GTK_NOTEBOOK (widget);
383   if (notebook == NULL || gtk_notebook_get_current_page (notebook) == -1)
384     return 0;
385
386   return 1;
387 }
388
389 static gboolean
390 gtk_notebook_accessible_is_child_selected (AtkSelection *selection,
391                                            gint          i)
392 {
393   GtkWidget *widget;
394   GtkNotebook *notebook;
395   gint pagenumber;
396
397   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
398   if (widget == NULL)
399     return FALSE;
400
401   notebook = GTK_NOTEBOOK (widget);
402   pagenumber = gtk_notebook_get_current_page(notebook);
403
404   if (pagenumber == i)
405     return TRUE;
406
407   return FALSE;
408 }
409
410 static void
411 atk_selection_interface_init (AtkSelectionIface *iface)
412 {
413   iface->add_selection = gtk_notebook_accessible_add_selection;
414   iface->ref_selection = gtk_notebook_accessible_ref_selection;
415   iface->get_selection_count = gtk_notebook_accessible_get_selection_count;
416   iface->is_child_selected = gtk_notebook_accessible_is_child_selected;
417 }