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"
89 #define GTK_TEXT_LAYOUT_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GTK_TYPE_TEXT_LAYOUT, GtkTextLayoutPrivate))
91 typedef struct _GtkTextLayoutPrivate GtkTextLayoutPrivate;
93 struct _GtkTextLayoutPrivate
95 /* Cache the line that the cursor is positioned on, as the keyboard
96 direction only influences the direction of the cursor line.
98 GtkTextLine *cursor_line;
101 static GtkTextLineData *gtk_text_layout_real_wrap (GtkTextLayout *layout,
104 GtkTextLineData *line_data);
106 static void gtk_text_layout_invalidated (GtkTextLayout *layout);
108 static void gtk_text_layout_real_invalidate (GtkTextLayout *layout,
109 const GtkTextIter *start,
110 const GtkTextIter *end);
111 static void gtk_text_layout_invalidate_cache (GtkTextLayout *layout,
113 static void gtk_text_layout_invalidate_cursor_line (GtkTextLayout *layout);
114 static void gtk_text_layout_real_free_line_data (GtkTextLayout *layout,
116 GtkTextLineData *line_data);
117 static void gtk_text_layout_emit_changed (GtkTextLayout *layout,
122 static void gtk_text_layout_invalidate_all (GtkTextLayout *layout);
124 static PangoAttribute *gtk_text_attr_appearance_new (const GtkTextAppearance *appearance);
126 static void gtk_text_layout_mark_set_handler (GtkTextBuffer *buffer,
127 const GtkTextIter *location,
130 static void gtk_text_layout_buffer_insert_text (GtkTextBuffer *textbuffer,
135 static void gtk_text_layout_buffer_delete_range (GtkTextBuffer *textbuffer,
140 static void gtk_text_layout_update_cursor_line (GtkTextLayout *layout);
154 #define PIXEL_BOUND(d) (((d) + PANGO_SCALE - 1) / PANGO_SCALE)
156 static void gtk_text_layout_init (GtkTextLayout *text_layout);
157 static void gtk_text_layout_class_init (GtkTextLayoutClass *klass);
158 static void gtk_text_layout_finalize (GObject *object);
161 static GtkObjectClass *parent_class = NULL;
162 static guint signals[LAST_SIGNAL] = { 0 };
164 PangoAttrType gtk_text_attr_appearance_type = 0;
167 gtk_text_layout_get_type (void)
169 static GType our_type = 0;
173 static const GTypeInfo our_info =
175 sizeof (GtkTextLayoutClass),
176 (GBaseInitFunc) NULL,
177 (GBaseFinalizeFunc) NULL,
178 (GClassInitFunc) gtk_text_layout_class_init,
179 NULL, /* class_finalize */
180 NULL, /* class_data */
181 sizeof (GtkTextLayout),
183 (GInstanceInitFunc) gtk_text_layout_init
186 our_type = g_type_register_static (G_TYPE_OBJECT, "GtkTextLayout",
194 gtk_text_layout_class_init (GtkTextLayoutClass *klass)
196 GObjectClass *object_class = G_OBJECT_CLASS (klass);
198 parent_class = g_type_class_peek_parent (klass);
200 object_class->finalize = gtk_text_layout_finalize;
202 klass->wrap = gtk_text_layout_real_wrap;
203 klass->invalidate = gtk_text_layout_real_invalidate;
204 klass->free_line_data = gtk_text_layout_real_free_line_data;
206 signals[INVALIDATED] =
207 g_signal_new ("invalidated",
208 G_OBJECT_CLASS_TYPE (object_class),
210 G_STRUCT_OFFSET (GtkTextLayoutClass, invalidated),
212 _gtk_marshal_VOID__VOID,
217 g_signal_new ("changed",
218 G_OBJECT_CLASS_TYPE (object_class),
220 G_STRUCT_OFFSET (GtkTextLayoutClass, changed),
222 _gtk_marshal_VOID__INT_INT_INT,
229 signals[ALLOCATE_CHILD] =
230 g_signal_new ("allocate_child",
231 G_OBJECT_CLASS_TYPE (object_class),
233 G_STRUCT_OFFSET (GtkTextLayoutClass, allocate_child),
235 _gtk_marshal_VOID__OBJECT_INT_INT,
242 g_type_class_add_private (object_class, sizeof (GtkTextLayoutPrivate));
246 gtk_text_layout_init (GtkTextLayout *text_layout)
248 text_layout->cursor_visible = TRUE;
252 gtk_text_layout_new (void)
254 return g_object_new (GTK_TYPE_TEXT_LAYOUT, NULL);
258 free_style_cache (GtkTextLayout *text_layout)
260 if (text_layout->one_style_cache)
262 gtk_text_attributes_unref (text_layout->one_style_cache);
263 text_layout->one_style_cache = NULL;
268 gtk_text_layout_finalize (GObject *object)
270 GtkTextLayout *layout;
272 layout = GTK_TEXT_LAYOUT (object);
274 gtk_text_layout_set_buffer (layout, NULL);
276 if (layout->default_style)
277 gtk_text_attributes_unref (layout->default_style);
278 layout->default_style = NULL;
280 if (layout->ltr_context)
282 g_object_unref (layout->ltr_context);
283 layout->ltr_context = NULL;
285 if (layout->rtl_context)
287 g_object_unref (layout->rtl_context);
288 layout->rtl_context = NULL;
291 if (layout->one_display_cache)
293 GtkTextLineDisplay *tmp_display = layout->one_display_cache;
294 layout->one_display_cache = NULL;
295 gtk_text_layout_free_line_display (layout, tmp_display);
298 (* G_OBJECT_CLASS (parent_class)->finalize) (object);
302 gtk_text_layout_set_buffer (GtkTextLayout *layout,
303 GtkTextBuffer *buffer)
305 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
306 g_return_if_fail (buffer == NULL || GTK_IS_TEXT_BUFFER (buffer));
308 if (layout->buffer == buffer)
311 free_style_cache (layout);
315 _gtk_text_btree_remove_view (_gtk_text_buffer_get_btree (layout->buffer),
318 g_signal_handlers_disconnect_by_func (layout->buffer,
319 G_CALLBACK (gtk_text_layout_mark_set_handler),
321 g_signal_handlers_disconnect_by_func (layout->buffer,
322 G_CALLBACK (gtk_text_layout_buffer_insert_text),
324 g_signal_handlers_disconnect_by_func (layout->buffer,
325 G_CALLBACK (gtk_text_layout_buffer_delete_range),
328 g_object_unref (layout->buffer);
329 layout->buffer = NULL;
334 layout->buffer = buffer;
336 g_object_ref (buffer);
338 _gtk_text_btree_add_view (_gtk_text_buffer_get_btree (buffer), layout);
340 /* Bind to all signals that move the insert mark. */
341 g_signal_connect_after (layout->buffer, "mark_set",
342 G_CALLBACK (gtk_text_layout_mark_set_handler), layout);
343 g_signal_connect_after (layout->buffer, "insert_text",
344 G_CALLBACK (gtk_text_layout_buffer_insert_text), layout);
345 g_signal_connect_after (layout->buffer, "delete_range",
346 G_CALLBACK (gtk_text_layout_buffer_delete_range), layout);
351 gtk_text_layout_default_style_changed (GtkTextLayout *layout)
353 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
355 DV (g_print ("invalidating all due to default style change (%s)\n", G_STRLOC));
356 gtk_text_layout_invalidate_all (layout);
360 gtk_text_layout_set_default_style (GtkTextLayout *layout,
361 GtkTextAttributes *values)
363 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
364 g_return_if_fail (values != NULL);
366 if (values == layout->default_style)
369 gtk_text_attributes_ref (values);
371 if (layout->default_style)
372 gtk_text_attributes_unref (layout->default_style);
374 layout->default_style = values;
376 gtk_text_layout_default_style_changed (layout);
380 gtk_text_layout_set_contexts (GtkTextLayout *layout,
381 PangoContext *ltr_context,
382 PangoContext *rtl_context)
384 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
386 if (layout->ltr_context)
387 g_object_unref (ltr_context);
389 layout->ltr_context = ltr_context;
390 g_object_ref (ltr_context);
392 if (layout->rtl_context)
393 g_object_unref (rtl_context);
395 layout->rtl_context = rtl_context;
396 g_object_ref (rtl_context);
398 DV (g_print ("invalidating all due to new pango contexts (%s)\n", G_STRLOC));
399 gtk_text_layout_invalidate_all (layout);
403 * gtk_text_layout_set_cursor_direction:
404 * @direction: the new direction(s) for which to draw cursors.
405 * %GTK_TEXT_DIR_NONE means draw cursors for both
406 * left-to-right insertion and right-to-left insertion.
407 * (The two cursors will be visually distinguished.)
409 * Sets which text directions (left-to-right and/or right-to-left) for
410 * which cursors will be drawn for the insertion point. The visual
411 * point at which new text is inserted depends on whether the new
412 * text is right-to-left or left-to-right, so it may be desired to
413 * make the drawn position of the cursor depend on the keyboard state.
416 gtk_text_layout_set_cursor_direction (GtkTextLayout *layout,
417 GtkTextDirection direction)
419 if (direction != layout->cursor_direction)
421 layout->cursor_direction = direction;
422 gtk_text_layout_invalidate_cursor_line (layout);
427 * gtk_text_layout_set_keyboard_direction:
428 * @keyboard_dir: the current direction of the keyboard.
430 * Sets the keyboard direction; this is used as for the bidirectional
431 * base direction for the line with the cursor if the line contains
432 * only neutral characters.
435 gtk_text_layout_set_keyboard_direction (GtkTextLayout *layout,
436 GtkTextDirection keyboard_dir)
438 if (keyboard_dir != layout->keyboard_direction)
440 layout->keyboard_direction = keyboard_dir;
441 gtk_text_layout_invalidate_cursor_line (layout);
446 * gtk_text_layout_get_buffer:
447 * @layout: a #GtkTextLayout
449 * Gets the text buffer used by the layout. See
450 * gtk_text_layout_set_buffer().
452 * Return value: the text buffer used by the layout.
455 gtk_text_layout_get_buffer (GtkTextLayout *layout)
457 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), NULL);
459 return layout->buffer;
463 gtk_text_layout_set_screen_width (GtkTextLayout *layout, gint width)
465 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
466 g_return_if_fail (width >= 0);
467 g_return_if_fail (layout->wrap_loop_count == 0);
469 if (layout->screen_width == width)
472 layout->screen_width = width;
474 DV (g_print ("invalidating all due to new screen width (%s)\n", G_STRLOC));
475 gtk_text_layout_invalidate_all (layout);
479 * gtk_text_layout_set_cursor_visible:
480 * @layout: a #GtkTextLayout
481 * @cursor_visible: If %FALSE, then the insertion cursor will not
482 * be shown, even if the text is editable.
484 * Sets whether the insertion cursor should be shown. Generally,
485 * widgets using #GtkTextLayout will hide the cursor when the
486 * widget does not have the input focus.
489 gtk_text_layout_set_cursor_visible (GtkTextLayout *layout,
490 gboolean cursor_visible)
492 cursor_visible = (cursor_visible != FALSE);
494 if (layout->cursor_visible != cursor_visible)
499 layout->cursor_visible = cursor_visible;
501 /* Now queue a redraw on the paragraph containing the cursor
503 gtk_text_buffer_get_iter_at_mark (layout->buffer, &iter,
504 gtk_text_buffer_get_mark (layout->buffer, "insert"));
506 gtk_text_layout_get_line_yrange (layout, &iter, &y, &height);
507 gtk_text_layout_emit_changed (layout, y, height, height);
509 gtk_text_layout_invalidate_cache (layout, _gtk_text_iter_get_text_line (&iter));
514 * gtk_text_layout_get_cursor_visible:
515 * @layout: a #GtkTextLayout
517 * Returns whether the insertion cursor will be shown.
519 * Return value: if %FALSE, the insertion cursor will not be
520 shown, even if the text is editable.
523 gtk_text_layout_get_cursor_visible (GtkTextLayout *layout)
525 return layout->cursor_visible;
529 * gtk_text_layout_set_preedit_string:
530 * @layout: a #PangoLayout
531 * @preedit_string: a string to display at the insertion point
532 * @preedit_attrs: a #PangoAttrList of attributes that apply to @preedit_string
533 * @cursor_pos: position of cursor within preedit string in chars
535 * Set the preedit string and attributes. The preedit string is a
536 * string showing text that is currently being edited and not
537 * yet committed into the buffer.
540 gtk_text_layout_set_preedit_string (GtkTextLayout *layout,
541 const gchar *preedit_string,
542 PangoAttrList *preedit_attrs,
545 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
546 g_return_if_fail (preedit_attrs != NULL || preedit_string == NULL);
548 if (layout->preedit_string)
549 g_free (layout->preedit_string);
551 if (layout->preedit_attrs)
552 pango_attr_list_unref (layout->preedit_attrs);
556 layout->preedit_string = g_strdup (preedit_string);
557 layout->preedit_len = strlen (layout->preedit_string);
558 pango_attr_list_ref (preedit_attrs);
559 layout->preedit_attrs = preedit_attrs;
561 cursor_pos = CLAMP (cursor_pos, 0, g_utf8_strlen (layout->preedit_string, -1));
562 layout->preedit_cursor = g_utf8_offset_to_pointer (layout->preedit_string, cursor_pos) - layout->preedit_string;
566 layout->preedit_string = NULL;
567 layout->preedit_len = 0;
568 layout->preedit_attrs = NULL;
569 layout->preedit_cursor = 0;
572 gtk_text_layout_invalidate_cursor_line (layout);
576 gtk_text_layout_get_size (GtkTextLayout *layout,
580 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
583 *width = layout->width;
586 *height = layout->height;
590 gtk_text_layout_invalidated (GtkTextLayout *layout)
592 g_signal_emit (layout, signals[INVALIDATED], 0);
596 gtk_text_layout_emit_changed (GtkTextLayout *layout,
601 g_signal_emit (layout, signals[CHANGED], 0, y, old_height, new_height);
605 gtk_text_layout_changed (GtkTextLayout *layout,
610 /* Check if the range intersects our cached line display,
611 * and invalidate the cached line if so.
613 if (layout->one_display_cache)
615 GtkTextLine *line = layout->one_display_cache->line;
616 gint cache_y = _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
618 gint cache_height = layout->one_display_cache->height;
620 if (cache_y + cache_height > y && cache_y < y + old_height)
621 gtk_text_layout_invalidate_cache (layout, line);
624 gtk_text_layout_emit_changed (layout, y, old_height, new_height);
628 gtk_text_layout_free_line_data (GtkTextLayout *layout,
630 GtkTextLineData *line_data)
632 (* GTK_TEXT_LAYOUT_GET_CLASS (layout)->free_line_data)
633 (layout, line, line_data);
637 gtk_text_layout_invalidate (GtkTextLayout *layout,
638 const GtkTextIter *start_index,
639 const GtkTextIter *end_index)
641 (* GTK_TEXT_LAYOUT_GET_CLASS (layout)->invalidate)
642 (layout, start_index, end_index);
646 gtk_text_layout_wrap (GtkTextLayout *layout,
649 GtkTextLineData *line_data)
651 return (* GTK_TEXT_LAYOUT_GET_CLASS (layout)->wrap) (layout, line, line_data);
655 gtk_text_layout_get_lines (GtkTextLayout *layout,
656 /* [top_y, bottom_y) */
661 GtkTextLine *first_btree_line;
662 GtkTextLine *last_btree_line;
666 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), NULL);
667 g_return_val_if_fail (bottom_y > top_y, NULL);
672 _gtk_text_btree_find_line_by_y (_gtk_text_buffer_get_btree (layout->buffer),
673 layout, top_y, first_line_y);
674 if (first_btree_line == NULL)
680 /* -1 since bottom_y is one past */
682 _gtk_text_btree_find_line_by_y (_gtk_text_buffer_get_btree (layout->buffer),
683 layout, bottom_y - 1, NULL);
685 if (!last_btree_line)
687 _gtk_text_btree_get_end_iter_line (_gtk_text_buffer_get_btree (layout->buffer));
689 g_assert (last_btree_line != NULL);
691 line = first_btree_line;
694 retval = g_slist_prepend (retval, line);
696 if (line == last_btree_line)
699 line = _gtk_text_line_next_excluding_last (line);
702 retval = g_slist_reverse (retval);
708 invalidate_cached_style (GtkTextLayout *layout)
710 free_style_cache (layout);
713 /* These should be called around a loop which wraps a CONTIGUOUS bunch
714 * of display lines. If the lines aren't contiguous you can't call
718 gtk_text_layout_wrap_loop_start (GtkTextLayout *layout)
720 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
721 g_return_if_fail (layout->one_style_cache == NULL);
723 layout->wrap_loop_count += 1;
727 gtk_text_layout_wrap_loop_end (GtkTextLayout *layout)
729 g_return_if_fail (layout->wrap_loop_count > 0);
731 layout->wrap_loop_count -= 1;
733 if (layout->wrap_loop_count == 0)
735 /* We cache a some stuff if we're iterating over some lines wrapping
736 * them. This cleans it up.
738 /* Nuke our cached style */
739 invalidate_cached_style (layout);
740 g_assert (layout->one_style_cache == NULL);
745 gtk_text_layout_invalidate_all (GtkTextLayout *layout)
750 if (layout->buffer == NULL)
753 gtk_text_buffer_get_bounds (layout->buffer, &start, &end);
755 gtk_text_layout_invalidate (layout, &start, &end);
759 gtk_text_layout_invalidate_cache (GtkTextLayout *layout,
762 if (layout->one_display_cache && line == layout->one_display_cache->line)
764 GtkTextLineDisplay *tmp_display = layout->one_display_cache;
765 layout->one_display_cache = NULL;
766 gtk_text_layout_free_line_display (layout, tmp_display);
770 /* Now invalidate the paragraph containing the cursor
773 gtk_text_layout_invalidate_cursor_line (GtkTextLayout *layout)
775 GtkTextLayoutPrivate *priv = GTK_TEXT_LAYOUT_GET_PRIVATE (layout);
776 GtkTextLineData *line_data;
778 if (priv->cursor_line == NULL)
781 line_data = _gtk_text_line_get_data (priv->cursor_line, layout);
784 gtk_text_layout_invalidate_cache (layout, priv->cursor_line);
785 _gtk_text_line_invalidate_wrap (priv->cursor_line, line_data);
786 gtk_text_layout_invalidated (layout);
791 gtk_text_layout_update_cursor_line(GtkTextLayout *layout)
793 GtkTextLayoutPrivate *priv = GTK_TEXT_LAYOUT_GET_PRIVATE (layout);
796 gtk_text_buffer_get_iter_at_mark (layout->buffer, &iter,
797 gtk_text_buffer_get_mark (layout->buffer, "insert"));
799 priv->cursor_line = _gtk_text_iter_get_text_line (&iter);
803 gtk_text_layout_real_invalidate (GtkTextLayout *layout,
804 const GtkTextIter *start,
805 const GtkTextIter *end)
808 GtkTextLine *last_line;
810 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
811 g_return_if_fail (layout->wrap_loop_count == 0);
813 /* Because we may be invalidating a mark, it's entirely possible
814 * that gtk_text_iter_equal (start, end) in which case we
815 * should still invalidate the line they are both on. i.e.
816 * we always invalidate the line with "start" even
817 * if there's an empty range.
821 gtk_text_view_index_spew (start_index, "invalidate start");
822 gtk_text_view_index_spew (end_index, "invalidate end");
825 last_line = _gtk_text_iter_get_text_line (end);
826 line = _gtk_text_iter_get_text_line (start);
830 GtkTextLineData *line_data = _gtk_text_line_get_data (line, layout);
832 gtk_text_layout_invalidate_cache (layout, line);
835 _gtk_text_line_invalidate_wrap (line, line_data);
837 if (line == last_line)
840 line = _gtk_text_line_next_excluding_last (line);
843 gtk_text_layout_invalidated (layout);
847 gtk_text_layout_real_free_line_data (GtkTextLayout *layout,
849 GtkTextLineData *line_data)
851 gtk_text_layout_invalidate_cache (layout, line);
859 * gtk_text_layout_is_valid:
860 * @layout: a #GtkTextLayout
862 * Check if there are any invalid regions in a #GtkTextLayout's buffer
864 * Return value: %TRUE if any invalid regions were found
867 gtk_text_layout_is_valid (GtkTextLayout *layout)
869 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), FALSE);
871 return _gtk_text_btree_is_valid (_gtk_text_buffer_get_btree (layout->buffer),
876 update_layout_size (GtkTextLayout *layout)
878 _gtk_text_btree_get_view_size (_gtk_text_buffer_get_btree (layout->buffer),
880 &layout->width, &layout->height);
884 * gtk_text_layout_validate_yrange:
885 * @layout: a #GtkTextLayout
886 * @anchor: iter pointing into a line that will be used as the
888 * @y0_: offset from the top of the line pointed to by @anchor at
889 * which to begin validation. (The offset here is in pixels
891 * @y1_: offset from the top of the line pointed to by @anchor at
892 * which to end validation. (The offset here is in pixels
895 * Ensure that a region of a #GtkTextLayout is valid. The ::changed
896 * signal will be emitted if any lines are validated.
899 gtk_text_layout_validate_yrange (GtkTextLayout *layout,
905 GtkTextLine *first_line = NULL;
906 GtkTextLine *last_line = NULL;
908 gint delta_height = 0;
909 gint first_line_y = 0; /* Quiet GCC */
910 gint last_line_y = 0; /* Quiet GCC */
912 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
919 /* Validate backwards from the anchor line to y0
921 line = _gtk_text_iter_get_text_line (anchor);
922 line = _gtk_text_line_previous (line);
924 while (line && seen < -y0)
926 GtkTextLineData *line_data = _gtk_text_line_get_data (line, layout);
927 if (!line_data || !line_data->valid)
929 gint old_height = line_data ? line_data->height : 0;
931 _gtk_text_btree_validate_line (_gtk_text_buffer_get_btree (layout->buffer),
933 line_data = _gtk_text_line_get_data (line, layout);
935 delta_height += line_data->height - old_height;
938 first_line_y = -seen - line_data->height;
946 seen += line_data->height;
947 line = _gtk_text_line_previous (line);
950 /* Validate forwards to y1 */
951 line = _gtk_text_iter_get_text_line (anchor);
953 while (line && seen < y1)
955 GtkTextLineData *line_data = _gtk_text_line_get_data (line, layout);
956 if (!line_data || !line_data->valid)
958 gint old_height = line_data ? line_data->height : 0;
960 _gtk_text_btree_validate_line (_gtk_text_buffer_get_btree (layout->buffer),
962 line_data = _gtk_text_line_get_data (line, layout);
964 delta_height += line_data->height - old_height;
972 last_line_y = seen + line_data->height;
975 seen += line_data->height;
976 line = _gtk_text_line_next_excluding_last (line);
979 /* If we found and validated any invalid lines, update size and
980 * emit the changed signal
986 update_layout_size (layout);
988 line_top = _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
991 gtk_text_layout_emit_changed (layout,
993 last_line_y - first_line_y - delta_height,
994 last_line_y - first_line_y);
999 * gtk_text_layout_validate:
1000 * @tree: a #GtkTextLayout
1001 * @max_pixels: the maximum number of pixels to validate. (No more
1002 * than one paragraph beyond this limit will be validated)
1004 * Validate regions of a #GtkTextLayout. The ::changed signal will
1005 * be emitted for each region validated.
1008 gtk_text_layout_validate (GtkTextLayout *layout,
1011 gint y, old_height, new_height;
1013 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
1015 while (max_pixels > 0 &&
1016 _gtk_text_btree_validate (_gtk_text_buffer_get_btree (layout->buffer),
1018 &y, &old_height, &new_height))
1020 max_pixels -= new_height;
1022 update_layout_size (layout);
1023 gtk_text_layout_emit_changed (layout, y, old_height, new_height);
1027 static GtkTextLineData*
1028 gtk_text_layout_real_wrap (GtkTextLayout *layout,
1031 GtkTextLineData *line_data)
1033 GtkTextLineDisplay *display;
1035 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), NULL);
1036 g_return_val_if_fail (line != NULL, NULL);
1038 if (line_data == NULL)
1040 line_data = _gtk_text_line_data_new (layout, line);
1041 _gtk_text_line_add_data (line, line_data);
1044 display = gtk_text_layout_get_line_display (layout, line, TRUE);
1045 line_data->width = display->width;
1046 line_data->height = display->height;
1047 line_data->valid = TRUE;
1048 gtk_text_layout_free_line_display (layout, display);
1054 * Layout utility functions
1057 /* If you get the style with get_style () you need to call
1058 release_style () to free it. */
1059 static GtkTextAttributes*
1060 get_style (GtkTextLayout *layout,
1061 const GtkTextIter *iter)
1065 GtkTextAttributes *style;
1067 /* If we have the one-style cache, then it means
1068 that we haven't seen a toggle since we filled in the
1071 if (layout->one_style_cache != NULL)
1073 gtk_text_attributes_ref (layout->one_style_cache);
1074 return layout->one_style_cache;
1077 g_assert (layout->one_style_cache == NULL);
1079 /* Get the tags at this spot */
1080 tags = _gtk_text_btree_get_tags (iter, &tag_count);
1082 /* No tags, use default style */
1083 if (tags == NULL || tag_count == 0)
1085 /* One ref for the return value, one ref for the
1086 layout->one_style_cache reference */
1087 gtk_text_attributes_ref (layout->default_style);
1088 gtk_text_attributes_ref (layout->default_style);
1089 layout->one_style_cache = layout->default_style;
1094 return layout->default_style;
1097 /* Sort tags in ascending order of priority */
1098 _gtk_text_tag_array_sort (tags, tag_count);
1100 style = gtk_text_attributes_new ();
1102 gtk_text_attributes_copy_values (layout->default_style,
1105 _gtk_text_attributes_fill_from_tags (style,
1111 g_assert (style->refcount == 1);
1113 /* Leave this style as the last one seen */
1114 g_assert (layout->one_style_cache == NULL);
1115 gtk_text_attributes_ref (style); /* ref held by layout->one_style_cache */
1116 layout->one_style_cache = style;
1118 /* Returning yet another refcount */
1123 release_style (GtkTextLayout *layout,
1124 GtkTextAttributes *style)
1126 g_return_if_fail (style != NULL);
1127 g_return_if_fail (style->refcount > 0);
1129 gtk_text_attributes_unref (style);
1136 /* This function tries to optimize the case where a line
1137 is completely invisible */
1139 totally_invisible_line (GtkTextLayout *layout,
1143 GtkTextLineSegment *seg;
1146 /* Check if the first char is visible, if so we are partially visible.
1147 * Note that we have to check this since we don't know the current
1148 * invisible/noninvisible toggle state; this function can use the whole btree
1151 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
1154 if (!_gtk_text_btree_char_is_invisible (iter))
1158 seg = line->segments;
1162 if (seg->byte_count > 0)
1163 bytes += seg->byte_count;
1165 /* Note that these two tests can cause us to bail out
1166 * when we shouldn't, because a higher-priority tag
1167 * may override these settings. However the important
1168 * thing is to only invisible really-invisible lines, rather
1169 * than to invisible all really-invisible lines.
1172 else if (seg->type == >k_text_toggle_on_type)
1174 invalidate_cached_style (layout);
1176 /* Bail out if an elision-unsetting tag begins */
1177 if (seg->body.toggle.info->tag->invisible_set &&
1178 !seg->body.toggle.info->tag->values->invisible)
1181 else if (seg->type == >k_text_toggle_off_type)
1183 invalidate_cached_style (layout);
1185 /* Bail out if an elision-setting tag ends */
1186 if (seg->body.toggle.info->tag->invisible_set &&
1187 seg->body.toggle.info->tag->values->invisible)
1194 if (seg != NULL) /* didn't reach line end */
1201 set_para_values (GtkTextLayout *layout,
1202 PangoDirection base_dir,
1203 GtkTextAttributes *style,
1204 GtkTextLineDisplay *display)
1206 PangoAlignment pango_align = PANGO_ALIGN_LEFT;
1211 /* If no base direction was found, then use the style direction */
1212 case PANGO_DIRECTION_NEUTRAL :
1213 display->direction = style->direction;
1215 /* Override the base direction */
1216 if (display->direction == GTK_TEXT_DIR_RTL)
1217 base_dir = PANGO_DIRECTION_RTL;
1219 base_dir = PANGO_DIRECTION_LTR;
1222 case PANGO_DIRECTION_RTL :
1223 display->direction = GTK_TEXT_DIR_RTL;
1226 display->direction = GTK_TEXT_DIR_LTR;
1230 if (display->direction == GTK_TEXT_DIR_RTL)
1231 display->layout = pango_layout_new (layout->rtl_context);
1233 display->layout = pango_layout_new (layout->ltr_context);
1235 switch (style->justification)
1237 case GTK_JUSTIFY_LEFT:
1238 pango_align = (base_dir == PANGO_DIRECTION_LTR) ? PANGO_ALIGN_LEFT : PANGO_ALIGN_RIGHT;
1240 case GTK_JUSTIFY_RIGHT:
1241 pango_align = (base_dir == PANGO_DIRECTION_LTR) ? PANGO_ALIGN_RIGHT : PANGO_ALIGN_LEFT;
1243 case GTK_JUSTIFY_CENTER:
1244 pango_align = PANGO_ALIGN_CENTER;
1246 case GTK_JUSTIFY_FILL:
1247 g_warning ("FIXME we don't support GTK_JUSTIFY_FILL yet");
1250 g_assert_not_reached ();
1254 pango_layout_set_alignment (display->layout, pango_align);
1255 pango_layout_set_spacing (display->layout,
1256 style->pixels_inside_wrap * PANGO_SCALE);
1259 pango_layout_set_tabs (display->layout, style->tabs);
1261 display->top_margin = style->pixels_above_lines;
1262 display->height = style->pixels_above_lines + style->pixels_below_lines;
1263 display->bottom_margin = style->pixels_below_lines;
1264 display->left_margin = style->left_margin;
1265 display->right_margin = style->right_margin;
1267 display->x_offset = display->left_margin;
1269 pango_layout_set_indent (display->layout,
1270 style->indent * PANGO_SCALE);
1272 switch (style->wrap_mode)
1275 layout_width = layout->screen_width - display->left_margin - display->right_margin;
1276 pango_layout_set_width (display->layout, layout_width * PANGO_SCALE);
1277 pango_layout_set_wrap (display->layout, PANGO_WRAP_CHAR);
1281 layout_width = layout->screen_width - display->left_margin - display->right_margin;
1282 pango_layout_set_width (display->layout, layout_width * PANGO_SCALE);
1283 pango_layout_set_wrap (display->layout, PANGO_WRAP_WORD);
1286 case GTK_WRAP_WORD_CHAR:
1287 layout_width = layout->screen_width - display->left_margin - display->right_margin;
1288 pango_layout_set_width (display->layout, layout_width * PANGO_SCALE);
1289 pango_layout_set_wrap (display->layout, PANGO_WRAP_WORD_CHAR);
1296 display->total_width = MAX (layout->screen_width, layout->width) - display->left_margin - display->right_margin;
1299 static PangoAttribute *
1300 gtk_text_attr_appearance_copy (const PangoAttribute *attr)
1302 const GtkTextAttrAppearance *appearance_attr = (const GtkTextAttrAppearance *)attr;
1304 return gtk_text_attr_appearance_new (&appearance_attr->appearance);
1308 gtk_text_attr_appearance_destroy (PangoAttribute *attr)
1310 GtkTextAppearance *appearance = &((GtkTextAttrAppearance *)attr)->appearance;
1312 if (appearance->bg_stipple)
1313 g_object_unref (appearance->bg_stipple);
1314 if (appearance->fg_stipple)
1315 g_object_unref (appearance->fg_stipple);
1321 gtk_text_attr_appearance_compare (const PangoAttribute *attr1,
1322 const PangoAttribute *attr2)
1324 const GtkTextAppearance *appearance1 = &((const GtkTextAttrAppearance *)attr1)->appearance;
1325 const GtkTextAppearance *appearance2 = &((const GtkTextAttrAppearance *)attr2)->appearance;
1327 return (gdk_color_equal (&appearance1->fg_color, &appearance2->fg_color) &&
1328 gdk_color_equal (&appearance1->bg_color, &appearance2->bg_color) &&
1329 appearance1->fg_stipple == appearance2->fg_stipple &&
1330 appearance1->bg_stipple == appearance2->bg_stipple &&
1331 appearance1->underline == appearance2->underline &&
1332 appearance1->strikethrough == appearance2->strikethrough &&
1333 appearance1->draw_bg == appearance2->draw_bg);
1338 * gtk_text_attr_appearance_new:
1341 * Create a new font description attribute. (This attribute
1342 * allows setting family, style, weight, variant, stretch,
1343 * and size simultaneously.)
1347 static PangoAttribute *
1348 gtk_text_attr_appearance_new (const GtkTextAppearance *appearance)
1350 static PangoAttrClass klass = {
1352 gtk_text_attr_appearance_copy,
1353 gtk_text_attr_appearance_destroy,
1354 gtk_text_attr_appearance_compare
1357 GtkTextAttrAppearance *result;
1360 klass.type = gtk_text_attr_appearance_type =
1361 pango_attr_type_register ("GtkTextAttrAppearance");
1363 result = g_new (GtkTextAttrAppearance, 1);
1364 result->attr.klass = &klass;
1366 result->appearance = *appearance;
1368 if (appearance->bg_stipple)
1369 g_object_ref (appearance->bg_stipple);
1370 if (appearance->fg_stipple)
1371 g_object_ref (appearance->fg_stipple);
1373 return (PangoAttribute *)result;
1378 add_generic_attrs (GtkTextLayout *layout,
1379 GtkTextAppearance *appearance,
1381 PangoAttrList *attrs,
1386 PangoAttribute *attr;
1388 if (appearance->underline != PANGO_UNDERLINE_NONE)
1390 attr = pango_attr_underline_new (appearance->underline);
1392 attr->start_index = start;
1393 attr->end_index = start + byte_count;
1395 pango_attr_list_insert (attrs, attr);
1398 if (appearance->rise != 0)
1400 attr = pango_attr_rise_new (appearance->rise);
1402 attr->start_index = start;
1403 attr->end_index = start + byte_count;
1405 pango_attr_list_insert (attrs, attr);
1410 attr = gtk_text_attr_appearance_new (appearance);
1412 attr->start_index = start;
1413 attr->end_index = start + byte_count;
1415 ((GtkTextAttrAppearance *)attr)->appearance.is_text = is_text;
1417 pango_attr_list_insert (attrs, attr);
1422 add_text_attrs (GtkTextLayout *layout,
1423 GtkTextAttributes *style,
1425 PangoAttrList *attrs,
1429 PangoAttribute *attr;
1431 attr = pango_attr_font_desc_new (style->font);
1432 attr->start_index = start;
1433 attr->end_index = start + byte_count;
1435 pango_attr_list_insert (attrs, attr);
1437 if (style->font_scale != 1.0)
1439 attr = pango_attr_scale_new (style->font_scale);
1441 attr->start_index = start;
1442 attr->end_index = start + byte_count;
1444 pango_attr_list_insert (attrs, attr);
1449 add_pixbuf_attrs (GtkTextLayout *layout,
1450 GtkTextLineDisplay *display,
1451 GtkTextAttributes *style,
1452 GtkTextLineSegment *seg,
1453 PangoAttrList *attrs,
1456 PangoAttribute *attr;
1457 PangoRectangle logical_rect;
1458 GtkTextPixbuf *pixbuf = &seg->body.pixbuf;
1461 width = gdk_pixbuf_get_width (pixbuf->pixbuf);
1462 height = gdk_pixbuf_get_height (pixbuf->pixbuf);
1465 logical_rect.y = -height * PANGO_SCALE;
1466 logical_rect.width = width * PANGO_SCALE;
1467 logical_rect.height = height * PANGO_SCALE;
1469 attr = pango_attr_shape_new (&logical_rect, &logical_rect);
1470 attr->start_index = start;
1471 attr->end_index = start + seg->byte_count;
1472 pango_attr_list_insert (attrs, attr);
1474 display->shaped_objects =
1475 g_slist_append (display->shaped_objects, pixbuf->pixbuf);
1479 add_child_attrs (GtkTextLayout *layout,
1480 GtkTextLineDisplay *display,
1481 GtkTextAttributes *style,
1482 GtkTextLineSegment *seg,
1483 PangoAttrList *attrs,
1486 PangoAttribute *attr;
1487 PangoRectangle logical_rect;
1488 GtkTextChildAnchor *anchor;
1495 anchor = seg->body.child.obj;
1497 tmp_list = seg->body.child.widgets;
1498 while (tmp_list != NULL)
1500 GtkWidget *child = tmp_list->data;
1502 if (_gtk_anchored_child_get_layout (child) == layout)
1507 gtk_widget_get_child_requisition (child, &req);
1510 height = req.height;
1512 display->shaped_objects =
1513 g_slist_append (display->shaped_objects, child);
1518 tmp_list = g_slist_next (tmp_list);
1521 if (tmp_list == NULL)
1523 /* If tmp_list == NULL then there is no widget at this anchor in
1524 * this display; not an error. We make up an arbitrary size
1525 * to use, just so the programmer can see the blank spot.
1526 * We also put a NULL in the shaped objects list, to keep
1527 * the correspondence between the list and the shaped chars in
1528 * the layout. A bad hack, yes.
1534 display->shaped_objects =
1535 g_slist_append (display->shaped_objects, NULL);
1539 logical_rect.y = -height * PANGO_SCALE;
1540 logical_rect.width = width * PANGO_SCALE;
1541 logical_rect.height = height * PANGO_SCALE;
1543 attr = pango_attr_shape_new (&logical_rect, &logical_rect);
1544 attr->start_index = start;
1545 attr->end_index = start + seg->byte_count;
1546 pango_attr_list_insert (attrs, attr);
1550 add_cursor (GtkTextLayout *layout,
1551 GtkTextLineDisplay *display,
1552 GtkTextLineSegment *seg,
1555 PangoRectangle strong_pos, weak_pos;
1556 GtkTextCursorDisplay *cursor = NULL; /* Quiet GCC */
1557 gboolean add_weak = FALSE;
1558 gboolean add_strong = FALSE;
1560 /* Hide insertion cursor when we have a selection or the layout
1561 * user has hidden the cursor.
1563 if (_gtk_text_btree_mark_is_insert (_gtk_text_buffer_get_btree (layout->buffer),
1564 seg->body.mark.obj) &&
1565 (!layout->cursor_visible ||
1566 gtk_text_buffer_get_selection_bounds (layout->buffer, NULL, NULL)))
1569 pango_layout_get_cursor_pos (display->layout, start, &strong_pos, &weak_pos);
1571 if (layout->cursor_direction == GTK_TEXT_DIR_NONE)
1576 else if (display->direction == layout->cursor_direction)
1583 cursor = g_new (GtkTextCursorDisplay, 1);
1585 cursor->x = PANGO_PIXELS (strong_pos.x);
1586 cursor->y = PANGO_PIXELS (strong_pos.y);
1587 cursor->height = PANGO_PIXELS (strong_pos.height);
1588 cursor->is_strong = TRUE;
1589 cursor->is_weak = (layout->cursor_direction == GTK_TEXT_DIR_NONE) ? FALSE : TRUE;
1590 display->cursors = g_slist_prepend (display->cursors, cursor);
1595 if (weak_pos.x == strong_pos.x && add_strong)
1596 cursor->is_weak = TRUE;
1599 cursor = g_new (GtkTextCursorDisplay, 1);
1601 cursor->x = PANGO_PIXELS (weak_pos.x);
1602 cursor->y = PANGO_PIXELS (weak_pos.y);
1603 cursor->height = PANGO_PIXELS (weak_pos.height);
1604 cursor->is_strong = (layout->cursor_direction == GTK_TEXT_DIR_NONE) ? FALSE : TRUE;
1605 cursor->is_weak = TRUE;
1606 display->cursors = g_slist_prepend (display->cursors, cursor);
1612 is_shape (PangoLayoutRun *run)
1614 GSList *tmp_list = run->item->analysis.extra_attrs;
1618 PangoAttribute *attr = tmp_list->data;
1620 if (attr->klass->type == PANGO_ATTR_SHAPE)
1623 tmp_list = tmp_list->next;
1630 allocate_child_widgets (GtkTextLayout *text_layout,
1631 GtkTextLineDisplay *display)
1633 GSList *shaped = display->shaped_objects;
1634 PangoLayout *layout = display->layout;
1635 PangoLayoutIter *iter;
1637 iter = pango_layout_get_iter (layout);
1641 PangoLayoutRun *run = pango_layout_iter_get_run (iter);
1643 if (run && is_shape (run))
1645 GObject *shaped_object = shaped->data;
1646 shaped = shaped->next;
1648 /* shaped_object is NULL for child anchors with no
1649 * widgets stored at them
1651 if (shaped_object && GTK_IS_WIDGET (shaped_object))
1653 PangoRectangle extents;
1655 /* We emit "allocate_child" with the x,y of
1656 * the widget with respect to the top of the line
1657 * and the left side of the buffer
1660 pango_layout_iter_get_run_extents (iter,
1664 g_signal_emit (text_layout,
1665 signals[ALLOCATE_CHILD],
1668 PANGO_PIXELS (extents.x) + display->x_offset,
1669 PANGO_PIXELS (extents.y) + display->top_margin);
1673 while (pango_layout_iter_next_run (iter));
1675 pango_layout_iter_free (iter);
1679 convert_color (GdkColor *result,
1680 PangoAttrColor *attr)
1682 result->red = attr->color.red;
1683 result->blue = attr->color.blue;
1684 result->green = attr->color.green;
1687 /* This function is used to convert the preedit string attributes, which are
1688 * standard PangoAttributes, into the custom attributes used by the text
1689 * widget and insert them into a attr list with a given offset.
1692 add_preedit_attrs (GtkTextLayout *layout,
1693 GtkTextAttributes *style,
1694 PangoAttrList *attrs,
1698 PangoAttrIterator *iter = pango_attr_list_get_iterator (layout->preedit_attrs);
1702 GtkTextAppearance appearance = style->appearance;
1703 PangoFontDescription *font_desc = pango_font_description_copy_static (style->font);
1704 PangoAttribute *insert_attr;
1705 GSList *extra_attrs = NULL;
1707 PangoLanguage *language;
1710 pango_attr_iterator_range (iter, &start, &end);
1712 if (end == G_MAXINT)
1713 end = layout->preedit_len;
1718 pango_attr_iterator_get_font (iter, font_desc, &language, &extra_attrs);
1720 tmp_list = extra_attrs;
1723 PangoAttribute *attr = tmp_list->data;
1725 switch (attr->klass->type)
1727 case PANGO_ATTR_FOREGROUND:
1728 convert_color (&appearance.fg_color, (PangoAttrColor *)attr);
1730 case PANGO_ATTR_BACKGROUND:
1731 convert_color (&appearance.bg_color, (PangoAttrColor *)attr);
1732 appearance.draw_bg = TRUE;
1734 case PANGO_ATTR_UNDERLINE:
1735 appearance.underline = ((PangoAttrInt *)attr)->value;
1737 case PANGO_ATTR_STRIKETHROUGH:
1738 appearance.strikethrough = ((PangoAttrInt *)attr)->value;
1740 case PANGO_ATTR_RISE:
1741 appearance.rise = ((PangoAttrInt *)attr)->value;
1747 pango_attribute_destroy (attr);
1748 tmp_list = tmp_list->next;
1751 g_slist_free (extra_attrs);
1753 insert_attr = pango_attr_font_desc_new (font_desc);
1754 insert_attr->start_index = start + offset;
1755 insert_attr->end_index = end + offset;
1757 pango_attr_list_insert (attrs, insert_attr);
1761 insert_attr = pango_attr_language_new (language);
1762 insert_attr->start_index = start + offset;
1763 insert_attr->end_index = end + offset;
1765 pango_attr_list_insert (attrs, insert_attr);
1768 add_generic_attrs (layout, &appearance, end - start,
1769 attrs, start + offset,
1772 pango_font_description_free (font_desc);
1774 while (pango_attr_iterator_next (iter));
1776 pango_attr_iterator_destroy (iter);
1779 GtkTextLineDisplay *
1780 gtk_text_layout_get_line_display (GtkTextLayout *layout,
1784 GtkTextLayoutPrivate *priv = GTK_TEXT_LAYOUT_GET_PRIVATE (layout);
1785 GtkTextLineDisplay *display;
1786 GtkTextLineSegment *seg;
1788 GtkTextAttributes *style;
1790 PangoAttrList *attrs;
1791 gint text_allocated, layout_byte_offset, buffer_byte_offset;
1792 PangoRectangle extents;
1793 gboolean para_values_set = FALSE;
1794 GSList *cursor_byte_offsets = NULL;
1795 GSList *cursor_segs = NULL;
1796 GSList *tmp_list1, *tmp_list2;
1797 gboolean saw_widget = FALSE;
1798 PangoDirection base_dir;
1800 g_return_val_if_fail (line != NULL, NULL);
1802 if (layout->one_display_cache)
1804 if (line == layout->one_display_cache->line &&
1805 (size_only || !layout->one_display_cache->size_only))
1806 return layout->one_display_cache;
1809 GtkTextLineDisplay *tmp_display = layout->one_display_cache;
1810 layout->one_display_cache = NULL;
1811 gtk_text_layout_free_line_display (layout, tmp_display);
1815 display = g_new0 (GtkTextLineDisplay, 1);
1817 display->size_only = size_only;
1818 display->line = line;
1819 display->insert_index = -1;
1821 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
1824 /* Special-case optimization for completely
1825 * invisible lines; makes it faster to deal
1826 * with sequences of invisible lines.
1828 if (totally_invisible_line (layout, line, &iter))
1831 /* Find the bidi base direction */
1832 base_dir = line->dir_propagated_forward;
1833 if (base_dir == PANGO_DIRECTION_NEUTRAL)
1834 base_dir = line->dir_propagated_back;
1836 if (line == priv->cursor_line &&
1837 line->dir_strong == PANGO_DIRECTION_NEUTRAL)
1839 base_dir = (layout->keyboard_direction == GTK_TEXT_DIR_LTR) ?
1840 PANGO_DIRECTION_LTR : PANGO_DIRECTION_RTL;
1843 /* Allocate space for flat text for buffer
1845 text_allocated = _gtk_text_line_byte_count (line);
1846 text = g_malloc (text_allocated);
1848 attrs = pango_attr_list_new ();
1850 /* Iterate over segments, creating display chunks for them. */
1851 layout_byte_offset = 0; /* current length of layout text (includes preedit, does not include invisible text) */
1852 buffer_byte_offset = 0; /* position in the buffer line */
1853 seg = _gtk_text_iter_get_any_segment (&iter);
1856 /* Displayable segments */
1857 if (seg->type == >k_text_char_type ||
1858 seg->type == >k_text_pixbuf_type ||
1859 seg->type == >k_text_child_type)
1861 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
1863 buffer_byte_offset);
1864 style = get_style (layout, &iter);
1866 /* We have to delay setting the paragraph values until we
1867 * hit the first pixbuf or text segment because toggles at
1868 * the beginning of the paragraph should affect the
1869 * paragraph-global values
1871 if (!para_values_set)
1873 set_para_values (layout, base_dir, style, display);
1874 para_values_set = TRUE;
1877 /* First see if the chunk is invisible, and ignore it if so. Tk
1878 * looked at tabs, wrap mode, etc. before doing this, but
1879 * that made no sense to me, so I am just skipping the
1882 if (!style->invisible)
1884 if (seg->type == >k_text_char_type)
1886 /* We don't want to split segments because of marks,
1887 * so we scan forward for more segments only
1888 * separated from us by marks. In theory, we should
1889 * also merge segments with identical styles, even
1890 * if there are toggles in-between
1894 GtkTextLineSegment *prev_seg = NULL;
1898 if (seg->type == >k_text_char_type)
1900 memcpy (text + layout_byte_offset, seg->body.chars, seg->byte_count);
1901 layout_byte_offset += seg->byte_count;
1902 buffer_byte_offset += seg->byte_count;
1903 bytes += seg->byte_count;
1905 else if (seg->type == >k_text_right_mark_type ||
1906 seg->type == >k_text_left_mark_type)
1908 /* If we have preedit string, break out of this loop - we'll almost
1909 * certainly have different attributes on the preedit string
1912 if (layout->preedit_len > 0 &&
1913 _gtk_text_btree_mark_is_insert (_gtk_text_buffer_get_btree (layout->buffer),
1914 seg->body.mark.obj))
1917 if (seg->body.mark.visible)
1919 cursor_byte_offsets = g_slist_prepend (cursor_byte_offsets, GINT_TO_POINTER (layout_byte_offset));
1920 cursor_segs = g_slist_prepend (cursor_segs, seg);
1930 seg = prev_seg; /* Back up one */
1931 add_generic_attrs (layout, &style->appearance,
1933 attrs, layout_byte_offset - bytes,
1935 add_text_attrs (layout, style, bytes, attrs,
1936 layout_byte_offset - bytes, size_only);
1938 else if (seg->type == >k_text_pixbuf_type)
1940 add_generic_attrs (layout,
1943 attrs, layout_byte_offset,
1945 add_pixbuf_attrs (layout, display, style,
1946 seg, attrs, layout_byte_offset);
1947 memcpy (text + layout_byte_offset, gtk_text_unknown_char_utf8,
1949 layout_byte_offset += seg->byte_count;
1950 buffer_byte_offset += seg->byte_count;
1952 else if (seg->type == >k_text_child_type)
1956 add_generic_attrs (layout, &style->appearance,
1958 attrs, layout_byte_offset,
1960 add_child_attrs (layout, display, style,
1961 seg, attrs, layout_byte_offset);
1962 memcpy (text + layout_byte_offset, gtk_text_unknown_char_utf8,
1964 layout_byte_offset += seg->byte_count;
1965 buffer_byte_offset += seg->byte_count;
1969 /* We don't know this segment type */
1970 g_assert_not_reached ();
1973 } /* if (segment was visible) */
1976 /* Invisible segment */
1977 buffer_byte_offset += seg->byte_count;
1980 release_style (layout, style);
1984 else if (seg->type == >k_text_toggle_on_type ||
1985 seg->type == >k_text_toggle_off_type)
1987 /* Style may have changed, drop our
1988 current cached style */
1989 invalidate_cached_style (layout);
1993 else if (seg->type == >k_text_right_mark_type ||
1994 seg->type == >k_text_left_mark_type)
1996 gint cursor_offset = 0;
1998 /* At the insertion point, add the preedit string, if any */
2000 if (_gtk_text_btree_mark_is_insert (_gtk_text_buffer_get_btree (layout->buffer),
2001 seg->body.mark.obj))
2003 display->insert_index = layout_byte_offset;
2005 if (layout->preedit_len > 0)
2007 text_allocated += layout->preedit_len;
2008 text = g_realloc (text, text_allocated);
2010 style = get_style (layout, &iter);
2011 add_preedit_attrs (layout, style, attrs, layout_byte_offset, size_only);
2012 release_style (layout, style);
2014 memcpy (text + layout_byte_offset, layout->preedit_string, layout->preedit_len);
2015 layout_byte_offset += layout->preedit_len;
2016 /* DO NOT increment the buffer byte offset for preedit */
2018 cursor_offset = layout->preedit_cursor - layout->preedit_len;
2023 /* Display visible marks */
2025 if (seg->body.mark.visible)
2027 cursor_byte_offsets = g_slist_prepend (cursor_byte_offsets,
2028 GINT_TO_POINTER (layout_byte_offset + cursor_offset));
2029 cursor_segs = g_slist_prepend (cursor_segs, seg);
2034 g_error ("Unknown segment type: %s", seg->type->name);
2039 if (!para_values_set)
2041 style = get_style (layout, &iter);
2042 set_para_values (layout, base_dir, style, display);
2043 release_style (layout, style);
2046 /* Pango doesn't want the trailing paragraph delimiters */
2049 /* Only one character has type G_UNICODE_PARAGRAPH_SEPARATOR in
2050 * Unicode 3.0; update this if that changes.
2052 #define PARAGRAPH_SEPARATOR 0x2029
2055 if (layout_byte_offset > 0)
2057 const char *prev = g_utf8_prev_char (text + layout_byte_offset);
2058 ch = g_utf8_get_char (prev);
2059 if (ch == PARAGRAPH_SEPARATOR || ch == '\r' || ch == '\n')
2060 layout_byte_offset = prev - text; /* chop off */
2062 if (ch == '\n' && layout_byte_offset > 0)
2064 /* Possibly chop a CR as well */
2065 prev = g_utf8_prev_char (text + layout_byte_offset);
2067 --layout_byte_offset;
2072 pango_layout_set_text (display->layout, text, layout_byte_offset);
2073 pango_layout_set_attributes (display->layout, attrs);
2075 tmp_list1 = cursor_byte_offsets;
2076 tmp_list2 = cursor_segs;
2079 add_cursor (layout, display, tmp_list2->data,
2080 GPOINTER_TO_INT (tmp_list1->data));
2081 tmp_list1 = tmp_list1->next;
2082 tmp_list2 = tmp_list2->next;
2084 g_slist_free (cursor_byte_offsets);
2085 g_slist_free (cursor_segs);
2087 pango_layout_get_extents (display->layout, NULL, &extents);
2089 display->width = PIXEL_BOUND (extents.width) + display->left_margin + display->right_margin;
2090 display->height += PANGO_PIXELS (extents.height);
2092 /* Free this if we aren't in a loop */
2093 if (layout->wrap_loop_count == 0)
2094 invalidate_cached_style (layout);
2097 pango_attr_list_unref (attrs);
2099 layout->one_display_cache = display;
2102 allocate_child_widgets (layout, display);
2108 gtk_text_layout_free_line_display (GtkTextLayout *layout,
2109 GtkTextLineDisplay *display)
2111 if (display != layout->one_display_cache)
2113 if (display->layout)
2114 g_object_unref (display->layout);
2116 if (display->cursors)
2118 g_slist_foreach (display->cursors, (GFunc)g_free, NULL);
2119 g_slist_free (display->cursors);
2121 g_slist_free (display->shaped_objects);
2127 /* Functions to convert iter <=> index for the line of a GtkTextLineDisplay
2128 * taking into account the preedit string and invisible text if necessary.
2131 line_display_iter_to_index (GtkTextLayout *layout,
2132 GtkTextLineDisplay *display,
2133 const GtkTextIter *iter)
2137 g_return_val_if_fail (_gtk_text_iter_get_text_line (iter) == display->line, 0);
2139 index = gtk_text_iter_get_visible_line_index (iter);
2141 if (layout->preedit_len > 0 && display->insert_index >= 0)
2143 if (index >= display->insert_index)
2144 index += layout->preedit_len;
2151 line_display_index_to_iter (GtkTextLayout *layout,
2152 GtkTextLineDisplay *display,
2157 g_return_if_fail (!_gtk_text_line_is_last (display->line,
2158 _gtk_text_buffer_get_btree (layout->buffer)));
2160 if (layout->preedit_len > 0 && display->insert_index >= 0)
2162 if (index >= display->insert_index + layout->preedit_len)
2163 index -= layout->preedit_len;
2164 else if (index > display->insert_index)
2166 index = display->insert_index;
2171 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
2172 iter, display->line, 0);
2174 gtk_text_iter_set_visible_line_index (iter, index);
2176 if (_gtk_text_iter_get_text_line (iter) != display->line)
2178 /* Clamp to end of line - really this clamping should have been done
2179 * before here, maybe in Pango, this is a broken band-aid I think
2181 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
2182 iter, display->line, 0);
2184 if (!gtk_text_iter_ends_line (iter))
2185 gtk_text_iter_forward_to_line_end (iter);
2188 gtk_text_iter_forward_chars (iter, trailing);
2192 get_line_at_y (GtkTextLayout *layout,
2199 if (y > layout->height)
2202 *line = _gtk_text_btree_find_line_by_y (_gtk_text_buffer_get_btree (layout->buffer),
2203 layout, y, line_top);
2206 *line = _gtk_text_btree_get_end_iter_line (_gtk_text_buffer_get_btree (layout->buffer));
2210 _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
2216 * gtk_text_layout_get_line_at_y:
2217 * @layout: a #GtkLayout
2218 * @target_iter: the iterator in which the result is stored
2219 * @y: the y positition
2220 * @line_top: location to store the y coordinate of the
2221 * top of the line. (Can by %NULL.)
2223 * Get the iter at the beginning of the line which is displayed
2227 gtk_text_layout_get_line_at_y (GtkTextLayout *layout,
2228 GtkTextIter *target_iter,
2234 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
2235 g_return_if_fail (target_iter != NULL);
2237 get_line_at_y (layout, y, &line, line_top);
2238 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
2239 target_iter, line, 0);
2243 gtk_text_layout_get_iter_at_pixel (GtkTextLayout *layout,
2244 GtkTextIter *target_iter,
2248 gint byte_index, trailing;
2250 GtkTextLineDisplay *display;
2252 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
2253 g_return_if_fail (target_iter != NULL);
2255 get_line_at_y (layout, y, &line, &line_top);
2257 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2259 x -= display->x_offset;
2260 y -= line_top + display->top_margin;
2262 /* If we are below the layout, position the cursor at the last character
2265 if (y > display->height - display->top_margin - display->bottom_margin)
2267 byte_index = _gtk_text_line_byte_count (line);
2272 /* Ignore the "outside" return value from pango. Pango is doing
2273 * the right thing even if we are outside the layout in the
2276 pango_layout_xy_to_index (display->layout, x * PANGO_SCALE, y * PANGO_SCALE,
2277 &byte_index, &trailing);
2280 line_display_index_to_iter (layout, display, target_iter, byte_index, trailing);
2282 gtk_text_layout_free_line_display (layout, display);
2286 * gtk_text_layout_get_cursor_locations:
2287 * @layout: a #GtkTextLayout
2288 * @iter: a #GtkTextIter
2289 * @strong_pos: location to store the strong cursor position (may be %NULL)
2290 * @weak_pos: location to store the weak cursor position (may be %NULL)
2292 * Given an iterator within a text layout, determine the positions of the
2293 * strong and weak cursors if the insertion point is at that
2294 * iterator. The position of each cursor is stored as a zero-width
2295 * rectangle. The strong cursor location is the location where
2296 * characters of the directionality equal to the base direction of the
2297 * paragraph are inserted. The weak cursor location is the location
2298 * where characters of the directionality opposite to the base
2299 * direction of the paragraph are inserted.
2302 gtk_text_layout_get_cursor_locations (GtkTextLayout *layout,
2304 GdkRectangle *strong_pos,
2305 GdkRectangle *weak_pos)
2308 GtkTextLineDisplay *display;
2311 GtkTextIter insert_iter;
2313 PangoRectangle pango_strong_pos;
2314 PangoRectangle pango_weak_pos;
2316 g_return_if_fail (layout != NULL);
2317 g_return_if_fail (iter != NULL);
2319 line = _gtk_text_iter_get_text_line (iter);
2320 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2321 index = line_display_iter_to_index (layout, display, iter);
2323 line_top = _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
2326 gtk_text_buffer_get_iter_at_mark (layout->buffer, &insert_iter,
2327 gtk_text_buffer_get_mark (layout->buffer,
2330 if (gtk_text_iter_equal (iter, &insert_iter))
2331 index += layout->preedit_cursor - layout->preedit_len;
2333 pango_layout_get_cursor_pos (display->layout, index,
2334 strong_pos ? &pango_strong_pos : NULL,
2335 weak_pos ? &pango_weak_pos : NULL);
2339 strong_pos->x = display->x_offset + pango_strong_pos.x / PANGO_SCALE;
2340 strong_pos->y = line_top + display->top_margin + pango_strong_pos.y / PANGO_SCALE;
2341 strong_pos->width = 0;
2342 strong_pos->height = pango_strong_pos.height / PANGO_SCALE;
2347 weak_pos->x = display->x_offset + pango_weak_pos.x / PANGO_SCALE;
2348 weak_pos->y = line_top + display->top_margin + pango_weak_pos.y / PANGO_SCALE;
2349 weak_pos->width = 0;
2350 weak_pos->height = pango_weak_pos.height / PANGO_SCALE;
2353 gtk_text_layout_free_line_display (layout, display);
2357 * gtk_text_layout_get_line_yrange:
2358 * @layout: a #GtkTextLayout
2359 * @iter: a #GtkTextIter
2360 * @y: location to store the top of the paragraph in pixels,
2362 * @height location to store the height of the paragraph in pixels,
2365 * Find the range of y coordinates for the paragraph containing
2369 gtk_text_layout_get_line_yrange (GtkTextLayout *layout,
2370 const GtkTextIter *iter,
2376 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
2377 g_return_if_fail (_gtk_text_iter_get_btree (iter) == _gtk_text_buffer_get_btree (layout->buffer));
2379 line = _gtk_text_iter_get_text_line (iter);
2382 *y = _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
2386 GtkTextLineData *line_data = _gtk_text_line_get_data (line, layout);
2388 *height = line_data->height;
2395 * _gtk_text_layout_get_line_xrange:
2396 * @layout: a #GtkTextLayout
2397 * @iter: a #GtkTextIter
2398 * @x: location to store the top of the paragraph in pixels,
2400 * @width location to store the height of the paragraph in pixels,
2403 * Find the range of X coordinates for the paragraph containing
2404 * the given iter. Private for 2.0 due to API freeze, could
2405 * be made public for 2.2.
2408 _gtk_text_layout_get_line_xrange (GtkTextLayout *layout,
2409 const GtkTextIter *iter,
2415 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
2416 g_return_if_fail (_gtk_text_iter_get_btree (iter) == _gtk_text_buffer_get_btree (layout->buffer));
2418 line = _gtk_text_iter_get_text_line (iter);
2421 *x = 0; /* FIXME This is wrong; should represent the first available cursor position */
2425 GtkTextLineData *line_data = _gtk_text_line_get_data (line, layout);
2427 *width = line_data->width;
2434 gtk_text_layout_get_iter_location (GtkTextLayout *layout,
2435 const GtkTextIter *iter,
2438 PangoRectangle pango_rect;
2441 GtkTextLineDisplay *display;
2445 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
2446 g_return_if_fail (_gtk_text_iter_get_btree (iter) == _gtk_text_buffer_get_btree (layout->buffer));
2447 g_return_if_fail (rect != NULL);
2449 tree = _gtk_text_iter_get_btree (iter);
2450 line = _gtk_text_iter_get_text_line (iter);
2452 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2454 rect->y = _gtk_text_btree_find_line_top (tree, line, layout);
2456 x_offset = display->x_offset * PANGO_SCALE;
2458 byte_index = gtk_text_iter_get_line_index (iter);
2460 pango_layout_index_to_pos (display->layout, byte_index, &pango_rect);
2462 rect->x = PANGO_PIXELS (x_offset + pango_rect.x);
2463 rect->y += PANGO_PIXELS (pango_rect.y) + display->top_margin;
2464 rect->width = PANGO_PIXELS (pango_rect.width);
2465 rect->height = PANGO_PIXELS (pango_rect.height);
2467 gtk_text_layout_free_line_display (layout, display);
2472 /* Find the iter for the logical beginning of the first display line whose
2473 * top y is >= y. If none exists, move the iter to the logical beginning
2474 * of the last line in the buffer.
2477 find_display_line_below (GtkTextLayout *layout,
2481 GtkTextLine *line, *next;
2482 GtkTextLine *found_line = NULL;
2484 gint found_byte = 0;
2486 line = _gtk_text_btree_find_line_by_y (_gtk_text_buffer_get_btree (layout->buffer),
2487 layout, y, &line_top);
2491 _gtk_text_btree_get_end_iter_line (_gtk_text_buffer_get_btree (layout->buffer));
2494 _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
2498 while (line && !found_line)
2500 GtkTextLineDisplay *display = gtk_text_layout_get_line_display (layout, line, FALSE);
2501 PangoLayoutIter *layout_iter;
2503 layout_iter = pango_layout_get_iter (display->layout);
2505 line_top += display->top_margin;
2509 gint first_y, last_y;
2510 PangoLayoutLine *layout_line = pango_layout_iter_get_line (layout_iter);
2512 found_byte = layout_line->start_index;
2520 pango_layout_iter_get_line_yrange (layout_iter, &first_y, &last_y);
2521 line_top += (last_y - first_y) / PANGO_SCALE;
2523 while (pango_layout_iter_next_line (layout_iter));
2525 pango_layout_iter_free (layout_iter);
2527 line_top += display->bottom_margin;
2528 gtk_text_layout_free_line_display (layout, display);
2530 next = _gtk_text_line_next_excluding_last (line);
2537 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
2538 iter, found_line, found_byte);
2541 /* Find the iter for the logical beginning of the last display line whose
2542 * top y is >= y. If none exists, move the iter to the logical beginning
2543 * of the first line in the buffer.
2546 find_display_line_above (GtkTextLayout *layout,
2551 GtkTextLine *found_line = NULL;
2553 gint found_byte = 0;
2555 line = _gtk_text_btree_find_line_by_y (_gtk_text_buffer_get_btree (layout->buffer), layout, y, &line_top);
2558 line = _gtk_text_btree_get_end_iter_line (_gtk_text_buffer_get_btree (layout->buffer));
2560 line_top = _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer), line, layout);
2563 while (line && !found_line)
2565 GtkTextLineDisplay *display = gtk_text_layout_get_line_display (layout, line, FALSE);
2566 PangoRectangle logical_rect;
2567 PangoLayoutIter *layout_iter;
2570 layout_iter = pango_layout_get_iter (display->layout);
2572 line_top -= display->top_margin + display->bottom_margin;
2573 pango_layout_iter_get_layout_extents (layout_iter, NULL, &logical_rect);
2574 line_top -= logical_rect.height / PANGO_SCALE;
2576 tmp_top = line_top + display->top_margin;
2580 gint first_y, last_y;
2581 PangoLayoutLine *layout_line = pango_layout_iter_get_line (layout_iter);
2583 found_byte = layout_line->start_index;
2585 pango_layout_iter_get_line_yrange (layout_iter, &first_y, &last_y);
2587 tmp_top -= (last_y - first_y) / PANGO_SCALE;
2592 pango_layout_iter_free (layout_iter);
2596 while (pango_layout_iter_next_line (layout_iter));
2598 pango_layout_iter_free (layout_iter);
2600 gtk_text_layout_free_line_display (layout, display);
2602 line = _gtk_text_line_previous (line);
2608 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
2609 iter, found_line, found_byte);
2611 gtk_text_buffer_get_iter_at_offset (layout->buffer, iter, 0);
2615 * gtk_text_layout_clamp_iter_to_vrange:
2616 * @layout: a #GtkTextLayout
2617 * @iter: a #GtkTextIter
2618 * @top: the top of the range
2619 * @bottom: the bottom the range
2621 * If the iterator is not fully in the range @top <= y < @bottom,
2622 * then, if possible, move it the minimum distance so that the
2623 * iterator in this range.
2625 * Returns: %TRUE if the iterator was moved, otherwise %FALSE.
2628 gtk_text_layout_clamp_iter_to_vrange (GtkTextLayout *layout,
2633 GdkRectangle iter_rect;
2635 gtk_text_layout_get_iter_location (layout, iter, &iter_rect);
2637 /* If the iter is at least partially above the range, put the iter
2638 * at the first fully visible line after the range.
2640 if (iter_rect.y < top)
2642 find_display_line_below (layout, iter, top);
2646 /* Otherwise, if the iter is at least partially below the screen, put the
2647 * iter on the last logical position of the last completely visible
2650 else if (iter_rect.y + iter_rect.height > bottom)
2652 find_display_line_above (layout, iter, bottom);
2661 * gtk_text_layout_move_iter_to_previous_line:
2662 * @layout: a #GtkLayout
2663 * @iter: a #GtkTextIter
2665 * Move the iterator to the beginning of the previous line. The lines
2666 * of a wrapped paragraph are treated as distinct for this operation.
2669 gtk_text_layout_move_iter_to_previous_line (GtkTextLayout *layout,
2673 GtkTextLineDisplay *display;
2676 PangoLayoutLine *layout_line;
2678 gboolean update_byte = FALSE;
2680 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), FALSE);
2681 g_return_val_if_fail (iter != NULL, FALSE);
2686 line = _gtk_text_iter_get_text_line (iter);
2687 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2688 line_byte = line_display_iter_to_index (layout, display, iter);
2690 /* If display->height == 0 then the line is invisible, so don't
2693 while (display->height == 0)
2695 GtkTextLine *prev_line;
2697 prev_line = _gtk_text_line_previous (line);
2699 if (prev_line == NULL)
2701 line_display_index_to_iter (layout, display, iter, 0, 0);
2705 gtk_text_layout_free_line_display (layout, display);
2708 display = gtk_text_layout_get_line_display (layout, prev_line, FALSE);
2712 tmp_list = pango_layout_get_lines (display->layout);
2713 layout_line = tmp_list->data;
2717 line_byte = layout_line->start_index + layout_line->length;
2720 if (line_byte < layout_line->length || !tmp_list->next) /* first line of paragraph */
2722 GtkTextLine *prev_line;
2724 prev_line = _gtk_text_line_previous (line);
2727 gtk_text_layout_free_line_display (layout, display);
2729 display = gtk_text_layout_get_line_display (layout, prev_line, FALSE);
2731 if (display->height > 0)
2733 tmp_list = g_slist_last (pango_layout_get_lines (display->layout));
2734 layout_line = tmp_list->data;
2736 line_display_index_to_iter (layout, display, iter,
2737 layout_line->start_index + layout_line->length, 0);
2741 prev_line = _gtk_text_line_previous (prev_line);
2744 if (prev_line == NULL)
2745 line_display_index_to_iter (layout, display, iter, 0, 0);
2749 gint prev_offset = layout_line->start_index;
2751 tmp_list = tmp_list->next;
2754 layout_line = tmp_list->data;
2756 if (line_byte < layout_line->start_index + layout_line->length ||
2759 line_display_index_to_iter (layout, display, iter, prev_offset, 0);
2763 prev_offset = layout_line->start_index;
2764 tmp_list = tmp_list->next;
2770 gtk_text_layout_free_line_display (layout, display);
2773 !gtk_text_iter_equal (iter, &orig) &&
2774 !gtk_text_iter_is_end (iter);
2778 * gtk_text_layout_move_iter_to_next_line:
2779 * @layout: a #GtkLayout
2780 * @iter: a #GtkTextIter
2782 * Move the iterator to the beginning of the next line. The
2783 * lines of a wrapped paragraph are treated as distinct for
2787 gtk_text_layout_move_iter_to_next_line (GtkTextLayout *layout,
2791 GtkTextLineDisplay *display;
2794 gboolean found = FALSE;
2795 gboolean found_after = FALSE;
2796 gboolean first = TRUE;
2798 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), FALSE);
2799 g_return_val_if_fail (iter != NULL, FALSE);
2803 line = _gtk_text_iter_get_text_line (iter);
2805 while (line && !found_after)
2809 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2811 if (display->height == 0)
2816 line_byte = line_display_iter_to_index (layout, display, iter);
2822 tmp_list = pango_layout_get_lines (display->layout);
2823 while (tmp_list && !found_after)
2825 PangoLayoutLine *layout_line = tmp_list->data;
2829 line_display_index_to_iter (layout, display, iter,
2830 layout_line->start_index, 0);
2833 else if (line_byte < layout_line->start_index + layout_line->length || !tmp_list->next)
2836 tmp_list = tmp_list->next;
2841 gtk_text_layout_free_line_display (layout, display);
2843 line = _gtk_text_line_next_excluding_last (line);
2847 gtk_text_buffer_get_end_iter (layout->buffer, iter);
2850 !gtk_text_iter_equal (iter, &orig) &&
2851 !gtk_text_iter_is_end (iter);
2855 * gtk_text_layout_move_iter_to_line_end:
2856 * @layout: a #GtkTextLayout
2857 * @direction: if negative, move to beginning of line, otherwise
2858 move to end of line.
2860 * Move to the beginning or end of a display line.
2863 gtk_text_layout_move_iter_to_line_end (GtkTextLayout *layout,
2868 GtkTextLineDisplay *display;
2873 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), FALSE);
2874 g_return_val_if_fail (iter != NULL, FALSE);
2878 line = _gtk_text_iter_get_text_line (iter);
2879 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2880 line_byte = line_display_iter_to_index (layout, display, iter);
2882 tmp_list = pango_layout_get_lines (display->layout);
2885 PangoLayoutLine *layout_line = tmp_list->data;
2887 if (line_byte < layout_line->start_index + layout_line->length || !tmp_list->next)
2889 line_display_index_to_iter (layout, display, iter,
2890 direction < 0 ? layout_line->start_index : layout_line->start_index + layout_line->length,
2893 /* FIXME: As a bad hack, we move back one position when we
2894 * are inside a paragraph to avoid going to next line on a
2895 * forced break not at whitespace. Real fix is to keep track
2896 * of whether marks are at leading or trailing edge? */
2897 if (direction > 0 && layout_line->length > 0 && !gtk_text_iter_ends_line (iter))
2898 gtk_text_iter_backward_char (iter);
2903 tmp_list = tmp_list->next;
2906 gtk_text_layout_free_line_display (layout, display);
2909 !gtk_text_iter_equal (iter, &orig) &&
2910 !gtk_text_iter_is_end (iter);
2915 * gtk_text_layout_iter_starts_line:
2916 * @layout: a #GtkTextLayout
2917 * @iter: iterator to test
2919 * Tests whether an iterator is at the start of a display line.
2922 gtk_text_layout_iter_starts_line (GtkTextLayout *layout,
2923 const GtkTextIter *iter)
2926 GtkTextLineDisplay *display;
2930 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), FALSE);
2931 g_return_val_if_fail (iter != NULL, FALSE);
2933 line = _gtk_text_iter_get_text_line (iter);
2934 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2935 line_byte = line_display_iter_to_index (layout, display, iter);
2937 tmp_list = pango_layout_get_lines (display->layout);
2940 PangoLayoutLine *layout_line = tmp_list->data;
2942 if (line_byte < layout_line->start_index + layout_line->length ||
2945 /* We're located on this line or the para delimiters before
2948 gtk_text_layout_free_line_display (layout, display);
2950 if (line_byte == layout_line->start_index)
2956 tmp_list = tmp_list->next;
2959 g_assert_not_reached ();
2964 gtk_text_layout_get_iter_at_line (GtkTextLayout *layout,
2969 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
2970 iter, line, byte_offset);
2975 * gtk_text_layout_move_iter_to_x:
2976 * @layout: a #GtkTextLayout
2977 * @iter: a #GtkTextIter
2980 * Keeping the iterator on the same line of the layout, move it to the
2981 * specified X coordinate. The lines of a wrapped paragraph are
2982 * treated as distinct for this operation.
2985 gtk_text_layout_move_iter_to_x (GtkTextLayout *layout,
2990 GtkTextLineDisplay *display;
2992 PangoLayoutIter *layout_iter;
2994 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
2995 g_return_if_fail (iter != NULL);
2997 line = _gtk_text_iter_get_text_line (iter);
2999 display = gtk_text_layout_get_line_display (layout, line, FALSE);
3000 line_byte = line_display_iter_to_index (layout, display, iter);
3002 layout_iter = pango_layout_get_iter (display->layout);
3006 PangoLayoutLine *layout_line = pango_layout_iter_get_line (layout_iter);
3008 if (line_byte < layout_line->start_index + layout_line->length ||
3009 pango_layout_iter_at_last_line (layout_iter))
3011 PangoRectangle logical_rect;
3012 gint byte_index, trailing;
3013 gint x_offset = display->x_offset * PANGO_SCALE;
3015 pango_layout_iter_get_line_extents (layout_iter, NULL, &logical_rect);
3017 pango_layout_line_x_to_index (layout_line,
3018 x * PANGO_SCALE - x_offset - logical_rect.x,
3019 &byte_index, &trailing);
3021 line_display_index_to_iter (layout, display, iter, byte_index, trailing);
3026 while (pango_layout_iter_next_line (layout_iter));
3028 pango_layout_iter_free (layout_iter);
3030 gtk_text_layout_free_line_display (layout, display);
3034 * gtk_text_layout_move_iter_visually:
3035 * @layout: a #GtkTextLayout
3036 * @iter: a #GtkTextIter
3037 * @count: number of characters to move (negative moves left, positive moves right)
3039 * Move the iterator a given number of characters visually, treating
3040 * it as the strong cursor position. If @count is positive, then the
3041 * new strong cursor position will be @count positions to the right of
3042 * the old cursor position. If @count is negative then the new strong
3043 * cursor position will be @count positions to the left of the old
3046 * In the presence of bidirection text, the correspondence
3047 * between logical and visual order will depend on the direction
3048 * of the current run, and there may be jumps when the cursor
3049 * is moved off of the end of a run.
3053 gtk_text_layout_move_iter_visually (GtkTextLayout *layout,
3057 GtkTextLineDisplay *display = NULL;
3060 g_return_val_if_fail (layout != NULL, FALSE);
3061 g_return_val_if_fail (iter != NULL, FALSE);
3067 GtkTextLine *line = _gtk_text_iter_get_text_line (iter);
3069 gint extra_back = 0;
3072 int byte_count = _gtk_text_line_byte_count (line);
3078 display = gtk_text_layout_get_line_display (layout, line, FALSE);
3080 if (layout->cursor_direction == GTK_TEXT_DIR_NONE)
3083 strong = display->direction == layout->cursor_direction;
3085 line_byte = line_display_iter_to_index (layout, display, iter);
3089 pango_layout_move_cursor_visually (display->layout, strong, line_byte, 0, 1, &new_index, &new_trailing);
3094 pango_layout_move_cursor_visually (display->layout, strong, line_byte, 0, -1, &new_index, &new_trailing);
3098 /* We need to handle the preedit string specially. Well, we don't really need to
3099 * handle it specially, since hopefully calling gtk_im_context_reset() will
3100 * remove the preedit string; but if we start off in front of the preedit
3101 * string (logically) and end up in or on the back edge of the preedit string,
3102 * we should move the iter one place farther.
3104 if (layout->preedit_len > 0 && display->insert_index >= 0)
3106 if (line_byte == display->insert_index + layout->preedit_len &&
3107 new_index < display->insert_index + layout->preedit_len)
3109 line_byte = display->insert_index;
3114 if (new_index < 0 || (new_index == 0 && extra_back))
3116 line = _gtk_text_line_previous (line);
3121 gtk_text_layout_free_line_display (layout, display);
3122 display = gtk_text_layout_get_line_display (layout, line, FALSE);
3123 new_index = _gtk_text_line_byte_count (line);
3125 else if (new_index > byte_count)
3127 line = _gtk_text_line_next_excluding_last (line);
3131 gtk_text_layout_free_line_display (layout, display);
3132 display = gtk_text_layout_get_line_display (layout, line, FALSE);
3136 line_display_index_to_iter (layout, display, iter, new_index, new_trailing);
3138 gtk_text_iter_backward_char (iter);
3141 gtk_text_layout_free_line_display (layout, display);
3146 !gtk_text_iter_equal (iter, &orig) &&
3147 !gtk_text_iter_is_end (iter);
3151 gtk_text_layout_spew (GtkTextLayout *layout)
3154 GtkTextDisplayLine *iter;
3156 guint paragraphs = 0;
3157 GtkTextLine *last_line = NULL;
3159 iter = layout->line_list;
3160 while (iter != NULL)
3162 if (iter->line != last_line)
3164 printf ("%5u paragraph (%p)\n", paragraphs, iter->line);
3166 last_line = iter->line;
3169 printf (" %5u y: %d len: %d start: %d bytes: %d\n",
3170 wrapped, iter->y, iter->length, iter->byte_offset,
3177 printf ("Layout %s recompute\n",
3178 layout->need_recompute ? "needs" : "doesn't need");
3180 printf ("Layout pars: %u lines: %u size: %d x %d Screen width: %d\n",
3181 paragraphs, wrapped, layout->width,
3182 layout->height, layout->screen_width);
3186 /* Catch all situations that move the insertion point.
3189 gtk_text_layout_mark_set_handler (GtkTextBuffer *buffer,
3190 const GtkTextIter *location,
3194 GtkTextLayout *layout = GTK_TEXT_LAYOUT (data);
3196 if (mark == gtk_text_buffer_get_insert (buffer))
3197 gtk_text_layout_update_cursor_line (layout);
3201 gtk_text_layout_buffer_insert_text (GtkTextBuffer *textbuffer,
3207 GtkTextLayout *layout = GTK_TEXT_LAYOUT (data);
3209 gtk_text_layout_update_cursor_line (layout);
3213 gtk_text_layout_buffer_delete_range (GtkTextBuffer *textbuffer,
3218 GtkTextLayout *layout = GTK_TEXT_LAYOUT (data);
3220 gtk_text_layout_update_cursor_line (layout);