2002-09-13 Michael Meeks <michael@ximian.com>
[platform/core/uifw/at-spi2-atk.git] / libspi / relation.c
1 /*
2  * AT-SPI - Assistive Technology Service Provider Interface
3  * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
4  *
5  * Copyright 2001, 2002 Sun Microsystems Inc.,
6  * Copyright 2001, 2002 Ximian, Inc.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23
24 /* relation.c : implements the Relation interface */
25
26 #include <config.h>
27 #include <stdio.h>
28 #include <libspi/accessible.h>
29 #include <libspi/relation.h>
30
31
32 static gboolean
33 spi_init_relation_type_table (Accessibility_RelationType *types)
34 {
35   gint i;
36
37   for (i = 0; i < ATK_RELATION_LAST_DEFINED; i++)
38     types[i] = ATK_RELATION_NULL;
39
40   types[ATK_RELATION_CONTROLLED_BY] = Accessibility_RELATION_CONTROLLED_BY;
41   types[ATK_RELATION_CONTROLLER_FOR] = Accessibility_RELATION_CONTROLLER_FOR;
42   types[ATK_RELATION_LABEL_FOR] = Accessibility_RELATION_LABEL_FOR;
43   types[ATK_RELATION_LABELLED_BY] = Accessibility_RELATION_LABELLED_BY;
44   types[ATK_RELATION_MEMBER_OF] = Accessibility_RELATION_MEMBER_OF;
45   types[ATK_RELATION_NODE_CHILD_OF] = Accessibility_RELATION_NODE_CHILD_OF;
46   return TRUE;
47 }
48
49
50
51 static Accessibility_RelationType
52 spi_relation_type_from_atk_relation_type (AtkRelationType type)
53 {
54   static gboolean is_initialized = FALSE;
55   static Accessibility_RelationType spi_relation_type_table [ATK_RELATION_LAST_DEFINED];
56   Accessibility_RelationType spi_type;
57
58   if (!is_initialized)
59     is_initialized = spi_init_relation_type_table (spi_relation_type_table);       
60
61   if (type > ATK_RELATION_NULL && type < ATK_RELATION_LAST_DEFINED)
62     spi_type = spi_relation_type_table[type];
63   else
64     spi_type = Accessibility_RELATION_EXTENDED;
65   return spi_type;
66 }
67
68
69
70 static AtkRelation *
71 get_relation_from_servant (PortableServer_Servant servant)
72 {
73   SpiBase *base = SPI_BASE (bonobo_object_from_servant(servant));
74
75   g_return_val_if_fail (base, NULL);
76   return  ATK_RELATION(base->gobj);
77 }
78
79
80
81 static Accessibility_RelationType
82 impl_getRelationType (PortableServer_Servant servant,
83                       CORBA_Environment * ev)
84 {
85   AtkRelation *relation = get_relation_from_servant (servant);
86   AtkRelationType type;
87
88   g_return_val_if_fail (relation != NULL, 0);
89   type = atk_relation_get_relation_type (relation);
90   return spi_relation_type_from_atk_relation_type (type);
91 }
92
93
94 static CORBA_short
95 impl_getNTargets (PortableServer_Servant servant,
96                   CORBA_Environment * ev)
97 {
98   AtkRelation *relation = get_relation_from_servant(servant);
99   g_return_val_if_fail (relation != NULL, 0);
100
101   return relation->target ? relation->target->len : 0;
102 }
103
104
105 static CORBA_Object
106 impl_getTarget (PortableServer_Servant servant,
107                 const CORBA_short index,
108                 CORBA_Environment * ev)
109 {
110   AtkObject *atk_object;
111   AtkRelation *relation = get_relation_from_servant (servant);
112   g_return_val_if_fail (relation, NULL);
113
114   if (!relation->target ||
115       index < 0 ||
116       index >= relation->target->len)
117     return CORBA_OBJECT_NIL;
118
119   atk_object = g_ptr_array_index (relation->target, index);
120   if (!atk_object)
121     return CORBA_OBJECT_NIL;
122
123   return spi_accessible_new_return (atk_object, FALSE, ev);
124 }
125
126
127 SpiRelation *
128 spi_relation_new (AtkRelation *obj)
129 {
130   SpiRelation *new_relation = g_object_new (SPI_RELATION_TYPE, NULL);
131   spi_base_construct (SPI_BASE (new_relation), G_OBJECT (obj));
132   return new_relation;
133 }
134
135
136 static void
137 spi_relation_class_init (SpiRelationClass *klass)
138 {
139   POA_Accessibility_Relation__epv *epv = &klass->epv;
140
141   epv->getRelationType  = impl_getRelationType;  
142   epv->getNTargets      = impl_getNTargets;
143   epv->getTarget        = impl_getTarget;
144 }
145
146
147 static void
148 spi_relation_init (SpiRelation *relation)
149 {
150 }
151
152
153 BONOBO_TYPE_FUNC_FULL (SpiRelation,
154                        Accessibility_Relation,
155                        SPI_TYPE_BASE,
156                        spi_relation);