summaryrefslogtreecommitdiff
path: root/modules/other/gail/gailnotebook.c
blob: 228641a8ceac190fddf42832f01095cd1fbb6324 (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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
/* 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, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "config.h"

#include <string.h>
#include <gtk/gtk.h>
#include "gailnotebook.h"
#include "gailnotebookpage.h"
#include "gail-private-macros.h"

static void         gail_notebook_class_init          (GailNotebookClass *klass);
static void         gail_notebook_init                (GailNotebook      *notebook);
static void         gail_notebook_finalize            (GObject           *object);
static void         gail_notebook_real_initialize     (AtkObject         *obj,
                                                       gpointer          data);

static void         gail_notebook_real_notify_gtk     (GObject           *obj,
                                                       GParamSpec        *pspec);

static AtkObject*   gail_notebook_ref_child           (AtkObject      *obj,
                                                       gint           i);
static gint         gail_notebook_real_remove_gtk     (GtkContainer   *container,
                                                       GtkWidget      *widget,
                                                       gpointer       data);    
static void         atk_selection_interface_init      (AtkSelectionIface *iface);
static gboolean     gail_notebook_add_selection       (AtkSelection   *selection,
                                                       gint           i);
static AtkObject*   gail_notebook_ref_selection       (AtkSelection   *selection,
                                                       gint           i);
static gint         gail_notebook_get_selection_count (AtkSelection   *selection);
static gboolean     gail_notebook_is_child_selected   (AtkSelection   *selection,
                                                       gint           i);
static AtkObject*   find_child_in_list                (GList          *list,
                                                       gint           index);
static void         check_cache                       (GailNotebook   *gail_notebook,
                                                       GtkNotebook    *notebook);
static void         reset_cache                       (GailNotebook   *gail_notebook,
                                                       gint           index);
static void         create_notebook_page_accessible   (GailNotebook   *gail_notebook,
                                                       GtkNotebook    *notebook,
                                                       gint           index,
                                                       gboolean       insert_before,
                                                       GList          *list);
static void         gail_notebook_child_parent_set    (GtkWidget      *widget,
                                                       GtkWidget      *old_parent,
                                                       gpointer       data);
static gboolean     gail_notebook_focus_cb            (GtkWidget      *widget,
                                                       GtkDirectionType type);
static gboolean     gail_notebook_check_focus_tab     (gpointer       data);
static void         gail_notebook_destroyed           (gpointer       data);


G_DEFINE_TYPE_WITH_CODE (GailNotebook, gail_notebook, GAIL_TYPE_CONTAINER,
                         G_IMPLEMENT_INTERFACE (ATK_TYPE_SELECTION, atk_selection_interface_init))

static void
gail_notebook_class_init (GailNotebookClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
  AtkObjectClass  *class = ATK_OBJECT_CLASS (klass);
  GailWidgetClass *widget_class;
  GailContainerClass *container_class;

  widget_class = (GailWidgetClass*)klass;
  container_class = (GailContainerClass*)klass;

  gobject_class->finalize = gail_notebook_finalize;

  widget_class->notify_gtk = gail_notebook_real_notify_gtk;

  class->ref_child = gail_notebook_ref_child;
  class->initialize = gail_notebook_real_initialize;
  /*
   * We do not provide an implementation of get_n_children
   * as the implementation in GailContainer returns the correct
   * number of children.
   */
  container_class->remove_gtk = gail_notebook_real_remove_gtk;
}

static void
gail_notebook_init (GailNotebook      *notebook)
{
  notebook->page_cache = NULL;
  notebook->selected_page = -1;
  notebook->focus_tab_page = -1;
  notebook->remove_index = -1;
  notebook->idle_focus_id = 0;
}

