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 "config.h"
21

            
22
#include "atk.h"
23
#include "atkmarshal.h"
24

            
25
#include <string.h>
26

            
27
/**
28
 * AtkText:
29
 *
30
 * The ATK interface implemented by components with text content.
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
{
57
  TEXT_CHANGED,
58
  TEXT_CARET_MOVED,
59
  TEXT_SELECTION_CHANGED,
60
  TEXT_ATTRIBUTES_CHANGED,
61
  TEXT_INSERT,
62
  TEXT_REMOVE,
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 const char text_position[] =
139
    "baseline\0"
140
    "super\0"
141
    "sub\0";
142
static const guint8 text_position_offsets[] = {
143
  0,
144
  9,
145
  15,
146
};
147

            
148
static void atk_text_base_init (AtkTextIface *class);
149

            
150
static void atk_text_real_get_range_extents (AtkText *text,
151
                                             gint start_offset,
152
                                             gint end_offset,
153
                                             AtkCoordType coord_type,
154
                                             AtkTextRectangle *rect);
155

            
156
static AtkTextRange **atk_text_real_get_bounded_ranges (AtkText *text,
157
                                                        AtkTextRectangle *rect,
158
                                                        AtkCoordType coord_type,
159
                                                        AtkTextClipType x_clip_type,
160
                                                        AtkTextClipType y_clip_type);
161

            
162
static guint atk_text_signals[LAST_SIGNAL] = { 0 };
163

            
164
GType
165
2868
atk_text_get_type (void)
166
{
167
  static GType type = 0;
168

            
169
2868
  if (!type)
170
    {
171
      static const GTypeInfo tinfo = {
172
        sizeof (AtkTextIface),
173
        (GBaseInitFunc) atk_text_base_init,
174
        (GBaseFinalizeFunc) NULL,
175
        (GClassInitFunc) NULL /* atk_text_interface_init */,
176
        (GClassFinalizeFunc) NULL,
177

            
178
      };
179

            
180
161
      type = g_type_register_static (G_TYPE_INTERFACE, "AtkText", &tinfo, 0);
181
    }
182

            
183
2868
  return type;
184
}
185

            
186
static void
187
343
atk_text_base_init (AtkTextIface *class)
188
{
189
  static gboolean initialized = FALSE;
190

            
191
343
  if (!initialized)
192
    {
193
      /*
194
       * Note that text_changed signal supports details "insert", "delete",
195
       * possibly "replace".
196
       */
197

            
198
161
      class->get_range_extents = atk_text_real_get_range_extents;
199
161
      class->get_bounded_ranges = atk_text_real_get_bounded_ranges;
200

            
201
      /**
202
       * AtkText::text-changed:
203
       * @atktext: the object which received the signal.
204
       * @arg1: The position (character offset) of the insertion or deletion.
205
       * @arg2: The length (in characters) of text inserted or deleted.
206
       *
207
       * The "text-changed" signal is emitted when the text of the
208
       * object which implements the AtkText interface changes, This
209
       * signal will have a detail which is either "insert" or
210
       * "delete" which identifies whether the text change was an
211
       * insertion or a deletion.
212
       *
213
       * Deprecated: 2.9.4: Use #AtkObject::text-insert or
214
       * #AtkObject::text-remove instead.
215
       */
216
161
      atk_text_signals[TEXT_CHANGED] =
217
161
          g_signal_new ("text_changed",
218
                        ATK_TYPE_TEXT,
219
                        G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
220
                        G_STRUCT_OFFSET (AtkTextIface, text_changed),
221
                        (GSignalAccumulator) NULL, NULL,
222
                        atk_marshal_VOID__INT_INT,
223
                        G_TYPE_NONE,
224
                        2, G_TYPE_INT, G_TYPE_INT);
225

            
226
      /**
227
       * AtkText::text-insert:
228
       * @atktext: the object which received the signal.
229
       * @arg1: The position (character offset) of the insertion.
230
       * @arg2: The length (in characters) of text inserted.
231
       * @arg3: The new text inserted
232
       *
233
       * The "text-insert" signal is emitted when a new text is
234
       * inserted. If the signal was not triggered by the user
235
       * (e.g. typing or pasting text), the "system" detail should be
236
       * included.
237
       */
238
161
      atk_text_signals[TEXT_INSERT] =
239
161
          g_signal_new ("text_insert",
240
                        ATK_TYPE_TEXT,
241
                        G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
242
                        0,
243
                        (GSignalAccumulator) NULL, NULL,
244
                        atk_marshal_VOID__INT_INT_STRING,
245
                        G_TYPE_NONE,
246
                        3, G_TYPE_INT, G_TYPE_INT, G_TYPE_STRING);
247

            
248
      /**
249
       * AtkText::text-remove:
250
       * @atktext: the object which received the signal.
251
       * @arg1: The position (character offset) of the removal.
252
       * @arg2: The length (in characters) of text removed.
253
       * @arg3: The old text removed
254
       *
255
       * The "text-remove" signal is emitted when a new text is
256
       * removed. If the signal was not triggered by the user
257
       * (e.g. typing or pasting text), the "system" detail should be
258
       * included.
259
       */
260
161
      atk_text_signals[TEXT_REMOVE] =
261
161
          g_signal_new ("text_remove",
262
                        ATK_TYPE_TEXT,
263
                        G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
264
                        0,
265
                        (GSignalAccumulator) NULL, NULL,
266
                        atk_marshal_VOID__INT_INT_STRING,
267
                        G_TYPE_NONE,
268
                        3, G_TYPE_INT, G_TYPE_INT, G_TYPE_STRING);
269

            
270
      /**
271
       * AtkText::text-caret-moved:
272
       * @atktext: the object which received the signal.
273
       * @arg1: The new position of the text caret.
274
       *
275
       * The "text-caret-moved" signal is emitted when the caret
276
       * position of the text of an object which implements AtkText
277
       * changes.
278
       */
279
161
      atk_text_signals[TEXT_CARET_MOVED] =
280
161
          g_signal_new ("text_caret_moved",
281
                        ATK_TYPE_TEXT,
282
                        G_SIGNAL_RUN_LAST,
283
                        G_STRUCT_OFFSET (AtkTextIface, text_caret_moved),
284
                        (GSignalAccumulator) NULL, NULL,
285
                        g_cclosure_marshal_VOID__INT,
286
                        G_TYPE_NONE,
287
                        1, G_TYPE_INT);
288

            
289
      /**
290
       * AtkText::text-selection-changed:
291
       * @atktext: the object which received the signal.
292
       *
293
       * The "text-selection-changed" signal is emitted when the
294
       * selected text of an object which implements AtkText changes.
295
       */
296
161
      atk_text_signals[TEXT_SELECTION_CHANGED] =
297
161
          g_signal_new ("text_selection_changed",
298
                        ATK_TYPE_TEXT,
299
                        G_SIGNAL_RUN_LAST,
300
                        G_STRUCT_OFFSET (AtkTextIface, text_selection_changed),
301
                        (GSignalAccumulator) NULL, NULL,
302
                        g_cclosure_marshal_VOID__VOID,
303
                        G_TYPE_NONE, 0);
304
      /**
305
       * AtkText::text-attributes-changed:
306
       * @atktext: the object which received the signal.
307
       *
308
       * The "text-attributes-changed" signal is emitted when the text
309
       * attributes of the text of an object which implements AtkText
310
       * changes.
311
       */
312
161
      atk_text_signals[TEXT_ATTRIBUTES_CHANGED] =
313
161
          g_signal_new ("text_attributes_changed",
314
                        ATK_TYPE_TEXT,
315
                        G_SIGNAL_RUN_LAST,
316
                        G_STRUCT_OFFSET (AtkTextIface, text_attributes_changed),
317
                        (GSignalAccumulator) NULL, NULL,
318
                        g_cclosure_marshal_VOID__VOID,
319
                        G_TYPE_NONE, 0);
320

            
321
161
      initialized = TRUE;
322
    }
323
343
}
324

            
325
/**
326
 * atk_text_get_text:
327
 * @text: an #AtkText
328
 * @start_offset: a starting character offset within @text
329
 * @end_offset: an ending character offset within @text, or -1 for the end of the string.
330
 *
331
 * Gets the specified text.
332
 *
333
 * Returns: a newly allocated string containing the text from @start_offset up
334
 *          to, but not including @end_offset. Use g_free() to free the returned
335
 *          string.
336
 **/
