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