Fixed string handling for NULL strings 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
32 /*
33  * This pulls the CORBA definitions for the "Accessibility::Accessible" server
34  */
35 #include <libspi/Accessibility.h>
36
37 /*
38  * This pulls the definition of the Text bonobo object
39  */
40 #include "text.h"
41
42 /*
43  * Static function declarations
44  */
45
46 static void
47 text_class_init (TextClass *klass);
48 static void
49 text_init (Text *text);
50 static void
51 text_finalize (GObject *obj);
52 static CORBA_string
53 impl_getText (PortableServer_Servant _servant,
54               const CORBA_long startOffset,
55               const CORBA_long endOffset,
56               CORBA_Environment * ev);
57 CORBA_string
58 impl_getTextAfterOffset (PortableServer_Servant _servant,
59                          const CORBA_long offset,
60                          const
61                          Accessibility_TEXT_BOUNDARY_TYPE
62                          type, CORBA_long * startOffset,
63                          CORBA_long * endOffset,
64                          CORBA_Environment * ev);
65 static CORBA_string
66 impl_getTextAtOffset (PortableServer_Servant _servant,
67                       const CORBA_long offset,
68                       const Accessibility_TEXT_BOUNDARY_TYPE
69                       type, CORBA_long * startOffset,
70                       CORBA_long * endOffset,
71                       CORBA_Environment * ev);
72 static CORBA_wchar
73 impl_getCharacterAtOffset (PortableServer_Servant _servant,
74                            const CORBA_long offset,
75                            CORBA_Environment * ev);
76 static CORBA_string
77 impl_getTextBeforeOffset (PortableServer_Servant _servant,
78                           const CORBA_long offset,
79                           const
80                           Accessibility_TEXT_BOUNDARY_TYPE
81                           type, CORBA_long * startOffset,
82                           CORBA_long * endOffset,
83                           CORBA_Environment * ev);
84 static CORBA_long
85 impl__get_caretOffset (PortableServer_Servant _servant,
86                      CORBA_Environment * ev);
87 static CORBA_string
88 impl_getAttributes (PortableServer_Servant _servant,
89                        const CORBA_long offset,
90                        CORBA_long * startOffset,
91                        CORBA_long * endOffset,
92                        CORBA_Environment * ev);
93 static void 
94 impl_getCharacterExtents (PortableServer_Servant _servant,
95                           const CORBA_long offset, CORBA_long * x,
96                           CORBA_long * y, CORBA_long * width,
97                           CORBA_long * height,
98                           const CORBA_short coordType,
99                           CORBA_Environment * ev);
100 static CORBA_long
101 impl__get_characterCount (PortableServer_Servant _servant,
102                         CORBA_Environment * ev);
103 static CORBA_long
104 impl_getOffsetAtPoint (PortableServer_Servant _servant,
105                        const CORBA_long x, const CORBA_long y,
106                        const CORBA_short coordType,
107                        CORBA_Environment * ev);
108 static CORBA_long
109 impl_getNSelections (PortableServer_Servant _servant,
110                      CORBA_Environment * ev);
111 static void 
112 impl_getSelection (PortableServer_Servant _servant,
113                    const CORBA_long selectionNum,
114                    CORBA_long * startOffset, CORBA_long * endOffset,
115                    CORBA_Environment * ev);
116 static CORBA_boolean
117 impl_addSelection (PortableServer_Servant _servant,
118                    const CORBA_long startOffset,
119                    const CORBA_long endOffset,
120                    CORBA_Environment * ev);
121 static CORBA_boolean
122 impl_removeSelection (PortableServer_Servant _servant,
123                       const CORBA_long selectionNum,
124                       CORBA_Environment * ev);
125 static CORBA_boolean
126 impl_setSelection (PortableServer_Servant _servant,
127                    const CORBA_long selectionNum,
128                    const CORBA_long startOffset,
129                    const CORBA_long endOffset,
130                    CORBA_Environment * ev);
131 static CORBA_boolean
132 impl_setCaretOffset (PortableServer_Servant _servant,
133                      const CORBA_long value,
134                      CORBA_Environment * ev);
135   
136  
137
138 static GObjectClass *parent_class;
139
140 GType
141 text_get_type (void)
142 {
143   static GType type = 0;
144
145   if (!type) {
146     static const GTypeInfo tinfo = {
147       sizeof (TextClass),
148       (GBaseInitFunc) NULL,
149       (GBaseFinalizeFunc) NULL,
150       (GClassInitFunc) text_class_init,
151       (GClassFinalizeFunc) NULL,
152       NULL, /* class data */
153       sizeof (Text),
154       0, /* n preallocs */
155       (GInstanceInitFunc) text_init,
156                         NULL /* value table */
157     };
158
159     /*
160      * Bonobo_type_unique auto-generates a load of
161      * CORBA structures for us. All derived types must
162      * use bonobo_type_unique.
163      */
164     type = bonobo_type_unique (
165                                BONOBO_OBJECT_TYPE,
166                                POA_Accessibility_Text__init,
167                                NULL,
168                                G_STRUCT_OFFSET (TextClass, epv),
169                                &tinfo,
170                                "AccessibleText");
171   }
172
173   return type;
174 }
175
176 static void
177 text_class_init (TextClass *klass)
178 {
179   GObjectClass * object_class = (GObjectClass *) klass;
180   POA_Accessibility_Text__epv *epv = &klass->epv;
181   parent_class = g_type_class_peek_parent (klass);
182
183   object_class->finalize = text_finalize;
184
185
186   /* Initialize epv table */
187
188   epv->getText = impl_getText;
189   epv->getTextAfterOffset = impl_getTextAfterOffset;
190   epv->getCharacterAtOffset = impl_getCharacterAtOffset;
191   epv->getTextAtOffset = impl_getTextAtOffset;
192   epv->getTextBeforeOffset = impl_getTextBeforeOffset;
193   epv->_get_caretOffset = impl__get_caretOffset;
194   epv->getAttributes = impl_getAttributes;
195   epv->getCharacterExtents = impl_getCharacterExtents;
196   epv->_get_characterCount = impl__get_characterCount;
197   epv->getOffsetAtPoint = impl_getOffsetAtPoint;
198   epv->getNSelections = impl_getNSelections;
199   epv->getSelection = impl_getSelection;
200   epv->addSelection = impl_addSelection;
201   epv->removeSelection = impl_removeSelection;
202   epv->setSelection = impl_setSelection;
203   epv->setCaretOffset = impl_setCaretOffset;
204 }
205
206 static void
207 text_init (Text *text)
208 {
209 }
210
211 static void
212 text_finalize (GObject *obj)
213 {
214   Text *text = TEXT (obj);
215   g_object_unref (text->atko);
216   text->atko = NULL;
217   parent_class->finalize (obj);
218 }
219
220 Text *
221 text_interface_new (AtkObject *obj)
222 {
223   Text *new_text = 
224     TEXT(g_object_new (TEXT_TYPE, NULL));
225   new_text->atko = obj;
226   g_object_ref (obj);
227   return new_text;
228 }
229
230
231
232 static CORBA_string
233 impl_getText (PortableServer_Servant _servant,
234               const CORBA_long startOffset,
235               const CORBA_long endOffset,
236               CORBA_Environment * ev)
237 {
238   Text *text = TEXT (bonobo_object_from_servant (_servant));
239   gchar *txt;
240   CORBA_char *rv;
241
242   txt = atk_text_get_text (ATK_TEXT(text->atko),
243                        (gint) startOffset, (gint) endOffset);
244   if (txt)
245     {
246       rv = CORBA_string_dup (txt);
247       g_free (txt);
248     }
249   else
250     rv = CORBA_string_dup ("");
251   return rv;
252 }
253
254
255
256 CORBA_string
257 impl_getTextAfterOffset (PortableServer_Servant _servant,
258                          const CORBA_long offset,
259                          const
260                          Accessibility_TEXT_BOUNDARY_TYPE
261                          type, CORBA_long * startOffset,
262                          CORBA_long * endOffset,
263                          CORBA_Environment * ev)
264 {
265   Text *text = TEXT (bonobo_object_from_servant (_servant));
266   gchar *txt;
267   CORBA_char *rv;
268
269
270   txt = atk_text_get_text_after_offset (ATK_TEXT(text->atko),
271                                     (gint) offset, (AtkTextBoundary) type,
272                                     (gint *) startOffset, (gint *) endOffset);
273   if (txt)
274     {
275       rv = CORBA_string_dup (txt);
276       g_free (txt);
277       }
278   else
279     rv = CORBA_string_dup ("");
280   return rv;
281 }
282
283
284
285 static CORBA_string
286 impl_getTextAtOffset (PortableServer_Servant _servant,
287                       const CORBA_long offset,
288                       const Accessibility_TEXT_BOUNDARY_TYPE
289                       type, CORBA_long * startOffset,
290                       CORBA_long * endOffset,
291                       CORBA_Environment * ev)
292 {
293   Text *text = TEXT (bonobo_object_from_servant (_servant));
294   gchar *txt;
295   CORBA_char *rv;
296
297   txt = atk_text_get_text_at_offset (ATK_TEXT(text->atko),
298                                     (gint) offset, (AtkTextBoundary) type,
299                                     (gint *) startOffset, (gint *) endOffset);
300   if (txt)
301     {
302       rv = CORBA_string_dup (txt);
303       g_free (txt);
304     }
305   else
306     rv = CORBA_string_dup ("");
307   return rv;
308 }
309
310
311
312 static CORBA_wchar
313 impl_getCharacterAtOffset (PortableServer_Servant _servant,
314                            const CORBA_long offset,
315                            CORBA_Environment * ev)
316 {
317   Text *text = TEXT (bonobo_object_from_servant (_servant));
318   return (CORBA_wchar)
319     atk_text_get_character_at_offset (ATK_TEXT(text->atko), (gint) offset);
320 }
321
322
323 static CORBA_string
324 impl_getTextBeforeOffset (PortableServer_Servant _servant,
325                           const CORBA_long offset,
326                           const
327                           Accessibility_TEXT_BOUNDARY_TYPE
328                           type, CORBA_long * startOffset,
329                           CORBA_long * endOffset,
330                           CORBA_Environment * ev)
331 {
332   Text *text = TEXT (bonobo_object_from_servant (_servant));
333   gchar *txt;
334   CORBA_char *rv;
335
336   txt = atk_text_get_text_before_offset (ATK_TEXT(text->atko),
337                                     (gint) offset, (AtkTextBoundary) type,
338                                     (gint *) startOffset, (gint *) endOffset);
339   if (txt)
340     {
341       rv = CORBA_string_dup (txt);
342       g_free (txt);
343     }
344   else
345     rv = CORBA_string_dup ("");
346   return rv;
347 }
348
349
350 static CORBA_long
351 impl__get_caretOffset (PortableServer_Servant _servant,
352                      CORBA_Environment * ev)
353 {
354   Text *text = TEXT (bonobo_object_from_servant (_servant));
355   return (CORBA_long)
356     atk_text_get_caret_offset (ATK_TEXT(text->atko));
357 }
358
359
360
361 static CORBA_string
362 impl_getAttributes (PortableServer_Servant _servant,
363                        const CORBA_long offset,
364                        CORBA_long * startOffset,
365                        CORBA_long * endOffset,
366                        CORBA_Environment * ev)
367 {
368   Text *text = TEXT (bonobo_object_from_servant (_servant));
369   g_print ("getAttributes not implemented.\n");
370 }
371
372
373
374 static void 
375 impl_getCharacterExtents (PortableServer_Servant _servant,
376                           const CORBA_long offset, CORBA_long * x,
377                           CORBA_long * y, CORBA_long * width,
378                           CORBA_long * height,
379                           const CORBA_short coordType,
380                           CORBA_Environment * ev)
381 {
382   Text *text = TEXT (bonobo_object_from_servant (_servant));
383   atk_text_get_character_extents (ATK_TEXT(text->atko), (gint) offset,
384                                   (gint *) x, (gint *) y, (gint *) width, (gint *) height,
385                                   (AtkCoordType) coordType);
386 }
387
388
389
390 static CORBA_long
391 impl__get_characterCount (PortableServer_Servant _servant,
392                         CORBA_Environment * ev)
393 {
394   Text *text = TEXT (bonobo_object_from_servant (_servant));
395   return (CORBA_long)
396     atk_text_get_character_count (ATK_TEXT(text->atko));
397 }
398
399
400
401 static CORBA_long
402 impl_getOffsetAtPoint (PortableServer_Servant _servant,
403                        const CORBA_long x, const CORBA_long y,
404                        const CORBA_short coordType,
405                        CORBA_Environment * ev)
406 {
407   Text *text = TEXT (bonobo_object_from_servant (_servant));
408   return (CORBA_long)
409     atk_text_get_offset_at_point (ATK_TEXT(text->atko),
410                                   (gint) x, (gint) y, (AtkCoordType) coordType);
411 }
412
413
414
415 static CORBA_long
416 impl_getNSelections (PortableServer_Servant _servant,
417                      CORBA_Environment * ev)
418 {
419   Text *text = TEXT (bonobo_object_from_servant (_servant));
420   return (CORBA_long)
421     atk_text_get_n_selections (ATK_TEXT(text->atko));
422 }
423
424
425
426 static void 
427 impl_getSelection (PortableServer_Servant _servant,
428                    const CORBA_long selectionNum,
429                    CORBA_long * startOffset, CORBA_long * endOffset,
430                    CORBA_Environment * ev)
431 {
432   Text *text = TEXT (bonobo_object_from_servant (_servant));
433   atk_text_get_selection (ATK_TEXT(text->atko), (gint) selectionNum,
434                           (gint *) startOffset, (gint *) endOffset);
435 }
436
437
438
439 static CORBA_boolean
440 impl_addSelection (PortableServer_Servant _servant,
441                    const CORBA_long startOffset,
442                    const CORBA_long endOffset,
443                    CORBA_Environment * ev)
444 {
445   Text *text = TEXT (bonobo_object_from_servant (_servant));
446   return (CORBA_boolean)
447     atk_text_add_selection (ATK_TEXT(text->atko),
448                             (gint) startOffset, (gint) endOffset);
449 }
450
451
452
453 static CORBA_boolean
454 impl_removeSelection (PortableServer_Servant _servant,
455                       const CORBA_long selectionNum,
456                       CORBA_Environment * ev)
457 {
458   Text *text = TEXT (bonobo_object_from_servant (_servant));
459   return (CORBA_boolean)
460     atk_text_remove_selection (ATK_TEXT(text->atko), (gint) selectionNum);
461 }
462
463
464
465 static CORBA_boolean
466 impl_setSelection (PortableServer_Servant _servant,
467                    const CORBA_long selectionNum,
468                    const CORBA_long startOffset,
469                    const CORBA_long endOffset,
470                    CORBA_Environment * ev)
471 {
472   Text *text = TEXT (bonobo_object_from_servant (_servant));
473   return (CORBA_boolean)
474     atk_text_set_selection (ATK_TEXT(text->atko),
475                             (gint) selectionNum, (gint) startOffset, (gint) endOffset);
476 }
477
478
479
480 static CORBA_boolean
481 impl_setCaretOffset (PortableServer_Servant _servant,
482                      const CORBA_long value,
483                      CORBA_Environment * ev)
484 {
485   Text *text = TEXT (bonobo_object_from_servant (_servant));
486   return (CORBA_boolean)
487     atk_text_set_caret_offset (ATK_TEXT(text->atko), (gint) value);
488 }
489
490
491
492 static void 
493 impl_getRowColAtOffset (PortableServer_Servant _servant,
494                         const CORBA_long offset, CORBA_long * row,
495                         CORBA_long * column, CORBA_Environment * ev)
496 {
497   Text *text = TEXT (bonobo_object_from_servant (_servant));
498 }
499