Install manifest
[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_dispose          (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 = g_object_get_qdata (obj,
92                                    quark_accessible_object);
93
94   if (!accessible)
95     {
96       AtkObjectFactory *factory;
97       AtkRegistry *default_registry;
98
99       default_registry = atk_get_default_registry ();
100       factory = atk_registry_get_factory (default_registry, 
101                                           G_OBJECT_TYPE (obj));
102       accessible = atk_object_factory_create_accessible (factory,
103                                                          obj);
104       if (!ATK_IS_GOBJECT_ACCESSIBLE (accessible))
105         {
106           /*
107            * The AtkObject which was created was not a AtkGObjectAccessible
108            */
109           g_object_weak_ref (obj,
110                              (GWeakNotify) g_object_unref,
111                              accessible); 
112           if (!quark_accessible_object)
113             quark_accessible_object = g_quark_from_static_string ("accessible-object");
114         }
115       g_object_set_qdata (obj, quark_accessible_object, accessible);
116     }
117   return accessible;
118 }
119
120 /**
121  * atk_gobject_accessible_get_object:
122  * @obj: a #AtkGObjectAccessible
123  *
124  * Gets the GObject for which @obj is the accessible object.
125  *
126  * Returns: (transfer none): a #GObject which is the object for which @obj is
127  * the accessible object
128  **/
129 GObject *
130 atk_gobject_accessible_get_object (AtkGObjectAccessible *obj)
131 {
132   g_return_val_if_fail (ATK_IS_GOBJECT_ACCESSIBLE (obj), NULL);
133
134   return g_object_get_qdata (G_OBJECT (obj), quark_object);
135 }
136  
137 static void
138 atk_real_gobject_accessible_initialize (AtkObject  *atk_obj,
139                                         gpointer   data)
140 {
141   AtkGObjectAccessible *atk_gobj;
142
143   atk_gobj = ATK_GOBJECT_ACCESSIBLE (atk_obj);
144
145   g_object_set_qdata (G_OBJECT (atk_gobj), quark_object, data);
146   atk_obj->layer = ATK_LAYER_WIDGET;
147
148   g_object_weak_ref (data,
149                      (GWeakNotify) atk_gobject_accessible_dispose,
150                      atk_gobj);
151 }
152
153 static void
154 atk_gobject_accessible_dispose (gpointer  data)
155 {
156   GObject *object;
157
158   g_return_if_fail (ATK_IS_GOBJECT_ACCESSIBLE (data));
159
160   object = atk_gobject_accessible_get_object (data);
161   if (object)
162       g_object_set_qdata (object, quark_accessible_object, NULL);
163
164   g_object_set_qdata (G_OBJECT (data), quark_object, NULL);
165   atk_object_notify_state_change (ATK_OBJECT (data), ATK_STATE_DEFUNCT,
166                                   TRUE); 
167   g_object_unref (data);
168 }
169
170 static void
171 atk_gobject_accessible_class_init (AtkGObjectAccessibleClass *klass)
172
173   AtkObjectClass *class;
174
175   class = ATK_OBJECT_CLASS (klass);
176
177   parent_class = g_type_class_peek_parent (klass);
178
179   class->initialize = atk_real_gobject_accessible_initialize;
180
181   if (!quark_accessible_object)
182     quark_accessible_object = g_quark_from_static_string ("accessible-object");
183   quark_object = g_quark_from_static_string ("object-for-accessible");
184 }