Revert "Merge remote-tracking branch 'origin/sandbox/mniesluchow/upstream_2_16' into...
[platform/upstream/atk.git] / atk / atktext.c
1 /* ATK - The Accessibility Toolkit for GTK+
2  * Copyright 2001 Sun Microsystems Inc.
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 #include "atk.h"
21 #include "atkmarshal.h"
22
23 #include <string.h>
24
25 /**
26  * SECTION:atktext
27  * @Short_description: The ATK interface implemented by components
28  *  with text content.
29  * @Title:AtkText
30  *
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.
42  *
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.
50  */
51
52 static GPtrArray *extra_attributes = NULL;
53
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
62 };
63
64 static const char boolean[] =
65   "false\0"
66   "true";
67 static const guint8 boolean_offsets[] = {
68   0, 6
69 };
70
71 static const char style[] =
72   "normal\0"
73   "oblique\0"
74   "italic";
75 static const guint8 style_offsets[] = {
76   0, 7, 15
77 };
78
79 static const char variant[] =
80   "normal\0"
81   "small_caps";
82 static const guint8 variant_offsets[] = {
83   0, 7
84 };
85
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
98 };
99
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
107 };
108
109 static const char direction[] =
110   "none\0"
111   "ltr\0"
112   "rtl";
113 static const guint8 direction_offsets[] = {
114   0, 5, 9
115 };
116
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
124 };
125
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
134 };
135
136 static void atk_text_base_init (AtkTextIface *class);
137
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);
143
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);
149
150 static guint atk_text_signals[LAST_SIGNAL] = { 0 };
151
152 GType
153 atk_text_get_type (void)
154 {
155   static GType type = 0;
156
157   if (!type) 
158     {
159       static const GTypeInfo tinfo =
160       {
161         sizeof (AtkTextIface),
162         (GBaseInitFunc) atk_text_base_init,
163         (GBaseFinalizeFunc) NULL,
164         (GClassInitFunc) NULL /* atk_text_interface_init */ ,
165         (GClassFinalizeFunc) NULL,
166
167       };
168
169       type = g_type_register_static (G_TYPE_INTERFACE, "AtkText", &tinfo, 0);
170     }
171
172   return type;
173 }
174
175 static void
176 atk_text_base_init (AtkTextIface *class)
177 {
178   static gboolean initialized = FALSE;
179   
180   if (! initialized)
181     {
182       /* 
183        * Note that text_changed signal supports details "insert", "delete", 
184        * possibly "replace". 
185        */
186      
187       class->get_range_extents = atk_text_real_get_range_extents; 
188       class->get_bounded_ranges = atk_text_real_get_bounded_ranges; 
189
190       /**
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.
195        *
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.
201        *
202        * Deprecated: Since 2.9.4. Use #AtkObject::text-insert or
203        * #AtkObject::text-remove instead.
204        */
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);
214
215       /**
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
221        *
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.
226        */
227       atk_text_signals[TEXT_INSERT] =
228         g_signal_new ("text_insert",
229                       ATK_TYPE_TEXT,
230                       G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
231                       0,
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);
236
237       /**
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
243        *
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.
248        */
249       atk_text_signals[TEXT_REMOVE] =
250         g_signal_new ("text_remove",
251                       ATK_TYPE_TEXT,
252                       G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
253                       0,
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);
258
259       /**
260        * AtkText::text-caret-moved:
261        * @atktext: the object which received the signal.
262        * @arg1: The new position of the text caret.
263        *
264        * The "text-caret-moved" signal is emitted when the caret
265        * position of the text of an object which implements AtkText
266        * changes.
267        */
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);
277
278       /**
279        * AtkText::text-selection-changed:
280        * @atktext: the object which received the signal.
281        *
282        * The "text-selection-changed" signal is emitted when the
283        * selected text of an object which implements AtkText changes.
284        */
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);
293       /**
294        * AtkText::text-attributes-changed:
295        * @atktext: the object which received the signal.
296        *
297        * The "text-attributes-changed" signal is emitted when the text
298        * attributes of the text of an object which implements AtkText
299        * changes.
300        */
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);
309
310       
311       initialized = TRUE;
312     }
313 }
314
315 /**
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.
320  *
321  * Gets the specified text.
322  *
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.
325  **/
326 gchar*
327 atk_text_get_text (AtkText      *text,
328                    gint         start_offset,
329                    gint         end_offset)
330 {
331   AtkTextIface *iface;
332   
333   g_return_val_if_fail (ATK_IS_TEXT (text), NULL);
334
335   iface = ATK_TEXT_GET_IFACE (text);
336
337   if (start_offset < 0 || end_offset < -1 ||
338       (end_offset != -1 && end_offset < start_offset))
339     return NULL;
340
341   if (iface->get_text)
342     return (*(iface->get_text)) (text, start_offset, end_offset);
343   else
344     return NULL;
345 }
346
347 /**
348  * atk_text_get_character_at_offset:
349  * @text: an #AtkText
350  * @offset: position
351  *
352  * Gets the specified text.
353  *
354  * Returns: the character at @offset.
355  **/
356 gunichar
357 atk_text_get_character_at_offset (AtkText      *text,
358                                   gint         offset)
359 {
360   AtkTextIface *iface;
361
362   g_return_val_if_fail (ATK_IS_TEXT (text), (gunichar) 0);
363
364   iface = ATK_TEXT_GET_IFACE (text);
365
366   if (iface->get_character_at_offset)
367     return (*(iface->get_character_at_offset)) (text, offset);
368   else
369     return (gunichar) 0;
370 }
371
372 /**
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
380  *
381  * Gets the specified text.
382  *
383  * Deprecated: This method is deprecated since ATK version
384  * 2.9.3. Please use atk_text_get_string_at_offset() instead.
385  *
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.
388  **/
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)
395 {
396   AtkTextIface *iface;
397   gint local_start_offset, local_end_offset;
398   gint *real_start_offset, *real_end_offset;
399
400   g_return_val_if_fail (ATK_IS_TEXT (text), NULL);
401
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;
410
411   if (offset < 0)
412     return NULL;
413
414   iface = ATK_TEXT_GET_IFACE (text);
415
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;
420 }
421
422 /**
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
430  *
431  * Gets the specified text.
432  *
433  * If the boundary_type if ATK_TEXT_BOUNDARY_CHAR the character at the
434  * offset is returned.
435  *
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.
439  *
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.
443  *
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.
447  *
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.
451  *
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.
455  *
456  * Deprecated: This method is deprecated since ATK version
457  * 2.9.4. Please use atk_text_get_string_at_offset() instead.
458  *
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.
461  **/
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)
468 {
469   AtkTextIface *iface;
470   gint local_start_offset, local_end_offset;
471   gint *real_start_offset, *real_end_offset;
472
473   g_return_val_if_fail (ATK_IS_TEXT (text), NULL);
474
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;
483
484   iface = ATK_TEXT_GET_IFACE (text);
485
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;
490 }
491
492 /**
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
500  *
501  * Gets the specified text.
502  *
503  * Deprecated: This method is deprecated since ATK version
504  * 2.9.3. Please use atk_text_get_string_at_offset() instead.
505  *
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.
508  **/
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)
515 {
516   AtkTextIface *iface;
517   gint local_start_offset, local_end_offset;
518   gint *real_start_offset, *real_end_offset;
519
520   g_return_val_if_fail (ATK_IS_TEXT (text), NULL);
521
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;
530
531   if (offset < 0)
532     return NULL;
533
534   iface = ATK_TEXT_GET_IFACE (text);
535
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;
540 }
541
542 /**
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)
551  *
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.
555  *
556  * If @granularity is ATK_TEXT_GRANULARITY_CHAR the character at the
557  * offset is returned.
558  *
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.
562  *
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.
566  *
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.
570  *
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.
574  *
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.
578  *
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.
582  *
583  * Since: 2.10
584  *
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.
588  **/
589 gchar* atk_text_get_string_at_offset (AtkText *text,
590                                       gint offset,
591                                       AtkTextGranularity granularity,
592                                       gint *start_offset,
593                                       gint *end_offset)
594 {
595   AtkTextIface *iface;
596   gint local_start_offset, local_end_offset;
597   gint *real_start_offset, *real_end_offset;
598
599   g_return_val_if_fail (ATK_IS_TEXT (text), NULL);
600
601   if (start_offset)
602     {
603       *start_offset = -1;
604       real_start_offset = start_offset;
605     }
606   else
607     real_start_offset = &local_start_offset;
608
609   if (end_offset)
610     {
611       *end_offset = -1;
612       real_end_offset = end_offset;
613     }
614   else
615     real_end_offset = &local_end_offset;
616
617   if (offset < 0)
618     return NULL;
619
620   iface = ATK_TEXT_GET_IFACE (text);
621
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;
626 }
627
628 /**
629  * atk_text_get_caret_offset:
630  * @text: an #AtkText
631  *
632  * Gets the offset position of the caret (cursor).
633  *
634  * Returns: the offset position of the caret (cursor).
635  **/
636 gint
637 atk_text_get_caret_offset (AtkText *text)
638 {
639   AtkTextIface *iface;
640
641   g_return_val_if_fail (ATK_IS_TEXT (text), 0);
642
643   iface = ATK_TEXT_GET_IFACE (text);
644
645   if (iface->get_caret_offset)
646     return (*(iface->get_caret_offset)) (text);
647   else
648     return 0;
649 }
650
651 /**
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 
660  *
661  * Get the bounding box containing the glyph representing the character at 
662  *     a particular text offset. 
663  **/
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)
672 {
673   AtkTextIface *iface;
674   gint local_x, local_y, local_width, local_height;
675   gint *real_x, *real_y, *real_width, *real_height;
676
677   g_return_if_fail (ATK_IS_TEXT (text));
678
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;
695
696   *real_x = 0;
697   *real_y = 0;
698   *real_width = 0;
699   *real_height = 0;
700
701   if (offset < 0)
702     return;
703  
704   iface = ATK_TEXT_GET_IFACE (text);
705
706   if (iface->get_character_extents)
707     (*(iface->get_character_extents)) (text, offset, real_x, real_y, real_width, real_height, coords);
708
709   if (*real_width <0)
710     {
711       *real_x = *real_x + *real_width;
712       *real_width *= -1;
713     }
714 }
715
716 /**
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
723  *
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.
731  *
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().
735  **/
736 AtkAttributeSet* 
737 atk_text_get_run_attributes (AtkText          *text,
738                              gint             offset,
739                              gint             *start_offset,
740                              gint             *end_offset)
741 {
742   AtkTextIface *iface;
743   gint local_start_offset, local_end_offset;
744   gint *real_start_offset, *real_end_offset;
745
746   g_return_val_if_fail (ATK_IS_TEXT (text), NULL);
747
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;
756
757   if (offset < -1)
758     return NULL;
759
760   iface = ATK_TEXT_GET_IFACE (text);
761
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;
766 }
767
768 /**
769  * atk_text_get_default_attributes:
770  *@text: an #AtkText
771  *
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.
776  *
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().
780  */
781 AtkAttributeSet* 
782 atk_text_get_default_attributes (AtkText          *text)
783 {
784   AtkTextIface *iface;
785
786   g_return_val_if_fail (ATK_IS_TEXT (text), NULL);
787
788   iface = ATK_TEXT_GET_IFACE (text);
789
790   if (iface->get_default_attributes)
791     return (*(iface->get_default_attributes)) (text);
792   else
793     return NULL;
794 }
795
796 /**
797  * atk_text_get_character_count:
798  * @text: an #AtkText
799  *
800  * Gets the character count.
801  *
802  * Returns: the number of characters.
803  **/
804 gint
805 atk_text_get_character_count (AtkText *text)
806 {
807   AtkTextIface *iface;
808
809   g_return_val_if_fail (ATK_IS_TEXT (text), -1);
810
811   iface = ATK_TEXT_GET_IFACE (text);
812
813   if (iface->get_character_count)
814     return (*(iface->get_character_count)) (text);
815   else
816     return -1;
817 }
818
819 /**
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 
826  *
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.
830  *
831  * Returns: the offset to the character which is located at
832  * the specified @x and @y coordinates.
833  **/
834 gint
835 atk_text_get_offset_at_point (AtkText *text,
836                               gint x,
837                               gint y,
838                               AtkCoordType coords)
839 {
840   AtkTextIface *iface;
841
842   g_return_val_if_fail (ATK_IS_TEXT (text), -1);
843
844   iface = ATK_TEXT_GET_IFACE (text);
845
846   if (iface->get_offset_at_point)
847     return (*(iface->get_offset_at_point)) (text, x, y, coords);
848   else
849     return -1;
850 }
851
852 /**
853  * atk_text_get_n_selections:
854  * @text: an #AtkText
855  *
856  * Gets the number of selected regions.
857  *
858  * Returns: The number of selected regions, or -1 if a failure
859  *   occurred.
860  **/
861 gint
862 atk_text_get_n_selections (AtkText *text)
863 {
864   AtkTextIface *iface;
865
866   g_return_val_if_fail (ATK_IS_TEXT (text), -1);
867
868   iface = ATK_TEXT_GET_IFACE (text);
869
870   if (iface->get_n_selections)
871     return (*(iface->get_n_selections)) (text);
872   else
873     return -1;
874 }
875
876 /**
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
887  *
888  * Gets the text from the specified selection.
889  *
890  * Returns: a newly allocated string containing the selected text. Use g_free()
891  *   to free the returned string.
892  **/
893 gchar*
894 atk_text_get_selection (AtkText *text, 
895                         gint    selection_num,
896                         gint    *start_offset,
897                         gint    *end_offset)
898 {
899   AtkTextIface *iface;
900   gint local_start_offset, local_end_offset;
901   gint *real_start_offset, *real_end_offset;
902
903   g_return_val_if_fail (ATK_IS_TEXT (text), NULL);
904
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;
913
914   iface = ATK_TEXT_GET_IFACE (text);
915
916   if (iface->get_selection)
917   {
918     return (*(iface->get_selection)) (text, selection_num,
919        real_start_offset, real_end_offset);
920   }
921   else
922     return NULL;
923 }
924
925 /**
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.
930  *
931  * Adds a selection bounded by the specified offsets.
932  *
933  * Returns: %TRUE if success, %FALSE otherwise
934  **/
935 gboolean
936 atk_text_add_selection (AtkText *text, 
937                         gint    start_offset,
938                         gint    end_offset)
939 {
940   AtkTextIface *iface;
941
942   g_return_val_if_fail (ATK_IS_TEXT (text), FALSE);
943
944   iface = ATK_TEXT_GET_IFACE (text);
945
946   if (iface->add_selection)
947     return (*(iface->add_selection)) (text, start_offset, end_offset);
948   else
949     return FALSE;
950 }
951
952 /**
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.
960  *
961  * Removes the specified selection.
962  *
963  * Returns: %TRUE if success, %FALSE otherwise
964  **/
965 gboolean
966 atk_text_remove_selection (AtkText *text, 
967                            gint    selection_num)
968 {
969   AtkTextIface *iface;
970
971   g_return_val_if_fail (ATK_IS_TEXT (text), FALSE);
972
973   iface = ATK_TEXT_GET_IFACE (text);
974
975   if (iface->remove_selection)
976     return (*(iface->remove_selection)) (text, selection_num);
977   else
978     return FALSE;
979 }
980
981 /**
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
992  *
993  * Changes the start and end offset of the specified selection.
994  *
995  * Returns: %TRUE if success, %FALSE otherwise
996  **/
997 gboolean
998 atk_text_set_selection (AtkText *text, 
999                         gint    selection_num,
1000                         gint    start_offset, 
1001                         gint    end_offset)
1002 {
1003   AtkTextIface *iface;
1004
1005   g_return_val_if_fail (ATK_IS_TEXT (text), FALSE);
1006
1007   iface = ATK_TEXT_GET_IFACE (text);
1008
1009   if (iface->set_selection)
1010   {
1011     return (*(iface->set_selection)) (text, selection_num,
1012        start_offset, end_offset);
1013   }
1014   else
1015     return FALSE;
1016 }
1017
1018 /**
1019  * atk_text_set_caret_offset:
1020  * @text: an #AtkText
1021  * @offset: position
1022  *
1023  * Sets the caret (cursor) position to the specified @offset.
1024  *
1025  * Returns: %TRUE if success, %FALSE otherwise.
1026  **/
1027 gboolean
1028 atk_text_set_caret_offset (AtkText *text,
1029                            gint    offset)
1030 {
1031   AtkTextIface *iface;
1032
1033   g_return_val_if_fail (ATK_IS_TEXT (text), FALSE);
1034
1035   iface = ATK_TEXT_GET_IFACE (text);
1036
1037   if (iface->set_caret_offset)
1038     {
1039       return (*(iface->set_caret_offset)) (text, offset);
1040     }
1041   else
1042     {
1043       return FALSE;
1044     }
1045 }
1046
1047 /**
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.
1056  *
1057  * Get the bounding box for text within the specified range.
1058  *
1059  * Since: 1.3
1060  **/
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)
1067 {
1068   AtkTextIface *iface;
1069
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);
1073
1074   iface = ATK_TEXT_GET_IFACE (text);
1075
1076   if (iface->get_range_extents)
1077     (*(iface->get_range_extents)) (text, start_offset, end_offset, coord_type, rect);
1078 }
1079
1080 /**
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.
1087  *
1088  * Get the ranges of text in the specified bounding box.
1089  *
1090  * Since: 1.3
1091  *
1092  * Returns: (array zero-terminated=1): Array of AtkTextRange. The last
1093  *          element of the array returned by this function will be NULL.
1094  **/
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)
1101 {
1102   AtkTextIface *iface;
1103
1104   g_return_val_if_fail (ATK_IS_TEXT (text), NULL);
1105   g_return_val_if_fail (rect, NULL);
1106
1107   iface = ATK_TEXT_GET_IFACE (text);
1108
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;
1113 }
1114
1115 /**
1116  * atk_attribute_set_free:
1117  * @attrib_set: The #AtkAttributeSet to free
1118  *
1119  * Frees the memory used by an #AtkAttributeSet, including all its
1120  * #AtkAttributes.
1121  **/
1122 void
1123 atk_attribute_set_free (AtkAttributeSet *attrib_set)
1124 {
1125   GSList *temp;
1126
1127   temp = attrib_set;
1128
1129   while (temp != NULL)
1130     {
1131       AtkAttribute *att;
1132
1133       att = temp->data;
1134
1135       g_free (att->name);
1136       g_free (att->value);
1137       g_free (att);
1138       temp = temp->next;
1139     }
1140   g_slist_free (attrib_set);
1141 }
1142
1143 /**
1144  * atk_text_attribute_register:
1145  * @name: a name string
1146  *
1147  * Associate @name with a new #AtkTextAttribute
1148  *
1149  * Returns: an #AtkTextAttribute associated with @name
1150  **/
1151 AtkTextAttribute
1152 atk_text_attribute_register (const gchar *name)
1153 {
1154   g_return_val_if_fail (name, ATK_TEXT_ATTR_INVALID);
1155
1156   if (!extra_attributes)
1157     extra_attributes = g_ptr_array_new ();
1158
1159   g_ptr_array_add (extra_attributes, g_strdup (name));
1160   return extra_attributes->len + ATK_TEXT_ATTR_LAST_DEFINED;
1161 }
1162
1163 /**
1164  * atk_text_attribute_get_name:
1165  * @attr: The #AtkTextAttribute whose name is required
1166  *
1167  * Gets the name corresponding to the #AtkTextAttribute
1168  *
1169  * Returns: a string containing the name; this string should not be freed
1170  **/
1171 const gchar*
1172 atk_text_attribute_get_name (AtkTextAttribute attr)
1173 {
1174   GTypeClass *type_class;
1175   GEnumValue *value;
1176   const gchar *name = NULL;
1177
1178   type_class = g_type_class_ref (ATK_TYPE_TEXT_ATTRIBUTE);
1179   g_return_val_if_fail (G_IS_ENUM_CLASS (type_class), NULL);
1180
1181   value = g_enum_get_value (G_ENUM_CLASS (type_class), attr);
1182
1183   if (value)
1184     {
1185       name = value->value_nick;
1186     }
1187   else
1188     {
1189       if (extra_attributes)
1190         {
1191           gint n = attr;
1192
1193           n -= ATK_TEXT_ATTR_LAST_DEFINED + 1;
1194
1195           if (n < extra_attributes->len)
1196
1197             name = g_ptr_array_index (extra_attributes, n);
1198         }
1199     }
1200   g_type_class_unref (type_class);
1201   return name;
1202 }
1203
1204 /**
1205  * atk_text_attribute_for_name:
1206  * @name: a string which is the (non-localized) name of an ATK text attribute.
1207  *
1208  * Get the #AtkTextAttribute type corresponding to a text attribute name.
1209  *
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.
1213  **/
1214 AtkTextAttribute
1215 atk_text_attribute_for_name (const gchar *name)
1216 {
1217   GTypeClass *type_class;
1218   GEnumValue *value;
1219   AtkTextAttribute type = ATK_TEXT_ATTR_INVALID;
1220
1221   g_return_val_if_fail (name, ATK_TEXT_ATTR_INVALID);
1222
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);
1225
1226   value = g_enum_get_value_by_nick (G_ENUM_CLASS (type_class), name);
1227
1228   if (value)
1229     {
1230       type = value->value;
1231     }
1232   else
1233     {
1234       gint i;
1235
1236       if (extra_attributes)
1237         {
1238           for (i = 0; i < extra_attributes->len; i++)
1239             {
1240               gchar *extra_attribute = (gchar *)g_ptr_array_index (extra_attributes, i);
1241
1242               g_return_val_if_fail (extra_attribute, ATK_TEXT_ATTR_INVALID);
1243
1244               if (strcmp (name, extra_attribute) == 0)
1245                 {
1246                   type = i + 1 + ATK_TEXT_ATTR_LAST_DEFINED;
1247                   break;
1248                 }
1249             }
1250         }
1251     }
1252   g_type_class_unref (type_class);
1253
1254   return type;
1255 }
1256
1257 /**
1258  * atk_text_attribute_get_value:
1259  * @attr: The #AtkTextAttribute for which a value is required
1260  * @index_: The index of the required value
1261  *
1262  * Gets the value for the index of the #AtkTextAttribute
1263  *
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. 
1266  **/
1267 const gchar*
1268 atk_text_attribute_get_value (AtkTextAttribute attr,
1269                               gint             index)
1270 {
1271   switch (attr)
1272     {
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;
1304    }
1305 }
1306
1307 static void
1308 atk_text_rectangle_union (AtkTextRectangle *src1,
1309                           AtkTextRectangle *src2,
1310                           AtkTextRectangle *dest)
1311 {
1312   gint dest_x, dest_y;
1313
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;
1320 }
1321
1322 static gboolean
1323 atk_text_rectangle_contain (AtkTextRectangle *clip,
1324                             AtkTextRectangle *bounds,
1325                             AtkTextClipType  x_clip_type,
1326                             AtkTextClipType  y_clip_type)
1327 {
1328   gboolean x_min_ok, x_max_ok, y_min_ok, y_max_ok;
1329
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)));
1334
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)));
1339
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)));
1344
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)));
1349
1350   return (x_min_ok && x_max_ok && y_min_ok && y_max_ok);
1351   
1352 }
1353
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)
1360 {
1361   gint i;
1362   AtkTextRectangle cbounds, bounds;
1363
1364   atk_text_get_character_extents (text, start_offset,
1365                                   &bounds.x, &bounds.y,
1366                                   &bounds.width, &bounds.height,
1367                                   coord_type);
1368
1369   for (i = start_offset + 1; i < end_offset; i++)
1370     {
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);
1376     }
1377
1378   rect->x = bounds.x;
1379   rect->y = bounds.y;
1380   rect->width = bounds.width;
1381   rect->height = bounds.height;
1382 }
1383
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)
1390 {
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;
1401
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);
1405
1406   if (bounds_min_offset == 0 &&
1407       bounds_min_offset == bounds_max_offset)
1408     return NULL;
1409
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);
1420
1421   curr_offset = bounds_min_offset;
1422   while (curr_offset < bounds_max_offset)
1423     {
1424       offset = curr_offset;
1425
1426       while (curr_offset < bounds_max_offset)
1427         {
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++;
1435         }
1436       if (curr_offset > offset)
1437         {
1438           AtkTextRange *one_range = g_new (AtkTextRange, 1);
1439
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);
1444
1445           if (num_ranges >= range_size - 1)
1446             {
1447               range_size *= 2;
1448               range = g_realloc (range, range_size * sizeof (gpointer));
1449             }
1450           range[num_ranges] = one_range;
1451           num_ranges++; 
1452         }   
1453       curr_offset++;
1454       if (range)
1455         range[num_ranges] = NULL;
1456     }
1457   return range;
1458 }
1459
1460 /**
1461  * atk_text_free_ranges:
1462  * @ranges: (array): A pointer to an array of #AtkTextRange which is
1463  *   to be freed.
1464  *
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.
1468  *
1469  * Since: 1.3
1470  **/
1471 void
1472 atk_text_free_ranges (AtkTextRange **ranges)
1473 {
1474   AtkTextRange **first = ranges;
1475
1476   if (ranges)
1477     {
1478       while (*ranges)
1479         {
1480           AtkTextRange *range;
1481
1482           range = *ranges;
1483           ranges++;
1484           g_free (range->content);
1485           g_free (range);
1486         }
1487       g_free (first);
1488     }
1489 }
1490
1491 static AtkTextRange *
1492 atk_text_range_copy (AtkTextRange *src)
1493 {
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;
1501 }
1502
1503 static void
1504 atk_text_range_free (AtkTextRange *range)
1505 {
1506   g_free (range->content);
1507   g_free (range);
1508 }
1509
1510 G_DEFINE_BOXED_TYPE (AtkTextRange, atk_text_range, atk_text_range_copy,
1511                      atk_text_range_free)