337
gchar *
338
2
atk_text_get_text (AtkText *text,
339
                   gint start_offset,
340
                   gint end_offset)
341
{
342
  AtkTextIface *iface;
343

            
344
2
  g_return_val_if_fail (ATK_IS_TEXT (text), NULL);
345

            
346
2
  iface = ATK_TEXT_GET_IFACE (text);
347

            
348
2
  if (start_offset < 0 || end_offset < -1 ||
349
2
      (end_offset != -1 && end_offset < start_offset))
350
    return NULL;
351

            
352
2
  if (iface->get_text)
353
2
    return (*(iface->get_text)) (text, start_offset, end_offset);
354
  else
355
    return NULL;
356
}
357

            
358
/**
359
 * atk_text_get_character_at_offset:
360
 * @text: an #AtkText
361
 * @offset: a character offset within @text
362
 *
363
 * Gets the specified text.
364
 *
365
 * Returns: the character at @offset or 0 in the case of failure.
366
 **/
367
gunichar
368
1
atk_text_get_character_at_offset (AtkText *text,
369
                                  gint offset)
370
{
371
  AtkTextIface *iface;
372

            
373
1
  g_return_val_if_fail (ATK_IS_TEXT (text), (gunichar) 0);
374

            
375
1
  iface = ATK_TEXT_GET_IFACE (text);
376

            
377
1
  if (iface->get_character_at_offset)
378
1
    return (*(iface->get_character_at_offset)) (text, offset);
379
  else
380
    return (gunichar) 0;
381
}
382

            
383
/**
384
 * atk_text_get_text_after_offset:
385
 * @text: an #AtkText
386
 * @offset: position
387
 * @boundary_type: An #AtkTextBoundary
388
 * @start_offset: (out): the starting character offset of the returned string
389
 * @end_offset: (out): the offset of the first character after the
390
 *              returned substring
391
 *
392
 * Gets the specified text.
393
 *
394
 * Deprecated: 2.9.3: Please use atk_text_get_string_at_offset() instead.
395
 *
396
 * Returns: a newly allocated string containing the text after @offset bounded
397
 *          by the specified @boundary_type. Use g_free() to free the returned
398
 *          string.
399
 **/
400
gchar *
401
atk_text_get_text_after_offset (AtkText *text,
402
                                gint offset,
403
                                AtkTextBoundary boundary_type,
404
                                gint *start_offset,
405
                                gint *end_offset)
406
{
407
  AtkTextIface *iface;
408
  gint local_start_offset, local_end_offset;
409
  gint *real_start_offset, *real_end_offset;
410

            
411
  g_return_val_if_fail (ATK_IS_TEXT (text), NULL);
412

            
413
  if (start_offset)
414
    real_start_offset = start_offset;
415
  else
416
    real_start_offset = &local_start_offset;
417
  if (end_offset)
418
    real_end_offset = end_offset;
419
  else
420
    real_end_offset = &local_end_offset;
421

            
422
  if (offset < 0)
423
    return NULL;
424

            
425
  iface = ATK_TEXT_GET_IFACE (text);
426

            
427
  if (iface->get_text_after_offset)
428
    return (*(iface->get_text_after_offset)) (text, offset, boundary_type, real_start_offset, real_end_offset);
429
  else
430
    return NULL;
431
}
432

            
433
/**
434
 * atk_text_get_text_at_offset:
435
 * @text: an #AtkText
436
 * @offset: position
437
 * @boundary_type: An #AtkTextBoundary
438
 * @start_offset: (out): the starting character offset of the returned string
439
 * @end_offset: (out): the offset of the first character after the
440
 *              returned substring
441
 *
442
 * Gets the specified text.
443
 *
444
 * If the boundary_type if ATK_TEXT_BOUNDARY_CHAR the character at the
445
 * offset is returned.
446
 *
447
 * If the boundary_type is ATK_TEXT_BOUNDARY_WORD_START the returned string
448
 * is from the word start at or before the offset to the word start after
449
 * the offset.
450
 *
451
 * The returned string will contain the word at the offset if the offset
452
 * is inside a word and will contain the word before the offset if the
453
 * offset is not inside a word.
454
 *
455
 * If the boundary type is ATK_TEXT_BOUNDARY_SENTENCE_START the returned
456
 * string is from the sentence start at or before the offset to the sentence
457
 * start after the offset.
458
 *
459
 * The returned string will contain the sentence at the offset if the offset
460
 * is inside a sentence and will contain the sentence before the offset
461
 * if the offset is not inside a sentence.
462
 *
463
 * If the boundary type is ATK_TEXT_BOUNDARY_LINE_START the returned
464
 * string is from the line start at or before the offset to the line
465
 * start after the offset.
466
 *
467
 * Deprecated: This method is deprecated since ATK version
468
 * 2.9.4. Please use atk_text_get_string_at_offset() instead.
469
 *
470
 * Returns: a newly allocated string containing the text at @offset bounded
471
 *          by the specified @boundary_type. Use g_free() to free the returned
472
 *          string.
473
 **/
