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