2007-03-29 Ariel Rios <ariel@gnu.org>
[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, 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 /* text.c : implements the Text interface */
25
26 #include <config.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <atk/atktext.h>
31 #include <libspi/text.h>
32 #include <libspi/spi-private.h>
33
34 /* Our parent Gtk object type */
35 #define PARENT_TYPE SPI_TYPE_BASE
36
37 typedef struct {
38   gint x;
39   gint y;
40   gint w;
41   gint h;
42 } SpiTextRect;
43
44 static AtkText *
45 get_text_from_servant (PortableServer_Servant servant)
46 {
47   SpiBase *object = SPI_BASE (bonobo_object_from_servant (servant));
48
49   g_return_val_if_fail (object, NULL);
50   g_return_val_if_fail (ATK_IS_OBJECT(object->gobj), NULL);
51   return ATK_TEXT (object->gobj);
52 }
53
54 static CORBA_string
55 impl_getText (PortableServer_Servant servant,
56               const CORBA_long       startOffset,
57               const CORBA_long       endOffset,
58               CORBA_Environment     *ev)
59 {
60   gchar *txt;
61   CORBA_string rv;
62   AtkText *text = get_text_from_servant (servant);
63
64   g_return_val_if_fail (text != NULL, CORBA_string_dup (""));
65   
66   txt = atk_text_get_text (text, startOffset, endOffset);
67   if (txt)
68     {
69       rv = CORBA_string_dup (txt);
70       g_free (txt);
71     }
72   else
73     rv = CORBA_string_dup ("");
74
75   return rv;
76 }
77
78
79 static CORBA_string
80 impl_getTextAfterOffset (PortableServer_Servant servant,
81                          const CORBA_long offset,
82                          const
83                          Accessibility_TEXT_BOUNDARY_TYPE
84                          type, CORBA_long * startOffset,
85                          CORBA_long * endOffset,
86                          CORBA_Environment *ev)
87 {
88   gchar *txt;
89   CORBA_char *rv;
90   gint intStartOffset, intEndOffset;
91   AtkText *text = get_text_from_servant (servant);
92
93   g_return_val_if_fail (text != NULL, CORBA_string_dup (""));
94
95   txt = atk_text_get_text_after_offset (text,
96                                         offset, (AtkTextBoundary) type,
97                                         &intStartOffset, &intEndOffset);
98   *startOffset = intStartOffset;
99   *endOffset = intEndOffset;
100
101   if (txt)
102     {
103       rv = CORBA_string_dup (txt);
104       g_free (txt);
105     }
106   else
107     rv = CORBA_string_dup ("");
108
109   return rv;
110 }
111
112
113 static CORBA_string
114 impl_getTextAtOffset (PortableServer_Servant servant,
115                       const CORBA_long offset,
116                       const Accessibility_TEXT_BOUNDARY_TYPE type,
117                       CORBA_long * startOffset,
118                       CORBA_long * endOffset,
119                       CORBA_Environment *ev)
120 {
121   gchar *txt;
122   CORBA_char *rv;
123   gint intStartOffset, intEndOffset;
124   AtkText *text = get_text_from_servant (servant);
125
126   g_return_val_if_fail (text != NULL, CORBA_string_dup (""));
127
128   txt = atk_text_get_text_at_offset (
129           text,
130           offset, (AtkTextBoundary) type,
131           &intStartOffset, &intEndOffset);
132
133   *startOffset = intStartOffset;
134   *endOffset = intEndOffset;
135
136   if (txt)
137     {
138       rv = CORBA_string_dup (txt);
139       g_free (txt);
140     }
141   else
142     rv = CORBA_string_dup ("");
143
144   return rv;
145 }
146
147
148 static CORBA_unsigned_long
149 impl_getCharacterAtOffset (PortableServer_Servant servant,
150                            const CORBA_long offset,
151                            CORBA_Environment *ev)
152 {
153   AtkText *text = get_text_from_servant (servant);
154
155   g_return_val_if_fail (text != NULL, 0);
156
157   return atk_text_get_character_at_offset (text, offset);
158 }
159
160
161 static CORBA_string
162 impl_getTextBeforeOffset (PortableServer_Servant servant,
163                           const CORBA_long offset,
164                           const
165                           Accessibility_TEXT_BOUNDARY_TYPE
166                           type, CORBA_long * startOffset,
167                           CORBA_long * endOffset,
168                           CORBA_Environment *ev)
169 {
170   gchar *txt;
171   CORBA_char *rv;
172   gint intStartOffset, intEndOffset;
173   AtkText *text = get_text_from_servant (servant);
174
175   g_return_val_if_fail (text != NULL, CORBA_string_dup (""));
176
177   txt = atk_text_get_text_before_offset (text,
178                                          offset, (AtkTextBoundary) type,
179                                          &intStartOffset, &intEndOffset);
180
181   *startOffset = intStartOffset;
182   *endOffset = intEndOffset;
183
184   if (txt)
185     {
186       rv = CORBA_string_dup (txt);
187       g_free (txt);
188     }
189   else
190     rv = CORBA_string_dup ("");
191
192   return rv;
193 }
194
195
196 static CORBA_long
197 impl__get_caretOffset (PortableServer_Servant servant,
198                      CORBA_Environment *ev)
199 {
200   AtkText *text = get_text_from_servant (servant);
201
202   g_return_val_if_fail (text != NULL, -1);
203
204   return atk_text_get_caret_offset (text);
205 }
206
207
208 static CORBA_char *
209 _string_from_attribute_set (AtkAttributeSet *set)
210 {
211   gchar *attributes, *tmp, *tmp2;
212   CORBA_char *rv;
213   GSList *cur_attr;
214   AtkAttribute *at;
215   
216   attributes = g_strdup ("");
217   cur_attr = (GSList *) set;
218   while (cur_attr)
219     {
220       at = (AtkAttribute *) cur_attr->data;
221       tmp = g_strdup_printf ("%s%s:%s%s",
222                              ((GSList *)(set) == cur_attr) ? "" : " ",
223                              at->name, at->value,
224                              (cur_attr->next) ? ";" : "");
225       tmp2 = g_strconcat (attributes, tmp, NULL);
226       g_free (tmp);
227       g_free (attributes);
228       attributes = tmp2;
229       cur_attr = cur_attr->next;
230     }
231   rv = CORBA_string_dup (attributes);
232   g_free (attributes);
233   return rv;
234 }
235
236
237
238 static CORBA_string
239 impl_getAttributes (PortableServer_Servant servant,
240                     const CORBA_long offset,
241                     CORBA_long * startOffset,
242                     CORBA_long * endOffset,
243                     CORBA_Environment *ev)
244 {
245   AtkAttributeSet *set;
246   gint intstart_offset, intend_offset;
247   CORBA_char *rv;
248   AtkText *text = get_text_from_servant (servant);
249
250   g_return_val_if_fail (text != NULL, CORBA_string_dup (""));
251
252   set = atk_text_get_run_attributes (text, offset,
253                                      &intstart_offset, &intend_offset);
254   *startOffset = intstart_offset;
255   *endOffset = intend_offset;
256   rv = _string_from_attribute_set (set);
257   atk_attribute_set_free (set);
258   return rv;  
259 }
260
261 static CORBA_string
262 impl_getAttributeValue (PortableServer_Servant servant,
263                         const CORBA_long offset,
264                         const CORBA_char *attributename,
265                         CORBA_long * startOffset,
266                         CORBA_long * endOffset,
267                         CORBA_boolean * defined,
268                         CORBA_Environment *ev)
269 {
270   AtkAttributeSet *set;
271   gint intstart_offset, intend_offset;
272   GSList *cur_attr;
273   CORBA_string rv = NULL;
274   AtkText *text = get_text_from_servant (servant);
275   AtkAttribute *at;
276
277   g_return_val_if_fail (text != NULL, CORBA_string_dup (""));
278
279   set = atk_text_get_run_attributes (text, offset,
280                                      &intstart_offset, &intend_offset);
281   *startOffset = intstart_offset;
282   *endOffset = intend_offset;
283   *defined = FALSE;
284   cur_attr = (GSList *) set;
285   while (cur_attr)
286     {
287       at = (AtkAttribute *) cur_attr->data;
288       if (!strcmp (at->name, attributename))
289       {
290           rv = CORBA_string_dup (at->value);
291           *defined = TRUE;
292           break;
293       }
294       cur_attr = cur_attr->next;
295     }
296   atk_attribute_set_free (set);
297   return (rv ? rv : CORBA_string_dup (""));  
298 }
299
300 static CORBA_string
301 impl_getDefaultAttributes (PortableServer_Servant servant,
302                            CORBA_Environment *ev)
303 {
304   AtkAttributeSet *set;
305   CORBA_char *rv;
306   AtkText *text = get_text_from_servant (servant);
307
308   g_return_val_if_fail (text != NULL, CORBA_string_dup (""));
309
310   set = atk_text_get_default_attributes (text);
311
312   rv = _string_from_attribute_set (set);
313   atk_attribute_set_free (set);
314   return rv;  
315 }
316
317 static void 
318 impl_getCharacterExtents (PortableServer_Servant servant,
319                           const CORBA_long offset, CORBA_long * x,
320                           CORBA_long * y, CORBA_long * width,
321                           CORBA_long * height,
322                           const CORBA_short coordType,
323                           CORBA_Environment *ev)
324 {
325   AtkText *text = get_text_from_servant (servant);
326   gint ix, iy, iw, ih;
327
328   g_return_if_fail (text != NULL);
329
330   atk_text_get_character_extents (
331           text, offset,
332           &ix, &iy, &iw, &ih,
333           (AtkCoordType) coordType);
334   *x = ix;
335   *y = iy;
336   *width = iw;
337   *height = ih;
338 }
339
340
341 static CORBA_long
342 impl__get_characterCount (PortableServer_Servant servant,
343                           CORBA_Environment    *ev)
344 {
345   AtkText *text = get_text_from_servant (servant);
346
347   g_return_val_if_fail (text != NULL, 0);
348
349   return atk_text_get_character_count (text);
350 }
351
352
353 static CORBA_long
354 impl_getOffsetAtPoint (PortableServer_Servant servant,
355                        const CORBA_long x, const CORBA_long y,
356                        const CORBA_short coordType,
357                        CORBA_Environment *ev)
358 {
359   AtkText *text = get_text_from_servant (servant);
360
361   g_return_val_if_fail (text != NULL, -1);
362
363   return atk_text_get_offset_at_point (text,
364                                   x, y,
365                                   (AtkCoordType) coordType);
366 }
367
368
369 static CORBA_long
370 impl_getNSelections (PortableServer_Servant servant,
371                      CORBA_Environment *ev)
372 {
373   AtkText *text = get_text_from_servant (servant);
374
375   g_return_val_if_fail (text != NULL, 0);
376
377   return atk_text_get_n_selections (text);
378 }
379
380
381 static void 
382 impl_getSelection (PortableServer_Servant servant,
383                    const CORBA_long selectionNum,
384                    CORBA_long * startOffset, CORBA_long * endOffset,
385                    CORBA_Environment *ev)
386 {
387   AtkText *text = get_text_from_servant (servant);
388   gint intStartOffset, intEndOffset;
389   
390   g_return_if_fail (text != NULL);
391
392   /* atk_text_get_selection returns gchar* which we discard */
393   g_free (atk_text_get_selection (text, selectionNum,
394                                   &intStartOffset, &intEndOffset));
395   
396   *startOffset = intStartOffset;
397   *endOffset = intEndOffset;
398 }
399
400
401 static CORBA_boolean
402 impl_addSelection (PortableServer_Servant servant,
403                    const CORBA_long startOffset,
404                    const CORBA_long endOffset,
405                    CORBA_Environment *ev)
406 {
407   AtkText *text = get_text_from_servant (servant);
408
409   g_return_val_if_fail (text != NULL, FALSE);
410
411   return atk_text_add_selection (text,
412                             startOffset, endOffset);
413 }
414
415
416 static CORBA_boolean
417 impl_removeSelection (PortableServer_Servant servant,
418                       const CORBA_long selectionNum,
419                       CORBA_Environment *ev)
420 {
421   AtkText *text = get_text_from_servant (servant);
422
423   g_return_val_if_fail (text != NULL, FALSE);
424
425   return atk_text_remove_selection (text, selectionNum);
426 }
427
428
429 static CORBA_boolean
430 impl_setSelection (PortableServer_Servant servant,
431                    const CORBA_long selectionNum,
432                    const CORBA_long startOffset,
433                    const CORBA_long endOffset,
434                    CORBA_Environment *ev)
435 {
436   AtkText *text = get_text_from_servant (servant);
437
438   g_return_val_if_fail (text != NULL, FALSE);
439
440   return atk_text_set_selection (text,
441                             selectionNum, startOffset, endOffset);
442 }
443
444
445 static CORBA_boolean
446 impl_setCaretOffset (PortableServer_Servant servant,
447                      const CORBA_long value,
448                      CORBA_Environment *ev)
449 {
450   AtkText *text = get_text_from_servant (servant);
451
452   g_return_val_if_fail (text != NULL, FALSE);
453
454   return atk_text_set_caret_offset (text, value);
455 }
456
457 static void
458 impl_getRangeExtents(PortableServer_Servant servant,
459                      const CORBA_long startOffset,
460                      const CORBA_long endOffset,
461                      CORBA_long * x, CORBA_long * y,
462                      CORBA_long * width,
463                      CORBA_long * height,
464                      const CORBA_short coordType,
465                      CORBA_Environment * ev)
466 {
467   AtkText *text = get_text_from_servant (servant);
468   AtkTextRectangle rect;
469
470   g_return_if_fail (text != NULL);
471   
472   atk_text_get_range_extents (text, (gint) startOffset, (gint) endOffset, 
473                               (AtkCoordType) coordType, &rect);
474   *x = rect.x;
475   *y = rect.y;
476   *width = rect.width;
477   *height = rect.height;
478 }
479
480 #define MAXRANGELEN 512
481
482 static Accessibility_Text_RangeList *
483 _spi_text_range_seq_from_atkrangelist (AtkTextRange **range_list) 
484
485   Accessibility_Text_RangeList *rangeList = 
486     Accessibility_Text_RangeList__alloc ();
487   int i, len;
488
489   for (len = 0; len < MAXRANGELEN && range_list[len]; ++len);
490
491   rangeList->_length = len;
492   rangeList->_buffer = Accessibility_Text_RangeList_allocbuf (len);
493   for (i = 0; i < len; ++i) 
494     {
495       rangeList->_buffer[i].startOffset = range_list[i]->start_offset; 
496       rangeList->_buffer[i].endOffset = range_list[i]->end_offset;
497       rangeList->_buffer[i].content = CORBA_string_dup (range_list[i]->content);
498     }
499
500   return rangeList;
501 }
502
503 static Accessibility_Text_RangeList *
504 impl_getBoundedRanges(PortableServer_Servant servant,
505                       const CORBA_long x,
506                       const CORBA_long y,
507                       const CORBA_long width,
508                       const CORBA_long height,
509                       const CORBA_short coordType,
510                       const Accessibility_TEXT_CLIP_TYPE xClipType,
511                       const Accessibility_TEXT_CLIP_TYPE yClipType, 
512                       CORBA_Environment * ev)
513 {
514   AtkText *text = get_text_from_servant (servant);
515   AtkTextRange **range_list = NULL;
516   AtkTextRectangle rect;
517
518   rect.x = x;
519   rect.y = y;
520   rect.width = width;
521   rect.height = height;
522
523   range_list = atk_text_get_bounded_ranges (text, &rect, 
524                                             (AtkCoordType) coordType,
525                                             (AtkTextClipType) xClipType,
526                                             (AtkTextClipType) yClipType);
527
528   return _spi_text_range_seq_from_atkrangelist (range_list); 
529 }
530
531
532
533 static Accessibility_AttributeSet *     
534 impl_getAttributeRun (PortableServer_Servant servant,              
535                       const CORBA_long offset, 
536                       CORBA_long *startOffset, CORBA_long *endOffset, 
537                       const CORBA_boolean includeDefaults, 
538                       CORBA_Environment *ev){
539                       
540      AtkAttributeSet *attributes, *default_attributes = NULL;
541      AtkAttribute *attr = NULL;
542      gint intstart_offset, intend_offset;
543      Accessibility_AttributeSet *retval = NULL;
544      AtkText *text = get_text_from_servant (servant);
545      gint n_attributes = 0, total_attributes = 0, n_default_attributes = 0;
546      gint i, j;
547      
548      g_return_val_if_fail (text != NULL, NULL);
549
550      attributes = atk_text_get_run_attributes (text, offset,
551                                                &intstart_offset, &intend_offset);
552
553      if (attributes) total_attributes = n_attributes = g_slist_length (attributes);
554
555      if (includeDefaults)
556      {
557          default_attributes = atk_text_get_default_attributes (text);
558          if (default_attributes)
559              n_default_attributes = g_slist_length (default_attributes);
560          total_attributes += n_default_attributes;
561      }
562
563      *startOffset = intstart_offset;
564      *endOffset = intend_offset; 
565
566      retval = CORBA_sequence_CORBA_string__alloc ();
567      retval->_length = retval->_maximum = total_attributes;
568      retval->_buffer = CORBA_sequence_CORBA_string_allocbuf (total_attributes);
569      CORBA_sequence_set_release (retval, CORBA_TRUE);
570
571      if (total_attributes)
572      {   
573          for (i = 0; i < n_attributes; ++i)
574          {
575              attr = g_slist_nth_data (attributes, i);
576              retval->_buffer[i] = CORBA_string_dup (g_strconcat (attr->name, ":", attr->value, NULL));
577          }
578          
579          for (j = 0; j < n_default_attributes; ++i, ++j)
580          {
581              attr = g_slist_nth_data (default_attributes, j);
582              retval->_buffer[i] = CORBA_string_dup (g_strconcat (attr->name, ":", attr->value, NULL));
583          }
584          
585          atk_attribute_set_free (attributes);
586          if (default_attributes)
587              atk_attribute_set_free (default_attributes);
588      }
589      return retval;
590 }
591
592 static Accessibility_AttributeSet *
593 impl_getDefaultAttributeSet (PortableServer_Servant servant,
594                              CORBA_Environment *ev){
595      AtkAttributeSet *attributes;
596      AtkAttribute *attr = NULL;
597      Accessibility_AttributeSet *retval = NULL;
598      AtkText *text = get_text_from_servant (servant);
599      gint n_attributes = 0;
600      gint i;
601      
602      g_return_val_if_fail (text != NULL, NULL);
603      
604      attributes = atk_text_get_default_attributes (text);
605      
606      if (attributes) 
607      {
608          n_attributes = g_slist_length (attributes);
609
610          retval = CORBA_sequence_CORBA_string__alloc ();
611          retval->_length = retval->_maximum = n_attributes;
612          retval->_buffer = CORBA_sequence_CORBA_string_allocbuf (n_attributes);
613          CORBA_sequence_set_release (retval, CORBA_TRUE);
614          
615          for (i = 0; i < n_attributes; ++i)
616          {
617              attr = g_slist_nth_data (attributes, i);
618              retval->_buffer [i] = CORBA_string_dup (g_strconcat (attr->name, ":", attr->value, NULL));
619          }
620          atk_attribute_set_free (attributes);
621      }     
622      return retval;       
623 }
624
625
626 static void
627 spi_text_class_init (SpiTextClass *klass)
628 {
629   POA_Accessibility_Text__epv *epv = &klass->epv;
630
631   /* Initialize epv table */
632
633   epv->getText = impl_getText;
634   epv->getTextAfterOffset = impl_getTextAfterOffset;
635   epv->getCharacterAtOffset = impl_getCharacterAtOffset;
636   epv->getTextAtOffset = impl_getTextAtOffset;
637   epv->getTextBeforeOffset = impl_getTextBeforeOffset;
638   epv->_get_caretOffset = impl__get_caretOffset;
639   epv->getAttributes = impl_getAttributes;
640   epv->getDefaultAttributes = impl_getDefaultAttributes;
641   epv->getCharacterExtents = impl_getCharacterExtents;
642   epv->_get_characterCount = impl__get_characterCount;
643   epv->getOffsetAtPoint = impl_getOffsetAtPoint;
644   epv->getNSelections = impl_getNSelections;
645   epv->getSelection = impl_getSelection;
646   epv->addSelection = impl_addSelection;
647   epv->removeSelection = impl_removeSelection;
648   epv->setSelection = impl_setSelection;
649   epv->setCaretOffset = impl_setCaretOffset;
650   epv->getRangeExtents = impl_getRangeExtents;
651   epv->getBoundedRanges = impl_getBoundedRanges;
652   epv->getAttributeValue = impl_getAttributeValue;
653   epv->getAttributeRun = impl_getAttributeRun;
654   epv->getDefaultAttributeSet = impl_getDefaultAttributeSet;
655 }
656
657 static void
658 spi_text_init (SpiText *text)
659 {
660 }
661
662 BONOBO_TYPE_FUNC_FULL (SpiText,
663                        Accessibility_Text,
664                        PARENT_TYPE,
665                        spi_text)
666
667 void
668 spi_text_construct (SpiText *text, AtkObject *obj)
669 {
670   spi_base_construct (SPI_BASE (text), G_OBJECT(obj));
671 }
672
673
674 SpiText *
675 spi_text_interface_new (AtkObject *obj)
676 {
677   SpiText *retval;
678
679   g_return_val_if_fail (ATK_IS_TEXT (obj), NULL);
680
681   retval = g_object_new (SPI_TEXT_TYPE, NULL);
682
683   spi_text_construct (retval, obj);
684
685   return retval;
686 }