474
gchar *
475
atk_text_get_text_at_offset (AtkText *text,
476
                             gint offset,
477
                             AtkTextBoundary boundary_type,
478
                             gint *start_offset,
479
                             gint *end_offset)
480
{
481
  AtkTextIface *iface;
482
  gint local_start_offset, local_end_offset;
483
  gint *real_start_offset, *real_end_offset;
484

            
485
  g_return_val_if_fail (ATK_IS_TEXT (text), NULL);
486

            
487
  if (start_offset)
488
    real_start_offset = start_offset;
489
  else
490
    real_start_offset = &local_start_offset;
491
  if (end_offset)
492
    real_end_offset = end_offset;
493
  else
494
    real_end_offset = &local_end_offset;
495

            
496
  iface = ATK_TEXT_GET_IFACE (text);
497

            
498
  if (iface->get_text_at_offset)
499
    return (*(iface->get_text_at_offset)) (text, offset, boundary_type, real_start_offset, real_end_offset);
500
  else
501
    return NULL;
502
}
503

            
504
/**
505
 * atk_text_get_text_before_offset:
506
 * @text: an #AtkText
507
 * @offset: position
508
 * @boundary_type: An #AtkTextBoundary
509
 * @start_offset: (out): the starting character offset of the returned string
510
 * @end_offset: (out): the offset of the first character after the
511
 *              returned substring
512
 *
513
 * Gets the specified text.
514
 *
515
 * Deprecated: 2.9.3: Please use atk_text_get_string_at_offset() instead.
516
 *
517
 * Returns: a newly allocated string containing the text before @offset bounded
518
 *          by the specified @boundary_type. Use g_free() to free the returned
519
 *          string.
520
 **/
521
gchar *
522
atk_text_get_text_before_offset (AtkText *text,
523
                                 gint offset,
524
                                 AtkTextBoundary boundary_type,
525
                                 gint *start_offset,
526
                                 gint *end_offset)
527
{
528
  AtkTextIface *iface;
529
  gint local_start_offset, local_end_offset;
530
  gint *real_start_offset, *real_end_offset;
531

            
532
  g_return_val_if_fail (ATK_IS_TEXT (text), NULL);
533

            
534
  if (start_offset)
535
    real_start_offset = start_offset;
536
  else
537
    real_start_offset = &local_start_offset;
538
  if (end_offset)
539
    real_end_offset = end_offset;
540
  else
541
    real_end_offset = &local_end_offset;
542

            
543
  if (offset < 0)
544
    return NULL;
545

            
546
  iface = ATK_TEXT_GET_IFACE (text);
547

            
548
  if (iface->get_text_before_offset)
549
    return (*(iface->get_text_before_offset)) (text, offset, boundary_type, real_start_offset, real_end_offset);
550
  else
551
    return NULL;
552
}
553

            
554
/**
555
 * atk_text_get_string_at_offset:
556
 * @text: an #AtkText
557
 * @offset: position
558
 * @granularity: An #AtkTextGranularity
559
 * @start_offset: (out): the starting character offset of the returned string, or -1
560
 *                in the case of error (e.g. invalid offset, not implemented)
561
 * @end_offset: (out): the offset of the first character after the returned string,
562
 *              or -1 in the case of error (e.g. invalid offset, not implemented)
563
 *
564
 * Gets a portion of the text exposed through an #AtkText according to a given @offset
565
 * and a specific @granularity, along with the start and end offsets defining the
566
 * boundaries of such a portion of text.
567
 *
568
 * If @granularity is ATK_TEXT_GRANULARITY_CHAR the character at the
569
 * offset is returned.
570
 *
571
 * If @granularity is ATK_TEXT_GRANULARITY_WORD the returned string
572
 * is from the word start at or before the offset to the word start after
573
 * the offset.
574
 *
575
 * The returned string will contain the word at the offset if the offset
576
 * is inside a word and will contain the word before the offset if the
577
 * offset is not inside a word.
578
 *
579
 * If @granularity is ATK_TEXT_GRANULARITY_SENTENCE the returned string
580
 * is from the sentence start at or before the offset to the sentence
581
 * start after the offset.
582
 *
583
 * The returned string will contain the sentence at the offset if the offset
584
 * is inside a sentence and will contain the sentence before the offset
585
 * if the offset is not inside a sentence.
586
 *
587
 * If @granularity is ATK_TEXT_GRANULARITY_LINE the returned string
588
 * is from the line start at or before the offset to the line
589
 * start after the offset.
590
 *
591
 * If @granularity is ATK_TEXT_GRANULARITY_PARAGRAPH the returned string
592
 * is from the start of the paragraph at or before the offset to the start
593
 * of the following paragraph after the offset.
594
 *
595
 * Since: 2.10
596
 *
597
 * Returns: (nullable): a newly allocated string containing the text at
598
 *          the @offset bounded by the specified @granularity. Use g_free()
599
 *          to free the returned string.  Returns %NULL if the offset is invalid
600
 *          or no implementation is available.
601
 **/
602
gchar *
603
5
atk_text_get_string_at_offset (AtkText *text,
604
                               gint offset,
605
                               AtkTextGranularity granularity,
606
                               gint *start_offset,
607
                               gint *end_offset)
608
{
609
  AtkTextIface *iface;
610
  gint local_start_offset, local_end_offset;
611
  gint *real_start_offset, *real_end_offset;
612

            
613
5
  g_return_val_if_fail (ATK_IS_TEXT (text), NULL);
614

            
615
5
  if (start_offset)
616
    {
617
5
      *start_offset = -1;
618
5
      real_start_offset = start_offset;
619
    }
620
  else
621
    real_start_offset = &local_start_offset;
622

            
623
5
  if (end_offset)
624
    {
625
5
      *end_offset = -1;
626
5
      real_end_offset = end_offset;
627
    }
628
  else
629
    real_end_offset = &local_end_offset;
630

            
631
5
  if (offset < 0)
632
    return NULL;
633

            
634
5
  iface = ATK_TEXT_GET_IFACE (text);
635

            
636
5
  if (iface->get_string_at_offset)
637
5
    return (*(iface->get_string_at_offset)) (text, offset, granularity, real_start_offset, real_end_offset);
638
  else
639
    return NULL;
640
}
641

            
642
/**
643
 * atk_text_get_caret_offset:
644
 * @text: an #AtkText
645
 *
646
 * Gets the offset of the position of the caret (cursor).
647
 *
648
 * Returns: the character offset of the position of the caret or -1 if
649
 *          the caret is not located inside the element or in the case of
650
 *          any other failure.
651
 **/
