3b8e3649899015a89920647fcb43bfb5ea8ad771
[platform/upstream/at-spi2-core.git] / libspi / accessible.c
1 /*
2  * AT-SPI - Assistive Technology Service Provider Interface
3  * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
4  *
5  * Copyright 2001 Sun Microsystems Inc.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 /* accessible.c: the core of the accessibility implementation */
24
25 #include <config.h>
26 #include <stdio.h>
27 #include <libspi/libspi.h>
28
29 /* Our parent Gtk object type  */
30 #define PARENT_TYPE BONOBO_TYPE_OBJECT
31
32 /* A pointer to our parent object class */
33 static GObjectClass *spi_accessible_parent_class;
34
35 /*
36  * Implemented GObject::finalize
37  */
38 static void
39 spi_accessible_object_finalize (GObject *object)
40 {
41         SpiAccessible *accessible = SPI_ACCESSIBLE (object);
42
43         ATK_OBJECT (accessible->atko); /* assertion */
44         g_object_unref (G_OBJECT(accessible->atko));
45         accessible->atko = NULL;
46
47         spi_accessible_parent_class->finalize (object);
48 }
49
50 /*
51  * CORBA Accessibility::Accessible::get_name method implementation
52  */
53 static CORBA_char *
54 impl_accessibility_accessible_get_name (PortableServer_Servant servant,
55                                         CORBA_Environment     *ev)
56 {
57   CORBA_char * retval;
58   SpiAccessible *accessible = SPI_ACCESSIBLE (bonobo_object_from_servant (servant));
59   retval = (CORBA_char *) atk_object_get_name (accessible->atko);
60   if (retval )
61     retval = CORBA_string_dup (retval);
62   else
63     retval = CORBA_string_dup ("");
64
65   return retval;
66 }
67
68 /*
69  * CORBA Accessibility::Accessible::set_name method implementation
70  */
71 static void
72 impl_accessibility_accessible_set_name (PortableServer_Servant servant,
73                                         const CORBA_char      *name,
74                                         CORBA_Environment     *ev)
75 {
76   SpiAccessible *accessible = SPI_ACCESSIBLE (bonobo_object_from_servant (servant));
77   atk_object_set_name (accessible->atko, name);
78   printf ("SpiAccessible set_name called: %s\n", name);
79 }
80
81 /*
82  * CORBA Accessibility::Accessible::get_description method implementation
83  */
84 static CORBA_char *
85 impl_accessibility_accessible_get_description (PortableServer_Servant servant,
86                                                CORBA_Environment     *ev)
87 {
88   CORBA_char * retval;
89   SpiAccessible *accessible = SPI_ACCESSIBLE (bonobo_object_from_servant (servant));
90   retval = CORBA_string_dup (atk_object_get_description (accessible->atko));
91   if (retval )
92     retval = CORBA_string_dup (retval);
93   else
94     retval = CORBA_string_dup ("");
95
96   return retval;
97 }
98
99 /*
100  * CORBA Accessibility::Accessible::set_description method implementation
101  */
102 static void
103 impl_accessibility_accessible_set_description (PortableServer_Servant servant,
104                                                const CORBA_char      *name,
105                                                CORBA_Environment     *ev)
106 {
107   SpiAccessible *accessible = SPI_ACCESSIBLE (bonobo_object_from_servant (servant));
108   atk_object_set_description (accessible->atko, name);
109   printf ("SpiAccessible set_description called: %s\n", name);
110 }
111
112 /*
113  * CORBA Accessibility::Accessible::get_parent method implementation
114  */
115 static Accessibility_Accessible
116 impl_accessibility_accessible_get_parent (PortableServer_Servant servant,
117                                           CORBA_Environment     *ev)
118 {
119   Accessibility_Accessible retval;
120   SpiAccessible *accessible = SPI_ACCESSIBLE (bonobo_object_from_servant (servant));
121   AtkObject     *parent;
122
123   parent = atk_object_get_parent (accessible->atko);
124   retval = BONOBO_OBJREF (spi_accessible_new (parent));
125
126   return bonobo_object_dup_ref (retval, ev);
127 }
128
129 /*
130  * CORBA Accessibility::Accessible::get_IndexInParent method implementation
131  */
132 static CORBA_long
133 impl_accessibility_accessible_get_index_in_parent (PortableServer_Servant servant,
134                                                    CORBA_Environment     *ev)
135 {
136   CORBA_long retval;
137   SpiAccessible *accessible = SPI_ACCESSIBLE (bonobo_object_from_servant (servant));
138   retval = (CORBA_long) atk_object_get_index_in_parent (accessible->atko);
139   printf ("SpiAccessible get_index_in_parent called\n");
140   return retval;
141 }
142
143 /*
144  * CORBA Accessibility::Accessible::get_childCount method implementation
145  */
146 static CORBA_long
147 impl_accessibility_accessible_get_child_count (PortableServer_Servant servant,
148                                                CORBA_Environment     *ev)
149 {
150   CORBA_long retval;
151   SpiAccessible *accessible = SPI_ACCESSIBLE (bonobo_object_from_servant (servant));
152   retval = (CORBA_long) atk_object_get_n_accessible_children (accessible->atko);
153   printf ("SpiAccessible get_childCount called: %d\n", (int) retval);
154   return retval;
155 }
156
157 /*
158  * CORBA Accessibility::Accessible::getChildAtIndex method implementation
159  */
160 static Accessibility_Accessible
161 impl_accessibility_accessible_get_child_at_index (PortableServer_Servant servant,
162                                                   const CORBA_long      index,
163                                                   CORBA_Environment     *ev)
164 {
165   Accessibility_Accessible retval;
166   SpiAccessible *accessible = SPI_ACCESSIBLE (bonobo_object_from_servant (servant));
167   AtkObject *child = atk_object_ref_accessible_child (accessible->atko, (gint) index);
168   retval = BONOBO_OBJREF (spi_accessible_new (child));
169   return bonobo_object_dup_ref (retval, ev);
170 }
171
172 /*
173  * CORBA Accessibility::Accessible::getState method implementation
174  */
175 static Accessibility_StateSet
176 impl_accessibility_accessible_get_state (PortableServer_Servant servant,
177                                          CORBA_Environment     *ev)
178 {
179   Accessibility_StateSet retval;
180 /*  SpiAccessible *accessible = SPI_ACCESSIBLE (bonobo_object_from_servant (servant));
181     AtkStateSet *state = atk_object_ref_state_set (accessible->atko); */
182   retval = CORBA_OBJECT_NIL;
183   printf ("SpiAccessible get_state.\n");
184   /* TODO: implement the bonobo stateset class */
185   return (Accessibility_StateSet) retval;
186 }
187
188 /*
189  * CORBA Accessibility::Accessible::getRelationSet method implementation
190  */
191 static Accessibility_RelationSet *
192 impl_accessibility_accessible_get_relation_set (PortableServer_Servant servant,
193                                                 CORBA_Environment     *ev)
194 {
195   Accessibility_RelationSet *retval;
196   gint n_relations;
197   gint i;
198   SpiAccessible *accessible = SPI_ACCESSIBLE (bonobo_object_from_servant (servant));
199   AtkRelationSet *relation_set = atk_object_ref_relation_set (accessible->atko);
200   n_relations = atk_relation_set_get_n_relations (relation_set);
201   retval = CORBA_sequence_Accessibility_Relation__alloc ();
202   CORBA_sequence_Accessibility_Relation_allocbuf (n_relations);
203           
204   for (i=0; i<n_relations; ++i)
205     {
206       retval->_buffer[i] =
207               bonobo_object_dup_ref (bonobo_object_corba_objref (
208                       BONOBO_OBJECT (spi_relation_new (atk_relation_set_get_relation (relation_set, i)))),
209                                       ev);
210     }
211   
212   printf ("SpiAccessible get_relation_set.\n");
213   return retval;
214 }
215
216 /*
217  * CORBA Accessibility::Accessible::getRole method implementation
218  */
219 static Accessibility_Role
220 impl_accessibility_accessible_get_role (PortableServer_Servant servant,
221                                         CORBA_Environment     *ev)
222 {
223   Accessibility_Role retval;
224   SpiAccessible *accessible = SPI_ACCESSIBLE (bonobo_object_from_servant (servant));
225   AtkRole role = atk_object_get_role (accessible->atko);
226   retval = role; /* relies on ability to cast these back and forth */
227   printf ("SpiAccessible get_role.\n");
228   return (Accessibility_Role) retval;
229 }
230
231 static void
232 spi_accessible_class_init (SpiAccessibleClass *klass)
233 {
234         GObjectClass * object_class = (GObjectClass *) klass;
235         POA_Accessibility_Accessible__epv *epv = &klass->epv;
236         spi_accessible_parent_class = g_type_class_peek_parent (klass);
237
238         object_class->finalize = spi_accessible_object_finalize;
239
240         epv->_get_name = impl_accessibility_accessible_get_name;
241         epv->_set_name = impl_accessibility_accessible_set_name;
242         epv->_get_description = impl_accessibility_accessible_get_description;
243         epv->_set_description = impl_accessibility_accessible_set_description;
244
245         epv->_get_parent = impl_accessibility_accessible_get_parent;
246         epv->_get_childCount = impl_accessibility_accessible_get_child_count;
247         epv->getChildAtIndex = impl_accessibility_accessible_get_child_at_index;
248         epv->getIndexInParent = impl_accessibility_accessible_get_index_in_parent;
249
250         epv->getRelationSet = impl_accessibility_accessible_get_relation_set;
251         epv->getState = impl_accessibility_accessible_get_state;
252         epv->getRole = impl_accessibility_accessible_get_role;
253 }
254
255 static void
256 spi_accessible_init (SpiAccessible *accessible)
257 {
258 }
259
260 BONOBO_TYPE_FUNC_FULL (SpiAccessible,
261                        Accessibility_Accessible,
262                        PARENT_TYPE,
263                        spi_accessible);
264
265 SpiAccessible *
266 spi_accessible_new (AtkObject *o)
267 {
268     SpiAccessible *retval = g_object_new (SPI_ACCESSIBLE_TYPE, NULL);
269     CORBA_Environment ev;
270     CORBA_exception_init (&ev);
271     g_object_ref (o);
272     retval->atko = ATK_OBJECT (o);
273
274     /* aggregate appropriate SPI interfaces based on ATK interfaces */
275
276     if (ATK_IS_ACTION (o))
277       {
278         bonobo_object_add_interface (bonobo_object (retval),
279                                      BONOBO_OBJECT (spi_action_interface_new (o)));
280       }
281
282     if (ATK_IS_COMPONENT (o))
283       {
284         bonobo_object_add_interface (bonobo_object (retval),
285                                      BONOBO_OBJECT (spi_component_interface_new (o)));
286       }
287
288     if (ATK_IS_EDITABLE_TEXT (o))
289       {
290         bonobo_object_add_interface (bonobo_object (retval),
291                                      BONOBO_OBJECT(spi_editable_text_interface_new (o)));
292       }
293
294     else if (ATK_IS_HYPERTEXT (o))
295       {
296         bonobo_object_add_interface (bonobo_object (retval),
297                                      BONOBO_OBJECT (spi_hypertext_interface_new (o)));
298       }
299
300     else if (ATK_IS_TEXT (o))
301       {
302         bonobo_object_add_interface (bonobo_object (retval),
303                                      BONOBO_OBJECT (spi_text_interface_new (o)));
304       }
305
306     if (ATK_IS_IMAGE (o))
307       {
308         bonobo_object_add_interface (bonobo_object (retval),
309                                      BONOBO_OBJECT (spi_image_interface_new (o)));
310       }
311
312     if (ATK_IS_SELECTION (o))
313       {
314         bonobo_object_add_interface (bonobo_object (retval),
315                                      BONOBO_OBJECT (spi_selection_interface_new (o)));
316       }
317
318     if (ATK_IS_TABLE (o))
319       {
320         bonobo_object_add_interface (bonobo_object (retval),
321                                      BONOBO_OBJECT (spi_table_interface_new (o)));
322       }
323
324     if (ATK_IS_VALUE (o))
325       {
326         bonobo_object_add_interface (bonobo_object (retval),
327                                      BONOBO_OBJECT (spi_value_interface_new (o)));
328       }
329
330     return retval;
331 }