Release 2.15.1
[atk.git] / atk / atktext.c
blobe4d421d2796757c979c9d099fcae662aa09910c7
1 /* ATK - The Accessibility Toolkit for GTK+
2 * Copyright 2001 Sun Microsystems Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
20 #include "config.h"
22 #include "atk.h"
23 #include "atkmarshal.h"
25 #include <string.h>
27 /**
28 * SECTION:atktext
29 * @Short_description: The ATK interface implemented by components
30 * with text content.
31 * @Title:AtkText
33 * #AtkText should be implemented by #AtkObjects on behalf of widgets
34 * that have text content which is either attributed or otherwise
35 * non-trivial. #AtkObjects whose text content is simple,
36 * unattributed, and very brief may expose that content via
37 * #atk_object_get_name instead; however if the text is editable,
38 * multi-line, typically longer than three or four words, attributed,
39 * selectable, or if the object already uses the 'name' ATK property
40 * for other information, the #AtkText interface should be used to
41 * expose the text content. In the case of editable text content,
42 * #AtkEditableText (a subtype of the #AtkText interface) should be
43 * implemented instead.
45 * #AtkText provides not only traversal facilities and change
46 * notification for text content, but also caret tracking and glyph
47 * bounding box calculations. Note that the text strings are exposed
48 * as UTF-8, and are therefore potentially multi-byte, and
49 * caret-to-byte offset mapping makes no assumptions about the
50 * character length; also bounding box glyph-to-offset mapping may be
51 * complex for languages which use ligatures.
54 static GPtrArray *extra_attributes = NULL;
56 enum {
57 TEXT_CHANGED,
58 TEXT_CARET_MOVED,
59 TEXT_SELECTION_CHANGED,
60 TEXT_ATTRIBUTES_CHANGED,
61 TEXT_INSERT,
62 TEXT_REMOVE,
63 LAST_SIGNAL
66 static const char boolean[] =
67 "false\0"
68 "true";
69 static const guint8 boolean_offsets[] = {
70 0, 6
73 static const char style[] =
74 "normal\0"
75 "oblique\0"
76 "italic";
77 static const guint8 style_offsets[] = {
78 0, 7, 15
81 static const char variant[] =
82 "normal\0"
83 "small_caps";
84 static const guint8 variant_offsets[] = {
85 0, 7
88 static const char stretch[] =
89 "ultra_condensed\0"
90 "extra_condensed\0"
91 "condensed\0"
92 "semi_condensed\0"
93 "normal\0"
94 "semi_expanded\0"
95 "expanded\0"
96 "extra_expanded\0"
97 "ultra_expanded";
98 static const guint8 stretch_offsets[] = {
99 0, 16, 32, 42, 57, 64, 78, 87, 102
102 static const char justification[] =
103 "left\0"
104 "right\0"
105 "center\0"
106 "fill";
107 static const guint8 justification_offsets[] = {
108 0, 5, 11, 18
111 static const char direction[] =
112 "none\0"
113 "ltr\0"
114 "rtl";
115 static const guint8 direction_offsets[] = {
116 0, 5, 9
119 static const char wrap_mode[] =
120 "none\0"
121 "char\0"
122 "word\0"
123 "word_char";
124 static const guint8 wrap_mode_offsets[] = {
125 0, 5, 10, 15
128 static const char underline[] =
129 "none\0"
130 "single\0"
131 "double\0"
132 "low\0"
133 "error";
134 static const guint8 underline_offsets[] = {
135 0, 5, 12, 19, 23
138 static void atk_text_base_init (AtkTextIface *class);
140 static void atk_text_real_get_range_extents (AtkText *text,
141 gint start_offset,
142 gint end_offset,
143 AtkCoordType coord_type,
144 AtkTextRectangle *rect);
146 static AtkTextRange** atk_text_real_get_bounded_ranges (AtkText *text,
147 AtkTextRectangle *rect,
148 AtkCoordType coord_type,
149 AtkTextClipType x_clip_type,
150 AtkTextClipType y_clip_type);
152 static guint atk_text_signals[LAST_SIGNAL] = { 0 };
154 GType
155 atk_text_get_type (void)
157 static GType type = 0;
159 if (!type)
161 static const GTypeInfo tinfo =
163 sizeof (AtkTextIface),
164 (GBaseInitFunc) atk_text_base_init,
165 (GBaseFinalizeFunc) NULL,
166 (GClassInitFunc) NULL /* atk_text_interface_init */ ,
167 (GClassFinalizeFunc) NULL,
171 type = g_type_register_static (G_TYPE_INTERFACE, "AtkText", &tinfo, 0);
174 return type;
177 static void
178 atk_text_base_init (AtkTextIface *class)
180 static gboolean initialized = FALSE;
182 if (! initialized)
185 * Note that text_changed signal supports details "insert", "delete",
186 * possibly "replace".
189 class->get_range_extents = atk_text_real_get_range_extents;
190 class->get_bounded_ranges = atk_text_real_get_bounded_ranges;
193 * AtkText::text-changed:
194 * @atktext: the object which received the signal.
195 * @arg1: The position (character offset) of the insertion or deletion.
196 * @arg2: The length (in characters) of text inserted or deleted.
198 * The "text-changed" signal is emitted when the text of the
199 * object which implements the AtkText interface changes, This
200 * signal will have a detail which is either "insert" or
201 * "delete" which identifies whether the text change was an
202 * insertion or a deletion.
204 * Deprecated: Since 2.9.4. Use #AtkObject::text-insert or
205 * #AtkObject::text-remove instead.
207 atk_text_signals[TEXT_CHANGED] =
208 g_signal_new ("text_changed",
209 ATK_TYPE_TEXT,
210 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
211 G_STRUCT_OFFSET (AtkTextIface, text_changed),
212 (GSignalAccumulator) NULL, NULL,
213 atk_marshal_VOID__INT_INT,
214 G_TYPE_NONE,
215 2, G_TYPE_INT, G_TYPE_INT);
218 * AtkText::text-insert:
219 * @atktext: the object which received the signal.
220 * @arg1: The position (character offset) of the insertion.
221 * @arg2: The length (in characters) of text inserted.
222 * @arg3: The new text inserted
224 * The "text-insert" signal is emitted when a new text is
225 * inserted. If the signal was not triggered by the user
226 * (e.g. typing or pasting text), the "system" detail should be
227 * included.
229 atk_text_signals[TEXT_INSERT] =
230 g_signal_new ("text_insert",
231 ATK_TYPE_TEXT,
232 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
234 (GSignalAccumulator) NULL, NULL,
235 atk_marshal_VOID__INT_INT_STRING,
236 G_TYPE_NONE,
237 3, G_TYPE_INT, G_TYPE_INT, G_TYPE_STRING);
240 * AtkText::text-remove:
241 * @atktext: the object which received the signal.
242 * @arg1: The position (character offset) of the removal.
243 * @arg2: The length (in characters) of text removed.
244 * @arg3: The old text removed
246 * The "text-remove" signal is emitted when a new text is
247 * removed. If the signal was not triggered by the user
248 * (e.g. typing or pasting text), the "system" detail should be
249 * included.
251 atk_text_signals[TEXT_REMOVE] =
252 g_signal_new ("text_remove",
253 ATK_TYPE_TEXT,
254 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
256 (GSignalAccumulator) NULL, NULL,
257 atk_marshal_VOID__INT_INT_STRING,
258 G_TYPE_NONE,
259 3, G_TYPE_INT, G_TYPE_INT, G_TYPE_STRING);
262 * AtkText::text-caret-moved:
263 * @atktext: the object which received the signal.
264 * @arg1: The new position of the text caret.
266 * The "text-caret-moved" signal is emitted when the caret
267 * position of the text of an object which implements AtkText
268 * changes.
270 atk_text_signals[TEXT_CARET_MOVED] =
271 g_signal_new ("text_caret_moved",
272 ATK_TYPE_TEXT,
273 G_SIGNAL_RUN_LAST,
274 G_STRUCT_OFFSET (AtkTextIface, text_caret_moved),
275 (GSignalAccumulator) NULL, NULL,
276 g_cclosure_marshal_VOID__INT,
277 G_TYPE_NONE,
278 1, G_TYPE_INT);
281 * AtkText::text-selection-changed:
282 * @atktext: the object which received the signal.
284 * The "text-selection-changed" signal is emitted when the
285 * selected text of an object which implements AtkText changes.
287 atk_text_signals[TEXT_SELECTION_CHANGED] =
288 g_signal_new ("text_selection_changed",
289 ATK_TYPE_TEXT,
290 G_SIGNAL_RUN_LAST,
291 G_STRUCT_OFFSET (AtkTextIface, text_selection_changed),
292 (GSignalAccumulator) NULL, NULL,
293 g_cclosure_marshal_VOID__VOID,
294 G_TYPE_NONE, 0);
296 * AtkText::text-attributes-changed:
297 * @atktext: the object which received the signal.
299 * The "text-attributes-changed" signal is emitted when the text
300 * attributes of the text of an object which implements AtkText
301 * changes.
303 atk_text_signals[TEXT_ATTRIBUTES_CHANGED] =
304 g_signal_new ("text_attributes_changed",
305 ATK_TYPE_TEXT,
306 G_SIGNAL_RUN_LAST,
307 G_STRUCT_OFFSET (AtkTextIface, text_attributes_changed),
308 (GSignalAccumulator) NULL, NULL,
309 g_cclosure_marshal_VOID__VOID,
310 G_TYPE_NONE, 0);
313 initialized = TRUE;
318 * atk_text_get_text:
319 * @text: an #AtkText
320 * @start_offset: start position
321 * @end_offset: end position, or -1 for the end of the string.
323 * Gets the specified text.
325 * Returns: a newly allocated string containing the text from @start_offset up
326 * to, but not including @end_offset. Use g_free() to free the returned string.
328 gchar*
329 atk_text_get_text (AtkText *text,
330 gint start_offset,
331 gint end_offset)
333 AtkTextIface *iface;
335 g_return_val_if_fail (ATK_IS_TEXT (text), NULL);
337 iface = ATK_TEXT_GET_IFACE (text);
339 if (start_offset < 0 || end_offset < -1 ||
340 (end_offset != -1 && end_offset < start_offset))
341 return NULL;
343 if (iface->get_text)
344 return (*(iface->get_text)) (text, start_offset, end_offset);
345 else
346 return NULL;
350 * atk_text_get_character_at_offset:
351 * @text: an #AtkText
352 * @offset: position
354 * Gets the specified text.
356 * Returns: the character at @offset.
358 gunichar
359 atk_text_get_character_at_offset (AtkText *text,
360 gint offset)
362 AtkTextIface *iface;
364 g_return_val_if_fail (ATK_IS_TEXT (text), (gunichar) 0);
366 iface = ATK_TEXT_GET_IFACE (text);
368 if (iface->get_character_at_offset)
369 return (*(iface->get_character_at_offset)) (text, offset);
370 else
371 return (gunichar) 0;
375 * atk_text_get_text_after_offset:
376 * @text: an #AtkText
377 * @offset: position
378 * @boundary_type: An #AtkTextBoundary
379 * @start_offset: (out): the start offset of the returned string
380 * @end_offset: (out): the offset of the first character after the
381 * returned substring
383 * Gets the specified text.
385 * Deprecated: This method is deprecated since ATK version
386 * 2.9.3. Please use atk_text_get_string_at_offset() instead.
388 * Returns: a newly allocated string containing the text after @offset bounded
389 * by the specified @boundary_type. Use g_free() to free the returned string.
391 gchar*
392 atk_text_get_text_after_offset (AtkText *text,
393 gint offset,
394 AtkTextBoundary boundary_type,
395 gint *start_offset,
396 gint *end_offset)
398 AtkTextIface *iface;
399 gint local_start_offset, local_end_offset;
400 gint *real_start_offset, *real_end_offset;
402 g_return_val_if_fail (ATK_IS_TEXT (text), NULL);
404 if (start_offset)
405 real_start_offset = start_offset;
406 else
407 real_start_offset = &local_start_offset;
408 if (end_offset)
409 real_end_offset = end_offset;
410 else
411 real_end_offset = &local_end_offset;
413 if (offset < 0)
414 return NULL;
416 iface = ATK_TEXT_GET_IFACE (text);
418 if (iface->get_text_after_offset)
419 return (*(iface->get_text_after_offset)) (text, offset, boundary_type, real_start_offset, real_end_offset);
420 else
421 return NULL;
425 * atk_text_get_text_at_offset:
426 * @text: an #AtkText
427 * @offset: position
428 * @boundary_type: An #AtkTextBoundary
429 * @start_offset: (out): the start offset of the returned string
430 * @end_offset: (out): the offset of the first character after the
431 * returned substring
433 * Gets the specified text.
435 * If the boundary_type if ATK_TEXT_BOUNDARY_CHAR the character at the
436 * offset is returned.
438 * If the boundary_type is ATK_TEXT_BOUNDARY_WORD_START the returned string
439 * is from the word start at or before the offset to the word start after
440 * the offset.
442 * The returned string will contain the word at the offset if the offset
443 * is inside a word and will contain the word before the offset if the
444 * offset is not inside a word.
446 * If the boundary type is ATK_TEXT_BOUNDARY_SENTENCE_START the returned
447 * string is from the sentence start at or before the offset to the sentence
448 * start after the offset.
450 * The returned string will contain the sentence at the offset if the offset
451 * is inside a sentence and will contain the sentence before the offset
452 * if the offset is not inside a sentence.
454 * If the boundary type is ATK_TEXT_BOUNDARY_LINE_START the returned
455 * string is from the line start at or before the offset to the line
456 * start after the offset.
458 * Deprecated: This method is deprecated since ATK version
459 * 2.9.4. Please use atk_text_get_string_at_offset() instead.
461 * Returns: a newly allocated string containing the text at @offset bounded by
462 * the specified @boundary_type. Use g_free() to free the returned string.
464 gchar*
465 atk_text_get_text_at_offset (AtkText *text,
466 gint offset,
467 AtkTextBoundary boundary_type,
468 gint *start_offset,
469 gint *end_offset)
471 AtkTextIface *iface;
472 gint local_start_offset, local_end_offset;
473 gint *real_start_offset, *real_end_offset;
475 g_return_val_if_fail (ATK_IS_TEXT (text), NULL);
477 if (start_offset)
478 real_start_offset = start_offset;
479 else
480 real_start_offset = &local_start_offset;
481 if (end_offset)
482 real_end_offset = end_offset;
483 else
484 real_end_offset = &local_end_offset;
486 iface = ATK_TEXT_GET_IFACE (text);
488 if (iface->get_text_at_offset)
489 return (*(iface->get_text_at_offset)) (text, offset, boundary_type, real_start_offset, real_end_offset);
490 else
491 return NULL;
495 * atk_text_get_text_before_offset:
496 * @text: an #AtkText
497 * @offset: position
498 * @boundary_type: An #AtkTextBoundary
499 * @start_offset: (out): the start offset of the returned string
500 * @end_offset: (out): the offset of the first character after the
501 * returned substring
503 * Gets the specified text.
505 * Deprecated: This method is deprecated since ATK version
506 * 2.9.3. Please use atk_text_get_string_at_offset() instead.
508 * Returns: a newly allocated string containing the text before @offset bounded
509 * by the specified @boundary_type. Use g_free() to free the returned string.
511 gchar*
512 atk_text_get_text_before_offset (AtkText *text,
513 gint offset,
514 AtkTextBoundary boundary_type,
515 gint *start_offset,
516 gint *end_offset)
518 AtkTextIface *iface;
519 gint local_start_offset, local_end_offset;
520 gint *real_start_offset, *real_end_offset;
522 g_return_val_if_fail (ATK_IS_TEXT (text), NULL);
524 if (start_offset)
525 real_start_offset = start_offset;
526 else
527 real_start_offset = &local_start_offset;
528 if (end_offset)
529 real_end_offset = end_offset;
530 else
531 real_end_offset = &local_end_offset;
533 if (offset < 0)
534 return NULL;
536 iface = ATK_TEXT_GET_IFACE (text);
538 if (iface->get_text_before_offset)
539 return (*(iface->get_text_before_offset)) (text, offset, boundary_type, real_start_offset, real_end_offset);
540 else
541 return NULL;
545 * atk_text_get_string_at_offset:
546 * @text: an #AtkText
547 * @offset: position
548 * @granularity: An #AtkTextGranularity
549 * @start_offset: (out): the start offset of the returned string, or -1
550 * if an error has occurred (e.g. invalid offset, not implemented)
551 * @end_offset: (out): the offset of the first character after the returned string,
552 * or -1 if an error has occurred (e.g. invalid offset, not implemented)
554 * Gets a portion of the text exposed through an #AtkText according to a given @offset
555 * and a specific @granularity, along with the start and end offsets defining the
556 * boundaries of such a portion of text.
558 * If @granularity is ATK_TEXT_GRANULARITY_CHAR the character at the
559 * offset is returned.
561 * If @granularity is ATK_TEXT_GRANULARITY_WORD the returned string
562 * is from the word start at or before the offset to the word start after
563 * the offset.
565 * The returned string will contain the word at the offset if the offset
566 * is inside a word and will contain the word before the offset if the
567 * offset is not inside a word.
569 * If @granularity is ATK_TEXT_GRANULARITY_SENTENCE the returned string
570 * is from the sentence start at or before the offset to the sentence
571 * start after the offset.
573 * The returned string will contain the sentence at the offset if the offset
574 * is inside a sentence and will contain the sentence before the offset
575 * if the offset is not inside a sentence.
577 * If @granularity is ATK_TEXT_GRANULARITY_LINE the returned string
578 * is from the line start at or before the offset to the line
579 * start after the offset.
581 * If @granularity is ATK_TEXT_GRANULARITY_PARAGRAPH the returned string
582 * is from the start of the paragraph at or before the offset to the start
583 * of the following paragraph after the offset.
585 * Since: 2.10
587 * Returns: (nullable): a newly allocated string containing the text
588 * at the @offset bounded by the specified @granularity. Use
589 * g_free() to free the returned string. Returns %NULL if the
590 * offset is invalid or no implementation is available.
592 gchar* atk_text_get_string_at_offset (AtkText *text,
593 gint offset,
594 AtkTextGranularity granularity,
595 gint *start_offset,
596 gint *end_offset)
598 AtkTextIface *iface;
599 gint local_start_offset, local_end_offset;
600 gint *real_start_offset, *real_end_offset;
602 g_return_val_if_fail (ATK_IS_TEXT (text), NULL);
604 if (start_offset)
606 *start_offset = -1;
607 real_start_offset = start_offset;
609 else
610 real_start_offset = &local_start_offset;
612 if (end_offset)
614 *end_offset = -1;
615 real_end_offset = end_offset;
617 else
618 real_end_offset = &local_end_offset;
620 if (offset < 0)
621 return NULL;
623 iface = ATK_TEXT_GET_IFACE (text);
625 if (iface->get_string_at_offset)
626 return (*(iface->get_string_at_offset)) (text, offset, granularity, real_start_offset, real_end_offset);
627 else
628 return NULL;
632 * atk_text_get_caret_offset:
633 * @text: an #AtkText
635 * Gets the offset position of the caret (cursor).
637 * Returns: the offset position of the caret (cursor).
639 gint
640 atk_text_get_caret_offset (AtkText *text)
642 AtkTextIface *iface;
644 g_return_val_if_fail (ATK_IS_TEXT (text), 0);
646 iface = ATK_TEXT_GET_IFACE (text);
648 if (iface->get_caret_offset)
649 return (*(iface->get_caret_offset)) (text);
650 else
651 return 0;
655 * atk_text_get_character_extents:
656 * @text: an #AtkText
657 * @offset: The offset of the text character for which bounding information is required.
658 * @x: Pointer for the x cordinate of the bounding box
659 * @y: Pointer for the y cordinate of the bounding box
660 * @width: Pointer for the width of the bounding box
661 * @height: Pointer for the height of the bounding box
662 * @coords: specify whether coordinates are relative to the screen or widget window
664 * Get the bounding box containing the glyph representing the character at
665 * a particular text offset.
667 void
668 atk_text_get_character_extents (AtkText *text,
669 gint offset,
670 gint *x,
671 gint *y,
672 gint *width,
673 gint *height,
674 AtkCoordType coords)
676 AtkTextIface *iface;
677 gint local_x, local_y, local_width, local_height;
678 gint *real_x, *real_y, *real_width, *real_height;
680 g_return_if_fail (ATK_IS_TEXT (text));
682 if (x)
683 real_x = x;
684 else
685 real_x = &local_x;
686 if (y)
687 real_y = y;
688 else
689 real_y = &local_y;
690 if (width)
691 real_width = width;
692 else
693 real_width = &local_width;
694 if (height)
695 real_height = height;
696 else
697 real_height = &local_height;
699 *real_x = 0;
700 *real_y = 0;
701 *real_width = 0;
702 *real_height = 0;
704 if (offset < 0)
705 return;
707 iface = ATK_TEXT_GET_IFACE (text);
709 if (iface->get_character_extents)
710 (*(iface->get_character_extents)) (text, offset, real_x, real_y, real_width, real_height, coords);
712 if (*real_width <0)
714 *real_x = *real_x + *real_width;
715 *real_width *= -1;
720 * atk_text_get_run_attributes:
721 *@text: an #AtkText
722 *@offset: the offset at which to get the attributes, -1 means the offset of
723 *the character to be inserted at the caret location.
724 *@start_offset: (out): the address to put the start offset of the range
725 *@end_offset: (out): the address to put the end offset of the range
727 *Creates an #AtkAttributeSet which consists of the attributes explicitly
728 *set at the position @offset in the text. @start_offset and @end_offset are
729 *set to the start and end of the range around @offset where the attributes are
730 *invariant. Note that @end_offset is the offset of the first character
731 *after the range. See the enum AtkTextAttribute for types of text
732 *attributes that can be returned. Note that other attributes may also be
733 *returned.
735 *Returns: (transfer full): an #AtkAttributeSet which contains the attributes
736 * explicitly set at @offset. This #AtkAttributeSet should be freed by a call
737 * to atk_attribute_set_free().
739 AtkAttributeSet*
740 atk_text_get_run_attributes (AtkText *text,
741 gint offset,
742 gint *start_offset,
743 gint *end_offset)
745 AtkTextIface *iface;
746 gint local_start_offset, local_end_offset;
747 gint *real_start_offset, *real_end_offset;
749 g_return_val_if_fail (ATK_IS_TEXT (text), NULL);
751 if (start_offset)
752 real_start_offset = start_offset;
753 else
754 real_start_offset = &local_start_offset;
755 if (end_offset)
756 real_end_offset = end_offset;
757 else
758 real_end_offset = &local_end_offset;
760 if (offset < -1)
761 return NULL;
763 iface = ATK_TEXT_GET_IFACE (text);
765 if (iface->get_run_attributes)
766 return (*(iface->get_run_attributes)) (text, offset, real_start_offset, real_end_offset);
767 else
768 return NULL;
772 * atk_text_get_default_attributes:
773 *@text: an #AtkText
775 *Creates an #AtkAttributeSet which consists of the default values of
776 *attributes for the text. See the enum AtkTextAttribute for types of text
777 *attributes that can be returned. Note that other attributes may also be
778 *returned.
780 *Returns: (transfer full): an #AtkAttributeSet which contains the default
781 * values of attributes. at @offset. this #atkattributeset should be freed by
782 * a call to atk_attribute_set_free().
784 AtkAttributeSet*
785 atk_text_get_default_attributes (AtkText *text)
787 AtkTextIface *iface;
789 g_return_val_if_fail (ATK_IS_TEXT (text), NULL);
791 iface = ATK_TEXT_GET_IFACE (text);
793 if (iface->get_default_attributes)
794 return (*(iface->get_default_attributes)) (text);
795 else
796 return NULL;
800 * atk_text_get_character_count:
801 * @text: an #AtkText
803 * Gets the character count.
805 * Returns: the number of characters.
807 gint
808 atk_text_get_character_count (AtkText *text)
810 AtkTextIface *iface;
812 g_return_val_if_fail (ATK_IS_TEXT (text), -1);
814 iface = ATK_TEXT_GET_IFACE (text);
816 if (iface->get_character_count)
817 return (*(iface->get_character_count)) (text);
818 else
819 return -1;
823 * atk_text_get_offset_at_point:
824 * @text: an #AtkText
825 * @x: screen x-position of character
826 * @y: screen y-position of character
827 * @coords: specify whether coordinates are relative to the screen or
828 * widget window
830 * Gets the offset of the character located at coordinates @x and @y. @x and @y
831 * are interpreted as being relative to the screen or this widget's window
832 * depending on @coords.
834 * Returns: the offset to the character which is located at
835 * the specified @x and @y coordinates.
837 gint
838 atk_text_get_offset_at_point (AtkText *text,
839 gint x,
840 gint y,
841 AtkCoordType coords)
843 AtkTextIface *iface;
845 g_return_val_if_fail (ATK_IS_TEXT (text), -1);
847 iface = ATK_TEXT_GET_IFACE (text);
849 if (iface->get_offset_at_point)
850 return (*(iface->get_offset_at_point)) (text, x, y, coords);
851 else
852 return -1;
856 * atk_text_get_n_selections:
857 * @text: an #AtkText
859 * Gets the number of selected regions.
861 * Returns: The number of selected regions, or -1 if a failure
862 * occurred.
864 gint
865 atk_text_get_n_selections (AtkText *text)
867 AtkTextIface *iface;
869 g_return_val_if_fail (ATK_IS_TEXT (text), -1);
871 iface = ATK_TEXT_GET_IFACE (text);
873 if (iface->get_n_selections)
874 return (*(iface->get_n_selections)) (text);
875 else
876 return -1;
880 * atk_text_get_selection:
881 * @text: an #AtkText
882 * @selection_num: The selection number. The selected regions are
883 * assigned numbers that correspond to how far the region is from the
884 * start of the text. The selected region closest to the beginning
885 * of the text region is assigned the number 0, etc. Note that adding,
886 * moving or deleting a selected region can change the numbering.
887 * @start_offset: (out): passes back the start position of the selected region
888 * @end_offset: (out): passes back the end position of (e.g. offset immediately past)
889 * the selected region
891 * Gets the text from the specified selection.
893 * Returns: a newly allocated string containing the selected text. Use g_free()
894 * to free the returned string.
896 gchar*
897 atk_text_get_selection (AtkText *text,
898 gint selection_num,
899 gint *start_offset,
900 gint *end_offset)
902 AtkTextIface *iface;
903 gint local_start_offset, local_end_offset;
904 gint *real_start_offset, *real_end_offset;
906 g_return_val_if_fail (ATK_IS_TEXT (text), NULL);
908 if (start_offset)
909 real_start_offset = start_offset;
910 else
911 real_start_offset = &local_start_offset;
912 if (end_offset)
913 real_end_offset = end_offset;
914 else
915 real_end_offset = &local_end_offset;
917 iface = ATK_TEXT_GET_IFACE (text);
919 if (iface->get_selection)
921 return (*(iface->get_selection)) (text, selection_num,
922 real_start_offset, real_end_offset);
924 else
925 return NULL;
929 * atk_text_add_selection:
930 * @text: an #AtkText
931 * @start_offset: the start position of the selected region
932 * @end_offset: the offset of the first character after the selected region.
934 * Adds a selection bounded by the specified offsets.
936 * Returns: %TRUE if success, %FALSE otherwise
938 gboolean
939 atk_text_add_selection (AtkText *text,
940 gint start_offset,
941 gint end_offset)
943 AtkTextIface *iface;
945 g_return_val_if_fail (ATK_IS_TEXT (text), FALSE);
947 iface = ATK_TEXT_GET_IFACE (text);
949 if (iface->add_selection)
950 return (*(iface->add_selection)) (text, start_offset, end_offset);
951 else
952 return FALSE;
956 * atk_text_remove_selection:
957 * @text: an #AtkText
958 * @selection_num: The selection number. The selected regions are
959 * assigned numbers that correspond to how far the region is from the
960 * start of the text. The selected region closest to the beginning
961 * of the text region is assigned the number 0, etc. Note that adding,
962 * moving or deleting a selected region can change the numbering.
964 * Removes the specified selection.
966 * Returns: %TRUE if success, %FALSE otherwise
968 gboolean
969 atk_text_remove_selection (AtkText *text,
970 gint selection_num)
972 AtkTextIface *iface;
974 g_return_val_if_fail (ATK_IS_TEXT (text), FALSE);
976 iface = ATK_TEXT_GET_IFACE (text);
978 if (iface->remove_selection)
979 return (*(iface->remove_selection)) (text, selection_num);
980 else
981 return FALSE;
985 * atk_text_set_selection:
986 * @text: an #AtkText
987 * @selection_num: The selection number. The selected regions are
988 * assigned numbers that correspond to how far the region is from the
989 * start of the text. The selected region closest to the beginning
990 * of the text region is assigned the number 0, etc. Note that adding,
991 * moving or deleting a selected region can change the numbering.
992 * @start_offset: the new start position of the selection
993 * @end_offset: the new end position of (e.g. offset immediately past)
994 * the selection
996 * Changes the start and end offset of the specified selection.
998 * Returns: %TRUE if success, %FALSE otherwise
1000 gboolean
1001 atk_text_set_selection (AtkText *text,
1002 gint selection_num,
1003 gint start_offset,
1004 gint end_offset)
1006 AtkTextIface *iface;
1008 g_return_val_if_fail (ATK_IS_TEXT (text), FALSE);
1010 iface = ATK_TEXT_GET_IFACE (text);
1012 if (iface->set_selection)
1014 return (*(iface->set_selection)) (text, selection_num,
1015 start_offset, end_offset);
1017 else
1018 return FALSE;
1022 * atk_text_set_caret_offset:
1023 * @text: an #AtkText
1024 * @offset: position
1026 * Sets the caret (cursor) position to the specified @offset.
1028 * Returns: %TRUE if success, %FALSE otherwise.
1030 gboolean
1031 atk_text_set_caret_offset (AtkText *text,
1032 gint offset)
1034 AtkTextIface *iface;
1036 g_return_val_if_fail (ATK_IS_TEXT (text), FALSE);
1038 iface = ATK_TEXT_GET_IFACE (text);
1040 if (iface->set_caret_offset)
1042 return (*(iface->set_caret_offset)) (text, offset);
1044 else
1046 return FALSE;
1051 * atk_text_get_range_extents:
1052 * @text: an #AtkText
1053 * @start_offset: The offset of the first text character for which boundary
1054 * information is required.
1055 * @end_offset: The offset of the text character after the last character
1056 * for which boundary information is required.
1057 * @coord_type: Specify whether coordinates are relative to the screen or widget window.
1058 * @rect: A pointer to a AtkTextRectangle which is filled in by this function.
1060 * Get the bounding box for text within the specified range.
1062 * Since: 1.3
1064 void
1065 atk_text_get_range_extents (AtkText *text,
1066 gint start_offset,
1067 gint end_offset,
1068 AtkCoordType coord_type,
1069 AtkTextRectangle *rect)
1071 AtkTextIface *iface;
1073 g_return_if_fail (ATK_IS_TEXT (text));
1074 g_return_if_fail (rect);
1075 g_return_if_fail (start_offset >= 0 && start_offset < end_offset);
1077 iface = ATK_TEXT_GET_IFACE (text);
1079 if (iface->get_range_extents)
1080 (*(iface->get_range_extents)) (text, start_offset, end_offset, coord_type, rect);
1084 * atk_text_get_bounded_ranges:
1085 * @text: an #AtkText
1086 * @rect: An AtkTextRectangle giving the dimensions of the bounding box.
1087 * @coord_type: Specify whether coordinates are relative to the screen or widget window.
1088 * @x_clip_type: Specify the horizontal clip type.
1089 * @y_clip_type: Specify the vertical clip type.
1091 * Get the ranges of text in the specified bounding box.
1093 * Since: 1.3
1095 * Returns: (array zero-terminated=1): Array of AtkTextRange. The last
1096 * element of the array returned by this function will be NULL.
1098 AtkTextRange**
1099 atk_text_get_bounded_ranges (AtkText *text,
1100 AtkTextRectangle *rect,
1101 AtkCoordType coord_type,
1102 AtkTextClipType x_clip_type,
1103 AtkTextClipType y_clip_type)
1105 AtkTextIface *iface;
1107 g_return_val_if_fail (ATK_IS_TEXT (text), NULL);
1108 g_return_val_if_fail (rect, NULL);
1110 iface = ATK_TEXT_GET_IFACE (text);
1112 if (iface->get_bounded_ranges)
1113 return (*(iface->get_bounded_ranges)) (text, rect, coord_type, x_clip_type, y_clip_type);
1114 else
1115 return NULL;
1119 * atk_attribute_set_free:
1120 * @attrib_set: The #AtkAttributeSet to free
1122 * Frees the memory used by an #AtkAttributeSet, including all its
1123 * #AtkAttributes.
1125 void
1126 atk_attribute_set_free (AtkAttributeSet *attrib_set)
1128 GSList *temp;
1130 temp = attrib_set;
1132 while (temp != NULL)
1134 AtkAttribute *att;
1136 att = temp->data;
1138 g_free (att->name);
1139 g_free (att->value);
1140 g_free (att);
1141 temp = temp->next;
1143 g_slist_free (attrib_set);
1147 * atk_text_attribute_register:
1148 * @name: a name string
1150 * Associate @name with a new #AtkTextAttribute
1152 * Returns: an #AtkTextAttribute associated with @name
1154 AtkTextAttribute
1155 atk_text_attribute_register (const gchar *name)
1157 g_return_val_if_fail (name, ATK_TEXT_ATTR_INVALID);
1159 if (!extra_attributes)
1160 extra_attributes = g_ptr_array_new ();
1162 g_ptr_array_add (extra_attributes, g_strdup (name));
1163 return extra_attributes->len + ATK_TEXT_ATTR_LAST_DEFINED;
1167 * atk_text_attribute_get_name:
1168 * @attr: The #AtkTextAttribute whose name is required
1170 * Gets the name corresponding to the #AtkTextAttribute
1172 * Returns: a string containing the name; this string should not be freed
1174 const gchar*
1175 atk_text_attribute_get_name (AtkTextAttribute attr)
1177 GTypeClass *type_class;
1178 GEnumValue *value;
1179 const gchar *name = NULL;
1181 type_class = g_type_class_ref (ATK_TYPE_TEXT_ATTRIBUTE);
1182 g_return_val_if_fail (G_IS_ENUM_CLASS (type_class), NULL);
1184 value = g_enum_get_value (G_ENUM_CLASS (type_class), attr);
1186 if (value)
1188 name = value->value_nick;
1190 else
1192 if (extra_attributes)
1194 gint n = attr;
1196 n -= ATK_TEXT_ATTR_LAST_DEFINED + 1;
1198 if (n < extra_attributes->len)
1200 name = g_ptr_array_index (extra_attributes, n);
1203 g_type_class_unref (type_class);
1204 return name;
1208 * atk_text_attribute_for_name:
1209 * @name: a string which is the (non-localized) name of an ATK text attribute.
1211 * Get the #AtkTextAttribute type corresponding to a text attribute name.
1213 * Returns: the #AtkTextAttribute enumerated type corresponding to the specified
1214 name,
1215 * or #ATK_TEXT_ATTRIBUTE_INVALID if no matching text attribute is found.
1217 AtkTextAttribute
1218 atk_text_attribute_for_name (const gchar *name)
1220 GTypeClass *type_class;
1221 GEnumValue *value;
1222 AtkTextAttribute type = ATK_TEXT_ATTR_INVALID;
1224 g_return_val_if_fail (name, ATK_TEXT_ATTR_INVALID);
1226 type_class = g_type_class_ref (ATK_TYPE_TEXT_ATTRIBUTE);
1227 g_return_val_if_fail (G_IS_ENUM_CLASS (type_class), ATK_TEXT_ATTR_INVALID);
1229 value = g_enum_get_value_by_nick (G_ENUM_CLASS (type_class), name);
1231 if (value)
1233 type = value->value;
1235 else
1237 gint i;
1239 if (extra_attributes)
1241 for (i = 0; i < extra_attributes->len; i++)
1243 gchar *extra_attribute = (gchar *)g_ptr_array_index (extra_attributes, i);
1245 g_return_val_if_fail (extra_attribute, ATK_TEXT_ATTR_INVALID);
1247 if (strcmp (name, extra_attribute) == 0)
1249 type = i + 1 + ATK_TEXT_ATTR_LAST_DEFINED;
1250 break;
1255 g_type_class_unref (type_class);
1257 return type;
1261 * atk_text_attribute_get_value:
1262 * @attr: The #AtkTextAttribute for which a value is required
1263 * @index_: The index of the required value
1265 * Gets the value for the index of the #AtkTextAttribute
1267 * Returns: (nullable): a string containing the value; this string
1268 * should not be freed; %NULL is returned if there are no values
1269 * maintained for the attr value.
1271 const gchar*
1272 atk_text_attribute_get_value (AtkTextAttribute attr,
1273 gint index)
1275 switch (attr)
1277 case ATK_TEXT_ATTR_INVISIBLE:
1278 case ATK_TEXT_ATTR_EDITABLE:
1279 case ATK_TEXT_ATTR_BG_FULL_HEIGHT:
1280 case ATK_TEXT_ATTR_STRIKETHROUGH:
1281 case ATK_TEXT_ATTR_BG_STIPPLE:
1282 case ATK_TEXT_ATTR_FG_STIPPLE:
1283 g_assert (index >= 0 && index < G_N_ELEMENTS (boolean_offsets));
1284 return boolean + boolean_offsets[index];
1285 case ATK_TEXT_ATTR_UNDERLINE:
1286 g_assert (index >= 0 && index < G_N_ELEMENTS (underline_offsets));
1287 return underline + underline_offsets[index];
1288 case ATK_TEXT_ATTR_WRAP_MODE:
1289 g_assert (index >= 0 && index < G_N_ELEMENTS (wrap_mode_offsets));
1290 return wrap_mode + wrap_mode_offsets[index];
1291 case ATK_TEXT_ATTR_DIRECTION:
1292 g_assert (index >= 0 && index < G_N_ELEMENTS (direction_offsets));
1293 return direction + direction_offsets[index];
1294 case ATK_TEXT_ATTR_JUSTIFICATION:
1295 g_assert (index >= 0 && index < G_N_ELEMENTS (justification_offsets));
1296 return justification + justification_offsets[index];
1297 case ATK_TEXT_ATTR_STRETCH:
1298 g_assert (index >= 0 && index < G_N_ELEMENTS (stretch_offsets));
1299 return stretch + stretch_offsets[index];
1300 case ATK_TEXT_ATTR_VARIANT:
1301 g_assert (index >= 0 && index < G_N_ELEMENTS (variant_offsets));
1302 return variant + variant_offsets[index];
1303 case ATK_TEXT_ATTR_STYLE:
1304 g_assert (index >= 0 && index < G_N_ELEMENTS (style_offsets));
1305 return style + style_offsets[index];
1306 default:
1307 return NULL;
1311 static void
1312 atk_text_rectangle_union (AtkTextRectangle *src1,
1313 AtkTextRectangle *src2,
1314 AtkTextRectangle *dest)
1316 gint dest_x, dest_y;
1318 dest_x = MIN (src1->x, src2->x);
1319 dest_y = MIN (src1->y, src2->y);
1320 dest->width = MAX (src1->x + src1->width, src2->x + src2->width) - dest_x;
1321 dest->height = MAX (src1->y + src1->height, src2->y + src2->height) - dest_y;
1322 dest->x = dest_x;
1323 dest->y = dest_y;
1326 static gboolean
1327 atk_text_rectangle_contain (AtkTextRectangle *clip,
1328 AtkTextRectangle *bounds,
1329 AtkTextClipType x_clip_type,
1330 AtkTextClipType y_clip_type)
1332 gboolean x_min_ok, x_max_ok, y_min_ok, y_max_ok;
1334 x_min_ok = (bounds->x >= clip->x) ||
1335 ((bounds->x + bounds->width >= clip->x) &&
1336 ((x_clip_type == ATK_TEXT_CLIP_NONE) ||
1337 (x_clip_type == ATK_TEXT_CLIP_MAX)));
1339 x_max_ok = (bounds->x + bounds->width <= clip->x + clip->width) ||
1340 ((bounds->x <= clip->x + clip->width) &&
1341 ((x_clip_type == ATK_TEXT_CLIP_NONE) ||
1342 (x_clip_type == ATK_TEXT_CLIP_MIN)));
1344 y_min_ok = (bounds->y >= clip->y) ||
1345 ((bounds->y + bounds->height >= clip->y) &&
1346 ((y_clip_type == ATK_TEXT_CLIP_NONE) ||
1347 (y_clip_type == ATK_TEXT_CLIP_MAX)));
1349 y_max_ok = (bounds->y + bounds->height <= clip->y + clip->height) ||
1350 ((bounds->y <= clip->y + clip->height) &&
1351 ((y_clip_type == ATK_TEXT_CLIP_NONE) ||
1352 (y_clip_type == ATK_TEXT_CLIP_MIN)));
1354 return (x_min_ok && x_max_ok && y_min_ok && y_max_ok);
1358 static void
1359 atk_text_real_get_range_extents (AtkText *text,
1360 gint start_offset,
1361 gint end_offset,
1362 AtkCoordType coord_type,
1363 AtkTextRectangle *rect)
1365 gint i;
1366 AtkTextRectangle cbounds, bounds;
1368 atk_text_get_character_extents (text, start_offset,
1369 &bounds.x, &bounds.y,
1370 &bounds.width, &bounds.height,
1371 coord_type);
1373 for (i = start_offset + 1; i < end_offset; i++)
1375 atk_text_get_character_extents (text, i,
1376 &cbounds.x, &cbounds.y,
1377 &cbounds.width, &cbounds.height,
1378 coord_type);
1379 atk_text_rectangle_union (&bounds, &cbounds, &bounds);
1382 rect->x = bounds.x;
1383 rect->y = bounds.y;
1384 rect->width = bounds.width;
1385 rect->height = bounds.height;
1388 static AtkTextRange**
1389 atk_text_real_get_bounded_ranges (AtkText *text,
1390 AtkTextRectangle *rect,
1391 AtkCoordType coord_type,
1392 AtkTextClipType x_clip_type,
1393 AtkTextClipType y_clip_type)
1395 gint bounds_min_offset, bounds_max_offset;
1396 gint min_line_start, min_line_end;
1397 gint max_line_start, max_line_end;
1398 gchar *line;
1399 gint curr_offset;
1400 gint offset;
1401 gint num_ranges = 0;
1402 gint range_size = 1;
1403 AtkTextRectangle cbounds;
1404 AtkTextRange **range;
1406 range = NULL;
1407 bounds_min_offset = atk_text_get_offset_at_point (text, rect->x, rect->y, coord_type);
1408 bounds_max_offset = atk_text_get_offset_at_point (text, rect->x + rect->width, rect->y + rect->height, coord_type);
1410 if (bounds_min_offset == 0 &&
1411 bounds_min_offset == bounds_max_offset)
1412 return NULL;
1414 line = atk_text_get_text_at_offset (text, bounds_min_offset,
1415 ATK_TEXT_BOUNDARY_LINE_START,
1416 &min_line_start, &min_line_end);
1417 g_free (line);
1418 line = atk_text_get_text_at_offset (text, bounds_max_offset,
1419 ATK_TEXT_BOUNDARY_LINE_START,
1420 &max_line_start, &max_line_end);
1421 g_free (line);
1422 bounds_min_offset = MIN (min_line_start, max_line_start);
1423 bounds_max_offset = MAX (min_line_end, max_line_end);
1425 curr_offset = bounds_min_offset;
1426 while (curr_offset < bounds_max_offset)
1428 offset = curr_offset;
1430 while (curr_offset < bounds_max_offset)
1432 atk_text_get_character_extents (text, curr_offset,
1433 &cbounds.x, &cbounds.y,
1434 &cbounds.width, &cbounds.height,
1435 coord_type);
1436 if (!atk_text_rectangle_contain (rect, &cbounds, x_clip_type, y_clip_type))
1437 break;
1438 curr_offset++;
1440 if (curr_offset > offset)
1442 AtkTextRange *one_range = g_new (AtkTextRange, 1);
1444 one_range->start_offset = offset;
1445 one_range->end_offset = curr_offset;
1446 one_range->content = atk_text_get_text (text, offset, curr_offset);
1447 atk_text_get_range_extents (text, offset, curr_offset, coord_type, &one_range->bounds);
1449 if (num_ranges >= range_size - 1)
1451 range_size *= 2;
1452 range = g_realloc (range, range_size * sizeof (gpointer));
1454 range[num_ranges] = one_range;
1455 num_ranges++;
1457 curr_offset++;
1458 if (range)
1459 range[num_ranges] = NULL;
1461 return range;
1465 * atk_text_free_ranges:
1466 * @ranges: (array): A pointer to an array of #AtkTextRange which is
1467 * to be freed.
1469 * Frees the memory associated with an array of AtkTextRange. It is assumed
1470 * that the array was returned by the function atk_text_get_bounded_ranges
1471 * and is NULL terminated.
1473 * Since: 1.3
1475 void
1476 atk_text_free_ranges (AtkTextRange **ranges)
1478 AtkTextRange **first = ranges;
1480 if (ranges)
1482 while (*ranges)
1484 AtkTextRange *range;
1486 range = *ranges;
1487 ranges++;
1488 g_free (range->content);
1489 g_free (range);
1491 g_free (first);
1495 static AtkTextRange *
1496 atk_text_range_copy (AtkTextRange *src)
1498 AtkTextRange *dst = g_new0 (AtkTextRange, 1);
1499 dst->bounds = src->bounds;
1500 dst->start_offset = src->start_offset;
1501 dst->end_offset = src->end_offset;
1502 if (src->content)
1503 dst->content = g_strdup (src->content);
1504 return dst;
1507 static void
1508 atk_text_range_free (AtkTextRange *range)
1510 g_free (range->content);
1511 g_free (range);
1514 G_DEFINE_BOXED_TYPE (AtkTextRange, atk_text_range, atk_text_range_copy,
1515 atk_text_range_free)