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 "gtksignal.h"
81 #include "gtkmarshalers.h"
82 #include "gtktextlayout.h"
83 #include "gtktextbtree.h"
84 #include "gtktextiterprivate.h"
89 static GtkTextLineData *gtk_text_layout_real_wrap (GtkTextLayout *layout,
92 GtkTextLineData *line_data);
94 static void gtk_text_layout_invalidated (GtkTextLayout *layout);
96 static void gtk_text_layout_real_invalidate (GtkTextLayout *layout,
97 const GtkTextIter *start,
98 const GtkTextIter *end);
99 static void gtk_text_layout_invalidate_cache (GtkTextLayout *layout,
101 static void gtk_text_layout_invalidate_cursor_line (GtkTextLayout *layout);
102 static void gtk_text_layout_real_free_line_data (GtkTextLayout *layout,
104 GtkTextLineData *line_data);
106 static void gtk_text_layout_invalidate_all (GtkTextLayout *layout);
108 static PangoAttribute *gtk_text_attr_appearance_new (const GtkTextAppearance *appearance);
122 static void gtk_text_layout_init (GtkTextLayout *text_layout);
123 static void gtk_text_layout_class_init (GtkTextLayoutClass *klass);
124 static void gtk_text_layout_finalize (GObject *object);
127 static GtkObjectClass *parent_class = NULL;
128 static guint signals[LAST_SIGNAL] = { 0 };
130 PangoAttrType gtk_text_attr_appearance_type = 0;
133 gtk_text_layout_get_type (void)
135 static GType our_type = 0;
139 static const GTypeInfo our_info =
141 sizeof (GtkTextLayoutClass),
142 (GBaseInitFunc) NULL,
143 (GBaseFinalizeFunc) NULL,
144 (GClassInitFunc) gtk_text_layout_class_init,
145 NULL, /* class_finalize */
146 NULL, /* class_data */
147 sizeof (GtkTextLayout),
149 (GInstanceInitFunc) gtk_text_layout_init
152 our_type = g_type_register_static (G_TYPE_OBJECT,
162 gtk_text_layout_class_init (GtkTextLayoutClass *klass)
164 GObjectClass *object_class = G_OBJECT_CLASS (klass);
166 parent_class = g_type_class_peek_parent (klass);
168 object_class->finalize = gtk_text_layout_finalize;
170 klass->wrap = gtk_text_layout_real_wrap;
171 klass->invalidate = gtk_text_layout_real_invalidate;
172 klass->free_line_data = gtk_text_layout_real_free_line_data;
174 signals[INVALIDATED] =
175 g_signal_new ("invalidated",
176 G_OBJECT_CLASS_TYPE (object_class),
178 G_STRUCT_OFFSET (GtkTextLayoutClass, invalidated),
180 _gtk_marshal_VOID__VOID,
185 g_signal_new ("changed",
186 G_OBJECT_CLASS_TYPE (object_class),
188 G_STRUCT_OFFSET (GtkTextLayoutClass, changed),
190 _gtk_marshal_VOID__INT_INT_INT,
197 signals[ALLOCATE_CHILD] =
198 g_signal_new ("allocate_child",
199 G_OBJECT_CLASS_TYPE (object_class),
201 G_STRUCT_OFFSET (GtkTextLayoutClass, allocate_child),
203 _gtk_marshal_VOID__OBJECT_INT_INT,
212 gtk_text_layout_init (GtkTextLayout *text_layout)
214 text_layout->cursor_visible = TRUE;
218 gtk_text_layout_new (void)
220 return GTK_TEXT_LAYOUT (g_object_new (gtk_text_layout_get_type (), NULL));
224 free_style_cache (GtkTextLayout *text_layout)
226 if (text_layout->one_style_cache)
228 gtk_text_attributes_unref (text_layout->one_style_cache);
229 text_layout->one_style_cache = NULL;
234 gtk_text_layout_finalize (GObject *object)
236 GtkTextLayout *layout;
238 layout = GTK_TEXT_LAYOUT (object);
240 gtk_text_layout_set_buffer (layout, NULL);
242 if (layout->default_style)
243 gtk_text_attributes_unref (layout->default_style);
244 layout->default_style = NULL;
246 if (layout->ltr_context)
248 g_object_unref (G_OBJECT (layout->ltr_context));
249 layout->ltr_context = NULL;
251 if (layout->rtl_context)
253 g_object_unref (G_OBJECT (layout->rtl_context));
254 layout->rtl_context = NULL;
257 (* G_OBJECT_CLASS (parent_class)->finalize) (object);
261 gtk_text_layout_set_buffer (GtkTextLayout *layout,
262 GtkTextBuffer *buffer)
264 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
265 g_return_if_fail (buffer == NULL || GTK_IS_TEXT_BUFFER (buffer));
267 if (layout->buffer == buffer)
270 free_style_cache (layout);
274 _gtk_text_btree_remove_view (_gtk_text_buffer_get_btree (layout->buffer),
277 g_object_unref (G_OBJECT (layout->buffer));
278 layout->buffer = NULL;
283 layout->buffer = buffer;
285 g_object_ref (G_OBJECT (buffer));
287 _gtk_text_btree_add_view (_gtk_text_buffer_get_btree (buffer), layout);
292 gtk_text_layout_default_style_changed (GtkTextLayout *layout)
294 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
296 DV (g_print ("invalidating all due to default style change (%s)\n", G_STRLOC));
297 gtk_text_layout_invalidate_all (layout);
301 gtk_text_layout_set_default_style (GtkTextLayout *layout,
302 GtkTextAttributes *values)
304 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
305 g_return_if_fail (values != NULL);
307 if (values == layout->default_style)
310 gtk_text_attributes_ref (values);
312 if (layout->default_style)
313 gtk_text_attributes_unref (layout->default_style);
315 layout->default_style = values;
317 gtk_text_layout_default_style_changed (layout);
321 gtk_text_layout_set_contexts (GtkTextLayout *layout,
322 PangoContext *ltr_context,
323 PangoContext *rtl_context)
325 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
327 if (layout->ltr_context)
328 g_object_unref (G_OBJECT (ltr_context));
330 layout->ltr_context = ltr_context;
331 g_object_ref (G_OBJECT (ltr_context));
333 if (layout->rtl_context)
334 g_object_unref (G_OBJECT (rtl_context));
336 layout->rtl_context = rtl_context;
337 g_object_ref (G_OBJECT (rtl_context));
339 DV (g_print ("invalidating all due to new pango contexts (%s)\n", G_STRLOC));
340 gtk_text_layout_invalidate_all (layout);
344 * gtk_text_layout_set_cursor_direction:
345 * @direction: the new direction(s) for which to draw cursors.
346 * %GTK_TEXT_DIR_NONE means draw cursors for both
347 * left-to-right insertion and right-to-left insertion.
348 * (The two cursors will be visually distinguished.)
350 * Sets which text directions (left-to-right and/or right-to-left) for
351 * which cursors will be drawn for the insertion point. The visual
352 * point at which new text is inserted depends on whether the new
353 * text is right-to-left or left-to-right, so it may be desired to
354 * make the drawn position of the cursor depend on the keyboard state.
357 gtk_text_layout_set_cursor_direction (GtkTextLayout *layout,
358 GtkTextDirection direction)
360 if (direction != layout->cursor_direction)
362 layout->cursor_direction = direction;
363 gtk_text_layout_invalidate_cursor_line (layout);
368 * gtk_text_layout_get_buffer:
369 * @layout: a #GtkTextLayout
371 * Gets the text buffer used by the layout. See
372 * gtk_text_layout_set_buffer().
374 * Return value: the text buffer used by the layout.
377 gtk_text_layout_get_buffer (GtkTextLayout *layout)
379 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), NULL);
381 return layout->buffer;
385 gtk_text_layout_set_screen_width (GtkTextLayout *layout, gint width)
387 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
388 g_return_if_fail (width >= 0);
389 g_return_if_fail (layout->wrap_loop_count == 0);
391 if (layout->screen_width == width)
394 layout->screen_width = width;
396 DV (g_print ("invalidating all due to new screen width (%s)\n", G_STRLOC));
397 gtk_text_layout_invalidate_all (layout);
401 * gtk_text_layout_set_cursor_visible:
402 * @layout: a #GtkTextLayout
403 * @cursor_visible: If %FALSE, then the insertion cursor will not
404 * be shown, even if the text is editable.
406 * Sets whether the insertion cursor should be shown. Generally,
407 * widgets using #GtkTextLayout will hide the cursor when the
408 * widget does not have the input focus.
411 gtk_text_layout_set_cursor_visible (GtkTextLayout *layout,
412 gboolean cursor_visible)
414 cursor_visible = (cursor_visible != FALSE);
416 if (layout->cursor_visible != cursor_visible)
421 layout->cursor_visible = cursor_visible;
423 /* Now queue a redraw on the paragraph containing the cursor
425 gtk_text_buffer_get_iter_at_mark (layout->buffer, &iter,
426 gtk_text_buffer_get_mark (layout->buffer, "insert"));
428 gtk_text_layout_get_line_yrange (layout, &iter, &y, &height);
429 gtk_text_layout_changed (layout, y, height, height);
431 gtk_text_layout_invalidate_cache (layout, _gtk_text_iter_get_text_line (&iter));
436 * gtk_text_layout_get_cursor_visible:
437 * @layout: a #GtkTextLayout
439 * Returns whether the insertion cursor will be shown.
441 * Return value: if %FALSE, the insertion cursor will not be
442 shown, even if the text is editable.
445 gtk_text_layout_get_cursor_visible (GtkTextLayout *layout)
447 return layout->cursor_visible;
451 * gtk_text_layout_set_preedit_string:
452 * @layout: a #PangoLayout
453 * @preedit_string: a string to display at the insertion point
454 * @preedit_attrs: a #PangoAttrList of attributes that apply to @preedit_string
455 * @cursor_pos: position of cursor within preedit string in chars
457 * Set the preedit string and attributes. The preedit string is a
458 * string showing text that is currently being edited and not
459 * yet committed into the buffer.
462 gtk_text_layout_set_preedit_string (GtkTextLayout *layout,
463 const gchar *preedit_string,
464 PangoAttrList *preedit_attrs,
467 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
468 g_return_if_fail (preedit_attrs != NULL || preedit_string == NULL);
470 if (layout->preedit_string)
471 g_free (layout->preedit_string);
473 if (layout->preedit_attrs)
474 pango_attr_list_unref (layout->preedit_attrs);
478 layout->preedit_string = g_strdup (preedit_string);
479 layout->preedit_len = strlen (layout->preedit_string);
480 pango_attr_list_ref (preedit_attrs);
481 layout->preedit_attrs = preedit_attrs;
483 cursor_pos = CLAMP (cursor_pos, 0, g_utf8_strlen (layout->preedit_string, -1));
484 layout->preedit_cursor = g_utf8_offset_to_pointer (layout->preedit_string, cursor_pos) - layout->preedit_string;
488 layout->preedit_string = NULL;
489 layout->preedit_len = 0;
490 layout->preedit_attrs = NULL;
491 layout->preedit_cursor = 0;
494 gtk_text_layout_invalidate_cursor_line (layout);
498 gtk_text_layout_get_size (GtkTextLayout *layout,
502 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
505 *width = layout->width;
508 *height = layout->height;
512 gtk_text_layout_invalidated (GtkTextLayout *layout)
514 g_signal_emit (G_OBJECT (layout), signals[INVALIDATED], 0);
518 gtk_text_layout_changed (GtkTextLayout *layout,
523 g_signal_emit (G_OBJECT (layout), signals[CHANGED], 0,
524 y, old_height, new_height);
528 gtk_text_layout_free_line_data (GtkTextLayout *layout,
530 GtkTextLineData *line_data)
532 (* GTK_TEXT_LAYOUT_GET_CLASS (layout)->free_line_data)
533 (layout, line, line_data);
537 gtk_text_layout_invalidate (GtkTextLayout *layout,
538 const GtkTextIter *start_index,
539 const GtkTextIter *end_index)
541 (* GTK_TEXT_LAYOUT_GET_CLASS (layout)->invalidate)
542 (layout, start_index, end_index);
546 gtk_text_layout_wrap (GtkTextLayout *layout,
549 GtkTextLineData *line_data)
551 return (* GTK_TEXT_LAYOUT_GET_CLASS (layout)->wrap) (layout, line, line_data);
555 gtk_text_layout_get_lines (GtkTextLayout *layout,
556 /* [top_y, bottom_y) */
561 GtkTextLine *first_btree_line;
562 GtkTextLine *last_btree_line;
566 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), NULL);
567 g_return_val_if_fail (bottom_y > top_y, NULL);
572 _gtk_text_btree_find_line_by_y (_gtk_text_buffer_get_btree (layout->buffer),
573 layout, top_y, first_line_y);
574 if (first_btree_line == NULL)
580 /* -1 since bottom_y is one past */
582 _gtk_text_btree_find_line_by_y (_gtk_text_buffer_get_btree (layout->buffer),
583 layout, bottom_y - 1, NULL);
585 if (!last_btree_line)
587 _gtk_text_btree_get_end_iter_line (_gtk_text_buffer_get_btree (layout->buffer));
589 g_assert (last_btree_line != NULL);
591 line = first_btree_line;
594 retval = g_slist_prepend (retval, line);
596 if (line == last_btree_line)
599 line = _gtk_text_line_next_excluding_last (line);
602 retval = g_slist_reverse (retval);
608 invalidate_cached_style (GtkTextLayout *layout)
610 free_style_cache (layout);
613 /* These should be called around a loop which wraps a CONTIGUOUS bunch
614 * of display lines. If the lines aren't contiguous you can't call
618 gtk_text_layout_wrap_loop_start (GtkTextLayout *layout)
620 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
621 g_return_if_fail (layout->one_style_cache == NULL);
623 layout->wrap_loop_count += 1;
627 gtk_text_layout_wrap_loop_end (GtkTextLayout *layout)
629 g_return_if_fail (layout->wrap_loop_count > 0);
631 layout->wrap_loop_count -= 1;
633 if (layout->wrap_loop_count == 0)
635 /* We cache a some stuff if we're iterating over some lines wrapping
636 * them. This cleans it up.
638 /* Nuke our cached style */
639 invalidate_cached_style (layout);
640 g_assert (layout->one_style_cache == NULL);
645 gtk_text_layout_invalidate_all (GtkTextLayout *layout)
650 if (layout->buffer == NULL)
653 gtk_text_buffer_get_bounds (layout->buffer, &start, &end);
655 gtk_text_layout_invalidate (layout, &start, &end);
659 gtk_text_layout_invalidate_cache (GtkTextLayout *layout,
662 if (layout->one_display_cache && line == layout->one_display_cache->line)
664 GtkTextLineDisplay *tmp_display = layout->one_display_cache;
665 layout->one_display_cache = NULL;
666 gtk_text_layout_free_line_display (layout, tmp_display);
670 /* Now invalidate the paragraph containing the cursor
673 gtk_text_layout_invalidate_cursor_line (GtkTextLayout *layout)
677 GtkTextLineData *line_data;
679 gtk_text_buffer_get_iter_at_mark (layout->buffer, &iter,
680 gtk_text_buffer_get_mark (layout->buffer, "insert"));
682 line = _gtk_text_iter_get_text_line (&iter);
683 line_data = _gtk_text_line_get_data (line, layout);
686 gtk_text_layout_invalidate_cache (layout, line);
687 _gtk_text_line_invalidate_wrap (line, line_data);
688 gtk_text_layout_invalidated (layout);
693 gtk_text_layout_real_invalidate (GtkTextLayout *layout,
694 const GtkTextIter *start,
695 const GtkTextIter *end)
698 GtkTextLine *last_line;
700 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
701 g_return_if_fail (layout->wrap_loop_count == 0);
703 /* Because we may be invalidating a mark, it's entirely possible
704 * that gtk_text_iter_equal (start, end) in which case we
705 * should still invalidate the line they are both on. i.e.
706 * we always invalidate the line with "start" even
707 * if there's an empty range.
711 gtk_text_view_index_spew (start_index, "invalidate start");
712 gtk_text_view_index_spew (end_index, "invalidate end");
715 last_line = _gtk_text_iter_get_text_line (end);
716 line = _gtk_text_iter_get_text_line (start);
720 GtkTextLineData *line_data = _gtk_text_line_get_data (line, layout);
724 gtk_text_layout_invalidate_cache (layout, line);
725 _gtk_text_line_invalidate_wrap (line, line_data);
728 if (line == last_line)
731 line = _gtk_text_line_next_excluding_last (line);
734 gtk_text_layout_invalidated (layout);
738 gtk_text_layout_real_free_line_data (GtkTextLayout *layout,
740 GtkTextLineData *line_data)
742 if (layout->one_display_cache && line == layout->one_display_cache->line)
744 GtkTextLineDisplay *tmp_display = layout->one_display_cache;
745 layout->one_display_cache = NULL;
746 gtk_text_layout_free_line_display (layout, tmp_display);
755 * gtk_text_layout_is_valid:
756 * @layout: a #GtkTextLayout
758 * Check if there are any invalid regions in a #GtkTextLayout's buffer
760 * Return value: %TRUE if any invalid regions were found
763 gtk_text_layout_is_valid (GtkTextLayout *layout)
765 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), FALSE);
767 return _gtk_text_btree_is_valid (_gtk_text_buffer_get_btree (layout->buffer),
772 update_layout_size (GtkTextLayout *layout)
774 _gtk_text_btree_get_view_size (_gtk_text_buffer_get_btree (layout->buffer),
776 &layout->width, &layout->height);
780 * gtk_text_layout_validate_yrange:
781 * @layout: a #GtkTextLayout
782 * @anchor: iter pointing into a line that will be used as the
784 * @y0: offset from the top of the line pointed to by @anchor at
785 * which to begin validation. (The offset here is in pixels
787 * @y1: offset from the top of the line pointed to by @anchor at
788 * which to end validation. (The offset here is in pixels
791 * Ensure that a region of a #GtkTextLayout is valid. The ::changed
792 * signal will be emitted if any lines are validated.
795 gtk_text_layout_validate_yrange (GtkTextLayout *layout,
801 GtkTextLine *first_line = NULL;
802 GtkTextLine *last_line = NULL;
804 gint delta_height = 0;
805 gint first_line_y = 0; /* Quiet GCC */
806 gint last_line_y = 0; /* Quiet GCC */
808 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
815 /* Validate backwards from the anchor line to y0
817 line = _gtk_text_iter_get_text_line (anchor);
819 while (line && seen < -y0)
821 GtkTextLineData *line_data = _gtk_text_line_get_data (line, layout);
822 if (!line_data || !line_data->valid)
824 gint old_height = line_data ? line_data->height : 0;
826 _gtk_text_btree_validate_line (_gtk_text_buffer_get_btree (layout->buffer),
828 line_data = _gtk_text_line_get_data (line, layout);
830 delta_height += line_data->height - old_height;
833 first_line_y = -seen;
837 last_line_y = -seen + line_data->height;
841 seen += line_data->height;
842 line = _gtk_text_line_previous (line);
845 /* Validate forwards to y1 */
846 line = _gtk_text_iter_get_text_line (anchor);
848 while (line && seen < y1)
850 GtkTextLineData *line_data = _gtk_text_line_get_data (line, layout);
851 if (!line_data || !line_data->valid)
853 gint old_height = line_data ? line_data->height : 0;
855 _gtk_text_btree_validate_line (_gtk_text_buffer_get_btree (layout->buffer),
857 line_data = _gtk_text_line_get_data (line, layout);
859 delta_height += line_data->height - old_height;
867 last_line_y = seen + line_data->height;
870 seen += line_data->height;
871 line = _gtk_text_line_next_excluding_last (line);
874 /* If we found and validated any invalid lines, update size and
875 * emit the changed signal
881 update_layout_size (layout);
883 line_top = _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
886 gtk_text_layout_changed (layout,
888 last_line_y - first_line_y - delta_height,
889 last_line_y - first_line_y);
894 * gtk_text_layout_validate:
895 * @tree: a #GtkTextLayout
896 * @max_pixels: the maximum number of pixels to validate. (No more
897 * than one paragraph beyond this limit will be validated)
899 * Validate regions of a #GtkTextLayout. The ::changed signal will
900 * be emitted for each region validated.
903 gtk_text_layout_validate (GtkTextLayout *layout,
906 gint y, old_height, new_height;
908 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
910 while (max_pixels > 0 &&
911 _gtk_text_btree_validate (_gtk_text_buffer_get_btree (layout->buffer),
913 &y, &old_height, &new_height))
915 max_pixels -= new_height;
917 update_layout_size (layout);
918 gtk_text_layout_changed (layout, y, old_height, new_height);
922 static GtkTextLineData*
923 gtk_text_layout_real_wrap (GtkTextLayout *layout,
926 GtkTextLineData *line_data)
928 GtkTextLineDisplay *display;
930 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), NULL);
931 g_return_val_if_fail (line != NULL, NULL);
933 if (line_data == NULL)
935 line_data = _gtk_text_line_data_new (layout, line);
936 _gtk_text_line_add_data (line, line_data);
939 display = gtk_text_layout_get_line_display (layout, line, TRUE);
940 line_data->width = display->width;
941 line_data->height = display->height;
942 line_data->valid = TRUE;
943 gtk_text_layout_free_line_display (layout, display);
949 * Layout utility functions
952 /* If you get the style with get_style () you need to call
953 release_style () to free it. */
954 static GtkTextAttributes*
955 get_style (GtkTextLayout *layout,
956 const GtkTextIter *iter)
960 GtkTextAttributes *style;
962 /* If we have the one-style cache, then it means
963 that we haven't seen a toggle since we filled in the
966 if (layout->one_style_cache != NULL)
968 gtk_text_attributes_ref (layout->one_style_cache);
969 return layout->one_style_cache;
972 g_assert (layout->one_style_cache == NULL);
974 /* Get the tags at this spot */
975 tags = _gtk_text_btree_get_tags (iter, &tag_count);
977 /* No tags, use default style */
978 if (tags == NULL || tag_count == 0)
980 /* One ref for the return value, one ref for the
981 layout->one_style_cache reference */
982 gtk_text_attributes_ref (layout->default_style);
983 gtk_text_attributes_ref (layout->default_style);
984 layout->one_style_cache = layout->default_style;
989 return layout->default_style;
992 /* Sort tags in ascending order of priority */
993 _gtk_text_tag_array_sort (tags, tag_count);
995 style = gtk_text_attributes_new ();
997 gtk_text_attributes_copy_values (layout->default_style,
1000 _gtk_text_attributes_fill_from_tags (style,
1006 g_assert (style->refcount == 1);
1008 /* Leave this style as the last one seen */
1009 g_assert (layout->one_style_cache == NULL);
1010 gtk_text_attributes_ref (style); /* ref held by layout->one_style_cache */
1011 layout->one_style_cache = style;
1013 /* Returning yet another refcount */
1018 release_style (GtkTextLayout *layout,
1019 GtkTextAttributes *style)
1021 g_return_if_fail (style != NULL);
1022 g_return_if_fail (style->refcount > 0);
1024 gtk_text_attributes_unref (style);
1031 /* This function tries to optimize the case where a line
1032 is completely invisible */
1034 totally_invisible_line (GtkTextLayout *layout,
1038 GtkTextLineSegment *seg;
1041 /* If we have a cached style, then we know it does actually apply
1042 * and we can just see if it is invisible.
1044 if (layout->one_style_cache &&
1045 !layout->one_style_cache->invisible)
1047 /* Without the cache, we check if the first char is visible, if so
1048 * we are partially visible. Note that we have to check this since
1049 * we don't know the current invisible/noninvisible toggle state; this
1050 * function can use the whole btree to get it right.
1054 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
1057 if (!_gtk_text_btree_char_is_invisible (iter))
1062 seg = line->segments;
1066 if (seg->byte_count > 0)
1067 bytes += seg->byte_count;
1069 /* Note that these two tests can cause us to bail out
1070 * when we shouldn't, because a higher-priority tag
1071 * may override these settings. However the important
1072 * thing is to only invisible really-invisible lines, rather
1073 * than to invisible all really-invisible lines.
1076 else if (seg->type == >k_text_toggle_on_type)
1078 invalidate_cached_style (layout);
1080 /* Bail out if an elision-unsetting tag begins */
1081 if (seg->body.toggle.info->tag->invisible_set &&
1082 !seg->body.toggle.info->tag->values->invisible)
1085 else if (seg->type == >k_text_toggle_off_type)
1087 invalidate_cached_style (layout);
1089 /* Bail out if an elision-setting tag ends */
1090 if (seg->body.toggle.info->tag->invisible_set &&
1091 seg->body.toggle.info->tag->values->invisible)
1098 if (seg != NULL) /* didn't reach line end */
1105 set_para_values (GtkTextLayout *layout,
1106 GtkTextAttributes *style,
1107 GtkTextLineDisplay *display)
1109 PangoAlignment pango_align = PANGO_ALIGN_LEFT;
1112 display->direction = style->direction;
1114 if (display->direction == GTK_TEXT_DIR_LTR)
1115 display->layout = pango_layout_new (layout->ltr_context);
1117 display->layout = pango_layout_new (layout->rtl_context);
1119 switch (style->justification)
1121 case GTK_JUSTIFY_LEFT:
1122 pango_align = (style->direction == GTK_TEXT_DIR_LTR) ? PANGO_ALIGN_LEFT : PANGO_ALIGN_RIGHT;
1124 case GTK_JUSTIFY_RIGHT:
1125 pango_align = (style->direction == GTK_TEXT_DIR_LTR) ? PANGO_ALIGN_RIGHT : PANGO_ALIGN_LEFT;
1127 case GTK_JUSTIFY_CENTER:
1128 pango_align = PANGO_ALIGN_CENTER;
1130 case GTK_JUSTIFY_FILL:
1131 g_warning ("FIXME we don't support GTK_JUSTIFY_FILL yet");
1134 g_assert_not_reached ();
1138 pango_layout_set_alignment (display->layout, pango_align);
1139 pango_layout_set_spacing (display->layout,
1140 style->pixels_inside_wrap * PANGO_SCALE);
1143 pango_layout_set_tabs (display->layout, style->tabs);
1145 display->top_margin = style->pixels_above_lines;
1146 display->height = style->pixels_above_lines + style->pixels_below_lines;
1147 display->bottom_margin = style->pixels_below_lines;
1148 display->left_margin = style->left_margin;
1149 display->right_margin = style->right_margin;
1151 display->x_offset = display->left_margin;
1153 pango_layout_set_indent (display->layout,
1154 style->indent * PANGO_SCALE);
1156 switch (style->wrap_mode)
1159 layout_width = layout->screen_width - display->left_margin - display->right_margin;
1160 pango_layout_set_width (display->layout, layout_width * PANGO_SCALE);
1161 pango_layout_set_wrap (display->layout, PANGO_WRAP_CHAR);
1165 layout_width = layout->screen_width - display->left_margin - display->right_margin;
1166 pango_layout_set_width (display->layout, layout_width * PANGO_SCALE);
1167 pango_layout_set_wrap (display->layout, PANGO_WRAP_WORD);
1174 display->total_width = MAX (layout->screen_width, layout->width) - display->left_margin - display->right_margin;
1177 static PangoAttribute *
1178 gtk_text_attr_appearance_copy (const PangoAttribute *attr)
1180 const GtkTextAttrAppearance *appearance_attr = (const GtkTextAttrAppearance *)attr;
1182 return gtk_text_attr_appearance_new (&appearance_attr->appearance);
1186 gtk_text_attr_appearance_destroy (PangoAttribute *attr)
1188 GtkTextAppearance *appearance = &((GtkTextAttrAppearance *)attr)->appearance;
1190 if (appearance->bg_stipple)
1191 gdk_drawable_unref (appearance->bg_stipple);
1192 if (appearance->fg_stipple)
1193 gdk_drawable_unref (appearance->fg_stipple);
1199 gtk_text_attr_appearance_compare (const PangoAttribute *attr1,
1200 const PangoAttribute *attr2)
1202 const GtkTextAppearance *appearance1 = &((const GtkTextAttrAppearance *)attr1)->appearance;
1203 const GtkTextAppearance *appearance2 = &((const GtkTextAttrAppearance *)attr2)->appearance;
1205 return (gdk_color_equal (&appearance1->fg_color, &appearance2->fg_color) &&
1206 gdk_color_equal (&appearance1->bg_color, &appearance2->bg_color) &&
1207 appearance1->fg_stipple == appearance2->fg_stipple &&
1208 appearance1->bg_stipple == appearance2->bg_stipple &&
1209 appearance1->underline == appearance2->underline &&
1210 appearance1->strikethrough == appearance2->strikethrough &&
1211 appearance1->draw_bg == appearance2->draw_bg);
1216 * gtk_text_attr_appearance_new:
1219 * Create a new font description attribute. (This attribute
1220 * allows setting family, style, weight, variant, stretch,
1221 * and size simultaneously.)
1225 static PangoAttribute *
1226 gtk_text_attr_appearance_new (const GtkTextAppearance *appearance)
1228 static PangoAttrClass klass = {
1230 gtk_text_attr_appearance_copy,
1231 gtk_text_attr_appearance_destroy,
1232 gtk_text_attr_appearance_compare
1235 GtkTextAttrAppearance *result;
1238 klass.type = gtk_text_attr_appearance_type =
1239 pango_attr_type_register ("GtkTextAttrAppearance");
1241 result = g_new (GtkTextAttrAppearance, 1);
1242 result->attr.klass = &klass;
1244 result->appearance = *appearance;
1246 if (appearance->bg_stipple)
1247 gdk_drawable_ref (appearance->bg_stipple);
1248 if (appearance->fg_stipple)
1249 gdk_drawable_ref (appearance->fg_stipple);
1251 return (PangoAttribute *)result;
1256 add_generic_attrs (GtkTextLayout *layout,
1257 GtkTextAppearance *appearance,
1259 PangoAttrList *attrs,
1264 PangoAttribute *attr;
1266 if (appearance->underline != PANGO_UNDERLINE_NONE)
1268 attr = pango_attr_underline_new (appearance->underline);
1270 attr->start_index = start;
1271 attr->end_index = start + byte_count;
1273 pango_attr_list_insert (attrs, attr);
1276 if (appearance->rise != 0)
1278 attr = pango_attr_rise_new (appearance->rise);
1280 attr->start_index = start;
1281 attr->end_index = start + byte_count;
1283 pango_attr_list_insert (attrs, attr);
1288 attr = gtk_text_attr_appearance_new (appearance);
1290 attr->start_index = start;
1291 attr->end_index = start + byte_count;
1293 ((GtkTextAttrAppearance *)attr)->appearance.is_text = is_text;
1295 pango_attr_list_insert (attrs, attr);
1300 add_text_attrs (GtkTextLayout *layout,
1301 GtkTextAttributes *style,
1303 PangoAttrList *attrs,
1307 PangoAttribute *attr;
1309 attr = pango_attr_font_desc_new (style->font);
1310 attr->start_index = start;
1311 attr->end_index = start + byte_count;
1313 pango_attr_list_insert (attrs, attr);
1315 if (style->font_scale != 1.0)
1317 attr = pango_attr_scale_new (style->font_scale);
1319 attr->start_index = start;
1320 attr->end_index = start + byte_count;
1322 pango_attr_list_insert (attrs, attr);
1327 add_pixbuf_attrs (GtkTextLayout *layout,
1328 GtkTextLineDisplay *display,
1329 GtkTextAttributes *style,
1330 GtkTextLineSegment *seg,
1331 PangoAttrList *attrs,
1334 PangoAttribute *attr;
1335 PangoRectangle logical_rect;
1336 GtkTextPixbuf *pixbuf = &seg->body.pixbuf;
1339 width = gdk_pixbuf_get_width (pixbuf->pixbuf);
1340 height = gdk_pixbuf_get_height (pixbuf->pixbuf);
1343 logical_rect.y = -height * PANGO_SCALE;
1344 logical_rect.width = width * PANGO_SCALE;
1345 logical_rect.height = height * PANGO_SCALE;
1347 attr = pango_attr_shape_new (&logical_rect, &logical_rect);
1348 attr->start_index = start;
1349 attr->end_index = start + seg->byte_count;
1350 pango_attr_list_insert (attrs, attr);
1352 display->shaped_objects =
1353 g_slist_append (display->shaped_objects, pixbuf->pixbuf);
1357 add_child_attrs (GtkTextLayout *layout,
1358 GtkTextLineDisplay *display,
1359 GtkTextAttributes *style,
1360 GtkTextLineSegment *seg,
1361 PangoAttrList *attrs,
1364 PangoAttribute *attr;
1365 PangoRectangle logical_rect;
1366 GtkTextChildAnchor *anchor;
1373 anchor = seg->body.child.obj;
1375 tmp_list = seg->body.child.widgets;
1376 while (tmp_list != NULL)
1378 GtkWidget *child = tmp_list->data;
1380 if (_gtk_anchored_child_get_layout (child) == layout)
1385 gtk_widget_get_child_requisition (child, &req);
1388 height = req.height;
1390 display->shaped_objects =
1391 g_slist_append (display->shaped_objects, child);
1396 tmp_list = g_slist_next (tmp_list);
1399 if (tmp_list == NULL)
1401 /* If tmp_list == NULL then there is no widget at this anchor in
1402 * this display; not an error. We make up an arbitrary size
1403 * to use, just so the programmer can see the blank spot.
1404 * We also put a NULL in the shaped objects list, to keep
1405 * the correspondence between the list and the shaped chars in
1406 * the layout. A bad hack, yes.
1412 display->shaped_objects =
1413 g_slist_append (display->shaped_objects, NULL);
1417 logical_rect.y = -height * PANGO_SCALE;
1418 logical_rect.width = width * PANGO_SCALE;
1419 logical_rect.height = height * PANGO_SCALE;
1421 attr = pango_attr_shape_new (&logical_rect, &logical_rect);
1422 attr->start_index = start;
1423 attr->end_index = start + seg->byte_count;
1424 pango_attr_list_insert (attrs, attr);
1428 add_cursor (GtkTextLayout *layout,
1429 GtkTextLineDisplay *display,
1430 GtkTextLineSegment *seg,
1433 PangoRectangle strong_pos, weak_pos;
1434 GtkTextCursorDisplay *cursor = NULL; /* Quiet GCC */
1435 gboolean add_weak = FALSE;
1436 gboolean add_strong = FALSE;
1438 /* Hide insertion cursor when we have a selection or the layout
1439 * user has hidden the cursor.
1441 if (_gtk_text_btree_mark_is_insert (_gtk_text_buffer_get_btree (layout->buffer),
1442 seg->body.mark.obj) &&
1443 (!layout->cursor_visible ||
1444 gtk_text_buffer_get_selection_bounds (layout->buffer, NULL, NULL)))
1447 pango_layout_get_cursor_pos (display->layout, start, &strong_pos, &weak_pos);
1449 if (layout->cursor_direction == GTK_TEXT_DIR_NONE)
1454 else if (display->direction == layout->cursor_direction)
1461 cursor = g_new (GtkTextCursorDisplay, 1);
1463 cursor->x = PANGO_PIXELS (strong_pos.x);
1464 cursor->y = PANGO_PIXELS (strong_pos.y);
1465 cursor->height = PANGO_PIXELS (strong_pos.height);
1466 cursor->is_strong = TRUE;
1467 cursor->is_weak = (layout->cursor_direction == GTK_TEXT_DIR_NONE) ? FALSE : TRUE;
1468 display->cursors = g_slist_prepend (display->cursors, cursor);
1473 if (weak_pos.x == strong_pos.x && add_strong)
1474 cursor->is_weak = TRUE;
1477 cursor = g_new (GtkTextCursorDisplay, 1);
1479 cursor->x = PANGO_PIXELS (weak_pos.x);
1480 cursor->y = PANGO_PIXELS (weak_pos.y);
1481 cursor->height = PANGO_PIXELS (weak_pos.height);
1482 cursor->is_strong = (layout->cursor_direction == GTK_TEXT_DIR_NONE) ? FALSE : TRUE;
1483 cursor->is_weak = TRUE;
1484 display->cursors = g_slist_prepend (display->cursors, cursor);
1490 is_shape (PangoLayoutRun *run)
1492 GSList *tmp_list = run->item->analysis.extra_attrs;
1496 PangoAttribute *attr = tmp_list->data;
1498 if (attr->klass->type == PANGO_ATTR_SHAPE)
1501 tmp_list = tmp_list->next;
1508 allocate_child_widgets (GtkTextLayout *text_layout,
1509 GtkTextLineDisplay *display)
1511 GSList *shaped = display->shaped_objects;
1512 PangoLayout *layout = display->layout;
1513 PangoLayoutIter *iter;
1515 iter = pango_layout_get_iter (layout);
1519 PangoLayoutRun *run = pango_layout_iter_get_run (iter);
1521 if (run && is_shape (run))
1523 GObject *shaped_object = shaped->data;
1524 shaped = shaped->next;
1526 /* shaped_object is NULL for child anchors with no
1527 * widgets stored at them
1529 if (shaped_object && GTK_IS_WIDGET (shaped_object))
1531 PangoRectangle extents;
1533 /* We emit "allocate_child" with the x,y of
1534 * the widget with respect to the top of the line
1535 * and the left side of the buffer
1538 pango_layout_iter_get_run_extents (iter,
1542 g_signal_emit (G_OBJECT (text_layout),
1543 signals[ALLOCATE_CHILD],
1546 PANGO_PIXELS (extents.x) + display->x_offset,
1547 PANGO_PIXELS (extents.y) + display->top_margin);
1551 while (pango_layout_iter_next_run (iter));
1553 pango_layout_iter_free (iter);
1557 convert_color (GdkColor *result,
1558 PangoAttrColor *attr)
1560 result->red = attr->color.red;
1561 result->blue = attr->color.blue;
1562 result->green = attr->color.green;
1565 /* This function is used to convert the preedit string attributes, which are
1566 * standard PangoAttributes, into the custom attributes used by the text
1567 * widget and insert them into a attr list with a given offset.
1570 add_preedit_attrs (GtkTextLayout *layout,
1571 GtkTextAttributes *style,
1572 PangoAttrList *attrs,
1576 PangoAttrIterator *iter = pango_attr_list_get_iterator (layout->preedit_attrs);
1580 GtkTextAppearance appearance = style->appearance;
1581 PangoFontDescription *font_desc = pango_font_description_copy_static (style->font);
1582 PangoAttribute *insert_attr;
1583 GSList *extra_attrs = NULL;
1585 PangoLanguage *language;
1588 pango_attr_iterator_range (iter, &start, &end);
1590 if (end == G_MAXINT)
1591 end = layout->preedit_len;
1593 pango_attr_iterator_get_font (iter, font_desc, &language, &extra_attrs);
1595 tmp_list = extra_attrs;
1598 PangoAttribute *attr = tmp_list->data;
1600 switch (attr->klass->type)
1602 case PANGO_ATTR_FOREGROUND:
1603 convert_color (&appearance.fg_color, (PangoAttrColor *)attr);
1605 case PANGO_ATTR_BACKGROUND:
1606 convert_color (&appearance.bg_color, (PangoAttrColor *)attr);
1607 appearance.draw_bg = TRUE;
1609 case PANGO_ATTR_UNDERLINE:
1610 appearance.underline = ((PangoAttrInt *)attr)->value;
1612 case PANGO_ATTR_STRIKETHROUGH:
1613 appearance.strikethrough = ((PangoAttrInt *)attr)->value;
1615 case PANGO_ATTR_RISE:
1616 appearance.rise = ((PangoAttrInt *)attr)->value;
1622 pango_attribute_destroy (attr);
1623 tmp_list = tmp_list->next;
1626 g_slist_free (extra_attrs);
1628 insert_attr = pango_attr_font_desc_new (font_desc);
1629 insert_attr->start_index = start + offset;
1630 insert_attr->end_index = end + offset;
1632 pango_attr_list_insert (attrs, insert_attr);
1636 insert_attr = pango_attr_language_new (language);
1637 insert_attr->start_index = start + offset;
1638 insert_attr->end_index = end + offset;
1640 pango_attr_list_insert (attrs, insert_attr);
1643 add_generic_attrs (layout, &appearance, end - start,
1644 attrs, start + offset,
1647 pango_font_description_free (font_desc);
1649 while (pango_attr_iterator_next (iter));
1651 pango_attr_iterator_destroy (iter);
1654 GtkTextLineDisplay *
1655 gtk_text_layout_get_line_display (GtkTextLayout *layout,
1659 GtkTextLineDisplay *display;
1660 GtkTextLineSegment *seg;
1662 GtkTextAttributes *style;
1664 PangoAttrList *attrs;
1665 gint text_allocated, layout_byte_offset, buffer_byte_offset;
1666 PangoRectangle extents;
1667 gboolean para_values_set = FALSE;
1668 GSList *cursor_byte_offsets = NULL;
1669 GSList *cursor_segs = NULL;
1670 GSList *tmp_list1, *tmp_list2;
1671 gboolean saw_widget = FALSE;
1673 g_return_val_if_fail (line != NULL, NULL);
1675 if (layout->one_display_cache)
1677 if (line == layout->one_display_cache->line &&
1678 (size_only || !layout->one_display_cache->size_only))
1679 return layout->one_display_cache;
1682 GtkTextLineDisplay *tmp_display = layout->one_display_cache;
1683 layout->one_display_cache = NULL;
1684 gtk_text_layout_free_line_display (layout, tmp_display);
1688 display = g_new0 (GtkTextLineDisplay, 1);
1690 display->size_only = size_only;
1691 display->line = line;
1692 display->insert_index = -1;
1694 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
1697 /* Special-case optimization for completely
1698 * invisible lines; makes it faster to deal
1699 * with sequences of invisible lines.
1701 if (totally_invisible_line (layout, line, &iter))
1704 /* Allocate space for flat text for buffer
1706 text_allocated = _gtk_text_line_byte_count (line);
1707 text = g_malloc (text_allocated);
1709 attrs = pango_attr_list_new ();
1711 /* Iterate over segments, creating display chunks for them. */
1712 layout_byte_offset = 0; /* current length of layout text (includes preedit, does not include invisible text) */
1713 buffer_byte_offset = 0; /* position in the buffer line */
1714 seg = _gtk_text_iter_get_any_segment (&iter);
1717 /* Displayable segments */
1718 if (seg->type == >k_text_char_type ||
1719 seg->type == >k_text_pixbuf_type ||
1720 seg->type == >k_text_child_type)
1722 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
1724 buffer_byte_offset);
1725 style = get_style (layout, &iter);
1727 /* We have to delay setting the paragraph values until we
1728 * hit the first pixbuf or text segment because toggles at
1729 * the beginning of the paragraph should affect the
1730 * paragraph-global values
1732 if (!para_values_set)
1734 set_para_values (layout, style, display);
1735 para_values_set = TRUE;
1738 /* First see if the chunk is invisible, and ignore it if so. Tk
1739 * looked at tabs, wrap mode, etc. before doing this, but
1740 * that made no sense to me, so I am just skipping the
1743 if (!style->invisible)
1745 if (seg->type == >k_text_char_type)
1747 /* We don't want to split segments because of marks,
1748 * so we scan forward for more segments only
1749 * separated from us by marks. In theory, we should
1750 * also merge segments with identical styles, even
1751 * if there are toggles in-between
1755 GtkTextLineSegment *prev_seg = NULL;
1759 if (seg->type == >k_text_char_type)
1761 memcpy (text + layout_byte_offset, seg->body.chars, seg->byte_count);
1762 layout_byte_offset += seg->byte_count;
1763 buffer_byte_offset += seg->byte_count;
1764 bytes += seg->byte_count;
1766 else if (seg->type == >k_text_right_mark_type ||
1767 seg->type == >k_text_left_mark_type)
1769 /* If we have preedit string, break out of this loop - we'll almost
1770 * certainly have different attributes on the preedit string
1773 if (layout->preedit_len > 0 &&
1774 _gtk_text_btree_mark_is_insert (_gtk_text_buffer_get_btree (layout->buffer),
1775 seg->body.mark.obj))
1778 if (seg->body.mark.visible)
1780 cursor_byte_offsets = g_slist_prepend (cursor_byte_offsets, GINT_TO_POINTER (layout_byte_offset));
1781 cursor_segs = g_slist_prepend (cursor_segs, seg);
1791 seg = prev_seg; /* Back up one */
1792 add_generic_attrs (layout, &style->appearance,
1794 attrs, layout_byte_offset - bytes,
1796 add_text_attrs (layout, style, bytes, attrs,
1797 layout_byte_offset - bytes, size_only);
1799 else if (seg->type == >k_text_pixbuf_type)
1801 add_generic_attrs (layout,
1804 attrs, layout_byte_offset,
1806 add_pixbuf_attrs (layout, display, style,
1807 seg, attrs, layout_byte_offset);
1808 memcpy (text + layout_byte_offset, gtk_text_unknown_char_utf8,
1810 layout_byte_offset += seg->byte_count;
1811 buffer_byte_offset += seg->byte_count;
1813 else if (seg->type == >k_text_child_type)
1817 add_generic_attrs (layout, &style->appearance,
1819 attrs, layout_byte_offset,
1821 add_child_attrs (layout, display, style,
1822 seg, attrs, layout_byte_offset);
1823 memcpy (text + layout_byte_offset, gtk_text_unknown_char_utf8,
1825 layout_byte_offset += seg->byte_count;
1826 buffer_byte_offset += seg->byte_count;
1830 /* We don't know this segment type */
1831 g_assert_not_reached ();
1834 } /* if (segment was visible) */
1837 /* Invisible segment */
1838 buffer_byte_offset += seg->byte_count;
1841 release_style (layout, style);
1845 else if (seg->type == >k_text_toggle_on_type ||
1846 seg->type == >k_text_toggle_off_type)
1848 /* Style may have changed, drop our
1849 current cached style */
1850 invalidate_cached_style (layout);
1854 else if (seg->type == >k_text_right_mark_type ||
1855 seg->type == >k_text_left_mark_type)
1857 gint cursor_offset = 0;
1859 /* At the insertion point, add the preedit string, if any */
1861 if (_gtk_text_btree_mark_is_insert (_gtk_text_buffer_get_btree (layout->buffer),
1862 seg->body.mark.obj))
1864 display->insert_index = layout_byte_offset;
1866 if (layout->preedit_len > 0)
1868 text_allocated += layout->preedit_len;
1869 text = g_realloc (text, text_allocated);
1871 style = get_style (layout, &iter);
1872 add_preedit_attrs (layout, style, attrs, layout_byte_offset, size_only);
1873 release_style (layout, style);
1875 memcpy (text + layout_byte_offset, layout->preedit_string, layout->preedit_len);
1876 layout_byte_offset += layout->preedit_len;
1877 /* DO NOT increment the buffer byte offset for preedit */
1879 cursor_offset = layout->preedit_cursor - layout->preedit_len;
1884 /* Display visible marks */
1886 if (seg->body.mark.visible)
1888 cursor_byte_offsets = g_slist_prepend (cursor_byte_offsets,
1889 GINT_TO_POINTER (layout_byte_offset + cursor_offset));
1890 cursor_segs = g_slist_prepend (cursor_segs, seg);
1895 g_error ("Unknown segment type: %s", seg->type->name);
1900 if (!para_values_set)
1902 style = get_style (layout, &iter);
1903 set_para_values (layout, style, display);
1904 release_style (layout, style);
1907 /* Pango doesn't want the trailing paragraph delimiters */
1910 /* Only one character has type G_UNICODE_PARAGRAPH_SEPARATOR in
1911 * Unicode 3.0; update this if that changes.
1913 #define PARAGRAPH_SEPARATOR 0x2029
1916 if (layout_byte_offset > 0)
1918 const char *prev = g_utf8_prev_char (text + layout_byte_offset);
1919 ch = g_utf8_get_char (prev);
1920 if (ch == PARAGRAPH_SEPARATOR || ch == '\r' || ch == '\n')
1921 layout_byte_offset = prev - text; /* chop off */
1923 if (ch == '\n' && layout_byte_offset > 0)
1925 /* Possibly chop a CR as well */
1926 prev = g_utf8_prev_char (text + layout_byte_offset);
1928 --layout_byte_offset;
1933 pango_layout_set_text (display->layout, text, layout_byte_offset);
1934 pango_layout_set_attributes (display->layout, attrs);
1936 tmp_list1 = cursor_byte_offsets;
1937 tmp_list2 = cursor_segs;
1940 add_cursor (layout, display, tmp_list2->data,
1941 GPOINTER_TO_INT (tmp_list1->data));
1942 tmp_list1 = tmp_list1->next;
1943 tmp_list2 = tmp_list2->next;
1945 g_slist_free (cursor_byte_offsets);
1946 g_slist_free (cursor_segs);
1948 pango_layout_get_extents (display->layout, NULL, &extents);
1950 display->width = PANGO_PIXELS (extents.width) + display->left_margin + display->right_margin;
1951 display->height += PANGO_PIXELS (extents.height);
1953 /* Free this if we aren't in a loop */
1954 if (layout->wrap_loop_count == 0)
1955 invalidate_cached_style (layout);
1958 pango_attr_list_unref (attrs);
1960 layout->one_display_cache = display;
1963 allocate_child_widgets (layout, display);
1969 gtk_text_layout_free_line_display (GtkTextLayout *layout,
1970 GtkTextLineDisplay *display)
1972 if (display != layout->one_display_cache)
1974 if (display->layout)
1975 g_object_unref (G_OBJECT (display->layout));
1977 if (display->cursors)
1979 g_slist_foreach (display->cursors, (GFunc)g_free, NULL);
1980 g_slist_free (display->cursors);
1982 g_slist_free (display->shaped_objects);
1988 /* Functions to convert iter <=> index for the line of a GtkTextLineDisplay
1989 * taking into account the preedit string and invisible text if necessary.
1992 line_display_iter_to_index (GtkTextLayout *layout,
1993 GtkTextLineDisplay *display,
1994 const GtkTextIter *iter)
1998 g_return_val_if_fail (_gtk_text_iter_get_text_line (iter) == display->line, 0);
2000 index = gtk_text_iter_get_visible_line_index (iter);
2002 if (index >= display->insert_index)
2003 index += layout->preedit_len;
2009 line_display_index_to_iter (GtkTextLayout *layout,
2010 GtkTextLineDisplay *display,
2015 g_return_if_fail (!_gtk_text_line_is_last (display->line,
2016 _gtk_text_buffer_get_btree (layout->buffer)));
2018 if (index >= display->insert_index + layout->preedit_len)
2019 index -= layout->preedit_len;
2020 else if (index > display->insert_index)
2022 index = display->insert_index;
2026 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
2027 iter, display->line, 0);
2029 gtk_text_iter_set_visible_line_index (iter, index);
2031 if (_gtk_text_iter_get_text_line (iter) != display->line)
2033 /* Clamp to end of line - really this clamping should have been done
2034 * before here, maybe in Pango, this is a broken band-aid I think
2036 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
2037 iter, display->line, 0);
2039 if (!gtk_text_iter_ends_line (iter))
2040 gtk_text_iter_forward_to_line_end (iter);
2043 gtk_text_iter_forward_chars (iter, trailing);
2047 get_line_at_y (GtkTextLayout *layout,
2054 if (y > layout->height)
2057 *line = _gtk_text_btree_find_line_by_y (_gtk_text_buffer_get_btree (layout->buffer),
2058 layout, y, line_top);
2061 *line = _gtk_text_btree_get_end_iter_line (_gtk_text_buffer_get_btree (layout->buffer));
2065 _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
2071 * gtk_text_layout_get_line_at_y:
2072 * @layout: a #GtkLayout
2073 * @target_iter: the iterator in which the result is stored
2074 * @y: the y positition
2075 * @line_top: location to store the y coordinate of the
2076 * top of the line. (Can by %NULL.)
2078 * Get the iter at the beginning of the line which is displayed
2082 gtk_text_layout_get_line_at_y (GtkTextLayout *layout,
2083 GtkTextIter *target_iter,
2089 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
2090 g_return_if_fail (target_iter != NULL);
2092 get_line_at_y (layout, y, &line, line_top);
2093 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
2094 target_iter, line, 0);
2098 gtk_text_layout_get_iter_at_pixel (GtkTextLayout *layout,
2099 GtkTextIter *target_iter,
2103 gint byte_index, trailing;
2105 GtkTextLineDisplay *display;
2107 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
2108 g_return_if_fail (target_iter != NULL);
2110 get_line_at_y (layout, y, &line, &line_top);
2112 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2114 x -= display->x_offset;
2115 y -= line_top + display->top_margin;
2117 /* If we are below the layout, position the cursor at the last character
2120 if (y > display->height - display->top_margin - display->bottom_margin)
2122 byte_index = _gtk_text_line_byte_count (line);
2127 /* Ignore the "outside" return value from pango. Pango is doing
2128 * the right thing even if we are outside the layout in the
2131 pango_layout_xy_to_index (display->layout, x * PANGO_SCALE, y * PANGO_SCALE,
2132 &byte_index, &trailing);
2135 line_display_index_to_iter (layout, display, target_iter, byte_index, trailing);
2137 gtk_text_layout_free_line_display (layout, display);
2141 * gtk_text_layout_get_cursor_locations
2142 * @layout: a #GtkTextLayout
2143 * @iter: a #GtkTextIter
2144 * @strong_pos: location to store the strong cursor position (may be %NULL)
2145 * @weak_pos: location to store the weak cursor position (may be %NULL)
2147 * Given an iterator within a text laout, determine the positions that of the
2148 * strong and weak cursors if the insertion point is at that
2149 * iterator. The position of each cursor is stored as a zero-width
2150 * rectangle. The strong cursor location is the location where
2151 * characters of the directionality equal to the base direction of the
2152 * paragraph are inserted. The weak cursor location is the location
2153 * where characters of the directionality opposite to the base
2154 * direction of the paragraph are inserted.
2157 gtk_text_layout_get_cursor_locations (GtkTextLayout *layout,
2159 GdkRectangle *strong_pos,
2160 GdkRectangle *weak_pos)
2163 GtkTextLineDisplay *display;
2167 PangoRectangle pango_strong_pos;
2168 PangoRectangle pango_weak_pos;
2170 g_return_if_fail (layout != NULL);
2171 g_return_if_fail (iter != NULL);
2173 line = _gtk_text_iter_get_text_line (iter);
2174 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2175 index = line_display_iter_to_index (layout, display, iter);
2177 line_top = _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
2180 pango_layout_get_cursor_pos (display->layout, index,
2181 strong_pos ? &pango_strong_pos : NULL,
2182 weak_pos ? &pango_weak_pos : NULL);
2186 strong_pos->x = display->x_offset + pango_strong_pos.x / PANGO_SCALE;
2187 strong_pos->y = line_top + display->top_margin + pango_strong_pos.y / PANGO_SCALE;
2188 strong_pos->width = 0;
2189 strong_pos->height = pango_strong_pos.height / PANGO_SCALE;
2194 weak_pos->x = display->x_offset + pango_weak_pos.x / PANGO_SCALE;
2195 weak_pos->y = line_top + display->top_margin + pango_weak_pos.y / PANGO_SCALE;
2196 weak_pos->width = 0;
2197 weak_pos->height = pango_weak_pos.height / PANGO_SCALE;
2200 gtk_text_layout_free_line_display (layout, display);
2204 * gtk_text_layout_get_line_yrange:
2205 * @layout: a #GtkTextLayout
2206 * @iter: a #GtkTextIter
2207 * @y: location to store the top of the paragraph in pixels,
2209 * @height location to store the height of the paragraph in pixels,
2212 * Find the range of y coordinates for the paragraph containing
2216 gtk_text_layout_get_line_yrange (GtkTextLayout *layout,
2217 const GtkTextIter *iter,
2223 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
2224 g_return_if_fail (_gtk_text_iter_get_btree (iter) == _gtk_text_buffer_get_btree (layout->buffer));
2226 line = _gtk_text_iter_get_text_line (iter);
2229 *y = _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
2233 GtkTextLineData *line_data = _gtk_text_line_get_data (line, layout);
2235 *height = line_data->height;
2242 * _gtk_text_layout_get_line_xrange:
2243 * @layout: a #GtkTextLayout
2244 * @iter: a #GtkTextIter
2245 * @x: location to store the top of the paragraph in pixels,
2247 * @width location to store the height of the paragraph in pixels,
2250 * Find the range of X coordinates for the paragraph containing
2251 * the given iter. Private for 2.0 due to API freeze, could
2252 * be made public for 2.2.
2255 _gtk_text_layout_get_line_xrange (GtkTextLayout *layout,
2256 const GtkTextIter *iter,
2262 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
2263 g_return_if_fail (_gtk_text_iter_get_btree (iter) == _gtk_text_buffer_get_btree (layout->buffer));
2265 line = _gtk_text_iter_get_text_line (iter);
2268 *x = 0; /* FIXME This is wrong; should represent the first available cursor position */
2272 GtkTextLineData *line_data = _gtk_text_line_get_data (line, layout);
2274 *width = line_data->width;
2281 gtk_text_layout_get_iter_location (GtkTextLayout *layout,
2282 const GtkTextIter *iter,
2285 PangoRectangle pango_rect;
2288 GtkTextLineDisplay *display;
2292 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
2293 g_return_if_fail (_gtk_text_iter_get_btree (iter) == _gtk_text_buffer_get_btree (layout->buffer));
2294 g_return_if_fail (rect != NULL);
2296 tree = _gtk_text_iter_get_btree (iter);
2297 line = _gtk_text_iter_get_text_line (iter);
2299 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2301 rect->y = _gtk_text_btree_find_line_top (tree, line, layout);
2303 x_offset = display->x_offset * PANGO_SCALE;
2305 byte_index = gtk_text_iter_get_line_index (iter);
2307 pango_layout_index_to_pos (display->layout, byte_index, &pango_rect);
2309 rect->x = PANGO_PIXELS (x_offset + pango_rect.x);
2310 rect->y += PANGO_PIXELS (pango_rect.y) + display->top_margin;
2311 rect->width = PANGO_PIXELS (pango_rect.width);
2312 rect->height = PANGO_PIXELS (pango_rect.height);
2314 gtk_text_layout_free_line_display (layout, display);
2319 /* Find the iter for the logical beginning of the first display line whose
2320 * top y is >= y. If none exists, move the iter to the logical beginning
2321 * of the last line in the buffer.
2324 find_display_line_below (GtkTextLayout *layout,
2328 GtkTextLine *line, *next;
2329 GtkTextLine *found_line = NULL;
2331 gint found_byte = 0;
2333 line = _gtk_text_btree_find_line_by_y (_gtk_text_buffer_get_btree (layout->buffer),
2334 layout, y, &line_top);
2338 _gtk_text_btree_get_end_iter_line (_gtk_text_buffer_get_btree (layout->buffer));
2341 _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
2345 while (line && !found_line)
2347 GtkTextLineDisplay *display = gtk_text_layout_get_line_display (layout, line, FALSE);
2348 PangoLayoutIter *layout_iter;
2350 layout_iter = pango_layout_get_iter (display->layout);
2352 line_top += display->top_margin;
2356 gint first_y, last_y;
2357 PangoLayoutLine *layout_line = pango_layout_iter_get_line (layout_iter);
2359 found_byte = layout_line->start_index;
2367 pango_layout_iter_get_line_yrange (layout_iter, &first_y, &last_y);
2368 line_top += (last_y - first_y) / PANGO_SCALE;
2370 while (pango_layout_iter_next_line (layout_iter));
2372 pango_layout_iter_free (layout_iter);
2374 line_top += display->bottom_margin;
2375 gtk_text_layout_free_line_display (layout, display);
2377 next = _gtk_text_line_next_excluding_last (line);
2384 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
2385 iter, found_line, found_byte);
2388 /* Find the iter for the logical beginning of the last display line whose
2389 * top y is >= y. If none exists, move the iter to the logical beginning
2390 * of the first line in the buffer.
2393 find_display_line_above (GtkTextLayout *layout,
2398 GtkTextLine *found_line = NULL;
2400 gint found_byte = 0;
2402 line = _gtk_text_btree_find_line_by_y (_gtk_text_buffer_get_btree (layout->buffer), layout, y, &line_top);
2405 line = _gtk_text_btree_get_end_iter_line (_gtk_text_buffer_get_btree (layout->buffer));
2407 line_top = _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer), line, layout);
2410 while (line && !found_line)
2412 GtkTextLineDisplay *display = gtk_text_layout_get_line_display (layout, line, FALSE);
2413 PangoRectangle logical_rect;
2414 PangoLayoutIter *layout_iter;
2417 layout_iter = pango_layout_get_iter (display->layout);
2419 line_top -= display->top_margin + display->bottom_margin;
2420 pango_layout_iter_get_layout_extents (layout_iter, NULL, &logical_rect);
2421 line_top -= logical_rect.height / PANGO_SCALE;
2423 tmp_top = line_top + display->top_margin;
2427 gint first_y, last_y;
2428 PangoLayoutLine *layout_line = pango_layout_iter_get_line (layout_iter);
2430 found_byte = layout_line->start_index;
2432 pango_layout_iter_get_line_yrange (layout_iter, &first_y, &last_y);
2434 tmp_top -= (last_y - first_y) / PANGO_SCALE;
2439 pango_layout_iter_free (layout_iter);
2443 while (pango_layout_iter_next_line (layout_iter));
2445 pango_layout_iter_free (layout_iter);
2447 gtk_text_layout_free_line_display (layout, display);
2449 line = _gtk_text_line_previous (line);
2455 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
2456 iter, found_line, found_byte);
2458 gtk_text_buffer_get_iter_at_offset (layout->buffer, iter, 0);
2462 * gtk_text_layout_clamp_iter_to_vrange:
2463 * @layout: a #GtkTextLayout
2464 * @iter: a #GtkTextIter
2465 * @top: the top of the range
2466 * @bottom: the bottom the range
2468 * If the iterator is not fully in the range @top <= y < @bottom,
2469 * then, if possible, move it the minimum distance so that the
2470 * iterator in this range.
2472 * Returns: %TRUE if the iterator was moved, otherwise %FALSE.
2475 gtk_text_layout_clamp_iter_to_vrange (GtkTextLayout *layout,
2480 GdkRectangle iter_rect;
2482 gtk_text_layout_get_iter_location (layout, iter, &iter_rect);
2484 /* If the iter is at least partially above the range, put the iter
2485 * at the first fully visible line after the range.
2487 if (iter_rect.y < top)
2489 find_display_line_below (layout, iter, top);
2493 /* Otherwise, if the iter is at least partially below the screen, put the
2494 * iter on the last logical position of the last completely visible
2497 else if (iter_rect.y + iter_rect.height > bottom)
2499 find_display_line_above (layout, iter, bottom);
2508 * gtk_text_layout_move_iter_to_previous_line:
2509 * @layout: a #GtkLayout
2510 * @iter: a #GtkTextIter
2512 * Move the iterator to the beginning of the previous line. The lines
2513 * of a wrapped paragraph are treated as distinct for this operation.
2516 gtk_text_layout_move_iter_to_previous_line (GtkTextLayout *layout,
2520 GtkTextLineDisplay *display;
2523 PangoLayoutLine *layout_line;
2525 gboolean update_byte = FALSE;
2527 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), FALSE);
2528 g_return_val_if_fail (iter != NULL, FALSE);
2533 line = _gtk_text_iter_get_text_line (iter);
2534 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2535 line_byte = line_display_iter_to_index (layout, display, iter);
2537 /* If display->height == 0 then the line is invisible, so don't
2540 while (display->height == 0)
2542 GtkTextLine *prev_line;
2544 prev_line = _gtk_text_line_previous (line);
2546 if (prev_line == NULL)
2548 line_display_index_to_iter (layout, display, iter, 0, 0);
2552 gtk_text_layout_free_line_display (layout, display);
2555 display = gtk_text_layout_get_line_display (layout, prev_line, FALSE);
2559 tmp_list = pango_layout_get_lines (display->layout);
2560 layout_line = tmp_list->data;
2564 line_byte = layout_line->start_index + layout_line->length;
2567 if (line_byte < layout_line->length || !tmp_list->next) /* first line of paragraph */
2569 GtkTextLine *prev_line;
2571 prev_line = _gtk_text_line_previous (line);
2574 gtk_text_layout_free_line_display (layout, display);
2576 display = gtk_text_layout_get_line_display (layout, prev_line, FALSE);
2578 if (display->height > 0)
2580 tmp_list = g_slist_last (pango_layout_get_lines (display->layout));
2581 layout_line = tmp_list->data;
2583 line_display_index_to_iter (layout, display, iter,
2584 layout_line->start_index + layout_line->length, 0);
2588 prev_line = _gtk_text_line_previous (prev_line);
2591 if (prev_line == NULL)
2592 line_display_index_to_iter (layout, display, iter, 0, 0);
2596 gint prev_offset = layout_line->start_index;
2598 tmp_list = tmp_list->next;
2601 layout_line = tmp_list->data;
2603 if (line_byte < layout_line->start_index + layout_line->length ||
2606 line_display_index_to_iter (layout, display, iter, prev_offset, 0);
2610 prev_offset = layout_line->start_index;
2611 tmp_list = tmp_list->next;
2617 gtk_text_layout_free_line_display (layout, display);
2620 !gtk_text_iter_equal (iter, &orig) &&
2621 !gtk_text_iter_is_end (iter);
2625 * gtk_text_layout_move_iter_to_next_line:
2626 * @layout: a #GtkLayout
2627 * @iter: a #GtkTextIter
2629 * Move the iterator to the beginning of the next line. The
2630 * lines of a wrapped paragraph are treated as distinct for
2634 gtk_text_layout_move_iter_to_next_line (GtkTextLayout *layout,
2638 GtkTextLineDisplay *display;
2641 gboolean found = FALSE;
2642 gboolean found_after = FALSE;
2643 gboolean first = TRUE;
2645 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), FALSE);
2646 g_return_val_if_fail (iter != NULL, FALSE);
2650 line = _gtk_text_iter_get_text_line (iter);
2652 while (line && !found_after)
2656 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2658 if (display->height == 0)
2663 line_byte = line_display_iter_to_index (layout, display, iter);
2669 tmp_list = pango_layout_get_lines (display->layout);
2670 while (tmp_list && !found_after)
2672 PangoLayoutLine *layout_line = tmp_list->data;
2676 line_display_index_to_iter (layout, display, iter,
2677 layout_line->start_index, 0);
2680 else if (line_byte < layout_line->start_index + layout_line->length || !tmp_list->next)
2683 tmp_list = tmp_list->next;
2688 gtk_text_layout_free_line_display (layout, display);
2690 line = _gtk_text_line_next_excluding_last (line);
2694 !gtk_text_iter_equal (iter, &orig) &&
2695 !gtk_text_iter_is_end (iter);
2699 * gtk_text_layout_move_iter_to_line_end:
2700 * @layout: a #GtkTextLayout
2701 * @direction: if negative, move to beginning of line, otherwise
2702 move to end of line.
2704 * Move to the beginning or end of a display line.
2707 gtk_text_layout_move_iter_to_line_end (GtkTextLayout *layout,
2712 GtkTextLineDisplay *display;
2717 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), FALSE);
2718 g_return_val_if_fail (iter != NULL, FALSE);
2722 line = _gtk_text_iter_get_text_line (iter);
2723 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2724 line_byte = line_display_iter_to_index (layout, display, iter);
2726 tmp_list = pango_layout_get_lines (display->layout);
2729 PangoLayoutLine *layout_line = tmp_list->data;
2731 if (line_byte < layout_line->start_index + layout_line->length || !tmp_list->next)
2733 line_display_index_to_iter (layout, display, iter,
2734 direction < 0 ? layout_line->start_index : layout_line->start_index + layout_line->length,
2737 /* FIXME: As a bad hack, we move back one position when we
2738 * are inside a paragraph to avoid going to next line on a
2739 * forced break not at whitespace. Real fix is to keep track
2740 * of whether marks are at leading or trailing edge? */
2741 if (direction > 0 && layout_line->length > 0 && !gtk_text_iter_ends_line (iter))
2742 gtk_text_iter_backward_char (iter);
2747 tmp_list = tmp_list->next;
2750 gtk_text_layout_free_line_display (layout, display);
2753 !gtk_text_iter_equal (iter, &orig) &&
2754 !gtk_text_iter_is_end (iter);
2759 * gtk_text_layout_iter_starts_line:
2760 * @layout: a #GtkTextLayout
2761 * @iter: iterator to test
2763 * Tests whether an iterator is at the start of a display line.
2766 gtk_text_layout_iter_starts_line (GtkTextLayout *layout,
2767 const GtkTextIter *iter)
2770 GtkTextLineDisplay *display;
2774 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), FALSE);
2775 g_return_val_if_fail (iter != NULL, FALSE);
2777 line = _gtk_text_iter_get_text_line (iter);
2778 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2779 line_byte = line_display_iter_to_index (layout, display, iter);
2781 tmp_list = pango_layout_get_lines (display->layout);
2784 PangoLayoutLine *layout_line = tmp_list->data;
2786 if (line_byte < layout_line->start_index + layout_line->length ||
2789 /* We're located on this line or the para delimiters before
2792 gtk_text_layout_free_line_display (layout, display);
2794 if (line_byte == layout_line->start_index)
2800 tmp_list = tmp_list->next;
2803 g_assert_not_reached ();
2808 gtk_text_layout_get_iter_at_line (GtkTextLayout *layout,
2813 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
2814 iter, line, byte_offset);
2819 * gtk_text_layout_move_iter_to_x:
2820 * @layout: a #GtkTextLayout
2821 * @iter: a #GtkTextIter
2824 * Keeping the iterator on the same line of the layout, move it to the
2825 * specified X coordinate. The lines of a wrapped paragraph are
2826 * treated as distinct for this operation.
2829 gtk_text_layout_move_iter_to_x (GtkTextLayout *layout,
2834 GtkTextLineDisplay *display;
2836 PangoLayoutIter *layout_iter;
2838 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
2839 g_return_if_fail (iter != NULL);
2841 line = _gtk_text_iter_get_text_line (iter);
2843 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2844 line_byte = line_display_iter_to_index (layout, display, iter);
2846 layout_iter = pango_layout_get_iter (display->layout);
2850 PangoLayoutLine *layout_line = pango_layout_iter_get_line (layout_iter);
2852 if (line_byte < layout_line->start_index + layout_line->length ||
2853 pango_layout_iter_at_last_line (layout_iter))
2855 PangoRectangle logical_rect;
2856 gint byte_index, trailing;
2857 gint x_offset = display->x_offset * PANGO_SCALE;
2859 pango_layout_iter_get_line_extents (layout_iter, NULL, &logical_rect);
2861 pango_layout_line_x_to_index (layout_line,
2862 x * PANGO_SCALE - x_offset - logical_rect.x,
2863 &byte_index, &trailing);
2865 line_display_index_to_iter (layout, display, iter, byte_index, trailing);
2870 while (pango_layout_iter_next_line (layout_iter));
2872 pango_layout_iter_free (layout_iter);
2874 gtk_text_layout_free_line_display (layout, display);
2878 * gtk_text_layout_move_iter_visually:
2879 * @layout: a #GtkTextLayout
2880 * @iter: a #GtkTextIter
2881 * @count: number of characters to move (negative moves left, positive moves right)
2883 * Move the iterator a given number of characters visually, treating
2884 * it as the strong cursor position. If @count is positive, then the
2885 * new strong cursor position will be @count positions to the right of
2886 * the old cursor position. If @count is negative then the new strong
2887 * cursor position will be @count positions to the left of the old
2890 * In the presence of bidirection text, the correspondence
2891 * between logical and visual order will depend on the direction
2892 * of the current run, and there may be jumps when the cursor
2893 * is moved off of the end of a run.
2897 gtk_text_layout_move_iter_visually (GtkTextLayout *layout,
2901 GtkTextLineDisplay *display = NULL;
2904 g_return_val_if_fail (layout != NULL, FALSE);
2905 g_return_val_if_fail (iter != NULL, FALSE);
2911 GtkTextLine *line = _gtk_text_iter_get_text_line (iter);
2913 gint extra_back = 0;
2916 int byte_count = _gtk_text_line_byte_count (line);
2922 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2924 if (layout->cursor_direction == GTK_TEXT_DIR_NONE)
2927 strong = display->direction == layout->cursor_direction;
2929 line_byte = line_display_iter_to_index (layout, display, iter);
2933 pango_layout_move_cursor_visually (display->layout, strong, line_byte, 0, 1, &new_index, &new_trailing);
2938 pango_layout_move_cursor_visually (display->layout, strong, line_byte, 0, -1, &new_index, &new_trailing);
2942 /* We need to handle the preedit string specially. Well, we don't really need to
2943 * handle it specially, since hopefully calling gtk_im_context_reset() will
2944 * remove the preedit string; but if we start off in front of the preedit
2945 * string (logically) and end up in or on the back edge of the preedit string,
2946 * we should move the iter one place farther.
2948 if (layout->preedit_len > 0 && display->insert_index >= 0)
2950 if (line_byte == display->insert_index + layout->preedit_len &&
2951 new_index < display->insert_index + layout->preedit_len)
2953 line_byte = display->insert_index;
2958 if (new_index < 0 || (new_index == 0 && extra_back))
2960 line = _gtk_text_line_previous (line);
2965 gtk_text_layout_free_line_display (layout, display);
2966 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2967 new_index = _gtk_text_line_byte_count (line);
2969 else if (new_index > byte_count)
2971 line = _gtk_text_line_next_excluding_last (line);
2975 gtk_text_layout_free_line_display (layout, display);
2976 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2980 line_display_index_to_iter (layout, display, iter, new_index, new_trailing);
2982 gtk_text_iter_backward_char (iter);
2985 gtk_text_layout_free_line_display (layout, display);
2990 !gtk_text_iter_equal (iter, &orig) &&
2991 !gtk_text_iter_is_end (iter);
2995 gtk_text_layout_spew (GtkTextLayout *layout)
2998 GtkTextDisplayLine *iter;
3000 guint paragraphs = 0;
3001 GtkTextLine *last_line = NULL;
3003 iter = layout->line_list;
3004 while (iter != NULL)
3006 if (iter->line != last_line)
3008 printf ("%5u paragraph (%p)\n", paragraphs, iter->line);
3010 last_line = iter->line;
3013 printf (" %5u y: %d len: %d start: %d bytes: %d\n",
3014 wrapped, iter->y, iter->length, iter->byte_offset,
3021 printf ("Layout %s recompute\n",
3022 layout->need_recompute ? "needs" : "doesn't need");
3024 printf ("Layout pars: %u lines: %u size: %d x %d Screen width: %d\n",
3025 paragraphs, wrapped, layout->width,
3026 layout->height, layout->screen_width);