win32/vsX: Fix 'make dist'
[platform/upstream/atk.git] / atk / atkgobjectaccessible.c
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  * SECTION:atkgobjectaccessible
28  * @Short_description: This object class is derived from AtkObject and
29  *  can be used as a basis implementing accessible objects.
30  * @Title:AtkGObjectAccessible
31  *
32  * This object class is derived from AtkObject. It can be used as a
33  * basis for implementing accessible objects for GObjects which are
34  * not derived from GtkWidget. One example of its use is in providing
35  * an accessible object for GnomeCanvasItem in the GAIL library.
36  */
37 static void       atk_gobject_accessible_class_init       (AtkGObjectAccessibleClass   *klass);
38 static void       atk_real_gobject_accessible_initialize  (AtkObject         *atk_obj,
39                                                            gpointer          data);
40 static void       atk_gobject_accessible_object_gone_cb   (gpointer          data);
41
42 static GQuark quark_accessible_object = 0;
43 static GQuark quark_object = 0;
44 static gpointer parent_class = NULL;
45
46 GType
47 atk_gobject_accessible_get_type (void)
48 {
49   static GType type = 0;
50
51   if (!type)
52     {
53       static const GTypeInfo tinfo =
54       {
55         sizeof (AtkGObjectAccessibleClass),
56         (GBaseInitFunc) NULL, /* base init */
57         (GBaseFinalizeFunc) NULL, /* base finalize */
58         (GClassInitFunc) atk_gobject_accessible_class_init,
59         (GClassFinalizeFunc) NULL, /* class finalize */
60         NULL, /* class data */
61         sizeof (AtkGObjectAccessible),
62         0, /* nb preallocs */
63         (GInstanceInitFunc) NULL, /* instance init */
64         NULL /* value table */
65       };
66
67       type = g_type_register_static (ATK_TYPE_OBJECT,
68                                      "AtkGObjectAccessible", &tinfo, 0);
69     }
70
71   return type;
72 }
73
74 /**
75  * atk_gobject_accessible_for_object:
76  * @obj: a #GObject
77  *
78  * Gets the accessible object for the specified @obj.
79  *
80  * Returns: (transfer none): a #AtkObject which is the accessible object for
81  * the @obj
82  **/
83 AtkObject*
84 atk_gobject_accessible_for_object (GObject *obj)
85 {
86   AtkObject* accessible;
87
88   g_return_val_if_fail (G_IS_OBJECT (obj), NULL);
89   /* See if we have a cached accessible for this object */
90
91   accessible = quark_accessible_object ? g_object_get_qdata (obj, quark_accessible_object) : NULL;
92
93   if (!accessible)
94     {
95       AtkObjectFactory *factory;
96       AtkRegistry *default_registry;
97
98       default_registry = atk_get_default_registry ();
99       factory = atk_registry_get_factory (default_registry, 
100                                           G_OBJECT_TYPE (obj));
101       accessible = atk_object_factory_create_accessible (factory,
102                                                          obj);
103       if (!ATK_IS_GOBJECT_ACCESSIBLE (accessible))
104         {
105           /*
106            * The AtkObject which was created was not a AtkGObjectAccessible
107            */
108           g_object_weak_ref (obj,
109                              (GWeakNotify) g_object_unref,
110                              accessible); 
111           if (!quark_accessible_object)
112             quark_accessible_object = g_quark_from_static_string ("accessible-object");
113         }
114       g_object_set_qdata (obj, quark_accessible_object, accessible);
115     }
116   return accessible;
117 }
118
119 /**
120  * atk_gobject_accessible_get_object:
121  * @obj: a #AtkGObjectAccessible
122  *
123  * Gets the GObject for which @obj is the accessible object.
124  *
125  * Returns: (transfer none): a #GObject which is the object for which @obj is
126  * the accessible object
127  **/
128 GObject *
129 atk_gobject_accessible_get_object (AtkGObjectAccessible *obj)
130 {
131   g_return_val_if_fail (ATK_IS_GOBJECT_ACCESSIBLE (obj), NULL);
132
133   return g_object_get_qdata (G_OBJECT (obj), quark_object);
134 }
135  
136 static void
137 atk_real_gobject_accessible_initialize (AtkObject  *atk_obj,
138                                         gpointer   data)
139 {
140   AtkGObjectAccessible *atk_gobj;
141
142   atk_gobj = ATK_GOBJECT_ACCESSIBLE (atk_obj);
143
144   g_object_set_qdata (G_OBJECT (atk_gobj), quark_object, data);
145   atk_obj->layer = ATK_LAYER_WIDGET;
146
147   g_object_weak_ref (data,
148                      (GWeakNotify) atk_gobject_accessible_object_gone_cb,
149                      atk_gobj);
150 }
151
152 static void
153 atk_gobject_accessible_object_gone_cb (gpointer  data)
154 {
155   GObject *object;
156
157   g_return_if_fail (ATK_IS_GOBJECT_ACCESSIBLE (data));
158
159   object = atk_gobject_accessible_get_object (data);
160   if (object)
161       g_object_set_qdata (object, quark_accessible_object, NULL);
162
163   g_object_set_qdata (G_OBJECT (data), quark_object, NULL);
164   atk_object_notify_state_change (ATK_OBJECT (data), ATK_STATE_DEFUNCT,
165                                   TRUE); 
166   g_object_unref (data);
167 }
168
169 static void
170 atk_gobject_accessible_dispose (GObject *atk_obj)
171 {
172    GObject *obj = atk_gobject_accessible_get_object (ATK_GOBJECT_ACCESSIBLE (atk_obj));
173
174    if (obj) {
175       g_object_set_qdata (obj, quark_accessible_object, NULL);
176       g_object_weak_unref (obj,
177                            (GWeakNotify) atk_gobject_accessible_object_gone_cb,
178                            atk_obj);
179
180       g_object_set_qdata (atk_obj, quark_object, NULL);
181       atk_object_notify_state_change (ATK_OBJECT (atk_obj), ATK_STATE_DEFUNCT,
182                                       TRUE); 
183    }
184
185    G_OBJECT_CLASS (parent_class)->dispose (atk_obj);
186 }
187
188 static void
189 atk_gobject_accessible_class_init (AtkGObjectAccessibleClass *klass)
190
191   AtkObjectClass *class;
192   GObjectClass *object_class;
193
194   class = ATK_OBJECT_CLASS (klass);
195
196   parent_class = g_type_class_peek_parent (klass);
197
198   class->initialize = atk_real_gobject_accessible_initialize;
199
200   object_class = G_OBJECT_CLASS (klass);
201   object_class->dispose = atk_gobject_accessible_dispose;
202
203   if (!quark_accessible_object)
204     quark_accessible_object = g_quark_from_static_string ("accessible-object");
205   quark_object = g_quark_from_static_string ("object-for-accessible");
206 }