2.30.0
[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: a starting character offset within @text
321  * @end_offset: an ending character offset within @text, 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
327  *          string.
328  **/
329 gchar*
330 atk_text_get_text (AtkText      *text,
331                    gint         start_offset,
332                    gint         end_offset)
333 {
334   AtkTextIface *iface;
335   
336   g_return_val_if_fail (ATK_IS_TEXT (text), NULL);
337
338   iface = ATK_TEXT_GET_IFACE (text);
339
340   if (start_offset < 0 || end_offset < -1 ||
341       (end_offset != -1 && end_offset < start_offset))
342     return NULL;
343
344   if (iface->get_text)
345     return (*(iface->get_text)) (text, start_offset, end_offset);
346   else
347     return NULL;
348 }
349
350 /**
351  * atk_text_get_character_at_offset:
352  * @text: an #AtkText
353  * @offset: a character offset within @text
354  *
355  * Gets the specified text.
356  *
357  * Returns: the character at @offset or 0 in the case of failure.
358  **/
359 gunichar
360 atk_text_get_character_at_offset (AtkText      *text,
361                                   gint         offset)
362 {
363   AtkTextIface *iface;
364
365   g_return_val_if_fail (ATK_IS_TEXT (text), (gunichar) 0);
366
367   iface = ATK_TEXT_GET_IFACE (text);
368
369   if (iface->get_character_at_offset)
370     return (*(iface->get_character_at_offset)) (text, offset);
371   else
372     return (gunichar) 0;
373 }
374
375 /**
376  * atk_text_get_text_after_offset:
377  * @text: an #AtkText
378  * @offset: position
379  * @boundary_type: An #AtkTextBoundary
380  * @start_offset: (out): the starting character offset of the returned string
381  * @end_offset: (out): the offset of the first character after the
382  *              returned substring
383  *
384  * Gets the specified text.
385  *
386  * Deprecated: 2.9.3: Please use atk_text_get_string_at_offset() instead.
387  *
388  * Returns: a newly allocated string containing the text after @offset bounded
389  *          by the specified @boundary_type. Use g_free() to free the returned
390  *          string.
391  **/
392 gchar*
393 atk_text_get_text_after_offset (AtkText          *text,
394                                 gint             offset,
395                                 AtkTextBoundary  boundary_type,
396                                 gint             *start_offset,
397                                 gint             *end_offset)
398 {
399   AtkTextIface *iface;
400   gint local_start_offset, local_end_offset;
401   gint *real_start_offset, *real_end_offset;
402
403   g_return_val_if_fail (ATK_IS_TEXT (text), NULL);
404
405   if (start_offset)
406     real_start_offset = start_offset;
407   else
408     real_start_offset = &local_start_offset;
409   if (end_offset)
410     real_end_offset = end_offset;
411   else
412     real_end_offset = &local_end_offset;
413
414   if (offset < 0)
415     return NULL;
416
417   iface = ATK_TEXT_GET_IFACE (text);
418
419   if (iface->get_text_after_offset)
420     return (*(iface->get_text_after_offset)) (text, offset, boundary_type, real_start_offset, real_end_offset);
421   else
422     return NULL;
423 }
424
425 /**
426  * atk_text_get_text_at_offset:
427  * @text: an #AtkText
428  * @offset: position
429  * @boundary_type: An #AtkTextBoundary
430  * @start_offset: (out): the starting character offset of the returned string
431  * @end_offset: (out): the offset of the first character after the
432  *              returned substring
433  *
434  * Gets the specified text.
435  *
436  * If the boundary_type if ATK_TEXT_BOUNDARY_CHAR the character at the
437  * offset is returned.
438  *
439  * If the boundary_type is ATK_TEXT_BOUNDARY_WORD_START the returned string
440  * is from the word start at or before the offset to the word start after
441  * the offset.
442  *
443  * The returned string will contain the word at the offset if the offset
444  * is inside a word and will contain the word before the offset if the
445  * offset is not inside a word.
446  *
447  * If the boundary type is ATK_TEXT_BOUNDARY_SENTENCE_START the returned
448  * string is from the sentence start at or before the offset to the sentence
449  * start after the offset.
450  *
451  * The returned string will contain the sentence at the offset if the offset
452  * is inside a sentence and will contain the sentence before the offset
453  * if the offset is not inside a sentence.
454  *
455  * If the boundary type is ATK_TEXT_BOUNDARY_LINE_START the returned
456  * string is from the line start at or before the offset to the line
457  * start after the offset.
458  *
459  * Deprecated: This method is deprecated since ATK version
460  * 2.9.4. Please use atk_text_get_string_at_offset() instead.
461  *
462  * Returns: a newly allocated string containing the text at @offset bounded
463  *          by the specified @boundary_type. Use g_free() to free the returned
464  *          string.
465  **/
466 gchar*
467 atk_text_get_text_at_offset (AtkText          *text,
468                              gint             offset,
469                              AtkTextBoundary  boundary_type,
470                              gint             *start_offset,
471                              gint             *end_offset)
472 {
473   AtkTextIface *iface;
474   gint local_start_offset, local_end_offset;
475   gint *real_start_offset, *real_end_offset;
476
477   g_return_val_if_fail (ATK_IS_TEXT (text), NULL);
478
479   if (start_offset)
480     real_start_offset = start_offset;
481   else
482     real_start_offset = &local_start_offset;
483   if (end_offset)
484     real_end_offset = end_offset;
485   else
486     real_end_offset = &local_end_offset;
487
488   iface = ATK_TEXT_GET_IFACE (text);
489
490   if (iface->get_text_at_offset)
491     return (*(iface->get_text_at_offset)) (text, offset, boundary_type, real_start_offset, real_end_offset);
492   else
493     return NULL;
494 }
495
496 /**
497  * atk_text_get_text_before_offset:
498  * @text: an #AtkText
499  * @offset: position
500  * @boundary_type: An #AtkTextBoundary
501  * @start_offset: (out): the starting character offset of the returned string
502  * @end_offset: (out): the offset of the first character after the
503  *              returned substring
504  *
505  * Gets the specified text.
506  *
507  * Deprecated: 2.9.3: Please use atk_text_get_string_at_offset() instead.
508  *
509  * Returns: a newly allocated string containing the text before @offset bounded
510  *          by the specified @boundary_type. Use g_free() to free the returned
511  *          string.
512  **/
513 gchar*
514 atk_text_get_text_before_offset (AtkText          *text,
515                                  gint             offset,
516                                  AtkTextBoundary  boundary_type,
517                                  gint             *start_offset,
518                                  gint             *end_offset)
519 {
520   AtkTextIface *iface;
521   gint local_start_offset, local_end_offset;
522   gint *real_start_offset, *real_end_offset;
523
524   g_return_val_if_fail (ATK_IS_TEXT (text), NULL);
525
526   if (start_offset)
527     real_start_offset = start_offset;
528   else
529     real_start_offset = &local_start_offset;
530   if (end_offset)
531     real_end_offset = end_offset;
532   else
533     real_end_offset = &local_end_offset;
534
535   if (offset < 0)
536     return NULL;
537
538   iface = ATK_TEXT_GET_IFACE (text);
539
540   if (iface->get_text_before_offset)
541     return (*(iface->get_text_before_offset)) (text, offset, boundary_type, real_start_offset, real_end_offset);
542   else
543     return NULL;
544 }
545
546 /**
547  * atk_text_get_string_at_offset:
548  * @text: an #AtkText
549  * @offset: position
550  * @granularity: An #AtkTextGranularity
551  * @start_offset: (out): the starting character offset of the returned string, or -1
552  *                in the case of error (e.g. invalid offset, not implemented)
553  * @end_offset: (out): the offset of the first character after the returned string,
554  *              or -1 in the case of error (e.g. invalid offset, not implemented)
555  *
556  * Gets a portion of the text exposed through an #AtkText according to a given @offset
557  * and a specific @granularity, along with the start and end offsets defining the
558  * boundaries of such a portion of text.
559  *
560  * If @granularity is ATK_TEXT_GRANULARITY_CHAR the character at the
561  * offset is returned.
562  *
563  * If @granularity is ATK_TEXT_GRANULARITY_WORD the returned string
564  * is from the word start at or before the offset to the word start after
565  * the offset.
566  *
567  * The returned string will contain the word at the offset if the offset
568  * is inside a word and will contain the word before the offset if the
569  * offset is not inside a word.
570  *
571  * If @granularity is ATK_TEXT_GRANULARITY_SENTENCE the returned string
572  * is from the sentence start at or before the offset to the sentence
573  * start after the offset.
574  *
575  * The returned string will contain the sentence at the offset if the offset
576  * is inside a sentence and will contain the sentence before the offset
577  * if the offset is not inside a sentence.
578  *
579  * If @granularity is ATK_TEXT_GRANULARITY_LINE the returned string
580  * is from the line start at or before the offset to the line
581  * start after the offset.
582  *
583  * If @granularity is ATK_TEXT_GRANULARITY_PARAGRAPH the returned string
584  * is from the start of the paragraph at or before the offset to the start
585  * of the following paragraph after the offset.
586  *
587  * Since: 2.10
588  *
589  * Returns: (nullable): a newly allocated string containing the text at
590  *          the @offset bounded by the specified @granularity. Use g_free()
591  *          to free the returned string.  Returns %NULL if the offset is invalid
592  *          or no implementation is available.
593  **/
594 gchar* atk_text_get_string_at_offset (AtkText *text,
595                                       gint offset,
596                                       AtkTextGranularity granularity,
597                                       gint *start_offset,
598                                       gint *end_offset)
599 {
600   AtkTextIface *iface;
601   gint local_start_offset, local_end_offset;
602   gint *real_start_offset, *real_end_offset;
603
604   g_return_val_if_fail (ATK_IS_TEXT (text), NULL);
605
606   if (start_offset)
607     {
608       *start_offset = -1;
609       real_start_offset = start_offset;
610     }
611   else
612     real_start_offset = &local_start_offset;
613
614   if (end_offset)
615     {
616       *end_offset = -1;
617       real_end_offset = end_offset;
618     }
619   else
620     real_end_offset = &local_end_offset;
621
622   if (offset < 0)
623     return NULL;
624
625   iface = ATK_TEXT_GET_IFACE (text);
626
627   if (iface->get_string_at_offset)
628     return (*(iface->get_string_at_offset)) (text, offset, granularity, real_start_offset, real_end_offset);
629   else
630     return NULL;
631 }
632
633 /**
634  * atk_text_get_caret_offset:
635  * @text: an #AtkText
636  *
637  * Gets the offset of the position of the caret (cursor).
638  *
639  * Returns: the character offset of the position of the caret or 0  if
640  *          the caret is not located inside the element or in the case of
641  *          any other failure.
642  **/
643 gint
644 atk_text_get_caret_offset (AtkText *text)
645 {
646   AtkTextIface *iface;
647
648   g_return_val_if_fail (ATK_IS_TEXT (text), 0);
649
650   iface = ATK_TEXT_GET_IFACE (text);
651
652   if (iface->get_caret_offset)
653     return (*(iface->get_caret_offset)) (text);
654   else
655     return 0;
656 }
657
658 /**
659  * atk_text_get_character_extents:
660  * @text: an #AtkText
661  * @offset: The offset of the text character for which bounding information is required.
662  * @x: (out) (optional): Pointer for the x coordinate of the bounding box
663  * @y: (out) (optional): Pointer for the y coordinate of the bounding box
664  * @width: (out) (optional): Pointer for the width of the bounding box
665  * @height: (out) (optional): Pointer for the height of the bounding box
666  * @coords: specify whether coordinates are relative to the screen or widget window 
667  *
668  * Get the bounding box containing the glyph representing the character at 
669  *     a particular text offset. 
670  **/
671 void
672 atk_text_get_character_extents (AtkText *text,
673                                 gint offset,
674                                 gint *x,
675                                 gint *y,
676                                 gint *width,
677                                 gint *height,
678                                 AtkCoordType coords)
679 {
680   AtkTextIface *iface;
681   gint local_x, local_y, local_width, local_height;
682   gint *real_x, *real_y, *real_width, *real_height;
683
684   g_return_if_fail (ATK_IS_TEXT (text));
685
686   if (x)
687     real_x = x;
688   else
689     real_x = &local_x;
690   if (y)
691     real_y = y;
692   else
693     real_y = &local_y;
694   if (width)
695     real_width = width;
696   else
697     real_width = &local_width;
698   if (height)
699     real_height = height;
700   else
701     real_height = &local_height;
702
703   *real_x = 0;
704   *real_y = 0;
705   *real_width = 0;
706   *real_height = 0;
707
708   if (offset < 0)
709     return;
710  
711   iface = ATK_TEXT_GET_IFACE (text);
712
713   if (iface->get_character_extents)
714     (*(iface->get_character_extents)) (text, offset, real_x, real_y, real_width, real_height, coords);
715
716   if (*real_width <0)
717     {
718       *real_x = *real_x + *real_width;
719       *real_width *= -1;
720     }
721 }
722
723 /**
724  * atk_text_get_run_attributes:
725  *@text: an #AtkText
726  *@offset: the character offset at which to get the attributes, -1 means the offset of
727  *the character to be inserted at the caret location.
728  *@start_offset: (out): the address to put the start offset of the range
729  *@end_offset: (out): the address to put the end offset of the range
730  *
731  *Creates an #AtkAttributeSet which consists of the attributes explicitly
732  *set at the position @offset in the text. @start_offset and @end_offset are
733  *set to the start and end of the range around @offset where the attributes are
734  *invariant. Note that @end_offset is the offset of the first character
735  *after the range.  See the enum AtkTextAttribute for types of text 
736  *attributes that can be returned. Note that other attributes may also be 
737  *returned.
738  *
739  *Returns: (transfer full): an #AtkAttributeSet which contains the attributes
740  *         explicitly set at @offset. This #AtkAttributeSet should be freed by
741  *         a call to atk_attribute_set_free().
742  **/
743 AtkAttributeSet* 
744 atk_text_get_run_attributes (AtkText          *text,
745                              gint             offset,
746                              gint             *start_offset,
747                              gint             *end_offset)
748 {
749   AtkTextIface *iface;
750   gint local_start_offset, local_end_offset;
751   gint *real_start_offset, *real_end_offset;
752
753   g_return_val_if_fail (ATK_IS_TEXT (text), NULL);
754
755   if (start_offset)
756     real_start_offset = start_offset;
757   else
758     real_start_offset = &local_start_offset;
759   if (end_offset)
760     real_end_offset = end_offset;
761   else
762     real_end_offset = &local_end_offset;
763
764   if (offset < -1)
765     return NULL;
766
767   iface = ATK_TEXT_GET_IFACE (text);
768
769   if (iface->get_run_attributes)
770     return (*(iface->get_run_attributes)) (text, offset, real_start_offset, real_end_offset);
771   else
772     return NULL;
773 }
774
775 /**
776  * atk_text_get_default_attributes:
777  *@text: an #AtkText
778  *
779  *Creates an #AtkAttributeSet which consists of the default values of
780  *attributes for the text. See the enum AtkTextAttribute for types of text 
781  *attributes that can be returned. Note that other attributes may also be 
782  *returned.
783  *
784  *Returns: (transfer full): an #AtkAttributeSet which contains the default values
785  *          of attributes.  at @offset. this #atkattributeset should be freed by
786  *          a call to atk_attribute_set_free().
787  */
788 AtkAttributeSet* 
789 atk_text_get_default_attributes (AtkText          *text)
790 {
791   AtkTextIface *iface;
792
793   g_return_val_if_fail (ATK_IS_TEXT (text), NULL);
794
795   iface = ATK_TEXT_GET_IFACE (text);
796
797   if (iface->get_default_attributes)
798     return (*(iface->get_default_attributes)) (text);
799   else
800     return NULL;
801 }
802
803 /**
804  * atk_text_get_character_count:
805  * @text: an #AtkText
806  *
807  * Gets the character count.
808  *
809  * Returns: the number of characters or -1 in case of failure.
810  **/
811 gint
812 atk_text_get_character_count (AtkText *text)
813 {
814   AtkTextIface *iface;
815
816   g_return_val_if_fail (ATK_IS_TEXT (text), -1);
817
818   iface = ATK_TEXT_GET_IFACE (text);
819
820   if (iface->get_character_count)
821     return (*(iface->get_character_count)) (text);
822   else
823     return -1;
824 }
825
826 /**
827  * atk_text_get_offset_at_point:
828  * @text: an #AtkText
829  * @x: screen x-position of character
830  * @y: screen y-position of character
831  * @coords: specify whether coordinates are relative to the screen or
832  * widget window 
833  *
834  * Gets the offset of the character located at coordinates @x and @y. @x and @y
835  * are interpreted as being relative to the screen or this widget's window
836  * depending on @coords.
837  *
838  * Returns: the offset to the character which is located at  the specified
839  *          @x and @y coordinates of -1 in case of failure.
840  **/
841 gint
842 atk_text_get_offset_at_point (AtkText *text,
843                               gint x,
844                               gint y,
845                               AtkCoordType coords)
846 {
847   AtkTextIface *iface;
848
849   g_return_val_if_fail (ATK_IS_TEXT (text), -1);
850
851   iface = ATK_TEXT_GET_IFACE (text);
852
853   if (iface->get_offset_at_point)
854     return (*(iface->get_offset_at_point)) (text, x, y, coords);
855   else
856     return -1;
857 }
858
859 /**
860  * atk_text_get_n_selections:
861  * @text: an #AtkText
862  *
863  * Gets the number of selected regions.
864  *
865  * Returns: The number of selected regions, or -1 in the case of failure.
866  **/
867 gint
868 atk_text_get_n_selections (AtkText *text)
869 {
870   AtkTextIface *iface;
871
872   g_return_val_if_fail (ATK_IS_TEXT (text), -1);
873
874   iface = ATK_TEXT_GET_IFACE (text);
875
876   if (iface->get_n_selections)
877     return (*(iface->get_n_selections)) (text);
878   else
879     return -1;
880 }
881
882 /**
883  * atk_text_get_selection:
884  * @text: an #AtkText
885  * @selection_num: The selection number.  The selected regions are
886  * assigned numbers that correspond to how far the region is from the
887  * start of the text.  The selected region closest to the beginning
888  * of the text region is assigned the number 0, etc.  Note that adding,
889  * moving or deleting a selected region can change the numbering.
890  * @start_offset: (out): passes back the starting character offset of the selected region
891  * @end_offset: (out): passes back the ending character offset (offset immediately past)
892  * of the selected region
893  *
894  * Gets the text from the specified selection.
895  *
896  * Returns: a newly allocated string containing the selected text. Use g_free()
897  *          to free the returned string.
898  **/
899 gchar*
900 atk_text_get_selection (AtkText *text, 
901                         gint    selection_num,
902                         gint    *start_offset,
903                         gint    *end_offset)
904 {
905   AtkTextIface *iface;
906   gint local_start_offset, local_end_offset;
907   gint *real_start_offset, *real_end_offset;
908
909   g_return_val_if_fail (ATK_IS_TEXT (text), NULL);
910
911   if (start_offset)
912     real_start_offset = start_offset;
913   else
914     real_start_offset = &local_start_offset;
915   if (end_offset)
916     real_end_offset = end_offset;
917   else
918     real_end_offset = &local_end_offset;
919
920   iface = ATK_TEXT_GET_IFACE (text);
921
922   if (iface->get_selection)
923   {
924     return (*(iface->get_selection)) (text, selection_num,
925        real_start_offset, real_end_offset);
926   }
927   else
928     return NULL;
929 }
930
931 /**
932  * atk_text_add_selection:
933  * @text: an #AtkText
934  * @start_offset: the starting character offset of the selected region
935  * @end_offset: the offset of the first character after the selected region.
936  *
937  * Adds a selection bounded by the specified offsets.
938  *
939  * Returns: %TRUE if successful, %FALSE otherwise
940  **/
941 gboolean
942 atk_text_add_selection (AtkText *text, 
943                         gint    start_offset,
944                         gint    end_offset)
945 {
946   AtkTextIface *iface;
947
948   g_return_val_if_fail (ATK_IS_TEXT (text), FALSE);
949
950   iface = ATK_TEXT_GET_IFACE (text);
951
952   if (iface->add_selection)
953     return (*(iface->add_selection)) (text, start_offset, end_offset);
954   else
955     return FALSE;
956 }
957
958 /**
959  * atk_text_remove_selection:
960  * @text: an #AtkText
961  * @selection_num: The selection number.  The selected regions are
962  * assigned numbers that correspond to how far the region is from the
963  * start of the text.  The selected region closest to the beginning
964  * of the text region is assigned the number 0, etc.  Note that adding,
965  * moving or deleting a selected region can change the numbering.
966  *
967  * Removes the specified selection.
968  *
969  * Returns: %TRUE if successful, %FALSE otherwise
970  **/
971 gboolean
972 atk_text_remove_selection (AtkText *text, 
973                            gint    selection_num)
974 {
975   AtkTextIface *iface;
976
977   g_return_val_if_fail (ATK_IS_TEXT (text), FALSE);
978
979   iface = ATK_TEXT_GET_IFACE (text);
980
981   if (iface->remove_selection)
982     return (*(iface->remove_selection)) (text, selection_num);
983   else
984     return FALSE;
985 }
986
987 /**
988  * atk_text_set_selection:
989  * @text: an #AtkText
990  * @selection_num: The selection number.  The selected regions are
991  * assigned numbers that correspond to how far the region is from the
992  * start of the text.  The selected region closest to the beginning
993  * of the text region is assigned the number 0, etc.  Note that adding,
994  * moving or deleting a selected region can change the numbering.
995  * @start_offset: the new starting character offset of the selection
996  * @end_offset: the new end position of (e.g. offset immediately past) 
997  * the selection
998  *
999  * Changes the start and end offset of the specified selection.
1000  *
1001  * Returns: %TRUE if successful, %FALSE otherwise
1002  **/
1003 gboolean
1004 atk_text_set_selection (AtkText *text, 
1005                         gint    selection_num,
1006                         gint    start_offset, 
1007                         gint    end_offset)
1008 {
1009   AtkTextIface *iface;
1010
1011   g_return_val_if_fail (ATK_IS_TEXT (text), FALSE);
1012
1013   iface = ATK_TEXT_GET_IFACE (text);
1014
1015   if (iface->set_selection)
1016   {
1017     return (*(iface->set_selection)) (text, selection_num,
1018        start_offset, end_offset);
1019   }
1020   else
1021     return FALSE;
1022 }
1023
1024 /**
1025  * atk_text_set_caret_offset:
1026  * @text: an #AtkText
1027  * @offset: the character offset of the new caret position
1028  *
1029  * Sets the caret (cursor) position to the specified @offset.
1030  *
1031  * Returns: %TRUE if successful, %FALSE otherwise.
1032  **/
1033 gboolean
1034 atk_text_set_caret_offset (AtkText *text,
1035                            gint    offset)
1036 {
1037   AtkTextIface *iface;
1038
1039   g_return_val_if_fail (ATK_IS_TEXT (text), FALSE);
1040
1041   iface = ATK_TEXT_GET_IFACE (text);
1042
1043   if (iface->set_caret_offset)
1044     {
1045       return (*(iface->set_caret_offset)) (text, offset);
1046     }
1047   else
1048     {
1049       return FALSE;
1050     }
1051 }
1052
1053 /**
1054  * atk_text_get_range_extents:
1055  * @text: an #AtkText
1056  * @start_offset: The offset of the first text character for which boundary 
1057  *        information is required.
1058  * @end_offset: The offset of the text character after the last character 
1059  *        for which boundary information is required.
1060  * @coord_type: Specify whether coordinates are relative to the screen or widget window.
1061  * @rect: (out): A pointer to a AtkTextRectangle which is filled in by this function.
1062  *
1063  * Get the bounding box for text within the specified range.
1064  *
1065  * Since: 1.3
1066  **/
1067 void
1068 atk_text_get_range_extents (AtkText          *text,
1069                             gint             start_offset,
1070                             gint             end_offset,
1071                             AtkCoordType     coord_type,
1072                             AtkTextRectangle *rect)
1073 {
1074   AtkTextIface *iface;
1075
1076   g_return_if_fail (ATK_IS_TEXT (text));
1077   g_return_if_fail (rect);
1078   g_return_if_fail (start_offset >= 0 && start_offset < end_offset);
1079
1080   iface = ATK_TEXT_GET_IFACE (text);
1081
1082   if (iface->get_range_extents)
1083     (*(iface->get_range_extents)) (text, start_offset, end_offset, coord_type, rect);
1084 }
1085
1086 /**
1087  * atk_text_get_bounded_ranges: (virtual get_bounded_ranges)
1088  * @text: an #AtkText
1089  * @rect: An AtkTextRectangle giving the dimensions of the bounding box.
1090  * @coord_type: Specify whether coordinates are relative to the screen or widget window.
1091  * @x_clip_type: Specify the horizontal clip type.
1092  * @y_clip_type: Specify the vertical clip type.
1093  *
1094  * Get the ranges of text in the specified bounding box.
1095  *
1096  * Since: 1.3
1097  *
1098  * Returns: (array zero-terminated=1): Array of AtkTextRange. The last
1099  *          element of the array returned by this function will be NULL.
1100  **/
1101 AtkTextRange**
1102 atk_text_get_bounded_ranges (AtkText          *text,
1103                              AtkTextRectangle *rect,
1104                              AtkCoordType      coord_type,
1105                              AtkTextClipType   x_clip_type,
1106                              AtkTextClipType   y_clip_type)
1107 {
1108   AtkTextIface *iface;
1109
1110   g_return_val_if_fail (ATK_IS_TEXT (text), NULL);
1111   g_return_val_if_fail (rect, NULL);
1112
1113   iface = ATK_TEXT_GET_IFACE (text);
1114
1115   if (iface->get_bounded_ranges)
1116     return (*(iface->get_bounded_ranges)) (text, rect, coord_type, x_clip_type, y_clip_type);
1117   else
1118     return NULL;
1119 }
1120
1121 /**
1122  * atk_attribute_set_free:
1123  * @attrib_set: The #AtkAttributeSet to free
1124  *
1125  * Frees the memory used by an #AtkAttributeSet, including all its
1126  * #AtkAttributes.
1127  **/
1128 void
1129 atk_attribute_set_free (AtkAttributeSet *attrib_set)
1130 {
1131   GSList *temp;
1132
1133   temp = attrib_set;
1134
1135   while (temp != NULL)
1136     {
1137       AtkAttribute *att;
1138
1139       att = temp->data;
1140
1141       g_free (att->name);
1142       g_free (att->value);
1143       g_free (att);
1144       temp = temp->next;
1145     }
1146   g_slist_free (attrib_set);
1147 }
1148
1149 /**
1150  * atk_text_attribute_register:
1151  * @name: a name string
1152  *
1153  * Associate @name with a new #AtkTextAttribute
1154  *
1155  * Returns: an #AtkTextAttribute associated with @name
1156  **/
1157 AtkTextAttribute
1158 atk_text_attribute_register (const gchar *name)
1159 {
1160   g_return_val_if_fail (name, ATK_TEXT_ATTR_INVALID);
1161
1162   if (!extra_attributes)
1163     extra_attributes = g_ptr_array_new ();
1164
1165   g_ptr_array_add (extra_attributes, g_strdup (name));
1166   return extra_attributes->len + ATK_TEXT_ATTR_LAST_DEFINED;
1167 }
1168
1169 /**
1170  * atk_text_attribute_get_name:
1171  * @attr: The #AtkTextAttribute whose name is required
1172  *
1173  * Gets the name corresponding to the #AtkTextAttribute
1174  *
1175  * Returns: a string containing the name; this string should not be freed
1176  **/
1177 const gchar*
1178 atk_text_attribute_get_name (AtkTextAttribute attr)
1179 {
1180   GTypeClass *type_class;
1181   GEnumValue *value;
1182   const gchar *name = NULL;
1183
1184   type_class = g_type_class_ref (ATK_TYPE_TEXT_ATTRIBUTE);
1185   g_return_val_if_fail (G_IS_ENUM_CLASS (type_class), NULL);
1186
1187   value = g_enum_get_value (G_ENUM_CLASS (type_class), attr);
1188
1189   if (value)
1190     {
1191       name = value->value_nick;
1192     }
1193   else
1194     {
1195       if (extra_attributes)
1196         {
1197           gint n = attr;
1198
1199           n -= ATK_TEXT_ATTR_LAST_DEFINED + 1;
1200
1201           if (n < extra_attributes->len)
1202
1203             name = g_ptr_array_index (extra_attributes, n);
1204         }
1205     }
1206   g_type_class_unref (type_class);
1207   return name;
1208 }
1209
1210 /**
1211  * atk_text_attribute_for_name:
1212  * @name: a string which is the (non-localized) name of an ATK text attribute.
1213  *
1214  * Get the #AtkTextAttribute type corresponding to a text attribute name.
1215  *
1216  * Returns: the #AtkTextAttribute enumerated type corresponding to the specified
1217  *          name, or #ATK_TEXT_ATTRIBUTE_INVALID if no matching text attribute
1218  *          is found.
1219  **/
1220 AtkTextAttribute
1221 atk_text_attribute_for_name (const gchar *name)
1222 {
1223   GTypeClass *type_class;
1224   GEnumValue *value;
1225   AtkTextAttribute type = ATK_TEXT_ATTR_INVALID;
1226
1227   g_return_val_if_fail (name, ATK_TEXT_ATTR_INVALID);
1228
1229   type_class = g_type_class_ref (ATK_TYPE_TEXT_ATTRIBUTE);
1230   g_return_val_if_fail (G_IS_ENUM_CLASS (type_class), ATK_TEXT_ATTR_INVALID);
1231
1232   value = g_enum_get_value_by_nick (G_ENUM_CLASS (type_class), name);
1233
1234   if (value)
1235     {
1236       type = value->value;
1237     }
1238   else
1239     {
1240       gint i;
1241
1242       if (extra_attributes)
1243         {
1244           for (i = 0; i < extra_attributes->len; i++)
1245             {
1246               gchar *extra_attribute = (gchar *)g_ptr_array_index (extra_attributes, i);
1247
1248               g_return_val_if_fail (extra_attribute, ATK_TEXT_ATTR_INVALID);
1249
1250               if (strcmp (name, extra_attribute) == 0)
1251                 {
1252                   type = i + 1 + ATK_TEXT_ATTR_LAST_DEFINED;
1253                   break;
1254                 }
1255             }
1256         }
1257     }
1258   g_type_class_unref (type_class);
1259
1260   return type;
1261 }
1262
1263 /**
1264  * atk_text_attribute_get_value:
1265  * @attr: The #AtkTextAttribute for which a value is required
1266  * @index_: The index of the required value
1267  *
1268  * Gets the value for the index of the #AtkTextAttribute
1269  *
1270  * Returns: (nullable): a string containing the value; this string
1271  * should not be freed; %NULL is returned if there are no values
1272  * maintained for the attr value.
1273  **/
1274 const gchar*
1275 atk_text_attribute_get_value (AtkTextAttribute attr,
1276                               gint             index)
1277 {
1278   switch (attr)
1279     {
1280     case ATK_TEXT_ATTR_INVISIBLE:
1281     case ATK_TEXT_ATTR_EDITABLE:
1282     case ATK_TEXT_ATTR_BG_FULL_HEIGHT:
1283     case ATK_TEXT_ATTR_STRIKETHROUGH:
1284     case ATK_TEXT_ATTR_BG_STIPPLE:
1285     case ATK_TEXT_ATTR_FG_STIPPLE:
1286       g_assert (index >= 0 && index < G_N_ELEMENTS (boolean_offsets));
1287       return boolean + boolean_offsets[index];
1288     case ATK_TEXT_ATTR_UNDERLINE:
1289       g_assert (index >= 0 && index < G_N_ELEMENTS (underline_offsets));
1290       return underline + underline_offsets[index];
1291     case ATK_TEXT_ATTR_WRAP_MODE:
1292       g_assert (index >= 0 && index < G_N_ELEMENTS (wrap_mode_offsets));
1293       return wrap_mode + wrap_mode_offsets[index];
1294     case ATK_TEXT_ATTR_DIRECTION:
1295       g_assert (index >= 0 && index < G_N_ELEMENTS (direction_offsets));
1296       return direction + direction_offsets[index];
1297     case ATK_TEXT_ATTR_JUSTIFICATION:
1298       g_assert (index >= 0 && index < G_N_ELEMENTS (justification_offsets));
1299       return justification + justification_offsets[index];
1300     case ATK_TEXT_ATTR_STRETCH:
1301       g_assert (index >= 0 && index < G_N_ELEMENTS (stretch_offsets));
1302       return stretch + stretch_offsets[index];
1303     case ATK_TEXT_ATTR_VARIANT:
1304       g_assert (index >= 0 && index < G_N_ELEMENTS (variant_offsets));
1305       return variant + variant_offsets[index];
1306     case ATK_TEXT_ATTR_STYLE:
1307       g_assert (index >= 0 && index < G_N_ELEMENTS (style_offsets));
1308       return style + style_offsets[index];
1309     default:
1310       return NULL;
1311    }
1312 }
1313
1314 static void
1315 atk_text_rectangle_union (AtkTextRectangle *src1,
1316                           AtkTextRectangle *src2,
1317                           AtkTextRectangle *dest)
1318 {
1319   gint dest_x, dest_y;
1320
1321   dest_x = MIN (src1->x, src2->x);
1322   dest_y = MIN (src1->y, src2->y);
1323   dest->width = MAX (src1->x + src1->width, src2->x + src2->width) - dest_x;
1324   dest->height = MAX (src1->y + src1->height, src2->y + src2->height) - dest_y;
1325   dest->x = dest_x;
1326   dest->y = dest_y;
1327 }
1328
1329 static gboolean
1330 atk_text_rectangle_contain (AtkTextRectangle *clip,
1331                             AtkTextRectangle *bounds,
1332                             AtkTextClipType  x_clip_type,
1333                             AtkTextClipType  y_clip_type)
1334 {
1335   gboolean x_min_ok, x_max_ok, y_min_ok, y_max_ok;
1336
1337   x_min_ok = (bounds->x >= clip->x) ||
1338              ((bounds->x + bounds->width >= clip->x) &&
1339               ((x_clip_type == ATK_TEXT_CLIP_NONE) ||
1340                (x_clip_type == ATK_TEXT_CLIP_MAX)));
1341
1342   x_max_ok = (bounds->x + bounds->width <= clip->x + clip->width) ||
1343              ((bounds->x <= clip->x + clip->width) &&
1344               ((x_clip_type == ATK_TEXT_CLIP_NONE) ||
1345                (x_clip_type == ATK_TEXT_CLIP_MIN)));
1346
1347   y_min_ok = (bounds->y >= clip->y) ||
1348              ((bounds->y + bounds->height >= clip->y) &&
1349               ((y_clip_type == ATK_TEXT_CLIP_NONE) ||
1350                (y_clip_type == ATK_TEXT_CLIP_MAX)));
1351
1352   y_max_ok = (bounds->y + bounds->height <= clip->y + clip->height) ||
1353              ((bounds->y <= clip->y + clip->height) &&
1354               ((y_clip_type == ATK_TEXT_CLIP_NONE) ||
1355                (y_clip_type == ATK_TEXT_CLIP_MIN)));
1356
1357   return (x_min_ok && x_max_ok && y_min_ok && y_max_ok);
1358   
1359 }
1360
1361 static void 
1362 atk_text_real_get_range_extents (AtkText           *text,
1363                                  gint              start_offset,
1364                                  gint              end_offset,
1365                                  AtkCoordType      coord_type,
1366                                  AtkTextRectangle  *rect)
1367 {
1368   gint i;
1369   AtkTextRectangle cbounds, bounds;
1370
1371   atk_text_get_character_extents (text, start_offset,
1372                                   &bounds.x, &bounds.y,
1373                                   &bounds.width, &bounds.height,
1374                                   coord_type);
1375
1376   for (i = start_offset + 1; i < end_offset; i++)
1377     {
1378       atk_text_get_character_extents (text, i,
1379                                       &cbounds.x, &cbounds.y, 
1380                                       &cbounds.width, &cbounds.height, 
1381                                       coord_type);
1382       atk_text_rectangle_union (&bounds, &cbounds, &bounds);
1383     }
1384
1385   rect->x = bounds.x;
1386   rect->y = bounds.y;
1387   rect->width = bounds.width;
1388   rect->height = bounds.height;
1389 }
1390
1391 static AtkTextRange**
1392 atk_text_real_get_bounded_ranges (AtkText          *text,
1393                                   AtkTextRectangle *rect,
1394                                   AtkCoordType     coord_type,
1395                                   AtkTextClipType  x_clip_type,
1396                                   AtkTextClipType  y_clip_type)
1397 {
1398   gint bounds_min_offset, bounds_max_offset;
1399   gint min_line_start, min_line_end;
1400   gint max_line_start, max_line_end;
1401   gchar *line;
1402   gint curr_offset;
1403   gint offset;
1404   gint num_ranges = 0;
1405   gint range_size = 1;
1406   AtkTextRectangle cbounds;
1407   AtkTextRange **range;
1408
1409   range = NULL;
1410   bounds_min_offset = atk_text_get_offset_at_point (text, rect->x, rect->y, coord_type);
1411   bounds_max_offset = atk_text_get_offset_at_point (text, rect->x + rect->width, rect->y + rect->height, coord_type);
1412
1413   if (bounds_min_offset == 0 &&
1414       bounds_min_offset == bounds_max_offset)
1415     return NULL;
1416
1417   line = atk_text_get_text_at_offset (text, bounds_min_offset, 
1418                                       ATK_TEXT_BOUNDARY_LINE_START,
1419                                       &min_line_start, &min_line_end);
1420   g_free (line);
1421   line = atk_text_get_text_at_offset (text, bounds_max_offset, 
1422                                       ATK_TEXT_BOUNDARY_LINE_START,
1423                                       &max_line_start, &max_line_end);
1424   g_free (line);
1425   bounds_min_offset = MIN (min_line_start, max_line_start);
1426   bounds_max_offset = MAX (min_line_end, max_line_end);
1427
1428   curr_offset = bounds_min_offset;
1429   while (curr_offset < bounds_max_offset)
1430     {
1431       offset = curr_offset;
1432
1433       while (curr_offset < bounds_max_offset)
1434         {
1435           atk_text_get_character_extents (text, curr_offset,
1436                                           &cbounds.x, &cbounds.y,
1437                                           &cbounds.width, &cbounds.height,
1438                                           coord_type);
1439           if (!atk_text_rectangle_contain (rect, &cbounds, x_clip_type, y_clip_type))
1440             break;
1441           curr_offset++;
1442         }
1443       if (curr_offset > offset)
1444         {
1445           AtkTextRange *one_range = g_new (AtkTextRange, 1);
1446
1447           one_range->start_offset = offset;
1448           one_range->end_offset = curr_offset;
1449           one_range->content = atk_text_get_text (text, offset, curr_offset);
1450           atk_text_get_range_extents (text, offset, curr_offset, coord_type, &one_range->bounds);
1451
1452           if (num_ranges >= range_size - 1)
1453             {
1454               range_size *= 2;
1455               range = g_realloc (range, range_size * sizeof (gpointer));
1456             }
1457           range[num_ranges] = one_range;
1458           num_ranges++; 
1459         }   
1460       curr_offset++;
1461       if (range)
1462         range[num_ranges] = NULL;
1463     }
1464   return range;
1465 }
1466
1467 /**
1468  * atk_text_free_ranges:
1469  * @ranges: (array): A pointer to an array of #AtkTextRange which is
1470  *   to be freed.
1471  *
1472  * Frees the memory associated with an array of AtkTextRange. It is assumed
1473  * that the array was returned by the function atk_text_get_bounded_ranges
1474  * and is NULL terminated.
1475  *
1476  * Since: 1.3
1477  **/
1478 void
1479 atk_text_free_ranges (AtkTextRange **ranges)
1480 {
1481   AtkTextRange **first = ranges;
1482
1483   if (ranges)
1484     {
1485       while (*ranges)
1486         {
1487           AtkTextRange *range;
1488
1489           range = *ranges;
1490           ranges++;
1491           g_free (range->content);
1492           g_free (range);
1493         }
1494       g_free (first);
1495     }
1496 }
1497
1498 static AtkTextRange *
1499 atk_text_range_copy (AtkTextRange *src)
1500 {
1501   AtkTextRange *dst = g_new0 (AtkTextRange, 1);
1502   dst->bounds = src->bounds;
1503   dst->start_offset = src->start_offset;
1504   dst->end_offset = src->end_offset;
1505   if (src->content)
1506     dst->content = g_strdup (src->content);
1507   return dst;
1508 }
1509
1510 static void
1511 atk_text_range_free (AtkTextRange *range)
1512 {
1513   g_free (range->content);
1514   g_free (range);
1515 }
1516
1517 G_DEFINE_BOXED_TYPE (AtkTextRange, atk_text_range, atk_text_range_copy,
1518                      atk_text_range_free)