Fix introspection for atk_text_get_bounded_ranges
[atk.git] / atk / atktext.c
blob76b36d95d92523ae65c816c99f71b427cd67f96e
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 static GPtrArray *extra_attributes = NULL;
28 enum {
29 TEXT_CHANGED,
30 TEXT_CARET_MOVED,
31 TEXT_SELECTION_CHANGED,
32 TEXT_ATTRIBUTES_CHANGED,
33 LAST_SIGNAL
36 static const char boolean[] =
37 "false\0"
38 "true";
39 static const guint8 boolean_offsets[] = {
40 0, 6
43 static const char style[] =
44 "normal\0"
45 "oblique\0"
46 "italic";
47 static const guint8 style_offsets[] = {
48 0, 7, 15
51 static const char variant[] =
52 "normal\0"
53 "small_caps";
54 static const guint8 variant_offsets[] = {
55 0, 7
58 static const char stretch[] =
59 "ultra_condensed\0"
60 "extra_condensed\0"
61 "condensed\0"
62 "semi_condensed\0"
63 "normal\0"
64 "semi_expanded\0"
65 "expanded\0"
66 "extra_expanded\0"
67 "ultra_expanded";
68 static const guint8 stretch_offsets[] = {
69 0, 16, 32, 42, 57, 64, 78, 87, 102
72 static const char justification[] =
73 "left\0"
74 "right\0"
75 "center\0"
76 "fill";
77 static const guint8 justification_offsets[] = {
78 0, 5, 11, 18
81 static const char direction[] =
82 "none\0"
83 "ltr\0"
84 "rtl";
85 static const guint8 direction_offsets[] = {
86 0, 5, 9
89 static const char wrap_mode[] =
90 "none\0"
91 "char\0"
92 "word\0"
93 "word_char";
94 static const guint8 wrap_mode_offsets[] = {
95 0, 5, 10, 15
98 static const char underline[] =
99 "none\0"
100 "single\0"
101 "double\0"
102 "low\0"
103 "error";
104 static const guint8 underline_offsets[] = {
105 0, 5, 12, 19, 23
108 static void atk_text_base_init (AtkTextIface *class);
110 static void atk_text_real_get_range_extents (AtkText *text,
111 gint start_offset,
112 gint end_offset,
113 AtkCoordType coord_type,
114 AtkTextRectangle *rect);
116 static AtkTextRange** atk_text_real_get_bounded_ranges (AtkText *text,
117 AtkTextRectangle *rect,
118 AtkCoordType coord_type,
119 AtkTextClipType x_clip_type,
120 AtkTextClipType y_clip_type);
122 static guint atk_text_signals[LAST_SIGNAL] = { 0 };
124 GType
125 atk_text_get_type (void)
127 static GType type = 0;
129 if (!type)
131 static const GTypeInfo tinfo =
133 sizeof (AtkTextIface),
134 (GBaseInitFunc) atk_text_base_init,
135 (GBaseFinalizeFunc) NULL,
136 (GClassInitFunc) NULL /* atk_text_interface_init */ ,
137 (GClassFinalizeFunc) NULL,
141 type = g_type_register_static (G_TYPE_INTERFACE, "AtkText", &tinfo, 0);
144 return type;
147 static void
148 atk_text_base_init (AtkTextIface *class)
150 static gboolean initialized = FALSE;
152 if (! initialized)
155 * Note that text_changed signal supports details "insert", "delete",
156 * possibly "replace".
159 class->get_range_extents = atk_text_real_get_range_extents;
160 class->get_bounded_ranges = atk_text_real_get_bounded_ranges;
162 atk_text_signals[TEXT_CHANGED] =
163 g_signal_new ("text_changed",
164 ATK_TYPE_TEXT,
165 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
166 G_STRUCT_OFFSET (AtkTextIface, text_changed),
167 (GSignalAccumulator) NULL, NULL,
168 atk_marshal_VOID__INT_INT,
169 G_TYPE_NONE,
170 2, G_TYPE_INT, G_TYPE_INT);
172 atk_text_signals[TEXT_CARET_MOVED] =
173 g_signal_new ("text_caret_moved",
174 ATK_TYPE_TEXT,
175 G_SIGNAL_RUN_LAST,
176 G_STRUCT_OFFSET (AtkTextIface, text_caret_moved),
177 (GSignalAccumulator) NULL, NULL,
178 g_cclosure_marshal_VOID__INT,
179 G_TYPE_NONE,
180 1, G_TYPE_INT);
181 atk_text_signals[TEXT_SELECTION_CHANGED] =
182 g_signal_new ("text_selection_changed",
183 ATK_TYPE_TEXT,
184 G_SIGNAL_RUN_LAST,
185 G_STRUCT_OFFSET (AtkTextIface, text_selection_changed),
186 (GSignalAccumulator) NULL, NULL,
187 g_cclosure_marshal_VOID__VOID,
188 G_TYPE_NONE, 0);
189 atk_text_signals[TEXT_ATTRIBUTES_CHANGED] =
190 g_signal_new ("text_attributes_changed",
191 ATK_TYPE_TEXT,
192 G_SIGNAL_RUN_LAST,
193 G_STRUCT_OFFSET (AtkTextIface, text_attributes_changed),
194 (GSignalAccumulator) NULL, NULL,
195 g_cclosure_marshal_VOID__VOID,
196 G_TYPE_NONE, 0);
199 initialized = TRUE;
204 * atk_text_get_text:
205 * @text: an #AtkText
206 * @start_offset: start position
207 * @end_offset: end position
209 * Gets the specified text.
211 * Returns: the text from @start_offset up to, but not including @end_offset.
213 gchar*
214 atk_text_get_text (AtkText *text,
215 gint start_offset,
216 gint end_offset)
218 AtkTextIface *iface;
220 g_return_val_if_fail (ATK_IS_TEXT (text), NULL);
222 iface = ATK_TEXT_GET_IFACE (text);
224 if (start_offset < 0 || end_offset < -1)
225 return NULL;
227 if (iface->get_text)
228 return (*(iface->get_text)) (text, start_offset, end_offset);
229 else
230 return NULL;
234 * atk_text_get_character_at_offset:
235 * @text: an #AtkText
236 * @offset: position
238 * Gets the specified text.
240 * Returns: the character at @offset.
242 gunichar
243 atk_text_get_character_at_offset (AtkText *text,
244 gint offset)
246 AtkTextIface *iface;
248 g_return_val_if_fail (ATK_IS_TEXT (text), (gunichar) 0);
250 iface = ATK_TEXT_GET_IFACE (text);
252 if (iface->get_character_at_offset)
253 return (*(iface->get_character_at_offset)) (text, offset);
254 else
255 return (gunichar) 0;
259 * atk_text_get_text_after_offset:
260 * @text: an #AtkText
261 * @offset: position
262 * @boundary_type: An #AtkTextBoundary
263 * @start_offset: the start offset of the returned string
264 * @end_offset: the offset of the first character after the
265 * returned substring
267 * Gets the specified text.
269 * If the boundary_type if ATK_TEXT_BOUNDARY_CHAR the character after the
270 * offset is returned.
272 * If the boundary_type is ATK_TEXT_BOUNDARY_WORD_START the returned string
273 * is from the word start after the offset to the next word start.
275 * The returned string will contain the word after the offset if the offset
276 * is inside a word or if the offset is not inside a word.
278 * If the boundary_type is ATK_TEXT_BOUNDARY_WORD_END the returned string
279 * is from the word end at or after the offset to the next work end.
281 * The returned string will contain the word after the offset if the offset
282 * is inside a word and will contain the word after the word after the offset
283 * if the offset is not inside a word.
285 * If the boundary type is ATK_TEXT_BOUNDARY_SENTENCE_START the returned
286 * string is from the sentence start after the offset to the next sentence
287 * start.
289 * The returned string will contain the sentence after the offset if the offset
290 * is inside a sentence or if the offset is not inside a sentence.
292 * If the boundary_type is ATK_TEXT_BOUNDARY_SENTENCE_END the returned string
293 * is from the sentence end at or after the offset to the next sentence end.
295 * The returned string will contain the sentence after the offset if the offset
296 * is inside a sentence and will contain the sentence after the sentence
297 * after the offset if the offset is not inside a sentence.
299 * If the boundary type is ATK_TEXT_BOUNDARY_LINE_START the returned
300 * string is from the line start after the offset to the next line start.
302 * If the boundary_type is ATK_TEXT_BOUNDARY_LINE_END the returned string
303 * is from the line end at or after the offset to the next line end.
305 * Returns: the text after @offset bounded by the specified @boundary_type.
307 gchar*
308 atk_text_get_text_after_offset (AtkText *text,
309 gint offset,
310 AtkTextBoundary boundary_type,
311 gint *start_offset,
312 gint *end_offset)
314 AtkTextIface *iface;
315 gint local_start_offset, local_end_offset;
316 gint *real_start_offset, *real_end_offset;
318 g_return_val_if_fail (ATK_IS_TEXT (text), NULL);
320 if (start_offset)
321 real_start_offset = start_offset;
322 else
323 real_start_offset = &local_start_offset;
324 if (end_offset)
325 real_end_offset = end_offset;
326 else
327 real_end_offset = &local_end_offset;
329 if (offset < 0)
330 return NULL;
332 iface = ATK_TEXT_GET_IFACE (text);
334 if (iface->get_text_after_offset)
335 return (*(iface->get_text_after_offset)) (text, offset, boundary_type, real_start_offset, real_end_offset);
336 else
337 return NULL;
341 * atk_text_get_text_at_offset:
342 * @text: an #AtkText
343 * @offset: position
344 * @boundary_type: An #AtkTextBoundary
345 * @start_offset: the start offset of the returned string
346 * @end_offset: the offset of the first character after the
347 * returned substring
349 * Gets the specified text.
351 * If the boundary_type if ATK_TEXT_BOUNDARY_CHAR the character at the
352 * offset is returned.
354 * If the boundary_type is ATK_TEXT_BOUNDARY_WORD_START the returned string
355 * is from the word start at or before the offset to the word start after
356 * the offset.
358 * The returned string will contain the word at the offset if the offset
359 * is inside a word and will contain the word before the offset if the
360 * offset is not inside a word.
362 * If the boundary_type is ATK_TEXT_BOUNDARY_WORD_END the returned string
363 * is from the word end before the offset to the word end at or after the
364 * offset.
366 * The returned string will contain the word at the offset if the offset
367 * is inside a word and will contain the word after to the offset if the
368 * offset is not inside a word.
370 * If the boundary type is ATK_TEXT_BOUNDARY_SENTENCE_START the returned
371 * string is from the sentence start at or before the offset to the sentence
372 * start after the offset.
374 * The returned string will contain the sentence at the offset if the offset
375 * is inside a sentence and will contain the sentence before the offset
376 * if the offset is not inside a sentence.
378 * If the boundary_type is ATK_TEXT_BOUNDARY_SENTENCE_END the returned string
379 * is from the sentence end before the offset to the sentence end at or
380 * after the offset.
382 * The returned string will contain the sentence at the offset if the offset
383 * is inside a sentence and will contain the sentence after the offset
384 * if the offset is not inside a sentence.
386 * If the boundary type is ATK_TEXT_BOUNDARY_LINE_START the returned
387 * string is from the line start at or before the offset to the line
388 * start after the offset.
390 * If the boundary_type is ATK_TEXT_BOUNDARY_LINE_END the returned string
391 * is from the line end before the offset to the line end at or after
392 * the offset.
394 * Returns: the text at @offset bounded by the specified @boundary_type.
396 gchar*
397 atk_text_get_text_at_offset (AtkText *text,
398 gint offset,
399 AtkTextBoundary boundary_type,
400 gint *start_offset,
401 gint *end_offset)
403 AtkTextIface *iface;
404 gint local_start_offset, local_end_offset;
405 gint *real_start_offset, *real_end_offset;
407 g_return_val_if_fail (ATK_IS_TEXT (text), NULL);
409 if (start_offset)
410 real_start_offset = start_offset;
411 else
412 real_start_offset = &local_start_offset;
413 if (end_offset)
414 real_end_offset = end_offset;
415 else
416 real_end_offset = &local_end_offset;
418 iface = ATK_TEXT_GET_IFACE (text);
420 if (iface->get_text_at_offset)
421 return (*(iface->get_text_at_offset)) (text, offset, boundary_type, real_start_offset, real_end_offset);
422 else
423 return NULL;
427 * atk_text_get_text_before_offset:
428 * @text: an #AtkText
429 * @offset: position
430 * @boundary_type: An #AtkTextBoundary
431 * @start_offset: the start offset of the returned string
432 * @end_offset: the offset of the first character after the
433 * returned substring
435 * Gets the specified text.
437 * If the boundary_type if ATK_TEXT_BOUNDARY_CHAR the character before the
438 * offset is returned.
440 * If the boundary_type is ATK_TEXT_BOUNDARY_WORD_START the returned string
441 * is from the word start before the word start before the offset to
442 * the word start before the offset.
444 * The returned string will contain the word before the offset if the offset
445 * is inside a word and will contain the word before the word before the
446 * offset if the offset is not inside a word.
448 * If the boundary_type is ATK_TEXT_BOUNDARY_WORD_END the returned string
449 * is from the word end before the word end at or before the offset to the
450 * word end at or before the offset.
452 * The returned string will contain the word before the offset if the offset
453 * is inside a word or if the offset is not inside a word.
455 * If the boundary type is ATK_TEXT_BOUNDARY_SENTENCE_START the returned
456 * string is from the sentence start before the sentence start before
457 * the offset to the sentence start before the offset.
459 * The returned string will contain the sentence before the offset if the
460 * offset is inside a sentence and will contain the sentence before the
461 * sentence before the offset if the offset is not inside a sentence.
463 * If the boundary_type is ATK_TEXT_BOUNDARY_SENTENCE_END the returned string
464 * is from the sentence end before the sentence end at or before the offset to
465 * the sentence end at or before the offset.
467 * The returned string will contain the sentence before the offset if the
468 * offset is inside a sentence or if the offset is not inside a sentence.
470 * If the boundary type is ATK_TEXT_BOUNDARY_LINE_START the returned
471 * string is from the line start before the line start ar or before the offset
472 * to the line start ar or before the offset.
474 * If the boundary_type is ATK_TEXT_BOUNDARY_LINE_END the returned string
475 * is from the line end before the line end before the offset to the
476 * line end before the offset.
478 * Returns: the text before @offset bounded by the specified @boundary_type.
480 gchar*
481 atk_text_get_text_before_offset (AtkText *text,
482 gint offset,
483 AtkTextBoundary boundary_type,
484 gint *start_offset,
485 gint *end_offset)
487 AtkTextIface *iface;
488 gint local_start_offset, local_end_offset;
489 gint *real_start_offset, *real_end_offset;
491 g_return_val_if_fail (ATK_IS_TEXT (text), NULL);
493 if (start_offset)
494 real_start_offset = start_offset;
495 else
496 real_start_offset = &local_start_offset;
497 if (end_offset)
498 real_end_offset = end_offset;
499 else
500 real_end_offset = &local_end_offset;
502 if (offset < 0)
503 return NULL;
505 iface = ATK_TEXT_GET_IFACE (text);
507 if (iface->get_text_before_offset)
508 return (*(iface->get_text_before_offset)) (text, offset, boundary_type, real_start_offset, real_end_offset);
509 else
510 return NULL;
514 * atk_text_get_caret_offset:
515 * @text: an #AtkText
517 * Gets the offset position of the caret (cursor).
519 * Returns: the offset position of the caret (cursor).
521 gint
522 atk_text_get_caret_offset (AtkText *text)
524 AtkTextIface *iface;
526 g_return_val_if_fail (ATK_IS_TEXT (text), 0);
528 iface = ATK_TEXT_GET_IFACE (text);
530 if (iface->get_caret_offset)
531 return (*(iface->get_caret_offset)) (text);
532 else
533 return 0;
537 * atk_text_get_character_extents:
538 * @text: an #AtkText
539 * @offset: The offset of the text character for which bounding information is required.
540 * @x: Pointer for the x cordinate of the bounding box
541 * @y: Pointer for the y cordinate of the bounding box
542 * @width: Pointer for the width of the bounding box
543 * @height: Pointer for the height of the bounding box
544 * @coords: specify whether coordinates are relative to the screen or widget window
546 * Get the bounding box containing the glyph representing the character at
547 * a particular text offset.
549 void
550 atk_text_get_character_extents (AtkText *text,
551 gint offset,
552 gint *x,
553 gint *y,
554 gint *width,
555 gint *height,
556 AtkCoordType coords)
558 AtkTextIface *iface;
559 gint local_x, local_y, local_width, local_height;
560 gint *real_x, *real_y, *real_width, *real_height;
562 g_return_if_fail (ATK_IS_TEXT (text));
564 if (x)
565 real_x = x;
566 else
567 real_x = &local_x;
568 if (y)
569 real_y = y;
570 else
571 real_y = &local_y;
572 if (width)
573 real_width = width;
574 else
575 real_width = &local_width;
576 if (height)
577 real_height = height;
578 else
579 real_height = &local_height;
581 *real_x = 0;
582 *real_y = 0;
583 *real_width = 0;
584 *real_height = 0;
586 if (offset < 0)
587 return;
589 iface = ATK_TEXT_GET_IFACE (text);
591 if (iface->get_character_extents)
592 (*(iface->get_character_extents)) (text, offset, real_x, real_y, real_width, real_height, coords);
594 if (*real_width <0)
596 *real_x = *real_x + *real_width;
597 *real_width *= -1;
602 * atk_text_get_run_attributes:
603 *@text: an #AtkText
604 *@offset: the offset at which to get the attributes, -1 means the offset of
605 *the character to be inserted at the caret location.
606 *@start_offset: the address to put the start offset of the range
607 *@end_offset: the address to put the end offset of the range
609 *Creates an #AtkAttributeSet which consists of the attributes explicitly
610 *set at the position @offset in the text. @start_offset and @end_offset are
611 *set to the start and end of the range around @offset where the attributes are
612 *invariant. Note that @end_offset is the offset of the first character
613 *after the range. See the enum AtkTextAttribute for types of text
614 *attributes that can be returned. Note that other attributes may also be
615 *returned.
617 *Returns: (transfer full): an #AtkAttributeSet which contains the attributes
618 * explicitly set at @offset. This #AtkAttributeSet should be freed by a call
619 * to atk_attribute_set_free().
621 AtkAttributeSet*
622 atk_text_get_run_attributes (AtkText *text,
623 gint offset,
624 gint *start_offset,
625 gint *end_offset)
627 AtkTextIface *iface;
628 gint local_start_offset, local_end_offset;
629 gint *real_start_offset, *real_end_offset;
631 g_return_val_if_fail (ATK_IS_TEXT (text), NULL);
633 if (start_offset)
634 real_start_offset = start_offset;
635 else
636 real_start_offset = &local_start_offset;
637 if (end_offset)
638 real_end_offset = end_offset;
639 else
640 real_end_offset = &local_end_offset;
642 if (offset < -1)
643 return NULL;
645 iface = ATK_TEXT_GET_IFACE (text);
647 if (iface->get_run_attributes)
648 return (*(iface->get_run_attributes)) (text, offset, real_start_offset, real_end_offset);
649 else
650 return NULL;
654 * atk_text_get_default_attributes:
655 *@text: an #AtkText
657 *Creates an #AtkAttributeSet which consists of the default values of
658 *attributes for the text. See the enum AtkTextAttribute for types of text
659 *attributes that can be returned. Note that other attributes may also be
660 *returned.
662 *Returns: (transfer full): an #AtkAttributeSet which contains the default
663 * values of attributes. at @offset. this #atkattributeset should be freed by
664 * a call to atk_attribute_set_free().
666 AtkAttributeSet*
667 atk_text_get_default_attributes (AtkText *text)
669 AtkTextIface *iface;
671 g_return_val_if_fail (ATK_IS_TEXT (text), NULL);
673 iface = ATK_TEXT_GET_IFACE (text);
675 if (iface->get_default_attributes)
676 return (*(iface->get_default_attributes)) (text);
677 else
678 return NULL;
682 * atk_text_get_character_count:
683 * @text: an #AtkText
685 * Gets the character count.
687 * Returns: the number of characters.
689 gint
690 atk_text_get_character_count (AtkText *text)
692 AtkTextIface *iface;
694 g_return_val_if_fail (ATK_IS_TEXT (text), -1);
696 iface = ATK_TEXT_GET_IFACE (text);
698 if (iface->get_character_count)
699 return (*(iface->get_character_count)) (text);
700 else
701 return -1;
705 * atk_text_get_offset_at_point:
706 * @text: an #AtkText
707 * @x: screen x-position of character
708 * @y: screen y-position of character
709 * @coords: specify whether coordinates are relative to the screen or
710 * widget window
712 * Gets the offset of the character located at coordinates @x and @y. @x and @y
713 * are interpreted as being relative to the screen or this widget's window
714 * depending on @coords.
716 * Returns: the offset to the character which is located at
717 * the specified @x and @y coordinates.
719 gint
720 atk_text_get_offset_at_point (AtkText *text,
721 gint x,
722 gint y,
723 AtkCoordType coords)
725 AtkTextIface *iface;
727 g_return_val_if_fail (ATK_IS_TEXT (text), -1);
729 iface = ATK_TEXT_GET_IFACE (text);
731 if (iface->get_offset_at_point)
732 return (*(iface->get_offset_at_point)) (text, x, y, coords);
733 else
734 return -1;
738 * atk_text_get_n_selections:
739 * @text: an #AtkText
741 * Gets the number of selected regions.
743 * Returns: The number of selected regions, or -1 if a failure
744 * occurred.
746 gint
747 atk_text_get_n_selections (AtkText *text)
749 AtkTextIface *iface;
751 g_return_val_if_fail (ATK_IS_TEXT (text), -1);
753 iface = ATK_TEXT_GET_IFACE (text);
755 if (iface->get_n_selections)
756 return (*(iface->get_n_selections)) (text);
757 else
758 return -1;
762 * atk_text_get_selection:
763 * @text: an #AtkText
764 * @selection_num: The selection number. The selected regions are
765 * assigned numbers that correspond to how far the region is from the
766 * start of the text. The selected region closest to the beginning
767 * of the text region is assigned the number 0, etc. Note that adding,
768 * moving or deleting a selected region can change the numbering.
769 * @start_offset: passes back the start position of the selected region
770 * @end_offset: passes back the end position of (e.g. offset immediately past)
771 * the selected region
773 * Gets the text from the specified selection.
775 * Returns: the selected text.
777 gchar*
778 atk_text_get_selection (AtkText *text,
779 gint selection_num,
780 gint *start_offset,
781 gint *end_offset)
783 AtkTextIface *iface;
784 gint local_start_offset, local_end_offset;
785 gint *real_start_offset, *real_end_offset;
787 g_return_val_if_fail (ATK_IS_TEXT (text), NULL);
789 if (start_offset)
790 real_start_offset = start_offset;
791 else
792 real_start_offset = &local_start_offset;
793 if (end_offset)
794 real_end_offset = end_offset;
795 else
796 real_end_offset = &local_end_offset;
798 iface = ATK_TEXT_GET_IFACE (text);
800 if (iface->get_selection)
802 return (*(iface->get_selection)) (text, selection_num,
803 real_start_offset, real_end_offset);
805 else
806 return NULL;
810 * atk_text_add_selection:
811 * @text: an #AtkText
812 * @start_offset: the start position of the selected region
813 * @end_offset: the offset of the first character after the selected region.
815 * Adds a selection bounded by the specified offsets.
817 * Returns: %TRUE if success, %FALSE otherwise
819 gboolean
820 atk_text_add_selection (AtkText *text,
821 gint start_offset,
822 gint end_offset)
824 AtkTextIface *iface;
826 g_return_val_if_fail (ATK_IS_TEXT (text), FALSE);
828 iface = ATK_TEXT_GET_IFACE (text);
830 if (iface->add_selection)
831 return (*(iface->add_selection)) (text, start_offset, end_offset);
832 else
833 return FALSE;
837 * atk_text_remove_selection:
838 * @text: an #AtkText
839 * @selection_num: The selection number. The selected regions are
840 * assigned numbers that correspond to how far the region is from the
841 * start of the text. The selected region closest to the beginning
842 * of the text region is assigned the number 0, etc. Note that adding,
843 * moving or deleting a selected region can change the numbering.
845 * Removes the specified selection.
847 * Returns: %TRUE if success, %FALSE otherwise
849 gboolean
850 atk_text_remove_selection (AtkText *text,
851 gint selection_num)
853 AtkTextIface *iface;
855 g_return_val_if_fail (ATK_IS_TEXT (text), FALSE);
857 iface = ATK_TEXT_GET_IFACE (text);
859 if (iface->remove_selection)
860 return (*(iface->remove_selection)) (text, selection_num);
861 else
862 return FALSE;
866 * atk_text_set_selection:
867 * @text: an #AtkText
868 * @selection_num: The selection number. The selected regions are
869 * assigned numbers that correspond to how far the region is from the
870 * start of the text. The selected region closest to the beginning
871 * of the text region is assigned the number 0, etc. Note that adding,
872 * moving or deleting a selected region can change the numbering.
873 * @start_offset: the new start position of the selection
874 * @end_offset: the new end position of (e.g. offset immediately past)
875 * the selection
877 * Changes the start and end offset of the specified selection.
879 * Returns: %TRUE if success, %FALSE otherwise
881 gboolean
882 atk_text_set_selection (AtkText *text,
883 gint selection_num,
884 gint start_offset,
885 gint end_offset)
887 AtkTextIface *iface;
889 g_return_val_if_fail (ATK_IS_TEXT (text), FALSE);
891 iface = ATK_TEXT_GET_IFACE (text);
893 if (iface->set_selection)
895 return (*(iface->set_selection)) (text, selection_num,
896 start_offset, end_offset);
898 else
899 return FALSE;
903 * atk_text_set_caret_offset:
904 * @text: an #AtkText
905 * @offset: position
907 * Sets the caret (cursor) position to the specified @offset.
909 * Returns: %TRUE if success, %FALSE otherwise.
911 gboolean
912 atk_text_set_caret_offset (AtkText *text,
913 gint offset)
915 AtkTextIface *iface;
917 g_return_val_if_fail (ATK_IS_TEXT (text), FALSE);
919 iface = ATK_TEXT_GET_IFACE (text);
921 if (iface->set_caret_offset)
923 return (*(iface->set_caret_offset)) (text, offset);
925 else
927 return FALSE;
932 * atk_text_get_range_extents:
933 * @text: an #AtkText
934 * @start_offset: The offset of the first text character for which boundary
935 * information is required.
936 * @end_offset: The offset of the text character after the last character
937 * for which boundary information is required.
938 * @coord_type: Specify whether coordinates are relative to the screen or widget window.
939 * @rect: A pointer to a AtkTextRectangle which is filled in by this function.
941 * Get the bounding box for text within the specified range.
943 * Since: 1.3
945 void
946 atk_text_get_range_extents (AtkText *text,
947 gint start_offset,
948 gint end_offset,
949 AtkCoordType coord_type,
950 AtkTextRectangle *rect)
952 AtkTextIface *iface;
954 g_return_if_fail (ATK_IS_TEXT (text));
955 g_return_if_fail (rect);
957 if (start_offset < 0 || start_offset >= end_offset)
958 return;
960 iface = ATK_TEXT_GET_IFACE (text);
962 if (iface->get_range_extents)
963 (*(iface->get_range_extents)) (text, start_offset, end_offset, coord_type, rect);
967 * atk_text_get_bounded_ranges:
968 * @text: an #AtkText
969 * @rect: An AtkTextRectangle giving the dimensions of the bounding box.
970 * @coord_type: Specify whether coordinates are relative to the screen or widget window.
971 * @x_clip_type: Specify the horizontal clip type.
972 * @y_clip_type: Specify the vertical clip type.
974 * Get the ranges of text in the specified bounding box.
976 * Since: 1.3
978 * Returns: (array zero-terminated=1): Array of AtkTextRange. The last
979 * element of the array returned by this function will be NULL.
981 AtkTextRange**
982 atk_text_get_bounded_ranges (AtkText *text,
983 AtkTextRectangle *rect,
984 AtkCoordType coord_type,
985 AtkTextClipType x_clip_type,
986 AtkTextClipType y_clip_type)
988 AtkTextIface *iface;
990 g_return_val_if_fail (ATK_IS_TEXT (text), NULL);
991 g_return_val_if_fail (rect, NULL);
993 iface = ATK_TEXT_GET_IFACE (text);
995 if (iface->get_bounded_ranges)
996 return (*(iface->get_bounded_ranges)) (text, rect, coord_type, x_clip_type, y_clip_type);
997 else
998 return NULL;
1002 * atk_attribute_set_free:
1003 * @attrib_set: The #AtkAttributeSet to free
1005 * Frees the memory used by an #AtkAttributeSet, including all its
1006 * #AtkAttributes.
1008 void
1009 atk_attribute_set_free (AtkAttributeSet *attrib_set)
1011 GSList *temp;
1013 temp = attrib_set;
1015 while (temp != NULL)
1017 AtkAttribute *att;
1019 att = temp->data;
1021 g_free (att->name);
1022 g_free (att->value);
1023 g_free (att);
1024 temp = temp->next;
1026 g_slist_free (attrib_set);
1030 * atk_text_attribute_register:
1031 * @name: a name string
1033 * Associate @name with a new #AtkTextAttribute
1035 * Returns: an #AtkTextAttribute associated with @name
1037 AtkTextAttribute
1038 atk_text_attribute_register (const gchar *name)
1040 g_return_val_if_fail (name, ATK_TEXT_ATTR_INVALID);
1042 if (!extra_attributes)
1043 extra_attributes = g_ptr_array_new ();
1045 g_ptr_array_add (extra_attributes, g_strdup (name));
1046 return extra_attributes->len + ATK_TEXT_ATTR_LAST_DEFINED;
1050 * atk_text_attribute_get_name:
1051 * @attr: The #AtkTextAttribute whose name is required
1053 * Gets the name corresponding to the #AtkTextAttribute
1055 * Returns: a string containing the name; this string should not be freed
1057 G_CONST_RETURN gchar*
1058 atk_text_attribute_get_name (AtkTextAttribute attr)
1060 GTypeClass *type_class;
1061 GEnumValue *value;
1062 const gchar *name = NULL;
1064 type_class = g_type_class_ref (ATK_TYPE_TEXT_ATTRIBUTE);
1065 g_return_val_if_fail (G_IS_ENUM_CLASS (type_class), NULL);
1067 value = g_enum_get_value (G_ENUM_CLASS (type_class), attr);
1069 if (value)
1071 name = value->value_nick;
1073 else
1075 if (extra_attributes)
1077 gint n = attr;
1079 n -= ATK_TEXT_ATTR_LAST_DEFINED + 1;
1081 if (n < extra_attributes->len)
1083 name = g_ptr_array_index (extra_attributes, n);
1086 g_type_class_unref (type_class);
1087 return name;
1091 * atk_text_attribute_for_name:
1092 * @name: a string which is the (non-localized) name of an ATK text attribute.
1094 * Get the #AtkTextAttribute type corresponding to a text attribute name.
1096 * Returns: the #AtkTextAttribute enumerated type corresponding to the specified
1097 name,
1098 * or #ATK_TEXT_ATTRIBUTE_INVALID if no matching text attribute is found.
1100 AtkTextAttribute
1101 atk_text_attribute_for_name (const gchar *name)
1103 GTypeClass *type_class;
1104 GEnumValue *value;
1105 AtkTextAttribute type = ATK_TEXT_ATTR_INVALID;
1107 g_return_val_if_fail (name, ATK_TEXT_ATTR_INVALID);
1109 type_class = g_type_class_ref (ATK_TYPE_TEXT_ATTRIBUTE);
1110 g_return_val_if_fail (G_IS_ENUM_CLASS (type_class), ATK_TEXT_ATTR_INVALID);
1112 value = g_enum_get_value_by_nick (G_ENUM_CLASS (type_class), name);
1114 if (value)
1116 type = value->value;
1118 else
1120 gint i;
1122 if (extra_attributes)
1124 for (i = 0; i < extra_attributes->len; i++)
1126 gchar *extra_attribute = (gchar *)g_ptr_array_index (extra_attributes, i);
1128 g_return_val_if_fail (extra_attribute, ATK_TEXT_ATTR_INVALID);
1130 if (strcmp (name, extra_attribute) == 0)
1132 type = i + 1 + ATK_TEXT_ATTR_LAST_DEFINED;
1133 break;
1138 g_type_class_unref (type_class);
1140 return type;
1144 * atk_text_attribute_get_value:
1145 * @attr: The #AtkTextAttribute for which a value is required
1146 * @index_: The index of the required value
1148 * Gets the value for the index of the #AtkTextAttribute
1150 * Returns: a string containing the value; this string should not be freed;
1151 * NULL is returned if there are no values maintained for the attr value.
1153 G_CONST_RETURN gchar*
1154 atk_text_attribute_get_value (AtkTextAttribute attr,
1155 gint index)
1157 switch (attr)
1159 case ATK_TEXT_ATTR_INVISIBLE:
1160 case ATK_TEXT_ATTR_EDITABLE:
1161 case ATK_TEXT_ATTR_BG_FULL_HEIGHT:
1162 case ATK_TEXT_ATTR_STRIKETHROUGH:
1163 case ATK_TEXT_ATTR_BG_STIPPLE:
1164 case ATK_TEXT_ATTR_FG_STIPPLE:
1165 g_assert (index >= 0 && index < G_N_ELEMENTS (boolean_offsets));
1166 return boolean + boolean_offsets[index];
1167 case ATK_TEXT_ATTR_UNDERLINE:
1168 g_assert (index >= 0 && index < G_N_ELEMENTS (underline_offsets));
1169 return underline + underline_offsets[index];
1170 case ATK_TEXT_ATTR_WRAP_MODE:
1171 g_assert (index >= 0 && index < G_N_ELEMENTS (wrap_mode_offsets));
1172 return wrap_mode + wrap_mode_offsets[index];
1173 case ATK_TEXT_ATTR_DIRECTION:
1174 g_assert (index >= 0 && index < G_N_ELEMENTS (direction_offsets));
1175 return direction + direction_offsets[index];
1176 case ATK_TEXT_ATTR_JUSTIFICATION:
1177 g_assert (index >= 0 && index < G_N_ELEMENTS (justification_offsets));
1178 return justification + justification_offsets[index];
1179 case ATK_TEXT_ATTR_STRETCH:
1180 g_assert (index >= 0 && index < G_N_ELEMENTS (stretch_offsets));
1181 return stretch + stretch_offsets[index];
1182 case ATK_TEXT_ATTR_VARIANT:
1183 g_assert (index >= 0 && index < G_N_ELEMENTS (variant_offsets));
1184 return variant + variant_offsets[index];
1185 case ATK_TEXT_ATTR_STYLE:
1186 g_assert (index >= 0 && index < G_N_ELEMENTS (style_offsets));
1187 return style + style_offsets[index];
1188 default:
1189 return NULL;
1193 static void
1194 atk_text_rectangle_union (AtkTextRectangle *src1,
1195 AtkTextRectangle *src2,
1196 AtkTextRectangle *dest)
1198 gint dest_x, dest_y;
1200 dest_x = MIN (src1->x, src2->x);
1201 dest_y = MIN (src1->y, src2->y);
1202 dest->width = MAX (src1->x + src1->width, src2->x + src2->width) - dest_x;
1203 dest->height = MAX (src1->y + src1->height, src2->y + src2->height) - dest_y;
1204 dest->x = dest_x;
1205 dest->y = dest_y;
1208 static gboolean
1209 atk_text_rectangle_contain (AtkTextRectangle *clip,
1210 AtkTextRectangle *bounds,
1211 AtkTextClipType x_clip_type,
1212 AtkTextClipType y_clip_type)
1214 gboolean x_min_ok, x_max_ok, y_min_ok, y_max_ok;
1216 x_min_ok = (bounds->x >= clip->x) ||
1217 ((bounds->x + bounds->width >= clip->x) &&
1218 ((x_clip_type == ATK_TEXT_CLIP_NONE) ||
1219 (x_clip_type == ATK_TEXT_CLIP_MAX)));
1221 x_max_ok = (bounds->x + bounds->width <= clip->x + clip->width) ||
1222 ((bounds->x <= clip->x + clip->width) &&
1223 ((x_clip_type == ATK_TEXT_CLIP_NONE) ||
1224 (x_clip_type == ATK_TEXT_CLIP_MIN)));
1226 y_min_ok = (bounds->y >= clip->y) ||
1227 ((bounds->y + bounds->height >= clip->y) &&
1228 ((y_clip_type == ATK_TEXT_CLIP_NONE) ||
1229 (y_clip_type == ATK_TEXT_CLIP_MAX)));
1231 y_max_ok = (bounds->y + bounds->height <= clip->y + clip->height) ||
1232 ((bounds->y <= clip->y + clip->height) &&
1233 ((y_clip_type == ATK_TEXT_CLIP_NONE) ||
1234 (y_clip_type == ATK_TEXT_CLIP_MIN)));
1236 return (x_min_ok && x_max_ok && y_min_ok && y_max_ok);
1240 static void
1241 atk_text_real_get_range_extents (AtkText *text,
1242 gint start_offset,
1243 gint end_offset,
1244 AtkCoordType coord_type,
1245 AtkTextRectangle *rect)
1247 gint i;
1248 AtkTextRectangle cbounds, bounds;
1250 atk_text_get_character_extents (text, start_offset,
1251 &bounds.x, &bounds.y,
1252 &bounds.width, &bounds.height,
1253 coord_type);
1255 for (i = start_offset + 1; i < end_offset; i++)
1257 atk_text_get_character_extents (text, i,
1258 &cbounds.x, &cbounds.y,
1259 &cbounds.width, &cbounds.height,
1260 coord_type);
1261 atk_text_rectangle_union (&bounds, &cbounds, &bounds);
1264 rect->x = bounds.x;
1265 rect->y = bounds.y;
1266 rect->width = bounds.width;
1267 rect->height = bounds.height;
1270 static AtkTextRange**
1271 atk_text_real_get_bounded_ranges (AtkText *text,
1272 AtkTextRectangle *rect,
1273 AtkCoordType coord_type,
1274 AtkTextClipType x_clip_type,
1275 AtkTextClipType y_clip_type)
1277 gint bounds_min_offset, bounds_max_offset;
1278 gint min_line_start, min_line_end;
1279 gint max_line_start, max_line_end;
1280 gchar *line;
1281 gint curr_offset;
1282 gint offset;
1283 gint num_ranges = 0;
1284 gint range_size = 1;
1285 AtkTextRectangle cbounds;
1286 AtkTextRange **range;
1288 range = NULL;
1289 bounds_min_offset = atk_text_get_offset_at_point (text, rect->x, rect->y, coord_type);
1290 bounds_max_offset = atk_text_get_offset_at_point (text, rect->x + rect->width, rect->y + rect->height, coord_type);
1292 if (bounds_min_offset == 0 &&
1293 bounds_min_offset == bounds_max_offset)
1294 return NULL;
1296 line = atk_text_get_text_at_offset (text, bounds_min_offset,
1297 ATK_TEXT_BOUNDARY_LINE_START,
1298 &min_line_start, &min_line_end);
1299 g_free (line);
1300 line = atk_text_get_text_at_offset (text, bounds_max_offset,
1301 ATK_TEXT_BOUNDARY_LINE_START,
1302 &max_line_start, &max_line_end);
1303 g_free (line);
1304 bounds_min_offset = MIN (min_line_start, max_line_start);
1305 bounds_max_offset = MAX (min_line_end, max_line_end);
1307 curr_offset = bounds_min_offset;
1308 while (curr_offset < bounds_max_offset)
1310 offset = curr_offset;
1312 while (curr_offset < bounds_max_offset)
1314 atk_text_get_character_extents (text, curr_offset,
1315 &cbounds.x, &cbounds.y,
1316 &cbounds.width, &cbounds.height,
1317 coord_type);
1318 if (!atk_text_rectangle_contain (rect, &cbounds, x_clip_type, y_clip_type))
1319 break;
1320 curr_offset++;
1322 if (curr_offset > offset)
1324 AtkTextRange *one_range = g_new (AtkTextRange, 1);
1326 one_range->start_offset = offset;
1327 one_range->end_offset = curr_offset;
1328 one_range->content = atk_text_get_text (text, offset, curr_offset);
1329 atk_text_get_range_extents (text, offset, curr_offset, coord_type, &one_range->bounds);
1331 if (num_ranges >= range_size - 1)
1333 range_size *= 2;
1334 range = g_realloc (range, range_size * sizeof (gpointer));
1336 range[num_ranges] = one_range;
1337 num_ranges++;
1339 curr_offset++;
1340 if (range)
1341 range[num_ranges] = NULL;
1343 return range;
1347 * atk_text_free_ranges:
1348 * @ranges: A pointer to an array of #AtkTextRange which is to be freed.
1350 * Frees the memory associated with an array of AtkTextRange. It is assumed
1351 * that the array was returned by the function atk_text_get_bounded_ranges
1352 * and is NULL terminated.
1354 * Since: 1.3
1356 void
1357 atk_text_free_ranges (AtkTextRange **ranges)
1359 AtkTextRange **first = ranges;
1361 if (ranges)
1363 while (*ranges)
1365 AtkTextRange *range;
1367 range = *ranges;
1368 ranges++;
1369 g_free (range->content);
1370 g_free (range);
1372 g_free (first);
1376 static AtkTextRange *
1377 atk_text_range_copy (AtkTextRange *src)
1379 AtkTextRange *dst = g_new0 (AtkTextRange, 1);
1380 dst->bounds = src->bounds;
1381 dst->start_offset = src->start_offset;
1382 dst->end_offset = src->end_offset;
1383 if (src->content)
1384 dst->content = g_strdup (src->content);
1385 return dst;
1388 static void
1389 atk_text_range_free (AtkTextRange *range)
1391 g_free (range->content);
1392 g_free (range);
1395 G_DEFINE_BOXED_TYPE (AtkTextRange, atk_text_range, atk_text_range_copy,
1396 atk_text_range_free)