summaryrefslogtreecommitdiff
path: root/gtk/a11y/gtknotebookaccessible.c
blob: 77b8f1fdd5ba5a3bdadd0745bcd0ba43ada585ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/* GAIL - The GNOME Accessibility Implementation Library
 * Copyright 2001, 2002, 2003 Sun Microsystems Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 */

#include "config.h"

#include <string.h>
#include <gtk/gtk.h>
#include "gtknotebookaccessible.h"
#include "gtknotebookpageaccessible.h"

struct _GtkNotebookAccessiblePrivate
{
  /*
   * page_cache maintains a list of pre-ref'd Notebook Pages.
   * This cache is queried by gtk_notebook_accessible_ref_child().
   * If the page is found in the list then a new page does not
   * need to be created
   */
  GHashTable * pages;
  gint         selected_page;
  gint         focus_tab_page;
  guint        idle_focus_id;
};

static void atk_selection_interface_init (AtkSelectionIface *iface);

G_DEFINE_TYPE_WITH_CODE (GtkNotebookAccessible, _gtk_notebook_accessible, GTK_TYPE_CONTAINER_ACCESSIBLE,
                         G_IMPLEMENT_INTERFACE (ATK_TYPE_SELECTION, atk_selection_interface_init))

static gboolean
check_focus_tab (gpointer data)
{
  GtkWidget *widget;
  AtkObject *atk_obj;
  gint focus_page_num, old_focus_page_num;
  GtkNotebookAccessible *accessible;
  GtkNotebook *notebook;

  atk_obj = ATK_OBJECT (data);
  accessible = GTK_NOTEBOOK_ACCESSIBLE (atk_obj);
  widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (atk_obj));
  if (widget == NULL)
    return FALSE;
  notebook = GTK_NOTEBOOK (widget);

  accessible->priv->idle_focus_id = 0;

  focus_page_num = gtk_notebook_get_current_page (notebook);
  if (focus_page_num == -1)
    return FALSE;

  old_focus_page_num = accessible->priv->focus_tab_page;
  accessible->priv->focus_tab_page = focus_page_num;
  if (old_focus_page_num != focus_page_num)
    {
      AtkObject *obj;

      obj = atk_object_ref_accessible_child (atk_obj, focus_page_num);
      atk_focus_tracker_notify (obj);
      g_object_unref (obj);
    }

  return FALSE;
}

static gboolean
focus_cb (GtkWidget        *widget,
          GtkDirectionType  type)
{
  AtkObject *atk_obj = gtk_widget_get_accessible (widget);
  GtkNotebookAccessible *accessible = GTK_NOTEBOOK_ACCESSIBLE (atk_obj);

  switch (type)
    {
    case GTK_DIR_LEFT:
    case GTK_DIR_RIGHT:
      if (accessible->priv->idle_focus_id == 0)
        accessible->priv->idle_focus_id = gdk_threads_add_idle (check_focus_tab, atk_obj);
      break;
    default:
      break;
    }
  return FALSE;
}

static void
create_notebook_page_accessible (GtkNotebookAccessible *accessible,
                                 GtkNotebook           *notebook,
                                 GtkWidget             *child,
                                 gint                   page_num)
{
  AtkObject *obj;

  obj = _gtk_notebook_page_accessible_new (accessible, child);
  g_hash_table_insert (accessible->priv->pages, child, obj);
  atk_object_set_parent (obj, ATK_OBJECT (accessible));
  g_signal_emit_by_name (accessible, "children-changed::add", page_num, obj, NULL);
}

static void
page_added_cb (GtkNotebook *notebook,
               GtkWidget   *child,
               guint        page_num,
               gpointer     data)
{
  AtkObject *atk_obj;
  GtkNotebookAccessible *accessible;

  atk_obj = gtk_widget_get_accessible (GTK_WIDGET (notebook));
  accessible = GTK_NOTEBOOK_ACCESSIBLE (atk_obj);
  create_notebook_page_accessible (accessible, notebook, child, page_num);
}

static void
page_removed_cb (GtkNotebook *notebook,
                 GtkWidget   *widget,
                 guint        page_num,
                 gpointer     data)
{
  GtkNotebookAccessible *accessible;
  AtkObject *obj;

  accessible = GTK_NOTEBOOK_ACCESSIBLE (gtk_widget_get_accessible (GTK_WIDGET (notebook)));

  obj = g_hash_table_lookup (accessible->priv->pages, widget);
  g_return_if_fail (obj);
  g_signal_emit_by_name (accessible, "children-changed::remove",
                         page_num, obj, NULL);
  _gtk_notebook_page_accessible_invalidate (GTK_NOTEBOOK_PAGE_ACCESSIBLE (obj));
  g_hash_table_remove (accessible->priv->pages, widget);
}


