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
81 #include "gtkmarshalers.h"
82 #include "gtktextlayout.h"
83 #include "gtktextbtree.h"
84 #include "gtktextiterprivate.h"
85 #include "gtktextutil.h"
92 #define GTK_TEXT_LAYOUT_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GTK_TYPE_TEXT_LAYOUT, GtkTextLayoutPrivate))
94 typedef struct _GtkTextLayoutPrivate GtkTextLayoutPrivate;
96 struct _GtkTextLayoutPrivate
98 /* Cache the line that the cursor is positioned on, as the keyboard
99 direction only influences the direction of the cursor line.
101 GtkTextLine *cursor_line;
104 static GtkTextLineData *gtk_text_layout_real_wrap (GtkTextLayout *layout,
107 GtkTextLineData *line_data);
109 static void gtk_text_layout_invalidated (GtkTextLayout *layout);
111 static void gtk_text_layout_real_invalidate (GtkTextLayout *layout,
112 const GtkTextIter *start,
113 const GtkTextIter *end);
114 static void gtk_text_layout_real_invalidate_cursors(GtkTextLayout *layout,
115 const GtkTextIter *start,
116 const GtkTextIter *end);
117 static void gtk_text_layout_invalidate_cache (GtkTextLayout *layout,
119 gboolean cursors_only);
120 static void gtk_text_layout_invalidate_cursor_line (GtkTextLayout *layout,
121 gboolean cursors_only);
122 static void gtk_text_layout_real_free_line_data (GtkTextLayout *layout,
124 GtkTextLineData *line_data);
125 static void gtk_text_layout_emit_changed (GtkTextLayout *layout,
130 static void gtk_text_layout_invalidate_all (GtkTextLayout *layout);
132 static PangoAttribute *gtk_text_attr_appearance_new (const GtkTextAppearance *appearance);
134 static void gtk_text_layout_mark_set_handler (GtkTextBuffer *buffer,
135 const GtkTextIter *location,
138 static void gtk_text_layout_buffer_insert_text (GtkTextBuffer *textbuffer,
143 static void gtk_text_layout_buffer_delete_range (GtkTextBuffer *textbuffer,
148 static void gtk_text_layout_update_cursor_line (GtkTextLayout *layout);
150 static void line_display_index_to_iter (GtkTextLayout *layout,
151 GtkTextLineDisplay *display,
156 static gint line_display_iter_to_index (GtkTextLayout *layout,
157 GtkTextLineDisplay *display,
158 const GtkTextIter *iter);
172 #define PIXEL_BOUND(d) (((d) + PANGO_SCALE - 1) / PANGO_SCALE)
174 static void gtk_text_layout_finalize (GObject *object);
176 static guint signals[LAST_SIGNAL] = { 0 };
178 PangoAttrType gtk_text_attr_appearance_type = 0;
180 G_DEFINE_TYPE (GtkTextLayout, gtk_text_layout, G_TYPE_OBJECT)
183 gtk_text_layout_class_init (GtkTextLayoutClass *klass)
185 GObjectClass *object_class = G_OBJECT_CLASS (klass);
187 object_class->finalize = gtk_text_layout_finalize;
189 klass->wrap = gtk_text_layout_real_wrap;
190 klass->invalidate = gtk_text_layout_real_invalidate;
191 klass->invalidate_cursors = gtk_text_layout_real_invalidate_cursors;
192 klass->free_line_data = gtk_text_layout_real_free_line_data;
194 signals[INVALIDATED] =
195 g_signal_new (I_("invalidated"),
196 G_OBJECT_CLASS_TYPE (object_class),
198 G_STRUCT_OFFSET (GtkTextLayoutClass, invalidated),
200 _gtk_marshal_VOID__VOID,
205 g_signal_new (I_("changed"),
206 G_OBJECT_CLASS_TYPE (object_class),
208 G_STRUCT_OFFSET (GtkTextLayoutClass, changed),
210 _gtk_marshal_VOID__INT_INT_INT,
217 signals[ALLOCATE_CHILD] =
218 g_signal_new (I_("allocate-child"),
219 G_OBJECT_CLASS_TYPE (object_class),
221 G_STRUCT_OFFSET (GtkTextLayoutClass, allocate_child),
223 _gtk_marshal_VOID__OBJECT_INT_INT,
230 g_type_class_add_private (object_class, sizeof (GtkTextLayoutPrivate));
234 gtk_text_layout_init (GtkTextLayout *text_layout)
236 text_layout->cursor_visible = TRUE;
240 gtk_text_layout_new (void)
242 return g_object_new (GTK_TYPE_TEXT_LAYOUT, NULL);
246 free_style_cache (GtkTextLayout *text_layout)
248 if (text_layout->one_style_cache)
250 gtk_text_attributes_unref (text_layout->one_style_cache);
251 text_layout->one_style_cache = NULL;
256 gtk_text_layout_finalize (GObject *object)
258 GtkTextLayout *layout;
260 layout = GTK_TEXT_LAYOUT (object);
262 gtk_text_layout_set_buffer (layout, NULL);
264 if (layout->default_style)
265 gtk_text_attributes_unref (layout->default_style);
266 layout->default_style = NULL;
268 if (layout->ltr_context)
270 g_object_unref (layout->ltr_context);
271 layout->ltr_context = NULL;
273 if (layout->rtl_context)
275 g_object_unref (layout->rtl_context);
276 layout->rtl_context = NULL;
279 if (layout->one_display_cache)
281 GtkTextLineDisplay *tmp_display = layout->one_display_cache;
282 layout->one_display_cache = NULL;
283 gtk_text_layout_free_line_display (layout, tmp_display);
286 if (layout->preedit_string)
288 g_free (layout->preedit_string);
289 layout->preedit_string = NULL;
292 if (layout->preedit_attrs)
294 pango_attr_list_unref (layout->preedit_attrs);
295 layout->preedit_attrs = NULL;
299 G_OBJECT_CLASS (gtk_text_layout_parent_class)->finalize (object);
303 * gtk_text_layout_set_buffer:
304 * @buffer: (allow-none):
307 gtk_text_layout_set_buffer (GtkTextLayout *layout,
308 GtkTextBuffer *buffer)
310 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
311 g_return_if_fail (buffer == NULL || GTK_IS_TEXT_BUFFER (buffer));
313 if (layout->buffer == buffer)
316 free_style_cache (layout);
320 _gtk_text_btree_remove_view (_gtk_text_buffer_get_btree (layout->buffer),
323 g_signal_handlers_disconnect_by_func (layout->buffer,
324 G_CALLBACK (gtk_text_layout_mark_set_handler),
326 g_signal_handlers_disconnect_by_func (layout->buffer,
327 G_CALLBACK (gtk_text_layout_buffer_insert_text),
329 g_signal_handlers_disconnect_by_func (layout->buffer,
330 G_CALLBACK (gtk_text_layout_buffer_delete_range),
333 g_object_unref (layout->buffer);
334 layout->buffer = NULL;
339 layout->buffer = buffer;
341 g_object_ref (buffer);
343 _gtk_text_btree_add_view (_gtk_text_buffer_get_btree (buffer), layout);
345 /* Bind to all signals that move the insert mark. */
346 g_signal_connect_after (layout->buffer, "mark-set",
347 G_CALLBACK (gtk_text_layout_mark_set_handler), layout);
348 g_signal_connect_after (layout->buffer, "insert-text",
349 G_CALLBACK (gtk_text_layout_buffer_insert_text), layout);
350 g_signal_connect_after (layout->buffer, "delete-range",
351 G_CALLBACK (gtk_text_layout_buffer_delete_range), layout);
353 gtk_text_layout_update_cursor_line (layout);
358 gtk_text_layout_default_style_changed (GtkTextLayout *layout)
360 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
362 DV (g_print ("invalidating all due to default style change (%s)\n", G_STRLOC));
363 gtk_text_layout_invalidate_all (layout);
367 gtk_text_layout_set_default_style (GtkTextLayout *layout,
368 GtkTextAttributes *values)
370 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
371 g_return_if_fail (values != NULL);
373 if (values == layout->default_style)
376 gtk_text_attributes_ref (values);
378 if (layout->default_style)
379 gtk_text_attributes_unref (layout->default_style);
381 layout->default_style = values;
383 gtk_text_layout_default_style_changed (layout);
387 gtk_text_layout_set_contexts (GtkTextLayout *layout,
388 PangoContext *ltr_context,
389 PangoContext *rtl_context)
391 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
393 if (layout->ltr_context != ltr_context)
395 if (layout->ltr_context)
396 g_object_unref (layout->ltr_context);
398 layout->ltr_context = ltr_context;
399 g_object_ref (layout->ltr_context);
402 if (layout->rtl_context != rtl_context)
404 if (layout->rtl_context)
405 g_object_unref (layout->rtl_context);
407 layout->rtl_context = rtl_context;
408 g_object_ref (layout->rtl_context);
411 DV (g_print ("invalidating all due to new pango contexts (%s)\n", G_STRLOC));
412 gtk_text_layout_invalidate_all (layout);
416 * gtk_text_layout_set_overwrite_mode:
417 * @layout: a #GtkTextLayout
418 * @overwrite: overwrite mode
420 * Sets overwrite mode
423 gtk_text_layout_set_overwrite_mode (GtkTextLayout *layout,
426 overwrite = overwrite != 0;
427 if (overwrite != layout->overwrite_mode)
429 layout->overwrite_mode = overwrite;
430 gtk_text_layout_invalidate_cursor_line (layout, TRUE);
435 * gtk_text_layout_set_cursor_direction:
436 * @direction: the new direction(s) for which to draw cursors.
437 * %GTK_TEXT_DIR_NONE means draw cursors for both
438 * left-to-right insertion and right-to-left insertion.
439 * (The two cursors will be visually distinguished.)
441 * Sets which text directions (left-to-right and/or right-to-left) for
442 * which cursors will be drawn for the insertion point. The visual
443 * point at which new text is inserted depends on whether the new
444 * text is right-to-left or left-to-right, so it may be desired to
445 * make the drawn position of the cursor depend on the keyboard state.
448 gtk_text_layout_set_cursor_direction (GtkTextLayout *layout,
449 GtkTextDirection direction)
451 if (direction != layout->cursor_direction)
453 layout->cursor_direction = direction;
454 gtk_text_layout_invalidate_cursor_line (layout, TRUE);
459 * gtk_text_layout_set_keyboard_direction:
460 * @keyboard_dir: the current direction of the keyboard.
462 * Sets the keyboard direction; this is used as for the bidirectional
463 * base direction for the line with the cursor if the line contains
464 * only neutral characters.
467 gtk_text_layout_set_keyboard_direction (GtkTextLayout *layout,
468 GtkTextDirection keyboard_dir)
470 if (keyboard_dir != layout->keyboard_direction)
472 layout->keyboard_direction = keyboard_dir;
473 gtk_text_layout_invalidate_cursor_line (layout, TRUE);
478 * gtk_text_layout_get_buffer:
479 * @layout: a #GtkTextLayout
481 * Gets the text buffer used by the layout. See
482 * gtk_text_layout_set_buffer().
484 * Return value: the text buffer used by the layout.
487 gtk_text_layout_get_buffer (GtkTextLayout *layout)
489 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), NULL);
491 return layout->buffer;
495 gtk_text_layout_set_screen_width (GtkTextLayout *layout, gint width)
497 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
498 g_return_if_fail (width >= 0);
499 g_return_if_fail (layout->wrap_loop_count == 0);
501 if (layout->screen_width == width)
504 layout->screen_width = width;
506 DV (g_print ("invalidating all due to new screen width (%s)\n", G_STRLOC));
507 gtk_text_layout_invalidate_all (layout);
511 * gtk_text_layout_set_cursor_visible:
512 * @layout: a #GtkTextLayout
513 * @cursor_visible: If %FALSE, then the insertion cursor will not
514 * be shown, even if the text is editable.
516 * Sets whether the insertion cursor should be shown. Generally,
517 * widgets using #GtkTextLayout will hide the cursor when the
518 * widget does not have the input focus.
521 gtk_text_layout_set_cursor_visible (GtkTextLayout *layout,
522 gboolean cursor_visible)
524 cursor_visible = (cursor_visible != FALSE);
526 if (layout->cursor_visible != cursor_visible)
531 layout->cursor_visible = cursor_visible;
533 /* Now queue a redraw on the paragraph containing the cursor
535 gtk_text_buffer_get_iter_at_mark (layout->buffer, &iter,
536 gtk_text_buffer_get_insert (layout->buffer));
538 gtk_text_layout_get_line_yrange (layout, &iter, &y, &height);
539 gtk_text_layout_emit_changed (layout, y, height, height);
541 gtk_text_layout_invalidate_cache (layout, _gtk_text_iter_get_text_line (&iter), TRUE);
546 * gtk_text_layout_get_cursor_visible:
547 * @layout: a #GtkTextLayout
549 * Returns whether the insertion cursor will be shown.
551 * Return value: if %FALSE, the insertion cursor will not be
552 * shown, even if the text is editable.
555 gtk_text_layout_get_cursor_visible (GtkTextLayout *layout)
557 return layout->cursor_visible;
561 * gtk_text_layout_set_preedit_string:
562 * @layout: a #PangoLayout
563 * @preedit_string: a string to display at the insertion point
564 * @preedit_attrs: a #PangoAttrList of attributes that apply to @preedit_string
565 * @cursor_pos: position of cursor within preedit string in chars
567 * Set the preedit string and attributes. The preedit string is a
568 * string showing text that is currently being edited and not
569 * yet committed into the buffer.
572 gtk_text_layout_set_preedit_string (GtkTextLayout *layout,
573 const gchar *preedit_string,
574 PangoAttrList *preedit_attrs,
577 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
578 g_return_if_fail (preedit_attrs != NULL || preedit_string == NULL);
580 g_free (layout->preedit_string);
582 if (layout->preedit_attrs)
583 pango_attr_list_unref (layout->preedit_attrs);
587 layout->preedit_string = g_strdup (preedit_string);
588 layout->preedit_len = strlen (layout->preedit_string);
589 pango_attr_list_ref (preedit_attrs);
590 layout->preedit_attrs = preedit_attrs;
592 cursor_pos = CLAMP (cursor_pos, 0, g_utf8_strlen (layout->preedit_string, -1));
593 layout->preedit_cursor = g_utf8_offset_to_pointer (layout->preedit_string, cursor_pos) - layout->preedit_string;
597 layout->preedit_string = NULL;
598 layout->preedit_len = 0;
599 layout->preedit_attrs = NULL;
600 layout->preedit_cursor = 0;
603 gtk_text_layout_invalidate_cursor_line (layout, FALSE);
607 gtk_text_layout_get_size (GtkTextLayout *layout,
611 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
614 *width = layout->width;
617 *height = layout->height;
621 gtk_text_layout_invalidated (GtkTextLayout *layout)
623 g_signal_emit (layout, signals[INVALIDATED], 0);
627 gtk_text_layout_emit_changed (GtkTextLayout *layout,
632 g_signal_emit (layout, signals[CHANGED], 0, y, old_height, new_height);
636 text_layout_changed (GtkTextLayout *layout,
640 gboolean cursors_only)
642 /* Check if the range intersects our cached line display,
643 * and invalidate the cached line if so.
645 if (layout->one_display_cache)
647 GtkTextLine *line = layout->one_display_cache->line;
648 gint cache_y = _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
650 gint cache_height = layout->one_display_cache->height;
652 if (cache_y + cache_height > y && cache_y < y + old_height)
653 gtk_text_layout_invalidate_cache (layout, line, cursors_only);
656 gtk_text_layout_emit_changed (layout, y, old_height, new_height);
660 gtk_text_layout_changed (GtkTextLayout *layout,
665 text_layout_changed (layout, y, old_height, new_height, FALSE);
669 gtk_text_layout_cursors_changed (GtkTextLayout *layout,
674 text_layout_changed (layout, y, old_height, new_height, TRUE);
678 gtk_text_layout_free_line_data (GtkTextLayout *layout,
680 GtkTextLineData *line_data)
682 GTK_TEXT_LAYOUT_GET_CLASS (layout)->free_line_data (layout, line, line_data);
686 gtk_text_layout_invalidate (GtkTextLayout *layout,
687 const GtkTextIter *start_index,
688 const GtkTextIter *end_index)
690 GTK_TEXT_LAYOUT_GET_CLASS (layout)->invalidate (layout, start_index, end_index);
694 gtk_text_layout_invalidate_cursors (GtkTextLayout *layout,
695 const GtkTextIter *start_index,
696 const GtkTextIter *end_index)
698 GTK_TEXT_LAYOUT_GET_CLASS (layout)->invalidate_cursors (layout, start_index, end_index);
702 gtk_text_layout_wrap (GtkTextLayout *layout,
705 GtkTextLineData *line_data)
707 return GTK_TEXT_LAYOUT_GET_CLASS (layout)->wrap (layout, line, line_data);
712 * gtk_text_layout_get_lines:
714 * Return value: (element-type GtkTextLine) (transfer container):
717 gtk_text_layout_get_lines (GtkTextLayout *layout,
718 /* [top_y, bottom_y) */
723 GtkTextLine *first_btree_line;
724 GtkTextLine *last_btree_line;
728 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), NULL);
729 g_return_val_if_fail (bottom_y > top_y, NULL);
734 _gtk_text_btree_find_line_by_y (_gtk_text_buffer_get_btree (layout->buffer),
735 layout, top_y, first_line_y);
736 if (first_btree_line == NULL)
742 /* -1 since bottom_y is one past */
744 _gtk_text_btree_find_line_by_y (_gtk_text_buffer_get_btree (layout->buffer),
745 layout, bottom_y - 1, NULL);
747 if (!last_btree_line)
749 _gtk_text_btree_get_end_iter_line (_gtk_text_buffer_get_btree (layout->buffer));
751 g_assert (last_btree_line != NULL);
753 line = first_btree_line;
756 retval = g_slist_prepend (retval, line);
758 if (line == last_btree_line)
761 line = _gtk_text_line_next_excluding_last (line);
764 retval = g_slist_reverse (retval);
770 invalidate_cached_style (GtkTextLayout *layout)
772 free_style_cache (layout);
775 /* These should be called around a loop which wraps a CONTIGUOUS bunch
776 * of display lines. If the lines aren't contiguous you can't call
780 gtk_text_layout_wrap_loop_start (GtkTextLayout *layout)
782 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
783 g_return_if_fail (layout->one_style_cache == NULL);
785 layout->wrap_loop_count += 1;
789 gtk_text_layout_wrap_loop_end (GtkTextLayout *layout)
791 g_return_if_fail (layout->wrap_loop_count > 0);
793 layout->wrap_loop_count -= 1;
795 if (layout->wrap_loop_count == 0)
797 /* We cache a some stuff if we're iterating over some lines wrapping
798 * them. This cleans it up.
800 /* Nuke our cached style */
801 invalidate_cached_style (layout);
802 g_assert (layout->one_style_cache == NULL);
807 gtk_text_layout_invalidate_all (GtkTextLayout *layout)
812 if (layout->buffer == NULL)
815 gtk_text_buffer_get_bounds (layout->buffer, &start, &end);
817 gtk_text_layout_invalidate (layout, &start, &end);
821 gtk_text_layout_invalidate_cache (GtkTextLayout *layout,
823 gboolean cursors_only)
825 if (layout->one_display_cache && line == layout->one_display_cache->line)
827 GtkTextLineDisplay *display = layout->one_display_cache;
831 g_slist_foreach (display->cursors, (GFunc)g_free, NULL);
832 g_slist_free (display->cursors);
833 display->cursors = NULL;
834 display->cursors_invalid = TRUE;
835 display->has_block_cursor = FALSE;
839 layout->one_display_cache = NULL;
840 gtk_text_layout_free_line_display (layout, display);
845 /* Now invalidate the paragraph containing the cursor
848 gtk_text_layout_invalidate_cursor_line (GtkTextLayout *layout,
849 gboolean cursors_only)
851 GtkTextLayoutPrivate *priv = GTK_TEXT_LAYOUT_GET_PRIVATE (layout);
852 GtkTextLineData *line_data;
854 if (priv->cursor_line == NULL)
857 line_data = _gtk_text_line_get_data (priv->cursor_line, layout);
861 gtk_text_layout_invalidate_cache (layout, priv->cursor_line, TRUE);
864 gtk_text_layout_invalidate_cache (layout, priv->cursor_line, FALSE);
865 _gtk_text_line_invalidate_wrap (priv->cursor_line, line_data);
868 gtk_text_layout_invalidated (layout);
873 gtk_text_layout_update_cursor_line(GtkTextLayout *layout)
875 GtkTextLayoutPrivate *priv = GTK_TEXT_LAYOUT_GET_PRIVATE (layout);
878 gtk_text_buffer_get_iter_at_mark (layout->buffer, &iter,
879 gtk_text_buffer_get_insert (layout->buffer));
881 priv->cursor_line = _gtk_text_iter_get_text_line (&iter);
885 gtk_text_layout_real_invalidate (GtkTextLayout *layout,
886 const GtkTextIter *start,
887 const GtkTextIter *end)
890 GtkTextLine *last_line;
892 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
893 g_return_if_fail (layout->wrap_loop_count == 0);
895 /* Because we may be invalidating a mark, it's entirely possible
896 * that gtk_text_iter_equal (start, end) in which case we
897 * should still invalidate the line they are both on. i.e.
898 * we always invalidate the line with "start" even
899 * if there's an empty range.
903 gtk_text_view_index_spew (start_index, "invalidate start");
904 gtk_text_view_index_spew (end_index, "invalidate end");
907 last_line = _gtk_text_iter_get_text_line (end);
908 line = _gtk_text_iter_get_text_line (start);
912 GtkTextLineData *line_data = _gtk_text_line_get_data (line, layout);
914 gtk_text_layout_invalidate_cache (layout, line, FALSE);
917 _gtk_text_line_invalidate_wrap (line, line_data);
919 if (line == last_line)
922 line = _gtk_text_line_next_excluding_last (line);
925 gtk_text_layout_invalidated (layout);
929 gtk_text_layout_real_invalidate_cursors (GtkTextLayout *layout,
930 const GtkTextIter *start,
931 const GtkTextIter *end)
933 /* Check if the range intersects our cached line display,
934 * and invalidate the cached line if so.
936 if (layout->one_display_cache)
938 GtkTextIter line_start, line_end;
939 GtkTextLine *line = layout->one_display_cache->line;
941 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
942 &line_start, line, 0);
943 line_end = line_start;
944 if (!gtk_text_iter_ends_line (&line_end))
945 gtk_text_iter_forward_to_line_end (&line_end);
947 if (gtk_text_iter_compare (start, end) > 0)
949 const GtkTextIter *tmp = start;
954 if (gtk_text_iter_compare (&line_start, end) <= 0 &&
955 gtk_text_iter_compare (start, &line_end) <= 0)
957 gtk_text_layout_invalidate_cache (layout, line, TRUE);
961 gtk_text_layout_invalidated (layout);
965 gtk_text_layout_real_free_line_data (GtkTextLayout *layout,
967 GtkTextLineData *line_data)
969 gtk_text_layout_invalidate_cache (layout, line, FALSE);
975 * gtk_text_layout_is_valid:
976 * @layout: a #GtkTextLayout
978 * Check if there are any invalid regions in a #GtkTextLayout's buffer
980 * Return value: %TRUE if any invalid regions were found
983 gtk_text_layout_is_valid (GtkTextLayout *layout)
985 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), FALSE);
987 return _gtk_text_btree_is_valid (_gtk_text_buffer_get_btree (layout->buffer),
992 update_layout_size (GtkTextLayout *layout)
994 _gtk_text_btree_get_view_size (_gtk_text_buffer_get_btree (layout->buffer),
996 &layout->width, &layout->height);
1000 * gtk_text_layout_validate_yrange:
1001 * @layout: a #GtkTextLayout
1002 * @anchor: iter pointing into a line that will be used as the
1004 * @y0_: offset from the top of the line pointed to by @anchor at
1005 * which to begin validation. (The offset here is in pixels
1006 * after validation.)
1007 * @y1_: offset from the top of the line pointed to by @anchor at
1008 * which to end validation. (The offset here is in pixels
1009 * after validation.)
1011 * Ensure that a region of a #GtkTextLayout is valid. The ::changed
1012 * signal will be emitted if any lines are validated.
1015 gtk_text_layout_validate_yrange (GtkTextLayout *layout,
1016 GtkTextIter *anchor,
1021 GtkTextLine *first_line = NULL;
1022 GtkTextLine *last_line = NULL;
1024 gint delta_height = 0;
1025 gint first_line_y = 0; /* Quiet GCC */
1026 gint last_line_y = 0; /* Quiet GCC */
1028 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
1035 /* Validate backwards from the anchor line to y0
1037 line = _gtk_text_iter_get_text_line (anchor);
1038 line = _gtk_text_line_previous (line);
1040 while (line && seen < -y0)
1042 GtkTextLineData *line_data = _gtk_text_line_get_data (line, layout);
1043 if (!line_data || !line_data->valid)
1045 gint old_height, new_height;
1047 old_height = line_data ? line_data->height : 0;
1049 _gtk_text_btree_validate_line (_gtk_text_buffer_get_btree (layout->buffer),
1051 line_data = _gtk_text_line_get_data (line, layout);
1053 new_height = line_data ? line_data->height : 0;
1055 delta_height += new_height - old_height;
1058 first_line_y = -seen - new_height;
1062 last_line_y = -seen;
1066 seen += line_data ? line_data->height : 0;
1067 line = _gtk_text_line_previous (line);
1070 /* Validate forwards to y1 */
1071 line = _gtk_text_iter_get_text_line (anchor);
1073 while (line && seen < y1)
1075 GtkTextLineData *line_data = _gtk_text_line_get_data (line, layout);
1076 if (!line_data || !line_data->valid)
1078 gint old_height, new_height;
1080 old_height = line_data ? line_data->height : 0;
1082 _gtk_text_btree_validate_line (_gtk_text_buffer_get_btree (layout->buffer),
1084 line_data = _gtk_text_line_get_data (line, layout);
1085 new_height = line_data ? line_data->height : 0;
1087 delta_height += new_height - old_height;
1092 first_line_y = seen;
1095 last_line_y = seen + new_height;
1098 seen += line_data ? line_data->height : 0;
1099 line = _gtk_text_line_next_excluding_last (line);
1102 /* If we found and validated any invalid lines, update size and
1103 * emit the changed signal
1109 update_layout_size (layout);
1111 line_top = _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
1112 first_line, layout);
1114 gtk_text_layout_emit_changed (layout,
1116 last_line_y - first_line_y - delta_height,
1117 last_line_y - first_line_y);
1122 * gtk_text_layout_validate:
1123 * @tree: a #GtkTextLayout
1124 * @max_pixels: the maximum number of pixels to validate. (No more
1125 * than one paragraph beyond this limit will be validated)
1127 * Validate regions of a #GtkTextLayout. The ::changed signal will
1128 * be emitted for each region validated.
1131 gtk_text_layout_validate (GtkTextLayout *layout,
1134 gint y, old_height, new_height;
1136 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
1138 while (max_pixels > 0 &&
1139 _gtk_text_btree_validate (_gtk_text_buffer_get_btree (layout->buffer),
1141 &y, &old_height, &new_height))
1143 max_pixels -= new_height;
1145 update_layout_size (layout);
1146 gtk_text_layout_emit_changed (layout, y, old_height, new_height);
1150 static GtkTextLineData*
1151 gtk_text_layout_real_wrap (GtkTextLayout *layout,
1154 GtkTextLineData *line_data)
1156 GtkTextLineDisplay *display;
1158 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), NULL);
1159 g_return_val_if_fail (line != NULL, NULL);
1161 if (line_data == NULL)
1163 line_data = _gtk_text_line_data_new (layout, line);
1164 _gtk_text_line_add_data (line, line_data);
1167 display = gtk_text_layout_get_line_display (layout, line, TRUE);
1168 line_data->width = display->width;
1169 line_data->height = display->height;
1170 line_data->valid = TRUE;
1171 gtk_text_layout_free_line_display (layout, display);
1177 * Layout utility functions
1180 /* If you get the style with get_style () you need to call
1181 release_style () to free it. */
1182 static GtkTextAttributes*
1183 get_style (GtkTextLayout *layout,
1186 GtkTextAttributes *style;
1188 /* If we have the one-style cache, then it means
1189 that we haven't seen a toggle since we filled in the
1192 if (layout->one_style_cache != NULL)
1194 gtk_text_attributes_ref (layout->one_style_cache);
1195 return layout->one_style_cache;
1198 g_assert (layout->one_style_cache == NULL);
1200 /* No tags, use default style */
1201 if (tags == NULL || tags->len == 0)
1203 /* One ref for the return value, one ref for the
1204 layout->one_style_cache reference */
1205 gtk_text_attributes_ref (layout->default_style);
1206 gtk_text_attributes_ref (layout->default_style);
1207 layout->one_style_cache = layout->default_style;
1209 return layout->default_style;
1212 style = gtk_text_attributes_new ();
1214 gtk_text_attributes_copy_values (layout->default_style,
1217 _gtk_text_attributes_fill_from_tags (style,
1218 (GtkTextTag**) tags->pdata,
1221 g_assert (style->refcount == 1);
1223 /* Leave this style as the last one seen */
1224 g_assert (layout->one_style_cache == NULL);
1225 gtk_text_attributes_ref (style); /* ref held by layout->one_style_cache */
1226 layout->one_style_cache = style;
1228 /* Returning yet another refcount */
1233 release_style (GtkTextLayout *layout,
1234 GtkTextAttributes *style)
1236 g_return_if_fail (style != NULL);
1237 g_return_if_fail (style->refcount > 0);
1239 gtk_text_attributes_unref (style);
1246 /* This function tries to optimize the case where a line
1247 is completely invisible */
1249 totally_invisible_line (GtkTextLayout *layout,
1253 GtkTextLineSegment *seg;
1256 /* Check if the first char is visible, if so we are partially visible.
1257 * Note that we have to check this since we don't know the current
1258 * invisible/noninvisible toggle state; this function can use the whole btree
1261 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
1264 if (!_gtk_text_btree_char_is_invisible (iter))
1268 seg = line->segments;
1272 if (seg->byte_count > 0)
1273 bytes += seg->byte_count;
1275 /* Note that these two tests can cause us to bail out
1276 * when we shouldn't, because a higher-priority tag
1277 * may override these settings. However the important
1278 * thing is to only invisible really-invisible lines, rather
1279 * than to invisible all really-invisible lines.
1282 else if (seg->type == >k_text_toggle_on_type)
1284 invalidate_cached_style (layout);
1286 /* Bail out if an elision-unsetting tag begins */
1287 if (seg->body.toggle.info->tag->invisible_set &&
1288 !seg->body.toggle.info->tag->values->invisible)
1291 else if (seg->type == >k_text_toggle_off_type)
1293 invalidate_cached_style (layout);
1295 /* Bail out if an elision-setting tag ends */
1296 if (seg->body.toggle.info->tag->invisible_set &&
1297 seg->body.toggle.info->tag->values->invisible)
1304 if (seg != NULL) /* didn't reach line end */
1311 set_para_values (GtkTextLayout *layout,
1312 PangoDirection base_dir,
1313 GtkTextAttributes *style,
1314 GtkTextLineDisplay *display)
1316 PangoAlignment pango_align = PANGO_ALIGN_LEFT;
1317 PangoWrapMode pango_wrap = PANGO_WRAP_WORD;
1321 /* If no base direction was found, then use the style direction */
1322 case PANGO_DIRECTION_NEUTRAL :
1323 display->direction = style->direction;
1325 /* Override the base direction */
1326 if (display->direction == GTK_TEXT_DIR_RTL)
1327 base_dir = PANGO_DIRECTION_RTL;
1329 base_dir = PANGO_DIRECTION_LTR;
1332 case PANGO_DIRECTION_RTL :
1333 display->direction = GTK_TEXT_DIR_RTL;
1336 display->direction = GTK_TEXT_DIR_LTR;
1340 if (display->direction == GTK_TEXT_DIR_RTL)
1341 display->layout = pango_layout_new (layout->rtl_context);
1343 display->layout = pango_layout_new (layout->ltr_context);
1345 switch (style->justification)
1347 case GTK_JUSTIFY_LEFT:
1348 pango_align = (base_dir == PANGO_DIRECTION_LTR) ? PANGO_ALIGN_LEFT : PANGO_ALIGN_RIGHT;
1350 case GTK_JUSTIFY_RIGHT:
1351 pango_align = (base_dir == PANGO_DIRECTION_LTR) ? PANGO_ALIGN_RIGHT : PANGO_ALIGN_LEFT;
1353 case GTK_JUSTIFY_CENTER:
1354 pango_align = PANGO_ALIGN_CENTER;
1356 case GTK_JUSTIFY_FILL:
1357 pango_align = (base_dir == PANGO_DIRECTION_LTR) ? PANGO_ALIGN_LEFT : PANGO_ALIGN_RIGHT;
1358 pango_layout_set_justify (display->layout, TRUE);
1361 g_assert_not_reached ();
1365 pango_layout_set_alignment (display->layout, pango_align);
1366 pango_layout_set_spacing (display->layout,
1367 style->pixels_inside_wrap * PANGO_SCALE);
1370 pango_layout_set_tabs (display->layout, style->tabs);
1372 display->top_margin = style->pixels_above_lines;
1373 display->height = style->pixels_above_lines + style->pixels_below_lines;
1374 display->bottom_margin = style->pixels_below_lines;
1375 display->left_margin = style->left_margin;
1376 display->right_margin = style->right_margin;
1378 display->x_offset = display->left_margin;
1380 pango_layout_set_indent (display->layout,
1381 style->indent * PANGO_SCALE);
1383 switch (style->wrap_mode)
1386 pango_wrap = PANGO_WRAP_CHAR;
1389 pango_wrap = PANGO_WRAP_WORD;
1392 case GTK_WRAP_WORD_CHAR:
1393 pango_wrap = PANGO_WRAP_WORD_CHAR;
1400 if (style->wrap_mode != GTK_WRAP_NONE)
1402 int layout_width = (layout->screen_width - display->left_margin - display->right_margin);
1403 pango_layout_set_width (display->layout, layout_width * PANGO_SCALE);
1404 pango_layout_set_wrap (display->layout, pango_wrap);
1407 display->total_width = MAX (layout->screen_width, layout->width) - display->left_margin - display->right_margin;
1409 if (style->pg_bg_color)
1410 display->pg_bg_color = gdk_color_copy (style->pg_bg_color);
1412 display->pg_bg_color = NULL;
1415 static PangoAttribute *
1416 gtk_text_attr_appearance_copy (const PangoAttribute *attr)
1418 const GtkTextAttrAppearance *appearance_attr = (const GtkTextAttrAppearance *)attr;
1420 return gtk_text_attr_appearance_new (&appearance_attr->appearance);
1424 gtk_text_attr_appearance_destroy (PangoAttribute *attr)
1426 GtkTextAttrAppearance *appearance_attr = (GtkTextAttrAppearance *)attr;
1428 if (appearance_attr->appearance.bg_stipple)
1429 g_object_unref (appearance_attr->appearance.bg_stipple);
1430 if (appearance_attr->appearance.fg_stipple)
1431 g_object_unref (appearance_attr->appearance.fg_stipple);
1433 g_slice_free (GtkTextAttrAppearance, appearance_attr);
1437 gtk_text_attr_appearance_compare (const PangoAttribute *attr1,
1438 const PangoAttribute *attr2)
1440 const GtkTextAppearance *appearance1 = &((const GtkTextAttrAppearance *)attr1)->appearance;
1441 const GtkTextAppearance *appearance2 = &((const GtkTextAttrAppearance *)attr2)->appearance;
1443 return (gdk_color_equal (&appearance1->fg_color, &appearance2->fg_color) &&
1444 gdk_color_equal (&appearance1->bg_color, &appearance2->bg_color) &&
1445 appearance1->fg_stipple == appearance2->fg_stipple &&
1446 appearance1->bg_stipple == appearance2->bg_stipple &&
1447 appearance1->underline == appearance2->underline &&
1448 appearance1->strikethrough == appearance2->strikethrough &&
1449 appearance1->draw_bg == appearance2->draw_bg);
1453 * gtk_text_attr_appearance_new:
1456 * Create a new font description attribute. (This attribute
1457 * allows setting family, style, weight, variant, stretch,
1458 * and size simultaneously.)
1462 static PangoAttribute *
1463 gtk_text_attr_appearance_new (const GtkTextAppearance *appearance)
1465 static PangoAttrClass klass = {
1467 gtk_text_attr_appearance_copy,
1468 gtk_text_attr_appearance_destroy,
1469 gtk_text_attr_appearance_compare
1472 GtkTextAttrAppearance *result;
1475 klass.type = gtk_text_attr_appearance_type =
1476 pango_attr_type_register ("GtkTextAttrAppearance");
1478 result = g_slice_new (GtkTextAttrAppearance);
1479 result->attr.klass = &klass;
1481 result->appearance = *appearance;
1483 if (appearance->bg_stipple)
1484 g_object_ref (appearance->bg_stipple);
1485 if (appearance->fg_stipple)
1486 g_object_ref (appearance->fg_stipple);
1488 return (PangoAttribute *)result;
1492 add_generic_attrs (GtkTextLayout *layout,
1493 GtkTextAppearance *appearance,
1495 PangoAttrList *attrs,
1500 PangoAttribute *attr;
1502 if (appearance->underline != PANGO_UNDERLINE_NONE)
1504 attr = pango_attr_underline_new (appearance->underline);
1506 attr->start_index = start;
1507 attr->end_index = start + byte_count;
1509 pango_attr_list_insert (attrs, attr);
1512 if (appearance->strikethrough)
1514 attr = pango_attr_strikethrough_new (appearance->strikethrough);
1516 attr->start_index = start;
1517 attr->end_index = start + byte_count;
1519 pango_attr_list_insert (attrs, attr);
1522 if (appearance->rise != 0)
1524 attr = pango_attr_rise_new (appearance->rise);
1526 attr->start_index = start;
1527 attr->end_index = start + byte_count;
1529 pango_attr_list_insert (attrs, attr);
1534 attr = gtk_text_attr_appearance_new (appearance);
1536 attr->start_index = start;
1537 attr->end_index = start + byte_count;
1539 ((GtkTextAttrAppearance *)attr)->appearance.is_text = is_text;
1541 pango_attr_list_insert (attrs, attr);
1546 add_text_attrs (GtkTextLayout *layout,
1547 GtkTextAttributes *style,
1549 PangoAttrList *attrs,
1553 PangoAttribute *attr;
1555 attr = pango_attr_font_desc_new (style->font);
1556 attr->start_index = start;
1557 attr->end_index = start + byte_count;
1559 pango_attr_list_insert (attrs, attr);
1561 if (style->font_scale != 1.0)
1563 attr = pango_attr_scale_new (style->font_scale);
1565 attr->start_index = start;
1566 attr->end_index = start + byte_count;
1568 pango_attr_list_insert (attrs, attr);
1573 add_pixbuf_attrs (GtkTextLayout *layout,
1574 GtkTextLineDisplay *display,
1575 GtkTextAttributes *style,
1576 GtkTextLineSegment *seg,
1577 PangoAttrList *attrs,
1580 PangoAttribute *attr;
1581 PangoRectangle logical_rect;
1582 GtkTextPixbuf *pixbuf = &seg->body.pixbuf;
1585 width = gdk_pixbuf_get_width (pixbuf->pixbuf);
1586 height = gdk_pixbuf_get_height (pixbuf->pixbuf);
1589 logical_rect.y = -height * PANGO_SCALE;
1590 logical_rect.width = width * PANGO_SCALE;
1591 logical_rect.height = height * PANGO_SCALE;
1593 attr = pango_attr_shape_new_with_data (&logical_rect, &logical_rect,
1594 pixbuf->pixbuf, NULL, NULL);
1595 attr->start_index = start;
1596 attr->end_index = start + seg->byte_count;
1597 pango_attr_list_insert (attrs, attr);
1599 display->shaped_objects =
1600 g_slist_append (display->shaped_objects, pixbuf->pixbuf);
1604 add_child_attrs (GtkTextLayout *layout,
1605 GtkTextLineDisplay *display,
1606 GtkTextAttributes *style,
1607 GtkTextLineSegment *seg,
1608 PangoAttrList *attrs,
1611 PangoAttribute *attr;
1612 PangoRectangle logical_rect;
1620 tmp_list = seg->body.child.widgets;
1621 while (tmp_list != NULL)
1623 GtkWidget *child = tmp_list->data;
1625 if (_gtk_anchored_child_get_layout (child) == layout)
1630 gtk_widget_get_child_requisition (child, &req);
1633 height = req.height;
1640 tmp_list = g_slist_next (tmp_list);
1643 if (tmp_list == NULL)
1645 /* If tmp_list == NULL then there is no widget at this anchor in
1646 * this display; not an error. We make up an arbitrary size
1647 * to use, just so the programmer can see the blank spot.
1648 * We also put a NULL in the shaped objects list, to keep
1649 * the correspondence between the list and the shaped chars in
1650 * the layout. A bad hack, yes.
1659 display->shaped_objects = g_slist_append (display->shaped_objects, widget);
1662 logical_rect.y = -height * PANGO_SCALE;
1663 logical_rect.width = width * PANGO_SCALE;
1664 logical_rect.height = height * PANGO_SCALE;
1666 attr = pango_attr_shape_new_with_data (&logical_rect, &logical_rect,
1667 widget, NULL, NULL);
1668 attr->start_index = start;
1669 attr->end_index = start + seg->byte_count;
1670 pango_attr_list_insert (attrs, attr);
1675 * @layout: a #GtkTextLayout
1676 * @display: a #GtkTextLineDisplay
1677 * @insert_iter: iter pointing to the cursor location
1678 * @insert_index: cursor offset in the @display's layout, it may
1679 * be different from @insert_iter's offset in case when preedit
1680 * string is present.
1681 * @pos: location to store cursor position
1682 * @cursor_at_line_end: whether cursor is at the end of line
1684 * Checks whether layout should display block cursor at given position.
1685 * For this layout must be in overwrite mode and text at @insert_iter
1689 get_block_cursor (GtkTextLayout *layout,
1690 GtkTextLineDisplay *display,
1691 const GtkTextIter *insert_iter,
1694 gboolean *cursor_at_line_end)
1696 PangoRectangle pango_pos;
1698 if (layout->overwrite_mode &&
1699 gtk_text_iter_editable (insert_iter, TRUE) &&
1700 _gtk_text_util_get_block_cursor_location (display->layout,
1703 cursor_at_line_end))
1707 pos->x = PANGO_PIXELS (pango_pos.x);
1708 pos->y = PANGO_PIXELS (pango_pos.y);
1709 pos->width = PANGO_PIXELS (pango_pos.width);
1710 pos->height = PANGO_PIXELS (pango_pos.height);
1720 add_cursor (GtkTextLayout *layout,
1721 GtkTextLineDisplay *display,
1722 GtkTextLineSegment *seg,
1725 PangoRectangle strong_pos, weak_pos;
1726 GtkTextCursorDisplay *cursor = NULL; /* Quiet GCC */
1727 gboolean add_weak = FALSE;
1728 gboolean add_strong = FALSE;
1730 /* Hide insertion cursor when we have a selection or the layout
1731 * user has hidden the cursor.
1733 if (_gtk_text_btree_mark_is_insert (_gtk_text_buffer_get_btree (layout->buffer),
1734 seg->body.mark.obj) &&
1735 (!layout->cursor_visible ||
1736 gtk_text_buffer_get_selection_bounds (layout->buffer, NULL, NULL)))
1739 if (layout->overwrite_mode &&
1740 _gtk_text_btree_mark_is_insert (_gtk_text_buffer_get_btree (layout->buffer),
1741 seg->body.mark.obj))
1744 gboolean cursor_at_line_end;
1746 _gtk_text_btree_get_iter_at_mark (_gtk_text_buffer_get_btree (layout->buffer),
1747 &iter, seg->body.mark.obj);
1749 if (get_block_cursor (layout, display, &iter, start,
1750 &display->block_cursor,
1751 &cursor_at_line_end))
1753 display->has_block_cursor = TRUE;
1754 display->cursor_at_line_end = cursor_at_line_end;
1759 pango_layout_get_cursor_pos (display->layout, start, &strong_pos, &weak_pos);
1761 if (layout->cursor_direction == GTK_TEXT_DIR_NONE)
1766 else if (display->direction == layout->cursor_direction)
1773 cursor = g_new (GtkTextCursorDisplay, 1);
1775 cursor->x = PANGO_PIXELS (strong_pos.x);
1776 cursor->y = PANGO_PIXELS (strong_pos.y);
1777 cursor->height = PANGO_PIXELS (strong_pos.height);
1778 cursor->is_strong = TRUE;
1779 cursor->is_weak = (layout->cursor_direction == GTK_TEXT_DIR_NONE) ? FALSE : TRUE;
1780 display->cursors = g_slist_prepend (display->cursors, cursor);
1785 if (weak_pos.x == strong_pos.x && add_strong)
1786 cursor->is_weak = TRUE;
1789 cursor = g_new (GtkTextCursorDisplay, 1);
1791 cursor->x = PANGO_PIXELS (weak_pos.x);
1792 cursor->y = PANGO_PIXELS (weak_pos.y);
1793 cursor->height = PANGO_PIXELS (weak_pos.height);
1794 cursor->is_strong = (layout->cursor_direction == GTK_TEXT_DIR_NONE) ? FALSE : TRUE;
1795 cursor->is_weak = TRUE;
1796 display->cursors = g_slist_prepend (display->cursors, cursor);
1802 is_shape (PangoLayoutRun *run)
1804 GSList *tmp_list = run->item->analysis.extra_attrs;
1808 PangoAttribute *attr = tmp_list->data;
1810 if (attr->klass->type == PANGO_ATTR_SHAPE)
1813 tmp_list = tmp_list->next;
1820 allocate_child_widgets (GtkTextLayout *text_layout,
1821 GtkTextLineDisplay *display)
1823 PangoLayout *layout = display->layout;
1824 PangoLayoutIter *run_iter;
1826 run_iter = pango_layout_get_iter (layout);
1829 PangoLayoutRun *run = pango_layout_iter_get_run_readonly (run_iter);
1831 if (run && is_shape (run))
1834 GtkTextIter text_iter;
1835 GtkTextChildAnchor *anchor = NULL;
1836 GList *widgets = NULL;
1839 /* The pango iterator iterates in visual order.
1840 * We use the byte index to find the child widget.
1842 byte_index = pango_layout_iter_get_index (run_iter);
1843 line_display_index_to_iter (text_layout, display, &text_iter, byte_index, 0);
1844 anchor = gtk_text_iter_get_child_anchor (&text_iter);
1846 widgets = gtk_text_child_anchor_get_widgets (anchor);
1848 for (l = widgets; l; l = l->next)
1850 PangoRectangle extents;
1851 GtkWidget *child = l->data;
1853 if (_gtk_anchored_child_get_layout (child) == text_layout)
1856 /* We emit "allocate_child" with the x,y of
1857 * the widget with respect to the top of the line
1858 * and the left side of the buffer
1860 pango_layout_iter_get_run_extents (run_iter,
1864 g_signal_emit (text_layout,
1865 signals[ALLOCATE_CHILD],
1868 PANGO_PIXELS (extents.x) + display->x_offset,
1869 PANGO_PIXELS (extents.y) + display->top_margin);
1873 g_list_free (widgets);
1876 while (pango_layout_iter_next_run (run_iter));
1878 pango_layout_iter_free (run_iter);
1882 convert_color (GdkColor *result,
1883 PangoAttrColor *attr)
1885 result->red = attr->color.red;
1886 result->blue = attr->color.blue;
1887 result->green = attr->color.green;
1890 /* This function is used to convert the preedit string attributes, which are
1891 * standard PangoAttributes, into the custom attributes used by the text
1892 * widget and insert them into a attr list with a given offset.
1895 add_preedit_attrs (GtkTextLayout *layout,
1896 GtkTextAttributes *style,
1897 PangoAttrList *attrs,
1901 PangoAttrIterator *iter = pango_attr_list_get_iterator (layout->preedit_attrs);
1905 GtkTextAppearance appearance = style->appearance;
1906 PangoFontDescription *font_desc = pango_font_description_copy_static (style->font);
1907 PangoAttribute *insert_attr;
1908 GSList *extra_attrs = NULL;
1910 PangoLanguage *language;
1913 pango_attr_iterator_range (iter, &start, &end);
1915 if (end == G_MAXINT)
1916 end = layout->preedit_len;
1921 pango_attr_iterator_get_font (iter, font_desc, &language, &extra_attrs);
1923 tmp_list = extra_attrs;
1926 PangoAttribute *attr = tmp_list->data;
1928 switch (attr->klass->type)
1930 case PANGO_ATTR_FOREGROUND:
1931 convert_color (&appearance.fg_color, (PangoAttrColor *)attr);
1933 case PANGO_ATTR_BACKGROUND:
1934 convert_color (&appearance.bg_color, (PangoAttrColor *)attr);
1935 appearance.draw_bg = TRUE;
1937 case PANGO_ATTR_UNDERLINE:
1938 appearance.underline = ((PangoAttrInt *)attr)->value;
1940 case PANGO_ATTR_STRIKETHROUGH:
1941 appearance.strikethrough = ((PangoAttrInt *)attr)->value;
1943 case PANGO_ATTR_RISE:
1944 appearance.rise = ((PangoAttrInt *)attr)->value;
1950 pango_attribute_destroy (attr);
1951 tmp_list = tmp_list->next;
1954 g_slist_free (extra_attrs);
1956 insert_attr = pango_attr_font_desc_new (font_desc);
1957 insert_attr->start_index = start + offset;
1958 insert_attr->end_index = end + offset;
1960 pango_attr_list_insert (attrs, insert_attr);
1964 insert_attr = pango_attr_language_new (language);
1965 insert_attr->start_index = start + offset;
1966 insert_attr->end_index = end + offset;
1968 pango_attr_list_insert (attrs, insert_attr);
1971 add_generic_attrs (layout, &appearance, end - start,
1972 attrs, start + offset,
1975 pango_font_description_free (font_desc);
1977 while (pango_attr_iterator_next (iter));
1979 pango_attr_iterator_destroy (iter);
1982 /* Iterate over the line and fill in display->cursors.
1983 * It's a stripped copy of gtk_text_layout_get_line_display() */
1985 update_text_display_cursors (GtkTextLayout *layout,
1987 GtkTextLineDisplay *display)
1989 GtkTextLineSegment *seg;
1991 gint layout_byte_offset, buffer_byte_offset;
1992 GSList *cursor_byte_offsets = NULL;
1993 GSList *cursor_segs = NULL;
1994 GSList *tmp_list1, *tmp_list2;
1996 if (!display->cursors_invalid)
1999 display->cursors_invalid = FALSE;
2001 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
2004 /* Special-case optimization for completely
2005 * invisible lines; makes it faster to deal
2006 * with sequences of invisible lines.
2008 if (totally_invisible_line (layout, line, &iter))
2011 /* Iterate over segments */
2012 layout_byte_offset = 0; /* position in the layout text (includes preedit, does not include invisible text) */
2013 buffer_byte_offset = 0; /* position in the buffer line */
2014 seg = _gtk_text_iter_get_any_segment (&iter);
2017 /* Displayable segments */
2018 if (seg->type == >k_text_char_type ||
2019 seg->type == >k_text_pixbuf_type ||
2020 seg->type == >k_text_child_type)
2022 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
2024 buffer_byte_offset);
2026 if (!_gtk_text_btree_char_is_invisible (&iter))
2027 layout_byte_offset += seg->byte_count;
2029 buffer_byte_offset += seg->byte_count;
2033 else if (seg->type == >k_text_right_mark_type ||
2034 seg->type == >k_text_left_mark_type)
2036 gint cursor_offset = 0;
2038 /* At the insertion point, add the preedit string, if any */
2040 if (_gtk_text_btree_mark_is_insert (_gtk_text_buffer_get_btree (layout->buffer),
2041 seg->body.mark.obj))
2043 display->insert_index = layout_byte_offset;
2045 if (layout->preedit_len > 0)
2047 layout_byte_offset += layout->preedit_len;
2048 /* DO NOT increment the buffer byte offset for preedit */
2049 cursor_offset = layout->preedit_cursor - layout->preedit_len;
2053 /* Display visible marks */
2055 if (seg->body.mark.visible)
2057 cursor_byte_offsets = g_slist_prepend (cursor_byte_offsets,
2058 GINT_TO_POINTER (layout_byte_offset + cursor_offset));
2059 cursor_segs = g_slist_prepend (cursor_segs, seg);
2064 else if (seg->type == >k_text_toggle_on_type ||
2065 seg->type == >k_text_toggle_off_type)
2070 g_error ("Unknown segment type: %s", seg->type->name);
2075 tmp_list1 = cursor_byte_offsets;
2076 tmp_list2 = cursor_segs;
2079 add_cursor (layout, display, tmp_list2->data,
2080 GPOINTER_TO_INT (tmp_list1->data));
2081 tmp_list1 = tmp_list1->next;
2082 tmp_list2 = tmp_list2->next;
2084 g_slist_free (cursor_byte_offsets);
2085 g_slist_free (cursor_segs);
2088 /* Same as _gtk_text_btree_get_tags(), except it returns GPtrArray,
2089 * to be used in gtk_text_layout_get_line_display(). */
2091 get_tags_array_at_iter (GtkTextIter *iter)
2094 GPtrArray *array = NULL;
2097 tags = _gtk_text_btree_get_tags (iter, &n_tags);
2101 array = g_ptr_array_sized_new (n_tags);
2102 g_ptr_array_set_size (array, n_tags);
2103 memcpy (array->pdata, tags, n_tags * sizeof (GtkTextTag*));
2110 /* Add the tag to the array if it's not there already, and remove
2111 * it otherwise. It keeps the array sorted by tags priority. */
2113 tags_array_toggle_tag (GPtrArray *array,
2120 array = g_ptr_array_new ();
2122 tags = (GtkTextTag**) array->pdata;
2124 for (pos = 0; pos < array->len && tags[pos]->priority < tag->priority; pos++) ;
2126 if (pos < array->len && tags[pos] == tag)
2127 g_ptr_array_remove_index (array, pos);
2130 g_ptr_array_set_size (array, array->len + 1);
2131 if (pos < array->len - 1)
2132 memmove (array->pdata + pos + 1, array->pdata + pos,
2133 (array->len - pos - 1) * sizeof (GtkTextTag*));
2134 array->pdata[pos] = tag;
2140 GtkTextLineDisplay *
2141 gtk_text_layout_get_line_display (GtkTextLayout *layout,
2145 GtkTextLayoutPrivate *priv = GTK_TEXT_LAYOUT_GET_PRIVATE (layout);
2146 GtkTextLineDisplay *display;
2147 GtkTextLineSegment *seg;
2149 GtkTextAttributes *style;
2151 PangoAttrList *attrs;
2152 gint text_allocated, layout_byte_offset, buffer_byte_offset;
2153 PangoRectangle extents;
2154 gboolean para_values_set = FALSE;
2155 GSList *cursor_byte_offsets = NULL;
2156 GSList *cursor_segs = NULL;
2157 GSList *tmp_list1, *tmp_list2;
2158 gboolean saw_widget = FALSE;
2159 PangoDirection base_dir;
2161 gboolean initial_toggle_segments;
2163 g_return_val_if_fail (line != NULL, NULL);
2165 if (layout->one_display_cache)
2167 if (line == layout->one_display_cache->line &&
2168 (size_only || !layout->one_display_cache->size_only))
2171 update_text_display_cursors (layout, line, layout->one_display_cache);
2172 return layout->one_display_cache;
2176 GtkTextLineDisplay *tmp_display = layout->one_display_cache;
2177 layout->one_display_cache = NULL;
2178 gtk_text_layout_free_line_display (layout, tmp_display);
2182 DV (g_print ("creating one line display cache (%s)\n", G_STRLOC));
2184 display = g_new0 (GtkTextLineDisplay, 1);
2186 display->size_only = size_only;
2187 display->line = line;
2188 display->insert_index = -1;
2190 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
2193 /* Special-case optimization for completely
2194 * invisible lines; makes it faster to deal
2195 * with sequences of invisible lines.
2197 if (totally_invisible_line (layout, line, &iter))
2199 if (display->direction == GTK_TEXT_DIR_RTL)
2200 display->layout = pango_layout_new (layout->rtl_context);
2202 display->layout = pango_layout_new (layout->ltr_context);
2207 /* Find the bidi base direction */
2208 base_dir = line->dir_propagated_forward;
2209 if (base_dir == PANGO_DIRECTION_NEUTRAL)
2210 base_dir = line->dir_propagated_back;
2212 if (line == priv->cursor_line &&
2213 line->dir_strong == PANGO_DIRECTION_NEUTRAL)
2215 base_dir = (layout->keyboard_direction == GTK_TEXT_DIR_LTR) ?
2216 PANGO_DIRECTION_LTR : PANGO_DIRECTION_RTL;
2219 /* Allocate space for flat text for buffer
2221 text_allocated = _gtk_text_line_byte_count (line);
2222 text = g_malloc (text_allocated);
2224 attrs = pango_attr_list_new ();
2226 /* Iterate over segments, creating display chunks for them, and updating the tags array. */
2227 layout_byte_offset = 0; /* current length of layout text (includes preedit, does not include invisible text) */
2228 buffer_byte_offset = 0; /* position in the buffer line */
2229 seg = _gtk_text_iter_get_any_segment (&iter);
2230 tags = get_tags_array_at_iter (&iter);
2231 initial_toggle_segments = TRUE;
2234 /* Displayable segments */
2235 if (seg->type == >k_text_char_type ||
2236 seg->type == >k_text_pixbuf_type ||
2237 seg->type == >k_text_child_type)
2239 style = get_style (layout, tags);
2240 initial_toggle_segments = FALSE;
2242 /* We have to delay setting the paragraph values until we
2243 * hit the first pixbuf or text segment because toggles at
2244 * the beginning of the paragraph should affect the
2245 * paragraph-global values
2247 if (!para_values_set)
2249 set_para_values (layout, base_dir, style, display);
2250 para_values_set = TRUE;
2253 /* First see if the chunk is invisible, and ignore it if so. Tk
2254 * looked at tabs, wrap mode, etc. before doing this, but
2255 * that made no sense to me, so I am just skipping the
2258 if (!style->invisible)
2260 if (seg->type == >k_text_char_type)
2262 /* We don't want to split segments because of marks,
2263 * so we scan forward for more segments only
2264 * separated from us by marks. In theory, we should
2265 * also merge segments with identical styles, even
2266 * if there are toggles in-between
2270 GtkTextLineSegment *prev_seg = NULL;
2274 if (seg->type == >k_text_char_type)
2276 memcpy (text + layout_byte_offset, seg->body.chars, seg->byte_count);
2277 layout_byte_offset += seg->byte_count;
2278 buffer_byte_offset += seg->byte_count;
2279 bytes += seg->byte_count;
2281 else if (seg->type == >k_text_right_mark_type ||
2282 seg->type == >k_text_left_mark_type)
2284 /* If we have preedit string, break out of this loop - we'll almost
2285 * certainly have different attributes on the preedit string
2288 if (layout->preedit_len > 0 &&
2289 _gtk_text_btree_mark_is_insert (_gtk_text_buffer_get_btree (layout->buffer),
2290 seg->body.mark.obj))
2293 if (seg->body.mark.visible)
2295 cursor_byte_offsets = g_slist_prepend (cursor_byte_offsets, GINT_TO_POINTER (layout_byte_offset));
2296 cursor_segs = g_slist_prepend (cursor_segs, seg);
2297 if (_gtk_text_btree_mark_is_insert (_gtk_text_buffer_get_btree (layout->buffer),
2298 seg->body.mark.obj))
2299 display->insert_index = layout_byte_offset;
2309 seg = prev_seg; /* Back up one */
2310 add_generic_attrs (layout, &style->appearance,
2312 attrs, layout_byte_offset - bytes,
2314 add_text_attrs (layout, style, bytes, attrs,
2315 layout_byte_offset - bytes, size_only);
2317 else if (seg->type == >k_text_pixbuf_type)
2319 add_generic_attrs (layout,
2322 attrs, layout_byte_offset,
2324 add_pixbuf_attrs (layout, display, style,
2325 seg, attrs, layout_byte_offset);
2326 memcpy (text + layout_byte_offset, gtk_text_unknown_char_utf8,
2328 layout_byte_offset += seg->byte_count;
2329 buffer_byte_offset += seg->byte_count;
2331 else if (seg->type == >k_text_child_type)
2335 add_generic_attrs (layout, &style->appearance,
2337 attrs, layout_byte_offset,
2339 add_child_attrs (layout, display, style,
2340 seg, attrs, layout_byte_offset);
2341 memcpy (text + layout_byte_offset, gtk_text_unknown_char_utf8,
2343 layout_byte_offset += seg->byte_count;
2344 buffer_byte_offset += seg->byte_count;
2348 /* We don't know this segment type */
2349 g_assert_not_reached ();
2352 } /* if (segment was visible) */
2355 /* Invisible segment */
2356 buffer_byte_offset += seg->byte_count;
2359 release_style (layout, style);
2363 else if (seg->type == >k_text_toggle_on_type ||
2364 seg->type == >k_text_toggle_off_type)
2366 /* Style may have changed, drop our
2367 current cached style */
2368 invalidate_cached_style (layout);
2369 /* Add the tag only after we have seen some non-toggle non-mark segment,
2370 * otherwise the tag is already accounted for by _gtk_text_btree_get_tags(). */
2371 if (!initial_toggle_segments)
2372 tags = tags_array_toggle_tag (tags, seg->body.toggle.info->tag);
2376 else if (seg->type == >k_text_right_mark_type ||
2377 seg->type == >k_text_left_mark_type)
2379 gint cursor_offset = 0;
2381 /* At the insertion point, add the preedit string, if any */
2383 if (_gtk_text_btree_mark_is_insert (_gtk_text_buffer_get_btree (layout->buffer),
2384 seg->body.mark.obj))
2386 display->insert_index = layout_byte_offset;
2388 if (layout->preedit_len > 0)
2390 text_allocated += layout->preedit_len;
2391 text = g_realloc (text, text_allocated);
2393 style = get_style (layout, tags);
2394 add_preedit_attrs (layout, style, attrs, layout_byte_offset, size_only);
2395 release_style (layout, style);
2397 memcpy (text + layout_byte_offset, layout->preedit_string, layout->preedit_len);
2398 layout_byte_offset += layout->preedit_len;
2399 /* DO NOT increment the buffer byte offset for preedit */
2401 cursor_offset = layout->preedit_cursor - layout->preedit_len;
2406 /* Display visible marks */
2408 if (seg->body.mark.visible)
2410 cursor_byte_offsets = g_slist_prepend (cursor_byte_offsets,
2411 GINT_TO_POINTER (layout_byte_offset + cursor_offset));
2412 cursor_segs = g_slist_prepend (cursor_segs, seg);
2417 g_error ("Unknown segment type: %s", seg->type->name);
2422 if (!para_values_set)
2424 style = get_style (layout, tags);
2425 set_para_values (layout, base_dir, style, display);
2426 release_style (layout, style);
2429 /* Pango doesn't want the trailing paragraph delimiters */
2432 /* Only one character has type G_UNICODE_PARAGRAPH_SEPARATOR in
2433 * Unicode 3.0; update this if that changes.
2435 #define PARAGRAPH_SEPARATOR 0x2029
2438 if (layout_byte_offset > 0)
2440 const char *prev = g_utf8_prev_char (text + layout_byte_offset);
2441 ch = g_utf8_get_char (prev);
2442 if (ch == PARAGRAPH_SEPARATOR || ch == '\r' || ch == '\n')
2443 layout_byte_offset = prev - text; /* chop off */
2445 if (ch == '\n' && layout_byte_offset > 0)
2447 /* Possibly chop a CR as well */
2448 prev = g_utf8_prev_char (text + layout_byte_offset);
2450 --layout_byte_offset;
2455 pango_layout_set_text (display->layout, text, layout_byte_offset);
2456 pango_layout_set_attributes (display->layout, attrs);
2458 tmp_list1 = cursor_byte_offsets;
2459 tmp_list2 = cursor_segs;
2462 add_cursor (layout, display, tmp_list2->data,
2463 GPOINTER_TO_INT (tmp_list1->data));
2464 tmp_list1 = tmp_list1->next;
2465 tmp_list2 = tmp_list2->next;
2467 g_slist_free (cursor_byte_offsets);
2468 g_slist_free (cursor_segs);
2470 pango_layout_get_extents (display->layout, NULL, &extents);
2472 display->width = PIXEL_BOUND (extents.width) + display->left_margin + display->right_margin;
2473 display->height += PANGO_PIXELS (extents.height);
2475 /* If we aren't wrapping, we need to do the alignment of each
2476 * paragraph ourselves.
2478 if (pango_layout_get_width (display->layout) < 0)
2480 gint excess = display->total_width - display->width;
2482 switch (pango_layout_get_alignment (display->layout))
2484 case PANGO_ALIGN_LEFT:
2486 case PANGO_ALIGN_CENTER:
2487 display->x_offset += excess / 2;
2489 case PANGO_ALIGN_RIGHT:
2490 display->x_offset += excess;
2495 /* Free this if we aren't in a loop */
2496 if (layout->wrap_loop_count == 0)
2497 invalidate_cached_style (layout);
2500 pango_attr_list_unref (attrs);
2502 g_ptr_array_free (tags, TRUE);
2504 layout->one_display_cache = display;
2507 allocate_child_widgets (layout, display);
2513 gtk_text_layout_free_line_display (GtkTextLayout *layout,
2514 GtkTextLineDisplay *display)
2516 if (display != layout->one_display_cache)
2518 if (display->layout)
2519 g_object_unref (display->layout);
2521 if (display->cursors)
2523 g_slist_foreach (display->cursors, (GFunc)g_free, NULL);
2524 g_slist_free (display->cursors);
2526 g_slist_free (display->shaped_objects);
2528 if (display->pg_bg_color)
2529 gdk_color_free (display->pg_bg_color);
2535 /* Functions to convert iter <=> index for the line of a GtkTextLineDisplay
2536 * taking into account the preedit string and invisible text if necessary.
2539 line_display_iter_to_index (GtkTextLayout *layout,
2540 GtkTextLineDisplay *display,
2541 const GtkTextIter *iter)
2545 g_return_val_if_fail (_gtk_text_iter_get_text_line (iter) == display->line, 0);
2547 index = gtk_text_iter_get_visible_line_index (iter);
2549 if (layout->preedit_len > 0 && display->insert_index >= 0)
2551 if (index >= display->insert_index)
2552 index += layout->preedit_len;
2559 line_display_index_to_iter (GtkTextLayout *layout,
2560 GtkTextLineDisplay *display,
2565 g_return_if_fail (!_gtk_text_line_is_last (display->line,
2566 _gtk_text_buffer_get_btree (layout->buffer)));
2568 if (layout->preedit_len > 0 && display->insert_index >= 0)
2570 if (index >= display->insert_index + layout->preedit_len)
2571 index -= layout->preedit_len;
2572 else if (index > display->insert_index)
2574 index = display->insert_index;
2579 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
2580 iter, display->line, 0);
2582 gtk_text_iter_set_visible_line_index (iter, index);
2584 if (_gtk_text_iter_get_text_line (iter) != display->line)
2586 /* Clamp to end of line - really this clamping should have been done
2587 * before here, maybe in Pango, this is a broken band-aid I think
2589 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
2590 iter, display->line, 0);
2592 if (!gtk_text_iter_ends_line (iter))
2593 gtk_text_iter_forward_to_line_end (iter);
2596 gtk_text_iter_forward_chars (iter, trailing);
2600 get_line_at_y (GtkTextLayout *layout,
2607 if (y > layout->height)
2610 *line = _gtk_text_btree_find_line_by_y (_gtk_text_buffer_get_btree (layout->buffer),
2611 layout, y, line_top);
2614 *line = _gtk_text_btree_get_end_iter_line (_gtk_text_buffer_get_btree (layout->buffer));
2618 _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
2624 * gtk_text_layout_get_line_at_y:
2625 * @layout: a #GtkLayout
2626 * @target_iter: the iterator in which the result is stored
2627 * @y: the y positition
2628 * @line_top: location to store the y coordinate of the
2629 * top of the line. (Can by %NULL)
2631 * Get the iter at the beginning of the line which is displayed
2635 gtk_text_layout_get_line_at_y (GtkTextLayout *layout,
2636 GtkTextIter *target_iter,
2642 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
2643 g_return_if_fail (target_iter != NULL);
2645 get_line_at_y (layout, y, &line, line_top);
2646 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
2647 target_iter, line, 0);
2651 gtk_text_layout_get_iter_at_pixel (GtkTextLayout *layout,
2652 GtkTextIter *target_iter,
2658 gtk_text_layout_get_iter_at_position (layout, target_iter, &trailing, x, y);
2660 gtk_text_iter_forward_chars (target_iter, trailing);
2663 void gtk_text_layout_get_iter_at_position (GtkTextLayout *layout,
2664 GtkTextIter *target_iter,
2672 GtkTextLineDisplay *display;
2674 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
2675 g_return_if_fail (target_iter != NULL);
2677 get_line_at_y (layout, y, &line, &line_top);
2679 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2681 x -= display->x_offset;
2682 y -= line_top + display->top_margin;
2684 /* If we are below the layout, position the cursor at the last character
2687 if (y > display->height - display->top_margin - display->bottom_margin)
2689 byte_index = _gtk_text_line_byte_count (line);
2695 /* Ignore the "outside" return value from pango. Pango is doing
2696 * the right thing even if we are outside the layout in the
2699 pango_layout_xy_to_index (display->layout, x * PANGO_SCALE, y * PANGO_SCALE,
2700 &byte_index, trailing);
2703 line_display_index_to_iter (layout, display, target_iter, byte_index, 0);
2705 gtk_text_layout_free_line_display (layout, display);
2710 * gtk_text_layout_get_cursor_locations:
2711 * @layout: a #GtkTextLayout
2712 * @iter: a #GtkTextIter
2713 * @strong_pos: (allow-none): location to store the strong cursor position (may be %NULL)
2714 * @weak_pos: (allow-none): location to store the weak cursor position (may be %NULL)
2716 * Given an iterator within a text layout, determine the positions of the
2717 * strong and weak cursors if the insertion point is at that
2718 * iterator. The position of each cursor is stored as a zero-width
2719 * rectangle. The strong cursor location is the location where
2720 * characters of the directionality equal to the base direction of the
2721 * paragraph are inserted. The weak cursor location is the location
2722 * where characters of the directionality opposite to the base
2723 * direction of the paragraph are inserted.
2726 gtk_text_layout_get_cursor_locations (GtkTextLayout *layout,
2728 GdkRectangle *strong_pos,
2729 GdkRectangle *weak_pos)
2732 GtkTextLineDisplay *display;
2735 GtkTextIter insert_iter;
2737 PangoRectangle pango_strong_pos;
2738 PangoRectangle pango_weak_pos;
2740 g_return_if_fail (layout != NULL);
2741 g_return_if_fail (iter != NULL);
2743 line = _gtk_text_iter_get_text_line (iter);
2744 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2745 index = line_display_iter_to_index (layout, display, iter);
2747 line_top = _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
2750 gtk_text_buffer_get_iter_at_mark (layout->buffer, &insert_iter,
2751 gtk_text_buffer_get_insert (layout->buffer));
2753 if (gtk_text_iter_equal (iter, &insert_iter))
2754 index += layout->preedit_cursor - layout->preedit_len;
2756 pango_layout_get_cursor_pos (display->layout, index,
2757 strong_pos ? &pango_strong_pos : NULL,
2758 weak_pos ? &pango_weak_pos : NULL);
2762 strong_pos->x = display->x_offset + pango_strong_pos.x / PANGO_SCALE;
2763 strong_pos->y = line_top + display->top_margin + pango_strong_pos.y / PANGO_SCALE;
2764 strong_pos->width = 0;
2765 strong_pos->height = pango_strong_pos.height / PANGO_SCALE;
2770 weak_pos->x = display->x_offset + pango_weak_pos.x / PANGO_SCALE;
2771 weak_pos->y = line_top + display->top_margin + pango_weak_pos.y / PANGO_SCALE;
2772 weak_pos->width = 0;
2773 weak_pos->height = pango_weak_pos.height / PANGO_SCALE;
2776 gtk_text_layout_free_line_display (layout, display);
2780 * _gtk_text_layout_get_block_cursor:
2781 * @layout: a #GtkTextLayout
2782 * @pos: a #GdkRectangle to store block cursor position
2784 * If layout is to display a block cursor, calculates its position
2785 * and returns %TRUE. Otherwise it returns %FALSE. In case when
2786 * cursor is visible, it simply returns the position stored in
2787 * the line display, otherwise it has to compute the position
2788 * (see get_block_cursor()).
2791 _gtk_text_layout_get_block_cursor (GtkTextLayout *layout,
2795 GtkTextLineDisplay *display;
2798 gboolean block = FALSE;
2800 g_return_val_if_fail (layout != NULL, FALSE);
2802 gtk_text_buffer_get_iter_at_mark (layout->buffer, &iter,
2803 gtk_text_buffer_get_insert (layout->buffer));
2804 line = _gtk_text_iter_get_text_line (&iter);
2805 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2807 if (display->has_block_cursor)
2810 rect = display->block_cursor;
2814 gint index = display->insert_index;
2817 index = gtk_text_iter_get_line_index (&iter);
2819 if (get_block_cursor (layout, display, &iter, index, &rect, NULL))
2827 line_top = _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
2831 pos->x += display->x_offset;
2832 pos->y += line_top + display->top_margin;
2835 gtk_text_layout_free_line_display (layout, display);
2840 * gtk_text_layout_get_line_yrange:
2841 * @layout: a #GtkTextLayout
2842 * @iter: a #GtkTextIter
2843 * @y: location to store the top of the paragraph in pixels,
2845 * @height location to store the height of the paragraph in pixels,
2848 * Find the range of y coordinates for the paragraph containing
2852 gtk_text_layout_get_line_yrange (GtkTextLayout *layout,
2853 const GtkTextIter *iter,
2859 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
2860 g_return_if_fail (_gtk_text_iter_get_btree (iter) == _gtk_text_buffer_get_btree (layout->buffer));
2862 line = _gtk_text_iter_get_text_line (iter);
2865 *y = _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
2869 GtkTextLineData *line_data = _gtk_text_line_get_data (line, layout);
2871 *height = line_data->height;
2878 * _gtk_text_layout_get_line_xrange:
2879 * @layout: a #GtkTextLayout
2880 * @iter: a #GtkTextIter
2881 * @x: location to store the top of the paragraph in pixels,
2883 * @width location to store the height of the paragraph in pixels,
2886 * Find the range of X coordinates for the paragraph containing
2887 * the given iter. Private for 2.0 due to API freeze, could
2888 * be made public for 2.2.
2891 _gtk_text_layout_get_line_xrange (GtkTextLayout *layout,
2892 const GtkTextIter *iter,
2898 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
2899 g_return_if_fail (_gtk_text_iter_get_btree (iter) == _gtk_text_buffer_get_btree (layout->buffer));
2901 line = _gtk_text_iter_get_text_line (iter);
2904 *x = 0; /* FIXME This is wrong; should represent the first available cursor position */
2908 GtkTextLineData *line_data = _gtk_text_line_get_data (line, layout);
2910 *width = line_data->width;
2917 gtk_text_layout_get_iter_location (GtkTextLayout *layout,
2918 const GtkTextIter *iter,
2921 PangoRectangle pango_rect;
2924 GtkTextLineDisplay *display;
2928 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
2929 g_return_if_fail (_gtk_text_iter_get_btree (iter) == _gtk_text_buffer_get_btree (layout->buffer));
2930 g_return_if_fail (rect != NULL);
2932 tree = _gtk_text_iter_get_btree (iter);
2933 line = _gtk_text_iter_get_text_line (iter);
2935 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2937 rect->y = _gtk_text_btree_find_line_top (tree, line, layout);
2939 x_offset = display->x_offset * PANGO_SCALE;
2941 byte_index = gtk_text_iter_get_line_index (iter);
2943 pango_layout_index_to_pos (display->layout, byte_index, &pango_rect);
2945 rect->x = PANGO_PIXELS (x_offset + pango_rect.x);
2946 rect->y += PANGO_PIXELS (pango_rect.y) + display->top_margin;
2947 rect->width = PANGO_PIXELS (pango_rect.width);
2948 rect->height = PANGO_PIXELS (pango_rect.height);
2950 gtk_text_layout_free_line_display (layout, display);
2955 /* Find the iter for the logical beginning of the first display line whose
2956 * top y is >= y. If none exists, move the iter to the logical beginning
2957 * of the last line in the buffer.
2960 find_display_line_below (GtkTextLayout *layout,
2964 GtkTextLine *line, *next;
2965 GtkTextLine *found_line = NULL;
2967 gint found_byte = 0;
2969 line = _gtk_text_btree_find_line_by_y (_gtk_text_buffer_get_btree (layout->buffer),
2970 layout, y, &line_top);
2974 _gtk_text_btree_get_end_iter_line (_gtk_text_buffer_get_btree (layout->buffer));
2977 _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
2981 while (line && !found_line)
2983 GtkTextLineDisplay *display = gtk_text_layout_get_line_display (layout, line, FALSE);
2984 PangoLayoutIter *layout_iter;
2986 layout_iter = pango_layout_get_iter (display->layout);
2988 line_top += display->top_margin;
2992 gint first_y, last_y;
2993 PangoLayoutLine *layout_line = pango_layout_iter_get_line_readonly (layout_iter);
2995 found_byte = layout_line->start_index;
3003 pango_layout_iter_get_line_yrange (layout_iter, &first_y, &last_y);
3004 line_top += (last_y - first_y) / PANGO_SCALE;
3006 while (pango_layout_iter_next_line (layout_iter));
3008 pango_layout_iter_free (layout_iter);
3010 line_top += display->bottom_margin;
3011 gtk_text_layout_free_line_display (layout, display);
3013 next = _gtk_text_line_next_excluding_last (line);
3020 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
3021 iter, found_line, found_byte);
3024 /* Find the iter for the logical beginning of the last display line whose
3025 * top y is >= y. If none exists, move the iter to the logical beginning
3026 * of the first line in the buffer.
3029 find_display_line_above (GtkTextLayout *layout,
3034 GtkTextLine *found_line = NULL;
3036 gint found_byte = 0;
3038 line = _gtk_text_btree_find_line_by_y (_gtk_text_buffer_get_btree (layout->buffer), layout, y, &line_top);
3041 line = _gtk_text_btree_get_end_iter_line (_gtk_text_buffer_get_btree (layout->buffer));
3043 line_top = _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer), line, layout);
3046 while (line && !found_line)
3048 GtkTextLineDisplay *display = gtk_text_layout_get_line_display (layout, line, FALSE);
3049 PangoRectangle logical_rect;
3050 PangoLayoutIter *layout_iter;
3053 layout_iter = pango_layout_get_iter (display->layout);
3055 line_top -= display->top_margin + display->bottom_margin;
3056 pango_layout_iter_get_layout_extents (layout_iter, NULL, &logical_rect);
3057 line_top -= logical_rect.height / PANGO_SCALE;
3059 tmp_top = line_top + display->top_margin;
3063 gint first_y, last_y;
3064 PangoLayoutLine *layout_line = pango_layout_iter_get_line_readonly (layout_iter);
3066 found_byte = layout_line->start_index;
3068 pango_layout_iter_get_line_yrange (layout_iter, &first_y, &last_y);
3070 tmp_top -= (last_y - first_y) / PANGO_SCALE;
3075 pango_layout_iter_free (layout_iter);
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);
3085 line = _gtk_text_line_previous (line);
3091 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
3092 iter, found_line, found_byte);
3094 gtk_text_buffer_get_iter_at_offset (layout->buffer, iter, 0);
3098 * gtk_text_layout_clamp_iter_to_vrange:
3099 * @layout: a #GtkTextLayout
3100 * @iter: a #GtkTextIter
3101 * @top: the top of the range
3102 * @bottom: the bottom the range
3104 * If the iterator is not fully in the range @top <= y < @bottom,
3105 * then, if possible, move it the minimum distance so that the
3106 * iterator in this range.
3108 * Returns: %TRUE if the iterator was moved, otherwise %FALSE.
3111 gtk_text_layout_clamp_iter_to_vrange (GtkTextLayout *layout,
3116 GdkRectangle iter_rect;
3118 gtk_text_layout_get_iter_location (layout, iter, &iter_rect);
3120 /* If the iter is at least partially above the range, put the iter
3121 * at the first fully visible line after the range.
3123 if (iter_rect.y < top)
3125 find_display_line_below (layout, iter, top);
3129 /* Otherwise, if the iter is at least partially below the screen, put the
3130 * iter on the last logical position of the last completely visible
3133 else if (iter_rect.y + iter_rect.height > bottom)
3135 find_display_line_above (layout, iter, bottom);
3144 * gtk_text_layout_move_iter_to_previous_line:
3145 * @layout: a #GtkLayout
3146 * @iter: a #GtkTextIter
3148 * Move the iterator to the beginning of the previous line. The lines
3149 * of a wrapped paragraph are treated as distinct for this operation.
3152 gtk_text_layout_move_iter_to_previous_line (GtkTextLayout *layout,
3156 GtkTextLineDisplay *display;
3159 PangoLayoutLine *layout_line;
3161 gboolean update_byte = FALSE;
3163 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), FALSE);
3164 g_return_val_if_fail (iter != NULL, FALSE);
3169 line = _gtk_text_iter_get_text_line (iter);
3170 display = gtk_text_layout_get_line_display (layout, line, FALSE);
3171 line_byte = line_display_iter_to_index (layout, display, iter);
3173 /* If display->height == 0 then the line is invisible, so don't
3176 while (display->height == 0)
3178 GtkTextLine *prev_line;
3180 prev_line = _gtk_text_line_previous (line);
3182 if (prev_line == NULL)
3184 line_display_index_to_iter (layout, display, iter, 0, 0);
3188 gtk_text_layout_free_line_display (layout, display);
3191 display = gtk_text_layout_get_line_display (layout, prev_line, FALSE);
3195 tmp_list = pango_layout_get_lines_readonly (display->layout);
3196 layout_line = tmp_list->data;
3200 line_byte = layout_line->start_index + layout_line->length;
3203 if (line_byte < layout_line->length || !tmp_list->next) /* first line of paragraph */
3205 GtkTextLine *prev_line;
3207 prev_line = _gtk_text_line_previous (line);
3209 /* first line of the whole buffer, do not move the iter and return FALSE */
3210 if (prev_line == NULL)
3215 gtk_text_layout_free_line_display (layout, display);
3217 display = gtk_text_layout_get_line_display (layout, prev_line, FALSE);
3219 if (display->height > 0)
3221 tmp_list = g_slist_last (pango_layout_get_lines_readonly (display->layout));
3222 layout_line = tmp_list->data;
3224 line_display_index_to_iter (layout, display, iter,
3225 layout_line->start_index + layout_line->length, 0);
3229 prev_line = _gtk_text_line_previous (prev_line);
3234 gint prev_offset = layout_line->start_index;
3236 tmp_list = tmp_list->next;
3239 layout_line = tmp_list->data;
3241 if (line_byte < layout_line->start_index + layout_line->length ||
3244 line_display_index_to_iter (layout, display, iter, prev_offset, 0);
3248 prev_offset = layout_line->start_index;
3249 tmp_list = tmp_list->next;
3255 gtk_text_layout_free_line_display (layout, display);
3258 !gtk_text_iter_equal (iter, &orig) &&
3259 !gtk_text_iter_is_end (iter);
3263 * gtk_text_layout_move_iter_to_next_line:
3264 * @layout: a #GtkLayout
3265 * @iter: a #GtkTextIter
3267 * Move the iterator to the beginning of the next line. The
3268 * lines of a wrapped paragraph are treated as distinct for
3272 gtk_text_layout_move_iter_to_next_line (GtkTextLayout *layout,
3276 GtkTextLineDisplay *display;
3279 gboolean found = FALSE;
3280 gboolean found_after = FALSE;
3281 gboolean first = TRUE;
3283 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), FALSE);
3284 g_return_val_if_fail (iter != NULL, FALSE);
3288 line = _gtk_text_iter_get_text_line (iter);
3290 while (line && !found_after)
3294 display = gtk_text_layout_get_line_display (layout, line, FALSE);
3296 if (display->height == 0)
3301 line_byte = line_display_iter_to_index (layout, display, iter);
3307 tmp_list = pango_layout_get_lines_readonly (display->layout);
3308 while (tmp_list && !found_after)
3310 PangoLayoutLine *layout_line = tmp_list->data;
3314 line_display_index_to_iter (layout, display, iter,
3315 layout_line->start_index, 0);
3318 else if (line_byte < layout_line->start_index + layout_line->length || !tmp_list->next)
3321 tmp_list = tmp_list->next;
3326 gtk_text_layout_free_line_display (layout, display);
3328 line = _gtk_text_line_next_excluding_last (line);
3332 gtk_text_buffer_get_end_iter (layout->buffer, iter);
3335 !gtk_text_iter_equal (iter, &orig) &&
3336 !gtk_text_iter_is_end (iter);
3340 * gtk_text_layout_move_iter_to_line_end:
3341 * @layout: a #GtkTextLayout
3342 * @direction: if negative, move to beginning of line, otherwise
3343 move to end of line.
3345 * Move to the beginning or end of a display line.
3348 gtk_text_layout_move_iter_to_line_end (GtkTextLayout *layout,
3353 GtkTextLineDisplay *display;
3358 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), FALSE);
3359 g_return_val_if_fail (iter != NULL, FALSE);
3363 line = _gtk_text_iter_get_text_line (iter);
3364 display = gtk_text_layout_get_line_display (layout, line, FALSE);
3365 line_byte = line_display_iter_to_index (layout, display, iter);
3367 tmp_list = pango_layout_get_lines_readonly (display->layout);
3370 PangoLayoutLine *layout_line = tmp_list->data;
3372 if (line_byte < layout_line->start_index + layout_line->length || !tmp_list->next)
3374 line_display_index_to_iter (layout, display, iter,
3375 direction < 0 ? layout_line->start_index : layout_line->start_index + layout_line->length,
3378 /* FIXME: As a bad hack, we move back one position when we
3379 * are inside a paragraph to avoid going to next line on a
3380 * forced break not at whitespace. Real fix is to keep track
3381 * of whether marks are at leading or trailing edge? */
3382 if (direction > 0 && layout_line->length > 0 &&
3383 !gtk_text_iter_ends_line (iter) &&
3384 !_gtk_text_btree_char_is_invisible (iter))
3385 gtk_text_iter_backward_char (iter);
3389 tmp_list = tmp_list->next;
3392 gtk_text_layout_free_line_display (layout, display);
3395 !gtk_text_iter_equal (iter, &orig) &&
3396 !gtk_text_iter_is_end (iter);
3401 * gtk_text_layout_iter_starts_line:
3402 * @layout: a #GtkTextLayout
3403 * @iter: iterator to test
3405 * Tests whether an iterator is at the start of a display line.
3408 gtk_text_layout_iter_starts_line (GtkTextLayout *layout,
3409 const GtkTextIter *iter)
3412 GtkTextLineDisplay *display;
3416 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), FALSE);
3417 g_return_val_if_fail (iter != NULL, FALSE);
3419 line = _gtk_text_iter_get_text_line (iter);
3420 display = gtk_text_layout_get_line_display (layout, line, FALSE);
3421 line_byte = line_display_iter_to_index (layout, display, iter);
3423 tmp_list = pango_layout_get_lines_readonly (display->layout);
3426 PangoLayoutLine *layout_line = tmp_list->data;
3428 if (line_byte < layout_line->start_index + layout_line->length ||
3431 /* We're located on this line or the para delimiters before
3434 gtk_text_layout_free_line_display (layout, display);
3436 if (line_byte == layout_line->start_index)
3442 tmp_list = tmp_list->next;
3445 g_assert_not_reached ();
3450 gtk_text_layout_get_iter_at_line (GtkTextLayout *layout,
3455 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
3456 iter, line, byte_offset);
3461 * gtk_text_layout_move_iter_to_x:
3462 * @layout: a #GtkTextLayout
3463 * @iter: a #GtkTextIter
3466 * Keeping the iterator on the same line of the layout, move it to the
3467 * specified X coordinate. The lines of a wrapped paragraph are
3468 * treated as distinct for this operation.
3471 gtk_text_layout_move_iter_to_x (GtkTextLayout *layout,
3476 GtkTextLineDisplay *display;
3478 PangoLayoutIter *layout_iter;
3480 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
3481 g_return_if_fail (iter != NULL);
3483 line = _gtk_text_iter_get_text_line (iter);
3485 display = gtk_text_layout_get_line_display (layout, line, FALSE);
3486 line_byte = line_display_iter_to_index (layout, display, iter);
3488 layout_iter = pango_layout_get_iter (display->layout);
3492 PangoLayoutLine *layout_line = pango_layout_iter_get_line_readonly (layout_iter);
3494 if (line_byte < layout_line->start_index + layout_line->length ||
3495 pango_layout_iter_at_last_line (layout_iter))
3497 PangoRectangle logical_rect;
3498 gint byte_index, trailing;
3499 gint x_offset = display->x_offset * PANGO_SCALE;
3501 pango_layout_iter_get_line_extents (layout_iter, NULL, &logical_rect);
3503 pango_layout_line_x_to_index (layout_line,
3504 x * PANGO_SCALE - x_offset - logical_rect.x,
3505 &byte_index, &trailing);
3507 line_display_index_to_iter (layout, display, iter, byte_index, trailing);
3512 while (pango_layout_iter_next_line (layout_iter));
3514 pango_layout_iter_free (layout_iter);
3516 gtk_text_layout_free_line_display (layout, display);
3520 * gtk_text_layout_move_iter_visually:
3521 * @layout: a #GtkTextLayout
3522 * @iter: a #GtkTextIter
3523 * @count: number of characters to move (negative moves left, positive moves right)
3525 * Move the iterator a given number of characters visually, treating
3526 * it as the strong cursor position. If @count is positive, then the
3527 * new strong cursor position will be @count positions to the right of
3528 * the old cursor position. If @count is negative then the new strong
3529 * cursor position will be @count positions to the left of the old
3532 * In the presence of bidirection text, the correspondence
3533 * between logical and visual order will depend on the direction
3534 * of the current run, and there may be jumps when the cursor
3535 * is moved off of the end of a run.
3539 gtk_text_layout_move_iter_visually (GtkTextLayout *layout,
3543 GtkTextLineDisplay *display = NULL;
3545 GtkTextIter lineiter;
3547 g_return_val_if_fail (layout != NULL, FALSE);
3548 g_return_val_if_fail (iter != NULL, FALSE);
3554 GtkTextLine *line = _gtk_text_iter_get_text_line (iter);
3556 gint extra_back = 0;
3559 int byte_count = _gtk_text_line_byte_count (line);
3565 display = gtk_text_layout_get_line_display (layout, line, FALSE);
3567 if (layout->cursor_direction == GTK_TEXT_DIR_NONE)
3570 strong = display->direction == layout->cursor_direction;
3572 line_byte = line_display_iter_to_index (layout, display, iter);
3576 pango_layout_move_cursor_visually (display->layout, strong, line_byte, 0, 1, &new_index, &new_trailing);
3581 pango_layout_move_cursor_visually (display->layout, strong, line_byte, 0, -1, &new_index, &new_trailing);
3585 /* We need to handle the preedit string specially. Well, we don't really need to
3586 * handle it specially, since hopefully calling gtk_im_context_reset() will
3587 * remove the preedit string; but if we start off in front of the preedit
3588 * string (logically) and end up in or on the back edge of the preedit string,
3589 * we should move the iter one place farther.
3591 if (layout->preedit_len > 0 && display->insert_index >= 0)
3593 if (line_byte == display->insert_index + layout->preedit_len &&
3594 new_index < display->insert_index + layout->preedit_len)
3596 line_byte = display->insert_index;
3601 if (new_index < 0 || (new_index == 0 && extra_back))
3605 line = _gtk_text_line_previous (line);
3610 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
3611 &lineiter, line, 0);
3613 while (totally_invisible_line (layout, line, &lineiter));
3615 gtk_text_layout_free_line_display (layout, display);
3616 display = gtk_text_layout_get_line_display (layout, line, FALSE);
3617 gtk_text_iter_forward_to_line_end (&lineiter);
3618 new_index = gtk_text_iter_get_visible_line_index (&lineiter);
3620 else if (new_index > byte_count)
3624 line = _gtk_text_line_next_excluding_last (line);
3628 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
3629 &lineiter, line, 0);
3631 while (totally_invisible_line (layout, line, &lineiter));
3633 gtk_text_layout_free_line_display (layout, display);
3634 display = gtk_text_layout_get_line_display (layout, line, FALSE);
3638 line_display_index_to_iter (layout, display, iter, new_index, new_trailing);
3640 gtk_text_iter_backward_char (iter);
3643 gtk_text_layout_free_line_display (layout, display);
3648 !gtk_text_iter_equal (iter, &orig) &&
3649 !gtk_text_iter_is_end (iter);
3653 gtk_text_layout_spew (GtkTextLayout *layout)
3656 GtkTextDisplayLine *iter;
3658 guint paragraphs = 0;
3659 GtkTextLine *last_line = NULL;
3661 iter = layout->line_list;
3662 while (iter != NULL)
3664 if (iter->line != last_line)
3666 printf ("%5u paragraph (%p)\n", paragraphs, iter->line);
3668 last_line = iter->line;
3671 printf (" %5u y: %d len: %d start: %d bytes: %d\n",
3672 wrapped, iter->y, iter->length, iter->byte_offset,
3679 printf ("Layout %s recompute\n",
3680 layout->need_recompute ? "needs" : "doesn't need");
3682 printf ("Layout pars: %u lines: %u size: %d x %d Screen width: %d\n",
3683 paragraphs, wrapped, layout->width,
3684 layout->height, layout->screen_width);
3688 /* Catch all situations that move the insertion point.
3691 gtk_text_layout_mark_set_handler (GtkTextBuffer *buffer,
3692 const GtkTextIter *location,
3696 GtkTextLayout *layout = GTK_TEXT_LAYOUT (data);
3698 if (mark == gtk_text_buffer_get_insert (buffer))
3699 gtk_text_layout_update_cursor_line (layout);
3703 gtk_text_layout_buffer_insert_text (GtkTextBuffer *textbuffer,
3709 GtkTextLayout *layout = GTK_TEXT_LAYOUT (data);
3711 gtk_text_layout_update_cursor_line (layout);
3715 gtk_text_layout_buffer_delete_range (GtkTextBuffer *textbuffer,
3720 GtkTextLayout *layout = GTK_TEXT_LAYOUT (data);
3722 gtk_text_layout_update_cursor_line (layout);
3725 #define __GTK_TEXT_LAYOUT_C__
3726 #include "gtkaliasdef.c"