[l10n] Updated Catalan (Valencian) translation
[platform/upstream/atk.git] / atk / atkregistry.c
1 /* ATK - Accessibility Toolkit
2  * Copyright 2001 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 Library 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  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library 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 "atkregistry.h"
23 #include "atknoopobjectfactory.h"
24
25 /**
26  * SECTION:atkregistry
27  * @Short_description: An object used to store the GType of the
28  * factories used to create an accessible object for an object of a
29  * particular GType.
30  * @Title:AtkRegistry
31  *
32  * The AtkRegistry is normally used to create appropriate ATK "peers"
33  * for user interface components.  Application developers usually need
34  * only interact with the AtkRegistry by associating appropriate ATK
35  * implementation classes with GObject classes via the
36  * atk_registry_set_factory_type call, passing the appropriate GType
37  * for application custom widget classes.
38  */
39
40 static AtkRegistry *default_registry = NULL;
41
42 static void              atk_registry_init           (AtkRegistry      *instance,
43                                                       AtkRegistryClass *klass);
44 static void              atk_registry_finalize       (GObject          *instance);
45 static void              atk_registry_class_init     (AtkRegistryClass *klass);
46 static AtkRegistry*      atk_registry_new            (void);
47
48 static gpointer parent_class = NULL;
49
50 GType
51 atk_registry_get_type (void)
52 {
53   static GType type = 0;
54
55   if (!type)
56     {
57       static const GTypeInfo info =
58       {
59         sizeof (AtkRegistryClass),
60         (GBaseInitFunc) NULL,                             /* base_init */
61         (GBaseFinalizeFunc) NULL,                         /* base_finalize */
62         (GClassInitFunc) atk_registry_class_init,         /* class_init */
63         (GClassFinalizeFunc) NULL,                        /* class_finalize */
64         NULL,                                             /* class_data */
65         sizeof (AtkRegistry),                             /* instance size */
66         0,                                                /* n_preallocs */
67         (GInstanceInitFunc) atk_registry_init,            /* instance init */
68         NULL                                              /* value table */
69       };
70
71       type = g_type_register_static (G_TYPE_OBJECT, "AtkRegistry", &info, 0);
72     }
73
74   return type;
75 }
76
77 static void
78 atk_registry_class_init (AtkRegistryClass *klass)
79 {
80   GObjectClass *object_class = (GObjectClass *) klass;
81
82   parent_class = g_type_class_peek_parent (klass);
83
84   object_class->finalize = atk_registry_finalize;
85 }
86
87 static void
88 atk_registry_init (AtkRegistry *instance, AtkRegistryClass *klass)
89 {
90   instance->factory_type_registry = g_hash_table_new ((GHashFunc) NULL, 
91                                                       (GEqualFunc) NULL);
92   instance->factory_singleton_cache = g_hash_table_new ((GHashFunc) NULL, 
93                                                         (GEqualFunc) NULL);
94 }
95
96 static AtkRegistry *
97 atk_registry_new (void)
98 {
99   GObject *object;
100
101   object = g_object_new (ATK_TYPE_REGISTRY, NULL);
102
103   g_return_val_if_fail (ATK_IS_REGISTRY (object), NULL);
104
105   return (AtkRegistry *) object;
106 }
107
108 static void
109 atk_registry_finalize (GObject *object)
110 {
111   AtkRegistry *registry = ATK_REGISTRY (object);
112
113   g_hash_table_destroy (registry->factory_type_registry);
114   g_hash_table_destroy (registry->factory_singleton_cache);
115
116   G_OBJECT_CLASS (parent_class)->finalize (object);
117 }
118
119 /**
120  * atk_registry_set_factory_type:
121  * @registry: the #AtkRegistry in which to register the type association
122  * @type: an #AtkObject type 
123  * @factory_type: an #AtkObjectFactory type to associate with @type.  Must
124  * implement AtkObject appropriate for @type.
125  *
126  * Associate an #AtkObjectFactory subclass with a #GType. Note:
127  * The associated @factory_type will thereafter be responsible for
128  * the creation of new #AtkObject implementations for instances
129  * appropriate for @type.
130  **/
131 void
132 atk_registry_set_factory_type (AtkRegistry *registry,
133                                GType type,
134                                GType factory_type)
135 {
136   GType old_type;
137   gpointer value;
138   AtkObjectFactory *old_factory;
139
140   g_return_if_fail (ATK_IS_REGISTRY (registry));
141
142   value = g_hash_table_lookup (registry->factory_type_registry, 
143                                   (gpointer) type);
144   old_type = (GType) value;
145   if (old_type && old_type != factory_type)
146     {
147       g_hash_table_remove (registry->factory_type_registry, 
148                            (gpointer) type);
149       /*
150        * If the old factory was created, notify it that it has
151        * been replaced, then free it.
152        */
153       old_factory = g_hash_table_lookup (registry->factory_singleton_cache, 
154                                          (gpointer) old_type);
155       if (old_factory)
156         {
157           atk_object_factory_invalidate (old_factory);
158           g_type_free_instance ((GTypeInstance *) old_factory);
159         }
160     }
161   g_hash_table_insert (registry->factory_type_registry, 
162                        (gpointer) type, 
163                        (gpointer) factory_type);
164 }
165
166 /**
167  * atk_registry_get_factory_type:
168  * @registry: an #AtkRegistry
169  * @type: a #GType with which to look up the associated #AtkObjectFactory
170  * subclass
171  *
172  * Provides a #GType indicating the #AtkObjectFactory subclass
173  * associated with @type.
174  *
175  * Returns: a #GType associated with type @type
176  **/
177 GType
178 atk_registry_get_factory_type (AtkRegistry *registry,
179                                GType type)
180 {
181   GType factory_type;
182   gpointer value;
183
184   /*
185    * look up factory type in first hash;
186    * if there isn't an explicitly registered factory type,
187    * try inheriting one...
188    */
189   do {
190     value =
191         g_hash_table_lookup (registry->factory_type_registry, 
192                              (gpointer) type);
193     type = g_type_parent (type);
194     if (type == G_TYPE_INVALID)
195       {
196         break;
197       }
198   } while (value == NULL);
199
200   factory_type = (GType) value;
201   return factory_type;
202 }
203
204 /**
205  * atk_registry_get_factory:
206  * @registry: an #AtkRegistry
207  * @type: a #GType with which to look up the associated #AtkObjectFactory
208  *
209  * Gets an #AtkObjectFactory appropriate for creating #AtkObjects
210  * appropriate for @type.
211  *
212  * Returns: (transfer none): an #AtkObjectFactory appropriate for creating
213  * #AtkObjects appropriate for @type.
214  **/
215 AtkObjectFactory*
216 atk_registry_get_factory (AtkRegistry *registry,
217                           GType type)
218 {
219   gpointer factory_pointer = NULL;
220   GType factory_type;
221
222   factory_type = atk_registry_get_factory_type (registry, type);
223
224   if (factory_type == G_TYPE_INVALID)
225   {
226   /* Factory type has not been specified for this object type */
227     static AtkObjectFactory* default_factory = NULL;
228
229     if (!default_factory)
230       default_factory = atk_no_op_object_factory_new ();
231
232     return default_factory;
233   }
234
235   /* ask second hashtable for instance of factory type */
236   factory_pointer =
237         g_hash_table_lookup (registry->factory_singleton_cache, 
238         (gpointer) factory_type);
239
240   /* if there isn't one already, create one and save it */
241   if (factory_pointer == NULL)
242     {
243       factory_pointer = g_type_create_instance (factory_type);
244       g_hash_table_insert (registry->factory_singleton_cache,
245                            (gpointer) factory_type,
246                            factory_pointer);
247     }
248
249   return ATK_OBJECT_FACTORY (factory_pointer);
250 }
251
252 /**
253  * atk_get_default_registry:
254  *
255  * Gets a default implementation of the #AtkObjectFactory/type
256  * registry.
257  * Note: For most toolkit maintainers, this will be the correct
258  * registry for registering new #AtkObject factories. Following
259  * a call to this function, maintainers may call atk_registry_set_factory_type()
260  * to associate an #AtkObjectFactory subclass with the GType of objects
261  * for whom accessibility information will be provided.
262  *
263  * Returns: (transfer full): a default implementation of the
264  * #AtkObjectFactory/type registry
265  **/
266 AtkRegistry*
267 atk_get_default_registry (void)
268 {
269   if (!default_registry)
270     {
271       default_registry = atk_registry_new ();
272     }
273   return default_registry;
274 }