efa989e8a992a9acf07ccbfe48d35c23dc529b8e
[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 void
908 atk_text_get_range_extents (AtkText          *text,
909                             gint             start_offset,
910                             gint             end_offset,
911                             AtkCoordType     coord_type,
912                             AtkTextRectangle *rect)
913 {
914   AtkTextIface *iface;
915
916   g_return_if_fail (ATK_IS_TEXT (text));
917   g_return_if_fail (rect);
918
919   if (start_offset < 0 || start_offset >= end_offset)
920     return;
921  
922   iface = ATK_TEXT_GET_IFACE (text);
923
924   if (iface->get_range_extents)
925     (*(iface->get_range_extents)) (text, start_offset, end_offset, coord_type, rect);
926 }
927
928 /**
929  * atk_text_get_bounded_ranges:
930  * @text: an #AtkText
931  * @rect: An AtkTextRectagle giving the dimensions of the bounding box.
932  * @coord_type: Specify whether coordinates are relative to the screen or widget window.
933  * @x_clip_type: Specify the horizontal clip type.
934  * @y_clip_type: Specify the vertical clip type.
935  *
936  * Get the ranges of text in the specified bounding box.
937  *
938  * Returns: Array of AtkTextRange. The last element of the array returned 
939  *          by this function will be NULL.
940  **/
941 AtkTextRange**
942 atk_text_get_bounded_ranges (AtkText          *text,
943                              AtkTextRectangle *rect,
944                              AtkCoordType      coord_type,
945                              AtkTextClipType   x_clip_type,
946                              AtkTextClipType   y_clip_type)
947 {
948   AtkTextIface *iface;
949
950   g_return_val_if_fail (ATK_IS_TEXT (text), NULL);
951   g_return_val_if_fail (rect, NULL);
952
953   iface = ATK_TEXT_GET_IFACE (text);
954
955   if (iface->get_bounded_ranges)
956     return (*(iface->get_bounded_ranges)) (text, rect, coord_type, x_clip_type, y_clip_type);
957   else
958     return NULL;
959 }
960
961 /**
962  * atk_attribute_set_free:
963  * @attrib_set: The #AtkAttributeSet to free
964  *
965  * Frees the memory used by an #AtkAttributeSet, including all its
966  * #AtkAttributes.
967  **/
968 void
969 atk_attribute_set_free (AtkAttributeSet *attrib_set)
970 {
971   GSList *temp;
972
973   temp = attrib_set;
974
975   while (temp != NULL)
976     {
977       AtkAttribute *att;
978
979       att = temp->data;
980
981       g_free (att->name);
982       g_free (att->value);
983       g_free (att);
984       temp = temp->next;
985     }
986   g_slist_free (attrib_set);
987 }
988
989 /**
990  * atk_text_attribute_register:
991  * @name: a name string
992  *
993  * Associate @name with a new #AtkTextAttribute
994  *
995  * Returns: an #AtkTextAttribute associated with @name
996  **/
997 AtkTextAttribute
998 atk_text_attribute_register (const gchar *name)
999 {
1000   g_return_val_if_fail (name, ATK_TEXT_ATTR_INVALID);
1001
1002   if (!extra_attributes)
1003     extra_attributes = g_ptr_array_new ();
1004
1005   g_ptr_array_add (extra_attributes, g_strdup (name));
1006   return extra_attributes->len + ATK_TEXT_ATTR_LAST_DEFINED;
1007 }
1008
1009 /**
1010  * atk_text_attribute_get_name:
1011  * @attr: The #AtkTextAttribute whose name is required
1012  *
1013  * Gets the name corresponding to the #AtkTextAttribute
1014  *
1015  * Returns: a string containing the name; this string should not be freed
1016  **/
1017 G_CONST_RETURN gchar*
1018 atk_text_attribute_get_name (AtkTextAttribute attr)
1019 {
1020   GTypeClass *type_class;
1021   GEnumValue *value;
1022   gchar *name = NULL;
1023
1024   type_class = g_type_class_ref (ATK_TYPE_TEXT_ATTRIBUTE);
1025   g_return_val_if_fail (G_IS_ENUM_CLASS (type_class), NULL);
1026
1027   value = g_enum_get_value (G_ENUM_CLASS (type_class), attr);
1028
1029   if (value)
1030     {
1031       name = value->value_nick;
1032     }
1033   else
1034     {
1035       if (extra_attributes)
1036         {
1037           gint n = attr;
1038
1039           n -= ATK_TEXT_ATTR_LAST_DEFINED + 1;
1040
1041           if (n < extra_attributes->len)
1042
1043             name = g_ptr_array_index (extra_attributes, n);
1044         }
1045     }
1046   g_type_class_unref (type_class);
1047   return name;
1048 }
1049
1050 /**
1051  * atk_text_attribute_for_name:
1052  * @name: a string which is the (non-localized) name of an ATK text attribute.
1053  *
1054  * Get the #AtkTextAttribute type corresponding to a text attribute name.
1055  *
1056  * Returns: the #AtkTextAttribute enumerated type corresponding to the specified
1057 name,
1058  *          or #ATK_TEXT_ATTRIBUTE_INVALID if no matching text attribute is found.
1059  **/
1060 AtkTextAttribute
1061 atk_text_attribute_for_name (const gchar *name)
1062 {
1063   GTypeClass *type_class;
1064   GEnumValue *value;
1065   AtkTextAttribute type = ATK_TEXT_ATTR_INVALID;
1066
1067   g_return_val_if_fail (name, ATK_TEXT_ATTR_INVALID);
1068
1069   type_class = g_type_class_ref (ATK_TYPE_TEXT_ATTRIBUTE);
1070   g_return_val_if_fail (G_IS_ENUM_CLASS (type_class), ATK_TEXT_ATTR_INVALID);
1071
1072   value = g_enum_get_value_by_nick (G_ENUM_CLASS (type_class), name);
1073
1074   if (value)
1075     {
1076       type = value->value;
1077     }
1078   else
1079     {
1080       gint i;
1081
1082       if (extra_attributes)
1083         {
1084           for (i = 0; i < extra_attributes->len; i++)
1085             {
1086               gchar *extra_attribute = (gchar *)g_ptr_array_index (extra_attributes, i);
1087
1088               g_return_val_if_fail (extra_attribute, ATK_TEXT_ATTR_INVALID);
1089
1090               if (strcmp (name, extra_attribute) == 0)
1091                 {
1092                   type = i + 1 + ATK_TEXT_ATTR_LAST_DEFINED;
1093                   break;
1094                 }
1095             }
1096         }
1097     }
1098   g_type_class_unref (type_class);
1099
1100   return type;
1101 }
1102
1103 /**
1104  * atk_text_attribute_get_value:
1105  * @attr: The #AtkTextAttribute for which a value is required
1106  * @index_: The index of the required value
1107  *
1108  * Gets the value for the index of the #AtkTextAttribute
1109  *
1110  * Returns: a string containing the value; this string should not be freed;
1111  * NULL is returned if there are no values maintained for the attr value. 
1112  **/
1113 G_CONST_RETURN gchar*
1114 atk_text_attribute_get_value (AtkTextAttribute attr,
1115                               gint             index)
1116 {
1117   switch (attr)
1118     {
1119     case ATK_TEXT_ATTR_INVISIBLE:
1120     case ATK_TEXT_ATTR_EDITABLE:
1121     case ATK_TEXT_ATTR_BG_FULL_HEIGHT:
1122     case ATK_TEXT_ATTR_STRIKETHROUGH:
1123     case ATK_TEXT_ATTR_BG_STIPPLE:
1124     case ATK_TEXT_ATTR_FG_STIPPLE:
1125       g_assert (index >= 0 && index < 2);
1126       return bool[index];
1127     case ATK_TEXT_ATTR_UNDERLINE:
1128       g_assert (index >= 0 && index < 4);
1129       return underline[index];
1130     case ATK_TEXT_ATTR_WRAP_MODE:
1131       g_assert (index >= 0 && index < 3);
1132       return wrap_mode[index];
1133     case ATK_TEXT_ATTR_DIRECTION:
1134       g_assert (index >= 0 && index < 3);
1135       return direction[index];
1136     case ATK_TEXT_ATTR_JUSTIFICATION:
1137       g_assert (index >= 0 && index < 3);
1138       return justification[index];
1139     case ATK_TEXT_ATTR_STRETCH:
1140       g_assert (index >= 0 && index < 9);
1141       return stretch[index];
1142     case ATK_TEXT_ATTR_VARIANT:
1143       g_assert (index >= 0 && index < 2);
1144       return variant[index];
1145     case ATK_TEXT_ATTR_STYLE:
1146       g_assert (index >= 0 && index < 3);
1147       return style[index];
1148     default:
1149       return NULL;
1150    }
1151 }
1152
1153 static void
1154 atk_text_rectangle_union (AtkTextRectangle *src1,
1155                           AtkTextRectangle *src2,
1156                           AtkTextRectangle *dest)
1157 {
1158   gint dest_x, dest_y;
1159
1160   dest_x = MIN (src1->x, src2->x);
1161   dest_y = MIN (src1->y, src2->y);
1162   dest->width = MAX (src1->x + src1->width, src2->x + src2->width) - dest_x;
1163   dest->height = MAX (src1->y + src1->height, src2->y + src2->height) - dest_y;
1164   dest->x = dest_x;
1165   dest->y = dest_y;
1166 }
1167
1168 static gboolean
1169 atk_text_rectangle_contain (AtkTextRectangle *clip,
1170                             AtkTextRectangle *bounds,
1171                             AtkTextClipType  x_clip_type,
1172                             AtkTextClipType  y_clip_type)
1173 {
1174   gboolean x_min_ok, x_max_ok, y_min_ok, y_max_ok;
1175
1176   x_min_ok = (bounds->x >= clip->x) ||
1177              ((bounds->x + bounds->width >= clip->x) &&
1178               ((x_clip_type == ATK_TEXT_CLIP_NONE) ||
1179                (x_clip_type == ATK_TEXT_CLIP_MAX)));
1180
1181   x_max_ok = (bounds->x + bounds->width <= clip->x + clip->width) ||
1182              ((bounds->x <= clip->x + clip->width) &&
1183               ((x_clip_type == ATK_TEXT_CLIP_NONE) ||
1184                (x_clip_type == ATK_TEXT_CLIP_MIN)));
1185
1186   y_min_ok = (bounds->y >= clip->y) ||
1187              ((bounds->y + bounds->height >= clip->y) &&
1188               ((y_clip_type == ATK_TEXT_CLIP_NONE) ||
1189                (y_clip_type == ATK_TEXT_CLIP_MAX)));
1190
1191   y_max_ok = (bounds->y + bounds->height <= clip->y + clip->height) ||
1192              ((bounds->y <= clip->y + clip->height) &&
1193               ((y_clip_type == ATK_TEXT_CLIP_NONE) ||
1194                (y_clip_type == ATK_TEXT_CLIP_MIN)));
1195
1196   return (x_min_ok && x_max_ok && y_min_ok && y_max_ok);
1197   
1198 }
1199
1200 static void 
1201 atk_text_real_get_range_extents (AtkText           *text,
1202                                  gint              start_offset,
1203                                  gint              end_offset,
1204                                  AtkCoordType      coord_type,
1205                                  AtkTextRectangle  *rect)
1206 {
1207   gint i;
1208   AtkTextRectangle cbounds, bounds;
1209
1210   atk_text_get_character_extents (text, start_offset,
1211                                   &bounds.x, &bounds.y,
1212                                   &bounds.width, &bounds.height,
1213                                   coord_type);
1214
1215   for (i = start_offset + 1; i < end_offset; i++)
1216     {
1217       atk_text_get_character_extents (text, i,
1218                                       &cbounds.x, &cbounds.y, 
1219                                       &cbounds.width, &cbounds.height, 
1220                                       coord_type);
1221       atk_text_rectangle_union (&bounds, &cbounds, &bounds);
1222     }
1223
1224   rect->x = bounds.x;
1225   rect->y = bounds.y;
1226   rect->width = bounds.width;
1227   rect->height = bounds.height;
1228 }
1229
1230 static AtkTextRange**
1231 atk_text_real_get_bounded_ranges (AtkText          *text,
1232                                   AtkTextRectangle *rect,
1233                                   AtkCoordType     coord_type,
1234                                   AtkTextClipType  x_clip_type,
1235                                   AtkTextClipType  y_clip_type)
1236 {
1237   gint bounds_min_offset, bounds_max_offset;
1238   gint min_line_start, min_line_end;
1239   gint max_line_start, max_line_end;
1240   gchar *line;
1241   gint curr_offset;
1242   gint offset;
1243   gint num_ranges = 0;
1244   gint range_size = 1;
1245   AtkTextRectangle cbounds;
1246   AtkTextRange **range;
1247
1248   range = NULL;
1249   bounds_min_offset = atk_text_get_offset_at_point (text, rect->x, rect->y, coord_type);
1250   bounds_max_offset = atk_text_get_offset_at_point (text, rect->x + rect->width, rect->y + rect->height, coord_type);
1251
1252   if (bounds_min_offset == 0 &&
1253       bounds_min_offset == bounds_max_offset)
1254     return NULL;
1255
1256   line = atk_text_get_text_at_offset (text, bounds_min_offset, 
1257                                       ATK_TEXT_BOUNDARY_LINE_START,
1258                                       &min_line_start, &min_line_end);
1259   g_free (line);
1260   line = atk_text_get_text_at_offset (text, bounds_max_offset, 
1261                                       ATK_TEXT_BOUNDARY_LINE_START,
1262                                       &max_line_start, &max_line_end);
1263   g_free (line);
1264   bounds_min_offset = MIN (min_line_start, max_line_start);
1265   bounds_max_offset = MAX (min_line_end, max_line_end);
1266
1267   curr_offset = bounds_min_offset;
1268   while (curr_offset < bounds_max_offset)
1269     {
1270       offset = curr_offset;
1271
1272       while (curr_offset < bounds_max_offset)
1273         {
1274           atk_text_get_character_extents (text, curr_offset,
1275                                           &cbounds.x, &cbounds.y,
1276                                           &cbounds.width, &cbounds.height,
1277                                           coord_type);
1278           if (!atk_text_rectangle_contain (rect, &cbounds, x_clip_type, y_clip_type))
1279             break;
1280           curr_offset++;
1281         }
1282       if (curr_offset > offset)
1283         {
1284           AtkTextRange *one_range = g_new (AtkTextRange, 1);
1285
1286           one_range->start_offset = offset;
1287           one_range->end_offset = curr_offset;
1288           one_range->content = atk_text_get_text (text, offset, curr_offset);
1289           atk_text_get_range_extents (text, offset, curr_offset, coord_type, &one_range->bounds);
1290
1291           if (num_ranges >= range_size - 1)
1292             {
1293               range_size *= 2;
1294               range = g_realloc (range, range_size * sizeof (gpointer));
1295             }
1296           range[num_ranges] = one_range;
1297           num_ranges++; 
1298         }   
1299       curr_offset++;
1300       if (range)
1301         range[num_ranges] = NULL;
1302     }
1303   return range;
1304 }
1305
1306 /**
1307  * atk_text_free_ranges:
1308  * @ranges: A pointer to an array of  #AtkTextRange which is to be freed.
1309  *
1310  * Frees the memory associated with an array of AtkTextRange. It is assumed
1311  * that the array was returned by the function atk_text_get_bounded_ranges
1312  * and is NULL terminated.
1313  **/
1314 void
1315 atk_text_free_ranges (AtkTextRange **ranges)
1316 {
1317   AtkTextRange **first = ranges;
1318
1319   if (ranges)
1320     {
1321       while (*ranges)
1322         {
1323           AtkTextRange *range;
1324
1325           range = *ranges;
1326           *ranges++;
1327           g_free (range->content);
1328           g_free (range);
1329         }
1330       g_free (first);
1331     }
1332 }