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 #include "gtksignal.h"
80 #include "gtktextlayout.h"
81 #include "gtktextbtree.h"
82 #include "gtktextiterprivate.h"
87 static GtkTextLineData *gtk_text_line_data_new (GtkTextLayout *layout,
90 static GtkTextLineData *gtk_text_layout_real_wrap (GtkTextLayout *layout,
93 GtkTextLineData *line_data);
95 static void gtk_text_layout_invalidated (GtkTextLayout *layout);
97 static void gtk_text_layout_real_invalidate (GtkTextLayout *layout,
98 const GtkTextIter *start,
99 const GtkTextIter *end);
100 static void gtk_text_layout_invalidate_cache (GtkTextLayout *layout,
102 static void gtk_text_layout_invalidate_cursor_line (GtkTextLayout *layout);
103 static void gtk_text_layout_real_free_line_data (GtkTextLayout *layout,
105 GtkTextLineData *line_data);
107 static void gtk_text_layout_invalidate_all (GtkTextLayout *layout);
109 static PangoAttribute *gtk_text_attr_appearance_new (const GtkTextAppearance *appearance);
123 static void gtk_text_layout_init (GtkTextLayout *text_layout);
124 static void gtk_text_layout_class_init (GtkTextLayoutClass *klass);
125 static void gtk_text_layout_finalize (GObject *object);
128 static GtkObjectClass *parent_class = NULL;
129 static guint signals[LAST_SIGNAL] = { 0 };
131 PangoAttrType gtk_text_attr_appearance_type = 0;
134 gtk_text_layout_get_type (void)
136 static GType our_type = 0;
140 static const GTypeInfo our_info =
142 sizeof (GtkTextLayoutClass),
143 (GBaseInitFunc) NULL,
144 (GBaseFinalizeFunc) NULL,
145 (GClassInitFunc) gtk_text_layout_class_init,
146 NULL, /* class_finalize */
147 NULL, /* class_data */
148 sizeof (GtkTextLayout),
150 (GInstanceInitFunc) gtk_text_layout_init
153 our_type = g_type_register_static (G_TYPE_OBJECT,
163 gtk_text_layout_class_init (GtkTextLayoutClass *klass)
165 GObjectClass *object_class = G_OBJECT_CLASS (klass);
167 parent_class = g_type_class_peek_parent (klass);
169 object_class->finalize = gtk_text_layout_finalize;
171 klass->wrap = gtk_text_layout_real_wrap;
172 klass->invalidate = gtk_text_layout_real_invalidate;
173 klass->free_line_data = gtk_text_layout_real_free_line_data;
175 signals[INVALIDATED] =
176 g_signal_new ("invalidated",
177 G_OBJECT_CLASS_TYPE (object_class),
179 G_STRUCT_OFFSET (GtkTextLayoutClass, invalidated),
181 gtk_marshal_VOID__VOID,
186 g_signal_new ("changed",
187 G_OBJECT_CLASS_TYPE (object_class),
189 G_STRUCT_OFFSET (GtkTextLayoutClass, changed),
191 gtk_marshal_VOID__INT_INT_INT,
198 signals[ALLOCATE_CHILD] =
199 g_signal_new ("allocate_child",
200 G_OBJECT_CLASS_TYPE (object_class),
202 G_STRUCT_OFFSET (GtkTextLayoutClass, allocate_child),
204 gtk_marshal_VOID__OBJECT_INT_INT,
213 gtk_text_layout_init (GtkTextLayout *text_layout)
215 text_layout->cursor_visible = TRUE;
219 gtk_text_layout_new (void)
221 return GTK_TEXT_LAYOUT (g_object_new (gtk_text_layout_get_type (), NULL));
225 free_style_cache (GtkTextLayout *text_layout)
227 if (text_layout->one_style_cache)
229 gtk_text_attributes_unref (text_layout->one_style_cache);
230 text_layout->one_style_cache = NULL;
235 gtk_text_layout_finalize (GObject *object)
237 GtkTextLayout *layout;
239 layout = GTK_TEXT_LAYOUT (object);
241 gtk_text_layout_set_buffer (layout, NULL);
243 if (layout->default_style)
244 gtk_text_attributes_unref (layout->default_style);
245 layout->default_style = NULL;
247 if (layout->ltr_context)
249 g_object_unref (G_OBJECT (layout->ltr_context));
250 layout->ltr_context = NULL;
252 if (layout->rtl_context)
254 g_object_unref (G_OBJECT (layout->rtl_context));
255 layout->rtl_context = NULL;
258 (* G_OBJECT_CLASS (parent_class)->finalize) (object);
262 gtk_text_layout_set_buffer (GtkTextLayout *layout,
263 GtkTextBuffer *buffer)
265 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
266 g_return_if_fail (buffer == NULL || GTK_IS_TEXT_BUFFER (buffer));
268 if (layout->buffer == buffer)
271 free_style_cache (layout);
275 _gtk_text_btree_remove_view (_gtk_text_buffer_get_btree (layout->buffer),
278 g_object_unref (G_OBJECT (layout->buffer));
279 layout->buffer = NULL;
284 layout->buffer = buffer;
286 g_object_ref (G_OBJECT (buffer));
288 _gtk_text_btree_add_view (_gtk_text_buffer_get_btree (buffer), layout);
293 gtk_text_layout_default_style_changed (GtkTextLayout *layout)
295 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
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 gtk_text_layout_invalidate_all (layout);
343 * gtk_text_layout_set_cursor_direction:
344 * @direction: the new direction(s) for which to draw cursors.
345 * %GTK_TEXT_DIR_NONE means draw cursors for both
346 * left-to-right insertion and right-to-left insertion.
347 * (The two cursors will be visually distinguished.)
349 * Sets which text directions (left-to-right and/or right-to-left) for
350 * which cursors will be drawn for the insertion point. The visual
351 * point at which new text is inserted depends on whether the new
352 * text is right-to-left or left-to-right, so it may be desired to
353 * make the drawn position of the cursor depend on the keyboard state.
356 gtk_text_layout_set_cursor_direction (GtkTextLayout *layout,
357 GtkTextDirection direction)
359 if (direction != layout->cursor_direction)
361 layout->cursor_direction = direction;
362 gtk_text_layout_invalidate_cursor_line (layout);
367 * gtk_text_layout_get_buffer:
368 * @layout: a #GtkTextLayout
370 * Gets the text buffer used by the layout. See
371 * gtk_text_layout_set_buffer().
373 * Return value: the text buffer used by the layout.
376 gtk_text_layout_get_buffer (GtkTextLayout *layout)
378 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), NULL);
380 return layout->buffer;
384 gtk_text_layout_set_screen_width (GtkTextLayout *layout, gint width)
386 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
387 g_return_if_fail (width >= 0);
388 g_return_if_fail (layout->wrap_loop_count == 0);
390 if (layout->screen_width == width)
393 layout->screen_width = width;
395 gtk_text_layout_invalidate_all (layout);
399 * gtk_text_layout_set_cursor_visible:
400 * @layout: a #GtkTextLayout
401 * @cursor_visible: If %FALSE, then the insertion cursor will not
402 * be shown, even if the text is editable.
404 * Sets whether the insertion cursor should be shown. Generally,
405 * widgets using #GtkTextLayout will hide the cursor when the
406 * widget does not have the input focus.
409 gtk_text_layout_set_cursor_visible (GtkTextLayout *layout,
410 gboolean cursor_visible)
412 cursor_visible = (cursor_visible != FALSE);
414 if (layout->cursor_visible != cursor_visible)
419 layout->cursor_visible = cursor_visible;
421 /* Now queue a redraw on the paragraph containing the cursor
423 gtk_text_buffer_get_iter_at_mark (layout->buffer, &iter,
424 gtk_text_buffer_get_mark (layout->buffer, "insert"));
426 gtk_text_layout_get_line_yrange (layout, &iter, &y, &height);
427 gtk_text_layout_changed (layout, y, height, height);
429 gtk_text_layout_invalidate_cache (layout, _gtk_text_iter_get_text_line (&iter));
434 * gtk_text_layout_get_cursor_visible:
435 * @layout: a #GtkTextLayout
437 * Returns whether the insertion cursor will be shown.
439 * Return value: if %FALSE, the insertion cursor will not be
440 shown, even if the text is editable.
443 gtk_text_layout_get_cursor_visible (GtkTextLayout *layout)
445 return layout->cursor_visible;
449 * gtk_text_layout_set_preedit_string:
450 * @layout: a #PangoLayout
451 * @preedit_string: a string to display at the insertion point
452 * @preedit_attrs: a #PangoAttrList of attributes that apply to @preedit_string
453 * @cursor_pos: position of cursor within preedit string in chars
455 * Set the preedit string and attributes. The preedit string is a
456 * string showing text that is currently being edited and not
457 * yet committed into the buffer.
460 gtk_text_layout_set_preedit_string (GtkTextLayout *layout,
461 const gchar *preedit_string,
462 PangoAttrList *preedit_attrs,
465 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
466 g_return_if_fail (preedit_attrs != NULL || preedit_string == NULL);
468 if (layout->preedit_string)
469 g_free (layout->preedit_string);
471 if (layout->preedit_attrs)
472 pango_attr_list_unref (layout->preedit_attrs);
476 layout->preedit_string = g_strdup (preedit_string);
477 layout->preedit_len = strlen (layout->preedit_string);
478 pango_attr_list_ref (preedit_attrs);
479 layout->preedit_attrs = preedit_attrs;
481 cursor_pos = CLAMP (cursor_pos, 0, g_utf8_strlen (layout->preedit_string, -1));
482 layout->preedit_cursor = g_utf8_offset_to_pointer (layout->preedit_string, cursor_pos) - layout->preedit_string;
486 layout->preedit_string = NULL;
487 layout->preedit_len = 0;
488 layout->preedit_attrs = NULL;
489 layout->preedit_cursor = 0;
492 gtk_text_layout_invalidate_cursor_line (layout);
496 gtk_text_layout_get_size (GtkTextLayout *layout,
500 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
503 *width = layout->width;
506 *height = layout->height;
510 gtk_text_layout_invalidated (GtkTextLayout *layout)
512 g_signal_emit (G_OBJECT (layout), signals[INVALIDATED], 0);
516 gtk_text_layout_changed (GtkTextLayout *layout,
521 g_signal_emit (G_OBJECT (layout), signals[CHANGED], 0,
522 y, old_height, new_height);
526 gtk_text_layout_free_line_data (GtkTextLayout *layout,
528 GtkTextLineData *line_data)
530 (* GTK_TEXT_LAYOUT_GET_CLASS (layout)->free_line_data)
531 (layout, line, line_data);
535 gtk_text_layout_invalidate (GtkTextLayout *layout,
536 const GtkTextIter *start_index,
537 const GtkTextIter *end_index)
539 (* GTK_TEXT_LAYOUT_GET_CLASS (layout)->invalidate)
540 (layout, start_index, end_index);
544 gtk_text_layout_wrap (GtkTextLayout *layout,
547 GtkTextLineData *line_data)
549 return (* GTK_TEXT_LAYOUT_GET_CLASS (layout)->wrap) (layout, line, line_data);
553 gtk_text_layout_get_lines (GtkTextLayout *layout,
554 /* [top_y, bottom_y) */
559 GtkTextLine *first_btree_line;
560 GtkTextLine *last_btree_line;
564 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), NULL);
565 g_return_val_if_fail (bottom_y > top_y, NULL);
570 _gtk_text_btree_find_line_by_y (_gtk_text_buffer_get_btree (layout->buffer),
571 layout, top_y, first_line_y);
572 if (first_btree_line == NULL)
578 /* -1 since bottom_y is one past */
580 _gtk_text_btree_find_line_by_y (_gtk_text_buffer_get_btree (layout->buffer),
581 layout, bottom_y - 1, NULL);
583 if (!last_btree_line)
585 _gtk_text_btree_get_end_iter_line (_gtk_text_buffer_get_btree (layout->buffer));
587 g_assert (last_btree_line != NULL);
589 line = first_btree_line;
592 retval = g_slist_prepend (retval, line);
594 if (line == last_btree_line)
597 line = _gtk_text_line_next_excluding_last (line);
600 retval = g_slist_reverse (retval);
606 invalidate_cached_style (GtkTextLayout *layout)
608 free_style_cache (layout);
611 /* These should be called around a loop which wraps a CONTIGUOUS bunch
612 * of display lines. If the lines aren't contiguous you can't call
616 gtk_text_layout_wrap_loop_start (GtkTextLayout *layout)
618 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
619 g_return_if_fail (layout->one_style_cache == NULL);
621 layout->wrap_loop_count += 1;
625 gtk_text_layout_wrap_loop_end (GtkTextLayout *layout)
627 g_return_if_fail (layout->wrap_loop_count > 0);
629 layout->wrap_loop_count -= 1;
631 if (layout->wrap_loop_count == 0)
633 /* We cache a some stuff if we're iterating over some lines wrapping
634 * them. This cleans it up.
636 /* Nuke our cached style */
637 invalidate_cached_style (layout);
638 g_assert (layout->one_style_cache == NULL);
643 gtk_text_layout_invalidate_all (GtkTextLayout *layout)
648 if (layout->buffer == NULL)
651 gtk_text_buffer_get_bounds (layout->buffer, &start, &end);
653 gtk_text_layout_invalidate (layout, &start, &end);
657 gtk_text_layout_invalidate_cache (GtkTextLayout *layout,
660 if (layout->one_display_cache && line == layout->one_display_cache->line)
662 GtkTextLineDisplay *tmp_display = layout->one_display_cache;
663 layout->one_display_cache = NULL;
664 gtk_text_layout_free_line_display (layout, tmp_display);
668 /* Now invalidate the paragraph containing the cursor
671 gtk_text_layout_invalidate_cursor_line (GtkTextLayout *layout)
675 GtkTextLineData *line_data;
677 gtk_text_buffer_get_iter_at_mark (layout->buffer, &iter,
678 gtk_text_buffer_get_mark (layout->buffer, "insert"));
680 line = _gtk_text_iter_get_text_line (&iter);
681 line_data = _gtk_text_line_get_data (line, layout);
684 gtk_text_layout_invalidate_cache (layout, line);
685 _gtk_text_line_invalidate_wrap (line, line_data);
686 gtk_text_layout_invalidated (layout);
691 gtk_text_layout_real_invalidate (GtkTextLayout *layout,
692 const GtkTextIter *start,
693 const GtkTextIter *end)
696 GtkTextLine *last_line;
698 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
699 g_return_if_fail (layout->wrap_loop_count == 0);
702 gtk_text_view_index_spew (start_index, "invalidate start");
703 gtk_text_view_index_spew (end_index, "invalidate end");
706 last_line = _gtk_text_iter_get_text_line (end);
707 line = _gtk_text_iter_get_text_line (start);
711 GtkTextLineData *line_data = _gtk_text_line_get_data (line, layout);
714 (line != last_line || !gtk_text_iter_starts_line (end)))
716 gtk_text_layout_invalidate_cache (layout, line);
717 _gtk_text_line_invalidate_wrap (line, line_data);
720 if (line == last_line)
723 line = _gtk_text_line_next_excluding_last (line);
726 gtk_text_layout_invalidated (layout);
730 gtk_text_layout_real_free_line_data (GtkTextLayout *layout,
732 GtkTextLineData *line_data)
734 if (layout->one_display_cache && line == layout->one_display_cache->line)
736 GtkTextLineDisplay *tmp_display = layout->one_display_cache;
737 layout->one_display_cache = NULL;
738 gtk_text_layout_free_line_display (layout, tmp_display);
747 * gtk_text_layout_is_valid:
748 * @layout: a #GtkTextLayout
750 * Check if there are any invalid regions in a #GtkTextLayout's buffer
752 * Return value: #TRUE if any invalid regions were found
755 gtk_text_layout_is_valid (GtkTextLayout *layout)
757 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), FALSE);
759 return _gtk_text_btree_is_valid (_gtk_text_buffer_get_btree (layout->buffer),
764 update_layout_size (GtkTextLayout *layout)
766 _gtk_text_btree_get_view_size (_gtk_text_buffer_get_btree (layout->buffer),
768 &layout->width, &layout->height);
772 * gtk_text_layout_validate_yrange:
773 * @layout: a #GtkTextLayout
774 * @anchor: iter pointing into a line that will be used as the
776 * @y0: offset from the top of the line pointed to by @anchor at
777 * which to begin validation. (The offset here is in pixels
779 * @y1: offset from the top of the line pointed to by @anchor at
780 * which to end validation. (The offset here is in pixels
783 * Ensure that a region of a #GtkTextLayout is valid. The ::changed
784 * signal will be emitted if any lines are validated.
787 gtk_text_layout_validate_yrange (GtkTextLayout *layout,
793 GtkTextLine *first_line = NULL;
794 GtkTextLine *last_line = NULL;
796 gint delta_height = 0;
797 gint first_line_y = 0; /* Quiet GCC */
798 gint last_line_y = 0; /* Quiet GCC */
800 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
807 /* Validate backwards from the anchor line to y0
809 line = _gtk_text_iter_get_text_line (anchor);
811 while (line && seen < -y0)
813 GtkTextLineData *line_data = _gtk_text_line_get_data (line, layout);
814 if (!line_data || !line_data->valid)
816 gint old_height = line_data ? line_data->height : 0;
818 _gtk_text_btree_validate_line (_gtk_text_buffer_get_btree (layout->buffer),
820 line_data = _gtk_text_line_get_data (line, layout);
822 delta_height += line_data->height - old_height;
825 first_line_y = -seen;
829 last_line_y = -seen + line_data->height;
833 seen += line_data->height;
834 line = _gtk_text_line_previous (line);
837 /* Validate forwards to y1 */
838 line = _gtk_text_iter_get_text_line (anchor);
840 while (line && seen < y1)
842 GtkTextLineData *line_data = _gtk_text_line_get_data (line, layout);
843 if (!line_data || !line_data->valid)
845 gint old_height = line_data ? line_data->height : 0;
847 _gtk_text_btree_validate_line (_gtk_text_buffer_get_btree (layout->buffer),
849 line_data = _gtk_text_line_get_data (line, layout);
851 delta_height += line_data->height - old_height;
859 last_line_y = seen + line_data->height;
862 seen += line_data->height;
863 line = _gtk_text_line_next_excluding_last (line);
866 /* If we found and validated any invalid lines, update size and
867 * emit the changed signal
873 update_layout_size (layout);
875 line_top = _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
878 gtk_text_layout_changed (layout,
880 last_line_y - first_line_y - delta_height,
881 last_line_y - first_line_y);
886 * gtk_text_layout_validate:
887 * @tree: a #GtkTextLayout
888 * @max_pixels: the maximum number of pixels to validate. (No more
889 * than one paragraph beyond this limit will be validated)
891 * Validate regions of a #GtkTextLayout. The ::changed signal will
892 * be emitted for each region validated.
895 gtk_text_layout_validate (GtkTextLayout *layout,
898 gint y, old_height, new_height;
900 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
902 while (max_pixels > 0 &&
903 _gtk_text_btree_validate (_gtk_text_buffer_get_btree (layout->buffer),
905 &y, &old_height, &new_height))
907 max_pixels -= new_height;
909 update_layout_size (layout);
910 gtk_text_layout_changed (layout, y, old_height, new_height);
914 static GtkTextLineData*
915 gtk_text_layout_real_wrap (GtkTextLayout *layout,
918 GtkTextLineData *line_data)
920 GtkTextLineDisplay *display;
922 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), NULL);
924 if (line_data == NULL)
926 line_data = gtk_text_line_data_new (layout, line);
927 _gtk_text_line_add_data (line, line_data);
930 display = gtk_text_layout_get_line_display (layout, line, TRUE);
931 line_data->width = display->width;
932 line_data->height = display->height;
933 line_data->valid = TRUE;
934 gtk_text_layout_free_line_display (layout, display);
940 * Layout utility functions
943 /* If you get the style with get_style () you need to call
944 release_style () to free it. */
945 static GtkTextAttributes*
946 get_style (GtkTextLayout *layout,
947 const GtkTextIter *iter)
951 GtkTextAttributes *style;
953 /* If we have the one-style cache, then it means
954 that we haven't seen a toggle since we filled in the
957 if (layout->one_style_cache != NULL)
959 gtk_text_attributes_ref (layout->one_style_cache);
960 return layout->one_style_cache;
963 g_assert (layout->one_style_cache == NULL);
965 /* Get the tags at this spot */
966 tags = _gtk_text_btree_get_tags (iter, &tag_count);
968 /* No tags, use default style */
969 if (tags == NULL || tag_count == 0)
971 /* One ref for the return value, one ref for the
972 layout->one_style_cache reference */
973 gtk_text_attributes_ref (layout->default_style);
974 gtk_text_attributes_ref (layout->default_style);
975 layout->one_style_cache = layout->default_style;
980 return layout->default_style;
983 /* Sort tags in ascending order of priority */
984 _gtk_text_tag_array_sort (tags, tag_count);
986 style = gtk_text_attributes_new ();
988 gtk_text_attributes_copy_values (layout->default_style,
991 _gtk_text_attributes_fill_from_tags (style,
997 g_assert (style->refcount == 1);
999 /* Leave this style as the last one seen */
1000 g_assert (layout->one_style_cache == NULL);
1001 gtk_text_attributes_ref (style); /* ref held by layout->one_style_cache */
1002 layout->one_style_cache = style;
1004 /* Returning yet another refcount */
1009 release_style (GtkTextLayout *layout,
1010 GtkTextAttributes *style)
1012 g_return_if_fail (style != NULL);
1013 g_return_if_fail (style->refcount > 0);
1015 gtk_text_attributes_unref (style);
1022 /* This function tries to optimize the case where a line
1023 is completely invisible */
1025 totally_invisible_line (GtkTextLayout *layout,
1029 GtkTextLineSegment *seg;
1032 /* If we have a cached style, then we know it does actually apply
1033 * and we can just see if it is invisible.
1035 if (layout->one_style_cache &&
1036 !layout->one_style_cache->invisible)
1038 /* Without the cache, we check if the first char is visible, if so
1039 * we are partially visible. Note that we have to check this since
1040 * we don't know the current invisible/noninvisible toggle state; this
1041 * function can use the whole btree to get it right.
1045 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
1048 if (!_gtk_text_btree_char_is_invisible (iter))
1053 seg = line->segments;
1057 if (seg->byte_count > 0)
1058 bytes += seg->byte_count;
1060 /* Note that these two tests can cause us to bail out
1061 * when we shouldn't, because a higher-priority tag
1062 * may override these settings. However the important
1063 * thing is to only invisible really-invisible lines, rather
1064 * than to invisible all really-invisible lines.
1067 else if (seg->type == >k_text_toggle_on_type)
1069 invalidate_cached_style (layout);
1071 /* Bail out if an elision-unsetting tag begins */
1072 if (seg->body.toggle.info->tag->invisible_set &&
1073 !seg->body.toggle.info->tag->values->invisible)
1076 else if (seg->type == >k_text_toggle_off_type)
1078 invalidate_cached_style (layout);
1080 /* Bail out if an elision-setting tag ends */
1081 if (seg->body.toggle.info->tag->invisible_set &&
1082 seg->body.toggle.info->tag->values->invisible)
1089 if (seg != NULL) /* didn't reach line end */
1096 set_para_values (GtkTextLayout *layout,
1097 GtkTextAttributes *style,
1098 GtkTextLineDisplay *display)
1100 PangoAlignment pango_align = PANGO_ALIGN_LEFT;
1103 display->direction = style->direction;
1105 if (display->direction == GTK_TEXT_DIR_LTR)
1106 display->layout = pango_layout_new (layout->ltr_context);
1108 display->layout = pango_layout_new (layout->rtl_context);
1110 switch (style->justification)
1112 case GTK_JUSTIFY_LEFT:
1113 pango_align = (style->direction == GTK_TEXT_DIR_LTR) ? PANGO_ALIGN_LEFT : PANGO_ALIGN_RIGHT;
1115 case GTK_JUSTIFY_RIGHT:
1116 pango_align = (style->direction == GTK_TEXT_DIR_LTR) ? PANGO_ALIGN_RIGHT : PANGO_ALIGN_LEFT;
1118 case GTK_JUSTIFY_CENTER:
1119 pango_align = PANGO_ALIGN_CENTER;
1121 case GTK_JUSTIFY_FILL:
1122 g_warning ("FIXME we don't support GTK_JUSTIFY_FILL yet");
1125 g_assert_not_reached ();
1129 pango_layout_set_alignment (display->layout, pango_align);
1130 pango_layout_set_spacing (display->layout,
1131 style->pixels_inside_wrap * PANGO_SCALE);
1134 pango_layout_set_tabs (display->layout, style->tabs);
1136 display->top_margin = style->pixels_above_lines;
1137 display->height = style->pixels_above_lines + style->pixels_below_lines;
1138 display->bottom_margin = style->pixels_below_lines;
1139 display->left_margin = style->left_margin;
1140 display->right_margin = style->right_margin;
1142 display->x_offset = display->left_margin;
1144 pango_layout_set_indent (display->layout,
1145 style->indent * PANGO_SCALE);
1147 switch (style->wrap_mode)
1150 layout_width = layout->screen_width - display->left_margin - display->right_margin;
1151 pango_layout_set_width (display->layout, layout_width * PANGO_SCALE);
1152 pango_layout_set_wrap (display->layout, PANGO_WRAP_CHAR);
1156 layout_width = layout->screen_width - display->left_margin - display->right_margin;
1157 pango_layout_set_width (display->layout, layout_width * PANGO_SCALE);
1158 pango_layout_set_wrap (display->layout, PANGO_WRAP_WORD);
1165 display->total_width = MAX (layout->screen_width, layout->width) - display->left_margin - display->right_margin;
1168 static PangoAttribute *
1169 gtk_text_attr_appearance_copy (const PangoAttribute *attr)
1171 const GtkTextAttrAppearance *appearance_attr = (const GtkTextAttrAppearance *)attr;
1173 return gtk_text_attr_appearance_new (&appearance_attr->appearance);
1177 gtk_text_attr_appearance_destroy (PangoAttribute *attr)
1179 GtkTextAppearance *appearance = &((GtkTextAttrAppearance *)attr)->appearance;
1181 if (appearance->bg_stipple)
1182 gdk_drawable_unref (appearance->bg_stipple);
1183 if (appearance->fg_stipple)
1184 gdk_drawable_unref (appearance->fg_stipple);
1190 gtk_text_attr_appearance_compare (const PangoAttribute *attr1,
1191 const PangoAttribute *attr2)
1193 const GtkTextAppearance *appearance1 = &((const GtkTextAttrAppearance *)attr1)->appearance;
1194 const GtkTextAppearance *appearance2 = &((const GtkTextAttrAppearance *)attr2)->appearance;
1196 return (gdk_color_equal (&appearance1->fg_color, &appearance2->fg_color) &&
1197 gdk_color_equal (&appearance1->bg_color, &appearance2->bg_color) &&
1198 appearance1->fg_stipple == appearance2->fg_stipple &&
1199 appearance1->bg_stipple == appearance2->bg_stipple &&
1200 appearance1->underline == appearance2->underline &&
1201 appearance1->strikethrough == appearance2->strikethrough &&
1202 appearance1->draw_bg == appearance2->draw_bg);
1207 * gtk_text_attr_appearance_new:
1210 * Create a new font description attribute. (This attribute
1211 * allows setting family, style, weight, variant, stretch,
1212 * and size simultaneously.)
1216 static PangoAttribute *
1217 gtk_text_attr_appearance_new (const GtkTextAppearance *appearance)
1219 static PangoAttrClass klass = {
1221 gtk_text_attr_appearance_copy,
1222 gtk_text_attr_appearance_destroy,
1223 gtk_text_attr_appearance_compare
1226 GtkTextAttrAppearance *result;
1229 klass.type = gtk_text_attr_appearance_type =
1230 pango_attr_type_register ("GtkTextAttrAppearance");
1232 result = g_new (GtkTextAttrAppearance, 1);
1233 result->attr.klass = &klass;
1235 result->appearance = *appearance;
1237 if (appearance->bg_stipple)
1238 gdk_drawable_ref (appearance->bg_stipple);
1239 if (appearance->fg_stipple)
1240 gdk_drawable_ref (appearance->fg_stipple);
1242 return (PangoAttribute *)result;
1247 add_generic_attrs (GtkTextLayout *layout,
1248 GtkTextAppearance *appearance,
1250 PangoAttrList *attrs,
1255 PangoAttribute *attr;
1257 if (appearance->underline != PANGO_UNDERLINE_NONE)
1259 attr = pango_attr_underline_new (appearance->underline);
1261 attr->start_index = start;
1262 attr->end_index = start + byte_count;
1264 pango_attr_list_insert (attrs, attr);
1267 if (appearance->rise != 0)
1269 attr = pango_attr_rise_new (appearance->rise);
1271 attr->start_index = start;
1272 attr->end_index = start + byte_count;
1274 pango_attr_list_insert (attrs, attr);
1279 attr = gtk_text_attr_appearance_new (appearance);
1281 attr->start_index = start;
1282 attr->end_index = start + byte_count;
1284 ((GtkTextAttrAppearance *)attr)->appearance.is_text = is_text;
1286 pango_attr_list_insert (attrs, attr);
1291 add_text_attrs (GtkTextLayout *layout,
1292 GtkTextAttributes *style,
1294 PangoAttrList *attrs,
1298 PangoAttribute *attr;
1300 attr = pango_attr_font_desc_new (style->font);
1301 attr->start_index = start;
1302 attr->end_index = start + byte_count;
1304 pango_attr_list_insert (attrs, attr);
1306 if (style->font_scale != 1.0)
1308 attr = pango_attr_scale_new (style->font_scale);
1310 attr->start_index = start;
1311 attr->end_index = start + byte_count;
1313 pango_attr_list_insert (attrs, attr);
1318 add_pixbuf_attrs (GtkTextLayout *layout,
1319 GtkTextLineDisplay *display,
1320 GtkTextAttributes *style,
1321 GtkTextLineSegment *seg,
1322 PangoAttrList *attrs,
1325 PangoAttribute *attr;
1326 PangoRectangle logical_rect;
1327 GtkTextPixbuf *pixbuf = &seg->body.pixbuf;
1330 width = gdk_pixbuf_get_width (pixbuf->pixbuf);
1331 height = gdk_pixbuf_get_height (pixbuf->pixbuf);
1334 logical_rect.y = -height * PANGO_SCALE;
1335 logical_rect.width = width * PANGO_SCALE;
1336 logical_rect.height = height * PANGO_SCALE;
1338 attr = pango_attr_shape_new (&logical_rect, &logical_rect);
1339 attr->start_index = start;
1340 attr->end_index = start + seg->byte_count;
1341 pango_attr_list_insert (attrs, attr);
1343 display->shaped_objects =
1344 g_slist_append (display->shaped_objects, pixbuf->pixbuf);
1348 add_child_attrs (GtkTextLayout *layout,
1349 GtkTextLineDisplay *display,
1350 GtkTextAttributes *style,
1351 GtkTextLineSegment *seg,
1352 PangoAttrList *attrs,
1355 PangoAttribute *attr;
1356 PangoRectangle logical_rect;
1357 GtkTextChildAnchor *anchor;
1364 anchor = seg->body.child.obj;
1366 tmp_list = seg->body.child.widgets;
1367 while (tmp_list != NULL)
1369 GtkWidget *child = tmp_list->data;
1371 if (_gtk_anchored_child_get_layout (child) == layout)
1376 gtk_widget_get_child_requisition (child, &req);
1379 height = req.height;
1381 display->shaped_objects =
1382 g_slist_append (display->shaped_objects, child);
1387 tmp_list = g_slist_next (tmp_list);
1390 if (tmp_list == NULL)
1392 /* If tmp_list == NULL then there is no widget at this anchor in
1393 * this display; not an error. We make up an arbitrary size
1394 * to use, just so the programmer can see the blank spot.
1395 * We also put a NULL in the shaped objects list, to keep
1396 * the correspondence between the list and the shaped chars in
1397 * the layout. A bad hack, yes.
1403 display->shaped_objects =
1404 g_slist_append (display->shaped_objects, NULL);
1408 logical_rect.y = -height * PANGO_SCALE;
1409 logical_rect.width = width * PANGO_SCALE;
1410 logical_rect.height = height * PANGO_SCALE;
1412 attr = pango_attr_shape_new (&logical_rect, &logical_rect);
1413 attr->start_index = start;
1414 attr->end_index = start + seg->byte_count;
1415 pango_attr_list_insert (attrs, attr);
1419 add_cursor (GtkTextLayout *layout,
1420 GtkTextLineDisplay *display,
1421 GtkTextLineSegment *seg,
1424 PangoRectangle strong_pos, weak_pos;
1425 GtkTextCursorDisplay *cursor = NULL; /* Quiet GCC */
1426 gboolean add_weak = FALSE;
1427 gboolean add_strong = FALSE;
1429 /* Hide insertion cursor when we have a selection or the layout
1430 * user has hidden the cursor.
1432 if (_gtk_text_btree_mark_is_insert (_gtk_text_buffer_get_btree (layout->buffer),
1433 seg->body.mark.obj) &&
1434 (!layout->cursor_visible ||
1435 gtk_text_buffer_get_selection_bounds (layout->buffer, NULL, NULL)))
1438 pango_layout_get_cursor_pos (display->layout, start, &strong_pos, &weak_pos);
1440 if (layout->cursor_direction == GTK_TEXT_DIR_NONE)
1445 else if (display->direction == layout->cursor_direction)
1452 cursor = g_new (GtkTextCursorDisplay, 1);
1454 cursor->x = PANGO_PIXELS (strong_pos.x);
1455 cursor->y = PANGO_PIXELS (strong_pos.y);
1456 cursor->height = PANGO_PIXELS (strong_pos.height);
1457 cursor->is_strong = TRUE;
1458 cursor->is_weak = FALSE;
1459 display->cursors = g_slist_prepend (display->cursors, cursor);
1464 if (weak_pos.x == strong_pos.x && add_strong)
1465 cursor->is_weak = TRUE;
1468 cursor = g_new (GtkTextCursorDisplay, 1);
1470 cursor->x = PANGO_PIXELS (weak_pos.x);
1471 cursor->y = PANGO_PIXELS (weak_pos.y);
1472 cursor->height = PANGO_PIXELS (weak_pos.height);
1473 cursor->is_strong = FALSE;
1474 cursor->is_weak = TRUE;
1475 display->cursors = g_slist_prepend (display->cursors, cursor);
1481 is_shape (PangoLayoutRun *run)
1483 GSList *tmp_list = run->item->analysis.extra_attrs;
1487 PangoAttribute *attr = tmp_list->data;
1489 if (attr->klass->type == PANGO_ATTR_SHAPE)
1492 tmp_list = tmp_list->next;
1499 allocate_child_widgets (GtkTextLayout *text_layout,
1500 GtkTextLineDisplay *display)
1502 GSList *shaped = display->shaped_objects;
1503 PangoLayout *layout = display->layout;
1504 PangoLayoutIter *iter;
1506 iter = pango_layout_get_iter (layout);
1510 PangoLayoutRun *run = pango_layout_iter_get_run (iter);
1512 if (run && is_shape (run))
1514 GObject *shaped_object = shaped->data;
1515 shaped = shaped->next;
1517 /* shaped_object is NULL for child anchors with no
1518 * widgets stored at them
1520 if (shaped_object && GTK_IS_WIDGET (shaped_object))
1522 PangoRectangle extents;
1524 /* We emit "allocate_child" with the x,y of
1525 * the widget with respect to the top of the line
1526 * and the left side of the buffer
1529 pango_layout_iter_get_run_extents (iter,
1533 g_signal_emit (G_OBJECT (text_layout),
1534 signals[ALLOCATE_CHILD],
1537 PANGO_PIXELS (extents.x) + display->x_offset,
1538 PANGO_PIXELS (extents.y) + display->top_margin);
1542 while (pango_layout_iter_next_run (iter));
1544 pango_layout_iter_free (iter);
1548 convert_color (GdkColor *result,
1549 PangoAttrColor *attr)
1551 result->red = attr->color.red;
1552 result->blue = attr->color.blue;
1553 result->green = attr->color.green;
1556 /* This function is used to convert the preedit string attributes, which are
1557 * standard PangoAttributes, into the custom attributes used by the text
1558 * widget and insert them into a attr list with a given offset.
1561 add_preedit_attrs (GtkTextLayout *layout,
1562 GtkTextAttributes *style,
1563 PangoAttrList *attrs,
1567 PangoAttrIterator *iter = pango_attr_list_get_iterator (layout->preedit_attrs);
1571 GtkTextAppearance appearance = style->appearance;
1572 PangoFontDescription *font_desc = pango_font_description_copy_static (style->font);
1573 PangoAttribute *insert_attr;
1574 GSList *extra_attrs = NULL;
1576 PangoLanguage *language;
1579 pango_attr_iterator_range (iter, &start, &end);
1581 if (end == G_MAXINT)
1582 end = layout->preedit_len;
1584 pango_attr_iterator_get_font (iter, font_desc, &language, &extra_attrs);
1586 tmp_list = extra_attrs;
1589 PangoAttribute *attr = tmp_list->data;
1591 switch (attr->klass->type)
1593 case PANGO_ATTR_FOREGROUND:
1594 convert_color (&appearance.fg_color, (PangoAttrColor *)attr);
1596 case PANGO_ATTR_BACKGROUND:
1597 convert_color (&appearance.bg_color, (PangoAttrColor *)attr);
1598 appearance.draw_bg = TRUE;
1600 case PANGO_ATTR_UNDERLINE:
1601 appearance.underline = ((PangoAttrInt *)attr)->value;
1603 case PANGO_ATTR_STRIKETHROUGH:
1604 appearance.strikethrough = ((PangoAttrInt *)attr)->value;
1606 case PANGO_ATTR_RISE:
1607 appearance.rise = ((PangoAttrInt *)attr)->value;
1613 pango_attribute_destroy (attr);
1614 tmp_list = tmp_list->next;
1617 g_slist_free (extra_attrs);
1619 insert_attr = pango_attr_font_desc_new (font_desc);
1620 insert_attr->start_index = start + offset;
1621 insert_attr->end_index = end + offset;
1623 pango_attr_list_insert (attrs, insert_attr);
1627 insert_attr = pango_attr_language_new (language);
1628 insert_attr->start_index = start + offset;
1629 insert_attr->end_index = end + offset;
1631 pango_attr_list_insert (attrs, insert_attr);
1634 add_generic_attrs (layout, &appearance, end - start,
1635 attrs, start + offset,
1638 pango_font_description_free (font_desc);
1640 while (pango_attr_iterator_next (iter));
1642 pango_attr_iterator_destroy (iter);
1645 GtkTextLineDisplay *
1646 gtk_text_layout_get_line_display (GtkTextLayout *layout,
1650 GtkTextLineDisplay *display;
1651 GtkTextLineSegment *seg;
1653 GtkTextAttributes *style;
1655 PangoAttrList *attrs;
1656 gint text_allocated, layout_byte_offset, buffer_byte_offset;
1657 PangoRectangle extents;
1658 gboolean para_values_set = FALSE;
1659 GSList *cursor_byte_offsets = NULL;
1660 GSList *cursor_segs = NULL;
1661 GSList *tmp_list1, *tmp_list2;
1662 gboolean saw_widget = FALSE;
1664 g_return_val_if_fail (line != NULL, NULL);
1666 if (layout->one_display_cache)
1668 if (line == layout->one_display_cache->line &&
1669 (size_only || !layout->one_display_cache->size_only))
1670 return layout->one_display_cache;
1673 GtkTextLineDisplay *tmp_display = layout->one_display_cache;
1674 layout->one_display_cache = NULL;
1675 gtk_text_layout_free_line_display (layout, tmp_display);
1679 display = g_new0 (GtkTextLineDisplay, 1);
1681 display->size_only = size_only;
1682 display->line = line;
1683 display->insert_index = -1;
1685 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
1688 /* Special-case optimization for completely
1689 * invisible lines; makes it faster to deal
1690 * with sequences of invisible lines.
1692 if (totally_invisible_line (layout, line, &iter))
1695 /* Allocate space for flat text for buffer
1697 text_allocated = _gtk_text_line_byte_count (line);
1698 text = g_malloc (text_allocated);
1700 attrs = pango_attr_list_new ();
1702 /* Iterate over segments, creating display chunks for them. */
1703 layout_byte_offset = 0; /* current length of layout text (includes preedit, does not include invisible text) */
1704 buffer_byte_offset = 0; /* position in the buffer line */
1705 seg = _gtk_text_iter_get_any_segment (&iter);
1708 /* Displayable segments */
1709 if (seg->type == >k_text_char_type ||
1710 seg->type == >k_text_pixbuf_type ||
1711 seg->type == >k_text_child_type)
1713 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
1715 buffer_byte_offset);
1716 style = get_style (layout, &iter);
1718 /* We have to delay setting the paragraph values until we
1719 * hit the first pixbuf or text segment because toggles at
1720 * the beginning of the paragraph should affect the
1721 * paragraph-global values
1723 if (!para_values_set)
1725 set_para_values (layout, style, display);
1726 para_values_set = TRUE;
1729 /* First see if the chunk is invisible, and ignore it if so. Tk
1730 * looked at tabs, wrap mode, etc. before doing this, but
1731 * that made no sense to me, so I am just skipping the
1734 if (!style->invisible)
1736 if (seg->type == >k_text_char_type)
1738 /* We don't want to split segments because of marks,
1739 * so we scan forward for more segments only
1740 * separated from us by marks. In theory, we should
1741 * also merge segments with identical styles, even
1742 * if there are toggles in-between
1746 GtkTextLineSegment *prev_seg = NULL;
1750 if (seg->type == >k_text_char_type)
1752 memcpy (text + layout_byte_offset, seg->body.chars, seg->byte_count);
1753 layout_byte_offset += seg->byte_count;
1754 buffer_byte_offset += seg->byte_count;
1755 bytes += seg->byte_count;
1757 else if (seg->type == >k_text_right_mark_type ||
1758 seg->type == >k_text_left_mark_type)
1760 /* If we have preedit string, break out of this loop - we'll almost
1761 * certainly have different attributes on the preedit string
1764 if (layout->preedit_len > 0 &&
1765 _gtk_text_btree_mark_is_insert (_gtk_text_buffer_get_btree (layout->buffer),
1766 seg->body.mark.obj))
1769 if (seg->body.mark.visible)
1771 cursor_byte_offsets = g_slist_prepend (cursor_byte_offsets, GINT_TO_POINTER (layout_byte_offset));
1772 cursor_segs = g_slist_prepend (cursor_segs, seg);
1782 seg = prev_seg; /* Back up one */
1783 add_generic_attrs (layout, &style->appearance,
1785 attrs, layout_byte_offset - bytes,
1787 add_text_attrs (layout, style, bytes, attrs,
1788 layout_byte_offset - bytes, size_only);
1790 else if (seg->type == >k_text_pixbuf_type)
1792 add_generic_attrs (layout,
1795 attrs, layout_byte_offset,
1797 add_pixbuf_attrs (layout, display, style,
1798 seg, attrs, layout_byte_offset);
1799 memcpy (text + layout_byte_offset, gtk_text_unknown_char_utf8,
1801 layout_byte_offset += seg->byte_count;
1802 buffer_byte_offset += seg->byte_count;
1804 else if (seg->type == >k_text_child_type)
1808 add_generic_attrs (layout, &style->appearance,
1810 attrs, layout_byte_offset,
1812 add_child_attrs (layout, display, style,
1813 seg, attrs, layout_byte_offset);
1814 memcpy (text + layout_byte_offset, gtk_text_unknown_char_utf8,
1816 layout_byte_offset += seg->byte_count;
1817 buffer_byte_offset += seg->byte_count;
1821 /* We don't know this segment type */
1822 g_assert_not_reached ();
1825 } /* if (segment was visible) */
1828 /* Invisible segment */
1829 buffer_byte_offset += seg->byte_count;
1832 release_style (layout, style);
1836 else if (seg->type == >k_text_toggle_on_type ||
1837 seg->type == >k_text_toggle_off_type)
1839 /* Style may have changed, drop our
1840 current cached style */
1841 invalidate_cached_style (layout);
1845 else if (seg->type == >k_text_right_mark_type ||
1846 seg->type == >k_text_left_mark_type)
1848 gint cursor_offset = 0;
1850 /* At the insertion point, add the preedit string, if any */
1852 if (_gtk_text_btree_mark_is_insert (_gtk_text_buffer_get_btree (layout->buffer),
1853 seg->body.mark.obj))
1855 display->insert_index = layout_byte_offset;
1857 if (layout->preedit_len > 0)
1859 text_allocated += layout->preedit_len;
1860 text = g_realloc (text, text_allocated);
1862 style = get_style (layout, &iter);
1863 add_preedit_attrs (layout, style, attrs, layout_byte_offset, size_only);
1864 release_style (layout, style);
1866 memcpy (text + layout_byte_offset, layout->preedit_string, layout->preedit_len);
1867 layout_byte_offset += layout->preedit_len;
1868 /* DO NOT increment the buffer byte offset for preedit */
1870 cursor_offset = layout->preedit_cursor - layout->preedit_len;
1875 /* Display visible marks */
1877 if (seg->body.mark.visible)
1879 cursor_byte_offsets = g_slist_prepend (cursor_byte_offsets,
1880 GINT_TO_POINTER (layout_byte_offset + cursor_offset));
1881 cursor_segs = g_slist_prepend (cursor_segs, seg);
1886 g_error ("Unknown segment type: %s", seg->type->name);
1891 if (!para_values_set)
1893 style = get_style (layout, &iter);
1894 set_para_values (layout, style, display);
1895 release_style (layout, style);
1898 /* Pango doesn't want the trailing paragraph delimiters */
1901 /* Only one character has type G_UNICODE_PARAGRAPH_SEPARATOR in
1902 * Unicode 3.0; update this if that changes.
1904 #define PARAGRAPH_SEPARATOR 0x2029
1907 if (layout_byte_offset > 0)
1909 const char *prev = g_utf8_prev_char (text + layout_byte_offset);
1910 ch = g_utf8_get_char (prev);
1911 if (ch == PARAGRAPH_SEPARATOR || ch == '\r' || ch == '\n')
1912 layout_byte_offset = prev - text; /* chop off */
1914 if (ch == '\n' && layout_byte_offset > 0)
1916 /* Possibly chop a CR as well */
1917 prev = g_utf8_prev_char (text + layout_byte_offset);
1919 --layout_byte_offset;
1924 pango_layout_set_text (display->layout, text, layout_byte_offset);
1925 pango_layout_set_attributes (display->layout, attrs);
1927 tmp_list1 = cursor_byte_offsets;
1928 tmp_list2 = cursor_segs;
1931 add_cursor (layout, display, tmp_list2->data,
1932 GPOINTER_TO_INT (tmp_list1->data));
1933 tmp_list1 = tmp_list1->next;
1934 tmp_list2 = tmp_list2->next;
1936 g_slist_free (cursor_byte_offsets);
1937 g_slist_free (cursor_segs);
1939 pango_layout_get_extents (display->layout, NULL, &extents);
1941 display->width = PANGO_PIXELS (extents.width) + display->left_margin + display->right_margin;
1942 display->height += PANGO_PIXELS (extents.height);
1944 /* Free this if we aren't in a loop */
1945 if (layout->wrap_loop_count == 0)
1946 invalidate_cached_style (layout);
1949 pango_attr_list_unref (attrs);
1951 layout->one_display_cache = display;
1954 allocate_child_widgets (layout, display);
1960 gtk_text_layout_free_line_display (GtkTextLayout *layout,
1961 GtkTextLineDisplay *display)
1963 if (display != layout->one_display_cache)
1965 if (display->layout)
1966 g_object_unref (G_OBJECT (display->layout));
1968 if (display->cursors)
1970 g_slist_foreach (display->cursors, (GFunc)g_free, NULL);
1971 g_slist_free (display->cursors);
1973 g_slist_free (display->shaped_objects);
1979 /* Functions to convert iter <=> index for the line of a GtkTextLineDisplay
1980 * taking into account the preedit string and invisible text if necessary.
1983 line_display_iter_to_index (GtkTextLayout *layout,
1984 GtkTextLineDisplay *display,
1985 const GtkTextIter *iter)
1989 g_return_val_if_fail (_gtk_text_iter_get_text_line (iter) == display->line, 0);
1991 index = gtk_text_iter_get_visible_line_index (iter);
1993 if (index >= display->insert_index)
1994 index += layout->preedit_len;
2000 line_display_index_to_iter (GtkTextLayout *layout,
2001 GtkTextLineDisplay *display,
2006 g_return_if_fail (!_gtk_text_line_is_last (display->line,
2007 _gtk_text_buffer_get_btree (layout->buffer)));
2009 if (index >= display->insert_index + layout->preedit_len)
2010 index -= layout->preedit_len;
2011 else if (index > display->insert_index)
2013 index = display->insert_index;
2017 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
2018 iter, display->line, 0);
2020 gtk_text_iter_set_visible_line_index (iter, index);
2022 if (_gtk_text_iter_get_text_line (iter) != display->line)
2024 /* Clamp to end of line - really this clamping should have been done
2025 * before here, maybe in Pango, this is a broken band-aid I think
2027 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
2028 iter, display->line, 0);
2030 if (!gtk_text_iter_ends_line (iter))
2031 gtk_text_iter_forward_to_line_end (iter);
2034 gtk_text_iter_forward_chars (iter, trailing);
2037 /* FIXME: This really doesn't belong in this file ... */
2038 static GtkTextLineData*
2039 gtk_text_line_data_new (GtkTextLayout *layout,
2042 GtkTextLineData *line_data;
2044 line_data = g_new (GtkTextLineData, 1);
2046 line_data->view_id = layout;
2047 line_data->next = NULL;
2048 line_data->width = 0;
2049 line_data->height = 0;
2050 line_data->valid = FALSE;
2056 get_line_at_y (GtkTextLayout *layout,
2063 if (y > layout->height)
2066 *line = _gtk_text_btree_find_line_by_y (_gtk_text_buffer_get_btree (layout->buffer),
2067 layout, y, line_top);
2070 *line = _gtk_text_btree_get_end_iter_line (_gtk_text_buffer_get_btree (layout->buffer));
2074 _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
2080 * gtk_text_layout_get_line_at_y:
2081 * @layout: a #GtkLayout
2082 * @target_iter: the iterator in which the result is stored
2083 * @y: the y positition
2084 * @line_top: location to store the y coordinate of the
2085 * top of the line. (Can by %NULL.)
2087 * Get the iter at the beginning of the line which is displayed
2091 gtk_text_layout_get_line_at_y (GtkTextLayout *layout,
2092 GtkTextIter *target_iter,
2098 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
2099 g_return_if_fail (target_iter != NULL);
2101 get_line_at_y (layout, y, &line, line_top);
2102 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
2103 target_iter, line, 0);
2107 gtk_text_layout_get_iter_at_pixel (GtkTextLayout *layout,
2108 GtkTextIter *target_iter,
2112 gint byte_index, trailing;
2114 GtkTextLineDisplay *display;
2116 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
2117 g_return_if_fail (target_iter != NULL);
2119 /* Adjust pixels to be on-screen. This gives nice
2120 behavior if the user is dragging with a pointer grab.
2124 if (x > layout->width)
2127 get_line_at_y (layout, y, &line, &line_top);
2129 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2131 x -= display->x_offset;
2132 y -= line_top + display->top_margin;
2134 /* We clamp y to the area of the actual layout so that the layouts
2135 * hit testing works OK on the space above and below the layout
2137 y = CLAMP (y, 0, display->height - display->top_margin - display->bottom_margin - 1);
2139 if (!pango_layout_xy_to_index (display->layout, x * PANGO_SCALE, y * PANGO_SCALE,
2140 &byte_index, &trailing))
2142 byte_index = _gtk_text_line_byte_count (line);
2146 line_display_index_to_iter (layout, display, target_iter, byte_index, trailing);
2148 gtk_text_layout_free_line_display (layout, display);
2152 * gtk_text_layout_get_cursor_locations
2153 * @layout: a #GtkTextLayout
2154 * @iter: a #GtkTextIter
2155 * @strong_pos: location to store the strong cursor position (may be %NULL)
2156 * @weak_pos: location to store the weak cursor position (may be %NULL)
2158 * Given an iterator within a text laout, determine the positions that of the
2159 * strong and weak cursors if the insertion point is at that
2160 * iterator. The position of each cursor is stored as a zero-width
2161 * rectangle. The strong cursor location is the location where
2162 * characters of the directionality equal to the base direction of the
2163 * paragraph are inserted. The weak cursor location is the location
2164 * where characters of the directionality opposite to the base
2165 * direction of the paragraph are inserted.
2168 gtk_text_layout_get_cursor_locations (GtkTextLayout *layout,
2170 GdkRectangle *strong_pos,
2171 GdkRectangle *weak_pos)
2174 GtkTextLineDisplay *display;
2178 PangoRectangle pango_strong_pos;
2179 PangoRectangle pango_weak_pos;
2181 g_return_if_fail (layout != NULL);
2182 g_return_if_fail (iter != NULL);
2184 line = _gtk_text_iter_get_text_line (iter);
2185 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2186 index = line_display_iter_to_index (layout, display, iter);
2188 line_top = _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
2191 pango_layout_get_cursor_pos (display->layout, index,
2192 strong_pos ? &pango_strong_pos : NULL,
2193 weak_pos ? &pango_weak_pos : NULL);
2197 strong_pos->x = display->x_offset + pango_strong_pos.x / PANGO_SCALE;
2198 strong_pos->y = line_top + display->top_margin + pango_strong_pos.y / PANGO_SCALE;
2199 strong_pos->width = 0;
2200 strong_pos->height = pango_strong_pos.height / PANGO_SCALE;
2205 weak_pos->x = display->x_offset + pango_weak_pos.x / PANGO_SCALE;
2206 weak_pos->y = line_top + display->top_margin + pango_weak_pos.y / PANGO_SCALE;
2207 weak_pos->width = 0;
2208 weak_pos->height = pango_weak_pos.height / PANGO_SCALE;
2211 gtk_text_layout_free_line_display (layout, display);
2215 * gtk_text_layout_get_line_yrange:
2216 * @layout: a #GtkTextLayout
2217 * @iter: a #GtkTextIter
2218 * @y: location to store the top of the paragraph in pixels,
2220 * @height location to store the height of the paragraph in pixels,
2223 * Find the range of y coordinates for the paragraph containing
2227 gtk_text_layout_get_line_yrange (GtkTextLayout *layout,
2228 const GtkTextIter *iter,
2234 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
2235 g_return_if_fail (_gtk_text_iter_get_btree (iter) == _gtk_text_buffer_get_btree (layout->buffer));
2237 line = _gtk_text_iter_get_text_line (iter);
2240 *y = _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
2244 GtkTextLineData *line_data = _gtk_text_line_get_data (line, layout);
2246 *height = line_data->height;
2253 gtk_text_layout_get_iter_location (GtkTextLayout *layout,
2254 const GtkTextIter *iter,
2257 PangoRectangle pango_rect;
2260 GtkTextLineDisplay *display;
2264 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
2265 g_return_if_fail (_gtk_text_iter_get_btree (iter) == _gtk_text_buffer_get_btree (layout->buffer));
2266 g_return_if_fail (rect != NULL);
2268 tree = _gtk_text_iter_get_btree (iter);
2269 line = _gtk_text_iter_get_text_line (iter);
2271 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2273 rect->y = _gtk_text_btree_find_line_top (tree, line, layout);
2275 x_offset = display->x_offset * PANGO_SCALE;
2277 byte_index = gtk_text_iter_get_line_index (iter);
2279 pango_layout_index_to_pos (display->layout, byte_index, &pango_rect);
2281 rect->x = PANGO_PIXELS (x_offset + pango_rect.x);
2282 rect->y += PANGO_PIXELS (pango_rect.y) + display->top_margin;
2283 rect->width = PANGO_PIXELS (pango_rect.width);
2284 rect->height = PANGO_PIXELS (pango_rect.height);
2286 gtk_text_layout_free_line_display (layout, display);
2291 /* Find the iter for the logical beginning of the first display line whose
2292 * top y is >= y. If none exists, move the iter to the logical beginning
2293 * of the last line in the buffer.
2296 find_display_line_below (GtkTextLayout *layout,
2300 GtkTextLine *line, *next;
2301 GtkTextLine *found_line = NULL;
2303 gint found_byte = 0;
2305 line = _gtk_text_btree_find_line_by_y (_gtk_text_buffer_get_btree (layout->buffer),
2306 layout, y, &line_top);
2310 _gtk_text_btree_get_end_iter_line (_gtk_text_buffer_get_btree (layout->buffer));
2313 _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
2317 while (line && !found_line)
2319 GtkTextLineDisplay *display = gtk_text_layout_get_line_display (layout, line, FALSE);
2320 PangoLayoutIter *layout_iter;
2322 layout_iter = pango_layout_get_iter (display->layout);
2324 line_top += display->top_margin;
2328 gint first_y, last_y;
2329 PangoLayoutLine *layout_line = pango_layout_iter_get_line (layout_iter);
2331 found_byte = layout_line->start_index;
2339 pango_layout_iter_get_line_yrange (layout_iter, &first_y, &last_y);
2340 line_top += (last_y - first_y) / PANGO_SCALE;
2342 while (pango_layout_iter_next_line (layout_iter));
2344 pango_layout_iter_free (layout_iter);
2346 line_top += display->bottom_margin;
2347 gtk_text_layout_free_line_display (layout, display);
2349 next = _gtk_text_line_next_excluding_last (line);
2356 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
2357 iter, found_line, found_byte);
2360 /* Find the iter for the logical beginning of the last display line whose
2361 * top y is >= y. If none exists, move the iter to the logical beginning
2362 * of the first line in the buffer.
2365 find_display_line_above (GtkTextLayout *layout,
2370 GtkTextLine *found_line = NULL;
2372 gint found_byte = 0;
2374 line = _gtk_text_btree_find_line_by_y (_gtk_text_buffer_get_btree (layout->buffer), layout, y, &line_top);
2377 line = _gtk_text_btree_get_end_iter_line (_gtk_text_buffer_get_btree (layout->buffer));
2379 line_top = _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer), line, layout);
2382 while (line && !found_line)
2384 GtkTextLineDisplay *display = gtk_text_layout_get_line_display (layout, line, FALSE);
2385 PangoRectangle logical_rect;
2386 PangoLayoutIter *layout_iter;
2389 layout_iter = pango_layout_get_iter (display->layout);
2391 line_top -= display->top_margin + display->bottom_margin;
2392 pango_layout_iter_get_layout_extents (layout_iter, NULL, &logical_rect);
2393 line_top -= logical_rect.height / PANGO_SCALE;
2395 tmp_top = line_top + display->top_margin;
2399 gint first_y, last_y;
2400 PangoLayoutLine *layout_line = pango_layout_iter_get_line (layout_iter);
2402 found_byte = layout_line->start_index;
2404 pango_layout_iter_get_line_yrange (layout_iter, &first_y, &last_y);
2406 tmp_top -= (last_y - first_y) / PANGO_SCALE;
2414 while (pango_layout_iter_next_line (layout_iter));
2416 pango_layout_iter_free (layout_iter);
2418 gtk_text_layout_free_line_display (layout, display);
2420 line = _gtk_text_line_previous (line);
2426 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
2427 iter, found_line, found_byte);
2429 gtk_text_buffer_get_iter_at_offset (layout->buffer, iter, 0);
2433 * gtk_text_layout_clamp_iter_to_vrange:
2434 * @layout: a #GtkTextLayout
2435 * @iter: a #GtkTextIter
2436 * @top: the top of the range
2437 * @bottom: the bottom the range
2439 * If the iterator is not fully in the range @top <= y < @bottom,
2440 * then, if possible, move it the minimum distance so that the
2441 * iterator in this range.
2443 * Returns: %TRUE if the iterator was moved, otherwise %FALSE.
2446 gtk_text_layout_clamp_iter_to_vrange (GtkTextLayout *layout,
2451 GdkRectangle iter_rect;
2453 gtk_text_layout_get_iter_location (layout, iter, &iter_rect);
2455 /* If the iter is at least partially above the range, put the iter
2456 * at the first fully visible line after the range.
2458 if (iter_rect.y < top)
2460 find_display_line_below (layout, iter, top);
2464 /* Otherwise, if the iter is at least partially below the screen, put the
2465 * iter on the last logical position of the last completely visible
2468 else if (iter_rect.y + iter_rect.height > bottom)
2470 find_display_line_above (layout, iter, bottom);
2479 * gtk_text_layout_move_iter_to_previous_line:
2480 * @layout: a #GtkLayout
2481 * @iter: a #GtkTextIter
2483 * Move the iterator to the beginning of the previous line. The lines
2484 * of a wrapped paragraph are treated as distinct for this operation.
2487 gtk_text_layout_move_iter_to_previous_line (GtkTextLayout *layout,
2491 GtkTextLineDisplay *display;
2494 PangoLayoutLine *layout_line;
2496 gboolean update_byte = FALSE;
2498 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), FALSE);
2499 g_return_val_if_fail (iter != NULL, FALSE);
2504 line = _gtk_text_iter_get_text_line (iter);
2505 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2506 line_byte = line_display_iter_to_index (layout, display, iter);
2508 /* If display->height == 0 then the line is invisible, so don't
2511 while (display->height == 0)
2513 GtkTextLine *prev_line;
2515 prev_line = _gtk_text_line_previous (line);
2517 if (prev_line == NULL)
2519 line_display_index_to_iter (layout, display, iter, 0, 0);
2523 gtk_text_layout_free_line_display (layout, display);
2526 display = gtk_text_layout_get_line_display (layout, prev_line, FALSE);
2530 tmp_list = pango_layout_get_lines (display->layout);
2531 layout_line = tmp_list->data;
2535 line_byte = layout_line->start_index + layout_line->length;
2538 if (line_byte < layout_line->length || !tmp_list->next) /* first line of paragraph */
2540 GtkTextLine *prev_line;
2542 prev_line = _gtk_text_line_previous (line);
2545 gtk_text_layout_free_line_display (layout, display);
2547 display = gtk_text_layout_get_line_display (layout, prev_line, FALSE);
2549 if (display->height > 0)
2551 tmp_list = g_slist_last (pango_layout_get_lines (display->layout));
2552 layout_line = tmp_list->data;
2554 line_display_index_to_iter (layout, display, iter,
2555 layout_line->start_index + layout_line->length, 0);
2559 prev_line = _gtk_text_line_previous (prev_line);
2562 if (prev_line == NULL)
2563 line_display_index_to_iter (layout, display, iter, 0, 0);
2567 gint prev_offset = layout_line->start_index;
2569 tmp_list = tmp_list->next;
2572 layout_line = tmp_list->data;
2574 if (line_byte < layout_line->start_index + layout_line->length ||
2577 line_display_index_to_iter (layout, display, iter, prev_offset, 0);
2581 prev_offset = layout_line->start_index;
2582 tmp_list = tmp_list->next;
2588 gtk_text_layout_free_line_display (layout, display);
2591 !gtk_text_iter_equal (iter, &orig) &&
2592 !gtk_text_iter_is_end (iter);
2596 * gtk_text_layout_move_iter_to_next_line:
2597 * @layout: a #GtkLayout
2598 * @iter: a #GtkTextIter
2600 * Move the iterator to the beginning of the next line. The
2601 * lines of a wrapped paragraph are treated as distinct for
2605 gtk_text_layout_move_iter_to_next_line (GtkTextLayout *layout,
2609 GtkTextLineDisplay *display;
2612 gboolean found = FALSE;
2613 gboolean found_after = FALSE;
2614 gboolean first = TRUE;
2616 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), FALSE);
2617 g_return_val_if_fail (iter != NULL, FALSE);
2621 line = _gtk_text_iter_get_text_line (iter);
2623 while (line && !found_after)
2627 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2629 if (display->height == 0)
2634 line_byte = line_display_iter_to_index (layout, display, iter);
2640 tmp_list = pango_layout_get_lines (display->layout);
2641 while (tmp_list && !found_after)
2643 PangoLayoutLine *layout_line = tmp_list->data;
2647 line_display_index_to_iter (layout, display, iter,
2648 layout_line->start_index, 0);
2651 else if (line_byte < layout_line->start_index + layout_line->length || !tmp_list->next)
2654 tmp_list = tmp_list->next;
2659 gtk_text_layout_free_line_display (layout, display);
2661 line = _gtk_text_line_next_excluding_last (line);
2665 !gtk_text_iter_equal (iter, &orig) &&
2666 !gtk_text_iter_is_end (iter);
2670 * gtk_text_layout_move_iter_to_line_end:
2671 * @layout: a #GtkTextLayout
2672 * @direction: if negative, move to beginning of line, otherwise
2673 move to end of line.
2675 * Move to the beginning or end of a display line.
2678 gtk_text_layout_move_iter_to_line_end (GtkTextLayout *layout,
2683 GtkTextLineDisplay *display;
2688 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), FALSE);
2689 g_return_val_if_fail (iter != NULL, FALSE);
2693 line = _gtk_text_iter_get_text_line (iter);
2694 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2695 line_byte = line_display_iter_to_index (layout, display, iter);
2697 tmp_list = pango_layout_get_lines (display->layout);
2700 PangoLayoutLine *layout_line = tmp_list->data;
2702 if (line_byte < layout_line->start_index + layout_line->length || !tmp_list->next)
2704 line_display_index_to_iter (layout, display, iter,
2705 direction < 0 ? layout_line->start_index : layout_line->start_index + layout_line->length,
2708 /* FIXME: As a bad hack, we move back one position when we
2709 * are inside a paragraph to avoid going to next line on a
2710 * forced break not at whitespace. Real fix is to keep track
2711 * of whether marks are at leading or trailing edge? */
2712 if (direction > 0 && layout_line->length > 0 && !gtk_text_iter_ends_line (iter))
2713 gtk_text_iter_backward_char (iter);
2718 tmp_list = tmp_list->next;
2721 gtk_text_layout_free_line_display (layout, display);
2724 !gtk_text_iter_equal (iter, &orig) &&
2725 !gtk_text_iter_is_end (iter);
2730 * gtk_text_layout_iter_starts_line:
2731 * @layout: a #GtkTextLayout
2732 * @iter: iterator to test
2734 * Tests whether an iterator is at the start of a display line.
2737 gtk_text_layout_iter_starts_line (GtkTextLayout *layout,
2738 const GtkTextIter *iter)
2741 GtkTextLineDisplay *display;
2745 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), FALSE);
2746 g_return_val_if_fail (iter != NULL, FALSE);
2748 line = _gtk_text_iter_get_text_line (iter);
2749 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2750 line_byte = line_display_iter_to_index (layout, display, iter);
2752 tmp_list = pango_layout_get_lines (display->layout);
2755 PangoLayoutLine *layout_line = tmp_list->data;
2757 if (line_byte < layout_line->start_index + layout_line->length ||
2760 /* We're located on this line or the para delimiters before
2763 gtk_text_layout_free_line_display (layout, display);
2765 if (line_byte == layout_line->start_index)
2771 tmp_list = tmp_list->next;
2774 g_assert_not_reached ();
2779 gtk_text_layout_get_iter_at_line (GtkTextLayout *layout,
2784 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
2785 iter, line, byte_offset);
2790 * gtk_text_layout_move_iter_to_x:
2791 * @layout: a #GtkTextLayout
2792 * @iter: a #GtkTextIter
2795 * Keeping the iterator on the same line of the layout, move it to the
2796 * specified X coordinate. The lines of a wrapped paragraph are
2797 * treated as distinct for this operation.
2800 gtk_text_layout_move_iter_to_x (GtkTextLayout *layout,
2805 GtkTextLineDisplay *display;
2807 PangoLayoutIter *layout_iter;
2809 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
2810 g_return_if_fail (iter != NULL);
2812 line = _gtk_text_iter_get_text_line (iter);
2814 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2815 line_byte = line_display_iter_to_index (layout, display, iter);
2817 layout_iter = pango_layout_get_iter (display->layout);
2821 PangoLayoutLine *layout_line = pango_layout_iter_get_line (layout_iter);
2823 if (line_byte < layout_line->start_index + layout_line->length ||
2824 pango_layout_iter_at_last_line (layout_iter))
2826 PangoRectangle logical_rect;
2827 gint byte_index, trailing;
2828 gint x_offset = display->x_offset * PANGO_SCALE;
2830 pango_layout_iter_get_line_extents (layout_iter, NULL, &logical_rect);
2832 pango_layout_line_x_to_index (layout_line,
2833 x * PANGO_SCALE - x_offset - logical_rect.x,
2834 &byte_index, &trailing);
2836 line_display_index_to_iter (layout, display, iter, byte_index, trailing);
2841 while (pango_layout_iter_next_line (layout_iter));
2843 pango_layout_iter_free (layout_iter);
2845 gtk_text_layout_free_line_display (layout, display);
2849 * gtk_text_layout_move_iter_visually:
2850 * @layout: a #GtkTextLayout
2851 * @iter: a #GtkTextIter
2852 * @count: number of characters to move (negative moves left, positive moves right)
2854 * Move the iterator a given number of characters visually, treating
2855 * it as the strong cursor position. If @count is positive, then the
2856 * new strong cursor position will be @count positions to the right of
2857 * the old cursor position. If @count is negative then the new strong
2858 * cursor position will be @count positions to the left of the old
2861 * In the presence of bidirection text, the correspondence
2862 * between logical and visual order will depend on the direction
2863 * of the current run, and there may be jumps when the cursor
2864 * is moved off of the end of a run.
2868 gtk_text_layout_move_iter_visually (GtkTextLayout *layout,
2872 GtkTextLineDisplay *display = NULL;
2875 g_return_val_if_fail (layout != NULL, FALSE);
2876 g_return_val_if_fail (iter != NULL, FALSE);
2882 GtkTextLine *line = _gtk_text_iter_get_text_line (iter);
2884 gint extra_back = 0;
2887 int byte_count = _gtk_text_line_byte_count (line);
2893 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2895 if (layout->cursor_direction == GTK_TEXT_DIR_NONE)
2898 strong = display->direction == layout->cursor_direction;
2900 line_byte = line_display_iter_to_index (layout, display, iter);
2904 pango_layout_move_cursor_visually (display->layout, strong, line_byte, 0, 1, &new_index, &new_trailing);
2909 pango_layout_move_cursor_visually (display->layout, strong, line_byte, 0, -1, &new_index, &new_trailing);
2913 /* We need to handle the preedit string specially. Well, we don't really need to
2914 * handle it specially, since hopefully calling gtk_im_context_reset() will
2915 * remove the preedit string; but if we start off in front of the preedit
2916 * string (logically) and end up in or on the back edge of the preedit string,
2917 * we should move the iter one place farther.
2919 if (layout->preedit_len > 0 && display->insert_index >= 0)
2921 if (line_byte == display->insert_index + layout->preedit_len &&
2922 new_index < display->insert_index + layout->preedit_len)
2924 line_byte = display->insert_index;
2929 if (new_index < 0 || (new_index == 0 && extra_back))
2931 line = _gtk_text_line_previous (line);
2936 gtk_text_layout_free_line_display (layout, display);
2937 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2938 new_index = _gtk_text_line_byte_count (line);
2940 else if (new_index > byte_count)
2942 line = _gtk_text_line_next_excluding_last (line);
2946 gtk_text_layout_free_line_display (layout, display);
2947 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2951 line_display_index_to_iter (layout, display, iter, new_index, new_trailing);
2953 gtk_text_iter_backward_char (iter);
2956 gtk_text_layout_free_line_display (layout, display);
2961 !gtk_text_iter_equal (iter, &orig) &&
2962 !gtk_text_iter_is_end (iter);
2966 gtk_text_layout_spew (GtkTextLayout *layout)
2969 GtkTextDisplayLine *iter;
2971 guint paragraphs = 0;
2972 GtkTextLine *last_line = NULL;
2974 iter = layout->line_list;
2975 while (iter != NULL)
2977 if (iter->line != last_line)
2979 printf ("%5u paragraph (%p)\n", paragraphs, iter->line);
2981 last_line = iter->line;
2984 printf (" %5u y: %d len: %d start: %d bytes: %d\n",
2985 wrapped, iter->y, iter->length, iter->byte_offset,
2992 printf ("Layout %s recompute\n",
2993 layout->need_recompute ? "needs" : "doesn't need");
2995 printf ("Layout pars: %u lines: %u size: %d x %d Screen width: %d\n",
2996 paragraphs, wrapped, layout->width,
2997 layout->height, layout->screen_width);