static AtkObject*
gail_notebook_ref_child (AtkObject      *obj,
                         gint           i)
{
  AtkObject *accessible = NULL;
  GailNotebook *gail_notebook;
  GtkNotebook *gtk_notebook;
  GtkWidget *widget;
 
  widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (obj));
  if (widget == NULL)
    /*
     * State is defunct
     */
    return NULL;

  gail_notebook = GAIL_NOTEBOOK (obj);
  
  gtk_notebook = GTK_NOTEBOOK (widget);

  if (gail_notebook->page_count < gtk_notebook_get_n_pages (gtk_notebook))
    check_cache (gail_notebook, gtk_notebook);

  accessible = find_child_in_list (gail_notebook->page_cache, i);

  if (accessible != NULL)
    g_object_ref (accessible);

  return accessible;
}

static void
gail_notebook_page_added (GtkNotebook *gtk_notebook,
                          GtkWidget   *child,
                          guint        page_num,
                          gpointer     data)
{
  AtkObject *atk_obj;
  GailNotebook *notebook;

  atk_obj = gtk_widget_get_accessible (GTK_WIDGET (gtk_notebook));
  notebook = GAIL_NOTEBOOK (atk_obj);
  create_notebook_page_accessible (notebook, gtk_notebook, page_num, FALSE, NULL);
  notebook->page_count++;
}

static void
gail_notebook_real_initialize (AtkObject *obj,
                               gpointer  data)
{
  GailNotebook *notebook;
  GtkNotebook *gtk_notebook;
  gint i;

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

  notebook = GAIL_NOTEBOOK (obj);
  gtk_notebook = GTK_NOTEBOOK (data);
  for (i = 0; i < gtk_notebook_get_n_pages (gtk_notebook); i++)
    {
      create_notebook_page_accessible (notebook, gtk_notebook, i, FALSE, NULL);
    }
  notebook->page_count = i;
  notebook->selected_page = gtk_notebook_get_current_page (gtk_notebook);

  g_signal_connect (gtk_notebook,
                    "focus",
                    G_CALLBACK (gail_notebook_focus_cb),
                    NULL);
  g_signal_connect (gtk_notebook,
                    "page-added",
                    G_CALLBACK (gail_notebook_page_added),
                    NULL);
  g_object_weak_ref (G_OBJECT(gtk_notebook),
                     (GWeakNotify) gail_notebook_destroyed,
                     obj);                     

  obj->role = ATK_ROLE_PAGE_TAB_LIST;
}

static void
gail_notebook_real_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;
      GailNotebook *gail_notebook;
      GtkNotebook *gtk_notebook;
     
      gail_notebook = GAIL_NOTEBOOK (atk_obj);
      gtk_notebook = GTK_NOTEBOOK (widget);
     
      if (gail_notebook->page_count < gtk_notebook_get_n_pages (gtk_notebook))
       check_cache (gail_notebook, gtk_notebook);
      /*
       * Notify SELECTED state change for old and new page
       */
      old_page_num = gail_notebook->selected_page;
      page_num = gtk_notebook_get_current_page (gtk_notebook);
      gail_notebook->selected_page = page_num;
      gail_notebook->focus_tab_page = page_num;
      old_focus_page_num = gail_notebook->focus_tab_page;

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

          if (old_page_num != -1)
            {
              obj = gail_notebook_ref_child (atk_obj, old_page_num);
              if (obj)
                {
                  atk_object_notify_state_change (obj,
                                                  ATK_STATE_SELECTED,
                                                  FALSE);
                  g_object_unref (obj);
                }
            }
          obj = gail_notebook_ref_child (atk_obj, page_num);
          if (obj)
            {
              atk_object_notify_state_change (obj,
                                              ATK_STATE_SELECTED,
                                              TRUE);
              g_object_unref (obj);
              /*
               * 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 (gtk_notebook) &&
         (focus_page_num != old_focus_page_num))
        {
          if (gail_notebook->idle_focus_id)
            g_source_remove (gail_notebook->idle_focus_id);
          gail_notebook->idle_focus_id = gdk_threads_add_idle (gail_notebook_check_focus_tab, atk_obj);
        }
    }
  else
    GAIL_WIDGET_CLASS (gail_notebook_parent_class)->notify_gtk (obj, pspec);
}

static void
gail_notebook_finalize (GObject            *object)
{
  GailNotebook *notebook = GAIL_NOTEBOOK (object);
  GList *list;

  /*
   * Get rid of the GailNotebookPage objects which we have cached.
   */
  list = notebook->page_cache;
  if (list != NULL)
    {
      while (list)
        {
          g_object_unref (list->data);
          list = list->next;
        }
    }

  g_list_free (notebook->page_cache);

  if (notebook->idle_focus_id)
    g_source_remove (notebook->idle_focus_id);

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

static void
atk_selection_interface_init (AtkSelectionIface *iface)
{
  iface->add_selection = gail_notebook_add_selection;
  iface->ref_selection = gail_notebook_ref_selection;
  iface->get_selection_count = gail_notebook_get_selection_count;
  iface->is_child_selected = gail_notebook_is_child_selected;
  /*
   * The following don't make any sense for GtkNotebook widgets.
   * Unsupported AtkSelection interfaces:
   * clear_selection();
   * remove_selection();
   * select_all_selection();
   */
}

/*
 * 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
gail_notebook_add_selection (AtkSelection *selection,
                             gint         i)
{
  GtkNotebook *notebook;
  GtkWidget *widget;
  
  widget =  gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
  if (widget == NULL)
    /*
     * State is defunct
     */
    return FALSE;
  
  notebook = GTK_NOTEBOOK (widget);
  gtk_notebook_set_current_page (notebook, i);
  return TRUE;
}

