2008-03-31 Li Yuan <li.yuan@sun.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, 2002 Sun Microsystems Inc.,
6  * Copyright 2001, 2002 Ximian, Inc.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23
24 /* selection.c : implements the Selection interface */
25
26 #include <config.h>
27 #include <stdio.h>
28 #include <libspi/accessible.h>
29 #include <libspi/selection.h>
30
31
32 SpiSelection *
33 spi_selection_interface_new (AtkObject *obj)
34 {
35   SpiSelection *new_selection = g_object_new (SPI_SELECTION_TYPE, NULL);
36
37   spi_base_construct (SPI_BASE (new_selection), G_OBJECT(obj));
38
39   return new_selection;
40 }
41
42
43 static AtkSelection *
44 get_selection_from_servant (PortableServer_Servant servant)
45 {
46   SpiBase *object = SPI_BASE (bonobo_object_from_servant (servant));
47
48   g_return_val_if_fail (object, NULL);
49   g_return_val_if_fail (ATK_IS_OBJECT(object->gobj), NULL);
50   return ATK_SELECTION (object->gobj);
51 }
52
53
54 static CORBA_long
55 impl__get_nSelectedChildren (PortableServer_Servant servant,
56                              CORBA_Environment     *ev)
57 {
58   AtkSelection *selection = get_selection_from_servant (servant);
59
60   g_return_val_if_fail (selection != NULL, 0);
61
62   return atk_selection_get_selection_count (selection);
63 }
64
65
66 static Accessibility_Accessible
67 impl_getSelectedChild (PortableServer_Servant servant,
68                        const CORBA_long       selectedChildIndex,
69                        CORBA_Environment     *ev)
70 {
71   AtkObject    *atk_object;
72   AtkSelection *selection = get_selection_from_servant (servant);
73
74   g_return_val_if_fail (selection != NULL, CORBA_OBJECT_NIL);
75
76 #ifdef SPI_DEBUG
77   fprintf (stderr, "calling impl_getSelectedChild\n");
78 #endif
79
80   atk_object = atk_selection_ref_selection (selection,
81                                             selectedChildIndex);
82
83   g_return_val_if_fail (ATK_IS_OBJECT (atk_object), CORBA_OBJECT_NIL);
84
85 #ifdef SPI_DEBUG
86   fprintf (stderr, "child type is %s\n",
87            g_type_name (G_OBJECT_TYPE (atk_object)));
88 #endif
89
90   return spi_accessible_new_return (atk_object, TRUE, ev);
91 }
92
93
94 static CORBA_boolean
95 impl_selectChild (PortableServer_Servant servant,
96                   const CORBA_long       childIndex,
97                   CORBA_Environment     *ev)
98 {
99   AtkSelection *selection = get_selection_from_servant (servant);
100
101   g_return_val_if_fail (selection != NULL, FALSE);
102
103   return atk_selection_add_selection (selection, childIndex);
104 }
105
106
107 static CORBA_boolean
108 impl_deselectSelectedChild (PortableServer_Servant servant,
109                             const CORBA_long       selectedChildIndex,
110                             CORBA_Environment     *ev)
111 {
112   AtkSelection *selection = get_selection_from_servant (servant);
113
114   g_return_val_if_fail (selection != NULL, FALSE);
115
116   return atk_selection_remove_selection (selection, selectedChildIndex);
117 }
118
119
120
121 static CORBA_boolean
122 impl_deselectChild (PortableServer_Servant servant,
123                     const CORBA_long       selectedChildIndex,
124                     CORBA_Environment     *ev)
125 {
126   AtkSelection *selection = get_selection_from_servant (servant);
127   gint i, nselected;
128
129   g_return_val_if_fail (selection != NULL, FALSE);
130   nselected = atk_selection_get_selection_count (selection);
131   for (i=0; i<nselected; ++i)
132   {
133       AtkObject *selected_obj = atk_selection_ref_selection (selection, i);
134       if (atk_object_get_index_in_parent (selected_obj) == selectedChildIndex)
135       {
136           g_object_unref (G_OBJECT (selected_obj));
137           return atk_selection_remove_selection (selection, i);
138       }
139       g_object_unref (G_OBJECT (selected_obj));
140   }
141   return FALSE;
142 }
143
144
145 static CORBA_boolean
146 impl_isChildSelected (PortableServer_Servant servant,
147                       const CORBA_long       childIndex,
148                       CORBA_Environment     *ev)
149 {
150   AtkSelection *selection = get_selection_from_servant (servant);
151
152   g_return_val_if_fail (selection != NULL, FALSE);
153
154   return atk_selection_is_child_selected (selection, childIndex);
155 }
156
157
158 static CORBA_boolean 
159 impl_selectAll (PortableServer_Servant servant,
160                 CORBA_Environment     *ev)
161 {
162   AtkSelection *selection = get_selection_from_servant (servant);
163
164   g_return_val_if_fail (selection != NULL, FALSE);
165
166   return atk_selection_select_all_selection (selection);
167
168 }
169
170
171 static CORBA_boolean
172 impl_clearSelection (PortableServer_Servant servant,
173                      CORBA_Environment     *ev)
174 {
175   AtkSelection *selection = get_selection_from_servant (servant);
176
177   g_return_val_if_fail (selection != NULL, FALSE);
178
179   return atk_selection_clear_selection (selection);
180 }
181
182
183 static void
184 spi_selection_class_init (SpiSelectionClass *klass)
185 {
186   POA_Accessibility_Selection__epv *epv = &klass->epv;
187
188   /* Initialize epv table */
189
190   epv->_get_nSelectedChildren = impl__get_nSelectedChildren;
191   epv->getSelectedChild       = impl_getSelectedChild;
192   epv->selectChild            = impl_selectChild;
193   epv->deselectSelectedChild  = impl_deselectSelectedChild;
194   epv->deselectChild          = impl_deselectChild;
195   epv->isChildSelected        = impl_isChildSelected;
196   epv->selectAll              = impl_selectAll;
197   epv->clearSelection         = impl_clearSelection;
198 }
199
200
201 static void
202 spi_selection_init (SpiSelection *selection)
203 {
204 }
205
206
207 BONOBO_TYPE_FUNC_FULL (SpiSelection,
208                        Accessibility_Selection,
209                        SPI_TYPE_BASE,
210                        spi_selection)