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