2001-11-13 Michael Meeks <michael@ximian.com>
[platform/core/uifw/at-spi2-atk.git] / libspi / selection.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  * component.c : bonobo wrapper for accessible component 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 #include "accessible.h"
37
38 /*
39  * This pulls the definition of the selection bonobo object
40  */
41 #include "selection.h"
42
43 /*
44  * Static function declarations
45  */
46
47 static void
48 spi_selection_class_init (SpiSelectionClass *klass);
49 static void
50 spi_selection_init (SpiSelection *selection);
51 static void
52 spi_selection_finalize (GObject *obj);
53 static CORBA_long
54 impl__get_nSelectedChildren (PortableServer_Servant _servant,
55                              CORBA_Environment * ev);
56 static Accessibility_Accessible
57 impl_getSelectedChild (PortableServer_Servant _servant,
58                        const CORBA_long selectedChildIndex,
59                        CORBA_Environment * ev);
60 static CORBA_boolean
61 impl_selectChild (PortableServer_Servant _servant,
62                   const CORBA_long childIndex,
63                   CORBA_Environment * ev);
64 static CORBA_boolean
65 impl_deselectSelectedChild (PortableServer_Servant _servant,
66                             const CORBA_long selectedChildIndex,
67                             CORBA_Environment * ev);
68 static CORBA_boolean
69 impl_isChildSelected (PortableServer_Servant _servant,
70                       const CORBA_long childIndex,
71                       CORBA_Environment * ev);
72 static void 
73 impl_selectAll (PortableServer_Servant _servant,
74                 CORBA_Environment * ev);
75 static void 
76 impl_clearSelection (PortableServer_Servant _servant,
77                      CORBA_Environment * ev);
78
79
80 static GObjectClass *parent_class;
81
82 GType
83 spi_selection_get_type (void)
84 {
85   static GType type = 0;
86
87   if (!type) {
88     static const GTypeInfo tinfo = {
89       sizeof (SpiSelectionClass),
90       (GBaseInitFunc) NULL,
91       (GBaseFinalizeFunc) NULL,
92       (GClassInitFunc) spi_selection_class_init,
93       (GClassFinalizeFunc) NULL,
94       NULL, /* class data */
95       sizeof (SpiSelection),
96       0, /* n preallocs */
97       (GInstanceInitFunc) spi_selection_init,
98                         NULL /* value table */
99     };
100
101     /*
102      * Bonobo_type_unique auto-generates a load of
103      * CORBA structures for us. All derived types must
104      * use bonobo_type_unique.
105      */
106     type = bonobo_type_unique (
107                                BONOBO_OBJECT_TYPE,
108                                POA_Accessibility_Selection__init,
109                                NULL,
110                                G_STRUCT_OFFSET (SpiSelectionClass, epv),
111                                &tinfo,
112                                "SpiAccessibleSelection");
113   }
114
115   return type;
116 }
117
118 static void
119 spi_selection_class_init (SpiSelectionClass *klass)
120 {
121   GObjectClass * object_class = (GObjectClass *) klass;
122   POA_Accessibility_Selection__epv *epv = &klass->epv;
123   parent_class = g_type_class_peek_parent (klass);
124
125   object_class->finalize = spi_selection_finalize;
126
127
128   /* Initialize epv table */
129
130   epv->_get_nSelectedChildren = impl__get_nSelectedChildren;
131   epv->getSelectedChild = impl_getSelectedChild;
132   epv->selectChild = impl_selectChild;
133   epv->deselectSelectedChild = impl_deselectSelectedChild;
134   epv->isChildSelected = impl_isChildSelected;
135   epv->selectAll = impl_selectAll;
136   epv->clearSelection = impl_clearSelection;
137 }
138
139 static void
140 spi_selection_init (SpiSelection *selection)
141 {
142 }
143
144 static void
145 spi_selection_finalize (GObject *obj)
146 {
147   SpiSelection *selection = SPI_SELECTION (obj);
148   g_object_unref (selection->atko);
149   selection->atko = NULL;
150   parent_class->finalize (obj);
151 }
152
153 SpiSelection *
154 spi_selection_interface_new (AtkObject *obj)
155 {
156   SpiSelection *new_selection = 
157     SPI_SELECTION(g_object_new (SPI_SELECTION_TYPE, NULL));
158   new_selection->atko = obj;
159   g_object_ref (obj);
160   return new_selection;
161 }
162
163
164
165 static CORBA_long
166 impl__get_nSelectedChildren (PortableServer_Servant _servant,
167                              CORBA_Environment * ev)
168 {
169   BonoboObject *obj = bonobo_object_from_servant (_servant);
170   SpiSelection *selection;
171 #ifdef SPI_DEBUG
172   fprintf (stderr, "calling impl__get_nSelectedChildren\n");
173 #endif
174   g_return_val_if_fail (IS_SPI_SELECTION (obj), 0);
175   selection = SPI_SELECTION (obj);
176   g_return_val_if_fail (ATK_IS_SELECTION (selection->atko), 0);
177   return (CORBA_long)
178     atk_selection_get_selection_count (ATK_SELECTION(selection->atko));
179 }
180
181
182 static Accessibility_Accessible
183 impl_getSelectedChild (PortableServer_Servant _servant,
184                        const CORBA_long selectedChildIndex,
185                        CORBA_Environment * ev)
186 {
187   BonoboObject *obj = bonobo_object_from_servant (_servant);
188   SpiSelection
189           *selection;
190   AtkObject *atk_object;
191   Accessibility_Accessible rv;
192 #ifdef SPI_DEBUG
193   fprintf (stderr, "calling impl_getSelectedChild\n");
194 #endif
195   g_return_val_if_fail (IS_SPI_SELECTION (obj), 0);
196   selection = SPI_SELECTION (obj);
197   g_return_val_if_fail (ATK_IS_SELECTION (selection->atko), 0);
198
199   atk_object = atk_selection_ref_selection (ATK_SELECTION (selection->atko),
200                                             (gint) selectedChildIndex);
201   g_return_val_if_fail (ATK_IS_OBJECT (atk_object), NULL);
202 #ifdef SPI_DEBUG
203   fprintf (stderr, "child type is %s\n", g_type_name (G_OBJECT_TYPE (atk_object)));
204 #endif
205   rv = bonobo_object_corba_objref (bonobo_object (spi_accessible_new (atk_object)));
206   g_object_unref (atk_object);
207   return CORBA_Object_duplicate (rv, ev);
208 }
209
210
211
212 static CORBA_boolean
213 impl_selectChild (PortableServer_Servant _servant,
214                   const CORBA_long childIndex,
215                   CORBA_Environment * ev)
216 {
217   SpiSelection *selection = SPI_SELECTION (bonobo_object_from_servant (_servant));
218   return (CORBA_boolean)
219     atk_selection_add_selection (ATK_SELECTION(selection->atko), (gint) childIndex);
220 }
221
222
223
224
225 static CORBA_boolean
226 impl_deselectSelectedChild (PortableServer_Servant _servant,
227                             const CORBA_long selectedChildIndex,
228                             CORBA_Environment * ev)
229 {
230   SpiSelection *selection = SPI_SELECTION (bonobo_object_from_servant (_servant));
231   return (CORBA_boolean)
232     atk_selection_remove_selection (ATK_SELECTION(selection->atko), (gint) selectedChildIndex);
233 }
234
235
236
237 static CORBA_boolean
238 impl_isChildSelected (PortableServer_Servant _servant,
239                       const CORBA_long childIndex,
240                       CORBA_Environment * ev)
241 {
242   SpiSelection *selection = SPI_SELECTION (bonobo_object_from_servant (_servant));
243   return (CORBA_boolean)
244     atk_selection_is_child_selected (ATK_SELECTION(selection->atko), (gint) childIndex);
245 }
246
247
248
249 static void 
250 impl_selectAll (PortableServer_Servant _servant,
251                 CORBA_Environment * ev)
252 {
253   SpiSelection *selection = SPI_SELECTION (bonobo_object_from_servant (_servant));
254   atk_selection_select_all_selection (ATK_SELECTION(selection->atko));
255 }
256
257
258
259 static void 
260 impl_clearSelection (PortableServer_Servant _servant,
261                      CORBA_Environment * ev)
262 {
263   SpiSelection *selection = SPI_SELECTION (bonobo_object_from_servant (_servant));
264   atk_selection_clear_selection (ATK_SELECTION(selection->atko));
265 }
266