f0ed8cad5518ae24e388dd763aee03c67cd846b4
[platform/core/uifw/at-spi2-atk.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 /*
24  * accessible.c: test for accessibility implementation
25  *
26  */
27 #include <config.h>
28 #include <bonobo/Bonobo.h>
29
30 #include <stdio.h>
31
32 /*
33  * This pulls the CORBA definitions for the "Accessibility::Accessible" server
34  */
35 #include <libspi/Accessibility.h>
36
37 /*
38  * This pulls the definition for the BonoboObject (Gtk Type)
39  */
40 #include "accessible.h"
41 #include "component.h"
42 #include "editabletext.h"
43 #include "hyperlink.h"
44 #include "hypertext.h"
45 #include "image.h"
46 #include "selection.h"
47 #include "table.h"
48 #include "text.h"
49 #include "value.h"
50 #include "action.h"
51
52 /*
53  * Our parent Gtk object type
54  */
55 #define PARENT_TYPE BONOBO_OBJECT_TYPE
56
57 /*
58  * A pointer to our parent object class
59  */
60 static GObjectClass *spi_accessible_parent_class;
61
62 /*
63  * Implemented GObject::finalize
64  */
65 static void
66 spi_accessible_object_finalize (GObject *object)
67 {
68         SpiAccessible *accessible = SPI_ACCESSIBLE (object);
69
70         printf("spi_accessible_object_finalize called\n");
71         ATK_OBJECT (accessible->atko); /* assertion */
72         g_object_unref (G_OBJECT(accessible->atko));
73         accessible->atko = NULL;
74
75         printf("atko freed, calling parent finalize\n");
76         spi_accessible_parent_class->finalize (object);
77 }
78
79 /*
80  * CORBA Accessibility::Accessible::get_name method implementation
81  */
82 static CORBA_char *
83 impl_accessibility_accessible_get_name (PortableServer_Servant servant,
84                                         CORBA_Environment     *ev)
85 {
86   CORBA_char * retval;
87   SpiAccessible *accessible = SPI_ACCESSIBLE (bonobo_object_from_servant (servant));
88   retval = (CORBA_char *) atk_object_get_name (accessible->atko);
89   if (retval )
90     retval = CORBA_string_dup (retval);
91   else
92     retval = CORBA_string_dup ("");
93
94   return retval;
95 }
96
97 /*
98  * CORBA Accessibility::Accessible::set_name method implementation
99  */
100 static void
101 impl_accessibility_accessible_set_name (PortableServer_Servant servant,
102                                         const CORBA_char      *name,
103                                         CORBA_Environment     *ev)
104 {
105   SpiAccessible *accessible = SPI_ACCESSIBLE (bonobo_object_from_servant (servant));
106   atk_object_set_name (accessible->atko, name);
107   printf ("SpiAccessible set_name called: %s\n", name);
108 }
109
110 /*
111  * CORBA Accessibility::Accessible::get_description method implementation
112  */
113 static CORBA_char *
114 impl_accessibility_accessible_get_description (PortableServer_Servant servant,
115                                                CORBA_Environment     *ev)
116 {
117   CORBA_char * retval;
118   SpiAccessible *accessible = SPI_ACCESSIBLE (bonobo_object_from_servant (servant));
119   retval = CORBA_string_dup (atk_object_get_description (accessible->atko));
120   if (retval )
121     retval = CORBA_string_dup (retval);
122   else
123     retval = CORBA_string_dup ("");
124
125   return retval;
126 }
127
128 /*
129  * CORBA Accessibility::Accessible::set_description method implementation
130  */
131 static void
132 impl_accessibility_accessible_set_description (PortableServer_Servant servant,
133                                                const CORBA_char      *name,
134                                                CORBA_Environment     *ev)
135 {
136   SpiAccessible *accessible = SPI_ACCESSIBLE (bonobo_object_from_servant (servant));
137   atk_object_set_description (accessible->atko, name);
138   printf ("SpiAccessible set_description called: %s\n", name);
139 }
140
141 /*
142  * CORBA Accessibility::Accessible::get_parent method implementation
143  */
144 static Accessibility_Accessible
145 impl_accessibility_accessible_get_parent (PortableServer_Servant servant,
146                                           CORBA_Environment     *ev)
147 {
148   Accessibility_Accessible retval;
149   SpiAccessible *accessible = SPI_ACCESSIBLE (bonobo_object_from_servant (servant));
150   AtkObject     *parent;
151
152   parent = atk_object_get_parent (accessible->atko);
153   retval = BONOBO_OBJREF (spi_accessible_new (parent));
154
155   printf ("SpiAccessible get_parent called\n");
156
157   return CORBA_Object_duplicate (retval, ev);
158 }
159
160 /*
161  * CORBA Accessibility::Accessible::get_IndexInParent method implementation
162  */
163 static CORBA_long
164 impl_accessibility_accessible_get_index_in_parent (PortableServer_Servant servant,
165                                                    CORBA_Environment     *ev)
166 {
167   CORBA_long retval;
168   SpiAccessible *accessible = SPI_ACCESSIBLE (bonobo_object_from_servant (servant));
169   retval = (CORBA_long) atk_object_get_index_in_parent (accessible->atko);
170   printf ("SpiAccessible get_index_in_parent called\n");
171   return retval;
172 }
173
174 /*
175  * CORBA Accessibility::Accessible::get_childCount method implementation
176  */
177 static CORBA_long
178 impl_accessibility_accessible_get_child_count (PortableServer_Servant servant,
179                                                CORBA_Environment     *ev)
180 {
181   CORBA_long retval;
182   SpiAccessible *accessible = SPI_ACCESSIBLE (bonobo_object_from_servant (servant));
183   retval = (CORBA_long) atk_object_get_n_accessible_children (accessible->atko);
184   printf ("SpiAccessible get_childCount called: %d\n", (int) retval);
185   return retval;
186 }
187
188 /*
189  * CORBA Accessibility::Accessible::getChildAtIndex method implementation
190  */
191 static Accessibility_Accessible
192 impl_accessibility_accessible_get_child_at_index (PortableServer_Servant servant,
193                                                   const CORBA_long      index,
194                                                   CORBA_Environment     *ev)
195 {
196   Accessibility_Accessible retval;
197   SpiAccessible *accessible = SPI_ACCESSIBLE (bonobo_object_from_servant (servant));
198   AtkObject *child = atk_object_ref_accessible_child (accessible->atko, (gint) index);
199   retval = BONOBO_OBJREF (spi_accessible_new (child));
200   printf ("SpiAccessible get_child_at_index called.\n");
201   return CORBA_Object_duplicate (retval, ev);
202 }
203
204 /*
205  * CORBA Accessibility::Accessible::getState method implementation
206  */
207 static Accessibility_StateSet
208 impl_accessibility_accessible_get_state (PortableServer_Servant servant,
209                                          CORBA_Environment     *ev)
210 {
211   Accessibility_StateSet retval;
212 /*  SpiAccessible *accessible = SPI_ACCESSIBLE (bonobo_object_from_servant (servant));
213     AtkStateSet *state = atk_object_ref_state_set (accessible->atko); */
214   retval = CORBA_OBJECT_NIL;
215   printf ("SpiAccessible get_state.\n");
216   /* TODO: implement the bonobo stateset class */
217   return (Accessibility_StateSet) retval;
218 }
219
220 /*
221  * CORBA Accessibility::Accessible::getRelationSet method implementation
222  */
223 static Accessibility_RelationSet *
224 impl_accessibility_accessible_get_relation_set (PortableServer_Servant servant,
225                                                 CORBA_Environment     *ev)
226 {
227   Accessibility_RelationSet *retval;
228   gint n_relations;
229   gint i;
230   SpiAccessible *accessible = SPI_ACCESSIBLE (bonobo_object_from_servant (servant));
231   AtkRelationSet *relation_set = atk_object_ref_relation_set (accessible->atko);
232   n_relations = atk_relation_set_get_n_relations (relation_set);
233   retval = CORBA_sequence_Accessibility_Relation__alloc ();
234   CORBA_sequence_Accessibility_Relation_allocbuf (n_relations);
235           
236   for (i=0; i<n_relations; ++i)
237     {
238       retval->_buffer[i] =
239               CORBA_Object_duplicate (bonobo_object_corba_objref (
240                       spi_relation_new (atk_relation_set_get_relation (relation_set, i))),
241                                       ev);
242     }
243   
244   printf ("SpiAccessible get_relation_set.\n");
245   return retval;
246 }
247
248 /*
249  * CORBA Accessibility::Accessible::getRole method implementation
250  */
251 static Accessibility_Role
252 impl_accessibility_accessible_get_role (PortableServer_Servant servant,
253                                         CORBA_Environment     *ev)
254 {
255   Accessibility_Role retval;
256   SpiAccessible *accessible = SPI_ACCESSIBLE (bonobo_object_from_servant (servant));
257   AtkRole role = atk_object_get_role (accessible->atko);
258   retval = role; /* relies on ability to cast these back and forth */
259   printf ("SpiAccessible get_role.\n");
260   return (Accessibility_Role) retval;
261 }
262
263 static void
264 spi_accessible_class_init (SpiAccessibleClass *klass)
265 {
266         GObjectClass * object_class = (GObjectClass *) klass;
267         POA_Accessibility_Accessible__epv *epv = &klass->epv;
268         spi_accessible_parent_class = g_type_class_peek_parent (klass);
269
270         object_class->finalize = spi_accessible_object_finalize;
271
272         epv->_get_name = impl_accessibility_accessible_get_name;
273         epv->_set_name = impl_accessibility_accessible_set_name;
274         epv->_get_description = impl_accessibility_accessible_get_description;
275         epv->_set_description = impl_accessibility_accessible_set_description;
276
277         epv->_get_parent = impl_accessibility_accessible_get_parent;
278         epv->_get_childCount = impl_accessibility_accessible_get_child_count;
279         epv->getChildAtIndex = impl_accessibility_accessible_get_child_at_index;
280         epv->getIndexInParent = impl_accessibility_accessible_get_index_in_parent;
281
282         epv->getRelationSet = impl_accessibility_accessible_get_relation_set;
283         epv->getState = impl_accessibility_accessible_get_state;
284         epv->getRole = impl_accessibility_accessible_get_role;
285 }
286
287 static void
288 spi_accessible_init (SpiAccessible *accessible)
289 {
290 }
291
292 GType
293 spi_accessible_get_type (void)
294 {
295         static GType type = 0;
296
297         if (!type) {
298                 static const GTypeInfo tinfo = {
299                         sizeof (SpiAccessibleClass),
300                         (GBaseInitFunc) NULL,
301                         (GBaseFinalizeFunc) NULL,
302                         (GClassInitFunc) spi_accessible_class_init,
303                         (GClassFinalizeFunc) NULL,
304                         NULL, /* class data */
305                         sizeof (SpiAccessible),
306                         0, /* n preallocs */
307                         (GInstanceInitFunc) spi_accessible_init,
308                         NULL /* value table */
309                 };
310                 /*
311                  * Bonobo_type_unique auto-generates a load of
312                  * CORBA structures for us. All derived types must
313                  * use bonobo_type_unique.
314                  */
315                 type = bonobo_type_unique (
316                         PARENT_TYPE,
317                         POA_Accessibility_Accessible__init,
318                         NULL,
319                         G_STRUCT_OFFSET (SpiAccessibleClass, epv),
320                         &tinfo,
321                         "SpiAccessible");
322         }
323
324         return type;
325 }
326
327 SpiAccessible *
328 spi_accessible_new (AtkObject *o)
329 {
330     SpiAccessible *retval =
331                SPI_ACCESSIBLE (g_object_new (spi_accessible_get_type (), NULL));
332     CORBA_Environment ev;
333     CORBA_exception_init (&ev);
334     g_object_ref (o);
335     retval->atko = ATK_OBJECT (o);
336
337     /*
338      * TODO: add interface containers/constructors for SPI_EDITABLE_TEXT, SPI_HYPERTEXT,
339      *  SPI_IMAGE, SPI_SELECTION, SPI_TABLE, SPI_TEXT, SPI_VALUE.
340      */
341
342     /* add appropriate ATK interfaces */
343
344     if (ATK_IS_ACTION (o))
345       {
346         bonobo_object_add_interface (bonobo_object (retval),
347                                      BONOBO_OBJECT (spi_action_interface_new (o)));
348       }
349
350     if (ATK_IS_COMPONENT (o))
351       {
352         bonobo_object_add_interface (bonobo_object (retval),
353                                      BONOBO_OBJECT (spi_component_interface_new (o)));
354       }
355
356     if (ATK_IS_EDITABLE_TEXT (o))
357       {
358         bonobo_object_add_interface (bonobo_object (retval),
359                                      BONOBO_OBJECT(spi_editable_text_interface_new (o)));
360       }
361
362     else if (ATK_IS_HYPERTEXT (o))
363       {
364         bonobo_object_add_interface (bonobo_object (retval),
365                                      BONOBO_OBJECT (spi_hypertext_interface_new (o)));
366       }
367
368     else if (ATK_IS_TEXT (o))
369       {
370         bonobo_object_add_interface (bonobo_object (retval),
371                                      BONOBO_OBJECT (spi_text_interface_new (o)));
372       }
373
374     if (ATK_IS_IMAGE (o))
375       {
376         bonobo_object_add_interface (bonobo_object (retval),
377                                      BONOBO_OBJECT (spi_image_interface_new (o)));
378       }
379
380     if (ATK_IS_SELECTION (o))
381       {
382         bonobo_object_add_interface (bonobo_object (retval),
383                                      BONOBO_OBJECT (spi_selection_interface_new (o)));
384       }
385
386     if (ATK_IS_TABLE (o))
387       {
388         bonobo_object_add_interface (bonobo_object (retval),
389                                      BONOBO_OBJECT (spi_table_interface_new (o)));
390       }
391
392     if (ATK_IS_VALUE (o))
393       {
394         bonobo_object_add_interface (bonobo_object (retval),
395                                      BONOBO_OBJECT (spi_value_interface_new (o)));
396       }
397
398     return retval;
399 }