652
gint
653
2
atk_text_get_caret_offset (AtkText *text)
654
{
655
  AtkTextIface *iface;
656

            
657
2
  g_return_val_if_fail (ATK_IS_TEXT (text), 0);
658

            
659
2
  iface = ATK_TEXT_GET_IFACE (text);
660

            
661
2
  if (iface->get_caret_offset)
662
2
    return (*(iface->get_caret_offset)) (text);
663
  else
664
    return -1;
665
}
666

            
667
/**
668
 * atk_text_get_character_extents:
669
 * @text: an #AtkText
670
 * @offset: The offset of the text character for which bounding information is required.
671
 * @x: (out) (optional): Pointer for the x coordinate of the bounding box
672
 * @y: (out) (optional): Pointer for the y coordinate of the bounding box
673
 * @width: (out) (optional): Pointer for the width of the bounding box
674
 * @height: (out) (optional): Pointer for the height of the bounding box
675
 * @coords: specify whether coordinates are relative to the screen or widget window
676
 *
677
 * If the extent can not be obtained (e.g. missing support), all of x, y, width,
678
 * height are set to -1.
679
 *
680
 * Get the bounding box containing the glyph representing the character at
681
 *     a particular text offset.
682
 **/
683
void
684
1
atk_text_get_character_extents (AtkText *text,
685
                                gint offset,
686
                                gint *x,
687
                                gint *y,
688
                                gint *width,
689
                                gint *height,
690
                                AtkCoordType coords)
691
{
692
  AtkTextIface *iface;
693
  gint local_x, local_y, local_width, local_height;
694
  gint *real_x, *real_y, *real_width, *real_height;
695

            
696
1
  g_return_if_fail (ATK_IS_TEXT (text));
697

            
698
1
  if (x)
699
1
    real_x = x;
700
  else
701
    real_x = &local_x;
702
1
  if (y)
703
1
    real_y = y;
704
  else
705
    real_y = &local_y;
706
1
  if (width)
707
1
    real_width = width;
708
  else
709
    real_width = &local_width;
710
1
  if (height)
711
1
    real_height = height;
712
  else
713
    real_height = &local_height;
714

            
715
1
  *real_x = -1;
716
1
  *real_y = -1;
717
1
  *real_width = -1;
718
1
  *real_height = -1;
719

            
720
1
  if (offset < 0)
721
    return;
722

            
723
1
  iface = ATK_TEXT_GET_IFACE (text);
724

            
725
1
  if (iface->get_character_extents)
726
1
    (*(iface->get_character_extents)) (text, offset, real_x, real_y, real_width, real_height, coords);
727

            
728
1
  if (*real_width < 0)
729
    {
730
      *real_x = *real_x + *real_width;
731
      *real_width *= -1;
732
    }
733
}
734

            
735
/**
736
 * atk_text_get_run_attributes:
737
 *@text: an #AtkText
738
 *@offset: the character offset at which to get the attributes, -1 means the offset of
739
 *the character to be inserted at the caret location.
740
 *@start_offset: (out): the address to put the start offset of the range
741
 *@end_offset: (out): the address to put the end offset of the range
742
 *
743
 *Creates an #AtkAttributeSet which consists of the attributes explicitly
744
 *set at the position @offset in the text. @start_offset and @end_offset are
745
 *set to the start and end of the range around @offset where the attributes are
746
 *invariant. Note that @end_offset is the offset of the first character
747
 *after the range.  See the enum AtkTextAttribute for types of text
748
 *attributes that can be returned. Note that other attributes may also be
749
 *returned.
750
 *
751
 *Returns: (transfer full): an #AtkAttributeSet which contains the attributes
752
 *         explicitly set at @offset. This #AtkAttributeSet should be freed by
753
 *         a call to atk_attribute_set_free().
754
 **/
755
AtkAttributeSet *
756
4
atk_text_get_run_attributes (AtkText *text,
757
                             gint offset,
758
                             gint *start_offset,
759
                             gint *end_offset)
760
{
761
  AtkTextIface *iface;
762
  gint local_start_offset, local_end_offset;
763
  gint *real_start_offset, *real_end_offset;
764

            
765
4
  g_return_val_if_fail (ATK_IS_TEXT (text), NULL);
766

            
767
4
  if (start_offset)
768
4
    real_start_offset = start_offset;
769
  else
770
    real_start_offset = &local_start_offset;
771
4
  if (end_offset)
772
4
    real_end_offset = end_offset;
773
  else
774
    real_end_offset = &local_end_offset;
775

            
776
4
  if (offset < -1)
777
    return NULL;
778

            
779
4
  iface = ATK_TEXT_GET_IFACE (text);
780

            
781
4
  if (iface->get_run_attributes)
782
4
    return (*(iface->get_run_attributes)) (text, offset, real_start_offset, real_end_offset);
783
  else
784
    return NULL;
785
}
786

            
787
/**
788
 * atk_text_get_default_attributes:
789
 *@text: an #AtkText
790
 *
791
 *Creates an #AtkAttributeSet which consists of the default values of
792
 *attributes for the text. See the enum AtkTextAttribute for types of text
793
 *attributes that can be returned. Note that other attributes may also be
794
 *returned.
795
 *
796
 *Returns: (transfer full): an #AtkAttributeSet which contains the default text
797
 *          attributes for this #AtkText. This #AtkAttributeSet should be freed by
798
 *          a call to atk_attribute_set_free().
799
 */
800
AtkAttributeSet *
801
1
atk_text_get_default_attributes (AtkText *text)
802
{
803
  AtkTextIface *iface;
804

            
805
1
  g_return_val_if_fail (ATK_IS_TEXT (text), NULL);
806

            
807
1
  iface = ATK_TEXT_GET_IFACE (text);
808

            
809
1
  if (iface->get_default_attributes)
810
1
    return (*(iface->get_default_attributes)) (text);
811
  else
812
    return NULL;
813
}
814

            
815
/**
816
 * atk_text_get_character_count:
817
 * @text: an #AtkText
818
 *
819
 * Gets the character count.
820
 *
821
 * Returns: the number of characters or -1 in case of failure.
822
 **/
823
gint
824
1
atk_text_get_character_count (AtkText *text)
825
{
826
  AtkTextIface *iface;
827

            
828
1
  g_return_val_if_fail (ATK_IS_TEXT (text), -1);
829

            
830
1
  iface = ATK_TEXT_GET_IFACE (text);
831

            
832
1
  if (iface->get_character_count)
833
1
    return (*(iface->get_character_count)) (text);
834
  else
835
    return -1;
836
}
837

            
838
/**
839
 * atk_text_get_offset_at_point:
840
 * @text: an #AtkText
841
 * @x: screen x-position of character
842
 * @y: screen y-position of character
843
 * @coords: specify whether coordinates are relative to the screen or
844
 * widget window
845
 *
846
 * Gets the offset of the character located at coordinates @x and @y. @x and @y
847
 * are interpreted as being relative to the screen or this widget's window
848
 * depending on @coords.
849
 *
850
 * Returns: the offset to the character which is located at  the specified
851
 *          @x and @y coordinates of -1 in case of failure.
852
 **/
