Cleaned up some suspect int* casts, and added assertions to text calls in libspi
[platform/core/uifw/at-spi2-atk.git] / libspi / text.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 /*
24  * component.c : bonobo wrapper for accessible component implementation
25  *
26  */
27 #include <config.h>
28 #include <bonobo/Bonobo.h>
29
30 #include <stdio.h>
31 #include <stdlib.h>
32
33 /*
34  * This pulls the CORBA definitions for the "Accessibility::Accessible" server
35  */
36 #include <libspi/Accessibility.h>
37
38 /*
39  * This pulls the definition of the Text bonobo object
40  */
41 #include "text.h"
42
43 /*
44  * Static function declarations
45  */
46
47 static void
48 text_class_init (TextClass *klass);
49 static void
50 text_init (Text *text);
51 static void
52 text_finalize (GObject *obj);
53 static CORBA_string
54 impl_getText (PortableServer_Servant _servant,
55               const CORBA_long startOffset,
56               const CORBA_long endOffset,
57               CORBA_Environment * ev);
58 CORBA_string
59 impl_getTextAfterOffset (PortableServer_Servant _servant,
60                          const CORBA_long offset,
61                          const
62                          Accessibility_TEXT_BOUNDARY_TYPE
63                          type, CORBA_long * startOffset,
64                          CORBA_long * endOffset,
65                          CORBA_Environment * ev);
66 static CORBA_string
67 impl_getTextAtOffset (PortableServer_Servant _servant,
68                       const CORBA_long offset,
69                       const Accessibility_TEXT_BOUNDARY_TYPE type,
70                       CORBA_long * startOffset,
71                       CORBA_long * endOffset,
72                       CORBA_Environment * ev);
73
74 static CORBA_unsigned_long
75 impl_getCharacterAtOffset (PortableServer_Servant _servant,
76                            const CORBA_long offset,
77                            CORBA_Environment * ev);
78 static CORBA_string
79 impl_getTextBeforeOffset (PortableServer_Servant _servant,
80                           const CORBA_long offset,
81                           const
82                           Accessibility_TEXT_BOUNDARY_TYPE
83                           type, CORBA_long * startOffset,
84                           CORBA_long * endOffset,
85                           CORBA_Environment * ev);
86 static CORBA_long
87 impl__get_caretOffset (PortableServer_Servant _servant,
88                      CORBA_Environment * ev);
89 static CORBA_string
90 impl_getAttributes (PortableServer_Servant _servant,
91                        const CORBA_long offset,
92                        CORBA_long * startOffset,
93                        CORBA_long * endOffset,
94                        CORBA_Environment * ev);
95 static void 
96 impl_getCharacterExtents (PortableServer_Servant _servant,
97                           const CORBA_long offset, CORBA_long * x,
98                           CORBA_long * y, CORBA_long * width,
99                           CORBA_long * height,
100                           const CORBA_short coordType,
101                           CORBA_Environment * ev);
102 static CORBA_long
103 impl__get_characterCount (PortableServer_Servant _servant,
104                         CORBA_Environment * ev);
105 static CORBA_long
106 impl_getOffsetAtPoint (PortableServer_Servant _servant,
107                        const CORBA_long x, const CORBA_long y,
108                        const CORBA_short coordType,
109                        CORBA_Environment * ev);
110 static CORBA_long
111 impl_getNSelections (PortableServer_Servant _servant,
112                      CORBA_Environment * ev);
113 static void 
114 impl_getSelection (PortableServer_Servant _servant,
115                    const CORBA_long selectionNum,
116                    CORBA_long * startOffset, CORBA_long * endOffset,
117                    CORBA_Environment * ev);
118 static CORBA_boolean
119 impl_addSelection (PortableServer_Servant _servant,
120                    const CORBA_long startOffset,
121                    const CORBA_long endOffset,
122                    CORBA_Environment * ev);
123 static CORBA_boolean
124 impl_removeSelection (PortableServer_Servant _servant,
125                       const CORBA_long selectionNum,
126                       CORBA_Environment * ev);
127 static CORBA_boolean
128 impl_setSelection (PortableServer_Servant _servant,
129                    const CORBA_long selectionNum,
130                    const CORBA_long startOffset,
131                    const CORBA_long endOffset,
132                    CORBA_Environment * ev);
133 static CORBA_boolean
134 impl_setCaretOffset (PortableServer_Servant _servant,
135                      const CORBA_long value,
136                      CORBA_Environment * ev);
137   
138  
139
140 static GObjectClass *parent_class;
141
142 GType
143 text_get_type (void)
144 {
145   static GType type = 0;
146
147   if (!type) {
148     static const GTypeInfo tinfo = {
149       sizeof (TextClass),
150       (GBaseInitFunc) NULL,
151       (GBaseFinalizeFunc) NULL,
152       (GClassInitFunc) text_class_init,
153       (GClassFinalizeFunc) NULL,
154       NULL, /* class data */
155       sizeof (Text),
156       0, /* n preallocs */
157       (GInstanceInitFunc) text_init,
158                         NULL /* value table */
159     };
160
161     /*
162      * Bonobo_type_unique auto-generates a load of
163      * CORBA structures for us. All derived types must
164      * use bonobo_type_unique.
165      */
166     type = bonobo_type_unique (
167                                BONOBO_OBJECT_TYPE,
168                                POA_Accessibility_Text__init,
169                                NULL,
170                                G_STRUCT_OFFSET (TextClass, epv),
171                                &tinfo,
172                                "AccessibleText");
173   }
174
175   return type;
176 }
177
178 static void
179 text_class_init (TextClass *klass)
180 {
181   GObjectClass * object_class = (GObjectClass *) klass;
182   POA_Accessibility_Text__epv *epv = &klass->epv;
183   parent_class = g_type_class_peek_parent (klass);
184
185   object_class->finalize = text_finalize;
186
187
188   /* Initialize epv table */
189
190   epv->getText = impl_getText;
191   epv->getTextAfterOffset = impl_getTextAfterOffset;
192   epv->getCharacterAtOffset = impl_getCharacterAtOffset;
193   epv->getTextAtOffset = impl_getTextAtOffset;
194   epv->getTextBeforeOffset = impl_getTextBeforeOffset;
195   epv->_get_caretOffset = impl__get_caretOffset;
196   epv->getAttributes = impl_getAttributes;
197   epv->getCharacterExtents = impl_getCharacterExtents;
198   epv->_get_characterCount = impl__get_characterCount;
199   epv->getOffsetAtPoint = impl_getOffsetAtPoint;
200   epv->getNSelections = impl_getNSelections;
201   epv->getSelection = impl_getSelection;
202   epv->addSelection = impl_addSelection;
203   epv->removeSelection = impl_removeSelection;
204   epv->setSelection = impl_setSelection;
205   epv->setCaretOffset = impl_setCaretOffset;
206 }
207
208 static void
209 text_init (Text *text)
210 {
211 }
212
213 static void
214 text_finalize (GObject *obj)
215 {
216   Text *text = TEXT (obj);
217   g_object_unref (text->atko);
218   text->atko = NULL;
219   parent_class->finalize (obj);
220 }
221
222 Text *
223 text_interface_new (AtkObject *obj)
224 {
225   Text *new_text = 
226     TEXT(g_object_new (TEXT_TYPE, NULL));
227   new_text->atko = obj;
228   g_object_ref (obj);
229   return new_text;
230 }
231
232
233
234 static CORBA_string
235 impl_getText (PortableServer_Servant _servant,
236               const CORBA_long startOffset,
237               const CORBA_long endOffset,
238               CORBA_Environment * ev)
239 {
240   Text *text;
241   gchar *txt;
242   CORBA_char *rv;
243   BonoboObject *obj;
244   
245   obj = (bonobo_object_from_servant (_servant));
246   g_return_val_if_fail (IS_TEXT (obj), (CORBA_char *)"");
247   text = TEXT (obj);
248   g_return_val_if_fail (ATK_IS_TEXT (text->atko), (CORBA_char *)"");
249   
250   txt = atk_text_get_text (ATK_TEXT(text->atko),
251                        (gint) startOffset, (gint) endOffset);
252   if (txt)
253     {
254       rv = CORBA_string_dup (txt);
255       g_free (txt);
256     }
257   else
258     rv = CORBA_string_dup ("");
259   return rv;
260 }
261
262
263
264 CORBA_string
265 impl_getTextAfterOffset (PortableServer_Servant _servant,
266                          const CORBA_long offset,
267                          const
268                          Accessibility_TEXT_BOUNDARY_TYPE
269                          type, CORBA_long * startOffset,
270                          CORBA_long * endOffset,
271                          CORBA_Environment * ev)
272 {
273   Text *text;
274   gchar *txt;
275   CORBA_char *rv;
276   gint intStartOffset, intEndOffset;
277   BonoboObject *obj;
278
279   obj = (bonobo_object_from_servant (_servant));
280   g_return_val_if_fail (IS_TEXT (obj), (CORBA_char *)"");
281   text = TEXT (obj);
282   g_return_val_if_fail (ATK_IS_TEXT (text->atko), (CORBA_char *)"");
283   
284   txt = atk_text_get_text_after_offset (ATK_TEXT(text->atko),
285                                     (gint) offset, (AtkTextBoundary) type,
286                                     &intStartOffset, &intEndOffset);
287   *startOffset = (CORBA_long) intStartOffset;
288   *endOffset = (CORBA_long) intEndOffset;
289
290   if (txt)
291     {
292       rv = CORBA_string_dup (txt);
293       g_free (txt);
294       }
295   else
296     rv = CORBA_string_dup ("");
297   return rv;
298 }
299
300
301
302 static CORBA_string
303 impl_getTextAtOffset (PortableServer_Servant _servant,
304                       const CORBA_long offset,
305                       const Accessibility_TEXT_BOUNDARY_TYPE type,
306                       CORBA_long * startOffset,
307                       CORBA_long * endOffset,
308                       CORBA_Environment * ev)
309 {
310   Text *text;
311   CORBA_char *txt;
312   CORBA_char *rv;
313   gint intStartOffset, intEndOffset;
314   BonoboObject *obj;
315
316   obj = (bonobo_object_from_servant (_servant));
317   g_return_val_if_fail (IS_TEXT (obj), (CORBA_char *)"");
318   text = TEXT (obj);
319   g_return_val_if_fail (ATK_IS_TEXT (text->atko), (CORBA_char *)"");
320
321   txt = (CORBA_char *) atk_text_get_text_at_offset (ATK_TEXT (text->atko),
322                                     (gint) offset, (AtkTextBoundary) type,
323                                     &intStartOffset, &intEndOffset);
324   *startOffset = (CORBA_long) intStartOffset;
325   *endOffset = (CORBA_long) intEndOffset;
326
327   if (txt)
328     {
329       rv = CORBA_string_dup (txt);
330       g_free (txt);
331     }
332   else
333     rv = CORBA_string_dup ("");
334
335   return rv;
336 }
337
338
339 static CORBA_unsigned_long
340 impl_getCharacterAtOffset (PortableServer_Servant _servant,
341                            const CORBA_long offset,
342                            CORBA_Environment * ev)
343 {
344   Text *text;
345   BonoboObject *obj;
346   obj = (bonobo_object_from_servant (_servant));
347   
348   g_return_val_if_fail (IS_TEXT (obj), (CORBA_unsigned_long)0);
349   text = TEXT (obj);
350   g_return_val_if_fail (ATK_IS_TEXT (text->atko), (CORBA_unsigned_long)0);
351
352   return (CORBA_unsigned_long)
353     atk_text_get_character_at_offset (ATK_TEXT(text->atko), (gint) offset);
354 }
355
356
357 static CORBA_string
358 impl_getTextBeforeOffset (PortableServer_Servant _servant,
359                           const CORBA_long offset,
360                           const
361                           Accessibility_TEXT_BOUNDARY_TYPE
362                           type, CORBA_long * startOffset,
363                           CORBA_long * endOffset,
364                           CORBA_Environment * ev)
365 {
366   Text *text;
367   gchar *txt;
368   CORBA_char *rv;
369   gint intStartOffset, intEndOffset;
370   BonoboObject *obj;
371
372   obj = (bonobo_object_from_servant (_servant));
373   g_return_val_if_fail (IS_TEXT (obj), (CORBA_char *)"");
374   text = TEXT (obj);
375   g_return_val_if_fail (ATK_IS_TEXT (text->atko), (CORBA_char *)"");
376
377   txt = atk_text_get_text_before_offset (ATK_TEXT(text->atko),
378                                     (gint) offset, (AtkTextBoundary) type,
379                                     &intStartOffset, &intEndOffset);
380   *startOffset = (CORBA_long) intStartOffset;
381   *endOffset = (CORBA_long) intEndOffset;
382
383   if (txt)
384     {
385       rv = CORBA_string_dup (txt);
386       g_free (txt);
387     }
388   else
389     rv = CORBA_string_dup ("");
390   return rv;
391 }
392
393
394 static CORBA_long
395 impl__get_caretOffset (PortableServer_Servant _servant,
396                      CORBA_Environment * ev)
397 {
398   Text *text;
399   BonoboObject *obj;
400
401   obj = (bonobo_object_from_servant (_servant));
402   g_return_val_if_fail (IS_TEXT (obj), (CORBA_long)-1);
403   text = TEXT (obj);
404   g_return_val_if_fail (ATK_IS_TEXT (text->atko), (CORBA_long)-1);
405   
406   return (CORBA_long)
407     atk_text_get_caret_offset (ATK_TEXT(text->atko));
408 }
409
410
411
412 static CORBA_string
413 impl_getAttributes (PortableServer_Servant _servant,
414                        const CORBA_long offset,
415                        CORBA_long * startOffset,
416                        CORBA_long * endOffset,
417                        CORBA_Environment * ev)
418 {
419   Text *text;
420   BonoboObject *obj;
421
422   obj = (bonobo_object_from_servant (_servant));
423   g_return_val_if_fail (IS_TEXT (obj), (CORBA_char *)"");
424   text = TEXT (obj);
425   g_return_val_if_fail (ATK_IS_TEXT (text->atko), (CORBA_char *)"");
426
427   g_print ("getAttributes not yet implemented.\n");
428 }
429
430 static void 
431 impl_getCharacterExtents (PortableServer_Servant _servant,
432                           const CORBA_long offset, CORBA_long * x,
433                           CORBA_long * y, CORBA_long * width,
434                           CORBA_long * height,
435                           const CORBA_short coordType,
436                           CORBA_Environment * ev)
437 {
438   Text *text;
439   BonoboObject *obj;
440
441   obj = (bonobo_object_from_servant (_servant));
442   g_return_if_fail (IS_TEXT (obj));
443   text = TEXT (obj);
444   g_return_if_fail (ATK_IS_TEXT (text->atko));
445
446   atk_text_get_character_extents (ATK_TEXT(text->atko), (gint) offset,
447                                   (gint *) x, (gint *) y, (gint *) width, (gint *) height,
448                                   (AtkCoordType) coordType);
449 }
450
451
452
453 static CORBA_long
454 impl__get_characterCount (PortableServer_Servant _servant,
455                         CORBA_Environment * ev)
456 {
457   Text *text;
458   BonoboObject *obj;
459
460   obj = (bonobo_object_from_servant (_servant));
461   g_return_val_if_fail (IS_TEXT (obj), (CORBA_long)0);
462   text = TEXT (obj);
463
464   return (CORBA_long)
465     atk_text_get_character_count (ATK_TEXT(text->atko));
466 }
467
468
469
470 static CORBA_long
471 impl_getOffsetAtPoint (PortableServer_Servant _servant,
472                        const CORBA_long x, const CORBA_long y,
473                        const CORBA_short coordType,
474                        CORBA_Environment * ev)
475 {
476   Text *text;
477   BonoboObject *obj;
478
479   obj = (bonobo_object_from_servant (_servant));
480   g_return_val_if_fail (IS_TEXT (obj), (CORBA_long)-1);
481   text = TEXT (obj);
482
483   return (CORBA_long)
484     atk_text_get_offset_at_point (ATK_TEXT(text->atko),
485                                   (gint) x, (gint) y, (AtkCoordType) coordType);
486 }
487
488
489
490 static CORBA_long
491 impl_getNSelections (PortableServer_Servant _servant,
492                      CORBA_Environment * ev)
493 {
494   Text *text;
495   BonoboObject *obj;
496
497   obj = (bonobo_object_from_servant (_servant));
498   g_return_val_if_fail (IS_TEXT (obj), (CORBA_long)0);
499   text = TEXT (obj);
500
501   return (CORBA_long)
502     atk_text_get_n_selections (ATK_TEXT(text->atko));
503 }
504
505
506
507 static void 
508 impl_getSelection (PortableServer_Servant _servant,
509                    const CORBA_long selectionNum,
510                    CORBA_long * startOffset, CORBA_long * endOffset,
511                    CORBA_Environment * ev)
512 {
513   Text *text;
514   BonoboObject *obj;
515
516   obj = (bonobo_object_from_servant (_servant));
517   g_return_if_fail (IS_TEXT (obj));
518   text = TEXT (obj);
519
520   atk_text_get_selection (ATK_TEXT(text->atko), (gint) selectionNum,
521                           (gint *) startOffset, (gint *) endOffset);
522 }
523
524
525
526 static CORBA_boolean
527 impl_addSelection (PortableServer_Servant _servant,
528                    const CORBA_long startOffset,
529                    const CORBA_long endOffset,
530                    CORBA_Environment * ev)
531 {
532   Text *text;
533   BonoboObject *obj;
534
535   obj = (bonobo_object_from_servant (_servant));
536   g_return_val_if_fail (IS_TEXT (obj), (CORBA_boolean)FALSE);
537   text = TEXT (obj);
538
539   return (CORBA_boolean)
540     atk_text_add_selection (ATK_TEXT(text->atko),
541                             (gint) startOffset, (gint) endOffset);
542 }
543
544
545
546 static CORBA_boolean
547 impl_removeSelection (PortableServer_Servant _servant,
548                       const CORBA_long selectionNum,
549                       CORBA_Environment * ev)
550 {
551   Text *text;
552   BonoboObject *obj;
553
554   obj = (bonobo_object_from_servant (_servant));
555   g_return_val_if_fail (IS_TEXT (obj), (CORBA_boolean)FALSE);
556   text = TEXT (obj);
557
558   return (CORBA_boolean)
559     atk_text_remove_selection (ATK_TEXT(text->atko), (gint) selectionNum);
560 }
561
562
563
564 static CORBA_boolean
565 impl_setSelection (PortableServer_Servant _servant,
566                    const CORBA_long selectionNum,
567                    const CORBA_long startOffset,
568                    const CORBA_long endOffset,
569                    CORBA_Environment * ev)
570 {
571   Text *text;
572   BonoboObject *obj;
573
574   obj = (bonobo_object_from_servant (_servant));
575   g_return_val_if_fail (IS_TEXT (obj), (CORBA_boolean)FALSE);
576   text = TEXT (obj);
577
578   return (CORBA_boolean)
579     atk_text_set_selection (ATK_TEXT(text->atko),
580                             (gint) selectionNum, (gint) startOffset, (gint) endOffset);
581 }
582
583
584
585 static CORBA_boolean
586 impl_setCaretOffset (PortableServer_Servant _servant,
587                      const CORBA_long value,
588                      CORBA_Environment * ev)
589 {
590   Text *text;
591   BonoboObject *obj;
592
593   obj = (bonobo_object_from_servant (_servant));
594   g_return_val_if_fail (IS_TEXT (obj), (CORBA_boolean)FALSE);
595   text = TEXT (obj);
596
597   return (CORBA_boolean)
598     atk_text_set_caret_offset (ATK_TEXT(text->atko), (gint) value);
599 }
600
601
602
603 static void 
604 impl_getRowColAtOffset (PortableServer_Servant _servant,
605                         const CORBA_long offset, CORBA_long * row,
606                         CORBA_long * column, CORBA_Environment * ev)
607 {
608   Text *text;
609   BonoboObject *obj;
610
611   obj = (bonobo_object_from_servant (_servant));
612   g_return_if_fail (IS_TEXT (obj));
613   text = TEXT (obj);
614
615   g_print ("getRowColAtOffset not yet implemented\n");
616 }
617