static AtkObject*
gail_notebook_ref_selection (AtkSelection *selection,
                             gint i)
{
  AtkObject *accessible;
  GtkWidget *widget;
  GtkNotebook *notebook;
  gint pagenum;
  
  /*
   * A note book can have only one selection.
   */
  gail_return_val_if_fail (i == 0, NULL);
  g_return_val_if_fail (GAIL_IS_NOTEBOOK (selection), NULL);
  
  widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
  if (widget == NULL)
    /* State is defunct */
	return NULL;
  
  notebook = GTK_NOTEBOOK (widget);
  pagenum = gtk_notebook_get_current_page (notebook);
  gail_return_val_if_fail (pagenum != -1, NULL);
  accessible = gail_notebook_ref_child (ATK_OBJECT (selection), pagenum);

  return accessible;
}

/*
 * Always return 1 because there can only be one page
 * selected at any time
 */
static gint
gail_notebook_get_selection_count (AtkSelection *selection)
{
  GtkWidget *widget;
  GtkNotebook *notebook;
  
  widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
  if (widget == NULL)
    /*
     * State is defunct
     */
    return 0;

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

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

  widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
  if (widget == NULL)
    /* 
     * State is defunct
     */
    return FALSE;

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

  if (pagenumber == i)
    return TRUE;
  else
    return FALSE; 
}

static AtkObject*
find_child_in_list (GList *list,
                    gint  index)
{
  AtkObject *obj = NULL;

  while (list)
    {
      if (GAIL_NOTEBOOK_PAGE (list->data)->index == index)
        {
          obj = ATK_OBJECT (list->data);
          break;
        }
      list = list->next;
    }
  return obj;
}

static void
check_cache (GailNotebook *gail_notebook,
             GtkNotebook  *notebook)
{
  GList *gtk_list;
  GList *gail_list;
  gint i;

  gtk_list = gtk_container_get_children (GTK_CONTAINER (notebook));
  gail_list = gail_notebook->page_cache;

  i = 0;
  while (gtk_list)
    {
      if (!gail_list)
        {
          create_notebook_page_accessible (gail_notebook, notebook, i, FALSE, NULL);
        }
      else if (GAIL_NOTEBOOK_PAGE (gail_list->data)->page != gtk_list->data)
        {
          create_notebook_page_accessible (gail_notebook, notebook, i, TRUE, gail_list);
        }
      else
        {
          gail_list = gail_list->next;
        }
      i++;
      gtk_list = gtk_list->next;
    }
  g_list_free (gtk_list);

  gail_notebook->page_count = i;
}

