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