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