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