Added additional documentation and fixed a couple of latent bugs.
[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 /*  SpiAccessible *accessible = SPI_ACCESSIBLE (bonobo_object_from_servant (servant));
229     AtkRelationSet *relation_set = atk_object_ref_relation_set (accessible->atko); */
230   retval = CORBA_sequence_Accessibility_Relation__alloc ();
231   /*
232    *  TODO: fill the sequence with relation set objects, themselves
233    *  initialized from the AtkRelation object in the AtkRelationSet.
234    */
235   printf ("SpiAccessible get_relation_set.\n");
236   return retval;
237 }
238
239 /*
240  * CORBA Accessibility::Accessible::getRole method implementation
241  */
242 static Accessibility_Role
243 impl_accessibility_accessible_get_role (PortableServer_Servant servant,
244                                         CORBA_Environment     *ev)
245 {
246   Accessibility_Role retval;
247   SpiAccessible *accessible = SPI_ACCESSIBLE (bonobo_object_from_servant (servant));
248   AtkRole role = atk_object_get_role (accessible->atko);
249   retval = role;
250   printf ("SpiAccessible get_role.\n");
251   return (Accessibility_Role) retval;
252 }
253
254 static void
255 spi_accessible_class_init (SpiAccessibleClass *klass)
256 {
257         GObjectClass * object_class = (GObjectClass *) klass;
258         POA_Accessibility_Accessible__epv *epv = &klass->epv;
259         spi_accessible_parent_class = g_type_class_peek_parent (klass);
260
261         object_class->finalize = spi_accessible_object_finalize;
262
263         epv->_get_name = impl_accessibility_accessible_get_name;
264         epv->_set_name = impl_accessibility_accessible_set_name;
265         epv->_get_description = impl_accessibility_accessible_get_description;
266         epv->_set_description = impl_accessibility_accessible_set_description;
267
268         epv->_get_parent = impl_accessibility_accessible_get_parent;
269         epv->_get_childCount = impl_accessibility_accessible_get_child_count;
270         epv->getChildAtIndex = impl_accessibility_accessible_get_child_at_index;
271         epv->getIndexInParent = impl_accessibility_accessible_get_index_in_parent;
272
273         epv->getRelationSet = impl_accessibility_accessible_get_relation_set;
274         epv->getState = impl_accessibility_accessible_get_state;
275         epv->getRole = impl_accessibility_accessible_get_role;
276 }
277
278 static void
279 spi_accessible_init (SpiAccessible *accessible)
280 {
281 }
282
283 GType
284 spi_accessible_get_type (void)
285 {
286         static GType type = 0;
287
288         if (!type) {
289                 static const GTypeInfo tinfo = {
290                         sizeof (SpiAccessibleClass),
291                         (GBaseInitFunc) NULL,
292                         (GBaseFinalizeFunc) NULL,
293                         (GClassInitFunc) spi_accessible_class_init,
294                         (GClassFinalizeFunc) NULL,
295                         NULL, /* class data */
296                         sizeof (SpiAccessible),
297                         0, /* n preallocs */
298                         (GInstanceInitFunc) spi_accessible_init,
299                         NULL /* value table */
300                 };
301                 /*
302                  * Bonobo_type_unique auto-generates a load of
303                  * CORBA structures for us. All derived types must
304                  * use bonobo_type_unique.
305                  */
306                 type = bonobo_type_unique (
307                         PARENT_TYPE,
308                         POA_Accessibility_Accessible__init,
309                         NULL,
310                         G_STRUCT_OFFSET (SpiAccessibleClass, epv),
311                         &tinfo,
312                         "SpiAccessible");
313         }
314
315         return type;
316 }
317
318 SpiAccessible *
319 spi_accessible_new (AtkObject *o)
320 {
321     SpiAccessible *retval =
322                SPI_ACCESSIBLE (g_object_new (spi_accessible_get_type (), NULL));
323     CORBA_Environment ev;
324     CORBA_exception_init (&ev);
325     g_object_ref (o);
326     retval->atko = ATK_OBJECT (o);
327
328     /*
329      * TODO: add interface containers/constructors for SPI_EDITABLE_TEXT, SPI_HYPERTEXT,
330      *  SPI_IMAGE, SPI_SELECTION, SPI_TABLE, SPI_TEXT, SPI_VALUE.
331      */
332
333     /* add appropriate ATK interfaces */
334
335     if (ATK_IS_ACTION (o))
336       {
337         bonobo_object_add_interface (bonobo_object (retval),
338                                      BONOBO_OBJECT (spi_action_interface_new (o)));
339       }
340
341     if (ATK_IS_COMPONENT (o))
342       {
343         bonobo_object_add_interface (bonobo_object (retval),
344                                      BONOBO_OBJECT (spi_component_interface_new (o)));
345       }
346
347     if (ATK_IS_EDITABLE_TEXT (o))
348       {
349         bonobo_object_add_interface (bonobo_object (retval),
350                                      BONOBO_OBJECT(spi_editable_text_interface_new (o)));
351       }
352
353     else if (ATK_IS_HYPERTEXT (o))
354       {
355         bonobo_object_add_interface (bonobo_object (retval),
356                                      BONOBO_OBJECT (spi_hypertext_interface_new (o)));
357       }
358
359     else if (ATK_IS_TEXT (o))
360       {
361         bonobo_object_add_interface (bonobo_object (retval),
362                                      BONOBO_OBJECT (spi_text_interface_new (o)));
363       }
364
365     if (ATK_IS_IMAGE (o))
366       {
367         bonobo_object_add_interface (bonobo_object (retval),
368                                      BONOBO_OBJECT (spi_image_interface_new (o)));
369       }
370
371     if (ATK_IS_SELECTION (o))
372       {
373         bonobo_object_add_interface (bonobo_object (retval),
374                                      BONOBO_OBJECT (spi_selection_interface_new (o)));
375       }
376
377     if (ATK_IS_TABLE (o))
378       {
379         bonobo_object_add_interface (bonobo_object (retval),
380                                      BONOBO_OBJECT (spi_table_interface_new (o)));
381       }
382
383     if (ATK_IS_VALUE (o))
384       {
385         bonobo_object_add_interface (bonobo_object (retval),
386                                      BONOBO_OBJECT (spi_value_interface_new (o)));
387       }
388
389     return retval;
390 }