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"
91 #define GTK_TEXT_LAYOUT_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GTK_TYPE_TEXT_LAYOUT, GtkTextLayoutPrivate))
93 typedef struct _GtkTextLayoutPrivate GtkTextLayoutPrivate;
95 struct _GtkTextLayoutPrivate
97 /* Cache the line that the cursor is positioned on, as the keyboard
98 direction only influences the direction of the cursor line.
100 GtkTextLine *cursor_line;
103 static GtkTextLineData *gtk_text_layout_real_wrap (GtkTextLayout *layout,
106 GtkTextLineData *line_data);
108 static void gtk_text_layout_invalidated (GtkTextLayout *layout);
110 static void gtk_text_layout_real_invalidate (GtkTextLayout *layout,
111 const GtkTextIter *start,
112 const GtkTextIter *end);
113 static void gtk_text_layout_real_invalidate_cursors(GtkTextLayout *layout,
114 const GtkTextIter *start,
115 const GtkTextIter *end);
116 static void gtk_text_layout_invalidate_cache (GtkTextLayout *layout,
118 gboolean cursors_only);
119 static void gtk_text_layout_invalidate_cursor_line (GtkTextLayout *layout,
120 gboolean cursors_only);
121 static void gtk_text_layout_real_free_line_data (GtkTextLayout *layout,
123 GtkTextLineData *line_data);
124 static void gtk_text_layout_emit_changed (GtkTextLayout *layout,
129 static void gtk_text_layout_invalidate_all (GtkTextLayout *layout);
131 static PangoAttribute *gtk_text_attr_appearance_new (const GtkTextAppearance *appearance);
133 static void gtk_text_layout_mark_set_handler (GtkTextBuffer *buffer,
134 const GtkTextIter *location,
137 static void gtk_text_layout_buffer_insert_text (GtkTextBuffer *textbuffer,
142 static void gtk_text_layout_buffer_delete_range (GtkTextBuffer *textbuffer,
147 static void gtk_text_layout_update_cursor_line (GtkTextLayout *layout);
149 static void line_display_index_to_iter (GtkTextLayout *layout,
150 GtkTextLineDisplay *display,
155 static gint line_display_iter_to_index (GtkTextLayout *layout,
156 GtkTextLineDisplay *display,
157 const GtkTextIter *iter);
171 #define PIXEL_BOUND(d) (((d) + PANGO_SCALE - 1) / PANGO_SCALE)
173 static void gtk_text_layout_finalize (GObject *object);
175 static guint signals[LAST_SIGNAL] = { 0 };
177 PangoAttrType gtk_text_attr_appearance_type = 0;
179 G_DEFINE_TYPE (GtkTextLayout, gtk_text_layout, G_TYPE_OBJECT)
182 gtk_text_layout_class_init (GtkTextLayoutClass *klass)
184 GObjectClass *object_class = G_OBJECT_CLASS (klass);
186 object_class->finalize = gtk_text_layout_finalize;
188 klass->wrap = gtk_text_layout_real_wrap;
189 klass->invalidate = gtk_text_layout_real_invalidate;
190 klass->invalidate_cursors = gtk_text_layout_real_invalidate_cursors;
191 klass->free_line_data = gtk_text_layout_real_free_line_data;
193 signals[INVALIDATED] =
194 g_signal_new (I_("invalidated"),
195 G_OBJECT_CLASS_TYPE (object_class),
197 G_STRUCT_OFFSET (GtkTextLayoutClass, invalidated),
199 _gtk_marshal_VOID__VOID,
204 g_signal_new (I_("changed"),
205 G_OBJECT_CLASS_TYPE (object_class),
207 G_STRUCT_OFFSET (GtkTextLayoutClass, changed),
209 _gtk_marshal_VOID__INT_INT_INT,
216 signals[ALLOCATE_CHILD] =
217 g_signal_new (I_("allocate-child"),
218 G_OBJECT_CLASS_TYPE (object_class),
220 G_STRUCT_OFFSET (GtkTextLayoutClass, allocate_child),
222 _gtk_marshal_VOID__OBJECT_INT_INT,
229 g_type_class_add_private (object_class, sizeof (GtkTextLayoutPrivate));
233 gtk_text_layout_init (GtkTextLayout *text_layout)
235 text_layout->cursor_visible = TRUE;
239 gtk_text_layout_new (void)
241 return g_object_new (GTK_TYPE_TEXT_LAYOUT, NULL);
245 free_style_cache (GtkTextLayout *text_layout)
247 if (text_layout->one_style_cache)
249 gtk_text_attributes_unref (text_layout->one_style_cache);
250 text_layout->one_style_cache = NULL;
255 gtk_text_layout_finalize (GObject *object)
257 GtkTextLayout *layout;
259 layout = GTK_TEXT_LAYOUT (object);
261 gtk_text_layout_set_buffer (layout, NULL);
263 if (layout->default_style)
264 gtk_text_attributes_unref (layout->default_style);
265 layout->default_style = NULL;
267 if (layout->ltr_context)
269 g_object_unref (layout->ltr_context);
270 layout->ltr_context = NULL;
272 if (layout->rtl_context)
274 g_object_unref (layout->rtl_context);
275 layout->rtl_context = NULL;
278 if (layout->one_display_cache)
280 GtkTextLineDisplay *tmp_display = layout->one_display_cache;
281 layout->one_display_cache = NULL;
282 gtk_text_layout_free_line_display (layout, tmp_display);
285 if (layout->preedit_string)
287 g_free (layout->preedit_string);
288 layout->preedit_string = NULL;
291 if (layout->preedit_attrs)
293 pango_attr_list_unref (layout->preedit_attrs);
294 layout->preedit_attrs = NULL;
298 G_OBJECT_CLASS (gtk_text_layout_parent_class)->finalize (object);
302 * gtk_text_layout_set_buffer:
303 * @buffer: (allow-none):
306 gtk_text_layout_set_buffer (GtkTextLayout *layout,
307 GtkTextBuffer *buffer)
309 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
310 g_return_if_fail (buffer == NULL || GTK_IS_TEXT_BUFFER (buffer));
312 if (layout->buffer == buffer)
315 free_style_cache (layout);
319 _gtk_text_btree_remove_view (_gtk_text_buffer_get_btree (layout->buffer),
322 g_signal_handlers_disconnect_by_func (layout->buffer,
323 G_CALLBACK (gtk_text_layout_mark_set_handler),
325 g_signal_handlers_disconnect_by_func (layout->buffer,
326 G_CALLBACK (gtk_text_layout_buffer_insert_text),
328 g_signal_handlers_disconnect_by_func (layout->buffer,
329 G_CALLBACK (gtk_text_layout_buffer_delete_range),
332 g_object_unref (layout->buffer);
333 layout->buffer = NULL;
338 layout->buffer = buffer;
340 g_object_ref (buffer);
342 _gtk_text_btree_add_view (_gtk_text_buffer_get_btree (buffer), layout);
344 /* Bind to all signals that move the insert mark. */
345 g_signal_connect_after (layout->buffer, "mark-set",
346 G_CALLBACK (gtk_text_layout_mark_set_handler), layout);
347 g_signal_connect_after (layout->buffer, "insert-text",
348 G_CALLBACK (gtk_text_layout_buffer_insert_text), layout);
349 g_signal_connect_after (layout->buffer, "delete-range",
350 G_CALLBACK (gtk_text_layout_buffer_delete_range), layout);
352 gtk_text_layout_update_cursor_line (layout);
357 gtk_text_layout_default_style_changed (GtkTextLayout *layout)
359 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
361 DV (g_print ("invalidating all due to default style change (%s)\n", G_STRLOC));
362 gtk_text_layout_invalidate_all (layout);
366 gtk_text_layout_set_default_style (GtkTextLayout *layout,
367 GtkTextAttributes *values)
369 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
370 g_return_if_fail (values != NULL);
372 if (values == layout->default_style)
375 gtk_text_attributes_ref (values);
377 if (layout->default_style)
378 gtk_text_attributes_unref (layout->default_style);
380 layout->default_style = values;
382 gtk_text_layout_default_style_changed (layout);
386 gtk_text_layout_set_contexts (GtkTextLayout *layout,
387 PangoContext *ltr_context,
388 PangoContext *rtl_context)
390 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
392 if (layout->ltr_context != ltr_context)
394 if (layout->ltr_context)
395 g_object_unref (layout->ltr_context);
397 layout->ltr_context = ltr_context;
398 g_object_ref (layout->ltr_context);
401 if (layout->rtl_context != rtl_context)
403 if (layout->rtl_context)
404 g_object_unref (layout->rtl_context);
406 layout->rtl_context = rtl_context;
407 g_object_ref (layout->rtl_context);
410 DV (g_print ("invalidating all due to new pango contexts (%s)\n", G_STRLOC));
411 gtk_text_layout_invalidate_all (layout);
415 * gtk_text_layout_set_overwrite_mode:
416 * @layout: a #GtkTextLayout
417 * @overwrite: overwrite mode
419 * Sets overwrite mode
422 gtk_text_layout_set_overwrite_mode (GtkTextLayout *layout,
425 overwrite = overwrite != 0;
426 if (overwrite != layout->overwrite_mode)
428 layout->overwrite_mode = overwrite;
429 gtk_text_layout_invalidate_cursor_line (layout, TRUE);
434 * gtk_text_layout_set_cursor_direction:
435 * @direction: the new direction(s) for which to draw cursors.
436 * %GTK_TEXT_DIR_NONE means draw cursors for both
437 * left-to-right insertion and right-to-left insertion.
438 * (The two cursors will be visually distinguished.)
440 * Sets which text directions (left-to-right and/or right-to-left) for
441 * which cursors will be drawn for the insertion point. The visual
442 * point at which new text is inserted depends on whether the new
443 * text is right-to-left or left-to-right, so it may be desired to
444 * make the drawn position of the cursor depend on the keyboard state.
447 gtk_text_layout_set_cursor_direction (GtkTextLayout *layout,
448 GtkTextDirection direction)
450 if (direction != layout->cursor_direction)
452 layout->cursor_direction = direction;
453 gtk_text_layout_invalidate_cursor_line (layout, TRUE);
458 * gtk_text_layout_set_keyboard_direction:
459 * @keyboard_dir: the current direction of the keyboard.
461 * Sets the keyboard direction; this is used as for the bidirectional
462 * base direction for the line with the cursor if the line contains
463 * only neutral characters.
466 gtk_text_layout_set_keyboard_direction (GtkTextLayout *layout,
467 GtkTextDirection keyboard_dir)
469 if (keyboard_dir != layout->keyboard_direction)
471 layout->keyboard_direction = keyboard_dir;
472 gtk_text_layout_invalidate_cursor_line (layout, TRUE);
477 * gtk_text_layout_get_buffer:
478 * @layout: a #GtkTextLayout
480 * Gets the text buffer used by the layout. See
481 * gtk_text_layout_set_buffer().
483 * Return value: the text buffer used by the layout.
486 gtk_text_layout_get_buffer (GtkTextLayout *layout)
488 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), NULL);
490 return layout->buffer;
494 gtk_text_layout_set_screen_width (GtkTextLayout *layout, gint width)
496 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
497 g_return_if_fail (width >= 0);
498 g_return_if_fail (layout->wrap_loop_count == 0);
500 if (layout->screen_width == width)
503 layout->screen_width = width;
505 DV (g_print ("invalidating all due to new screen width (%s)\n", G_STRLOC));
506 gtk_text_layout_invalidate_all (layout);
510 * gtk_text_layout_set_cursor_visible:
511 * @layout: a #GtkTextLayout
512 * @cursor_visible: If %FALSE, then the insertion cursor will not
513 * be shown, even if the text is editable.
515 * Sets whether the insertion cursor should be shown. Generally,
516 * widgets using #GtkTextLayout will hide the cursor when the
517 * widget does not have the input focus.
520 gtk_text_layout_set_cursor_visible (GtkTextLayout *layout,
521 gboolean cursor_visible)
523 cursor_visible = (cursor_visible != FALSE);
525 if (layout->cursor_visible != cursor_visible)
530 layout->cursor_visible = cursor_visible;
532 /* Now queue a redraw on the paragraph containing the cursor
534 gtk_text_buffer_get_iter_at_mark (layout->buffer, &iter,
535 gtk_text_buffer_get_insert (layout->buffer));
537 gtk_text_layout_get_line_yrange (layout, &iter, &y, &height);
538 gtk_text_layout_emit_changed (layout, y, height, height);
540 gtk_text_layout_invalidate_cache (layout, _gtk_text_iter_get_text_line (&iter), TRUE);
545 * gtk_text_layout_get_cursor_visible:
546 * @layout: a #GtkTextLayout
548 * Returns whether the insertion cursor will be shown.
550 * Return value: if %FALSE, the insertion cursor will not be
551 * shown, even if the text is editable.
554 gtk_text_layout_get_cursor_visible (GtkTextLayout *layout)
556 return layout->cursor_visible;
560 * gtk_text_layout_set_preedit_string:
561 * @layout: a #PangoLayout
562 * @preedit_string: a string to display at the insertion point
563 * @preedit_attrs: a #PangoAttrList of attributes that apply to @preedit_string
564 * @cursor_pos: position of cursor within preedit string in chars
566 * Set the preedit string and attributes. The preedit string is a
567 * string showing text that is currently being edited and not
568 * yet committed into the buffer.
571 gtk_text_layout_set_preedit_string (GtkTextLayout *layout,
572 const gchar *preedit_string,
573 PangoAttrList *preedit_attrs,
576 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
577 g_return_if_fail (preedit_attrs != NULL || preedit_string == NULL);
579 g_free (layout->preedit_string);
581 if (layout->preedit_attrs)
582 pango_attr_list_unref (layout->preedit_attrs);
586 layout->preedit_string = g_strdup (preedit_string);
587 layout->preedit_len = strlen (layout->preedit_string);
588 pango_attr_list_ref (preedit_attrs);
589 layout->preedit_attrs = preedit_attrs;
591 cursor_pos = CLAMP (cursor_pos, 0, g_utf8_strlen (layout->preedit_string, -1));
592 layout->preedit_cursor = g_utf8_offset_to_pointer (layout->preedit_string, cursor_pos) - layout->preedit_string;
596 layout->preedit_string = NULL;
597 layout->preedit_len = 0;
598 layout->preedit_attrs = NULL;
599 layout->preedit_cursor = 0;
602 gtk_text_layout_invalidate_cursor_line (layout, FALSE);
606 gtk_text_layout_get_size (GtkTextLayout *layout,
610 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
613 *width = layout->width;
616 *height = layout->height;
620 gtk_text_layout_invalidated (GtkTextLayout *layout)
622 g_signal_emit (layout, signals[INVALIDATED], 0);
626 gtk_text_layout_emit_changed (GtkTextLayout *layout,
631 g_signal_emit (layout, signals[CHANGED], 0, y, old_height, new_height);
635 text_layout_changed (GtkTextLayout *layout,
639 gboolean cursors_only)
641 /* Check if the range intersects our cached line display,
642 * and invalidate the cached line if so.
644 if (layout->one_display_cache)
646 GtkTextLine *line = layout->one_display_cache->line;
647 gint cache_y = _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
649 gint cache_height = layout->one_display_cache->height;
651 if (cache_y + cache_height > y && cache_y < y + old_height)
652 gtk_text_layout_invalidate_cache (layout, line, cursors_only);
655 gtk_text_layout_emit_changed (layout, y, old_height, new_height);
659 gtk_text_layout_changed (GtkTextLayout *layout,
664 text_layout_changed (layout, y, old_height, new_height, FALSE);
668 gtk_text_layout_cursors_changed (GtkTextLayout *layout,
673 text_layout_changed (layout, y, old_height, new_height, TRUE);
677 gtk_text_layout_free_line_data (GtkTextLayout *layout,
679 GtkTextLineData *line_data)
681 GTK_TEXT_LAYOUT_GET_CLASS (layout)->free_line_data (layout, line, line_data);
685 gtk_text_layout_invalidate (GtkTextLayout *layout,
686 const GtkTextIter *start_index,
687 const GtkTextIter *end_index)
689 GTK_TEXT_LAYOUT_GET_CLASS (layout)->invalidate (layout, start_index, end_index);
693 gtk_text_layout_invalidate_cursors (GtkTextLayout *layout,
694 const GtkTextIter *start_index,
695 const GtkTextIter *end_index)
697 GTK_TEXT_LAYOUT_GET_CLASS (layout)->invalidate_cursors (layout, start_index, end_index);
701 gtk_text_layout_wrap (GtkTextLayout *layout,
704 GtkTextLineData *line_data)
706 return GTK_TEXT_LAYOUT_GET_CLASS (layout)->wrap (layout, line, line_data);
711 * gtk_text_layout_get_lines:
713 * Return value: (element-type GtkTextLine) (transfer container):
716 gtk_text_layout_get_lines (GtkTextLayout *layout,
717 /* [top_y, bottom_y) */
722 GtkTextLine *first_btree_line;
723 GtkTextLine *last_btree_line;
727 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), NULL);
728 g_return_val_if_fail (bottom_y > top_y, NULL);
733 _gtk_text_btree_find_line_by_y (_gtk_text_buffer_get_btree (layout->buffer),
734 layout, top_y, first_line_y);
735 if (first_btree_line == NULL)
741 /* -1 since bottom_y is one past */
743 _gtk_text_btree_find_line_by_y (_gtk_text_buffer_get_btree (layout->buffer),
744 layout, bottom_y - 1, NULL);
746 if (!last_btree_line)
748 _gtk_text_btree_get_end_iter_line (_gtk_text_buffer_get_btree (layout->buffer));
750 g_assert (last_btree_line != NULL);
752 line = first_btree_line;
755 retval = g_slist_prepend (retval, line);
757 if (line == last_btree_line)
760 line = _gtk_text_line_next_excluding_last (line);
763 retval = g_slist_reverse (retval);
769 invalidate_cached_style (GtkTextLayout *layout)
771 free_style_cache (layout);
774 /* These should be called around a loop which wraps a CONTIGUOUS bunch
775 * of display lines. If the lines aren't contiguous you can't call
779 gtk_text_layout_wrap_loop_start (GtkTextLayout *layout)
781 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
782 g_return_if_fail (layout->one_style_cache == NULL);
784 layout->wrap_loop_count += 1;
788 gtk_text_layout_wrap_loop_end (GtkTextLayout *layout)
790 g_return_if_fail (layout->wrap_loop_count > 0);
792 layout->wrap_loop_count -= 1;
794 if (layout->wrap_loop_count == 0)
796 /* We cache a some stuff if we're iterating over some lines wrapping
797 * them. This cleans it up.
799 /* Nuke our cached style */
800 invalidate_cached_style (layout);
801 g_assert (layout->one_style_cache == NULL);
806 gtk_text_layout_invalidate_all (GtkTextLayout *layout)
811 if (layout->buffer == NULL)
814 gtk_text_buffer_get_bounds (layout->buffer, &start, &end);
816 gtk_text_layout_invalidate (layout, &start, &end);
820 gtk_text_layout_invalidate_cache (GtkTextLayout *layout,
822 gboolean cursors_only)
824 if (layout->one_display_cache && line == layout->one_display_cache->line)
826 GtkTextLineDisplay *display = layout->one_display_cache;
830 g_slist_foreach (display->cursors, (GFunc)g_free, NULL);
831 g_slist_free (display->cursors);
832 display->cursors = NULL;
833 display->cursors_invalid = TRUE;
834 display->has_block_cursor = FALSE;
838 layout->one_display_cache = NULL;
839 gtk_text_layout_free_line_display (layout, display);
844 /* Now invalidate the paragraph containing the cursor
847 gtk_text_layout_invalidate_cursor_line (GtkTextLayout *layout,
848 gboolean cursors_only)
850 GtkTextLayoutPrivate *priv = GTK_TEXT_LAYOUT_GET_PRIVATE (layout);
851 GtkTextLineData *line_data;
853 if (priv->cursor_line == NULL)
856 line_data = _gtk_text_line_get_data (priv->cursor_line, layout);
860 gtk_text_layout_invalidate_cache (layout, priv->cursor_line, TRUE);
863 gtk_text_layout_invalidate_cache (layout, priv->cursor_line, FALSE);
864 _gtk_text_line_invalidate_wrap (priv->cursor_line, line_data);
867 gtk_text_layout_invalidated (layout);
872 gtk_text_layout_update_cursor_line(GtkTextLayout *layout)
874 GtkTextLayoutPrivate *priv = GTK_TEXT_LAYOUT_GET_PRIVATE (layout);
877 gtk_text_buffer_get_iter_at_mark (layout->buffer, &iter,
878 gtk_text_buffer_get_insert (layout->buffer));
880 priv->cursor_line = _gtk_text_iter_get_text_line (&iter);
884 gtk_text_layout_real_invalidate (GtkTextLayout *layout,
885 const GtkTextIter *start,
886 const GtkTextIter *end)
889 GtkTextLine *last_line;
891 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
892 g_return_if_fail (layout->wrap_loop_count == 0);
894 /* Because we may be invalidating a mark, it's entirely possible
895 * that gtk_text_iter_equal (start, end) in which case we
896 * should still invalidate the line they are both on. i.e.
897 * we always invalidate the line with "start" even
898 * if there's an empty range.
902 gtk_text_view_index_spew (start_index, "invalidate start");
903 gtk_text_view_index_spew (end_index, "invalidate end");
906 last_line = _gtk_text_iter_get_text_line (end);
907 line = _gtk_text_iter_get_text_line (start);
911 GtkTextLineData *line_data = _gtk_text_line_get_data (line, layout);
913 gtk_text_layout_invalidate_cache (layout, line, FALSE);
916 _gtk_text_line_invalidate_wrap (line, line_data);
918 if (line == last_line)
921 line = _gtk_text_line_next_excluding_last (line);
924 gtk_text_layout_invalidated (layout);
928 gtk_text_layout_real_invalidate_cursors (GtkTextLayout *layout,
929 const GtkTextIter *start,
930 const GtkTextIter *end)
932 /* Check if the range intersects our cached line display,
933 * and invalidate the cached line if so.
935 if (layout->one_display_cache)
937 GtkTextIter line_start, line_end;
938 GtkTextLine *line = layout->one_display_cache->line;
940 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
941 &line_start, line, 0);
942 line_end = line_start;
943 if (!gtk_text_iter_ends_line (&line_end))
944 gtk_text_iter_forward_to_line_end (&line_end);
946 if (gtk_text_iter_compare (start, end) > 0)
948 const GtkTextIter *tmp = start;
953 if (gtk_text_iter_compare (&line_start, end) <= 0 &&
954 gtk_text_iter_compare (start, &line_end) <= 0)
956 gtk_text_layout_invalidate_cache (layout, line, TRUE);
960 gtk_text_layout_invalidated (layout);
964 gtk_text_layout_real_free_line_data (GtkTextLayout *layout,
966 GtkTextLineData *line_data)
968 gtk_text_layout_invalidate_cache (layout, line, FALSE);
974 * gtk_text_layout_is_valid:
975 * @layout: a #GtkTextLayout
977 * Check if there are any invalid regions in a #GtkTextLayout's buffer
979 * Return value: %TRUE if any invalid regions were found
982 gtk_text_layout_is_valid (GtkTextLayout *layout)
984 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), FALSE);
986 return _gtk_text_btree_is_valid (_gtk_text_buffer_get_btree (layout->buffer),
991 update_layout_size (GtkTextLayout *layout)
993 _gtk_text_btree_get_view_size (_gtk_text_buffer_get_btree (layout->buffer),
995 &layout->width, &layout->height);
999 * gtk_text_layout_validate_yrange:
1000 * @layout: a #GtkTextLayout
1001 * @anchor: iter pointing into a line that will be used as the
1003 * @y0_: offset from the top of the line pointed to by @anchor at
1004 * which to begin validation. (The offset here is in pixels
1005 * after validation.)
1006 * @y1_: offset from the top of the line pointed to by @anchor at
1007 * which to end validation. (The offset here is in pixels
1008 * after validation.)
1010 * Ensure that a region of a #GtkTextLayout is valid. The ::changed
1011 * signal will be emitted if any lines are validated.
1014 gtk_text_layout_validate_yrange (GtkTextLayout *layout,
1015 GtkTextIter *anchor,
1020 GtkTextLine *first_line = NULL;
1021 GtkTextLine *last_line = NULL;
1023 gint delta_height = 0;
1024 gint first_line_y = 0; /* Quiet GCC */
1025 gint last_line_y = 0; /* Quiet GCC */
1027 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
1034 /* Validate backwards from the anchor line to y0
1036 line = _gtk_text_iter_get_text_line (anchor);
1037 line = _gtk_text_line_previous (line);
1039 while (line && seen < -y0)
1041 GtkTextLineData *line_data = _gtk_text_line_get_data (line, layout);
1042 if (!line_data || !line_data->valid)
1044 gint old_height, new_height;
1046 old_height = line_data ? line_data->height : 0;
1048 _gtk_text_btree_validate_line (_gtk_text_buffer_get_btree (layout->buffer),
1050 line_data = _gtk_text_line_get_data (line, layout);
1052 new_height = line_data ? line_data->height : 0;
1054 delta_height += new_height - old_height;
1057 first_line_y = -seen - new_height;
1061 last_line_y = -seen;
1065 seen += line_data ? line_data->height : 0;
1066 line = _gtk_text_line_previous (line);
1069 /* Validate forwards to y1 */
1070 line = _gtk_text_iter_get_text_line (anchor);
1072 while (line && seen < y1)
1074 GtkTextLineData *line_data = _gtk_text_line_get_data (line, layout);
1075 if (!line_data || !line_data->valid)
1077 gint old_height, new_height;
1079 old_height = line_data ? line_data->height : 0;
1081 _gtk_text_btree_validate_line (_gtk_text_buffer_get_btree (layout->buffer),
1083 line_data = _gtk_text_line_get_data (line, layout);
1084 new_height = line_data ? line_data->height : 0;
1086 delta_height += new_height - old_height;
1091 first_line_y = seen;
1094 last_line_y = seen + new_height;
1097 seen += line_data ? line_data->height : 0;
1098 line = _gtk_text_line_next_excluding_last (line);
1101 /* If we found and validated any invalid lines, update size and
1102 * emit the changed signal
1108 update_layout_size (layout);
1110 line_top = _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
1111 first_line, layout);
1113 gtk_text_layout_emit_changed (layout,
1115 last_line_y - first_line_y - delta_height,
1116 last_line_y - first_line_y);
1121 * gtk_text_layout_validate:
1122 * @tree: a #GtkTextLayout
1123 * @max_pixels: the maximum number of pixels to validate. (No more
1124 * than one paragraph beyond this limit will be validated)
1126 * Validate regions of a #GtkTextLayout. The ::changed signal will
1127 * be emitted for each region validated.
1130 gtk_text_layout_validate (GtkTextLayout *layout,
1133 gint y, old_height, new_height;
1135 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
1137 while (max_pixels > 0 &&
1138 _gtk_text_btree_validate (_gtk_text_buffer_get_btree (layout->buffer),
1140 &y, &old_height, &new_height))
1142 max_pixels -= new_height;
1144 update_layout_size (layout);
1145 gtk_text_layout_emit_changed (layout, y, old_height, new_height);
1149 static GtkTextLineData*
1150 gtk_text_layout_real_wrap (GtkTextLayout *layout,
1153 GtkTextLineData *line_data)
1155 GtkTextLineDisplay *display;
1157 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), NULL);
1158 g_return_val_if_fail (line != NULL, NULL);
1160 if (line_data == NULL)
1162 line_data = _gtk_text_line_data_new (layout, line);
1163 _gtk_text_line_add_data (line, line_data);
1166 display = gtk_text_layout_get_line_display (layout, line, TRUE);
1167 line_data->width = display->width;
1168 line_data->height = display->height;
1169 line_data->valid = TRUE;
1170 gtk_text_layout_free_line_display (layout, display);
1176 * Layout utility functions
1179 /* If you get the style with get_style () you need to call
1180 release_style () to free it. */
1181 static GtkTextAttributes*
1182 get_style (GtkTextLayout *layout,
1185 GtkTextAttributes *style;
1187 /* If we have the one-style cache, then it means
1188 that we haven't seen a toggle since we filled in the
1191 if (layout->one_style_cache != NULL)
1193 gtk_text_attributes_ref (layout->one_style_cache);
1194 return layout->one_style_cache;
1197 g_assert (layout->one_style_cache == NULL);
1199 /* No tags, use default style */
1200 if (tags == NULL || tags->len == 0)
1202 /* One ref for the return value, one ref for the
1203 layout->one_style_cache reference */
1204 gtk_text_attributes_ref (layout->default_style);
1205 gtk_text_attributes_ref (layout->default_style);
1206 layout->one_style_cache = layout->default_style;
1208 return layout->default_style;
1211 style = gtk_text_attributes_new ();
1213 gtk_text_attributes_copy_values (layout->default_style,
1216 _gtk_text_attributes_fill_from_tags (style,
1217 (GtkTextTag**) tags->pdata,
1220 g_assert (style->refcount == 1);
1222 /* Leave this style as the last one seen */
1223 g_assert (layout->one_style_cache == NULL);
1224 gtk_text_attributes_ref (style); /* ref held by layout->one_style_cache */
1225 layout->one_style_cache = style;
1227 /* Returning yet another refcount */
1232 release_style (GtkTextLayout *layout,
1233 GtkTextAttributes *style)
1235 g_return_if_fail (style != NULL);
1236 g_return_if_fail (style->refcount > 0);
1238 gtk_text_attributes_unref (style);
1245 /* This function tries to optimize the case where a line
1246 is completely invisible */
1248 totally_invisible_line (GtkTextLayout *layout,
1252 GtkTextLineSegment *seg;
1255 /* Check if the first char is visible, if so we are partially visible.
1256 * Note that we have to check this since we don't know the current
1257 * invisible/noninvisible toggle state; this function can use the whole btree
1260 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
1263 if (!_gtk_text_btree_char_is_invisible (iter))
1267 seg = line->segments;
1271 if (seg->byte_count > 0)
1272 bytes += seg->byte_count;
1274 /* Note that these two tests can cause us to bail out
1275 * when we shouldn't, because a higher-priority tag
1276 * may override these settings. However the important
1277 * thing is to only invisible really-invisible lines, rather
1278 * than to invisible all really-invisible lines.
1281 else if (seg->type == >k_text_toggle_on_type)
1283 invalidate_cached_style (layout);
1285 /* Bail out if an elision-unsetting tag begins */
1286 if (seg->body.toggle.info->tag->invisible_set &&
1287 !seg->body.toggle.info->tag->values->invisible)
1290 else if (seg->type == >k_text_toggle_off_type)
1292 invalidate_cached_style (layout);
1294 /* Bail out if an elision-setting tag ends */
1295 if (seg->body.toggle.info->tag->invisible_set &&
1296 seg->body.toggle.info->tag->values->invisible)
1303 if (seg != NULL) /* didn't reach line end */
1310 set_para_values (GtkTextLayout *layout,
1311 PangoDirection base_dir,
1312 GtkTextAttributes *style,
1313 GtkTextLineDisplay *display)
1315 PangoAlignment pango_align = PANGO_ALIGN_LEFT;
1316 PangoWrapMode pango_wrap = PANGO_WRAP_WORD;
1320 /* If no base direction was found, then use the style direction */
1321 case PANGO_DIRECTION_NEUTRAL :
1322 display->direction = style->direction;
1324 /* Override the base direction */
1325 if (display->direction == GTK_TEXT_DIR_RTL)
1326 base_dir = PANGO_DIRECTION_RTL;
1328 base_dir = PANGO_DIRECTION_LTR;
1331 case PANGO_DIRECTION_RTL :
1332 display->direction = GTK_TEXT_DIR_RTL;
1335 display->direction = GTK_TEXT_DIR_LTR;
1339 if (display->direction == GTK_TEXT_DIR_RTL)
1340 display->layout = pango_layout_new (layout->rtl_context);
1342 display->layout = pango_layout_new (layout->ltr_context);
1344 switch (style->justification)
1346 case GTK_JUSTIFY_LEFT:
1347 pango_align = (base_dir == PANGO_DIRECTION_LTR) ? PANGO_ALIGN_LEFT : PANGO_ALIGN_RIGHT;
1349 case GTK_JUSTIFY_RIGHT:
1350 pango_align = (base_dir == PANGO_DIRECTION_LTR) ? PANGO_ALIGN_RIGHT : PANGO_ALIGN_LEFT;
1352 case GTK_JUSTIFY_CENTER:
1353 pango_align = PANGO_ALIGN_CENTER;
1355 case GTK_JUSTIFY_FILL:
1356 pango_align = (base_dir == PANGO_DIRECTION_LTR) ? PANGO_ALIGN_LEFT : PANGO_ALIGN_RIGHT;
1357 pango_layout_set_justify (display->layout, TRUE);
1360 g_assert_not_reached ();
1364 pango_layout_set_alignment (display->layout, pango_align);
1365 pango_layout_set_spacing (display->layout,
1366 style->pixels_inside_wrap * PANGO_SCALE);
1369 pango_layout_set_tabs (display->layout, style->tabs);
1371 display->top_margin = style->pixels_above_lines;
1372 display->height = style->pixels_above_lines + style->pixels_below_lines;
1373 display->bottom_margin = style->pixels_below_lines;
1374 display->left_margin = style->left_margin;
1375 display->right_margin = style->right_margin;
1377 display->x_offset = display->left_margin;
1379 pango_layout_set_indent (display->layout,
1380 style->indent * PANGO_SCALE);
1382 switch (style->wrap_mode)
1385 pango_wrap = PANGO_WRAP_CHAR;
1388 pango_wrap = PANGO_WRAP_WORD;
1391 case GTK_WRAP_WORD_CHAR:
1392 pango_wrap = PANGO_WRAP_WORD_CHAR;
1399 if (style->wrap_mode != GTK_WRAP_NONE)
1401 int layout_width = (layout->screen_width - display->left_margin - display->right_margin);
1402 pango_layout_set_width (display->layout, layout_width * PANGO_SCALE);
1403 pango_layout_set_wrap (display->layout, pango_wrap);
1406 display->total_width = MAX (layout->screen_width, layout->width) - display->left_margin - display->right_margin;
1408 if (style->pg_bg_color)
1409 display->pg_bg_color = gdk_color_copy (style->pg_bg_color);
1411 display->pg_bg_color = 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 g_slice_free (GtkTextAttrAppearance, appearance_attr);
1431 gtk_text_attr_appearance_compare (const PangoAttribute *attr1,
1432 const PangoAttribute *attr2)
1434 const GtkTextAppearance *appearance1 = &((const GtkTextAttrAppearance *)attr1)->appearance;
1435 const GtkTextAppearance *appearance2 = &((const GtkTextAttrAppearance *)attr2)->appearance;
1437 return (gdk_color_equal (&appearance1->fg_color, &appearance2->fg_color) &&
1438 gdk_color_equal (&appearance1->bg_color, &appearance2->bg_color) &&
1439 appearance1->underline == appearance2->underline &&
1440 appearance1->strikethrough == appearance2->strikethrough &&
1441 appearance1->draw_bg == appearance2->draw_bg);
1445 * gtk_text_attr_appearance_new:
1448 * Create a new font description attribute. (This attribute
1449 * allows setting family, style, weight, variant, stretch,
1450 * and size simultaneously.)
1454 static PangoAttribute *
1455 gtk_text_attr_appearance_new (const GtkTextAppearance *appearance)
1457 static PangoAttrClass klass = {
1459 gtk_text_attr_appearance_copy,
1460 gtk_text_attr_appearance_destroy,
1461 gtk_text_attr_appearance_compare
1464 GtkTextAttrAppearance *result;
1467 klass.type = gtk_text_attr_appearance_type =
1468 pango_attr_type_register ("GtkTextAttrAppearance");
1470 result = g_slice_new (GtkTextAttrAppearance);
1471 result->attr.klass = &klass;
1473 result->appearance = *appearance;
1475 return (PangoAttribute *)result;
1479 add_generic_attrs (GtkTextLayout *layout,
1480 GtkTextAppearance *appearance,
1482 PangoAttrList *attrs,
1487 PangoAttribute *attr;
1489 if (appearance->underline != PANGO_UNDERLINE_NONE)
1491 attr = pango_attr_underline_new (appearance->underline);
1493 attr->start_index = start;
1494 attr->end_index = start + byte_count;
1496 pango_attr_list_insert (attrs, attr);
1499 if (appearance->strikethrough)
1501 attr = pango_attr_strikethrough_new (appearance->strikethrough);
1503 attr->start_index = start;
1504 attr->end_index = start + byte_count;
1506 pango_attr_list_insert (attrs, attr);
1509 if (appearance->rise != 0)
1511 attr = pango_attr_rise_new (appearance->rise);
1513 attr->start_index = start;
1514 attr->end_index = start + byte_count;
1516 pango_attr_list_insert (attrs, attr);
1521 attr = gtk_text_attr_appearance_new (appearance);
1523 attr->start_index = start;
1524 attr->end_index = start + byte_count;
1526 ((GtkTextAttrAppearance *)attr)->appearance.is_text = is_text;
1528 pango_attr_list_insert (attrs, attr);
1533 add_text_attrs (GtkTextLayout *layout,
1534 GtkTextAttributes *style,
1536 PangoAttrList *attrs,
1540 PangoAttribute *attr;
1542 attr = pango_attr_font_desc_new (style->font);
1543 attr->start_index = start;
1544 attr->end_index = start + byte_count;
1546 pango_attr_list_insert (attrs, attr);
1548 if (style->font_scale != 1.0)
1550 attr = pango_attr_scale_new (style->font_scale);
1552 attr->start_index = start;
1553 attr->end_index = start + byte_count;
1555 pango_attr_list_insert (attrs, attr);
1560 add_pixbuf_attrs (GtkTextLayout *layout,
1561 GtkTextLineDisplay *display,
1562 GtkTextAttributes *style,
1563 GtkTextLineSegment *seg,
1564 PangoAttrList *attrs,
1567 PangoAttribute *attr;
1568 PangoRectangle logical_rect;
1569 GtkTextPixbuf *pixbuf = &seg->body.pixbuf;
1572 width = gdk_pixbuf_get_width (pixbuf->pixbuf);
1573 height = gdk_pixbuf_get_height (pixbuf->pixbuf);
1576 logical_rect.y = -height * PANGO_SCALE;
1577 logical_rect.width = width * PANGO_SCALE;
1578 logical_rect.height = height * PANGO_SCALE;
1580 attr = pango_attr_shape_new_with_data (&logical_rect, &logical_rect,
1581 pixbuf->pixbuf, NULL, NULL);
1582 attr->start_index = start;
1583 attr->end_index = start + seg->byte_count;
1584 pango_attr_list_insert (attrs, attr);
1588 add_child_attrs (GtkTextLayout *layout,
1589 GtkTextLineDisplay *display,
1590 GtkTextAttributes *style,
1591 GtkTextLineSegment *seg,
1592 PangoAttrList *attrs,
1595 PangoAttribute *attr;
1596 PangoRectangle logical_rect;
1604 tmp_list = seg->body.child.widgets;
1605 while (tmp_list != NULL)
1607 GtkWidget *child = tmp_list->data;
1609 if (_gtk_anchored_child_get_layout (child) == layout)
1614 gtk_widget_get_preferred_size (child, &req, NULL);
1617 height = req.height;
1624 tmp_list = g_slist_next (tmp_list);
1627 if (tmp_list == NULL)
1629 /* If tmp_list == NULL then there is no widget at this anchor in
1630 * this display; not an error. We make up an arbitrary size
1631 * to use, just so the programmer can see the blank spot.
1632 * We also put a NULL in the shaped objects list, to keep
1633 * the correspondence between the list and the shaped chars in
1634 * the layout. A bad hack, yes.
1644 logical_rect.y = -height * PANGO_SCALE;
1645 logical_rect.width = width * PANGO_SCALE;
1646 logical_rect.height = height * PANGO_SCALE;
1648 attr = pango_attr_shape_new_with_data (&logical_rect, &logical_rect,
1649 widget, NULL, NULL);
1650 attr->start_index = start;
1651 attr->end_index = start + seg->byte_count;
1652 pango_attr_list_insert (attrs, attr);
1657 * @layout: a #GtkTextLayout
1658 * @display: a #GtkTextLineDisplay
1659 * @insert_iter: iter pointing to the cursor location
1660 * @insert_index: cursor offset in the @display's layout, it may
1661 * be different from @insert_iter's offset in case when preedit
1662 * string is present.
1663 * @pos: location to store cursor position
1664 * @cursor_at_line_end: whether cursor is at the end of line
1666 * Checks whether layout should display block cursor at given position.
1667 * For this layout must be in overwrite mode and text at @insert_iter
1671 get_block_cursor (GtkTextLayout *layout,
1672 GtkTextLineDisplay *display,
1673 const GtkTextIter *insert_iter,
1676 gboolean *cursor_at_line_end)
1678 PangoRectangle pango_pos;
1680 if (layout->overwrite_mode &&
1681 gtk_text_iter_editable (insert_iter, TRUE) &&
1682 _gtk_text_util_get_block_cursor_location (display->layout,
1685 cursor_at_line_end))
1689 pos->x = PANGO_PIXELS (pango_pos.x);
1690 pos->y = PANGO_PIXELS (pango_pos.y);
1691 pos->width = PANGO_PIXELS (pango_pos.width);
1692 pos->height = PANGO_PIXELS (pango_pos.height);
1702 add_cursor (GtkTextLayout *layout,
1703 GtkTextLineDisplay *display,
1704 GtkTextLineSegment *seg,
1707 PangoRectangle strong_pos, weak_pos;
1708 GtkTextCursorDisplay *cursor = NULL; /* Quiet GCC */
1709 gboolean add_weak = FALSE;
1710 gboolean add_strong = FALSE;
1712 /* Hide insertion cursor when we have a selection or the layout
1713 * user has hidden the cursor.
1715 if (_gtk_text_btree_mark_is_insert (_gtk_text_buffer_get_btree (layout->buffer),
1716 seg->body.mark.obj) &&
1717 (!layout->cursor_visible ||
1718 gtk_text_buffer_get_selection_bounds (layout->buffer, NULL, NULL)))
1721 if (layout->overwrite_mode &&
1722 _gtk_text_btree_mark_is_insert (_gtk_text_buffer_get_btree (layout->buffer),
1723 seg->body.mark.obj))
1726 gboolean cursor_at_line_end;
1728 _gtk_text_btree_get_iter_at_mark (_gtk_text_buffer_get_btree (layout->buffer),
1729 &iter, seg->body.mark.obj);
1731 if (get_block_cursor (layout, display, &iter, start,
1732 &display->block_cursor,
1733 &cursor_at_line_end))
1735 display->has_block_cursor = TRUE;
1736 display->cursor_at_line_end = cursor_at_line_end;
1741 pango_layout_get_cursor_pos (display->layout, start, &strong_pos, &weak_pos);
1743 if (layout->cursor_direction == GTK_TEXT_DIR_NONE)
1748 else if (display->direction == layout->cursor_direction)
1755 cursor = g_new (GtkTextCursorDisplay, 1);
1757 cursor->x = PANGO_PIXELS (strong_pos.x);
1758 cursor->y = PANGO_PIXELS (strong_pos.y);
1759 cursor->height = PANGO_PIXELS (strong_pos.height);
1760 cursor->is_strong = TRUE;
1761 cursor->is_weak = (layout->cursor_direction == GTK_TEXT_DIR_NONE) ? FALSE : TRUE;
1762 display->cursors = g_slist_prepend (display->cursors, cursor);
1767 if (weak_pos.x == strong_pos.x && add_strong)
1768 cursor->is_weak = TRUE;
1771 cursor = g_new (GtkTextCursorDisplay, 1);
1773 cursor->x = PANGO_PIXELS (weak_pos.x);
1774 cursor->y = PANGO_PIXELS (weak_pos.y);
1775 cursor->height = PANGO_PIXELS (weak_pos.height);
1776 cursor->is_strong = (layout->cursor_direction == GTK_TEXT_DIR_NONE) ? FALSE : TRUE;
1777 cursor->is_weak = TRUE;
1778 display->cursors = g_slist_prepend (display->cursors, cursor);
1784 is_shape (PangoLayoutRun *run)
1786 GSList *tmp_list = run->item->analysis.extra_attrs;
1790 PangoAttribute *attr = tmp_list->data;
1792 if (attr->klass->type == PANGO_ATTR_SHAPE)
1795 tmp_list = tmp_list->next;
1802 allocate_child_widgets (GtkTextLayout *text_layout,
1803 GtkTextLineDisplay *display)
1805 PangoLayout *layout = display->layout;
1806 PangoLayoutIter *run_iter;
1808 run_iter = pango_layout_get_iter (layout);
1811 PangoLayoutRun *run = pango_layout_iter_get_run_readonly (run_iter);
1813 if (run && is_shape (run))
1816 GtkTextIter text_iter;
1817 GtkTextChildAnchor *anchor = NULL;
1818 GList *widgets = NULL;
1821 /* The pango iterator iterates in visual order.
1822 * We use the byte index to find the child widget.
1824 byte_index = pango_layout_iter_get_index (run_iter);
1825 line_display_index_to_iter (text_layout, display, &text_iter, byte_index, 0);
1826 anchor = gtk_text_iter_get_child_anchor (&text_iter);
1828 widgets = gtk_text_child_anchor_get_widgets (anchor);
1830 for (l = widgets; l; l = l->next)
1832 PangoRectangle extents;
1833 GtkWidget *child = l->data;
1835 if (_gtk_anchored_child_get_layout (child) == text_layout)
1838 /* We emit "allocate_child" with the x,y of
1839 * the widget with respect to the top of the line
1840 * and the left side of the buffer
1842 pango_layout_iter_get_run_extents (run_iter,
1846 g_signal_emit (text_layout,
1847 signals[ALLOCATE_CHILD],
1850 PANGO_PIXELS (extents.x) + display->x_offset,
1851 PANGO_PIXELS (extents.y) + display->top_margin);
1855 g_list_free (widgets);
1858 while (pango_layout_iter_next_run (run_iter));
1860 pango_layout_iter_free (run_iter);
1864 convert_color (GdkColor *result,
1865 PangoAttrColor *attr)
1867 result->red = attr->color.red;
1868 result->blue = attr->color.blue;
1869 result->green = attr->color.green;
1872 /* This function is used to convert the preedit string attributes, which are
1873 * standard PangoAttributes, into the custom attributes used by the text
1874 * widget and insert them into a attr list with a given offset.
1877 add_preedit_attrs (GtkTextLayout *layout,
1878 GtkTextAttributes *style,
1879 PangoAttrList *attrs,
1883 PangoAttrIterator *iter = pango_attr_list_get_iterator (layout->preedit_attrs);
1887 GtkTextAppearance appearance = style->appearance;
1888 PangoFontDescription *font_desc = pango_font_description_copy_static (style->font);
1889 PangoAttribute *insert_attr;
1890 GSList *extra_attrs = NULL;
1892 PangoLanguage *language;
1895 pango_attr_iterator_range (iter, &start, &end);
1897 if (end == G_MAXINT)
1898 end = layout->preedit_len;
1903 pango_attr_iterator_get_font (iter, font_desc, &language, &extra_attrs);
1905 tmp_list = extra_attrs;
1908 PangoAttribute *attr = tmp_list->data;
1910 switch (attr->klass->type)
1912 case PANGO_ATTR_FOREGROUND:
1913 convert_color (&appearance.fg_color, (PangoAttrColor *)attr);
1915 case PANGO_ATTR_BACKGROUND:
1916 convert_color (&appearance.bg_color, (PangoAttrColor *)attr);
1917 appearance.draw_bg = TRUE;
1919 case PANGO_ATTR_UNDERLINE:
1920 appearance.underline = ((PangoAttrInt *)attr)->value;
1922 case PANGO_ATTR_STRIKETHROUGH:
1923 appearance.strikethrough = ((PangoAttrInt *)attr)->value;
1925 case PANGO_ATTR_RISE:
1926 appearance.rise = ((PangoAttrInt *)attr)->value;
1932 pango_attribute_destroy (attr);
1933 tmp_list = tmp_list->next;
1936 g_slist_free (extra_attrs);
1938 insert_attr = pango_attr_font_desc_new (font_desc);
1939 insert_attr->start_index = start + offset;
1940 insert_attr->end_index = end + offset;
1942 pango_attr_list_insert (attrs, insert_attr);
1946 insert_attr = pango_attr_language_new (language);
1947 insert_attr->start_index = start + offset;
1948 insert_attr->end_index = end + offset;
1950 pango_attr_list_insert (attrs, insert_attr);
1953 add_generic_attrs (layout, &appearance, end - start,
1954 attrs, start + offset,
1957 pango_font_description_free (font_desc);
1959 while (pango_attr_iterator_next (iter));
1961 pango_attr_iterator_destroy (iter);
1964 /* Iterate over the line and fill in display->cursors.
1965 * It's a stripped copy of gtk_text_layout_get_line_display() */
1967 update_text_display_cursors (GtkTextLayout *layout,
1969 GtkTextLineDisplay *display)
1971 GtkTextLineSegment *seg;
1973 gint layout_byte_offset, buffer_byte_offset;
1974 GSList *cursor_byte_offsets = NULL;
1975 GSList *cursor_segs = NULL;
1976 GSList *tmp_list1, *tmp_list2;
1978 if (!display->cursors_invalid)
1981 display->cursors_invalid = FALSE;
1983 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
1986 /* Special-case optimization for completely
1987 * invisible lines; makes it faster to deal
1988 * with sequences of invisible lines.
1990 if (totally_invisible_line (layout, line, &iter))
1993 /* Iterate over segments */
1994 layout_byte_offset = 0; /* position in the layout text (includes preedit, does not include invisible text) */
1995 buffer_byte_offset = 0; /* position in the buffer line */
1996 seg = _gtk_text_iter_get_any_segment (&iter);
1999 /* Displayable segments */
2000 if (seg->type == >k_text_char_type ||
2001 seg->type == >k_text_pixbuf_type ||
2002 seg->type == >k_text_child_type)
2004 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
2006 buffer_byte_offset);
2008 if (!_gtk_text_btree_char_is_invisible (&iter))
2009 layout_byte_offset += seg->byte_count;
2011 buffer_byte_offset += seg->byte_count;
2015 else if (seg->type == >k_text_right_mark_type ||
2016 seg->type == >k_text_left_mark_type)
2018 gint cursor_offset = 0;
2020 /* At the insertion point, add the preedit string, if any */
2022 if (_gtk_text_btree_mark_is_insert (_gtk_text_buffer_get_btree (layout->buffer),
2023 seg->body.mark.obj))
2025 display->insert_index = layout_byte_offset;
2027 if (layout->preedit_len > 0)
2029 layout_byte_offset += layout->preedit_len;
2030 /* DO NOT increment the buffer byte offset for preedit */
2031 cursor_offset = layout->preedit_cursor - layout->preedit_len;
2035 /* Display visible marks */
2037 if (seg->body.mark.visible)
2039 cursor_byte_offsets = g_slist_prepend (cursor_byte_offsets,
2040 GINT_TO_POINTER (layout_byte_offset + cursor_offset));
2041 cursor_segs = g_slist_prepend (cursor_segs, seg);
2046 else if (seg->type == >k_text_toggle_on_type ||
2047 seg->type == >k_text_toggle_off_type)
2052 g_error ("Unknown segment type: %s", seg->type->name);
2057 tmp_list1 = cursor_byte_offsets;
2058 tmp_list2 = cursor_segs;
2061 add_cursor (layout, display, tmp_list2->data,
2062 GPOINTER_TO_INT (tmp_list1->data));
2063 tmp_list1 = tmp_list1->next;
2064 tmp_list2 = tmp_list2->next;
2066 g_slist_free (cursor_byte_offsets);
2067 g_slist_free (cursor_segs);
2070 /* Same as _gtk_text_btree_get_tags(), except it returns GPtrArray,
2071 * to be used in gtk_text_layout_get_line_display(). */
2073 get_tags_array_at_iter (GtkTextIter *iter)
2076 GPtrArray *array = NULL;
2079 tags = _gtk_text_btree_get_tags (iter, &n_tags);
2083 array = g_ptr_array_sized_new (n_tags);
2084 g_ptr_array_set_size (array, n_tags);
2085 memcpy (array->pdata, tags, n_tags * sizeof (GtkTextTag*));
2092 /* Add the tag to the array if it's not there already, and remove
2093 * it otherwise. It keeps the array sorted by tags priority. */
2095 tags_array_toggle_tag (GPtrArray *array,
2102 array = g_ptr_array_new ();
2104 tags = (GtkTextTag**) array->pdata;
2106 for (pos = 0; pos < array->len && tags[pos]->priority < tag->priority; pos++) ;
2108 if (pos < array->len && tags[pos] == tag)
2109 g_ptr_array_remove_index (array, pos);
2112 g_ptr_array_set_size (array, array->len + 1);
2113 if (pos < array->len - 1)
2114 memmove (array->pdata + pos + 1, array->pdata + pos,
2115 (array->len - pos - 1) * sizeof (GtkTextTag*));
2116 array->pdata[pos] = tag;
2122 GtkTextLineDisplay *
2123 gtk_text_layout_get_line_display (GtkTextLayout *layout,
2127 GtkTextLayoutPrivate *priv = GTK_TEXT_LAYOUT_GET_PRIVATE (layout);
2128 GtkTextLineDisplay *display;
2129 GtkTextLineSegment *seg;
2131 GtkTextAttributes *style;
2133 PangoAttrList *attrs;
2134 gint text_allocated, layout_byte_offset, buffer_byte_offset;
2135 PangoRectangle extents;
2136 gboolean para_values_set = FALSE;
2137 GSList *cursor_byte_offsets = NULL;
2138 GSList *cursor_segs = NULL;
2139 GSList *tmp_list1, *tmp_list2;
2140 gboolean saw_widget = FALSE;
2141 PangoDirection base_dir;
2143 gboolean initial_toggle_segments;
2145 g_return_val_if_fail (line != NULL, NULL);
2147 if (layout->one_display_cache)
2149 if (line == layout->one_display_cache->line &&
2150 (size_only || !layout->one_display_cache->size_only))
2153 update_text_display_cursors (layout, line, layout->one_display_cache);
2154 return layout->one_display_cache;
2158 GtkTextLineDisplay *tmp_display = layout->one_display_cache;
2159 layout->one_display_cache = NULL;
2160 gtk_text_layout_free_line_display (layout, tmp_display);
2164 DV (g_print ("creating one line display cache (%s)\n", G_STRLOC));
2166 display = g_new0 (GtkTextLineDisplay, 1);
2168 display->size_only = size_only;
2169 display->line = line;
2170 display->insert_index = -1;
2172 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
2175 /* Special-case optimization for completely
2176 * invisible lines; makes it faster to deal
2177 * with sequences of invisible lines.
2179 if (totally_invisible_line (layout, line, &iter))
2181 if (display->direction == GTK_TEXT_DIR_RTL)
2182 display->layout = pango_layout_new (layout->rtl_context);
2184 display->layout = pango_layout_new (layout->ltr_context);
2189 /* Find the bidi base direction */
2190 base_dir = line->dir_propagated_forward;
2191 if (base_dir == PANGO_DIRECTION_NEUTRAL)
2192 base_dir = line->dir_propagated_back;
2194 if (line == priv->cursor_line &&
2195 line->dir_strong == PANGO_DIRECTION_NEUTRAL)
2197 base_dir = (layout->keyboard_direction == GTK_TEXT_DIR_LTR) ?
2198 PANGO_DIRECTION_LTR : PANGO_DIRECTION_RTL;
2201 /* Allocate space for flat text for buffer
2203 text_allocated = _gtk_text_line_byte_count (line);
2204 text = g_malloc (text_allocated);
2206 attrs = pango_attr_list_new ();
2208 /* Iterate over segments, creating display chunks for them, and updating the tags array. */
2209 layout_byte_offset = 0; /* current length of layout text (includes preedit, does not include invisible text) */
2210 buffer_byte_offset = 0; /* position in the buffer line */
2211 seg = _gtk_text_iter_get_any_segment (&iter);
2212 tags = get_tags_array_at_iter (&iter);
2213 initial_toggle_segments = TRUE;
2216 /* Displayable segments */
2217 if (seg->type == >k_text_char_type ||
2218 seg->type == >k_text_pixbuf_type ||
2219 seg->type == >k_text_child_type)
2221 style = get_style (layout, tags);
2222 initial_toggle_segments = FALSE;
2224 /* We have to delay setting the paragraph values until we
2225 * hit the first pixbuf or text segment because toggles at
2226 * the beginning of the paragraph should affect the
2227 * paragraph-global values
2229 if (!para_values_set)
2231 set_para_values (layout, base_dir, style, display);
2232 para_values_set = TRUE;
2235 /* First see if the chunk is invisible, and ignore it if so. Tk
2236 * looked at tabs, wrap mode, etc. before doing this, but
2237 * that made no sense to me, so I am just skipping the
2240 if (!style->invisible)
2242 if (seg->type == >k_text_char_type)
2244 /* We don't want to split segments because of marks,
2245 * so we scan forward for more segments only
2246 * separated from us by marks. In theory, we should
2247 * also merge segments with identical styles, even
2248 * if there are toggles in-between
2252 GtkTextLineSegment *prev_seg = NULL;
2256 if (seg->type == >k_text_char_type)
2258 memcpy (text + layout_byte_offset, seg->body.chars, seg->byte_count);
2259 layout_byte_offset += seg->byte_count;
2260 buffer_byte_offset += seg->byte_count;
2261 bytes += seg->byte_count;
2263 else if (seg->type == >k_text_right_mark_type ||
2264 seg->type == >k_text_left_mark_type)
2266 /* If we have preedit string, break out of this loop - we'll almost
2267 * certainly have different attributes on the preedit string
2270 if (layout->preedit_len > 0 &&
2271 _gtk_text_btree_mark_is_insert (_gtk_text_buffer_get_btree (layout->buffer),
2272 seg->body.mark.obj))
2275 if (seg->body.mark.visible)
2277 cursor_byte_offsets = g_slist_prepend (cursor_byte_offsets, GINT_TO_POINTER (layout_byte_offset));
2278 cursor_segs = g_slist_prepend (cursor_segs, seg);
2279 if (_gtk_text_btree_mark_is_insert (_gtk_text_buffer_get_btree (layout->buffer),
2280 seg->body.mark.obj))
2281 display->insert_index = layout_byte_offset;
2291 seg = prev_seg; /* Back up one */
2292 add_generic_attrs (layout, &style->appearance,
2294 attrs, layout_byte_offset - bytes,
2296 add_text_attrs (layout, style, bytes, attrs,
2297 layout_byte_offset - bytes, size_only);
2299 else if (seg->type == >k_text_pixbuf_type)
2301 add_generic_attrs (layout,
2304 attrs, layout_byte_offset,
2306 add_pixbuf_attrs (layout, display, style,
2307 seg, attrs, layout_byte_offset);
2308 memcpy (text + layout_byte_offset, _gtk_text_unknown_char_utf8,
2310 layout_byte_offset += seg->byte_count;
2311 buffer_byte_offset += seg->byte_count;
2313 else if (seg->type == >k_text_child_type)
2317 add_generic_attrs (layout, &style->appearance,
2319 attrs, layout_byte_offset,
2321 add_child_attrs (layout, display, style,
2322 seg, attrs, layout_byte_offset);
2323 memcpy (text + layout_byte_offset, _gtk_text_unknown_char_utf8,
2325 layout_byte_offset += seg->byte_count;
2326 buffer_byte_offset += seg->byte_count;
2330 /* We don't know this segment type */
2331 g_assert_not_reached ();
2334 } /* if (segment was visible) */
2337 /* Invisible segment */
2338 buffer_byte_offset += seg->byte_count;
2341 release_style (layout, style);
2345 else if (seg->type == >k_text_toggle_on_type ||
2346 seg->type == >k_text_toggle_off_type)
2348 /* Style may have changed, drop our
2349 current cached style */
2350 invalidate_cached_style (layout);
2351 /* Add the tag only after we have seen some non-toggle non-mark segment,
2352 * otherwise the tag is already accounted for by _gtk_text_btree_get_tags(). */
2353 if (!initial_toggle_segments)
2354 tags = tags_array_toggle_tag (tags, seg->body.toggle.info->tag);
2358 else if (seg->type == >k_text_right_mark_type ||
2359 seg->type == >k_text_left_mark_type)
2361 gint cursor_offset = 0;
2363 /* At the insertion point, add the preedit string, if any */
2365 if (_gtk_text_btree_mark_is_insert (_gtk_text_buffer_get_btree (layout->buffer),
2366 seg->body.mark.obj))
2368 display->insert_index = layout_byte_offset;
2370 if (layout->preedit_len > 0)
2372 text_allocated += layout->preedit_len;
2373 text = g_realloc (text, text_allocated);
2375 style = get_style (layout, tags);
2376 add_preedit_attrs (layout, style, attrs, layout_byte_offset, size_only);
2377 release_style (layout, style);
2379 memcpy (text + layout_byte_offset, layout->preedit_string, layout->preedit_len);
2380 layout_byte_offset += layout->preedit_len;
2381 /* DO NOT increment the buffer byte offset for preedit */
2383 cursor_offset = layout->preedit_cursor - layout->preedit_len;
2388 /* Display visible marks */
2390 if (seg->body.mark.visible)
2392 cursor_byte_offsets = g_slist_prepend (cursor_byte_offsets,
2393 GINT_TO_POINTER (layout_byte_offset + cursor_offset));
2394 cursor_segs = g_slist_prepend (cursor_segs, seg);
2399 g_error ("Unknown segment type: %s", seg->type->name);
2404 if (!para_values_set)
2406 style = get_style (layout, tags);
2407 set_para_values (layout, base_dir, style, display);
2408 release_style (layout, style);
2411 /* Pango doesn't want the trailing paragraph delimiters */
2414 /* Only one character has type G_UNICODE_PARAGRAPH_SEPARATOR in
2415 * Unicode 3.0; update this if that changes.
2417 #define PARAGRAPH_SEPARATOR 0x2029
2420 if (layout_byte_offset > 0)
2422 const char *prev = g_utf8_prev_char (text + layout_byte_offset);
2423 ch = g_utf8_get_char (prev);
2424 if (ch == PARAGRAPH_SEPARATOR || ch == '\r' || ch == '\n')
2425 layout_byte_offset = prev - text; /* chop off */
2427 if (ch == '\n' && layout_byte_offset > 0)
2429 /* Possibly chop a CR as well */
2430 prev = g_utf8_prev_char (text + layout_byte_offset);
2432 --layout_byte_offset;
2437 pango_layout_set_text (display->layout, text, layout_byte_offset);
2438 pango_layout_set_attributes (display->layout, attrs);
2440 tmp_list1 = cursor_byte_offsets;
2441 tmp_list2 = cursor_segs;
2444 add_cursor (layout, display, tmp_list2->data,
2445 GPOINTER_TO_INT (tmp_list1->data));
2446 tmp_list1 = tmp_list1->next;
2447 tmp_list2 = tmp_list2->next;
2449 g_slist_free (cursor_byte_offsets);
2450 g_slist_free (cursor_segs);
2452 pango_layout_get_extents (display->layout, NULL, &extents);
2454 display->width = PIXEL_BOUND (extents.width) + display->left_margin + display->right_margin;
2455 display->height += PANGO_PIXELS (extents.height);
2457 /* If we aren't wrapping, we need to do the alignment of each
2458 * paragraph ourselves.
2460 if (pango_layout_get_width (display->layout) < 0)
2462 gint excess = display->total_width - display->width;
2464 switch (pango_layout_get_alignment (display->layout))
2466 case PANGO_ALIGN_LEFT:
2468 case PANGO_ALIGN_CENTER:
2469 display->x_offset += excess / 2;
2471 case PANGO_ALIGN_RIGHT:
2472 display->x_offset += excess;
2477 /* Free this if we aren't in a loop */
2478 if (layout->wrap_loop_count == 0)
2479 invalidate_cached_style (layout);
2482 pango_attr_list_unref (attrs);
2484 g_ptr_array_free (tags, TRUE);
2486 layout->one_display_cache = display;
2489 allocate_child_widgets (layout, display);
2495 gtk_text_layout_free_line_display (GtkTextLayout *layout,
2496 GtkTextLineDisplay *display)
2498 if (display != layout->one_display_cache)
2500 if (display->layout)
2501 g_object_unref (display->layout);
2503 if (display->cursors)
2505 g_slist_foreach (display->cursors, (GFunc)g_free, NULL);
2506 g_slist_free (display->cursors);
2509 if (display->pg_bg_color)
2510 gdk_color_free (display->pg_bg_color);
2516 /* Functions to convert iter <=> index for the line of a GtkTextLineDisplay
2517 * taking into account the preedit string and invisible text if necessary.
2520 line_display_iter_to_index (GtkTextLayout *layout,
2521 GtkTextLineDisplay *display,
2522 const GtkTextIter *iter)
2526 g_return_val_if_fail (_gtk_text_iter_get_text_line (iter) == display->line, 0);
2528 index = gtk_text_iter_get_visible_line_index (iter);
2530 if (layout->preedit_len > 0 && display->insert_index >= 0)
2532 if (index >= display->insert_index)
2533 index += layout->preedit_len;
2540 line_display_index_to_iter (GtkTextLayout *layout,
2541 GtkTextLineDisplay *display,
2546 g_return_if_fail (!_gtk_text_line_is_last (display->line,
2547 _gtk_text_buffer_get_btree (layout->buffer)));
2549 if (layout->preedit_len > 0 && display->insert_index >= 0)
2551 if (index >= display->insert_index + layout->preedit_len)
2552 index -= layout->preedit_len;
2553 else if (index > display->insert_index)
2555 index = display->insert_index;
2560 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
2561 iter, display->line, 0);
2563 gtk_text_iter_set_visible_line_index (iter, index);
2565 if (_gtk_text_iter_get_text_line (iter) != display->line)
2567 /* Clamp to end of line - really this clamping should have been done
2568 * before here, maybe in Pango, this is a broken band-aid I think
2570 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
2571 iter, display->line, 0);
2573 if (!gtk_text_iter_ends_line (iter))
2574 gtk_text_iter_forward_to_line_end (iter);
2577 gtk_text_iter_forward_chars (iter, trailing);
2581 get_line_at_y (GtkTextLayout *layout,
2588 if (y > layout->height)
2591 *line = _gtk_text_btree_find_line_by_y (_gtk_text_buffer_get_btree (layout->buffer),
2592 layout, y, line_top);
2595 *line = _gtk_text_btree_get_end_iter_line (_gtk_text_buffer_get_btree (layout->buffer));
2599 _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
2605 * gtk_text_layout_get_line_at_y:
2606 * @layout: a #GtkLayout
2607 * @target_iter: the iterator in which the result is stored
2608 * @y: the y positition
2609 * @line_top: location to store the y coordinate of the
2610 * top of the line. (Can by %NULL)
2612 * Get the iter at the beginning of the line which is displayed
2616 gtk_text_layout_get_line_at_y (GtkTextLayout *layout,
2617 GtkTextIter *target_iter,
2623 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
2624 g_return_if_fail (target_iter != NULL);
2626 get_line_at_y (layout, y, &line, line_top);
2627 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
2628 target_iter, line, 0);
2632 gtk_text_layout_get_iter_at_pixel (GtkTextLayout *layout,
2633 GtkTextIter *target_iter,
2639 gtk_text_layout_get_iter_at_position (layout, target_iter, &trailing, x, y);
2641 gtk_text_iter_forward_chars (target_iter, trailing);
2644 void gtk_text_layout_get_iter_at_position (GtkTextLayout *layout,
2645 GtkTextIter *target_iter,
2653 GtkTextLineDisplay *display;
2655 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
2656 g_return_if_fail (target_iter != NULL);
2658 get_line_at_y (layout, y, &line, &line_top);
2660 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2662 x -= display->x_offset;
2663 y -= line_top + display->top_margin;
2665 /* If we are below the layout, position the cursor at the last character
2668 if (y > display->height - display->top_margin - display->bottom_margin)
2670 byte_index = _gtk_text_line_byte_count (line);
2676 /* Ignore the "outside" return value from pango. Pango is doing
2677 * the right thing even if we are outside the layout in the
2680 pango_layout_xy_to_index (display->layout, x * PANGO_SCALE, y * PANGO_SCALE,
2681 &byte_index, trailing);
2684 line_display_index_to_iter (layout, display, target_iter, byte_index, 0);
2686 gtk_text_layout_free_line_display (layout, display);
2691 * gtk_text_layout_get_cursor_locations:
2692 * @layout: a #GtkTextLayout
2693 * @iter: a #GtkTextIter
2694 * @strong_pos: (allow-none): location to store the strong cursor position (may be %NULL)
2695 * @weak_pos: (allow-none): location to store the weak cursor position (may be %NULL)
2697 * Given an iterator within a text layout, determine the positions of the
2698 * strong and weak cursors if the insertion point is at that
2699 * iterator. The position of each cursor is stored as a zero-width
2700 * rectangle. The strong cursor location is the location where
2701 * characters of the directionality equal to the base direction of the
2702 * paragraph are inserted. The weak cursor location is the location
2703 * where characters of the directionality opposite to the base
2704 * direction of the paragraph are inserted.
2707 gtk_text_layout_get_cursor_locations (GtkTextLayout *layout,
2709 GdkRectangle *strong_pos,
2710 GdkRectangle *weak_pos)
2713 GtkTextLineDisplay *display;
2716 GtkTextIter insert_iter;
2718 PangoRectangle pango_strong_pos;
2719 PangoRectangle pango_weak_pos;
2721 g_return_if_fail (layout != NULL);
2722 g_return_if_fail (iter != NULL);
2724 line = _gtk_text_iter_get_text_line (iter);
2725 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2726 index = line_display_iter_to_index (layout, display, iter);
2728 line_top = _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
2731 gtk_text_buffer_get_iter_at_mark (layout->buffer, &insert_iter,
2732 gtk_text_buffer_get_insert (layout->buffer));
2734 if (gtk_text_iter_equal (iter, &insert_iter))
2735 index += layout->preedit_cursor - layout->preedit_len;
2737 pango_layout_get_cursor_pos (display->layout, index,
2738 strong_pos ? &pango_strong_pos : NULL,
2739 weak_pos ? &pango_weak_pos : NULL);
2743 strong_pos->x = display->x_offset + pango_strong_pos.x / PANGO_SCALE;
2744 strong_pos->y = line_top + display->top_margin + pango_strong_pos.y / PANGO_SCALE;
2745 strong_pos->width = 0;
2746 strong_pos->height = pango_strong_pos.height / PANGO_SCALE;
2751 weak_pos->x = display->x_offset + pango_weak_pos.x / PANGO_SCALE;
2752 weak_pos->y = line_top + display->top_margin + pango_weak_pos.y / PANGO_SCALE;
2753 weak_pos->width = 0;
2754 weak_pos->height = pango_weak_pos.height / PANGO_SCALE;
2757 gtk_text_layout_free_line_display (layout, display);
2761 * _gtk_text_layout_get_block_cursor:
2762 * @layout: a #GtkTextLayout
2763 * @pos: a #GdkRectangle to store block cursor position
2765 * If layout is to display a block cursor, calculates its position
2766 * and returns %TRUE. Otherwise it returns %FALSE. In case when
2767 * cursor is visible, it simply returns the position stored in
2768 * the line display, otherwise it has to compute the position
2769 * (see get_block_cursor()).
2772 _gtk_text_layout_get_block_cursor (GtkTextLayout *layout,
2776 GtkTextLineDisplay *display;
2779 gboolean block = FALSE;
2781 g_return_val_if_fail (layout != NULL, FALSE);
2783 gtk_text_buffer_get_iter_at_mark (layout->buffer, &iter,
2784 gtk_text_buffer_get_insert (layout->buffer));
2785 line = _gtk_text_iter_get_text_line (&iter);
2786 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2788 if (display->has_block_cursor)
2791 rect = display->block_cursor;
2795 gint index = display->insert_index;
2798 index = gtk_text_iter_get_line_index (&iter);
2800 if (get_block_cursor (layout, display, &iter, index, &rect, NULL))
2808 line_top = _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
2812 pos->x += display->x_offset;
2813 pos->y += line_top + display->top_margin;
2816 gtk_text_layout_free_line_display (layout, display);
2821 * gtk_text_layout_get_line_yrange:
2822 * @layout: a #GtkTextLayout
2823 * @iter: a #GtkTextIter
2824 * @y: location to store the top of the paragraph in pixels,
2826 * @height location to store the height of the paragraph in pixels,
2829 * Find the range of y coordinates for the paragraph containing
2833 gtk_text_layout_get_line_yrange (GtkTextLayout *layout,
2834 const GtkTextIter *iter,
2840 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
2841 g_return_if_fail (_gtk_text_iter_get_btree (iter) == _gtk_text_buffer_get_btree (layout->buffer));
2843 line = _gtk_text_iter_get_text_line (iter);
2846 *y = _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
2850 GtkTextLineData *line_data = _gtk_text_line_get_data (line, layout);
2852 *height = line_data->height;
2859 * _gtk_text_layout_get_line_xrange:
2860 * @layout: a #GtkTextLayout
2861 * @iter: a #GtkTextIter
2862 * @x: location to store the top of the paragraph in pixels,
2864 * @width location to store the height of the paragraph in pixels,
2867 * Find the range of X coordinates for the paragraph containing
2868 * the given iter. Private for 2.0 due to API freeze, could
2869 * be made public for 2.2.
2872 _gtk_text_layout_get_line_xrange (GtkTextLayout *layout,
2873 const GtkTextIter *iter,
2879 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
2880 g_return_if_fail (_gtk_text_iter_get_btree (iter) == _gtk_text_buffer_get_btree (layout->buffer));
2882 line = _gtk_text_iter_get_text_line (iter);
2885 *x = 0; /* FIXME This is wrong; should represent the first available cursor position */
2889 GtkTextLineData *line_data = _gtk_text_line_get_data (line, layout);
2891 *width = line_data->width;
2898 gtk_text_layout_get_iter_location (GtkTextLayout *layout,
2899 const GtkTextIter *iter,
2902 PangoRectangle pango_rect;
2905 GtkTextLineDisplay *display;
2909 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
2910 g_return_if_fail (_gtk_text_iter_get_btree (iter) == _gtk_text_buffer_get_btree (layout->buffer));
2911 g_return_if_fail (rect != NULL);
2913 tree = _gtk_text_iter_get_btree (iter);
2914 line = _gtk_text_iter_get_text_line (iter);
2916 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2918 rect->y = _gtk_text_btree_find_line_top (tree, line, layout);
2920 x_offset = display->x_offset * PANGO_SCALE;
2922 byte_index = gtk_text_iter_get_line_index (iter);
2924 pango_layout_index_to_pos (display->layout, byte_index, &pango_rect);
2926 rect->x = PANGO_PIXELS (x_offset + pango_rect.x);
2927 rect->y += PANGO_PIXELS (pango_rect.y) + display->top_margin;
2928 rect->width = PANGO_PIXELS (pango_rect.width);
2929 rect->height = PANGO_PIXELS (pango_rect.height);
2931 gtk_text_layout_free_line_display (layout, display);
2936 /* Find the iter for the logical beginning of the first display line whose
2937 * top y is >= y. If none exists, move the iter to the logical beginning
2938 * of the last line in the buffer.
2941 find_display_line_below (GtkTextLayout *layout,
2945 GtkTextLine *line, *next;
2946 GtkTextLine *found_line = NULL;
2948 gint found_byte = 0;
2950 line = _gtk_text_btree_find_line_by_y (_gtk_text_buffer_get_btree (layout->buffer),
2951 layout, y, &line_top);
2955 _gtk_text_btree_get_end_iter_line (_gtk_text_buffer_get_btree (layout->buffer));
2958 _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
2962 while (line && !found_line)
2964 GtkTextLineDisplay *display = gtk_text_layout_get_line_display (layout, line, FALSE);
2965 PangoLayoutIter *layout_iter;
2967 layout_iter = pango_layout_get_iter (display->layout);
2969 line_top += display->top_margin;
2973 gint first_y, last_y;
2974 PangoLayoutLine *layout_line = pango_layout_iter_get_line_readonly (layout_iter);
2976 found_byte = layout_line->start_index;
2984 pango_layout_iter_get_line_yrange (layout_iter, &first_y, &last_y);
2985 line_top += (last_y - first_y) / PANGO_SCALE;
2987 while (pango_layout_iter_next_line (layout_iter));
2989 pango_layout_iter_free (layout_iter);
2991 line_top += display->bottom_margin;
2992 gtk_text_layout_free_line_display (layout, display);
2994 next = _gtk_text_line_next_excluding_last (line);
3001 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
3002 iter, found_line, found_byte);
3005 /* Find the iter for the logical beginning of the last display line whose
3006 * top y is >= y. If none exists, move the iter to the logical beginning
3007 * of the first line in the buffer.
3010 find_display_line_above (GtkTextLayout *layout,
3015 GtkTextLine *found_line = NULL;
3017 gint found_byte = 0;
3019 line = _gtk_text_btree_find_line_by_y (_gtk_text_buffer_get_btree (layout->buffer), layout, y, &line_top);
3022 line = _gtk_text_btree_get_end_iter_line (_gtk_text_buffer_get_btree (layout->buffer));
3024 line_top = _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer), line, layout);
3027 while (line && !found_line)
3029 GtkTextLineDisplay *display = gtk_text_layout_get_line_display (layout, line, FALSE);
3030 PangoRectangle logical_rect;
3031 PangoLayoutIter *layout_iter;
3034 layout_iter = pango_layout_get_iter (display->layout);
3036 line_top -= display->top_margin + display->bottom_margin;
3037 pango_layout_iter_get_layout_extents (layout_iter, NULL, &logical_rect);
3038 line_top -= logical_rect.height / PANGO_SCALE;
3040 tmp_top = line_top + display->top_margin;
3044 gint first_y, last_y;
3045 PangoLayoutLine *layout_line = pango_layout_iter_get_line_readonly (layout_iter);
3047 found_byte = layout_line->start_index;
3049 pango_layout_iter_get_line_yrange (layout_iter, &first_y, &last_y);
3051 tmp_top -= (last_y - first_y) / PANGO_SCALE;
3056 pango_layout_iter_free (layout_iter);
3060 while (pango_layout_iter_next_line (layout_iter));
3062 pango_layout_iter_free (layout_iter);
3064 gtk_text_layout_free_line_display (layout, display);
3066 line = _gtk_text_line_previous (line);
3072 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
3073 iter, found_line, found_byte);
3075 gtk_text_buffer_get_iter_at_offset (layout->buffer, iter, 0);
3079 * gtk_text_layout_clamp_iter_to_vrange:
3080 * @layout: a #GtkTextLayout
3081 * @iter: a #GtkTextIter
3082 * @top: the top of the range
3083 * @bottom: the bottom the range
3085 * If the iterator is not fully in the range @top <= y < @bottom,
3086 * then, if possible, move it the minimum distance so that the
3087 * iterator in this range.
3089 * Returns: %TRUE if the iterator was moved, otherwise %FALSE.
3092 gtk_text_layout_clamp_iter_to_vrange (GtkTextLayout *layout,
3097 GdkRectangle iter_rect;
3099 gtk_text_layout_get_iter_location (layout, iter, &iter_rect);
3101 /* If the iter is at least partially above the range, put the iter
3102 * at the first fully visible line after the range.
3104 if (iter_rect.y < top)
3106 find_display_line_below (layout, iter, top);
3110 /* Otherwise, if the iter is at least partially below the screen, put the
3111 * iter on the last logical position of the last completely visible
3114 else if (iter_rect.y + iter_rect.height > bottom)
3116 find_display_line_above (layout, iter, bottom);
3125 * gtk_text_layout_move_iter_to_previous_line:
3126 * @layout: a #GtkLayout
3127 * @iter: a #GtkTextIter
3129 * Move the iterator to the beginning of the previous line. The lines
3130 * of a wrapped paragraph are treated as distinct for this operation.
3133 gtk_text_layout_move_iter_to_previous_line (GtkTextLayout *layout,
3137 GtkTextLineDisplay *display;
3140 PangoLayoutLine *layout_line;
3142 gboolean update_byte = FALSE;
3144 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), FALSE);
3145 g_return_val_if_fail (iter != NULL, FALSE);
3150 line = _gtk_text_iter_get_text_line (iter);
3151 display = gtk_text_layout_get_line_display (layout, line, FALSE);
3152 line_byte = line_display_iter_to_index (layout, display, iter);
3154 /* If display->height == 0 then the line is invisible, so don't
3157 while (display->height == 0)
3159 GtkTextLine *prev_line;
3161 prev_line = _gtk_text_line_previous (line);
3163 if (prev_line == NULL)
3165 line_display_index_to_iter (layout, display, iter, 0, 0);
3169 gtk_text_layout_free_line_display (layout, display);
3172 display = gtk_text_layout_get_line_display (layout, prev_line, FALSE);
3176 tmp_list = pango_layout_get_lines_readonly (display->layout);
3177 layout_line = tmp_list->data;
3181 line_byte = layout_line->start_index + layout_line->length;
3184 if (line_byte < layout_line->length || !tmp_list->next) /* first line of paragraph */
3186 GtkTextLine *prev_line;
3188 prev_line = _gtk_text_line_previous (line);
3190 /* first line of the whole buffer, do not move the iter and return FALSE */
3191 if (prev_line == NULL)
3196 gtk_text_layout_free_line_display (layout, display);
3198 display = gtk_text_layout_get_line_display (layout, prev_line, FALSE);
3200 if (display->height > 0)
3202 tmp_list = g_slist_last (pango_layout_get_lines_readonly (display->layout));
3203 layout_line = tmp_list->data;
3205 line_display_index_to_iter (layout, display, iter,
3206 layout_line->start_index + layout_line->length, 0);
3210 prev_line = _gtk_text_line_previous (prev_line);
3215 gint prev_offset = layout_line->start_index;
3217 tmp_list = tmp_list->next;
3220 layout_line = tmp_list->data;
3222 if (line_byte < layout_line->start_index + layout_line->length ||
3225 line_display_index_to_iter (layout, display, iter, prev_offset, 0);
3229 prev_offset = layout_line->start_index;
3230 tmp_list = tmp_list->next;
3236 gtk_text_layout_free_line_display (layout, display);
3239 !gtk_text_iter_equal (iter, &orig) &&
3240 !gtk_text_iter_is_end (iter);
3244 * gtk_text_layout_move_iter_to_next_line:
3245 * @layout: a #GtkLayout
3246 * @iter: a #GtkTextIter
3248 * Move the iterator to the beginning of the next line. The
3249 * lines of a wrapped paragraph are treated as distinct for
3253 gtk_text_layout_move_iter_to_next_line (GtkTextLayout *layout,
3257 GtkTextLineDisplay *display;
3260 gboolean found = FALSE;
3261 gboolean found_after = FALSE;
3262 gboolean first = TRUE;
3264 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), FALSE);
3265 g_return_val_if_fail (iter != NULL, FALSE);
3269 line = _gtk_text_iter_get_text_line (iter);
3271 while (line && !found_after)
3275 display = gtk_text_layout_get_line_display (layout, line, FALSE);
3277 if (display->height == 0)
3282 line_byte = line_display_iter_to_index (layout, display, iter);
3288 tmp_list = pango_layout_get_lines_readonly (display->layout);
3289 while (tmp_list && !found_after)
3291 PangoLayoutLine *layout_line = tmp_list->data;
3295 line_display_index_to_iter (layout, display, iter,
3296 layout_line->start_index, 0);
3299 else if (line_byte < layout_line->start_index + layout_line->length || !tmp_list->next)
3302 tmp_list = tmp_list->next;
3307 gtk_text_layout_free_line_display (layout, display);
3309 line = _gtk_text_line_next_excluding_last (line);
3313 gtk_text_buffer_get_end_iter (layout->buffer, iter);
3316 !gtk_text_iter_equal (iter, &orig) &&
3317 !gtk_text_iter_is_end (iter);
3321 * gtk_text_layout_move_iter_to_line_end:
3322 * @layout: a #GtkTextLayout
3323 * @direction: if negative, move to beginning of line, otherwise
3324 move to end of line.
3326 * Move to the beginning or end of a display line.
3329 gtk_text_layout_move_iter_to_line_end (GtkTextLayout *layout,
3334 GtkTextLineDisplay *display;
3339 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), FALSE);
3340 g_return_val_if_fail (iter != NULL, FALSE);
3344 line = _gtk_text_iter_get_text_line (iter);
3345 display = gtk_text_layout_get_line_display (layout, line, FALSE);
3346 line_byte = line_display_iter_to_index (layout, display, iter);
3348 tmp_list = pango_layout_get_lines_readonly (display->layout);
3351 PangoLayoutLine *layout_line = tmp_list->data;
3353 if (line_byte < layout_line->start_index + layout_line->length || !tmp_list->next)
3355 line_display_index_to_iter (layout, display, iter,
3356 direction < 0 ? layout_line->start_index : layout_line->start_index + layout_line->length,
3359 /* FIXME: As a bad hack, we move back one position when we
3360 * are inside a paragraph to avoid going to next line on a
3361 * forced break not at whitespace. Real fix is to keep track
3362 * of whether marks are at leading or trailing edge? */
3363 if (direction > 0 && layout_line->length > 0 &&
3364 !gtk_text_iter_ends_line (iter) &&
3365 !_gtk_text_btree_char_is_invisible (iter))
3366 gtk_text_iter_backward_char (iter);
3370 tmp_list = tmp_list->next;
3373 gtk_text_layout_free_line_display (layout, display);
3376 !gtk_text_iter_equal (iter, &orig) &&
3377 !gtk_text_iter_is_end (iter);
3382 * gtk_text_layout_iter_starts_line:
3383 * @layout: a #GtkTextLayout
3384 * @iter: iterator to test
3386 * Tests whether an iterator is at the start of a display line.
3389 gtk_text_layout_iter_starts_line (GtkTextLayout *layout,
3390 const GtkTextIter *iter)
3393 GtkTextLineDisplay *display;
3397 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), FALSE);
3398 g_return_val_if_fail (iter != NULL, FALSE);
3400 line = _gtk_text_iter_get_text_line (iter);
3401 display = gtk_text_layout_get_line_display (layout, line, FALSE);
3402 line_byte = line_display_iter_to_index (layout, display, iter);
3404 tmp_list = pango_layout_get_lines_readonly (display->layout);
3407 PangoLayoutLine *layout_line = tmp_list->data;
3409 if (line_byte < layout_line->start_index + layout_line->length ||
3412 /* We're located on this line or the para delimiters before
3415 gtk_text_layout_free_line_display (layout, display);
3417 if (line_byte == layout_line->start_index)
3423 tmp_list = tmp_list->next;
3426 g_assert_not_reached ();
3431 gtk_text_layout_get_iter_at_line (GtkTextLayout *layout,
3436 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
3437 iter, line, byte_offset);
3442 * gtk_text_layout_move_iter_to_x:
3443 * @layout: a #GtkTextLayout
3444 * @iter: a #GtkTextIter
3447 * Keeping the iterator on the same line of the layout, move it to the
3448 * specified X coordinate. The lines of a wrapped paragraph are
3449 * treated as distinct for this operation.
3452 gtk_text_layout_move_iter_to_x (GtkTextLayout *layout,
3457 GtkTextLineDisplay *display;
3459 PangoLayoutIter *layout_iter;
3461 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
3462 g_return_if_fail (iter != NULL);
3464 line = _gtk_text_iter_get_text_line (iter);
3466 display = gtk_text_layout_get_line_display (layout, line, FALSE);
3467 line_byte = line_display_iter_to_index (layout, display, iter);
3469 layout_iter = pango_layout_get_iter (display->layout);
3473 PangoLayoutLine *layout_line = pango_layout_iter_get_line_readonly (layout_iter);
3475 if (line_byte < layout_line->start_index + layout_line->length ||
3476 pango_layout_iter_at_last_line (layout_iter))
3478 PangoRectangle logical_rect;
3479 gint byte_index, trailing;
3480 gint x_offset = display->x_offset * PANGO_SCALE;
3482 pango_layout_iter_get_line_extents (layout_iter, NULL, &logical_rect);
3484 pango_layout_line_x_to_index (layout_line,
3485 x * PANGO_SCALE - x_offset - logical_rect.x,
3486 &byte_index, &trailing);
3488 line_display_index_to_iter (layout, display, iter, byte_index, trailing);
3493 while (pango_layout_iter_next_line (layout_iter));
3495 pango_layout_iter_free (layout_iter);
3497 gtk_text_layout_free_line_display (layout, display);
3501 * gtk_text_layout_move_iter_visually:
3502 * @layout: a #GtkTextLayout
3503 * @iter: a #GtkTextIter
3504 * @count: number of characters to move (negative moves left, positive moves right)
3506 * Move the iterator a given number of characters visually, treating
3507 * it as the strong cursor position. If @count is positive, then the
3508 * new strong cursor position will be @count positions to the right of
3509 * the old cursor position. If @count is negative then the new strong
3510 * cursor position will be @count positions to the left of the old
3513 * In the presence of bidirection text, the correspondence
3514 * between logical and visual order will depend on the direction
3515 * of the current run, and there may be jumps when the cursor
3516 * is moved off of the end of a run.
3520 gtk_text_layout_move_iter_visually (GtkTextLayout *layout,
3524 GtkTextLineDisplay *display = NULL;
3526 GtkTextIter lineiter;
3528 g_return_val_if_fail (layout != NULL, FALSE);
3529 g_return_val_if_fail (iter != NULL, FALSE);
3535 GtkTextLine *line = _gtk_text_iter_get_text_line (iter);
3537 gint extra_back = 0;
3540 int byte_count = _gtk_text_line_byte_count (line);
3546 display = gtk_text_layout_get_line_display (layout, line, FALSE);
3548 if (layout->cursor_direction == GTK_TEXT_DIR_NONE)
3551 strong = display->direction == layout->cursor_direction;
3553 line_byte = line_display_iter_to_index (layout, display, iter);
3557 pango_layout_move_cursor_visually (display->layout, strong, line_byte, 0, 1, &new_index, &new_trailing);
3562 pango_layout_move_cursor_visually (display->layout, strong, line_byte, 0, -1, &new_index, &new_trailing);
3566 /* We need to handle the preedit string specially. Well, we don't really need to
3567 * handle it specially, since hopefully calling gtk_im_context_reset() will
3568 * remove the preedit string; but if we start off in front of the preedit
3569 * string (logically) and end up in or on the back edge of the preedit string,
3570 * we should move the iter one place farther.
3572 if (layout->preedit_len > 0 && display->insert_index >= 0)
3574 if (line_byte == display->insert_index + layout->preedit_len &&
3575 new_index < display->insert_index + layout->preedit_len)
3577 line_byte = display->insert_index;
3582 if (new_index < 0 || (new_index == 0 && extra_back))
3586 line = _gtk_text_line_previous (line);
3591 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
3592 &lineiter, line, 0);
3594 while (totally_invisible_line (layout, line, &lineiter));
3596 gtk_text_layout_free_line_display (layout, display);
3597 display = gtk_text_layout_get_line_display (layout, line, FALSE);
3598 gtk_text_iter_forward_to_line_end (&lineiter);
3599 new_index = gtk_text_iter_get_visible_line_index (&lineiter);
3601 else if (new_index > byte_count)
3605 line = _gtk_text_line_next_excluding_last (line);
3609 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
3610 &lineiter, line, 0);
3612 while (totally_invisible_line (layout, line, &lineiter));
3614 gtk_text_layout_free_line_display (layout, display);
3615 display = gtk_text_layout_get_line_display (layout, line, FALSE);
3619 line_display_index_to_iter (layout, display, iter, new_index, new_trailing);
3621 gtk_text_iter_backward_char (iter);
3624 gtk_text_layout_free_line_display (layout, display);
3629 !gtk_text_iter_equal (iter, &orig) &&
3630 !gtk_text_iter_is_end (iter);
3634 gtk_text_layout_spew (GtkTextLayout *layout)
3637 GtkTextDisplayLine *iter;
3639 guint paragraphs = 0;
3640 GtkTextLine *last_line = NULL;
3642 iter = layout->line_list;
3643 while (iter != NULL)
3645 if (iter->line != last_line)
3647 printf ("%5u paragraph (%p)\n", paragraphs, iter->line);
3649 last_line = iter->line;
3652 printf (" %5u y: %d len: %d start: %d bytes: %d\n",
3653 wrapped, iter->y, iter->length, iter->byte_offset,
3660 printf ("Layout %s recompute\n",
3661 layout->need_recompute ? "needs" : "doesn't need");
3663 printf ("Layout pars: %u lines: %u size: %d x %d Screen width: %d\n",
3664 paragraphs, wrapped, layout->width,
3665 layout->height, layout->screen_width);
3669 /* Catch all situations that move the insertion point.
3672 gtk_text_layout_mark_set_handler (GtkTextBuffer *buffer,
3673 const GtkTextIter *location,
3677 GtkTextLayout *layout = GTK_TEXT_LAYOUT (data);
3679 if (mark == gtk_text_buffer_get_insert (buffer))
3680 gtk_text_layout_update_cursor_line (layout);
3684 gtk_text_layout_buffer_insert_text (GtkTextBuffer *textbuffer,
3690 GtkTextLayout *layout = GTK_TEXT_LAYOUT (data);
3692 gtk_text_layout_update_cursor_line (layout);
3696 gtk_text_layout_buffer_delete_range (GtkTextBuffer *textbuffer,
3701 GtkTextLayout *layout = GTK_TEXT_LAYOUT (data);
3703 gtk_text_layout_update_cursor_line (layout);