Version 0.7
[atk.git] / atk / atkrelationset.c
blob8cc2bc5d3f1bfd6596ee702c7d5be40407252b3f
1 /* ATK - Accessibility Toolkit
2 * Copyright 2001 Sun Microsystems Inc.
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.
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.
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.
20 #include <glib-object.h>
22 #include "atk.h"
24 static void atk_relation_set_class_init (AtkRelationSetClass *klass);
25 static void atk_relation_set_finalize (GObject *object);
27 GType
28 atk_relation_set_get_type (void)
30 static GType type = 0;
32 if (!type)
34 static const GTypeInfo typeInfo =
36 sizeof (AtkRelationSetClass),
37 (GBaseInitFunc) NULL,
38 (GBaseFinalizeFunc) NULL,
39 (GClassInitFunc) atk_relation_set_class_init,
40 (GClassFinalizeFunc) NULL,
41 NULL,
42 sizeof (AtkRelationSet),
44 (GInstanceInitFunc) NULL,
45 } ;
46 type = g_type_register_static (G_TYPE_OBJECT, "AtkRelationSet", &typeInfo, 0) ;
48 return type;
51 static void
52 atk_relation_set_class_init (AtkRelationSetClass *klass)
54 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
56 gobject_class->finalize = atk_relation_set_finalize;
59 /**
60 * atk_relation_set_new:
62 * Creates a new empty relation set.
64 * Returns: a new #AtkRelationSet
65 **/
66 AtkRelationSet*
67 atk_relation_set_new (void)
69 AtkRelationSet *relation_set;
71 relation_set = g_object_new (ATK_TYPE_RELATION_SET, NULL);
72 return relation_set;
75 /**
76 * atk_relation_set_contains:
77 * @set: an #AtkRelationSet
78 * @relationship: an #AtkRelationType
80 * Determines whether the relation set contains a relation that matches the
81 * specified type.
83 * Returns: %TRUE if @relationship is the relationship type of a relation
84 * in @set, %FALSE otherwise
85 **/
86 gboolean
87 atk_relation_set_contains (AtkRelationSet *set,
88 AtkRelationType relationship)
90 GPtrArray *array_item;
91 AtkRelation *item;
92 gint i;
94 g_return_val_if_fail (ATK_IS_RELATION_SET (set), FALSE);
96 array_item = set->relations;
97 if (array_item == NULL)
98 return FALSE;
99 for (i = 0; i < array_item->len; i++)
101 item = g_ptr_array_index (array_item, i);
102 if (item->relationship == relationship)
103 return TRUE;
105 return FALSE;
109 * atk_relation_set_remove:
110 * @set: an #AtkRelationSet
111 * @relation: an #AtkRelation
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.
117 void
118 atk_relation_set_remove (AtkRelationSet *set,
119 AtkRelation *relation)
121 GPtrArray *array_item;
123 g_return_if_fail (ATK_IS_RELATION_SET (set));
125 array_item = set->relations;
126 if (array_item == NULL)
127 return;
129 if (g_ptr_array_remove (array_item, relation))
131 g_object_unref (relation);
136 * atk_relation_set_add:
137 * @set: an #AtkRelationSet
138 * @relation: an #AtkRelation
140 * Add a new relation to the current relation set if it is not already
141 * present.
142 * This function ref's the AtkRelation so the caller of this function
143 * should unref it to ensure that it will be destroyed when the AtkRelationSet
144 * is destroyed.
146 void
147 atk_relation_set_add (AtkRelationSet *set,
148 AtkRelation *relation)
150 g_return_if_fail (ATK_IS_RELATION_SET (set));
151 g_return_if_fail (relation != NULL);
153 if (set->relations == NULL)
155 set->relations = g_ptr_array_new ();
157 g_ptr_array_add (set->relations, relation);
158 g_object_ref (relation);
162 * atk_relation_set_get_n_relations:
163 * @set: an #AtkRelationSet
165 * Determines the number of relations in a relation set.
167 * Returns: an integer representing the number of relations in the set.
169 gint
170 atk_relation_set_get_n_relations (AtkRelationSet *set)
172 g_return_val_if_fail (ATK_IS_RELATION_SET (set), FALSE);
174 if (set->relations == NULL)
175 return 0;
177 return set->relations->len;
181 * atk_relation_set_get_relation
182 * @set: an #AtkRelationSet
183 * @i: a gint representing a position in the set, starting from 0.
185 * Determines the relation at the specified position in the relation set.
187 * Returns: a #AtkRelation, which is the relation at position i in the set.
189 AtkRelation*
190 atk_relation_set_get_relation (AtkRelationSet *set,
191 gint i)
193 GPtrArray *array_item;
194 AtkRelation* item;
196 g_return_val_if_fail (ATK_IS_RELATION_SET (set), FALSE);
197 g_return_val_if_fail (i >= 0, NULL);
199 array_item = set->relations;
200 if (array_item == NULL)
201 return NULL;
202 item = g_ptr_array_index (array_item, i);
203 if (item == NULL)
204 return NULL;
206 return item;
210 * atk_relation_set_get_relation_by_type:
211 * @set: an #AtkRelationSet
212 * @relationship: an #AtkRelationType
214 * Finds a relation that matches the specified type.
216 * Returns: an #AtkRelation, which is a relation matching the specified type.
218 AtkRelation*
219 atk_relation_set_get_relation_by_type (AtkRelationSet *set,
220 AtkRelationType relationship)
222 GPtrArray *array_item;
223 AtkRelation *item;
224 gint i;
226 g_return_val_if_fail (ATK_IS_RELATION_SET (set), FALSE);
228 array_item = set->relations;
229 if (array_item == NULL)
230 return NULL;
231 for (i = 0; i < array_item->len; i++)
233 item = g_ptr_array_index (array_item, i);
234 if (item->relationship == relationship)
235 return item;
237 return NULL;
240 static void
241 atk_relation_set_finalize (GObject *object)
243 AtkRelationSet *relation_set;
244 GPtrArray *array;
245 gint i;
247 g_return_if_fail (ATK_IS_RELATION_SET (object));
249 relation_set = ATK_RELATION_SET (object);
250 array = relation_set->relations;
252 if (array)
254 for (i = 0; i < array->len; i++)
256 g_object_unref (g_ptr_array_index (array, i));
258 g_ptr_array_free (array, TRUE);