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
/* CTK+ - accessibility implementations
 * 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 <ctk/ctk.h>
#include "ctknotebookaccessible.h"
#include "ctknotebookpageaccessible.h"

struct _CtkNotebookAccessiblePrivate
{
  /*
   * page_cache maintains a list of pre-ref'd Notebook Pages.
   * This cache is queried by ctk_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;
};

static void atk_selection_interface_init (AtkSelectionIface *iface);

G_DEFINE_TYPE_WITH_CODE (CtkNotebookAccessible, ctk_notebook_accessible, CTK_TYPE_CONTAINER_ACCESSIBLE,<--- There is an unknown macro here somewhere. Configuration is required. If G_DEFINE_TYPE_WITH_CODE is a macro then please configure it.
                         G_ADD_PRIVATE (CtkNotebookAccessible)
                         G_IMPLEMENT_INTERFACE (ATK_TYPE_SELECTION, atk_selection_interface_init))

static void
create_notebook_page_accessible (CtkNotebookAccessible *accessible,
                                 CtkNotebook           *notebook G_GNUC_UNUSED,
                                 CtkWidget             *child,
                                 gint                   page_num)
{
  AtkObject *obj;

  obj = ctk_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 (CtkNotebook *notebook,
               CtkWidget   *child,
               guint        page_num,
               gpointer     data G_GNUC_UNUSED)
{
  AtkObject *atk_obj;
  CtkNotebookAccessible *accessible;

  atk_obj = ctk_widget_get_accessible (CTK_WIDGET (notebook));
  accessible = CTK_NOTEBOOK_ACCESSIBLE (atk_obj);
  create_notebook_page_accessible (accessible, notebook, child, page_num);
}

static void
page_removed_cb (CtkNotebook *notebook,
                 CtkWidget   *widget,
                 guint        page_num,
                 gpointer     data G_GNUC_UNUSED)
{
  CtkNotebookAccessible *accessible;
  AtkObject *obj;

  accessible = CTK_NOTEBOOK_ACCESSIBLE (ctk_widget_get_accessible (CTK_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);
  ctk_notebook_page_accessible_invalidate (CTK_NOTEBOOK_PAGE_ACCESSIBLE (obj));
  g_hash_table_remove (accessible->priv->pages, widget);
}


static void
ctk_notebook_accessible_initialize (AtkObject *obj,
                                    gpointer   data)
{
  CtkNotebookAccessible *accessible;
  CtkNotebook *notebook;
  gint i;

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

  accessible = CTK_NOTEBOOK_ACCESSIBLE (obj);
  notebook = CTK_NOTEBOOK (data);
  for (i = 0; i < ctk_notebook_get_n_pages (notebook); i++)
    {
      create_notebook_page_accessible (accessible,
                                       notebook,
                                       ctk_notebook_get_nth_page (notebook, i),
                                       i);
    }
  accessible->priv->selected_page = ctk_notebook_get_current_page (notebook);

  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
ctk_notebook_accessible_finalize (GObject *object)
{
  CtkNotebookAccessible *accessible = CTK_NOTEBOOK_ACCESSIBLE (object);

  g_hash_table_destroy (accessible->priv->pages);

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

static AtkObject *
ctk_notebook_accessible_ref_child (AtkObject *obj,
                                   gint       i)
{
  AtkObject *child;
  CtkNotebookAccessible *accessible;
  CtkNotebook *notebook;
  CtkWidget *widget;
 
  widget = ctk_accessible_get_widget (CTK_ACCESSIBLE (obj));
  if (widget == NULL)
    return NULL;

  accessible = CTK_NOTEBOOK_ACCESSIBLE (obj);
  notebook = CTK_NOTEBOOK (widget);

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

  if (child)
    g_object_ref (child);

  return child;
}

static void
ctk_notebook_accessible_notify_ctk (GObject    *obj,
                                    GParamSpec *pspec)
{
  CtkWidget *widget;
  AtkObject* atk_obj;

  widget = CTK_WIDGET (obj);
  atk_obj = ctk_widget_get_accessible (widget);

  if (strcmp (pspec->name, "page") == 0)
    {
      gint page_num, old_page_num;
      CtkNotebookAccessible *accessible;
      CtkNotebook *notebook;

      accessible = CTK_NOTEBOOK_ACCESSIBLE (atk_obj);
      notebook = CTK_NOTEBOOK (widget);

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

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

          if (old_page_num != -1)
            {
              child = ctk_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 = ctk_notebook_accessible_ref_child (atk_obj, page_num);
          if (child)
            {
              atk_object_notify_state_change (child, ATK_STATE_SELECTED, TRUE);
              g_object_unref (child);
            }
          g_signal_emit_by_name (atk_obj, "selection-changed");
          g_signal_emit_by_name (atk_obj, "visible-data-changed");
        }
    }
  else
    CTK_WIDGET_ACCESSIBLE_CLASS (ctk_notebook_accessible_parent_class)->notify_ctk (obj, pspec);
}

/*
 * CtkNotebook 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
ctk_notebook_accessible_add_selection (AtkSelection *selection,
                                       gint          i)
{
  CtkNotebook *notebook;
  CtkWidget *widget;

  widget =  ctk_accessible_get_widget (CTK_ACCESSIBLE (selection));
  if (widget == NULL)
    return FALSE;

  notebook = CTK_NOTEBOOK (widget);
  ctk_notebook_set_current_page (notebook, i);
  return TRUE;
}

static void
ctk_notebook_accessible_class_init (CtkNotebookAccessibleClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
  AtkObjectClass  *class = ATK_OBJECT_CLASS (klass);
  CtkWidgetAccessibleClass *widget_class = (CtkWidgetAccessibleClass*)klass;
  CtkContainerAccessibleClass *container_class = (CtkContainerAccessibleClass*)klass;

  gobject_class->finalize = ctk_notebook_accessible_finalize;

  class->ref_child = ctk_notebook_accessible_ref_child;
  class->initialize = ctk_notebook_accessible_initialize;

  widget_class->notify_ctk = ctk_notebook_accessible_notify_ctk;

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

static void
ctk_notebook_accessible_init (CtkNotebookAccessible *notebook)
{
  notebook->priv = ctk_notebook_accessible_get_instance_private (notebook);
  notebook->priv->pages = g_hash_table_new_full (g_direct_hash,
                                                 g_direct_equal,
                                                 NULL,
                                                 g_object_unref);
  notebook->priv->selected_page = -1;
}

static AtkObject *
ctk_notebook_accessible_ref_selection (AtkSelection *selection,
                                       gint          i)
{
  AtkObject *accessible;
  CtkWidget *widget;
  CtkNotebook *notebook;
  gint pagenum;

  if (i != 0)
    return NULL;

  widget = ctk_accessible_get_widget (CTK_ACCESSIBLE (selection));
  if (widget == NULL)
    return NULL;

  notebook = CTK_NOTEBOOK (widget);
  pagenum = ctk_notebook_get_current_page (notebook);
  if (pagenum == -1)
    return NULL;
  accessible = ctk_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
ctk_notebook_accessible_get_selection_count (AtkSelection *selection)
{
  CtkWidget *widget;
  CtkNotebook *notebook;

  widget = ctk_accessible_get_widget (CTK_ACCESSIBLE (selection));
  if (widget == NULL)
    return 0;

  notebook = CTK_NOTEBOOK (widget);
  if (notebook == NULL || ctk_notebook_get_current_page (notebook) == -1)
    return 0;

  return 1;
}

static gboolean
ctk_notebook_accessible_is_child_selected (AtkSelection *selection,
                                           gint          i)
{
  CtkWidget *widget;
  CtkNotebook *notebook;
  gint pagenumber;

  widget = ctk_accessible_get_widget (CTK_ACCESSIBLE (selection));
  if (widget == NULL)
    return FALSE;

  notebook = CTK_NOTEBOOK (widget);
  pagenumber = ctk_notebook_get_current_page(notebook);

  if (pagenumber == i)
    return TRUE;

  return FALSE;
}

static void
atk_selection_interface_init (AtkSelectionIface *iface)
{
  iface->add_selection = ctk_notebook_accessible_add_selection;
  iface->ref_selection = ctk_notebook_accessible_ref_selection;
  iface->get_selection_count = ctk_notebook_accessible_get_selection_count;
  iface->is_child_selected = ctk_notebook_accessible_is_child_selected;
}