1
/* ATK - Accessibility Toolkit
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 <atk/atkgobjectaccessible.h>
23
#include <atk/atkregistry.h>
24
#include <atk/atkutil.h>
25

            
26
/**
27
 * AtkGObjectAccessible:
28
 *
29
 * This object class is derived from AtkObject and can be used as a basis implementing accessible objects.
30
 *
31
 * This object class is derived from AtkObject. It can be used as a
32
 * basis for implementing accessible objects for GObjects which are
33
 * not derived from GtkWidget. One example of its use is in providing
34
 * an accessible object for GnomeCanvasItem in the GAIL library.
35
 */
36
static void atk_gobject_accessible_class_init (AtkGObjectAccessibleClass *klass);
37
static void atk_real_gobject_accessible_initialize (AtkObject *atk_obj,
38
                                                    gpointer data);
39
static void atk_gobject_accessible_object_gone_cb (gpointer data);
40

            
41
static GQuark quark_accessible_object = 0;
42
static GQuark quark_object = 0;
43
static gpointer parent_class = NULL;
44

            
45
GType
46
atk_gobject_accessible_get_type (void)
47
{
48
  static GType type = 0;
49

            
50
  if (!type)
51
    {
52
      static const GTypeInfo tinfo = {
53
        sizeof (AtkGObjectAccessibleClass),
54
        (GBaseInitFunc) NULL,     /* base init */
55
        (GBaseFinalizeFunc) NULL, /* base finalize */
56
        (GClassInitFunc) atk_gobject_accessible_class_init,
57
        (GClassFinalizeFunc) NULL, /* class finalize */
58
        NULL,                      /* class data */
59
        sizeof (AtkGObjectAccessible),
60
        0,                        /* nb preallocs */
61
        (GInstanceInitFunc) NULL, /* instance init */
62
        NULL                      /* value table */
63
      };
64

            
65
      type = g_type_register_static (ATK_TYPE_OBJECT,
66
                                     "AtkGObjectAccessible", &tinfo, 0);
67
    }
68

            
69
  return type;
70
}
71

            
72
/**
73
 * atk_gobject_accessible_for_object:
74
 * @obj: a #GObject
75
 *
76
 * Gets the accessible object for the specified @obj.
77
 *
78
 * Returns: (transfer none): a #AtkObject which is the accessible object for
79
 * the @obj
80
 **/
81
AtkObject *
82
atk_gobject_accessible_for_object (GObject *obj)
83
{
84
  AtkObject *accessible;
85

            
86
  g_return_val_if_fail (G_IS_OBJECT (obj), NULL);
87
  /* See if we have a cached accessible for this object */
88

            
89
  accessible = quark_accessible_object ? g_object_get_qdata (obj, quark_accessible_object) : NULL;
90

            
91
  if (!accessible)
92
    {
93
      AtkObjectFactory *factory;
94
      AtkRegistry *default_registry;
95

            
96
      default_registry = atk_get_default_registry ();
97
      factory = atk_registry_get_factory (default_registry,
98
                                          G_OBJECT_TYPE (obj));
99
      accessible = atk_object_factory_create_accessible (factory,
100
                                                         obj);
101
      if (!ATK_IS_GOBJECT_ACCESSIBLE (accessible))
102
        {
103
          /*
104
           * The AtkObject which was created was not a AtkGObjectAccessible
105
           */
106
          g_object_weak_ref (obj,
107
                             (GWeakNotify) g_object_unref,
108
                             accessible);
109
          if (!quark_accessible_object)
110
            quark_accessible_object = g_quark_from_static_string ("accessible-object");
111
        }
112
      g_object_set_qdata (obj, quark_accessible_object, accessible);
113
    }
114
  return accessible;
115
}
116

            
117
/**
118
 * atk_gobject_accessible_get_object:
119
 * @obj: a #AtkGObjectAccessible
120
 *
121
 * Gets the GObject for which @obj is the accessible object.
122
 *
123
 * Returns: (transfer none): a #GObject which is the object for which @obj is
124
 * the accessible object
125
 **/
126
GObject *
127
atk_gobject_accessible_get_object (AtkGObjectAccessible *obj)
128
{
129
  g_return_val_if_fail (ATK_IS_GOBJECT_ACCESSIBLE (obj), NULL);
130

            
131
  return g_object_get_qdata (G_OBJECT (obj), quark_object);
132
}
133

            
134
static void
135
atk_real_gobject_accessible_initialize (AtkObject *atk_obj,
136
                                        gpointer data)
137
{
138
  AtkGObjectAccessible *atk_gobj;
139

            
140
  atk_gobj = ATK_GOBJECT_ACCESSIBLE (atk_obj);
141

            
142
  g_object_set_qdata (G_OBJECT (atk_gobj), quark_object, data);
143
  atk_obj->layer = ATK_LAYER_WIDGET;
144

            
145
  g_object_weak_ref (data,
146
                     (GWeakNotify) atk_gobject_accessible_object_gone_cb,
147
                     atk_gobj);
148
}
149

            
150
static void
151
atk_gobject_accessible_object_gone_cb (gpointer data)
152
{
153
  GObject *object;
154

            
155
  g_return_if_fail (ATK_IS_GOBJECT_ACCESSIBLE (data));
156

            
157
  object = atk_gobject_accessible_get_object (data);
158
  if (object)
159
    g_object_set_qdata (object, quark_accessible_object, NULL);
160

            
161
  g_object_set_qdata (G_OBJECT (data), quark_object, NULL);
162
  atk_object_notify_state_change (ATK_OBJECT (data), ATK_STATE_DEFUNCT,
163
                                  TRUE);
164
  g_object_unref (data);
165
}
166

            
167
static void
168
atk_gobject_accessible_dispose (GObject *atk_obj)
169
{
170
  GObject *obj = atk_gobject_accessible_get_object (ATK_GOBJECT_ACCESSIBLE (atk_obj));
171

            
172
  if (obj)
173
    {
174
      g_object_set_qdata (obj, quark_accessible_object, NULL);
175
      g_object_weak_unref (obj,
176
                           (GWeakNotify) atk_gobject_accessible_object_gone_cb,
177
                           atk_obj);
178

            
179
      g_object_set_qdata (atk_obj, quark_object, NULL);
180
      atk_object_notify_state_change (ATK_OBJECT (atk_obj), ATK_STATE_DEFUNCT,
181
                                      TRUE);
182
    }
183

            
184
  G_OBJECT_CLASS (parent_class)->dispose (atk_obj);
185
}
186

            
187
static void
188
atk_gobject_accessible_class_init (AtkGObjectAccessibleClass *klass)
189
{
190
  AtkObjectClass *class;
191
  GObjectClass *object_class;
192

            
193
  class = ATK_OBJECT_CLASS (klass);
194

            
195
  parent_class = g_type_class_peek_parent (klass);
196

            
197
  class->initialize = atk_real_gobject_accessible_initialize;
198

            
199
  object_class = G_OBJECT_CLASS (klass);
200
  object_class->dispose = atk_gobject_accessible_dispose;
201

            
202
  if (!quark_accessible_object)
203
    quark_accessible_object = g_quark_from_static_string ("accessible-object");
204
  quark_object = g_quark_from_static_string ("object-for-accessible");
205
}