Deprecate 'text-changed' signal
[atk.git] / atk / atktext.c
blobf99a092f4cea03e48a0e1f768513321f937ad793
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 "atktext.h"
21 #include "atkmarshal.h"
22 #include "atk-enum-types.h"
24 #include <string.h>
26 /**
27 * SECTION:atktext
28 * @Short_description: The ATK interface implemented by components
29 * with text content.
30 * @Title:AtkText
32 * #AtkText should be implemented by #AtkObjects on behalf of widgets
33 * that have text content which is either attributed or otherwise
34 * non-trivial. #AtkObjects whose text content is simple,
35 * unattributed, and very brief may expose that content via
36 * #atk_object_get_name instead; however if the text is editable,
37 * multi-line, typically longer than three or four words, attributed,
38 * selectable, or if the object already uses the 'name' ATK property
39 * for other information, the #AtkText interface should be used to
40 * expose the text content. In the case of editable text content,
41 * #AtkEditableText (a subtype of the #AtkText interface) should be
42 * implemented instead.
44 * #AtkText provides not only traversal facilities and change
45 * notification for text content, but also caret tracking and glyph
46 * bounding box calculations. Note that the text strings are exposed
47 * as UTF-8, and are therefore potentially multi-byte, and
48 * caret-to-byte offset mapping makes no assumptions about the
49 * character length; also bounding box glyph-to-offset mapping may be
50 * complex for languages which use ligatures.
53 static GPtrArray *extra_attributes = NULL;
55 enum {
56 TEXT_CHANGED,
57 TEXT_CARET_MOVED,
58 TEXT_SELECTION_CHANGED,
59 TEXT_ATTRIBUTES_CHANGED,
60 TEXT_INSERT,
61 TEXT_REMOVE,
62 TEXT_UPDATE,
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 text inserted
224 * The "text-insert" signal is emitted when a new text is
225 * inserted.
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 text inserted
244 * The "text-remove" signal is emitted when a new text is
245 * removed.
247 atk_text_signals[TEXT_REMOVE] =
248 g_signal_new ("text_remove",
249 ATK_TYPE_TEXT,
250 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
252 (GSignalAccumulator) NULL, NULL,
253 atk_marshal_VOID__INT_INT_STRING,
254 G_TYPE_NONE,
255 3, G_TYPE_INT, G_TYPE_INT, G_TYPE_STRING);
258 * AtkText::text-update:
259 * @atktext: the object which received the signal.
260 * @arg1: unknown
261 * @arg2: unknown
262 * @arg3: unknown
263 * @arg4: unknown
265 * The "text-update" signal is emitted when a new text is
266 * updated.
268 atk_text_signals[TEXT_UPDATE] =
269 g_signal_new ("text_update",
270 ATK_TYPE_TEXT,
271 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
273 (GSignalAccumulator) NULL, NULL,
274 atk_marshal_VOID__INT_INT_INT_STRING,
275 G_TYPE_NONE,
276 4, G_TYPE_INT, G_TYPE_INT, G_TYPE_INT, G_TYPE_STRING);
280 * AtkText::text-caret-moved:
281 * @atktext: the object which received the signal.
282 * @arg1: The new position of the text caret.
284 * The "text-caret-moved" signal is emitted when the caret
285 * position of the text of an object which implements AtkText
286 * changes.
288 atk_text_signals[TEXT_CARET_MOVED] =
289 g_signal_new ("text_caret_moved",
290 ATK_TYPE_TEXT,
291 G_SIGNAL_RUN_LAST,
292 G_STRUCT_OFFSET (AtkTextIface, text_caret_moved),
293 (GSignalAccumulator) NULL, NULL,
294 g_cclosure_marshal_VOID__INT,
295 G_TYPE_NONE,
296 1, G_TYPE_INT);
299 * AtkText::text-selection-changed:
300 * @atktext: the object which received the signal.
302 * The "text-selection-changed" signal is emitted when the
303 * selected text of an object which implements AtkText changes.
305 atk_text_signals[TEXT_SELECTION_CHANGED] =
306 g_signal_new ("text_selection_changed",
307 ATK_TYPE_TEXT,
308 G_SIGNAL_RUN_LAST,
309 G_STRUCT_OFFSET (AtkTextIface, text_selection_changed),
310 (GSignalAccumulator) NULL, NULL,
311 g_cclosure_marshal_VOID__VOID,
312 G_TYPE_NONE, 0);
314 * AtkText::text-attributes-changed:
315 * @atktext: the object which received the signal.
317 * The "text-attributes-changed" signal is emitted when the text
318 * attributes of the text of an object which implements AtkText
319 * changes.
321 atk_text_signals[TEXT_ATTRIBUTES_CHANGED] =
322 g_signal_new ("text_attributes_changed",
323 ATK_TYPE_TEXT,
324 G_SIGNAL_RUN_LAST,
325 G_STRUCT_OFFSET (AtkTextIface, text_attributes_changed),
326 (GSignalAccumulator) NULL, NULL,
327 g_cclosure_marshal_VOID__VOID,
328 G_TYPE_NONE, 0);
331 initialized = TRUE;
336 * atk_text_get_text:
337 * @text: an #AtkText
338 * @start_offset: start position
339 * @end_offset: end position, or -1 for the end of the string.
341 * Gets the specified text.
343 * Returns: a newly allocated string containing the text from @start_offset up
344 * to, but not including @end_offset. Use g_free() to free the returned string.
346 gchar*
347 atk_text_get_text (AtkText *text,
348 gint start_offset,
349 gint end_offset)
351 AtkTextIface *iface;
353 g_return_val_if_fail (ATK_IS_TEXT (text), NULL);
355 iface = ATK_TEXT_GET_IFACE (text);
357 if (start_offset < 0 || end_offset < -1 ||
358 (end_offset != -1 && end_offset < start_offset))
359 return NULL;
361 if (iface->get_text)
362 return (*(iface->get_text)) (text, start_offset, end_offset);
363 else
364 return NULL;
368 * atk_text_get_character_at_offset:
369 * @text: an #AtkText
370 * @offset: position
372 * Gets the specified text.
374 * Returns: the character at @offset.
376 gunichar
377 atk_text_get_character_at_offset (AtkText *text,
378 gint offset)
380 AtkTextIface *iface;
382 g_return_val_if_fail (ATK_IS_TEXT (text), (gunichar) 0);
384 iface = ATK_TEXT_GET_IFACE (text);
386 if (iface->get_character_at_offset)
387 return (*(iface->get_character_at_offset)) (text, offset);
388 else
389 return (gunichar) 0;
393 * atk_text_get_text_after_offset:
394 * @text: an #AtkText
395 * @offset: position
396 * @boundary_type: An #AtkTextBoundary
397 * @start_offset: (out): the start offset of the returned string
398 * @end_offset: (out): the offset of the first character after the
399 * returned substring
401 * Gets the specified text.
403 * Deprecated: This method is deprecated since ATK version
404 * 2.9.3. Please use atk_text_get_at_offset() instead.
406 * Returns: a newly allocated string containing the text after @offset bounded
407 * by the specified @boundary_type. Use g_free() to free the returned string.
409 gchar*
410 atk_text_get_text_after_offset (AtkText *text,
411 gint offset,
412 AtkTextBoundary boundary_type,
413 gint *start_offset,
414 gint *end_offset)
416 AtkTextIface *iface;
417 gint local_start_offset, local_end_offset;
418 gint *real_start_offset, *real_end_offset;
420 g_return_val_if_fail (ATK_IS_TEXT (text), NULL);
422 if (start_offset)
423 real_start_offset = start_offset;
424 else
425 real_start_offset = &local_start_offset;
426 if (end_offset)
427 real_end_offset = end_offset;
428 else
429 real_end_offset = &local_end_offset;
431 if (offset < 0)
432 return NULL;
434 iface = ATK_TEXT_GET_IFACE (text);
436 if (iface->get_text_after_offset)
437 return (*(iface->get_text_after_offset)) (text, offset, boundary_type, real_start_offset, real_end_offset);
438 else
439 return NULL;
443 * atk_text_get_text_at_offset:
444 * @text: an #AtkText
445 * @offset: position
446 * @boundary_type: An #AtkTextBoundary
447 * @start_offset: (out): the start offset of the returned string
448 * @end_offset: (out): the offset of the first character after the
449 * returned substring
451 * Gets the specified text.
453 * If the boundary_type if ATK_TEXT_BOUNDARY_CHAR the character at the
454 * offset is returned.
456 * If the boundary_type is ATK_TEXT_BOUNDARY_WORD_START the returned string
457 * is from the word start at or before the offset to the word start after
458 * the offset.
460 * The returned string will contain the word at the offset if the offset
461 * is inside a word and will contain the word before the offset if the
462 * offset is not inside a word.
464 * If the boundary type is ATK_TEXT_BOUNDARY_SENTENCE_START the returned
465 * string is from the sentence start at or before the offset to the sentence
466 * start after the offset.
468 * The returned string will contain the sentence at the offset if the offset
469 * is inside a sentence and will contain the sentence before the offset
470 * if the offset is not inside a sentence.
472 * If the boundary type is ATK_TEXT_BOUNDARY_LINE_START the returned
473 * string is from the line start at or before the offset to the line
474 * start after the offset.
476 * Returns: a newly allocated string containing the text at @offset bounded by
477 * the specified @boundary_type. Use g_free() to free the returned string.
479 gchar*
480 atk_text_get_text_at_offset (AtkText *text,
481 gint offset,
482 AtkTextBoundary boundary_type,
483 gint *start_offset,
484 gint *end_offset)
486 AtkTextIface *iface;
487 gint local_start_offset, local_end_offset;
488 gint *real_start_offset, *real_end_offset;
490 g_return_val_if_fail (ATK_IS_TEXT (text), NULL);
492 if (start_offset)
493 real_start_offset = start_offset;
494 else
495 real_start_offset = &local_start_offset;
496 if (end_offset)
497 real_end_offset = end_offset;
498 else
499 real_end_offset = &local_end_offset;
501 iface = ATK_TEXT_GET_IFACE (text);
503 if (iface->get_text_at_offset)
504 return (*(iface->get_text_at_offset)) (text, offset, boundary_type, real_start_offset, real_end_offset);
505 else
506 return NULL;
510 * atk_text_get_text_before_offset:
511 * @text: an #AtkText
512 * @offset: position
513 * @boundary_type: An #AtkTextBoundary
514 * @start_offset: (out): the start offset of the returned string
515 * @end_offset: (out): the offset of the first character after the
516 * returned substring
518 * Gets the specified text.
520 * Deprecated: This method is deprecated since ATK version
521 * 2.9.3. Please use atk_text_get_at_offset() instead.
523 * Returns: a newly allocated string containing the text before @offset bounded
524 * by the specified @boundary_type. Use g_free() to free the returned string.
526 gchar*
527 atk_text_get_text_before_offset (AtkText *text,
528 gint offset,
529 AtkTextBoundary boundary_type,
530 gint *start_offset,
531 gint *end_offset)
533 AtkTextIface *iface;
534 gint local_start_offset, local_end_offset;
535 gint *real_start_offset, *real_end_offset;
537 g_return_val_if_fail (ATK_IS_TEXT (text), NULL);
539 if (start_offset)
540 real_start_offset = start_offset;
541 else
542 real_start_offset = &local_start_offset;
543 if (end_offset)
544 real_end_offset = end_offset;
545 else
546 real_end_offset = &local_end_offset;
548 if (offset < 0)
549 return NULL;
551 iface = ATK_TEXT_GET_IFACE (text);
553 if (iface->get_text_before_offset)
554 return (*(iface->get_text_before_offset)) (text, offset, boundary_type, real_start_offset, real_end_offset);
555 else
556 return NULL;
560 * atk_text_get_caret_offset:
561 * @text: an #AtkText
563 * Gets the offset position of the caret (cursor).
565 * Returns: the offset position of the caret (cursor).
567 gint
568 atk_text_get_caret_offset (AtkText *text)
570 AtkTextIface *iface;
572 g_return_val_if_fail (ATK_IS_TEXT (text), 0);
574 iface = ATK_TEXT_GET_IFACE (text);
576 if (iface->get_caret_offset)
577 return (*(iface->get_caret_offset)) (text);
578 else
579 return 0;
583 * atk_text_get_character_extents:
584 * @text: an #AtkText
585 * @offset: The offset of the text character for which bounding information is required.
586 * @x: Pointer for the x cordinate of the bounding box
587 * @y: Pointer for the y cordinate of the bounding box
588 * @width: Pointer for the width of the bounding box
589 * @height: Pointer for the height of the bounding box
590 * @coords: specify whether coordinates are relative to the screen or widget window
592 * Get the bounding box containing the glyph representing the character at
593 * a particular text offset.
595 void
596 atk_text_get_character_extents (AtkText *text,
597 gint offset,
598 gint *x,
599 gint *y,
600 gint *width,
601 gint *height,
602 AtkCoordType coords)
604 AtkTextIface *iface;
605 gint local_x, local_y, local_width, local_height;
606 gint *real_x, *real_y, *real_width, *real_height;
608 g_return_if_fail (ATK_IS_TEXT (text));
610 if (x)
611 real_x = x;
612 else
613 real_x = &local_x;
614 if (y)
615 real_y = y;
616 else
617 real_y = &local_y;
618 if (width)
619 real_width = width;
620 else
621 real_width = &local_width;
622 if (height)
623 real_height = height;
624 else
625 real_height = &local_height;
627 *real_x = 0;
628 *real_y = 0;
629 *real_width = 0;
630 *real_height = 0;
632 if (offset < 0)
633 return;
635 iface = ATK_TEXT_GET_IFACE (text);
637 if (iface->get_character_extents)
638 (*(iface->get_character_extents)) (text, offset, real_x, real_y, real_width, real_height, coords);
640 if (*real_width <0)
642 *real_x = *real_x + *real_width;
643 *real_width *= -1;
648 * atk_text_get_run_attributes:
649 *@text: an #AtkText
650 *@offset: the offset at which to get the attributes, -1 means the offset of
651 *the character to be inserted at the caret location.
652 *@start_offset: (out): the address to put the start offset of the range
653 *@end_offset: (out): the address to put the end offset of the range
655 *Creates an #AtkAttributeSet which consists of the attributes explicitly
656 *set at the position @offset in the text. @start_offset and @end_offset are
657 *set to the start and end of the range around @offset where the attributes are
658 *invariant. Note that @end_offset is the offset of the first character
659 *after the range. See the enum AtkTextAttribute for types of text
660 *attributes that can be returned. Note that other attributes may also be
661 *returned.
663 *Returns: (transfer full): an #AtkAttributeSet which contains the attributes
664 * explicitly set at @offset. This #AtkAttributeSet should be freed by a call
665 * to atk_attribute_set_free().
667 AtkAttributeSet*
668 atk_text_get_run_attributes (AtkText *text,
669 gint offset,
670 gint *start_offset,
671 gint *end_offset)
673 AtkTextIface *iface;
674 gint local_start_offset, local_end_offset;
675 gint *real_start_offset, *real_end_offset;
677 g_return_val_if_fail (ATK_IS_TEXT (text), NULL);
679 if (start_offset)
680 real_start_offset = start_offset;
681 else
682 real_start_offset = &local_start_offset;
683 if (end_offset)
684 real_end_offset = end_offset;
685 else
686 real_end_offset = &local_end_offset;
688 if (offset < -1)
689 return NULL;
691 iface = ATK_TEXT_GET_IFACE (text);
693 if (iface->get_run_attributes)
694 return (*(iface->get_run_attributes)) (text, offset, real_start_offset, real_end_offset);
695 else
696 return NULL;
700 * atk_text_get_default_attributes:
701 *@text: an #AtkText
703 *Creates an #AtkAttributeSet which consists of the default values of
704 *attributes for the text. See the enum AtkTextAttribute for types of text
705 *attributes that can be returned. Note that other attributes may also be
706 *returned.
708 *Returns: (transfer full): an #AtkAttributeSet which contains the default
709 * values of attributes. at @offset. this #atkattributeset should be freed by
710 * a call to atk_attribute_set_free().
712 AtkAttributeSet*
713 atk_text_get_default_attributes (AtkText *text)
715 AtkTextIface *iface;
717 g_return_val_if_fail (ATK_IS_TEXT (text), NULL);
719 iface = ATK_TEXT_GET_IFACE (text);
721 if (iface->get_default_attributes)
722 return (*(iface->get_default_attributes)) (text);
723 else
724 return NULL;
728 * atk_text_get_character_count:
729 * @text: an #AtkText
731 * Gets the character count.
733 * Returns: the number of characters.
735 gint
736 atk_text_get_character_count (AtkText *text)
738 AtkTextIface *iface;
740 g_return_val_if_fail (ATK_IS_TEXT (text), -1);
742 iface = ATK_TEXT_GET_IFACE (text);
744 if (iface->get_character_count)
745 return (*(iface->get_character_count)) (text);
746 else
747 return -1;
751 * atk_text_get_offset_at_point:
752 * @text: an #AtkText
753 * @x: screen x-position of character
754 * @y: screen y-position of character
755 * @coords: specify whether coordinates are relative to the screen or
756 * widget window
758 * Gets the offset of the character located at coordinates @x and @y. @x and @y
759 * are interpreted as being relative to the screen or this widget's window
760 * depending on @coords.
762 * Returns: the offset to the character which is located at
763 * the specified @x and @y coordinates.
765 gint
766 atk_text_get_offset_at_point (AtkText *text,
767 gint x,
768 gint y,
769 AtkCoordType coords)
771 AtkTextIface *iface;
773 g_return_val_if_fail (ATK_IS_TEXT (text), -1);
775 iface = ATK_TEXT_GET_IFACE (text);
777 if (iface->get_offset_at_point)
778 return (*(iface->get_offset_at_point)) (text, x, y, coords);
779 else
780 return -1;
784 * atk_text_get_n_selections:
785 * @text: an #AtkText
787 * Gets the number of selected regions.
789 * Returns: The number of selected regions, or -1 if a failure
790 * occurred.
792 gint
793 atk_text_get_n_selections (AtkText *text)
795 AtkTextIface *iface;
797 g_return_val_if_fail (ATK_IS_TEXT (text), -1);
799 iface = ATK_TEXT_GET_IFACE (text);
801 if (iface->get_n_selections)
802 return (*(iface->get_n_selections)) (text);
803 else
804 return -1;
808 * atk_text_get_selection:
809 * @text: an #AtkText
810 * @selection_num: The selection number. The selected regions are
811 * assigned numbers that correspond to how far the region is from the
812 * start of the text. The selected region closest to the beginning
813 * of the text region is assigned the number 0, etc. Note that adding,
814 * moving or deleting a selected region can change the numbering.
815 * @start_offset: (out): passes back the start position of the selected region
816 * @end_offset: (out): passes back the end position of (e.g. offset immediately past)
817 * the selected region
819 * Gets the text from the specified selection.
821 * Returns: a newly allocated string containing the selected text. Use g_free()
822 * to free the returned string.
824 gchar*
825 atk_text_get_selection (AtkText *text,
826 gint selection_num,
827 gint *start_offset,
828 gint *end_offset)
830 AtkTextIface *iface;
831 gint local_start_offset, local_end_offset;
832 gint *real_start_offset, *real_end_offset;
834 g_return_val_if_fail (ATK_IS_TEXT (text), NULL);
836 if (start_offset)
837 real_start_offset = start_offset;
838 else
839 real_start_offset = &local_start_offset;
840 if (end_offset)
841 real_end_offset = end_offset;
842 else
843 real_end_offset = &local_end_offset;
845 iface = ATK_TEXT_GET_IFACE (text);
847 if (iface->get_selection)
849 return (*(iface->get_selection)) (text, selection_num,
850 real_start_offset, real_end_offset);
852 else
853 return NULL;
857 * atk_text_add_selection:
858 * @text: an #AtkText
859 * @start_offset: the start position of the selected region
860 * @end_offset: the offset of the first character after the selected region.
862 * Adds a selection bounded by the specified offsets.
864 * Returns: %TRUE if success, %FALSE otherwise
866 gboolean
867 atk_text_add_selection (AtkText *text,
868 gint start_offset,
869 gint end_offset)
871 AtkTextIface *iface;
873 g_return_val_if_fail (ATK_IS_TEXT (text), FALSE);
875 iface = ATK_TEXT_GET_IFACE (text);
877 if (iface->add_selection)
878 return (*(iface->add_selection)) (text, start_offset, end_offset);
879 else
880 return FALSE;
884 * atk_text_remove_selection:
885 * @text: an #AtkText
886 * @selection_num: The selection number. The selected regions are
887 * assigned numbers that correspond to how far the region is from the
888 * start of the text. The selected region closest to the beginning
889 * of the text region is assigned the number 0, etc. Note that adding,
890 * moving or deleting a selected region can change the numbering.
892 * Removes the specified selection.
894 * Returns: %TRUE if success, %FALSE otherwise
896 gboolean
897 atk_text_remove_selection (AtkText *text,
898 gint selection_num)
900 AtkTextIface *iface;
902 g_return_val_if_fail (ATK_IS_TEXT (text), FALSE);
904 iface = ATK_TEXT_GET_IFACE (text);
906 if (iface->remove_selection)
907 return (*(iface->remove_selection)) (text, selection_num);
908 else
909 return FALSE;
913 * atk_text_set_selection:
914 * @text: an #AtkText
915 * @selection_num: The selection number. The selected regions are
916 * assigned numbers that correspond to how far the region is from the
917 * start of the text. The selected region closest to the beginning
918 * of the text region is assigned the number 0, etc. Note that adding,
919 * moving or deleting a selected region can change the numbering.
920 * @start_offset: the new start position of the selection
921 * @end_offset: the new end position of (e.g. offset immediately past)
922 * the selection
924 * Changes the start and end offset of the specified selection.
926 * Returns: %TRUE if success, %FALSE otherwise
928 gboolean
929 atk_text_set_selection (AtkText *text,
930 gint selection_num,
931 gint start_offset,
932 gint end_offset)
934 AtkTextIface *iface;
936 g_return_val_if_fail (ATK_IS_TEXT (text), FALSE);
938 iface = ATK_TEXT_GET_IFACE (text);
940 if (iface->set_selection)
942 return (*(iface->set_selection)) (text, selection_num,
943 start_offset, end_offset);
945 else
946 return FALSE;
950 * atk_text_set_caret_offset:
951 * @text: an #AtkText
952 * @offset: position
954 * Sets the caret (cursor) position to the specified @offset.
956 * Returns: %TRUE if success, %FALSE otherwise.
958 gboolean
959 atk_text_set_caret_offset (AtkText *text,
960 gint offset)
962 AtkTextIface *iface;
964 g_return_val_if_fail (ATK_IS_TEXT (text), FALSE);
966 iface = ATK_TEXT_GET_IFACE (text);
968 if (iface->set_caret_offset)
970 return (*(iface->set_caret_offset)) (text, offset);
972 else
974 return FALSE;
979 * atk_text_get_range_extents:
980 * @text: an #AtkText
981 * @start_offset: The offset of the first text character for which boundary
982 * information is required.
983 * @end_offset: The offset of the text character after the last character
984 * for which boundary information is required.
985 * @coord_type: Specify whether coordinates are relative to the screen or widget window.
986 * @rect: A pointer to a AtkTextRectangle which is filled in by this function.
988 * Get the bounding box for text within the specified range.
990 * Since: 1.3
992 void
993 atk_text_get_range_extents (AtkText *text,
994 gint start_offset,
995 gint end_offset,
996 AtkCoordType coord_type,
997 AtkTextRectangle *rect)
999 AtkTextIface *iface;
1001 g_return_if_fail (ATK_IS_TEXT (text));
1002 g_return_if_fail (rect);
1004 if (start_offset < 0 || start_offset >= end_offset)
1005 return;
1007 iface = ATK_TEXT_GET_IFACE (text);
1009 if (iface->get_range_extents)
1010 (*(iface->get_range_extents)) (text, start_offset, end_offset, coord_type, rect);
1014 * atk_text_get_bounded_ranges:
1015 * @text: an #AtkText
1016 * @rect: An AtkTextRectangle giving the dimensions of the bounding box.
1017 * @coord_type: Specify whether coordinates are relative to the screen or widget window.
1018 * @x_clip_type: Specify the horizontal clip type.
1019 * @y_clip_type: Specify the vertical clip type.
1021 * Get the ranges of text in the specified bounding box.
1023 * Since: 1.3
1025 * Returns: (array zero-terminated=1): Array of AtkTextRange. The last
1026 * element of the array returned by this function will be NULL.
1027 * Virtual: get_bounded_ranges
1029 AtkTextRange**
1030 atk_text_get_bounded_ranges (AtkText *text,
1031 AtkTextRectangle *rect,
1032 AtkCoordType coord_type,
1033 AtkTextClipType x_clip_type,
1034 AtkTextClipType y_clip_type)
1036 AtkTextIface *iface;
1038 g_return_val_if_fail (ATK_IS_TEXT (text), NULL);
1039 g_return_val_if_fail (rect, NULL);
1041 iface = ATK_TEXT_GET_IFACE (text);
1043 if (iface->get_bounded_ranges)
1044 return (*(iface->get_bounded_ranges)) (text, rect, coord_type, x_clip_type, y_clip_type);
1045 else
1046 return NULL;
1050 * atk_attribute_set_free:
1051 * @attrib_set: The #AtkAttributeSet to free
1053 * Frees the memory used by an #AtkAttributeSet, including all its
1054 * #AtkAttributes.
1056 void
1057 atk_attribute_set_free (AtkAttributeSet *attrib_set)
1059 GSList *temp;
1061 temp = attrib_set;
1063 while (temp != NULL)
1065 AtkAttribute *att;
1067 att = temp->data;
1069 g_free (att->name);
1070 g_free (att->value);
1071 g_free (att);
1072 temp = temp->next;
1074 g_slist_free (attrib_set);
1078 * atk_text_attribute_register:
1079 * @name: a name string
1081 * Associate @name with a new #AtkTextAttribute
1083 * Returns: an #AtkTextAttribute associated with @name
1085 AtkTextAttribute
1086 atk_text_attribute_register (const gchar *name)
1088 g_return_val_if_fail (name, ATK_TEXT_ATTR_INVALID);
1090 if (!extra_attributes)
1091 extra_attributes = g_ptr_array_new ();
1093 g_ptr_array_add (extra_attributes, g_strdup (name));
1094 return extra_attributes->len + ATK_TEXT_ATTR_LAST_DEFINED;
1098 * atk_text_attribute_get_name:
1099 * @attr: The #AtkTextAttribute whose name is required
1101 * Gets the name corresponding to the #AtkTextAttribute
1103 * Returns: a string containing the name; this string should not be freed
1105 const gchar*
1106 atk_text_attribute_get_name (AtkTextAttribute attr)
1108 GTypeClass *type_class;
1109 GEnumValue *value;
1110 const gchar *name = NULL;
1112 type_class = g_type_class_ref (ATK_TYPE_TEXT_ATTRIBUTE);
1113 g_return_val_if_fail (G_IS_ENUM_CLASS (type_class), NULL);
1115 value = g_enum_get_value (G_ENUM_CLASS (type_class), attr);
1117 if (value)
1119 name = value->value_nick;
1121 else
1123 if (extra_attributes)
1125 gint n = attr;
1127 n -= ATK_TEXT_ATTR_LAST_DEFINED + 1;
1129 if (n < extra_attributes->len)
1131 name = g_ptr_array_index (extra_attributes, n);
1134 g_type_class_unref (type_class);
1135 return name;
1139 * atk_text_attribute_for_name:
1140 * @name: a string which is the (non-localized) name of an ATK text attribute.
1142 * Get the #AtkTextAttribute type corresponding to a text attribute name.
1144 * Returns: the #AtkTextAttribute enumerated type corresponding to the specified
1145 name,
1146 * or #ATK_TEXT_ATTRIBUTE_INVALID if no matching text attribute is found.
1148 AtkTextAttribute
1149 atk_text_attribute_for_name (const gchar *name)
1151 GTypeClass *type_class;
1152 GEnumValue *value;
1153 AtkTextAttribute type = ATK_TEXT_ATTR_INVALID;
1155 g_return_val_if_fail (name, ATK_TEXT_ATTR_INVALID);
1157 type_class = g_type_class_ref (ATK_TYPE_TEXT_ATTRIBUTE);
1158 g_return_val_if_fail (G_IS_ENUM_CLASS (type_class), ATK_TEXT_ATTR_INVALID);
1160 value = g_enum_get_value_by_nick (G_ENUM_CLASS (type_class), name);
1162 if (value)
1164 type = value->value;
1166 else
1168 gint i;
1170 if (extra_attributes)
1172 for (i = 0; i < extra_attributes->len; i++)
1174 gchar *extra_attribute = (gchar *)g_ptr_array_index (extra_attributes, i);
1176 g_return_val_if_fail (extra_attribute, ATK_TEXT_ATTR_INVALID);
1178 if (strcmp (name, extra_attribute) == 0)
1180 type = i + 1 + ATK_TEXT_ATTR_LAST_DEFINED;
1181 break;
1186 g_type_class_unref (type_class);
1188 return type;
1192 * atk_text_attribute_get_value:
1193 * @attr: The #AtkTextAttribute for which a value is required
1194 * @index_: The index of the required value
1196 * Gets the value for the index of the #AtkTextAttribute
1198 * Returns: a string containing the value; this string should not be freed;
1199 * NULL is returned if there are no values maintained for the attr value.
1201 const gchar*
1202 atk_text_attribute_get_value (AtkTextAttribute attr,
1203 gint index)
1205 switch (attr)
1207 case ATK_TEXT_ATTR_INVISIBLE:
1208 case ATK_TEXT_ATTR_EDITABLE:
1209 case ATK_TEXT_ATTR_BG_FULL_HEIGHT:
1210 case ATK_TEXT_ATTR_STRIKETHROUGH:
1211 case ATK_TEXT_ATTR_BG_STIPPLE:
1212 case ATK_TEXT_ATTR_FG_STIPPLE:
1213 g_assert (index >= 0 && index < G_N_ELEMENTS (boolean_offsets));
1214 return boolean + boolean_offsets[index];
1215 case ATK_TEXT_ATTR_UNDERLINE:
1216 g_assert (index >= 0 && index < G_N_ELEMENTS (underline_offsets));
1217 return underline + underline_offsets[index];
1218 case ATK_TEXT_ATTR_WRAP_MODE:
1219 g_assert (index >= 0 && index < G_N_ELEMENTS (wrap_mode_offsets));
1220 return wrap_mode + wrap_mode_offsets[index];
1221 case ATK_TEXT_ATTR_DIRECTION:
1222 g_assert (index >= 0 && index < G_N_ELEMENTS (direction_offsets));
1223 return direction + direction_offsets[index];
1224 case ATK_TEXT_ATTR_JUSTIFICATION:
1225 g_assert (index >= 0 && index < G_N_ELEMENTS (justification_offsets));
1226 return justification + justification_offsets[index];
1227 case ATK_TEXT_ATTR_STRETCH:
1228 g_assert (index >= 0 && index < G_N_ELEMENTS (stretch_offsets));
1229 return stretch + stretch_offsets[index];
1230 case ATK_TEXT_ATTR_VARIANT:
1231 g_assert (index >= 0 && index < G_N_ELEMENTS (variant_offsets));
1232 return variant + variant_offsets[index];
1233 case ATK_TEXT_ATTR_STYLE:
1234 g_assert (index >= 0 && index < G_N_ELEMENTS (style_offsets));
1235 return style + style_offsets[index];
1236 default:
1237 return NULL;
1241 static void
1242 atk_text_rectangle_union (AtkTextRectangle *src1,
1243 AtkTextRectangle *src2,
1244 AtkTextRectangle *dest)
1246 gint dest_x, dest_y;
1248 dest_x = MIN (src1->x, src2->x);
1249 dest_y = MIN (src1->y, src2->y);
1250 dest->width = MAX (src1->x + src1->width, src2->x + src2->width) - dest_x;
1251 dest->height = MAX (src1->y + src1->height, src2->y + src2->height) - dest_y;
1252 dest->x = dest_x;
1253 dest->y = dest_y;
1256 static gboolean
1257 atk_text_rectangle_contain (AtkTextRectangle *clip,
1258 AtkTextRectangle *bounds,
1259 AtkTextClipType x_clip_type,
1260 AtkTextClipType y_clip_type)
1262 gboolean x_min_ok, x_max_ok, y_min_ok, y_max_ok;
1264 x_min_ok = (bounds->x >= clip->x) ||
1265 ((bounds->x + bounds->width >= clip->x) &&
1266 ((x_clip_type == ATK_TEXT_CLIP_NONE) ||
1267 (x_clip_type == ATK_TEXT_CLIP_MAX)));
1269 x_max_ok = (bounds->x + bounds->width <= clip->x + clip->width) ||
1270 ((bounds->x <= clip->x + clip->width) &&
1271 ((x_clip_type == ATK_TEXT_CLIP_NONE) ||
1272 (x_clip_type == ATK_TEXT_CLIP_MIN)));
1274 y_min_ok = (bounds->y >= clip->y) ||
1275 ((bounds->y + bounds->height >= clip->y) &&
1276 ((y_clip_type == ATK_TEXT_CLIP_NONE) ||
1277 (y_clip_type == ATK_TEXT_CLIP_MAX)));
1279 y_max_ok = (bounds->y + bounds->height <= clip->y + clip->height) ||
1280 ((bounds->y <= clip->y + clip->height) &&
1281 ((y_clip_type == ATK_TEXT_CLIP_NONE) ||
1282 (y_clip_type == ATK_TEXT_CLIP_MIN)));
1284 return (x_min_ok && x_max_ok && y_min_ok && y_max_ok);
1288 static void
1289 atk_text_real_get_range_extents (AtkText *text,
1290 gint start_offset,
1291 gint end_offset,
1292 AtkCoordType coord_type,
1293 AtkTextRectangle *rect)
1295 gint i;
1296 AtkTextRectangle cbounds, bounds;
1298 atk_text_get_character_extents (text, start_offset,
1299 &bounds.x, &bounds.y,
1300 &bounds.width, &bounds.height,
1301 coord_type);
1303 for (i = start_offset + 1; i < end_offset; i++)
1305 atk_text_get_character_extents (text, i,
1306 &cbounds.x, &cbounds.y,
1307 &cbounds.width, &cbounds.height,
1308 coord_type);
1309 atk_text_rectangle_union (&bounds, &cbounds, &bounds);
1312 rect->x = bounds.x;
1313 rect->y = bounds.y;
1314 rect->width = bounds.width;
1315 rect->height = bounds.height;
1318 static AtkTextRange**
1319 atk_text_real_get_bounded_ranges (AtkText *text,
1320 AtkTextRectangle *rect,
1321 AtkCoordType coord_type,
1322 AtkTextClipType x_clip_type,
1323 AtkTextClipType y_clip_type)
1325 gint bounds_min_offset, bounds_max_offset;
1326 gint min_line_start, min_line_end;
1327 gint max_line_start, max_line_end;
1328 gchar *line;
1329 gint curr_offset;
1330 gint offset;
1331 gint num_ranges = 0;
1332 gint range_size = 1;
1333 AtkTextRectangle cbounds;
1334 AtkTextRange **range;
1336 range = NULL;
1337 bounds_min_offset = atk_text_get_offset_at_point (text, rect->x, rect->y, coord_type);
1338 bounds_max_offset = atk_text_get_offset_at_point (text, rect->x + rect->width, rect->y + rect->height, coord_type);
1340 if (bounds_min_offset == 0 &&
1341 bounds_min_offset == bounds_max_offset)
1342 return NULL;
1344 line = atk_text_get_text_at_offset (text, bounds_min_offset,
1345 ATK_TEXT_BOUNDARY_LINE_START,
1346 &min_line_start, &min_line_end);
1347 g_free (line);
1348 line = atk_text_get_text_at_offset (text, bounds_max_offset,
1349 ATK_TEXT_BOUNDARY_LINE_START,
1350 &max_line_start, &max_line_end);
1351 g_free (line);
1352 bounds_min_offset = MIN (min_line_start, max_line_start);
1353 bounds_max_offset = MAX (min_line_end, max_line_end);
1355 curr_offset = bounds_min_offset;
1356 while (curr_offset < bounds_max_offset)
1358 offset = curr_offset;
1360 while (curr_offset < bounds_max_offset)
1362 atk_text_get_character_extents (text, curr_offset,
1363 &cbounds.x, &cbounds.y,
1364 &cbounds.width, &cbounds.height,
1365 coord_type);
1366 if (!atk_text_rectangle_contain (rect, &cbounds, x_clip_type, y_clip_type))
1367 break;
1368 curr_offset++;
1370 if (curr_offset > offset)
1372 AtkTextRange *one_range = g_new (AtkTextRange, 1);
1374 one_range->start_offset = offset;
1375 one_range->end_offset = curr_offset;
1376 one_range->content = atk_text_get_text (text, offset, curr_offset);
1377 atk_text_get_range_extents (text, offset, curr_offset, coord_type, &one_range->bounds);
1379 if (num_ranges >= range_size - 1)
1381 range_size *= 2;
1382 range = g_realloc (range, range_size * sizeof (gpointer));
1384 range[num_ranges] = one_range;
1385 num_ranges++;
1387 curr_offset++;
1388 if (range)
1389 range[num_ranges] = NULL;
1391 return range;
1395 * atk_text_free_ranges:
1396 * @ranges: (array): A pointer to an array of #AtkTextRange which is
1397 * to be freed.
1399 * Frees the memory associated with an array of AtkTextRange. It is assumed
1400 * that the array was returned by the function atk_text_get_bounded_ranges
1401 * and is NULL terminated.
1403 * Since: 1.3
1405 void
1406 atk_text_free_ranges (AtkTextRange **ranges)
1408 AtkTextRange **first = ranges;
1410 if (ranges)
1412 while (*ranges)
1414 AtkTextRange *range;
1416 range = *ranges;
1417 ranges++;
1418 g_free (range->content);
1419 g_free (range);
1421 g_free (first);
1425 static AtkTextRange *
1426 atk_text_range_copy (AtkTextRange *src)
1428 AtkTextRange *dst = g_new0 (AtkTextRange, 1);
1429 dst->bounds = src->bounds;
1430 dst->start_offset = src->start_offset;
1431 dst->end_offset = src->end_offset;
1432 if (src->content)
1433 dst->content = g_strdup (src->content);
1434 return dst;
1437 static void
1438 atk_text_range_free (AtkTextRange *range)
1440 g_free (range->content);
1441 g_free (range);
1444 G_DEFINE_BOXED_TYPE (AtkTextRange, atk_text_range, atk_text_range_copy,
1445 atk_text_range_free)