static void
reset_cache (GailNotebook *gail_notebook,
             gint         index)
{
  GList *l;

  for (l = gail_notebook->page_cache; l; l = l->next)
    {
      if (GAIL_NOTEBOOK_PAGE (l->data)->index > index)
        GAIL_NOTEBOOK_PAGE (l->data)->index -= 1;
    }
}

static void
create_notebook_page_accessible (GailNotebook *gail_notebook,
                                 GtkNotebook  *notebook,
                                 gint         index,
                                 gboolean     insert_before,
                                 GList        *list)
{
  AtkObject *obj;

  obj = gail_notebook_page_new (notebook, index);
  g_object_ref (obj);
  if (insert_before)
    gail_notebook->page_cache = g_list_insert_before (gail_notebook->page_cache, list, obj);
  else
    gail_notebook->page_cache = g_list_append (gail_notebook->page_cache, obj);
  g_signal_connect (gtk_notebook_get_nth_page (notebook, index), 
                    "parent_set",
                    G_CALLBACK (gail_notebook_child_parent_set),
                    obj);
}

static void
gail_notebook_child_parent_set (GtkWidget *widget,
                                GtkWidget *old_parent,
                                gpointer   data)
{
  GailNotebook *gail_notebook;

  gail_return_if_fail (old_parent != NULL);
  gail_notebook = GAIL_NOTEBOOK (gtk_widget_get_accessible (old_parent));
  gail_notebook->remove_index = GAIL_NOTEBOOK_PAGE (data)->index;
}

static gint
gail_notebook_real_remove_gtk (GtkContainer *container,
                               GtkWidget    *widget,
                               gpointer      data)    
{
  GailNotebook *gail_notebook;
  AtkObject *obj;
  gint index;

  g_return_val_if_fail (container != NULL, 1);
  gail_notebook = GAIL_NOTEBOOK (gtk_widget_get_accessible (GTK_WIDGET (container)));
  index = gail_notebook->remove_index;
  gail_notebook->remove_index = -1;

  obj = find_child_in_list (gail_notebook->page_cache, index);
  g_return_val_if_fail (obj, 1);
  gail_notebook->page_cache = g_list_remove (gail_notebook->page_cache, obj);
  gail_notebook->page_count -= 1;
  reset_cache (gail_notebook, index);
  g_signal_emit_by_name (gail_notebook,
                         "children_changed::remove",
                          GAIL_NOTEBOOK_PAGE (obj)->index, 
                          obj, NULL);
  g_object_unref (obj);
  return 1;
}

static gboolean
gail_notebook_focus_cb (GtkWidget      *widget,
                        GtkDirectionType type)
{
  AtkObject *atk_obj = gtk_widget_get_accessible (widget);
  GailNotebook *gail_notebook = GAIL_NOTEBOOK (atk_obj);

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

static gboolean
gail_notebook_check_focus_tab (gpointer data)
{
  GtkWidget *widget;
  AtkObject *atk_obj;
  gint focus_page_num, old_focus_page_num;
  GailNotebook *gail_notebook;
  GtkNotebook *gtk_notebook;

  atk_obj = ATK_OBJECT (data);
  gail_notebook = GAIL_NOTEBOOK (atk_obj);
  widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (atk_obj));

  gtk_notebook = GTK_NOTEBOOK (widget);

  gail_notebook->idle_focus_id = 0;

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

  old_focus_page_num = gail_notebook->focus_tab_page;
  gail_notebook->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 void
gail_notebook_destroyed (gpointer data)
{
  GailNotebook *gail_notebook = GAIL_NOTEBOOK (data);

  if (gail_notebook->idle_focus_id)
    {
      g_source_remove (gail_notebook->idle_focus_id);
      gail_notebook->idle_focus_id = 0;
    }
}