853
gint
854
1
atk_text_get_offset_at_point (AtkText *text,
855
                              gint x,
856
                              gint y,
857
                              AtkCoordType coords)
858
{
859
  AtkTextIface *iface;
860

            
861
1
  g_return_val_if_fail (ATK_IS_TEXT (text), -1);
862

            
863
1
  iface = ATK_TEXT_GET_IFACE (text);
864

            
865
1
  if (iface->get_offset_at_point)
866
1
    return (*(iface->get_offset_at_point)) (text, x, y, coords);
867
  else
868
    return -1;
869
}
870

            
871
/**
872
 * atk_text_get_n_selections:
873
 * @text: an #AtkText
874
 *
875
 * Gets the number of selected regions.
876
 *
877
 * Returns: The number of selected regions, or -1 in the case of failure.
878
 **/
879
gint
880
6
atk_text_get_n_selections (AtkText *text)
881
{
882
  AtkTextIface *iface;
883

            
884
6
  g_return_val_if_fail (ATK_IS_TEXT (text), -1);
885

            
886
6
  iface = ATK_TEXT_GET_IFACE (text);
887

            
888
6
  if (iface->get_n_selections)
889
6
    return (*(iface->get_n_selections)) (text);
890
  else
891
    return -1;
892
}
893

            
894
/**
895
 * atk_text_get_selection:
896
 * @text: an #AtkText
897
 * @selection_num: The selection number.  The selected regions are
898
 * assigned numbers that correspond to how far the region is from the
899
 * start of the text.  The selected region closest to the beginning
900
 * of the text region is assigned the number 0, etc.  Note that adding,
901
 * moving or deleting a selected region can change the numbering.
902
 * @start_offset: (out): passes back the starting character offset of the selected region
903
 * @end_offset: (out): passes back the ending character offset (offset immediately past)
904
 * of the selected region
905
 *
906
 * Gets the text from the specified selection.
907
 *
908
 * Returns: a newly allocated string containing the selected text. Use g_free()
909
 *          to free the returned string.
910
 **/
911
gchar *
912
4
atk_text_get_selection (AtkText *text,
913
                        gint selection_num,
914
                        gint *start_offset,
915
                        gint *end_offset)
916
{
917
  AtkTextIface *iface;
918
  gint local_start_offset, local_end_offset;
919
  gint *real_start_offset, *real_end_offset;
920

            
921
4
  g_return_val_if_fail (ATK_IS_TEXT (text), NULL);
922

            
923
4
  if (start_offset)
924
4
    real_start_offset = start_offset;
925
  else
926
    real_start_offset = &local_start_offset;
927
4
  if (end_offset)
928
4
    real_end_offset = end_offset;
929
  else
930
    real_end_offset = &local_end_offset;
931

            
932
4
  iface = ATK_TEXT_GET_IFACE (text);
933

            
934
4
  if (iface->get_selection)
935
    {
936
4
      return (*(iface->get_selection)) (text, selection_num,
937
                                        real_start_offset, real_end_offset);
938
    }
939
  else
940
    return NULL;
941
}
942

            
943
/**
944
 * atk_text_add_selection:
945
 * @text: an #AtkText
946
 * @start_offset: the starting character offset of the selected region
947
 * @end_offset: the offset of the first character after the selected region.
948
 *
949
 * Adds a selection bounded by the specified offsets.
950
 *
951
 * Returns: %TRUE if successful, %FALSE otherwise
952
 **/
953
gboolean
954
10
atk_text_add_selection (AtkText *text,
955
                        gint start_offset,
956
                        gint end_offset)
957
{
958
  AtkTextIface *iface;
959

            
960
10
  g_return_val_if_fail (ATK_IS_TEXT (text), FALSE);
961

            
962
10
  iface = ATK_TEXT_GET_IFACE (text);
963

            
964
10
  if (iface->add_selection)
965
10
    return (*(iface->add_selection)) (text, start_offset, end_offset);
966
  else
967
    return FALSE;
968
}
969

            
970
/**
971
 * atk_text_remove_selection:
972
 * @text: an #AtkText
973
 * @selection_num: The selection number.  The selected regions are
974
 * assigned numbers that correspond to how far the region is from the
975
 * start of the text.  The selected region closest to the beginning
976
 * of the text region is assigned the number 0, etc.  Note that adding,
977
 * moving or deleting a selected region can change the numbering.
978
 *
979
 * Removes the specified selection.
980
 *
981
 * Returns: %TRUE if successful, %FALSE otherwise
982
 **/
983
gboolean
984
2
atk_text_remove_selection (AtkText *text,
985
                           gint selection_num)
986
{
987
  AtkTextIface *iface;
988

            
989
2
  g_return_val_if_fail (ATK_IS_TEXT (text), FALSE);
990

            
991
2
  iface = ATK_TEXT_GET_IFACE (text);
992

            
993
2
  if (iface->remove_selection)
994
2
    return (*(iface->remove_selection)) (text, selection_num);
995
  else
996
    return FALSE;
997
}
998

            
999
/**
 * atk_text_set_selection:
 * @text: an #AtkText
 * @selection_num: The selection number.  The selected regions are
 * assigned numbers that correspond to how far the region is from the
 * start of the text.  The selected region closest to the beginning
 * of the text region is assigned the number 0, etc.  Note that adding,
 * moving or deleting a selected region can change the numbering.
 * @start_offset: the new starting character offset of the selection
 * @end_offset: the new end position of (e.g. offset immediately past)
 * the selection
 *
 * Changes the start and end offset of the specified selection.
 *
 * Returns: %TRUE if successful, %FALSE otherwise
 **/
