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