switch to class_peek.
[platform/upstream/atk.git] / atk / atkrelation.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 <string.h>
21 #include <glib-object.h>
22 #include "atkobject.h"
23 #include "atkrelation.h"
24
25 static void            atk_relation_class_init       (AtkRelationClass  *klass);
26 static void            atk_relation_finalize         (GObject           *object);
27
28 GType
29 atk_relation_get_type (void)
30 {
31   static GType type = 0;
32
33   if (!type)
34     {
35       static const GTypeInfo typeInfo =
36       {
37         sizeof (AtkRelationClass),
38         (GBaseInitFunc) NULL,
39         (GBaseFinalizeFunc) NULL,
40         (GClassInitFunc) atk_relation_class_init,
41         (GClassFinalizeFunc) NULL,
42         NULL,
43         sizeof (AtkRelation),
44         0,
45         (GInstanceInitFunc) NULL,
46       } ;
47       type = g_type_register_static (G_TYPE_OBJECT, "AtkRelation", &typeInfo, 0) ;
48     }
49   return type;
50 }
51
52 static void
53 atk_relation_class_init (AtkRelationClass *klass)
54 {
55   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
56
57   gobject_class->finalize = atk_relation_finalize;
58 }
59
60 /**
61  * atk_relation_type_register:
62  * @name: a name string
63  *
64  * Associate @name with a new #AtkRelationType
65  *
66  * Returns: an #AtkRelationType associated with @name
67  **/
68 AtkRelationType
69 atk_relation_type_register (const gchar *name)
70 {
71   /* TODO: associate name with new type */
72   static guint type = ATK_RELATION_LAST_DEFINED;
73   return (++type);
74 }
75
76
77 /**
78  * atk_relation_type_from_string:
79  * @name: a string which is the (non-localized) name of an ATK relation type.
80  *
81  * Get the #AtkRelationType type corresponding to a relation name.
82  *
83  * Returns: the #AtkRelationType enumerated type corresponding to the specified name,
84  *          or #ATK_RELATION_NULL if no matching relation type is found.
85  **/
86 AtkRelationType
87 atk_relation_type_from_string (const gchar *name)
88 {
89   /*
90    * TODO: implement properly,
91    *  checking type namelist in conjunction with above function.
92    */
93         if ( !strcmp (name, "controlled_by") ) return ATK_RELATION_CONTROLLED_BY;
94         else if (!strcmp (name, "controller_for")) return ATK_RELATION_CONTROLLER_FOR;
95         else if (!strcmp (name, "label_for")) return ATK_RELATION_LABEL_FOR;
96         else if (!strcmp (name, "labelled_by")) return ATK_RELATION_LABELLED_BY;
97         else if (!strcmp (name, "member_of")) return ATK_RELATION_MEMBER_OF;
98         else return ATK_RELATION_NULL;
99 }
100
101
102 /**
103  * atk_relation_new:
104  * @targets: an array of pointers to #AtkObjects  
105  * @n_targets: number of #AtkObjects pointed to by @targets
106  * @relationship: an #AtkRelationType with which to create the new
107  *  #AtkRelation
108  *
109  * Create a new relation for the specified key and the specified list
110  * of targets.
111  *
112  * Returns: a pointer to a new #AtkRelation
113  **/
114 AtkRelation*
115 atk_relation_new (AtkObject       **targets,
116                   gint            n_targets,
117                   AtkRelationType relationship)
118 {
119   AtkRelation *relation;
120   int         i;
121   GPtrArray      *array;
122
123   g_return_val_if_fail (targets != NULL, NULL);
124
125   relation = g_object_new (ATK_TYPE_RELATION, NULL);
126   array = g_ptr_array_sized_new (n_targets);
127   for (i = 0; i < n_targets; i++)
128   {
129     /*
130      * Add a reference to AtkObject being added to a relation
131      */
132     g_object_ref (targets[i]);
133     g_ptr_array_add (array, targets[i]);
134   }
135   
136   relation->target = array;
137   relation->relationship = relationship;
138
139   return relation;
140 }
141
142 /**
143  * atk_relation_get_relation_type:
144  * @relation: an #AtkRelation 
145  *
146  * Gets the type of @relation
147  *
148  * Returns: the type of @relation
149  **/
150 AtkRelationType
151 atk_relation_get_relation_type (AtkRelation *relation)
152 {
153   g_return_val_if_fail (ATK_IS_RELATION (relation), 0);
154   
155   return relation->relationship;
156 }
157
158 /**
159  * atk_relation_get_target:
160  * @relation: an #AtkRelation
161  *
162  * Gets the target list of @relation
163  *
164  * Returns: the target list of @relation
165  **/
166 GPtrArray*
167 atk_relation_get_target (AtkRelation *relation)
168 {
169   g_return_val_if_fail (ATK_IS_RELATION (relation), FALSE);
170
171   return relation->target;
172 }
173
174 static void
175 atk_relation_finalize (GObject *object)
176 {
177   AtkRelation        *relation;
178
179   g_return_if_fail (ATK_IS_RELATION (object));
180
181   relation = ATK_RELATION (object);
182
183   if (relation->target)
184   {
185     gint i;
186
187     for (i = 0; i < relation->target->len; i++)
188     {
189       /*
190        * Remove a reference to AtkObject being removed from a relation
191        */
192       g_object_unref (g_ptr_array_index (relation->target, i));
193     }
194     g_ptr_array_free (relation->target, TRUE);
195   } 
196 }