Replace DEPRECATED by @Deprecated in comments to fix warning when
[platform/upstream/atk.git] / atk / atkrelationset.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 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 <glib-object.h>
21
22 #include "atk.h"
23
24 static gpointer parent_class = NULL;
25
26 static void atk_relation_set_class_init (AtkRelationSetClass  *klass);
27 static void atk_relation_set_finalize   (GObject              *object);
28
29 GType
30 atk_relation_set_get_type (void)
31 {
32   static GType type = 0;
33
34   if (!type)
35     {
36       static const GTypeInfo typeInfo =
37       {
38         sizeof (AtkRelationSetClass),
39         (GBaseInitFunc) NULL,
40         (GBaseFinalizeFunc) NULL,
41         (GClassInitFunc) atk_relation_set_class_init,
42         (GClassFinalizeFunc) NULL,
43         NULL,
44         sizeof (AtkRelationSet),
45         0,
46         (GInstanceInitFunc) NULL,
47       } ;
48       type = g_type_register_static (G_TYPE_OBJECT, "AtkRelationSet", &typeInfo, 0) ;
49     }
50   return type;
51 }
52
53 static void
54 atk_relation_set_class_init (AtkRelationSetClass *klass)
55 {
56   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
57
58   parent_class = g_type_class_peek_parent (klass);
59
60   gobject_class->finalize = atk_relation_set_finalize;
61 }
62
63 /**
64  * atk_relation_set_new:
65  * 
66  * Creates a new empty relation set.
67  * 
68  * Returns: a new #AtkRelationSet 
69  **/
70 AtkRelationSet*
71 atk_relation_set_new (void)
72 {
73   AtkRelationSet *relation_set;
74
75   relation_set = g_object_new (ATK_TYPE_RELATION_SET, NULL);
76   return relation_set;
77 }
78
79 /**
80  * atk_relation_set_contains:
81  * @set: an #AtkRelationSet
82  * @relationship: an #AtkRelationType
83  *
84  * Determines whether the relation set contains a relation that matches the
85  * specified type.
86  *
87  * Returns: %TRUE if @relationship is the relationship type of a relation
88  * in @set, %FALSE otherwise
89  **/
90 gboolean
91 atk_relation_set_contains (AtkRelationSet   *set,
92                            AtkRelationType  relationship)
93 {
94   GPtrArray *array_item;
95   AtkRelation *item;
96   gint  i;
97
98   g_return_val_if_fail (ATK_IS_RELATION_SET (set), FALSE);
99
100   array_item = set->relations;
101   if (array_item == NULL)
102     return FALSE;
103   for (i = 0; i < array_item->len; i++)
104   {
105     item = g_ptr_array_index (array_item, i);
106     if (item->relationship == relationship)
107       return TRUE;
108   }
109   return FALSE;
110 }
111
112 /**
113  * atk_relation_set_remove:
114  * @set: an #AtkRelationSet
115  * @relation: an #AtkRelation
116  *
117  * Removes a relation from the relation set.
118  * This function unref's the #AtkRelation so it will be deleted unless there
119  * is another reference to it.
120  **/
121 void
122 atk_relation_set_remove (AtkRelationSet *set,
123                          AtkRelation    *relation)
124 {
125   GPtrArray *array_item;
126
127   g_return_if_fail (ATK_IS_RELATION_SET (set));
128
129   array_item = set->relations;
130   if (array_item == NULL)
131     return;
132   
133   if (g_ptr_array_remove (array_item, relation))
134   {
135     g_object_unref (relation);
136   }
137 }
138
139 /**
140  * atk_relation_set_add:
141  * @set: an #AtkRelationSet
142  * @relation: an #AtkRelation
143  *
144  * Add a new relation to the current relation set if it is not already
145  * present.
146  * This function ref's the AtkRelation so the caller of this function
147  * should unref it to ensure that it will be destroyed when the AtkRelationSet
148  * is destroyed.
149  **/
150 void
151 atk_relation_set_add (AtkRelationSet *set,
152                       AtkRelation    *relation)
153 {
154   g_return_if_fail (ATK_IS_RELATION_SET (set));
155   g_return_if_fail (relation != NULL);
156
157   if (set->relations == NULL)
158   {
159     set->relations = g_ptr_array_new ();
160   }
161   g_ptr_array_add (set->relations, relation);
162   g_object_ref (relation);
163 }
164
165 /**
166  * atk_relation_set_get_n_relations:
167  * @set: an #AtkRelationSet
168  *
169  * Determines the number of relations in a relation set.
170  *
171  * Returns: an integer representing the number of relations in the set.
172  **/
173 gint
174 atk_relation_set_get_n_relations (AtkRelationSet *set)
175 {
176   g_return_val_if_fail (ATK_IS_RELATION_SET (set), 0);
177
178   if (set->relations == NULL)
179     return 0;
180
181   return set->relations->len;
182 }
183
184 /**
185  * atk_relation_set_get_relation
186  * @set: an #AtkRelationSet
187  * @i: a gint representing a position in the set, starting from 0.
188  *
189  * Determines the relation at the specified position in the relation set.
190  *
191  * Returns: a #AtkRelation, which is the relation at position i in the set.
192  **/
193 AtkRelation*
194 atk_relation_set_get_relation (AtkRelationSet *set,
195                                gint           i)
196 {
197   GPtrArray *array_item;
198   AtkRelation* item;
199
200   g_return_val_if_fail (ATK_IS_RELATION_SET (set), NULL);
201   g_return_val_if_fail (i >= 0, NULL);
202
203   array_item = set->relations;
204   if (array_item == NULL)
205     return NULL;
206   item = g_ptr_array_index (array_item, i);
207   if (item == NULL)
208     return NULL;
209
210   return item;
211 }
212
213 /**
214  * atk_relation_set_get_relation_by_type:
215  * @set: an #AtkRelationSet
216  * @relationship: an #AtkRelationType
217  *
218  * Finds a relation that matches the specified type.
219  *
220  * Returns: an #AtkRelation, which is a relation matching the specified type.
221  **/
222 AtkRelation*
223 atk_relation_set_get_relation_by_type (AtkRelationSet  *set,
224                                        AtkRelationType relationship)
225 {
226   GPtrArray *array_item;
227   AtkRelation *item;
228   gint i;
229
230   g_return_val_if_fail (ATK_IS_RELATION_SET (set), NULL);
231
232   array_item = set->relations;
233   if (array_item == NULL)
234     return NULL;
235   for (i = 0; i < array_item->len; i++)
236   {
237     item = g_ptr_array_index (array_item, i);
238     if (item->relationship == relationship)
239       return item;
240   }
241   return NULL;
242 }
243
244 static void
245 atk_relation_set_finalize (GObject *object)
246 {
247   AtkRelationSet     *relation_set;
248   GPtrArray             *array;
249   gint               i;
250
251   g_return_if_fail (ATK_IS_RELATION_SET (object));
252
253   relation_set = ATK_RELATION_SET (object);
254   array = relation_set->relations;
255
256   if (array)
257   {
258     for (i = 0; i < array->len; i++)
259     {
260       g_object_unref (g_ptr_array_index (array, i));
261     }
262     g_ptr_array_free (array, TRUE);
263   }
264
265   G_OBJECT_CLASS (parent_class)->finalize (object);
266 }
267
268 /**
269  * atk_relation_set_add_relation_by_type:
270  * @set: an #AtkRelationSet
271  * @relationship: an #AtkRelationType
272  * @target: an #AtkObject
273  *
274  * Add a new relation of the specified type with the specified target to 
275  * the current relation set if the relation set does not contain a relation
276  * of that type. If it is does contain a relation of that typea the target
277  * is added to the relation.
278  **/
279 void
280 atk_relation_set_add_relation_by_type (AtkRelationSet  *set,
281                                        AtkRelationType relationship,
282                                        AtkObject       *target)
283 {
284   AtkRelation *relation;
285
286   g_return_if_fail (ATK_IS_RELATION_SET (set));
287   g_return_if_fail (ATK_IS_OBJECT (target));
288
289   relation = atk_relation_set_get_relation_by_type (set,
290                                                     relationship);
291   if (relation)
292     {
293       atk_relation_add_target (relation, target);
294     } 
295   else 
296     {
297       /* the relation hasn't been created yet ... */
298       relation = atk_relation_new (&target, 1, relationship);
299       atk_relation_set_add (set, relation);
300       g_object_unref(relation);
301     }
302 }
303