Make AtkRelation and AtkRelationSet into GObjects.
[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   GArray *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_array_index (array_item, AtkRelation*, 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   GArray *array_item;
96   AtkRelation *item;
97   gint  i;
98
99   g_return_if_fail (set != NULL);
100   g_return_if_fail (ATK_IS_RELATION_SET (set));
101   g_return_if_fail (relation != NULL);
102
103   array_item = set->relations;
104   if (array_item == NULL)
105     return;
106   for (i = 0; i < array_item->len; i++)
107   {
108     item = g_array_index (array_item, AtkRelation*, i);
109     if (item == relation)
110     {
111       g_array_remove_index (array_item, i);
112       return;
113     }
114   }
115 }
116
117 void
118 atk_relation_set_add (AtkRelationSet *set,
119                       AtkRelation    *relation)
120 {
121   g_return_if_fail (set != NULL);
122   g_return_if_fail (ATK_IS_RELATION_SET (set));
123   g_return_if_fail (relation != NULL);
124
125   if (set->relations == NULL)
126   {
127     set->relations = g_array_new (FALSE, TRUE, sizeof (AtkRelation));
128   }
129   set->relations = g_array_append_val (set->relations, relation);
130 }
131
132 gint
133 atk_relation_set_get_n_relations (AtkRelationSet *set)
134 {
135   g_return_val_if_fail (set != NULL, 0);
136   g_return_val_if_fail (ATK_IS_RELATION_SET (set), FALSE);
137
138   if (set->relations == NULL)
139     return 0;
140
141   return set->relations->len;
142 }
143
144 AtkRelation*
145 atk_relation_set_get_relation (AtkRelationSet *set,
146                                gint           i)
147 {
148   GArray *array_item;
149   AtkRelation* item;
150
151   g_return_val_if_fail (set != NULL, NULL);
152   g_return_val_if_fail (ATK_IS_RELATION_SET (set), FALSE);
153   g_return_val_if_fail (i >= 0, NULL);
154
155   array_item = set->relations;
156   if (array_item == NULL)
157     return NULL;
158   item = g_array_index (array_item, AtkRelation*, i);
159   if (item == NULL)
160     return NULL;
161
162   return item;
163 }
164
165 AtkRelation*
166 atk_relation_set_get_relation_by_type (AtkRelationSet  *set,
167                                        AtkRelationType relationship)
168 {
169   GArray *array_item;
170   AtkRelation *item;
171   gint i;
172
173   g_return_val_if_fail (set != NULL, NULL);
174   g_return_val_if_fail (ATK_IS_RELATION_SET (set), FALSE);
175
176   array_item = set->relations;
177   if (array_item == NULL)
178     return NULL;
179   for (i = 0; i < array_item->len; i++)
180   {
181     item = g_array_index (array_item, AtkRelation*, i);
182     if (item->relationship == relationship)
183       return item;
184   }
185   return NULL;
186 }
187
188 static void
189 atk_relation_set_finalize (GObject *object)
190 {
191   AtkRelationSet     *relation_set;
192   GArray             *array;
193   gint               i;
194
195   g_return_if_fail (ATK_IS_RELATION_SET (object));
196
197   relation_set = ATK_RELATION_SET (object);
198   array = relation_set->relations;
199
200   if (array)
201   {
202     for (i = 0; i < array->len; i++)
203     {
204       g_object_unref (g_array_index (array, AtkRelation *, i));
205     }
206     g_array_free (array, TRUE);
207   }
208 }