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