atkcomponent: more info on the deprecation guards for get_size and get_position
[atk.git] / atk / atktext.c
blobcdbc1edaaa33ce0118b192dda58982c0028e8065
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 "atk.h"
21 #include "atkmarshal.h"
23 #include <string.h>
25 /**
26 * SECTION:atktext
27 * @Short_description: The ATK interface implemented by components
28 * with text content.
29 * @Title:AtkText
31 * #AtkText should be implemented by #AtkObjects on behalf of widgets
32 * that have text content which is either attributed or otherwise
33 * non-trivial. #AtkObjects whose text content is simple,
34 * unattributed, and very brief may expose that content via
35 * #atk_object_get_name instead; however if the text is editable,
36 * multi-line, typically longer than three or four words, attributed,
37 * selectable, or if the object already uses the 'name' ATK property
38 * for other information, the #AtkText interface should be used to
39 * expose the text content. In the case of editable text content,
40 * #AtkEditableText (a subtype of the #AtkText interface) should be
41 * implemented instead.
43 * #AtkText provides not only traversal facilities and change
44 * notification for text content, but also caret tracking and glyph
45 * bounding box calculations. Note that the text strings are exposed
46 * as UTF-8, and are therefore potentially multi-byte, and
47 * caret-to-byte offset mapping makes no assumptions about the
48 * character length; also bounding box glyph-to-offset mapping may be
49 * complex for languages which use ligatures.
52 static GPtrArray *extra_attributes = NULL;
54 enum {
55 TEXT_CHANGED,
56 TEXT_CARET_MOVED,
57 TEXT_SELECTION_CHANGED,
58 TEXT_ATTRIBUTES_CHANGED,
59 TEXT_INSERT,
60 TEXT_REMOVE,
61 LAST_SIGNAL
64 static const char boolean[] =
65 "false\0"
66 "true";
67 static const guint8 boolean_offsets[] = {
68 0, 6
71 static const char style[] =
72 "normal\0"
73 "oblique\0"
74 "italic";
75 static const guint8 style_offsets[] = {
76 0, 7, 15
79 static const char variant[] =
80 "normal\0"
81 "small_caps";
82 static const guint8 variant_offsets[] = {
83 0, 7
86 static const char stretch[] =
87 "ultra_condensed\0"
88 "extra_condensed\0"
89 "condensed\0"
90 "semi_condensed\0"
91 "normal\0"
92 "semi_expanded\0"
93 "expanded\0"
94 "extra_expanded\0"
95 "ultra_expanded";
96 static const guint8 stretch_offsets[] = {
97 0, 16, 32, 42, 57, 64, 78, 87, 102
100 static const char justification[] =
101 "left\0"
102 "right\0"
103 "center\0"
104 "fill";
105 static const guint8 justification_offsets[] = {
106 0, 5, 11, 18
109 static const char direction[] =
110 "none\0"
111 "ltr\0"
112 "rtl";
113 static const guint8 direction_offsets[] = {
114 0, 5, 9
117 static const char wrap_mode[] =
118 "none\0"
119 "char\0"
120 "word\0"
121 "word_char";
122 static const guint8 wrap_mode_offsets[] = {
123 0, 5, 10, 15
126 static const char underline[] =
127 "none\0"
128 "single\0"
129 "double\0"
130 "low\0"
131 "error";
132 static const guint8 underline_offsets[] = {
133 0, 5, 12, 19, 23
136 static void atk_text_base_init (AtkTextIface *class);
138 static void atk_text_real_get_range_extents (AtkText *text,
139 gint start_offset,
140 gint end_offset,
141 AtkCoordType coord_type,
142 AtkTextRectangle *rect);
144 static AtkTextRange** atk_text_real_get_bounded_ranges (AtkText *text,
145 AtkTextRectangle *rect,
146 AtkCoordType coord_type,
147 AtkTextClipType x_clip_type,
148 AtkTextClipType y_clip_type);
150 static guint atk_text_signals[LAST_SIGNAL] = { 0 };
152 GType
153 atk_text_get_type (void)
155 static GType type = 0;
157 if (!type)
159 static const GTypeInfo tinfo =
161 sizeof (AtkTextIface),
162 (GBaseInitFunc) atk_text_base_init,
163 (GBaseFinalizeFunc) NULL,
164 (GClassInitFunc) NULL /* atk_text_interface_init */ ,
165 (GClassFinalizeFunc) NULL,
169 type = g_type_register_static (G_TYPE_INTERFACE, "AtkText", &tinfo, 0);
172 return type;
175 static void
176 atk_text_base_init (AtkTextIface *class)
178 static gboolean initialized = FALSE;
180 if (! initialized)
183 * Note that text_changed signal supports details "insert", "delete",
184 * possibly "replace".
187 class->get_range_extents = atk_text_real_get_range_extents;
188 class->get_bounded_ranges = atk_text_real_get_bounded_ranges;
191 * AtkText::text-changed:
192 * @atktext: the object which received the signal.
193 * @arg1: The position (character offset) of the insertion or deletion.
194 * @arg2: The length (in characters) of text inserted or deleted.
196 * The "text-changed" signal is emitted when the text of the
197 * object which implements the AtkText interface changes, This
198 * signal will have a detail which is either "insert" or
199 * "delete" which identifies whether the text change was an
200 * insertion or a deletion.
202 * Deprecated: Since 2.9.4. Use #AtkObject::text-insert or
203 * #AtkObject::text-remove instead.
205 atk_text_signals[TEXT_CHANGED] =
206 g_signal_new ("text_changed",
207 ATK_TYPE_TEXT,
208 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
209 G_STRUCT_OFFSET (AtkTextIface, text_changed),
210 (GSignalAccumulator) NULL, NULL,
211 atk_marshal_VOID__INT_INT,
212 G_TYPE_NONE,
213 2, G_TYPE_INT, G_TYPE_INT);
216 * AtkText::text-insert:
217 * @atktext: the object which received the signal.
218 * @arg1: The position (character offset) of the insertion.
219 * @arg2: The length (in characters) of text inserted.
220 * @arg3: The new text inserted
222 * The "text-insert" signal is emitted when a new text is
223 * inserted. If the signal was not triggered by the user
224 * (e.g. typing or pasting text), the "system" detail should be
225 * included.
227 atk_text_signals[TEXT_INSERT] =
228 g_signal_new ("text_insert",
229 ATK_TYPE_TEXT,
230 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
232 (GSignalAccumulator) NULL, NULL,
233 atk_marshal_VOID__INT_INT_STRING,
234 G_TYPE_NONE,
235 3, G_TYPE_INT, G_TYPE_INT, G_TYPE_STRING);
238 * AtkText::text-remove:
239 * @atktext: the object which received the signal.
240 * @arg1: The position (character offset) of the removal.
241 * @arg2: The length (in characters) of text removed.
242 * @arg3: The old text removed
244 * The "text-remove" signal is emitted when a new text is
245 * removed. If the signal was not triggered by the user
246 * (e.g. typing or pasting text), the "system" detail should be
247 * included.
249 atk_text_signals[TEXT_REMOVE] =
250 g_signal_new ("text_remove",
251 ATK_TYPE_TEXT,
252 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
254 (GSignalAccumulator) NULL, NULL,
255 atk_marshal_VOID__INT_INT_STRING,
256 G_TYPE_NONE,
257 3, G_TYPE_INT, G_TYPE_INT, G_TYPE_STRING);
260 * AtkText::text-caret-moved:
261 * @atktext: the object which received the signal.
262 * @arg1: The new position of the text caret.
264 * The "text-caret-moved" signal is emitted when the caret
265 * position of the text of an object which implements AtkText
266 * changes.
268 atk_text_signals[TEXT_CARET_MOVED] =
269 g_signal_new ("text_caret_moved",
270 ATK_TYPE_TEXT,
271 G_SIGNAL_RUN_LAST,
272 G_STRUCT_OFFSET (AtkTextIface, text_caret_moved),
273 (GSignalAccumulator) NULL, NULL,
274 g_cclosure_marshal_VOID__INT,
275 G_TYPE_NONE,
276 1, G_TYPE_INT);
279 * AtkText::text-selection-changed:
280 * @atktext: the object which received the signal.
282 * The "text-selection-changed" signal is emitted when the
283 * selected text of an object which implements AtkText changes.
285 atk_text_signals[TEXT_SELECTION_CHANGED] =
286 g_signal_new ("text_selection_changed",
287 ATK_TYPE_TEXT,
288 G_SIGNAL_RUN_LAST,
289 G_STRUCT_OFFSET (AtkTextIface, text_selection_changed),
290 (GSignalAccumulator) NULL, NULL,
291 g_cclosure_marshal_VOID__VOID,
292 G_TYPE_NONE, 0);
294 * AtkText::text-attributes-changed:
295 * @atktext: the object which received the signal.
297 * The "text-attributes-changed" signal is emitted when the text
298 * attributes of the text of an object which implements AtkText
299 * changes.
301 atk_text_signals[TEXT_ATTRIBUTES_CHANGED] =
302 g_signal_new ("text_attributes_changed",
303 ATK_TYPE_TEXT,
304 G_SIGNAL_RUN_LAST,
305 G_STRUCT_OFFSET (AtkTextIface, text_attributes_changed),
306 (GSignalAccumulator) NULL, NULL,
307 g_cclosure_marshal_VOID__VOID,
308 G_TYPE_NONE, 0);
311 initialized = TRUE;
316 * atk_text_get_text:
317 * @text: an #AtkText
318 * @start_offset: start position
319 * @end_offset: end position, or -1 for the end of the string.
321 * Gets the specified text.
323 * Returns: a newly allocated string containing the text from @start_offset up
324 * to, but not including @end_offset. Use g_free() to free the returned string.
326 gchar*
327 atk_text_get_text (AtkText *text,
328 gint start_offset,
329 gint end_offset)
331 AtkTextIface *iface;
333 g_return_val_if_fail (ATK_IS_TEXT (text), NULL);
335 iface = ATK_TEXT_GET_IFACE (text);
337 if (start_offset < 0 || end_offset < -1 ||
338 (end_offset != -1 && end_offset < start_offset))
339 return NULL;
341 if (iface->get_text)
342 return (*(iface->get_text)) (text, start_offset, end_offset);
343 else
344 return NULL;
348 * atk_text_get_character_at_offset:
349 * @text: an #AtkText
350 * @offset: position
352 * Gets the specified text.
354 * Returns: the character at @offset.
356 gunichar
357 atk_text_get_character_at_offset (AtkText *text,
358 gint offset)
360 AtkTextIface *iface;
362 g_return_val_if_fail (ATK_IS_TEXT (text), (gunichar) 0);
364 iface = ATK_TEXT_GET_IFACE (text);
366 if (iface->get_character_at_offset)
367 return (*(iface->get_character_at_offset)) (text, offset);
368 else
369 return (gunichar) 0;
373 * atk_text_get_text_after_offset:
374 * @text: an #AtkText
375 * @offset: position
376 * @boundary_type: An #AtkTextBoundary
377 * @start_offset: (out): the start offset of the returned string
378 * @end_offset: (out): the offset of the first character after the
379 * returned substring
381 * Gets the specified text.
383 * Deprecated: This method is deprecated since ATK version
384 * 2.9.3. Please use atk_text_get_string_at_offset() instead.
386 * Returns: a newly allocated string containing the text after @offset bounded
387 * by the specified @boundary_type. Use g_free() to free the returned string.
389 gchar*
390 atk_text_get_text_after_offset (AtkText *text,
391 gint offset,
392 AtkTextBoundary boundary_type,
393 gint *start_offset,
394 gint *end_offset)
396 AtkTextIface *iface;
397 gint local_start_offset, local_end_offset;
398 gint *real_start_offset, *real_end_offset;
400 g_return_val_if_fail (ATK_IS_TEXT (text), NULL);
402 if (start_offset)
403 real_start_offset = start_offset;
404 else
405 real_start_offset = &local_start_offset;
406 if (end_offset)
407 real_end_offset = end_offset;
408 else
409 real_end_offset = &local_end_offset;
411 if (offset < 0)
412 return NULL;
414 iface = ATK_TEXT_GET_IFACE (text);
416 if (iface->get_text_after_offset)
417 return (*(iface->get_text_after_offset)) (text, offset, boundary_type, real_start_offset, real_end_offset);
418 else
419 return NULL;
423 * atk_text_get_text_at_offset:
424 * @text: an #AtkText
425 * @offset: position
426 * @boundary_type: An #AtkTextBoundary
427 * @start_offset: (out): the start offset of the returned string
428 * @end_offset: (out): the offset of the first character after the
429 * returned substring
431 * Gets the specified text.
433 * If the boundary_type if ATK_TEXT_BOUNDARY_CHAR the character at the
434 * offset is returned.
436 * If the boundary_type is ATK_TEXT_BOUNDARY_WORD_START the returned string
437 * is from the word start at or before the offset to the word start after
438 * the offset.
440 * The returned string will contain the word at the offset if the offset
441 * is inside a word and will contain the word before the offset if the
442 * offset is not inside a word.
444 * If the boundary type is ATK_TEXT_BOUNDARY_SENTENCE_START the returned
445 * string is from the sentence start at or before the offset to the sentence
446 * start after the offset.
448 * The returned string will contain the sentence at the offset if the offset
449 * is inside a sentence and will contain the sentence before the offset
450 * if the offset is not inside a sentence.
452 * If the boundary type is ATK_TEXT_BOUNDARY_LINE_START the returned
453 * string is from the line start at or before the offset to the line
454 * start after the offset.
456 * Deprecated: This method is deprecated since ATK version
457 * 2.9.4. Please use atk_text_get_string_at_offset() instead.
459 * Returns: a newly allocated string containing the text at @offset bounded by
460 * the specified @boundary_type. Use g_free() to free the returned string.
462 gchar*
463 atk_text_get_text_at_offset (AtkText *text,
464 gint offset,
465 AtkTextBoundary boundary_type,
466 gint *start_offset,
467 gint *end_offset)
469 AtkTextIface *iface;
470 gint local_start_offset, local_end_offset;
471 gint *real_start_offset, *real_end_offset;
473 g_return_val_if_fail (ATK_IS_TEXT (text), NULL);
475 if (start_offset)
476 real_start_offset = start_offset;
477 else
478 real_start_offset = &local_start_offset;
479 if (end_offset)
480 real_end_offset = end_offset;
481 else
482 real_end_offset = &local_end_offset;
484 iface = ATK_TEXT_GET_IFACE (text);
486 if (iface->get_text_at_offset)
487 return (*(iface->get_text_at_offset)) (text, offset, boundary_type, real_start_offset, real_end_offset);
488 else
489 return NULL;
493 * atk_text_get_text_before_offset:
494 * @text: an #AtkText
495 * @offset: position
496 * @boundary_type: An #AtkTextBoundary
497 * @start_offset: (out): the start offset of the returned string
498 * @end_offset: (out): the offset of the first character after the
499 * returned substring
501 * Gets the specified text.
503 * Deprecated: This method is deprecated since ATK version
504 * 2.9.3. Please use atk_text_get_string_at_offset() instead.
506 * Returns: a newly allocated string containing the text before @offset bounded
507 * by the specified @boundary_type. Use g_free() to free the returned string.
509 gchar*
510 atk_text_get_text_before_offset (AtkText *text,
511 gint offset,
512 AtkTextBoundary boundary_type,
513 gint *start_offset,
514 gint *end_offset)
516 AtkTextIface *iface;
517 gint local_start_offset, local_end_offset;
518 gint *real_start_offset, *real_end_offset;
520 g_return_val_if_fail (ATK_IS_TEXT (text), NULL);
522 if (start_offset)
523 real_start_offset = start_offset;
524 else
525 real_start_offset = &local_start_offset;
526 if (end_offset)
527 real_end_offset = end_offset;
528 else
529 real_end_offset = &local_end_offset;
531 if (offset < 0)
532 return NULL;
534 iface = ATK_TEXT_GET_IFACE (text);
536 if (iface->get_text_before_offset)
537 return (*(iface->get_text_before_offset)) (text, offset, boundary_type, real_start_offset, real_end_offset);
538 else
539 return NULL;
543 * atk_text_get_string_at_offset:
544 * @text: an #AtkText
545 * @offset: position
546 * @granularity: An #AtkTextGranularity
547 * @start_offset: (out): the start offset of the returned string, or -1
548 * if an error has occurred (e.g. invalid offset, not implemented)
549 * @end_offset: (out): the offset of the first character after the returned string,
550 * or -1 if an error has occurred (e.g. invalid offset, not implemented)
552 * Gets a portion of the text exposed through an #AtkText according to a given @offset
553 * and a specific @granularity, along with the start and end offsets defining the
554 * boundaries of such a portion of text.
556 * If @granularity is ATK_TEXT_GRANULARITY_CHAR the character at the
557 * offset is returned.
559 * If @granularity is ATK_TEXT_GRANULARITY_WORD the returned string
560 * is from the word start at or before the offset to the word start after
561 * the offset.
563 * The returned string will contain the word at the offset if the offset
564 * is inside a word and will contain the word before the offset if the
565 * offset is not inside a word.
567 * If @granularity is ATK_TEXT_GRANULARITY_SENTENCE the returned string
568 * is from the sentence start at or before the offset to the sentence
569 * start after the offset.
571 * The returned string will contain the sentence at the offset if the offset
572 * is inside a sentence and will contain the sentence before the offset
573 * if the offset is not inside a sentence.
575 * If @granularity is ATK_TEXT_GRANULARITY_LINE the returned string
576 * is from the line start at or before the offset to the line
577 * start after the offset.
579 * If @granularity is ATK_TEXT_GRANULARITY_PARAGRAPH the returned string
580 * is from the start of the paragraph at or before the offset to the start
581 * of the following paragraph after the offset.
583 * Since: 2.10
585 * Returns: a newly allocated string containing the text at the @offset bounded
586 * by the specified @granularity. Use g_free() to free the returned string.
587 * Returns %NULL if the offset is invalid or no implementation is available.
589 gchar* atk_text_get_string_at_offset (AtkText *text,
590 gint offset,
591 AtkTextGranularity granularity,
592 gint *start_offset,
593 gint *end_offset)
595 AtkTextIface *iface;
596 gint local_start_offset, local_end_offset;
597 gint *real_start_offset, *real_end_offset;
599 g_return_val_if_fail (ATK_IS_TEXT (text), NULL);
601 if (start_offset)
603 *start_offset = -1;
604 real_start_offset = start_offset;
606 else
607 real_start_offset = &local_start_offset;
609 if (end_offset)
611 *end_offset = -1;
612 real_end_offset = end_offset;
614 else
615 real_end_offset = &local_end_offset;
617 if (offset < 0)
618 return NULL;
620 iface = ATK_TEXT_GET_IFACE (text);
622 if (iface->get_string_at_offset)
623 return (*(iface->get_string_at_offset)) (text, offset, granularity, real_start_offset, real_end_offset);
624 else
625 return NULL;
629 * atk_text_get_caret_offset:
630 * @text: an #AtkText
632 * Gets the offset position of the caret (cursor).
634 * Returns: the offset position of the caret (cursor).
636 gint
637 atk_text_get_caret_offset (AtkText *text)
639 AtkTextIface *iface;
641 g_return_val_if_fail (ATK_IS_TEXT (text), 0);
643 iface = ATK_TEXT_GET_IFACE (text);
645 if (iface->get_caret_offset)
646 return (*(iface->get_caret_offset)) (text);
647 else
648 return 0;
652 * atk_text_get_character_extents:
653 * @text: an #AtkText
654 * @offset: The offset of the text character for which bounding information is required.
655 * @x: Pointer for the x cordinate of the bounding box
656 * @y: Pointer for the y cordinate of the bounding box
657 * @width: Pointer for the width of the bounding box
658 * @height: Pointer for the height of the bounding box
659 * @coords: specify whether coordinates are relative to the screen or widget window
661 * Get the bounding box containing the glyph representing the character at
662 * a particular text offset.
664 void
665 atk_text_get_character_extents (AtkText *text,
666 gint offset,
667 gint *x,
668 gint *y,
669 gint *width,
670 gint *height,
671 AtkCoordType coords)
673 AtkTextIface *iface;
674 gint local_x, local_y, local_width, local_height;
675 gint *real_x, *real_y, *real_width, *real_height;
677 g_return_if_fail (ATK_IS_TEXT (text));
679 if (x)
680 real_x = x;
681 else
682 real_x = &local_x;
683 if (y)
684 real_y = y;
685 else
686 real_y = &local_y;
687 if (width)
688 real_width = width;
689 else
690 real_width = &local_width;
691 if (height)
692 real_height = height;
693 else
694 real_height = &local_height;
696 *real_x = 0;
697 *real_y = 0;
698 *real_width = 0;
699 *real_height = 0;
701 if (offset < 0)
702 return;
704 iface = ATK_TEXT_GET_IFACE (text);
706 if (iface->get_character_extents)
707 (*(iface->get_character_extents)) (text, offset, real_x, real_y, real_width, real_height, coords);
709 if (*real_width <0)
711 *real_x = *real_x + *real_width;
712 *real_width *= -1;
717 * atk_text_get_run_attributes:
718 *@text: an #AtkText
719 *@offset: the offset at which to get the attributes, -1 means the offset of
720 *the character to be inserted at the caret location.
721 *@start_offset: (out): the address to put the start offset of the range
722 *@end_offset: (out): the address to put the end offset of the range
724 *Creates an #AtkAttributeSet which consists of the attributes explicitly
725 *set at the position @offset in the text. @start_offset and @end_offset are
726 *set to the start and end of the range around @offset where the attributes are
727 *invariant. Note that @end_offset is the offset of the first character
728 *after the range. See the enum AtkTextAttribute for types of text
729 *attributes that can be returned. Note that other attributes may also be
730 *returned.
732 *Returns: (transfer full): an #AtkAttributeSet which contains the attributes
733 * explicitly set at @offset. This #AtkAttributeSet should be freed by a call
734 * to atk_attribute_set_free().
736 AtkAttributeSet*
737 atk_text_get_run_attributes (AtkText *text,
738 gint offset,
739 gint *start_offset,
740 gint *end_offset)
742 AtkTextIface *iface;
743 gint local_start_offset, local_end_offset;
744 gint *real_start_offset, *real_end_offset;
746 g_return_val_if_fail (ATK_IS_TEXT (text), NULL);
748 if (start_offset)
749 real_start_offset = start_offset;
750 else
751 real_start_offset = &local_start_offset;
752 if (end_offset)
753 real_end_offset = end_offset;
754 else
755 real_end_offset = &local_end_offset;
757 if (offset < -1)
758 return NULL;
760 iface = ATK_TEXT_GET_IFACE (text);
762 if (iface->get_run_attributes)
763 return (*(iface->get_run_attributes)) (text, offset, real_start_offset, real_end_offset);
764 else
765 return NULL;
769 * atk_text_get_default_attributes:
770 *@text: an #AtkText
772 *Creates an #AtkAttributeSet which consists of the default values of
773 *attributes for the text. See the enum AtkTextAttribute for types of text
774 *attributes that can be returned. Note that other attributes may also be
775 *returned.
777 *Returns: (transfer full): an #AtkAttributeSet which contains the default
778 * values of attributes. at @offset. this #atkattributeset should be freed by
779 * a call to atk_attribute_set_free().
781 AtkAttributeSet*
782 atk_text_get_default_attributes (AtkText *text)
784 AtkTextIface *iface;
786 g_return_val_if_fail (ATK_IS_TEXT (text), NULL);
788 iface = ATK_TEXT_GET_IFACE (text);
790 if (iface->get_default_attributes)
791 return (*(iface->get_default_attributes)) (text);
792 else
793 return NULL;
797 * atk_text_get_character_count:
798 * @text: an #AtkText
800 * Gets the character count.
802 * Returns: the number of characters.
804 gint
805 atk_text_get_character_count (AtkText *text)
807 AtkTextIface *iface;
809 g_return_val_if_fail (ATK_IS_TEXT (text), -1);
811 iface = ATK_TEXT_GET_IFACE (text);
813 if (iface->get_character_count)
814 return (*(iface->get_character_count)) (text);
815 else
816 return -1;
820 * atk_text_get_offset_at_point:
821 * @text: an #AtkText
822 * @x: screen x-position of character
823 * @y: screen y-position of character
824 * @coords: specify whether coordinates are relative to the screen or
825 * widget window
827 * Gets the offset of the character located at coordinates @x and @y. @x and @y
828 * are interpreted as being relative to the screen or this widget's window
829 * depending on @coords.
831 * Returns: the offset to the character which is located at
832 * the specified @x and @y coordinates.
834 gint
835 atk_text_get_offset_at_point (AtkText *text,
836 gint x,
837 gint y,
838 AtkCoordType coords)
840 AtkTextIface *iface;
842 g_return_val_if_fail (ATK_IS_TEXT (text), -1);
844 iface = ATK_TEXT_GET_IFACE (text);
846 if (iface->get_offset_at_point)
847 return (*(iface->get_offset_at_point)) (text, x, y, coords);
848 else
849 return -1;
853 * atk_text_get_n_selections:
854 * @text: an #AtkText
856 * Gets the number of selected regions.
858 * Returns: The number of selected regions, or -1 if a failure
859 * occurred.
861 gint
862 atk_text_get_n_selections (AtkText *text)
864 AtkTextIface *iface;
866 g_return_val_if_fail (ATK_IS_TEXT (text), -1);
868 iface = ATK_TEXT_GET_IFACE (text);
870 if (iface->get_n_selections)
871 return (*(iface->get_n_selections)) (text);
872 else
873 return -1;
877 * atk_text_get_selection:
878 * @text: an #AtkText
879 * @selection_num: The selection number. The selected regions are
880 * assigned numbers that correspond to how far the region is from the
881 * start of the text. The selected region closest to the beginning
882 * of the text region is assigned the number 0, etc. Note that adding,
883 * moving or deleting a selected region can change the numbering.
884 * @start_offset: (out): passes back the start position of the selected region
885 * @end_offset: (out): passes back the end position of (e.g. offset immediately past)
886 * the selected region
888 * Gets the text from the specified selection.
890 * Returns: a newly allocated string containing the selected text. Use g_free()
891 * to free the returned string.
893 gchar*
894 atk_text_get_selection (AtkText *text,
895 gint selection_num,
896 gint *start_offset,
897 gint *end_offset)
899 AtkTextIface *iface;
900 gint local_start_offset, local_end_offset;
901 gint *real_start_offset, *real_end_offset;
903 g_return_val_if_fail (ATK_IS_TEXT (text), NULL);
905 if (start_offset)
906 real_start_offset = start_offset;
907 else
908 real_start_offset = &local_start_offset;
909 if (end_offset)
910 real_end_offset = end_offset;
911 else
912 real_end_offset = &local_end_offset;
914 iface = ATK_TEXT_GET_IFACE (text);
916 if (iface->get_selection)
918 return (*(iface->get_selection)) (text, selection_num,
919 real_start_offset, real_end_offset);
921 else
922 return NULL;
926 * atk_text_add_selection:
927 * @text: an #AtkText
928 * @start_offset: the start position of the selected region
929 * @end_offset: the offset of the first character after the selected region.
931 * Adds a selection bounded by the specified offsets.
933 * Returns: %TRUE if success, %FALSE otherwise
935 gboolean
936 atk_text_add_selection (AtkText *text,
937 gint start_offset,
938 gint end_offset)
940 AtkTextIface *iface;
942 g_return_val_if_fail (ATK_IS_TEXT (text), FALSE);
944 iface = ATK_TEXT_GET_IFACE (text);
946 if (iface->add_selection)
947 return (*(iface->add_selection)) (text, start_offset, end_offset);
948 else
949 return FALSE;
953 * atk_text_remove_selection:
954 * @text: an #AtkText
955 * @selection_num: The selection number. The selected regions are
956 * assigned numbers that correspond to how far the region is from the
957 * start of the text. The selected region closest to the beginning
958 * of the text region is assigned the number 0, etc. Note that adding,
959 * moving or deleting a selected region can change the numbering.
961 * Removes the specified selection.
963 * Returns: %TRUE if success, %FALSE otherwise
965 gboolean
966 atk_text_remove_selection (AtkText *text,
967 gint selection_num)
969 AtkTextIface *iface;
971 g_return_val_if_fail (ATK_IS_TEXT (text), FALSE);
973 iface = ATK_TEXT_GET_IFACE (text);
975 if (iface->remove_selection)
976 return (*(iface->remove_selection)) (text, selection_num);
977 else
978 return FALSE;
982 * atk_text_set_selection:
983 * @text: an #AtkText
984 * @selection_num: The selection number. The selected regions are
985 * assigned numbers that correspond to how far the region is from the
986 * start of the text. The selected region closest to the beginning
987 * of the text region is assigned the number 0, etc. Note that adding,
988 * moving or deleting a selected region can change the numbering.
989 * @start_offset: the new start position of the selection
990 * @end_offset: the new end position of (e.g. offset immediately past)
991 * the selection
993 * Changes the start and end offset of the specified selection.
995 * Returns: %TRUE if success, %FALSE otherwise
997 gboolean
998 atk_text_set_selection (AtkText *text,
999 gint selection_num,
1000 gint start_offset,
1001 gint end_offset)
1003 AtkTextIface *iface;
1005 g_return_val_if_fail (ATK_IS_TEXT (text), FALSE);
1007 iface = ATK_TEXT_GET_IFACE (text);
1009 if (iface->set_selection)
1011 return (*(iface->set_selection)) (text, selection_num,
1012 start_offset, end_offset);
1014 else
1015 return FALSE;
1019 * atk_text_set_caret_offset:
1020 * @text: an #AtkText
1021 * @offset: position
1023 * Sets the caret (cursor) position to the specified @offset.
1025 * Returns: %TRUE if success, %FALSE otherwise.
1027 gboolean
1028 atk_text_set_caret_offset (AtkText *text,
1029 gint offset)
1031 AtkTextIface *iface;
1033 g_return_val_if_fail (ATK_IS_TEXT (text), FALSE);
1035 iface = ATK_TEXT_GET_IFACE (text);
1037 if (iface->set_caret_offset)
1039 return (*(iface->set_caret_offset)) (text, offset);
1041 else
1043 return FALSE;
1048 * atk_text_get_range_extents:
1049 * @text: an #AtkText
1050 * @start_offset: The offset of the first text character for which boundary
1051 * information is required.
1052 * @end_offset: The offset of the text character after the last character
1053 * for which boundary information is required.
1054 * @coord_type: Specify whether coordinates are relative to the screen or widget window.
1055 * @rect: A pointer to a AtkTextRectangle which is filled in by this function.
1057 * Get the bounding box for text within the specified range.
1059 * Since: 1.3
1061 void
1062 atk_text_get_range_extents (AtkText *text,
1063 gint start_offset,
1064 gint end_offset,
1065 AtkCoordType coord_type,
1066 AtkTextRectangle *rect)
1068 AtkTextIface *iface;
1070 g_return_if_fail (ATK_IS_TEXT (text));
1071 g_return_if_fail (rect);
1072 g_return_if_fail (start_offset >= 0 && start_offset < end_offset);
1074 iface = ATK_TEXT_GET_IFACE (text);
1076 if (iface->get_range_extents)
1077 (*(iface->get_range_extents)) (text, start_offset, end_offset, coord_type, rect);
1081 * atk_text_get_bounded_ranges:
1082 * @text: an #AtkText
1083 * @rect: An AtkTextRectangle giving the dimensions of the bounding box.
1084 * @coord_type: Specify whether coordinates are relative to the screen or widget window.
1085 * @x_clip_type: Specify the horizontal clip type.
1086 * @y_clip_type: Specify the vertical clip type.
1088 * Get the ranges of text in the specified bounding box.
1090 * Since: 1.3
1092 * Returns: (array zero-terminated=1): Array of AtkTextRange. The last
1093 * element of the array returned by this function will be NULL.
1095 AtkTextRange**
1096 atk_text_get_bounded_ranges (AtkText *text,
1097 AtkTextRectangle *rect,
1098 AtkCoordType coord_type,
1099 AtkTextClipType x_clip_type,
1100 AtkTextClipType y_clip_type)
1102 AtkTextIface *iface;
1104 g_return_val_if_fail (ATK_IS_TEXT (text), NULL);
1105 g_return_val_if_fail (rect, NULL);
1107 iface = ATK_TEXT_GET_IFACE (text);
1109 if (iface->get_bounded_ranges)
1110 return (*(iface->get_bounded_ranges)) (text, rect, coord_type, x_clip_type, y_clip_type);
1111 else
1112 return NULL;
1116 * atk_attribute_set_free:
1117 * @attrib_set: The #AtkAttributeSet to free
1119 * Frees the memory used by an #AtkAttributeSet, including all its
1120 * #AtkAttributes.
1122 void
1123 atk_attribute_set_free (AtkAttributeSet *attrib_set)
1125 GSList *temp;
1127 temp = attrib_set;
1129 while (temp != NULL)
1131 AtkAttribute *att;
1133 att = temp->data;
1135 g_free (att->name);
1136 g_free (att->value);
1137 g_free (att);
1138 temp = temp->next;
1140 g_slist_free (attrib_set);
1144 * atk_text_attribute_register:
1145 * @name: a name string
1147 * Associate @name with a new #AtkTextAttribute
1149 * Returns: an #AtkTextAttribute associated with @name
1151 AtkTextAttribute
1152 atk_text_attribute_register (const gchar *name)
1154 g_return_val_if_fail (name, ATK_TEXT_ATTR_INVALID);
1156 if (!extra_attributes)
1157 extra_attributes = g_ptr_array_new ();
1159 g_ptr_array_add (extra_attributes, g_strdup (name));
1160 return extra_attributes->len + ATK_TEXT_ATTR_LAST_DEFINED;
1164 * atk_text_attribute_get_name:
1165 * @attr: The #AtkTextAttribute whose name is required
1167 * Gets the name corresponding to the #AtkTextAttribute
1169 * Returns: a string containing the name; this string should not be freed
1171 const gchar*
1172 atk_text_attribute_get_name (AtkTextAttribute attr)
1174 GTypeClass *type_class;
1175 GEnumValue *value;
1176 const gchar *name = NULL;
1178 type_class = g_type_class_ref (ATK_TYPE_TEXT_ATTRIBUTE);
1179 g_return_val_if_fail (G_IS_ENUM_CLASS (type_class), NULL);
1181 value = g_enum_get_value (G_ENUM_CLASS (type_class), attr);
1183 if (value)
1185 name = value->value_nick;
1187 else
1189 if (extra_attributes)
1191 gint n = attr;
1193 n -= ATK_TEXT_ATTR_LAST_DEFINED + 1;
1195 if (n < extra_attributes->len)
1197 name = g_ptr_array_index (extra_attributes, n);
1200 g_type_class_unref (type_class);
1201 return name;
1205 * atk_text_attribute_for_name:
1206 * @name: a string which is the (non-localized) name of an ATK text attribute.
1208 * Get the #AtkTextAttribute type corresponding to a text attribute name.
1210 * Returns: the #AtkTextAttribute enumerated type corresponding to the specified
1211 name,
1212 * or #ATK_TEXT_ATTRIBUTE_INVALID if no matching text attribute is found.
1214 AtkTextAttribute
1215 atk_text_attribute_for_name (const gchar *name)
1217 GTypeClass *type_class;
1218 GEnumValue *value;
1219 AtkTextAttribute type = ATK_TEXT_ATTR_INVALID;
1221 g_return_val_if_fail (name, ATK_TEXT_ATTR_INVALID);
1223 type_class = g_type_class_ref (ATK_TYPE_TEXT_ATTRIBUTE);
1224 g_return_val_if_fail (G_IS_ENUM_CLASS (type_class), ATK_TEXT_ATTR_INVALID);
1226 value = g_enum_get_value_by_nick (G_ENUM_CLASS (type_class), name);
1228 if (value)
1230 type = value->value;
1232 else
1234 gint i;
1236 if (extra_attributes)
1238 for (i = 0; i < extra_attributes->len; i++)
1240 gchar *extra_attribute = (gchar *)g_ptr_array_index (extra_attributes, i);
1242 g_return_val_if_fail (extra_attribute, ATK_TEXT_ATTR_INVALID);
1244 if (strcmp (name, extra_attribute) == 0)
1246 type = i + 1 + ATK_TEXT_ATTR_LAST_DEFINED;
1247 break;
1252 g_type_class_unref (type_class);
1254 return type;
1258 * atk_text_attribute_get_value:
1259 * @attr: The #AtkTextAttribute for which a value is required
1260 * @index_: The index of the required value
1262 * Gets the value for the index of the #AtkTextAttribute
1264 * Returns: a string containing the value; this string should not be freed;
1265 * NULL is returned if there are no values maintained for the attr value.
1267 const gchar*
1268 atk_text_attribute_get_value (AtkTextAttribute attr,
1269 gint index)
1271 switch (attr)
1273 case ATK_TEXT_ATTR_INVISIBLE:
1274 case ATK_TEXT_ATTR_EDITABLE:
1275 case ATK_TEXT_ATTR_BG_FULL_HEIGHT:
1276 case ATK_TEXT_ATTR_STRIKETHROUGH:
1277 case ATK_TEXT_ATTR_BG_STIPPLE:
1278 case ATK_TEXT_ATTR_FG_STIPPLE:
1279 g_assert (index >= 0 && index < G_N_ELEMENTS (boolean_offsets));
1280 return boolean + boolean_offsets[index];
1281 case ATK_TEXT_ATTR_UNDERLINE:
1282 g_assert (index >= 0 && index < G_N_ELEMENTS (underline_offsets));
1283 return underline + underline_offsets[index];
1284 case ATK_TEXT_ATTR_WRAP_MODE:
1285 g_assert (index >= 0 && index < G_N_ELEMENTS (wrap_mode_offsets));
1286 return wrap_mode + wrap_mode_offsets[index];
1287 case ATK_TEXT_ATTR_DIRECTION:
1288 g_assert (index >= 0 && index < G_N_ELEMENTS (direction_offsets));
1289 return direction + direction_offsets[index];
1290 case ATK_TEXT_ATTR_JUSTIFICATION:
1291 g_assert (index >= 0 && index < G_N_ELEMENTS (justification_offsets));
1292 return justification + justification_offsets[index];
1293 case ATK_TEXT_ATTR_STRETCH:
1294 g_assert (index >= 0 && index < G_N_ELEMENTS (stretch_offsets));
1295 return stretch + stretch_offsets[index];
1296 case ATK_TEXT_ATTR_VARIANT:
1297 g_assert (index >= 0 && index < G_N_ELEMENTS (variant_offsets));
1298 return variant + variant_offsets[index];
1299 case ATK_TEXT_ATTR_STYLE:
1300 g_assert (index >= 0 && index < G_N_ELEMENTS (style_offsets));
1301 return style + style_offsets[index];
1302 default:
1303 return NULL;
1307 static void
1308 atk_text_rectangle_union (AtkTextRectangle *src1,
1309 AtkTextRectangle *src2,
1310 AtkTextRectangle *dest)
1312 gint dest_x, dest_y;
1314 dest_x = MIN (src1->x, src2->x);
1315 dest_y = MIN (src1->y, src2->y);
1316 dest->width = MAX (src1->x + src1->width, src2->x + src2->width) - dest_x;
1317 dest->height = MAX (src1->y + src1->height, src2->y + src2->height) - dest_y;
1318 dest->x = dest_x;
1319 dest->y = dest_y;
1322 static gboolean
1323 atk_text_rectangle_contain (AtkTextRectangle *clip,
1324 AtkTextRectangle *bounds,
1325 AtkTextClipType x_clip_type,
1326 AtkTextClipType y_clip_type)
1328 gboolean x_min_ok, x_max_ok, y_min_ok, y_max_ok;
1330 x_min_ok = (bounds->x >= clip->x) ||
1331 ((bounds->x + bounds->width >= clip->x) &&
1332 ((x_clip_type == ATK_TEXT_CLIP_NONE) ||
1333 (x_clip_type == ATK_TEXT_CLIP_MAX)));
1335 x_max_ok = (bounds->x + bounds->width <= clip->x + clip->width) ||
1336 ((bounds->x <= clip->x + clip->width) &&
1337 ((x_clip_type == ATK_TEXT_CLIP_NONE) ||
1338 (x_clip_type == ATK_TEXT_CLIP_MIN)));
1340 y_min_ok = (bounds->y >= clip->y) ||
1341 ((bounds->y + bounds->height >= clip->y) &&
1342 ((y_clip_type == ATK_TEXT_CLIP_NONE) ||
1343 (y_clip_type == ATK_TEXT_CLIP_MAX)));
1345 y_max_ok = (bounds->y + bounds->height <= clip->y + clip->height) ||
1346 ((bounds->y <= clip->y + clip->height) &&
1347 ((y_clip_type == ATK_TEXT_CLIP_NONE) ||
1348 (y_clip_type == ATK_TEXT_CLIP_MIN)));
1350 return (x_min_ok && x_max_ok && y_min_ok && y_max_ok);
1354 static void
1355 atk_text_real_get_range_extents (AtkText *text,
1356 gint start_offset,
1357 gint end_offset,
1358 AtkCoordType coord_type,
1359 AtkTextRectangle *rect)
1361 gint i;
1362 AtkTextRectangle cbounds, bounds;
1364 atk_text_get_character_extents (text, start_offset,
1365 &bounds.x, &bounds.y,
1366 &bounds.width, &bounds.height,
1367 coord_type);
1369 for (i = start_offset + 1; i < end_offset; i++)
1371 atk_text_get_character_extents (text, i,
1372 &cbounds.x, &cbounds.y,
1373 &cbounds.width, &cbounds.height,
1374 coord_type);
1375 atk_text_rectangle_union (&bounds, &cbounds, &bounds);
1378 rect->x = bounds.x;
1379 rect->y = bounds.y;
1380 rect->width = bounds.width;
1381 rect->height = bounds.height;
1384 static AtkTextRange**
1385 atk_text_real_get_bounded_ranges (AtkText *text,
1386 AtkTextRectangle *rect,
1387 AtkCoordType coord_type,
1388 AtkTextClipType x_clip_type,
1389 AtkTextClipType y_clip_type)
1391 gint bounds_min_offset, bounds_max_offset;
1392 gint min_line_start, min_line_end;
1393 gint max_line_start, max_line_end;
1394 gchar *line;
1395 gint curr_offset;
1396 gint offset;
1397 gint num_ranges = 0;
1398 gint range_size = 1;
1399 AtkTextRectangle cbounds;
1400 AtkTextRange **range;
1402 range = NULL;
1403 bounds_min_offset = atk_text_get_offset_at_point (text, rect->x, rect->y, coord_type);
1404 bounds_max_offset = atk_text_get_offset_at_point (text, rect->x + rect->width, rect->y + rect->height, coord_type);
1406 if (bounds_min_offset == 0 &&
1407 bounds_min_offset == bounds_max_offset)
1408 return NULL;
1410 line = atk_text_get_text_at_offset (text, bounds_min_offset,
1411 ATK_TEXT_BOUNDARY_LINE_START,
1412 &min_line_start, &min_line_end);
1413 g_free (line);
1414 line = atk_text_get_text_at_offset (text, bounds_max_offset,
1415 ATK_TEXT_BOUNDARY_LINE_START,
1416 &max_line_start, &max_line_end);
1417 g_free (line);
1418 bounds_min_offset = MIN (min_line_start, max_line_start);
1419 bounds_max_offset = MAX (min_line_end, max_line_end);
1421 curr_offset = bounds_min_offset;
1422 while (curr_offset < bounds_max_offset)
1424 offset = curr_offset;
1426 while (curr_offset < bounds_max_offset)
1428 atk_text_get_character_extents (text, curr_offset,
1429 &cbounds.x, &cbounds.y,
1430 &cbounds.width, &cbounds.height,
1431 coord_type);
1432 if (!atk_text_rectangle_contain (rect, &cbounds, x_clip_type, y_clip_type))
1433 break;
1434 curr_offset++;
1436 if (curr_offset > offset)
1438 AtkTextRange *one_range = g_new (AtkTextRange, 1);
1440 one_range->start_offset = offset;
1441 one_range->end_offset = curr_offset;
1442 one_range->content = atk_text_get_text (text, offset, curr_offset);
1443 atk_text_get_range_extents (text, offset, curr_offset, coord_type, &one_range->bounds);
1445 if (num_ranges >= range_size - 1)
1447 range_size *= 2;
1448 range = g_realloc (range, range_size * sizeof (gpointer));
1450 range[num_ranges] = one_range;
1451 num_ranges++;
1453 curr_offset++;
1454 if (range)
1455 range[num_ranges] = NULL;
1457 return range;
1461 * atk_text_free_ranges:
1462 * @ranges: (array): A pointer to an array of #AtkTextRange which is
1463 * to be freed.
1465 * Frees the memory associated with an array of AtkTextRange. It is assumed
1466 * that the array was returned by the function atk_text_get_bounded_ranges
1467 * and is NULL terminated.
1469 * Since: 1.3
1471 void
1472 atk_text_free_ranges (AtkTextRange **ranges)
1474 AtkTextRange **first = ranges;
1476 if (ranges)
1478 while (*ranges)
1480 AtkTextRange *range;
1482 range = *ranges;
1483 ranges++;
1484 g_free (range->content);
1485 g_free (range);
1487 g_free (first);
1491 static AtkTextRange *
1492 atk_text_range_copy (AtkTextRange *src)
1494 AtkTextRange *dst = g_new0 (AtkTextRange, 1);
1495 dst->bounds = src->bounds;
1496 dst->start_offset = src->start_offset;
1497 dst->end_offset = src->end_offset;
1498 if (src->content)
1499 dst->content = g_strdup (src->content);
1500 return dst;
1503 static void
1504 atk_text_range_free (AtkTextRange *range)
1506 g_free (range->content);
1507 g_free (range);
1510 G_DEFINE_BOXED_TYPE (AtkTextRange, atk_text_range, atk_text_range_copy,
1511 atk_text_range_free)