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