1 /* GTK - The GIMP Toolkit
2 * gtktextlayout.c - calculate the layout of the text
4 * Copyright (c) 1992-1994 The Regents of the University of California.
5 * Copyright (c) 1994-1997 Sun Microsystems, Inc.
6 * Copyright (c) 2000 Red Hat, Inc.
7 * Tk->Gtk port by Havoc Pennington
8 * Pango support by Owen Taylor
10 * This file can be used under your choice of two licenses, the LGPL
11 * and the original Tk license.
15 * This library is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU Lesser General Public
17 * License as published by the Free Software Foundation; either
18 * version 2 of the License, or (at your option) any later version.
20 * This library is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 * Lesser General Public License for more details.
25 * You should have received a copy of the GNU Lesser General Public
26 * License along with this library. If not, see <http://www.gnu.org/licenses/>.Free
28 * Original Tk license:
30 * This software is copyrighted by the Regents of the University of
31 * California, Sun Microsystems, Inc., and other parties. The
32 * following terms apply to all files associated with the software
33 * unless explicitly disclaimed in individual files.
35 * The authors hereby grant permission to use, copy, modify,
36 * distribute, and license this software and its documentation for any
37 * purpose, provided that existing copyright notices are retained in
38 * all copies and that this notice is included verbatim in any
39 * distributions. No written agreement, license, or royalty fee is
40 * required for any of the authorized uses. Modifications to this
41 * software may be copyrighted by their authors and need not follow
42 * the licensing terms described here, provided that the new terms are
43 * clearly indicated on the first page of each file where they apply.
45 * IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY
46 * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
47 * DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION,
48 * OR ANY DERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE BEEN ADVISED
49 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES,
52 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
53 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND
54 * NON-INFRINGEMENT. THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS,
55 * AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO PROVIDE
56 * MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
58 * GOVERNMENT USE: If you are acquiring this software on behalf of the
59 * U.S. government, the Government shall have only "Restricted Rights"
60 * in the software and related documentation as defined in the Federal
61 * Acquisition Regulations (FARs) in Clause 52.227.19 (c) (2). If you
62 * are acquiring the software on behalf of the Department of Defense,
63 * the software shall be classified as "Commercial Computer Software"
64 * and the Government shall have only "Restricted Rights" as defined
65 * in Clause 252.227-7013 (c) (1) of DFARs. Notwithstanding the
66 * foregoing, the authors grant the U.S. Government and others acting
67 * in its behalf permission to use and distribute the software in
68 * accordance with the terms specified in this license.
72 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
73 * file for a list of people on the GTK+ Team. See the ChangeLog
74 * files for a list of changes. These files are distributed with
75 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
78 #define GTK_TEXT_USE_INTERNAL_UNSUPPORTED_API
80 #include "gtkmarshalers.h"
81 #include "gtktextlayout.h"
82 #include "gtktextbtree.h"
83 #include "gtktextiterprivate.h"
84 #include "gtktextutil.h"
90 #define GTK_TEXT_LAYOUT_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GTK_TYPE_TEXT_LAYOUT, GtkTextLayoutPrivate))
92 typedef struct _GtkTextLayoutPrivate GtkTextLayoutPrivate;
94 struct _GtkTextLayoutPrivate
96 /* Cache the line that the cursor is positioned on, as the keyboard
97 direction only influences the direction of the cursor line.
99 GtkTextLine *cursor_line;
102 static GtkTextLineData *gtk_text_layout_real_wrap (GtkTextLayout *layout,
105 GtkTextLineData *line_data);
107 static void gtk_text_layout_invalidated (GtkTextLayout *layout);
109 static void gtk_text_layout_real_invalidate (GtkTextLayout *layout,
110 const GtkTextIter *start,
111 const GtkTextIter *end);
112 static void gtk_text_layout_real_invalidate_cursors(GtkTextLayout *layout,
113 const GtkTextIter *start,
114 const GtkTextIter *end);
115 static void gtk_text_layout_invalidate_cache (GtkTextLayout *layout,
117 gboolean cursors_only);
118 static void gtk_text_layout_invalidate_cursor_line (GtkTextLayout *layout,
119 gboolean cursors_only);
120 static void gtk_text_layout_real_free_line_data (GtkTextLayout *layout,
122 GtkTextLineData *line_data);
123 static void gtk_text_layout_emit_changed (GtkTextLayout *layout,
128 static void gtk_text_layout_invalidate_all (GtkTextLayout *layout);
130 static PangoAttribute *gtk_text_attr_appearance_new (const GtkTextAppearance *appearance);
132 static void gtk_text_layout_mark_set_handler (GtkTextBuffer *buffer,
133 const GtkTextIter *location,
136 static void gtk_text_layout_buffer_insert_text (GtkTextBuffer *textbuffer,
141 static void gtk_text_layout_buffer_delete_range (GtkTextBuffer *textbuffer,
146 static void gtk_text_layout_update_cursor_line (GtkTextLayout *layout);
148 static void line_display_index_to_iter (GtkTextLayout *layout,
149 GtkTextLineDisplay *display,
154 static gint line_display_iter_to_index (GtkTextLayout *layout,
155 GtkTextLineDisplay *display,
156 const GtkTextIter *iter);
170 #define PIXEL_BOUND(d) (((d) + PANGO_SCALE - 1) / PANGO_SCALE)
172 static guint signals[LAST_SIGNAL] = { 0 };
174 PangoAttrType gtk_text_attr_appearance_type = 0;
176 G_DEFINE_TYPE (GtkTextLayout, gtk_text_layout, G_TYPE_OBJECT)
179 gtk_text_layout_dispose (GObject *object)
181 GtkTextLayout *layout;
183 layout = GTK_TEXT_LAYOUT (object);
185 gtk_text_layout_set_buffer (layout, NULL);
187 if (layout->default_style != NULL)
189 gtk_text_attributes_unref (layout->default_style);
190 layout->default_style = NULL;
193 g_clear_object (&layout->ltr_context);
194 g_clear_object (&layout->rtl_context);
196 if (layout->one_display_cache)
198 GtkTextLineDisplay *tmp_display = layout->one_display_cache;
199 layout->one_display_cache = NULL;
200 gtk_text_layout_free_line_display (layout, tmp_display);
203 if (layout->preedit_attrs != NULL)
205 pango_attr_list_unref (layout->preedit_attrs);
206 layout->preedit_attrs = NULL;
209 G_OBJECT_CLASS (gtk_text_layout_parent_class)->dispose (object);
213 gtk_text_layout_finalize (GObject *object)
215 GtkTextLayout *layout;
217 layout = GTK_TEXT_LAYOUT (object);
219 g_free (layout->preedit_string);
221 G_OBJECT_CLASS (gtk_text_layout_parent_class)->finalize (object);
225 gtk_text_layout_class_init (GtkTextLayoutClass *klass)
227 GObjectClass *object_class = G_OBJECT_CLASS (klass);
229 object_class->dispose = gtk_text_layout_dispose;
230 object_class->finalize = gtk_text_layout_finalize;
232 klass->wrap = gtk_text_layout_real_wrap;
233 klass->invalidate = gtk_text_layout_real_invalidate;
234 klass->invalidate_cursors = gtk_text_layout_real_invalidate_cursors;
235 klass->free_line_data = gtk_text_layout_real_free_line_data;
237 signals[INVALIDATED] =
238 g_signal_new (I_("invalidated"),
239 G_OBJECT_CLASS_TYPE (object_class),
241 G_STRUCT_OFFSET (GtkTextLayoutClass, invalidated),
243 _gtk_marshal_VOID__VOID,
248 g_signal_new (I_("changed"),
249 G_OBJECT_CLASS_TYPE (object_class),
251 G_STRUCT_OFFSET (GtkTextLayoutClass, changed),
253 _gtk_marshal_VOID__INT_INT_INT,
260 signals[ALLOCATE_CHILD] =
261 g_signal_new (I_("allocate-child"),
262 G_OBJECT_CLASS_TYPE (object_class),
264 G_STRUCT_OFFSET (GtkTextLayoutClass, allocate_child),
266 _gtk_marshal_VOID__OBJECT_INT_INT,
273 g_type_class_add_private (object_class, sizeof (GtkTextLayoutPrivate));
277 gtk_text_layout_init (GtkTextLayout *text_layout)
279 text_layout->cursor_visible = TRUE;
283 gtk_text_layout_new (void)
285 return g_object_new (GTK_TYPE_TEXT_LAYOUT, NULL);
289 free_style_cache (GtkTextLayout *text_layout)
291 if (text_layout->one_style_cache)
293 gtk_text_attributes_unref (text_layout->one_style_cache);
294 text_layout->one_style_cache = NULL;
299 * gtk_text_layout_set_buffer:
300 * @buffer: (allow-none):
303 gtk_text_layout_set_buffer (GtkTextLayout *layout,
304 GtkTextBuffer *buffer)
306 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
307 g_return_if_fail (buffer == NULL || GTK_IS_TEXT_BUFFER (buffer));
309 if (layout->buffer == buffer)
312 free_style_cache (layout);
316 _gtk_text_btree_remove_view (_gtk_text_buffer_get_btree (layout->buffer),
319 g_signal_handlers_disconnect_by_func (layout->buffer,
320 G_CALLBACK (gtk_text_layout_mark_set_handler),
322 g_signal_handlers_disconnect_by_func (layout->buffer,
323 G_CALLBACK (gtk_text_layout_buffer_insert_text),
325 g_signal_handlers_disconnect_by_func (layout->buffer,
326 G_CALLBACK (gtk_text_layout_buffer_delete_range),
329 g_object_unref (layout->buffer);
330 layout->buffer = NULL;
335 layout->buffer = buffer;
337 g_object_ref (buffer);
339 _gtk_text_btree_add_view (_gtk_text_buffer_get_btree (buffer), layout);
341 /* Bind to all signals that move the insert mark. */
342 g_signal_connect_after (layout->buffer, "mark-set",
343 G_CALLBACK (gtk_text_layout_mark_set_handler), layout);
344 g_signal_connect_after (layout->buffer, "insert-text",
345 G_CALLBACK (gtk_text_layout_buffer_insert_text), layout);
346 g_signal_connect_after (layout->buffer, "delete-range",
347 G_CALLBACK (gtk_text_layout_buffer_delete_range), layout);
349 gtk_text_layout_update_cursor_line (layout);
354 gtk_text_layout_default_style_changed (GtkTextLayout *layout)
356 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
358 DV (g_print ("invalidating all due to default style change (%s)\n", G_STRLOC));
359 gtk_text_layout_invalidate_all (layout);
363 gtk_text_layout_set_default_style (GtkTextLayout *layout,
364 GtkTextAttributes *values)
366 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
367 g_return_if_fail (values != NULL);
369 if (values == layout->default_style)
372 gtk_text_attributes_ref (values);
374 if (layout->default_style)
375 gtk_text_attributes_unref (layout->default_style);
377 layout->default_style = values;
379 gtk_text_layout_default_style_changed (layout);
383 gtk_text_layout_set_contexts (GtkTextLayout *layout,
384 PangoContext *ltr_context,
385 PangoContext *rtl_context)
387 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
389 if (layout->ltr_context != ltr_context)
391 if (layout->ltr_context)
392 g_object_unref (layout->ltr_context);
394 layout->ltr_context = ltr_context;
395 g_object_ref (layout->ltr_context);
398 if (layout->rtl_context != rtl_context)
400 if (layout->rtl_context)
401 g_object_unref (layout->rtl_context);
403 layout->rtl_context = rtl_context;
404 g_object_ref (layout->rtl_context);
407 DV (g_print ("invalidating all due to new pango contexts (%s)\n", G_STRLOC));
408 gtk_text_layout_invalidate_all (layout);
412 * gtk_text_layout_set_overwrite_mode:
413 * @layout: a #GtkTextLayout
414 * @overwrite: overwrite mode
416 * Sets overwrite mode
419 gtk_text_layout_set_overwrite_mode (GtkTextLayout *layout,
422 overwrite = overwrite != 0;
423 if (overwrite != layout->overwrite_mode)
425 layout->overwrite_mode = overwrite;
426 gtk_text_layout_invalidate_cursor_line (layout, TRUE);
431 * gtk_text_layout_set_cursor_direction:
432 * @direction: the new direction(s) for which to draw cursors.
433 * %GTK_TEXT_DIR_NONE means draw cursors for both
434 * left-to-right insertion and right-to-left insertion.
435 * (The two cursors will be visually distinguished.)
437 * Sets which text directions (left-to-right and/or right-to-left) for
438 * which cursors will be drawn for the insertion point. The visual
439 * point at which new text is inserted depends on whether the new
440 * text is right-to-left or left-to-right, so it may be desired to
441 * make the drawn position of the cursor depend on the keyboard state.
444 gtk_text_layout_set_cursor_direction (GtkTextLayout *layout,
445 GtkTextDirection direction)
447 if (direction != layout->cursor_direction)
449 layout->cursor_direction = direction;
450 gtk_text_layout_invalidate_cursor_line (layout, TRUE);
455 * gtk_text_layout_set_keyboard_direction:
456 * @keyboard_dir: the current direction of the keyboard.
458 * Sets the keyboard direction; this is used as for the bidirectional
459 * base direction for the line with the cursor if the line contains
460 * only neutral characters.
463 gtk_text_layout_set_keyboard_direction (GtkTextLayout *layout,
464 GtkTextDirection keyboard_dir)
466 if (keyboard_dir != layout->keyboard_direction)
468 layout->keyboard_direction = keyboard_dir;
469 gtk_text_layout_invalidate_cursor_line (layout, TRUE);
474 * gtk_text_layout_get_buffer:
475 * @layout: a #GtkTextLayout
477 * Gets the text buffer used by the layout. See
478 * gtk_text_layout_set_buffer().
480 * Return value: the text buffer used by the layout.
483 gtk_text_layout_get_buffer (GtkTextLayout *layout)
485 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), NULL);
487 return layout->buffer;
491 gtk_text_layout_set_screen_width (GtkTextLayout *layout, gint width)
493 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
494 g_return_if_fail (width >= 0);
495 g_return_if_fail (layout->wrap_loop_count == 0);
497 if (layout->screen_width == width)
500 layout->screen_width = width;
502 DV (g_print ("invalidating all due to new screen width (%s)\n", G_STRLOC));
503 gtk_text_layout_invalidate_all (layout);
507 * gtk_text_layout_set_cursor_visible:
508 * @layout: a #GtkTextLayout
509 * @cursor_visible: If %FALSE, then the insertion cursor will not
510 * be shown, even if the text is editable.
512 * Sets whether the insertion cursor should be shown. Generally,
513 * widgets using #GtkTextLayout will hide the cursor when the
514 * widget does not have the input focus.
517 gtk_text_layout_set_cursor_visible (GtkTextLayout *layout,
518 gboolean cursor_visible)
520 cursor_visible = (cursor_visible != FALSE);
522 if (layout->cursor_visible != cursor_visible)
527 layout->cursor_visible = cursor_visible;
529 /* Now queue a redraw on the paragraph containing the cursor
531 gtk_text_buffer_get_iter_at_mark (layout->buffer, &iter,
532 gtk_text_buffer_get_insert (layout->buffer));
534 gtk_text_layout_get_line_yrange (layout, &iter, &y, &height);
535 gtk_text_layout_emit_changed (layout, y, height, height);
537 gtk_text_layout_invalidate_cache (layout, _gtk_text_iter_get_text_line (&iter), TRUE);
542 * gtk_text_layout_get_cursor_visible:
543 * @layout: a #GtkTextLayout
545 * Returns whether the insertion cursor will be shown.
547 * Return value: if %FALSE, the insertion cursor will not be
548 * shown, even if the text is editable.
551 gtk_text_layout_get_cursor_visible (GtkTextLayout *layout)
553 return layout->cursor_visible;
557 * gtk_text_layout_set_preedit_string:
558 * @layout: a #PangoLayout
559 * @preedit_string: a string to display at the insertion point
560 * @preedit_attrs: a #PangoAttrList of attributes that apply to @preedit_string
561 * @cursor_pos: position of cursor within preedit string in chars
563 * Set the preedit string and attributes. The preedit string is a
564 * string showing text that is currently being edited and not
565 * yet committed into the buffer.
568 gtk_text_layout_set_preedit_string (GtkTextLayout *layout,
569 const gchar *preedit_string,
570 PangoAttrList *preedit_attrs,
573 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
574 g_return_if_fail (preedit_attrs != NULL || preedit_string == NULL);
576 g_free (layout->preedit_string);
578 if (layout->preedit_attrs)
579 pango_attr_list_unref (layout->preedit_attrs);
583 layout->preedit_string = g_strdup (preedit_string);
584 layout->preedit_len = strlen (layout->preedit_string);
585 pango_attr_list_ref (preedit_attrs);
586 layout->preedit_attrs = preedit_attrs;
588 cursor_pos = CLAMP (cursor_pos, 0, g_utf8_strlen (layout->preedit_string, -1));
589 layout->preedit_cursor = g_utf8_offset_to_pointer (layout->preedit_string, cursor_pos) - layout->preedit_string;
593 layout->preedit_string = NULL;
594 layout->preedit_len = 0;
595 layout->preedit_attrs = NULL;
596 layout->preedit_cursor = 0;
599 gtk_text_layout_invalidate_cursor_line (layout, FALSE);
603 gtk_text_layout_get_size (GtkTextLayout *layout,
607 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
610 *width = layout->width;
613 *height = layout->height;
617 gtk_text_layout_invalidated (GtkTextLayout *layout)
619 g_signal_emit (layout, signals[INVALIDATED], 0);
623 gtk_text_layout_emit_changed (GtkTextLayout *layout,
628 g_signal_emit (layout, signals[CHANGED], 0, y, old_height, new_height);
632 text_layout_changed (GtkTextLayout *layout,
636 gboolean cursors_only)
638 /* Check if the range intersects our cached line display,
639 * and invalidate the cached line if so.
641 if (layout->one_display_cache)
643 GtkTextLine *line = layout->one_display_cache->line;
644 gint cache_y = _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
646 gint cache_height = layout->one_display_cache->height;
648 if (cache_y + cache_height > y && cache_y < y + old_height)
649 gtk_text_layout_invalidate_cache (layout, line, cursors_only);
652 gtk_text_layout_emit_changed (layout, y, old_height, new_height);
656 gtk_text_layout_changed (GtkTextLayout *layout,
661 text_layout_changed (layout, y, old_height, new_height, FALSE);
665 gtk_text_layout_cursors_changed (GtkTextLayout *layout,
670 text_layout_changed (layout, y, old_height, new_height, TRUE);
674 gtk_text_layout_free_line_data (GtkTextLayout *layout,
676 GtkTextLineData *line_data)
678 GTK_TEXT_LAYOUT_GET_CLASS (layout)->free_line_data (layout, line, line_data);
682 gtk_text_layout_invalidate (GtkTextLayout *layout,
683 const GtkTextIter *start_index,
684 const GtkTextIter *end_index)
686 GTK_TEXT_LAYOUT_GET_CLASS (layout)->invalidate (layout, start_index, end_index);
690 gtk_text_layout_invalidate_cursors (GtkTextLayout *layout,
691 const GtkTextIter *start_index,
692 const GtkTextIter *end_index)
694 GTK_TEXT_LAYOUT_GET_CLASS (layout)->invalidate_cursors (layout, start_index, end_index);
698 gtk_text_layout_wrap (GtkTextLayout *layout,
701 GtkTextLineData *line_data)
703 return GTK_TEXT_LAYOUT_GET_CLASS (layout)->wrap (layout, line, line_data);
708 * gtk_text_layout_get_lines:
710 * Return value: (element-type GtkTextLine) (transfer container):
713 gtk_text_layout_get_lines (GtkTextLayout *layout,
714 /* [top_y, bottom_y) */
719 GtkTextLine *first_btree_line;
720 GtkTextLine *last_btree_line;
724 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), NULL);
725 g_return_val_if_fail (bottom_y > top_y, NULL);
730 _gtk_text_btree_find_line_by_y (_gtk_text_buffer_get_btree (layout->buffer),
731 layout, top_y, first_line_y);
732 if (first_btree_line == NULL)
738 /* -1 since bottom_y is one past */
740 _gtk_text_btree_find_line_by_y (_gtk_text_buffer_get_btree (layout->buffer),
741 layout, bottom_y - 1, NULL);
743 if (!last_btree_line)
745 _gtk_text_btree_get_end_iter_line (_gtk_text_buffer_get_btree (layout->buffer));
747 g_assert (last_btree_line != NULL);
749 line = first_btree_line;
752 retval = g_slist_prepend (retval, line);
754 if (line == last_btree_line)
757 line = _gtk_text_line_next_excluding_last (line);
760 retval = g_slist_reverse (retval);
766 invalidate_cached_style (GtkTextLayout *layout)
768 free_style_cache (layout);
771 /* These should be called around a loop which wraps a CONTIGUOUS bunch
772 * of display lines. If the lines aren't contiguous you can't call
776 gtk_text_layout_wrap_loop_start (GtkTextLayout *layout)
778 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
779 g_return_if_fail (layout->one_style_cache == NULL);
781 layout->wrap_loop_count += 1;
785 gtk_text_layout_wrap_loop_end (GtkTextLayout *layout)
787 g_return_if_fail (layout->wrap_loop_count > 0);
789 layout->wrap_loop_count -= 1;
791 if (layout->wrap_loop_count == 0)
793 /* We cache a some stuff if we're iterating over some lines wrapping
794 * them. This cleans it up.
796 /* Nuke our cached style */
797 invalidate_cached_style (layout);
798 g_assert (layout->one_style_cache == NULL);
803 gtk_text_layout_invalidate_all (GtkTextLayout *layout)
808 if (layout->buffer == NULL)
811 gtk_text_buffer_get_bounds (layout->buffer, &start, &end);
813 gtk_text_layout_invalidate (layout, &start, &end);
817 gtk_text_layout_invalidate_cache (GtkTextLayout *layout,
819 gboolean cursors_only)
821 if (layout->one_display_cache && line == layout->one_display_cache->line)
823 GtkTextLineDisplay *display = layout->one_display_cache;
827 if (display->cursors)
828 g_array_free (display->cursors, TRUE);
829 display->cursors = NULL;
830 display->cursors_invalid = TRUE;
831 display->has_block_cursor = FALSE;
835 layout->one_display_cache = NULL;
836 gtk_text_layout_free_line_display (layout, display);
841 /* Now invalidate the paragraph containing the cursor
844 gtk_text_layout_invalidate_cursor_line (GtkTextLayout *layout,
845 gboolean cursors_only)
847 GtkTextLayoutPrivate *priv = GTK_TEXT_LAYOUT_GET_PRIVATE (layout);
848 GtkTextLineData *line_data;
850 if (priv->cursor_line == NULL)
853 line_data = _gtk_text_line_get_data (priv->cursor_line, layout);
857 gtk_text_layout_invalidate_cache (layout, priv->cursor_line, TRUE);
860 gtk_text_layout_invalidate_cache (layout, priv->cursor_line, FALSE);
861 _gtk_text_line_invalidate_wrap (priv->cursor_line, line_data);
864 gtk_text_layout_invalidated (layout);
869 gtk_text_layout_update_cursor_line(GtkTextLayout *layout)
871 GtkTextLayoutPrivate *priv = GTK_TEXT_LAYOUT_GET_PRIVATE (layout);
874 gtk_text_buffer_get_iter_at_mark (layout->buffer, &iter,
875 gtk_text_buffer_get_insert (layout->buffer));
877 priv->cursor_line = _gtk_text_iter_get_text_line (&iter);
881 gtk_text_layout_real_invalidate (GtkTextLayout *layout,
882 const GtkTextIter *start,
883 const GtkTextIter *end)
886 GtkTextLine *last_line;
888 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
889 g_return_if_fail (layout->wrap_loop_count == 0);
891 /* Because we may be invalidating a mark, it's entirely possible
892 * that gtk_text_iter_equal (start, end) in which case we
893 * should still invalidate the line they are both on. i.e.
894 * we always invalidate the line with "start" even
895 * if there's an empty range.
899 gtk_text_view_index_spew (start_index, "invalidate start");
900 gtk_text_view_index_spew (end_index, "invalidate end");
903 last_line = _gtk_text_iter_get_text_line (end);
904 line = _gtk_text_iter_get_text_line (start);
908 GtkTextLineData *line_data = _gtk_text_line_get_data (line, layout);
910 gtk_text_layout_invalidate_cache (layout, line, FALSE);
913 _gtk_text_line_invalidate_wrap (line, line_data);
915 if (line == last_line)
918 line = _gtk_text_line_next_excluding_last (line);
921 gtk_text_layout_invalidated (layout);
925 gtk_text_layout_real_invalidate_cursors (GtkTextLayout *layout,
926 const GtkTextIter *start,
927 const GtkTextIter *end)
929 /* Check if the range intersects our cached line display,
930 * and invalidate the cached line if so.
932 if (layout->one_display_cache)
934 GtkTextIter line_start, line_end;
935 GtkTextLine *line = layout->one_display_cache->line;
937 gtk_text_layout_get_iter_at_line (layout, &line_start, line, 0);
939 line_end = line_start;
940 if (!gtk_text_iter_ends_line (&line_end))
941 gtk_text_iter_forward_to_line_end (&line_end);
943 if (gtk_text_iter_compare (start, end) > 0)
945 const GtkTextIter *tmp = start;
950 if (gtk_text_iter_compare (&line_start, end) <= 0 &&
951 gtk_text_iter_compare (start, &line_end) <= 0)
953 gtk_text_layout_invalidate_cache (layout, line, TRUE);
957 gtk_text_layout_invalidated (layout);
961 gtk_text_layout_real_free_line_data (GtkTextLayout *layout,
963 GtkTextLineData *line_data)
965 gtk_text_layout_invalidate_cache (layout, line, FALSE);
971 * gtk_text_layout_is_valid:
972 * @layout: a #GtkTextLayout
974 * Check if there are any invalid regions in a #GtkTextLayout's buffer
976 * Return value: %TRUE if any invalid regions were found
979 gtk_text_layout_is_valid (GtkTextLayout *layout)
981 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), FALSE);
983 return _gtk_text_btree_is_valid (_gtk_text_buffer_get_btree (layout->buffer),
988 update_layout_size (GtkTextLayout *layout)
990 _gtk_text_btree_get_view_size (_gtk_text_buffer_get_btree (layout->buffer),
992 &layout->width, &layout->height);
996 * gtk_text_layout_validate_yrange:
997 * @layout: a #GtkTextLayout
998 * @anchor: iter pointing into a line that will be used as the
1000 * @y0_: offset from the top of the line pointed to by @anchor at
1001 * which to begin validation. (The offset here is in pixels
1002 * after validation.)
1003 * @y1_: offset from the top of the line pointed to by @anchor at
1004 * which to end validation. (The offset here is in pixels
1005 * after validation.)
1007 * Ensure that a region of a #GtkTextLayout is valid. The ::changed
1008 * signal will be emitted if any lines are validated.
1011 gtk_text_layout_validate_yrange (GtkTextLayout *layout,
1012 GtkTextIter *anchor,
1017 GtkTextLine *first_line = NULL;
1018 GtkTextLine *last_line = NULL;
1020 gint delta_height = 0;
1021 gint first_line_y = 0; /* Quiet GCC */
1022 gint last_line_y = 0; /* Quiet GCC */
1024 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
1031 /* Validate backwards from the anchor line to y0
1033 line = _gtk_text_iter_get_text_line (anchor);
1034 line = _gtk_text_line_previous (line);
1036 while (line && seen < -y0)
1038 GtkTextLineData *line_data = _gtk_text_line_get_data (line, layout);
1039 if (!line_data || !line_data->valid)
1041 gint old_height, new_height;
1043 old_height = line_data ? line_data->height : 0;
1045 _gtk_text_btree_validate_line (_gtk_text_buffer_get_btree (layout->buffer),
1047 line_data = _gtk_text_line_get_data (line, layout);
1049 new_height = line_data ? line_data->height : 0;
1051 delta_height += new_height - old_height;
1054 first_line_y = -seen - new_height;
1058 last_line_y = -seen;
1062 seen += line_data ? line_data->height : 0;
1063 line = _gtk_text_line_previous (line);
1066 /* Validate forwards to y1 */
1067 line = _gtk_text_iter_get_text_line (anchor);
1069 while (line && seen < y1)
1071 GtkTextLineData *line_data = _gtk_text_line_get_data (line, layout);
1072 if (!line_data || !line_data->valid)
1074 gint old_height, new_height;
1076 old_height = line_data ? line_data->height : 0;
1078 _gtk_text_btree_validate_line (_gtk_text_buffer_get_btree (layout->buffer),
1080 line_data = _gtk_text_line_get_data (line, layout);
1081 new_height = line_data ? line_data->height : 0;
1083 delta_height += new_height - old_height;
1088 first_line_y = seen;
1091 last_line_y = seen + new_height;
1094 seen += line_data ? line_data->height : 0;
1095 line = _gtk_text_line_next_excluding_last (line);
1098 /* If we found and validated any invalid lines, update size and
1099 * emit the changed signal
1105 update_layout_size (layout);
1107 line_top = _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
1108 first_line, layout);
1110 gtk_text_layout_emit_changed (layout,
1112 last_line_y - first_line_y - delta_height,
1113 last_line_y - first_line_y);
1118 * gtk_text_layout_validate:
1119 * @tree: a #GtkTextLayout
1120 * @max_pixels: the maximum number of pixels to validate. (No more
1121 * than one paragraph beyond this limit will be validated)
1123 * Validate regions of a #GtkTextLayout. The ::changed signal will
1124 * be emitted for each region validated.
1127 gtk_text_layout_validate (GtkTextLayout *layout,
1130 gint y, old_height, new_height;
1132 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
1134 while (max_pixels > 0 &&
1135 _gtk_text_btree_validate (_gtk_text_buffer_get_btree (layout->buffer),
1137 &y, &old_height, &new_height))
1139 max_pixels -= new_height;
1141 update_layout_size (layout);
1142 gtk_text_layout_emit_changed (layout, y, old_height, new_height);
1146 static GtkTextLineData*
1147 gtk_text_layout_real_wrap (GtkTextLayout *layout,
1150 GtkTextLineData *line_data)
1152 GtkTextLineDisplay *display;
1154 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), NULL);
1155 g_return_val_if_fail (line != NULL, NULL);
1157 if (line_data == NULL)
1159 line_data = _gtk_text_line_data_new (layout, line);
1160 _gtk_text_line_add_data (line, line_data);
1163 display = gtk_text_layout_get_line_display (layout, line, TRUE);
1164 line_data->width = display->width;
1165 line_data->height = display->height;
1166 line_data->valid = TRUE;
1167 gtk_text_layout_free_line_display (layout, display);
1173 * Layout utility functions
1176 /* If you get the style with get_style () you need to call
1177 release_style () to free it. */
1178 static GtkTextAttributes*
1179 get_style (GtkTextLayout *layout,
1182 GtkTextAttributes *style;
1184 /* If we have the one-style cache, then it means
1185 that we haven't seen a toggle since we filled in the
1188 if (layout->one_style_cache != NULL)
1190 gtk_text_attributes_ref (layout->one_style_cache);
1191 return layout->one_style_cache;
1194 g_assert (layout->one_style_cache == NULL);
1196 /* No tags, use default style */
1197 if (tags == NULL || tags->len == 0)
1199 /* One ref for the return value, one ref for the
1200 layout->one_style_cache reference */
1201 gtk_text_attributes_ref (layout->default_style);
1202 gtk_text_attributes_ref (layout->default_style);
1203 layout->one_style_cache = layout->default_style;
1205 return layout->default_style;
1208 style = gtk_text_attributes_new ();
1210 gtk_text_attributes_copy_values (layout->default_style,
1213 _gtk_text_attributes_fill_from_tags (style,
1214 (GtkTextTag**) tags->pdata,
1217 g_assert (style->refcount == 1);
1219 /* Leave this style as the last one seen */
1220 g_assert (layout->one_style_cache == NULL);
1221 gtk_text_attributes_ref (style); /* ref held by layout->one_style_cache */
1222 layout->one_style_cache = style;
1224 /* Returning yet another refcount */
1229 release_style (GtkTextLayout *layout,
1230 GtkTextAttributes *style)
1232 g_return_if_fail (style != NULL);
1233 g_return_if_fail (style->refcount > 0);
1235 gtk_text_attributes_unref (style);
1242 /* This function tries to optimize the case where a line
1243 is completely invisible */
1245 totally_invisible_line (GtkTextLayout *layout,
1249 GtkTextLineSegment *seg;
1252 /* Check if the first char is visible, if so we are partially visible.
1253 * Note that we have to check this since we don't know the current
1254 * invisible/noninvisible toggle state; this function can use the whole btree
1257 gtk_text_layout_get_iter_at_line (layout, iter, line, 0);
1258 if (!_gtk_text_btree_char_is_invisible (iter))
1262 seg = line->segments;
1266 if (seg->byte_count > 0)
1267 bytes += seg->byte_count;
1269 /* Note that these two tests can cause us to bail out
1270 * when we shouldn't, because a higher-priority tag
1271 * may override these settings. However the important
1272 * thing is to only invisible really-invisible lines, rather
1273 * than to invisible all really-invisible lines.
1276 else if (seg->type == >k_text_toggle_on_type)
1278 invalidate_cached_style (layout);
1280 /* Bail out if an elision-unsetting tag begins */
1281 if (seg->body.toggle.info->tag->priv->invisible_set &&
1282 !seg->body.toggle.info->tag->priv->values->invisible)
1285 else if (seg->type == >k_text_toggle_off_type)
1287 invalidate_cached_style (layout);
1289 /* Bail out if an elision-setting tag ends */
1290 if (seg->body.toggle.info->tag->priv->invisible_set &&
1291 seg->body.toggle.info->tag->priv->values->invisible)
1298 if (seg != NULL) /* didn't reach line end */
1305 set_para_values (GtkTextLayout *layout,
1306 PangoDirection base_dir,
1307 GtkTextAttributes *style,
1308 GtkTextLineDisplay *display)
1310 PangoAlignment pango_align = PANGO_ALIGN_LEFT;
1311 PangoWrapMode pango_wrap = PANGO_WRAP_WORD;
1315 /* If no base direction was found, then use the style direction */
1316 case PANGO_DIRECTION_NEUTRAL :
1317 display->direction = style->direction;
1319 /* Override the base direction */
1320 if (display->direction == GTK_TEXT_DIR_RTL)
1321 base_dir = PANGO_DIRECTION_RTL;
1323 base_dir = PANGO_DIRECTION_LTR;
1326 case PANGO_DIRECTION_RTL :
1327 display->direction = GTK_TEXT_DIR_RTL;
1330 display->direction = GTK_TEXT_DIR_LTR;
1334 if (display->direction == GTK_TEXT_DIR_RTL)
1335 display->layout = pango_layout_new (layout->rtl_context);
1337 display->layout = pango_layout_new (layout->ltr_context);
1339 switch (style->justification)
1341 case GTK_JUSTIFY_LEFT:
1342 pango_align = (base_dir == PANGO_DIRECTION_LTR) ? PANGO_ALIGN_LEFT : PANGO_ALIGN_RIGHT;
1344 case GTK_JUSTIFY_RIGHT:
1345 pango_align = (base_dir == PANGO_DIRECTION_LTR) ? PANGO_ALIGN_RIGHT : PANGO_ALIGN_LEFT;
1347 case GTK_JUSTIFY_CENTER:
1348 pango_align = PANGO_ALIGN_CENTER;
1350 case GTK_JUSTIFY_FILL:
1351 pango_align = (base_dir == PANGO_DIRECTION_LTR) ? PANGO_ALIGN_LEFT : PANGO_ALIGN_RIGHT;
1352 pango_layout_set_justify (display->layout, TRUE);
1355 g_assert_not_reached ();
1359 pango_layout_set_alignment (display->layout, pango_align);
1360 pango_layout_set_spacing (display->layout,
1361 style->pixels_inside_wrap * PANGO_SCALE);
1364 pango_layout_set_tabs (display->layout, style->tabs);
1366 display->top_margin = style->pixels_above_lines;
1367 display->height = style->pixels_above_lines + style->pixels_below_lines;
1368 display->bottom_margin = style->pixels_below_lines;
1369 display->left_margin = style->left_margin;
1370 display->right_margin = style->right_margin;
1372 display->x_offset = display->left_margin;
1374 pango_layout_set_indent (display->layout,
1375 style->indent * PANGO_SCALE);
1377 switch (style->wrap_mode)
1380 pango_wrap = PANGO_WRAP_CHAR;
1383 pango_wrap = PANGO_WRAP_WORD;
1386 case GTK_WRAP_WORD_CHAR:
1387 pango_wrap = PANGO_WRAP_WORD_CHAR;
1394 if (style->wrap_mode != GTK_WRAP_NONE)
1396 int layout_width = (layout->screen_width - display->left_margin - display->right_margin);
1397 pango_layout_set_width (display->layout, layout_width * PANGO_SCALE);
1398 pango_layout_set_wrap (display->layout, pango_wrap);
1401 display->total_width = MAX (layout->screen_width, layout->width) - display->left_margin - display->right_margin;
1403 if (style->pg_bg_color)
1404 display->pg_bg_color = gdk_color_copy (style->pg_bg_color);
1406 display->pg_bg_color = NULL;
1408 if (style->pg_bg_rgba)
1409 display->pg_bg_rgba = gdk_rgba_copy (style->pg_bg_rgba);
1411 display->pg_bg_rgba = NULL;
1414 static PangoAttribute *
1415 gtk_text_attr_appearance_copy (const PangoAttribute *attr)
1417 const GtkTextAttrAppearance *appearance_attr = (const GtkTextAttrAppearance *)attr;
1419 return gtk_text_attr_appearance_new (&appearance_attr->appearance);
1423 gtk_text_attr_appearance_destroy (PangoAttribute *attr)
1425 GtkTextAttrAppearance *appearance_attr = (GtkTextAttrAppearance *)attr;
1427 if (appearance_attr->appearance.rgba[0])
1428 gdk_rgba_free (appearance_attr->appearance.rgba[0]);
1430 if (appearance_attr->appearance.rgba[1])
1431 gdk_rgba_free (appearance_attr->appearance.rgba[1]);
1433 g_slice_free (GtkTextAttrAppearance, appearance_attr);
1437 rgba_equal (const GdkRGBA *rgba1, const GdkRGBA *rgba2)
1440 return gdk_rgba_equal (rgba1, rgba2);
1449 gtk_text_attr_appearance_compare (const PangoAttribute *attr1,
1450 const PangoAttribute *attr2)
1452 const GtkTextAppearance *appearance1 = &((const GtkTextAttrAppearance *)attr1)->appearance;
1453 const GtkTextAppearance *appearance2 = &((const GtkTextAttrAppearance *)attr2)->appearance;
1455 return (rgba_equal (appearance1->rgba[0], appearance2->rgba[0]) &&
1456 rgba_equal (appearance1->rgba[1], appearance2->rgba[1]) &&
1457 appearance1->underline == appearance2->underline &&
1458 appearance1->strikethrough == appearance2->strikethrough &&
1459 appearance1->draw_bg == appearance2->draw_bg);
1463 * gtk_text_attr_appearance_new:
1466 * Create a new font description attribute. (This attribute
1467 * allows setting family, style, weight, variant, stretch,
1468 * and size simultaneously.)
1472 static PangoAttribute *
1473 gtk_text_attr_appearance_new (const GtkTextAppearance *appearance)
1475 static PangoAttrClass klass = {
1477 gtk_text_attr_appearance_copy,
1478 gtk_text_attr_appearance_destroy,
1479 gtk_text_attr_appearance_compare
1482 GtkTextAttrAppearance *result;
1485 klass.type = gtk_text_attr_appearance_type =
1486 pango_attr_type_register ("GtkTextAttrAppearance");
1488 result = g_slice_new (GtkTextAttrAppearance);
1489 result->attr.klass = &klass;
1491 result->appearance = *appearance;
1493 if (appearance->rgba[0])
1494 result->appearance.rgba[0] = gdk_rgba_copy (appearance->rgba[0]);
1496 if (appearance->rgba[1])
1497 result->appearance.rgba[1] = gdk_rgba_copy (appearance->rgba[1]);
1499 return (PangoAttribute *)result;
1503 add_generic_attrs (GtkTextLayout *layout,
1504 GtkTextAppearance *appearance,
1506 PangoAttrList *attrs,
1511 PangoAttribute *attr;
1513 if (appearance->underline != PANGO_UNDERLINE_NONE)
1515 attr = pango_attr_underline_new (appearance->underline);
1517 attr->start_index = start;
1518 attr->end_index = start + byte_count;
1520 pango_attr_list_insert (attrs, attr);
1523 if (appearance->strikethrough)
1525 attr = pango_attr_strikethrough_new (appearance->strikethrough);
1527 attr->start_index = start;
1528 attr->end_index = start + byte_count;
1530 pango_attr_list_insert (attrs, attr);
1533 if (appearance->rise != 0)
1535 attr = pango_attr_rise_new (appearance->rise);
1537 attr->start_index = start;
1538 attr->end_index = start + byte_count;
1540 pango_attr_list_insert (attrs, attr);
1545 attr = gtk_text_attr_appearance_new (appearance);
1547 attr->start_index = start;
1548 attr->end_index = start + byte_count;
1550 ((GtkTextAttrAppearance *)attr)->appearance.is_text = is_text;
1552 pango_attr_list_insert (attrs, attr);
1557 add_text_attrs (GtkTextLayout *layout,
1558 GtkTextAttributes *style,
1560 PangoAttrList *attrs,
1564 PangoAttribute *attr;
1566 attr = pango_attr_font_desc_new (style->font);
1567 attr->start_index = start;
1568 attr->end_index = start + byte_count;
1570 pango_attr_list_insert (attrs, attr);
1572 if (style->font_scale != 1.0)
1574 attr = pango_attr_scale_new (style->font_scale);
1576 attr->start_index = start;
1577 attr->end_index = start + byte_count;
1579 pango_attr_list_insert (attrs, attr);
1584 add_pixbuf_attrs (GtkTextLayout *layout,
1585 GtkTextLineDisplay *display,
1586 GtkTextAttributes *style,
1587 GtkTextLineSegment *seg,
1588 PangoAttrList *attrs,
1591 PangoAttribute *attr;
1592 PangoRectangle logical_rect;
1593 GtkTextPixbuf *pixbuf = &seg->body.pixbuf;
1596 width = gdk_pixbuf_get_width (pixbuf->pixbuf);
1597 height = gdk_pixbuf_get_height (pixbuf->pixbuf);
1600 logical_rect.y = -height * PANGO_SCALE;
1601 logical_rect.width = width * PANGO_SCALE;
1602 logical_rect.height = height * PANGO_SCALE;
1604 attr = pango_attr_shape_new_with_data (&logical_rect, &logical_rect,
1605 pixbuf->pixbuf, NULL, NULL);
1606 attr->start_index = start;
1607 attr->end_index = start + seg->byte_count;
1608 pango_attr_list_insert (attrs, attr);
1612 add_child_attrs (GtkTextLayout *layout,
1613 GtkTextLineDisplay *display,
1614 GtkTextAttributes *style,
1615 GtkTextLineSegment *seg,
1616 PangoAttrList *attrs,
1619 PangoAttribute *attr;
1620 PangoRectangle logical_rect;
1628 tmp_list = seg->body.child.widgets;
1629 while (tmp_list != NULL)
1631 GtkWidget *child = tmp_list->data;
1633 if (_gtk_anchored_child_get_layout (child) == layout)
1638 gtk_widget_get_preferred_size (child, &req, NULL);
1641 height = req.height;
1648 tmp_list = g_slist_next (tmp_list);
1651 if (tmp_list == NULL)
1653 /* If tmp_list == NULL then there is no widget at this anchor in
1654 * this display; not an error. We make up an arbitrary size
1655 * to use, just so the programmer can see the blank spot.
1656 * We also put a NULL in the shaped objects list, to keep
1657 * the correspondence between the list and the shaped chars in
1658 * the layout. A bad hack, yes.
1668 logical_rect.y = -height * PANGO_SCALE;
1669 logical_rect.width = width * PANGO_SCALE;
1670 logical_rect.height = height * PANGO_SCALE;
1672 attr = pango_attr_shape_new_with_data (&logical_rect, &logical_rect,
1673 widget, NULL, NULL);
1674 attr->start_index = start;
1675 attr->end_index = start + seg->byte_count;
1676 pango_attr_list_insert (attrs, attr);
1681 * @layout: a #GtkTextLayout
1682 * @display: a #GtkTextLineDisplay
1683 * @insert_iter: iter pointing to the cursor location
1684 * @insert_index: cursor offset in the @display's layout, it may
1685 * be different from @insert_iter's offset in case when preedit
1686 * string is present.
1687 * @pos: location to store cursor position
1688 * @cursor_at_line_end: whether cursor is at the end of line
1690 * Checks whether layout should display block cursor at given position.
1691 * For this layout must be in overwrite mode and text at @insert_iter
1695 get_block_cursor (GtkTextLayout *layout,
1696 GtkTextLineDisplay *display,
1697 const GtkTextIter *insert_iter,
1700 gboolean *cursor_at_line_end)
1702 PangoRectangle pango_pos;
1704 if (layout->overwrite_mode &&
1705 gtk_text_iter_editable (insert_iter, TRUE) &&
1706 _gtk_text_util_get_block_cursor_location (display->layout,
1709 cursor_at_line_end))
1713 pos->x = PANGO_PIXELS (pango_pos.x);
1714 pos->y = PANGO_PIXELS (pango_pos.y);
1715 pos->width = PANGO_PIXELS (pango_pos.width);
1716 pos->height = PANGO_PIXELS (pango_pos.height);
1726 add_cursor (GtkTextLayout *layout,
1727 GtkTextLineDisplay *display,
1728 GtkTextLineSegment *seg,
1731 /* Hide insertion cursor when we have a selection or the layout
1732 * user has hidden the cursor.
1734 if (_gtk_text_btree_mark_is_insert (_gtk_text_buffer_get_btree (layout->buffer),
1735 seg->body.mark.obj) &&
1736 (!layout->cursor_visible ||
1737 gtk_text_buffer_get_selection_bounds (layout->buffer, NULL, NULL)))
1740 if (layout->overwrite_mode &&
1741 _gtk_text_btree_mark_is_insert (_gtk_text_buffer_get_btree (layout->buffer),
1742 seg->body.mark.obj))
1745 gboolean cursor_at_line_end;
1747 _gtk_text_btree_get_iter_at_mark (_gtk_text_buffer_get_btree (layout->buffer),
1748 &iter, seg->body.mark.obj);
1750 if (get_block_cursor (layout, display, &iter, start,
1751 &display->block_cursor,
1752 &cursor_at_line_end))
1754 display->has_block_cursor = TRUE;
1755 display->cursor_at_line_end = cursor_at_line_end;
1760 if (!display->cursors)
1761 display->cursors = g_array_new (FALSE, FALSE, sizeof(int));
1763 display->cursors = g_array_append_val (display->cursors, start);
1767 is_shape (PangoLayoutRun *run)
1769 GSList *tmp_list = run->item->analysis.extra_attrs;
1773 PangoAttribute *attr = tmp_list->data;
1775 if (attr->klass->type == PANGO_ATTR_SHAPE)
1778 tmp_list = tmp_list->next;
1785 allocate_child_widgets (GtkTextLayout *text_layout,
1786 GtkTextLineDisplay *display)
1788 PangoLayout *layout = display->layout;
1789 PangoLayoutIter *run_iter;
1791 run_iter = pango_layout_get_iter (layout);
1794 PangoLayoutRun *run = pango_layout_iter_get_run_readonly (run_iter);
1796 if (run && is_shape (run))
1799 GtkTextIter text_iter;
1800 GtkTextChildAnchor *anchor = NULL;
1801 GList *widgets = NULL;
1804 /* The pango iterator iterates in visual order.
1805 * We use the byte index to find the child widget.
1807 byte_index = pango_layout_iter_get_index (run_iter);
1808 line_display_index_to_iter (text_layout, display, &text_iter, byte_index, 0);
1809 anchor = gtk_text_iter_get_child_anchor (&text_iter);
1811 widgets = gtk_text_child_anchor_get_widgets (anchor);
1813 for (l = widgets; l; l = l->next)
1815 PangoRectangle extents;
1816 GtkWidget *child = l->data;
1818 if (_gtk_anchored_child_get_layout (child) == text_layout)
1821 /* We emit "allocate_child" with the x,y of
1822 * the widget with respect to the top of the line
1823 * and the left side of the buffer
1825 pango_layout_iter_get_run_extents (run_iter,
1829 g_signal_emit (text_layout,
1830 signals[ALLOCATE_CHILD],
1833 PANGO_PIXELS (extents.x) + display->x_offset,
1834 PANGO_PIXELS (extents.y) + display->top_margin);
1838 g_list_free (widgets);
1841 while (pango_layout_iter_next_run (run_iter));
1843 pango_layout_iter_free (run_iter);
1847 convert_color (GdkColor *result,
1848 PangoAttrColor *attr)
1850 result->red = attr->color.red;
1851 result->blue = attr->color.blue;
1852 result->green = attr->color.green;
1855 /* This function is used to convert the preedit string attributes, which are
1856 * standard PangoAttributes, into the custom attributes used by the text
1857 * widget and insert them into a attr list with a given offset.
1860 add_preedit_attrs (GtkTextLayout *layout,
1861 GtkTextAttributes *style,
1862 PangoAttrList *attrs,
1866 PangoAttrIterator *iter = pango_attr_list_get_iterator (layout->preedit_attrs);
1870 GtkTextAppearance appearance = style->appearance;
1871 PangoFontDescription *font_desc = pango_font_description_copy_static (style->font);
1872 PangoAttribute *insert_attr;
1873 GSList *extra_attrs = NULL;
1875 PangoLanguage *language;
1878 pango_attr_iterator_range (iter, &start, &end);
1880 if (end == G_MAXINT)
1881 end = layout->preedit_len;
1886 pango_attr_iterator_get_font (iter, font_desc, &language, &extra_attrs);
1888 tmp_list = extra_attrs;
1891 PangoAttribute *attr = tmp_list->data;
1893 switch (attr->klass->type)
1895 case PANGO_ATTR_FOREGROUND:
1896 convert_color (&appearance.fg_color, (PangoAttrColor *)attr);
1898 case PANGO_ATTR_BACKGROUND:
1899 convert_color (&appearance.bg_color, (PangoAttrColor *)attr);
1900 appearance.draw_bg = TRUE;
1902 case PANGO_ATTR_UNDERLINE:
1903 appearance.underline = ((PangoAttrInt *)attr)->value;
1905 case PANGO_ATTR_STRIKETHROUGH:
1906 appearance.strikethrough = ((PangoAttrInt *)attr)->value;
1908 case PANGO_ATTR_RISE:
1909 appearance.rise = ((PangoAttrInt *)attr)->value;
1915 pango_attribute_destroy (attr);
1916 tmp_list = tmp_list->next;
1919 g_slist_free (extra_attrs);
1921 insert_attr = pango_attr_font_desc_new (font_desc);
1922 insert_attr->start_index = start + offset;
1923 insert_attr->end_index = end + offset;
1925 pango_attr_list_insert (attrs, insert_attr);
1929 insert_attr = pango_attr_language_new (language);
1930 insert_attr->start_index = start + offset;
1931 insert_attr->end_index = end + offset;
1933 pango_attr_list_insert (attrs, insert_attr);
1936 add_generic_attrs (layout, &appearance, end - start,
1937 attrs, start + offset,
1940 pango_font_description_free (font_desc);
1942 while (pango_attr_iterator_next (iter));
1944 pango_attr_iterator_destroy (iter);
1947 /* Iterate over the line and fill in display->cursors.
1948 * It's a stripped copy of gtk_text_layout_get_line_display() */
1950 update_text_display_cursors (GtkTextLayout *layout,
1952 GtkTextLineDisplay *display)
1954 GtkTextLineSegment *seg;
1956 gint layout_byte_offset, buffer_byte_offset;
1957 GSList *cursor_byte_offsets = NULL;
1958 GSList *cursor_segs = NULL;
1959 GSList *tmp_list1, *tmp_list2;
1961 if (!display->cursors_invalid)
1964 display->cursors_invalid = FALSE;
1966 /* Special-case optimization for completely
1967 * invisible lines; makes it faster to deal
1968 * with sequences of invisible lines.
1970 if (totally_invisible_line (layout, line, &iter))
1973 /* Iterate over segments */
1974 layout_byte_offset = 0; /* position in the layout text (includes preedit, does not include invisible text) */
1975 buffer_byte_offset = 0; /* position in the buffer line */
1976 seg = _gtk_text_iter_get_any_segment (&iter);
1979 /* Displayable segments */
1980 if (seg->type == >k_text_char_type ||
1981 seg->type == >k_text_pixbuf_type ||
1982 seg->type == >k_text_child_type)
1984 gtk_text_layout_get_iter_at_line (layout, &iter, line,
1985 buffer_byte_offset);
1987 if (!_gtk_text_btree_char_is_invisible (&iter))
1988 layout_byte_offset += seg->byte_count;
1990 buffer_byte_offset += seg->byte_count;
1994 else if (seg->type == >k_text_right_mark_type ||
1995 seg->type == >k_text_left_mark_type)
1997 gint cursor_offset = 0;
1999 /* At the insertion point, add the preedit string, if any */
2001 if (_gtk_text_btree_mark_is_insert (_gtk_text_buffer_get_btree (layout->buffer),
2002 seg->body.mark.obj))
2004 display->insert_index = layout_byte_offset;
2006 if (layout->preedit_len > 0)
2008 layout_byte_offset += layout->preedit_len;
2009 /* DO NOT increment the buffer byte offset for preedit */
2010 cursor_offset = layout->preedit_cursor - layout->preedit_len;
2014 /* Display visible marks */
2016 if (seg->body.mark.visible)
2018 cursor_byte_offsets = g_slist_prepend (cursor_byte_offsets,
2019 GINT_TO_POINTER (layout_byte_offset + cursor_offset));
2020 cursor_segs = g_slist_prepend (cursor_segs, seg);
2025 else if (seg->type == >k_text_toggle_on_type ||
2026 seg->type == >k_text_toggle_off_type)
2031 g_error ("Unknown segment type: %s", seg->type->name);
2036 tmp_list1 = cursor_byte_offsets;
2037 tmp_list2 = cursor_segs;
2040 add_cursor (layout, display, tmp_list2->data,
2041 GPOINTER_TO_INT (tmp_list1->data));
2042 tmp_list1 = tmp_list1->next;
2043 tmp_list2 = tmp_list2->next;
2045 g_slist_free (cursor_byte_offsets);
2046 g_slist_free (cursor_segs);
2049 /* Same as _gtk_text_btree_get_tags(), except it returns GPtrArray,
2050 * to be used in gtk_text_layout_get_line_display(). */
2052 get_tags_array_at_iter (GtkTextIter *iter)
2055 GPtrArray *array = NULL;
2058 tags = _gtk_text_btree_get_tags (iter, &n_tags);
2062 array = g_ptr_array_sized_new (n_tags);
2063 g_ptr_array_set_size (array, n_tags);
2064 memcpy (array->pdata, tags, n_tags * sizeof (GtkTextTag*));
2071 /* Add the tag to the array if it's not there already, and remove
2072 * it otherwise. It keeps the array sorted by tags priority. */
2074 tags_array_toggle_tag (GPtrArray *array,
2081 array = g_ptr_array_new ();
2083 tags = (GtkTextTag**) array->pdata;
2085 for (pos = 0; pos < array->len && tags[pos]->priv->priority < tag->priv->priority; pos++) ;
2087 if (pos < array->len && tags[pos] == tag)
2088 g_ptr_array_remove_index (array, pos);
2091 g_ptr_array_set_size (array, array->len + 1);
2092 if (pos < array->len - 1)
2093 memmove (array->pdata + pos + 1, array->pdata + pos,
2094 (array->len - pos - 1) * sizeof (GtkTextTag*));
2095 array->pdata[pos] = tag;
2101 GtkTextLineDisplay *
2102 gtk_text_layout_get_line_display (GtkTextLayout *layout,
2106 GtkTextLayoutPrivate *priv = GTK_TEXT_LAYOUT_GET_PRIVATE (layout);
2107 GtkTextLineDisplay *display;
2108 GtkTextLineSegment *seg;
2110 GtkTextAttributes *style;
2112 PangoAttrList *attrs;
2113 gint text_allocated, layout_byte_offset, buffer_byte_offset;
2114 PangoRectangle extents;
2115 gboolean para_values_set = FALSE;
2116 GSList *cursor_byte_offsets = NULL;
2117 GSList *cursor_segs = NULL;
2118 GSList *tmp_list1, *tmp_list2;
2119 gboolean saw_widget = FALSE;
2120 PangoDirection base_dir;
2122 gboolean initial_toggle_segments;
2124 g_return_val_if_fail (line != NULL, NULL);
2126 if (layout->one_display_cache)
2128 if (line == layout->one_display_cache->line &&
2129 (size_only || !layout->one_display_cache->size_only))
2132 update_text_display_cursors (layout, line, layout->one_display_cache);
2133 return layout->one_display_cache;
2137 GtkTextLineDisplay *tmp_display = layout->one_display_cache;
2138 layout->one_display_cache = NULL;
2139 gtk_text_layout_free_line_display (layout, tmp_display);
2143 DV (g_print ("creating one line display cache (%s)\n", G_STRLOC));
2145 display = g_slice_new0 (GtkTextLineDisplay);
2147 display->size_only = size_only;
2148 display->line = line;
2149 display->insert_index = -1;
2151 /* Special-case optimization for completely
2152 * invisible lines; makes it faster to deal
2153 * with sequences of invisible lines.
2155 if (totally_invisible_line (layout, line, &iter))
2157 if (display->direction == GTK_TEXT_DIR_RTL)
2158 display->layout = pango_layout_new (layout->rtl_context);
2160 display->layout = pango_layout_new (layout->ltr_context);
2165 /* Find the bidi base direction */
2166 base_dir = line->dir_propagated_forward;
2167 if (base_dir == PANGO_DIRECTION_NEUTRAL)
2168 base_dir = line->dir_propagated_back;
2170 if (line == priv->cursor_line &&
2171 line->dir_strong == PANGO_DIRECTION_NEUTRAL)
2173 base_dir = (layout->keyboard_direction == GTK_TEXT_DIR_LTR) ?
2174 PANGO_DIRECTION_LTR : PANGO_DIRECTION_RTL;
2177 /* Allocate space for flat text for buffer
2179 text_allocated = _gtk_text_line_byte_count (line);
2180 text = g_malloc (text_allocated);
2182 attrs = pango_attr_list_new ();
2184 /* Iterate over segments, creating display chunks for them, and updating the tags array. */
2185 layout_byte_offset = 0; /* current length of layout text (includes preedit, does not include invisible text) */
2186 buffer_byte_offset = 0; /* position in the buffer line */
2187 seg = _gtk_text_iter_get_any_segment (&iter);
2188 tags = get_tags_array_at_iter (&iter);
2189 initial_toggle_segments = TRUE;
2192 /* Displayable segments */
2193 if (seg->type == >k_text_char_type ||
2194 seg->type == >k_text_pixbuf_type ||
2195 seg->type == >k_text_child_type)
2197 style = get_style (layout, tags);
2198 initial_toggle_segments = FALSE;
2200 /* We have to delay setting the paragraph values until we
2201 * hit the first pixbuf or text segment because toggles at
2202 * the beginning of the paragraph should affect the
2203 * paragraph-global values
2205 if (!para_values_set)
2207 set_para_values (layout, base_dir, style, display);
2208 para_values_set = TRUE;
2211 /* First see if the chunk is invisible, and ignore it if so. Tk
2212 * looked at tabs, wrap mode, etc. before doing this, but
2213 * that made no sense to me, so I am just skipping the
2216 if (!style->invisible)
2218 if (seg->type == >k_text_char_type)
2220 /* We don't want to split segments because of marks,
2221 * so we scan forward for more segments only
2222 * separated from us by marks. In theory, we should
2223 * also merge segments with identical styles, even
2224 * if there are toggles in-between
2228 GtkTextLineSegment *prev_seg = NULL;
2232 if (seg->type == >k_text_char_type)
2234 memcpy (text + layout_byte_offset, seg->body.chars, seg->byte_count);
2235 layout_byte_offset += seg->byte_count;
2236 buffer_byte_offset += seg->byte_count;
2237 bytes += seg->byte_count;
2239 else if (seg->type == >k_text_right_mark_type ||
2240 seg->type == >k_text_left_mark_type)
2242 /* If we have preedit string, break out of this loop - we'll almost
2243 * certainly have different attributes on the preedit string
2246 if (layout->preedit_len > 0 &&
2247 _gtk_text_btree_mark_is_insert (_gtk_text_buffer_get_btree (layout->buffer),
2248 seg->body.mark.obj))
2251 if (seg->body.mark.visible)
2253 cursor_byte_offsets = g_slist_prepend (cursor_byte_offsets, GINT_TO_POINTER (layout_byte_offset));
2254 cursor_segs = g_slist_prepend (cursor_segs, seg);
2255 if (_gtk_text_btree_mark_is_insert (_gtk_text_buffer_get_btree (layout->buffer),
2256 seg->body.mark.obj))
2257 display->insert_index = layout_byte_offset;
2267 seg = prev_seg; /* Back up one */
2268 add_generic_attrs (layout, &style->appearance,
2270 attrs, layout_byte_offset - bytes,
2272 add_text_attrs (layout, style, bytes, attrs,
2273 layout_byte_offset - bytes, size_only);
2275 else if (seg->type == >k_text_pixbuf_type)
2277 add_generic_attrs (layout,
2280 attrs, layout_byte_offset,
2282 add_pixbuf_attrs (layout, display, style,
2283 seg, attrs, layout_byte_offset);
2284 memcpy (text + layout_byte_offset, _gtk_text_unknown_char_utf8,
2286 layout_byte_offset += seg->byte_count;
2287 buffer_byte_offset += seg->byte_count;
2289 else if (seg->type == >k_text_child_type)
2293 add_generic_attrs (layout, &style->appearance,
2295 attrs, layout_byte_offset,
2297 add_child_attrs (layout, display, style,
2298 seg, attrs, layout_byte_offset);
2299 memcpy (text + layout_byte_offset, _gtk_text_unknown_char_utf8,
2301 layout_byte_offset += seg->byte_count;
2302 buffer_byte_offset += seg->byte_count;
2306 /* We don't know this segment type */
2307 g_assert_not_reached ();
2310 } /* if (segment was visible) */
2313 /* Invisible segment */
2314 buffer_byte_offset += seg->byte_count;
2317 release_style (layout, style);
2321 else if (seg->type == >k_text_toggle_on_type ||
2322 seg->type == >k_text_toggle_off_type)
2324 /* Style may have changed, drop our
2325 current cached style */
2326 invalidate_cached_style (layout);
2327 /* Add the tag only after we have seen some non-toggle non-mark segment,
2328 * otherwise the tag is already accounted for by _gtk_text_btree_get_tags(). */
2329 if (!initial_toggle_segments)
2330 tags = tags_array_toggle_tag (tags, seg->body.toggle.info->tag);
2334 else if (seg->type == >k_text_right_mark_type ||
2335 seg->type == >k_text_left_mark_type)
2337 gint cursor_offset = 0;
2339 /* At the insertion point, add the preedit string, if any */
2341 if (_gtk_text_btree_mark_is_insert (_gtk_text_buffer_get_btree (layout->buffer),
2342 seg->body.mark.obj))
2344 display->insert_index = layout_byte_offset;
2346 if (layout->preedit_len > 0)
2348 text_allocated += layout->preedit_len;
2349 text = g_realloc (text, text_allocated);
2351 style = get_style (layout, tags);
2352 add_preedit_attrs (layout, style, attrs, layout_byte_offset, size_only);
2353 release_style (layout, style);
2355 memcpy (text + layout_byte_offset, layout->preedit_string, layout->preedit_len);
2356 layout_byte_offset += layout->preedit_len;
2357 /* DO NOT increment the buffer byte offset for preedit */
2359 cursor_offset = layout->preedit_cursor - layout->preedit_len;
2364 /* Display visible marks */
2366 if (seg->body.mark.visible)
2368 cursor_byte_offsets = g_slist_prepend (cursor_byte_offsets,
2369 GINT_TO_POINTER (layout_byte_offset + cursor_offset));
2370 cursor_segs = g_slist_prepend (cursor_segs, seg);
2375 g_error ("Unknown segment type: %s", seg->type->name);
2380 if (!para_values_set)
2382 style = get_style (layout, tags);
2383 set_para_values (layout, base_dir, style, display);
2384 release_style (layout, style);
2387 /* Pango doesn't want the trailing paragraph delimiters */
2390 /* Only one character has type G_UNICODE_PARAGRAPH_SEPARATOR in
2391 * Unicode 3.0; update this if that changes.
2393 #define PARAGRAPH_SEPARATOR 0x2029
2396 if (layout_byte_offset > 0)
2398 const char *prev = g_utf8_prev_char (text + layout_byte_offset);
2399 ch = g_utf8_get_char (prev);
2400 if (ch == PARAGRAPH_SEPARATOR || ch == '\r' || ch == '\n')
2401 layout_byte_offset = prev - text; /* chop off */
2403 if (ch == '\n' && layout_byte_offset > 0)
2405 /* Possibly chop a CR as well */
2406 prev = g_utf8_prev_char (text + layout_byte_offset);
2408 --layout_byte_offset;
2413 pango_layout_set_text (display->layout, text, layout_byte_offset);
2414 pango_layout_set_attributes (display->layout, attrs);
2416 tmp_list1 = cursor_byte_offsets;
2417 tmp_list2 = cursor_segs;
2420 add_cursor (layout, display, tmp_list2->data,
2421 GPOINTER_TO_INT (tmp_list1->data));
2422 tmp_list1 = tmp_list1->next;
2423 tmp_list2 = tmp_list2->next;
2425 g_slist_free (cursor_byte_offsets);
2426 g_slist_free (cursor_segs);
2428 pango_layout_get_extents (display->layout, NULL, &extents);
2430 display->width = PIXEL_BOUND (extents.width) + display->left_margin + display->right_margin;
2431 display->height += PANGO_PIXELS (extents.height);
2433 /* If we aren't wrapping, we need to do the alignment of each
2434 * paragraph ourselves.
2436 if (pango_layout_get_width (display->layout) < 0)
2438 gint excess = display->total_width - display->width;
2440 switch (pango_layout_get_alignment (display->layout))
2442 case PANGO_ALIGN_LEFT:
2444 case PANGO_ALIGN_CENTER:
2445 display->x_offset += excess / 2;
2447 case PANGO_ALIGN_RIGHT:
2448 display->x_offset += excess;
2453 /* Free this if we aren't in a loop */
2454 if (layout->wrap_loop_count == 0)
2455 invalidate_cached_style (layout);
2458 pango_attr_list_unref (attrs);
2460 g_ptr_array_free (tags, TRUE);
2462 layout->one_display_cache = display;
2465 allocate_child_widgets (layout, display);
2471 gtk_text_layout_free_line_display (GtkTextLayout *layout,
2472 GtkTextLineDisplay *display)
2474 if (display != layout->one_display_cache)
2476 if (display->layout)
2477 g_object_unref (display->layout);
2479 if (display->cursors)
2480 g_array_free (display->cursors, TRUE);
2482 if (display->pg_bg_color)
2483 gdk_color_free (display->pg_bg_color);
2485 if (display->pg_bg_rgba)
2486 gdk_rgba_free (display->pg_bg_rgba);
2488 g_slice_free (GtkTextLineDisplay, display);
2492 /* Functions to convert iter <=> index for the line of a GtkTextLineDisplay
2493 * taking into account the preedit string and invisible text if necessary.
2496 line_display_iter_to_index (GtkTextLayout *layout,
2497 GtkTextLineDisplay *display,
2498 const GtkTextIter *iter)
2502 g_return_val_if_fail (_gtk_text_iter_get_text_line (iter) == display->line, 0);
2504 index = gtk_text_iter_get_visible_line_index (iter);
2506 if (layout->preedit_len > 0 && display->insert_index >= 0)
2508 if (index >= display->insert_index)
2509 index += layout->preedit_len;
2516 line_display_index_to_iter (GtkTextLayout *layout,
2517 GtkTextLineDisplay *display,
2522 g_return_if_fail (!_gtk_text_line_is_last (display->line,
2523 _gtk_text_buffer_get_btree (layout->buffer)));
2525 if (layout->preedit_len > 0 && display->insert_index >= 0)
2527 if (index >= display->insert_index + layout->preedit_len)
2528 index -= layout->preedit_len;
2529 else if (index > display->insert_index)
2531 index = display->insert_index;
2536 gtk_text_layout_get_iter_at_line (layout, iter, display->line, 0);
2538 gtk_text_iter_set_visible_line_index (iter, index);
2540 if (_gtk_text_iter_get_text_line (iter) != display->line)
2542 /* Clamp to end of line - really this clamping should have been done
2543 * before here, maybe in Pango, this is a broken band-aid I think
2545 gtk_text_layout_get_iter_at_line (layout, iter, display->line, 0);
2546 if (!gtk_text_iter_ends_line (iter))
2547 gtk_text_iter_forward_to_line_end (iter);
2550 gtk_text_iter_forward_chars (iter, trailing);
2554 get_line_at_y (GtkTextLayout *layout,
2561 if (y > layout->height)
2564 *line = _gtk_text_btree_find_line_by_y (_gtk_text_buffer_get_btree (layout->buffer),
2565 layout, y, line_top);
2568 *line = _gtk_text_btree_get_end_iter_line (_gtk_text_buffer_get_btree (layout->buffer));
2572 _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
2578 * gtk_text_layout_get_line_at_y:
2579 * @layout: a #GtkLayout
2580 * @target_iter: the iterator in which the result is stored
2581 * @y: the y positition
2582 * @line_top: location to store the y coordinate of the
2583 * top of the line. (Can by %NULL)
2585 * Get the iter at the beginning of the line which is displayed
2589 gtk_text_layout_get_line_at_y (GtkTextLayout *layout,
2590 GtkTextIter *target_iter,
2596 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
2597 g_return_if_fail (target_iter != NULL);
2599 get_line_at_y (layout, y, &line, line_top);
2600 gtk_text_layout_get_iter_at_line (layout, target_iter, line, 0);
2604 gtk_text_layout_get_iter_at_pixel (GtkTextLayout *layout,
2605 GtkTextIter *target_iter,
2611 gtk_text_layout_get_iter_at_position (layout, target_iter, &trailing, x, y);
2613 gtk_text_iter_forward_chars (target_iter, trailing);
2616 void gtk_text_layout_get_iter_at_position (GtkTextLayout *layout,
2617 GtkTextIter *target_iter,
2625 GtkTextLineDisplay *display;
2627 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
2628 g_return_if_fail (target_iter != NULL);
2630 get_line_at_y (layout, y, &line, &line_top);
2632 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2634 x -= display->x_offset;
2635 y -= line_top + display->top_margin;
2637 /* If we are below the layout, position the cursor at the last character
2640 if (y > display->height - display->top_margin - display->bottom_margin)
2642 byte_index = _gtk_text_line_byte_count (line);
2648 /* Ignore the "outside" return value from pango. Pango is doing
2649 * the right thing even if we are outside the layout in the
2652 pango_layout_xy_to_index (display->layout, x * PANGO_SCALE, y * PANGO_SCALE,
2653 &byte_index, trailing);
2656 line_display_index_to_iter (layout, display, target_iter, byte_index, 0);
2658 gtk_text_layout_free_line_display (layout, display);
2663 * gtk_text_layout_get_cursor_locations:
2664 * @layout: a #GtkTextLayout
2665 * @iter: a #GtkTextIter
2666 * @strong_pos: (allow-none): location to store the strong cursor position (may be %NULL)
2667 * @weak_pos: (allow-none): location to store the weak cursor position (may be %NULL)
2669 * Given an iterator within a text layout, determine the positions of the
2670 * strong and weak cursors if the insertion point is at that
2671 * iterator. The position of each cursor is stored as a zero-width
2672 * rectangle. The strong cursor location is the location where
2673 * characters of the directionality equal to the base direction of the
2674 * paragraph are inserted. The weak cursor location is the location
2675 * where characters of the directionality opposite to the base
2676 * direction of the paragraph are inserted.
2679 gtk_text_layout_get_cursor_locations (GtkTextLayout *layout,
2681 GdkRectangle *strong_pos,
2682 GdkRectangle *weak_pos)
2685 GtkTextLineDisplay *display;
2688 GtkTextIter insert_iter;
2690 PangoRectangle pango_strong_pos;
2691 PangoRectangle pango_weak_pos;
2693 g_return_if_fail (layout != NULL);
2694 g_return_if_fail (iter != NULL);
2696 line = _gtk_text_iter_get_text_line (iter);
2697 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2698 index = line_display_iter_to_index (layout, display, iter);
2700 line_top = _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
2703 gtk_text_buffer_get_iter_at_mark (layout->buffer, &insert_iter,
2704 gtk_text_buffer_get_insert (layout->buffer));
2706 if (gtk_text_iter_equal (iter, &insert_iter))
2707 index += layout->preedit_cursor - layout->preedit_len;
2709 pango_layout_get_cursor_pos (display->layout, index,
2710 strong_pos ? &pango_strong_pos : NULL,
2711 weak_pos ? &pango_weak_pos : NULL);
2715 strong_pos->x = display->x_offset + pango_strong_pos.x / PANGO_SCALE;
2716 strong_pos->y = line_top + display->top_margin + pango_strong_pos.y / PANGO_SCALE;
2717 strong_pos->width = 0;
2718 strong_pos->height = pango_strong_pos.height / PANGO_SCALE;
2723 weak_pos->x = display->x_offset + pango_weak_pos.x / PANGO_SCALE;
2724 weak_pos->y = line_top + display->top_margin + pango_weak_pos.y / PANGO_SCALE;
2725 weak_pos->width = 0;
2726 weak_pos->height = pango_weak_pos.height / PANGO_SCALE;
2729 gtk_text_layout_free_line_display (layout, display);
2733 * _gtk_text_layout_get_block_cursor:
2734 * @layout: a #GtkTextLayout
2735 * @pos: a #GdkRectangle to store block cursor position
2737 * If layout is to display a block cursor, calculates its position
2738 * and returns %TRUE. Otherwise it returns %FALSE. In case when
2739 * cursor is visible, it simply returns the position stored in
2740 * the line display, otherwise it has to compute the position
2741 * (see get_block_cursor()).
2744 _gtk_text_layout_get_block_cursor (GtkTextLayout *layout,
2748 GtkTextLineDisplay *display;
2751 gboolean block = FALSE;
2753 g_return_val_if_fail (layout != NULL, FALSE);
2755 gtk_text_buffer_get_iter_at_mark (layout->buffer, &iter,
2756 gtk_text_buffer_get_insert (layout->buffer));
2757 line = _gtk_text_iter_get_text_line (&iter);
2758 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2760 if (display->has_block_cursor)
2763 rect = display->block_cursor;
2767 gint index = display->insert_index;
2770 index = gtk_text_iter_get_line_index (&iter);
2772 if (get_block_cursor (layout, display, &iter, index, &rect, NULL))
2780 line_top = _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
2784 pos->x += display->x_offset;
2785 pos->y += line_top + display->top_margin;
2788 gtk_text_layout_free_line_display (layout, display);
2793 * gtk_text_layout_get_line_yrange:
2794 * @layout: a #GtkTextLayout
2795 * @iter: a #GtkTextIter
2796 * @y: location to store the top of the paragraph in pixels,
2798 * @height location to store the height of the paragraph in pixels,
2801 * Find the range of y coordinates for the paragraph containing
2805 gtk_text_layout_get_line_yrange (GtkTextLayout *layout,
2806 const GtkTextIter *iter,
2812 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
2813 g_return_if_fail (_gtk_text_iter_get_btree (iter) == _gtk_text_buffer_get_btree (layout->buffer));
2815 line = _gtk_text_iter_get_text_line (iter);
2818 *y = _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
2822 GtkTextLineData *line_data = _gtk_text_line_get_data (line, layout);
2824 *height = line_data->height;
2831 * _gtk_text_layout_get_line_xrange:
2832 * @layout: a #GtkTextLayout
2833 * @iter: a #GtkTextIter
2834 * @x: location to store the top of the paragraph in pixels,
2836 * @width location to store the height of the paragraph in pixels,
2839 * Find the range of X coordinates for the paragraph containing
2840 * the given iter. Private for 2.0 due to API freeze, could
2841 * be made public for 2.2.
2844 _gtk_text_layout_get_line_xrange (GtkTextLayout *layout,
2845 const GtkTextIter *iter,
2851 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
2852 g_return_if_fail (_gtk_text_iter_get_btree (iter) == _gtk_text_buffer_get_btree (layout->buffer));
2854 line = _gtk_text_iter_get_text_line (iter);
2857 *x = 0; /* FIXME This is wrong; should represent the first available cursor position */
2861 GtkTextLineData *line_data = _gtk_text_line_get_data (line, layout);
2863 *width = line_data->width;
2870 gtk_text_layout_get_iter_location (GtkTextLayout *layout,
2871 const GtkTextIter *iter,
2874 PangoRectangle pango_rect;
2877 GtkTextLineDisplay *display;
2881 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
2882 g_return_if_fail (_gtk_text_iter_get_btree (iter) == _gtk_text_buffer_get_btree (layout->buffer));
2883 g_return_if_fail (rect != NULL);
2885 tree = _gtk_text_iter_get_btree (iter);
2886 line = _gtk_text_iter_get_text_line (iter);
2888 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2890 rect->y = _gtk_text_btree_find_line_top (tree, line, layout);
2892 x_offset = display->x_offset * PANGO_SCALE;
2894 byte_index = gtk_text_iter_get_line_index (iter);
2896 pango_layout_index_to_pos (display->layout, byte_index, &pango_rect);
2898 rect->x = PANGO_PIXELS (x_offset + pango_rect.x);
2899 rect->y += PANGO_PIXELS (pango_rect.y) + display->top_margin;
2900 rect->width = PANGO_PIXELS (pango_rect.width);
2901 rect->height = PANGO_PIXELS (pango_rect.height);
2903 gtk_text_layout_free_line_display (layout, display);
2908 /* Find the iter for the logical beginning of the first display line whose
2909 * top y is >= y. If none exists, move the iter to the logical beginning
2910 * of the last line in the buffer.
2913 find_display_line_below (GtkTextLayout *layout,
2917 GtkTextLine *line, *next;
2918 GtkTextLine *found_line = NULL;
2920 gint found_byte = 0;
2922 line = _gtk_text_btree_find_line_by_y (_gtk_text_buffer_get_btree (layout->buffer),
2923 layout, y, &line_top);
2927 _gtk_text_btree_get_end_iter_line (_gtk_text_buffer_get_btree (layout->buffer));
2930 _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
2934 while (line && !found_line)
2936 GtkTextLineDisplay *display = gtk_text_layout_get_line_display (layout, line, FALSE);
2937 PangoLayoutIter *layout_iter;
2939 layout_iter = pango_layout_get_iter (display->layout);
2941 line_top += display->top_margin;
2945 gint first_y, last_y;
2946 PangoLayoutLine *layout_line = pango_layout_iter_get_line_readonly (layout_iter);
2948 found_byte = layout_line->start_index;
2956 pango_layout_iter_get_line_yrange (layout_iter, &first_y, &last_y);
2957 line_top += (last_y - first_y) / PANGO_SCALE;
2959 while (pango_layout_iter_next_line (layout_iter));
2961 pango_layout_iter_free (layout_iter);
2963 line_top += display->bottom_margin;
2964 gtk_text_layout_free_line_display (layout, display);
2966 next = _gtk_text_line_next_excluding_last (line);
2973 gtk_text_layout_get_iter_at_line (layout, iter, found_line, found_byte);
2976 /* Find the iter for the logical beginning of the last display line whose
2977 * top y is >= y. If none exists, move the iter to the logical beginning
2978 * of the first line in the buffer.
2981 find_display_line_above (GtkTextLayout *layout,
2986 GtkTextLine *found_line = NULL;
2988 gint found_byte = 0;
2990 line = _gtk_text_btree_find_line_by_y (_gtk_text_buffer_get_btree (layout->buffer), layout, y, &line_top);
2993 line = _gtk_text_btree_get_end_iter_line (_gtk_text_buffer_get_btree (layout->buffer));
2995 line_top = _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer), line, layout);
2998 while (line && !found_line)
3000 GtkTextLineDisplay *display = gtk_text_layout_get_line_display (layout, line, FALSE);
3001 PangoRectangle logical_rect;
3002 PangoLayoutIter *layout_iter;
3005 layout_iter = pango_layout_get_iter (display->layout);
3007 line_top -= display->top_margin + display->bottom_margin;
3008 pango_layout_iter_get_layout_extents (layout_iter, NULL, &logical_rect);
3009 line_top -= logical_rect.height / PANGO_SCALE;
3011 tmp_top = line_top + display->top_margin;
3015 gint first_y, last_y;
3016 PangoLayoutLine *layout_line = pango_layout_iter_get_line_readonly (layout_iter);
3018 found_byte = layout_line->start_index;
3020 pango_layout_iter_get_line_yrange (layout_iter, &first_y, &last_y);
3022 tmp_top -= (last_y - first_y) / PANGO_SCALE;
3027 pango_layout_iter_free (layout_iter);
3031 while (pango_layout_iter_next_line (layout_iter));
3033 pango_layout_iter_free (layout_iter);
3035 gtk_text_layout_free_line_display (layout, display);
3037 line = _gtk_text_line_previous (line);
3043 gtk_text_layout_get_iter_at_line (layout, iter, found_line, found_byte);
3045 gtk_text_buffer_get_iter_at_offset (layout->buffer, iter, 0);
3049 * gtk_text_layout_clamp_iter_to_vrange:
3050 * @layout: a #GtkTextLayout
3051 * @iter: a #GtkTextIter
3052 * @top: the top of the range
3053 * @bottom: the bottom the range
3055 * If the iterator is not fully in the range @top <= y < @bottom,
3056 * then, if possible, move it the minimum distance so that the
3057 * iterator in this range.
3059 * Returns: %TRUE if the iterator was moved, otherwise %FALSE.
3062 gtk_text_layout_clamp_iter_to_vrange (GtkTextLayout *layout,
3067 GdkRectangle iter_rect;
3069 gtk_text_layout_get_iter_location (layout, iter, &iter_rect);
3071 /* If the iter is at least partially above the range, put the iter
3072 * at the first fully visible line after the range.
3074 if (iter_rect.y < top)
3076 find_display_line_below (layout, iter, top);
3080 /* Otherwise, if the iter is at least partially below the screen, put the
3081 * iter on the last logical position of the last completely visible
3084 else if (iter_rect.y + iter_rect.height > bottom)
3086 find_display_line_above (layout, iter, bottom);
3095 * gtk_text_layout_move_iter_to_previous_line:
3096 * @layout: a #GtkLayout
3097 * @iter: a #GtkTextIter
3099 * Move the iterator to the beginning of the previous line. The lines
3100 * of a wrapped paragraph are treated as distinct for this operation.
3103 gtk_text_layout_move_iter_to_previous_line (GtkTextLayout *layout,
3107 GtkTextLineDisplay *display;
3110 PangoLayoutLine *layout_line;
3112 gboolean update_byte = FALSE;
3114 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), FALSE);
3115 g_return_val_if_fail (iter != NULL, FALSE);
3120 line = _gtk_text_iter_get_text_line (iter);
3121 display = gtk_text_layout_get_line_display (layout, line, FALSE);
3122 line_byte = line_display_iter_to_index (layout, display, iter);
3124 /* If display->height == 0 then the line is invisible, so don't
3127 while (display->height == 0)
3129 GtkTextLine *prev_line;
3131 prev_line = _gtk_text_line_previous (line);
3133 if (prev_line == NULL)
3135 line_display_index_to_iter (layout, display, iter, 0, 0);
3139 gtk_text_layout_free_line_display (layout, display);
3142 display = gtk_text_layout_get_line_display (layout, prev_line, FALSE);
3146 tmp_list = pango_layout_get_lines_readonly (display->layout);
3147 layout_line = tmp_list->data;
3151 line_byte = layout_line->start_index + layout_line->length;
3154 if (line_byte < layout_line->length || !tmp_list->next) /* first line of paragraph */
3156 GtkTextLine *prev_line;
3158 prev_line = _gtk_text_line_previous (line);
3160 /* first line of the whole buffer, do not move the iter and return FALSE */
3161 if (prev_line == NULL)
3166 gtk_text_layout_free_line_display (layout, display);
3168 display = gtk_text_layout_get_line_display (layout, prev_line, FALSE);
3170 if (display->height > 0)
3172 tmp_list = g_slist_last (pango_layout_get_lines_readonly (display->layout));
3173 layout_line = tmp_list->data;
3175 line_display_index_to_iter (layout, display, iter,
3176 layout_line->start_index + layout_line->length, 0);
3180 prev_line = _gtk_text_line_previous (prev_line);
3185 gint prev_offset = layout_line->start_index;
3187 tmp_list = tmp_list->next;
3190 layout_line = tmp_list->data;
3192 if (line_byte < layout_line->start_index + layout_line->length ||
3195 line_display_index_to_iter (layout, display, iter, prev_offset, 0);
3199 prev_offset = layout_line->start_index;
3200 tmp_list = tmp_list->next;
3206 gtk_text_layout_free_line_display (layout, display);
3209 !gtk_text_iter_equal (iter, &orig) &&
3210 !gtk_text_iter_is_end (iter);
3214 * gtk_text_layout_move_iter_to_next_line:
3215 * @layout: a #GtkLayout
3216 * @iter: a #GtkTextIter
3218 * Move the iterator to the beginning of the next line. The
3219 * lines of a wrapped paragraph are treated as distinct for
3223 gtk_text_layout_move_iter_to_next_line (GtkTextLayout *layout,
3227 GtkTextLineDisplay *display;
3230 gboolean found = FALSE;
3231 gboolean found_after = FALSE;
3232 gboolean first = TRUE;
3234 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), FALSE);
3235 g_return_val_if_fail (iter != NULL, FALSE);
3239 line = _gtk_text_iter_get_text_line (iter);
3241 while (line && !found_after)
3245 display = gtk_text_layout_get_line_display (layout, line, FALSE);
3247 if (display->height == 0)
3252 line_byte = line_display_iter_to_index (layout, display, iter);
3258 tmp_list = pango_layout_get_lines_readonly (display->layout);
3259 while (tmp_list && !found_after)
3261 PangoLayoutLine *layout_line = tmp_list->data;
3265 line_display_index_to_iter (layout, display, iter,
3266 layout_line->start_index, 0);
3269 else if (line_byte < layout_line->start_index + layout_line->length || !tmp_list->next)
3272 tmp_list = tmp_list->next;
3277 gtk_text_layout_free_line_display (layout, display);
3279 line = _gtk_text_line_next_excluding_last (line);
3283 gtk_text_buffer_get_end_iter (layout->buffer, iter);
3286 !gtk_text_iter_equal (iter, &orig) &&
3287 !gtk_text_iter_is_end (iter);
3291 * gtk_text_layout_move_iter_to_line_end:
3292 * @layout: a #GtkTextLayout
3293 * @direction: if negative, move to beginning of line, otherwise
3294 move to end of line.
3296 * Move to the beginning or end of a display line.
3299 gtk_text_layout_move_iter_to_line_end (GtkTextLayout *layout,
3304 GtkTextLineDisplay *display;
3309 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), FALSE);
3310 g_return_val_if_fail (iter != NULL, FALSE);
3314 line = _gtk_text_iter_get_text_line (iter);
3315 display = gtk_text_layout_get_line_display (layout, line, FALSE);
3316 line_byte = line_display_iter_to_index (layout, display, iter);
3318 tmp_list = pango_layout_get_lines_readonly (display->layout);
3321 PangoLayoutLine *layout_line = tmp_list->data;
3323 if (line_byte < layout_line->start_index + layout_line->length || !tmp_list->next)
3325 line_display_index_to_iter (layout, display, iter,
3326 direction < 0 ? layout_line->start_index : layout_line->start_index + layout_line->length,
3329 /* FIXME: As a bad hack, we move back one position when we
3330 * are inside a paragraph to avoid going to next line on a
3331 * forced break not at whitespace. Real fix is to keep track
3332 * of whether marks are at leading or trailing edge? */
3333 if (direction > 0 && layout_line->length > 0 &&
3334 !gtk_text_iter_ends_line (iter) &&
3335 !_gtk_text_btree_char_is_invisible (iter))
3336 gtk_text_iter_backward_char (iter);
3340 tmp_list = tmp_list->next;
3343 gtk_text_layout_free_line_display (layout, display);
3346 !gtk_text_iter_equal (iter, &orig) &&
3347 !gtk_text_iter_is_end (iter);
3352 * gtk_text_layout_iter_starts_line:
3353 * @layout: a #GtkTextLayout
3354 * @iter: iterator to test
3356 * Tests whether an iterator is at the start of a display line.
3359 gtk_text_layout_iter_starts_line (GtkTextLayout *layout,
3360 const GtkTextIter *iter)
3363 GtkTextLineDisplay *display;
3367 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), FALSE);
3368 g_return_val_if_fail (iter != NULL, FALSE);
3370 line = _gtk_text_iter_get_text_line (iter);
3371 display = gtk_text_layout_get_line_display (layout, line, FALSE);
3372 line_byte = line_display_iter_to_index (layout, display, iter);
3374 tmp_list = pango_layout_get_lines_readonly (display->layout);
3377 PangoLayoutLine *layout_line = tmp_list->data;
3379 if (line_byte < layout_line->start_index + layout_line->length ||
3382 /* We're located on this line or the para delimiters before
3385 gtk_text_layout_free_line_display (layout, display);
3387 if (line_byte == layout_line->start_index)
3393 tmp_list = tmp_list->next;
3396 g_assert_not_reached ();
3401 gtk_text_layout_get_iter_at_line (GtkTextLayout *layout,
3406 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
3407 iter, line, byte_offset);
3411 * gtk_text_layout_move_iter_to_x:
3412 * @layout: a #GtkTextLayout
3413 * @iter: a #GtkTextIter
3416 * Keeping the iterator on the same line of the layout, move it to the
3417 * specified X coordinate. The lines of a wrapped paragraph are
3418 * treated as distinct for this operation.
3421 gtk_text_layout_move_iter_to_x (GtkTextLayout *layout,
3426 GtkTextLineDisplay *display;
3428 PangoLayoutIter *layout_iter;
3430 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
3431 g_return_if_fail (iter != NULL);
3433 line = _gtk_text_iter_get_text_line (iter);
3435 display = gtk_text_layout_get_line_display (layout, line, FALSE);
3436 line_byte = line_display_iter_to_index (layout, display, iter);
3438 layout_iter = pango_layout_get_iter (display->layout);
3442 PangoLayoutLine *layout_line = pango_layout_iter_get_line_readonly (layout_iter);
3444 if (line_byte < layout_line->start_index + layout_line->length ||
3445 pango_layout_iter_at_last_line (layout_iter))
3447 PangoRectangle logical_rect;
3448 gint byte_index, trailing;
3449 gint x_offset = display->x_offset * PANGO_SCALE;
3451 pango_layout_iter_get_line_extents (layout_iter, NULL, &logical_rect);
3453 pango_layout_line_x_to_index (layout_line,
3454 x * PANGO_SCALE - x_offset - logical_rect.x,
3455 &byte_index, &trailing);
3457 line_display_index_to_iter (layout, display, iter, byte_index, trailing);
3462 while (pango_layout_iter_next_line (layout_iter));
3464 pango_layout_iter_free (layout_iter);
3466 gtk_text_layout_free_line_display (layout, display);
3470 * gtk_text_layout_move_iter_visually:
3471 * @layout: a #GtkTextLayout
3472 * @iter: a #GtkTextIter
3473 * @count: number of characters to move (negative moves left, positive moves right)
3475 * Move the iterator a given number of characters visually, treating
3476 * it as the strong cursor position. If @count is positive, then the
3477 * new strong cursor position will be @count positions to the right of
3478 * the old cursor position. If @count is negative then the new strong
3479 * cursor position will be @count positions to the left of the old
3482 * In the presence of bidirection text, the correspondence
3483 * between logical and visual order will depend on the direction
3484 * of the current run, and there may be jumps when the cursor
3485 * is moved off of the end of a run.
3489 gtk_text_layout_move_iter_visually (GtkTextLayout *layout,
3493 GtkTextLineDisplay *display = NULL;
3495 GtkTextIter lineiter;
3497 g_return_val_if_fail (layout != NULL, FALSE);
3498 g_return_val_if_fail (iter != NULL, FALSE);
3504 GtkTextLine *line = _gtk_text_iter_get_text_line (iter);
3506 gint extra_back = 0;
3509 int byte_count = _gtk_text_line_byte_count (line);
3515 display = gtk_text_layout_get_line_display (layout, line, FALSE);
3517 if (layout->cursor_direction == GTK_TEXT_DIR_NONE)
3520 strong = display->direction == layout->cursor_direction;
3522 line_byte = line_display_iter_to_index (layout, display, iter);
3526 pango_layout_move_cursor_visually (display->layout, strong, line_byte, 0, 1, &new_index, &new_trailing);
3531 pango_layout_move_cursor_visually (display->layout, strong, line_byte, 0, -1, &new_index, &new_trailing);
3535 /* We need to handle the preedit string specially. Well, we don't really need to
3536 * handle it specially, since hopefully calling gtk_im_context_reset() will
3537 * remove the preedit string; but if we start off in front of the preedit
3538 * string (logically) and end up in or on the back edge of the preedit string,
3539 * we should move the iter one place farther.
3541 if (layout->preedit_len > 0 && display->insert_index >= 0)
3543 if (line_byte == display->insert_index + layout->preedit_len &&
3544 new_index < display->insert_index + layout->preedit_len)
3546 line_byte = display->insert_index;
3551 if (new_index < 0 || (new_index == 0 && extra_back))
3555 line = _gtk_text_line_previous (line);
3559 while (totally_invisible_line (layout, line, &lineiter));
3561 gtk_text_layout_free_line_display (layout, display);
3562 display = gtk_text_layout_get_line_display (layout, line, FALSE);
3563 gtk_text_iter_forward_to_line_end (&lineiter);
3564 new_index = gtk_text_iter_get_visible_line_index (&lineiter);
3566 else if (new_index > byte_count)
3570 line = _gtk_text_line_next_excluding_last (line);
3574 while (totally_invisible_line (layout, line, &lineiter));
3576 gtk_text_layout_free_line_display (layout, display);
3577 display = gtk_text_layout_get_line_display (layout, line, FALSE);
3581 line_display_index_to_iter (layout, display, iter, new_index, new_trailing);
3583 gtk_text_iter_backward_char (iter);
3586 gtk_text_layout_free_line_display (layout, display);
3591 !gtk_text_iter_equal (iter, &orig) &&
3592 !gtk_text_iter_is_end (iter);
3596 gtk_text_layout_spew (GtkTextLayout *layout)
3599 GtkTextDisplayLine *iter;
3601 guint paragraphs = 0;
3602 GtkTextLine *last_line = NULL;
3604 iter = layout->line_list;
3605 while (iter != NULL)
3607 if (iter->line != last_line)
3609 printf ("%5u paragraph (%p)\n", paragraphs, iter->line);
3611 last_line = iter->line;
3614 printf (" %5u y: %d len: %d start: %d bytes: %d\n",
3615 wrapped, iter->y, iter->length, iter->byte_offset,
3622 printf ("Layout %s recompute\n",
3623 layout->need_recompute ? "needs" : "doesn't need");
3625 printf ("Layout pars: %u lines: %u size: %d x %d Screen width: %d\n",
3626 paragraphs, wrapped, layout->width,
3627 layout->height, layout->screen_width);
3631 /* Catch all situations that move the insertion point.
3634 gtk_text_layout_mark_set_handler (GtkTextBuffer *buffer,
3635 const GtkTextIter *location,
3639 GtkTextLayout *layout = GTK_TEXT_LAYOUT (data);
3641 if (mark == gtk_text_buffer_get_insert (buffer))
3642 gtk_text_layout_update_cursor_line (layout);
3646 gtk_text_layout_buffer_insert_text (GtkTextBuffer *textbuffer,
3652 GtkTextLayout *layout = GTK_TEXT_LAYOUT (data);
3654 gtk_text_layout_update_cursor_line (layout);
3658 gtk_text_layout_buffer_delete_range (GtkTextBuffer *textbuffer,
3663 GtkTextLayout *layout = GTK_TEXT_LAYOUT (data);
3665 gtk_text_layout_update_cursor_line (layout);