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