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
80 #include "gtkmarshalers.h"
81 #include "gtktextlayout.h"
82 #include "gtktextbtree.h"
83 #include "gtktextiterprivate.h"
88 static GtkTextLineData *gtk_text_layout_real_wrap (GtkTextLayout *layout,
91 GtkTextLineData *line_data);
93 static void gtk_text_layout_invalidated (GtkTextLayout *layout);
95 static void gtk_text_layout_real_invalidate (GtkTextLayout *layout,
96 const GtkTextIter *start,
97 const GtkTextIter *end);
98 static void gtk_text_layout_invalidate_cache (GtkTextLayout *layout,
100 static void gtk_text_layout_invalidate_cursor_line (GtkTextLayout *layout);
101 static void gtk_text_layout_real_free_line_data (GtkTextLayout *layout,
103 GtkTextLineData *line_data);
104 static void gtk_text_layout_emit_changed (GtkTextLayout *layout,
109 static void gtk_text_layout_invalidate_all (GtkTextLayout *layout);
111 static PangoAttribute *gtk_text_attr_appearance_new (const GtkTextAppearance *appearance);
125 #define PIXEL_BOUND(d) (((d) + PANGO_SCALE - 1) / PANGO_SCALE)
127 static void gtk_text_layout_init (GtkTextLayout *text_layout);
128 static void gtk_text_layout_class_init (GtkTextLayoutClass *klass);
129 static void gtk_text_layout_finalize (GObject *object);
132 static GtkObjectClass *parent_class = NULL;
133 static guint signals[LAST_SIGNAL] = { 0 };
135 PangoAttrType gtk_text_attr_appearance_type = 0;
138 gtk_text_layout_get_type (void)
140 static GType our_type = 0;
144 static const GTypeInfo our_info =
146 sizeof (GtkTextLayoutClass),
147 (GBaseInitFunc) NULL,
148 (GBaseFinalizeFunc) NULL,
149 (GClassInitFunc) gtk_text_layout_class_init,
150 NULL, /* class_finalize */
151 NULL, /* class_data */
152 sizeof (GtkTextLayout),
154 (GInstanceInitFunc) gtk_text_layout_init
157 our_type = g_type_register_static (G_TYPE_OBJECT, "GtkTextLayout",
165 gtk_text_layout_class_init (GtkTextLayoutClass *klass)
167 GObjectClass *object_class = G_OBJECT_CLASS (klass);
169 parent_class = g_type_class_peek_parent (klass);
171 object_class->finalize = gtk_text_layout_finalize;
173 klass->wrap = gtk_text_layout_real_wrap;
174 klass->invalidate = gtk_text_layout_real_invalidate;
175 klass->free_line_data = gtk_text_layout_real_free_line_data;
177 signals[INVALIDATED] =
178 g_signal_new ("invalidated",
179 G_OBJECT_CLASS_TYPE (object_class),
181 G_STRUCT_OFFSET (GtkTextLayoutClass, invalidated),
183 _gtk_marshal_VOID__VOID,
188 g_signal_new ("changed",
189 G_OBJECT_CLASS_TYPE (object_class),
191 G_STRUCT_OFFSET (GtkTextLayoutClass, changed),
193 _gtk_marshal_VOID__INT_INT_INT,
200 signals[ALLOCATE_CHILD] =
201 g_signal_new ("allocate_child",
202 G_OBJECT_CLASS_TYPE (object_class),
204 G_STRUCT_OFFSET (GtkTextLayoutClass, allocate_child),
206 _gtk_marshal_VOID__OBJECT_INT_INT,
215 gtk_text_layout_init (GtkTextLayout *text_layout)
217 text_layout->cursor_visible = TRUE;
221 gtk_text_layout_new (void)
223 return g_object_new (GTK_TYPE_TEXT_LAYOUT, NULL);
227 free_style_cache (GtkTextLayout *text_layout)
229 if (text_layout->one_style_cache)
231 gtk_text_attributes_unref (text_layout->one_style_cache);
232 text_layout->one_style_cache = NULL;
237 gtk_text_layout_finalize (GObject *object)
239 GtkTextLayout *layout;
241 layout = GTK_TEXT_LAYOUT (object);
243 gtk_text_layout_set_buffer (layout, NULL);
245 if (layout->default_style)
246 gtk_text_attributes_unref (layout->default_style);
247 layout->default_style = NULL;
249 if (layout->ltr_context)
251 g_object_unref (layout->ltr_context);
252 layout->ltr_context = NULL;
254 if (layout->rtl_context)
256 g_object_unref (layout->rtl_context);
257 layout->rtl_context = NULL;
260 if (layout->one_display_cache)
262 GtkTextLineDisplay *tmp_display = layout->one_display_cache;
263 layout->one_display_cache = NULL;
264 gtk_text_layout_free_line_display (layout, tmp_display);
267 (* G_OBJECT_CLASS (parent_class)->finalize) (object);
271 gtk_text_layout_set_buffer (GtkTextLayout *layout,
272 GtkTextBuffer *buffer)
274 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
275 g_return_if_fail (buffer == NULL || GTK_IS_TEXT_BUFFER (buffer));
277 if (layout->buffer == buffer)
280 free_style_cache (layout);
284 _gtk_text_btree_remove_view (_gtk_text_buffer_get_btree (layout->buffer),
287 g_object_unref (layout->buffer);
288 layout->buffer = NULL;
293 layout->buffer = buffer;
295 g_object_ref (buffer);
297 _gtk_text_btree_add_view (_gtk_text_buffer_get_btree (buffer), layout);
302 gtk_text_layout_default_style_changed (GtkTextLayout *layout)
304 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
306 DV (g_print ("invalidating all due to default style change (%s)\n", G_STRLOC));
307 gtk_text_layout_invalidate_all (layout);
311 gtk_text_layout_set_default_style (GtkTextLayout *layout,
312 GtkTextAttributes *values)
314 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
315 g_return_if_fail (values != NULL);
317 if (values == layout->default_style)
320 gtk_text_attributes_ref (values);
322 if (layout->default_style)
323 gtk_text_attributes_unref (layout->default_style);
325 layout->default_style = values;
327 gtk_text_layout_default_style_changed (layout);
331 gtk_text_layout_set_contexts (GtkTextLayout *layout,
332 PangoContext *ltr_context,
333 PangoContext *rtl_context)
335 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
337 if (layout->ltr_context)
338 g_object_unref (ltr_context);
340 layout->ltr_context = ltr_context;
341 g_object_ref (ltr_context);
343 if (layout->rtl_context)
344 g_object_unref (rtl_context);
346 layout->rtl_context = rtl_context;
347 g_object_ref (rtl_context);
349 DV (g_print ("invalidating all due to new pango contexts (%s)\n", G_STRLOC));
350 gtk_text_layout_invalidate_all (layout);
354 * gtk_text_layout_set_cursor_direction:
355 * @direction: the new direction(s) for which to draw cursors.
356 * %GTK_TEXT_DIR_NONE means draw cursors for both
357 * left-to-right insertion and right-to-left insertion.
358 * (The two cursors will be visually distinguished.)
360 * Sets which text directions (left-to-right and/or right-to-left) for
361 * which cursors will be drawn for the insertion point. The visual
362 * point at which new text is inserted depends on whether the new
363 * text is right-to-left or left-to-right, so it may be desired to
364 * make the drawn position of the cursor depend on the keyboard state.
367 gtk_text_layout_set_cursor_direction (GtkTextLayout *layout,
368 GtkTextDirection direction)
370 if (direction != layout->cursor_direction)
372 layout->cursor_direction = direction;
373 gtk_text_layout_invalidate_cursor_line (layout);
378 * gtk_text_layout_get_buffer:
379 * @layout: a #GtkTextLayout
381 * Gets the text buffer used by the layout. See
382 * gtk_text_layout_set_buffer().
384 * Return value: the text buffer used by the layout.
387 gtk_text_layout_get_buffer (GtkTextLayout *layout)
389 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), NULL);
391 return layout->buffer;
395 gtk_text_layout_set_screen_width (GtkTextLayout *layout, gint width)
397 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
398 g_return_if_fail (width >= 0);
399 g_return_if_fail (layout->wrap_loop_count == 0);
401 if (layout->screen_width == width)
404 layout->screen_width = width;
406 DV (g_print ("invalidating all due to new screen width (%s)\n", G_STRLOC));
407 gtk_text_layout_invalidate_all (layout);
411 * gtk_text_layout_set_cursor_visible:
412 * @layout: a #GtkTextLayout
413 * @cursor_visible: If %FALSE, then the insertion cursor will not
414 * be shown, even if the text is editable.
416 * Sets whether the insertion cursor should be shown. Generally,
417 * widgets using #GtkTextLayout will hide the cursor when the
418 * widget does not have the input focus.
421 gtk_text_layout_set_cursor_visible (GtkTextLayout *layout,
422 gboolean cursor_visible)
424 cursor_visible = (cursor_visible != FALSE);
426 if (layout->cursor_visible != cursor_visible)
431 layout->cursor_visible = cursor_visible;
433 /* Now queue a redraw on the paragraph containing the cursor
435 gtk_text_buffer_get_iter_at_mark (layout->buffer, &iter,
436 gtk_text_buffer_get_mark (layout->buffer, "insert"));
438 gtk_text_layout_get_line_yrange (layout, &iter, &y, &height);
439 gtk_text_layout_emit_changed (layout, y, height, height);
441 gtk_text_layout_invalidate_cache (layout, _gtk_text_iter_get_text_line (&iter));
446 * gtk_text_layout_get_cursor_visible:
447 * @layout: a #GtkTextLayout
449 * Returns whether the insertion cursor will be shown.
451 * Return value: if %FALSE, the insertion cursor will not be
452 shown, even if the text is editable.
455 gtk_text_layout_get_cursor_visible (GtkTextLayout *layout)
457 return layout->cursor_visible;
461 * gtk_text_layout_set_preedit_string:
462 * @layout: a #PangoLayout
463 * @preedit_string: a string to display at the insertion point
464 * @preedit_attrs: a #PangoAttrList of attributes that apply to @preedit_string
465 * @cursor_pos: position of cursor within preedit string in chars
467 * Set the preedit string and attributes. The preedit string is a
468 * string showing text that is currently being edited and not
469 * yet committed into the buffer.
472 gtk_text_layout_set_preedit_string (GtkTextLayout *layout,
473 const gchar *preedit_string,
474 PangoAttrList *preedit_attrs,
477 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
478 g_return_if_fail (preedit_attrs != NULL || preedit_string == NULL);
480 if (layout->preedit_string)
481 g_free (layout->preedit_string);
483 if (layout->preedit_attrs)
484 pango_attr_list_unref (layout->preedit_attrs);
488 layout->preedit_string = g_strdup (preedit_string);
489 layout->preedit_len = strlen (layout->preedit_string);
490 pango_attr_list_ref (preedit_attrs);
491 layout->preedit_attrs = preedit_attrs;
493 cursor_pos = CLAMP (cursor_pos, 0, g_utf8_strlen (layout->preedit_string, -1));
494 layout->preedit_cursor = g_utf8_offset_to_pointer (layout->preedit_string, cursor_pos) - layout->preedit_string;
498 layout->preedit_string = NULL;
499 layout->preedit_len = 0;
500 layout->preedit_attrs = NULL;
501 layout->preedit_cursor = 0;
504 gtk_text_layout_invalidate_cursor_line (layout);
508 gtk_text_layout_get_size (GtkTextLayout *layout,
512 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
515 *width = layout->width;
518 *height = layout->height;
522 gtk_text_layout_invalidated (GtkTextLayout *layout)
524 g_signal_emit (layout, signals[INVALIDATED], 0);
528 gtk_text_layout_emit_changed (GtkTextLayout *layout,
533 g_signal_emit (layout, signals[CHANGED], 0, y, old_height, new_height);
537 gtk_text_layout_changed (GtkTextLayout *layout,
542 /* Check if the range intersects our cached line display,
543 * and invalidate the cached line if so.
545 if (layout->one_display_cache)
547 GtkTextLine *line = layout->one_display_cache->line;
548 gint cache_y = _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
550 gint cache_height = layout->one_display_cache->height;
552 if (cache_y + cache_height > y && cache_y < y + old_height)
553 gtk_text_layout_invalidate_cache (layout, line);
556 gtk_text_layout_emit_changed (layout, y, old_height, new_height);
560 gtk_text_layout_free_line_data (GtkTextLayout *layout,
562 GtkTextLineData *line_data)
564 (* GTK_TEXT_LAYOUT_GET_CLASS (layout)->free_line_data)
565 (layout, line, line_data);
569 gtk_text_layout_invalidate (GtkTextLayout *layout,
570 const GtkTextIter *start_index,
571 const GtkTextIter *end_index)
573 (* GTK_TEXT_LAYOUT_GET_CLASS (layout)->invalidate)
574 (layout, start_index, end_index);
578 gtk_text_layout_wrap (GtkTextLayout *layout,
581 GtkTextLineData *line_data)
583 return (* GTK_TEXT_LAYOUT_GET_CLASS (layout)->wrap) (layout, line, line_data);
587 gtk_text_layout_get_lines (GtkTextLayout *layout,
588 /* [top_y, bottom_y) */
593 GtkTextLine *first_btree_line;
594 GtkTextLine *last_btree_line;
598 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), NULL);
599 g_return_val_if_fail (bottom_y > top_y, NULL);
604 _gtk_text_btree_find_line_by_y (_gtk_text_buffer_get_btree (layout->buffer),
605 layout, top_y, first_line_y);
606 if (first_btree_line == NULL)
612 /* -1 since bottom_y is one past */
614 _gtk_text_btree_find_line_by_y (_gtk_text_buffer_get_btree (layout->buffer),
615 layout, bottom_y - 1, NULL);
617 if (!last_btree_line)
619 _gtk_text_btree_get_end_iter_line (_gtk_text_buffer_get_btree (layout->buffer));
621 g_assert (last_btree_line != NULL);
623 line = first_btree_line;
626 retval = g_slist_prepend (retval, line);
628 if (line == last_btree_line)
631 line = _gtk_text_line_next_excluding_last (line);
634 retval = g_slist_reverse (retval);
640 invalidate_cached_style (GtkTextLayout *layout)
642 free_style_cache (layout);
645 /* These should be called around a loop which wraps a CONTIGUOUS bunch
646 * of display lines. If the lines aren't contiguous you can't call
650 gtk_text_layout_wrap_loop_start (GtkTextLayout *layout)
652 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
653 g_return_if_fail (layout->one_style_cache == NULL);
655 layout->wrap_loop_count += 1;
659 gtk_text_layout_wrap_loop_end (GtkTextLayout *layout)
661 g_return_if_fail (layout->wrap_loop_count > 0);
663 layout->wrap_loop_count -= 1;
665 if (layout->wrap_loop_count == 0)
667 /* We cache a some stuff if we're iterating over some lines wrapping
668 * them. This cleans it up.
670 /* Nuke our cached style */
671 invalidate_cached_style (layout);
672 g_assert (layout->one_style_cache == NULL);
677 gtk_text_layout_invalidate_all (GtkTextLayout *layout)
682 if (layout->buffer == NULL)
685 gtk_text_buffer_get_bounds (layout->buffer, &start, &end);
687 gtk_text_layout_invalidate (layout, &start, &end);
691 gtk_text_layout_invalidate_cache (GtkTextLayout *layout,
694 if (layout->one_display_cache && line == layout->one_display_cache->line)
696 GtkTextLineDisplay *tmp_display = layout->one_display_cache;
697 layout->one_display_cache = NULL;
698 gtk_text_layout_free_line_display (layout, tmp_display);
702 /* Now invalidate the paragraph containing the cursor
705 gtk_text_layout_invalidate_cursor_line (GtkTextLayout *layout)
709 GtkTextLineData *line_data;
711 gtk_text_buffer_get_iter_at_mark (layout->buffer, &iter,
712 gtk_text_buffer_get_mark (layout->buffer, "insert"));
714 line = _gtk_text_iter_get_text_line (&iter);
715 line_data = _gtk_text_line_get_data (line, layout);
718 gtk_text_layout_invalidate_cache (layout, line);
719 _gtk_text_line_invalidate_wrap (line, line_data);
720 gtk_text_layout_invalidated (layout);
725 gtk_text_layout_real_invalidate (GtkTextLayout *layout,
726 const GtkTextIter *start,
727 const GtkTextIter *end)
730 GtkTextLine *last_line;
732 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
733 g_return_if_fail (layout->wrap_loop_count == 0);
735 /* Because we may be invalidating a mark, it's entirely possible
736 * that gtk_text_iter_equal (start, end) in which case we
737 * should still invalidate the line they are both on. i.e.
738 * we always invalidate the line with "start" even
739 * if there's an empty range.
743 gtk_text_view_index_spew (start_index, "invalidate start");
744 gtk_text_view_index_spew (end_index, "invalidate end");
747 last_line = _gtk_text_iter_get_text_line (end);
748 line = _gtk_text_iter_get_text_line (start);
752 GtkTextLineData *line_data = _gtk_text_line_get_data (line, layout);
754 gtk_text_layout_invalidate_cache (layout, line);
757 _gtk_text_line_invalidate_wrap (line, line_data);
759 if (line == last_line)
762 line = _gtk_text_line_next_excluding_last (line);
765 gtk_text_layout_invalidated (layout);
769 gtk_text_layout_real_free_line_data (GtkTextLayout *layout,
771 GtkTextLineData *line_data)
773 gtk_text_layout_invalidate_cache (layout, line);
781 * gtk_text_layout_is_valid:
782 * @layout: a #GtkTextLayout
784 * Check if there are any invalid regions in a #GtkTextLayout's buffer
786 * Return value: %TRUE if any invalid regions were found
789 gtk_text_layout_is_valid (GtkTextLayout *layout)
791 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), FALSE);
793 return _gtk_text_btree_is_valid (_gtk_text_buffer_get_btree (layout->buffer),
798 update_layout_size (GtkTextLayout *layout)
800 _gtk_text_btree_get_view_size (_gtk_text_buffer_get_btree (layout->buffer),
802 &layout->width, &layout->height);
806 * gtk_text_layout_validate_yrange:
807 * @layout: a #GtkTextLayout
808 * @anchor: iter pointing into a line that will be used as the
810 * @y0_: offset from the top of the line pointed to by @anchor at
811 * which to begin validation. (The offset here is in pixels
813 * @y1_: offset from the top of the line pointed to by @anchor at
814 * which to end validation. (The offset here is in pixels
817 * Ensure that a region of a #GtkTextLayout is valid. The ::changed
818 * signal will be emitted if any lines are validated.
821 gtk_text_layout_validate_yrange (GtkTextLayout *layout,
827 GtkTextLine *first_line = NULL;
828 GtkTextLine *last_line = NULL;
830 gint delta_height = 0;
831 gint first_line_y = 0; /* Quiet GCC */
832 gint last_line_y = 0; /* Quiet GCC */
834 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
841 /* Validate backwards from the anchor line to y0
843 line = _gtk_text_iter_get_text_line (anchor);
844 line = _gtk_text_line_previous (line);
846 while (line && seen < -y0)
848 GtkTextLineData *line_data = _gtk_text_line_get_data (line, layout);
849 if (!line_data || !line_data->valid)
851 gint old_height = line_data ? line_data->height : 0;
853 _gtk_text_btree_validate_line (_gtk_text_buffer_get_btree (layout->buffer),
855 line_data = _gtk_text_line_get_data (line, layout);
857 delta_height += line_data->height - old_height;
860 first_line_y = -seen - line_data->height;
868 seen += line_data->height;
869 line = _gtk_text_line_previous (line);
872 /* Validate forwards to y1 */
873 line = _gtk_text_iter_get_text_line (anchor);
875 while (line && seen < y1)
877 GtkTextLineData *line_data = _gtk_text_line_get_data (line, layout);
878 if (!line_data || !line_data->valid)
880 gint old_height = line_data ? line_data->height : 0;
882 _gtk_text_btree_validate_line (_gtk_text_buffer_get_btree (layout->buffer),
884 line_data = _gtk_text_line_get_data (line, layout);
886 delta_height += line_data->height - old_height;
894 last_line_y = seen + line_data->height;
897 seen += line_data->height;
898 line = _gtk_text_line_next_excluding_last (line);
901 /* If we found and validated any invalid lines, update size and
902 * emit the changed signal
908 update_layout_size (layout);
910 line_top = _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
913 gtk_text_layout_emit_changed (layout,
915 last_line_y - first_line_y - delta_height,
916 last_line_y - first_line_y);
921 * gtk_text_layout_validate:
922 * @tree: a #GtkTextLayout
923 * @max_pixels: the maximum number of pixels to validate. (No more
924 * than one paragraph beyond this limit will be validated)
926 * Validate regions of a #GtkTextLayout. The ::changed signal will
927 * be emitted for each region validated.
930 gtk_text_layout_validate (GtkTextLayout *layout,
933 gint y, old_height, new_height;
935 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
937 while (max_pixels > 0 &&
938 _gtk_text_btree_validate (_gtk_text_buffer_get_btree (layout->buffer),
940 &y, &old_height, &new_height))
942 max_pixels -= new_height;
944 update_layout_size (layout);
945 gtk_text_layout_emit_changed (layout, y, old_height, new_height);
949 static GtkTextLineData*
950 gtk_text_layout_real_wrap (GtkTextLayout *layout,
953 GtkTextLineData *line_data)
955 GtkTextLineDisplay *display;
957 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), NULL);
958 g_return_val_if_fail (line != NULL, NULL);
960 if (line_data == NULL)
962 line_data = _gtk_text_line_data_new (layout, line);
963 _gtk_text_line_add_data (line, line_data);
966 display = gtk_text_layout_get_line_display (layout, line, TRUE);
967 line_data->width = display->width;
968 line_data->height = display->height;
969 line_data->valid = TRUE;
970 gtk_text_layout_free_line_display (layout, display);
976 * Layout utility functions
979 /* If you get the style with get_style () you need to call
980 release_style () to free it. */
981 static GtkTextAttributes*
982 get_style (GtkTextLayout *layout,
983 const GtkTextIter *iter)
987 GtkTextAttributes *style;
989 /* If we have the one-style cache, then it means
990 that we haven't seen a toggle since we filled in the
993 if (layout->one_style_cache != NULL)
995 gtk_text_attributes_ref (layout->one_style_cache);
996 return layout->one_style_cache;
999 g_assert (layout->one_style_cache == NULL);
1001 /* Get the tags at this spot */
1002 tags = _gtk_text_btree_get_tags (iter, &tag_count);
1004 /* No tags, use default style */
1005 if (tags == NULL || tag_count == 0)
1007 /* One ref for the return value, one ref for the
1008 layout->one_style_cache reference */
1009 gtk_text_attributes_ref (layout->default_style);
1010 gtk_text_attributes_ref (layout->default_style);
1011 layout->one_style_cache = layout->default_style;
1016 return layout->default_style;
1019 /* Sort tags in ascending order of priority */
1020 _gtk_text_tag_array_sort (tags, tag_count);
1022 style = gtk_text_attributes_new ();
1024 gtk_text_attributes_copy_values (layout->default_style,
1027 _gtk_text_attributes_fill_from_tags (style,
1033 g_assert (style->refcount == 1);
1035 /* Leave this style as the last one seen */
1036 g_assert (layout->one_style_cache == NULL);
1037 gtk_text_attributes_ref (style); /* ref held by layout->one_style_cache */
1038 layout->one_style_cache = style;
1040 /* Returning yet another refcount */
1045 release_style (GtkTextLayout *layout,
1046 GtkTextAttributes *style)
1048 g_return_if_fail (style != NULL);
1049 g_return_if_fail (style->refcount > 0);
1051 gtk_text_attributes_unref (style);
1058 /* This function tries to optimize the case where a line
1059 is completely invisible */
1061 totally_invisible_line (GtkTextLayout *layout,
1065 GtkTextLineSegment *seg;
1068 /* Check if the first char is visible, if so we are partially visible.
1069 * Note that we have to check this since we don't know the current
1070 * invisible/noninvisible toggle state; this function can use the whole btree
1073 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
1076 if (!_gtk_text_btree_char_is_invisible (iter))
1080 seg = line->segments;
1084 if (seg->byte_count > 0)
1085 bytes += seg->byte_count;
1087 /* Note that these two tests can cause us to bail out
1088 * when we shouldn't, because a higher-priority tag
1089 * may override these settings. However the important
1090 * thing is to only invisible really-invisible lines, rather
1091 * than to invisible all really-invisible lines.
1094 else if (seg->type == >k_text_toggle_on_type)
1096 invalidate_cached_style (layout);
1098 /* Bail out if an elision-unsetting tag begins */
1099 if (seg->body.toggle.info->tag->invisible_set &&
1100 !seg->body.toggle.info->tag->values->invisible)
1103 else if (seg->type == >k_text_toggle_off_type)
1105 invalidate_cached_style (layout);
1107 /* Bail out if an elision-setting tag ends */
1108 if (seg->body.toggle.info->tag->invisible_set &&
1109 seg->body.toggle.info->tag->values->invisible)
1116 if (seg != NULL) /* didn't reach line end */
1123 set_para_values (GtkTextLayout *layout,
1124 GtkTextAttributes *style,
1125 GtkTextLineDisplay *display)
1127 PangoAlignment pango_align = PANGO_ALIGN_LEFT;
1130 display->direction = style->direction;
1132 if (display->direction == GTK_TEXT_DIR_LTR)
1133 display->layout = pango_layout_new (layout->ltr_context);
1135 display->layout = pango_layout_new (layout->rtl_context);
1137 switch (style->justification)
1139 case GTK_JUSTIFY_LEFT:
1140 pango_align = (style->direction == GTK_TEXT_DIR_LTR) ? PANGO_ALIGN_LEFT : PANGO_ALIGN_RIGHT;
1142 case GTK_JUSTIFY_RIGHT:
1143 pango_align = (style->direction == GTK_TEXT_DIR_LTR) ? PANGO_ALIGN_RIGHT : PANGO_ALIGN_LEFT;
1145 case GTK_JUSTIFY_CENTER:
1146 pango_align = PANGO_ALIGN_CENTER;
1148 case GTK_JUSTIFY_FILL:
1149 g_warning ("FIXME we don't support GTK_JUSTIFY_FILL yet");
1152 g_assert_not_reached ();
1156 pango_layout_set_alignment (display->layout, pango_align);
1157 pango_layout_set_spacing (display->layout,
1158 style->pixels_inside_wrap * PANGO_SCALE);
1161 pango_layout_set_tabs (display->layout, style->tabs);
1163 display->top_margin = style->pixels_above_lines;
1164 display->height = style->pixels_above_lines + style->pixels_below_lines;
1165 display->bottom_margin = style->pixels_below_lines;
1166 display->left_margin = style->left_margin;
1167 display->right_margin = style->right_margin;
1169 display->x_offset = display->left_margin;
1171 pango_layout_set_indent (display->layout,
1172 style->indent * PANGO_SCALE);
1174 switch (style->wrap_mode)
1177 layout_width = layout->screen_width - display->left_margin - display->right_margin;
1178 pango_layout_set_width (display->layout, layout_width * PANGO_SCALE);
1179 pango_layout_set_wrap (display->layout, PANGO_WRAP_CHAR);
1183 layout_width = layout->screen_width - display->left_margin - display->right_margin;
1184 pango_layout_set_width (display->layout, layout_width * PANGO_SCALE);
1185 pango_layout_set_wrap (display->layout, PANGO_WRAP_WORD);
1188 case GTK_WRAP_WORD_CHAR:
1189 layout_width = layout->screen_width - display->left_margin - display->right_margin;
1190 pango_layout_set_width (display->layout, layout_width * PANGO_SCALE);
1191 pango_layout_set_wrap (display->layout, PANGO_WRAP_WORD_CHAR);
1198 display->total_width = MAX (layout->screen_width, layout->width) - display->left_margin - display->right_margin;
1201 static PangoAttribute *
1202 gtk_text_attr_appearance_copy (const PangoAttribute *attr)
1204 const GtkTextAttrAppearance *appearance_attr = (const GtkTextAttrAppearance *)attr;
1206 return gtk_text_attr_appearance_new (&appearance_attr->appearance);
1210 gtk_text_attr_appearance_destroy (PangoAttribute *attr)
1212 GtkTextAppearance *appearance = &((GtkTextAttrAppearance *)attr)->appearance;
1214 if (appearance->bg_stipple)
1215 g_object_unref (appearance->bg_stipple);
1216 if (appearance->fg_stipple)
1217 g_object_unref (appearance->fg_stipple);
1223 gtk_text_attr_appearance_compare (const PangoAttribute *attr1,
1224 const PangoAttribute *attr2)
1226 const GtkTextAppearance *appearance1 = &((const GtkTextAttrAppearance *)attr1)->appearance;
1227 const GtkTextAppearance *appearance2 = &((const GtkTextAttrAppearance *)attr2)->appearance;
1229 return (gdk_color_equal (&appearance1->fg_color, &appearance2->fg_color) &&
1230 gdk_color_equal (&appearance1->bg_color, &appearance2->bg_color) &&
1231 appearance1->fg_stipple == appearance2->fg_stipple &&
1232 appearance1->bg_stipple == appearance2->bg_stipple &&
1233 appearance1->underline == appearance2->underline &&
1234 appearance1->strikethrough == appearance2->strikethrough &&
1235 appearance1->draw_bg == appearance2->draw_bg);
1240 * gtk_text_attr_appearance_new:
1243 * Create a new font description attribute. (This attribute
1244 * allows setting family, style, weight, variant, stretch,
1245 * and size simultaneously.)
1249 static PangoAttribute *
1250 gtk_text_attr_appearance_new (const GtkTextAppearance *appearance)
1252 static PangoAttrClass klass = {
1254 gtk_text_attr_appearance_copy,
1255 gtk_text_attr_appearance_destroy,
1256 gtk_text_attr_appearance_compare
1259 GtkTextAttrAppearance *result;
1262 klass.type = gtk_text_attr_appearance_type =
1263 pango_attr_type_register ("GtkTextAttrAppearance");
1265 result = g_new (GtkTextAttrAppearance, 1);
1266 result->attr.klass = &klass;
1268 result->appearance = *appearance;
1270 if (appearance->bg_stipple)
1271 g_object_ref (appearance->bg_stipple);
1272 if (appearance->fg_stipple)
1273 g_object_ref (appearance->fg_stipple);
1275 return (PangoAttribute *)result;
1280 add_generic_attrs (GtkTextLayout *layout,
1281 GtkTextAppearance *appearance,
1283 PangoAttrList *attrs,
1288 PangoAttribute *attr;
1290 if (appearance->underline != PANGO_UNDERLINE_NONE)
1292 attr = pango_attr_underline_new (appearance->underline);
1294 attr->start_index = start;
1295 attr->end_index = start + byte_count;
1297 pango_attr_list_insert (attrs, attr);
1300 if (appearance->rise != 0)
1302 attr = pango_attr_rise_new (appearance->rise);
1304 attr->start_index = start;
1305 attr->end_index = start + byte_count;
1307 pango_attr_list_insert (attrs, attr);
1312 attr = gtk_text_attr_appearance_new (appearance);
1314 attr->start_index = start;
1315 attr->end_index = start + byte_count;
1317 ((GtkTextAttrAppearance *)attr)->appearance.is_text = is_text;
1319 pango_attr_list_insert (attrs, attr);
1324 add_text_attrs (GtkTextLayout *layout,
1325 GtkTextAttributes *style,
1327 PangoAttrList *attrs,
1331 PangoAttribute *attr;
1333 attr = pango_attr_font_desc_new (style->font);
1334 attr->start_index = start;
1335 attr->end_index = start + byte_count;
1337 pango_attr_list_insert (attrs, attr);
1339 if (style->font_scale != 1.0)
1341 attr = pango_attr_scale_new (style->font_scale);
1343 attr->start_index = start;
1344 attr->end_index = start + byte_count;
1346 pango_attr_list_insert (attrs, attr);
1351 add_pixbuf_attrs (GtkTextLayout *layout,
1352 GtkTextLineDisplay *display,
1353 GtkTextAttributes *style,
1354 GtkTextLineSegment *seg,
1355 PangoAttrList *attrs,
1358 PangoAttribute *attr;
1359 PangoRectangle logical_rect;
1360 GtkTextPixbuf *pixbuf = &seg->body.pixbuf;
1363 width = gdk_pixbuf_get_width (pixbuf->pixbuf);
1364 height = gdk_pixbuf_get_height (pixbuf->pixbuf);
1367 logical_rect.y = -height * PANGO_SCALE;
1368 logical_rect.width = width * PANGO_SCALE;
1369 logical_rect.height = height * PANGO_SCALE;
1371 attr = pango_attr_shape_new (&logical_rect, &logical_rect);
1372 attr->start_index = start;
1373 attr->end_index = start + seg->byte_count;
1374 pango_attr_list_insert (attrs, attr);
1376 display->shaped_objects =
1377 g_slist_append (display->shaped_objects, pixbuf->pixbuf);
1381 add_child_attrs (GtkTextLayout *layout,
1382 GtkTextLineDisplay *display,
1383 GtkTextAttributes *style,
1384 GtkTextLineSegment *seg,
1385 PangoAttrList *attrs,
1388 PangoAttribute *attr;
1389 PangoRectangle logical_rect;
1390 GtkTextChildAnchor *anchor;
1397 anchor = seg->body.child.obj;
1399 tmp_list = seg->body.child.widgets;
1400 while (tmp_list != NULL)
1402 GtkWidget *child = tmp_list->data;
1404 if (_gtk_anchored_child_get_layout (child) == layout)
1409 gtk_widget_get_child_requisition (child, &req);
1412 height = req.height;
1414 display->shaped_objects =
1415 g_slist_append (display->shaped_objects, child);
1420 tmp_list = g_slist_next (tmp_list);
1423 if (tmp_list == NULL)
1425 /* If tmp_list == NULL then there is no widget at this anchor in
1426 * this display; not an error. We make up an arbitrary size
1427 * to use, just so the programmer can see the blank spot.
1428 * We also put a NULL in the shaped objects list, to keep
1429 * the correspondence between the list and the shaped chars in
1430 * the layout. A bad hack, yes.
1436 display->shaped_objects =
1437 g_slist_append (display->shaped_objects, NULL);
1441 logical_rect.y = -height * PANGO_SCALE;
1442 logical_rect.width = width * PANGO_SCALE;
1443 logical_rect.height = height * PANGO_SCALE;
1445 attr = pango_attr_shape_new (&logical_rect, &logical_rect);
1446 attr->start_index = start;
1447 attr->end_index = start + seg->byte_count;
1448 pango_attr_list_insert (attrs, attr);
1452 add_cursor (GtkTextLayout *layout,
1453 GtkTextLineDisplay *display,
1454 GtkTextLineSegment *seg,
1457 PangoRectangle strong_pos, weak_pos;
1458 GtkTextCursorDisplay *cursor = NULL; /* Quiet GCC */
1459 gboolean add_weak = FALSE;
1460 gboolean add_strong = FALSE;
1462 /* Hide insertion cursor when we have a selection or the layout
1463 * user has hidden the cursor.
1465 if (_gtk_text_btree_mark_is_insert (_gtk_text_buffer_get_btree (layout->buffer),
1466 seg->body.mark.obj) &&
1467 (!layout->cursor_visible ||
1468 gtk_text_buffer_get_selection_bounds (layout->buffer, NULL, NULL)))
1471 pango_layout_get_cursor_pos (display->layout, start, &strong_pos, &weak_pos);
1473 if (layout->cursor_direction == GTK_TEXT_DIR_NONE)
1478 else if (display->direction == layout->cursor_direction)
1485 cursor = g_new (GtkTextCursorDisplay, 1);
1487 cursor->x = PANGO_PIXELS (strong_pos.x);
1488 cursor->y = PANGO_PIXELS (strong_pos.y);
1489 cursor->height = PANGO_PIXELS (strong_pos.height);
1490 cursor->is_strong = TRUE;
1491 cursor->is_weak = (layout->cursor_direction == GTK_TEXT_DIR_NONE) ? FALSE : TRUE;
1492 display->cursors = g_slist_prepend (display->cursors, cursor);
1497 if (weak_pos.x == strong_pos.x && add_strong)
1498 cursor->is_weak = TRUE;
1501 cursor = g_new (GtkTextCursorDisplay, 1);
1503 cursor->x = PANGO_PIXELS (weak_pos.x);
1504 cursor->y = PANGO_PIXELS (weak_pos.y);
1505 cursor->height = PANGO_PIXELS (weak_pos.height);
1506 cursor->is_strong = (layout->cursor_direction == GTK_TEXT_DIR_NONE) ? FALSE : TRUE;
1507 cursor->is_weak = TRUE;
1508 display->cursors = g_slist_prepend (display->cursors, cursor);
1514 is_shape (PangoLayoutRun *run)
1516 GSList *tmp_list = run->item->analysis.extra_attrs;
1520 PangoAttribute *attr = tmp_list->data;
1522 if (attr->klass->type == PANGO_ATTR_SHAPE)
1525 tmp_list = tmp_list->next;
1532 allocate_child_widgets (GtkTextLayout *text_layout,
1533 GtkTextLineDisplay *display)
1535 GSList *shaped = display->shaped_objects;
1536 PangoLayout *layout = display->layout;
1537 PangoLayoutIter *iter;
1539 iter = pango_layout_get_iter (layout);
1543 PangoLayoutRun *run = pango_layout_iter_get_run (iter);
1545 if (run && is_shape (run))
1547 GObject *shaped_object = shaped->data;
1548 shaped = shaped->next;
1550 /* shaped_object is NULL for child anchors with no
1551 * widgets stored at them
1553 if (shaped_object && GTK_IS_WIDGET (shaped_object))
1555 PangoRectangle extents;
1557 /* We emit "allocate_child" with the x,y of
1558 * the widget with respect to the top of the line
1559 * and the left side of the buffer
1562 pango_layout_iter_get_run_extents (iter,
1566 g_signal_emit (text_layout,
1567 signals[ALLOCATE_CHILD],
1570 PANGO_PIXELS (extents.x) + display->x_offset,
1571 PANGO_PIXELS (extents.y) + display->top_margin);
1575 while (pango_layout_iter_next_run (iter));
1577 pango_layout_iter_free (iter);
1581 convert_color (GdkColor *result,
1582 PangoAttrColor *attr)
1584 result->red = attr->color.red;
1585 result->blue = attr->color.blue;
1586 result->green = attr->color.green;
1589 /* This function is used to convert the preedit string attributes, which are
1590 * standard PangoAttributes, into the custom attributes used by the text
1591 * widget and insert them into a attr list with a given offset.
1594 add_preedit_attrs (GtkTextLayout *layout,
1595 GtkTextAttributes *style,
1596 PangoAttrList *attrs,
1600 PangoAttrIterator *iter = pango_attr_list_get_iterator (layout->preedit_attrs);
1604 GtkTextAppearance appearance = style->appearance;
1605 PangoFontDescription *font_desc = pango_font_description_copy_static (style->font);
1606 PangoAttribute *insert_attr;
1607 GSList *extra_attrs = NULL;
1609 PangoLanguage *language;
1612 pango_attr_iterator_range (iter, &start, &end);
1614 if (end == G_MAXINT)
1615 end = layout->preedit_len;
1620 pango_attr_iterator_get_font (iter, font_desc, &language, &extra_attrs);
1622 tmp_list = extra_attrs;
1625 PangoAttribute *attr = tmp_list->data;
1627 switch (attr->klass->type)
1629 case PANGO_ATTR_FOREGROUND:
1630 convert_color (&appearance.fg_color, (PangoAttrColor *)attr);
1632 case PANGO_ATTR_BACKGROUND:
1633 convert_color (&appearance.bg_color, (PangoAttrColor *)attr);
1634 appearance.draw_bg = TRUE;
1636 case PANGO_ATTR_UNDERLINE:
1637 appearance.underline = ((PangoAttrInt *)attr)->value;
1639 case PANGO_ATTR_STRIKETHROUGH:
1640 appearance.strikethrough = ((PangoAttrInt *)attr)->value;
1642 case PANGO_ATTR_RISE:
1643 appearance.rise = ((PangoAttrInt *)attr)->value;
1649 pango_attribute_destroy (attr);
1650 tmp_list = tmp_list->next;
1653 g_slist_free (extra_attrs);
1655 insert_attr = pango_attr_font_desc_new (font_desc);
1656 insert_attr->start_index = start + offset;
1657 insert_attr->end_index = end + offset;
1659 pango_attr_list_insert (attrs, insert_attr);
1663 insert_attr = pango_attr_language_new (language);
1664 insert_attr->start_index = start + offset;
1665 insert_attr->end_index = end + offset;
1667 pango_attr_list_insert (attrs, insert_attr);
1670 add_generic_attrs (layout, &appearance, end - start,
1671 attrs, start + offset,
1674 pango_font_description_free (font_desc);
1676 while (pango_attr_iterator_next (iter));
1678 pango_attr_iterator_destroy (iter);
1681 GtkTextLineDisplay *
1682 gtk_text_layout_get_line_display (GtkTextLayout *layout,
1686 GtkTextLineDisplay *display;
1687 GtkTextLineSegment *seg;
1689 GtkTextAttributes *style;
1691 PangoAttrList *attrs;
1692 gint text_allocated, layout_byte_offset, buffer_byte_offset;
1693 PangoRectangle extents;
1694 gboolean para_values_set = FALSE;
1695 GSList *cursor_byte_offsets = NULL;
1696 GSList *cursor_segs = NULL;
1697 GSList *tmp_list1, *tmp_list2;
1698 gboolean saw_widget = FALSE;
1700 g_return_val_if_fail (line != NULL, NULL);
1702 if (layout->one_display_cache)
1704 if (line == layout->one_display_cache->line &&
1705 (size_only || !layout->one_display_cache->size_only))
1706 return layout->one_display_cache;
1709 GtkTextLineDisplay *tmp_display = layout->one_display_cache;
1710 layout->one_display_cache = NULL;
1711 gtk_text_layout_free_line_display (layout, tmp_display);
1715 display = g_new0 (GtkTextLineDisplay, 1);
1717 display->size_only = size_only;
1718 display->line = line;
1719 display->insert_index = -1;
1721 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
1724 /* Special-case optimization for completely
1725 * invisible lines; makes it faster to deal
1726 * with sequences of invisible lines.
1728 if (totally_invisible_line (layout, line, &iter))
1731 /* Allocate space for flat text for buffer
1733 text_allocated = _gtk_text_line_byte_count (line);
1734 text = g_malloc (text_allocated);
1736 attrs = pango_attr_list_new ();
1738 /* Iterate over segments, creating display chunks for them. */
1739 layout_byte_offset = 0; /* current length of layout text (includes preedit, does not include invisible text) */
1740 buffer_byte_offset = 0; /* position in the buffer line */
1741 seg = _gtk_text_iter_get_any_segment (&iter);
1744 /* Displayable segments */
1745 if (seg->type == >k_text_char_type ||
1746 seg->type == >k_text_pixbuf_type ||
1747 seg->type == >k_text_child_type)
1749 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
1751 buffer_byte_offset);
1752 style = get_style (layout, &iter);
1754 /* We have to delay setting the paragraph values until we
1755 * hit the first pixbuf or text segment because toggles at
1756 * the beginning of the paragraph should affect the
1757 * paragraph-global values
1759 if (!para_values_set)
1761 set_para_values (layout, style, display);
1762 para_values_set = TRUE;
1765 /* First see if the chunk is invisible, and ignore it if so. Tk
1766 * looked at tabs, wrap mode, etc. before doing this, but
1767 * that made no sense to me, so I am just skipping the
1770 if (!style->invisible)
1772 if (seg->type == >k_text_char_type)
1774 /* We don't want to split segments because of marks,
1775 * so we scan forward for more segments only
1776 * separated from us by marks. In theory, we should
1777 * also merge segments with identical styles, even
1778 * if there are toggles in-between
1782 GtkTextLineSegment *prev_seg = NULL;
1786 if (seg->type == >k_text_char_type)
1788 memcpy (text + layout_byte_offset, seg->body.chars, seg->byte_count);
1789 layout_byte_offset += seg->byte_count;
1790 buffer_byte_offset += seg->byte_count;
1791 bytes += seg->byte_count;
1793 else if (seg->type == >k_text_right_mark_type ||
1794 seg->type == >k_text_left_mark_type)
1796 /* If we have preedit string, break out of this loop - we'll almost
1797 * certainly have different attributes on the preedit string
1800 if (layout->preedit_len > 0 &&
1801 _gtk_text_btree_mark_is_insert (_gtk_text_buffer_get_btree (layout->buffer),
1802 seg->body.mark.obj))
1805 if (seg->body.mark.visible)
1807 cursor_byte_offsets = g_slist_prepend (cursor_byte_offsets, GINT_TO_POINTER (layout_byte_offset));
1808 cursor_segs = g_slist_prepend (cursor_segs, seg);
1818 seg = prev_seg; /* Back up one */
1819 add_generic_attrs (layout, &style->appearance,
1821 attrs, layout_byte_offset - bytes,
1823 add_text_attrs (layout, style, bytes, attrs,
1824 layout_byte_offset - bytes, size_only);
1826 else if (seg->type == >k_text_pixbuf_type)
1828 add_generic_attrs (layout,
1831 attrs, layout_byte_offset,
1833 add_pixbuf_attrs (layout, display, style,
1834 seg, attrs, layout_byte_offset);
1835 memcpy (text + layout_byte_offset, gtk_text_unknown_char_utf8,
1837 layout_byte_offset += seg->byte_count;
1838 buffer_byte_offset += seg->byte_count;
1840 else if (seg->type == >k_text_child_type)
1844 add_generic_attrs (layout, &style->appearance,
1846 attrs, layout_byte_offset,
1848 add_child_attrs (layout, display, style,
1849 seg, attrs, layout_byte_offset);
1850 memcpy (text + layout_byte_offset, gtk_text_unknown_char_utf8,
1852 layout_byte_offset += seg->byte_count;
1853 buffer_byte_offset += seg->byte_count;
1857 /* We don't know this segment type */
1858 g_assert_not_reached ();
1861 } /* if (segment was visible) */
1864 /* Invisible segment */
1865 buffer_byte_offset += seg->byte_count;
1868 release_style (layout, style);
1872 else if (seg->type == >k_text_toggle_on_type ||
1873 seg->type == >k_text_toggle_off_type)
1875 /* Style may have changed, drop our
1876 current cached style */
1877 invalidate_cached_style (layout);
1881 else if (seg->type == >k_text_right_mark_type ||
1882 seg->type == >k_text_left_mark_type)
1884 gint cursor_offset = 0;
1886 /* At the insertion point, add the preedit string, if any */
1888 if (_gtk_text_btree_mark_is_insert (_gtk_text_buffer_get_btree (layout->buffer),
1889 seg->body.mark.obj))
1891 display->insert_index = layout_byte_offset;
1893 if (layout->preedit_len > 0)
1895 text_allocated += layout->preedit_len;
1896 text = g_realloc (text, text_allocated);
1898 style = get_style (layout, &iter);
1899 add_preedit_attrs (layout, style, attrs, layout_byte_offset, size_only);
1900 release_style (layout, style);
1902 memcpy (text + layout_byte_offset, layout->preedit_string, layout->preedit_len);
1903 layout_byte_offset += layout->preedit_len;
1904 /* DO NOT increment the buffer byte offset for preedit */
1906 cursor_offset = layout->preedit_cursor - layout->preedit_len;
1911 /* Display visible marks */
1913 if (seg->body.mark.visible)
1915 cursor_byte_offsets = g_slist_prepend (cursor_byte_offsets,
1916 GINT_TO_POINTER (layout_byte_offset + cursor_offset));
1917 cursor_segs = g_slist_prepend (cursor_segs, seg);
1922 g_error ("Unknown segment type: %s", seg->type->name);
1927 if (!para_values_set)
1929 style = get_style (layout, &iter);
1930 set_para_values (layout, style, display);
1931 release_style (layout, style);
1934 /* Pango doesn't want the trailing paragraph delimiters */
1937 /* Only one character has type G_UNICODE_PARAGRAPH_SEPARATOR in
1938 * Unicode 3.0; update this if that changes.
1940 #define PARAGRAPH_SEPARATOR 0x2029
1943 if (layout_byte_offset > 0)
1945 const char *prev = g_utf8_prev_char (text + layout_byte_offset);
1946 ch = g_utf8_get_char (prev);
1947 if (ch == PARAGRAPH_SEPARATOR || ch == '\r' || ch == '\n')
1948 layout_byte_offset = prev - text; /* chop off */
1950 if (ch == '\n' && layout_byte_offset > 0)
1952 /* Possibly chop a CR as well */
1953 prev = g_utf8_prev_char (text + layout_byte_offset);
1955 --layout_byte_offset;
1960 pango_layout_set_text (display->layout, text, layout_byte_offset);
1961 pango_layout_set_attributes (display->layout, attrs);
1963 tmp_list1 = cursor_byte_offsets;
1964 tmp_list2 = cursor_segs;
1967 add_cursor (layout, display, tmp_list2->data,
1968 GPOINTER_TO_INT (tmp_list1->data));
1969 tmp_list1 = tmp_list1->next;
1970 tmp_list2 = tmp_list2->next;
1972 g_slist_free (cursor_byte_offsets);
1973 g_slist_free (cursor_segs);
1975 pango_layout_get_extents (display->layout, NULL, &extents);
1977 display->width = PIXEL_BOUND (extents.width) + display->left_margin + display->right_margin;
1978 display->height += PANGO_PIXELS (extents.height);
1980 /* Free this if we aren't in a loop */
1981 if (layout->wrap_loop_count == 0)
1982 invalidate_cached_style (layout);
1985 pango_attr_list_unref (attrs);
1987 layout->one_display_cache = display;
1990 allocate_child_widgets (layout, display);
1996 gtk_text_layout_free_line_display (GtkTextLayout *layout,
1997 GtkTextLineDisplay *display)
1999 if (display != layout->one_display_cache)
2001 if (display->layout)
2002 g_object_unref (display->layout);
2004 if (display->cursors)
2006 g_slist_foreach (display->cursors, (GFunc)g_free, NULL);
2007 g_slist_free (display->cursors);
2009 g_slist_free (display->shaped_objects);
2015 /* Functions to convert iter <=> index for the line of a GtkTextLineDisplay
2016 * taking into account the preedit string and invisible text if necessary.
2019 line_display_iter_to_index (GtkTextLayout *layout,
2020 GtkTextLineDisplay *display,
2021 const GtkTextIter *iter)
2025 g_return_val_if_fail (_gtk_text_iter_get_text_line (iter) == display->line, 0);
2027 index = gtk_text_iter_get_visible_line_index (iter);
2029 if (index >= display->insert_index)
2030 index += layout->preedit_len;
2036 line_display_index_to_iter (GtkTextLayout *layout,
2037 GtkTextLineDisplay *display,
2042 g_return_if_fail (!_gtk_text_line_is_last (display->line,
2043 _gtk_text_buffer_get_btree (layout->buffer)));
2045 if (index >= display->insert_index + layout->preedit_len)
2046 index -= layout->preedit_len;
2047 else if (index > display->insert_index)
2049 index = display->insert_index;
2053 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
2054 iter, display->line, 0);
2056 gtk_text_iter_set_visible_line_index (iter, index);
2058 if (_gtk_text_iter_get_text_line (iter) != display->line)
2060 /* Clamp to end of line - really this clamping should have been done
2061 * before here, maybe in Pango, this is a broken band-aid I think
2063 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
2064 iter, display->line, 0);
2066 if (!gtk_text_iter_ends_line (iter))
2067 gtk_text_iter_forward_to_line_end (iter);
2070 gtk_text_iter_forward_chars (iter, trailing);
2074 get_line_at_y (GtkTextLayout *layout,
2081 if (y > layout->height)
2084 *line = _gtk_text_btree_find_line_by_y (_gtk_text_buffer_get_btree (layout->buffer),
2085 layout, y, line_top);
2088 *line = _gtk_text_btree_get_end_iter_line (_gtk_text_buffer_get_btree (layout->buffer));
2092 _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
2098 * gtk_text_layout_get_line_at_y:
2099 * @layout: a #GtkLayout
2100 * @target_iter: the iterator in which the result is stored
2101 * @y: the y positition
2102 * @line_top: location to store the y coordinate of the
2103 * top of the line. (Can by %NULL.)
2105 * Get the iter at the beginning of the line which is displayed
2109 gtk_text_layout_get_line_at_y (GtkTextLayout *layout,
2110 GtkTextIter *target_iter,
2116 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
2117 g_return_if_fail (target_iter != NULL);
2119 get_line_at_y (layout, y, &line, line_top);
2120 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
2121 target_iter, line, 0);
2125 gtk_text_layout_get_iter_at_pixel (GtkTextLayout *layout,
2126 GtkTextIter *target_iter,
2130 gint byte_index, trailing;
2132 GtkTextLineDisplay *display;
2134 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
2135 g_return_if_fail (target_iter != NULL);
2137 get_line_at_y (layout, y, &line, &line_top);
2139 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2141 x -= display->x_offset;
2142 y -= line_top + display->top_margin;
2144 /* If we are below the layout, position the cursor at the last character
2147 if (y > display->height - display->top_margin - display->bottom_margin)
2149 byte_index = _gtk_text_line_byte_count (line);
2154 /* Ignore the "outside" return value from pango. Pango is doing
2155 * the right thing even if we are outside the layout in the
2158 pango_layout_xy_to_index (display->layout, x * PANGO_SCALE, y * PANGO_SCALE,
2159 &byte_index, &trailing);
2162 line_display_index_to_iter (layout, display, target_iter, byte_index, trailing);
2164 gtk_text_layout_free_line_display (layout, display);
2168 * gtk_text_layout_get_cursor_locations:
2169 * @layout: a #GtkTextLayout
2170 * @iter: a #GtkTextIter
2171 * @strong_pos: location to store the strong cursor position (may be %NULL)
2172 * @weak_pos: location to store the weak cursor position (may be %NULL)
2174 * Given an iterator within a text layout, determine the positions of the
2175 * strong and weak cursors if the insertion point is at that
2176 * iterator. The position of each cursor is stored as a zero-width
2177 * rectangle. The strong cursor location is the location where
2178 * characters of the directionality equal to the base direction of the
2179 * paragraph are inserted. The weak cursor location is the location
2180 * where characters of the directionality opposite to the base
2181 * direction of the paragraph are inserted.
2184 gtk_text_layout_get_cursor_locations (GtkTextLayout *layout,
2186 GdkRectangle *strong_pos,
2187 GdkRectangle *weak_pos)
2190 GtkTextLineDisplay *display;
2193 GtkTextIter insert_iter;
2195 PangoRectangle pango_strong_pos;
2196 PangoRectangle pango_weak_pos;
2198 g_return_if_fail (layout != NULL);
2199 g_return_if_fail (iter != NULL);
2201 line = _gtk_text_iter_get_text_line (iter);
2202 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2203 index = line_display_iter_to_index (layout, display, iter);
2205 line_top = _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
2208 gtk_text_buffer_get_iter_at_mark (layout->buffer, &insert_iter,
2209 gtk_text_buffer_get_mark (layout->buffer,
2212 if (gtk_text_iter_equal (iter, &insert_iter))
2213 index += layout->preedit_cursor - layout->preedit_len;
2215 pango_layout_get_cursor_pos (display->layout, index,
2216 strong_pos ? &pango_strong_pos : NULL,
2217 weak_pos ? &pango_weak_pos : NULL);
2221 strong_pos->x = display->x_offset + pango_strong_pos.x / PANGO_SCALE;
2222 strong_pos->y = line_top + display->top_margin + pango_strong_pos.y / PANGO_SCALE;
2223 strong_pos->width = 0;
2224 strong_pos->height = pango_strong_pos.height / PANGO_SCALE;
2229 weak_pos->x = display->x_offset + pango_weak_pos.x / PANGO_SCALE;
2230 weak_pos->y = line_top + display->top_margin + pango_weak_pos.y / PANGO_SCALE;
2231 weak_pos->width = 0;
2232 weak_pos->height = pango_weak_pos.height / PANGO_SCALE;
2235 gtk_text_layout_free_line_display (layout, display);
2239 * gtk_text_layout_get_line_yrange:
2240 * @layout: a #GtkTextLayout
2241 * @iter: a #GtkTextIter
2242 * @y: location to store the top of the paragraph in pixels,
2244 * @height location to store the height of the paragraph in pixels,
2247 * Find the range of y coordinates for the paragraph containing
2251 gtk_text_layout_get_line_yrange (GtkTextLayout *layout,
2252 const GtkTextIter *iter,
2258 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
2259 g_return_if_fail (_gtk_text_iter_get_btree (iter) == _gtk_text_buffer_get_btree (layout->buffer));
2261 line = _gtk_text_iter_get_text_line (iter);
2264 *y = _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
2268 GtkTextLineData *line_data = _gtk_text_line_get_data (line, layout);
2270 *height = line_data->height;
2277 * _gtk_text_layout_get_line_xrange:
2278 * @layout: a #GtkTextLayout
2279 * @iter: a #GtkTextIter
2280 * @x: location to store the top of the paragraph in pixels,
2282 * @width location to store the height of the paragraph in pixels,
2285 * Find the range of X coordinates for the paragraph containing
2286 * the given iter. Private for 2.0 due to API freeze, could
2287 * be made public for 2.2.
2290 _gtk_text_layout_get_line_xrange (GtkTextLayout *layout,
2291 const GtkTextIter *iter,
2297 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
2298 g_return_if_fail (_gtk_text_iter_get_btree (iter) == _gtk_text_buffer_get_btree (layout->buffer));
2300 line = _gtk_text_iter_get_text_line (iter);
2303 *x = 0; /* FIXME This is wrong; should represent the first available cursor position */
2307 GtkTextLineData *line_data = _gtk_text_line_get_data (line, layout);
2309 *width = line_data->width;
2316 gtk_text_layout_get_iter_location (GtkTextLayout *layout,
2317 const GtkTextIter *iter,
2320 PangoRectangle pango_rect;
2323 GtkTextLineDisplay *display;
2327 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
2328 g_return_if_fail (_gtk_text_iter_get_btree (iter) == _gtk_text_buffer_get_btree (layout->buffer));
2329 g_return_if_fail (rect != NULL);
2331 tree = _gtk_text_iter_get_btree (iter);
2332 line = _gtk_text_iter_get_text_line (iter);
2334 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2336 rect->y = _gtk_text_btree_find_line_top (tree, line, layout);
2338 x_offset = display->x_offset * PANGO_SCALE;
2340 byte_index = gtk_text_iter_get_line_index (iter);
2342 pango_layout_index_to_pos (display->layout, byte_index, &pango_rect);
2344 rect->x = PANGO_PIXELS (x_offset + pango_rect.x);
2345 rect->y += PANGO_PIXELS (pango_rect.y) + display->top_margin;
2346 rect->width = PANGO_PIXELS (pango_rect.width);
2347 rect->height = PANGO_PIXELS (pango_rect.height);
2349 gtk_text_layout_free_line_display (layout, display);
2354 /* Find the iter for the logical beginning of the first display line whose
2355 * top y is >= y. If none exists, move the iter to the logical beginning
2356 * of the last line in the buffer.
2359 find_display_line_below (GtkTextLayout *layout,
2363 GtkTextLine *line, *next;
2364 GtkTextLine *found_line = NULL;
2366 gint found_byte = 0;
2368 line = _gtk_text_btree_find_line_by_y (_gtk_text_buffer_get_btree (layout->buffer),
2369 layout, y, &line_top);
2373 _gtk_text_btree_get_end_iter_line (_gtk_text_buffer_get_btree (layout->buffer));
2376 _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
2380 while (line && !found_line)
2382 GtkTextLineDisplay *display = gtk_text_layout_get_line_display (layout, line, FALSE);
2383 PangoLayoutIter *layout_iter;
2385 layout_iter = pango_layout_get_iter (display->layout);
2387 line_top += display->top_margin;
2391 gint first_y, last_y;
2392 PangoLayoutLine *layout_line = pango_layout_iter_get_line (layout_iter);
2394 found_byte = layout_line->start_index;
2402 pango_layout_iter_get_line_yrange (layout_iter, &first_y, &last_y);
2403 line_top += (last_y - first_y) / PANGO_SCALE;
2405 while (pango_layout_iter_next_line (layout_iter));
2407 pango_layout_iter_free (layout_iter);
2409 line_top += display->bottom_margin;
2410 gtk_text_layout_free_line_display (layout, display);
2412 next = _gtk_text_line_next_excluding_last (line);
2419 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
2420 iter, found_line, found_byte);
2423 /* Find the iter for the logical beginning of the last display line whose
2424 * top y is >= y. If none exists, move the iter to the logical beginning
2425 * of the first line in the buffer.
2428 find_display_line_above (GtkTextLayout *layout,
2433 GtkTextLine *found_line = NULL;
2435 gint found_byte = 0;
2437 line = _gtk_text_btree_find_line_by_y (_gtk_text_buffer_get_btree (layout->buffer), layout, y, &line_top);
2440 line = _gtk_text_btree_get_end_iter_line (_gtk_text_buffer_get_btree (layout->buffer));
2442 line_top = _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer), line, layout);
2445 while (line && !found_line)
2447 GtkTextLineDisplay *display = gtk_text_layout_get_line_display (layout, line, FALSE);
2448 PangoRectangle logical_rect;
2449 PangoLayoutIter *layout_iter;
2452 layout_iter = pango_layout_get_iter (display->layout);
2454 line_top -= display->top_margin + display->bottom_margin;
2455 pango_layout_iter_get_layout_extents (layout_iter, NULL, &logical_rect);
2456 line_top -= logical_rect.height / PANGO_SCALE;
2458 tmp_top = line_top + display->top_margin;
2462 gint first_y, last_y;
2463 PangoLayoutLine *layout_line = pango_layout_iter_get_line (layout_iter);
2465 found_byte = layout_line->start_index;
2467 pango_layout_iter_get_line_yrange (layout_iter, &first_y, &last_y);
2469 tmp_top -= (last_y - first_y) / PANGO_SCALE;
2474 pango_layout_iter_free (layout_iter);
2478 while (pango_layout_iter_next_line (layout_iter));
2480 pango_layout_iter_free (layout_iter);
2482 gtk_text_layout_free_line_display (layout, display);
2484 line = _gtk_text_line_previous (line);
2490 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
2491 iter, found_line, found_byte);
2493 gtk_text_buffer_get_iter_at_offset (layout->buffer, iter, 0);
2497 * gtk_text_layout_clamp_iter_to_vrange:
2498 * @layout: a #GtkTextLayout
2499 * @iter: a #GtkTextIter
2500 * @top: the top of the range
2501 * @bottom: the bottom the range
2503 * If the iterator is not fully in the range @top <= y < @bottom,
2504 * then, if possible, move it the minimum distance so that the
2505 * iterator in this range.
2507 * Returns: %TRUE if the iterator was moved, otherwise %FALSE.
2510 gtk_text_layout_clamp_iter_to_vrange (GtkTextLayout *layout,
2515 GdkRectangle iter_rect;
2517 gtk_text_layout_get_iter_location (layout, iter, &iter_rect);
2519 /* If the iter is at least partially above the range, put the iter
2520 * at the first fully visible line after the range.
2522 if (iter_rect.y < top)
2524 find_display_line_below (layout, iter, top);
2528 /* Otherwise, if the iter is at least partially below the screen, put the
2529 * iter on the last logical position of the last completely visible
2532 else if (iter_rect.y + iter_rect.height > bottom)
2534 find_display_line_above (layout, iter, bottom);
2543 * gtk_text_layout_move_iter_to_previous_line:
2544 * @layout: a #GtkLayout
2545 * @iter: a #GtkTextIter
2547 * Move the iterator to the beginning of the previous line. The lines
2548 * of a wrapped paragraph are treated as distinct for this operation.
2551 gtk_text_layout_move_iter_to_previous_line (GtkTextLayout *layout,
2555 GtkTextLineDisplay *display;
2558 PangoLayoutLine *layout_line;
2560 gboolean update_byte = FALSE;
2562 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), FALSE);
2563 g_return_val_if_fail (iter != NULL, FALSE);
2568 line = _gtk_text_iter_get_text_line (iter);
2569 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2570 line_byte = line_display_iter_to_index (layout, display, iter);
2572 /* If display->height == 0 then the line is invisible, so don't
2575 while (display->height == 0)
2577 GtkTextLine *prev_line;
2579 prev_line = _gtk_text_line_previous (line);
2581 if (prev_line == NULL)
2583 line_display_index_to_iter (layout, display, iter, 0, 0);
2587 gtk_text_layout_free_line_display (layout, display);
2590 display = gtk_text_layout_get_line_display (layout, prev_line, FALSE);
2594 tmp_list = pango_layout_get_lines (display->layout);
2595 layout_line = tmp_list->data;
2599 line_byte = layout_line->start_index + layout_line->length;
2602 if (line_byte < layout_line->length || !tmp_list->next) /* first line of paragraph */
2604 GtkTextLine *prev_line;
2606 prev_line = _gtk_text_line_previous (line);
2609 gtk_text_layout_free_line_display (layout, display);
2611 display = gtk_text_layout_get_line_display (layout, prev_line, FALSE);
2613 if (display->height > 0)
2615 tmp_list = g_slist_last (pango_layout_get_lines (display->layout));
2616 layout_line = tmp_list->data;
2618 line_display_index_to_iter (layout, display, iter,
2619 layout_line->start_index + layout_line->length, 0);
2623 prev_line = _gtk_text_line_previous (prev_line);
2626 if (prev_line == NULL)
2627 line_display_index_to_iter (layout, display, iter, 0, 0);
2631 gint prev_offset = layout_line->start_index;
2633 tmp_list = tmp_list->next;
2636 layout_line = tmp_list->data;
2638 if (line_byte < layout_line->start_index + layout_line->length ||
2641 line_display_index_to_iter (layout, display, iter, prev_offset, 0);
2645 prev_offset = layout_line->start_index;
2646 tmp_list = tmp_list->next;
2652 gtk_text_layout_free_line_display (layout, display);
2655 !gtk_text_iter_equal (iter, &orig) &&
2656 !gtk_text_iter_is_end (iter);
2660 * gtk_text_layout_move_iter_to_next_line:
2661 * @layout: a #GtkLayout
2662 * @iter: a #GtkTextIter
2664 * Move the iterator to the beginning of the next line. The
2665 * lines of a wrapped paragraph are treated as distinct for
2669 gtk_text_layout_move_iter_to_next_line (GtkTextLayout *layout,
2673 GtkTextLineDisplay *display;
2676 gboolean found = FALSE;
2677 gboolean found_after = FALSE;
2678 gboolean first = TRUE;
2680 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), FALSE);
2681 g_return_val_if_fail (iter != NULL, FALSE);
2685 line = _gtk_text_iter_get_text_line (iter);
2687 while (line && !found_after)
2691 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2693 if (display->height == 0)
2698 line_byte = line_display_iter_to_index (layout, display, iter);
2704 tmp_list = pango_layout_get_lines (display->layout);
2705 while (tmp_list && !found_after)
2707 PangoLayoutLine *layout_line = tmp_list->data;
2711 line_display_index_to_iter (layout, display, iter,
2712 layout_line->start_index, 0);
2715 else if (line_byte < layout_line->start_index + layout_line->length || !tmp_list->next)
2718 tmp_list = tmp_list->next;
2723 gtk_text_layout_free_line_display (layout, display);
2725 line = _gtk_text_line_next_excluding_last (line);
2729 gtk_text_buffer_get_end_iter (layout->buffer, iter);
2732 !gtk_text_iter_equal (iter, &orig) &&
2733 !gtk_text_iter_is_end (iter);
2737 * gtk_text_layout_move_iter_to_line_end:
2738 * @layout: a #GtkTextLayout
2739 * @direction: if negative, move to beginning of line, otherwise
2740 move to end of line.
2742 * Move to the beginning or end of a display line.
2745 gtk_text_layout_move_iter_to_line_end (GtkTextLayout *layout,
2750 GtkTextLineDisplay *display;
2755 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), FALSE);
2756 g_return_val_if_fail (iter != NULL, FALSE);
2760 line = _gtk_text_iter_get_text_line (iter);
2761 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2762 line_byte = line_display_iter_to_index (layout, display, iter);
2764 tmp_list = pango_layout_get_lines (display->layout);
2767 PangoLayoutLine *layout_line = tmp_list->data;
2769 if (line_byte < layout_line->start_index + layout_line->length || !tmp_list->next)
2771 line_display_index_to_iter (layout, display, iter,
2772 direction < 0 ? layout_line->start_index : layout_line->start_index + layout_line->length,
2775 /* FIXME: As a bad hack, we move back one position when we
2776 * are inside a paragraph to avoid going to next line on a
2777 * forced break not at whitespace. Real fix is to keep track
2778 * of whether marks are at leading or trailing edge? */
2779 if (direction > 0 && layout_line->length > 0 && !gtk_text_iter_ends_line (iter))
2780 gtk_text_iter_backward_char (iter);
2785 tmp_list = tmp_list->next;
2788 gtk_text_layout_free_line_display (layout, display);
2791 !gtk_text_iter_equal (iter, &orig) &&
2792 !gtk_text_iter_is_end (iter);
2797 * gtk_text_layout_iter_starts_line:
2798 * @layout: a #GtkTextLayout
2799 * @iter: iterator to test
2801 * Tests whether an iterator is at the start of a display line.
2804 gtk_text_layout_iter_starts_line (GtkTextLayout *layout,
2805 const GtkTextIter *iter)
2808 GtkTextLineDisplay *display;
2812 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), FALSE);
2813 g_return_val_if_fail (iter != NULL, FALSE);
2815 line = _gtk_text_iter_get_text_line (iter);
2816 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2817 line_byte = line_display_iter_to_index (layout, display, iter);
2819 tmp_list = pango_layout_get_lines (display->layout);
2822 PangoLayoutLine *layout_line = tmp_list->data;
2824 if (line_byte < layout_line->start_index + layout_line->length ||
2827 /* We're located on this line or the para delimiters before
2830 gtk_text_layout_free_line_display (layout, display);
2832 if (line_byte == layout_line->start_index)
2838 tmp_list = tmp_list->next;
2841 g_assert_not_reached ();
2846 gtk_text_layout_get_iter_at_line (GtkTextLayout *layout,
2851 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
2852 iter, line, byte_offset);
2857 * gtk_text_layout_move_iter_to_x:
2858 * @layout: a #GtkTextLayout
2859 * @iter: a #GtkTextIter
2862 * Keeping the iterator on the same line of the layout, move it to the
2863 * specified X coordinate. The lines of a wrapped paragraph are
2864 * treated as distinct for this operation.
2867 gtk_text_layout_move_iter_to_x (GtkTextLayout *layout,
2872 GtkTextLineDisplay *display;
2874 PangoLayoutIter *layout_iter;
2876 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
2877 g_return_if_fail (iter != NULL);
2879 line = _gtk_text_iter_get_text_line (iter);
2881 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2882 line_byte = line_display_iter_to_index (layout, display, iter);
2884 layout_iter = pango_layout_get_iter (display->layout);
2888 PangoLayoutLine *layout_line = pango_layout_iter_get_line (layout_iter);
2890 if (line_byte < layout_line->start_index + layout_line->length ||
2891 pango_layout_iter_at_last_line (layout_iter))
2893 PangoRectangle logical_rect;
2894 gint byte_index, trailing;
2895 gint x_offset = display->x_offset * PANGO_SCALE;
2897 pango_layout_iter_get_line_extents (layout_iter, NULL, &logical_rect);
2899 pango_layout_line_x_to_index (layout_line,
2900 x * PANGO_SCALE - x_offset - logical_rect.x,
2901 &byte_index, &trailing);
2903 line_display_index_to_iter (layout, display, iter, byte_index, trailing);
2908 while (pango_layout_iter_next_line (layout_iter));
2910 pango_layout_iter_free (layout_iter);
2912 gtk_text_layout_free_line_display (layout, display);
2916 * gtk_text_layout_move_iter_visually:
2917 * @layout: a #GtkTextLayout
2918 * @iter: a #GtkTextIter
2919 * @count: number of characters to move (negative moves left, positive moves right)
2921 * Move the iterator a given number of characters visually, treating
2922 * it as the strong cursor position. If @count is positive, then the
2923 * new strong cursor position will be @count positions to the right of
2924 * the old cursor position. If @count is negative then the new strong
2925 * cursor position will be @count positions to the left of the old
2928 * In the presence of bidirection text, the correspondence
2929 * between logical and visual order will depend on the direction
2930 * of the current run, and there may be jumps when the cursor
2931 * is moved off of the end of a run.
2935 gtk_text_layout_move_iter_visually (GtkTextLayout *layout,
2939 GtkTextLineDisplay *display = NULL;
2942 g_return_val_if_fail (layout != NULL, FALSE);
2943 g_return_val_if_fail (iter != NULL, FALSE);
2949 GtkTextLine *line = _gtk_text_iter_get_text_line (iter);
2951 gint extra_back = 0;
2954 int byte_count = _gtk_text_line_byte_count (line);
2960 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2962 if (layout->cursor_direction == GTK_TEXT_DIR_NONE)
2965 strong = display->direction == layout->cursor_direction;
2967 line_byte = line_display_iter_to_index (layout, display, iter);
2971 pango_layout_move_cursor_visually (display->layout, strong, line_byte, 0, 1, &new_index, &new_trailing);
2976 pango_layout_move_cursor_visually (display->layout, strong, line_byte, 0, -1, &new_index, &new_trailing);
2980 /* We need to handle the preedit string specially. Well, we don't really need to
2981 * handle it specially, since hopefully calling gtk_im_context_reset() will
2982 * remove the preedit string; but if we start off in front of the preedit
2983 * string (logically) and end up in or on the back edge of the preedit string,
2984 * we should move the iter one place farther.
2986 if (layout->preedit_len > 0 && display->insert_index >= 0)
2988 if (line_byte == display->insert_index + layout->preedit_len &&
2989 new_index < display->insert_index + layout->preedit_len)
2991 line_byte = display->insert_index;
2996 if (new_index < 0 || (new_index == 0 && extra_back))
2998 line = _gtk_text_line_previous (line);
3003 gtk_text_layout_free_line_display (layout, display);
3004 display = gtk_text_layout_get_line_display (layout, line, FALSE);
3005 new_index = _gtk_text_line_byte_count (line);
3007 else if (new_index > byte_count)
3009 line = _gtk_text_line_next_excluding_last (line);
3013 gtk_text_layout_free_line_display (layout, display);
3014 display = gtk_text_layout_get_line_display (layout, line, FALSE);
3018 line_display_index_to_iter (layout, display, iter, new_index, new_trailing);
3020 gtk_text_iter_backward_char (iter);
3023 gtk_text_layout_free_line_display (layout, display);
3028 !gtk_text_iter_equal (iter, &orig) &&
3029 !gtk_text_iter_is_end (iter);
3033 gtk_text_layout_spew (GtkTextLayout *layout)
3036 GtkTextDisplayLine *iter;
3038 guint paragraphs = 0;
3039 GtkTextLine *last_line = NULL;
3041 iter = layout->line_list;
3042 while (iter != NULL)
3044 if (iter->line != last_line)
3046 printf ("%5u paragraph (%p)\n", paragraphs, iter->line);
3048 last_line = iter->line;
3051 printf (" %5u y: %d len: %d start: %d bytes: %d\n",
3052 wrapped, iter->y, iter->length, iter->byte_offset,
3059 printf ("Layout %s recompute\n",
3060 layout->need_recompute ? "needs" : "doesn't need");
3062 printf ("Layout pars: %u lines: %u size: %d x %d Screen width: %d\n",
3063 paragraphs, wrapped, layout->width,
3064 layout->height, layout->screen_width);