Change to use GPtrArray instead GArray
[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 (AtkObjectClass),
37         (GBaseInitFunc) NULL,
38         (GBaseFinalizeFunc) NULL,
39         (GClassInitFunc) atk_relation_set_class_init,
40         (GClassFinalizeFunc) NULL,
41         NULL,
42         sizeof (AtkObject),
43         0,
44         (GInstanceInitFunc) NULL,
45       } ;
46       type = g_type_register_static (G_TYPE_OBJECT, "AtkRelatioSet", &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 AtkRelationSet*
60 atk_relation_set_new (void)
61 {
62   AtkRelationSet *relation_set;
63
64   relation_set = g_object_new (ATK_TYPE_RELATION_SET, NULL);
65   return relation_set;
66 }
67
68 gboolean
69 atk_relation_set_contains (AtkRelationSet   *set,
70                            AtkRelationType  relationship)
71 {
72   GPtrArray *array_item;
73   AtkRelation *item;
74   gint  i;
75
76   g_return_val_if_fail (set != NULL, FALSE);
77   g_return_val_if_fail (ATK_IS_RELATION_SET (set), FALSE);
78
79   array_item = set->relations;
80   if (array_item == NULL)
81     return FALSE;
82   for (i = 0; i < array_item->len; i++)
83   {
84     item = g_ptr_array_index (array_item, i);
85     if (item->relationship == relationship)
86       return TRUE;
87   }
88   return FALSE;
89 }
90
91 void
92 atk_relation_set_remove (AtkRelationSet *set,
93                          AtkRelation    *relation)
94 {
95   GPtrArray *array_item;
96
97   g_return_if_fail (set != NULL);
98   g_return_if_fail (ATK_IS_RELATION_SET (set));
99   g_return_if_fail (relation != NULL);
100
101   array_item = set->relations;
102   if (array_item == NULL)
103     return;
104   
105   if (g_ptr_array_remove (array_item, relation))
106   {
107     g_object_unref (relation);
108   }
109 }
110
111 void
112 atk_relation_set_add (AtkRelationSet *set,
113                       AtkRelation    *relation)
114 {
115   g_return_if_fail (set != NULL);
116   g_return_if_fail (ATK_IS_RELATION_SET (set));
117   g_return_if_fail (relation != NULL);
118
119   if (set->relations == NULL)
120   {
121     set->relations = g_ptr_array_new ();
122   }
123   g_ptr_array_add (set->relations, relation);
124   g_object_ref (relation);
125 }
126
127 gint
128 atk_relation_set_get_n_relations (AtkRelationSet *set)
129 {
130   g_return_val_if_fail (set != NULL, 0);
131   g_return_val_if_fail (ATK_IS_RELATION_SET (set), FALSE);
132
133   if (set->relations == NULL)
134     return 0;
135
136   return set->relations->len;
137 }
138
139 AtkRelation*
140 atk_relation_set_get_relation (AtkRelationSet *set,
141                                gint           i)
142 {
143   GPtrArray *array_item;
144   AtkRelation* item;
145
146   g_return_val_if_fail (set != NULL, NULL);
147   g_return_val_if_fail (ATK_IS_RELATION_SET (set), FALSE);
148   g_return_val_if_fail (i >= 0, NULL);
149
150   array_item = set->relations;
151   if (array_item == NULL)
152     return NULL;
153   item = g_ptr_array_index (array_item, i);
154   if (item == NULL)
155     return NULL;
156
157   return item;
158 }
159
160 AtkRelation*
161 atk_relation_set_get_relation_by_type (AtkRelationSet  *set,
162                                        AtkRelationType relationship)
163 {
164   GPtrArray *array_item;
165   AtkRelation *item;
166   gint i;
167
168   g_return_val_if_fail (set != NULL, NULL);
169   g_return_val_if_fail (ATK_IS_RELATION_SET (set), FALSE);
170
171   array_item = set->relations;
172   if (array_item == NULL)
173     return NULL;
174   for (i = 0; i < array_item->len; i++)
175   {
176     item = g_ptr_array_index (array_item, i);
177     if (item->relationship == relationship)
178       return item;
179   }
180   return NULL;
181 }
182
183 static void
184 atk_relation_set_finalize (GObject *object)
185 {
186   AtkRelationSet     *relation_set;
187   GPtrArray             *array;
188   gint               i;
189
190   g_return_if_fail (ATK_IS_RELATION_SET (object));
191
192   relation_set = ATK_RELATION_SET (object);
193   array = relation_set->relations;
194
195   if (array)
196   {
197     for (i = 0; i < array->len; i++)
198     {
199       g_object_unref (g_ptr_array_index (array, i));
200     }
201     g_ptr_array_free (array, TRUE);
202   }
203 }