8e6b9c3e7232191619857942d0d5f37ad1f81247
[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 "atktext.h"
21 #include "atkmarshal.h"
22 #include "atk-enum-types.h"
23
24 #include <string.h>
25
26 /**
27  * SECTION:atktext
28  * @Short_description: The ATK interface implemented by components
29  *  with text content.
30  * @Title:AtkText
31  *
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.
43  *
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.
51  */
52
53 static GPtrArray *extra_attributes = NULL;
54
55 enum {
56   TEXT_CHANGED,
57   TEXT_CARET_MOVED,
58   TEXT_SELECTION_CHANGED,
59   TEXT_ATTRIBUTES_CHANGED,
60   TEXT_INSERT,
61   TEXT_REMOVE,
62   LAST_SIGNAL
63 };
64
65 static const char boolean[] =
66   "false\0"
67   "true";
68 static const guint8 boolean_offsets[] = {
69   0, 6
70 };
71
72 static const char style[] =
73   "normal\0"
74   "oblique\0"
75   "italic";
76 static const guint8 style_offsets[] = {
77   0, 7, 15
78 };
79
80 static const char variant[] =
81   "normal\0"
82   "small_caps";
83 static const guint8 variant_offsets[] = {
84   0, 7
85 };
86
87 static const char stretch[] =
88   "ultra_condensed\0"
89   "extra_condensed\0"
90   "condensed\0"
91   "semi_condensed\0"
92   "normal\0"
93   "semi_expanded\0"
94   "expanded\0"
95   "extra_expanded\0"
96   "ultra_expanded";
97 static const guint8 stretch_offsets[] = {
98   0, 16, 32, 42, 57, 64, 78, 87, 102
99 };
100
101 static const char justification[] =
102   "left\0"
103   "right\0"
104   "center\0"
105   "fill";
106 static const guint8 justification_offsets[] = {
107   0, 5, 11, 18
108 };
109
110 static const char direction[] =
111   "none\0"
112   "ltr\0"
113   "rtl";
114 static const guint8 direction_offsets[] = {
115   0, 5, 9
116 };
117
118 static const char wrap_mode[] =
119   "none\0"
120   "char\0"
121   "word\0"
122   "word_char";
123 static const guint8 wrap_mode_offsets[] = {
124   0, 5, 10, 15
125 };
126
127 static const char underline[] =
128   "none\0"
129   "single\0"
130   "double\0"
131   "low\0"
132   "error";
133 static const guint8 underline_offsets[] = {
134   0, 5, 12, 19, 23
135 };
136
137 static void atk_text_base_init (AtkTextIface *class);
138
139 static void atk_text_real_get_range_extents  (AtkText          *text,
140                                               gint             start_offset,
141                                               gint             end_offset,
142                                               AtkCoordType     coord_type,
143                                               AtkTextRectangle *rect);
144
145 static AtkTextRange** atk_text_real_get_bounded_ranges (AtkText          *text,
146                                                         AtkTextRectangle *rect,
147                                                         AtkCoordType     coord_type,
148                                                         AtkTextClipType  x_clip_type,
149                                                         AtkTextClipType  y_clip_type);
150
151 static guint atk_text_signals[LAST_SIGNAL] = { 0 };
152
153 GType
154 atk_text_get_type (void)
155 {
156   static GType type = 0;
157
158   if (!type) 
159     {
160       static const GTypeInfo tinfo =
161       {
162         sizeof (AtkTextIface),
163         (GBaseInitFunc) atk_text_base_init,
164         (GBaseFinalizeFunc) NULL,
165         (GClassInitFunc) NULL /* atk_text_interface_init */ ,
166         (GClassFinalizeFunc) NULL,
167
168       };
169
170       type = g_type_register_static (G_TYPE_INTERFACE, "AtkText", &tinfo, 0);
171     }
172
173   return type;
174 }
175
176 static void
177 atk_text_base_init (AtkTextIface *class)
178 {
179   static gboolean initialized = FALSE;
180   
181   if (! initialized)
182     {
183       /* 
184        * Note that text_changed signal supports details "insert", "delete", 
185        * possibly "replace". 
186        */
187      
188       class->get_range_extents = atk_text_real_get_range_extents; 
189       class->get_bounded_ranges = atk_text_real_get_bounded_ranges; 
190
191       /**
192        * AtkText::text-changed:
193        * @atktext: the object which received the signal.
194        * @arg1: The position (character offset) of the insertion or deletion.
195        * @arg2: The length (in characters) of text inserted or deleted.
196        *
197        * The "text-changed" signal is emitted when the text of the
198        * object which implements the AtkText interface changes, This
199        * signal will have a detail which is either "insert" or
200        * "delete" which identifies whether the text change was an
201        * insertion or a deletion.
202        *
203        * Deprecated: Since 2.9.4. Use #AtkObject::text-insert or
204        * #AtkObject::text-remove instead.
205        */
206       atk_text_signals[TEXT_CHANGED] =
207         g_signal_new ("text_changed",
208                       ATK_TYPE_TEXT,
209                       G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
210                       G_STRUCT_OFFSET (AtkTextIface, text_changed), 
211                       (GSignalAccumulator) NULL, NULL,
212                       atk_marshal_VOID__INT_INT,
213                       G_TYPE_NONE,
214                       2, G_TYPE_INT, G_TYPE_INT);
215
216       /**
217        * AtkText::text-insert:
218        * @atktext: the object which received the signal.
219        * @arg1: The position (character offset) of the insertion.
220        * @arg2: The length (in characters) of text inserted.
221        * @arg3: The new text inserted
222        *
223        * The "text-insert" signal is emitted when a new text is
224        * inserted.
225        */
226       atk_text_signals[TEXT_INSERT] =
227         g_signal_new ("text_insert",
228                       ATK_TYPE_TEXT,
229                       G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
230                       0,
231                       (GSignalAccumulator) NULL, NULL,
232                       atk_marshal_VOID__INT_INT_STRING,
233                       G_TYPE_NONE,
234                       3, G_TYPE_INT, G_TYPE_INT, G_TYPE_STRING);
235
236       /**
237        * AtkText::text-remove:
238        * @atktext: the object which received the signal.
239        * @arg1: The position (character offset) of the removal.
240        * @arg2: The length (in characters) of text removed.
241        * @arg3: The old text removed
242        *
243        * The "text-remove" signal is emitted when a new text is
244        * removed.
245        */
246       atk_text_signals[TEXT_REMOVE] =
247         g_signal_new ("text_remove",
248                       ATK_TYPE_TEXT,
249                       G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
250                       0,
251                       (GSignalAccumulator) NULL, NULL,
252                       atk_marshal_VOID__INT_INT_STRING,
253                       G_TYPE_NONE,
254                       3, G_TYPE_INT, G_TYPE_INT, G_TYPE_STRING);
255
256       /**
257        * AtkText::text-caret-moved:
258        * @atktext: the object which received the signal.
259        * @arg1: The new position of the text caret.
260        *
261        * The "text-caret-moved" signal is emitted when the caret
262        * position of the text of an object which implements AtkText
263        * changes.
264        */
265       atk_text_signals[TEXT_CARET_MOVED] =
266         g_signal_new ("text_caret_moved",
267                       ATK_TYPE_TEXT,
268                       G_SIGNAL_RUN_LAST,
269                       G_STRUCT_OFFSET (AtkTextIface, text_caret_moved),
270                       (GSignalAccumulator) NULL, NULL,
271                       g_cclosure_marshal_VOID__INT,
272                       G_TYPE_NONE,
273                       1, G_TYPE_INT);
274
275       /**
276        * AtkText::text-selection-changed:
277        * @atktext: the object which received the signal.
278        *
279        * The "text-selection-changed" signal is emitted when the
280        * selected text of an object which implements AtkText changes.
281        */
282       atk_text_signals[TEXT_SELECTION_CHANGED] =
283         g_signal_new ("text_selection_changed",
284                       ATK_TYPE_TEXT,
285                       G_SIGNAL_RUN_LAST,
286                       G_STRUCT_OFFSET (AtkTextIface, text_selection_changed),
287                       (GSignalAccumulator) NULL, NULL,
288                       g_cclosure_marshal_VOID__VOID,
289                       G_TYPE_NONE, 0);
290       /**
291        * AtkText::text-attributes-changed:
292        * @atktext: the object which received the signal.
293        *
294        * The "text-attributes-changed" signal is emitted when the text
295        * attributes of the text of an object which implements AtkText
296        * changes.
297        */
298       atk_text_signals[TEXT_ATTRIBUTES_CHANGED] =
299         g_signal_new ("text_attributes_changed",
300                       ATK_TYPE_TEXT,
301                       G_SIGNAL_RUN_LAST,
302                       G_STRUCT_OFFSET (AtkTextIface, text_attributes_changed),
303                       (GSignalAccumulator) NULL, NULL,
304                       g_cclosure_marshal_VOID__VOID,
305                       G_TYPE_NONE, 0);
306
307       
308       initialized = TRUE;
309     }
310 }
311
312 /**
313  * atk_text_get_text:
314  * @text: an #AtkText
315  * @start_offset: start position
316  * @end_offset: end position, or -1 for the end of the string.
317  *
318  * Gets the specified text.
319  *
320  * Returns: a newly allocated string containing the text from @start_offset up
321  *   to, but not including @end_offset. Use g_free() to free the returned string.
322  **/
323 gchar*
324 atk_text_get_text (AtkText      *text,
325                    gint         start_offset,
326                    gint         end_offset)
327 {
328   AtkTextIface *iface;
329   
330   g_return_val_if_fail (ATK_IS_TEXT (text), NULL);
331
332   iface = ATK_TEXT_GET_IFACE (text);
333
334   if (start_offset < 0 || end_offset < -1 ||
335       (end_offset != -1 && end_offset < start_offset))
336     return NULL;
337
338   if (iface->get_text)
339     return (*(iface->get_text)) (text, start_offset, end_offset);
340   else
341     return NULL;
342 }
343
344 /**
345  * atk_text_get_character_at_offset:
346  * @text: an #AtkText
347  * @offset: position
348  *
349  * Gets the specified text.
350  *
351  * Returns: the character at @offset.
352  **/
353 gunichar
354 atk_text_get_character_at_offset (AtkText      *text,
355                                   gint         offset)
356 {
357   AtkTextIface *iface;
358
359   g_return_val_if_fail (ATK_IS_TEXT (text), (gunichar) 0);
360
361   iface = ATK_TEXT_GET_IFACE (text);
362
363   if (iface->get_character_at_offset)
364     return (*(iface->get_character_at_offset)) (text, offset);
365   else
366     return (gunichar) 0;
367 }
368
369 /**
370  * atk_text_get_text_after_offset:
371  * @text: an #AtkText
372  * @offset: position
373  * @boundary_type: An #AtkTextBoundary
374  * @start_offset: (out): the start offset of the returned string
375  * @end_offset: (out): the offset of the first character after the
376  *              returned substring
377  *
378  * Gets the specified text.
379  *
380  * Deprecated: This method is deprecated since ATK version
381  * 2.9.3. Please use atk_text_get_at_offset() instead.
382  *
383  * Returns: a newly allocated string containing the text after @offset bounded
384  *   by the specified @boundary_type. Use g_free() to free the returned string.
385  **/
386 gchar*
387 atk_text_get_text_after_offset (AtkText          *text,
388                                 gint             offset,
389                                 AtkTextBoundary  boundary_type,
390                                 gint             *start_offset,
391                                 gint             *end_offset)
392 {
393   AtkTextIface *iface;
394   gint local_start_offset, local_end_offset;
395   gint *real_start_offset, *real_end_offset;
396
397   g_return_val_if_fail (ATK_IS_TEXT (text), NULL);
398
399   if (start_offset)
400     real_start_offset = start_offset;
401   else
402     real_start_offset = &local_start_offset;
403   if (end_offset)
404     real_end_offset = end_offset;
405   else
406     real_end_offset = &local_end_offset;
407
408   if (offset < 0)
409     return NULL;
410
411   iface = ATK_TEXT_GET_IFACE (text);
412
413   if (iface->get_text_after_offset)
414     return (*(iface->get_text_after_offset)) (text, offset, boundary_type, real_start_offset, real_end_offset);
415   else
416     return NULL;
417 }
418
419 /**
420  * atk_text_get_text_at_offset:
421  * @text: an #AtkText
422  * @offset: position
423  * @boundary_type: An #AtkTextBoundary
424  * @start_offset: (out): the start offset of the returned string
425  * @end_offset: (out): the offset of the first character after the
426  *              returned substring
427  *
428  * Gets the specified text.
429  *
430  * If the boundary_type if ATK_TEXT_BOUNDARY_CHAR the character at the
431  * offset is returned.
432  *
433  * If the boundary_type is ATK_TEXT_BOUNDARY_WORD_START the returned string
434  * is from the word start at or before the offset to the word start after
435  * the offset.
436  *
437  * The returned string will contain the word at the offset if the offset
438  * is inside a word and will contain the word before the offset if the
439  * offset is not inside a word.
440  *
441  * If the boundary type is ATK_TEXT_BOUNDARY_SENTENCE_START the returned
442  * string is from the sentence start at or before the offset to the sentence
443  * start after the offset.
444  *
445  * The returned string will contain the sentence at the offset if the offset
446  * is inside a sentence and will contain the sentence before the offset
447  * if the offset is not inside a sentence.
448  *
449  * If the boundary type is ATK_TEXT_BOUNDARY_LINE_START the returned
450  * string is from the line start at or before the offset to the line
451  * start after the offset.
452  *
453  * Returns: a newly allocated string containing the text at @offset bounded by
454  *   the specified @boundary_type. Use g_free() to free the returned string.
455  **/
456 gchar*
457 atk_text_get_text_at_offset (AtkText          *text,
458                              gint             offset,
459                              AtkTextBoundary  boundary_type,
460                              gint             *start_offset,
461                              gint             *end_offset)
462 {
463   AtkTextIface *iface;
464   gint local_start_offset, local_end_offset;
465   gint *real_start_offset, *real_end_offset;
466
467   g_return_val_if_fail (ATK_IS_TEXT (text), NULL);
468
469   if (start_offset)
470     real_start_offset = start_offset;
471   else
472     real_start_offset = &local_start_offset;
473   if (end_offset)
474     real_end_offset = end_offset;
475   else
476     real_end_offset = &local_end_offset;
477
478   iface = ATK_TEXT_GET_IFACE (text);
479
480   if (iface->get_text_at_offset)
481     return (*(iface->get_text_at_offset)) (text, offset, boundary_type, real_start_offset, real_end_offset);
482   else
483     return NULL;
484 }
485
486 /**
487  * atk_text_get_text_before_offset:
488  * @text: an #AtkText
489  * @offset: position
490  * @boundary_type: An #AtkTextBoundary
491  * @start_offset: (out): the start offset of the returned string
492  * @end_offset: (out): the offset of the first character after the
493  *              returned substring
494  *
495  * Gets the specified text.
496  *
497  * Deprecated: This method is deprecated since ATK version
498  * 2.9.3. Please use atk_text_get_at_offset() instead.
499  *
500  * Returns: a newly allocated string containing the text before @offset bounded
501  *   by the specified @boundary_type. Use g_free() to free the returned string.
502  **/
503 gchar*
504 atk_text_get_text_before_offset (AtkText          *text,
505                                  gint             offset,
506                                  AtkTextBoundary  boundary_type,
507                                  gint             *start_offset,
508                                  gint             *end_offset)
509 {
510   AtkTextIface *iface;
511   gint local_start_offset, local_end_offset;
512   gint *real_start_offset, *real_end_offset;
513
514   g_return_val_if_fail (ATK_IS_TEXT (text), NULL);
515
516   if (start_offset)
517     real_start_offset = start_offset;
518   else
519     real_start_offset = &local_start_offset;
520   if (end_offset)
521     real_end_offset = end_offset;
522   else
523     real_end_offset = &local_end_offset;
524
525   if (offset < 0)
526     return NULL;
527
528   iface = ATK_TEXT_GET_IFACE (text);
529
530   if (iface->get_text_before_offset)
531     return (*(iface->get_text_before_offset)) (text, offset, boundary_type, real_start_offset, real_end_offset);
532   else
533     return NULL;
534 }
535
536 /**
537  * atk_text_get_string_at_offset:
538  * @text: an #AtkText
539  * @offset: position
540  * @granularity: An #AtkTextGranularity
541  * @start_offset: (out): the start offset of the returned string, or -1
542  *                if an error has occurred (e.g. invalid offset, not implemented)
543  * @end_offset: (out): the offset of the first character after the returned string,
544  *              or -1 if an error has occurred (e.g. invalid offset, not implemented)
545  *
546  * Gets a portion of the text exposed through an #AtkText according to a given @offset
547  * and a specific @granularity, along with the start and end offsets defining the
548  * boundaries of such a portion of text.
549  *
550  * If @granularity is ATK_TEXT_GRANULARITY_CHAR the character at the
551  * offset is returned.
552  *
553  * If @granularity is ATK_TEXT_GRANULARITY_WORD the returned string
554  * is from the word start at or before the offset to the word start after
555  * the offset.
556  *
557  * The returned string will contain the word at the offset if the offset
558  * is inside a word and will contain the word before the offset if the
559  * offset is not inside a word.
560  *
561  * If @granularity is ATK_TEXT_GRANULARITY_SENTENCE the returned string
562  * is from the sentence start at or before the offset to the sentence
563  * start after the offset.
564  *
565  * The returned string will contain the sentence at the offset if the offset
566  * is inside a sentence and will contain the sentence before the offset
567  * if the offset is not inside a sentence.
568  *
569  * If @granularity is ATK_TEXT_GRANULARITY_LINE the returned string
570  * is from the line start at or before the offset to the line
571  * start after the offset.
572  *
573  * If @granularity is ATK_TEXT_GRANULARITY_PARAGRAPH the returned string
574  * is from the start of the paragraph at or before the offset to the start
575  * of the following paragraph after the offset.
576  *
577  * Since: 2.9.4
578  *
579  * Returns: a newly allocated string containing the text at the @offset bounded
580  *   by the specified @granularity. Use g_free() to free the returned string.
581  *   Returns %NULL if the offset is invalid or no implementation is available.
582  **/
583 gchar* atk_text_get_string_at_offset (AtkText *text,
584                                       gint offset,
585                                       AtkTextGranularity granularity,
586                                       gint *start_offset,
587                                       gint *end_offset)
588 {
589   AtkTextIface *iface;
590   gint local_start_offset, local_end_offset;
591   gint *real_start_offset, *real_end_offset;
592
593   g_return_val_if_fail (ATK_IS_TEXT (text), NULL);
594
595   if (start_offset)
596     {
597       *start_offset = -1;
598       real_start_offset = start_offset;
599     }
600   else
601     real_start_offset = &local_start_offset;
602
603   if (end_offset)
604     {
605       *end_offset = -1;
606       real_end_offset = end_offset;
607     }
608   else
609     real_end_offset = &local_end_offset;
610
611   if (offset < 0)
612     return NULL;
613
614   iface = ATK_TEXT_GET_IFACE (text);
615
616   if (iface->get_string_at_offset)
617     return (*(iface->get_string_at_offset)) (text, offset, granularity, real_start_offset, real_end_offset);
618   else
619     return NULL;
620 }
621
622 /**
623  * atk_text_get_caret_offset:
624  * @text: an #AtkText
625  *
626  * Gets the offset position of the caret (cursor).
627  *
628  * Returns: the offset position of the caret (cursor).
629  **/
630 gint
631 atk_text_get_caret_offset (AtkText *text)
632 {
633   AtkTextIface *iface;
634
635   g_return_val_if_fail (ATK_IS_TEXT (text), 0);
636
637   iface = ATK_TEXT_GET_IFACE (text);
638
639   if (iface->get_caret_offset)
640     return (*(iface->get_caret_offset)) (text);
641   else
642     return 0;
643 }
644
645 /**
646  * atk_text_get_character_extents:
647  * @text: an #AtkText
648  * @offset: The offset of the text character for which bounding information is required.
649  * @x: Pointer for the x cordinate of the bounding box
650  * @y: Pointer for the y cordinate of the bounding box
651  * @width: Pointer for the width of the bounding box
652  * @height: Pointer for the height of the bounding box
653  * @coords: specify whether coordinates are relative to the screen or widget window 
654  *
655  * Get the bounding box containing the glyph representing the character at 
656  *     a particular text offset. 
657  **/
658 void
659 atk_text_get_character_extents (AtkText *text,
660                                 gint offset,
661                                 gint *x,
662                                 gint *y,
663                                 gint *width,
664                                 gint *height,
665                                 AtkCoordType coords)
666 {
667   AtkTextIface *iface;
668   gint local_x, local_y, local_width, local_height;
669   gint *real_x, *real_y, *real_width, *real_height;
670
671   g_return_if_fail (ATK_IS_TEXT (text));
672
673   if (x)
674     real_x = x;
675   else
676     real_x = &local_x;
677   if (y)
678     real_y = y;
679   else
680     real_y = &local_y;
681   if (width)
682     real_width = width;
683   else
684     real_width = &local_width;
685   if (height)
686     real_height = height;
687   else
688     real_height = &local_height;
689
690   *real_x = 0;
691   *real_y = 0;
692   *real_width = 0;
693   *real_height = 0;
694
695   if (offset < 0)
696     return;
697  
698   iface = ATK_TEXT_GET_IFACE (text);
699
700   if (iface->get_character_extents)
701     (*(iface->get_character_extents)) (text, offset, real_x, real_y, real_width, real_height, coords);
702
703   if (*real_width <0)
704     {
705       *real_x = *real_x + *real_width;
706       *real_width *= -1;
707     }
708 }
709
710 /**
711  * atk_text_get_run_attributes:
712  *@text: an #AtkText
713  *@offset: the offset at which to get the attributes, -1 means the offset of
714  *the character to be inserted at the caret location.
715  *@start_offset: (out): the address to put the start offset of the range
716  *@end_offset: (out): the address to put the end offset of the range
717  *
718  *Creates an #AtkAttributeSet which consists of the attributes explicitly
719  *set at the position @offset in the text. @start_offset and @end_offset are
720  *set to the start and end of the range around @offset where the attributes are
721  *invariant. Note that @end_offset is the offset of the first character
722  *after the range.  See the enum AtkTextAttribute for types of text 
723  *attributes that can be returned. Note that other attributes may also be 
724  *returned.
725  *
726  *Returns: (transfer full): an #AtkAttributeSet which contains the attributes
727  * explicitly set at @offset. This #AtkAttributeSet should be freed by a call
728  * to atk_attribute_set_free().
729  **/
730 AtkAttributeSet* 
731 atk_text_get_run_attributes (AtkText          *text,
732                              gint             offset,
733                              gint             *start_offset,
734                              gint             *end_offset)
735 {
736   AtkTextIface *iface;
737   gint local_start_offset, local_end_offset;
738   gint *real_start_offset, *real_end_offset;
739
740   g_return_val_if_fail (ATK_IS_TEXT (text), NULL);
741
742   if (start_offset)
743     real_start_offset = start_offset;
744   else
745     real_start_offset = &local_start_offset;
746   if (end_offset)
747     real_end_offset = end_offset;
748   else
749     real_end_offset = &local_end_offset;
750
751   if (offset < -1)
752     return NULL;
753
754   iface = ATK_TEXT_GET_IFACE (text);
755
756   if (iface->get_run_attributes)
757     return (*(iface->get_run_attributes)) (text, offset, real_start_offset, real_end_offset);
758   else
759     return NULL;
760 }
761
762 /**
763  * atk_text_get_default_attributes:
764  *@text: an #AtkText
765  *
766  *Creates an #AtkAttributeSet which consists of the default values of
767  *attributes for the text. See the enum AtkTextAttribute for types of text 
768  *attributes that can be returned. Note that other attributes may also be 
769  *returned.
770  *
771  *Returns: (transfer full): an #AtkAttributeSet which contains the default
772  * values of attributes.  at @offset. this #atkattributeset should be freed by
773  * a call to atk_attribute_set_free().
774  */
775 AtkAttributeSet* 
776 atk_text_get_default_attributes (AtkText          *text)
777 {
778   AtkTextIface *iface;
779
780   g_return_val_if_fail (ATK_IS_TEXT (text), NULL);
781
782   iface = ATK_TEXT_GET_IFACE (text);
783
784   if (iface->get_default_attributes)
785     return (*(iface->get_default_attributes)) (text);
786   else
787     return NULL;
788 }
789
790 /**
791  * atk_text_get_character_count:
792  * @text: an #AtkText
793  *
794  * Gets the character count.
795  *
796  * Returns: the number of characters.
797  **/
798 gint
799 atk_text_get_character_count (AtkText *text)
800 {
801   AtkTextIface *iface;
802
803   g_return_val_if_fail (ATK_IS_TEXT (text), -1);
804
805   iface = ATK_TEXT_GET_IFACE (text);
806
807   if (iface->get_character_count)
808     return (*(iface->get_character_count)) (text);
809   else
810     return -1;
811 }
812
813 /**
814  * atk_text_get_offset_at_point:
815  * @text: an #AtkText
816  * @x: screen x-position of character
817  * @y: screen y-position of character
818  * @coords: specify whether coordinates are relative to the screen or
819  * widget window 
820  *
821  * Gets the offset of the character located at coordinates @x and @y. @x and @y
822  * are interpreted as being relative to the screen or this widget's window
823  * depending on @coords.
824  *
825  * Returns: the offset to the character which is located at
826  * the specified @x and @y coordinates.
827  **/
828 gint
829 atk_text_get_offset_at_point (AtkText *text,
830                               gint x,
831                               gint y,
832                               AtkCoordType coords)
833 {
834   AtkTextIface *iface;
835
836   g_return_val_if_fail (ATK_IS_TEXT (text), -1);
837
838   iface = ATK_TEXT_GET_IFACE (text);
839
840   if (iface->get_offset_at_point)
841     return (*(iface->get_offset_at_point)) (text, x, y, coords);
842   else
843     return -1;
844 }
845
846 /**
847  * atk_text_get_n_selections:
848  * @text: an #AtkText
849  *
850  * Gets the number of selected regions.
851  *
852  * Returns: The number of selected regions, or -1 if a failure
853  *   occurred.
854  **/
855 gint
856 atk_text_get_n_selections (AtkText *text)
857 {
858   AtkTextIface *iface;
859
860   g_return_val_if_fail (ATK_IS_TEXT (text), -1);
861
862   iface = ATK_TEXT_GET_IFACE (text);
863
864   if (iface->get_n_selections)
865     return (*(iface->get_n_selections)) (text);
866   else
867     return -1;
868 }
869
870 /**
871  * atk_text_get_selection:
872  * @text: an #AtkText
873  * @selection_num: The selection number.  The selected regions are
874  * assigned numbers that correspond to how far the region is from the
875  * start of the text.  The selected region closest to the beginning
876  * of the text region is assigned the number 0, etc.  Note that adding,
877  * moving or deleting a selected region can change the numbering.
878  * @start_offset: (out): passes back the start position of the selected region
879  * @end_offset: (out): passes back the end position of (e.g. offset immediately past)
880  * the selected region
881  *
882  * Gets the text from the specified selection.
883  *
884  * Returns: a newly allocated string containing the selected text. Use g_free()
885  *   to free the returned string.
886  **/
887 gchar*
888 atk_text_get_selection (AtkText *text, 
889                         gint    selection_num,
890                         gint    *start_offset,
891                         gint    *end_offset)
892 {
893   AtkTextIface *iface;
894   gint local_start_offset, local_end_offset;
895   gint *real_start_offset, *real_end_offset;
896
897   g_return_val_if_fail (ATK_IS_TEXT (text), NULL);
898
899   if (start_offset)
900     real_start_offset = start_offset;
901   else
902     real_start_offset = &local_start_offset;
903   if (end_offset)
904     real_end_offset = end_offset;
905   else
906     real_end_offset = &local_end_offset;
907
908   iface = ATK_TEXT_GET_IFACE (text);
909
910   if (iface->get_selection)
911   {
912     return (*(iface->get_selection)) (text, selection_num,
913        real_start_offset, real_end_offset);
914   }
915   else
916     return NULL;
917 }
918
919 /**
920  * atk_text_add_selection:
921  * @text: an #AtkText
922  * @start_offset: the start position of the selected region
923  * @end_offset: the offset of the first character after the selected region.
924  *
925  * Adds a selection bounded by the specified offsets.
926  *
927  * Returns: %TRUE if success, %FALSE otherwise
928  **/
929 gboolean
930 atk_text_add_selection (AtkText *text, 
931                         gint    start_offset,
932                         gint    end_offset)
933 {
934   AtkTextIface *iface;
935
936   g_return_val_if_fail (ATK_IS_TEXT (text), FALSE);
937
938   iface = ATK_TEXT_GET_IFACE (text);
939
940   if (iface->add_selection)
941     return (*(iface->add_selection)) (text, start_offset, end_offset);
942   else
943     return FALSE;
944 }
945
946 /**
947  * atk_text_remove_selection:
948  * @text: an #AtkText
949  * @selection_num: The selection number.  The selected regions are
950  * assigned numbers that correspond to how far the region is from the
951  * start of the text.  The selected region closest to the beginning
952  * of the text region is assigned the number 0, etc.  Note that adding,
953  * moving or deleting a selected region can change the numbering.
954  *
955  * Removes the specified selection.
956  *
957  * Returns: %TRUE if success, %FALSE otherwise
958  **/
959 gboolean
960 atk_text_remove_selection (AtkText *text, 
961                            gint    selection_num)
962 {
963   AtkTextIface *iface;
964
965   g_return_val_if_fail (ATK_IS_TEXT (text), FALSE);
966
967   iface = ATK_TEXT_GET_IFACE (text);
968
969   if (iface->remove_selection)
970     return (*(iface->remove_selection)) (text, selection_num);
971   else
972     return FALSE;
973 }
974
975 /**
976  * atk_text_set_selection:
977  * @text: an #AtkText
978  * @selection_num: The selection number.  The selected regions are
979  * assigned numbers that correspond to how far the region is from the
980  * start of the text.  The selected region closest to the beginning
981  * of the text region is assigned the number 0, etc.  Note that adding,
982  * moving or deleting a selected region can change the numbering.
983  * @start_offset: the new start position of the selection
984  * @end_offset: the new end position of (e.g. offset immediately past) 
985  * the selection
986  *
987  * Changes the start and end offset of the specified selection.
988  *
989  * Returns: %TRUE if success, %FALSE otherwise
990  **/
991 gboolean
992 atk_text_set_selection (AtkText *text, 
993                         gint    selection_num,
994                         gint    start_offset, 
995                         gint    end_offset)
996 {
997   AtkTextIface *iface;
998
999   g_return_val_if_fail (ATK_IS_TEXT (text), FALSE);
1000
1001   iface = ATK_TEXT_GET_IFACE (text);
1002
1003   if (iface->set_selection)
1004   {
1005     return (*(iface->set_selection)) (text, selection_num,
1006        start_offset, end_offset);
1007   }
1008   else
1009     return FALSE;
1010 }
1011
1012 /**
1013  * atk_text_set_caret_offset:
1014  * @text: an #AtkText
1015  * @offset: position
1016  *
1017  * Sets the caret (cursor) position to the specified @offset.
1018  *
1019  * Returns: %TRUE if success, %FALSE otherwise.
1020  **/
1021 gboolean
1022 atk_text_set_caret_offset (AtkText *text,
1023                            gint    offset)
1024 {
1025   AtkTextIface *iface;
1026
1027   g_return_val_if_fail (ATK_IS_TEXT (text), FALSE);
1028
1029   iface = ATK_TEXT_GET_IFACE (text);
1030
1031   if (iface->set_caret_offset)
1032     {
1033       return (*(iface->set_caret_offset)) (text, offset);
1034     }
1035   else
1036     {
1037       return FALSE;
1038     }
1039 }
1040
1041 /**
1042  * atk_text_get_range_extents:
1043  * @text: an #AtkText
1044  * @start_offset: The offset of the first text character for which boundary 
1045  *        information is required.
1046  * @end_offset: The offset of the text character after the last character 
1047  *        for which boundary information is required.
1048  * @coord_type: Specify whether coordinates are relative to the screen or widget window.
1049  * @rect: A pointer to a AtkTextRectangle which is filled in by this function.
1050  *
1051  * Get the bounding box for text within the specified range.
1052  *
1053  * Since: 1.3
1054  **/
1055 void
1056 atk_text_get_range_extents (AtkText          *text,
1057                             gint             start_offset,
1058                             gint             end_offset,
1059                             AtkCoordType     coord_type,
1060                             AtkTextRectangle *rect)
1061 {
1062   AtkTextIface *iface;
1063
1064   g_return_if_fail (ATK_IS_TEXT (text));
1065   g_return_if_fail (rect);
1066
1067   if (start_offset < 0 || start_offset >= end_offset)
1068     return;
1069  
1070   iface = ATK_TEXT_GET_IFACE (text);
1071
1072   if (iface->get_range_extents)
1073     (*(iface->get_range_extents)) (text, start_offset, end_offset, coord_type, rect);
1074 }
1075
1076 /**
1077  * atk_text_get_bounded_ranges:
1078  * @text: an #AtkText
1079  * @rect: An AtkTextRectangle giving the dimensions of the bounding box.
1080  * @coord_type: Specify whether coordinates are relative to the screen or widget window.
1081  * @x_clip_type: Specify the horizontal clip type.
1082  * @y_clip_type: Specify the vertical clip type.
1083  *
1084  * Get the ranges of text in the specified bounding box.
1085  *
1086  * Since: 1.3
1087  *
1088  * Returns: (array zero-terminated=1): Array of AtkTextRange. The last
1089  *          element of the array returned by this function will be NULL.
1090  * Virtual: get_bounded_ranges
1091  **/
1092 AtkTextRange**
1093 atk_text_get_bounded_ranges (AtkText          *text,
1094                              AtkTextRectangle *rect,
1095                              AtkCoordType      coord_type,
1096                              AtkTextClipType   x_clip_type,
1097                              AtkTextClipType   y_clip_type)
1098 {
1099   AtkTextIface *iface;
1100
1101   g_return_val_if_fail (ATK_IS_TEXT (text), NULL);
1102   g_return_val_if_fail (rect, NULL);
1103
1104   iface = ATK_TEXT_GET_IFACE (text);
1105
1106   if (iface->get_bounded_ranges)
1107     return (*(iface->get_bounded_ranges)) (text, rect, coord_type, x_clip_type, y_clip_type);
1108   else
1109     return NULL;
1110 }
1111
1112 /**
1113  * atk_attribute_set_free:
1114  * @attrib_set: The #AtkAttributeSet to free
1115  *
1116  * Frees the memory used by an #AtkAttributeSet, including all its
1117  * #AtkAttributes.
1118  **/
1119 void
1120 atk_attribute_set_free (AtkAttributeSet *attrib_set)
1121 {
1122   GSList *temp;
1123
1124   temp = attrib_set;
1125
1126   while (temp != NULL)
1127     {
1128       AtkAttribute *att;
1129
1130       att = temp->data;
1131
1132       g_free (att->name);
1133       g_free (att->value);
1134       g_free (att);
1135       temp = temp->next;
1136     }
1137   g_slist_free (attrib_set);
1138 }
1139
1140 /**
1141  * atk_text_attribute_register:
1142  * @name: a name string
1143  *
1144  * Associate @name with a new #AtkTextAttribute
1145  *
1146  * Returns: an #AtkTextAttribute associated with @name
1147  **/
1148 AtkTextAttribute
1149 atk_text_attribute_register (const gchar *name)
1150 {
1151   g_return_val_if_fail (name, ATK_TEXT_ATTR_INVALID);
1152
1153   if (!extra_attributes)
1154     extra_attributes = g_ptr_array_new ();
1155
1156   g_ptr_array_add (extra_attributes, g_strdup (name));
1157   return extra_attributes->len + ATK_TEXT_ATTR_LAST_DEFINED;
1158 }
1159
1160 /**
1161  * atk_text_attribute_get_name:
1162  * @attr: The #AtkTextAttribute whose name is required
1163  *
1164  * Gets the name corresponding to the #AtkTextAttribute
1165  *
1166  * Returns: a string containing the name; this string should not be freed
1167  **/
1168 const gchar*
1169 atk_text_attribute_get_name (AtkTextAttribute attr)
1170 {
1171   GTypeClass *type_class;
1172   GEnumValue *value;
1173   const gchar *name = NULL;
1174
1175   type_class = g_type_class_ref (ATK_TYPE_TEXT_ATTRIBUTE);
1176   g_return_val_if_fail (G_IS_ENUM_CLASS (type_class), NULL);
1177
1178   value = g_enum_get_value (G_ENUM_CLASS (type_class), attr);
1179
1180   if (value)
1181     {
1182       name = value->value_nick;
1183     }
1184   else
1185     {
1186       if (extra_attributes)
1187         {
1188           gint n = attr;
1189
1190           n -= ATK_TEXT_ATTR_LAST_DEFINED + 1;
1191
1192           if (n < extra_attributes->len)
1193
1194             name = g_ptr_array_index (extra_attributes, n);
1195         }
1196     }
1197   g_type_class_unref (type_class);
1198   return name;
1199 }
1200
1201 /**
1202  * atk_text_attribute_for_name:
1203  * @name: a string which is the (non-localized) name of an ATK text attribute.
1204  *
1205  * Get the #AtkTextAttribute type corresponding to a text attribute name.
1206  *
1207  * Returns: the #AtkTextAttribute enumerated type corresponding to the specified
1208 name,
1209  *          or #ATK_TEXT_ATTRIBUTE_INVALID if no matching text attribute is found.
1210  **/
1211 AtkTextAttribute
1212 atk_text_attribute_for_name (const gchar *name)
1213 {
1214   GTypeClass *type_class;
1215   GEnumValue *value;
1216   AtkTextAttribute type = ATK_TEXT_ATTR_INVALID;
1217
1218   g_return_val_if_fail (name, ATK_TEXT_ATTR_INVALID);
1219
1220   type_class = g_type_class_ref (ATK_TYPE_TEXT_ATTRIBUTE);
1221   g_return_val_if_fail (G_IS_ENUM_CLASS (type_class), ATK_TEXT_ATTR_INVALID);
1222
1223   value = g_enum_get_value_by_nick (G_ENUM_CLASS (type_class), name);
1224
1225   if (value)
1226     {
1227       type = value->value;
1228     }
1229   else
1230     {
1231       gint i;
1232
1233       if (extra_attributes)
1234         {
1235           for (i = 0; i < extra_attributes->len; i++)
1236             {
1237               gchar *extra_attribute = (gchar *)g_ptr_array_index (extra_attributes, i);
1238
1239               g_return_val_if_fail (extra_attribute, ATK_TEXT_ATTR_INVALID);
1240
1241               if (strcmp (name, extra_attribute) == 0)
1242                 {
1243                   type = i + 1 + ATK_TEXT_ATTR_LAST_DEFINED;
1244                   break;
1245                 }
1246             }
1247         }
1248     }
1249   g_type_class_unref (type_class);
1250
1251   return type;
1252 }
1253
1254 /**
1255  * atk_text_attribute_get_value:
1256  * @attr: The #AtkTextAttribute for which a value is required
1257  * @index_: The index of the required value
1258  *
1259  * Gets the value for the index of the #AtkTextAttribute
1260  *
1261  * Returns: a string containing the value; this string should not be freed;
1262  * NULL is returned if there are no values maintained for the attr value. 
1263  **/
1264 const gchar*
1265 atk_text_attribute_get_value (AtkTextAttribute attr,
1266                               gint             index)
1267 {
1268   switch (attr)
1269     {
1270     case ATK_TEXT_ATTR_INVISIBLE:
1271     case ATK_TEXT_ATTR_EDITABLE:
1272     case ATK_TEXT_ATTR_BG_FULL_HEIGHT:
1273     case ATK_TEXT_ATTR_STRIKETHROUGH:
1274     case ATK_TEXT_ATTR_BG_STIPPLE:
1275     case ATK_TEXT_ATTR_FG_STIPPLE:
1276       g_assert (index >= 0 && index < G_N_ELEMENTS (boolean_offsets));
1277       return boolean + boolean_offsets[index];
1278     case ATK_TEXT_ATTR_UNDERLINE:
1279       g_assert (index >= 0 && index < G_N_ELEMENTS (underline_offsets));
1280       return underline + underline_offsets[index];
1281     case ATK_TEXT_ATTR_WRAP_MODE:
1282       g_assert (index >= 0 && index < G_N_ELEMENTS (wrap_mode_offsets));
1283       return wrap_mode + wrap_mode_offsets[index];
1284     case ATK_TEXT_ATTR_DIRECTION:
1285       g_assert (index >= 0 && index < G_N_ELEMENTS (direction_offsets));
1286       return direction + direction_offsets[index];
1287     case ATK_TEXT_ATTR_JUSTIFICATION:
1288       g_assert (index >= 0 && index < G_N_ELEMENTS (justification_offsets));
1289       return justification + justification_offsets[index];
1290     case ATK_TEXT_ATTR_STRETCH:
1291       g_assert (index >= 0 && index < G_N_ELEMENTS (stretch_offsets));
1292       return stretch + stretch_offsets[index];
1293     case ATK_TEXT_ATTR_VARIANT:
1294       g_assert (index >= 0 && index < G_N_ELEMENTS (variant_offsets));
1295       return variant + variant_offsets[index];
1296     case ATK_TEXT_ATTR_STYLE:
1297       g_assert (index >= 0 && index < G_N_ELEMENTS (style_offsets));
1298       return style + style_offsets[index];
1299     default:
1300       return NULL;
1301    }
1302 }
1303
1304 static void
1305 atk_text_rectangle_union (AtkTextRectangle *src1,
1306                           AtkTextRectangle *src2,
1307                           AtkTextRectangle *dest)
1308 {
1309   gint dest_x, dest_y;
1310
1311   dest_x = MIN (src1->x, src2->x);
1312   dest_y = MIN (src1->y, src2->y);
1313   dest->width = MAX (src1->x + src1->width, src2->x + src2->width) - dest_x;
1314   dest->height = MAX (src1->y + src1->height, src2->y + src2->height) - dest_y;
1315   dest->x = dest_x;
1316   dest->y = dest_y;
1317 }
1318
1319 static gboolean
1320 atk_text_rectangle_contain (AtkTextRectangle *clip,
1321                             AtkTextRectangle *bounds,
1322                             AtkTextClipType  x_clip_type,
1323                             AtkTextClipType  y_clip_type)
1324 {
1325   gboolean x_min_ok, x_max_ok, y_min_ok, y_max_ok;
1326
1327   x_min_ok = (bounds->x >= clip->x) ||
1328              ((bounds->x + bounds->width >= clip->x) &&
1329               ((x_clip_type == ATK_TEXT_CLIP_NONE) ||
1330                (x_clip_type == ATK_TEXT_CLIP_MAX)));
1331
1332   x_max_ok = (bounds->x + bounds->width <= clip->x + clip->width) ||
1333              ((bounds->x <= clip->x + clip->width) &&
1334               ((x_clip_type == ATK_TEXT_CLIP_NONE) ||
1335                (x_clip_type == ATK_TEXT_CLIP_MIN)));
1336
1337   y_min_ok = (bounds->y >= clip->y) ||
1338              ((bounds->y + bounds->height >= clip->y) &&
1339               ((y_clip_type == ATK_TEXT_CLIP_NONE) ||
1340                (y_clip_type == ATK_TEXT_CLIP_MAX)));
1341
1342   y_max_ok = (bounds->y + bounds->height <= clip->y + clip->height) ||
1343              ((bounds->y <= clip->y + clip->height) &&
1344               ((y_clip_type == ATK_TEXT_CLIP_NONE) ||
1345                (y_clip_type == ATK_TEXT_CLIP_MIN)));
1346
1347   return (x_min_ok && x_max_ok && y_min_ok && y_max_ok);
1348   
1349 }
1350
1351 static void 
1352 atk_text_real_get_range_extents (AtkText           *text,
1353                                  gint              start_offset,
1354                                  gint              end_offset,
1355                                  AtkCoordType      coord_type,
1356                                  AtkTextRectangle  *rect)
1357 {
1358   gint i;
1359   AtkTextRectangle cbounds, bounds;
1360
1361   atk_text_get_character_extents (text, start_offset,
1362                                   &bounds.x, &bounds.y,
1363                                   &bounds.width, &bounds.height,
1364                                   coord_type);
1365
1366   for (i = start_offset + 1; i < end_offset; i++)
1367     {
1368       atk_text_get_character_extents (text, i,
1369                                       &cbounds.x, &cbounds.y, 
1370                                       &cbounds.width, &cbounds.height, 
1371                                       coord_type);
1372       atk_text_rectangle_union (&bounds, &cbounds, &bounds);
1373     }
1374
1375   rect->x = bounds.x;
1376   rect->y = bounds.y;
1377   rect->width = bounds.width;
1378   rect->height = bounds.height;
1379 }
1380
1381 static AtkTextRange**
1382 atk_text_real_get_bounded_ranges (AtkText          *text,
1383                                   AtkTextRectangle *rect,
1384                                   AtkCoordType     coord_type,
1385                                   AtkTextClipType  x_clip_type,
1386                                   AtkTextClipType  y_clip_type)
1387 {
1388   gint bounds_min_offset, bounds_max_offset;
1389   gint min_line_start, min_line_end;
1390   gint max_line_start, max_line_end;
1391   gchar *line;
1392   gint curr_offset;
1393   gint offset;
1394   gint num_ranges = 0;
1395   gint range_size = 1;
1396   AtkTextRectangle cbounds;
1397   AtkTextRange **range;
1398
1399   range = NULL;
1400   bounds_min_offset = atk_text_get_offset_at_point (text, rect->x, rect->y, coord_type);
1401   bounds_max_offset = atk_text_get_offset_at_point (text, rect->x + rect->width, rect->y + rect->height, coord_type);
1402
1403   if (bounds_min_offset == 0 &&
1404       bounds_min_offset == bounds_max_offset)
1405     return NULL;
1406
1407   line = atk_text_get_text_at_offset (text, bounds_min_offset, 
1408                                       ATK_TEXT_BOUNDARY_LINE_START,
1409                                       &min_line_start, &min_line_end);
1410   g_free (line);
1411   line = atk_text_get_text_at_offset (text, bounds_max_offset, 
1412                                       ATK_TEXT_BOUNDARY_LINE_START,
1413                                       &max_line_start, &max_line_end);
1414   g_free (line);
1415   bounds_min_offset = MIN (min_line_start, max_line_start);
1416   bounds_max_offset = MAX (min_line_end, max_line_end);
1417
1418   curr_offset = bounds_min_offset;
1419   while (curr_offset < bounds_max_offset)
1420     {
1421       offset = curr_offset;
1422
1423       while (curr_offset < bounds_max_offset)
1424         {
1425           atk_text_get_character_extents (text, curr_offset,
1426                                           &cbounds.x, &cbounds.y,
1427                                           &cbounds.width, &cbounds.height,
1428                                           coord_type);
1429           if (!atk_text_rectangle_contain (rect, &cbounds, x_clip_type, y_clip_type))
1430             break;
1431           curr_offset++;
1432         }
1433       if (curr_offset > offset)
1434         {
1435           AtkTextRange *one_range = g_new (AtkTextRange, 1);
1436
1437           one_range->start_offset = offset;
1438           one_range->end_offset = curr_offset;
1439           one_range->content = atk_text_get_text (text, offset, curr_offset);
1440           atk_text_get_range_extents (text, offset, curr_offset, coord_type, &one_range->bounds);
1441
1442           if (num_ranges >= range_size - 1)
1443             {
1444               range_size *= 2;
1445               range = g_realloc (range, range_size * sizeof (gpointer));
1446             }
1447           range[num_ranges] = one_range;
1448           num_ranges++; 
1449         }   
1450       curr_offset++;
1451       if (range)
1452         range[num_ranges] = NULL;
1453     }
1454   return range;
1455 }
1456
1457 /**
1458  * atk_text_free_ranges:
1459  * @ranges: (array): A pointer to an array of #AtkTextRange which is
1460  *   to be freed.
1461  *
1462  * Frees the memory associated with an array of AtkTextRange. It is assumed
1463  * that the array was returned by the function atk_text_get_bounded_ranges
1464  * and is NULL terminated.
1465  *
1466  * Since: 1.3
1467  **/
1468 void
1469 atk_text_free_ranges (AtkTextRange **ranges)
1470 {
1471   AtkTextRange **first = ranges;
1472
1473   if (ranges)
1474     {
1475       while (*ranges)
1476         {
1477           AtkTextRange *range;
1478
1479           range = *ranges;
1480           ranges++;
1481           g_free (range->content);
1482           g_free (range);
1483         }
1484       g_free (first);
1485     }
1486 }
1487
1488 static AtkTextRange *
1489 atk_text_range_copy (AtkTextRange *src)
1490 {
1491   AtkTextRange *dst = g_new0 (AtkTextRange, 1);
1492   dst->bounds = src->bounds;
1493   dst->start_offset = src->start_offset;
1494   dst->end_offset = src->end_offset;
1495   if (src->content)
1496     dst->content = g_strdup (src->content);
1497   return dst;
1498 }
1499
1500 static void
1501 atk_text_range_free (AtkTextRange *range)
1502 {
1503   g_free (range->content);
1504   g_free (range);
1505 }
1506
1507 G_DEFINE_BOXED_TYPE (AtkTextRange, atk_text_range, atk_text_range_copy,
1508                      atk_text_range_free)