gboolean
2
atk_text_set_selection (AtkText *text,
                        gint selection_num,
                        gint start_offset,
                        gint end_offset)
{
  AtkTextIface *iface;
2
  g_return_val_if_fail (ATK_IS_TEXT (text), FALSE);
2
  iface = ATK_TEXT_GET_IFACE (text);
2
  if (iface->set_selection)
    {
2
      return (*(iface->set_selection)) (text, selection_num,
                                        start_offset, end_offset);
    }
  else
    return FALSE;
}
/**
 * atk_text_set_caret_offset:
 * @text: an #AtkText
 * @offset: the character offset of the new caret position
 *
 * Sets the caret (cursor) position to the specified @offset.
 *
 * In the case of rich-text content, this method should either grab focus
 * or move the sequential focus navigation starting point (if the application
 * supports this concept) as if the user had clicked on the new caret position.
 * Typically, this means that the target of this operation is the node containing
 * the new caret position or one of its ancestors. In other words, after this
 * method is called, if the user advances focus, it should move to the first
 * focusable node following the new caret position.
 *
 * Calling this method should also scroll the application viewport in a way
 * that matches the behavior of the application's typical caret motion or tab
 * navigation as closely as possible. This also means that if the application's
 * caret motion or focus navigation does not trigger a scroll operation, this
 * method should not trigger one either. If the application does not have a caret
 * motion or focus navigation operation, this method should try to scroll the new
 * caret position into view while minimizing unnecessary scroll motion.
 *
 * Returns: %TRUE if successful, %FALSE otherwise.
 **/
gboolean
2
atk_text_set_caret_offset (AtkText *text,
                           gint offset)
{
  AtkTextIface *iface;
2
  g_return_val_if_fail (ATK_IS_TEXT (text), FALSE);
2
  iface = ATK_TEXT_GET_IFACE (text);
2
  if (iface->set_caret_offset)
    {
2
      return (*(iface->set_caret_offset)) (text, offset);
    }
  else
    {
      return FALSE;
    }
}
/**
 * atk_text_get_range_extents:
 * @text: an #AtkText
 * @start_offset: The offset of the first text character for which boundary
 *        information is required.
 * @end_offset: The offset of the text character after the last character
 *        for which boundary information is required.
 * @coord_type: Specify whether coordinates are relative to the screen or widget window.
 * @rect: (out): A pointer to a AtkTextRectangle which is filled in by this function.
 *
 * Get the bounding box for text within the specified range.
 *
 * If the extents can not be obtained (e.g. or missing support), the rectangle
 * fields are set to -1.
 *
 * Since: 1.3
 **/
void
1
atk_text_get_range_extents (AtkText *text,
                            gint start_offset,
                            gint end_offset,
                            AtkCoordType coord_type,
                            AtkTextRectangle *rect)
{
  AtkTextIface *iface;
1
  g_return_if_fail (ATK_IS_TEXT (text));
1
  g_return_if_fail (rect);
1
  g_return_if_fail (start_offset >= 0 && start_offset < end_offset);
1
  iface = ATK_TEXT_GET_IFACE (text);
1
  if (iface->get_range_extents)
1
    (*(iface->get_range_extents)) (text, start_offset, end_offset, coord_type, rect);
  else
    {
      rect->x = -1;
      rect->y = -1;
      rect->width = -1;
      rect->height = -1;
    }
}
/**
 * atk_text_get_bounded_ranges: (virtual get_bounded_ranges)
 * @text: an #AtkText
 * @rect: An AtkTextRectangle giving the dimensions of the bounding box.
 * @coord_type: Specify whether coordinates are relative to the screen or widget window.
 * @x_clip_type: Specify the horizontal clip type.
 * @y_clip_type: Specify the vertical clip type.
 *
 * Get the ranges of text in the specified bounding box.
 *
 * Since: 1.3
 *
 * Returns: (array zero-terminated=1): Array of AtkTextRange. The last
 *          element of the array returned by this function will be NULL.
 **/
AtkTextRange **
1
atk_text_get_bounded_ranges (AtkText *text,
                             AtkTextRectangle *rect,
                             AtkCoordType coord_type,
                             AtkTextClipType x_clip_type,
                             AtkTextClipType y_clip_type)
{
  AtkTextIface *iface;
1
  g_return_val_if_fail (ATK_IS_TEXT (text), NULL);
1
  g_return_val_if_fail (rect, NULL);
1
  iface = ATK_TEXT_GET_IFACE (text);
1
  if (iface->get_bounded_ranges)
1
    return (*(iface->get_bounded_ranges)) (text, rect, coord_type, x_clip_type, y_clip_type);
  else
    return NULL;
}
/**
 * atk_attribute_set_free:
 * @attrib_set: The #AtkAttributeSet to free
 *
 * Frees the memory used by an #AtkAttributeSet, including all its
 * #AtkAttributes.
 **/
void
20
atk_attribute_set_free (AtkAttributeSet *attrib_set)
{
  GSList *temp;
20
  temp = attrib_set;
52
  while (temp != NULL)
    {
      AtkAttribute *att;
32
      att = temp->data;
32
      g_free (att->name);
32
      g_free (att->value);
32
      g_free (att);
32
      temp = temp->next;
    }
20
  g_slist_free (attrib_set);
20
}
/**
 * atk_text_attribute_register:
 * @name: a name string
 *
 * Associate @name with a new #AtkTextAttribute
 *
 * Returns: an #AtkTextAttribute associated with @name
 **/
AtkTextAttribute
1
atk_text_attribute_register (const gchar *name)
{
1
  g_return_val_if_fail (name, ATK_TEXT_ATTR_INVALID);
1
  if (!extra_attributes)
1
    extra_attributes = g_ptr_array_new ();
1
  g_ptr_array_add (extra_attributes, g_strdup (name));
1
  return extra_attributes->len + ATK_TEXT_ATTR_LAST_DEFINED;
}
/**
 * atk_text_attribute_get_name:
 * @attr: The #AtkTextAttribute whose name is required
 *
 * Gets the name corresponding to the #AtkTextAttribute
 *
 * Returns: a string containing the name; this string should not be freed
 **/
const gchar *
4
atk_text_attribute_get_name (AtkTextAttribute attr)
{
  GTypeClass *type_class;
  GEnumValue *value;
4
  const gchar *name = NULL;
4
  type_class = g_type_class_ref (ATK_TYPE_TEXT_ATTRIBUTE);
4
  g_return_val_if_fail (G_IS_ENUM_CLASS (type_class), NULL);
4
  value = g_enum_get_value (G_ENUM_CLASS (type_class), attr);
4
  if (value)
    {
2
      name = value->value_nick;
    }
  else
    {
2
      if (extra_attributes)
        {
2
          gint n = attr;
2
          n -= ATK_TEXT_ATTR_LAST_DEFINED + 1;
2
          if (n < extra_attributes->len)
1
            name = g_ptr_array_index (extra_attributes, n);
        }
    }
4
  g_type_class_unref (type_class);
4
  return name;
}
/**
 * atk_text_attribute_for_name:
 * @name: a string which is the (non-localized) name of an ATK text attribute.
 *
 * Get the #AtkTextAttribute type corresponding to a text attribute name.
 *
 * Returns: the #AtkTextAttribute enumerated type corresponding to the specified
 *          name, or #ATK_TEXT_ATTRIBUTE_INVALID if no matching text attribute
 *          is found.
 **/
