Fixed refcounting issues with interface server
[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 static void
196 accessible_class_init (AccessibleClass *klass)
197 {
198         GObjectClass * object_class = (GObjectClass *) klass;
199         POA_Accessibility_Accessible__epv *epv = &klass->epv;
200         accessible_parent_class = g_type_class_peek_parent (klass);
201
202         object_class->finalize = accessible_object_finalize;
203
204         epv->_get_name = impl_accessibility_accessible_get_name;
205         epv->_set_name = impl_accessibility_accessible_set_name;
206         epv->_get_description = impl_accessibility_accessible_get_description;
207         epv->_set_description = impl_accessibility_accessible_set_description;
208
209         epv->_get_parent = impl_accessibility_accessible_get_parent;
210         epv->_get_childCount = impl_accessibility_accessible_get_child_count;
211         epv->getChildAtIndex = impl_accessibility_accessible_get_child_at_index;
212         epv->getIndexInParent = impl_accessibility_accessible_get_index_in_parent;
213
214         /* epv->getRelationSet = impl_accessibility_accessible_get_relation_set;      */
215         /* epv->getState = impl_accessibility_accessible_get_state;                   */
216         /* epv->getRole = impl_accessibility_accessible_get_role;                     */
217 }
218
219 static void
220 accessible_init (Accessible *accessible)
221 {
222 }
223
224 GType
225 accessible_get_type (void)
226 {
227         static GType type = 0;
228
229         if (!type) {
230                 static const GTypeInfo tinfo = {
231                         sizeof (AccessibleClass),
232                         (GBaseInitFunc) NULL,
233                         (GBaseFinalizeFunc) NULL,
234                         (GClassInitFunc) accessible_class_init,
235                         (GClassFinalizeFunc) NULL,
236                         NULL, /* class data */
237                         sizeof (Accessible),
238                         0, /* n preallocs */
239                         (GInstanceInitFunc) accessible_init,
240                         NULL /* value table */
241                 };
242                 /*
243                  * Bonobo_type_unique auto-generates a load of
244                  * CORBA structures for us. All derived types must
245                  * use bonobo_type_unique.
246                  */
247                 type = bonobo_type_unique (
248                         PARENT_TYPE,
249                         POA_Accessibility_Accessible__init,
250                         NULL,
251                         G_STRUCT_OFFSET (AccessibleClass, epv),
252                         &tinfo,
253                         "Accessible");
254         }
255
256         return type;
257 }
258
259 Accessible *
260 accessible_new (AtkObject *o)
261 {
262     Accessible *retval =
263                ACCESSIBLE (g_object_new (accessible_get_type (), NULL));
264     g_object_ref (o);
265     retval->atko = ATK_OBJECT (o);
266
267     /*
268      * TODO: add interface containers/constructors for EDITABLE_TEXT, HYPERTEXT,
269      *  IMAGE, SELECTION, TABLE, TEXT, VALUE.
270      */
271
272     /* add appropriate ATK interfaces */
273
274     if (ATK_IS_ACTION (o))
275       {
276         bonobo_object_add_interface (bonobo_object (retval),
277                                      BONOBO_OBJECT (action_interface_new (o)));
278       }
279
280     if (ATK_IS_COMPONENT (o))
281       {
282         bonobo_object_add_interface (bonobo_object (retval),
283                                      BONOBO_OBJECT (component_interface_new (o)));
284       }
285
286     if (ATK_IS_EDITABLE_TEXT (o))
287       {
288         bonobo_object_add_interface (bonobo_object (retval),
289                                      BONOBO_OBJECT(editable_text_interface_new (o)));
290       }
291
292     else if (ATK_IS_HYPERTEXT (o))
293       {
294         bonobo_object_add_interface (bonobo_object (retval),
295                                      BONOBO_OBJECT (hypertext_interface_new (o)));
296       }
297
298     else if (ATK_IS_TEXT (o))
299       {
300         bonobo_object_add_interface (bonobo_object (retval),
301                                      BONOBO_OBJECT (text_interface_new (o)));
302       }
303
304     if (ATK_IS_HYPERLINK (o))
305       {
306         bonobo_object_add_interface (bonobo_object (retval),
307                                      BONOBO_OBJECT (hyperlink_interface_new(o)));
308       }
309
310     if (ATK_IS_IMAGE (o))
311       {
312         bonobo_object_add_interface (bonobo_object (retval),
313                                      BONOBO_OBJECT (image_interface_new (o)));
314       }
315
316     if (ATK_IS_SELECTION (o))
317       {
318         bonobo_object_add_interface (bonobo_object (retval),
319                                      BONOBO_OBJECT (selection_interface_new (o)));
320       }
321
322     if (ATK_IS_TABLE (o))
323       {
324         bonobo_object_add_interface (bonobo_object (retval),
325                                      BONOBO_OBJECT (table_interface_new (o)));
326       }
327
328     if (ATK_IS_VALUE (o))
329       {
330         bonobo_object_add_interface (bonobo_object (retval),
331                                      BONOBO_OBJECT (value_interface_new (o)));
332       }
333
334
335     return retval;
336 }