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_newc ("invalidated",
177 G_OBJECT_CLASS_TYPE (object_class),
179 G_STRUCT_OFFSET (GtkTextLayoutClass, invalidated),
181 gtk_marshal_VOID__VOID,
186 g_signal_newc ("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_newc ("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_line (_gtk_text_buffer_get_btree (layout->buffer),
586 _gtk_text_btree_line_count (_gtk_text_buffer_get_btree (layout->buffer)) - 1,
590 GtkTextLineData *ld = _gtk_text_line_get_data (last_btree_line, layout);
595 g_assert (last_btree_line != NULL);
597 line = first_btree_line;
600 retval = g_slist_prepend (retval, line);
602 if (line == last_btree_line)
605 line = _gtk_text_line_next (line);
608 retval = g_slist_reverse (retval);
614 invalidate_cached_style (GtkTextLayout *layout)
616 free_style_cache (layout);
619 /* These should be called around a loop which wraps a CONTIGUOUS bunch
620 * of display lines. If the lines aren't contiguous you can't call
624 gtk_text_layout_wrap_loop_start (GtkTextLayout *layout)
626 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
627 g_return_if_fail (layout->one_style_cache == NULL);
629 layout->wrap_loop_count += 1;
633 gtk_text_layout_wrap_loop_end (GtkTextLayout *layout)
635 g_return_if_fail (layout->wrap_loop_count > 0);
637 layout->wrap_loop_count -= 1;
639 if (layout->wrap_loop_count == 0)
641 /* We cache a some stuff if we're iterating over some lines wrapping
642 * them. This cleans it up.
644 /* Nuke our cached style */
645 invalidate_cached_style (layout);
646 g_assert (layout->one_style_cache == NULL);
651 gtk_text_layout_invalidate_all (GtkTextLayout *layout)
656 if (layout->buffer == NULL)
659 gtk_text_buffer_get_bounds (layout->buffer, &start, &end);
661 gtk_text_layout_invalidate (layout, &start, &end);
665 gtk_text_layout_invalidate_cache (GtkTextLayout *layout,
668 if (layout->one_display_cache && line == layout->one_display_cache->line)
670 GtkTextLineDisplay *tmp_display = layout->one_display_cache;
671 layout->one_display_cache = NULL;
672 gtk_text_layout_free_line_display (layout, tmp_display);
676 /* Now invalidate the paragraph containing the cursor
679 gtk_text_layout_invalidate_cursor_line (GtkTextLayout *layout)
683 GtkTextLineData *line_data;
685 gtk_text_buffer_get_iter_at_mark (layout->buffer, &iter,
686 gtk_text_buffer_get_mark (layout->buffer, "insert"));
688 line = _gtk_text_iter_get_text_line (&iter);
689 line_data = _gtk_text_line_get_data (line, layout);
692 gtk_text_layout_invalidate_cache (layout, line);
693 _gtk_text_line_invalidate_wrap (line, line_data);
694 gtk_text_layout_invalidated (layout);
699 gtk_text_layout_real_invalidate (GtkTextLayout *layout,
700 const GtkTextIter *start,
701 const GtkTextIter *end)
704 GtkTextLine *last_line;
706 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
707 g_return_if_fail (layout->wrap_loop_count == 0);
710 gtk_text_view_index_spew (start_index, "invalidate start");
711 gtk_text_view_index_spew (end_index, "invalidate end");
714 last_line = _gtk_text_iter_get_text_line (end);
715 line = _gtk_text_iter_get_text_line (start);
719 GtkTextLineData *line_data = _gtk_text_line_get_data (line, layout);
722 (line != last_line || !gtk_text_iter_starts_line (end)))
724 gtk_text_layout_invalidate_cache (layout, line);
725 _gtk_text_line_invalidate_wrap (line, line_data);
728 if (line == last_line)
731 line = _gtk_text_line_next (line);
734 gtk_text_layout_invalidated (layout);
738 gtk_text_layout_real_free_line_data (GtkTextLayout *layout,
740 GtkTextLineData *line_data)
742 if (layout->one_display_cache && line == layout->one_display_cache->line)
744 GtkTextLineDisplay *tmp_display = layout->one_display_cache;
745 layout->one_display_cache = NULL;
746 gtk_text_layout_free_line_display (layout, tmp_display);
755 * gtk_text_layout_is_valid:
756 * @layout: a #GtkTextLayout
758 * Check if there are any invalid regions in a #GtkTextLayout's buffer
760 * Return value: #TRUE if any invalid regions were found
763 gtk_text_layout_is_valid (GtkTextLayout *layout)
765 g_return_val_if_fail (layout != NULL, FALSE);
766 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), FALSE);
768 return _gtk_text_btree_is_valid (_gtk_text_buffer_get_btree (layout->buffer),
773 update_layout_size (GtkTextLayout *layout)
775 _gtk_text_btree_get_view_size (_gtk_text_buffer_get_btree (layout->buffer),
777 &layout->width, &layout->height);
781 * gtk_text_layout_validate_yrange:
782 * @layout: a #GtkTextLayout
783 * @anchor: iter pointing into a line that will be used as the
785 * @y0: offset from the top of the line pointed to by @anchor at
786 * which to begin validation. (The offset here is in pixels
788 * @y1: offset from the top of the line pointed to by @anchor at
789 * which to end validation. (The offset here is in pixels
792 * Ensure that a region of a #GtkTextLayout is valid. The ::changed
793 * signal will be emitted if any lines are validated.
796 gtk_text_layout_validate_yrange (GtkTextLayout *layout,
802 GtkTextLine *first_line = NULL;
803 GtkTextLine *last_line = NULL;
805 gint delta_height = 0;
806 gint first_line_y = 0; /* Quiet GCC */
807 gint last_line_y = 0; /* Quiet GCC */
809 g_return_if_fail (layout != NULL);
810 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
817 /* Validate backwards from the anchor line to y0
819 line = _gtk_text_iter_get_text_line (anchor);
821 while (line && seen < -y0)
823 GtkTextLineData *line_data = _gtk_text_line_get_data (line, layout);
824 if (!line_data || !line_data->valid)
826 gint old_height = line_data ? line_data->height : 0;
828 _gtk_text_btree_validate_line (_gtk_text_buffer_get_btree (layout->buffer),
830 line_data = _gtk_text_line_get_data (line, layout);
832 delta_height += line_data->height - old_height;
835 first_line_y = -seen;
839 last_line_y = -seen + line_data->height;
843 seen += line_data->height;
844 line = _gtk_text_line_previous (line);
847 /* Validate forwards to y1 */
848 line = _gtk_text_iter_get_text_line (anchor);
850 while (line && seen < y1)
852 GtkTextLineData *line_data = _gtk_text_line_get_data (line, layout);
853 if (!line_data || !line_data->valid)
855 gint old_height = line_data ? line_data->height : 0;
857 _gtk_text_btree_validate_line (_gtk_text_buffer_get_btree (layout->buffer),
859 line_data = _gtk_text_line_get_data (line, layout);
861 delta_height += line_data->height - old_height;
869 last_line_y = seen + line_data->height;
872 seen += line_data->height;
873 line = _gtk_text_line_next (line);
876 /* If we found and validated any invalid lines, update size and
877 * emit the changed signal
883 update_layout_size (layout);
885 line_top = _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
888 gtk_text_layout_changed (layout,
890 last_line_y - first_line_y - delta_height,
891 last_line_y - first_line_y);
896 * gtk_text_layout_validate:
897 * @tree: a #GtkTextLayout
898 * @max_pixels: the maximum number of pixels to validate. (No more
899 * than one paragraph beyond this limit will be validated)
901 * Validate regions of a #GtkTextLayout. The ::changed signal will
902 * be emitted for each region validated.
905 gtk_text_layout_validate (GtkTextLayout *layout,
908 gint y, old_height, new_height;
910 g_return_if_fail (layout != NULL);
911 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
913 while (max_pixels > 0 &&
914 _gtk_text_btree_validate (_gtk_text_buffer_get_btree (layout->buffer),
916 &y, &old_height, &new_height))
918 max_pixels -= new_height;
920 update_layout_size (layout);
921 gtk_text_layout_changed (layout, y, old_height, new_height);
925 static GtkTextLineData*
926 gtk_text_layout_real_wrap (GtkTextLayout *layout,
929 GtkTextLineData *line_data)
931 GtkTextLineDisplay *display;
933 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), NULL);
935 if (line_data == NULL)
937 line_data = gtk_text_line_data_new (layout, line);
938 _gtk_text_line_add_data (line, line_data);
941 display = gtk_text_layout_get_line_display (layout, line, TRUE);
942 line_data->width = display->width;
943 line_data->height = display->height;
944 line_data->valid = TRUE;
945 gtk_text_layout_free_line_display (layout, display);
951 * Layout utility functions
954 /* If you get the style with get_style () you need to call
955 release_style () to free it. */
956 static GtkTextAttributes*
957 get_style (GtkTextLayout *layout,
958 const GtkTextIter *iter)
962 GtkTextAttributes *style;
964 /* If we have the one-style cache, then it means
965 that we haven't seen a toggle since we filled in the
968 if (layout->one_style_cache != NULL)
970 gtk_text_attributes_ref (layout->one_style_cache);
971 return layout->one_style_cache;
974 g_assert (layout->one_style_cache == NULL);
976 /* Get the tags at this spot */
977 tags = _gtk_text_btree_get_tags (iter, &tag_count);
979 /* No tags, use default style */
980 if (tags == NULL || tag_count == 0)
982 /* One ref for the return value, one ref for the
983 layout->one_style_cache reference */
984 gtk_text_attributes_ref (layout->default_style);
985 gtk_text_attributes_ref (layout->default_style);
986 layout->one_style_cache = layout->default_style;
991 return layout->default_style;
994 /* Sort tags in ascending order of priority */
995 _gtk_text_tag_array_sort (tags, tag_count);
997 style = gtk_text_attributes_new ();
999 gtk_text_attributes_copy_values (layout->default_style,
1002 _gtk_text_attributes_fill_from_tags (style,
1008 g_assert (style->refcount == 1);
1010 /* Leave this style as the last one seen */
1011 g_assert (layout->one_style_cache == NULL);
1012 gtk_text_attributes_ref (style); /* ref held by layout->one_style_cache */
1013 layout->one_style_cache = style;
1015 /* Returning yet another refcount */
1020 release_style (GtkTextLayout *layout,
1021 GtkTextAttributes *style)
1023 g_return_if_fail (style != NULL);
1024 g_return_if_fail (style->refcount > 0);
1026 gtk_text_attributes_unref (style);
1033 /* This function tries to optimize the case where a line
1034 is completely invisible */
1036 totally_invisible_line (GtkTextLayout *layout,
1040 GtkTextLineSegment *seg;
1043 /* If we have a cached style, then we know it does actually apply
1044 * and we can just see if it is invisible.
1046 if (layout->one_style_cache &&
1047 !layout->one_style_cache->invisible)
1049 /* Without the cache, we check if the first char is visible, if so
1050 * we are partially visible. Note that we have to check this since
1051 * we don't know the current invisible/noninvisible toggle state; this
1052 * function can use the whole btree to get it right.
1056 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
1059 if (!_gtk_text_btree_char_is_invisible (iter))
1064 seg = line->segments;
1068 if (seg->byte_count > 0)
1069 bytes += seg->byte_count;
1071 /* Note that these two tests can cause us to bail out
1072 * when we shouldn't, because a higher-priority tag
1073 * may override these settings. However the important
1074 * thing is to only invisible really-invisible lines, rather
1075 * than to invisible all really-invisible lines.
1078 else if (seg->type == >k_text_toggle_on_type)
1080 invalidate_cached_style (layout);
1082 /* Bail out if an elision-unsetting tag begins */
1083 if (seg->body.toggle.info->tag->invisible_set &&
1084 !seg->body.toggle.info->tag->values->invisible)
1087 else if (seg->type == >k_text_toggle_off_type)
1089 invalidate_cached_style (layout);
1091 /* Bail out if an elision-setting tag ends */
1092 if (seg->body.toggle.info->tag->invisible_set &&
1093 seg->body.toggle.info->tag->values->invisible)
1100 if (seg != NULL) /* didn't reach line end */
1107 set_para_values (GtkTextLayout *layout,
1108 GtkTextAttributes *style,
1109 GtkTextLineDisplay *display,
1112 PangoAlignment pango_align = PANGO_ALIGN_LEFT;
1115 display->direction = style->direction;
1117 if (display->direction == GTK_TEXT_DIR_LTR)
1118 display->layout = pango_layout_new (layout->ltr_context);
1120 display->layout = pango_layout_new (layout->rtl_context);
1122 switch (style->justification)
1124 case GTK_JUSTIFY_LEFT:
1125 pango_align = (style->direction == GTK_TEXT_DIR_LTR) ? PANGO_ALIGN_LEFT : PANGO_ALIGN_RIGHT;
1127 case GTK_JUSTIFY_RIGHT:
1128 pango_align = (style->direction == GTK_TEXT_DIR_LTR) ? PANGO_ALIGN_RIGHT : PANGO_ALIGN_LEFT;
1130 case GTK_JUSTIFY_CENTER:
1131 pango_align = PANGO_ALIGN_CENTER;
1133 case GTK_JUSTIFY_FILL:
1134 g_warning ("FIXME we don't support GTK_JUSTIFY_FILL yet");
1137 g_assert_not_reached ();
1141 switch (pango_align)
1143 case PANGO_ALIGN_LEFT:
1146 case PANGO_ALIGN_RIGHT:
1149 case PANGO_ALIGN_CENTER:
1154 pango_layout_set_alignment (display->layout, pango_align);
1155 pango_layout_set_spacing (display->layout,
1156 style->pixels_inside_wrap * PANGO_SCALE);
1159 pango_layout_set_tabs (display->layout, style->tabs);
1161 display->top_margin = style->pixels_above_lines;
1162 display->height = style->pixels_above_lines + style->pixels_below_lines;
1163 display->bottom_margin = style->pixels_below_lines;
1164 display->left_margin = style->left_margin;
1165 display->right_margin = style->right_margin;
1167 display->x_offset = display->left_margin;
1169 pango_layout_set_indent (display->layout,
1170 style->indent * PANGO_SCALE);
1172 switch (style->wrap_mode)
1175 layout_width = layout->screen_width - display->left_margin - display->right_margin;
1176 pango_layout_set_width (display->layout, layout_width * PANGO_SCALE);
1177 pango_layout_set_wrap (display->layout, PANGO_WRAP_CHAR);
1181 layout_width = layout->screen_width - display->left_margin - display->right_margin;
1182 pango_layout_set_width (display->layout, layout_width * PANGO_SCALE);
1183 pango_layout_set_wrap (display->layout, PANGO_WRAP_WORD);
1190 display->total_width = MAX (layout->screen_width, layout->width) - display->left_margin - display->right_margin;
1193 static PangoAttribute *
1194 gtk_text_attr_appearance_copy (const PangoAttribute *attr)
1196 const GtkTextAttrAppearance *appearance_attr = (const GtkTextAttrAppearance *)attr;
1198 return gtk_text_attr_appearance_new (&appearance_attr->appearance);
1202 gtk_text_attr_appearance_destroy (PangoAttribute *attr)
1204 GtkTextAppearance *appearance = &((GtkTextAttrAppearance *)attr)->appearance;
1206 if (appearance->bg_stipple)
1207 gdk_drawable_unref (appearance->bg_stipple);
1208 if (appearance->fg_stipple)
1209 gdk_drawable_unref (appearance->fg_stipple);
1215 gtk_text_attr_appearance_compare (const PangoAttribute *attr1,
1216 const PangoAttribute *attr2)
1218 const GtkTextAppearance *appearance1 = &((const GtkTextAttrAppearance *)attr1)->appearance;
1219 const GtkTextAppearance *appearance2 = &((const GtkTextAttrAppearance *)attr2)->appearance;
1221 return (gdk_color_equal (&appearance1->fg_color, &appearance2->fg_color) &&
1222 gdk_color_equal (&appearance1->bg_color, &appearance2->bg_color) &&
1223 appearance1->fg_stipple == appearance2->fg_stipple &&
1224 appearance1->bg_stipple == appearance2->bg_stipple &&
1225 appearance1->underline == appearance2->underline &&
1226 appearance1->strikethrough == appearance2->strikethrough &&
1227 appearance1->draw_bg == appearance2->draw_bg);
1232 * gtk_text_attr_appearance_new:
1235 * Create a new font description attribute. (This attribute
1236 * allows setting family, style, weight, variant, stretch,
1237 * and size simultaneously.)
1241 static PangoAttribute *
1242 gtk_text_attr_appearance_new (const GtkTextAppearance *appearance)
1244 static PangoAttrClass klass = {
1246 gtk_text_attr_appearance_copy,
1247 gtk_text_attr_appearance_destroy,
1248 gtk_text_attr_appearance_compare
1251 GtkTextAttrAppearance *result;
1254 klass.type = gtk_text_attr_appearance_type =
1255 pango_attr_type_register ("GtkTextAttrAppearance");
1257 result = g_new (GtkTextAttrAppearance, 1);
1258 result->attr.klass = &klass;
1260 result->appearance = *appearance;
1262 if (appearance->bg_stipple)
1263 gdk_drawable_ref (appearance->bg_stipple);
1264 if (appearance->fg_stipple)
1265 gdk_drawable_ref (appearance->fg_stipple);
1267 return (PangoAttribute *)result;
1272 add_generic_attrs (GtkTextLayout *layout,
1273 GtkTextAppearance *appearance,
1275 PangoAttrList *attrs,
1280 PangoAttribute *attr;
1282 if (appearance->underline != PANGO_UNDERLINE_NONE)
1284 attr = pango_attr_underline_new (appearance->underline);
1286 attr->start_index = start;
1287 attr->end_index = start + byte_count;
1289 pango_attr_list_insert (attrs, attr);
1292 if (appearance->rise != 0)
1294 attr = pango_attr_rise_new (appearance->rise);
1296 attr->start_index = start;
1297 attr->end_index = start + byte_count;
1299 pango_attr_list_insert (attrs, attr);
1304 attr = gtk_text_attr_appearance_new (appearance);
1306 attr->start_index = start;
1307 attr->end_index = start + byte_count;
1309 ((GtkTextAttrAppearance *)attr)->appearance.is_text = is_text;
1311 pango_attr_list_insert (attrs, attr);
1316 add_text_attrs (GtkTextLayout *layout,
1317 GtkTextAttributes *style,
1319 PangoAttrList *attrs,
1323 PangoAttribute *attr;
1325 attr = pango_attr_font_desc_new (&style->font);
1326 attr->start_index = start;
1327 attr->end_index = start + byte_count;
1329 pango_attr_list_insert (attrs, attr);
1331 if (style->font_scale != 1.0)
1333 attr = pango_attr_scale_new (style->font_scale);
1335 attr->start_index = start;
1336 attr->end_index = start + byte_count;
1338 pango_attr_list_insert (attrs, attr);
1343 add_pixbuf_attrs (GtkTextLayout *layout,
1344 GtkTextLineDisplay *display,
1345 GtkTextAttributes *style,
1346 GtkTextLineSegment *seg,
1347 PangoAttrList *attrs,
1350 PangoAttribute *attr;
1351 PangoRectangle logical_rect;
1352 GtkTextPixbuf *pixbuf = &seg->body.pixbuf;
1355 width = gdk_pixbuf_get_width (pixbuf->pixbuf);
1356 height = gdk_pixbuf_get_height (pixbuf->pixbuf);
1359 logical_rect.y = -height * PANGO_SCALE;
1360 logical_rect.width = width * PANGO_SCALE;
1361 logical_rect.height = height * PANGO_SCALE;
1363 attr = pango_attr_shape_new (&logical_rect, &logical_rect);
1364 attr->start_index = start;
1365 attr->end_index = start + seg->byte_count;
1366 pango_attr_list_insert (attrs, attr);
1368 display->shaped_objects =
1369 g_slist_append (display->shaped_objects, pixbuf->pixbuf);
1373 add_child_attrs (GtkTextLayout *layout,
1374 GtkTextLineDisplay *display,
1375 GtkTextAttributes *style,
1376 GtkTextLineSegment *seg,
1377 PangoAttrList *attrs,
1380 PangoAttribute *attr;
1381 PangoRectangle logical_rect;
1382 GtkTextChildAnchor *anchor;
1389 anchor = seg->body.child.obj;
1391 tmp_list = seg->body.child.widgets;
1392 while (tmp_list != NULL)
1394 GtkWidget *child = tmp_list->data;
1396 if (_gtk_anchored_child_get_layout (child) == layout)
1401 gtk_widget_get_child_requisition (child, &req);
1404 height = req.height;
1406 display->shaped_objects =
1407 g_slist_append (display->shaped_objects, child);
1412 tmp_list = g_slist_next (tmp_list);
1415 if (tmp_list == NULL)
1417 /* No widget at this anchor in this display;
1424 if (layout->preedit_string)
1426 g_free (layout->preedit_string);
1427 layout->preedit_string = NULL;
1430 if (layout->preedit_attrs)
1432 pango_attr_list_unref (layout->preedit_attrs);
1433 layout->preedit_attrs = NULL;
1437 logical_rect.y = -height * PANGO_SCALE;
1438 logical_rect.width = width * PANGO_SCALE;
1439 logical_rect.height = height * PANGO_SCALE;
1441 attr = pango_attr_shape_new (&logical_rect, &logical_rect);
1442 attr->start_index = start;
1443 attr->end_index = start + seg->byte_count;
1444 pango_attr_list_insert (attrs, attr);
1448 add_cursor (GtkTextLayout *layout,
1449 GtkTextLineDisplay *display,
1450 GtkTextLineSegment *seg,
1453 PangoRectangle strong_pos, weak_pos;
1454 GtkTextCursorDisplay *cursor = NULL; /* Quiet GCC */
1455 gboolean add_weak = FALSE;
1456 gboolean add_strong = FALSE;
1458 /* Hide insertion cursor when we have a selection or the layout
1459 * user has hidden the cursor.
1461 if (_gtk_text_btree_mark_is_insert (_gtk_text_buffer_get_btree (layout->buffer),
1462 seg->body.mark.obj) &&
1463 (!layout->cursor_visible ||
1464 gtk_text_buffer_get_selection_bounds (layout->buffer, NULL, NULL)))
1467 pango_layout_get_cursor_pos (display->layout, start, &strong_pos, &weak_pos);
1469 if (layout->cursor_direction == GTK_TEXT_DIR_NONE)
1474 else if (display->direction == layout->cursor_direction)
1481 cursor = g_new (GtkTextCursorDisplay, 1);
1483 cursor->x = PANGO_PIXELS (strong_pos.x);
1484 cursor->y = PANGO_PIXELS (strong_pos.y);
1485 cursor->height = PANGO_PIXELS (strong_pos.height);
1486 cursor->is_strong = TRUE;
1487 cursor->is_weak = FALSE;
1488 display->cursors = g_slist_prepend (display->cursors, cursor);
1493 if (weak_pos.x == strong_pos.x && add_strong)
1494 cursor->is_weak = TRUE;
1497 cursor = g_new (GtkTextCursorDisplay, 1);
1499 cursor->x = PANGO_PIXELS (weak_pos.x);
1500 cursor->y = PANGO_PIXELS (weak_pos.y);
1501 cursor->height = PANGO_PIXELS (weak_pos.height);
1502 cursor->is_strong = FALSE;
1503 cursor->is_weak = TRUE;
1504 display->cursors = g_slist_prepend (display->cursors, cursor);
1510 is_shape (PangoLayoutRun *run)
1512 GSList *tmp_list = run->item->analysis.extra_attrs;
1516 PangoAttribute *attr = tmp_list->data;
1518 if (attr->klass->type == PANGO_ATTR_SHAPE)
1521 tmp_list = tmp_list->next;
1528 allocate_child_widgets (GtkTextLayout *text_layout,
1529 GtkTextLineDisplay *display)
1531 GSList *shaped = display->shaped_objects;
1532 PangoLayout *layout = display->layout;
1533 PangoLayoutIter *iter;
1535 iter = pango_layout_get_iter (layout);
1539 PangoLayoutRun *run = pango_layout_iter_get_run (iter);
1541 if (run && is_shape (run))
1543 GObject *shaped_object = shaped->data;
1544 shaped = shaped->next;
1546 if (GTK_IS_WIDGET (shaped_object))
1548 PangoRectangle extents;
1550 /* We emit "allocate_child" with the x,y of
1551 * the widget with respect to the top of the line
1552 * and the left side of the buffer
1555 pango_layout_iter_get_run_extents (iter,
1559 g_signal_emit (G_OBJECT (text_layout),
1560 signals[ALLOCATE_CHILD],
1563 PANGO_PIXELS (extents.x) + display->x_offset,
1564 PANGO_PIXELS (extents.y) + display->top_margin);
1568 while (pango_layout_iter_next_run (iter));
1570 pango_layout_iter_free (iter);
1574 convert_color (GdkColor *result,
1575 PangoAttrColor *attr)
1577 result->red = attr->color.red;
1578 result->blue = attr->color.blue;
1579 result->green = attr->color.green;
1582 /* This function is used to convert the preedit string attributes, which are
1583 * standard PangoAttributes, into the custom attributes used by the text
1584 * widget and insert them into a attr list with a given offset.
1587 add_preedit_attrs (GtkTextLayout *layout,
1588 GtkTextAttributes *style,
1589 PangoAttrList *attrs,
1593 PangoAttrIterator *iter = pango_attr_list_get_iterator (layout->preedit_attrs);
1597 GtkTextAppearance appearance = style->appearance;
1598 PangoFontDescription font_desc;
1599 PangoAttribute *insert_attr;
1600 GSList *extra_attrs = NULL;
1602 PangoLanguage *language;
1605 pango_attr_iterator_range (iter, &start, &end);
1607 if (end == G_MAXINT)
1608 end = layout->preedit_len;
1610 pango_attr_iterator_get_font (iter, &style->font,
1611 &font_desc, &language, &extra_attrs);
1613 tmp_list = extra_attrs;
1616 PangoAttribute *attr = tmp_list->data;
1618 switch (attr->klass->type)
1620 case PANGO_ATTR_FOREGROUND:
1621 convert_color (&appearance.fg_color, (PangoAttrColor *)attr);
1623 case PANGO_ATTR_BACKGROUND:
1624 convert_color (&appearance.bg_color, (PangoAttrColor *)attr);
1625 appearance.draw_bg = TRUE;
1627 case PANGO_ATTR_UNDERLINE:
1628 appearance.underline = ((PangoAttrInt *)attr)->value;
1630 case PANGO_ATTR_STRIKETHROUGH:
1631 appearance.strikethrough = ((PangoAttrInt *)attr)->value;
1633 case PANGO_ATTR_RISE:
1634 appearance.rise = ((PangoAttrInt *)attr)->value;
1640 pango_attribute_destroy (attr);
1641 tmp_list = tmp_list->next;
1644 g_slist_free (extra_attrs);
1646 insert_attr = pango_attr_font_desc_new (&font_desc);
1647 insert_attr->start_index = start + offset;
1648 insert_attr->end_index = end + offset;
1650 pango_attr_list_insert (attrs, insert_attr);
1654 insert_attr = pango_attr_language_new (language);
1655 insert_attr->start_index = start + offset;
1656 insert_attr->end_index = end + offset;
1658 pango_attr_list_insert (attrs, insert_attr);
1661 add_generic_attrs (layout, &appearance, end - start,
1662 attrs, start + offset,
1665 while (pango_attr_iterator_next (iter));
1667 pango_attr_iterator_destroy (iter);
1670 GtkTextLineDisplay *
1671 gtk_text_layout_get_line_display (GtkTextLayout *layout,
1675 GtkTextLineDisplay *display;
1676 GtkTextLineSegment *seg;
1678 GtkTextAttributes *style;
1680 PangoAttrList *attrs;
1681 gint text_allocated, layout_byte_offset, buffer_byte_offset;
1683 PangoRectangle extents;
1684 gboolean para_values_set = FALSE;
1685 GSList *cursor_byte_offsets = NULL;
1686 GSList *cursor_segs = NULL;
1687 GSList *tmp_list1, *tmp_list2;
1688 gboolean saw_widget = FALSE;
1690 g_return_val_if_fail (line != NULL, NULL);
1692 if (layout->one_display_cache)
1694 if (line == layout->one_display_cache->line &&
1695 (size_only || !layout->one_display_cache->size_only))
1696 return layout->one_display_cache;
1699 GtkTextLineDisplay *tmp_display = layout->one_display_cache;
1700 layout->one_display_cache = NULL;
1701 gtk_text_layout_free_line_display (layout, tmp_display);
1705 display = g_new0 (GtkTextLineDisplay, 1);
1707 display->size_only = size_only;
1708 display->line = line;
1709 display->insert_index = -1;
1711 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
1714 /* Special-case optimization for completely
1715 * invisible lines; makes it faster to deal
1716 * with sequences of invisible lines.
1718 if (totally_invisible_line (layout, line, &iter))
1721 /* Allocate space for flat text for buffer
1723 text_allocated = _gtk_text_line_byte_count (line);
1724 text = g_malloc (text_allocated);
1726 attrs = pango_attr_list_new ();
1728 /* Iterate over segments, creating display chunks for them. */
1729 layout_byte_offset = 0; /* current length of layout text (includes preedit, does not include invisible text) */
1730 buffer_byte_offset = 0; /* position in the buffer line */
1731 seg = _gtk_text_iter_get_any_segment (&iter);
1734 /* Displayable segments */
1735 if (seg->type == >k_text_char_type ||
1736 seg->type == >k_text_pixbuf_type ||
1737 seg->type == >k_text_child_type)
1739 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
1741 buffer_byte_offset);
1742 style = get_style (layout, &iter);
1744 /* We have to delay setting the paragraph values until we
1745 * hit the first pixbuf or text segment because toggles at
1746 * the beginning of the paragraph should affect the
1747 * paragraph-global values
1749 if (!para_values_set)
1751 set_para_values (layout, style, display, &align);
1752 para_values_set = TRUE;
1755 /* First see if the chunk is invisible, and ignore it if so. Tk
1756 * looked at tabs, wrap mode, etc. before doing this, but
1757 * that made no sense to me, so I am just skipping the
1760 if (!style->invisible)
1762 if (seg->type == >k_text_char_type)
1764 /* We don't want to split segments because of marks,
1765 * so we scan forward for more segments only
1766 * separated from us by marks. In theory, we should
1767 * also merge segments with identical styles, even
1768 * if there are toggles in-between
1772 GtkTextLineSegment *prev_seg = NULL;
1776 if (seg->type == >k_text_char_type)
1778 memcpy (text + layout_byte_offset, seg->body.chars, seg->byte_count);
1779 layout_byte_offset += seg->byte_count;
1780 buffer_byte_offset += seg->byte_count;
1781 bytes += seg->byte_count;
1783 else if (seg->type == >k_text_right_mark_type ||
1784 seg->type == >k_text_left_mark_type)
1786 /* If we have preedit string, break out of this loop - we'll almost
1787 * certainly have different attributes on the preedit string
1790 if (layout->preedit_len > 0 &&
1791 _gtk_text_btree_mark_is_insert (_gtk_text_buffer_get_btree (layout->buffer),
1792 seg->body.mark.obj))
1795 if (seg->body.mark.visible)
1797 cursor_byte_offsets = g_slist_prepend (cursor_byte_offsets, GINT_TO_POINTER (layout_byte_offset));
1798 cursor_segs = g_slist_prepend (cursor_segs, seg);
1808 seg = prev_seg; /* Back up one */
1809 add_generic_attrs (layout, &style->appearance,
1811 attrs, layout_byte_offset - bytes,
1813 add_text_attrs (layout, style, bytes, attrs,
1814 layout_byte_offset - bytes, size_only);
1816 else if (seg->type == >k_text_pixbuf_type)
1818 add_generic_attrs (layout,
1821 attrs, layout_byte_offset,
1823 add_pixbuf_attrs (layout, display, style,
1824 seg, attrs, layout_byte_offset);
1825 memcpy (text + layout_byte_offset, gtk_text_unknown_char_utf8,
1827 layout_byte_offset += seg->byte_count;
1828 buffer_byte_offset += seg->byte_count;
1830 else if (seg->type == >k_text_child_type)
1834 add_generic_attrs (layout, &style->appearance,
1836 attrs, layout_byte_offset,
1838 add_child_attrs (layout, display, style,
1839 seg, attrs, layout_byte_offset);
1840 memcpy (text + layout_byte_offset, gtk_text_unknown_char_utf8,
1842 layout_byte_offset += seg->byte_count;
1843 buffer_byte_offset += seg->byte_count;
1847 /* We don't know this segment type */
1848 g_assert_not_reached ();
1851 } /* if (segment was visible) */
1854 /* Invisible segment */
1855 buffer_byte_offset += seg->byte_count;
1858 release_style (layout, style);
1862 else if (seg->type == >k_text_toggle_on_type ||
1863 seg->type == >k_text_toggle_off_type)
1865 /* Style may have changed, drop our
1866 current cached style */
1867 invalidate_cached_style (layout);
1871 else if (seg->type == >k_text_right_mark_type ||
1872 seg->type == >k_text_left_mark_type)
1874 gint cursor_offset = 0;
1876 /* At the insertion point, add the preedit string, if any */
1878 if (_gtk_text_btree_mark_is_insert (_gtk_text_buffer_get_btree (layout->buffer),
1879 seg->body.mark.obj))
1881 display->insert_index = layout_byte_offset;
1883 if (layout->preedit_len > 0)
1885 text_allocated += layout->preedit_len;
1886 text = g_realloc (text, text_allocated);
1888 style = get_style (layout, &iter);
1889 add_preedit_attrs (layout, style, attrs, layout_byte_offset, size_only);
1890 release_style (layout, style);
1892 memcpy (text + layout_byte_offset, layout->preedit_string, layout->preedit_len);
1893 layout_byte_offset += layout->preedit_len;
1894 /* DO NOT increment the buffer byte offset for preedit */
1896 cursor_offset = layout->preedit_cursor - layout->preedit_len;
1901 /* Display visible marks */
1903 if (seg->body.mark.visible)
1905 cursor_byte_offsets = g_slist_prepend (cursor_byte_offsets,
1906 GINT_TO_POINTER (layout_byte_offset + cursor_offset));
1907 cursor_segs = g_slist_prepend (cursor_segs, seg);
1912 g_error ("Unknown segment type: %s", seg->type->name);
1917 if (!para_values_set)
1919 style = get_style (layout, &iter);
1920 set_para_values (layout, style, display, &align);
1921 release_style (layout, style);
1924 /* Pango doesn't want the trailing paragraph delimiters */
1927 /* Only one character has type G_UNICODE_PARAGRAPH_SEPARATOR in
1928 * Unicode 3.0; update this if that changes.
1930 #define PARAGRAPH_SEPARATOR 0x2029
1933 if (layout_byte_offset > 0)
1935 const char *prev = g_utf8_prev_char (text + layout_byte_offset);
1936 ch = g_utf8_get_char (prev);
1937 if (ch == PARAGRAPH_SEPARATOR || ch == '\r' || ch == '\n')
1938 layout_byte_offset = prev - text; /* chop off */
1940 if (ch == '\n' && layout_byte_offset > 0)
1942 /* Possibly chop a CR as well */
1943 prev = g_utf8_prev_char (text + layout_byte_offset);
1945 --layout_byte_offset;
1950 pango_layout_set_text (display->layout, text, layout_byte_offset);
1951 pango_layout_set_attributes (display->layout, attrs);
1953 tmp_list1 = cursor_byte_offsets;
1954 tmp_list2 = cursor_segs;
1957 add_cursor (layout, display, tmp_list2->data,
1958 GPOINTER_TO_INT (tmp_list1->data));
1959 tmp_list1 = tmp_list1->next;
1960 tmp_list2 = tmp_list2->next;
1962 g_slist_free (cursor_byte_offsets);
1963 g_slist_free (cursor_segs);
1965 pango_layout_get_extents (display->layout, NULL, &extents);
1967 display->x_offset += (display->total_width - PANGO_PIXELS (extents.x + extents.width)) * align;
1969 display->width = PANGO_PIXELS (extents.width) + display->left_margin + display->right_margin;
1970 display->height += PANGO_PIXELS (extents.height);
1972 /* Free this if we aren't in a loop */
1973 if (layout->wrap_loop_count == 0)
1974 invalidate_cached_style (layout);
1977 pango_attr_list_unref (attrs);
1979 layout->one_display_cache = display;
1982 allocate_child_widgets (layout, display);
1988 gtk_text_layout_free_line_display (GtkTextLayout *layout,
1989 GtkTextLineDisplay *display)
1991 if (display != layout->one_display_cache)
1993 if (display->layout)
1994 g_object_unref (G_OBJECT (display->layout));
1996 if (display->cursors)
1998 g_slist_foreach (display->cursors, (GFunc)g_free, NULL);
1999 g_slist_free (display->cursors);
2001 g_slist_free (display->shaped_objects);
2007 /* Functions to convert iter <=> index for the line of a GtkTextLineDisplay
2008 * taking into account the preedit string and invisible text if necessary.
2011 line_display_iter_to_index (GtkTextLayout *layout,
2012 GtkTextLineDisplay *display,
2013 const GtkTextIter *iter)
2017 g_return_val_if_fail (_gtk_text_iter_get_text_line (iter) == display->line, 0);
2019 index = gtk_text_iter_get_visible_line_index (iter);
2021 if (index >= display->insert_index)
2022 index += layout->preedit_len;
2028 line_display_index_to_iter (GtkTextLayout *layout,
2029 GtkTextLineDisplay *display,
2034 if (index >= display->insert_index + layout->preedit_len)
2035 index -= layout->preedit_len;
2036 else if (index > display->insert_index)
2038 index = display->insert_index;
2042 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
2043 iter, display->line, 0);
2045 gtk_text_iter_set_visible_line_index (iter, index);
2047 if (_gtk_text_iter_get_text_line (iter) != display->line)
2049 /* Clamp to end of line - really this clamping should have been done
2050 * before here, maybe in Pango, this is a broken band-aid I think
2052 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
2053 iter, display->line, 0);
2055 if (!gtk_text_iter_ends_line (iter))
2056 gtk_text_iter_forward_to_line_end (iter);
2059 gtk_text_iter_forward_chars (iter, trailing);
2062 /* FIXME: This really doesn't belong in this file ... */
2063 static GtkTextLineData*
2064 gtk_text_line_data_new (GtkTextLayout *layout,
2067 GtkTextLineData *line_data;
2069 line_data = g_new (GtkTextLineData, 1);
2071 line_data->view_id = layout;
2072 line_data->next = NULL;
2073 line_data->width = 0;
2074 line_data->height = 0;
2075 line_data->valid = FALSE;
2081 get_line_at_y (GtkTextLayout *layout,
2088 if (y > layout->height)
2091 *line = _gtk_text_btree_find_line_by_y (_gtk_text_buffer_get_btree (layout->buffer),
2092 layout, y, line_top);
2095 *line = _gtk_text_btree_get_line (_gtk_text_buffer_get_btree (layout->buffer),
2096 _gtk_text_btree_line_count (_gtk_text_buffer_get_btree (layout->buffer)) - 1, NULL);
2099 _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
2105 * gtk_text_layout_get_line_at_y:
2106 * @layout: a #GtkLayout
2107 * @target_iter: the iterator in which the result is stored
2108 * @y: the y positition
2109 * @line_top: location to store the y coordinate of the
2110 * top of the line. (Can by %NULL.)
2112 * Get the iter at the beginning of the line which is displayed
2116 gtk_text_layout_get_line_at_y (GtkTextLayout *layout,
2117 GtkTextIter *target_iter,
2123 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
2124 g_return_if_fail (target_iter != NULL);
2126 get_line_at_y (layout, y, &line, line_top);
2127 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
2128 target_iter, line, 0);
2132 gtk_text_layout_get_iter_at_pixel (GtkTextLayout *layout,
2133 GtkTextIter *target_iter,
2137 gint byte_index, trailing;
2139 GtkTextLineDisplay *display;
2141 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
2142 g_return_if_fail (target_iter != NULL);
2144 /* Adjust pixels to be on-screen. This gives nice
2145 behavior if the user is dragging with a pointer grab.
2149 if (x > layout->width)
2152 get_line_at_y (layout, y, &line, &line_top);
2154 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2156 x -= display->x_offset;
2157 y -= line_top + display->top_margin;
2159 /* We clamp y to the area of the actual layout so that the layouts
2160 * hit testing works OK on the space above and below the layout
2162 y = CLAMP (y, 0, display->height - display->top_margin - display->bottom_margin - 1);
2164 if (!pango_layout_xy_to_index (display->layout, x * PANGO_SCALE, y * PANGO_SCALE,
2165 &byte_index, &trailing))
2167 byte_index = _gtk_text_line_byte_count (line);
2171 line_display_index_to_iter (layout, display, target_iter, byte_index, trailing);
2173 gtk_text_layout_free_line_display (layout, display);
2177 * gtk_text_layout_get_cursor_locations
2178 * @layout: a #GtkTextLayout
2179 * @iter: a #GtkTextIter
2180 * @strong_pos: location to store the strong cursor position (may be %NULL)
2181 * @weak_pos: location to store the weak cursor position (may be %NULL)
2183 * Given an iterator within a text laout, determine the positions that of the
2184 * strong and weak cursors if the insertion point is at that
2185 * iterator. The position of each cursor is stored as a zero-width
2186 * rectangle. The strong cursor location is the location where
2187 * characters of the directionality equal to the base direction of the
2188 * paragraph are inserted. The weak cursor location is the location
2189 * where characters of the directionality opposite to the base
2190 * direction of the paragraph are inserted.
2193 gtk_text_layout_get_cursor_locations (GtkTextLayout *layout,
2195 GdkRectangle *strong_pos,
2196 GdkRectangle *weak_pos)
2199 GtkTextLineDisplay *display;
2203 PangoRectangle pango_strong_pos;
2204 PangoRectangle pango_weak_pos;
2206 g_return_if_fail (layout != NULL);
2207 g_return_if_fail (iter != NULL);
2209 line = _gtk_text_iter_get_text_line (iter);
2210 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2211 index = line_display_iter_to_index (layout, display, iter);
2213 line_top = _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
2216 pango_layout_get_cursor_pos (display->layout, index,
2217 strong_pos ? &pango_strong_pos : NULL,
2218 weak_pos ? &pango_weak_pos : NULL);
2222 strong_pos->x = display->x_offset + pango_strong_pos.x / PANGO_SCALE;
2223 strong_pos->y = line_top + display->top_margin + pango_strong_pos.y / PANGO_SCALE;
2224 strong_pos->width = 0;
2225 strong_pos->height = pango_strong_pos.height / PANGO_SCALE;
2230 weak_pos->x = display->x_offset + pango_weak_pos.x / PANGO_SCALE;
2231 weak_pos->y = line_top + display->top_margin + pango_weak_pos.y / PANGO_SCALE;
2232 weak_pos->width = 0;
2233 weak_pos->height = pango_weak_pos.height / PANGO_SCALE;
2236 gtk_text_layout_free_line_display (layout, display);
2240 * gtk_text_layout_get_line_yrange:
2241 * @layout: a #GtkTextLayout
2242 * @iter: a #GtkTextIter
2243 * @y: location to store the top of the paragraph in pixels,
2245 * @height location to store the height of the paragraph in pixels,
2248 * Find the range of y coordinates for the paragraph containing
2252 gtk_text_layout_get_line_yrange (GtkTextLayout *layout,
2253 const GtkTextIter *iter,
2259 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
2260 g_return_if_fail (_gtk_text_iter_get_btree (iter) == _gtk_text_buffer_get_btree (layout->buffer));
2262 line = _gtk_text_iter_get_text_line (iter);
2265 *y = _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
2269 GtkTextLineData *line_data = _gtk_text_line_get_data (line, layout);
2271 *height = line_data->height;
2278 gtk_text_layout_get_iter_location (GtkTextLayout *layout,
2279 const GtkTextIter *iter,
2282 PangoRectangle pango_rect;
2285 GtkTextLineDisplay *display;
2289 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
2290 g_return_if_fail (_gtk_text_iter_get_btree (iter) == _gtk_text_buffer_get_btree (layout->buffer));
2291 g_return_if_fail (rect != NULL);
2293 tree = _gtk_text_iter_get_btree (iter);
2294 line = _gtk_text_iter_get_text_line (iter);
2296 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2298 rect->y = _gtk_text_btree_find_line_top (tree, line, layout);
2300 x_offset = display->x_offset * PANGO_SCALE;
2302 byte_index = gtk_text_iter_get_line_index (iter);
2304 pango_layout_index_to_pos (display->layout, byte_index, &pango_rect);
2306 rect->x = PANGO_PIXELS (x_offset + pango_rect.x);
2307 rect->y += PANGO_PIXELS (pango_rect.y) + display->top_margin;
2308 rect->width = PANGO_PIXELS (pango_rect.width);
2309 rect->height = PANGO_PIXELS (pango_rect.height);
2311 gtk_text_layout_free_line_display (layout, display);
2316 /* Find the iter for the logical beginning of the first display line whose
2317 * top y is >= y. If none exists, move the iter to the logical beginning
2318 * of the last line in the buffer.
2321 find_display_line_below (GtkTextLayout *layout,
2325 GtkTextLine *line, *next;
2326 GtkTextLine *found_line = NULL;
2328 gint found_byte = 0;
2330 line = _gtk_text_btree_find_line_by_y (_gtk_text_buffer_get_btree (layout->buffer),
2331 layout, y, &line_top);
2335 _gtk_text_btree_get_line (_gtk_text_buffer_get_btree (layout->buffer),
2336 _gtk_text_btree_line_count (_gtk_text_buffer_get_btree (layout->buffer)) - 1,
2339 _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
2343 while (line && !found_line)
2345 GtkTextLineDisplay *display = gtk_text_layout_get_line_display (layout, line, FALSE);
2346 PangoLayoutIter *layout_iter;
2348 layout_iter = pango_layout_get_iter (display->layout);
2350 line_top += display->top_margin;
2354 gint first_y, last_y;
2355 PangoLayoutLine *layout_line = pango_layout_iter_get_line (layout_iter);
2357 found_byte = layout_line->start_index;
2365 pango_layout_iter_get_line_yrange (layout_iter, &first_y, &last_y);
2366 line_top += (last_y - first_y) / PANGO_SCALE;
2368 while (pango_layout_iter_next_line (layout_iter));
2370 pango_layout_iter_free (layout_iter);
2372 line_top += display->bottom_margin;
2373 gtk_text_layout_free_line_display (layout, display);
2375 next = _gtk_text_line_next (line);
2382 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
2383 iter, found_line, found_byte);
2386 /* Find the iter for the logical beginning of the last display line whose
2387 * top y is >= y. If none exists, move the iter to the logical beginning
2388 * of the first line in the buffer.
2391 find_display_line_above (GtkTextLayout *layout,
2396 GtkTextLine *found_line = NULL;
2398 gint found_byte = 0;
2400 line = _gtk_text_btree_find_line_by_y (_gtk_text_buffer_get_btree (layout->buffer), layout, y, &line_top);
2403 line = _gtk_text_btree_get_line (_gtk_text_buffer_get_btree (layout->buffer),
2404 _gtk_text_btree_line_count (_gtk_text_buffer_get_btree (layout->buffer)) - 1, NULL);
2405 line_top = _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer), line, layout);
2408 while (line && !found_line)
2410 GtkTextLineDisplay *display = gtk_text_layout_get_line_display (layout, line, FALSE);
2411 PangoRectangle logical_rect;
2412 PangoLayoutIter *layout_iter;
2415 layout_iter = pango_layout_get_iter (display->layout);
2417 line_top -= display->top_margin + display->bottom_margin;
2418 pango_layout_iter_get_layout_extents (layout_iter, NULL, &logical_rect);
2419 line_top -= logical_rect.height / PANGO_SCALE;
2421 tmp_top = line_top + display->top_margin;
2425 gint first_y, last_y;
2426 PangoLayoutLine *layout_line = pango_layout_iter_get_line (layout_iter);
2428 found_byte = layout_line->start_index;
2430 pango_layout_iter_get_line_yrange (layout_iter, &first_y, &last_y);
2432 tmp_top -= (last_y - first_y) / PANGO_SCALE;
2440 while (pango_layout_iter_next_line (layout_iter));
2442 pango_layout_iter_free (layout_iter);
2444 gtk_text_layout_free_line_display (layout, display);
2446 line = _gtk_text_line_previous (line);
2452 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
2453 iter, found_line, found_byte);
2455 gtk_text_buffer_get_iter_at_offset (layout->buffer, iter, 0);
2459 * gtk_text_layout_clamp_iter_to_vrange:
2460 * @layout: a #GtkTextLayout
2461 * @iter: a #GtkTextIter
2462 * @top: the top of the range
2463 * @bottom: the bottom the range
2465 * If the iterator is not fully in the range @top <= y < @bottom,
2466 * then, if possible, move it the minimum distance so that the
2467 * iterator in this range.
2469 * Returns: %TRUE if the iterator was moved, otherwise %FALSE.
2472 gtk_text_layout_clamp_iter_to_vrange (GtkTextLayout *layout,
2477 GdkRectangle iter_rect;
2479 gtk_text_layout_get_iter_location (layout, iter, &iter_rect);
2481 /* If the iter is at least partially above the range, put the iter
2482 * at the first fully visible line after the range.
2484 if (iter_rect.y < top)
2486 find_display_line_below (layout, iter, top);
2490 /* Otherwise, if the iter is at least partially below the screen, put the
2491 * iter on the last logical position of the last completely visible
2494 else if (iter_rect.y + iter_rect.height > bottom)
2496 find_display_line_above (layout, iter, bottom);
2505 * gtk_text_layout_move_iter_to_previous_line:
2506 * @layout: a #GtkLayout
2507 * @iter: a #GtkTextIter
2509 * Move the iterator to the beginning of the previous line. The lines
2510 * of a wrapped paragraph are treated as distinct for this operation.
2513 gtk_text_layout_move_iter_to_previous_line (GtkTextLayout *layout,
2517 GtkTextLineDisplay *display;
2520 PangoLayoutLine *layout_line;
2522 gboolean update_byte = FALSE;
2524 g_return_val_if_fail (layout != NULL, FALSE);
2525 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), FALSE);
2526 g_return_val_if_fail (iter != NULL, FALSE);
2531 line = _gtk_text_iter_get_text_line (iter);
2532 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2533 line_byte = line_display_iter_to_index (layout, display, iter);
2535 /* If display->height == 0 then the line is invisible, so don't
2538 while (display->height == 0)
2540 GtkTextLine *prev_line;
2542 prev_line = _gtk_text_line_previous (line);
2544 if (prev_line == NULL)
2546 line_display_index_to_iter (layout, display, iter, 0, 0);
2550 gtk_text_layout_free_line_display (layout, display);
2553 display = gtk_text_layout_get_line_display (layout, prev_line, FALSE);
2557 tmp_list = pango_layout_get_lines (display->layout);
2558 layout_line = tmp_list->data;
2562 line_byte = layout_line->start_index + layout_line->length;
2565 if (line_byte < layout_line->length || !tmp_list->next) /* first line of paragraph */
2567 GtkTextLine *prev_line;
2569 prev_line = _gtk_text_line_previous (line);
2572 gtk_text_layout_free_line_display (layout, display);
2574 display = gtk_text_layout_get_line_display (layout, prev_line, FALSE);
2576 if (display->height > 0)
2578 tmp_list = g_slist_last (pango_layout_get_lines (display->layout));
2579 layout_line = tmp_list->data;
2581 line_display_index_to_iter (layout, display, iter,
2582 layout_line->start_index + layout_line->length, 0);
2586 prev_line = _gtk_text_line_previous (prev_line);
2589 if (prev_line == NULL)
2590 line_display_index_to_iter (layout, display, iter, 0, 0);
2594 gint prev_offset = layout_line->start_index;
2596 tmp_list = tmp_list->next;
2599 layout_line = tmp_list->data;
2601 if (line_byte < layout_line->start_index + layout_line->length ||
2604 line_display_index_to_iter (layout, display, iter, prev_offset, 0);
2608 prev_offset = layout_line->start_index;
2609 tmp_list = tmp_list->next;
2615 gtk_text_layout_free_line_display (layout, display);
2618 !gtk_text_iter_equal (iter, &orig) &&
2619 !gtk_text_iter_is_end (iter);
2623 * gtk_text_layout_move_iter_to_next_line:
2624 * @layout: a #GtkLayout
2625 * @iter: a #GtkTextIter
2627 * Move the iterator to the beginning of the next line. The
2628 * lines of a wrapped paragraph are treated as distinct for
2632 gtk_text_layout_move_iter_to_next_line (GtkTextLayout *layout,
2636 GtkTextLineDisplay *display;
2639 gboolean found = FALSE;
2640 gboolean found_after = FALSE;
2641 gboolean first = TRUE;
2643 g_return_val_if_fail (layout != NULL, FALSE);
2644 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), FALSE);
2645 g_return_val_if_fail (iter != NULL, FALSE);
2649 line = _gtk_text_iter_get_text_line (iter);
2651 while (line && !found_after)
2655 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2657 if (display->height == 0)
2662 line_byte = line_display_iter_to_index (layout, display, iter);
2668 tmp_list = pango_layout_get_lines (display->layout);
2669 while (tmp_list && !found_after)
2671 PangoLayoutLine *layout_line = tmp_list->data;
2675 line_display_index_to_iter (layout, display, iter,
2676 layout_line->start_index, 0);
2679 else if (line_byte < layout_line->start_index + layout_line->length || !tmp_list->next)
2682 tmp_list = tmp_list->next;
2687 gtk_text_layout_free_line_display (layout, display);
2689 line = _gtk_text_line_next (line);
2693 !gtk_text_iter_equal (iter, &orig) &&
2694 !gtk_text_iter_is_end (iter);
2698 * gtk_text_layout_move_iter_to_line_end:
2699 * @layout: a #GtkTextLayout
2700 * @direction: if negative, move to beginning of line, otherwise
2701 move to end of line.
2703 * Move to the beginning or end of a display line.
2706 gtk_text_layout_move_iter_to_line_end (GtkTextLayout *layout,
2711 GtkTextLineDisplay *display;
2716 g_return_val_if_fail (layout != NULL, FALSE);
2717 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), FALSE);
2718 g_return_val_if_fail (iter != NULL, FALSE);
2722 line = _gtk_text_iter_get_text_line (iter);
2723 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2724 line_byte = line_display_iter_to_index (layout, display, iter);
2726 tmp_list = pango_layout_get_lines (display->layout);
2729 PangoLayoutLine *layout_line = tmp_list->data;
2731 if (line_byte < layout_line->start_index + layout_line->length || !tmp_list->next)
2733 line_display_index_to_iter (layout, display, iter,
2734 direction < 0 ? layout_line->start_index : layout_line->start_index + layout_line->length,
2737 /* FIXME: As a bad hack, we move back one position when we
2738 * are inside a paragraph to avoid going to next line on a
2739 * forced break not at whitespace. Real fix is to keep track
2740 * of whether marks are at leading or trailing edge? */
2741 if (direction > 0 && layout_line->length > 0 && !gtk_text_iter_ends_line (iter))
2742 gtk_text_iter_backward_char (iter);
2747 tmp_list = tmp_list->next;
2750 gtk_text_layout_free_line_display (layout, display);
2753 !gtk_text_iter_equal (iter, &orig) &&
2754 !gtk_text_iter_is_end (iter);
2759 * gtk_text_layout_iter_starts_line:
2760 * @layout: a #GtkTextLayout
2761 * @iter: iterator to test
2763 * Tests whether an iterator is at the start of a display line.
2766 gtk_text_layout_iter_starts_line (GtkTextLayout *layout,
2767 const GtkTextIter *iter)
2770 GtkTextLineDisplay *display;
2774 g_return_val_if_fail (layout != NULL, FALSE);
2775 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), FALSE);
2776 g_return_val_if_fail (iter != NULL, FALSE);
2778 line = _gtk_text_iter_get_text_line (iter);
2779 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2780 line_byte = line_display_iter_to_index (layout, display, iter);
2782 tmp_list = pango_layout_get_lines (display->layout);
2785 PangoLayoutLine *layout_line = tmp_list->data;
2787 if (line_byte < layout_line->start_index + layout_line->length ||
2790 /* We're located on this line or the para delimiters before
2793 gtk_text_layout_free_line_display (layout, display);
2795 if (line_byte == layout_line->start_index)
2801 tmp_list = tmp_list->next;
2804 g_assert_not_reached ();
2809 gtk_text_layout_get_iter_at_line (GtkTextLayout *layout,
2814 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
2815 iter, line, byte_offset);
2820 * gtk_text_layout_move_iter_to_x:
2821 * @layout: a #GtkTextLayout
2822 * @iter: a #GtkTextIter
2825 * Keeping the iterator on the same line of the layout, move it to the
2826 * specified X coordinate. The lines of a wrapped paragraph are
2827 * treated as distinct for this operation.
2830 gtk_text_layout_move_iter_to_x (GtkTextLayout *layout,
2835 GtkTextLineDisplay *display;
2837 PangoLayoutIter *layout_iter;
2839 g_return_if_fail (layout != NULL);
2840 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
2841 g_return_if_fail (iter != NULL);
2843 line = _gtk_text_iter_get_text_line (iter);
2845 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2846 line_byte = line_display_iter_to_index (layout, display, iter);
2848 layout_iter = pango_layout_get_iter (display->layout);
2852 PangoLayoutLine *layout_line = pango_layout_iter_get_line (layout_iter);
2854 if (line_byte < layout_line->start_index + layout_line->length ||
2855 pango_layout_iter_at_last_line (layout_iter))
2857 PangoRectangle logical_rect;
2858 gint byte_index, trailing;
2859 gint x_offset = display->x_offset * PANGO_SCALE;
2861 pango_layout_iter_get_line_extents (layout_iter, NULL, &logical_rect);
2863 pango_layout_line_x_to_index (layout_line,
2864 x * PANGO_SCALE - x_offset - logical_rect.x,
2865 &byte_index, &trailing);
2867 line_display_index_to_iter (layout, display, iter, byte_index, trailing);
2872 while (pango_layout_iter_next_line (layout_iter));
2874 pango_layout_iter_free (layout_iter);
2876 gtk_text_layout_free_line_display (layout, display);
2880 * gtk_text_layout_move_iter_visually:
2881 * @layout: a #GtkTextLayout
2882 * @iter: a #GtkTextIter
2883 * @count: number of characters to move (negative moves left, positive moves right)
2885 * Move the iterator a given number of characters visually, treating
2886 * it as the strong cursor position. If @count is positive, then the
2887 * new strong cursor position will be @count positions to the right of
2888 * the old cursor position. If @count is negative then the new strong
2889 * cursor position will be @count positions to the left of the old
2892 * In the presence of bidirection text, the correspondence
2893 * between logical and visual order will depend on the direction
2894 * of the current run, and there may be jumps when the cursor
2895 * is moved off of the end of a run.
2899 gtk_text_layout_move_iter_visually (GtkTextLayout *layout,
2903 GtkTextLineDisplay *display = NULL;
2906 g_return_val_if_fail (layout != NULL, FALSE);
2907 g_return_val_if_fail (iter != NULL, FALSE);
2913 GtkTextLine *line = _gtk_text_iter_get_text_line (iter);
2915 gint extra_back = 0;
2918 int byte_count = _gtk_text_line_byte_count (line);
2924 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2926 if (layout->cursor_direction == GTK_TEXT_DIR_NONE)
2929 strong = display->direction == layout->cursor_direction;
2931 line_byte = line_display_iter_to_index (layout, display, iter);
2935 pango_layout_move_cursor_visually (display->layout, strong, line_byte, 0, 1, &new_index, &new_trailing);
2940 pango_layout_move_cursor_visually (display->layout, strong, line_byte, 0, -1, &new_index, &new_trailing);
2944 /* We need to handle the preedit string specially. Well, we don't really need to
2945 * handle it specially, since hopefully calling gtk_im_context_reset() will
2946 * remove the preedit string; but if we start off in front of the preedit
2947 * string (logically) and end up in or on the back edge of the preedit string,
2948 * we should move the iter one place farther.
2950 if (layout->preedit_len > 0 && display->insert_index >= 0)
2952 if (line_byte == display->insert_index + layout->preedit_len &&
2953 new_index < display->insert_index + layout->preedit_len)
2955 line_byte = display->insert_index;
2960 if (new_index < 0 || (new_index == 0 && extra_back))
2962 line = _gtk_text_line_previous (line);
2967 gtk_text_layout_free_line_display (layout, display);
2968 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2969 new_index = _gtk_text_line_byte_count (line);
2971 else if (new_index > byte_count)
2973 line = _gtk_text_line_next (line);
2977 gtk_text_layout_free_line_display (layout, display);
2978 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2982 line_display_index_to_iter (layout, display, iter, new_index, new_trailing);
2984 gtk_text_iter_backward_char (iter);
2987 gtk_text_layout_free_line_display (layout, display);
2992 !gtk_text_iter_equal (iter, &orig) &&
2993 !gtk_text_iter_is_end (iter);
2997 gtk_text_layout_spew (GtkTextLayout *layout)
3000 GtkTextDisplayLine *iter;
3002 guint paragraphs = 0;
3003 GtkTextLine *last_line = NULL;
3005 iter = layout->line_list;
3006 while (iter != NULL)
3008 if (iter->line != last_line)
3010 printf ("%5u paragraph (%p)\n", paragraphs, iter->line);
3012 last_line = iter->line;
3015 printf (" %5u y: %d len: %d start: %d bytes: %d\n",
3016 wrapped, iter->y, iter->length, iter->byte_offset,
3023 printf ("Layout %s recompute\n",
3024 layout->need_recompute ? "needs" : "doesn't need");
3026 printf ("Layout pars: %u lines: %u size: %d x %d Screen width: %d\n",
3027 paragraphs, wrapped, layout->width,
3028 layout->height, layout->screen_width);