d55f5dedd01511eafd3a600a7dcd931f5afa72ba
[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_string_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  * Deprecated: This method is deprecated since ATK version
454  * 2.9.4. Please use atk_text_get_string_at_offset() instead.
455  *
456  * Returns: a newly allocated string containing the text at @offset bounded by
457  *   the specified @boundary_type. Use g_free() to free the returned string.
458  **/
459 gchar*
460 atk_text_get_text_at_offset (AtkText          *text,
461                              gint             offset,
462                              AtkTextBoundary  boundary_type,
463                              gint             *start_offset,
464                              gint             *end_offset)
465 {
466   AtkTextIface *iface;
467   gint local_start_offset, local_end_offset;
468   gint *real_start_offset, *real_end_offset;
469
470   g_return_val_if_fail (ATK_IS_TEXT (text), NULL);
471
472   if (start_offset)
473     real_start_offset = start_offset;
474   else
475     real_start_offset = &local_start_offset;
476   if (end_offset)
477     real_end_offset = end_offset;
478   else
479     real_end_offset = &local_end_offset;
480
481   iface = ATK_TEXT_GET_IFACE (text);
482
483   if (iface->get_text_at_offset)
484     return (*(iface->get_text_at_offset)) (text, offset, boundary_type, real_start_offset, real_end_offset);
485   else
486     return NULL;
487 }
488
489 /**
490  * atk_text_get_text_before_offset:
491  * @text: an #AtkText
492  * @offset: position
493  * @boundary_type: An #AtkTextBoundary
494  * @start_offset: (out): the start offset of the returned string
495  * @end_offset: (out): the offset of the first character after the
496  *              returned substring
497  *
498  * Gets the specified text.
499  *
500  * Deprecated: This method is deprecated since ATK version
501  * 2.9.3. Please use atk_text_get_string_at_offset() instead.
502  *
503  * Returns: a newly allocated string containing the text before @offset bounded
504  *   by the specified @boundary_type. Use g_free() to free the returned string.
505  **/
506 gchar*
507 atk_text_get_text_before_offset (AtkText          *text,
508                                  gint             offset,
509                                  AtkTextBoundary  boundary_type,
510                                  gint             *start_offset,
511                                  gint             *end_offset)
512 {
513   AtkTextIface *iface;
514   gint local_start_offset, local_end_offset;
515   gint *real_start_offset, *real_end_offset;
516
517   g_return_val_if_fail (ATK_IS_TEXT (text), NULL);
518
519   if (start_offset)
520     real_start_offset = start_offset;
521   else
522     real_start_offset = &local_start_offset;
523   if (end_offset)
524     real_end_offset = end_offset;
525   else
526     real_end_offset = &local_end_offset;
527
528   if (offset < 0)
529     return NULL;
530
531   iface = ATK_TEXT_GET_IFACE (text);
532
533   if (iface->get_text_before_offset)
534     return (*(iface->get_text_before_offset)) (text, offset, boundary_type, real_start_offset, real_end_offset);
535   else
536     return NULL;
537 }
538
539 /**
540  * atk_text_get_string_at_offset:
541  * @text: an #AtkText
542  * @offset: position
543  * @granularity: An #AtkTextGranularity
544  * @start_offset: (out): the start offset of the returned string, or -1
545  *                if an error has occurred (e.g. invalid offset, not implemented)
546  * @end_offset: (out): the offset of the first character after the returned string,
547  *              or -1 if an error has occurred (e.g. invalid offset, not implemented)
548  *
549  * Gets a portion of the text exposed through an #AtkText according to a given @offset
550  * and a specific @granularity, along with the start and end offsets defining the
551  * boundaries of such a portion of text.
552  *
553  * If @granularity is ATK_TEXT_GRANULARITY_CHAR the character at the
554  * offset is returned.
555  *
556  * If @granularity is ATK_TEXT_GRANULARITY_WORD the returned string
557  * is from the word start at or before the offset to the word start after
558  * the offset.
559  *
560  * The returned string will contain the word at the offset if the offset
561  * is inside a word and will contain the word before the offset if the
562  * offset is not inside a word.
563  *
564  * If @granularity is ATK_TEXT_GRANULARITY_SENTENCE the returned string
565  * is from the sentence start at or before the offset to the sentence
566  * start after the offset.
567  *
568  * The returned string will contain the sentence at the offset if the offset
569  * is inside a sentence and will contain the sentence before the offset
570  * if the offset is not inside a sentence.
571  *
572  * If @granularity is ATK_TEXT_GRANULARITY_LINE the returned string
573  * is from the line start at or before the offset to the line
574  * start after the offset.
575  *
576  * If @granularity is ATK_TEXT_GRANULARITY_PARAGRAPH the returned string
577  * is from the start of the paragraph at or before the offset to the start
578  * of the following paragraph after the offset.
579  *
580  * Since: 2.9.4
581  *
582  * Returns: a newly allocated string containing the text at the @offset bounded
583  *   by the specified @granularity. Use g_free() to free the returned string.
584  *   Returns %NULL if the offset is invalid or no implementation is available.
585  **/
586 gchar* atk_text_get_string_at_offset (AtkText *text,
587                                       gint offset,
588                                       AtkTextGranularity granularity,
589                                       gint *start_offset,
590                                       gint *end_offset)
591 {
592   AtkTextIface *iface;
593   gint local_start_offset, local_end_offset;
594   gint *real_start_offset, *real_end_offset;
595
596   g_return_val_if_fail (ATK_IS_TEXT (text), NULL);
597
598   if (start_offset)
599     {
600       *start_offset = -1;
601       real_start_offset = start_offset;
602     }
603   else
604     real_start_offset = &local_start_offset;
605
606   if (end_offset)
607     {
608       *end_offset = -1;
609       real_end_offset = end_offset;
610     }
611   else
612     real_end_offset = &local_end_offset;
613
614   if (offset < 0)
615     return NULL;
616
617   iface = ATK_TEXT_GET_IFACE (text);
618
619   if (iface->get_string_at_offset)
620     return (*(iface->get_string_at_offset)) (text, offset, granularity, real_start_offset, real_end_offset);
621   else
622     return NULL;
623 }
624
625 /**
626  * atk_text_get_caret_offset:
627  * @text: an #AtkText
628  *
629  * Gets the offset position of the caret (cursor).
630  *
631  * Returns: the offset position of the caret (cursor).
632  **/
633 gint
634 atk_text_get_caret_offset (AtkText *text)
635 {
636   AtkTextIface *iface;
637
638   g_return_val_if_fail (ATK_IS_TEXT (text), 0);
639
640   iface = ATK_TEXT_GET_IFACE (text);
641
642   if (iface->get_caret_offset)
643     return (*(iface->get_caret_offset)) (text);
644   else
645     return 0;
646 }
647
648 /**
649  * atk_text_get_character_extents:
650  * @text: an #AtkText
651  * @offset: The offset of the text character for which bounding information is required.
652  * @x: Pointer for the x cordinate of the bounding box
653  * @y: Pointer for the y cordinate of the bounding box
654  * @width: Pointer for the width of the bounding box
655  * @height: Pointer for the height of the bounding box
656  * @coords: specify whether coordinates are relative to the screen or widget window 
657  *
658  * Get the bounding box containing the glyph representing the character at 
659  *     a particular text offset. 
660  **/
661 void
662 atk_text_get_character_extents (AtkText *text,
663                                 gint offset,
664                                 gint *x,
665                                 gint *y,
666                                 gint *width,
667                                 gint *height,
668                                 AtkCoordType coords)
669 {
670   AtkTextIface *iface;
671   gint local_x, local_y, local_width, local_height;
672   gint *real_x, *real_y, *real_width, *real_height;
673
674   g_return_if_fail (ATK_IS_TEXT (text));
675
676   if (x)
677     real_x = x;
678   else
679     real_x = &local_x;
680   if (y)
681     real_y = y;
682   else
683     real_y = &local_y;
684   if (width)
685     real_width = width;
686   else
687     real_width = &local_width;
688   if (height)
689     real_height = height;
690   else
691     real_height = &local_height;
692
693   *real_x = 0;
694   *real_y = 0;
695   *real_width = 0;
696   *real_height = 0;
697
698   if (offset < 0)
699     return;
700  
701   iface = ATK_TEXT_GET_IFACE (text);
702
703   if (iface->get_character_extents)
704     (*(iface->get_character_extents)) (text, offset, real_x, real_y, real_width, real_height, coords);
705
706   if (*real_width <0)
707     {
708       *real_x = *real_x + *real_width;
709       *real_width *= -1;
710     }
711 }
712
713 /**
714  * atk_text_get_run_attributes:
715  *@text: an #AtkText
716  *@offset: the offset at which to get the attributes, -1 means the offset of
717  *the character to be inserted at the caret location.
718  *@start_offset: (out): the address to put the start offset of the range
719  *@end_offset: (out): the address to put the end offset of the range
720  *
721  *Creates an #AtkAttributeSet which consists of the attributes explicitly
722  *set at the position @offset in the text. @start_offset and @end_offset are
723  *set to the start and end of the range around @offset where the attributes are
724  *invariant. Note that @end_offset is the offset of the first character
725  *after the range.  See the enum AtkTextAttribute for types of text 
726  *attributes that can be returned. Note that other attributes may also be 
727  *returned.
728  *
729  *Returns: (transfer full): an #AtkAttributeSet which contains the attributes
730  * explicitly set at @offset. This #AtkAttributeSet should be freed by a call
731  * to atk_attribute_set_free().
732  **/
733 AtkAttributeSet* 
734 atk_text_get_run_attributes (AtkText          *text,
735                              gint             offset,
736                              gint             *start_offset,
737                              gint             *end_offset)
738 {
739   AtkTextIface *iface;
740   gint local_start_offset, local_end_offset;
741   gint *real_start_offset, *real_end_offset;
742
743   g_return_val_if_fail (ATK_IS_TEXT (text), NULL);
744
745   if (start_offset)
746     real_start_offset = start_offset;
747   else
748     real_start_offset = &local_start_offset;
749   if (end_offset)
750     real_end_offset = end_offset;
751   else
752     real_end_offset = &local_end_offset;
753
754   if (offset < -1)
755     return NULL;
756
757   iface = ATK_TEXT_GET_IFACE (text);
758
759   if (iface->get_run_attributes)
760     return (*(iface->get_run_attributes)) (text, offset, real_start_offset, real_end_offset);
761   else
762     return NULL;
763 }
764
765 /**
766  * atk_text_get_default_attributes:
767  *@text: an #AtkText
768  *
769  *Creates an #AtkAttributeSet which consists of the default values of
770  *attributes for the text. See the enum AtkTextAttribute for types of text 
771  *attributes that can be returned. Note that other attributes may also be 
772  *returned.
773  *
774  *Returns: (transfer full): an #AtkAttributeSet which contains the default
775  * values of attributes.  at @offset. this #atkattributeset should be freed by
776  * a call to atk_attribute_set_free().
777  */
778 AtkAttributeSet* 
779 atk_text_get_default_attributes (AtkText          *text)
780 {
781   AtkTextIface *iface;
782
783   g_return_val_if_fail (ATK_IS_TEXT (text), NULL);
784
785   iface = ATK_TEXT_GET_IFACE (text);
786
787   if (iface->get_default_attributes)
788     return (*(iface->get_default_attributes)) (text);
789   else
790     return NULL;
791 }
792
793 /**
794  * atk_text_get_character_count:
795  * @text: an #AtkText
796  *
797  * Gets the character count.
798  *
799  * Returns: the number of characters.
800  **/
801 gint
802 atk_text_get_character_count (AtkText *text)
803 {
804   AtkTextIface *iface;
805
806   g_return_val_if_fail (ATK_IS_TEXT (text), -1);
807
808   iface = ATK_TEXT_GET_IFACE (text);
809
810   if (iface->get_character_count)
811     return (*(iface->get_character_count)) (text);
812   else
813     return -1;
814 }
815
816 /**
817  * atk_text_get_offset_at_point:
818  * @text: an #AtkText
819  * @x: screen x-position of character
820  * @y: screen y-position of character
821  * @coords: specify whether coordinates are relative to the screen or
822  * widget window 
823  *
824  * Gets the offset of the character located at coordinates @x and @y. @x and @y
825  * are interpreted as being relative to the screen or this widget's window
826  * depending on @coords.
827  *
828  * Returns: the offset to the character which is located at
829  * the specified @x and @y coordinates.
830  **/
831 gint
832 atk_text_get_offset_at_point (AtkText *text,
833                               gint x,
834                               gint y,
835                               AtkCoordType coords)
836 {
837   AtkTextIface *iface;
838
839   g_return_val_if_fail (ATK_IS_TEXT (text), -1);
840
841   iface = ATK_TEXT_GET_IFACE (text);
842
843   if (iface->get_offset_at_point)
844     return (*(iface->get_offset_at_point)) (text, x, y, coords);
845   else
846     return -1;
847 }
848
849 /**
850  * atk_text_get_n_selections:
851  * @text: an #AtkText
852  *
853  * Gets the number of selected regions.
854  *
855  * Returns: The number of selected regions, or -1 if a failure
856  *   occurred.
857  **/
858 gint
859 atk_text_get_n_selections (AtkText *text)
860 {
861   AtkTextIface *iface;
862
863   g_return_val_if_fail (ATK_IS_TEXT (text), -1);
864
865   iface = ATK_TEXT_GET_IFACE (text);
866
867   if (iface->get_n_selections)
868     return (*(iface->get_n_selections)) (text);
869   else
870     return -1;
871 }
872
873 /**
874  * atk_text_get_selection:
875  * @text: an #AtkText
876  * @selection_num: The selection number.  The selected regions are
877  * assigned numbers that correspond to how far the region is from the
878  * start of the text.  The selected region closest to the beginning
879  * of the text region is assigned the number 0, etc.  Note that adding,
880  * moving or deleting a selected region can change the numbering.
881  * @start_offset: (out): passes back the start position of the selected region
882  * @end_offset: (out): passes back the end position of (e.g. offset immediately past)
883  * the selected region
884  *
885  * Gets the text from the specified selection.
886  *
887  * Returns: a newly allocated string containing the selected text. Use g_free()
888  *   to free the returned string.
889  **/
890 gchar*
891 atk_text_get_selection (AtkText *text, 
892                         gint    selection_num,
893                         gint    *start_offset,
894                         gint    *end_offset)
895 {
896   AtkTextIface *iface;
897   gint local_start_offset, local_end_offset;
898   gint *real_start_offset, *real_end_offset;
899
900   g_return_val_if_fail (ATK_IS_TEXT (text), NULL);
901
902   if (start_offset)
903     real_start_offset = start_offset;
904   else
905     real_start_offset = &local_start_offset;
906   if (end_offset)
907     real_end_offset = end_offset;
908   else
909     real_end_offset = &local_end_offset;
910
911   iface = ATK_TEXT_GET_IFACE (text);
912
913   if (iface->get_selection)
914   {
915     return (*(iface->get_selection)) (text, selection_num,
916        real_start_offset, real_end_offset);
917   }
918   else
919     return NULL;
920 }
921
922 /**
923  * atk_text_add_selection:
924  * @text: an #AtkText
925  * @start_offset: the start position of the selected region
926  * @end_offset: the offset of the first character after the selected region.
927  *
928  * Adds a selection bounded by the specified offsets.
929  *
930  * Returns: %TRUE if success, %FALSE otherwise
931  **/
932 gboolean
933 atk_text_add_selection (AtkText *text, 
934                         gint    start_offset,
935                         gint    end_offset)
936 {
937   AtkTextIface *iface;
938
939   g_return_val_if_fail (ATK_IS_TEXT (text), FALSE);
940
941   iface = ATK_TEXT_GET_IFACE (text);
942
943   if (iface->add_selection)
944     return (*(iface->add_selection)) (text, start_offset, end_offset);
945   else
946     return FALSE;
947 }
948
949 /**
950  * atk_text_remove_selection:
951  * @text: an #AtkText
952  * @selection_num: The selection number.  The selected regions are
953  * assigned numbers that correspond to how far the region is from the
954  * start of the text.  The selected region closest to the beginning
955  * of the text region is assigned the number 0, etc.  Note that adding,
956  * moving or deleting a selected region can change the numbering.
957  *
958  * Removes the specified selection.
959  *
960  * Returns: %TRUE if success, %FALSE otherwise
961  **/
962 gboolean
963 atk_text_remove_selection (AtkText *text, 
964                            gint    selection_num)
965 {
966   AtkTextIface *iface;
967
968   g_return_val_if_fail (ATK_IS_TEXT (text), FALSE);
969
970   iface = ATK_TEXT_GET_IFACE (text);
971
972   if (iface->remove_selection)
973     return (*(iface->remove_selection)) (text, selection_num);
974   else
975     return FALSE;
976 }
977
978 /**
979  * atk_text_set_selection:
980  * @text: an #AtkText
981  * @selection_num: The selection number.  The selected regions are
982  * assigned numbers that correspond to how far the region is from the
983  * start of the text.  The selected region closest to the beginning
984  * of the text region is assigned the number 0, etc.  Note that adding,
985  * moving or deleting a selected region can change the numbering.
986  * @start_offset: the new start position of the selection
987  * @end_offset: the new end position of (e.g. offset immediately past) 
988  * the selection
989  *
990  * Changes the start and end offset of the specified selection.
991  *
992  * Returns: %TRUE if success, %FALSE otherwise
993  **/
994 gboolean
995 atk_text_set_selection (AtkText *text, 
996                         gint    selection_num,
997                         gint    start_offset, 
998                         gint    end_offset)
999 {
1000   AtkTextIface *iface;
1001
1002   g_return_val_if_fail (ATK_IS_TEXT (text), FALSE);
1003
1004   iface = ATK_TEXT_GET_IFACE (text);
1005
1006   if (iface->set_selection)
1007   {
1008     return (*(iface->set_selection)) (text, selection_num,
1009        start_offset, end_offset);
1010   }
1011   else
1012     return FALSE;
1013 }
1014
1015 /**
1016  * atk_text_set_caret_offset:
1017  * @text: an #AtkText
1018  * @offset: position
1019  *
1020  * Sets the caret (cursor) position to the specified @offset.
1021  *
1022  * Returns: %TRUE if success, %FALSE otherwise.
1023  **/
1024 gboolean
1025 atk_text_set_caret_offset (AtkText *text,
1026                            gint    offset)
1027 {
1028   AtkTextIface *iface;
1029
1030   g_return_val_if_fail (ATK_IS_TEXT (text), FALSE);
1031
1032   iface = ATK_TEXT_GET_IFACE (text);
1033
1034   if (iface->set_caret_offset)
1035     {
1036       return (*(iface->set_caret_offset)) (text, offset);
1037     }
1038   else
1039     {
1040       return FALSE;
1041     }
1042 }
1043
1044 /**
1045  * atk_text_get_range_extents:
1046  * @text: an #AtkText
1047  * @start_offset: The offset of the first text character for which boundary 
1048  *        information is required.
1049  * @end_offset: The offset of the text character after the last character 
1050  *        for which boundary information is required.
1051  * @coord_type: Specify whether coordinates are relative to the screen or widget window.
1052  * @rect: A pointer to a AtkTextRectangle which is filled in by this function.
1053  *
1054  * Get the bounding box for text within the specified range.
1055  *
1056  * Since: 1.3
1057  **/
1058 void
1059 atk_text_get_range_extents (AtkText          *text,
1060                             gint             start_offset,
1061                             gint             end_offset,
1062                             AtkCoordType     coord_type,
1063                             AtkTextRectangle *rect)
1064 {
1065   AtkTextIface *iface;
1066
1067   g_return_if_fail (ATK_IS_TEXT (text));
1068   g_return_if_fail (rect);
1069
1070   if (start_offset < 0 || start_offset >= end_offset)
1071     return;
1072  
1073   iface = ATK_TEXT_GET_IFACE (text);
1074
1075   if (iface->get_range_extents)
1076     (*(iface->get_range_extents)) (text, start_offset, end_offset, coord_type, rect);
1077 }
1078
1079 /**
1080  * atk_text_get_bounded_ranges:
1081  * @text: an #AtkText
1082  * @rect: An AtkTextRectangle giving the dimensions of the bounding box.
1083  * @coord_type: Specify whether coordinates are relative to the screen or widget window.
1084  * @x_clip_type: Specify the horizontal clip type.
1085  * @y_clip_type: Specify the vertical clip type.
1086  *
1087  * Get the ranges of text in the specified bounding box.
1088  *
1089  * Since: 1.3
1090  *
1091  * Returns: (array zero-terminated=1): Array of AtkTextRange. The last
1092  *          element of the array returned by this function will be NULL.
1093  * Virtual: get_bounded_ranges
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)