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