AtkTextAttribute
3
atk_text_attribute_for_name (const gchar *name)
{
  GTypeClass *type_class;
  GEnumValue *value;
3
  AtkTextAttribute type = ATK_TEXT_ATTR_INVALID;
3
  g_return_val_if_fail (name, ATK_TEXT_ATTR_INVALID);
3
  type_class = g_type_class_ref (ATK_TYPE_TEXT_ATTRIBUTE);
3
  g_return_val_if_fail (G_IS_ENUM_CLASS (type_class), ATK_TEXT_ATTR_INVALID);
3
  value = g_enum_get_value_by_nick (G_ENUM_CLASS (type_class), name);
3
  if (value)
    {
1
      type = value->value;
    }
  else
    {
      gint i;
2
      if (extra_attributes)
        {
3
          for (i = 0; i < extra_attributes->len; i++)
            {
2
              gchar *extra_attribute = (gchar *) g_ptr_array_index (extra_attributes, i);
2
              g_return_val_if_fail (extra_attribute, ATK_TEXT_ATTR_INVALID);
2
              if (strcmp (name, extra_attribute) == 0)
                {
1
                  type = i + 1 + ATK_TEXT_ATTR_LAST_DEFINED;
1
                  break;
                }
            }
        }
    }
3
  g_type_class_unref (type_class);
3
  return type;
}
/**
 * atk_text_attribute_get_value:
 * @attr: The #AtkTextAttribute for which a value is required
 * @index_: The index of the required value
 *
 * Gets the value for the index of the #AtkTextAttribute
 *
 * Returns: (nullable): a string containing the value; this string
 * should not be freed; %NULL is returned if there are no values
 * maintained for the attr value.
 **/
const gchar *
atk_text_attribute_get_value (AtkTextAttribute attr,
                              gint index)
{
  switch (attr)
    {
    case ATK_TEXT_ATTR_INVISIBLE:
    case ATK_TEXT_ATTR_EDITABLE:
    case ATK_TEXT_ATTR_BG_FULL_HEIGHT:
    case ATK_TEXT_ATTR_STRIKETHROUGH:
    case ATK_TEXT_ATTR_BG_STIPPLE:
    case ATK_TEXT_ATTR_FG_STIPPLE:
      g_assert (index >= 0 && index < G_N_ELEMENTS (boolean_offsets));
      return boolean + boolean_offsets[index];
    case ATK_TEXT_ATTR_UNDERLINE:
      g_assert (index >= 0 && index < G_N_ELEMENTS (underline_offsets));
      return underline + underline_offsets[index];
    case ATK_TEXT_ATTR_WRAP_MODE:
      g_assert (index >= 0 && index < G_N_ELEMENTS (wrap_mode_offsets));
      return wrap_mode + wrap_mode_offsets[index];
    case ATK_TEXT_ATTR_DIRECTION:
      g_assert (index >= 0 && index < G_N_ELEMENTS (direction_offsets));
      return direction + direction_offsets[index];
    case ATK_TEXT_ATTR_JUSTIFICATION:
      g_assert (index >= 0 && index < G_N_ELEMENTS (justification_offsets));
      return justification + justification_offsets[index];
    case ATK_TEXT_ATTR_STRETCH:
      g_assert (index >= 0 && index < G_N_ELEMENTS (stretch_offsets));
      return stretch + stretch_offsets[index];
    case ATK_TEXT_ATTR_VARIANT:
      g_assert (index >= 0 && index < G_N_ELEMENTS (variant_offsets));
      return variant + variant_offsets[index];
    case ATK_TEXT_ATTR_STYLE:
      g_assert (index >= 0 && index < G_N_ELEMENTS (style_offsets));
      return style + style_offsets[index];
    case ATK_TEXT_ATTR_TEXT_POSITION:
      g_assert (index >= 0 && index < G_N_ELEMENTS (text_position_offsets));
      return text_position + text_position_offsets[index];
    default:
      return NULL;
    }
}
static void
atk_text_rectangle_union (AtkTextRectangle *src1,
                          AtkTextRectangle *src2,
                          AtkTextRectangle *dest)
{
  gint dest_x, dest_y;
  /*
   * Some invocations of e.g. atk_text_get_character_extents
   * may return "-1" rectangles for character positions without support for
   * getting an extent. In that case we have to ignore them instead of using -1
   * values in computations.
   */
  if (src1->width == -1)
    {
      *dest = *src2;
      return;
    }
  if (src2->width == -1)
    {
      *dest = *src1;
      return;
    }
  dest_x = MIN (src1->x, src2->x);
  dest_y = MIN (src1->y, src2->y);
  dest->width = MAX (src1->x + src1->width, src2->x + src2->width) - dest_x;
  dest->height = MAX (src1->y + src1->height, src2->y + src2->height) - dest_y;
  dest->x = dest_x;
  dest->y = dest_y;
}
static gboolean
atk_text_rectangle_contain (AtkTextRectangle *clip,
                            AtkTextRectangle *bounds,
                            AtkTextClipType x_clip_type,
                            AtkTextClipType y_clip_type)
{
  gboolean x_min_ok, x_max_ok, y_min_ok, y_max_ok;
  x_min_ok = (bounds->x >= clip->x) ||
             ((bounds->x + bounds->width >= clip->x) &&
              ((x_clip_type == ATK_TEXT_CLIP_NONE) ||
               (x_clip_type == ATK_TEXT_CLIP_MAX)));
  x_max_ok = (bounds->x + bounds->width <= clip->x + clip->width) ||
             ((bounds->x <= clip->x + clip->width) &&
              ((x_clip_type == ATK_TEXT_CLIP_NONE) ||
               (x_clip_type == ATK_TEXT_CLIP_MIN)));
  y_min_ok = (bounds->y >= clip->y) ||
             ((bounds->y + bounds->height >= clip->y) &&
              ((y_clip_type == ATK_TEXT_CLIP_NONE) ||
               (y_clip_type == ATK_TEXT_CLIP_MAX)));
  y_max_ok = (bounds->y + bounds->height <= clip->y + clip->height) ||
             ((bounds->y <= clip->y + clip->height) &&
              ((y_clip_type == ATK_TEXT_CLIP_NONE) ||
               (y_clip_type == ATK_TEXT_CLIP_MIN)));
  return (x_min_ok && x_max_ok && y_min_ok && y_max_ok);
}
/**
 * atk_text_scroll_substring_to:
 * @text: an #AtkText
 * @start_offset: start offset in the @text
 * @end_offset: end offset in the @text, or -1 for the end of the text.
 * @type: specify where the object should be made visible.
 *
 * Makes a substring of @text visible on the screen by scrolling all necessary parents.
 *
 * Since: 2.32
 *
 * Returns: whether scrolling was successful.
 */
