eb0bf09734cc48daf51eccabf4f4bbc3bc682a46
[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 <atk/atkgobjectaccessible.h>
21 #include <atk/atkregistry.h>
22 #include <atk/atkutil.h>
23
24 static void       atk_gobject_accessible_class_init       (AtkGObjectAccessibleClass   *klass);
25 static void       atk_real_gobject_accessible_initialize  (AtkObject         *atk_obj,
26                                                            gpointer          data);
27 static void       atk_gobject_accessible_dispose          (gpointer          data);
28
29 static GQuark quark_accessible_object = 0;
30 static GQuark quark_object = 0;
31 static gpointer parent_class = NULL;
32
33 GType
34 atk_gobject_accessible_get_type (void)
35 {
36   static GType type = 0;
37
38   if (!type)
39     {
40       static const GTypeInfo tinfo =
41       {
42         sizeof (AtkGObjectAccessibleClass),
43         (GBaseInitFunc) NULL, /* base init */
44         (GBaseFinalizeFunc) NULL, /* base finalize */
45         (GClassInitFunc) atk_gobject_accessible_class_init,
46         (GClassFinalizeFunc) NULL, /* class finalize */
47         NULL, /* class data */
48         sizeof (AtkGObjectAccessible),
49         0, /* nb preallocs */
50         (GInstanceInitFunc) NULL, /* instance init */
51         NULL /* value table */
52       };
53
54       type = g_type_register_static (ATK_TYPE_OBJECT,
55                                      "AtkGObjectAccessible", &tinfo, 0);
56     }
57
58   return type;
59 }
60
61 /**
62  * atk_gobject_accessible_for_object:
63  * @obj: a #GObject
64  *
65  * Gets the accessible object for the specified @obj.
66  *
67  * Returns: (transfer none): a #AtkObject which is the accessible object for
68  * the @obj
69  **/
70 AtkObject*
71 atk_gobject_accessible_for_object (GObject *obj)
72 {
73   AtkObject* accessible;
74
75   g_return_val_if_fail (G_IS_OBJECT (obj), NULL);
76   /* See if we have a cached accessible for this object */
77
78   accessible = g_object_get_qdata (obj,
79                                    quark_accessible_object);
80
81   if (!accessible)
82     {
83       AtkObjectFactory *factory;
84       AtkRegistry *default_registry;
85
86       default_registry = atk_get_default_registry ();
87       factory = atk_registry_get_factory (default_registry, 
88                                           G_OBJECT_TYPE (obj));
89       accessible = atk_object_factory_create_accessible (factory,
90                                                          obj);
91       if (!ATK_IS_GOBJECT_ACCESSIBLE (accessible))
92         {
93           /*
94            * The AtkObject which was created was not a AtkGObjectAccessible
95            */
96           g_object_weak_ref (obj,
97                              (GWeakNotify) g_object_unref,
98                              accessible); 
99           if (!quark_accessible_object)
100             quark_accessible_object = g_quark_from_static_string ("accessible-object");
101         }
102       g_object_set_qdata (obj, quark_accessible_object, accessible);
103     }
104   return accessible;
105 }
106
107 /**
108  * atk_gobject_accessible_get_object:
109  * @obj: a #AtkGObjectAccessible
110  *
111  * Gets the GObject for which @obj is the accessible object.
112  *
113  * Returns: (transfer none): a #GObject which is the object for which @obj is
114  * the accessible object
115  **/
116 GObject *
117 atk_gobject_accessible_get_object (AtkGObjectAccessible *obj)
118 {
119   g_return_val_if_fail (ATK_IS_GOBJECT_ACCESSIBLE (obj), NULL);
120
121   return g_object_get_qdata (G_OBJECT (obj), quark_object);
122 }
123  
124 static void
125 atk_real_gobject_accessible_initialize (AtkObject  *atk_obj,
126                                         gpointer   data)
127 {
128   AtkGObjectAccessible *atk_gobj;
129
130   atk_gobj = ATK_GOBJECT_ACCESSIBLE (atk_obj);
131
132   g_object_set_qdata (G_OBJECT (atk_gobj), quark_object, data);
133   atk_obj->layer = ATK_LAYER_WIDGET;
134
135   g_object_weak_ref (data,
136                      (GWeakNotify) atk_gobject_accessible_dispose,
137                      atk_gobj);
138 }
139
140 static void
141 atk_gobject_accessible_dispose (gpointer  data)
142 {
143   GObject *object;
144
145   g_return_if_fail (ATK_IS_GOBJECT_ACCESSIBLE (data));
146
147   object = atk_gobject_accessible_get_object (data);
148   if (object)
149       g_object_set_qdata (object, quark_accessible_object, NULL);
150
151   g_object_set_qdata (G_OBJECT (data), quark_object, NULL);
152   atk_object_notify_state_change (ATK_OBJECT (data), ATK_STATE_DEFUNCT,
153                                   TRUE); 
154   g_object_unref (data);
155 }
156
157 static void
158 atk_gobject_accessible_class_init (AtkGObjectAccessibleClass *klass)
159
160   AtkObjectClass *class;
161
162   class = ATK_OBJECT_CLASS (klass);
163
164   parent_class = g_type_class_peek_parent (klass);
165
166   class->initialize = atk_real_gobject_accessible_initialize;
167
168   if (!quark_accessible_object)
169     quark_accessible_object = g_quark_from_static_string ("accessible-object");
170   quark_object = g_quark_from_static_string ("object-for-accessible");
171 }