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, write to the Free
27 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 * Original Tk license:
31 * This software is copyrighted by the Regents of the University of
32 * California, Sun Microsystems, Inc., and other parties. The
33 * following terms apply to all files associated with the software
34 * unless explicitly disclaimed in individual files.
36 * The authors hereby grant permission to use, copy, modify,
37 * distribute, and license this software and its documentation for any
38 * purpose, provided that existing copyright notices are retained in
39 * all copies and that this notice is included verbatim in any
40 * distributions. No written agreement, license, or royalty fee is
41 * required for any of the authorized uses. Modifications to this
42 * software may be copyrighted by their authors and need not follow
43 * the licensing terms described here, provided that the new terms are
44 * clearly indicated on the first page of each file where they apply.
46 * IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY
47 * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
48 * DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION,
49 * OR ANY DERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE BEEN ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
52 * THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES,
53 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
54 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND
55 * NON-INFRINGEMENT. THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS,
56 * AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO PROVIDE
57 * MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
59 * GOVERNMENT USE: If you are acquiring this software on behalf of the
60 * U.S. government, the Government shall have only "Restricted Rights"
61 * in the software and related documentation as defined in the Federal
62 * Acquisition Regulations (FARs) in Clause 52.227.19 (c) (2). If you
63 * are acquiring the software on behalf of the Department of Defense,
64 * the software shall be classified as "Commercial Computer Software"
65 * and the Government shall have only "Restricted Rights" as defined
66 * in Clause 252.227-7013 (c) (1) of DFARs. Notwithstanding the
67 * foregoing, the authors grant the U.S. Government and others acting
68 * in its behalf permission to use and distribute the software in
69 * accordance with the terms specified in this license.
73 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
74 * file for a list of people on the GTK+ Team. See the ChangeLog
75 * files for a list of changes. These files are distributed with
76 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
79 #define GTK_TEXT_USE_INTERNAL_UNSUPPORTED_API
81 #include "gtkmarshalers.h"
82 #include "gtktextlayout.h"
83 #include "gtktextbtree.h"
84 #include "gtktextiterprivate.h"
85 #include "gtktextutil.h"
92 #define GTK_TEXT_LAYOUT_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GTK_TYPE_TEXT_LAYOUT, GtkTextLayoutPrivate))
94 typedef struct _GtkTextLayoutPrivate GtkTextLayoutPrivate;
96 struct _GtkTextLayoutPrivate
98 /* Cache the line that the cursor is positioned on, as the keyboard
99 direction only influences the direction of the cursor line.
101 GtkTextLine *cursor_line;
104 static GtkTextLineData *gtk_text_layout_real_wrap (GtkTextLayout *layout,
107 GtkTextLineData *line_data);
109 static void gtk_text_layout_invalidated (GtkTextLayout *layout);
111 static void gtk_text_layout_real_invalidate (GtkTextLayout *layout,
112 const GtkTextIter *start,
113 const GtkTextIter *end);
114 static void gtk_text_layout_real_invalidate_cursors(GtkTextLayout *layout,
115 const GtkTextIter *start,
116 const GtkTextIter *end);
117 static void gtk_text_layout_invalidate_cache (GtkTextLayout *layout,
119 gboolean cursors_only);
120 static void gtk_text_layout_invalidate_cursor_line (GtkTextLayout *layout,
121 gboolean cursors_only);
122 static void gtk_text_layout_real_free_line_data (GtkTextLayout *layout,
124 GtkTextLineData *line_data);
125 static void gtk_text_layout_emit_changed (GtkTextLayout *layout,
130 static void gtk_text_layout_invalidate_all (GtkTextLayout *layout);
132 static PangoAttribute *gtk_text_attr_appearance_new (const GtkTextAppearance *appearance);
134 static void gtk_text_layout_mark_set_handler (GtkTextBuffer *buffer,
135 const GtkTextIter *location,
138 static void gtk_text_layout_buffer_insert_text (GtkTextBuffer *textbuffer,
143 static void gtk_text_layout_buffer_delete_range (GtkTextBuffer *textbuffer,
148 static void gtk_text_layout_update_cursor_line (GtkTextLayout *layout);
162 #define PIXEL_BOUND(d) (((d) + PANGO_SCALE - 1) / PANGO_SCALE)
164 static void gtk_text_layout_finalize (GObject *object);
166 static guint signals[LAST_SIGNAL] = { 0 };
168 PangoAttrType gtk_text_attr_appearance_type = 0;
170 G_DEFINE_TYPE (GtkTextLayout, gtk_text_layout, G_TYPE_OBJECT)
173 gtk_text_layout_class_init (GtkTextLayoutClass *klass)
175 GObjectClass *object_class = G_OBJECT_CLASS (klass);
177 object_class->finalize = gtk_text_layout_finalize;
179 klass->wrap = gtk_text_layout_real_wrap;
180 klass->invalidate = gtk_text_layout_real_invalidate;
181 klass->invalidate_cursors = gtk_text_layout_real_invalidate_cursors;
182 klass->free_line_data = gtk_text_layout_real_free_line_data;
184 signals[INVALIDATED] =
185 g_signal_new (I_("invalidated"),
186 G_OBJECT_CLASS_TYPE (object_class),
188 G_STRUCT_OFFSET (GtkTextLayoutClass, invalidated),
190 _gtk_marshal_VOID__VOID,
195 g_signal_new (I_("changed"),
196 G_OBJECT_CLASS_TYPE (object_class),
198 G_STRUCT_OFFSET (GtkTextLayoutClass, changed),
200 _gtk_marshal_VOID__INT_INT_INT,
207 signals[ALLOCATE_CHILD] =
208 g_signal_new (I_("allocate_child"),
209 G_OBJECT_CLASS_TYPE (object_class),
211 G_STRUCT_OFFSET (GtkTextLayoutClass, allocate_child),
213 _gtk_marshal_VOID__OBJECT_INT_INT,
220 g_type_class_add_private (object_class, sizeof (GtkTextLayoutPrivate));
224 gtk_text_layout_init (GtkTextLayout *text_layout)
226 text_layout->cursor_visible = TRUE;
230 gtk_text_layout_new (void)
232 return g_object_new (GTK_TYPE_TEXT_LAYOUT, NULL);
236 free_style_cache (GtkTextLayout *text_layout)
238 if (text_layout->one_style_cache)
240 gtk_text_attributes_unref (text_layout->one_style_cache);
241 text_layout->one_style_cache = NULL;
246 gtk_text_layout_finalize (GObject *object)
248 GtkTextLayout *layout;
250 layout = GTK_TEXT_LAYOUT (object);
252 gtk_text_layout_set_buffer (layout, NULL);
254 if (layout->default_style)
255 gtk_text_attributes_unref (layout->default_style);
256 layout->default_style = NULL;
258 if (layout->ltr_context)
260 g_object_unref (layout->ltr_context);
261 layout->ltr_context = NULL;
263 if (layout->rtl_context)
265 g_object_unref (layout->rtl_context);
266 layout->rtl_context = NULL;
269 if (layout->one_display_cache)
271 GtkTextLineDisplay *tmp_display = layout->one_display_cache;
272 layout->one_display_cache = NULL;
273 gtk_text_layout_free_line_display (layout, tmp_display);
276 if (layout->preedit_string)
278 g_free (layout->preedit_string);
279 layout->preedit_string = NULL;
282 if (layout->preedit_attrs)
284 pango_attr_list_unref (layout->preedit_attrs);
285 layout->preedit_attrs = NULL;
289 (* G_OBJECT_CLASS (gtk_text_layout_parent_class)->finalize) (object);
293 gtk_text_layout_set_buffer (GtkTextLayout *layout,
294 GtkTextBuffer *buffer)
296 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
297 g_return_if_fail (buffer == NULL || GTK_IS_TEXT_BUFFER (buffer));
299 if (layout->buffer == buffer)
302 free_style_cache (layout);
306 _gtk_text_btree_remove_view (_gtk_text_buffer_get_btree (layout->buffer),
309 g_signal_handlers_disconnect_by_func (layout->buffer,
310 G_CALLBACK (gtk_text_layout_mark_set_handler),
312 g_signal_handlers_disconnect_by_func (layout->buffer,
313 G_CALLBACK (gtk_text_layout_buffer_insert_text),
315 g_signal_handlers_disconnect_by_func (layout->buffer,
316 G_CALLBACK (gtk_text_layout_buffer_delete_range),
319 g_object_unref (layout->buffer);
320 layout->buffer = NULL;
325 layout->buffer = buffer;
327 g_object_ref (buffer);
329 _gtk_text_btree_add_view (_gtk_text_buffer_get_btree (buffer), layout);
331 /* Bind to all signals that move the insert mark. */
332 g_signal_connect_after (layout->buffer, "mark_set",
333 G_CALLBACK (gtk_text_layout_mark_set_handler), layout);
334 g_signal_connect_after (layout->buffer, "insert_text",
335 G_CALLBACK (gtk_text_layout_buffer_insert_text), layout);
336 g_signal_connect_after (layout->buffer, "delete_range",
337 G_CALLBACK (gtk_text_layout_buffer_delete_range), layout);
339 gtk_text_layout_update_cursor_line (layout);
344 gtk_text_layout_default_style_changed (GtkTextLayout *layout)
346 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
348 DV (g_print ("invalidating all due to default style change (%s)\n", G_STRLOC));
349 gtk_text_layout_invalidate_all (layout);
353 gtk_text_layout_set_default_style (GtkTextLayout *layout,
354 GtkTextAttributes *values)
356 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
357 g_return_if_fail (values != NULL);
359 if (values == layout->default_style)
362 gtk_text_attributes_ref (values);
364 if (layout->default_style)
365 gtk_text_attributes_unref (layout->default_style);
367 layout->default_style = values;
369 gtk_text_layout_default_style_changed (layout);
373 gtk_text_layout_set_contexts (GtkTextLayout *layout,
374 PangoContext *ltr_context,
375 PangoContext *rtl_context)
377 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
379 if (layout->ltr_context != ltr_context)
381 if (layout->ltr_context)
382 g_object_unref (layout->ltr_context);
384 layout->ltr_context = ltr_context;
385 g_object_ref (layout->ltr_context);
388 if (layout->rtl_context != rtl_context)
390 if (layout->rtl_context)
391 g_object_unref (layout->rtl_context);
393 layout->rtl_context = rtl_context;
394 g_object_ref (layout->rtl_context);
397 DV (g_print ("invalidating all due to new pango contexts (%s)\n", G_STRLOC));
398 gtk_text_layout_invalidate_all (layout);
402 * gtk_text_layout_set_overwrite_mode:
403 * @layout: a #GtkTextLayout
404 * @overwrite: overwrite mode
406 * Sets overwrite mode
409 gtk_text_layout_set_overwrite_mode (GtkTextLayout *layout,
412 overwrite = overwrite != 0;
413 if (overwrite != layout->overwrite_mode)
415 layout->overwrite_mode = overwrite;
416 gtk_text_layout_invalidate_cursor_line (layout, TRUE);
421 * gtk_text_layout_set_cursor_direction:
422 * @direction: the new direction(s) for which to draw cursors.
423 * %GTK_TEXT_DIR_NONE means draw cursors for both
424 * left-to-right insertion and right-to-left insertion.
425 * (The two cursors will be visually distinguished.)
427 * Sets which text directions (left-to-right and/or right-to-left) for
428 * which cursors will be drawn for the insertion point. The visual
429 * point at which new text is inserted depends on whether the new
430 * text is right-to-left or left-to-right, so it may be desired to
431 * make the drawn position of the cursor depend on the keyboard state.
434 gtk_text_layout_set_cursor_direction (GtkTextLayout *layout,
435 GtkTextDirection direction)
437 if (direction != layout->cursor_direction)
439 layout->cursor_direction = direction;
440 gtk_text_layout_invalidate_cursor_line (layout, TRUE);
445 * gtk_text_layout_set_keyboard_direction:
446 * @keyboard_dir: the current direction of the keyboard.
448 * Sets the keyboard direction; this is used as for the bidirectional
449 * base direction for the line with the cursor if the line contains
450 * only neutral characters.
453 gtk_text_layout_set_keyboard_direction (GtkTextLayout *layout,
454 GtkTextDirection keyboard_dir)
456 if (keyboard_dir != layout->keyboard_direction)
458 layout->keyboard_direction = keyboard_dir;
459 gtk_text_layout_invalidate_cursor_line (layout, TRUE);
464 * gtk_text_layout_get_buffer:
465 * @layout: a #GtkTextLayout
467 * Gets the text buffer used by the layout. See
468 * gtk_text_layout_set_buffer().
470 * Return value: the text buffer used by the layout.
473 gtk_text_layout_get_buffer (GtkTextLayout *layout)
475 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), NULL);
477 return layout->buffer;
481 gtk_text_layout_set_screen_width (GtkTextLayout *layout, gint width)
483 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
484 g_return_if_fail (width >= 0);
485 g_return_if_fail (layout->wrap_loop_count == 0);
487 if (layout->screen_width == width)
490 layout->screen_width = width;
492 DV (g_print ("invalidating all due to new screen width (%s)\n", G_STRLOC));
493 gtk_text_layout_invalidate_all (layout);
497 * gtk_text_layout_set_cursor_visible:
498 * @layout: a #GtkTextLayout
499 * @cursor_visible: If %FALSE, then the insertion cursor will not
500 * be shown, even if the text is editable.
502 * Sets whether the insertion cursor should be shown. Generally,
503 * widgets using #GtkTextLayout will hide the cursor when the
504 * widget does not have the input focus.
507 gtk_text_layout_set_cursor_visible (GtkTextLayout *layout,
508 gboolean cursor_visible)
510 cursor_visible = (cursor_visible != FALSE);
512 if (layout->cursor_visible != cursor_visible)
517 layout->cursor_visible = cursor_visible;
519 /* Now queue a redraw on the paragraph containing the cursor
521 gtk_text_buffer_get_iter_at_mark (layout->buffer, &iter,
522 gtk_text_buffer_get_mark (layout->buffer, "insert"));
524 gtk_text_layout_get_line_yrange (layout, &iter, &y, &height);
525 gtk_text_layout_emit_changed (layout, y, height, height);
527 gtk_text_layout_invalidate_cache (layout, _gtk_text_iter_get_text_line (&iter), TRUE);
532 * gtk_text_layout_get_cursor_visible:
533 * @layout: a #GtkTextLayout
535 * Returns whether the insertion cursor will be shown.
537 * Return value: if %FALSE, the insertion cursor will not be
538 shown, even if the text is editable.
541 gtk_text_layout_get_cursor_visible (GtkTextLayout *layout)
543 return layout->cursor_visible;
547 * gtk_text_layout_set_preedit_string:
548 * @layout: a #PangoLayout
549 * @preedit_string: a string to display at the insertion point
550 * @preedit_attrs: a #PangoAttrList of attributes that apply to @preedit_string
551 * @cursor_pos: position of cursor within preedit string in chars
553 * Set the preedit string and attributes. The preedit string is a
554 * string showing text that is currently being edited and not
555 * yet committed into the buffer.
558 gtk_text_layout_set_preedit_string (GtkTextLayout *layout,
559 const gchar *preedit_string,
560 PangoAttrList *preedit_attrs,
563 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
564 g_return_if_fail (preedit_attrs != NULL || preedit_string == NULL);
566 g_free (layout->preedit_string);
568 if (layout->preedit_attrs)
569 pango_attr_list_unref (layout->preedit_attrs);
573 layout->preedit_string = g_strdup (preedit_string);
574 layout->preedit_len = strlen (layout->preedit_string);
575 pango_attr_list_ref (preedit_attrs);
576 layout->preedit_attrs = preedit_attrs;
578 cursor_pos = CLAMP (cursor_pos, 0, g_utf8_strlen (layout->preedit_string, -1));
579 layout->preedit_cursor = g_utf8_offset_to_pointer (layout->preedit_string, cursor_pos) - layout->preedit_string;
583 layout->preedit_string = NULL;
584 layout->preedit_len = 0;
585 layout->preedit_attrs = NULL;
586 layout->preedit_cursor = 0;
589 gtk_text_layout_invalidate_cursor_line (layout, FALSE);
593 gtk_text_layout_get_size (GtkTextLayout *layout,
597 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
600 *width = layout->width;
603 *height = layout->height;
607 gtk_text_layout_invalidated (GtkTextLayout *layout)
609 g_signal_emit (layout, signals[INVALIDATED], 0);
613 gtk_text_layout_emit_changed (GtkTextLayout *layout,
618 g_signal_emit (layout, signals[CHANGED], 0, y, old_height, new_height);
622 text_layout_changed (GtkTextLayout *layout,
626 gboolean cursors_only)
628 /* Check if the range intersects our cached line display,
629 * and invalidate the cached line if so.
631 if (layout->one_display_cache)
633 GtkTextLine *line = layout->one_display_cache->line;
634 gint cache_y = _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
636 gint cache_height = layout->one_display_cache->height;
638 if (cache_y + cache_height > y && cache_y < y + old_height)
639 gtk_text_layout_invalidate_cache (layout, line, cursors_only);
642 gtk_text_layout_emit_changed (layout, y, old_height, new_height);
646 gtk_text_layout_changed (GtkTextLayout *layout,
651 text_layout_changed (layout, y, old_height, new_height, FALSE);
655 gtk_text_layout_cursors_changed (GtkTextLayout *layout,
660 text_layout_changed (layout, y, old_height, new_height, TRUE);
664 gtk_text_layout_free_line_data (GtkTextLayout *layout,
666 GtkTextLineData *line_data)
668 (* GTK_TEXT_LAYOUT_GET_CLASS (layout)->free_line_data)
669 (layout, line, line_data);
673 gtk_text_layout_invalidate (GtkTextLayout *layout,
674 const GtkTextIter *start_index,
675 const GtkTextIter *end_index)
677 (* GTK_TEXT_LAYOUT_GET_CLASS (layout)->invalidate)
678 (layout, start_index, end_index);
682 gtk_text_layout_invalidate_cursors (GtkTextLayout *layout,
683 const GtkTextIter *start_index,
684 const GtkTextIter *end_index)
686 (* GTK_TEXT_LAYOUT_GET_CLASS (layout)->invalidate_cursors)
687 (layout, start_index, end_index);
691 gtk_text_layout_wrap (GtkTextLayout *layout,
694 GtkTextLineData *line_data)
696 return (* GTK_TEXT_LAYOUT_GET_CLASS (layout)->wrap) (layout, line, line_data);
700 gtk_text_layout_get_lines (GtkTextLayout *layout,
701 /* [top_y, bottom_y) */
706 GtkTextLine *first_btree_line;
707 GtkTextLine *last_btree_line;
711 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), NULL);
712 g_return_val_if_fail (bottom_y > top_y, NULL);
717 _gtk_text_btree_find_line_by_y (_gtk_text_buffer_get_btree (layout->buffer),
718 layout, top_y, first_line_y);
719 if (first_btree_line == NULL)
725 /* -1 since bottom_y is one past */
727 _gtk_text_btree_find_line_by_y (_gtk_text_buffer_get_btree (layout->buffer),
728 layout, bottom_y - 1, NULL);
730 if (!last_btree_line)
732 _gtk_text_btree_get_end_iter_line (_gtk_text_buffer_get_btree (layout->buffer));
734 g_assert (last_btree_line != NULL);
736 line = first_btree_line;
739 retval = g_slist_prepend (retval, line);
741 if (line == last_btree_line)
744 line = _gtk_text_line_next_excluding_last (line);
747 retval = g_slist_reverse (retval);
753 invalidate_cached_style (GtkTextLayout *layout)
755 free_style_cache (layout);
758 /* These should be called around a loop which wraps a CONTIGUOUS bunch
759 * of display lines. If the lines aren't contiguous you can't call
763 gtk_text_layout_wrap_loop_start (GtkTextLayout *layout)
765 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
766 g_return_if_fail (layout->one_style_cache == NULL);
768 layout->wrap_loop_count += 1;
772 gtk_text_layout_wrap_loop_end (GtkTextLayout *layout)
774 g_return_if_fail (layout->wrap_loop_count > 0);
776 layout->wrap_loop_count -= 1;
778 if (layout->wrap_loop_count == 0)
780 /* We cache a some stuff if we're iterating over some lines wrapping
781 * them. This cleans it up.
783 /* Nuke our cached style */
784 invalidate_cached_style (layout);
785 g_assert (layout->one_style_cache == NULL);
790 gtk_text_layout_invalidate_all (GtkTextLayout *layout)
795 if (layout->buffer == NULL)
798 gtk_text_buffer_get_bounds (layout->buffer, &start, &end);
800 gtk_text_layout_invalidate (layout, &start, &end);
804 gtk_text_layout_invalidate_cache (GtkTextLayout *layout,
806 gboolean cursors_only)
808 if (layout->one_display_cache && line == layout->one_display_cache->line)
810 GtkTextLineDisplay *display = layout->one_display_cache;
814 g_slist_foreach (display->cursors, (GFunc)g_free, NULL);
815 g_slist_free (display->cursors);
816 display->cursors = NULL;
817 display->cursors_invalid = TRUE;
818 display->has_block_cursor = FALSE;
822 layout->one_display_cache = NULL;
823 gtk_text_layout_free_line_display (layout, display);
828 /* Now invalidate the paragraph containing the cursor
831 gtk_text_layout_invalidate_cursor_line (GtkTextLayout *layout,
832 gboolean cursors_only)
834 GtkTextLayoutPrivate *priv = GTK_TEXT_LAYOUT_GET_PRIVATE (layout);
835 GtkTextLineData *line_data;
837 if (priv->cursor_line == NULL)
840 line_data = _gtk_text_line_get_data (priv->cursor_line, layout);
844 gtk_text_layout_invalidate_cache (layout, priv->cursor_line, TRUE);
847 gtk_text_layout_invalidate_cache (layout, priv->cursor_line, FALSE);
848 _gtk_text_line_invalidate_wrap (priv->cursor_line, line_data);
851 gtk_text_layout_invalidated (layout);
856 gtk_text_layout_update_cursor_line(GtkTextLayout *layout)
858 GtkTextLayoutPrivate *priv = GTK_TEXT_LAYOUT_GET_PRIVATE (layout);
861 gtk_text_buffer_get_iter_at_mark (layout->buffer, &iter,
862 gtk_text_buffer_get_mark (layout->buffer, "insert"));
864 priv->cursor_line = _gtk_text_iter_get_text_line (&iter);
868 gtk_text_layout_real_invalidate (GtkTextLayout *layout,
869 const GtkTextIter *start,
870 const GtkTextIter *end)
873 GtkTextLine *last_line;
875 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
876 g_return_if_fail (layout->wrap_loop_count == 0);
878 /* Because we may be invalidating a mark, it's entirely possible
879 * that gtk_text_iter_equal (start, end) in which case we
880 * should still invalidate the line they are both on. i.e.
881 * we always invalidate the line with "start" even
882 * if there's an empty range.
886 gtk_text_view_index_spew (start_index, "invalidate start");
887 gtk_text_view_index_spew (end_index, "invalidate end");
890 last_line = _gtk_text_iter_get_text_line (end);
891 line = _gtk_text_iter_get_text_line (start);
895 GtkTextLineData *line_data = _gtk_text_line_get_data (line, layout);
897 gtk_text_layout_invalidate_cache (layout, line, FALSE);
900 _gtk_text_line_invalidate_wrap (line, line_data);
902 if (line == last_line)
905 line = _gtk_text_line_next_excluding_last (line);
908 gtk_text_layout_invalidated (layout);
912 gtk_text_layout_real_invalidate_cursors (GtkTextLayout *layout,
913 const GtkTextIter *start,
914 const GtkTextIter *end)
916 /* Check if the range intersects our cached line display,
917 * and invalidate the cached line if so.
919 if (layout->one_display_cache)
921 GtkTextIter line_start, line_end;
922 GtkTextLine *line = layout->one_display_cache->line;
924 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
925 &line_start, line, 0);
926 line_end = line_start;
927 if (!gtk_text_iter_ends_line (&line_end))
928 gtk_text_iter_forward_to_line_end (&line_end);
930 if (gtk_text_iter_compare (start, end) > 0)
932 const GtkTextIter *tmp = start;
937 if (gtk_text_iter_compare (&line_start, end) <= 0 &&
938 gtk_text_iter_compare (start, &line_end) <= 0)
940 gtk_text_layout_invalidate_cache (layout, line, TRUE);
944 gtk_text_layout_invalidated (layout);
948 gtk_text_layout_real_free_line_data (GtkTextLayout *layout,
950 GtkTextLineData *line_data)
952 gtk_text_layout_invalidate_cache (layout, line, FALSE);
960 * gtk_text_layout_is_valid:
961 * @layout: a #GtkTextLayout
963 * Check if there are any invalid regions in a #GtkTextLayout's buffer
965 * Return value: %TRUE if any invalid regions were found
968 gtk_text_layout_is_valid (GtkTextLayout *layout)
970 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), FALSE);
972 return _gtk_text_btree_is_valid (_gtk_text_buffer_get_btree (layout->buffer),
977 update_layout_size (GtkTextLayout *layout)
979 _gtk_text_btree_get_view_size (_gtk_text_buffer_get_btree (layout->buffer),
981 &layout->width, &layout->height);
985 * gtk_text_layout_validate_yrange:
986 * @layout: a #GtkTextLayout
987 * @anchor: iter pointing into a line that will be used as the
989 * @y0_: offset from the top of the line pointed to by @anchor at
990 * which to begin validation. (The offset here is in pixels
992 * @y1_: offset from the top of the line pointed to by @anchor at
993 * which to end validation. (The offset here is in pixels
996 * Ensure that a region of a #GtkTextLayout is valid. The ::changed
997 * signal will be emitted if any lines are validated.
1000 gtk_text_layout_validate_yrange (GtkTextLayout *layout,
1001 GtkTextIter *anchor,
1006 GtkTextLine *first_line = NULL;
1007 GtkTextLine *last_line = NULL;
1009 gint delta_height = 0;
1010 gint first_line_y = 0; /* Quiet GCC */
1011 gint last_line_y = 0; /* Quiet GCC */
1013 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
1020 /* Validate backwards from the anchor line to y0
1022 line = _gtk_text_iter_get_text_line (anchor);
1023 line = _gtk_text_line_previous (line);
1025 while (line && seen < -y0)
1027 GtkTextLineData *line_data = _gtk_text_line_get_data (line, layout);
1028 if (!line_data || !line_data->valid)
1030 gint old_height, new_height;
1032 old_height = line_data ? line_data->height : 0;
1034 _gtk_text_btree_validate_line (_gtk_text_buffer_get_btree (layout->buffer),
1036 line_data = _gtk_text_line_get_data (line, layout);
1038 new_height = line_data ? line_data->height : 0;
1040 delta_height += new_height - old_height;
1043 first_line_y = -seen - new_height;
1047 last_line_y = -seen;
1051 seen += line_data ? line_data->height : 0;
1052 line = _gtk_text_line_previous (line);
1055 /* Validate forwards to y1 */
1056 line = _gtk_text_iter_get_text_line (anchor);
1058 while (line && seen < y1)
1060 GtkTextLineData *line_data = _gtk_text_line_get_data (line, layout);
1061 if (!line_data || !line_data->valid)
1063 gint old_height, new_height;
1065 old_height = line_data ? line_data->height : 0;
1067 _gtk_text_btree_validate_line (_gtk_text_buffer_get_btree (layout->buffer),
1069 line_data = _gtk_text_line_get_data (line, layout);
1070 new_height = line_data ? line_data->height : 0;
1072 delta_height += new_height - old_height;
1077 first_line_y = seen;
1080 last_line_y = seen + new_height;
1083 seen += line_data ? line_data->height : 0;
1084 line = _gtk_text_line_next_excluding_last (line);
1087 /* If we found and validated any invalid lines, update size and
1088 * emit the changed signal
1094 update_layout_size (layout);
1096 line_top = _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
1097 first_line, layout);
1099 gtk_text_layout_emit_changed (layout,
1101 last_line_y - first_line_y - delta_height,
1102 last_line_y - first_line_y);
1107 * gtk_text_layout_validate:
1108 * @tree: a #GtkTextLayout
1109 * @max_pixels: the maximum number of pixels to validate. (No more
1110 * than one paragraph beyond this limit will be validated)
1112 * Validate regions of a #GtkTextLayout. The ::changed signal will
1113 * be emitted for each region validated.
1116 gtk_text_layout_validate (GtkTextLayout *layout,
1119 gint y, old_height, new_height;
1121 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
1123 while (max_pixels > 0 &&
1124 _gtk_text_btree_validate (_gtk_text_buffer_get_btree (layout->buffer),
1126 &y, &old_height, &new_height))
1128 max_pixels -= new_height;
1130 update_layout_size (layout);
1131 gtk_text_layout_emit_changed (layout, y, old_height, new_height);
1135 static GtkTextLineData*
1136 gtk_text_layout_real_wrap (GtkTextLayout *layout,
1139 GtkTextLineData *line_data)
1141 GtkTextLineDisplay *display;
1143 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), NULL);
1144 g_return_val_if_fail (line != NULL, NULL);
1146 if (line_data == NULL)
1148 line_data = _gtk_text_line_data_new (layout, line);
1149 _gtk_text_line_add_data (line, line_data);
1152 display = gtk_text_layout_get_line_display (layout, line, TRUE);
1153 line_data->width = display->width;
1154 line_data->height = display->height;
1155 line_data->valid = TRUE;
1156 gtk_text_layout_free_line_display (layout, display);
1162 * Layout utility functions
1165 /* If you get the style with get_style () you need to call
1166 release_style () to free it. */
1167 static GtkTextAttributes*
1168 get_style (GtkTextLayout *layout,
1169 const GtkTextIter *iter)
1173 GtkTextAttributes *style;
1175 /* If we have the one-style cache, then it means
1176 that we haven't seen a toggle since we filled in the
1179 if (layout->one_style_cache != NULL)
1181 gtk_text_attributes_ref (layout->one_style_cache);
1182 return layout->one_style_cache;
1185 g_assert (layout->one_style_cache == NULL);
1187 /* Get the tags at this spot */
1188 tags = _gtk_text_btree_get_tags (iter, &tag_count);
1190 /* No tags, use default style */
1191 if (tags == NULL || tag_count == 0)
1193 /* One ref for the return value, one ref for the
1194 layout->one_style_cache reference */
1195 gtk_text_attributes_ref (layout->default_style);
1196 gtk_text_attributes_ref (layout->default_style);
1197 layout->one_style_cache = layout->default_style;
1201 return layout->default_style;
1204 /* Sort tags in ascending order of priority */
1205 _gtk_text_tag_array_sort (tags, tag_count);
1207 style = gtk_text_attributes_new ();
1209 gtk_text_attributes_copy_values (layout->default_style,
1212 _gtk_text_attributes_fill_from_tags (style,
1218 g_assert (style->refcount == 1);
1220 /* Leave this style as the last one seen */
1221 g_assert (layout->one_style_cache == NULL);
1222 gtk_text_attributes_ref (style); /* ref held by layout->one_style_cache */
1223 layout->one_style_cache = style;
1225 /* Returning yet another refcount */
1230 release_style (GtkTextLayout *layout,
1231 GtkTextAttributes *style)
1233 g_return_if_fail (style != NULL);
1234 g_return_if_fail (style->refcount > 0);
1236 gtk_text_attributes_unref (style);
1243 /* This function tries to optimize the case where a line
1244 is completely invisible */
1246 totally_invisible_line (GtkTextLayout *layout,
1250 GtkTextLineSegment *seg;
1253 /* Check if the first char is visible, if so we are partially visible.
1254 * Note that we have to check this since we don't know the current
1255 * invisible/noninvisible toggle state; this function can use the whole btree
1258 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
1261 if (!_gtk_text_btree_char_is_invisible (iter))
1265 seg = line->segments;
1269 if (seg->byte_count > 0)
1270 bytes += seg->byte_count;
1272 /* Note that these two tests can cause us to bail out
1273 * when we shouldn't, because a higher-priority tag
1274 * may override these settings. However the important
1275 * thing is to only invisible really-invisible lines, rather
1276 * than to invisible all really-invisible lines.
1279 else if (seg->type == >k_text_toggle_on_type)
1281 invalidate_cached_style (layout);
1283 /* Bail out if an elision-unsetting tag begins */
1284 if (seg->body.toggle.info->tag->invisible_set &&
1285 !seg->body.toggle.info->tag->values->invisible)
1288 else if (seg->type == >k_text_toggle_off_type)
1290 invalidate_cached_style (layout);
1292 /* Bail out if an elision-setting tag ends */
1293 if (seg->body.toggle.info->tag->invisible_set &&
1294 seg->body.toggle.info->tag->values->invisible)
1301 if (seg != NULL) /* didn't reach line end */
1308 set_para_values (GtkTextLayout *layout,
1309 PangoDirection base_dir,
1310 GtkTextAttributes *style,
1311 GtkTextLineDisplay *display)
1313 PangoAlignment pango_align = PANGO_ALIGN_LEFT;
1314 PangoWrapMode pango_wrap = PANGO_WRAP_WORD;
1318 /* If no base direction was found, then use the style direction */
1319 case PANGO_DIRECTION_NEUTRAL :
1320 display->direction = style->direction;
1322 /* Override the base direction */
1323 if (display->direction == GTK_TEXT_DIR_RTL)
1324 base_dir = PANGO_DIRECTION_RTL;
1326 base_dir = PANGO_DIRECTION_LTR;
1329 case PANGO_DIRECTION_RTL :
1330 display->direction = GTK_TEXT_DIR_RTL;
1333 display->direction = GTK_TEXT_DIR_LTR;
1337 if (display->direction == GTK_TEXT_DIR_RTL)
1338 display->layout = pango_layout_new (layout->rtl_context);
1340 display->layout = pango_layout_new (layout->ltr_context);
1342 switch (style->justification)
1344 case GTK_JUSTIFY_LEFT:
1345 pango_align = (base_dir == PANGO_DIRECTION_LTR) ? PANGO_ALIGN_LEFT : PANGO_ALIGN_RIGHT;
1347 case GTK_JUSTIFY_RIGHT:
1348 pango_align = (base_dir == PANGO_DIRECTION_LTR) ? PANGO_ALIGN_RIGHT : PANGO_ALIGN_LEFT;
1350 case GTK_JUSTIFY_CENTER:
1351 pango_align = PANGO_ALIGN_CENTER;
1353 case GTK_JUSTIFY_FILL:
1354 pango_align = (base_dir == PANGO_DIRECTION_LTR) ? PANGO_ALIGN_LEFT : PANGO_ALIGN_RIGHT;
1355 pango_layout_set_justify (display->layout, TRUE);
1358 g_assert_not_reached ();
1362 pango_layout_set_alignment (display->layout, pango_align);
1363 pango_layout_set_spacing (display->layout,
1364 style->pixels_inside_wrap * PANGO_SCALE);
1367 pango_layout_set_tabs (display->layout, style->tabs);
1369 display->top_margin = style->pixels_above_lines;
1370 display->height = style->pixels_above_lines + style->pixels_below_lines;
1371 display->bottom_margin = style->pixels_below_lines;
1372 display->left_margin = style->left_margin;
1373 display->right_margin = style->right_margin;
1375 display->x_offset = display->left_margin;
1377 pango_layout_set_indent (display->layout,
1378 style->indent * PANGO_SCALE);
1380 switch (style->wrap_mode)
1383 pango_wrap = PANGO_WRAP_CHAR;
1386 pango_wrap = PANGO_WRAP_WORD;
1389 case GTK_WRAP_WORD_CHAR:
1390 pango_wrap = PANGO_WRAP_WORD_CHAR;
1397 if (style->wrap_mode != GTK_WRAP_NONE)
1399 int layout_width = (layout->screen_width - display->left_margin - display->right_margin);
1400 pango_layout_set_width (display->layout, layout_width * PANGO_SCALE);
1401 pango_layout_set_wrap (display->layout, pango_wrap);
1404 display->total_width = MAX (layout->screen_width, layout->width) - display->left_margin - display->right_margin;
1406 if (style->pg_bg_color)
1407 display->pg_bg_color = gdk_color_copy (style->pg_bg_color);
1409 display->pg_bg_color = NULL;
1412 static PangoAttribute *
1413 gtk_text_attr_appearance_copy (const PangoAttribute *attr)
1415 const GtkTextAttrAppearance *appearance_attr = (const GtkTextAttrAppearance *)attr;
1417 return gtk_text_attr_appearance_new (&appearance_attr->appearance);
1421 gtk_text_attr_appearance_destroy (PangoAttribute *attr)
1423 GtkTextAttrAppearance *appearance_attr = (GtkTextAttrAppearance *)attr;
1425 if (appearance_attr->appearance.bg_stipple)
1426 g_object_unref (appearance_attr->appearance.bg_stipple);
1427 if (appearance_attr->appearance.fg_stipple)
1428 g_object_unref (appearance_attr->appearance.fg_stipple);
1430 g_slice_free (GtkTextAttrAppearance, appearance_attr);
1434 gtk_text_attr_appearance_compare (const PangoAttribute *attr1,
1435 const PangoAttribute *attr2)
1437 const GtkTextAppearance *appearance1 = &((const GtkTextAttrAppearance *)attr1)->appearance;
1438 const GtkTextAppearance *appearance2 = &((const GtkTextAttrAppearance *)attr2)->appearance;
1440 return (gdk_color_equal (&appearance1->fg_color, &appearance2->fg_color) &&
1441 gdk_color_equal (&appearance1->bg_color, &appearance2->bg_color) &&
1442 appearance1->fg_stipple == appearance2->fg_stipple &&
1443 appearance1->bg_stipple == appearance2->bg_stipple &&
1444 appearance1->underline == appearance2->underline &&
1445 appearance1->strikethrough == appearance2->strikethrough &&
1446 appearance1->draw_bg == appearance2->draw_bg);
1451 * gtk_text_attr_appearance_new:
1454 * Create a new font description attribute. (This attribute
1455 * allows setting family, style, weight, variant, stretch,
1456 * and size simultaneously.)
1460 static PangoAttribute *
1461 gtk_text_attr_appearance_new (const GtkTextAppearance *appearance)
1463 static PangoAttrClass klass = {
1465 gtk_text_attr_appearance_copy,
1466 gtk_text_attr_appearance_destroy,
1467 gtk_text_attr_appearance_compare
1470 GtkTextAttrAppearance *result;
1473 klass.type = gtk_text_attr_appearance_type =
1474 pango_attr_type_register ("GtkTextAttrAppearance");
1476 result = g_slice_new (GtkTextAttrAppearance);
1477 result->attr.klass = &klass;
1479 result->appearance = *appearance;
1481 if (appearance->bg_stipple)
1482 g_object_ref (appearance->bg_stipple);
1483 if (appearance->fg_stipple)
1484 g_object_ref (appearance->fg_stipple);
1486 return (PangoAttribute *)result;
1491 add_generic_attrs (GtkTextLayout *layout,
1492 GtkTextAppearance *appearance,
1494 PangoAttrList *attrs,
1499 PangoAttribute *attr;
1501 if (appearance->underline != PANGO_UNDERLINE_NONE)
1503 attr = pango_attr_underline_new (appearance->underline);
1505 attr->start_index = start;
1506 attr->end_index = start + byte_count;
1508 pango_attr_list_insert (attrs, attr);
1511 if (appearance->strikethrough)
1513 attr = pango_attr_strikethrough_new (appearance->strikethrough);
1515 attr->start_index = start;
1516 attr->end_index = start + byte_count;
1518 pango_attr_list_insert (attrs, attr);
1521 if (appearance->rise != 0)
1523 attr = pango_attr_rise_new (appearance->rise);
1525 attr->start_index = start;
1526 attr->end_index = start + byte_count;
1528 pango_attr_list_insert (attrs, attr);
1533 attr = gtk_text_attr_appearance_new (appearance);
1535 attr->start_index = start;
1536 attr->end_index = start + byte_count;
1538 ((GtkTextAttrAppearance *)attr)->appearance.is_text = is_text;
1540 pango_attr_list_insert (attrs, attr);
1545 add_text_attrs (GtkTextLayout *layout,
1546 GtkTextAttributes *style,
1548 PangoAttrList *attrs,
1552 PangoAttribute *attr;
1554 attr = pango_attr_font_desc_new (style->font);
1555 attr->start_index = start;
1556 attr->end_index = start + byte_count;
1558 pango_attr_list_insert (attrs, attr);
1560 if (style->font_scale != 1.0)
1562 attr = pango_attr_scale_new (style->font_scale);
1564 attr->start_index = start;
1565 attr->end_index = start + byte_count;
1567 pango_attr_list_insert (attrs, attr);
1572 add_pixbuf_attrs (GtkTextLayout *layout,
1573 GtkTextLineDisplay *display,
1574 GtkTextAttributes *style,
1575 GtkTextLineSegment *seg,
1576 PangoAttrList *attrs,
1579 PangoAttribute *attr;
1580 PangoRectangle logical_rect;
1581 GtkTextPixbuf *pixbuf = &seg->body.pixbuf;
1584 width = gdk_pixbuf_get_width (pixbuf->pixbuf);
1585 height = gdk_pixbuf_get_height (pixbuf->pixbuf);
1588 logical_rect.y = -height * PANGO_SCALE;
1589 logical_rect.width = width * PANGO_SCALE;
1590 logical_rect.height = height * PANGO_SCALE;
1592 attr = pango_attr_shape_new_with_data (&logical_rect, &logical_rect,
1593 pixbuf->pixbuf, NULL, NULL);
1594 attr->start_index = start;
1595 attr->end_index = start + seg->byte_count;
1596 pango_attr_list_insert (attrs, attr);
1598 display->shaped_objects =
1599 g_slist_append (display->shaped_objects, pixbuf->pixbuf);
1603 add_child_attrs (GtkTextLayout *layout,
1604 GtkTextLineDisplay *display,
1605 GtkTextAttributes *style,
1606 GtkTextLineSegment *seg,
1607 PangoAttrList *attrs,
1610 PangoAttribute *attr;
1611 PangoRectangle logical_rect;
1619 tmp_list = seg->body.child.widgets;
1620 while (tmp_list != NULL)
1622 GtkWidget *child = tmp_list->data;
1624 if (_gtk_anchored_child_get_layout (child) == layout)
1629 gtk_widget_get_child_requisition (child, &req);
1632 height = req.height;
1639 tmp_list = g_slist_next (tmp_list);
1642 if (tmp_list == NULL)
1644 /* If tmp_list == NULL then there is no widget at this anchor in
1645 * this display; not an error. We make up an arbitrary size
1646 * to use, just so the programmer can see the blank spot.
1647 * We also put a NULL in the shaped objects list, to keep
1648 * the correspondence between the list and the shaped chars in
1649 * the layout. A bad hack, yes.
1658 display->shaped_objects = g_slist_append (display->shaped_objects, widget);
1661 logical_rect.y = -height * PANGO_SCALE;
1662 logical_rect.width = width * PANGO_SCALE;
1663 logical_rect.height = height * PANGO_SCALE;
1665 attr = pango_attr_shape_new_with_data (&logical_rect, &logical_rect,
1666 widget, NULL, NULL);
1667 attr->start_index = start;
1668 attr->end_index = start + seg->byte_count;
1669 pango_attr_list_insert (attrs, attr);
1674 * @layout: a #GtkTextLayout
1675 * @display: a #GtkTextLineDisplay
1676 * @insert_iter: iter pointing to the cursor location
1677 * @insert_index: cursor offset in the @display's layout, it may
1678 * be different from @insert_iter's offset in case when preedit
1679 * string is present.
1680 * @pos: location to store cursor position
1681 * @cursor_at_line_end: whether cursor is at the end of line
1683 * Checks whether layout should display block cursor at given position.
1684 * For this layout must be in overwrite mode and text at @insert_iter
1688 get_block_cursor (GtkTextLayout *layout,
1689 GtkTextLineDisplay *display,
1690 const GtkTextIter *insert_iter,
1693 gboolean *cursor_at_line_end)
1695 PangoRectangle pango_pos;
1697 if (layout->overwrite_mode &&
1698 gtk_text_iter_editable (insert_iter, TRUE) &&
1699 _gtk_text_util_get_block_cursor_location (display->layout,
1702 cursor_at_line_end))
1706 pos->x = PANGO_PIXELS (pango_pos.x);
1707 pos->y = PANGO_PIXELS (pango_pos.y);
1708 pos->width = PANGO_PIXELS (pango_pos.width);
1709 pos->height = PANGO_PIXELS (pango_pos.height);
1719 add_cursor (GtkTextLayout *layout,
1720 GtkTextLineDisplay *display,
1721 GtkTextLineSegment *seg,
1724 PangoRectangle strong_pos, weak_pos;
1725 GtkTextCursorDisplay *cursor = NULL; /* Quiet GCC */
1726 gboolean add_weak = FALSE;
1727 gboolean add_strong = FALSE;
1729 /* Hide insertion cursor when we have a selection or the layout
1730 * user has hidden the cursor.
1732 if (_gtk_text_btree_mark_is_insert (_gtk_text_buffer_get_btree (layout->buffer),
1733 seg->body.mark.obj) &&
1734 (!layout->cursor_visible ||
1735 gtk_text_buffer_get_selection_bounds (layout->buffer, NULL, NULL)))
1738 if (layout->overwrite_mode &&
1739 _gtk_text_btree_mark_is_insert (_gtk_text_buffer_get_btree (layout->buffer),
1740 seg->body.mark.obj))
1743 gboolean cursor_at_line_end;
1745 _gtk_text_btree_get_iter_at_mark (_gtk_text_buffer_get_btree (layout->buffer),
1746 &iter, seg->body.mark.obj);
1748 if (get_block_cursor (layout, display, &iter, start,
1749 &display->block_cursor,
1750 &cursor_at_line_end))
1752 display->has_block_cursor = TRUE;
1753 display->cursor_at_line_end = cursor_at_line_end;
1758 pango_layout_get_cursor_pos (display->layout, start, &strong_pos, &weak_pos);
1760 if (layout->cursor_direction == GTK_TEXT_DIR_NONE)
1765 else if (display->direction == layout->cursor_direction)
1772 cursor = g_new (GtkTextCursorDisplay, 1);
1774 cursor->x = PANGO_PIXELS (strong_pos.x);
1775 cursor->y = PANGO_PIXELS (strong_pos.y);
1776 cursor->height = PANGO_PIXELS (strong_pos.height);
1777 cursor->is_strong = TRUE;
1778 cursor->is_weak = (layout->cursor_direction == GTK_TEXT_DIR_NONE) ? FALSE : TRUE;
1779 display->cursors = g_slist_prepend (display->cursors, cursor);
1784 if (weak_pos.x == strong_pos.x && add_strong)
1785 cursor->is_weak = TRUE;
1788 cursor = g_new (GtkTextCursorDisplay, 1);
1790 cursor->x = PANGO_PIXELS (weak_pos.x);
1791 cursor->y = PANGO_PIXELS (weak_pos.y);
1792 cursor->height = PANGO_PIXELS (weak_pos.height);
1793 cursor->is_strong = (layout->cursor_direction == GTK_TEXT_DIR_NONE) ? FALSE : TRUE;
1794 cursor->is_weak = TRUE;
1795 display->cursors = g_slist_prepend (display->cursors, cursor);
1801 is_shape (PangoLayoutRun *run)
1803 GSList *tmp_list = run->item->analysis.extra_attrs;
1807 PangoAttribute *attr = tmp_list->data;
1809 if (attr->klass->type == PANGO_ATTR_SHAPE)
1812 tmp_list = tmp_list->next;
1819 allocate_child_widgets (GtkTextLayout *text_layout,
1820 GtkTextLineDisplay *display)
1822 GSList *shaped = display->shaped_objects;
1823 PangoLayout *layout = display->layout;
1824 PangoLayoutIter *iter;
1826 iter = pango_layout_get_iter (layout);
1830 PangoLayoutRun *run = pango_layout_iter_get_run_readonly (iter);
1832 if (run && is_shape (run))
1834 GObject *shaped_object = shaped->data;
1835 shaped = shaped->next;
1837 /* shaped_object is NULL for child anchors with no
1838 * widgets stored at them
1840 if (shaped_object && GTK_IS_WIDGET (shaped_object))
1842 PangoRectangle extents;
1844 /* We emit "allocate_child" with the x,y of
1845 * the widget with respect to the top of the line
1846 * and the left side of the buffer
1849 pango_layout_iter_get_run_extents (iter,
1853 g_signal_emit (text_layout,
1854 signals[ALLOCATE_CHILD],
1857 PANGO_PIXELS (extents.x) + display->x_offset,
1858 PANGO_PIXELS (extents.y) + display->top_margin);
1862 while (pango_layout_iter_next_run (iter));
1864 pango_layout_iter_free (iter);
1868 convert_color (GdkColor *result,
1869 PangoAttrColor *attr)
1871 result->red = attr->color.red;
1872 result->blue = attr->color.blue;
1873 result->green = attr->color.green;
1876 /* This function is used to convert the preedit string attributes, which are
1877 * standard PangoAttributes, into the custom attributes used by the text
1878 * widget and insert them into a attr list with a given offset.
1881 add_preedit_attrs (GtkTextLayout *layout,
1882 GtkTextAttributes *style,
1883 PangoAttrList *attrs,
1887 PangoAttrIterator *iter = pango_attr_list_get_iterator (layout->preedit_attrs);
1891 GtkTextAppearance appearance = style->appearance;
1892 PangoFontDescription *font_desc = pango_font_description_copy_static (style->font);
1893 PangoAttribute *insert_attr;
1894 GSList *extra_attrs = NULL;
1896 PangoLanguage *language;
1899 pango_attr_iterator_range (iter, &start, &end);
1901 if (end == G_MAXINT)
1902 end = layout->preedit_len;
1907 pango_attr_iterator_get_font (iter, font_desc, &language, &extra_attrs);
1909 tmp_list = extra_attrs;
1912 PangoAttribute *attr = tmp_list->data;
1914 switch (attr->klass->type)
1916 case PANGO_ATTR_FOREGROUND:
1917 convert_color (&appearance.fg_color, (PangoAttrColor *)attr);
1919 case PANGO_ATTR_BACKGROUND:
1920 convert_color (&appearance.bg_color, (PangoAttrColor *)attr);
1921 appearance.draw_bg = TRUE;
1923 case PANGO_ATTR_UNDERLINE:
1924 appearance.underline = ((PangoAttrInt *)attr)->value;
1926 case PANGO_ATTR_STRIKETHROUGH:
1927 appearance.strikethrough = ((PangoAttrInt *)attr)->value;
1929 case PANGO_ATTR_RISE:
1930 appearance.rise = ((PangoAttrInt *)attr)->value;
1936 pango_attribute_destroy (attr);
1937 tmp_list = tmp_list->next;
1940 g_slist_free (extra_attrs);
1942 insert_attr = pango_attr_font_desc_new (font_desc);
1943 insert_attr->start_index = start + offset;
1944 insert_attr->end_index = end + offset;
1946 pango_attr_list_insert (attrs, insert_attr);
1950 insert_attr = pango_attr_language_new (language);
1951 insert_attr->start_index = start + offset;
1952 insert_attr->end_index = end + offset;
1954 pango_attr_list_insert (attrs, insert_attr);
1957 add_generic_attrs (layout, &appearance, end - start,
1958 attrs, start + offset,
1961 pango_font_description_free (font_desc);
1963 while (pango_attr_iterator_next (iter));
1965 pango_attr_iterator_destroy (iter);
1968 /* Iterate over the line and fill in display->cursors.
1969 * It's a stripped copy of gtk_text_layout_get_line_display() */
1971 update_text_display_cursors (GtkTextLayout *layout,
1973 GtkTextLineDisplay *display)
1975 GtkTextLineSegment *seg;
1977 gint layout_byte_offset, buffer_byte_offset;
1978 GSList *cursor_byte_offsets = NULL;
1979 GSList *cursor_segs = NULL;
1980 GSList *tmp_list1, *tmp_list2;
1982 if (!display->cursors_invalid)
1985 display->cursors_invalid = FALSE;
1987 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
1990 /* Special-case optimization for completely
1991 * invisible lines; makes it faster to deal
1992 * with sequences of invisible lines.
1994 if (totally_invisible_line (layout, line, &iter))
1997 /* Iterate over segments */
1998 layout_byte_offset = 0; /* position in the layout text (includes preedit, does not include invisible text) */
1999 buffer_byte_offset = 0; /* position in the buffer line */
2000 seg = _gtk_text_iter_get_any_segment (&iter);
2003 /* Displayable segments */
2004 if (seg->type == >k_text_char_type ||
2005 seg->type == >k_text_pixbuf_type ||
2006 seg->type == >k_text_child_type)
2008 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
2010 buffer_byte_offset);
2012 if (!_gtk_text_btree_char_is_invisible (&iter))
2013 layout_byte_offset += seg->byte_count;
2015 buffer_byte_offset += seg->byte_count;
2019 else if (seg->type == >k_text_right_mark_type ||
2020 seg->type == >k_text_left_mark_type)
2022 gint cursor_offset = 0;
2024 /* At the insertion point, add the preedit string, if any */
2026 if (_gtk_text_btree_mark_is_insert (_gtk_text_buffer_get_btree (layout->buffer),
2027 seg->body.mark.obj))
2029 display->insert_index = layout_byte_offset;
2031 if (layout->preedit_len > 0)
2033 layout_byte_offset += layout->preedit_len;
2034 /* DO NOT increment the buffer byte offset for preedit */
2035 cursor_offset = layout->preedit_cursor - layout->preedit_len;
2039 /* Display visible marks */
2041 if (seg->body.mark.visible)
2043 cursor_byte_offsets = g_slist_prepend (cursor_byte_offsets,
2044 GINT_TO_POINTER (layout_byte_offset + cursor_offset));
2045 cursor_segs = g_slist_prepend (cursor_segs, seg);
2050 else if (seg->type == >k_text_toggle_on_type ||
2051 seg->type == >k_text_toggle_off_type)
2056 g_error ("Unknown segment type: %s", seg->type->name);
2061 tmp_list1 = cursor_byte_offsets;
2062 tmp_list2 = cursor_segs;
2065 add_cursor (layout, display, tmp_list2->data,
2066 GPOINTER_TO_INT (tmp_list1->data));
2067 tmp_list1 = tmp_list1->next;
2068 tmp_list2 = tmp_list2->next;
2070 g_slist_free (cursor_byte_offsets);
2071 g_slist_free (cursor_segs);
2074 GtkTextLineDisplay *
2075 gtk_text_layout_get_line_display (GtkTextLayout *layout,
2079 GtkTextLayoutPrivate *priv = GTK_TEXT_LAYOUT_GET_PRIVATE (layout);
2080 GtkTextLineDisplay *display;
2081 GtkTextLineSegment *seg;
2083 GtkTextAttributes *style;
2085 PangoAttrList *attrs;
2086 gint text_allocated, layout_byte_offset, buffer_byte_offset;
2087 PangoRectangle extents;
2088 gboolean para_values_set = FALSE;
2089 GSList *cursor_byte_offsets = NULL;
2090 GSList *cursor_segs = NULL;
2091 GSList *tmp_list1, *tmp_list2;
2092 gboolean saw_widget = FALSE;
2093 PangoDirection base_dir;
2095 g_return_val_if_fail (line != NULL, NULL);
2097 if (layout->one_display_cache)
2099 if (line == layout->one_display_cache->line &&
2100 (size_only || !layout->one_display_cache->size_only))
2103 update_text_display_cursors (layout, line, layout->one_display_cache);
2104 return layout->one_display_cache;
2108 GtkTextLineDisplay *tmp_display = layout->one_display_cache;
2109 layout->one_display_cache = NULL;
2110 gtk_text_layout_free_line_display (layout, tmp_display);
2114 DV (g_print ("creating one line display cache (%s)\n", G_STRLOC));
2116 display = g_new0 (GtkTextLineDisplay, 1);
2118 display->size_only = size_only;
2119 display->line = line;
2120 display->insert_index = -1;
2122 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
2125 /* Special-case optimization for completely
2126 * invisible lines; makes it faster to deal
2127 * with sequences of invisible lines.
2129 if (totally_invisible_line (layout, line, &iter))
2131 if (display->direction == GTK_TEXT_DIR_RTL)
2132 display->layout = pango_layout_new (layout->rtl_context);
2134 display->layout = pango_layout_new (layout->ltr_context);
2139 /* Find the bidi base direction */
2140 base_dir = line->dir_propagated_forward;
2141 if (base_dir == PANGO_DIRECTION_NEUTRAL)
2142 base_dir = line->dir_propagated_back;
2144 if (line == priv->cursor_line &&
2145 line->dir_strong == PANGO_DIRECTION_NEUTRAL)
2147 base_dir = (layout->keyboard_direction == GTK_TEXT_DIR_LTR) ?
2148 PANGO_DIRECTION_LTR : PANGO_DIRECTION_RTL;
2151 /* Allocate space for flat text for buffer
2153 text_allocated = _gtk_text_line_byte_count (line);
2154 text = g_malloc (text_allocated);
2156 attrs = pango_attr_list_new ();
2158 /* Iterate over segments, creating display chunks for them. */
2159 layout_byte_offset = 0; /* current length of layout text (includes preedit, does not include invisible text) */
2160 buffer_byte_offset = 0; /* position in the buffer line */
2161 seg = _gtk_text_iter_get_any_segment (&iter);
2164 /* Displayable segments */
2165 if (seg->type == >k_text_char_type ||
2166 seg->type == >k_text_pixbuf_type ||
2167 seg->type == >k_text_child_type)
2169 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
2171 buffer_byte_offset);
2172 style = get_style (layout, &iter);
2174 /* We have to delay setting the paragraph values until we
2175 * hit the first pixbuf or text segment because toggles at
2176 * the beginning of the paragraph should affect the
2177 * paragraph-global values
2179 if (!para_values_set)
2181 set_para_values (layout, base_dir, style, display);
2182 para_values_set = TRUE;
2185 /* First see if the chunk is invisible, and ignore it if so. Tk
2186 * looked at tabs, wrap mode, etc. before doing this, but
2187 * that made no sense to me, so I am just skipping the
2190 if (!style->invisible)
2192 if (seg->type == >k_text_char_type)
2194 /* We don't want to split segments because of marks,
2195 * so we scan forward for more segments only
2196 * separated from us by marks. In theory, we should
2197 * also merge segments with identical styles, even
2198 * if there are toggles in-between
2202 GtkTextLineSegment *prev_seg = NULL;
2206 if (seg->type == >k_text_char_type)
2208 memcpy (text + layout_byte_offset, seg->body.chars, seg->byte_count);
2209 layout_byte_offset += seg->byte_count;
2210 buffer_byte_offset += seg->byte_count;
2211 bytes += seg->byte_count;
2213 else if (seg->type == >k_text_right_mark_type ||
2214 seg->type == >k_text_left_mark_type)
2216 /* If we have preedit string, break out of this loop - we'll almost
2217 * certainly have different attributes on the preedit string
2220 if (layout->preedit_len > 0 &&
2221 _gtk_text_btree_mark_is_insert (_gtk_text_buffer_get_btree (layout->buffer),
2222 seg->body.mark.obj))
2225 if (seg->body.mark.visible)
2227 cursor_byte_offsets = g_slist_prepend (cursor_byte_offsets, GINT_TO_POINTER (layout_byte_offset));
2228 cursor_segs = g_slist_prepend (cursor_segs, seg);
2229 if (_gtk_text_btree_mark_is_insert (_gtk_text_buffer_get_btree (layout->buffer),
2230 seg->body.mark.obj))
2231 display->insert_index = layout_byte_offset;
2241 seg = prev_seg; /* Back up one */
2242 add_generic_attrs (layout, &style->appearance,
2244 attrs, layout_byte_offset - bytes,
2246 add_text_attrs (layout, style, bytes, attrs,
2247 layout_byte_offset - bytes, size_only);
2249 else if (seg->type == >k_text_pixbuf_type)
2251 add_generic_attrs (layout,
2254 attrs, layout_byte_offset,
2256 add_pixbuf_attrs (layout, display, style,
2257 seg, attrs, layout_byte_offset);
2258 memcpy (text + layout_byte_offset, gtk_text_unknown_char_utf8,
2260 layout_byte_offset += seg->byte_count;
2261 buffer_byte_offset += seg->byte_count;
2263 else if (seg->type == >k_text_child_type)
2267 add_generic_attrs (layout, &style->appearance,
2269 attrs, layout_byte_offset,
2271 add_child_attrs (layout, display, style,
2272 seg, attrs, layout_byte_offset);
2273 memcpy (text + layout_byte_offset, gtk_text_unknown_char_utf8,
2275 layout_byte_offset += seg->byte_count;
2276 buffer_byte_offset += seg->byte_count;
2280 /* We don't know this segment type */
2281 g_assert_not_reached ();
2284 } /* if (segment was visible) */
2287 /* Invisible segment */
2288 buffer_byte_offset += seg->byte_count;
2291 release_style (layout, style);
2295 else if (seg->type == >k_text_toggle_on_type ||
2296 seg->type == >k_text_toggle_off_type)
2298 /* Style may have changed, drop our
2299 current cached style */
2300 invalidate_cached_style (layout);
2304 else if (seg->type == >k_text_right_mark_type ||
2305 seg->type == >k_text_left_mark_type)
2307 gint cursor_offset = 0;
2309 /* At the insertion point, add the preedit string, if any */
2311 if (_gtk_text_btree_mark_is_insert (_gtk_text_buffer_get_btree (layout->buffer),
2312 seg->body.mark.obj))
2314 display->insert_index = layout_byte_offset;
2316 if (layout->preedit_len > 0)
2318 text_allocated += layout->preedit_len;
2319 text = g_realloc (text, text_allocated);
2321 style = get_style (layout, &iter);
2322 add_preedit_attrs (layout, style, attrs, layout_byte_offset, size_only);
2323 release_style (layout, style);
2325 memcpy (text + layout_byte_offset, layout->preedit_string, layout->preedit_len);
2326 layout_byte_offset += layout->preedit_len;
2327 /* DO NOT increment the buffer byte offset for preedit */
2329 cursor_offset = layout->preedit_cursor - layout->preedit_len;
2334 /* Display visible marks */
2336 if (seg->body.mark.visible)
2338 cursor_byte_offsets = g_slist_prepend (cursor_byte_offsets,
2339 GINT_TO_POINTER (layout_byte_offset + cursor_offset));
2340 cursor_segs = g_slist_prepend (cursor_segs, seg);
2345 g_error ("Unknown segment type: %s", seg->type->name);
2350 if (!para_values_set)
2352 style = get_style (layout, &iter);
2353 set_para_values (layout, base_dir, style, display);
2354 release_style (layout, style);
2357 /* Pango doesn't want the trailing paragraph delimiters */
2360 /* Only one character has type G_UNICODE_PARAGRAPH_SEPARATOR in
2361 * Unicode 3.0; update this if that changes.
2363 #define PARAGRAPH_SEPARATOR 0x2029
2366 if (layout_byte_offset > 0)
2368 const char *prev = g_utf8_prev_char (text + layout_byte_offset);
2369 ch = g_utf8_get_char (prev);
2370 if (ch == PARAGRAPH_SEPARATOR || ch == '\r' || ch == '\n')
2371 layout_byte_offset = prev - text; /* chop off */
2373 if (ch == '\n' && layout_byte_offset > 0)
2375 /* Possibly chop a CR as well */
2376 prev = g_utf8_prev_char (text + layout_byte_offset);
2378 --layout_byte_offset;
2383 pango_layout_set_text (display->layout, text, layout_byte_offset);
2384 pango_layout_set_attributes (display->layout, attrs);
2386 tmp_list1 = cursor_byte_offsets;
2387 tmp_list2 = cursor_segs;
2390 add_cursor (layout, display, tmp_list2->data,
2391 GPOINTER_TO_INT (tmp_list1->data));
2392 tmp_list1 = tmp_list1->next;
2393 tmp_list2 = tmp_list2->next;
2395 g_slist_free (cursor_byte_offsets);
2396 g_slist_free (cursor_segs);
2398 pango_layout_get_extents (display->layout, NULL, &extents);
2400 display->width = PIXEL_BOUND (extents.width) + display->left_margin + display->right_margin;
2401 display->height += PANGO_PIXELS (extents.height);
2403 /* If we aren't wrapping, we need to do the alignment of each
2404 * paragraph ourselves.
2406 if (pango_layout_get_width (display->layout) < 0)
2408 gint excess = display->total_width - display->width;
2410 switch (pango_layout_get_alignment (display->layout))
2412 case PANGO_ALIGN_LEFT:
2414 case PANGO_ALIGN_CENTER:
2415 display->x_offset += excess / 2;
2417 case PANGO_ALIGN_RIGHT:
2418 display->x_offset += excess;
2423 /* Free this if we aren't in a loop */
2424 if (layout->wrap_loop_count == 0)
2425 invalidate_cached_style (layout);
2428 pango_attr_list_unref (attrs);
2430 layout->one_display_cache = display;
2433 allocate_child_widgets (layout, display);
2439 gtk_text_layout_free_line_display (GtkTextLayout *layout,
2440 GtkTextLineDisplay *display)
2442 if (display != layout->one_display_cache)
2444 if (display->layout)
2445 g_object_unref (display->layout);
2447 if (display->cursors)
2449 g_slist_foreach (display->cursors, (GFunc)g_free, NULL);
2450 g_slist_free (display->cursors);
2452 g_slist_free (display->shaped_objects);
2454 if (display->pg_bg_color)
2455 gdk_color_free (display->pg_bg_color);
2461 /* Functions to convert iter <=> index for the line of a GtkTextLineDisplay
2462 * taking into account the preedit string and invisible text if necessary.
2465 line_display_iter_to_index (GtkTextLayout *layout,
2466 GtkTextLineDisplay *display,
2467 const GtkTextIter *iter)
2471 g_return_val_if_fail (_gtk_text_iter_get_text_line (iter) == display->line, 0);
2473 index = gtk_text_iter_get_visible_line_index (iter);
2475 if (layout->preedit_len > 0 && display->insert_index >= 0)
2477 if (index >= display->insert_index)
2478 index += layout->preedit_len;
2485 line_display_index_to_iter (GtkTextLayout *layout,
2486 GtkTextLineDisplay *display,
2491 g_return_if_fail (!_gtk_text_line_is_last (display->line,
2492 _gtk_text_buffer_get_btree (layout->buffer)));
2494 if (layout->preedit_len > 0 && display->insert_index >= 0)
2496 if (index >= display->insert_index + layout->preedit_len)
2497 index -= layout->preedit_len;
2498 else if (index > display->insert_index)
2500 index = display->insert_index;
2505 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
2506 iter, display->line, 0);
2508 gtk_text_iter_set_visible_line_index (iter, index);
2510 if (_gtk_text_iter_get_text_line (iter) != display->line)
2512 /* Clamp to end of line - really this clamping should have been done
2513 * before here, maybe in Pango, this is a broken band-aid I think
2515 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
2516 iter, display->line, 0);
2518 if (!gtk_text_iter_ends_line (iter))
2519 gtk_text_iter_forward_to_line_end (iter);
2522 gtk_text_iter_forward_chars (iter, trailing);
2526 get_line_at_y (GtkTextLayout *layout,
2533 if (y > layout->height)
2536 *line = _gtk_text_btree_find_line_by_y (_gtk_text_buffer_get_btree (layout->buffer),
2537 layout, y, line_top);
2540 *line = _gtk_text_btree_get_end_iter_line (_gtk_text_buffer_get_btree (layout->buffer));
2544 _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
2550 * gtk_text_layout_get_line_at_y:
2551 * @layout: a #GtkLayout
2552 * @target_iter: the iterator in which the result is stored
2553 * @y: the y positition
2554 * @line_top: location to store the y coordinate of the
2555 * top of the line. (Can by %NULL.)
2557 * Get the iter at the beginning of the line which is displayed
2561 gtk_text_layout_get_line_at_y (GtkTextLayout *layout,
2562 GtkTextIter *target_iter,
2568 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
2569 g_return_if_fail (target_iter != NULL);
2571 get_line_at_y (layout, y, &line, line_top);
2572 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
2573 target_iter, line, 0);
2577 gtk_text_layout_get_iter_at_pixel (GtkTextLayout *layout,
2578 GtkTextIter *target_iter,
2584 gtk_text_layout_get_iter_at_position (layout, target_iter, &trailing, x, y);
2586 gtk_text_iter_forward_chars (target_iter, trailing);
2589 void gtk_text_layout_get_iter_at_position (GtkTextLayout *layout,
2590 GtkTextIter *target_iter,
2598 GtkTextLineDisplay *display;
2600 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
2601 g_return_if_fail (target_iter != NULL);
2603 get_line_at_y (layout, y, &line, &line_top);
2605 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2607 x -= display->x_offset;
2608 y -= line_top + display->top_margin;
2610 /* If we are below the layout, position the cursor at the last character
2613 if (y > display->height - display->top_margin - display->bottom_margin)
2615 byte_index = _gtk_text_line_byte_count (line);
2620 /* Ignore the "outside" return value from pango. Pango is doing
2621 * the right thing even if we are outside the layout in the
2624 pango_layout_xy_to_index (display->layout, x * PANGO_SCALE, y * PANGO_SCALE,
2625 &byte_index, trailing);
2628 line_display_index_to_iter (layout, display, target_iter, byte_index, 0);
2630 gtk_text_layout_free_line_display (layout, display);
2635 * gtk_text_layout_get_cursor_locations:
2636 * @layout: a #GtkTextLayout
2637 * @iter: a #GtkTextIter
2638 * @strong_pos: location to store the strong cursor position (may be %NULL)
2639 * @weak_pos: location to store the weak cursor position (may be %NULL)
2641 * Given an iterator within a text layout, determine the positions of the
2642 * strong and weak cursors if the insertion point is at that
2643 * iterator. The position of each cursor is stored as a zero-width
2644 * rectangle. The strong cursor location is the location where
2645 * characters of the directionality equal to the base direction of the
2646 * paragraph are inserted. The weak cursor location is the location
2647 * where characters of the directionality opposite to the base
2648 * direction of the paragraph are inserted.
2651 gtk_text_layout_get_cursor_locations (GtkTextLayout *layout,
2653 GdkRectangle *strong_pos,
2654 GdkRectangle *weak_pos)
2657 GtkTextLineDisplay *display;
2660 GtkTextIter insert_iter;
2662 PangoRectangle pango_strong_pos;
2663 PangoRectangle pango_weak_pos;
2665 g_return_if_fail (layout != NULL);
2666 g_return_if_fail (iter != NULL);
2668 line = _gtk_text_iter_get_text_line (iter);
2669 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2670 index = line_display_iter_to_index (layout, display, iter);
2672 line_top = _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
2675 gtk_text_buffer_get_iter_at_mark (layout->buffer, &insert_iter,
2676 gtk_text_buffer_get_mark (layout->buffer,
2679 if (gtk_text_iter_equal (iter, &insert_iter))
2680 index += layout->preedit_cursor - layout->preedit_len;
2682 pango_layout_get_cursor_pos (display->layout, index,
2683 strong_pos ? &pango_strong_pos : NULL,
2684 weak_pos ? &pango_weak_pos : NULL);
2688 strong_pos->x = display->x_offset + pango_strong_pos.x / PANGO_SCALE;
2689 strong_pos->y = line_top + display->top_margin + pango_strong_pos.y / PANGO_SCALE;
2690 strong_pos->width = 0;
2691 strong_pos->height = pango_strong_pos.height / PANGO_SCALE;
2696 weak_pos->x = display->x_offset + pango_weak_pos.x / PANGO_SCALE;
2697 weak_pos->y = line_top + display->top_margin + pango_weak_pos.y / PANGO_SCALE;
2698 weak_pos->width = 0;
2699 weak_pos->height = pango_weak_pos.height / PANGO_SCALE;
2702 gtk_text_layout_free_line_display (layout, display);
2706 * _gtk_text_layout_get_block_cursor:
2707 * @layout: a #GtkTextLayout
2708 * @pos: a #GdkRectangle to store block cursor position
2710 * If layout is to display a block cursor, calculates its position
2711 * and returns %TRUE. Otherwise it returns %FALSE. In case when
2712 * cursor is visible, it simply returns the position stored in
2713 * the line display, otherwise it has to compute the position
2714 * (see get_block_cursor()).
2717 _gtk_text_layout_get_block_cursor (GtkTextLayout *layout,
2721 GtkTextLineDisplay *display;
2724 gboolean block = FALSE;
2726 g_return_val_if_fail (layout != NULL, FALSE);
2728 gtk_text_buffer_get_iter_at_mark (layout->buffer, &iter,
2729 gtk_text_buffer_get_insert (layout->buffer));
2730 line = _gtk_text_iter_get_text_line (&iter);
2731 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2733 if (display->has_block_cursor)
2736 rect = display->block_cursor;
2740 gint index = display->insert_index;
2743 index = gtk_text_iter_get_line_index (&iter);
2745 if (get_block_cursor (layout, display, &iter, index, &rect, NULL))
2753 line_top = _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
2757 pos->x += display->x_offset;
2758 pos->y += line_top + display->top_margin;
2761 gtk_text_layout_free_line_display (layout, display);
2766 * gtk_text_layout_get_line_yrange:
2767 * @layout: a #GtkTextLayout
2768 * @iter: a #GtkTextIter
2769 * @y: location to store the top of the paragraph in pixels,
2771 * @height location to store the height of the paragraph in pixels,
2774 * Find the range of y coordinates for the paragraph containing
2778 gtk_text_layout_get_line_yrange (GtkTextLayout *layout,
2779 const GtkTextIter *iter,
2785 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
2786 g_return_if_fail (_gtk_text_iter_get_btree (iter) == _gtk_text_buffer_get_btree (layout->buffer));
2788 line = _gtk_text_iter_get_text_line (iter);
2791 *y = _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
2795 GtkTextLineData *line_data = _gtk_text_line_get_data (line, layout);
2797 *height = line_data->height;
2804 * _gtk_text_layout_get_line_xrange:
2805 * @layout: a #GtkTextLayout
2806 * @iter: a #GtkTextIter
2807 * @x: location to store the top of the paragraph in pixels,
2809 * @width location to store the height of the paragraph in pixels,
2812 * Find the range of X coordinates for the paragraph containing
2813 * the given iter. Private for 2.0 due to API freeze, could
2814 * be made public for 2.2.
2817 _gtk_text_layout_get_line_xrange (GtkTextLayout *layout,
2818 const GtkTextIter *iter,
2824 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
2825 g_return_if_fail (_gtk_text_iter_get_btree (iter) == _gtk_text_buffer_get_btree (layout->buffer));
2827 line = _gtk_text_iter_get_text_line (iter);
2830 *x = 0; /* FIXME This is wrong; should represent the first available cursor position */
2834 GtkTextLineData *line_data = _gtk_text_line_get_data (line, layout);
2836 *width = line_data->width;
2843 gtk_text_layout_get_iter_location (GtkTextLayout *layout,
2844 const GtkTextIter *iter,
2847 PangoRectangle pango_rect;
2850 GtkTextLineDisplay *display;
2854 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
2855 g_return_if_fail (_gtk_text_iter_get_btree (iter) == _gtk_text_buffer_get_btree (layout->buffer));
2856 g_return_if_fail (rect != NULL);
2858 tree = _gtk_text_iter_get_btree (iter);
2859 line = _gtk_text_iter_get_text_line (iter);
2861 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2863 rect->y = _gtk_text_btree_find_line_top (tree, line, layout);
2865 x_offset = display->x_offset * PANGO_SCALE;
2867 byte_index = gtk_text_iter_get_line_index (iter);
2869 pango_layout_index_to_pos (display->layout, byte_index, &pango_rect);
2871 rect->x = PANGO_PIXELS (x_offset + pango_rect.x);
2872 rect->y += PANGO_PIXELS (pango_rect.y) + display->top_margin;
2873 rect->width = PANGO_PIXELS (pango_rect.width);
2874 rect->height = PANGO_PIXELS (pango_rect.height);
2876 gtk_text_layout_free_line_display (layout, display);
2881 /* Find the iter for the logical beginning of the first display line whose
2882 * top y is >= y. If none exists, move the iter to the logical beginning
2883 * of the last line in the buffer.
2886 find_display_line_below (GtkTextLayout *layout,
2890 GtkTextLine *line, *next;
2891 GtkTextLine *found_line = NULL;
2893 gint found_byte = 0;
2895 line = _gtk_text_btree_find_line_by_y (_gtk_text_buffer_get_btree (layout->buffer),
2896 layout, y, &line_top);
2900 _gtk_text_btree_get_end_iter_line (_gtk_text_buffer_get_btree (layout->buffer));
2903 _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
2907 while (line && !found_line)
2909 GtkTextLineDisplay *display = gtk_text_layout_get_line_display (layout, line, FALSE);
2910 PangoLayoutIter *layout_iter;
2912 layout_iter = pango_layout_get_iter (display->layout);
2914 line_top += display->top_margin;
2918 gint first_y, last_y;
2919 PangoLayoutLine *layout_line = pango_layout_iter_get_line_readonly (layout_iter);
2921 found_byte = layout_line->start_index;
2929 pango_layout_iter_get_line_yrange (layout_iter, &first_y, &last_y);
2930 line_top += (last_y - first_y) / PANGO_SCALE;
2932 while (pango_layout_iter_next_line (layout_iter));
2934 pango_layout_iter_free (layout_iter);
2936 line_top += display->bottom_margin;
2937 gtk_text_layout_free_line_display (layout, display);
2939 next = _gtk_text_line_next_excluding_last (line);
2946 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
2947 iter, found_line, found_byte);
2950 /* Find the iter for the logical beginning of the last display line whose
2951 * top y is >= y. If none exists, move the iter to the logical beginning
2952 * of the first line in the buffer.
2955 find_display_line_above (GtkTextLayout *layout,
2960 GtkTextLine *found_line = NULL;
2962 gint found_byte = 0;
2964 line = _gtk_text_btree_find_line_by_y (_gtk_text_buffer_get_btree (layout->buffer), layout, y, &line_top);
2967 line = _gtk_text_btree_get_end_iter_line (_gtk_text_buffer_get_btree (layout->buffer));
2969 line_top = _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer), line, layout);
2972 while (line && !found_line)
2974 GtkTextLineDisplay *display = gtk_text_layout_get_line_display (layout, line, FALSE);
2975 PangoRectangle logical_rect;
2976 PangoLayoutIter *layout_iter;
2979 layout_iter = pango_layout_get_iter (display->layout);
2981 line_top -= display->top_margin + display->bottom_margin;
2982 pango_layout_iter_get_layout_extents (layout_iter, NULL, &logical_rect);
2983 line_top -= logical_rect.height / PANGO_SCALE;
2985 tmp_top = line_top + display->top_margin;
2989 gint first_y, last_y;
2990 PangoLayoutLine *layout_line = pango_layout_iter_get_line_readonly (layout_iter);
2992 found_byte = layout_line->start_index;
2994 pango_layout_iter_get_line_yrange (layout_iter, &first_y, &last_y);
2996 tmp_top -= (last_y - first_y) / PANGO_SCALE;
3001 pango_layout_iter_free (layout_iter);
3005 while (pango_layout_iter_next_line (layout_iter));
3007 pango_layout_iter_free (layout_iter);
3009 gtk_text_layout_free_line_display (layout, display);
3011 line = _gtk_text_line_previous (line);
3017 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
3018 iter, found_line, found_byte);
3020 gtk_text_buffer_get_iter_at_offset (layout->buffer, iter, 0);
3024 * gtk_text_layout_clamp_iter_to_vrange:
3025 * @layout: a #GtkTextLayout
3026 * @iter: a #GtkTextIter
3027 * @top: the top of the range
3028 * @bottom: the bottom the range
3030 * If the iterator is not fully in the range @top <= y < @bottom,
3031 * then, if possible, move it the minimum distance so that the
3032 * iterator in this range.
3034 * Returns: %TRUE if the iterator was moved, otherwise %FALSE.
3037 gtk_text_layout_clamp_iter_to_vrange (GtkTextLayout *layout,
3042 GdkRectangle iter_rect;
3044 gtk_text_layout_get_iter_location (layout, iter, &iter_rect);
3046 /* If the iter is at least partially above the range, put the iter
3047 * at the first fully visible line after the range.
3049 if (iter_rect.y < top)
3051 find_display_line_below (layout, iter, top);
3055 /* Otherwise, if the iter is at least partially below the screen, put the
3056 * iter on the last logical position of the last completely visible
3059 else if (iter_rect.y + iter_rect.height > bottom)
3061 find_display_line_above (layout, iter, bottom);
3070 * gtk_text_layout_move_iter_to_previous_line:
3071 * @layout: a #GtkLayout
3072 * @iter: a #GtkTextIter
3074 * Move the iterator to the beginning of the previous line. The lines
3075 * of a wrapped paragraph are treated as distinct for this operation.
3078 gtk_text_layout_move_iter_to_previous_line (GtkTextLayout *layout,
3082 GtkTextLineDisplay *display;
3085 PangoLayoutLine *layout_line;
3087 gboolean update_byte = FALSE;
3089 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), FALSE);
3090 g_return_val_if_fail (iter != NULL, FALSE);
3095 line = _gtk_text_iter_get_text_line (iter);
3096 display = gtk_text_layout_get_line_display (layout, line, FALSE);
3097 line_byte = line_display_iter_to_index (layout, display, iter);
3099 /* If display->height == 0 then the line is invisible, so don't
3102 while (display->height == 0)
3104 GtkTextLine *prev_line;
3106 prev_line = _gtk_text_line_previous (line);
3108 if (prev_line == NULL)
3110 line_display_index_to_iter (layout, display, iter, 0, 0);
3114 gtk_text_layout_free_line_display (layout, display);
3117 display = gtk_text_layout_get_line_display (layout, prev_line, FALSE);
3121 tmp_list = pango_layout_get_lines_readonly (display->layout);
3122 layout_line = tmp_list->data;
3126 line_byte = layout_line->start_index + layout_line->length;
3129 if (line_byte < layout_line->length || !tmp_list->next) /* first line of paragraph */
3131 GtkTextLine *prev_line;
3133 prev_line = _gtk_text_line_previous (line);
3135 /* first line of the whole buffer, do not move the iter and return FALSE */
3136 if (prev_line == NULL)
3141 gtk_text_layout_free_line_display (layout, display);
3143 display = gtk_text_layout_get_line_display (layout, prev_line, FALSE);
3145 if (display->height > 0)
3147 tmp_list = g_slist_last (pango_layout_get_lines_readonly (display->layout));
3148 layout_line = tmp_list->data;
3150 line_display_index_to_iter (layout, display, iter,
3151 layout_line->start_index + layout_line->length, 0);
3155 prev_line = _gtk_text_line_previous (prev_line);
3160 gint prev_offset = layout_line->start_index;
3162 tmp_list = tmp_list->next;
3165 layout_line = tmp_list->data;
3167 if (line_byte < layout_line->start_index + layout_line->length ||
3170 line_display_index_to_iter (layout, display, iter, prev_offset, 0);
3174 prev_offset = layout_line->start_index;
3175 tmp_list = tmp_list->next;
3181 gtk_text_layout_free_line_display (layout, display);
3184 !gtk_text_iter_equal (iter, &orig) &&
3185 !gtk_text_iter_is_end (iter);
3189 * gtk_text_layout_move_iter_to_next_line:
3190 * @layout: a #GtkLayout
3191 * @iter: a #GtkTextIter
3193 * Move the iterator to the beginning of the next line. The
3194 * lines of a wrapped paragraph are treated as distinct for
3198 gtk_text_layout_move_iter_to_next_line (GtkTextLayout *layout,
3202 GtkTextLineDisplay *display;
3205 gboolean found = FALSE;
3206 gboolean found_after = FALSE;
3207 gboolean first = TRUE;
3209 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), FALSE);
3210 g_return_val_if_fail (iter != NULL, FALSE);
3214 line = _gtk_text_iter_get_text_line (iter);
3216 while (line && !found_after)
3220 display = gtk_text_layout_get_line_display (layout, line, FALSE);
3222 if (display->height == 0)
3227 line_byte = line_display_iter_to_index (layout, display, iter);
3233 tmp_list = pango_layout_get_lines_readonly (display->layout);
3234 while (tmp_list && !found_after)
3236 PangoLayoutLine *layout_line = tmp_list->data;
3240 line_display_index_to_iter (layout, display, iter,
3241 layout_line->start_index, 0);
3244 else if (line_byte < layout_line->start_index + layout_line->length || !tmp_list->next)
3247 tmp_list = tmp_list->next;
3252 gtk_text_layout_free_line_display (layout, display);
3254 line = _gtk_text_line_next_excluding_last (line);
3258 gtk_text_buffer_get_end_iter (layout->buffer, iter);
3261 !gtk_text_iter_equal (iter, &orig) &&
3262 !gtk_text_iter_is_end (iter);
3266 * gtk_text_layout_move_iter_to_line_end:
3267 * @layout: a #GtkTextLayout
3268 * @direction: if negative, move to beginning of line, otherwise
3269 move to end of line.
3271 * Move to the beginning or end of a display line.
3274 gtk_text_layout_move_iter_to_line_end (GtkTextLayout *layout,
3279 GtkTextLineDisplay *display;
3284 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), FALSE);
3285 g_return_val_if_fail (iter != NULL, FALSE);
3289 line = _gtk_text_iter_get_text_line (iter);
3290 display = gtk_text_layout_get_line_display (layout, line, FALSE);
3291 line_byte = line_display_iter_to_index (layout, display, iter);
3293 tmp_list = pango_layout_get_lines_readonly (display->layout);
3296 PangoLayoutLine *layout_line = tmp_list->data;
3298 if (line_byte < layout_line->start_index + layout_line->length || !tmp_list->next)
3300 line_display_index_to_iter (layout, display, iter,
3301 direction < 0 ? layout_line->start_index : layout_line->start_index + layout_line->length,
3304 /* FIXME: As a bad hack, we move back one position when we
3305 * are inside a paragraph to avoid going to next line on a
3306 * forced break not at whitespace. Real fix is to keep track
3307 * of whether marks are at leading or trailing edge? */
3308 if (direction > 0 && layout_line->length > 0 &&
3309 !gtk_text_iter_ends_line (iter) &&
3310 !_gtk_text_btree_char_is_invisible (iter))
3311 gtk_text_iter_backward_char (iter);
3315 tmp_list = tmp_list->next;
3318 gtk_text_layout_free_line_display (layout, display);
3321 !gtk_text_iter_equal (iter, &orig) &&
3322 !gtk_text_iter_is_end (iter);
3327 * gtk_text_layout_iter_starts_line:
3328 * @layout: a #GtkTextLayout
3329 * @iter: iterator to test
3331 * Tests whether an iterator is at the start of a display line.
3334 gtk_text_layout_iter_starts_line (GtkTextLayout *layout,
3335 const GtkTextIter *iter)
3338 GtkTextLineDisplay *display;
3342 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), FALSE);
3343 g_return_val_if_fail (iter != NULL, FALSE);
3345 line = _gtk_text_iter_get_text_line (iter);
3346 display = gtk_text_layout_get_line_display (layout, line, FALSE);
3347 line_byte = line_display_iter_to_index (layout, display, iter);
3349 tmp_list = pango_layout_get_lines_readonly (display->layout);
3352 PangoLayoutLine *layout_line = tmp_list->data;
3354 if (line_byte < layout_line->start_index + layout_line->length ||
3357 /* We're located on this line or the para delimiters before
3360 gtk_text_layout_free_line_display (layout, display);
3362 if (line_byte == layout_line->start_index)
3368 tmp_list = tmp_list->next;
3371 g_assert_not_reached ();
3376 gtk_text_layout_get_iter_at_line (GtkTextLayout *layout,
3381 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
3382 iter, line, byte_offset);
3387 * gtk_text_layout_move_iter_to_x:
3388 * @layout: a #GtkTextLayout
3389 * @iter: a #GtkTextIter
3392 * Keeping the iterator on the same line of the layout, move it to the
3393 * specified X coordinate. The lines of a wrapped paragraph are
3394 * treated as distinct for this operation.
3397 gtk_text_layout_move_iter_to_x (GtkTextLayout *layout,
3402 GtkTextLineDisplay *display;
3404 PangoLayoutIter *layout_iter;
3406 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
3407 g_return_if_fail (iter != NULL);
3409 line = _gtk_text_iter_get_text_line (iter);
3411 display = gtk_text_layout_get_line_display (layout, line, FALSE);
3412 line_byte = line_display_iter_to_index (layout, display, iter);
3414 layout_iter = pango_layout_get_iter (display->layout);
3418 PangoLayoutLine *layout_line = pango_layout_iter_get_line_readonly (layout_iter);
3420 if (line_byte < layout_line->start_index + layout_line->length ||
3421 pango_layout_iter_at_last_line (layout_iter))
3423 PangoRectangle logical_rect;
3424 gint byte_index, trailing;
3425 gint x_offset = display->x_offset * PANGO_SCALE;
3427 pango_layout_iter_get_line_extents (layout_iter, NULL, &logical_rect);
3429 pango_layout_line_x_to_index (layout_line,
3430 x * PANGO_SCALE - x_offset - logical_rect.x,
3431 &byte_index, &trailing);
3433 line_display_index_to_iter (layout, display, iter, byte_index, trailing);
3438 while (pango_layout_iter_next_line (layout_iter));
3440 pango_layout_iter_free (layout_iter);
3442 gtk_text_layout_free_line_display (layout, display);
3446 * gtk_text_layout_move_iter_visually:
3447 * @layout: a #GtkTextLayout
3448 * @iter: a #GtkTextIter
3449 * @count: number of characters to move (negative moves left, positive moves right)
3451 * Move the iterator a given number of characters visually, treating
3452 * it as the strong cursor position. If @count is positive, then the
3453 * new strong cursor position will be @count positions to the right of
3454 * the old cursor position. If @count is negative then the new strong
3455 * cursor position will be @count positions to the left of the old
3458 * In the presence of bidirection text, the correspondence
3459 * between logical and visual order will depend on the direction
3460 * of the current run, and there may be jumps when the cursor
3461 * is moved off of the end of a run.
3465 gtk_text_layout_move_iter_visually (GtkTextLayout *layout,
3469 GtkTextLineDisplay *display = NULL;
3471 GtkTextIter lineiter;
3473 g_return_val_if_fail (layout != NULL, FALSE);
3474 g_return_val_if_fail (iter != NULL, FALSE);
3480 GtkTextLine *line = _gtk_text_iter_get_text_line (iter);
3482 gint extra_back = 0;
3485 int byte_count = _gtk_text_line_byte_count (line);
3491 display = gtk_text_layout_get_line_display (layout, line, FALSE);
3493 if (layout->cursor_direction == GTK_TEXT_DIR_NONE)
3496 strong = display->direction == layout->cursor_direction;
3498 line_byte = line_display_iter_to_index (layout, display, iter);
3502 pango_layout_move_cursor_visually (display->layout, strong, line_byte, 0, 1, &new_index, &new_trailing);
3507 pango_layout_move_cursor_visually (display->layout, strong, line_byte, 0, -1, &new_index, &new_trailing);
3511 /* We need to handle the preedit string specially. Well, we don't really need to
3512 * handle it specially, since hopefully calling gtk_im_context_reset() will
3513 * remove the preedit string; but if we start off in front of the preedit
3514 * string (logically) and end up in or on the back edge of the preedit string,
3515 * we should move the iter one place farther.
3517 if (layout->preedit_len > 0 && display->insert_index >= 0)
3519 if (line_byte == display->insert_index + layout->preedit_len &&
3520 new_index < display->insert_index + layout->preedit_len)
3522 line_byte = display->insert_index;
3527 if (new_index < 0 || (new_index == 0 && extra_back))
3531 line = _gtk_text_line_previous (line);
3536 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
3537 &lineiter, line, 0);
3539 while (totally_invisible_line (layout, line, &lineiter));
3541 gtk_text_layout_free_line_display (layout, display);
3542 display = gtk_text_layout_get_line_display (layout, line, FALSE);
3543 gtk_text_iter_forward_to_line_end (&lineiter);
3544 new_index = gtk_text_iter_get_visible_line_index (&lineiter);
3546 else if (new_index > byte_count)
3550 line = _gtk_text_line_next_excluding_last (line);
3554 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
3555 &lineiter, line, 0);
3557 while (totally_invisible_line (layout, line, &lineiter));
3559 gtk_text_layout_free_line_display (layout, display);
3560 display = gtk_text_layout_get_line_display (layout, line, FALSE);
3564 line_display_index_to_iter (layout, display, iter, new_index, new_trailing);
3566 gtk_text_iter_backward_char (iter);
3569 gtk_text_layout_free_line_display (layout, display);
3574 !gtk_text_iter_equal (iter, &orig) &&
3575 !gtk_text_iter_is_end (iter);
3579 gtk_text_layout_spew (GtkTextLayout *layout)
3582 GtkTextDisplayLine *iter;
3584 guint paragraphs = 0;
3585 GtkTextLine *last_line = NULL;
3587 iter = layout->line_list;
3588 while (iter != NULL)
3590 if (iter->line != last_line)
3592 printf ("%5u paragraph (%p)\n", paragraphs, iter->line);
3594 last_line = iter->line;
3597 printf (" %5u y: %d len: %d start: %d bytes: %d\n",
3598 wrapped, iter->y, iter->length, iter->byte_offset,
3605 printf ("Layout %s recompute\n",
3606 layout->need_recompute ? "needs" : "doesn't need");
3608 printf ("Layout pars: %u lines: %u size: %d x %d Screen width: %d\n",
3609 paragraphs, wrapped, layout->width,
3610 layout->height, layout->screen_width);
3614 /* Catch all situations that move the insertion point.
3617 gtk_text_layout_mark_set_handler (GtkTextBuffer *buffer,
3618 const GtkTextIter *location,
3622 GtkTextLayout *layout = GTK_TEXT_LAYOUT (data);
3624 if (mark == gtk_text_buffer_get_insert (buffer))
3625 gtk_text_layout_update_cursor_line (layout);
3629 gtk_text_layout_buffer_insert_text (GtkTextBuffer *textbuffer,
3635 GtkTextLayout *layout = GTK_TEXT_LAYOUT (data);
3637 gtk_text_layout_update_cursor_line (layout);
3641 gtk_text_layout_buffer_delete_range (GtkTextBuffer *textbuffer,
3646 GtkTextLayout *layout = GTK_TEXT_LAYOUT (data);
3648 gtk_text_layout_update_cursor_line (layout);
3651 #define __GTK_TEXT_LAYOUT_C__
3652 #include "gtkaliasdef.c"