Cleaned up some suspect int* casts, and added assertions to text calls in libspi
[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 *accessible_parent_class;
60
61 /*
62  * Implemented GObject::finalize
63  */
64 static void
65 accessible_object_finalize (GObject *object)
66 {
67         Accessible *accessible = ACCESSIBLE (object);
68
69         printf("accessible_object_finalize called\n");
70         g_object_unref (accessible->atko);
71         accessible->atko = NULL;
72
73         printf("atko freed, calling parent finalize\n");
74         accessible_parent_class->finalize (object);
75 }
76
77 /*
78  * CORBA Accessibility::Accessible::get_name method implementation
79  */
80 static CORBA_char *
81 impl_accessibility_accessible_get_name (PortableServer_Servant servant,
82                                         CORBA_Environment     *ev)
83 {
84   CORBA_char * retval;
85   Accessible *accessible = ACCESSIBLE (bonobo_object_from_servant (servant));
86   retval = (CORBA_char *) atk_object_get_name (accessible->atko);
87   if (retval )
88     retval = CORBA_string_dup (retval);
89   else
90     retval = CORBA_string_dup ("");
91   fprintf (stderr, "Accessible get_name called: %s\n", retval);
92   return retval;
93 }
94
95 /*
96  * CORBA Accessibility::Accessible::set_name method implementation
97  */
98 static void
99 impl_accessibility_accessible_set_name (PortableServer_Servant servant,
100                                         const CORBA_char      *name,
101                                         CORBA_Environment     *ev)
102 {
103   Accessible *accessible = ACCESSIBLE (bonobo_object_from_servant (servant));
104   atk_object_set_name (accessible->atko, name);
105   printf ("Accessible set_name called: %s\n", name);
106 }
107
108 /*
109  * CORBA Accessibility::Accessible::get_description method implementation
110  */
111 static CORBA_char *
112 impl_accessibility_accessible_get_description (PortableServer_Servant servant,
113                                                CORBA_Environment     *ev)
114 {
115   CORBA_char * retval;
116   Accessible *accessible = ACCESSIBLE (bonobo_object_from_servant (servant));
117   retval = CORBA_string_dup (atk_object_get_description (accessible->atko));
118   fprintf (stderr, "Accessible get_description called: %s\n", retval);
119   return retval;
120 }
121
122 /*
123  * CORBA Accessibility::Accessible::set_description method implementation
124  */
125 static void
126 impl_accessibility_accessible_set_description (PortableServer_Servant servant,
127                                                const CORBA_char      *name,
128                                                CORBA_Environment     *ev)
129 {
130   Accessible *accessible = ACCESSIBLE (bonobo_object_from_servant (servant));
131   atk_object_set_description (accessible->atko, name);
132   printf ("Accessible set_description called: %s\n", name);
133 }
134
135 /*
136  * CORBA Accessibility::Accessible::get_parent method implementation
137  */
138 static Accessibility_Accessible
139 impl_accessibility_accessible_get_parent (PortableServer_Servant servant,
140                                           CORBA_Environment     *ev)
141 {
142   Accessibility_Accessible retval;
143   Accessible *accessible = ACCESSIBLE (bonobo_object_from_servant (servant));
144   AtkObject *parent;
145   parent = atk_object_get_parent (accessible->atko);
146   retval = bonobo_object_corba_objref (bonobo_object (accessible_new (parent)));
147   printf ("Accessible get_parent called\n");
148   return retval;
149 }
150
151 /*
152  * CORBA Accessibility::Accessible::get_IndexInParent method implementation
153  */
154 static CORBA_long
155 impl_accessibility_accessible_get_index_in_parent (PortableServer_Servant servant,
156                                                    CORBA_Environment     *ev)
157 {
158   CORBA_long retval;
159   Accessible *accessible = ACCESSIBLE (bonobo_object_from_servant (servant));
160   retval = (CORBA_long) atk_object_get_index_in_parent (accessible->atko);
161   printf ("Accessible get_index_in_parent called\n");
162   return retval;
163 }
164
165 /*
166  * CORBA Accessibility::Accessible::get_childCount method implementation
167  */
168 static CORBA_long
169 impl_accessibility_accessible_get_child_count (PortableServer_Servant servant,
170                                                CORBA_Environment     *ev)
171 {
172   CORBA_long retval;
173   Accessible *accessible = ACCESSIBLE (bonobo_object_from_servant (servant));
174   retval = (CORBA_long) atk_object_get_n_accessible_children (accessible->atko);
175   printf ("Accessible get_childCount called: %d\n", (int) retval);
176   return retval;
177 }
178
179 /*
180  * CORBA Accessibility::Accessible::getChildAtIndex method implementation
181  */
182 static Accessibility_Accessible
183 impl_accessibility_accessible_get_child_at_index (PortableServer_Servant servant,
184                                                   const CORBA_long      index,
185                                                   CORBA_Environment     *ev)
186 {
187   Accessibility_Accessible retval;
188   Accessible *accessible = ACCESSIBLE (bonobo_object_from_servant (servant));
189   AtkObject *child = atk_object_ref_accessible_child (accessible->atko, (gint) index);
190   retval = bonobo_object_corba_objref ( bonobo_object (accessible_new (child)));
191   printf ("Accessible get_child_at_index called.\n");
192   return retval;
193 }
194
195 /*
196  * CORBA Accessibility::Accessible::getState method implementation
197  */
198 static Accessibility_StateSet
199 impl_accessibility_accessible_get_state (PortableServer_Servant servant,
200                                          CORBA_Environment     *ev)
201 {
202   Accessibility_StateSet retval;
203   Accessible *accessible = ACCESSIBLE (bonobo_object_from_servant (servant));
204   AtkStateSet *state = atk_object_ref_state_set (accessible->atko);
205   retval = CORBA_OBJECT_NIL;
206   printf ("Accessible get_state.\n");
207   /* TODO: implement the bonobo stateset class */
208   return (Accessibility_StateSet) retval;
209 }
210
211 /*
212  * CORBA Accessibility::Accessible::getRelationSet method implementation
213  */
214 static Accessibility_RelationSet *
215 impl_accessibility_accessible_get_relation_set (PortableServer_Servant servant,
216                                                 const CORBA_long      index,
217                                                 CORBA_Environment     *ev)
218 {
219   Accessibility_RelationSet *retval;
220   Accessible *accessible = 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 ("Accessible 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                                         const CORBA_long      index,
237                                         CORBA_Environment     *ev)
238 {
239   Accessibility_Role retval;
240   Accessible *accessible = ACCESSIBLE (bonobo_object_from_servant (servant));
241   AtkRole role = atk_object_get_role (accessible->atko);
242   retval = role;
243   printf ("Accessible get_role.\n");
244   return (Accessibility_Role) retval;
245 }
246
247 static void
248 accessible_class_init (AccessibleClass *klass)
249 {
250         GObjectClass * object_class = (GObjectClass *) klass;
251         POA_Accessibility_Accessible__epv *epv = &klass->epv;
252         accessible_parent_class = g_type_class_peek_parent (klass);
253
254         object_class->finalize = accessible_object_finalize;
255
256         epv->_get_name = impl_accessibility_accessible_get_name;
257         epv->_set_name = impl_accessibility_accessible_set_name;
258         epv->_get_description = impl_accessibility_accessible_get_description;
259         epv->_set_description = impl_accessibility_accessible_set_description;
260
261         epv->_get_parent = impl_accessibility_accessible_get_parent;
262         epv->_get_childCount = impl_accessibility_accessible_get_child_count;
263         epv->getChildAtIndex = impl_accessibility_accessible_get_child_at_index;
264         epv->getIndexInParent = impl_accessibility_accessible_get_index_in_parent;
265
266         epv->getRelationSet = impl_accessibility_accessible_get_relation_set;
267         epv->getState = impl_accessibility_accessible_get_state;
268         epv->getRole = impl_accessibility_accessible_get_role;
269 }
270
271 static void
272 accessible_init (Accessible *accessible)
273 {
274 }
275
276 GType
277 accessible_get_type (void)
278 {
279         static GType type = 0;
280
281         if (!type) {
282                 static const GTypeInfo tinfo = {
283                         sizeof (AccessibleClass),
284                         (GBaseInitFunc) NULL,
285                         (GBaseFinalizeFunc) NULL,
286                         (GClassInitFunc) accessible_class_init,
287                         (GClassFinalizeFunc) NULL,
288                         NULL, /* class data */
289                         sizeof (Accessible),
290                         0, /* n preallocs */
291                         (GInstanceInitFunc) accessible_init,
292                         NULL /* value table */
293                 };
294                 /*
295                  * Bonobo_type_unique auto-generates a load of
296                  * CORBA structures for us. All derived types must
297                  * use bonobo_type_unique.
298                  */
299                 type = bonobo_type_unique (
300                         PARENT_TYPE,
301                         POA_Accessibility_Accessible__init,
302                         NULL,
303                         G_STRUCT_OFFSET (AccessibleClass, epv),
304                         &tinfo,
305                         "Accessible");
306         }
307
308         return type;
309 }
310
311 Accessible *
312 accessible_new (AtkObject *o)
313 {
314     Accessible *retval =
315                ACCESSIBLE (g_object_new (accessible_get_type (), NULL));
316     g_object_ref (o);
317     retval->atko = ATK_OBJECT (o);
318
319     /*
320      * TODO: add interface containers/constructors for EDITABLE_TEXT, HYPERTEXT,
321      *  IMAGE, SELECTION, TABLE, TEXT, VALUE.
322      */
323
324     /* add appropriate ATK interfaces */
325
326     if (ATK_IS_ACTION (o))
327       {
328         bonobo_object_add_interface (bonobo_object (retval),
329                                      BONOBO_OBJECT (action_interface_new (o)));
330       }
331
332     if (ATK_IS_COMPONENT (o))
333       {
334         bonobo_object_add_interface (bonobo_object (retval),
335                                      BONOBO_OBJECT (component_interface_new (o)));
336       }
337
338     if (ATK_IS_EDITABLE_TEXT (o))
339       {
340         bonobo_object_add_interface (bonobo_object (retval),
341                                      BONOBO_OBJECT(editable_text_interface_new (o)));
342       }
343
344     else if (ATK_IS_HYPERTEXT (o))
345       {
346         bonobo_object_add_interface (bonobo_object (retval),
347                                      BONOBO_OBJECT (hypertext_interface_new (o)));
348       }
349
350     else if (ATK_IS_TEXT (o))
351       {
352         bonobo_object_add_interface (bonobo_object (retval),
353                                      BONOBO_OBJECT (text_interface_new (o)));
354       }
355
356     if (ATK_IS_HYPERLINK (o))
357       {
358         bonobo_object_add_interface (bonobo_object (retval),
359                                      BONOBO_OBJECT (hyperlink_interface_new(o)));
360       }
361
362     if (ATK_IS_IMAGE (o))
363       {
364         bonobo_object_add_interface (bonobo_object (retval),
365                                      BONOBO_OBJECT (image_interface_new (o)));
366       }
367
368     if (ATK_IS_SELECTION (o))
369       {
370         bonobo_object_add_interface (bonobo_object (retval),
371                                      BONOBO_OBJECT (selection_interface_new (o)));
372       }
373
374     if (ATK_IS_TABLE (o))
375       {
376         bonobo_object_add_interface (bonobo_object (retval),
377                                      BONOBO_OBJECT (table_interface_new (o)));
378       }
379
380     if (ATK_IS_VALUE (o))
381       {
382         bonobo_object_add_interface (bonobo_object (retval),
383                                      BONOBO_OBJECT (value_interface_new (o)));
384       }
385
386
387     return retval;
388 }