2001-12-18 Marc Mulcahy <marc.mulcahy@sun.com>
[platform/core/uifw/at-spi2-atk.git] / libspi / table.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 /* table.c : implements the Table interface */
24
25 #include <config.h>
26 #include <stdio.h>
27 #include <bonobo/bonobo-exception.h>
28 #include <atk/atktable.h>
29 #include <libspi/accessible.h>
30 #include <libspi/table.h>
31
32
33 SpiTable *
34 spi_table_interface_new (AtkObject *obj)
35 {
36   SpiTable *new_table = g_object_new (SPI_TABLE_TYPE, NULL);
37
38   spi_base_construct (SPI_BASE (new_table), G_OBJECT(obj));
39
40   return new_table;
41 }
42
43
44 static AtkTable *
45 get_table_from_servant (PortableServer_Servant servant)
46 {
47   SpiBase *object = SPI_BASE (bonobo_object_from_servant (servant));
48
49   g_return_val_if_fail (object, NULL);
50   g_return_val_if_fail (ATK_IS_OBJECT(object->gobj), NULL);
51   return ATK_TABLE (object->gobj);
52 }
53
54
55 static Accessibility_Accessible
56 impl__get_caption (PortableServer_Servant servant,
57                    CORBA_Environment     *ev)
58 {
59   AtkObject *atk_object;
60   AtkTable  *table = get_table_from_servant (servant);
61
62   g_return_val_if_fail (table != NULL, CORBA_OBJECT_NIL);
63
64   atk_object = atk_table_get_caption (table);
65
66   return spi_accessible_new_return (atk_object, FALSE, ev);
67 }
68
69
70 static Accessibility_Accessible
71 impl__get_summary (PortableServer_Servant servant,
72                    CORBA_Environment     *ev)
73 {
74   AtkObject *atk_object;
75   AtkTable  *table = get_table_from_servant (servant);
76
77   g_return_val_if_fail (table != NULL, CORBA_OBJECT_NIL);
78
79   atk_object = atk_table_get_summary (table);
80
81   return spi_accessible_new_return (atk_object, FALSE, ev);
82 }
83
84
85 static CORBA_long
86 impl__get_nRows (PortableServer_Servant servant,
87                  CORBA_Environment     *ev)
88 {
89   AtkTable *table = get_table_from_servant (servant);
90
91   g_return_val_if_fail (table != NULL, 0);
92
93   return (CORBA_long) atk_table_get_n_rows (table);
94 }
95
96
97 static CORBA_long
98 impl__get_nColumns (PortableServer_Servant servant,
99                     CORBA_Environment     *ev)
100 {
101   AtkTable *table = get_table_from_servant (servant);
102
103   g_return_val_if_fail (table != NULL, 0);
104
105   return (CORBA_long) atk_table_get_n_columns (table);
106 }
107
108
109 static Accessibility_Accessible
110 impl_getAccessibleAt (PortableServer_Servant servant,
111                       const CORBA_long       row,
112                       const CORBA_long       column,
113                       CORBA_Environment     *ev)
114 {
115   AtkObject *atk_object;
116   AtkTable  *table = get_table_from_servant (servant);
117
118   g_return_val_if_fail (table != NULL, CORBA_OBJECT_NIL);
119
120   atk_object = atk_table_ref_at (table,
121                                  (gint) row, (gint) column);
122
123   return spi_accessible_new_return (atk_object, TRUE, ev);
124 }
125
126
127 static CORBA_long
128 impl_getIndexAt (PortableServer_Servant servant,
129                  const CORBA_long       row,
130                  const CORBA_long       column,
131                  CORBA_Environment     *ev)
132 {
133   AtkTable *table = get_table_from_servant (servant);
134
135   g_return_val_if_fail (table != NULL, 0);
136
137   return (CORBA_long)
138     atk_table_get_index_at (table, (gint) row, (gint) column);
139 }
140
141
142 static CORBA_long
143 impl_getRowAtIndex (PortableServer_Servant servant,
144                     const CORBA_long       index,
145                     CORBA_Environment     *ev)
146 {
147   AtkTable *table = get_table_from_servant (servant);
148
149   g_return_val_if_fail (table != NULL, 0);
150
151   return (CORBA_long)
152     atk_table_get_row_at_index (table, (gint) index);
153 }
154
155
156 static CORBA_long
157 impl_getColumnAtIndex (PortableServer_Servant servant,
158                        const CORBA_long       index,
159                        CORBA_Environment     *ev)
160 {
161   AtkTable *table = get_table_from_servant (servant);
162
163   g_return_val_if_fail (table != NULL, 0);
164
165   return (CORBA_long)
166     atk_table_get_column_at_index (table, (gint) index);
167 }
168
169
170 static CORBA_string
171 impl_getRowDescription (PortableServer_Servant servant,
172                         const CORBA_long       row,
173                         CORBA_Environment     *ev)
174 {
175   const char *rv;
176   AtkTable   *table = get_table_from_servant (servant);
177
178   g_return_val_if_fail (table != NULL, 0);
179   
180   rv = atk_table_get_row_description (table, row);
181
182   if (rv)
183     {
184       return CORBA_string_dup (rv);
185     }
186   else
187     {
188       return CORBA_string_dup ("");
189     }
190 }
191
192
193 static CORBA_string
194 impl_getColumnDescription (PortableServer_Servant servant,
195                            const CORBA_long       column,
196                            CORBA_Environment     *ev)
197 {
198   const char *rv;
199   AtkTable   *table = get_table_from_servant (servant);
200
201   g_return_val_if_fail (table != NULL, CORBA_string_dup (""));
202   
203   rv = atk_table_get_row_description (table, column);
204
205   if (rv)
206     {
207       return CORBA_string_dup (rv);
208     }
209   else
210     {
211       return CORBA_string_dup ("");
212     }
213 }
214
215
216 static CORBA_long
217 impl_getRowExtentAt (PortableServer_Servant servant,
218                      const CORBA_long       row,
219                      const CORBA_long       column,
220                      CORBA_Environment     *ev)
221 {
222   AtkTable *table = get_table_from_servant (servant);
223
224   g_return_val_if_fail (table != NULL, -1);
225
226   return (CORBA_long)
227     atk_table_get_row_extent_at (table,
228                                  (gint) row, (gint) column);
229 }
230
231
232 static CORBA_long
233 impl_getColumnExtentAt (PortableServer_Servant servant,
234                         const CORBA_long       row,
235                         const CORBA_long       column,
236                         CORBA_Environment     *ev)
237 {
238   AtkTable *table = get_table_from_servant (servant);
239
240   g_return_val_if_fail (table != NULL, -1);
241
242   return (CORBA_long)
243     atk_table_get_column_extent_at (table,
244                                     (gint) row, (gint) column);
245 }
246
247
248 static Accessibility_Table
249 impl_getRowHeader (PortableServer_Servant servant,
250                    const CORBA_long       row,
251                    CORBA_Environment     *ev)
252 {
253   AtkObject *header;
254   AtkTable  *table = get_table_from_servant (servant);
255
256   g_return_val_if_fail (table != NULL, CORBA_OBJECT_NIL);
257
258   header = atk_table_get_row_header (table, (gint) row);
259
260   return spi_accessible_new_return (header, FALSE, ev);
261 }
262
263
264 static Accessibility_Table
265 impl_getColumnHeader (PortableServer_Servant servant,
266                       const CORBA_long       column,
267                       CORBA_Environment     *ev)
268 {
269   AtkObject *header;
270   AtkTable  *table = get_table_from_servant (servant);
271
272   g_return_val_if_fail (table != NULL, CORBA_OBJECT_NIL);
273
274   header = atk_table_get_column_header (table, (gint) column);
275
276   return spi_accessible_new_return (header, FALSE, ev);
277 }
278
279
280 static Accessibility_LongSeq *
281 impl_getSelectedRows (PortableServer_Servant servant,
282                       CORBA_Environment     *ev)
283 {
284   gint *selectedRows;
285   gint length;
286   Accessibility_LongSeq *retval;
287   AtkTable *table = get_table_from_servant (servant);
288
289   bonobo_return_val_if_fail (table != NULL, NULL, ev);
290
291   length = atk_table_get_selected_rows (table, &selectedRows);
292
293   bonobo_return_val_if_fail (length > 0, NULL, ev);
294
295   retval = Accessibility_LongSeq__alloc ();
296   retval->_maximum = retval->_length = (CORBA_long) length;
297   retval->_buffer = Accessibility_LongSeq_allocbuf (length);
298
299   while (--length)
300     {
301       retval->_buffer[length] = (CORBA_long) selectedRows[length];
302     }
303
304   g_free ((gpointer) selectedRows);
305
306   return retval;
307 }
308
309
310 static Accessibility_LongSeq *
311 impl_getSelectedColumns (PortableServer_Servant servant,
312                          CORBA_Environment     *ev)
313 {
314   gint *selectedColumns;
315   gint length;
316   Accessibility_LongSeq *retval;
317   AtkTable *table = get_table_from_servant (servant);
318
319   bonobo_return_val_if_fail (table != NULL, NULL, ev);
320
321   length = atk_table_get_selected_columns (table, &selectedColumns);
322
323   bonobo_return_val_if_fail (length >= 0, NULL, ev);
324
325   retval = Accessibility_LongSeq__alloc ();
326   retval->_maximum = retval->_length = (CORBA_long) length;
327   retval->_buffer = Accessibility_LongSeq_allocbuf (length);
328
329   while (--length)
330     {
331       retval->_buffer[length] = (CORBA_long) selectedColumns[length];
332     }
333
334   g_free ((gpointer) selectedColumns);
335
336   return retval;
337 }
338
339
340 static CORBA_boolean
341 impl_isRowSelected (PortableServer_Servant servant,
342                     const CORBA_long       row,
343                     CORBA_Environment     *ev)
344 {
345   AtkTable *table = get_table_from_servant (servant);
346
347   g_return_val_if_fail (table != NULL, FALSE);
348
349   return (CORBA_boolean) atk_table_is_row_selected (table, (gint) row);
350 }
351
352
353 static CORBA_boolean
354 impl_isColumnSelected (PortableServer_Servant servant,
355                        const CORBA_long       column,
356                        CORBA_Environment     *ev)
357 {
358   AtkTable *table = get_table_from_servant (servant);
359
360   g_return_val_if_fail (table != NULL, FALSE);
361
362   return (CORBA_boolean) atk_table_is_column_selected (table, (gint) column);
363 }
364
365
366 static CORBA_boolean
367 impl_isSelected (PortableServer_Servant servant,
368                  const CORBA_long       row,
369                  const CORBA_long       column,
370                  CORBA_Environment     *ev)
371 {
372   AtkTable *table = get_table_from_servant (servant);
373
374   g_return_val_if_fail (table != NULL, FALSE);
375
376   return (CORBA_boolean) atk_table_is_selected (table,
377                                                 (gint) row, (gint) column);
378 }
379
380
381 static void
382 spi_table_class_init (SpiTableClass *klass)
383 {
384   POA_Accessibility_Table__epv *epv = &klass->epv;
385
386   /* Initialize epv table */
387
388   epv->_get_caption = impl__get_caption;
389   epv->_get_summary = impl__get_summary;
390   epv->_get_nRows = impl__get_nRows;
391   epv->_get_nColumns = impl__get_nColumns;
392   epv->getAccessibleAt = impl_getAccessibleAt;
393   epv->getIndexAt = impl_getIndexAt;
394   epv->getRowAtIndex = impl_getRowAtIndex;
395   epv->getColumnAtIndex = impl_getColumnAtIndex;
396   epv->getRowDescription = impl_getRowDescription;
397   epv->getColumnDescription = impl_getColumnDescription;
398   epv->getRowExtentAt = impl_getRowExtentAt;
399   epv->getColumnExtentAt = impl_getColumnExtentAt;
400   epv->getRowHeader = impl_getRowHeader;
401   epv->getColumnHeader = impl_getColumnHeader;
402   epv->getSelectedRows = impl_getSelectedRows;
403   epv->getSelectedColumns = impl_getSelectedColumns;
404   epv->isRowSelected = impl_isRowSelected;
405   epv->isColumnSelected = impl_isColumnSelected;
406   epv->isSelected = impl_isSelected;
407 }
408
409 static void
410 spi_table_init (SpiTable *table)
411 {
412 }
413
414 BONOBO_TYPE_FUNC_FULL (SpiTable,
415                        Accessibility_Table,
416                        SPI_TYPE_BASE,
417                        spi_table);