1 /* GTK - The GIMP Toolkit
2 * gtktextlayout.c - calculate the layout of the text
4 * Copyright (c) 1992-1994 The Regents of the University of California.
5 * Copyright (c) 1994-1997 Sun Microsystems, Inc.
6 * Copyright (c) 2000 Red Hat, Inc.
7 * Tk->Gtk port by Havoc Pennington
8 * Pango support by Owen Taylor
10 * This file can be used under your choice of two licenses, the LGPL
11 * and the original Tk license.
15 * This library is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU Lesser General Public
17 * License as published by the Free Software Foundation; either
18 * version 2 of the License, or (at your option) any later version.
20 * This library is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 * Lesser General Public License for more details.
25 * You should have received a copy of the GNU Lesser General Public
26 * License along with this library; if not, write to the Free
27 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 * Original Tk license:
31 * This software is copyrighted by the Regents of the University of
32 * California, Sun Microsystems, Inc., and other parties. The
33 * following terms apply to all files associated with the software
34 * unless explicitly disclaimed in individual files.
36 * The authors hereby grant permission to use, copy, modify,
37 * distribute, and license this software and its documentation for any
38 * purpose, provided that existing copyright notices are retained in
39 * all copies and that this notice is included verbatim in any
40 * distributions. No written agreement, license, or royalty fee is
41 * required for any of the authorized uses. Modifications to this
42 * software may be copyrighted by their authors and need not follow
43 * the licensing terms described here, provided that the new terms are
44 * clearly indicated on the first page of each file where they apply.
46 * IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY
47 * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
48 * DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION,
49 * OR ANY DERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE BEEN ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
52 * THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES,
53 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
54 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND
55 * NON-INFRINGEMENT. THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS,
56 * AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO PROVIDE
57 * MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
59 * GOVERNMENT USE: If you are acquiring this software on behalf of the
60 * U.S. government, the Government shall have only "Restricted Rights"
61 * in the software and related documentation as defined in the Federal
62 * Acquisition Regulations (FARs) in Clause 52.227.19 (c) (2). If you
63 * are acquiring the software on behalf of the Department of Defense,
64 * the software shall be classified as "Commercial Computer Software"
65 * and the Government shall have only "Restricted Rights" as defined
66 * in Clause 252.227-7013 (c) (1) of DFARs. Notwithstanding the
67 * foregoing, the authors grant the U.S. Government and others acting
68 * in its behalf permission to use and distribute the software in
69 * accordance with the terms specified in this license.
73 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
74 * file for a list of people on the GTK+ Team. See the ChangeLog
75 * files for a list of changes. These files are distributed with
76 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
79 #define GTK_TEXT_USE_INTERNAL_UNSUPPORTED_API
82 #include "gtkmarshalers.h"
83 #include "gtktextlayout.h"
84 #include "gtktextbtree.h"
85 #include "gtktextiterprivate.h"
90 #define GTK_TEXT_LAYOUT_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GTK_TYPE_TEXT_LAYOUT, GtkTextLayoutPrivate))
92 typedef struct _GtkTextLayoutPrivate GtkTextLayoutPrivate;
94 struct _GtkTextLayoutPrivate
96 /* Cache the line that the cursor is positioned on, as the keyboard
97 direction only influences the direction of the cursor line.
99 GtkTextLine *cursor_line;
102 static GtkTextLineData *gtk_text_layout_real_wrap (GtkTextLayout *layout,
105 GtkTextLineData *line_data);
107 static void gtk_text_layout_invalidated (GtkTextLayout *layout);
109 static void gtk_text_layout_real_invalidate (GtkTextLayout *layout,
110 const GtkTextIter *start,
111 const GtkTextIter *end);
112 static void gtk_text_layout_invalidate_cache (GtkTextLayout *layout,
114 static void gtk_text_layout_invalidate_cursor_line (GtkTextLayout *layout);
115 static void gtk_text_layout_real_free_line_data (GtkTextLayout *layout,
117 GtkTextLineData *line_data);
118 static void gtk_text_layout_emit_changed (GtkTextLayout *layout,
123 static void gtk_text_layout_invalidate_all (GtkTextLayout *layout);
125 static PangoAttribute *gtk_text_attr_appearance_new (const GtkTextAppearance *appearance);
127 static void gtk_text_layout_mark_set_handler (GtkTextBuffer *buffer,
128 const GtkTextIter *location,
131 static void gtk_text_layout_buffer_insert_text (GtkTextBuffer *textbuffer,
136 static void gtk_text_layout_buffer_delete_range (GtkTextBuffer *textbuffer,
141 static void gtk_text_layout_update_cursor_line (GtkTextLayout *layout);
155 #define PIXEL_BOUND(d) (((d) + PANGO_SCALE - 1) / PANGO_SCALE)
157 static void gtk_text_layout_init (GtkTextLayout *text_layout);
158 static void gtk_text_layout_class_init (GtkTextLayoutClass *klass);
159 static void gtk_text_layout_finalize (GObject *object);
162 static GtkObjectClass *parent_class = NULL;
163 static guint signals[LAST_SIGNAL] = { 0 };
165 PangoAttrType gtk_text_attr_appearance_type = 0;
168 gtk_text_layout_get_type (void)
170 static GType our_type = 0;
174 static const GTypeInfo our_info =
176 sizeof (GtkTextLayoutClass),
177 (GBaseInitFunc) NULL,
178 (GBaseFinalizeFunc) NULL,
179 (GClassInitFunc) gtk_text_layout_class_init,
180 NULL, /* class_finalize */
181 NULL, /* class_data */
182 sizeof (GtkTextLayout),
184 (GInstanceInitFunc) gtk_text_layout_init
187 our_type = g_type_register_static (G_TYPE_OBJECT, "GtkTextLayout",
195 gtk_text_layout_class_init (GtkTextLayoutClass *klass)
197 GObjectClass *object_class = G_OBJECT_CLASS (klass);
199 parent_class = g_type_class_peek_parent (klass);
201 object_class->finalize = gtk_text_layout_finalize;
203 klass->wrap = gtk_text_layout_real_wrap;
204 klass->invalidate = gtk_text_layout_real_invalidate;
205 klass->free_line_data = gtk_text_layout_real_free_line_data;
207 signals[INVALIDATED] =
208 g_signal_new ("invalidated",
209 G_OBJECT_CLASS_TYPE (object_class),
211 G_STRUCT_OFFSET (GtkTextLayoutClass, invalidated),
213 _gtk_marshal_VOID__VOID,
218 g_signal_new ("changed",
219 G_OBJECT_CLASS_TYPE (object_class),
221 G_STRUCT_OFFSET (GtkTextLayoutClass, changed),
223 _gtk_marshal_VOID__INT_INT_INT,
230 signals[ALLOCATE_CHILD] =
231 g_signal_new ("allocate_child",
232 G_OBJECT_CLASS_TYPE (object_class),
234 G_STRUCT_OFFSET (GtkTextLayoutClass, allocate_child),
236 _gtk_marshal_VOID__OBJECT_INT_INT,
243 g_type_class_add_private (object_class, sizeof (GtkTextLayoutPrivate));
247 gtk_text_layout_init (GtkTextLayout *text_layout)
249 text_layout->cursor_visible = TRUE;
253 gtk_text_layout_new (void)
255 return g_object_new (GTK_TYPE_TEXT_LAYOUT, NULL);
259 free_style_cache (GtkTextLayout *text_layout)
261 if (text_layout->one_style_cache)
263 gtk_text_attributes_unref (text_layout->one_style_cache);
264 text_layout->one_style_cache = NULL;
269 gtk_text_layout_finalize (GObject *object)
271 GtkTextLayout *layout;
273 layout = GTK_TEXT_LAYOUT (object);
275 gtk_text_layout_set_buffer (layout, NULL);
277 if (layout->default_style)
278 gtk_text_attributes_unref (layout->default_style);
279 layout->default_style = NULL;
281 if (layout->ltr_context)
283 g_object_unref (layout->ltr_context);
284 layout->ltr_context = NULL;
286 if (layout->rtl_context)
288 g_object_unref (layout->rtl_context);
289 layout->rtl_context = NULL;
292 if (layout->one_display_cache)
294 GtkTextLineDisplay *tmp_display = layout->one_display_cache;
295 layout->one_display_cache = NULL;
296 gtk_text_layout_free_line_display (layout, tmp_display);
299 (* G_OBJECT_CLASS (parent_class)->finalize) (object);
303 gtk_text_layout_set_buffer (GtkTextLayout *layout,
304 GtkTextBuffer *buffer)
306 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
307 g_return_if_fail (buffer == NULL || GTK_IS_TEXT_BUFFER (buffer));
309 if (layout->buffer == buffer)
312 free_style_cache (layout);
316 _gtk_text_btree_remove_view (_gtk_text_buffer_get_btree (layout->buffer),
319 g_signal_handlers_disconnect_by_func (layout->buffer,
320 G_CALLBACK (gtk_text_layout_mark_set_handler),
322 g_signal_handlers_disconnect_by_func (layout->buffer,
323 G_CALLBACK (gtk_text_layout_buffer_insert_text),
325 g_signal_handlers_disconnect_by_func (layout->buffer,
326 G_CALLBACK (gtk_text_layout_buffer_delete_range),
329 g_object_unref (layout->buffer);
330 layout->buffer = NULL;
335 layout->buffer = buffer;
337 g_object_ref (buffer);
339 _gtk_text_btree_add_view (_gtk_text_buffer_get_btree (buffer), layout);
341 /* Bind to all signals that move the insert mark. */
342 g_signal_connect_after (layout->buffer, "mark_set",
343 G_CALLBACK (gtk_text_layout_mark_set_handler), layout);
344 g_signal_connect_after (layout->buffer, "insert_text",
345 G_CALLBACK (gtk_text_layout_buffer_insert_text), layout);
346 g_signal_connect_after (layout->buffer, "delete_range",
347 G_CALLBACK (gtk_text_layout_buffer_delete_range), layout);
349 gtk_text_layout_update_cursor_line (layout);
354 gtk_text_layout_default_style_changed (GtkTextLayout *layout)
356 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
358 DV (g_print ("invalidating all due to default style change (%s)\n", G_STRLOC));
359 gtk_text_layout_invalidate_all (layout);
363 gtk_text_layout_set_default_style (GtkTextLayout *layout,
364 GtkTextAttributes *values)
366 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
367 g_return_if_fail (values != NULL);
369 if (values == layout->default_style)
372 gtk_text_attributes_ref (values);
374 if (layout->default_style)
375 gtk_text_attributes_unref (layout->default_style);
377 layout->default_style = values;
379 gtk_text_layout_default_style_changed (layout);
383 gtk_text_layout_set_contexts (GtkTextLayout *layout,
384 PangoContext *ltr_context,
385 PangoContext *rtl_context)
387 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
389 if (layout->ltr_context)
390 g_object_unref (ltr_context);
392 layout->ltr_context = ltr_context;
393 g_object_ref (ltr_context);
395 if (layout->rtl_context)
396 g_object_unref (rtl_context);
398 layout->rtl_context = rtl_context;
399 g_object_ref (rtl_context);
401 DV (g_print ("invalidating all due to new pango contexts (%s)\n", G_STRLOC));
402 gtk_text_layout_invalidate_all (layout);
406 * gtk_text_layout_set_cursor_direction:
407 * @direction: the new direction(s) for which to draw cursors.
408 * %GTK_TEXT_DIR_NONE means draw cursors for both
409 * left-to-right insertion and right-to-left insertion.
410 * (The two cursors will be visually distinguished.)
412 * Sets which text directions (left-to-right and/or right-to-left) for
413 * which cursors will be drawn for the insertion point. The visual
414 * point at which new text is inserted depends on whether the new
415 * text is right-to-left or left-to-right, so it may be desired to
416 * make the drawn position of the cursor depend on the keyboard state.
419 gtk_text_layout_set_cursor_direction (GtkTextLayout *layout,
420 GtkTextDirection direction)
422 if (direction != layout->cursor_direction)
424 layout->cursor_direction = direction;
425 gtk_text_layout_invalidate_cursor_line (layout);
430 * gtk_text_layout_set_keyboard_direction:
431 * @keyboard_dir: the current direction of the keyboard.
433 * Sets the keyboard direction; this is used as for the bidirectional
434 * base direction for the line with the cursor if the line contains
435 * only neutral characters.
438 gtk_text_layout_set_keyboard_direction (GtkTextLayout *layout,
439 GtkTextDirection keyboard_dir)
441 if (keyboard_dir != layout->keyboard_direction)
443 layout->keyboard_direction = keyboard_dir;
444 gtk_text_layout_invalidate_cursor_line (layout);
449 * gtk_text_layout_get_buffer:
450 * @layout: a #GtkTextLayout
452 * Gets the text buffer used by the layout. See
453 * gtk_text_layout_set_buffer().
455 * Return value: the text buffer used by the layout.
458 gtk_text_layout_get_buffer (GtkTextLayout *layout)
460 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), NULL);
462 return layout->buffer;
466 gtk_text_layout_set_screen_width (GtkTextLayout *layout, gint width)
468 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
469 g_return_if_fail (width >= 0);
470 g_return_if_fail (layout->wrap_loop_count == 0);
472 if (layout->screen_width == width)
475 layout->screen_width = width;
477 DV (g_print ("invalidating all due to new screen width (%s)\n", G_STRLOC));
478 gtk_text_layout_invalidate_all (layout);
482 * gtk_text_layout_set_cursor_visible:
483 * @layout: a #GtkTextLayout
484 * @cursor_visible: If %FALSE, then the insertion cursor will not
485 * be shown, even if the text is editable.
487 * Sets whether the insertion cursor should be shown. Generally,
488 * widgets using #GtkTextLayout will hide the cursor when the
489 * widget does not have the input focus.
492 gtk_text_layout_set_cursor_visible (GtkTextLayout *layout,
493 gboolean cursor_visible)
495 cursor_visible = (cursor_visible != FALSE);
497 if (layout->cursor_visible != cursor_visible)
502 layout->cursor_visible = cursor_visible;
504 /* Now queue a redraw on the paragraph containing the cursor
506 gtk_text_buffer_get_iter_at_mark (layout->buffer, &iter,
507 gtk_text_buffer_get_mark (layout->buffer, "insert"));
509 gtk_text_layout_get_line_yrange (layout, &iter, &y, &height);
510 gtk_text_layout_emit_changed (layout, y, height, height);
512 gtk_text_layout_invalidate_cache (layout, _gtk_text_iter_get_text_line (&iter));
517 * gtk_text_layout_get_cursor_visible:
518 * @layout: a #GtkTextLayout
520 * Returns whether the insertion cursor will be shown.
522 * Return value: if %FALSE, the insertion cursor will not be
523 shown, even if the text is editable.
526 gtk_text_layout_get_cursor_visible (GtkTextLayout *layout)
528 return layout->cursor_visible;
532 * gtk_text_layout_set_preedit_string:
533 * @layout: a #PangoLayout
534 * @preedit_string: a string to display at the insertion point
535 * @preedit_attrs: a #PangoAttrList of attributes that apply to @preedit_string
536 * @cursor_pos: position of cursor within preedit string in chars
538 * Set the preedit string and attributes. The preedit string is a
539 * string showing text that is currently being edited and not
540 * yet committed into the buffer.
543 gtk_text_layout_set_preedit_string (GtkTextLayout *layout,
544 const gchar *preedit_string,
545 PangoAttrList *preedit_attrs,
548 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
549 g_return_if_fail (preedit_attrs != NULL || preedit_string == NULL);
551 if (layout->preedit_string)
552 g_free (layout->preedit_string);
554 if (layout->preedit_attrs)
555 pango_attr_list_unref (layout->preedit_attrs);
559 layout->preedit_string = g_strdup (preedit_string);
560 layout->preedit_len = strlen (layout->preedit_string);
561 pango_attr_list_ref (preedit_attrs);
562 layout->preedit_attrs = preedit_attrs;
564 cursor_pos = CLAMP (cursor_pos, 0, g_utf8_strlen (layout->preedit_string, -1));
565 layout->preedit_cursor = g_utf8_offset_to_pointer (layout->preedit_string, cursor_pos) - layout->preedit_string;
569 layout->preedit_string = NULL;
570 layout->preedit_len = 0;
571 layout->preedit_attrs = NULL;
572 layout->preedit_cursor = 0;
575 gtk_text_layout_invalidate_cursor_line (layout);
579 gtk_text_layout_get_size (GtkTextLayout *layout,
583 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
586 *width = layout->width;
589 *height = layout->height;
593 gtk_text_layout_invalidated (GtkTextLayout *layout)
595 g_signal_emit (layout, signals[INVALIDATED], 0);
599 gtk_text_layout_emit_changed (GtkTextLayout *layout,
604 g_signal_emit (layout, signals[CHANGED], 0, y, old_height, new_height);
608 gtk_text_layout_changed (GtkTextLayout *layout,
613 /* Check if the range intersects our cached line display,
614 * and invalidate the cached line if so.
616 if (layout->one_display_cache)
618 GtkTextLine *line = layout->one_display_cache->line;
619 gint cache_y = _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
621 gint cache_height = layout->one_display_cache->height;
623 if (cache_y + cache_height > y && cache_y < y + old_height)
624 gtk_text_layout_invalidate_cache (layout, line);
627 gtk_text_layout_emit_changed (layout, y, old_height, new_height);
631 gtk_text_layout_free_line_data (GtkTextLayout *layout,
633 GtkTextLineData *line_data)
635 (* GTK_TEXT_LAYOUT_GET_CLASS (layout)->free_line_data)
636 (layout, line, line_data);
640 gtk_text_layout_invalidate (GtkTextLayout *layout,
641 const GtkTextIter *start_index,
642 const GtkTextIter *end_index)
644 (* GTK_TEXT_LAYOUT_GET_CLASS (layout)->invalidate)
645 (layout, start_index, end_index);
649 gtk_text_layout_wrap (GtkTextLayout *layout,
652 GtkTextLineData *line_data)
654 return (* GTK_TEXT_LAYOUT_GET_CLASS (layout)->wrap) (layout, line, line_data);
658 gtk_text_layout_get_lines (GtkTextLayout *layout,
659 /* [top_y, bottom_y) */
664 GtkTextLine *first_btree_line;
665 GtkTextLine *last_btree_line;
669 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), NULL);
670 g_return_val_if_fail (bottom_y > top_y, NULL);
675 _gtk_text_btree_find_line_by_y (_gtk_text_buffer_get_btree (layout->buffer),
676 layout, top_y, first_line_y);
677 if (first_btree_line == NULL)
683 /* -1 since bottom_y is one past */
685 _gtk_text_btree_find_line_by_y (_gtk_text_buffer_get_btree (layout->buffer),
686 layout, bottom_y - 1, NULL);
688 if (!last_btree_line)
690 _gtk_text_btree_get_end_iter_line (_gtk_text_buffer_get_btree (layout->buffer));
692 g_assert (last_btree_line != NULL);
694 line = first_btree_line;
697 retval = g_slist_prepend (retval, line);
699 if (line == last_btree_line)
702 line = _gtk_text_line_next_excluding_last (line);
705 retval = g_slist_reverse (retval);
711 invalidate_cached_style (GtkTextLayout *layout)
713 free_style_cache (layout);
716 /* These should be called around a loop which wraps a CONTIGUOUS bunch
717 * of display lines. If the lines aren't contiguous you can't call
721 gtk_text_layout_wrap_loop_start (GtkTextLayout *layout)
723 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
724 g_return_if_fail (layout->one_style_cache == NULL);
726 layout->wrap_loop_count += 1;
730 gtk_text_layout_wrap_loop_end (GtkTextLayout *layout)
732 g_return_if_fail (layout->wrap_loop_count > 0);
734 layout->wrap_loop_count -= 1;
736 if (layout->wrap_loop_count == 0)
738 /* We cache a some stuff if we're iterating over some lines wrapping
739 * them. This cleans it up.
741 /* Nuke our cached style */
742 invalidate_cached_style (layout);
743 g_assert (layout->one_style_cache == NULL);
748 gtk_text_layout_invalidate_all (GtkTextLayout *layout)
753 if (layout->buffer == NULL)
756 gtk_text_buffer_get_bounds (layout->buffer, &start, &end);
758 gtk_text_layout_invalidate (layout, &start, &end);
762 gtk_text_layout_invalidate_cache (GtkTextLayout *layout,
765 if (layout->one_display_cache && line == layout->one_display_cache->line)
767 GtkTextLineDisplay *tmp_display = layout->one_display_cache;
768 layout->one_display_cache = NULL;
769 gtk_text_layout_free_line_display (layout, tmp_display);
773 /* Now invalidate the paragraph containing the cursor
776 gtk_text_layout_invalidate_cursor_line (GtkTextLayout *layout)
778 GtkTextLayoutPrivate *priv = GTK_TEXT_LAYOUT_GET_PRIVATE (layout);
779 GtkTextLineData *line_data;
781 if (priv->cursor_line == NULL)
784 line_data = _gtk_text_line_get_data (priv->cursor_line, layout);
787 gtk_text_layout_invalidate_cache (layout, priv->cursor_line);
788 _gtk_text_line_invalidate_wrap (priv->cursor_line, line_data);
789 gtk_text_layout_invalidated (layout);
794 gtk_text_layout_update_cursor_line(GtkTextLayout *layout)
796 GtkTextLayoutPrivate *priv = GTK_TEXT_LAYOUT_GET_PRIVATE (layout);
799 gtk_text_buffer_get_iter_at_mark (layout->buffer, &iter,
800 gtk_text_buffer_get_mark (layout->buffer, "insert"));
802 priv->cursor_line = _gtk_text_iter_get_text_line (&iter);
806 gtk_text_layout_real_invalidate (GtkTextLayout *layout,
807 const GtkTextIter *start,
808 const GtkTextIter *end)
811 GtkTextLine *last_line;
813 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
814 g_return_if_fail (layout->wrap_loop_count == 0);
816 /* Because we may be invalidating a mark, it's entirely possible
817 * that gtk_text_iter_equal (start, end) in which case we
818 * should still invalidate the line they are both on. i.e.
819 * we always invalidate the line with "start" even
820 * if there's an empty range.
824 gtk_text_view_index_spew (start_index, "invalidate start");
825 gtk_text_view_index_spew (end_index, "invalidate end");
828 last_line = _gtk_text_iter_get_text_line (end);
829 line = _gtk_text_iter_get_text_line (start);
833 GtkTextLineData *line_data = _gtk_text_line_get_data (line, layout);
835 gtk_text_layout_invalidate_cache (layout, line);
838 _gtk_text_line_invalidate_wrap (line, line_data);
840 if (line == last_line)
843 line = _gtk_text_line_next_excluding_last (line);
846 gtk_text_layout_invalidated (layout);
850 gtk_text_layout_real_free_line_data (GtkTextLayout *layout,
852 GtkTextLineData *line_data)
854 gtk_text_layout_invalidate_cache (layout, line);
862 * gtk_text_layout_is_valid:
863 * @layout: a #GtkTextLayout
865 * Check if there are any invalid regions in a #GtkTextLayout's buffer
867 * Return value: %TRUE if any invalid regions were found
870 gtk_text_layout_is_valid (GtkTextLayout *layout)
872 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), FALSE);
874 return _gtk_text_btree_is_valid (_gtk_text_buffer_get_btree (layout->buffer),
879 update_layout_size (GtkTextLayout *layout)
881 _gtk_text_btree_get_view_size (_gtk_text_buffer_get_btree (layout->buffer),
883 &layout->width, &layout->height);
887 * gtk_text_layout_validate_yrange:
888 * @layout: a #GtkTextLayout
889 * @anchor: iter pointing into a line that will be used as the
891 * @y0_: offset from the top of the line pointed to by @anchor at
892 * which to begin validation. (The offset here is in pixels
894 * @y1_: offset from the top of the line pointed to by @anchor at
895 * which to end validation. (The offset here is in pixels
898 * Ensure that a region of a #GtkTextLayout is valid. The ::changed
899 * signal will be emitted if any lines are validated.
902 gtk_text_layout_validate_yrange (GtkTextLayout *layout,
908 GtkTextLine *first_line = NULL;
909 GtkTextLine *last_line = NULL;
911 gint delta_height = 0;
912 gint first_line_y = 0; /* Quiet GCC */
913 gint last_line_y = 0; /* Quiet GCC */
915 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
922 /* Validate backwards from the anchor line to y0
924 line = _gtk_text_iter_get_text_line (anchor);
925 line = _gtk_text_line_previous (line);
927 while (line && seen < -y0)
929 GtkTextLineData *line_data = _gtk_text_line_get_data (line, layout);
930 if (!line_data || !line_data->valid)
932 gint old_height = line_data ? line_data->height : 0;
934 _gtk_text_btree_validate_line (_gtk_text_buffer_get_btree (layout->buffer),
936 line_data = _gtk_text_line_get_data (line, layout);
938 delta_height += line_data->height - old_height;
941 first_line_y = -seen - line_data->height;
949 seen += line_data->height;
950 line = _gtk_text_line_previous (line);
953 /* Validate forwards to y1 */
954 line = _gtk_text_iter_get_text_line (anchor);
956 while (line && seen < y1)
958 GtkTextLineData *line_data = _gtk_text_line_get_data (line, layout);
959 if (!line_data || !line_data->valid)
961 gint old_height = line_data ? line_data->height : 0;
963 _gtk_text_btree_validate_line (_gtk_text_buffer_get_btree (layout->buffer),
965 line_data = _gtk_text_line_get_data (line, layout);
967 delta_height += line_data->height - old_height;
975 last_line_y = seen + line_data->height;
978 seen += line_data->height;
979 line = _gtk_text_line_next_excluding_last (line);
982 /* If we found and validated any invalid lines, update size and
983 * emit the changed signal
989 update_layout_size (layout);
991 line_top = _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
994 gtk_text_layout_emit_changed (layout,
996 last_line_y - first_line_y - delta_height,
997 last_line_y - first_line_y);
1002 * gtk_text_layout_validate:
1003 * @tree: a #GtkTextLayout
1004 * @max_pixels: the maximum number of pixels to validate. (No more
1005 * than one paragraph beyond this limit will be validated)
1007 * Validate regions of a #GtkTextLayout. The ::changed signal will
1008 * be emitted for each region validated.
1011 gtk_text_layout_validate (GtkTextLayout *layout,
1014 gint y, old_height, new_height;
1016 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
1018 while (max_pixels > 0 &&
1019 _gtk_text_btree_validate (_gtk_text_buffer_get_btree (layout->buffer),
1021 &y, &old_height, &new_height))
1023 max_pixels -= new_height;
1025 update_layout_size (layout);
1026 gtk_text_layout_emit_changed (layout, y, old_height, new_height);
1030 static GtkTextLineData*
1031 gtk_text_layout_real_wrap (GtkTextLayout *layout,
1034 GtkTextLineData *line_data)
1036 GtkTextLineDisplay *display;
1038 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), NULL);
1039 g_return_val_if_fail (line != NULL, NULL);
1041 if (line_data == NULL)
1043 line_data = _gtk_text_line_data_new (layout, line);
1044 _gtk_text_line_add_data (line, line_data);
1047 display = gtk_text_layout_get_line_display (layout, line, TRUE);
1048 line_data->width = display->width;
1049 line_data->height = display->height;
1050 line_data->valid = TRUE;
1051 gtk_text_layout_free_line_display (layout, display);
1057 * Layout utility functions
1060 /* If you get the style with get_style () you need to call
1061 release_style () to free it. */
1062 static GtkTextAttributes*
1063 get_style (GtkTextLayout *layout,
1064 const GtkTextIter *iter)
1068 GtkTextAttributes *style;
1070 /* If we have the one-style cache, then it means
1071 that we haven't seen a toggle since we filled in the
1074 if (layout->one_style_cache != NULL)
1076 gtk_text_attributes_ref (layout->one_style_cache);
1077 return layout->one_style_cache;
1080 g_assert (layout->one_style_cache == NULL);
1082 /* Get the tags at this spot */
1083 tags = _gtk_text_btree_get_tags (iter, &tag_count);
1085 /* No tags, use default style */
1086 if (tags == NULL || tag_count == 0)
1088 /* One ref for the return value, one ref for the
1089 layout->one_style_cache reference */
1090 gtk_text_attributes_ref (layout->default_style);
1091 gtk_text_attributes_ref (layout->default_style);
1092 layout->one_style_cache = layout->default_style;
1097 return layout->default_style;
1100 /* Sort tags in ascending order of priority */
1101 _gtk_text_tag_array_sort (tags, tag_count);
1103 style = gtk_text_attributes_new ();
1105 gtk_text_attributes_copy_values (layout->default_style,
1108 _gtk_text_attributes_fill_from_tags (style,
1114 g_assert (style->refcount == 1);
1116 /* Leave this style as the last one seen */
1117 g_assert (layout->one_style_cache == NULL);
1118 gtk_text_attributes_ref (style); /* ref held by layout->one_style_cache */
1119 layout->one_style_cache = style;
1121 /* Returning yet another refcount */
1126 release_style (GtkTextLayout *layout,
1127 GtkTextAttributes *style)
1129 g_return_if_fail (style != NULL);
1130 g_return_if_fail (style->refcount > 0);
1132 gtk_text_attributes_unref (style);
1139 /* This function tries to optimize the case where a line
1140 is completely invisible */
1142 totally_invisible_line (GtkTextLayout *layout,
1146 GtkTextLineSegment *seg;
1149 /* Check if the first char is visible, if so we are partially visible.
1150 * Note that we have to check this since we don't know the current
1151 * invisible/noninvisible toggle state; this function can use the whole btree
1154 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
1157 if (!_gtk_text_btree_char_is_invisible (iter))
1161 seg = line->segments;
1165 if (seg->byte_count > 0)
1166 bytes += seg->byte_count;
1168 /* Note that these two tests can cause us to bail out
1169 * when we shouldn't, because a higher-priority tag
1170 * may override these settings. However the important
1171 * thing is to only invisible really-invisible lines, rather
1172 * than to invisible all really-invisible lines.
1175 else if (seg->type == >k_text_toggle_on_type)
1177 invalidate_cached_style (layout);
1179 /* Bail out if an elision-unsetting tag begins */
1180 if (seg->body.toggle.info->tag->invisible_set &&
1181 !seg->body.toggle.info->tag->values->invisible)
1184 else if (seg->type == >k_text_toggle_off_type)
1186 invalidate_cached_style (layout);
1188 /* Bail out if an elision-setting tag ends */
1189 if (seg->body.toggle.info->tag->invisible_set &&
1190 seg->body.toggle.info->tag->values->invisible)
1197 if (seg != NULL) /* didn't reach line end */
1204 set_para_values (GtkTextLayout *layout,
1205 PangoDirection base_dir,
1206 GtkTextAttributes *style,
1207 GtkTextLineDisplay *display)
1209 PangoAlignment pango_align = PANGO_ALIGN_LEFT;
1214 /* If no base direction was found, then use the style direction */
1215 case PANGO_DIRECTION_NEUTRAL :
1216 display->direction = style->direction;
1218 /* Override the base direction */
1219 if (display->direction == GTK_TEXT_DIR_RTL)
1220 base_dir = PANGO_DIRECTION_RTL;
1222 base_dir = PANGO_DIRECTION_LTR;
1225 case PANGO_DIRECTION_RTL :
1226 display->direction = GTK_TEXT_DIR_RTL;
1229 display->direction = GTK_TEXT_DIR_LTR;
1233 if (display->direction == GTK_TEXT_DIR_RTL)
1234 display->layout = pango_layout_new (layout->rtl_context);
1236 display->layout = pango_layout_new (layout->ltr_context);
1238 switch (style->justification)
1240 case GTK_JUSTIFY_LEFT:
1241 pango_align = (base_dir == PANGO_DIRECTION_LTR) ? PANGO_ALIGN_LEFT : PANGO_ALIGN_RIGHT;
1243 case GTK_JUSTIFY_RIGHT:
1244 pango_align = (base_dir == PANGO_DIRECTION_LTR) ? PANGO_ALIGN_RIGHT : PANGO_ALIGN_LEFT;
1246 case GTK_JUSTIFY_CENTER:
1247 pango_align = PANGO_ALIGN_CENTER;
1249 case GTK_JUSTIFY_FILL:
1250 g_warning ("FIXME we don't support GTK_JUSTIFY_FILL yet");
1253 g_assert_not_reached ();
1257 pango_layout_set_alignment (display->layout, pango_align);
1258 pango_layout_set_spacing (display->layout,
1259 style->pixels_inside_wrap * PANGO_SCALE);
1262 pango_layout_set_tabs (display->layout, style->tabs);
1264 display->top_margin = style->pixels_above_lines;
1265 display->height = style->pixels_above_lines + style->pixels_below_lines;
1266 display->bottom_margin = style->pixels_below_lines;
1267 display->left_margin = style->left_margin;
1268 display->right_margin = style->right_margin;
1270 display->x_offset = display->left_margin;
1272 pango_layout_set_indent (display->layout,
1273 style->indent * PANGO_SCALE);
1275 switch (style->wrap_mode)
1278 layout_width = layout->screen_width - display->left_margin - display->right_margin;
1279 pango_layout_set_width (display->layout, layout_width * PANGO_SCALE);
1280 pango_layout_set_wrap (display->layout, PANGO_WRAP_CHAR);
1284 layout_width = layout->screen_width - display->left_margin - display->right_margin;
1285 pango_layout_set_width (display->layout, layout_width * PANGO_SCALE);
1286 pango_layout_set_wrap (display->layout, PANGO_WRAP_WORD);
1289 case GTK_WRAP_WORD_CHAR:
1290 layout_width = layout->screen_width - display->left_margin - display->right_margin;
1291 pango_layout_set_width (display->layout, layout_width * PANGO_SCALE);
1292 pango_layout_set_wrap (display->layout, PANGO_WRAP_WORD_CHAR);
1299 display->total_width = MAX (layout->screen_width, layout->width) - display->left_margin - display->right_margin;
1302 static PangoAttribute *
1303 gtk_text_attr_appearance_copy (const PangoAttribute *attr)
1305 const GtkTextAttrAppearance *appearance_attr = (const GtkTextAttrAppearance *)attr;
1307 return gtk_text_attr_appearance_new (&appearance_attr->appearance);
1311 gtk_text_attr_appearance_destroy (PangoAttribute *attr)
1313 GtkTextAppearance *appearance = &((GtkTextAttrAppearance *)attr)->appearance;
1315 if (appearance->bg_stipple)
1316 g_object_unref (appearance->bg_stipple);
1317 if (appearance->fg_stipple)
1318 g_object_unref (appearance->fg_stipple);
1324 gtk_text_attr_appearance_compare (const PangoAttribute *attr1,
1325 const PangoAttribute *attr2)
1327 const GtkTextAppearance *appearance1 = &((const GtkTextAttrAppearance *)attr1)->appearance;
1328 const GtkTextAppearance *appearance2 = &((const GtkTextAttrAppearance *)attr2)->appearance;
1330 return (gdk_color_equal (&appearance1->fg_color, &appearance2->fg_color) &&
1331 gdk_color_equal (&appearance1->bg_color, &appearance2->bg_color) &&
1332 appearance1->fg_stipple == appearance2->fg_stipple &&
1333 appearance1->bg_stipple == appearance2->bg_stipple &&
1334 appearance1->underline == appearance2->underline &&
1335 appearance1->strikethrough == appearance2->strikethrough &&
1336 appearance1->draw_bg == appearance2->draw_bg);
1341 * gtk_text_attr_appearance_new:
1344 * Create a new font description attribute. (This attribute
1345 * allows setting family, style, weight, variant, stretch,
1346 * and size simultaneously.)
1350 static PangoAttribute *
1351 gtk_text_attr_appearance_new (const GtkTextAppearance *appearance)
1353 static PangoAttrClass klass = {
1355 gtk_text_attr_appearance_copy,
1356 gtk_text_attr_appearance_destroy,
1357 gtk_text_attr_appearance_compare
1360 GtkTextAttrAppearance *result;
1363 klass.type = gtk_text_attr_appearance_type =
1364 pango_attr_type_register ("GtkTextAttrAppearance");
1366 result = g_new (GtkTextAttrAppearance, 1);
1367 result->attr.klass = &klass;
1369 result->appearance = *appearance;
1371 if (appearance->bg_stipple)
1372 g_object_ref (appearance->bg_stipple);
1373 if (appearance->fg_stipple)
1374 g_object_ref (appearance->fg_stipple);
1376 return (PangoAttribute *)result;
1381 add_generic_attrs (GtkTextLayout *layout,
1382 GtkTextAppearance *appearance,
1384 PangoAttrList *attrs,
1389 PangoAttribute *attr;
1391 if (appearance->underline != PANGO_UNDERLINE_NONE)
1393 attr = pango_attr_underline_new (appearance->underline);
1395 attr->start_index = start;
1396 attr->end_index = start + byte_count;
1398 pango_attr_list_insert (attrs, attr);
1401 if (appearance->strikethrough)
1403 attr = pango_attr_strikethrough_new (appearance->strikethrough);
1405 attr->start_index = start;
1406 attr->end_index = start + byte_count;
1408 pango_attr_list_insert (attrs, attr);
1411 if (appearance->rise != 0)
1413 attr = pango_attr_rise_new (appearance->rise);
1415 attr->start_index = start;
1416 attr->end_index = start + byte_count;
1418 pango_attr_list_insert (attrs, attr);
1423 attr = gtk_text_attr_appearance_new (appearance);
1425 attr->start_index = start;
1426 attr->end_index = start + byte_count;
1428 ((GtkTextAttrAppearance *)attr)->appearance.is_text = is_text;
1430 pango_attr_list_insert (attrs, attr);
1435 add_text_attrs (GtkTextLayout *layout,
1436 GtkTextAttributes *style,
1438 PangoAttrList *attrs,
1442 PangoAttribute *attr;
1444 attr = pango_attr_font_desc_new (style->font);
1445 attr->start_index = start;
1446 attr->end_index = start + byte_count;
1448 pango_attr_list_insert (attrs, attr);
1450 if (style->font_scale != 1.0)
1452 attr = pango_attr_scale_new (style->font_scale);
1454 attr->start_index = start;
1455 attr->end_index = start + byte_count;
1457 pango_attr_list_insert (attrs, attr);
1462 add_pixbuf_attrs (GtkTextLayout *layout,
1463 GtkTextLineDisplay *display,
1464 GtkTextAttributes *style,
1465 GtkTextLineSegment *seg,
1466 PangoAttrList *attrs,
1469 PangoAttribute *attr;
1470 PangoRectangle logical_rect;
1471 GtkTextPixbuf *pixbuf = &seg->body.pixbuf;
1474 width = gdk_pixbuf_get_width (pixbuf->pixbuf);
1475 height = gdk_pixbuf_get_height (pixbuf->pixbuf);
1478 logical_rect.y = -height * PANGO_SCALE;
1479 logical_rect.width = width * PANGO_SCALE;
1480 logical_rect.height = height * PANGO_SCALE;
1482 attr = pango_attr_shape_new_with_data (&logical_rect, &logical_rect,
1483 pixbuf->pixbuf, NULL, NULL);
1484 attr->start_index = start;
1485 attr->end_index = start + seg->byte_count;
1486 pango_attr_list_insert (attrs, attr);
1488 display->shaped_objects =
1489 g_slist_append (display->shaped_objects, pixbuf->pixbuf);
1493 add_child_attrs (GtkTextLayout *layout,
1494 GtkTextLineDisplay *display,
1495 GtkTextAttributes *style,
1496 GtkTextLineSegment *seg,
1497 PangoAttrList *attrs,
1500 PangoAttribute *attr;
1501 PangoRectangle logical_rect;
1502 GtkTextChildAnchor *anchor;
1510 anchor = seg->body.child.obj;
1512 tmp_list = seg->body.child.widgets;
1513 while (tmp_list != NULL)
1515 GtkWidget *child = tmp_list->data;
1517 if (_gtk_anchored_child_get_layout (child) == layout)
1522 gtk_widget_get_child_requisition (child, &req);
1525 height = req.height;
1532 tmp_list = g_slist_next (tmp_list);
1535 if (tmp_list == NULL)
1537 /* If tmp_list == NULL then there is no widget at this anchor in
1538 * this display; not an error. We make up an arbitrary size
1539 * to use, just so the programmer can see the blank spot.
1540 * We also put a NULL in the shaped objects list, to keep
1541 * the correspondence between the list and the shaped chars in
1542 * the layout. A bad hack, yes.
1551 display->shaped_objects = g_slist_append (display->shaped_objects, widget);
1554 logical_rect.y = -height * PANGO_SCALE;
1555 logical_rect.width = width * PANGO_SCALE;
1556 logical_rect.height = height * PANGO_SCALE;
1558 attr = pango_attr_shape_new_with_data (&logical_rect, &logical_rect,
1559 widget, NULL, NULL);
1560 attr->start_index = start;
1561 attr->end_index = start + seg->byte_count;
1562 pango_attr_list_insert (attrs, attr);
1566 add_cursor (GtkTextLayout *layout,
1567 GtkTextLineDisplay *display,
1568 GtkTextLineSegment *seg,
1571 PangoRectangle strong_pos, weak_pos;
1572 GtkTextCursorDisplay *cursor = NULL; /* Quiet GCC */
1573 gboolean add_weak = FALSE;
1574 gboolean add_strong = FALSE;
1576 /* Hide insertion cursor when we have a selection or the layout
1577 * user has hidden the cursor.
1579 if (_gtk_text_btree_mark_is_insert (_gtk_text_buffer_get_btree (layout->buffer),
1580 seg->body.mark.obj) &&
1581 (!layout->cursor_visible ||
1582 gtk_text_buffer_get_selection_bounds (layout->buffer, NULL, NULL)))
1585 pango_layout_get_cursor_pos (display->layout, start, &strong_pos, &weak_pos);
1587 if (layout->cursor_direction == GTK_TEXT_DIR_NONE)
1592 else if (display->direction == layout->cursor_direction)
1599 cursor = g_new (GtkTextCursorDisplay, 1);
1601 cursor->x = PANGO_PIXELS (strong_pos.x);
1602 cursor->y = PANGO_PIXELS (strong_pos.y);
1603 cursor->height = PANGO_PIXELS (strong_pos.height);
1604 cursor->is_strong = TRUE;
1605 cursor->is_weak = (layout->cursor_direction == GTK_TEXT_DIR_NONE) ? FALSE : TRUE;
1606 display->cursors = g_slist_prepend (display->cursors, cursor);
1611 if (weak_pos.x == strong_pos.x && add_strong)
1612 cursor->is_weak = TRUE;
1615 cursor = g_new (GtkTextCursorDisplay, 1);
1617 cursor->x = PANGO_PIXELS (weak_pos.x);
1618 cursor->y = PANGO_PIXELS (weak_pos.y);
1619 cursor->height = PANGO_PIXELS (weak_pos.height);
1620 cursor->is_strong = (layout->cursor_direction == GTK_TEXT_DIR_NONE) ? FALSE : TRUE;
1621 cursor->is_weak = TRUE;
1622 display->cursors = g_slist_prepend (display->cursors, cursor);
1628 is_shape (PangoLayoutRun *run)
1630 GSList *tmp_list = run->item->analysis.extra_attrs;
1634 PangoAttribute *attr = tmp_list->data;
1636 if (attr->klass->type == PANGO_ATTR_SHAPE)
1639 tmp_list = tmp_list->next;
1646 allocate_child_widgets (GtkTextLayout *text_layout,
1647 GtkTextLineDisplay *display)
1649 GSList *shaped = display->shaped_objects;
1650 PangoLayout *layout = display->layout;
1651 PangoLayoutIter *iter;
1653 iter = pango_layout_get_iter (layout);
1657 PangoLayoutRun *run = pango_layout_iter_get_run (iter);
1659 if (run && is_shape (run))
1661 GObject *shaped_object = shaped->data;
1662 shaped = shaped->next;
1664 /* shaped_object is NULL for child anchors with no
1665 * widgets stored at them
1667 if (shaped_object && GTK_IS_WIDGET (shaped_object))
1669 PangoRectangle extents;
1671 /* We emit "allocate_child" with the x,y of
1672 * the widget with respect to the top of the line
1673 * and the left side of the buffer
1676 pango_layout_iter_get_run_extents (iter,
1680 g_signal_emit (text_layout,
1681 signals[ALLOCATE_CHILD],
1684 PANGO_PIXELS (extents.x) + display->x_offset,
1685 PANGO_PIXELS (extents.y) + display->top_margin);
1689 while (pango_layout_iter_next_run (iter));
1691 pango_layout_iter_free (iter);
1695 convert_color (GdkColor *result,
1696 PangoAttrColor *attr)
1698 result->red = attr->color.red;
1699 result->blue = attr->color.blue;
1700 result->green = attr->color.green;
1703 /* This function is used to convert the preedit string attributes, which are
1704 * standard PangoAttributes, into the custom attributes used by the text
1705 * widget and insert them into a attr list with a given offset.
1708 add_preedit_attrs (GtkTextLayout *layout,
1709 GtkTextAttributes *style,
1710 PangoAttrList *attrs,
1714 PangoAttrIterator *iter = pango_attr_list_get_iterator (layout->preedit_attrs);
1718 GtkTextAppearance appearance = style->appearance;
1719 PangoFontDescription *font_desc = pango_font_description_copy_static (style->font);
1720 PangoAttribute *insert_attr;
1721 GSList *extra_attrs = NULL;
1723 PangoLanguage *language;
1726 pango_attr_iterator_range (iter, &start, &end);
1728 if (end == G_MAXINT)
1729 end = layout->preedit_len;
1734 pango_attr_iterator_get_font (iter, font_desc, &language, &extra_attrs);
1736 tmp_list = extra_attrs;
1739 PangoAttribute *attr = tmp_list->data;
1741 switch (attr->klass->type)
1743 case PANGO_ATTR_FOREGROUND:
1744 convert_color (&appearance.fg_color, (PangoAttrColor *)attr);
1746 case PANGO_ATTR_BACKGROUND:
1747 convert_color (&appearance.bg_color, (PangoAttrColor *)attr);
1748 appearance.draw_bg = TRUE;
1750 case PANGO_ATTR_UNDERLINE:
1751 appearance.underline = ((PangoAttrInt *)attr)->value;
1753 case PANGO_ATTR_STRIKETHROUGH:
1754 appearance.strikethrough = ((PangoAttrInt *)attr)->value;
1756 case PANGO_ATTR_RISE:
1757 appearance.rise = ((PangoAttrInt *)attr)->value;
1763 pango_attribute_destroy (attr);
1764 tmp_list = tmp_list->next;
1767 g_slist_free (extra_attrs);
1769 insert_attr = pango_attr_font_desc_new (font_desc);
1770 insert_attr->start_index = start + offset;
1771 insert_attr->end_index = end + offset;
1773 pango_attr_list_insert (attrs, insert_attr);
1777 insert_attr = pango_attr_language_new (language);
1778 insert_attr->start_index = start + offset;
1779 insert_attr->end_index = end + offset;
1781 pango_attr_list_insert (attrs, insert_attr);
1784 add_generic_attrs (layout, &appearance, end - start,
1785 attrs, start + offset,
1788 pango_font_description_free (font_desc);
1790 while (pango_attr_iterator_next (iter));
1792 pango_attr_iterator_destroy (iter);
1795 GtkTextLineDisplay *
1796 gtk_text_layout_get_line_display (GtkTextLayout *layout,
1800 GtkTextLayoutPrivate *priv = GTK_TEXT_LAYOUT_GET_PRIVATE (layout);
1801 GtkTextLineDisplay *display;
1802 GtkTextLineSegment *seg;
1804 GtkTextAttributes *style;
1806 PangoAttrList *attrs;
1807 gint text_allocated, layout_byte_offset, buffer_byte_offset;
1808 PangoRectangle extents;
1809 gboolean para_values_set = FALSE;
1810 GSList *cursor_byte_offsets = NULL;
1811 GSList *cursor_segs = NULL;
1812 GSList *tmp_list1, *tmp_list2;
1813 gboolean saw_widget = FALSE;
1814 PangoDirection base_dir;
1816 g_return_val_if_fail (line != NULL, NULL);
1818 if (layout->one_display_cache)
1820 if (line == layout->one_display_cache->line &&
1821 (size_only || !layout->one_display_cache->size_only))
1822 return layout->one_display_cache;
1825 GtkTextLineDisplay *tmp_display = layout->one_display_cache;
1826 layout->one_display_cache = NULL;
1827 gtk_text_layout_free_line_display (layout, tmp_display);
1831 display = g_new0 (GtkTextLineDisplay, 1);
1833 display->size_only = size_only;
1834 display->line = line;
1835 display->insert_index = -1;
1837 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
1840 /* Special-case optimization for completely
1841 * invisible lines; makes it faster to deal
1842 * with sequences of invisible lines.
1844 if (totally_invisible_line (layout, line, &iter))
1847 /* Find the bidi base direction */
1848 base_dir = line->dir_propagated_forward;
1849 if (base_dir == PANGO_DIRECTION_NEUTRAL)
1850 base_dir = line->dir_propagated_back;
1852 if (line == priv->cursor_line &&
1853 line->dir_strong == PANGO_DIRECTION_NEUTRAL)
1855 base_dir = (layout->keyboard_direction == GTK_TEXT_DIR_LTR) ?
1856 PANGO_DIRECTION_LTR : PANGO_DIRECTION_RTL;
1859 /* Allocate space for flat text for buffer
1861 text_allocated = _gtk_text_line_byte_count (line);
1862 text = g_malloc (text_allocated);
1864 attrs = pango_attr_list_new ();
1866 /* Iterate over segments, creating display chunks for them. */
1867 layout_byte_offset = 0; /* current length of layout text (includes preedit, does not include invisible text) */
1868 buffer_byte_offset = 0; /* position in the buffer line */
1869 seg = _gtk_text_iter_get_any_segment (&iter);
1872 /* Displayable segments */
1873 if (seg->type == >k_text_char_type ||
1874 seg->type == >k_text_pixbuf_type ||
1875 seg->type == >k_text_child_type)
1877 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
1879 buffer_byte_offset);
1880 style = get_style (layout, &iter);
1882 /* We have to delay setting the paragraph values until we
1883 * hit the first pixbuf or text segment because toggles at
1884 * the beginning of the paragraph should affect the
1885 * paragraph-global values
1887 if (!para_values_set)
1889 set_para_values (layout, base_dir, style, display);
1890 para_values_set = TRUE;
1893 /* First see if the chunk is invisible, and ignore it if so. Tk
1894 * looked at tabs, wrap mode, etc. before doing this, but
1895 * that made no sense to me, so I am just skipping the
1898 if (!style->invisible)
1900 if (seg->type == >k_text_char_type)
1902 /* We don't want to split segments because of marks,
1903 * so we scan forward for more segments only
1904 * separated from us by marks. In theory, we should
1905 * also merge segments with identical styles, even
1906 * if there are toggles in-between
1910 GtkTextLineSegment *prev_seg = NULL;
1914 if (seg->type == >k_text_char_type)
1916 memcpy (text + layout_byte_offset, seg->body.chars, seg->byte_count);
1917 layout_byte_offset += seg->byte_count;
1918 buffer_byte_offset += seg->byte_count;
1919 bytes += seg->byte_count;
1921 else if (seg->type == >k_text_right_mark_type ||
1922 seg->type == >k_text_left_mark_type)
1924 /* If we have preedit string, break out of this loop - we'll almost
1925 * certainly have different attributes on the preedit string
1928 if (layout->preedit_len > 0 &&
1929 _gtk_text_btree_mark_is_insert (_gtk_text_buffer_get_btree (layout->buffer),
1930 seg->body.mark.obj))
1933 if (seg->body.mark.visible)
1935 cursor_byte_offsets = g_slist_prepend (cursor_byte_offsets, GINT_TO_POINTER (layout_byte_offset));
1936 cursor_segs = g_slist_prepend (cursor_segs, seg);
1946 seg = prev_seg; /* Back up one */
1947 add_generic_attrs (layout, &style->appearance,
1949 attrs, layout_byte_offset - bytes,
1951 add_text_attrs (layout, style, bytes, attrs,
1952 layout_byte_offset - bytes, size_only);
1954 else if (seg->type == >k_text_pixbuf_type)
1956 add_generic_attrs (layout,
1959 attrs, layout_byte_offset,
1961 add_pixbuf_attrs (layout, display, style,
1962 seg, attrs, layout_byte_offset);
1963 memcpy (text + layout_byte_offset, gtk_text_unknown_char_utf8,
1965 layout_byte_offset += seg->byte_count;
1966 buffer_byte_offset += seg->byte_count;
1968 else if (seg->type == >k_text_child_type)
1972 add_generic_attrs (layout, &style->appearance,
1974 attrs, layout_byte_offset,
1976 add_child_attrs (layout, display, style,
1977 seg, attrs, layout_byte_offset);
1978 memcpy (text + layout_byte_offset, gtk_text_unknown_char_utf8,
1980 layout_byte_offset += seg->byte_count;
1981 buffer_byte_offset += seg->byte_count;
1985 /* We don't know this segment type */
1986 g_assert_not_reached ();
1989 } /* if (segment was visible) */
1992 /* Invisible segment */
1993 buffer_byte_offset += seg->byte_count;
1996 release_style (layout, style);
2000 else if (seg->type == >k_text_toggle_on_type ||
2001 seg->type == >k_text_toggle_off_type)
2003 /* Style may have changed, drop our
2004 current cached style */
2005 invalidate_cached_style (layout);
2009 else if (seg->type == >k_text_right_mark_type ||
2010 seg->type == >k_text_left_mark_type)
2012 gint cursor_offset = 0;
2014 /* At the insertion point, add the preedit string, if any */
2016 if (_gtk_text_btree_mark_is_insert (_gtk_text_buffer_get_btree (layout->buffer),
2017 seg->body.mark.obj))
2019 display->insert_index = layout_byte_offset;
2021 if (layout->preedit_len > 0)
2023 text_allocated += layout->preedit_len;
2024 text = g_realloc (text, text_allocated);
2026 style = get_style (layout, &iter);
2027 add_preedit_attrs (layout, style, attrs, layout_byte_offset, size_only);
2028 release_style (layout, style);
2030 memcpy (text + layout_byte_offset, layout->preedit_string, layout->preedit_len);
2031 layout_byte_offset += layout->preedit_len;
2032 /* DO NOT increment the buffer byte offset for preedit */
2034 cursor_offset = layout->preedit_cursor - layout->preedit_len;
2039 /* Display visible marks */
2041 if (seg->body.mark.visible)
2043 cursor_byte_offsets = g_slist_prepend (cursor_byte_offsets,
2044 GINT_TO_POINTER (layout_byte_offset + cursor_offset));
2045 cursor_segs = g_slist_prepend (cursor_segs, seg);
2050 g_error ("Unknown segment type: %s", seg->type->name);
2055 if (!para_values_set)
2057 style = get_style (layout, &iter);
2058 set_para_values (layout, base_dir, style, display);
2059 release_style (layout, style);
2062 /* Pango doesn't want the trailing paragraph delimiters */
2065 /* Only one character has type G_UNICODE_PARAGRAPH_SEPARATOR in
2066 * Unicode 3.0; update this if that changes.
2068 #define PARAGRAPH_SEPARATOR 0x2029
2071 if (layout_byte_offset > 0)
2073 const char *prev = g_utf8_prev_char (text + layout_byte_offset);
2074 ch = g_utf8_get_char (prev);
2075 if (ch == PARAGRAPH_SEPARATOR || ch == '\r' || ch == '\n')
2076 layout_byte_offset = prev - text; /* chop off */
2078 if (ch == '\n' && layout_byte_offset > 0)
2080 /* Possibly chop a CR as well */
2081 prev = g_utf8_prev_char (text + layout_byte_offset);
2083 --layout_byte_offset;
2088 pango_layout_set_text (display->layout, text, layout_byte_offset);
2089 pango_layout_set_attributes (display->layout, attrs);
2091 tmp_list1 = cursor_byte_offsets;
2092 tmp_list2 = cursor_segs;
2095 add_cursor (layout, display, tmp_list2->data,
2096 GPOINTER_TO_INT (tmp_list1->data));
2097 tmp_list1 = tmp_list1->next;
2098 tmp_list2 = tmp_list2->next;
2100 g_slist_free (cursor_byte_offsets);
2101 g_slist_free (cursor_segs);
2103 pango_layout_get_extents (display->layout, NULL, &extents);
2105 display->width = PIXEL_BOUND (extents.width) + display->left_margin + display->right_margin;
2106 display->height += PANGO_PIXELS (extents.height);
2108 /* If we aren't wrapping, we need to do the alignment of each
2109 * paragraph ourselves.
2111 if (pango_layout_get_width (display->layout) < 0)
2113 gint excess = display->total_width - display->width;
2115 switch (pango_layout_get_alignment (display->layout))
2117 case PANGO_ALIGN_LEFT:
2119 case PANGO_ALIGN_CENTER:
2120 display->x_offset += excess / 2;
2122 case PANGO_ALIGN_RIGHT:
2123 display->x_offset += excess;
2128 /* Free this if we aren't in a loop */
2129 if (layout->wrap_loop_count == 0)
2130 invalidate_cached_style (layout);
2133 pango_attr_list_unref (attrs);
2135 layout->one_display_cache = display;
2138 allocate_child_widgets (layout, display);
2144 gtk_text_layout_free_line_display (GtkTextLayout *layout,
2145 GtkTextLineDisplay *display)
2147 if (display != layout->one_display_cache)
2149 if (display->layout)
2150 g_object_unref (display->layout);
2152 if (display->cursors)
2154 g_slist_foreach (display->cursors, (GFunc)g_free, NULL);
2155 g_slist_free (display->cursors);
2157 g_slist_free (display->shaped_objects);
2163 /* Functions to convert iter <=> index for the line of a GtkTextLineDisplay
2164 * taking into account the preedit string and invisible text if necessary.
2167 line_display_iter_to_index (GtkTextLayout *layout,
2168 GtkTextLineDisplay *display,
2169 const GtkTextIter *iter)
2173 g_return_val_if_fail (_gtk_text_iter_get_text_line (iter) == display->line, 0);
2175 index = gtk_text_iter_get_visible_line_index (iter);
2177 if (layout->preedit_len > 0 && display->insert_index >= 0)
2179 if (index >= display->insert_index)
2180 index += layout->preedit_len;
2187 line_display_index_to_iter (GtkTextLayout *layout,
2188 GtkTextLineDisplay *display,
2193 g_return_if_fail (!_gtk_text_line_is_last (display->line,
2194 _gtk_text_buffer_get_btree (layout->buffer)));
2196 if (layout->preedit_len > 0 && display->insert_index >= 0)
2198 if (index >= display->insert_index + layout->preedit_len)
2199 index -= layout->preedit_len;
2200 else if (index > display->insert_index)
2202 index = display->insert_index;
2207 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
2208 iter, display->line, 0);
2210 gtk_text_iter_set_visible_line_index (iter, index);
2212 if (_gtk_text_iter_get_text_line (iter) != display->line)
2214 /* Clamp to end of line - really this clamping should have been done
2215 * before here, maybe in Pango, this is a broken band-aid I think
2217 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
2218 iter, display->line, 0);
2220 if (!gtk_text_iter_ends_line (iter))
2221 gtk_text_iter_forward_to_line_end (iter);
2224 gtk_text_iter_forward_chars (iter, trailing);
2228 get_line_at_y (GtkTextLayout *layout,
2235 if (y > layout->height)
2238 *line = _gtk_text_btree_find_line_by_y (_gtk_text_buffer_get_btree (layout->buffer),
2239 layout, y, line_top);
2242 *line = _gtk_text_btree_get_end_iter_line (_gtk_text_buffer_get_btree (layout->buffer));
2246 _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
2252 * gtk_text_layout_get_line_at_y:
2253 * @layout: a #GtkLayout
2254 * @target_iter: the iterator in which the result is stored
2255 * @y: the y positition
2256 * @line_top: location to store the y coordinate of the
2257 * top of the line. (Can by %NULL.)
2259 * Get the iter at the beginning of the line which is displayed
2263 gtk_text_layout_get_line_at_y (GtkTextLayout *layout,
2264 GtkTextIter *target_iter,
2270 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
2271 g_return_if_fail (target_iter != NULL);
2273 get_line_at_y (layout, y, &line, line_top);
2274 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
2275 target_iter, line, 0);
2279 gtk_text_layout_get_iter_at_pixel (GtkTextLayout *layout,
2280 GtkTextIter *target_iter,
2286 gtk_text_layout_get_iter_at_position (layout, target_iter, &trailing, x, y);
2288 gtk_text_iter_forward_chars (target_iter, trailing);
2291 void gtk_text_layout_get_iter_at_position (GtkTextLayout *layout,
2292 GtkTextIter *target_iter,
2300 GtkTextLineDisplay *display;
2302 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
2303 g_return_if_fail (target_iter != NULL);
2305 get_line_at_y (layout, y, &line, &line_top);
2307 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2309 x -= display->x_offset;
2310 y -= line_top + display->top_margin;
2312 /* If we are below the layout, position the cursor at the last character
2315 if (y > display->height - display->top_margin - display->bottom_margin)
2317 byte_index = _gtk_text_line_byte_count (line);
2322 /* Ignore the "outside" return value from pango. Pango is doing
2323 * the right thing even if we are outside the layout in the
2326 pango_layout_xy_to_index (display->layout, x * PANGO_SCALE, y * PANGO_SCALE,
2327 &byte_index, trailing);
2330 line_display_index_to_iter (layout, display, target_iter, byte_index, 0);
2332 gtk_text_layout_free_line_display (layout, display);
2337 * gtk_text_layout_get_cursor_locations:
2338 * @layout: a #GtkTextLayout
2339 * @iter: a #GtkTextIter
2340 * @strong_pos: location to store the strong cursor position (may be %NULL)
2341 * @weak_pos: location to store the weak cursor position (may be %NULL)
2343 * Given an iterator within a text layout, determine the positions of the
2344 * strong and weak cursors if the insertion point is at that
2345 * iterator. The position of each cursor is stored as a zero-width
2346 * rectangle. The strong cursor location is the location where
2347 * characters of the directionality equal to the base direction of the
2348 * paragraph are inserted. The weak cursor location is the location
2349 * where characters of the directionality opposite to the base
2350 * direction of the paragraph are inserted.
2353 gtk_text_layout_get_cursor_locations (GtkTextLayout *layout,
2355 GdkRectangle *strong_pos,
2356 GdkRectangle *weak_pos)
2359 GtkTextLineDisplay *display;
2362 GtkTextIter insert_iter;
2364 PangoRectangle pango_strong_pos;
2365 PangoRectangle pango_weak_pos;
2367 g_return_if_fail (layout != NULL);
2368 g_return_if_fail (iter != NULL);
2370 line = _gtk_text_iter_get_text_line (iter);
2371 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2372 index = line_display_iter_to_index (layout, display, iter);
2374 line_top = _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
2377 gtk_text_buffer_get_iter_at_mark (layout->buffer, &insert_iter,
2378 gtk_text_buffer_get_mark (layout->buffer,
2381 if (gtk_text_iter_equal (iter, &insert_iter))
2382 index += layout->preedit_cursor - layout->preedit_len;
2384 pango_layout_get_cursor_pos (display->layout, index,
2385 strong_pos ? &pango_strong_pos : NULL,
2386 weak_pos ? &pango_weak_pos : NULL);
2390 strong_pos->x = display->x_offset + pango_strong_pos.x / PANGO_SCALE;
2391 strong_pos->y = line_top + display->top_margin + pango_strong_pos.y / PANGO_SCALE;
2392 strong_pos->width = 0;
2393 strong_pos->height = pango_strong_pos.height / PANGO_SCALE;
2398 weak_pos->x = display->x_offset + pango_weak_pos.x / PANGO_SCALE;
2399 weak_pos->y = line_top + display->top_margin + pango_weak_pos.y / PANGO_SCALE;
2400 weak_pos->width = 0;
2401 weak_pos->height = pango_weak_pos.height / PANGO_SCALE;
2404 gtk_text_layout_free_line_display (layout, display);
2408 * gtk_text_layout_get_line_yrange:
2409 * @layout: a #GtkTextLayout
2410 * @iter: a #GtkTextIter
2411 * @y: location to store the top of the paragraph in pixels,
2413 * @height location to store the height of the paragraph in pixels,
2416 * Find the range of y coordinates for the paragraph containing
2420 gtk_text_layout_get_line_yrange (GtkTextLayout *layout,
2421 const GtkTextIter *iter,
2427 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
2428 g_return_if_fail (_gtk_text_iter_get_btree (iter) == _gtk_text_buffer_get_btree (layout->buffer));
2430 line = _gtk_text_iter_get_text_line (iter);
2433 *y = _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
2437 GtkTextLineData *line_data = _gtk_text_line_get_data (line, layout);
2439 *height = line_data->height;
2446 * _gtk_text_layout_get_line_xrange:
2447 * @layout: a #GtkTextLayout
2448 * @iter: a #GtkTextIter
2449 * @x: location to store the top of the paragraph in pixels,
2451 * @width location to store the height of the paragraph in pixels,
2454 * Find the range of X coordinates for the paragraph containing
2455 * the given iter. Private for 2.0 due to API freeze, could
2456 * be made public for 2.2.
2459 _gtk_text_layout_get_line_xrange (GtkTextLayout *layout,
2460 const GtkTextIter *iter,
2466 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
2467 g_return_if_fail (_gtk_text_iter_get_btree (iter) == _gtk_text_buffer_get_btree (layout->buffer));
2469 line = _gtk_text_iter_get_text_line (iter);
2472 *x = 0; /* FIXME This is wrong; should represent the first available cursor position */
2476 GtkTextLineData *line_data = _gtk_text_line_get_data (line, layout);
2478 *width = line_data->width;
2485 gtk_text_layout_get_iter_location (GtkTextLayout *layout,
2486 const GtkTextIter *iter,
2489 PangoRectangle pango_rect;
2492 GtkTextLineDisplay *display;
2496 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
2497 g_return_if_fail (_gtk_text_iter_get_btree (iter) == _gtk_text_buffer_get_btree (layout->buffer));
2498 g_return_if_fail (rect != NULL);
2500 tree = _gtk_text_iter_get_btree (iter);
2501 line = _gtk_text_iter_get_text_line (iter);
2503 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2505 rect->y = _gtk_text_btree_find_line_top (tree, line, layout);
2507 x_offset = display->x_offset * PANGO_SCALE;
2509 byte_index = gtk_text_iter_get_line_index (iter);
2511 pango_layout_index_to_pos (display->layout, byte_index, &pango_rect);
2513 rect->x = PANGO_PIXELS (x_offset + pango_rect.x);
2514 rect->y += PANGO_PIXELS (pango_rect.y) + display->top_margin;
2515 rect->width = PANGO_PIXELS (pango_rect.width);
2516 rect->height = PANGO_PIXELS (pango_rect.height);
2518 gtk_text_layout_free_line_display (layout, display);
2523 /* Find the iter for the logical beginning of the first display line whose
2524 * top y is >= y. If none exists, move the iter to the logical beginning
2525 * of the last line in the buffer.
2528 find_display_line_below (GtkTextLayout *layout,
2532 GtkTextLine *line, *next;
2533 GtkTextLine *found_line = NULL;
2535 gint found_byte = 0;
2537 line = _gtk_text_btree_find_line_by_y (_gtk_text_buffer_get_btree (layout->buffer),
2538 layout, y, &line_top);
2542 _gtk_text_btree_get_end_iter_line (_gtk_text_buffer_get_btree (layout->buffer));
2545 _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
2549 while (line && !found_line)
2551 GtkTextLineDisplay *display = gtk_text_layout_get_line_display (layout, line, FALSE);
2552 PangoLayoutIter *layout_iter;
2554 layout_iter = pango_layout_get_iter (display->layout);
2556 line_top += display->top_margin;
2560 gint first_y, last_y;
2561 PangoLayoutLine *layout_line = pango_layout_iter_get_line (layout_iter);
2563 found_byte = layout_line->start_index;
2571 pango_layout_iter_get_line_yrange (layout_iter, &first_y, &last_y);
2572 line_top += (last_y - first_y) / PANGO_SCALE;
2574 while (pango_layout_iter_next_line (layout_iter));
2576 pango_layout_iter_free (layout_iter);
2578 line_top += display->bottom_margin;
2579 gtk_text_layout_free_line_display (layout, display);
2581 next = _gtk_text_line_next_excluding_last (line);
2588 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
2589 iter, found_line, found_byte);
2592 /* Find the iter for the logical beginning of the last display line whose
2593 * top y is >= y. If none exists, move the iter to the logical beginning
2594 * of the first line in the buffer.
2597 find_display_line_above (GtkTextLayout *layout,
2602 GtkTextLine *found_line = NULL;
2604 gint found_byte = 0;
2606 line = _gtk_text_btree_find_line_by_y (_gtk_text_buffer_get_btree (layout->buffer), layout, y, &line_top);
2609 line = _gtk_text_btree_get_end_iter_line (_gtk_text_buffer_get_btree (layout->buffer));
2611 line_top = _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer), line, layout);
2614 while (line && !found_line)
2616 GtkTextLineDisplay *display = gtk_text_layout_get_line_display (layout, line, FALSE);
2617 PangoRectangle logical_rect;
2618 PangoLayoutIter *layout_iter;
2621 layout_iter = pango_layout_get_iter (display->layout);
2623 line_top -= display->top_margin + display->bottom_margin;
2624 pango_layout_iter_get_layout_extents (layout_iter, NULL, &logical_rect);
2625 line_top -= logical_rect.height / PANGO_SCALE;
2627 tmp_top = line_top + display->top_margin;
2631 gint first_y, last_y;
2632 PangoLayoutLine *layout_line = pango_layout_iter_get_line (layout_iter);
2634 found_byte = layout_line->start_index;
2636 pango_layout_iter_get_line_yrange (layout_iter, &first_y, &last_y);
2638 tmp_top -= (last_y - first_y) / PANGO_SCALE;
2643 pango_layout_iter_free (layout_iter);
2647 while (pango_layout_iter_next_line (layout_iter));
2649 pango_layout_iter_free (layout_iter);
2651 gtk_text_layout_free_line_display (layout, display);
2653 line = _gtk_text_line_previous (line);
2659 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
2660 iter, found_line, found_byte);
2662 gtk_text_buffer_get_iter_at_offset (layout->buffer, iter, 0);
2666 * gtk_text_layout_clamp_iter_to_vrange:
2667 * @layout: a #GtkTextLayout
2668 * @iter: a #GtkTextIter
2669 * @top: the top of the range
2670 * @bottom: the bottom the range
2672 * If the iterator is not fully in the range @top <= y < @bottom,
2673 * then, if possible, move it the minimum distance so that the
2674 * iterator in this range.
2676 * Returns: %TRUE if the iterator was moved, otherwise %FALSE.
2679 gtk_text_layout_clamp_iter_to_vrange (GtkTextLayout *layout,
2684 GdkRectangle iter_rect;
2686 gtk_text_layout_get_iter_location (layout, iter, &iter_rect);
2688 /* If the iter is at least partially above the range, put the iter
2689 * at the first fully visible line after the range.
2691 if (iter_rect.y < top)
2693 find_display_line_below (layout, iter, top);
2697 /* Otherwise, if the iter is at least partially below the screen, put the
2698 * iter on the last logical position of the last completely visible
2701 else if (iter_rect.y + iter_rect.height > bottom)
2703 find_display_line_above (layout, iter, bottom);
2712 * gtk_text_layout_move_iter_to_previous_line:
2713 * @layout: a #GtkLayout
2714 * @iter: a #GtkTextIter
2716 * Move the iterator to the beginning of the previous line. The lines
2717 * of a wrapped paragraph are treated as distinct for this operation.
2720 gtk_text_layout_move_iter_to_previous_line (GtkTextLayout *layout,
2724 GtkTextLineDisplay *display;
2727 PangoLayoutLine *layout_line;
2729 gboolean update_byte = FALSE;
2731 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), FALSE);
2732 g_return_val_if_fail (iter != NULL, FALSE);
2737 line = _gtk_text_iter_get_text_line (iter);
2738 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2739 line_byte = line_display_iter_to_index (layout, display, iter);
2741 /* If display->height == 0 then the line is invisible, so don't
2744 while (display->height == 0)
2746 GtkTextLine *prev_line;
2748 prev_line = _gtk_text_line_previous (line);
2750 if (prev_line == NULL)
2752 line_display_index_to_iter (layout, display, iter, 0, 0);
2756 gtk_text_layout_free_line_display (layout, display);
2759 display = gtk_text_layout_get_line_display (layout, prev_line, FALSE);
2763 tmp_list = pango_layout_get_lines (display->layout);
2764 layout_line = tmp_list->data;
2768 line_byte = layout_line->start_index + layout_line->length;
2771 if (line_byte < layout_line->length || !tmp_list->next) /* first line of paragraph */
2773 GtkTextLine *prev_line;
2775 prev_line = _gtk_text_line_previous (line);
2777 /* first line of the whole buffer, do not move the iter and return FALSE */
2778 if (prev_line == NULL)
2783 gtk_text_layout_free_line_display (layout, display);
2785 display = gtk_text_layout_get_line_display (layout, prev_line, FALSE);
2787 if (display->height > 0)
2789 tmp_list = g_slist_last (pango_layout_get_lines (display->layout));
2790 layout_line = tmp_list->data;
2792 line_display_index_to_iter (layout, display, iter,
2793 layout_line->start_index + layout_line->length, 0);
2797 prev_line = _gtk_text_line_previous (prev_line);
2802 gint prev_offset = layout_line->start_index;
2804 tmp_list = tmp_list->next;
2807 layout_line = tmp_list->data;
2809 if (line_byte < layout_line->start_index + layout_line->length ||
2812 line_display_index_to_iter (layout, display, iter, prev_offset, 0);
2816 prev_offset = layout_line->start_index;
2817 tmp_list = tmp_list->next;
2823 gtk_text_layout_free_line_display (layout, display);
2826 !gtk_text_iter_equal (iter, &orig) &&
2827 !gtk_text_iter_is_end (iter);
2831 * gtk_text_layout_move_iter_to_next_line:
2832 * @layout: a #GtkLayout
2833 * @iter: a #GtkTextIter
2835 * Move the iterator to the beginning of the next line. The
2836 * lines of a wrapped paragraph are treated as distinct for
2840 gtk_text_layout_move_iter_to_next_line (GtkTextLayout *layout,
2844 GtkTextLineDisplay *display;
2847 gboolean found = FALSE;
2848 gboolean found_after = FALSE;
2849 gboolean first = TRUE;
2851 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), FALSE);
2852 g_return_val_if_fail (iter != NULL, FALSE);
2856 line = _gtk_text_iter_get_text_line (iter);
2858 while (line && !found_after)
2862 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2864 if (display->height == 0)
2869 line_byte = line_display_iter_to_index (layout, display, iter);
2875 tmp_list = pango_layout_get_lines (display->layout);
2876 while (tmp_list && !found_after)
2878 PangoLayoutLine *layout_line = tmp_list->data;
2882 line_display_index_to_iter (layout, display, iter,
2883 layout_line->start_index, 0);
2886 else if (line_byte < layout_line->start_index + layout_line->length || !tmp_list->next)
2889 tmp_list = tmp_list->next;
2894 gtk_text_layout_free_line_display (layout, display);
2896 line = _gtk_text_line_next_excluding_last (line);
2900 gtk_text_buffer_get_end_iter (layout->buffer, iter);
2903 !gtk_text_iter_equal (iter, &orig) &&
2904 !gtk_text_iter_is_end (iter);
2908 * gtk_text_layout_move_iter_to_line_end:
2909 * @layout: a #GtkTextLayout
2910 * @direction: if negative, move to beginning of line, otherwise
2911 move to end of line.
2913 * Move to the beginning or end of a display line.
2916 gtk_text_layout_move_iter_to_line_end (GtkTextLayout *layout,
2921 GtkTextLineDisplay *display;
2926 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), FALSE);
2927 g_return_val_if_fail (iter != NULL, FALSE);
2931 line = _gtk_text_iter_get_text_line (iter);
2932 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2933 line_byte = line_display_iter_to_index (layout, display, iter);
2935 tmp_list = pango_layout_get_lines (display->layout);
2938 PangoLayoutLine *layout_line = tmp_list->data;
2940 if (line_byte < layout_line->start_index + layout_line->length || !tmp_list->next)
2942 line_display_index_to_iter (layout, display, iter,
2943 direction < 0 ? layout_line->start_index : layout_line->start_index + layout_line->length,
2946 /* FIXME: As a bad hack, we move back one position when we
2947 * are inside a paragraph to avoid going to next line on a
2948 * forced break not at whitespace. Real fix is to keep track
2949 * of whether marks are at leading or trailing edge? */
2950 if (direction > 0 && layout_line->length > 0 && !gtk_text_iter_ends_line (iter))
2951 gtk_text_iter_backward_char (iter);
2956 tmp_list = tmp_list->next;
2959 gtk_text_layout_free_line_display (layout, display);
2962 !gtk_text_iter_equal (iter, &orig) &&
2963 !gtk_text_iter_is_end (iter);
2968 * gtk_text_layout_iter_starts_line:
2969 * @layout: a #GtkTextLayout
2970 * @iter: iterator to test
2972 * Tests whether an iterator is at the start of a display line.
2975 gtk_text_layout_iter_starts_line (GtkTextLayout *layout,
2976 const GtkTextIter *iter)
2979 GtkTextLineDisplay *display;
2983 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), FALSE);
2984 g_return_val_if_fail (iter != NULL, FALSE);
2986 line = _gtk_text_iter_get_text_line (iter);
2987 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2988 line_byte = line_display_iter_to_index (layout, display, iter);
2990 tmp_list = pango_layout_get_lines (display->layout);
2993 PangoLayoutLine *layout_line = tmp_list->data;
2995 if (line_byte < layout_line->start_index + layout_line->length ||
2998 /* We're located on this line or the para delimiters before
3001 gtk_text_layout_free_line_display (layout, display);
3003 if (line_byte == layout_line->start_index)
3009 tmp_list = tmp_list->next;
3012 g_assert_not_reached ();
3017 gtk_text_layout_get_iter_at_line (GtkTextLayout *layout,
3022 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
3023 iter, line, byte_offset);
3028 * gtk_text_layout_move_iter_to_x:
3029 * @layout: a #GtkTextLayout
3030 * @iter: a #GtkTextIter
3033 * Keeping the iterator on the same line of the layout, move it to the
3034 * specified X coordinate. The lines of a wrapped paragraph are
3035 * treated as distinct for this operation.
3038 gtk_text_layout_move_iter_to_x (GtkTextLayout *layout,
3043 GtkTextLineDisplay *display;
3045 PangoLayoutIter *layout_iter;
3047 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
3048 g_return_if_fail (iter != NULL);
3050 line = _gtk_text_iter_get_text_line (iter);
3052 display = gtk_text_layout_get_line_display (layout, line, FALSE);
3053 line_byte = line_display_iter_to_index (layout, display, iter);
3055 layout_iter = pango_layout_get_iter (display->layout);
3059 PangoLayoutLine *layout_line = pango_layout_iter_get_line (layout_iter);
3061 if (line_byte < layout_line->start_index + layout_line->length ||
3062 pango_layout_iter_at_last_line (layout_iter))
3064 PangoRectangle logical_rect;
3065 gint byte_index, trailing;
3066 gint x_offset = display->x_offset * PANGO_SCALE;
3068 pango_layout_iter_get_line_extents (layout_iter, NULL, &logical_rect);
3070 pango_layout_line_x_to_index (layout_line,
3071 x * PANGO_SCALE - x_offset - logical_rect.x,
3072 &byte_index, &trailing);
3074 line_display_index_to_iter (layout, display, iter, byte_index, trailing);
3079 while (pango_layout_iter_next_line (layout_iter));
3081 pango_layout_iter_free (layout_iter);
3083 gtk_text_layout_free_line_display (layout, display);
3087 * gtk_text_layout_move_iter_visually:
3088 * @layout: a #GtkTextLayout
3089 * @iter: a #GtkTextIter
3090 * @count: number of characters to move (negative moves left, positive moves right)
3092 * Move the iterator a given number of characters visually, treating
3093 * it as the strong cursor position. If @count is positive, then the
3094 * new strong cursor position will be @count positions to the right of
3095 * the old cursor position. If @count is negative then the new strong
3096 * cursor position will be @count positions to the left of the old
3099 * In the presence of bidirection text, the correspondence
3100 * between logical and visual order will depend on the direction
3101 * of the current run, and there may be jumps when the cursor
3102 * is moved off of the end of a run.
3106 gtk_text_layout_move_iter_visually (GtkTextLayout *layout,
3110 GtkTextLineDisplay *display = NULL;
3113 g_return_val_if_fail (layout != NULL, FALSE);
3114 g_return_val_if_fail (iter != NULL, FALSE);
3120 GtkTextLine *line = _gtk_text_iter_get_text_line (iter);
3122 gint extra_back = 0;
3125 int byte_count = _gtk_text_line_byte_count (line);
3131 display = gtk_text_layout_get_line_display (layout, line, FALSE);
3133 if (layout->cursor_direction == GTK_TEXT_DIR_NONE)
3136 strong = display->direction == layout->cursor_direction;
3138 line_byte = line_display_iter_to_index (layout, display, iter);
3142 pango_layout_move_cursor_visually (display->layout, strong, line_byte, 0, 1, &new_index, &new_trailing);
3147 pango_layout_move_cursor_visually (display->layout, strong, line_byte, 0, -1, &new_index, &new_trailing);
3151 /* We need to handle the preedit string specially. Well, we don't really need to
3152 * handle it specially, since hopefully calling gtk_im_context_reset() will
3153 * remove the preedit string; but if we start off in front of the preedit
3154 * string (logically) and end up in or on the back edge of the preedit string,
3155 * we should move the iter one place farther.
3157 if (layout->preedit_len > 0 && display->insert_index >= 0)
3159 if (line_byte == display->insert_index + layout->preedit_len &&
3160 new_index < display->insert_index + layout->preedit_len)
3162 line_byte = display->insert_index;
3167 if (new_index < 0 || (new_index == 0 && extra_back))
3169 line = _gtk_text_line_previous (line);
3174 gtk_text_layout_free_line_display (layout, display);
3175 display = gtk_text_layout_get_line_display (layout, line, FALSE);
3176 new_index = _gtk_text_line_byte_count (line);
3178 else if (new_index > byte_count)
3180 line = _gtk_text_line_next_excluding_last (line);
3184 gtk_text_layout_free_line_display (layout, display);
3185 display = gtk_text_layout_get_line_display (layout, line, FALSE);
3189 line_display_index_to_iter (layout, display, iter, new_index, new_trailing);
3191 gtk_text_iter_backward_char (iter);
3194 gtk_text_layout_free_line_display (layout, display);
3199 !gtk_text_iter_equal (iter, &orig) &&
3200 !gtk_text_iter_is_end (iter);
3204 gtk_text_layout_spew (GtkTextLayout *layout)
3207 GtkTextDisplayLine *iter;
3209 guint paragraphs = 0;
3210 GtkTextLine *last_line = NULL;
3212 iter = layout->line_list;
3213 while (iter != NULL)
3215 if (iter->line != last_line)
3217 printf ("%5u paragraph (%p)\n", paragraphs, iter->line);
3219 last_line = iter->line;
3222 printf (" %5u y: %d len: %d start: %d bytes: %d\n",
3223 wrapped, iter->y, iter->length, iter->byte_offset,
3230 printf ("Layout %s recompute\n",
3231 layout->need_recompute ? "needs" : "doesn't need");
3233 printf ("Layout pars: %u lines: %u size: %d x %d Screen width: %d\n",
3234 paragraphs, wrapped, layout->width,
3235 layout->height, layout->screen_width);
3239 /* Catch all situations that move the insertion point.
3242 gtk_text_layout_mark_set_handler (GtkTextBuffer *buffer,
3243 const GtkTextIter *location,
3247 GtkTextLayout *layout = GTK_TEXT_LAYOUT (data);
3249 if (mark == gtk_text_buffer_get_insert (buffer))
3250 gtk_text_layout_update_cursor_line (layout);
3254 gtk_text_layout_buffer_insert_text (GtkTextBuffer *textbuffer,
3260 GtkTextLayout *layout = GTK_TEXT_LAYOUT (data);
3262 gtk_text_layout_update_cursor_line (layout);
3266 gtk_text_layout_buffer_delete_range (GtkTextBuffer *textbuffer,
3271 GtkTextLayout *layout = GTK_TEXT_LAYOUT (data);
3273 gtk_text_layout_update_cursor_line (layout);