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