static void
gtk_notebook_accessible_initialize (AtkObject *obj,
                                    gpointer   data)
{
  GtkNotebookAccessible *accessible;
  GtkNotebook *notebook;
  gint i;

  ATK_OBJECT_CLASS (_gtk_notebook_accessible_parent_class)->initialize (obj, data);

  accessible = GTK_NOTEBOOK_ACCESSIBLE (obj);
  notebook = GTK_NOTEBOOK (data);
  for (i = 0; i < gtk_notebook_get_n_pages (notebook); i++)
    {
      create_notebook_page_accessible (accessible,
                                       notebook,
                                       gtk_notebook_get_nth_page (notebook, i),
                                       i);
    }
  accessible->priv->selected_page = gtk_notebook_get_current_page (notebook);

  g_signal_connect (notebook, "focus",
                    G_CALLBACK (focus_cb), NULL);
  g_signal_connect (notebook, "page-added",
                    G_CALLBACK (page_added_cb), NULL);
  g_signal_connect (notebook, "page-removed",
                    G_CALLBACK (page_removed_cb), NULL);

  obj->role = ATK_ROLE_PAGE_TAB_LIST;
}

static void
gtk_notebook_accessible_finalize (GObject *object)
{
  GtkNotebookAccessible *accessible = GTK_NOTEBOOK_ACCESSIBLE (object);

  g_hash_table_destroy (accessible->priv->pages);

  if (accessible->priv->idle_focus_id)
    g_source_remove (accessible->priv->idle_focus_id);

  G_OBJECT_CLASS (_gtk_notebook_accessible_parent_class)->finalize (object);
}

static AtkObject *
gtk_notebook_accessible_ref_child (AtkObject *obj,
                                   gint       i)
{
  AtkObject *child;
  GtkNotebookAccessible *accessible;
  GtkNotebook *notebook;
  GtkWidget *widget;
 
  widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (obj));
  if (widget == NULL)
    return NULL;

  accessible = GTK_NOTEBOOK_ACCESSIBLE (obj);
  notebook = GTK_NOTEBOOK (widget);

  child = g_hash_table_lookup (accessible->priv->pages,
                               gtk_notebook_get_nth_page (notebook, i));
  /* can return NULL when i >= n_children */

  if (child)
    g_object_ref (child);

  return child;
}

static void
gtk_notebook_accessible_notify_gtk (GObject    *obj,
                                    GParamSpec *pspec)
{
  GtkWidget *widget;
  AtkObject* atk_obj;

  widget = GTK_WIDGET (obj);
  atk_obj = gtk_widget_get_accessible (widget);

  if (strcmp (pspec->name, "page") == 0)
    {
      gint page_num, old_page_num;
      gint focus_page_num = 0;
      gint old_focus_page_num;
      GtkNotebookAccessible *accessible;
      GtkNotebook *notebook;

      accessible = GTK_NOTEBOOK_ACCESSIBLE (atk_obj);
      notebook = GTK_NOTEBOOK (widget);

      /* Notify SELECTED state change for old and new page */
      old_page_num = accessible->priv->selected_page;
      page_num = gtk_notebook_get_current_page (notebook);
      accessible->priv->selected_page = page_num;
      accessible->priv->focus_tab_page = page_num;
      old_focus_page_num = accessible->priv->focus_tab_page;

      if (page_num != old_page_num)
        {
          AtkObject *child;

          if (old_page_num != -1)
            {
              child = gtk_notebook_accessible_ref_child (atk_obj, old_page_num);
              if (child)
                {
                  atk_object_notify_state_change (child, ATK_STATE_SELECTED, FALSE);
                  g_object_unref (child);
                }
            }
          child = gtk_notebook_accessible_ref_child (atk_obj, page_num);
          if (child)
            {
              atk_object_notify_state_change (child, ATK_STATE_SELECTED, TRUE);
              g_object_unref (child);
              /*
               * The page which is being displayed has changed but there
               * is no need to tell the focus tracker as the focus page
               * will also change or a widget in the page will receive
               * focus if the notebook does not have tabs.
               */
            }
          g_signal_emit_by_name (atk_obj, "selection-changed");
          g_signal_emit_by_name (atk_obj, "visible-data-changed");
        }
      if (gtk_notebook_get_show_tabs (notebook) &&
         (focus_page_num != old_focus_page_num))
        {
          if (accessible->priv->idle_focus_id)
            g_source_remove (accessible->priv->idle_focus_id);
          accessible->priv->idle_focus_id = gdk_threads_add_idle (check_focus_tab, atk_obj);
        }
    }
  else
    GTK_WIDGET_ACCESSIBLE_CLASS (_gtk_notebook_accessible_parent_class)->notify_gtk (obj, pspec);
}

