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