1 /* GAIL - The GNOME Accessibility Implementation Library
2 * Copyright 2001 Sun Microsystems Inc.
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.
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.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
21 #include "gailtextutil.h"
24 * SECTION:gailtextutil
25 * @Short_description: GailTextUtil is a utility class which can be used to
26 * implement some of the #AtkText functions for accessible objects
27 * which implement #AtkText.
28 * @Title: GailTextUtil
30 * GailTextUtil is a utility class which can be used to implement the
31 * #AtkText functions which get text for accessible objects which implement
34 * In GAIL it is used by the accsesible objects for #GnomeCanvasText, #GtkEntry,
35 * #GtkLabel, #GtkCellRendererText and #GtkTextView.
38 static void gail_text_util_class_init (GailTextUtilClass *klass);
40 static void gail_text_util_init (GailTextUtil *textutil);
41 static void gail_text_util_finalize (GObject *object);
44 static void get_pango_text_offsets (PangoLayout *layout,
45 GtkTextBuffer *buffer,
46 GailOffsetType function,
47 AtkTextBoundary boundary_type,
51 GtkTextIter *start_iter,
52 GtkTextIter *end_iter);
53 static GObjectClass *parent_class = NULL;
56 gail_text_util_get_type(void)
58 static GType type = 0;
62 const GTypeInfo tinfo =
64 sizeof (GailTextUtilClass),
65 (GBaseInitFunc) NULL, /* base init */
66 (GBaseFinalizeFunc) NULL, /* base finalize */
67 (GClassInitFunc) gail_text_util_class_init,
68 (GClassFinalizeFunc) NULL, /* class finalize */
69 NULL, /* class data */
72 (GInstanceInitFunc) gail_text_util_init,
73 NULL, /* value table */
76 type = g_type_register_static (G_TYPE_OBJECT, "GailTextUtil", &tinfo, 0);
84 * This function creates a new GailTextUtil object.
86 * Returns: the GailTextUtil object
89 gail_text_util_new (void)
91 return GAIL_TEXT_UTIL (g_object_new (GAIL_TYPE_TEXT_UTIL, NULL));
95 gail_text_util_init (GailTextUtil *textutil)
97 textutil->buffer = NULL;
101 gail_text_util_class_init (GailTextUtilClass *klass)
103 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
105 parent_class = g_type_class_peek_parent (klass);
107 gobject_class->finalize = gail_text_util_finalize;
111 gail_text_util_finalize (GObject *object)
113 GailTextUtil *textutil = GAIL_TEXT_UTIL (object);
115 if (textutil->buffer)
116 g_object_unref (textutil->buffer);
118 G_OBJECT_CLASS (parent_class)->finalize (object);
122 * gail_text_util_text_setup:
123 * @textutil: The #GailTextUtil to be initialized.
124 * @text: A gchar* which points to the text to be stored in the GailTextUtil
126 * This function initializes the GailTextUtil with the specified character string,
129 gail_text_util_text_setup (GailTextUtil *textutil,
132 g_return_if_fail (GAIL_IS_TEXT_UTIL (textutil));
134 if (textutil->buffer)
138 g_object_unref (textutil->buffer);
139 textutil->buffer = NULL;
145 textutil->buffer = gtk_text_buffer_new (NULL);
148 gtk_text_buffer_set_text (textutil->buffer, text, -1);
152 * gail_text_util_buffer_setup:
153 * @textutil: A #GailTextUtil to be initialized
154 * @buffer: The #GtkTextBuffer which identifies the text to be stored in the GailUtil.
156 * This function initializes the GailTextUtil with the specified GtkTextBuffer
159 gail_text_util_buffer_setup (GailTextUtil *textutil,
160 GtkTextBuffer *buffer)
162 g_return_if_fail (GAIL_IS_TEXT_UTIL (textutil));
164 textutil->buffer = g_object_ref (buffer);
168 * gail_text_util_get_text:
169 * @textutil: A #GailTextUtil
170 * @layout: A gpointer which is a PangoLayout, a GtkTreeView of NULL
171 * @function: An enumeration specifying whether to return the text before, at, or
173 * @boundary_type: The boundary type.
174 * @offset: The offset of the text in the GailTextUtil
175 * @start_offset: Address of location in which the start offset is returned
176 * @end_offset: Address of location in which the end offset is returned
178 * This function gets the requested substring from the text in the GtkTextUtil.
179 * The layout is used only for getting the text on a line. The value is NULL
180 * for a GtkTextView which is not wrapped, is a GtkTextView for a GtkTextView
181 * which is wrapped and is a PangoLayout otherwise.
183 * Returns: the substring requested
186 gail_text_util_get_text (GailTextUtil *textutil,
188 GailOffsetType function,
189 AtkTextBoundary boundary_type,
194 GtkTextIter start, end;
196 GtkTextBuffer *buffer;
198 g_return_val_if_fail (GAIL_IS_TEXT_UTIL (textutil), NULL);
200 buffer = textutil->buffer;
208 if (!gtk_text_buffer_get_char_count (buffer))
212 return g_strdup ("");
214 gtk_text_buffer_get_iter_at_offset (buffer, &start, offset);
221 case GAIL_BEFORE_OFFSET:
222 switch (boundary_type)
224 case ATK_TEXT_BOUNDARY_CHAR:
225 gtk_text_iter_backward_char(&start);
227 case ATK_TEXT_BOUNDARY_WORD_START:
228 if (!gtk_text_iter_starts_word (&start))
229 gtk_text_iter_backward_word_start (&start);
231 gtk_text_iter_backward_word_start(&start);
233 case ATK_TEXT_BOUNDARY_WORD_END:
234 if (gtk_text_iter_inside_word (&start) &&
235 !gtk_text_iter_starts_word (&start))
236 gtk_text_iter_backward_word_start (&start);
237 while (!gtk_text_iter_ends_word (&start))
239 if (!gtk_text_iter_backward_char (&start))
243 gtk_text_iter_backward_word_start(&start);
244 while (!gtk_text_iter_ends_word (&start))
246 if (!gtk_text_iter_backward_char (&start))
250 case ATK_TEXT_BOUNDARY_SENTENCE_START:
251 if (!gtk_text_iter_starts_sentence (&start))
252 gtk_text_iter_backward_sentence_start (&start);
254 gtk_text_iter_backward_sentence_start (&start);
256 case ATK_TEXT_BOUNDARY_SENTENCE_END:
257 if (gtk_text_iter_inside_sentence (&start) &&
258 !gtk_text_iter_starts_sentence (&start))
259 gtk_text_iter_backward_sentence_start (&start);
260 while (!gtk_text_iter_ends_sentence (&start))
262 if (!gtk_text_iter_backward_char (&start))
266 gtk_text_iter_backward_sentence_start (&start);
267 while (!gtk_text_iter_ends_sentence (&start))
269 if (!gtk_text_iter_backward_char (&start))
273 case ATK_TEXT_BOUNDARY_LINE_START:
276 line_number = gtk_text_iter_get_line (&start);
277 if (line_number == 0)
279 gtk_text_buffer_get_iter_at_offset (buffer,
284 gtk_text_iter_backward_line (&start);
285 gtk_text_iter_forward_line (&start);
288 gtk_text_iter_backward_line (&start);
290 else if GTK_IS_TEXT_VIEW (layout)
292 GtkTextView *view = GTK_TEXT_VIEW (layout);
294 gtk_text_view_backward_display_line_start (view, &start);
296 gtk_text_view_backward_display_line (view, &start);
298 else if (PANGO_IS_LAYOUT (layout))
299 get_pango_text_offsets (PANGO_LAYOUT (layout),
309 case ATK_TEXT_BOUNDARY_LINE_END:
312 line_number = gtk_text_iter_get_line (&start);
313 if (line_number == 0)
315 gtk_text_buffer_get_iter_at_offset (buffer,
321 gtk_text_iter_backward_line (&start);
323 while (!gtk_text_iter_ends_line (&start))
325 if (!gtk_text_iter_backward_char (&start))
328 gtk_text_iter_forward_to_line_end (&end);
331 else if GTK_IS_TEXT_VIEW (layout)
333 GtkTextView *view = GTK_TEXT_VIEW (layout);
335 gtk_text_view_backward_display_line_start (view, &start);
336 if (!gtk_text_iter_is_start (&start))
338 gtk_text_view_backward_display_line (view, &start);
340 if (!gtk_text_iter_is_start (&start))
342 gtk_text_view_backward_display_line (view, &start);
343 gtk_text_view_forward_display_line_end (view, &start);
345 gtk_text_view_forward_display_line_end (view, &end);
352 else if (PANGO_IS_LAYOUT (layout))
353 get_pango_text_offsets (PANGO_LAYOUT (layout),
367 switch (boundary_type)
369 case ATK_TEXT_BOUNDARY_CHAR:
370 gtk_text_iter_forward_char (&end);
372 case ATK_TEXT_BOUNDARY_WORD_START:
373 if (!gtk_text_iter_starts_word (&start))
374 gtk_text_iter_backward_word_start (&start);
375 if (gtk_text_iter_inside_word (&end))
376 gtk_text_iter_forward_word_end (&end);
377 while (!gtk_text_iter_starts_word (&end))
379 if (!gtk_text_iter_forward_char (&end))
383 case ATK_TEXT_BOUNDARY_WORD_END:
384 if (gtk_text_iter_inside_word (&start) &&
385 !gtk_text_iter_starts_word (&start))
386 gtk_text_iter_backward_word_start (&start);
387 while (!gtk_text_iter_ends_word (&start))
389 if (!gtk_text_iter_backward_char (&start))
392 gtk_text_iter_forward_word_end (&end);
394 case ATK_TEXT_BOUNDARY_SENTENCE_START:
395 if (!gtk_text_iter_starts_sentence (&start))
396 gtk_text_iter_backward_sentence_start (&start);
397 if (gtk_text_iter_inside_sentence (&end))
398 gtk_text_iter_forward_sentence_end (&end);
399 while (!gtk_text_iter_starts_sentence (&end))
401 if (!gtk_text_iter_forward_char (&end))
405 case ATK_TEXT_BOUNDARY_SENTENCE_END:
406 if (gtk_text_iter_inside_sentence (&start) &&
407 !gtk_text_iter_starts_sentence (&start))
408 gtk_text_iter_backward_sentence_start (&start);
409 while (!gtk_text_iter_ends_sentence (&start))
411 if (!gtk_text_iter_backward_char (&start))
414 gtk_text_iter_forward_sentence_end (&end);
416 case ATK_TEXT_BOUNDARY_LINE_START:
419 line_number = gtk_text_iter_get_line (&start);
420 if (line_number == 0)
422 gtk_text_buffer_get_iter_at_offset (buffer,
427 gtk_text_iter_backward_line (&start);
428 gtk_text_iter_forward_line (&start);
430 gtk_text_iter_forward_line (&end);
432 else if GTK_IS_TEXT_VIEW (layout)
434 GtkTextView *view = GTK_TEXT_VIEW (layout);
436 gtk_text_view_backward_display_line_start (view, &start);
438 * The call to gtk_text_iter_forward_to_end() is needed
439 * because of bug 81960
441 if (!gtk_text_view_forward_display_line (view, &end))
442 gtk_text_iter_forward_to_end (&end);
444 else if PANGO_IS_LAYOUT (layout)
445 get_pango_text_offsets (PANGO_LAYOUT (layout),
456 case ATK_TEXT_BOUNDARY_LINE_END:
459 line_number = gtk_text_iter_get_line (&start);
460 if (line_number == 0)
462 gtk_text_buffer_get_iter_at_offset (buffer,
467 gtk_text_iter_backward_line (&start);
468 gtk_text_iter_forward_line (&start);
470 while (!gtk_text_iter_ends_line (&start))
472 if (!gtk_text_iter_backward_char (&start))
475 gtk_text_iter_forward_to_line_end (&end);
477 else if GTK_IS_TEXT_VIEW (layout)
479 GtkTextView *view = GTK_TEXT_VIEW (layout);
481 gtk_text_view_backward_display_line_start (view, &start);
482 if (!gtk_text_iter_is_start (&start))
484 gtk_text_view_backward_display_line (view, &start);
485 gtk_text_view_forward_display_line_end (view, &start);
487 gtk_text_view_forward_display_line_end (view, &end);
489 else if PANGO_IS_LAYOUT (layout)
490 get_pango_text_offsets (PANGO_LAYOUT (layout),
503 case GAIL_AFTER_OFFSET:
504 switch (boundary_type)
506 case ATK_TEXT_BOUNDARY_CHAR:
507 gtk_text_iter_forward_char(&start);
508 gtk_text_iter_forward_chars(&end, 2);
510 case ATK_TEXT_BOUNDARY_WORD_START:
511 if (gtk_text_iter_inside_word (&end))
512 gtk_text_iter_forward_word_end (&end);
513 while (!gtk_text_iter_starts_word (&end))
515 if (!gtk_text_iter_forward_char (&end))
519 if (!gtk_text_iter_is_end (&end))
521 gtk_text_iter_forward_word_end (&end);
522 while (!gtk_text_iter_starts_word (&end))
524 if (!gtk_text_iter_forward_char (&end))
529 case ATK_TEXT_BOUNDARY_WORD_END:
530 gtk_text_iter_forward_word_end (&end);
532 if (!gtk_text_iter_is_end (&end))
533 gtk_text_iter_forward_word_end (&end);
535 case ATK_TEXT_BOUNDARY_SENTENCE_START:
536 if (gtk_text_iter_inside_sentence (&end))
537 gtk_text_iter_forward_sentence_end (&end);
538 while (!gtk_text_iter_starts_sentence (&end))
540 if (!gtk_text_iter_forward_char (&end))
544 if (!gtk_text_iter_is_end (&end))
546 gtk_text_iter_forward_sentence_end (&end);
547 while (!gtk_text_iter_starts_sentence (&end))
549 if (!gtk_text_iter_forward_char (&end))
554 case ATK_TEXT_BOUNDARY_SENTENCE_END:
555 gtk_text_iter_forward_sentence_end (&end);
557 if (!gtk_text_iter_is_end (&end))
558 gtk_text_iter_forward_sentence_end (&end);
560 case ATK_TEXT_BOUNDARY_LINE_START:
563 gtk_text_iter_forward_line (&end);
565 gtk_text_iter_forward_line (&end);
567 else if GTK_IS_TEXT_VIEW (layout)
569 GtkTextView *view = GTK_TEXT_VIEW (layout);
571 gtk_text_view_forward_display_line (view, &end);
573 gtk_text_view_forward_display_line (view, &end);
575 else if (PANGO_IS_LAYOUT (layout))
576 get_pango_text_offsets (PANGO_LAYOUT (layout),
586 case ATK_TEXT_BOUNDARY_LINE_END:
589 gtk_text_iter_forward_line (&start);
591 if (!gtk_text_iter_is_end (&start))
593 while (!gtk_text_iter_ends_line (&start))
595 if (!gtk_text_iter_backward_char (&start))
598 gtk_text_iter_forward_to_line_end (&end);
601 else if GTK_IS_TEXT_VIEW (layout)
603 GtkTextView *view = GTK_TEXT_VIEW (layout);
605 gtk_text_view_forward_display_line_end (view, &end);
607 gtk_text_view_forward_display_line (view, &end);
608 gtk_text_view_forward_display_line_end (view, &end);
610 else if (PANGO_IS_LAYOUT (layout))
611 get_pango_text_offsets (PANGO_LAYOUT (layout),
624 *start_offset = gtk_text_iter_get_offset (&start);
625 *end_offset = gtk_text_iter_get_offset (&end);
627 return gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
631 * gail_text_util_get_substring:
632 * @textutil: A #GailTextUtil
633 * @start_pos: The start position of the substring
634 * @end_pos: The end position of the substring.
636 * Gets the substring indicated by @start_pos and @end_pos
638 * Returns: the substring indicated by @start_pos and @end_pos
641 gail_text_util_get_substring (GailTextUtil *textutil,
645 GtkTextIter start, end;
646 GtkTextBuffer *buffer;
648 g_return_val_if_fail(GAIL_IS_TEXT_UTIL (textutil), NULL);
650 buffer = textutil->buffer;
654 gtk_text_buffer_get_iter_at_offset (buffer, &start, start_pos);
656 gtk_text_buffer_get_end_iter (buffer, &end);
658 gtk_text_buffer_get_iter_at_offset (buffer, &end, end_pos);
660 return gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
664 get_pango_text_offsets (PangoLayout *layout,
665 GtkTextBuffer *buffer,
666 GailOffsetType function,
667 AtkTextBoundary boundary_type,
671 GtkTextIter *start_iter,
672 GtkTextIter *end_iter)
674 PangoLayoutIter *iter;
675 PangoLayoutLine *line, *prev_line = NULL, *prev_prev_line = NULL;
676 gint index, start_index, end_index;
678 gboolean found = FALSE;
680 text = pango_layout_get_text (layout);
681 index = g_utf8_offset_to_pointer (text, offset) - text;
682 iter = pango_layout_get_iter (layout);
685 line = pango_layout_iter_get_line (iter);
686 start_index = line->start_index;
687 end_index = start_index + line->length;
689 if (index >= start_index && index <= end_index)
692 * Found line for offset
696 case GAIL_BEFORE_OFFSET:
698 * We want the previous line
702 switch (boundary_type)
704 case ATK_TEXT_BOUNDARY_LINE_START:
705 end_index = start_index;
706 start_index = prev_line->start_index;
708 case ATK_TEXT_BOUNDARY_LINE_END:
710 start_index = prev_prev_line->start_index +
711 prev_prev_line->length;
712 end_index = prev_line->start_index + prev_line->length;
715 g_assert_not_reached();
719 start_index = end_index = 0;
722 switch (boundary_type)
724 case ATK_TEXT_BOUNDARY_LINE_START:
725 if (pango_layout_iter_next_line (iter))
726 end_index = pango_layout_iter_get_line (iter)->start_index;
728 case ATK_TEXT_BOUNDARY_LINE_END:
730 start_index = prev_line->start_index +
734 g_assert_not_reached();
737 case GAIL_AFTER_OFFSET:
739 * We want the next line
741 if (pango_layout_iter_next_line (iter))
743 line = pango_layout_iter_get_line (iter);
744 switch (boundary_type)
746 case ATK_TEXT_BOUNDARY_LINE_START:
747 start_index = line->start_index;
748 if (pango_layout_iter_next_line (iter))
749 end_index = pango_layout_iter_get_line (iter)->start_index;
751 end_index = start_index + line->length;
753 case ATK_TEXT_BOUNDARY_LINE_END:
754 start_index = end_index;
755 end_index = line->start_index + line->length;
758 g_assert_not_reached();
762 start_index = end_index;
768 prev_prev_line = prev_line;
771 while (pango_layout_iter_next_line (iter));
775 start_index = prev_line->start_index + prev_line->length;
776 end_index = start_index;
778 pango_layout_iter_free (iter);
779 *start_offset = g_utf8_pointer_to_offset (text, text + start_index);
780 *end_offset = g_utf8_pointer_to_offset (text, text + end_index);
782 gtk_text_buffer_get_iter_at_offset (buffer, start_iter, *start_offset);
783 gtk_text_buffer_get_iter_at_offset (buffer, end_iter, *end_offset);