/*
 * GtkNotebook only supports the selection of one page at a time.
 * Selecting a page unselects any previous selection, so this
 * changes the current selection instead of adding to it.
 */
static gboolean
gtk_notebook_accessible_add_selection (AtkSelection *selection,
                                       gint          i)
{
  GtkNotebook *notebook;
  GtkWidget *widget;

  widget =  gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
  if (widget == NULL)
    return FALSE;

  notebook = GTK_NOTEBOOK (widget);
  gtk_notebook_set_current_page (notebook, i);
  return TRUE;
}

static void
_gtk_notebook_accessible_class_init (GtkNotebookAccessibleClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
  AtkObjectClass  *class = ATK_OBJECT_CLASS (klass);
  GtkWidgetAccessibleClass *widget_class = (GtkWidgetAccessibleClass*)klass;
  GtkContainerAccessibleClass *container_class = (GtkContainerAccessibleClass*)klass;

  gobject_class->finalize = gtk_notebook_accessible_finalize;

  class->ref_child = gtk_notebook_accessible_ref_child;
  class->initialize = gtk_notebook_accessible_initialize;

  widget_class->notify_gtk = gtk_notebook_accessible_notify_gtk;

  /* we listen to page-added/-removed, so we don't care about these */
  container_class->add_gtk = NULL;
  container_class->remove_gtk = NULL;

  g_type_class_add_private (klass, sizeof (GtkNotebookAccessiblePrivate));
}

static void
_gtk_notebook_accessible_init (GtkNotebookAccessible *notebook)
{
  notebook->priv = G_TYPE_INSTANCE_GET_PRIVATE (notebook,
                                                GTK_TYPE_NOTEBOOK_ACCESSIBLE,
                                                GtkNotebookAccessiblePrivate);
  notebook->priv->pages = g_hash_table_new_full (g_direct_hash,
                                                 g_direct_equal,
                                                 NULL,
                                                 g_object_unref);
  notebook->priv->selected_page = -1;
  notebook->priv->focus_tab_page = -1;
  notebook->priv->idle_focus_id = 0;
}

static AtkObject *
gtk_notebook_accessible_ref_selection (AtkSelection *selection,
                                       gint          i)
{
  AtkObject *accessible;
  GtkWidget *widget;
  GtkNotebook *notebook;
  gint pagenum;

  if (i != 0)
    return NULL;

  widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
  if (widget == NULL)
    return NULL;

  notebook = GTK_NOTEBOOK (widget);
  pagenum = gtk_notebook_get_current_page (notebook);
  if (pagenum == -1)
    return NULL;
  accessible = gtk_notebook_accessible_ref_child (ATK_OBJECT (selection), pagenum);

  return accessible;
}

/* Always return 1 because there can only be one page
 * selected at any time
 */
static gint
gtk_notebook_accessible_get_selection_count (AtkSelection *selection)
{
  GtkWidget *widget;
  GtkNotebook *notebook;

  widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
  if (widget == NULL)
    return 0;

  notebook = GTK_NOTEBOOK (widget);
  if (notebook == NULL || gtk_notebook_get_current_page (notebook) == -1)
    return 0;

  return 1;
}

static gboolean
gtk_notebook_accessible_is_child_selected (AtkSelection *selection,
                                           gint          i)
{
  GtkWidget *widget;
  GtkNotebook *notebook;
  gint pagenumber;

  widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
  if (widget == NULL)
    return FALSE;

  notebook = GTK_NOTEBOOK (widget);
  pagenumber = gtk_notebook_get_current_page(notebook);

  if (pagenumber == i)
    return TRUE;

  return FALSE;
}

static void
atk_selection_interface_init (AtkSelectionIface *iface)
{
  iface->add_selection = gtk_notebook_accessible_add_selection;
  iface->ref_selection = gtk_notebook_accessible_ref_selection;
  iface->get_selection_count = gtk_notebook_accessible_get_selection_count;
  iface->is_child_selected = gtk_notebook_accessible_is_child_selected;
}