2 * AT-SPI - Assistive Technology Service Provider Interface
3 * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
5 * Copyright 2001, 2002 Sun Microsystems Inc.,
6 * Copyright 2001, 2002 Ximian, Inc.
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.
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.
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.
24 /* text.c : implements the Text interface */
30 #include <atk/atktext.h>
31 #include <libspi/text.h>
32 #include <libspi/spi-private.h>
34 /* Our parent Gtk object type */
35 #define PARENT_TYPE SPI_TYPE_BASE
45 _spi_text_rect_union (SpiTextRect *aggregate, SpiTextRect *subrect)
49 /* 'normalize' subrect */
52 subrect->x += subrect->w;
57 subrect->y += subrect->h;
60 if (aggregate == NULL)
62 aggregate = g_new (SpiTextRect, 1);
63 memcpy (aggregate, subrect, sizeof (SpiTextRect));
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)
73 aggregate->w += (aggregate->x - subrect->x);
74 aggregate->x = subrect->x;
78 aggregate->w += (sx2 - ax2);
80 if (subrect->y < aggregate->y)
82 aggregate->h += (aggregate->y - subrect->y);
83 aggregate->y = subrect->y;
87 aggregate->h += (sy2 - ay2);
95 get_text_from_servant (PortableServer_Servant servant)
97 SpiBase *object = SPI_BASE (bonobo_object_from_servant (servant));
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);
105 impl_getText (PortableServer_Servant servant,
106 const CORBA_long startOffset,
107 const CORBA_long endOffset,
108 CORBA_Environment *ev)
112 AtkText *text = get_text_from_servant (servant);
114 g_return_val_if_fail (text != NULL, CORBA_string_dup (""));
116 txt = atk_text_get_text (text, startOffset, endOffset);
119 rv = CORBA_string_dup (txt);
123 rv = CORBA_string_dup ("");
130 impl_getTextAfterOffset (PortableServer_Servant servant,
131 const CORBA_long offset,
133 Accessibility_TEXT_BOUNDARY_TYPE
134 type, CORBA_long * startOffset,
135 CORBA_long * endOffset,
136 CORBA_Environment *ev)
140 gint intStartOffset, intEndOffset;
141 AtkText *text = get_text_from_servant (servant);
143 g_return_val_if_fail (text != NULL, CORBA_string_dup (""));
145 txt = atk_text_get_text_after_offset (text,
146 offset, (AtkTextBoundary) type,
147 &intStartOffset, &intEndOffset);
148 *startOffset = intStartOffset;
149 *endOffset = intEndOffset;
153 rv = CORBA_string_dup (txt);
157 rv = CORBA_string_dup ("");
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)
173 gint intStartOffset, intEndOffset;
174 AtkText *text = get_text_from_servant (servant);
176 g_return_val_if_fail (text != NULL, CORBA_string_dup (""));
178 txt = atk_text_get_text_at_offset (
180 offset, (AtkTextBoundary) type,
181 &intStartOffset, &intEndOffset);
183 *startOffset = intStartOffset;
184 *endOffset = intEndOffset;
188 rv = CORBA_string_dup (txt);
192 rv = CORBA_string_dup ("");
198 static CORBA_unsigned_long
199 impl_getCharacterAtOffset (PortableServer_Servant servant,
200 const CORBA_long offset,
201 CORBA_Environment *ev)
203 AtkText *text = get_text_from_servant (servant);
205 g_return_val_if_fail (text != NULL, 0);
207 return atk_text_get_character_at_offset (text, offset);
212 impl_getTextBeforeOffset (PortableServer_Servant servant,
213 const CORBA_long offset,
215 Accessibility_TEXT_BOUNDARY_TYPE
216 type, CORBA_long * startOffset,
217 CORBA_long * endOffset,
218 CORBA_Environment *ev)
222 gint intStartOffset, intEndOffset;
223 AtkText *text = get_text_from_servant (servant);
225 g_return_val_if_fail (text != NULL, CORBA_string_dup (""));
227 txt = atk_text_get_text_before_offset (text,
228 offset, (AtkTextBoundary) type,
229 &intStartOffset, &intEndOffset);
231 *startOffset = intStartOffset;
232 *endOffset = intEndOffset;
236 rv = CORBA_string_dup (txt);
240 rv = CORBA_string_dup ("");
247 impl__get_caretOffset (PortableServer_Servant servant,
248 CORBA_Environment *ev)
250 AtkText *text = get_text_from_servant (servant);
252 g_return_val_if_fail (text != NULL, -1);
254 return atk_text_get_caret_offset (text);
259 _string_from_attribute_set (AtkAttributeSet *set)
261 gchar *attributes, *tmp, *tmp2;
266 attributes = g_strdup ("");
267 cur_attr = (GSList *) set;
270 at = (AtkAttribute *) cur_attr->data;
271 tmp = g_strdup_printf ("%s%s:%s%s",
272 ((GSList *)(set) == cur_attr) ? "" : " ",
274 (cur_attr->next) ? ";" : "");
275 tmp2 = g_strconcat (attributes, tmp, NULL);
279 cur_attr = cur_attr->next;
281 rv = CORBA_string_dup (attributes);
289 impl_getAttributes (PortableServer_Servant servant,
290 const CORBA_long offset,
291 CORBA_long * startOffset,
292 CORBA_long * endOffset,
293 CORBA_Environment *ev)
295 AtkAttributeSet *set;
296 gint intstart_offset, intend_offset;
298 AtkText *text = get_text_from_servant (servant);
300 g_return_val_if_fail (text != NULL, CORBA_string_dup (""));
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);
312 impl_getDefaultAttributes (PortableServer_Servant servant,
313 CORBA_Environment *ev)
315 AtkAttributeSet *set;
317 AtkText *text = get_text_from_servant (servant);
319 g_return_val_if_fail (text != NULL, CORBA_string_dup (""));
321 set = atk_text_get_default_attributes (text);
323 rv = _string_from_attribute_set (set);
324 atk_attribute_set_free (set);
329 impl_getCharacterExtents (PortableServer_Servant servant,
330 const CORBA_long offset, CORBA_long * x,
331 CORBA_long * y, CORBA_long * width,
333 const CORBA_short coordType,
334 CORBA_Environment *ev)
336 AtkText *text = get_text_from_servant (servant);
339 g_return_if_fail (text != NULL);
341 atk_text_get_character_extents (
344 (AtkCoordType) coordType);
353 impl__get_characterCount (PortableServer_Servant servant,
354 CORBA_Environment *ev)
356 AtkText *text = get_text_from_servant (servant);
358 g_return_val_if_fail (text != NULL, 0);
360 return atk_text_get_character_count (text);
365 impl_getOffsetAtPoint (PortableServer_Servant servant,
366 const CORBA_long x, const CORBA_long y,
367 const CORBA_short coordType,
368 CORBA_Environment *ev)
370 AtkText *text = get_text_from_servant (servant);
372 g_return_val_if_fail (text != NULL, -1);
374 return atk_text_get_offset_at_point (text,
376 (AtkCoordType) coordType);
381 impl_getNSelections (PortableServer_Servant servant,
382 CORBA_Environment *ev)
384 AtkText *text = get_text_from_servant (servant);
386 g_return_val_if_fail (text != NULL, 0);
388 return atk_text_get_n_selections (text);
393 impl_getSelection (PortableServer_Servant servant,
394 const CORBA_long selectionNum,
395 CORBA_long * startOffset, CORBA_long * endOffset,
396 CORBA_Environment *ev)
398 AtkText *text = get_text_from_servant (servant);
399 gint intStartOffset, intEndOffset;
401 g_return_if_fail (text != NULL);
403 /* atk_text_get_selection returns gchar* which we discard */
404 g_free (atk_text_get_selection (text, selectionNum,
405 &intStartOffset, &intEndOffset));
407 *startOffset = intStartOffset;
408 *endOffset = intEndOffset;
413 impl_addSelection (PortableServer_Servant servant,
414 const CORBA_long startOffset,
415 const CORBA_long endOffset,
416 CORBA_Environment *ev)
418 AtkText *text = get_text_from_servant (servant);
420 g_return_val_if_fail (text != NULL, FALSE);
422 return atk_text_add_selection (text,
423 startOffset, endOffset);
428 impl_removeSelection (PortableServer_Servant servant,
429 const CORBA_long selectionNum,
430 CORBA_Environment *ev)
432 AtkText *text = get_text_from_servant (servant);
434 g_return_val_if_fail (text != NULL, FALSE);
436 return atk_text_remove_selection (text, selectionNum);
441 impl_setSelection (PortableServer_Servant servant,
442 const CORBA_long selectionNum,
443 const CORBA_long startOffset,
444 const CORBA_long endOffset,
445 CORBA_Environment *ev)
447 AtkText *text = get_text_from_servant (servant);
449 g_return_val_if_fail (text != NULL, FALSE);
451 return atk_text_set_selection (text,
452 selectionNum, startOffset, endOffset);
457 impl_setCaretOffset (PortableServer_Servant servant,
458 const CORBA_long value,
459 CORBA_Environment *ev)
461 AtkText *text = get_text_from_servant (servant);
463 g_return_val_if_fail (text != NULL, FALSE);
465 return atk_text_set_caret_offset (text, value);
468 #define SPI_TEXT_MIN_RANGE_FOR_LINE_CHECK 6
471 impl_getRangeExtents(PortableServer_Servant servant,
472 const CORBA_long startOffset,
473 const CORBA_long endOffset,
474 CORBA_long * x, CORBA_long * y,
477 const CORBA_short coordType,
478 CORBA_Environment * ev)
480 AtkText *text = get_text_from_servant (servant);
481 SpiTextRect cbounds, bounds;
484 g_return_if_fail (text != NULL);
486 atk_text_get_character_extents (text, startOffset,
487 &bounds.x, &bounds.y, &bounds.w, &bounds.h,
488 (AtkCoordType) coordType);
489 /* no equivalent ATK API yet, must do the hard way. :-( */
490 for (i = startOffset + 1; i < endOffset; i++)
492 atk_text_get_character_extents (text, i,
493 &cbounds.x, &cbounds.y, &cbounds.w, &cbounds.h,
494 (AtkCoordType) coordType);
495 _spi_text_rect_union (&bounds, &cbounds);
504 static Accessibility_Text_RangeList *
505 _spi_text_range_seq_from_gslist (GSList *range_list)
507 Accessibility_Text_RangeList *rangeList =
508 Accessibility_Text_RangeList__alloc ();
509 int i, len = g_slist_length (range_list);
510 GSList *list = range_list;
512 rangeList->_length = len;
513 rangeList->_buffer = Accessibility_Text_RangeList_allocbuf (len);
514 for (i = 0; i < len; ++i)
516 memcpy (&rangeList->_buffer[i], list->data, sizeof (Accessibility_Text_Range));
517 spi_init_any_nil (&rangeList->_buffer[i].data);
519 list = g_slist_next (range_list);
521 g_slist_free (range_list);
527 _spi_bounds_contain (SpiTextRect *clip, SpiTextRect *cbounds,
528 Accessibility_TEXT_CLIP_TYPE xClipType,
529 Accessibility_TEXT_CLIP_TYPE yClipType)
531 gint clipx2 = clip->x + clip->w;
532 gint clipy2 = clip->y + clip->h;
533 gint charx2 = cbounds->x + cbounds->w;
534 gint chary2 = cbounds->y + cbounds->h;
535 gboolean x_min_ok, y_min_ok, x_max_ok, y_max_ok;
537 x_min_ok = (cbounds->x >= clip->x) ||
538 ((charx2 >= clip->x) &&
539 ((xClipType == Accessibility_TEXT_CLIP_NONE) ||
540 (xClipType == Accessibility_TEXT_CLIP_MAX)));
541 x_max_ok = (charx2 <= clipx2) ||
542 ((cbounds->x <= clipx2) &&
543 ((xClipType == Accessibility_TEXT_CLIP_NONE) ||
544 (xClipType == Accessibility_TEXT_CLIP_MIN)));
545 y_min_ok = (cbounds->y >= clip->y) ||
546 ((chary2 >= clip->y) &&
547 ((yClipType == Accessibility_TEXT_CLIP_NONE) ||
548 (yClipType == Accessibility_TEXT_CLIP_MAX)));
549 y_max_ok = (chary2 <= clipy2) ||
550 ((cbounds->y <= clipy2) &&
551 ((yClipType == Accessibility_TEXT_CLIP_NONE) ||
552 (yClipType == Accessibility_TEXT_CLIP_MIN)));
554 if (x_min_ok && y_min_ok && x_max_ok && y_max_ok)
560 Accessibility_Text_RangeList *
561 impl_getBoundedRanges(PortableServer_Servant servant,
564 const CORBA_long width,
565 const CORBA_long height,
566 const CORBA_short coordType,
567 const Accessibility_TEXT_CLIP_TYPE xClipType,
568 const Accessibility_TEXT_CLIP_TYPE yClipType,
569 CORBA_Environment * ev)
571 AtkText *text = get_text_from_servant (servant);
572 GSList *range_list = NULL;
574 int startOffset = 0, endOffset = atk_text_get_character_count (text);
576 gint minLineStart, minLineEnd, maxLineStart, maxLineEnd;
577 long bounds_min_offset;
578 long bounds_max_offset;
585 /* for horizontal text layouts, at least, the following check helps. */
586 bounds_min_offset = atk_text_get_offset_at_point (text, x, y,
587 (AtkCoordType) coordType);
588 bounds_max_offset = atk_text_get_offset_at_point (text, x + width, y + height,
589 (AtkCoordType) coordType);
590 atk_text_get_text_at_offset (text, bounds_min_offset,
591 ATK_TEXT_BOUNDARY_LINE_START,
592 &minLineStart, &minLineEnd);
593 atk_text_get_text_at_offset (text, bounds_max_offset,
594 ATK_TEXT_BOUNDARY_LINE_START,
595 &maxLineStart, &maxLineEnd);
596 startOffset = MIN (minLineStart, maxLineStart);
597 endOffset = MAX (minLineEnd, maxLineEnd);
599 curr_offset = startOffset;
601 while (curr_offset < endOffset)
603 int offset = startOffset;
605 while (curr_offset < endOffset)
607 atk_text_get_character_extents (text, curr_offset,
608 &cbounds.x, &cbounds.y,
609 &cbounds.w, &cbounds.h,
610 (AtkCoordType) coordType);
611 if (!_spi_bounds_contain (&clip, &cbounds, xClipType, yClipType))
615 /* add the range to our list */
616 if (curr_offset > offset)
618 Accessibility_Text_Range *range = g_malloc (sizeof (Accessibility_Text_Range));
620 range->startOffset = offset;
621 range->endOffset = curr_offset;
622 s = atk_text_get_text (text, offset, curr_offset);
623 range->content = CORBA_string_dup (s ? s : "");
624 range_list = g_slist_append (range_list, range);
625 offset = curr_offset;
629 return _spi_text_range_seq_from_gslist (range_list); /* frees the GSList too */
634 spi_text_class_init (SpiTextClass *klass)
636 POA_Accessibility_Text__epv *epv = &klass->epv;
638 /* Initialize epv table */
640 epv->getText = impl_getText;
641 epv->getTextAfterOffset = impl_getTextAfterOffset;
642 epv->getCharacterAtOffset = impl_getCharacterAtOffset;
643 epv->getTextAtOffset = impl_getTextAtOffset;
644 epv->getTextBeforeOffset = impl_getTextBeforeOffset;
645 epv->_get_caretOffset = impl__get_caretOffset;
646 epv->getAttributes = impl_getAttributes;
647 epv->getDefaultAttributes = impl_getDefaultAttributes;
648 epv->getCharacterExtents = impl_getCharacterExtents;
649 epv->_get_characterCount = impl__get_characterCount;
650 epv->getOffsetAtPoint = impl_getOffsetAtPoint;
651 epv->getNSelections = impl_getNSelections;
652 epv->getSelection = impl_getSelection;
653 epv->addSelection = impl_addSelection;
654 epv->removeSelection = impl_removeSelection;
655 epv->setSelection = impl_setSelection;
656 epv->setCaretOffset = impl_setCaretOffset;
657 epv->getRangeExtents = impl_getRangeExtents;
658 epv->getBoundedRanges = impl_getBoundedRanges;
662 spi_text_init (SpiText *text)
666 BONOBO_TYPE_FUNC_FULL (SpiText,
672 spi_text_construct (SpiText *text, AtkObject *obj)
674 spi_base_construct (SPI_BASE (text), G_OBJECT(obj));
679 spi_text_interface_new (AtkObject *obj)
683 g_return_val_if_fail (ATK_IS_TEXT (obj), NULL);
685 retval = g_object_new (SPI_TEXT_TYPE, NULL);
687 spi_text_construct (retval, obj);