gboolean
atk_text_scroll_substring_to (AtkText *text,
                              gint start_offset,
                              gint end_offset,
                              AtkScrollType type)
{
  AtkTextIface *iface = NULL;
  g_return_val_if_fail (ATK_IS_TEXT (text), FALSE);
  iface = ATK_TEXT_GET_IFACE (text);
  if (iface->scroll_substring_to)
    return (iface->scroll_substring_to) (text, start_offset, end_offset, type);
  else
    return FALSE;
}
/**
 * atk_text_scroll_substring_to_point:
 * @text: an #AtkText
 * @start_offset: start offset in the @text
 * @end_offset: end offset in the @text, or -1 for the end of the text.
 * @coords: specify whether coordinates are relative to the screen or to the
 * parent object.
 * @x: x-position where to scroll to
 * @y: y-position where to scroll to
 *
 * Move the top-left of a substring of @text to a given position of the screen
 * by scrolling all necessary parents.
 *
 * Since: 2.32
 *
 * Returns: whether scrolling was successful.
 */
gboolean
atk_text_scroll_substring_to_point (AtkText *text,
                                    gint start_offset,
                                    gint end_offset,
                                    AtkCoordType coords,
                                    gint x,
                                    gint y)
{
  AtkTextIface *iface = NULL;
  g_return_val_if_fail (ATK_IS_TEXT (text), FALSE);
  iface = ATK_TEXT_GET_IFACE (text);
  if (iface->scroll_substring_to_point)
    return (iface->scroll_substring_to_point) (text, start_offset, end_offset, coords, x, y);
  else
    return FALSE;
}
static void
atk_text_real_get_range_extents (AtkText *text,
                                 gint start_offset,
                                 gint end_offset,
                                 AtkCoordType coord_type,
                                 AtkTextRectangle *rect)
{
  gint i;
  AtkTextRectangle cbounds, bounds;
  atk_text_get_character_extents (text, start_offset,
                                  &bounds.x, &bounds.y,
                                  &bounds.width, &bounds.height,
                                  coord_type);
  for (i = start_offset + 1; i < end_offset; i++)
    {
      atk_text_get_character_extents (text, i,
                                      &cbounds.x, &cbounds.y,
                                      &cbounds.width, &cbounds.height,
                                      coord_type);
      atk_text_rectangle_union (&bounds, &cbounds, &bounds);
    }
  rect->x = bounds.x;
  rect->y = bounds.y;
  rect->width = bounds.width;
  rect->height = bounds.height;
}
static AtkTextRange **
atk_text_real_get_bounded_ranges (AtkText *text,
                                  AtkTextRectangle *rect,
                                  AtkCoordType coord_type,
                                  AtkTextClipType x_clip_type,
                                  AtkTextClipType y_clip_type)
{
  gint bounds_min_offset, bounds_max_offset;
  gint min_line_start = 0, min_line_end = 0;
  gint max_line_start = 0, max_line_end = 0;
  gchar *line;
  gint curr_offset;
  gint offset;
  gint num_ranges = 0;
  gint range_size = 1;
  AtkTextRectangle cbounds;
  AtkTextRange **range;
  range = NULL;
  bounds_min_offset = atk_text_get_offset_at_point (text, rect->x, rect->y, coord_type);
  bounds_max_offset = atk_text_get_offset_at_point (text, rect->x + rect->width, rect->y + rect->height, coord_type);
  if (bounds_min_offset == 0 &&
      bounds_min_offset == bounds_max_offset)
    return NULL;
  line = atk_text_get_text_at_offset (text, bounds_min_offset,
                                      ATK_TEXT_BOUNDARY_LINE_START,
                                      &min_line_start, &min_line_end);
  g_free (line);
  line = atk_text_get_text_at_offset (text, bounds_max_offset,
                                      ATK_TEXT_BOUNDARY_LINE_START,
                                      &max_line_start, &max_line_end);
  g_free (line);
  bounds_min_offset = MIN (min_line_start, max_line_start);
  bounds_max_offset = MAX (min_line_end, max_line_end);
  curr_offset = bounds_min_offset;
  while (curr_offset < bounds_max_offset)
    {
      offset = curr_offset;
      while (curr_offset < bounds_max_offset)
        {
          atk_text_get_character_extents (text, curr_offset,
                                          &cbounds.x, &cbounds.y,
                                          &cbounds.width, &cbounds.height,
                                          coord_type);
          if (!atk_text_rectangle_contain (rect, &cbounds, x_clip_type, y_clip_type))
            break;
          curr_offset++;
        }
      if (curr_offset > offset)
        {
          AtkTextRange *one_range = g_new (AtkTextRange, 1);
          one_range->start_offset = offset;
          one_range->end_offset = curr_offset;
          one_range->content = atk_text_get_text (text, offset, curr_offset);
          atk_text_get_range_extents (text, offset, curr_offset, coord_type, &one_range->bounds);
          if (num_ranges >= range_size - 1)
            {
              range_size *= 2;
              range = g_realloc (range, range_size * sizeof (gpointer));
            }
          range[num_ranges] = one_range;
          num_ranges++;
        }
      curr_offset++;
      if (range)
        range[num_ranges] = NULL;
    }
  return range;
}
/**
 * atk_text_free_ranges:
 * @ranges: (array): A pointer to an array of #AtkTextRange which is
 *   to be freed.
 *
 * Frees the memory associated with an array of AtkTextRange. It is assumed
 * that the array was returned by the function atk_text_get_bounded_ranges
 * and is NULL terminated.
 *
 * Since: 1.3
 **/
void
atk_text_free_ranges (AtkTextRange **ranges)
{
  AtkTextRange **first = ranges;
  if (ranges)
    {
      while (*ranges)
        {
          AtkTextRange *range;
          range = *ranges;
          ranges++;
          g_free (range->content);
          g_free (range);
        }
      g_free (first);
    }
}
static AtkTextRange *
atk_text_range_copy (AtkTextRange *src)
{
  AtkTextRange *dst = g_new0 (AtkTextRange, 1);
  dst->bounds = src->bounds;
  dst->start_offset = src->start_offset;
  dst->end_offset = src->end_offset;
  if (src->content)
    dst->content = g_strdup (src->content);
  return dst;
}
static void
atk_text_range_free (AtkTextRange *range)
{
  g_free (range->content);
  g_free (range);
}
G_DEFINE_BOXED_TYPE (AtkTextRange, atk_text_range, atk_text_range_copy, atk_text_range_free)