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);
704 gtk_text_view_index_spew (start_index, "invalidate start");
705 gtk_text_view_index_spew (end_index, "invalidate end");
708 last_line = _gtk_text_iter_get_text_line (end);
709 line = _gtk_text_iter_get_text_line (start);
713 GtkTextLineData *line_data = _gtk_text_line_get_data (line, layout);
716 (line != last_line || !gtk_text_iter_starts_line (end)))
718 gtk_text_layout_invalidate_cache (layout, line);
719 _gtk_text_line_invalidate_wrap (line, line_data);
722 if (line == last_line)
725 line = _gtk_text_line_next_excluding_last (line);
728 gtk_text_layout_invalidated (layout);
732 gtk_text_layout_real_free_line_data (GtkTextLayout *layout,
734 GtkTextLineData *line_data)
736 if (layout->one_display_cache && line == layout->one_display_cache->line)
738 GtkTextLineDisplay *tmp_display = layout->one_display_cache;
739 layout->one_display_cache = NULL;
740 gtk_text_layout_free_line_display (layout, tmp_display);
749 * gtk_text_layout_is_valid:
750 * @layout: a #GtkTextLayout
752 * Check if there are any invalid regions in a #GtkTextLayout's buffer
754 * Return value: %TRUE if any invalid regions were found
757 gtk_text_layout_is_valid (GtkTextLayout *layout)
759 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), FALSE);
761 return _gtk_text_btree_is_valid (_gtk_text_buffer_get_btree (layout->buffer),
766 update_layout_size (GtkTextLayout *layout)
768 _gtk_text_btree_get_view_size (_gtk_text_buffer_get_btree (layout->buffer),
770 &layout->width, &layout->height);
774 * gtk_text_layout_validate_yrange:
775 * @layout: a #GtkTextLayout
776 * @anchor: iter pointing into a line that will be used as the
778 * @y0: offset from the top of the line pointed to by @anchor at
779 * which to begin validation. (The offset here is in pixels
781 * @y1: offset from the top of the line pointed to by @anchor at
782 * which to end validation. (The offset here is in pixels
785 * Ensure that a region of a #GtkTextLayout is valid. The ::changed
786 * signal will be emitted if any lines are validated.
789 gtk_text_layout_validate_yrange (GtkTextLayout *layout,
795 GtkTextLine *first_line = NULL;
796 GtkTextLine *last_line = NULL;
798 gint delta_height = 0;
799 gint first_line_y = 0; /* Quiet GCC */
800 gint last_line_y = 0; /* Quiet GCC */
802 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
809 /* Validate backwards from the anchor line to y0
811 line = _gtk_text_iter_get_text_line (anchor);
813 while (line && seen < -y0)
815 GtkTextLineData *line_data = _gtk_text_line_get_data (line, layout);
816 if (!line_data || !line_data->valid)
818 gint old_height = line_data ? line_data->height : 0;
820 _gtk_text_btree_validate_line (_gtk_text_buffer_get_btree (layout->buffer),
822 line_data = _gtk_text_line_get_data (line, layout);
824 delta_height += line_data->height - old_height;
827 first_line_y = -seen;
831 last_line_y = -seen + line_data->height;
835 seen += line_data->height;
836 line = _gtk_text_line_previous (line);
839 /* Validate forwards to y1 */
840 line = _gtk_text_iter_get_text_line (anchor);
842 while (line && seen < y1)
844 GtkTextLineData *line_data = _gtk_text_line_get_data (line, layout);
845 if (!line_data || !line_data->valid)
847 gint old_height = line_data ? line_data->height : 0;
849 _gtk_text_btree_validate_line (_gtk_text_buffer_get_btree (layout->buffer),
851 line_data = _gtk_text_line_get_data (line, layout);
853 delta_height += line_data->height - old_height;
861 last_line_y = seen + line_data->height;
864 seen += line_data->height;
865 line = _gtk_text_line_next_excluding_last (line);
868 /* If we found and validated any invalid lines, update size and
869 * emit the changed signal
875 update_layout_size (layout);
877 line_top = _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
880 gtk_text_layout_changed (layout,
882 last_line_y - first_line_y - delta_height,
883 last_line_y - first_line_y);
888 * gtk_text_layout_validate:
889 * @tree: a #GtkTextLayout
890 * @max_pixels: the maximum number of pixels to validate. (No more
891 * than one paragraph beyond this limit will be validated)
893 * Validate regions of a #GtkTextLayout. The ::changed signal will
894 * be emitted for each region validated.
897 gtk_text_layout_validate (GtkTextLayout *layout,
900 gint y, old_height, new_height;
902 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
904 while (max_pixels > 0 &&
905 _gtk_text_btree_validate (_gtk_text_buffer_get_btree (layout->buffer),
907 &y, &old_height, &new_height))
909 max_pixels -= new_height;
911 update_layout_size (layout);
912 gtk_text_layout_changed (layout, y, old_height, new_height);
916 static GtkTextLineData*
917 gtk_text_layout_real_wrap (GtkTextLayout *layout,
920 GtkTextLineData *line_data)
922 GtkTextLineDisplay *display;
924 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), NULL);
925 g_return_val_if_fail (line != NULL, NULL);
927 if (line_data == NULL)
929 line_data = _gtk_text_line_data_new (layout, line);
930 _gtk_text_line_add_data (line, line_data);
933 display = gtk_text_layout_get_line_display (layout, line, TRUE);
934 line_data->width = display->width;
935 line_data->height = display->height;
936 line_data->valid = TRUE;
937 gtk_text_layout_free_line_display (layout, display);
943 * Layout utility functions
946 /* If you get the style with get_style () you need to call
947 release_style () to free it. */
948 static GtkTextAttributes*
949 get_style (GtkTextLayout *layout,
950 const GtkTextIter *iter)
954 GtkTextAttributes *style;
956 /* If we have the one-style cache, then it means
957 that we haven't seen a toggle since we filled in the
960 if (layout->one_style_cache != NULL)
962 gtk_text_attributes_ref (layout->one_style_cache);
963 return layout->one_style_cache;
966 g_assert (layout->one_style_cache == NULL);
968 /* Get the tags at this spot */
969 tags = _gtk_text_btree_get_tags (iter, &tag_count);
971 /* No tags, use default style */
972 if (tags == NULL || tag_count == 0)
974 /* One ref for the return value, one ref for the
975 layout->one_style_cache reference */
976 gtk_text_attributes_ref (layout->default_style);
977 gtk_text_attributes_ref (layout->default_style);
978 layout->one_style_cache = layout->default_style;
983 return layout->default_style;
986 /* Sort tags in ascending order of priority */
987 _gtk_text_tag_array_sort (tags, tag_count);
989 style = gtk_text_attributes_new ();
991 gtk_text_attributes_copy_values (layout->default_style,
994 _gtk_text_attributes_fill_from_tags (style,
1000 g_assert (style->refcount == 1);
1002 /* Leave this style as the last one seen */
1003 g_assert (layout->one_style_cache == NULL);
1004 gtk_text_attributes_ref (style); /* ref held by layout->one_style_cache */
1005 layout->one_style_cache = style;
1007 /* Returning yet another refcount */
1012 release_style (GtkTextLayout *layout,
1013 GtkTextAttributes *style)
1015 g_return_if_fail (style != NULL);
1016 g_return_if_fail (style->refcount > 0);
1018 gtk_text_attributes_unref (style);
1025 /* This function tries to optimize the case where a line
1026 is completely invisible */
1028 totally_invisible_line (GtkTextLayout *layout,
1032 GtkTextLineSegment *seg;
1035 /* If we have a cached style, then we know it does actually apply
1036 * and we can just see if it is invisible.
1038 if (layout->one_style_cache &&
1039 !layout->one_style_cache->invisible)
1041 /* Without the cache, we check if the first char is visible, if so
1042 * we are partially visible. Note that we have to check this since
1043 * we don't know the current invisible/noninvisible toggle state; this
1044 * function can use the whole btree to get it right.
1048 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
1051 if (!_gtk_text_btree_char_is_invisible (iter))
1056 seg = line->segments;
1060 if (seg->byte_count > 0)
1061 bytes += seg->byte_count;
1063 /* Note that these two tests can cause us to bail out
1064 * when we shouldn't, because a higher-priority tag
1065 * may override these settings. However the important
1066 * thing is to only invisible really-invisible lines, rather
1067 * than to invisible all really-invisible lines.
1070 else if (seg->type == >k_text_toggle_on_type)
1072 invalidate_cached_style (layout);
1074 /* Bail out if an elision-unsetting tag begins */
1075 if (seg->body.toggle.info->tag->invisible_set &&
1076 !seg->body.toggle.info->tag->values->invisible)
1079 else if (seg->type == >k_text_toggle_off_type)
1081 invalidate_cached_style (layout);
1083 /* Bail out if an elision-setting tag ends */
1084 if (seg->body.toggle.info->tag->invisible_set &&
1085 seg->body.toggle.info->tag->values->invisible)
1092 if (seg != NULL) /* didn't reach line end */
1099 set_para_values (GtkTextLayout *layout,
1100 GtkTextAttributes *style,
1101 GtkTextLineDisplay *display)
1103 PangoAlignment pango_align = PANGO_ALIGN_LEFT;
1106 display->direction = style->direction;
1108 if (display->direction == GTK_TEXT_DIR_LTR)
1109 display->layout = pango_layout_new (layout->ltr_context);
1111 display->layout = pango_layout_new (layout->rtl_context);
1113 switch (style->justification)
1115 case GTK_JUSTIFY_LEFT:
1116 pango_align = (style->direction == GTK_TEXT_DIR_LTR) ? PANGO_ALIGN_LEFT : PANGO_ALIGN_RIGHT;
1118 case GTK_JUSTIFY_RIGHT:
1119 pango_align = (style->direction == GTK_TEXT_DIR_LTR) ? PANGO_ALIGN_RIGHT : PANGO_ALIGN_LEFT;
1121 case GTK_JUSTIFY_CENTER:
1122 pango_align = PANGO_ALIGN_CENTER;
1124 case GTK_JUSTIFY_FILL:
1125 g_warning ("FIXME we don't support GTK_JUSTIFY_FILL yet");
1128 g_assert_not_reached ();
1132 pango_layout_set_alignment (display->layout, pango_align);
1133 pango_layout_set_spacing (display->layout,
1134 style->pixels_inside_wrap * PANGO_SCALE);
1137 pango_layout_set_tabs (display->layout, style->tabs);
1139 display->top_margin = style->pixels_above_lines;
1140 display->height = style->pixels_above_lines + style->pixels_below_lines;
1141 display->bottom_margin = style->pixels_below_lines;
1142 display->left_margin = style->left_margin;
1143 display->right_margin = style->right_margin;
1145 display->x_offset = display->left_margin;
1147 pango_layout_set_indent (display->layout,
1148 style->indent * PANGO_SCALE);
1150 switch (style->wrap_mode)
1153 layout_width = layout->screen_width - display->left_margin - display->right_margin;
1154 pango_layout_set_width (display->layout, layout_width * PANGO_SCALE);
1155 pango_layout_set_wrap (display->layout, PANGO_WRAP_CHAR);
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_WORD);
1168 display->total_width = MAX (layout->screen_width, layout->width) - display->left_margin - display->right_margin;
1171 static PangoAttribute *
1172 gtk_text_attr_appearance_copy (const PangoAttribute *attr)
1174 const GtkTextAttrAppearance *appearance_attr = (const GtkTextAttrAppearance *)attr;
1176 return gtk_text_attr_appearance_new (&appearance_attr->appearance);
1180 gtk_text_attr_appearance_destroy (PangoAttribute *attr)
1182 GtkTextAppearance *appearance = &((GtkTextAttrAppearance *)attr)->appearance;
1184 if (appearance->bg_stipple)
1185 gdk_drawable_unref (appearance->bg_stipple);
1186 if (appearance->fg_stipple)
1187 gdk_drawable_unref (appearance->fg_stipple);
1193 gtk_text_attr_appearance_compare (const PangoAttribute *attr1,
1194 const PangoAttribute *attr2)
1196 const GtkTextAppearance *appearance1 = &((const GtkTextAttrAppearance *)attr1)->appearance;
1197 const GtkTextAppearance *appearance2 = &((const GtkTextAttrAppearance *)attr2)->appearance;
1199 return (gdk_color_equal (&appearance1->fg_color, &appearance2->fg_color) &&
1200 gdk_color_equal (&appearance1->bg_color, &appearance2->bg_color) &&
1201 appearance1->fg_stipple == appearance2->fg_stipple &&
1202 appearance1->bg_stipple == appearance2->bg_stipple &&
1203 appearance1->underline == appearance2->underline &&
1204 appearance1->strikethrough == appearance2->strikethrough &&
1205 appearance1->draw_bg == appearance2->draw_bg);
1210 * gtk_text_attr_appearance_new:
1213 * Create a new font description attribute. (This attribute
1214 * allows setting family, style, weight, variant, stretch,
1215 * and size simultaneously.)
1219 static PangoAttribute *
1220 gtk_text_attr_appearance_new (const GtkTextAppearance *appearance)
1222 static PangoAttrClass klass = {
1224 gtk_text_attr_appearance_copy,
1225 gtk_text_attr_appearance_destroy,
1226 gtk_text_attr_appearance_compare
1229 GtkTextAttrAppearance *result;
1232 klass.type = gtk_text_attr_appearance_type =
1233 pango_attr_type_register ("GtkTextAttrAppearance");
1235 result = g_new (GtkTextAttrAppearance, 1);
1236 result->attr.klass = &klass;
1238 result->appearance = *appearance;
1240 if (appearance->bg_stipple)
1241 gdk_drawable_ref (appearance->bg_stipple);
1242 if (appearance->fg_stipple)
1243 gdk_drawable_ref (appearance->fg_stipple);
1245 return (PangoAttribute *)result;
1250 add_generic_attrs (GtkTextLayout *layout,
1251 GtkTextAppearance *appearance,
1253 PangoAttrList *attrs,
1258 PangoAttribute *attr;
1260 if (appearance->underline != PANGO_UNDERLINE_NONE)
1262 attr = pango_attr_underline_new (appearance->underline);
1264 attr->start_index = start;
1265 attr->end_index = start + byte_count;
1267 pango_attr_list_insert (attrs, attr);
1270 if (appearance->rise != 0)
1272 attr = pango_attr_rise_new (appearance->rise);
1274 attr->start_index = start;
1275 attr->end_index = start + byte_count;
1277 pango_attr_list_insert (attrs, attr);
1282 attr = gtk_text_attr_appearance_new (appearance);
1284 attr->start_index = start;
1285 attr->end_index = start + byte_count;
1287 ((GtkTextAttrAppearance *)attr)->appearance.is_text = is_text;
1289 pango_attr_list_insert (attrs, attr);
1294 add_text_attrs (GtkTextLayout *layout,
1295 GtkTextAttributes *style,
1297 PangoAttrList *attrs,
1301 PangoAttribute *attr;
1303 attr = pango_attr_font_desc_new (style->font);
1304 attr->start_index = start;
1305 attr->end_index = start + byte_count;
1307 pango_attr_list_insert (attrs, attr);
1309 if (style->font_scale != 1.0)
1311 attr = pango_attr_scale_new (style->font_scale);
1313 attr->start_index = start;
1314 attr->end_index = start + byte_count;
1316 pango_attr_list_insert (attrs, attr);
1321 add_pixbuf_attrs (GtkTextLayout *layout,
1322 GtkTextLineDisplay *display,
1323 GtkTextAttributes *style,
1324 GtkTextLineSegment *seg,
1325 PangoAttrList *attrs,
1328 PangoAttribute *attr;
1329 PangoRectangle logical_rect;
1330 GtkTextPixbuf *pixbuf = &seg->body.pixbuf;
1333 width = gdk_pixbuf_get_width (pixbuf->pixbuf);
1334 height = gdk_pixbuf_get_height (pixbuf->pixbuf);
1337 logical_rect.y = -height * PANGO_SCALE;
1338 logical_rect.width = width * PANGO_SCALE;
1339 logical_rect.height = height * PANGO_SCALE;
1341 attr = pango_attr_shape_new (&logical_rect, &logical_rect);
1342 attr->start_index = start;
1343 attr->end_index = start + seg->byte_count;
1344 pango_attr_list_insert (attrs, attr);
1346 display->shaped_objects =
1347 g_slist_append (display->shaped_objects, pixbuf->pixbuf);
1351 add_child_attrs (GtkTextLayout *layout,
1352 GtkTextLineDisplay *display,
1353 GtkTextAttributes *style,
1354 GtkTextLineSegment *seg,
1355 PangoAttrList *attrs,
1358 PangoAttribute *attr;
1359 PangoRectangle logical_rect;
1360 GtkTextChildAnchor *anchor;
1367 anchor = seg->body.child.obj;
1369 tmp_list = seg->body.child.widgets;
1370 while (tmp_list != NULL)
1372 GtkWidget *child = tmp_list->data;
1374 if (_gtk_anchored_child_get_layout (child) == layout)
1379 gtk_widget_get_child_requisition (child, &req);
1382 height = req.height;
1384 display->shaped_objects =
1385 g_slist_append (display->shaped_objects, child);
1390 tmp_list = g_slist_next (tmp_list);
1393 if (tmp_list == NULL)
1395 /* If tmp_list == NULL then there is no widget at this anchor in
1396 * this display; not an error. We make up an arbitrary size
1397 * to use, just so the programmer can see the blank spot.
1398 * We also put a NULL in the shaped objects list, to keep
1399 * the correspondence between the list and the shaped chars in
1400 * the layout. A bad hack, yes.
1406 display->shaped_objects =
1407 g_slist_append (display->shaped_objects, NULL);
1411 logical_rect.y = -height * PANGO_SCALE;
1412 logical_rect.width = width * PANGO_SCALE;
1413 logical_rect.height = height * PANGO_SCALE;
1415 attr = pango_attr_shape_new (&logical_rect, &logical_rect);
1416 attr->start_index = start;
1417 attr->end_index = start + seg->byte_count;
1418 pango_attr_list_insert (attrs, attr);
1422 add_cursor (GtkTextLayout *layout,
1423 GtkTextLineDisplay *display,
1424 GtkTextLineSegment *seg,
1427 PangoRectangle strong_pos, weak_pos;
1428 GtkTextCursorDisplay *cursor = NULL; /* Quiet GCC */
1429 gboolean add_weak = FALSE;
1430 gboolean add_strong = FALSE;
1432 /* Hide insertion cursor when we have a selection or the layout
1433 * user has hidden the cursor.
1435 if (_gtk_text_btree_mark_is_insert (_gtk_text_buffer_get_btree (layout->buffer),
1436 seg->body.mark.obj) &&
1437 (!layout->cursor_visible ||
1438 gtk_text_buffer_get_selection_bounds (layout->buffer, NULL, NULL)))
1441 pango_layout_get_cursor_pos (display->layout, start, &strong_pos, &weak_pos);
1443 if (layout->cursor_direction == GTK_TEXT_DIR_NONE)
1448 else if (display->direction == layout->cursor_direction)
1455 cursor = g_new (GtkTextCursorDisplay, 1);
1457 cursor->x = PANGO_PIXELS (strong_pos.x);
1458 cursor->y = PANGO_PIXELS (strong_pos.y);
1459 cursor->height = PANGO_PIXELS (strong_pos.height);
1460 cursor->is_strong = TRUE;
1461 cursor->is_weak = FALSE;
1462 display->cursors = g_slist_prepend (display->cursors, cursor);
1467 if (weak_pos.x == strong_pos.x && add_strong)
1468 cursor->is_weak = TRUE;
1471 cursor = g_new (GtkTextCursorDisplay, 1);
1473 cursor->x = PANGO_PIXELS (weak_pos.x);
1474 cursor->y = PANGO_PIXELS (weak_pos.y);
1475 cursor->height = PANGO_PIXELS (weak_pos.height);
1476 cursor->is_strong = FALSE;
1477 cursor->is_weak = TRUE;
1478 display->cursors = g_slist_prepend (display->cursors, cursor);
1484 is_shape (PangoLayoutRun *run)
1486 GSList *tmp_list = run->item->analysis.extra_attrs;
1490 PangoAttribute *attr = tmp_list->data;
1492 if (attr->klass->type == PANGO_ATTR_SHAPE)
1495 tmp_list = tmp_list->next;
1502 allocate_child_widgets (GtkTextLayout *text_layout,
1503 GtkTextLineDisplay *display)
1505 GSList *shaped = display->shaped_objects;
1506 PangoLayout *layout = display->layout;
1507 PangoLayoutIter *iter;
1509 iter = pango_layout_get_iter (layout);
1513 PangoLayoutRun *run = pango_layout_iter_get_run (iter);
1515 if (run && is_shape (run))
1517 GObject *shaped_object = shaped->data;
1518 shaped = shaped->next;
1520 /* shaped_object is NULL for child anchors with no
1521 * widgets stored at them
1523 if (shaped_object && GTK_IS_WIDGET (shaped_object))
1525 PangoRectangle extents;
1527 /* We emit "allocate_child" with the x,y of
1528 * the widget with respect to the top of the line
1529 * and the left side of the buffer
1532 pango_layout_iter_get_run_extents (iter,
1536 g_signal_emit (G_OBJECT (text_layout),
1537 signals[ALLOCATE_CHILD],
1540 PANGO_PIXELS (extents.x) + display->x_offset,
1541 PANGO_PIXELS (extents.y) + display->top_margin);
1545 while (pango_layout_iter_next_run (iter));
1547 pango_layout_iter_free (iter);
1551 convert_color (GdkColor *result,
1552 PangoAttrColor *attr)
1554 result->red = attr->color.red;
1555 result->blue = attr->color.blue;
1556 result->green = attr->color.green;
1559 /* This function is used to convert the preedit string attributes, which are
1560 * standard PangoAttributes, into the custom attributes used by the text
1561 * widget and insert them into a attr list with a given offset.
1564 add_preedit_attrs (GtkTextLayout *layout,
1565 GtkTextAttributes *style,
1566 PangoAttrList *attrs,
1570 PangoAttrIterator *iter = pango_attr_list_get_iterator (layout->preedit_attrs);
1574 GtkTextAppearance appearance = style->appearance;
1575 PangoFontDescription *font_desc = pango_font_description_copy_static (style->font);
1576 PangoAttribute *insert_attr;
1577 GSList *extra_attrs = NULL;
1579 PangoLanguage *language;
1582 pango_attr_iterator_range (iter, &start, &end);
1584 if (end == G_MAXINT)
1585 end = layout->preedit_len;
1587 pango_attr_iterator_get_font (iter, font_desc, &language, &extra_attrs);
1589 tmp_list = extra_attrs;
1592 PangoAttribute *attr = tmp_list->data;
1594 switch (attr->klass->type)
1596 case PANGO_ATTR_FOREGROUND:
1597 convert_color (&appearance.fg_color, (PangoAttrColor *)attr);
1599 case PANGO_ATTR_BACKGROUND:
1600 convert_color (&appearance.bg_color, (PangoAttrColor *)attr);
1601 appearance.draw_bg = TRUE;
1603 case PANGO_ATTR_UNDERLINE:
1604 appearance.underline = ((PangoAttrInt *)attr)->value;
1606 case PANGO_ATTR_STRIKETHROUGH:
1607 appearance.strikethrough = ((PangoAttrInt *)attr)->value;
1609 case PANGO_ATTR_RISE:
1610 appearance.rise = ((PangoAttrInt *)attr)->value;
1616 pango_attribute_destroy (attr);
1617 tmp_list = tmp_list->next;
1620 g_slist_free (extra_attrs);
1622 insert_attr = pango_attr_font_desc_new (font_desc);
1623 insert_attr->start_index = start + offset;
1624 insert_attr->end_index = end + offset;
1626 pango_attr_list_insert (attrs, insert_attr);
1630 insert_attr = pango_attr_language_new (language);
1631 insert_attr->start_index = start + offset;
1632 insert_attr->end_index = end + offset;
1634 pango_attr_list_insert (attrs, insert_attr);
1637 add_generic_attrs (layout, &appearance, end - start,
1638 attrs, start + offset,
1641 pango_font_description_free (font_desc);
1643 while (pango_attr_iterator_next (iter));
1645 pango_attr_iterator_destroy (iter);
1648 GtkTextLineDisplay *
1649 gtk_text_layout_get_line_display (GtkTextLayout *layout,
1653 GtkTextLineDisplay *display;
1654 GtkTextLineSegment *seg;
1656 GtkTextAttributes *style;
1658 PangoAttrList *attrs;
1659 gint text_allocated, layout_byte_offset, buffer_byte_offset;
1660 PangoRectangle extents;
1661 gboolean para_values_set = FALSE;
1662 GSList *cursor_byte_offsets = NULL;
1663 GSList *cursor_segs = NULL;
1664 GSList *tmp_list1, *tmp_list2;
1665 gboolean saw_widget = FALSE;
1667 g_return_val_if_fail (line != NULL, NULL);
1669 if (layout->one_display_cache)
1671 if (line == layout->one_display_cache->line &&
1672 (size_only || !layout->one_display_cache->size_only))
1673 return layout->one_display_cache;
1676 GtkTextLineDisplay *tmp_display = layout->one_display_cache;
1677 layout->one_display_cache = NULL;
1678 gtk_text_layout_free_line_display (layout, tmp_display);
1682 display = g_new0 (GtkTextLineDisplay, 1);
1684 display->size_only = size_only;
1685 display->line = line;
1686 display->insert_index = -1;
1688 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
1691 /* Special-case optimization for completely
1692 * invisible lines; makes it faster to deal
1693 * with sequences of invisible lines.
1695 if (totally_invisible_line (layout, line, &iter))
1698 /* Allocate space for flat text for buffer
1700 text_allocated = _gtk_text_line_byte_count (line);
1701 text = g_malloc (text_allocated);
1703 attrs = pango_attr_list_new ();
1705 /* Iterate over segments, creating display chunks for them. */
1706 layout_byte_offset = 0; /* current length of layout text (includes preedit, does not include invisible text) */
1707 buffer_byte_offset = 0; /* position in the buffer line */
1708 seg = _gtk_text_iter_get_any_segment (&iter);
1711 /* Displayable segments */
1712 if (seg->type == >k_text_char_type ||
1713 seg->type == >k_text_pixbuf_type ||
1714 seg->type == >k_text_child_type)
1716 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
1718 buffer_byte_offset);
1719 style = get_style (layout, &iter);
1721 /* We have to delay setting the paragraph values until we
1722 * hit the first pixbuf or text segment because toggles at
1723 * the beginning of the paragraph should affect the
1724 * paragraph-global values
1726 if (!para_values_set)
1728 set_para_values (layout, style, display);
1729 para_values_set = TRUE;
1732 /* First see if the chunk is invisible, and ignore it if so. Tk
1733 * looked at tabs, wrap mode, etc. before doing this, but
1734 * that made no sense to me, so I am just skipping the
1737 if (!style->invisible)
1739 if (seg->type == >k_text_char_type)
1741 /* We don't want to split segments because of marks,
1742 * so we scan forward for more segments only
1743 * separated from us by marks. In theory, we should
1744 * also merge segments with identical styles, even
1745 * if there are toggles in-between
1749 GtkTextLineSegment *prev_seg = NULL;
1753 if (seg->type == >k_text_char_type)
1755 memcpy (text + layout_byte_offset, seg->body.chars, seg->byte_count);
1756 layout_byte_offset += seg->byte_count;
1757 buffer_byte_offset += seg->byte_count;
1758 bytes += seg->byte_count;
1760 else if (seg->type == >k_text_right_mark_type ||
1761 seg->type == >k_text_left_mark_type)
1763 /* If we have preedit string, break out of this loop - we'll almost
1764 * certainly have different attributes on the preedit string
1767 if (layout->preedit_len > 0 &&
1768 _gtk_text_btree_mark_is_insert (_gtk_text_buffer_get_btree (layout->buffer),
1769 seg->body.mark.obj))
1772 if (seg->body.mark.visible)
1774 cursor_byte_offsets = g_slist_prepend (cursor_byte_offsets, GINT_TO_POINTER (layout_byte_offset));
1775 cursor_segs = g_slist_prepend (cursor_segs, seg);
1785 seg = prev_seg; /* Back up one */
1786 add_generic_attrs (layout, &style->appearance,
1788 attrs, layout_byte_offset - bytes,
1790 add_text_attrs (layout, style, bytes, attrs,
1791 layout_byte_offset - bytes, size_only);
1793 else if (seg->type == >k_text_pixbuf_type)
1795 add_generic_attrs (layout,
1798 attrs, layout_byte_offset,
1800 add_pixbuf_attrs (layout, display, style,
1801 seg, attrs, layout_byte_offset);
1802 memcpy (text + layout_byte_offset, gtk_text_unknown_char_utf8,
1804 layout_byte_offset += seg->byte_count;
1805 buffer_byte_offset += seg->byte_count;
1807 else if (seg->type == >k_text_child_type)
1811 add_generic_attrs (layout, &style->appearance,
1813 attrs, layout_byte_offset,
1815 add_child_attrs (layout, display, style,
1816 seg, attrs, layout_byte_offset);
1817 memcpy (text + layout_byte_offset, gtk_text_unknown_char_utf8,
1819 layout_byte_offset += seg->byte_count;
1820 buffer_byte_offset += seg->byte_count;
1824 /* We don't know this segment type */
1825 g_assert_not_reached ();
1828 } /* if (segment was visible) */
1831 /* Invisible segment */
1832 buffer_byte_offset += seg->byte_count;
1835 release_style (layout, style);
1839 else if (seg->type == >k_text_toggle_on_type ||
1840 seg->type == >k_text_toggle_off_type)
1842 /* Style may have changed, drop our
1843 current cached style */
1844 invalidate_cached_style (layout);
1848 else if (seg->type == >k_text_right_mark_type ||
1849 seg->type == >k_text_left_mark_type)
1851 gint cursor_offset = 0;
1853 /* At the insertion point, add the preedit string, if any */
1855 if (_gtk_text_btree_mark_is_insert (_gtk_text_buffer_get_btree (layout->buffer),
1856 seg->body.mark.obj))
1858 display->insert_index = layout_byte_offset;
1860 if (layout->preedit_len > 0)
1862 text_allocated += layout->preedit_len;
1863 text = g_realloc (text, text_allocated);
1865 style = get_style (layout, &iter);
1866 add_preedit_attrs (layout, style, attrs, layout_byte_offset, size_only);
1867 release_style (layout, style);
1869 memcpy (text + layout_byte_offset, layout->preedit_string, layout->preedit_len);
1870 layout_byte_offset += layout->preedit_len;
1871 /* DO NOT increment the buffer byte offset for preedit */
1873 cursor_offset = layout->preedit_cursor - layout->preedit_len;
1878 /* Display visible marks */
1880 if (seg->body.mark.visible)
1882 cursor_byte_offsets = g_slist_prepend (cursor_byte_offsets,
1883 GINT_TO_POINTER (layout_byte_offset + cursor_offset));
1884 cursor_segs = g_slist_prepend (cursor_segs, seg);
1889 g_error ("Unknown segment type: %s", seg->type->name);
1894 if (!para_values_set)
1896 style = get_style (layout, &iter);
1897 set_para_values (layout, style, display);
1898 release_style (layout, style);
1901 /* Pango doesn't want the trailing paragraph delimiters */
1904 /* Only one character has type G_UNICODE_PARAGRAPH_SEPARATOR in
1905 * Unicode 3.0; update this if that changes.
1907 #define PARAGRAPH_SEPARATOR 0x2029
1910 if (layout_byte_offset > 0)
1912 const char *prev = g_utf8_prev_char (text + layout_byte_offset);
1913 ch = g_utf8_get_char (prev);
1914 if (ch == PARAGRAPH_SEPARATOR || ch == '\r' || ch == '\n')
1915 layout_byte_offset = prev - text; /* chop off */
1917 if (ch == '\n' && layout_byte_offset > 0)
1919 /* Possibly chop a CR as well */
1920 prev = g_utf8_prev_char (text + layout_byte_offset);
1922 --layout_byte_offset;
1927 pango_layout_set_text (display->layout, text, layout_byte_offset);
1928 pango_layout_set_attributes (display->layout, attrs);
1930 tmp_list1 = cursor_byte_offsets;
1931 tmp_list2 = cursor_segs;
1934 add_cursor (layout, display, tmp_list2->data,
1935 GPOINTER_TO_INT (tmp_list1->data));
1936 tmp_list1 = tmp_list1->next;
1937 tmp_list2 = tmp_list2->next;
1939 g_slist_free (cursor_byte_offsets);
1940 g_slist_free (cursor_segs);
1942 pango_layout_get_extents (display->layout, NULL, &extents);
1944 display->width = PANGO_PIXELS (extents.width) + display->left_margin + display->right_margin;
1945 display->height += PANGO_PIXELS (extents.height);
1947 /* Free this if we aren't in a loop */
1948 if (layout->wrap_loop_count == 0)
1949 invalidate_cached_style (layout);
1952 pango_attr_list_unref (attrs);
1954 layout->one_display_cache = display;
1957 allocate_child_widgets (layout, display);
1963 gtk_text_layout_free_line_display (GtkTextLayout *layout,
1964 GtkTextLineDisplay *display)
1966 if (display != layout->one_display_cache)
1968 if (display->layout)
1969 g_object_unref (G_OBJECT (display->layout));
1971 if (display->cursors)
1973 g_slist_foreach (display->cursors, (GFunc)g_free, NULL);
1974 g_slist_free (display->cursors);
1976 g_slist_free (display->shaped_objects);
1982 /* Functions to convert iter <=> index for the line of a GtkTextLineDisplay
1983 * taking into account the preedit string and invisible text if necessary.
1986 line_display_iter_to_index (GtkTextLayout *layout,
1987 GtkTextLineDisplay *display,
1988 const GtkTextIter *iter)
1992 g_return_val_if_fail (_gtk_text_iter_get_text_line (iter) == display->line, 0);
1994 index = gtk_text_iter_get_visible_line_index (iter);
1996 if (index >= display->insert_index)
1997 index += layout->preedit_len;
2003 line_display_index_to_iter (GtkTextLayout *layout,
2004 GtkTextLineDisplay *display,
2009 g_return_if_fail (!_gtk_text_line_is_last (display->line,
2010 _gtk_text_buffer_get_btree (layout->buffer)));
2012 if (index >= display->insert_index + layout->preedit_len)
2013 index -= layout->preedit_len;
2014 else if (index > display->insert_index)
2016 index = display->insert_index;
2020 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
2021 iter, display->line, 0);
2023 gtk_text_iter_set_visible_line_index (iter, index);
2025 if (_gtk_text_iter_get_text_line (iter) != display->line)
2027 /* Clamp to end of line - really this clamping should have been done
2028 * before here, maybe in Pango, this is a broken band-aid I think
2030 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
2031 iter, display->line, 0);
2033 if (!gtk_text_iter_ends_line (iter))
2034 gtk_text_iter_forward_to_line_end (iter);
2037 gtk_text_iter_forward_chars (iter, trailing);
2041 get_line_at_y (GtkTextLayout *layout,
2048 if (y > layout->height)
2051 *line = _gtk_text_btree_find_line_by_y (_gtk_text_buffer_get_btree (layout->buffer),
2052 layout, y, line_top);
2055 *line = _gtk_text_btree_get_end_iter_line (_gtk_text_buffer_get_btree (layout->buffer));
2059 _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
2065 * gtk_text_layout_get_line_at_y:
2066 * @layout: a #GtkLayout
2067 * @target_iter: the iterator in which the result is stored
2068 * @y: the y positition
2069 * @line_top: location to store the y coordinate of the
2070 * top of the line. (Can by %NULL.)
2072 * Get the iter at the beginning of the line which is displayed
2076 gtk_text_layout_get_line_at_y (GtkTextLayout *layout,
2077 GtkTextIter *target_iter,
2083 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
2084 g_return_if_fail (target_iter != NULL);
2086 get_line_at_y (layout, y, &line, line_top);
2087 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
2088 target_iter, line, 0);
2092 gtk_text_layout_get_iter_at_pixel (GtkTextLayout *layout,
2093 GtkTextIter *target_iter,
2097 gint byte_index, trailing;
2099 GtkTextLineDisplay *display;
2101 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
2102 g_return_if_fail (target_iter != NULL);
2104 get_line_at_y (layout, y, &line, &line_top);
2106 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2108 x -= display->x_offset;
2109 y -= line_top + display->top_margin;
2111 /* If we are below the layout, position the cursor at the last character
2114 if (y > display->height - display->top_margin - display->bottom_margin)
2116 byte_index = _gtk_text_line_byte_count (line);
2121 /* Ignore the "outside" return value from pango. Pango is doing
2122 * the right thing even if we are outside the layout in the
2125 pango_layout_xy_to_index (display->layout, x * PANGO_SCALE, y * PANGO_SCALE,
2126 &byte_index, &trailing);
2129 line_display_index_to_iter (layout, display, target_iter, byte_index, trailing);
2131 gtk_text_layout_free_line_display (layout, display);
2135 * gtk_text_layout_get_cursor_locations
2136 * @layout: a #GtkTextLayout
2137 * @iter: a #GtkTextIter
2138 * @strong_pos: location to store the strong cursor position (may be %NULL)
2139 * @weak_pos: location to store the weak cursor position (may be %NULL)
2141 * Given an iterator within a text laout, determine the positions that of the
2142 * strong and weak cursors if the insertion point is at that
2143 * iterator. The position of each cursor is stored as a zero-width
2144 * rectangle. The strong cursor location is the location where
2145 * characters of the directionality equal to the base direction of the
2146 * paragraph are inserted. The weak cursor location is the location
2147 * where characters of the directionality opposite to the base
2148 * direction of the paragraph are inserted.
2151 gtk_text_layout_get_cursor_locations (GtkTextLayout *layout,
2153 GdkRectangle *strong_pos,
2154 GdkRectangle *weak_pos)
2157 GtkTextLineDisplay *display;
2161 PangoRectangle pango_strong_pos;
2162 PangoRectangle pango_weak_pos;
2164 g_return_if_fail (layout != NULL);
2165 g_return_if_fail (iter != NULL);
2167 line = _gtk_text_iter_get_text_line (iter);
2168 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2169 index = line_display_iter_to_index (layout, display, iter);
2171 line_top = _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
2174 pango_layout_get_cursor_pos (display->layout, index,
2175 strong_pos ? &pango_strong_pos : NULL,
2176 weak_pos ? &pango_weak_pos : NULL);
2180 strong_pos->x = display->x_offset + pango_strong_pos.x / PANGO_SCALE;
2181 strong_pos->y = line_top + display->top_margin + pango_strong_pos.y / PANGO_SCALE;
2182 strong_pos->width = 0;
2183 strong_pos->height = pango_strong_pos.height / PANGO_SCALE;
2188 weak_pos->x = display->x_offset + pango_weak_pos.x / PANGO_SCALE;
2189 weak_pos->y = line_top + display->top_margin + pango_weak_pos.y / PANGO_SCALE;
2190 weak_pos->width = 0;
2191 weak_pos->height = pango_weak_pos.height / PANGO_SCALE;
2194 gtk_text_layout_free_line_display (layout, display);
2198 * gtk_text_layout_get_line_yrange:
2199 * @layout: a #GtkTextLayout
2200 * @iter: a #GtkTextIter
2201 * @y: location to store the top of the paragraph in pixels,
2203 * @height location to store the height of the paragraph in pixels,
2206 * Find the range of y coordinates for the paragraph containing
2210 gtk_text_layout_get_line_yrange (GtkTextLayout *layout,
2211 const GtkTextIter *iter,
2217 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
2218 g_return_if_fail (_gtk_text_iter_get_btree (iter) == _gtk_text_buffer_get_btree (layout->buffer));
2220 line = _gtk_text_iter_get_text_line (iter);
2223 *y = _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
2227 GtkTextLineData *line_data = _gtk_text_line_get_data (line, layout);
2229 *height = line_data->height;
2236 * _gtk_text_layout_get_line_xrange:
2237 * @layout: a #GtkTextLayout
2238 * @iter: a #GtkTextIter
2239 * @x: location to store the top of the paragraph in pixels,
2241 * @width location to store the height of the paragraph in pixels,
2244 * Find the range of X coordinates for the paragraph containing
2245 * the given iter. Private for 2.0 due to API freeze, could
2246 * be made public for 2.2.
2249 _gtk_text_layout_get_line_xrange (GtkTextLayout *layout,
2250 const GtkTextIter *iter,
2256 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
2257 g_return_if_fail (_gtk_text_iter_get_btree (iter) == _gtk_text_buffer_get_btree (layout->buffer));
2259 line = _gtk_text_iter_get_text_line (iter);
2262 *x = 0; /* FIXME This is wrong; should represent the first available cursor position */
2266 GtkTextLineData *line_data = _gtk_text_line_get_data (line, layout);
2268 *width = line_data->width;
2275 gtk_text_layout_get_iter_location (GtkTextLayout *layout,
2276 const GtkTextIter *iter,
2279 PangoRectangle pango_rect;
2282 GtkTextLineDisplay *display;
2286 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
2287 g_return_if_fail (_gtk_text_iter_get_btree (iter) == _gtk_text_buffer_get_btree (layout->buffer));
2288 g_return_if_fail (rect != NULL);
2290 tree = _gtk_text_iter_get_btree (iter);
2291 line = _gtk_text_iter_get_text_line (iter);
2293 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2295 rect->y = _gtk_text_btree_find_line_top (tree, line, layout);
2297 x_offset = display->x_offset * PANGO_SCALE;
2299 byte_index = gtk_text_iter_get_line_index (iter);
2301 pango_layout_index_to_pos (display->layout, byte_index, &pango_rect);
2303 rect->x = PANGO_PIXELS (x_offset + pango_rect.x);
2304 rect->y += PANGO_PIXELS (pango_rect.y) + display->top_margin;
2305 rect->width = PANGO_PIXELS (pango_rect.width);
2306 rect->height = PANGO_PIXELS (pango_rect.height);
2308 gtk_text_layout_free_line_display (layout, display);
2313 /* Find the iter for the logical beginning of the first display line whose
2314 * top y is >= y. If none exists, move the iter to the logical beginning
2315 * of the last line in the buffer.
2318 find_display_line_below (GtkTextLayout *layout,
2322 GtkTextLine *line, *next;
2323 GtkTextLine *found_line = NULL;
2325 gint found_byte = 0;
2327 line = _gtk_text_btree_find_line_by_y (_gtk_text_buffer_get_btree (layout->buffer),
2328 layout, y, &line_top);
2332 _gtk_text_btree_get_end_iter_line (_gtk_text_buffer_get_btree (layout->buffer));
2335 _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
2339 while (line && !found_line)
2341 GtkTextLineDisplay *display = gtk_text_layout_get_line_display (layout, line, FALSE);
2342 PangoLayoutIter *layout_iter;
2344 layout_iter = pango_layout_get_iter (display->layout);
2346 line_top += display->top_margin;
2350 gint first_y, last_y;
2351 PangoLayoutLine *layout_line = pango_layout_iter_get_line (layout_iter);
2353 found_byte = layout_line->start_index;
2361 pango_layout_iter_get_line_yrange (layout_iter, &first_y, &last_y);
2362 line_top += (last_y - first_y) / PANGO_SCALE;
2364 while (pango_layout_iter_next_line (layout_iter));
2366 pango_layout_iter_free (layout_iter);
2368 line_top += display->bottom_margin;
2369 gtk_text_layout_free_line_display (layout, display);
2371 next = _gtk_text_line_next_excluding_last (line);
2378 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
2379 iter, found_line, found_byte);
2382 /* Find the iter for the logical beginning of the last display line whose
2383 * top y is >= y. If none exists, move the iter to the logical beginning
2384 * of the first line in the buffer.
2387 find_display_line_above (GtkTextLayout *layout,
2392 GtkTextLine *found_line = NULL;
2394 gint found_byte = 0;
2396 line = _gtk_text_btree_find_line_by_y (_gtk_text_buffer_get_btree (layout->buffer), layout, y, &line_top);
2399 line = _gtk_text_btree_get_end_iter_line (_gtk_text_buffer_get_btree (layout->buffer));
2401 line_top = _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer), line, layout);
2404 while (line && !found_line)
2406 GtkTextLineDisplay *display = gtk_text_layout_get_line_display (layout, line, FALSE);
2407 PangoRectangle logical_rect;
2408 PangoLayoutIter *layout_iter;
2411 layout_iter = pango_layout_get_iter (display->layout);
2413 line_top -= display->top_margin + display->bottom_margin;
2414 pango_layout_iter_get_layout_extents (layout_iter, NULL, &logical_rect);
2415 line_top -= logical_rect.height / PANGO_SCALE;
2417 tmp_top = line_top + display->top_margin;
2421 gint first_y, last_y;
2422 PangoLayoutLine *layout_line = pango_layout_iter_get_line (layout_iter);
2424 found_byte = layout_line->start_index;
2426 pango_layout_iter_get_line_yrange (layout_iter, &first_y, &last_y);
2428 tmp_top -= (last_y - first_y) / PANGO_SCALE;
2436 while (pango_layout_iter_next_line (layout_iter));
2438 pango_layout_iter_free (layout_iter);
2440 gtk_text_layout_free_line_display (layout, display);
2442 line = _gtk_text_line_previous (line);
2448 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
2449 iter, found_line, found_byte);
2451 gtk_text_buffer_get_iter_at_offset (layout->buffer, iter, 0);
2455 * gtk_text_layout_clamp_iter_to_vrange:
2456 * @layout: a #GtkTextLayout
2457 * @iter: a #GtkTextIter
2458 * @top: the top of the range
2459 * @bottom: the bottom the range
2461 * If the iterator is not fully in the range @top <= y < @bottom,
2462 * then, if possible, move it the minimum distance so that the
2463 * iterator in this range.
2465 * Returns: %TRUE if the iterator was moved, otherwise %FALSE.
2468 gtk_text_layout_clamp_iter_to_vrange (GtkTextLayout *layout,
2473 GdkRectangle iter_rect;
2475 gtk_text_layout_get_iter_location (layout, iter, &iter_rect);
2477 /* If the iter is at least partially above the range, put the iter
2478 * at the first fully visible line after the range.
2480 if (iter_rect.y < top)
2482 find_display_line_below (layout, iter, top);
2486 /* Otherwise, if the iter is at least partially below the screen, put the
2487 * iter on the last logical position of the last completely visible
2490 else if (iter_rect.y + iter_rect.height > bottom)
2492 find_display_line_above (layout, iter, bottom);
2501 * gtk_text_layout_move_iter_to_previous_line:
2502 * @layout: a #GtkLayout
2503 * @iter: a #GtkTextIter
2505 * Move the iterator to the beginning of the previous line. The lines
2506 * of a wrapped paragraph are treated as distinct for this operation.
2509 gtk_text_layout_move_iter_to_previous_line (GtkTextLayout *layout,
2513 GtkTextLineDisplay *display;
2516 PangoLayoutLine *layout_line;
2518 gboolean update_byte = FALSE;
2520 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), FALSE);
2521 g_return_val_if_fail (iter != NULL, FALSE);
2526 line = _gtk_text_iter_get_text_line (iter);
2527 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2528 line_byte = line_display_iter_to_index (layout, display, iter);
2530 /* If display->height == 0 then the line is invisible, so don't
2533 while (display->height == 0)
2535 GtkTextLine *prev_line;
2537 prev_line = _gtk_text_line_previous (line);
2539 if (prev_line == NULL)
2541 line_display_index_to_iter (layout, display, iter, 0, 0);
2545 gtk_text_layout_free_line_display (layout, display);
2548 display = gtk_text_layout_get_line_display (layout, prev_line, FALSE);
2552 tmp_list = pango_layout_get_lines (display->layout);
2553 layout_line = tmp_list->data;
2557 line_byte = layout_line->start_index + layout_line->length;
2560 if (line_byte < layout_line->length || !tmp_list->next) /* first line of paragraph */
2562 GtkTextLine *prev_line;
2564 prev_line = _gtk_text_line_previous (line);
2567 gtk_text_layout_free_line_display (layout, display);
2569 display = gtk_text_layout_get_line_display (layout, prev_line, FALSE);
2571 if (display->height > 0)
2573 tmp_list = g_slist_last (pango_layout_get_lines (display->layout));
2574 layout_line = tmp_list->data;
2576 line_display_index_to_iter (layout, display, iter,
2577 layout_line->start_index + layout_line->length, 0);
2581 prev_line = _gtk_text_line_previous (prev_line);
2584 if (prev_line == NULL)
2585 line_display_index_to_iter (layout, display, iter, 0, 0);
2589 gint prev_offset = layout_line->start_index;
2591 tmp_list = tmp_list->next;
2594 layout_line = tmp_list->data;
2596 if (line_byte < layout_line->start_index + layout_line->length ||
2599 line_display_index_to_iter (layout, display, iter, prev_offset, 0);
2603 prev_offset = layout_line->start_index;
2604 tmp_list = tmp_list->next;
2610 gtk_text_layout_free_line_display (layout, display);
2613 !gtk_text_iter_equal (iter, &orig) &&
2614 !gtk_text_iter_is_end (iter);
2618 * gtk_text_layout_move_iter_to_next_line:
2619 * @layout: a #GtkLayout
2620 * @iter: a #GtkTextIter
2622 * Move the iterator to the beginning of the next line. The
2623 * lines of a wrapped paragraph are treated as distinct for
2627 gtk_text_layout_move_iter_to_next_line (GtkTextLayout *layout,
2631 GtkTextLineDisplay *display;
2634 gboolean found = FALSE;
2635 gboolean found_after = FALSE;
2636 gboolean first = TRUE;
2638 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), FALSE);
2639 g_return_val_if_fail (iter != NULL, FALSE);
2643 line = _gtk_text_iter_get_text_line (iter);
2645 while (line && !found_after)
2649 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2651 if (display->height == 0)
2656 line_byte = line_display_iter_to_index (layout, display, iter);
2662 tmp_list = pango_layout_get_lines (display->layout);
2663 while (tmp_list && !found_after)
2665 PangoLayoutLine *layout_line = tmp_list->data;
2669 line_display_index_to_iter (layout, display, iter,
2670 layout_line->start_index, 0);
2673 else if (line_byte < layout_line->start_index + layout_line->length || !tmp_list->next)
2676 tmp_list = tmp_list->next;
2681 gtk_text_layout_free_line_display (layout, display);
2683 line = _gtk_text_line_next_excluding_last (line);
2687 !gtk_text_iter_equal (iter, &orig) &&
2688 !gtk_text_iter_is_end (iter);
2692 * gtk_text_layout_move_iter_to_line_end:
2693 * @layout: a #GtkTextLayout
2694 * @direction: if negative, move to beginning of line, otherwise
2695 move to end of line.
2697 * Move to the beginning or end of a display line.
2700 gtk_text_layout_move_iter_to_line_end (GtkTextLayout *layout,
2705 GtkTextLineDisplay *display;
2710 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), FALSE);
2711 g_return_val_if_fail (iter != NULL, FALSE);
2715 line = _gtk_text_iter_get_text_line (iter);
2716 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2717 line_byte = line_display_iter_to_index (layout, display, iter);
2719 tmp_list = pango_layout_get_lines (display->layout);
2722 PangoLayoutLine *layout_line = tmp_list->data;
2724 if (line_byte < layout_line->start_index + layout_line->length || !tmp_list->next)
2726 line_display_index_to_iter (layout, display, iter,
2727 direction < 0 ? layout_line->start_index : layout_line->start_index + layout_line->length,
2730 /* FIXME: As a bad hack, we move back one position when we
2731 * are inside a paragraph to avoid going to next line on a
2732 * forced break not at whitespace. Real fix is to keep track
2733 * of whether marks are at leading or trailing edge? */
2734 if (direction > 0 && layout_line->length > 0 && !gtk_text_iter_ends_line (iter))
2735 gtk_text_iter_backward_char (iter);
2740 tmp_list = tmp_list->next;
2743 gtk_text_layout_free_line_display (layout, display);
2746 !gtk_text_iter_equal (iter, &orig) &&
2747 !gtk_text_iter_is_end (iter);
2752 * gtk_text_layout_iter_starts_line:
2753 * @layout: a #GtkTextLayout
2754 * @iter: iterator to test
2756 * Tests whether an iterator is at the start of a display line.
2759 gtk_text_layout_iter_starts_line (GtkTextLayout *layout,
2760 const GtkTextIter *iter)
2763 GtkTextLineDisplay *display;
2767 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), FALSE);
2768 g_return_val_if_fail (iter != NULL, FALSE);
2770 line = _gtk_text_iter_get_text_line (iter);
2771 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2772 line_byte = line_display_iter_to_index (layout, display, iter);
2774 tmp_list = pango_layout_get_lines (display->layout);
2777 PangoLayoutLine *layout_line = tmp_list->data;
2779 if (line_byte < layout_line->start_index + layout_line->length ||
2782 /* We're located on this line or the para delimiters before
2785 gtk_text_layout_free_line_display (layout, display);
2787 if (line_byte == layout_line->start_index)
2793 tmp_list = tmp_list->next;
2796 g_assert_not_reached ();
2801 gtk_text_layout_get_iter_at_line (GtkTextLayout *layout,
2806 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
2807 iter, line, byte_offset);
2812 * gtk_text_layout_move_iter_to_x:
2813 * @layout: a #GtkTextLayout
2814 * @iter: a #GtkTextIter
2817 * Keeping the iterator on the same line of the layout, move it to the
2818 * specified X coordinate. The lines of a wrapped paragraph are
2819 * treated as distinct for this operation.
2822 gtk_text_layout_move_iter_to_x (GtkTextLayout *layout,
2827 GtkTextLineDisplay *display;
2829 PangoLayoutIter *layout_iter;
2831 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
2832 g_return_if_fail (iter != NULL);
2834 line = _gtk_text_iter_get_text_line (iter);
2836 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2837 line_byte = line_display_iter_to_index (layout, display, iter);
2839 layout_iter = pango_layout_get_iter (display->layout);
2843 PangoLayoutLine *layout_line = pango_layout_iter_get_line (layout_iter);
2845 if (line_byte < layout_line->start_index + layout_line->length ||
2846 pango_layout_iter_at_last_line (layout_iter))
2848 PangoRectangle logical_rect;
2849 gint byte_index, trailing;
2850 gint x_offset = display->x_offset * PANGO_SCALE;
2852 pango_layout_iter_get_line_extents (layout_iter, NULL, &logical_rect);
2854 pango_layout_line_x_to_index (layout_line,
2855 x * PANGO_SCALE - x_offset - logical_rect.x,
2856 &byte_index, &trailing);
2858 line_display_index_to_iter (layout, display, iter, byte_index, trailing);
2863 while (pango_layout_iter_next_line (layout_iter));
2865 pango_layout_iter_free (layout_iter);
2867 gtk_text_layout_free_line_display (layout, display);
2871 * gtk_text_layout_move_iter_visually:
2872 * @layout: a #GtkTextLayout
2873 * @iter: a #GtkTextIter
2874 * @count: number of characters to move (negative moves left, positive moves right)
2876 * Move the iterator a given number of characters visually, treating
2877 * it as the strong cursor position. If @count is positive, then the
2878 * new strong cursor position will be @count positions to the right of
2879 * the old cursor position. If @count is negative then the new strong
2880 * cursor position will be @count positions to the left of the old
2883 * In the presence of bidirection text, the correspondence
2884 * between logical and visual order will depend on the direction
2885 * of the current run, and there may be jumps when the cursor
2886 * is moved off of the end of a run.
2890 gtk_text_layout_move_iter_visually (GtkTextLayout *layout,
2894 GtkTextLineDisplay *display = NULL;
2897 g_return_val_if_fail (layout != NULL, FALSE);
2898 g_return_val_if_fail (iter != NULL, FALSE);
2904 GtkTextLine *line = _gtk_text_iter_get_text_line (iter);
2906 gint extra_back = 0;
2909 int byte_count = _gtk_text_line_byte_count (line);
2915 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2917 if (layout->cursor_direction == GTK_TEXT_DIR_NONE)
2920 strong = display->direction == layout->cursor_direction;
2922 line_byte = line_display_iter_to_index (layout, display, iter);
2926 pango_layout_move_cursor_visually (display->layout, strong, line_byte, 0, 1, &new_index, &new_trailing);
2931 pango_layout_move_cursor_visually (display->layout, strong, line_byte, 0, -1, &new_index, &new_trailing);
2935 /* We need to handle the preedit string specially. Well, we don't really need to
2936 * handle it specially, since hopefully calling gtk_im_context_reset() will
2937 * remove the preedit string; but if we start off in front of the preedit
2938 * string (logically) and end up in or on the back edge of the preedit string,
2939 * we should move the iter one place farther.
2941 if (layout->preedit_len > 0 && display->insert_index >= 0)
2943 if (line_byte == display->insert_index + layout->preedit_len &&
2944 new_index < display->insert_index + layout->preedit_len)
2946 line_byte = display->insert_index;
2951 if (new_index < 0 || (new_index == 0 && extra_back))
2953 line = _gtk_text_line_previous (line);
2958 gtk_text_layout_free_line_display (layout, display);
2959 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2960 new_index = _gtk_text_line_byte_count (line);
2962 else if (new_index > byte_count)
2964 line = _gtk_text_line_next_excluding_last (line);
2968 gtk_text_layout_free_line_display (layout, display);
2969 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2973 line_display_index_to_iter (layout, display, iter, new_index, new_trailing);
2975 gtk_text_iter_backward_char (iter);
2978 gtk_text_layout_free_line_display (layout, display);
2983 !gtk_text_iter_equal (iter, &orig) &&
2984 !gtk_text_iter_is_end (iter);
2988 gtk_text_layout_spew (GtkTextLayout *layout)
2991 GtkTextDisplayLine *iter;
2993 guint paragraphs = 0;
2994 GtkTextLine *last_line = NULL;
2996 iter = layout->line_list;
2997 while (iter != NULL)
2999 if (iter->line != last_line)
3001 printf ("%5u paragraph (%p)\n", paragraphs, iter->line);
3003 last_line = iter->line;
3006 printf (" %5u y: %d len: %d start: %d bytes: %d\n",
3007 wrapped, iter->y, iter->length, iter->byte_offset,
3014 printf ("Layout %s recompute\n",
3015 layout->need_recompute ? "needs" : "doesn't need");
3017 printf ("Layout pars: %u lines: %u size: %d x %d Screen width: %d\n",
3018 paragraphs, wrapped, layout->width,
3019 layout->height, layout->screen_width);