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>
24 static gpointer parent_class = NULL;
26 static void atk_relation_set_class_init (AtkRelationSetClass *klass);
27 static void atk_relation_set_finalize (GObject *object);
30 atk_relation_set_get_type (void)
32 static GType type = 0;
36 static const GTypeInfo typeInfo =
38 sizeof (AtkRelationSetClass),
40 (GBaseFinalizeFunc) NULL,
41 (GClassInitFunc) atk_relation_set_class_init,
42 (GClassFinalizeFunc) NULL,
44 sizeof (AtkRelationSet),
46 (GInstanceInitFunc) NULL,
48 type = g_type_register_static (G_TYPE_OBJECT, "AtkRelationSet", &typeInfo, 0) ;
54 atk_relation_set_class_init (AtkRelationSetClass *klass)
56 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
58 parent_class = g_type_class_peek_parent (klass);
60 gobject_class->finalize = atk_relation_set_finalize;
64 * atk_relation_set_new:
66 * Creates a new empty relation set.
68 * Returns: a new #AtkRelationSet
71 atk_relation_set_new (void)
73 AtkRelationSet *relation_set;
75 relation_set = g_object_new (ATK_TYPE_RELATION_SET, NULL);
80 * atk_relation_set_contains:
81 * @set: an #AtkRelationSet
82 * @relationship: an #AtkRelationType
84 * Determines whether the relation set contains a relation that matches the
87 * Returns: %TRUE if @relationship is the relationship type of a relation
88 * in @set, %FALSE otherwise
91 atk_relation_set_contains (AtkRelationSet *set,
92 AtkRelationType relationship)
94 GPtrArray *array_item;
98 g_return_val_if_fail (ATK_IS_RELATION_SET (set), FALSE);
100 array_item = set->relations;
101 if (array_item == NULL)
103 for (i = 0; i < array_item->len; i++)
105 item = g_ptr_array_index (array_item, i);
106 if (item->relationship == relationship)
113 * atk_relation_set_remove:
114 * @set: an #AtkRelationSet
115 * @relation: an #AtkRelation
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.
122 atk_relation_set_remove (AtkRelationSet *set,
123 AtkRelation *relation)
125 GPtrArray *array_item;
126 AtkRelationType relationship;
128 g_return_if_fail (ATK_IS_RELATION_SET (set));
130 array_item = set->relations;
131 if (array_item == NULL)
134 if (g_ptr_array_remove (array_item, relation))
136 g_object_unref (relation);
140 relationship = atk_relation_get_relation_type (relation);
141 if (atk_relation_set_contains (set, relationship))
143 AtkRelation *exist_relation;
145 exist_relation = atk_relation_set_get_relation_by_type (set, relationship);
146 for (i = 0; i < relation->target->len; i++)
148 AtkObject *target = g_ptr_array_index(relation->target, i);
149 atk_relation_remove_target (exist_relation, target);
156 * atk_relation_set_add:
157 * @set: an #AtkRelationSet
158 * @relation: an #AtkRelation
160 * Add a new relation to the current relation set if it is not already
162 * This function ref's the AtkRelation so the caller of this function
163 * should unref it to ensure that it will be destroyed when the AtkRelationSet
167 atk_relation_set_add (AtkRelationSet *set,
168 AtkRelation *relation)
170 AtkRelationType relationship;
172 g_return_if_fail (ATK_IS_RELATION_SET (set));
173 g_return_if_fail (relation != NULL);
175 if (set->relations == NULL)
177 set->relations = g_ptr_array_new ();
180 relationship = atk_relation_get_relation_type (relation);
181 if (!atk_relation_set_contains (set, relationship))
183 g_ptr_array_add (set->relations, relation);
184 g_object_ref (relation);
188 AtkRelation *exist_relation;
190 exist_relation = atk_relation_set_get_relation_by_type (set, relationship);
191 for (i = 0; i < relation->target->len; i++)
193 AtkObject *target = g_ptr_array_index(relation->target, i);
194 atk_relation_add_target (exist_relation, target);
200 * atk_relation_set_get_n_relations:
201 * @set: an #AtkRelationSet
203 * Determines the number of relations in a relation set.
205 * Returns: an integer representing the number of relations in the set.
208 atk_relation_set_get_n_relations (AtkRelationSet *set)
210 g_return_val_if_fail (ATK_IS_RELATION_SET (set), 0);
212 if (set->relations == NULL)
215 return set->relations->len;
219 * atk_relation_set_get_relation
220 * @set: an #AtkRelationSet
221 * @i: a gint representing a position in the set, starting from 0.
223 * Determines the relation at the specified position in the relation set.
225 * Returns: a #AtkRelation, which is the relation at position i in the set.
228 atk_relation_set_get_relation (AtkRelationSet *set,
231 GPtrArray *array_item;
234 g_return_val_if_fail (ATK_IS_RELATION_SET (set), NULL);
235 g_return_val_if_fail (i >= 0, NULL);
237 array_item = set->relations;
238 if (array_item == NULL)
240 item = g_ptr_array_index (array_item, i);
248 * atk_relation_set_get_relation_by_type:
249 * @set: an #AtkRelationSet
250 * @relationship: an #AtkRelationType
252 * Finds a relation that matches the specified type.
254 * Returns: an #AtkRelation, which is a relation matching the specified type.
257 atk_relation_set_get_relation_by_type (AtkRelationSet *set,
258 AtkRelationType relationship)
260 GPtrArray *array_item;
264 g_return_val_if_fail (ATK_IS_RELATION_SET (set), NULL);
266 array_item = set->relations;
267 if (array_item == NULL)
269 for (i = 0; i < array_item->len; i++)
271 item = g_ptr_array_index (array_item, i);
272 if (item->relationship == relationship)
279 atk_relation_set_finalize (GObject *object)
281 AtkRelationSet *relation_set;
285 g_return_if_fail (ATK_IS_RELATION_SET (object));
287 relation_set = ATK_RELATION_SET (object);
288 array = relation_set->relations;
292 for (i = 0; i < array->len; i++)
294 g_object_unref (g_ptr_array_index (array, i));
296 g_ptr_array_free (array, TRUE);
299 G_OBJECT_CLASS (parent_class)->finalize (object);
303 * atk_relation_set_add_relation_by_type:
304 * @set: an #AtkRelationSet
305 * @relationship: an #AtkRelationType
306 * @target: an #AtkObject
308 * Add a new relation of the specified type with the specified target to
309 * the current relation set if the relation set does not contain a relation
310 * of that type. If it is does contain a relation of that typea the target
311 * is added to the relation.
316 atk_relation_set_add_relation_by_type (AtkRelationSet *set,
317 AtkRelationType relationship,
320 AtkRelation *relation;
322 g_return_if_fail (ATK_IS_RELATION_SET (set));
323 g_return_if_fail (ATK_IS_OBJECT (target));
325 relation = atk_relation_set_get_relation_by_type (set,
329 atk_relation_add_target (relation, target);
333 /* the relation hasn't been created yet ... */
334 relation = atk_relation_new (&target, 1, relationship);
335 atk_relation_set_add (set, relation);
336 g_object_unref(relation);