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