Added better gtk-doc comments.
[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 void            atk_relation_set_class_init       (AtkRelationSetClass  *klass);
25 static void            atk_relation_set_finalize         (GObject              *object);
26
27 GType
28 atk_relation_set_get_type (void)
29 {
30   static GType type = 0;
31
32   if (!type)
33     {
34       static const GTypeInfo typeInfo =
35       {
36         sizeof (AtkRelationSetClass),
37         (GBaseInitFunc) NULL,
38         (GBaseFinalizeFunc) NULL,
39         (GClassInitFunc) atk_relation_set_class_init,
40         (GClassFinalizeFunc) NULL,
41         NULL,
42         sizeof (AtkRelationSet),
43         0,
44         (GInstanceInitFunc) NULL,
45       } ;
46       type = g_type_register_static (G_TYPE_OBJECT, "AtkRelationSet", &typeInfo, 0) ;
47     }
48   return type;
49 }
50
51 static void
52 atk_relation_set_class_init (AtkRelationSetClass *klass)
53 {
54   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
55
56   gobject_class->finalize = atk_relation_set_finalize;
57 }
58
59 /**
60  * atk_relation_set_new:
61  * 
62  * Creates a new empty relation set.
63  * 
64  * Returns: a new #AtkRelationSet 
65  **/
66 AtkRelationSet*
67 atk_relation_set_new (void)
68 {
69   AtkRelationSet *relation_set;
70
71   relation_set = g_object_new (ATK_TYPE_RELATION_SET, NULL);
72   return relation_set;
73 }
74
75 /**
76  * atk_relation_set_contains:
77  * @set: a #AtkRelationSet
78  * @relationship: a #AtkRelationType
79  *
80  * Determines whether the relation set contains a relation that matches the
81  * specified type.
82  *
83  * Returns: %TRUE if @relationtype is the relationship type of a relation in @set.
84  **/
85 gboolean
86 atk_relation_set_contains (AtkRelationSet   *set,
87                            AtkRelationType  relationship)
88 {
89   GPtrArray *array_item;
90   AtkRelation *item;
91   gint  i;
92
93   g_return_val_if_fail (set != NULL, FALSE);
94   g_return_val_if_fail (ATK_IS_RELATION_SET (set), FALSE);
95
96   array_item = set->relations;
97   if (array_item == NULL)
98     return FALSE;
99   for (i = 0; i < array_item->len; i++)
100   {
101     item = g_ptr_array_index (array_item, i);
102     if (item->relationship == relationship)
103       return TRUE;
104   }
105   return FALSE;
106 }
107
108 /**
109  * atk_relation_set_remove:
110  * @set: a #AtkRelationSet
111  * @relation: a #AtkRelation
112  *
113  * Removes a relation from the relation set.
114  * This function unref's the AtkRelation so it will be deleted unless there
115  * is another reference to it.
116  **/
117 void
118 atk_relation_set_remove (AtkRelationSet *set,
119                          AtkRelation    *relation)
120 {
121   GPtrArray *array_item;
122
123   g_return_if_fail (set != NULL);
124   g_return_if_fail (ATK_IS_RELATION_SET (set));
125   g_return_if_fail (relation != NULL);
126
127   array_item = set->relations;
128   if (array_item == NULL)
129     return;
130   
131   if (g_ptr_array_remove (array_item, relation))
132   {
133     g_object_unref (relation);
134   }
135 }
136
137 /**
138  * atk_relation_set_add:
139  * @set: a #AtkRelationSet
140  * @relation: a #AtkRelation
141  *
142  * Add a new relation to the current relation set if it is not already
143  * present.
144  * This function ref's the AtkRelation so the caller of this function
145  * should unref it to ensure that it will be destroyed when the AtkRelationSet
146  * is destroyed.
147  **/
148 void
149 atk_relation_set_add (AtkRelationSet *set,
150                       AtkRelation    *relation)
151 {
152   g_return_if_fail (set != NULL);
153   g_return_if_fail (ATK_IS_RELATION_SET (set));
154   g_return_if_fail (relation != NULL);
155
156   if (set->relations == NULL)
157   {
158     set->relations = g_ptr_array_new ();
159   }
160   g_ptr_array_add (set->relations, relation);
161   g_object_ref (relation);
162 }
163
164 /**
165  * atk_relation_set_get_n_relations:
166  * @set: a #AtkRelationSet
167  *
168  * Determines the number of relations in a relation set.
169  *
170  * Returns: a gint representing the number of relations in the set.
171  **/
172 gint
173 atk_relation_set_get_n_relations (AtkRelationSet *set)
174 {
175   g_return_val_if_fail (set != NULL, 0);
176   g_return_val_if_fail (ATK_IS_RELATION_SET (set), FALSE);
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: a #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 (set != NULL, NULL);
201   g_return_val_if_fail (ATK_IS_RELATION_SET (set), FALSE);
202   g_return_val_if_fail (i >= 0, NULL);
203
204   array_item = set->relations;
205   if (array_item == NULL)
206     return NULL;
207   item = g_ptr_array_index (array_item, i);
208   if (item == NULL)
209     return NULL;
210
211   return item;
212 }
213
214 /**
215  * atk_relation_set_get_relation_by_type:
216  * @set: a #AtkRelationSet
217  * @relationship: a #AtkRelationType
218  *
219  * Finds a relation that matches the specified type.
220  *
221  * Returns: a #AtkRelation, which is a relation matching the specified type.
222  **/
223 AtkRelation*
224 atk_relation_set_get_relation_by_type (AtkRelationSet  *set,
225                                        AtkRelationType relationship)
226 {
227   GPtrArray *array_item;
228   AtkRelation *item;
229   gint i;
230
231   g_return_val_if_fail (set != NULL, NULL);
232   g_return_val_if_fail (ATK_IS_RELATION_SET (set), FALSE);
233
234   array_item = set->relations;
235   if (array_item == NULL)
236     return NULL;
237   for (i = 0; i < array_item->len; i++)
238   {
239     item = g_ptr_array_index (array_item, i);
240     if (item->relationship == relationship)
241       return item;
242   }
243   return NULL;
244 }
245
246 static void
247 atk_relation_set_finalize (GObject *object)
248 {
249   AtkRelationSet     *relation_set;
250   GPtrArray             *array;
251   gint               i;
252
253   g_return_if_fail (ATK_IS_RELATION_SET (object));
254
255   relation_set = ATK_RELATION_SET (object);
256   array = relation_set->relations;
257
258   if (array)
259   {
260     for (i = 0; i < array->len; i++)
261     {
262       g_object_unref (g_ptr_array_index (array, i));
263     }
264     g_ptr_array_free (array, TRUE);
265   }
266 }