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"
91 #define GTK_TEXT_LAYOUT_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GTK_TYPE_TEXT_LAYOUT, GtkTextLayoutPrivate))
93 typedef struct _GtkTextLayoutPrivate GtkTextLayoutPrivate;
95 struct _GtkTextLayoutPrivate
97 /* Cache the line that the cursor is positioned on, as the keyboard
98 direction only influences the direction of the cursor line.
100 GtkTextLine *cursor_line;
103 static GtkTextLineData *gtk_text_layout_real_wrap (GtkTextLayout *layout,
106 GtkTextLineData *line_data);
108 static void gtk_text_layout_invalidated (GtkTextLayout *layout);
110 static void gtk_text_layout_real_invalidate (GtkTextLayout *layout,
111 const GtkTextIter *start,
112 const GtkTextIter *end);
113 static void gtk_text_layout_invalidate_cache (GtkTextLayout *layout,
115 static void gtk_text_layout_invalidate_cursor_line (GtkTextLayout *layout);
116 static void gtk_text_layout_real_free_line_data (GtkTextLayout *layout,
118 GtkTextLineData *line_data);
119 static void gtk_text_layout_emit_changed (GtkTextLayout *layout,
124 static void gtk_text_layout_invalidate_all (GtkTextLayout *layout);
126 static PangoAttribute *gtk_text_attr_appearance_new (const GtkTextAppearance *appearance);
128 static void gtk_text_layout_mark_set_handler (GtkTextBuffer *buffer,
129 const GtkTextIter *location,
132 static void gtk_text_layout_buffer_insert_text (GtkTextBuffer *textbuffer,
137 static void gtk_text_layout_buffer_delete_range (GtkTextBuffer *textbuffer,
142 static void gtk_text_layout_update_cursor_line (GtkTextLayout *layout);
156 #define PIXEL_BOUND(d) (((d) + PANGO_SCALE - 1) / PANGO_SCALE)
158 static void gtk_text_layout_finalize (GObject *object);
160 static guint signals[LAST_SIGNAL] = { 0 };
162 PangoAttrType gtk_text_attr_appearance_type = 0;
164 G_DEFINE_TYPE (GtkTextLayout, gtk_text_layout, G_TYPE_OBJECT)
167 gtk_text_layout_class_init (GtkTextLayoutClass *klass)
169 GObjectClass *object_class = G_OBJECT_CLASS (klass);
171 object_class->finalize = gtk_text_layout_finalize;
173 klass->wrap = gtk_text_layout_real_wrap;
174 klass->invalidate = gtk_text_layout_real_invalidate;
175 klass->free_line_data = gtk_text_layout_real_free_line_data;
177 signals[INVALIDATED] =
178 g_signal_new (I_("invalidated"),
179 G_OBJECT_CLASS_TYPE (object_class),
181 G_STRUCT_OFFSET (GtkTextLayoutClass, invalidated),
183 _gtk_marshal_VOID__VOID,
188 g_signal_new (I_("changed"),
189 G_OBJECT_CLASS_TYPE (object_class),
191 G_STRUCT_OFFSET (GtkTextLayoutClass, changed),
193 _gtk_marshal_VOID__INT_INT_INT,
200 signals[ALLOCATE_CHILD] =
201 g_signal_new (I_("allocate_child"),
202 G_OBJECT_CLASS_TYPE (object_class),
204 G_STRUCT_OFFSET (GtkTextLayoutClass, allocate_child),
206 _gtk_marshal_VOID__OBJECT_INT_INT,
213 g_type_class_add_private (object_class, sizeof (GtkTextLayoutPrivate));
217 gtk_text_layout_init (GtkTextLayout *text_layout)
219 text_layout->cursor_visible = TRUE;
223 gtk_text_layout_new (void)
225 return g_object_new (GTK_TYPE_TEXT_LAYOUT, NULL);
229 free_style_cache (GtkTextLayout *text_layout)
231 if (text_layout->one_style_cache)
233 gtk_text_attributes_unref (text_layout->one_style_cache);
234 text_layout->one_style_cache = NULL;
239 gtk_text_layout_finalize (GObject *object)
241 GtkTextLayout *layout;
243 layout = GTK_TEXT_LAYOUT (object);
245 gtk_text_layout_set_buffer (layout, NULL);
247 if (layout->default_style)
248 gtk_text_attributes_unref (layout->default_style);
249 layout->default_style = NULL;
251 if (layout->ltr_context)
253 g_object_unref (layout->ltr_context);
254 layout->ltr_context = NULL;
256 if (layout->rtl_context)
258 g_object_unref (layout->rtl_context);
259 layout->rtl_context = NULL;
262 if (layout->one_display_cache)
264 GtkTextLineDisplay *tmp_display = layout->one_display_cache;
265 layout->one_display_cache = NULL;
266 gtk_text_layout_free_line_display (layout, tmp_display);
269 if (layout->preedit_string)
271 g_free (layout->preedit_string);
272 layout->preedit_string = NULL;
275 if (layout->preedit_attrs)
277 pango_attr_list_unref (layout->preedit_attrs);
278 layout->preedit_attrs = NULL;
282 (* G_OBJECT_CLASS (gtk_text_layout_parent_class)->finalize) (object);
286 gtk_text_layout_set_buffer (GtkTextLayout *layout,
287 GtkTextBuffer *buffer)
289 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
290 g_return_if_fail (buffer == NULL || GTK_IS_TEXT_BUFFER (buffer));
292 if (layout->buffer == buffer)
295 free_style_cache (layout);
299 _gtk_text_btree_remove_view (_gtk_text_buffer_get_btree (layout->buffer),
302 g_signal_handlers_disconnect_by_func (layout->buffer,
303 G_CALLBACK (gtk_text_layout_mark_set_handler),
305 g_signal_handlers_disconnect_by_func (layout->buffer,
306 G_CALLBACK (gtk_text_layout_buffer_insert_text),
308 g_signal_handlers_disconnect_by_func (layout->buffer,
309 G_CALLBACK (gtk_text_layout_buffer_delete_range),
312 g_object_unref (layout->buffer);
313 layout->buffer = NULL;
318 layout->buffer = buffer;
320 g_object_ref (buffer);
322 _gtk_text_btree_add_view (_gtk_text_buffer_get_btree (buffer), layout);
324 /* Bind to all signals that move the insert mark. */
325 g_signal_connect_after (layout->buffer, "mark_set",
326 G_CALLBACK (gtk_text_layout_mark_set_handler), layout);
327 g_signal_connect_after (layout->buffer, "insert_text",
328 G_CALLBACK (gtk_text_layout_buffer_insert_text), layout);
329 g_signal_connect_after (layout->buffer, "delete_range",
330 G_CALLBACK (gtk_text_layout_buffer_delete_range), layout);
332 gtk_text_layout_update_cursor_line (layout);
337 gtk_text_layout_default_style_changed (GtkTextLayout *layout)
339 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
341 DV (g_print ("invalidating all due to default style change (%s)\n", G_STRLOC));
342 gtk_text_layout_invalidate_all (layout);
346 gtk_text_layout_set_default_style (GtkTextLayout *layout,
347 GtkTextAttributes *values)
349 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
350 g_return_if_fail (values != NULL);
352 if (values == layout->default_style)
355 gtk_text_attributes_ref (values);
357 if (layout->default_style)
358 gtk_text_attributes_unref (layout->default_style);
360 layout->default_style = values;
362 gtk_text_layout_default_style_changed (layout);
366 gtk_text_layout_set_contexts (GtkTextLayout *layout,
367 PangoContext *ltr_context,
368 PangoContext *rtl_context)
370 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
372 if (layout->ltr_context != ltr_context)
374 if (layout->ltr_context)
375 g_object_unref (layout->ltr_context);
377 layout->ltr_context = ltr_context;
378 g_object_ref (layout->ltr_context);
381 if (layout->rtl_context != rtl_context)
383 if (layout->rtl_context)
384 g_object_unref (layout->rtl_context);
386 layout->rtl_context = rtl_context;
387 g_object_ref (layout->rtl_context);
390 DV (g_print ("invalidating all due to new pango contexts (%s)\n", G_STRLOC));
391 gtk_text_layout_invalidate_all (layout);
395 * gtk_text_layout_set_cursor_direction:
396 * @direction: the new direction(s) for which to draw cursors.
397 * %GTK_TEXT_DIR_NONE means draw cursors for both
398 * left-to-right insertion and right-to-left insertion.
399 * (The two cursors will be visually distinguished.)
401 * Sets which text directions (left-to-right and/or right-to-left) for
402 * which cursors will be drawn for the insertion point. The visual
403 * point at which new text is inserted depends on whether the new
404 * text is right-to-left or left-to-right, so it may be desired to
405 * make the drawn position of the cursor depend on the keyboard state.
408 gtk_text_layout_set_cursor_direction (GtkTextLayout *layout,
409 GtkTextDirection direction)
411 if (direction != layout->cursor_direction)
413 layout->cursor_direction = direction;
414 gtk_text_layout_invalidate_cursor_line (layout);
419 * gtk_text_layout_set_keyboard_direction:
420 * @keyboard_dir: the current direction of the keyboard.
422 * Sets the keyboard direction; this is used as for the bidirectional
423 * base direction for the line with the cursor if the line contains
424 * only neutral characters.
427 gtk_text_layout_set_keyboard_direction (GtkTextLayout *layout,
428 GtkTextDirection keyboard_dir)
430 if (keyboard_dir != layout->keyboard_direction)
432 layout->keyboard_direction = keyboard_dir;
433 gtk_text_layout_invalidate_cursor_line (layout);
438 * gtk_text_layout_get_buffer:
439 * @layout: a #GtkTextLayout
441 * Gets the text buffer used by the layout. See
442 * gtk_text_layout_set_buffer().
444 * Return value: the text buffer used by the layout.
447 gtk_text_layout_get_buffer (GtkTextLayout *layout)
449 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), NULL);
451 return layout->buffer;
455 gtk_text_layout_set_screen_width (GtkTextLayout *layout, gint width)
457 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
458 g_return_if_fail (width >= 0);
459 g_return_if_fail (layout->wrap_loop_count == 0);
461 if (layout->screen_width == width)
464 layout->screen_width = width;
466 DV (g_print ("invalidating all due to new screen width (%s)\n", G_STRLOC));
467 gtk_text_layout_invalidate_all (layout);
471 * gtk_text_layout_set_cursor_visible:
472 * @layout: a #GtkTextLayout
473 * @cursor_visible: If %FALSE, then the insertion cursor will not
474 * be shown, even if the text is editable.
476 * Sets whether the insertion cursor should be shown. Generally,
477 * widgets using #GtkTextLayout will hide the cursor when the
478 * widget does not have the input focus.
481 gtk_text_layout_set_cursor_visible (GtkTextLayout *layout,
482 gboolean cursor_visible)
484 cursor_visible = (cursor_visible != FALSE);
486 if (layout->cursor_visible != cursor_visible)
491 layout->cursor_visible = cursor_visible;
493 /* Now queue a redraw on the paragraph containing the cursor
495 gtk_text_buffer_get_iter_at_mark (layout->buffer, &iter,
496 gtk_text_buffer_get_mark (layout->buffer, "insert"));
498 gtk_text_layout_get_line_yrange (layout, &iter, &y, &height);
499 gtk_text_layout_emit_changed (layout, y, height, height);
501 gtk_text_layout_invalidate_cache (layout, _gtk_text_iter_get_text_line (&iter));
506 * gtk_text_layout_get_cursor_visible:
507 * @layout: a #GtkTextLayout
509 * Returns whether the insertion cursor will be shown.
511 * Return value: if %FALSE, the insertion cursor will not be
512 shown, even if the text is editable.
515 gtk_text_layout_get_cursor_visible (GtkTextLayout *layout)
517 return layout->cursor_visible;
521 * gtk_text_layout_set_preedit_string:
522 * @layout: a #PangoLayout
523 * @preedit_string: a string to display at the insertion point
524 * @preedit_attrs: a #PangoAttrList of attributes that apply to @preedit_string
525 * @cursor_pos: position of cursor within preedit string in chars
527 * Set the preedit string and attributes. The preedit string is a
528 * string showing text that is currently being edited and not
529 * yet committed into the buffer.
532 gtk_text_layout_set_preedit_string (GtkTextLayout *layout,
533 const gchar *preedit_string,
534 PangoAttrList *preedit_attrs,
537 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
538 g_return_if_fail (preedit_attrs != NULL || preedit_string == NULL);
540 if (layout->preedit_string)
541 g_free (layout->preedit_string);
543 if (layout->preedit_attrs)
544 pango_attr_list_unref (layout->preedit_attrs);
548 layout->preedit_string = g_strdup (preedit_string);
549 layout->preedit_len = strlen (layout->preedit_string);
550 pango_attr_list_ref (preedit_attrs);
551 layout->preedit_attrs = preedit_attrs;
553 cursor_pos = CLAMP (cursor_pos, 0, g_utf8_strlen (layout->preedit_string, -1));
554 layout->preedit_cursor = g_utf8_offset_to_pointer (layout->preedit_string, cursor_pos) - layout->preedit_string;
558 layout->preedit_string = NULL;
559 layout->preedit_len = 0;
560 layout->preedit_attrs = NULL;
561 layout->preedit_cursor = 0;
564 gtk_text_layout_invalidate_cursor_line (layout);
568 gtk_text_layout_get_size (GtkTextLayout *layout,
572 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
575 *width = layout->width;
578 *height = layout->height;
582 gtk_text_layout_invalidated (GtkTextLayout *layout)
584 g_signal_emit (layout, signals[INVALIDATED], 0);
588 gtk_text_layout_emit_changed (GtkTextLayout *layout,
593 g_signal_emit (layout, signals[CHANGED], 0, y, old_height, new_height);
597 gtk_text_layout_changed (GtkTextLayout *layout,
602 /* Check if the range intersects our cached line display,
603 * and invalidate the cached line if so.
605 if (layout->one_display_cache)
607 GtkTextLine *line = layout->one_display_cache->line;
608 gint cache_y = _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
610 gint cache_height = layout->one_display_cache->height;
612 if (cache_y + cache_height > y && cache_y < y + old_height)
613 gtk_text_layout_invalidate_cache (layout, line);
616 gtk_text_layout_emit_changed (layout, y, old_height, new_height);
620 gtk_text_layout_free_line_data (GtkTextLayout *layout,
622 GtkTextLineData *line_data)
624 (* GTK_TEXT_LAYOUT_GET_CLASS (layout)->free_line_data)
625 (layout, line, line_data);
629 gtk_text_layout_invalidate (GtkTextLayout *layout,
630 const GtkTextIter *start_index,
631 const GtkTextIter *end_index)
633 (* GTK_TEXT_LAYOUT_GET_CLASS (layout)->invalidate)
634 (layout, start_index, end_index);
638 gtk_text_layout_wrap (GtkTextLayout *layout,
641 GtkTextLineData *line_data)
643 return (* GTK_TEXT_LAYOUT_GET_CLASS (layout)->wrap) (layout, line, line_data);
647 gtk_text_layout_get_lines (GtkTextLayout *layout,
648 /* [top_y, bottom_y) */
653 GtkTextLine *first_btree_line;
654 GtkTextLine *last_btree_line;
658 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), NULL);
659 g_return_val_if_fail (bottom_y > top_y, NULL);
664 _gtk_text_btree_find_line_by_y (_gtk_text_buffer_get_btree (layout->buffer),
665 layout, top_y, first_line_y);
666 if (first_btree_line == NULL)
672 /* -1 since bottom_y is one past */
674 _gtk_text_btree_find_line_by_y (_gtk_text_buffer_get_btree (layout->buffer),
675 layout, bottom_y - 1, NULL);
677 if (!last_btree_line)
679 _gtk_text_btree_get_end_iter_line (_gtk_text_buffer_get_btree (layout->buffer));
681 g_assert (last_btree_line != NULL);
683 line = first_btree_line;
686 retval = g_slist_prepend (retval, line);
688 if (line == last_btree_line)
691 line = _gtk_text_line_next_excluding_last (line);
694 retval = g_slist_reverse (retval);
700 invalidate_cached_style (GtkTextLayout *layout)
702 free_style_cache (layout);
705 /* These should be called around a loop which wraps a CONTIGUOUS bunch
706 * of display lines. If the lines aren't contiguous you can't call
710 gtk_text_layout_wrap_loop_start (GtkTextLayout *layout)
712 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
713 g_return_if_fail (layout->one_style_cache == NULL);
715 layout->wrap_loop_count += 1;
719 gtk_text_layout_wrap_loop_end (GtkTextLayout *layout)
721 g_return_if_fail (layout->wrap_loop_count > 0);
723 layout->wrap_loop_count -= 1;
725 if (layout->wrap_loop_count == 0)
727 /* We cache a some stuff if we're iterating over some lines wrapping
728 * them. This cleans it up.
730 /* Nuke our cached style */
731 invalidate_cached_style (layout);
732 g_assert (layout->one_style_cache == NULL);
737 gtk_text_layout_invalidate_all (GtkTextLayout *layout)
742 if (layout->buffer == NULL)
745 gtk_text_buffer_get_bounds (layout->buffer, &start, &end);
747 gtk_text_layout_invalidate (layout, &start, &end);
751 gtk_text_layout_invalidate_cache (GtkTextLayout *layout,
754 if (layout->one_display_cache && line == layout->one_display_cache->line)
756 GtkTextLineDisplay *tmp_display = layout->one_display_cache;
757 layout->one_display_cache = NULL;
758 gtk_text_layout_free_line_display (layout, tmp_display);
762 /* Now invalidate the paragraph containing the cursor
765 gtk_text_layout_invalidate_cursor_line (GtkTextLayout *layout)
767 GtkTextLayoutPrivate *priv = GTK_TEXT_LAYOUT_GET_PRIVATE (layout);
768 GtkTextLineData *line_data;
770 if (priv->cursor_line == NULL)
773 line_data = _gtk_text_line_get_data (priv->cursor_line, layout);
776 gtk_text_layout_invalidate_cache (layout, priv->cursor_line);
777 _gtk_text_line_invalidate_wrap (priv->cursor_line, line_data);
778 gtk_text_layout_invalidated (layout);
783 gtk_text_layout_update_cursor_line(GtkTextLayout *layout)
785 GtkTextLayoutPrivate *priv = GTK_TEXT_LAYOUT_GET_PRIVATE (layout);
788 gtk_text_buffer_get_iter_at_mark (layout->buffer, &iter,
789 gtk_text_buffer_get_mark (layout->buffer, "insert"));
791 priv->cursor_line = _gtk_text_iter_get_text_line (&iter);
795 gtk_text_layout_real_invalidate (GtkTextLayout *layout,
796 const GtkTextIter *start,
797 const GtkTextIter *end)
800 GtkTextLine *last_line;
802 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
803 g_return_if_fail (layout->wrap_loop_count == 0);
805 /* Because we may be invalidating a mark, it's entirely possible
806 * that gtk_text_iter_equal (start, end) in which case we
807 * should still invalidate the line they are both on. i.e.
808 * we always invalidate the line with "start" even
809 * if there's an empty range.
813 gtk_text_view_index_spew (start_index, "invalidate start");
814 gtk_text_view_index_spew (end_index, "invalidate end");
817 last_line = _gtk_text_iter_get_text_line (end);
818 line = _gtk_text_iter_get_text_line (start);
822 GtkTextLineData *line_data = _gtk_text_line_get_data (line, layout);
824 gtk_text_layout_invalidate_cache (layout, line);
827 _gtk_text_line_invalidate_wrap (line, line_data);
829 if (line == last_line)
832 line = _gtk_text_line_next_excluding_last (line);
835 gtk_text_layout_invalidated (layout);
839 gtk_text_layout_real_free_line_data (GtkTextLayout *layout,
841 GtkTextLineData *line_data)
843 gtk_text_layout_invalidate_cache (layout, line);
851 * gtk_text_layout_is_valid:
852 * @layout: a #GtkTextLayout
854 * Check if there are any invalid regions in a #GtkTextLayout's buffer
856 * Return value: %TRUE if any invalid regions were found
859 gtk_text_layout_is_valid (GtkTextLayout *layout)
861 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), FALSE);
863 return _gtk_text_btree_is_valid (_gtk_text_buffer_get_btree (layout->buffer),
868 update_layout_size (GtkTextLayout *layout)
870 _gtk_text_btree_get_view_size (_gtk_text_buffer_get_btree (layout->buffer),
872 &layout->width, &layout->height);
876 * gtk_text_layout_validate_yrange:
877 * @layout: a #GtkTextLayout
878 * @anchor: iter pointing into a line that will be used as the
880 * @y0_: offset from the top of the line pointed to by @anchor at
881 * which to begin validation. (The offset here is in pixels
883 * @y1_: offset from the top of the line pointed to by @anchor at
884 * which to end validation. (The offset here is in pixels
887 * Ensure that a region of a #GtkTextLayout is valid. The ::changed
888 * signal will be emitted if any lines are validated.
891 gtk_text_layout_validate_yrange (GtkTextLayout *layout,
897 GtkTextLine *first_line = NULL;
898 GtkTextLine *last_line = NULL;
900 gint delta_height = 0;
901 gint first_line_y = 0; /* Quiet GCC */
902 gint last_line_y = 0; /* Quiet GCC */
904 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
911 /* Validate backwards from the anchor line to y0
913 line = _gtk_text_iter_get_text_line (anchor);
914 line = _gtk_text_line_previous (line);
916 while (line && seen < -y0)
918 GtkTextLineData *line_data = _gtk_text_line_get_data (line, layout);
919 if (!line_data || !line_data->valid)
921 gint old_height, new_height;
923 old_height = line_data ? line_data->height : 0;
925 _gtk_text_btree_validate_line (_gtk_text_buffer_get_btree (layout->buffer),
927 line_data = _gtk_text_line_get_data (line, layout);
929 new_height = line_data ? line_data->height : 0;
931 delta_height += new_height - old_height;
934 first_line_y = -seen - new_height;
942 seen += line_data ? line_data->height : 0;
943 line = _gtk_text_line_previous (line);
946 /* Validate forwards to y1 */
947 line = _gtk_text_iter_get_text_line (anchor);
949 while (line && seen < y1)
951 GtkTextLineData *line_data = _gtk_text_line_get_data (line, layout);
952 if (!line_data || !line_data->valid)
954 gint old_height, new_height;
956 old_height = line_data ? line_data->height : 0;
958 _gtk_text_btree_validate_line (_gtk_text_buffer_get_btree (layout->buffer),
960 line_data = _gtk_text_line_get_data (line, layout);
961 new_height = line_data ? line_data->height : 0;
963 delta_height += new_height - old_height;
971 last_line_y = seen + new_height;
974 seen += line_data ? line_data->height : 0;
975 line = _gtk_text_line_next_excluding_last (line);
978 /* If we found and validated any invalid lines, update size and
979 * emit the changed signal
985 update_layout_size (layout);
987 line_top = _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
990 gtk_text_layout_emit_changed (layout,
992 last_line_y - first_line_y - delta_height,
993 last_line_y - first_line_y);
998 * gtk_text_layout_validate:
999 * @tree: a #GtkTextLayout
1000 * @max_pixels: the maximum number of pixels to validate. (No more
1001 * than one paragraph beyond this limit will be validated)
1003 * Validate regions of a #GtkTextLayout. The ::changed signal will
1004 * be emitted for each region validated.
1007 gtk_text_layout_validate (GtkTextLayout *layout,
1010 gint y, old_height, new_height;
1012 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
1014 while (max_pixels > 0 &&
1015 _gtk_text_btree_validate (_gtk_text_buffer_get_btree (layout->buffer),
1017 &y, &old_height, &new_height))
1019 max_pixels -= new_height;
1021 update_layout_size (layout);
1022 gtk_text_layout_emit_changed (layout, y, old_height, new_height);
1026 static GtkTextLineData*
1027 gtk_text_layout_real_wrap (GtkTextLayout *layout,
1030 GtkTextLineData *line_data)
1032 GtkTextLineDisplay *display;
1034 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), NULL);
1035 g_return_val_if_fail (line != NULL, NULL);
1037 if (line_data == NULL)
1039 line_data = _gtk_text_line_data_new (layout, line);
1040 _gtk_text_line_add_data (line, line_data);
1043 display = gtk_text_layout_get_line_display (layout, line, TRUE);
1044 line_data->width = display->width;
1045 line_data->height = display->height;
1046 line_data->valid = TRUE;
1047 gtk_text_layout_free_line_display (layout, display);
1053 * Layout utility functions
1056 /* If you get the style with get_style () you need to call
1057 release_style () to free it. */
1058 static GtkTextAttributes*
1059 get_style (GtkTextLayout *layout,
1060 const GtkTextIter *iter)
1064 GtkTextAttributes *style;
1066 /* If we have the one-style cache, then it means
1067 that we haven't seen a toggle since we filled in the
1070 if (layout->one_style_cache != NULL)
1072 gtk_text_attributes_ref (layout->one_style_cache);
1073 return layout->one_style_cache;
1076 g_assert (layout->one_style_cache == NULL);
1078 /* Get the tags at this spot */
1079 tags = _gtk_text_btree_get_tags (iter, &tag_count);
1081 /* No tags, use default style */
1082 if (tags == NULL || tag_count == 0)
1084 /* One ref for the return value, one ref for the
1085 layout->one_style_cache reference */
1086 gtk_text_attributes_ref (layout->default_style);
1087 gtk_text_attributes_ref (layout->default_style);
1088 layout->one_style_cache = layout->default_style;
1093 return layout->default_style;
1096 /* Sort tags in ascending order of priority */
1097 _gtk_text_tag_array_sort (tags, tag_count);
1099 style = gtk_text_attributes_new ();
1101 gtk_text_attributes_copy_values (layout->default_style,
1104 _gtk_text_attributes_fill_from_tags (style,
1110 g_assert (style->refcount == 1);
1112 /* Leave this style as the last one seen */
1113 g_assert (layout->one_style_cache == NULL);
1114 gtk_text_attributes_ref (style); /* ref held by layout->one_style_cache */
1115 layout->one_style_cache = style;
1117 /* Returning yet another refcount */
1122 release_style (GtkTextLayout *layout,
1123 GtkTextAttributes *style)
1125 g_return_if_fail (style != NULL);
1126 g_return_if_fail (style->refcount > 0);
1128 gtk_text_attributes_unref (style);
1135 /* This function tries to optimize the case where a line
1136 is completely invisible */
1138 totally_invisible_line (GtkTextLayout *layout,
1142 GtkTextLineSegment *seg;
1145 /* Check if the first char is visible, if so we are partially visible.
1146 * Note that we have to check this since we don't know the current
1147 * invisible/noninvisible toggle state; this function can use the whole btree
1150 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
1153 if (!_gtk_text_btree_char_is_invisible (iter))
1157 seg = line->segments;
1161 if (seg->byte_count > 0)
1162 bytes += seg->byte_count;
1164 /* Note that these two tests can cause us to bail out
1165 * when we shouldn't, because a higher-priority tag
1166 * may override these settings. However the important
1167 * thing is to only invisible really-invisible lines, rather
1168 * than to invisible all really-invisible lines.
1171 else if (seg->type == >k_text_toggle_on_type)
1173 invalidate_cached_style (layout);
1175 /* Bail out if an elision-unsetting tag begins */
1176 if (seg->body.toggle.info->tag->invisible_set &&
1177 !seg->body.toggle.info->tag->values->invisible)
1180 else if (seg->type == >k_text_toggle_off_type)
1182 invalidate_cached_style (layout);
1184 /* Bail out if an elision-setting tag ends */
1185 if (seg->body.toggle.info->tag->invisible_set &&
1186 seg->body.toggle.info->tag->values->invisible)
1193 if (seg != NULL) /* didn't reach line end */
1200 set_para_values (GtkTextLayout *layout,
1201 PangoDirection base_dir,
1202 GtkTextAttributes *style,
1203 GtkTextLineDisplay *display)
1205 PangoAlignment pango_align = PANGO_ALIGN_LEFT;
1210 /* If no base direction was found, then use the style direction */
1211 case PANGO_DIRECTION_NEUTRAL :
1212 display->direction = style->direction;
1214 /* Override the base direction */
1215 if (display->direction == GTK_TEXT_DIR_RTL)
1216 base_dir = PANGO_DIRECTION_RTL;
1218 base_dir = PANGO_DIRECTION_LTR;
1221 case PANGO_DIRECTION_RTL :
1222 display->direction = GTK_TEXT_DIR_RTL;
1225 display->direction = GTK_TEXT_DIR_LTR;
1229 if (display->direction == GTK_TEXT_DIR_RTL)
1230 display->layout = pango_layout_new (layout->rtl_context);
1232 display->layout = pango_layout_new (layout->ltr_context);
1234 switch (style->justification)
1236 case GTK_JUSTIFY_LEFT:
1237 pango_align = (base_dir == PANGO_DIRECTION_LTR) ? PANGO_ALIGN_LEFT : PANGO_ALIGN_RIGHT;
1239 case GTK_JUSTIFY_RIGHT:
1240 pango_align = (base_dir == PANGO_DIRECTION_LTR) ? PANGO_ALIGN_RIGHT : PANGO_ALIGN_LEFT;
1242 case GTK_JUSTIFY_CENTER:
1243 pango_align = PANGO_ALIGN_CENTER;
1245 case GTK_JUSTIFY_FILL:
1246 g_warning ("FIXME we don't support GTK_JUSTIFY_FILL yet");
1249 g_assert_not_reached ();
1253 pango_layout_set_alignment (display->layout, pango_align);
1254 pango_layout_set_spacing (display->layout,
1255 style->pixels_inside_wrap * PANGO_SCALE);
1258 pango_layout_set_tabs (display->layout, style->tabs);
1260 display->top_margin = style->pixels_above_lines;
1261 display->height = style->pixels_above_lines + style->pixels_below_lines;
1262 display->bottom_margin = style->pixels_below_lines;
1263 display->left_margin = style->left_margin;
1264 display->right_margin = style->right_margin;
1266 display->x_offset = display->left_margin;
1268 pango_layout_set_indent (display->layout,
1269 style->indent * PANGO_SCALE);
1271 switch (style->wrap_mode)
1274 layout_width = layout->screen_width - display->left_margin - display->right_margin;
1275 pango_layout_set_width (display->layout, layout_width * PANGO_SCALE);
1276 pango_layout_set_wrap (display->layout, PANGO_WRAP_CHAR);
1279 layout_width = layout->screen_width - display->left_margin - display->right_margin;
1280 pango_layout_set_width (display->layout, layout_width * PANGO_SCALE);
1281 pango_layout_set_wrap (display->layout, PANGO_WRAP_WORD);
1284 case GTK_WRAP_WORD_CHAR:
1285 layout_width = layout->screen_width - display->left_margin - display->right_margin;
1286 pango_layout_set_width (display->layout, layout_width * PANGO_SCALE);
1287 pango_layout_set_wrap (display->layout, PANGO_WRAP_WORD_CHAR);
1294 display->total_width = MAX (layout->screen_width, layout->width) - display->left_margin - display->right_margin;
1296 if (style->pg_bg_color)
1297 display->pg_bg_color = gdk_color_copy (style->pg_bg_color);
1299 display->pg_bg_color = NULL;
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 GtkTextAttrAppearance *appearance_attr = (GtkTextAttrAppearance *)attr;
1315 if (appearance_attr->appearance.bg_stipple)
1316 g_object_unref (appearance_attr->appearance.bg_stipple);
1317 if (appearance_attr->appearance.fg_stipple)
1318 g_object_unref (appearance_attr->appearance.fg_stipple);
1320 g_slice_free (GtkTextAttrAppearance, appearance_attr);
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_slice_new (GtkTextAttrAppearance);
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;
1509 tmp_list = seg->body.child.widgets;
1510 while (tmp_list != NULL)
1512 GtkWidget *child = tmp_list->data;
1514 if (_gtk_anchored_child_get_layout (child) == layout)
1519 gtk_widget_get_child_requisition (child, &req);
1522 height = req.height;
1529 tmp_list = g_slist_next (tmp_list);
1532 if (tmp_list == NULL)
1534 /* If tmp_list == NULL then there is no widget at this anchor in
1535 * this display; not an error. We make up an arbitrary size
1536 * to use, just so the programmer can see the blank spot.
1537 * We also put a NULL in the shaped objects list, to keep
1538 * the correspondence between the list and the shaped chars in
1539 * the layout. A bad hack, yes.
1548 display->shaped_objects = g_slist_append (display->shaped_objects, widget);
1551 logical_rect.y = -height * PANGO_SCALE;
1552 logical_rect.width = width * PANGO_SCALE;
1553 logical_rect.height = height * PANGO_SCALE;
1555 attr = pango_attr_shape_new_with_data (&logical_rect, &logical_rect,
1556 widget, NULL, NULL);
1557 attr->start_index = start;
1558 attr->end_index = start + seg->byte_count;
1559 pango_attr_list_insert (attrs, attr);
1563 add_cursor (GtkTextLayout *layout,
1564 GtkTextLineDisplay *display,
1565 GtkTextLineSegment *seg,
1568 PangoRectangle strong_pos, weak_pos;
1569 GtkTextCursorDisplay *cursor = NULL; /* Quiet GCC */
1570 gboolean add_weak = FALSE;
1571 gboolean add_strong = FALSE;
1573 /* Hide insertion cursor when we have a selection or the layout
1574 * user has hidden the cursor.
1576 if (_gtk_text_btree_mark_is_insert (_gtk_text_buffer_get_btree (layout->buffer),
1577 seg->body.mark.obj) &&
1578 (!layout->cursor_visible ||
1579 gtk_text_buffer_get_selection_bounds (layout->buffer, NULL, NULL)))
1582 pango_layout_get_cursor_pos (display->layout, start, &strong_pos, &weak_pos);
1584 if (layout->cursor_direction == GTK_TEXT_DIR_NONE)
1589 else if (display->direction == layout->cursor_direction)
1596 cursor = g_new (GtkTextCursorDisplay, 1);
1598 cursor->x = PANGO_PIXELS (strong_pos.x);
1599 cursor->y = PANGO_PIXELS (strong_pos.y);
1600 cursor->height = PANGO_PIXELS (strong_pos.height);
1601 cursor->is_strong = TRUE;
1602 cursor->is_weak = (layout->cursor_direction == GTK_TEXT_DIR_NONE) ? FALSE : TRUE;
1603 display->cursors = g_slist_prepend (display->cursors, cursor);
1608 if (weak_pos.x == strong_pos.x && add_strong)
1609 cursor->is_weak = TRUE;
1612 cursor = g_new (GtkTextCursorDisplay, 1);
1614 cursor->x = PANGO_PIXELS (weak_pos.x);
1615 cursor->y = PANGO_PIXELS (weak_pos.y);
1616 cursor->height = PANGO_PIXELS (weak_pos.height);
1617 cursor->is_strong = (layout->cursor_direction == GTK_TEXT_DIR_NONE) ? FALSE : TRUE;
1618 cursor->is_weak = TRUE;
1619 display->cursors = g_slist_prepend (display->cursors, cursor);
1625 is_shape (PangoLayoutRun *run)
1627 GSList *tmp_list = run->item->analysis.extra_attrs;
1631 PangoAttribute *attr = tmp_list->data;
1633 if (attr->klass->type == PANGO_ATTR_SHAPE)
1636 tmp_list = tmp_list->next;
1643 allocate_child_widgets (GtkTextLayout *text_layout,
1644 GtkTextLineDisplay *display)
1646 GSList *shaped = display->shaped_objects;
1647 PangoLayout *layout = display->layout;
1648 PangoLayoutIter *iter;
1650 iter = pango_layout_get_iter (layout);
1654 PangoLayoutRun *run = pango_layout_iter_get_run_readonly (iter);
1656 if (run && is_shape (run))
1658 GObject *shaped_object = shaped->data;
1659 shaped = shaped->next;
1661 /* shaped_object is NULL for child anchors with no
1662 * widgets stored at them
1664 if (shaped_object && GTK_IS_WIDGET (shaped_object))
1666 PangoRectangle extents;
1668 /* We emit "allocate_child" with the x,y of
1669 * the widget with respect to the top of the line
1670 * and the left side of the buffer
1673 pango_layout_iter_get_run_extents (iter,
1677 g_signal_emit (text_layout,
1678 signals[ALLOCATE_CHILD],
1681 PANGO_PIXELS (extents.x) + display->x_offset,
1682 PANGO_PIXELS (extents.y) + display->top_margin);
1686 while (pango_layout_iter_next_run (iter));
1688 pango_layout_iter_free (iter);
1692 convert_color (GdkColor *result,
1693 PangoAttrColor *attr)
1695 result->red = attr->color.red;
1696 result->blue = attr->color.blue;
1697 result->green = attr->color.green;
1700 /* This function is used to convert the preedit string attributes, which are
1701 * standard PangoAttributes, into the custom attributes used by the text
1702 * widget and insert them into a attr list with a given offset.
1705 add_preedit_attrs (GtkTextLayout *layout,
1706 GtkTextAttributes *style,
1707 PangoAttrList *attrs,
1711 PangoAttrIterator *iter = pango_attr_list_get_iterator (layout->preedit_attrs);
1715 GtkTextAppearance appearance = style->appearance;
1716 PangoFontDescription *font_desc = pango_font_description_copy_static (style->font);
1717 PangoAttribute *insert_attr;
1718 GSList *extra_attrs = NULL;
1720 PangoLanguage *language;
1723 pango_attr_iterator_range (iter, &start, &end);
1725 if (end == G_MAXINT)
1726 end = layout->preedit_len;
1731 pango_attr_iterator_get_font (iter, font_desc, &language, &extra_attrs);
1733 tmp_list = extra_attrs;
1736 PangoAttribute *attr = tmp_list->data;
1738 switch (attr->klass->type)
1740 case PANGO_ATTR_FOREGROUND:
1741 convert_color (&appearance.fg_color, (PangoAttrColor *)attr);
1743 case PANGO_ATTR_BACKGROUND:
1744 convert_color (&appearance.bg_color, (PangoAttrColor *)attr);
1745 appearance.draw_bg = TRUE;
1747 case PANGO_ATTR_UNDERLINE:
1748 appearance.underline = ((PangoAttrInt *)attr)->value;
1750 case PANGO_ATTR_STRIKETHROUGH:
1751 appearance.strikethrough = ((PangoAttrInt *)attr)->value;
1753 case PANGO_ATTR_RISE:
1754 appearance.rise = ((PangoAttrInt *)attr)->value;
1760 pango_attribute_destroy (attr);
1761 tmp_list = tmp_list->next;
1764 g_slist_free (extra_attrs);
1766 insert_attr = pango_attr_font_desc_new (font_desc);
1767 insert_attr->start_index = start + offset;
1768 insert_attr->end_index = end + offset;
1770 pango_attr_list_insert (attrs, insert_attr);
1774 insert_attr = pango_attr_language_new (language);
1775 insert_attr->start_index = start + offset;
1776 insert_attr->end_index = end + offset;
1778 pango_attr_list_insert (attrs, insert_attr);
1781 add_generic_attrs (layout, &appearance, end - start,
1782 attrs, start + offset,
1785 pango_font_description_free (font_desc);
1787 while (pango_attr_iterator_next (iter));
1789 pango_attr_iterator_destroy (iter);
1792 GtkTextLineDisplay *
1793 gtk_text_layout_get_line_display (GtkTextLayout *layout,
1797 GtkTextLayoutPrivate *priv = GTK_TEXT_LAYOUT_GET_PRIVATE (layout);
1798 GtkTextLineDisplay *display;
1799 GtkTextLineSegment *seg;
1801 GtkTextAttributes *style;
1803 PangoAttrList *attrs;
1804 gint text_allocated, layout_byte_offset, buffer_byte_offset;
1805 PangoRectangle extents;
1806 gboolean para_values_set = FALSE;
1807 GSList *cursor_byte_offsets = NULL;
1808 GSList *cursor_segs = NULL;
1809 GSList *tmp_list1, *tmp_list2;
1810 gboolean saw_widget = FALSE;
1811 PangoDirection base_dir;
1813 g_return_val_if_fail (line != NULL, NULL);
1815 if (layout->one_display_cache)
1817 if (line == layout->one_display_cache->line &&
1818 (size_only || !layout->one_display_cache->size_only))
1819 return layout->one_display_cache;
1822 GtkTextLineDisplay *tmp_display = layout->one_display_cache;
1823 layout->one_display_cache = NULL;
1824 gtk_text_layout_free_line_display (layout, tmp_display);
1828 display = g_new0 (GtkTextLineDisplay, 1);
1830 display->size_only = size_only;
1831 display->line = line;
1832 display->insert_index = -1;
1834 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
1837 /* Special-case optimization for completely
1838 * invisible lines; makes it faster to deal
1839 * with sequences of invisible lines.
1841 if (totally_invisible_line (layout, line, &iter))
1843 if (display->direction == GTK_TEXT_DIR_RTL)
1844 display->layout = pango_layout_new (layout->rtl_context);
1846 display->layout = pango_layout_new (layout->ltr_context);
1851 /* Find the bidi base direction */
1852 base_dir = line->dir_propagated_forward;
1853 if (base_dir == PANGO_DIRECTION_NEUTRAL)
1854 base_dir = line->dir_propagated_back;
1856 if (line == priv->cursor_line &&
1857 line->dir_strong == PANGO_DIRECTION_NEUTRAL)
1859 base_dir = (layout->keyboard_direction == GTK_TEXT_DIR_LTR) ?
1860 PANGO_DIRECTION_LTR : PANGO_DIRECTION_RTL;
1863 /* Allocate space for flat text for buffer
1865 text_allocated = _gtk_text_line_byte_count (line);
1866 text = g_malloc (text_allocated);
1868 attrs = pango_attr_list_new ();
1870 /* Iterate over segments, creating display chunks for them. */
1871 layout_byte_offset = 0; /* current length of layout text (includes preedit, does not include invisible text) */
1872 buffer_byte_offset = 0; /* position in the buffer line */
1873 seg = _gtk_text_iter_get_any_segment (&iter);
1876 /* Displayable segments */
1877 if (seg->type == >k_text_char_type ||
1878 seg->type == >k_text_pixbuf_type ||
1879 seg->type == >k_text_child_type)
1881 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
1883 buffer_byte_offset);
1884 style = get_style (layout, &iter);
1886 /* We have to delay setting the paragraph values until we
1887 * hit the first pixbuf or text segment because toggles at
1888 * the beginning of the paragraph should affect the
1889 * paragraph-global values
1891 if (!para_values_set)
1893 set_para_values (layout, base_dir, style, display);
1894 para_values_set = TRUE;
1897 /* First see if the chunk is invisible, and ignore it if so. Tk
1898 * looked at tabs, wrap mode, etc. before doing this, but
1899 * that made no sense to me, so I am just skipping the
1902 if (!style->invisible)
1904 if (seg->type == >k_text_char_type)
1906 /* We don't want to split segments because of marks,
1907 * so we scan forward for more segments only
1908 * separated from us by marks. In theory, we should
1909 * also merge segments with identical styles, even
1910 * if there are toggles in-between
1914 GtkTextLineSegment *prev_seg = NULL;
1918 if (seg->type == >k_text_char_type)
1920 memcpy (text + layout_byte_offset, seg->body.chars, seg->byte_count);
1921 layout_byte_offset += seg->byte_count;
1922 buffer_byte_offset += seg->byte_count;
1923 bytes += seg->byte_count;
1925 else if (seg->type == >k_text_right_mark_type ||
1926 seg->type == >k_text_left_mark_type)
1928 /* If we have preedit string, break out of this loop - we'll almost
1929 * certainly have different attributes on the preedit string
1932 if (layout->preedit_len > 0 &&
1933 _gtk_text_btree_mark_is_insert (_gtk_text_buffer_get_btree (layout->buffer),
1934 seg->body.mark.obj))
1937 if (seg->body.mark.visible)
1939 cursor_byte_offsets = g_slist_prepend (cursor_byte_offsets, GINT_TO_POINTER (layout_byte_offset));
1940 cursor_segs = g_slist_prepend (cursor_segs, seg);
1950 seg = prev_seg; /* Back up one */
1951 add_generic_attrs (layout, &style->appearance,
1953 attrs, layout_byte_offset - bytes,
1955 add_text_attrs (layout, style, bytes, attrs,
1956 layout_byte_offset - bytes, size_only);
1958 else if (seg->type == >k_text_pixbuf_type)
1960 add_generic_attrs (layout,
1963 attrs, layout_byte_offset,
1965 add_pixbuf_attrs (layout, display, style,
1966 seg, attrs, layout_byte_offset);
1967 memcpy (text + layout_byte_offset, gtk_text_unknown_char_utf8,
1969 layout_byte_offset += seg->byte_count;
1970 buffer_byte_offset += seg->byte_count;
1972 else if (seg->type == >k_text_child_type)
1976 add_generic_attrs (layout, &style->appearance,
1978 attrs, layout_byte_offset,
1980 add_child_attrs (layout, display, style,
1981 seg, attrs, layout_byte_offset);
1982 memcpy (text + layout_byte_offset, gtk_text_unknown_char_utf8,
1984 layout_byte_offset += seg->byte_count;
1985 buffer_byte_offset += seg->byte_count;
1989 /* We don't know this segment type */
1990 g_assert_not_reached ();
1993 } /* if (segment was visible) */
1996 /* Invisible segment */
1997 buffer_byte_offset += seg->byte_count;
2000 release_style (layout, style);
2004 else if (seg->type == >k_text_toggle_on_type ||
2005 seg->type == >k_text_toggle_off_type)
2007 /* Style may have changed, drop our
2008 current cached style */
2009 invalidate_cached_style (layout);
2013 else if (seg->type == >k_text_right_mark_type ||
2014 seg->type == >k_text_left_mark_type)
2016 gint cursor_offset = 0;
2018 /* At the insertion point, add the preedit string, if any */
2020 if (_gtk_text_btree_mark_is_insert (_gtk_text_buffer_get_btree (layout->buffer),
2021 seg->body.mark.obj))
2023 display->insert_index = layout_byte_offset;
2025 if (layout->preedit_len > 0)
2027 text_allocated += layout->preedit_len;
2028 text = g_realloc (text, text_allocated);
2030 style = get_style (layout, &iter);
2031 add_preedit_attrs (layout, style, attrs, layout_byte_offset, size_only);
2032 release_style (layout, style);
2034 memcpy (text + layout_byte_offset, layout->preedit_string, layout->preedit_len);
2035 layout_byte_offset += layout->preedit_len;
2036 /* DO NOT increment the buffer byte offset for preedit */
2038 cursor_offset = layout->preedit_cursor - layout->preedit_len;
2043 /* Display visible marks */
2045 if (seg->body.mark.visible)
2047 cursor_byte_offsets = g_slist_prepend (cursor_byte_offsets,
2048 GINT_TO_POINTER (layout_byte_offset + cursor_offset));
2049 cursor_segs = g_slist_prepend (cursor_segs, seg);
2054 g_error ("Unknown segment type: %s", seg->type->name);
2059 if (!para_values_set)
2061 style = get_style (layout, &iter);
2062 set_para_values (layout, base_dir, style, display);
2063 release_style (layout, style);
2066 /* Pango doesn't want the trailing paragraph delimiters */
2069 /* Only one character has type G_UNICODE_PARAGRAPH_SEPARATOR in
2070 * Unicode 3.0; update this if that changes.
2072 #define PARAGRAPH_SEPARATOR 0x2029
2075 if (layout_byte_offset > 0)
2077 const char *prev = g_utf8_prev_char (text + layout_byte_offset);
2078 ch = g_utf8_get_char (prev);
2079 if (ch == PARAGRAPH_SEPARATOR || ch == '\r' || ch == '\n')
2080 layout_byte_offset = prev - text; /* chop off */
2082 if (ch == '\n' && layout_byte_offset > 0)
2084 /* Possibly chop a CR as well */
2085 prev = g_utf8_prev_char (text + layout_byte_offset);
2087 --layout_byte_offset;
2092 pango_layout_set_text (display->layout, text, layout_byte_offset);
2093 pango_layout_set_attributes (display->layout, attrs);
2095 tmp_list1 = cursor_byte_offsets;
2096 tmp_list2 = cursor_segs;
2099 add_cursor (layout, display, tmp_list2->data,
2100 GPOINTER_TO_INT (tmp_list1->data));
2101 tmp_list1 = tmp_list1->next;
2102 tmp_list2 = tmp_list2->next;
2104 g_slist_free (cursor_byte_offsets);
2105 g_slist_free (cursor_segs);
2107 pango_layout_get_extents (display->layout, NULL, &extents);
2109 display->width = PIXEL_BOUND (extents.width) + display->left_margin + display->right_margin;
2110 display->height += PANGO_PIXELS (extents.height);
2112 /* If we aren't wrapping, we need to do the alignment of each
2113 * paragraph ourselves.
2115 if (pango_layout_get_width (display->layout) < 0)
2117 gint excess = display->total_width - display->width;
2119 switch (pango_layout_get_alignment (display->layout))
2121 case PANGO_ALIGN_LEFT:
2123 case PANGO_ALIGN_CENTER:
2124 display->x_offset += excess / 2;
2126 case PANGO_ALIGN_RIGHT:
2127 display->x_offset += excess;
2132 /* Free this if we aren't in a loop */
2133 if (layout->wrap_loop_count == 0)
2134 invalidate_cached_style (layout);
2137 pango_attr_list_unref (attrs);
2139 layout->one_display_cache = display;
2142 allocate_child_widgets (layout, display);
2148 gtk_text_layout_free_line_display (GtkTextLayout *layout,
2149 GtkTextLineDisplay *display)
2151 if (display != layout->one_display_cache)
2153 if (display->layout)
2154 g_object_unref (display->layout);
2156 if (display->cursors)
2158 g_slist_foreach (display->cursors, (GFunc)g_free, NULL);
2159 g_slist_free (display->cursors);
2161 g_slist_free (display->shaped_objects);
2163 if (display->pg_bg_color)
2164 gdk_color_free (display->pg_bg_color);
2170 /* Functions to convert iter <=> index for the line of a GtkTextLineDisplay
2171 * taking into account the preedit string and invisible text if necessary.
2174 line_display_iter_to_index (GtkTextLayout *layout,
2175 GtkTextLineDisplay *display,
2176 const GtkTextIter *iter)
2180 g_return_val_if_fail (_gtk_text_iter_get_text_line (iter) == display->line, 0);
2182 index = gtk_text_iter_get_visible_line_index (iter);
2184 if (layout->preedit_len > 0 && display->insert_index >= 0)
2186 if (index >= display->insert_index)
2187 index += layout->preedit_len;
2194 line_display_index_to_iter (GtkTextLayout *layout,
2195 GtkTextLineDisplay *display,
2200 g_return_if_fail (!_gtk_text_line_is_last (display->line,
2201 _gtk_text_buffer_get_btree (layout->buffer)));
2203 if (layout->preedit_len > 0 && display->insert_index >= 0)
2205 if (index >= display->insert_index + layout->preedit_len)
2206 index -= layout->preedit_len;
2207 else if (index > display->insert_index)
2209 index = display->insert_index;
2214 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
2215 iter, display->line, 0);
2217 gtk_text_iter_set_visible_line_index (iter, index);
2219 if (_gtk_text_iter_get_text_line (iter) != display->line)
2221 /* Clamp to end of line - really this clamping should have been done
2222 * before here, maybe in Pango, this is a broken band-aid I think
2224 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
2225 iter, display->line, 0);
2227 if (!gtk_text_iter_ends_line (iter))
2228 gtk_text_iter_forward_to_line_end (iter);
2231 gtk_text_iter_forward_chars (iter, trailing);
2235 get_line_at_y (GtkTextLayout *layout,
2242 if (y > layout->height)
2245 *line = _gtk_text_btree_find_line_by_y (_gtk_text_buffer_get_btree (layout->buffer),
2246 layout, y, line_top);
2249 *line = _gtk_text_btree_get_end_iter_line (_gtk_text_buffer_get_btree (layout->buffer));
2253 _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
2259 * gtk_text_layout_get_line_at_y:
2260 * @layout: a #GtkLayout
2261 * @target_iter: the iterator in which the result is stored
2262 * @y: the y positition
2263 * @line_top: location to store the y coordinate of the
2264 * top of the line. (Can by %NULL.)
2266 * Get the iter at the beginning of the line which is displayed
2270 gtk_text_layout_get_line_at_y (GtkTextLayout *layout,
2271 GtkTextIter *target_iter,
2277 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
2278 g_return_if_fail (target_iter != NULL);
2280 get_line_at_y (layout, y, &line, line_top);
2281 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
2282 target_iter, line, 0);
2286 gtk_text_layout_get_iter_at_pixel (GtkTextLayout *layout,
2287 GtkTextIter *target_iter,
2293 gtk_text_layout_get_iter_at_position (layout, target_iter, &trailing, x, y);
2295 gtk_text_iter_forward_chars (target_iter, trailing);
2298 void gtk_text_layout_get_iter_at_position (GtkTextLayout *layout,
2299 GtkTextIter *target_iter,
2307 GtkTextLineDisplay *display;
2309 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
2310 g_return_if_fail (target_iter != NULL);
2312 get_line_at_y (layout, y, &line, &line_top);
2314 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2316 x -= display->x_offset;
2317 y -= line_top + display->top_margin;
2319 /* If we are below the layout, position the cursor at the last character
2322 if (y > display->height - display->top_margin - display->bottom_margin)
2324 byte_index = _gtk_text_line_byte_count (line);
2329 /* Ignore the "outside" return value from pango. Pango is doing
2330 * the right thing even if we are outside the layout in the
2333 pango_layout_xy_to_index (display->layout, x * PANGO_SCALE, y * PANGO_SCALE,
2334 &byte_index, trailing);
2337 line_display_index_to_iter (layout, display, target_iter, byte_index, 0);
2339 gtk_text_layout_free_line_display (layout, display);
2344 * gtk_text_layout_get_cursor_locations:
2345 * @layout: a #GtkTextLayout
2346 * @iter: a #GtkTextIter
2347 * @strong_pos: location to store the strong cursor position (may be %NULL)
2348 * @weak_pos: location to store the weak cursor position (may be %NULL)
2350 * Given an iterator within a text layout, determine the positions of the
2351 * strong and weak cursors if the insertion point is at that
2352 * iterator. The position of each cursor is stored as a zero-width
2353 * rectangle. The strong cursor location is the location where
2354 * characters of the directionality equal to the base direction of the
2355 * paragraph are inserted. The weak cursor location is the location
2356 * where characters of the directionality opposite to the base
2357 * direction of the paragraph are inserted.
2360 gtk_text_layout_get_cursor_locations (GtkTextLayout *layout,
2362 GdkRectangle *strong_pos,
2363 GdkRectangle *weak_pos)
2366 GtkTextLineDisplay *display;
2369 GtkTextIter insert_iter;
2371 PangoRectangle pango_strong_pos;
2372 PangoRectangle pango_weak_pos;
2374 g_return_if_fail (layout != NULL);
2375 g_return_if_fail (iter != NULL);
2377 line = _gtk_text_iter_get_text_line (iter);
2378 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2379 index = line_display_iter_to_index (layout, display, iter);
2381 line_top = _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
2384 gtk_text_buffer_get_iter_at_mark (layout->buffer, &insert_iter,
2385 gtk_text_buffer_get_mark (layout->buffer,
2388 if (gtk_text_iter_equal (iter, &insert_iter))
2389 index += layout->preedit_cursor - layout->preedit_len;
2391 pango_layout_get_cursor_pos (display->layout, index,
2392 strong_pos ? &pango_strong_pos : NULL,
2393 weak_pos ? &pango_weak_pos : NULL);
2397 strong_pos->x = display->x_offset + pango_strong_pos.x / PANGO_SCALE;
2398 strong_pos->y = line_top + display->top_margin + pango_strong_pos.y / PANGO_SCALE;
2399 strong_pos->width = 0;
2400 strong_pos->height = pango_strong_pos.height / PANGO_SCALE;
2405 weak_pos->x = display->x_offset + pango_weak_pos.x / PANGO_SCALE;
2406 weak_pos->y = line_top + display->top_margin + pango_weak_pos.y / PANGO_SCALE;
2407 weak_pos->width = 0;
2408 weak_pos->height = pango_weak_pos.height / PANGO_SCALE;
2411 gtk_text_layout_free_line_display (layout, display);
2415 * gtk_text_layout_get_line_yrange:
2416 * @layout: a #GtkTextLayout
2417 * @iter: a #GtkTextIter
2418 * @y: location to store the top of the paragraph in pixels,
2420 * @height location to store the height of the paragraph in pixels,
2423 * Find the range of y coordinates for the paragraph containing
2427 gtk_text_layout_get_line_yrange (GtkTextLayout *layout,
2428 const GtkTextIter *iter,
2434 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
2435 g_return_if_fail (_gtk_text_iter_get_btree (iter) == _gtk_text_buffer_get_btree (layout->buffer));
2437 line = _gtk_text_iter_get_text_line (iter);
2440 *y = _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
2444 GtkTextLineData *line_data = _gtk_text_line_get_data (line, layout);
2446 *height = line_data->height;
2453 * _gtk_text_layout_get_line_xrange:
2454 * @layout: a #GtkTextLayout
2455 * @iter: a #GtkTextIter
2456 * @x: location to store the top of the paragraph in pixels,
2458 * @width location to store the height of the paragraph in pixels,
2461 * Find the range of X coordinates for the paragraph containing
2462 * the given iter. Private for 2.0 due to API freeze, could
2463 * be made public for 2.2.
2466 _gtk_text_layout_get_line_xrange (GtkTextLayout *layout,
2467 const GtkTextIter *iter,
2473 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
2474 g_return_if_fail (_gtk_text_iter_get_btree (iter) == _gtk_text_buffer_get_btree (layout->buffer));
2476 line = _gtk_text_iter_get_text_line (iter);
2479 *x = 0; /* FIXME This is wrong; should represent the first available cursor position */
2483 GtkTextLineData *line_data = _gtk_text_line_get_data (line, layout);
2485 *width = line_data->width;
2492 gtk_text_layout_get_iter_location (GtkTextLayout *layout,
2493 const GtkTextIter *iter,
2496 PangoRectangle pango_rect;
2499 GtkTextLineDisplay *display;
2503 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
2504 g_return_if_fail (_gtk_text_iter_get_btree (iter) == _gtk_text_buffer_get_btree (layout->buffer));
2505 g_return_if_fail (rect != NULL);
2507 tree = _gtk_text_iter_get_btree (iter);
2508 line = _gtk_text_iter_get_text_line (iter);
2510 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2512 rect->y = _gtk_text_btree_find_line_top (tree, line, layout);
2514 x_offset = display->x_offset * PANGO_SCALE;
2516 byte_index = gtk_text_iter_get_line_index (iter);
2518 pango_layout_index_to_pos (display->layout, byte_index, &pango_rect);
2520 rect->x = PANGO_PIXELS (x_offset + pango_rect.x);
2521 rect->y += PANGO_PIXELS (pango_rect.y) + display->top_margin;
2522 rect->width = PANGO_PIXELS (pango_rect.width);
2523 rect->height = PANGO_PIXELS (pango_rect.height);
2525 gtk_text_layout_free_line_display (layout, display);
2530 /* Find the iter for the logical beginning of the first display line whose
2531 * top y is >= y. If none exists, move the iter to the logical beginning
2532 * of the last line in the buffer.
2535 find_display_line_below (GtkTextLayout *layout,
2539 GtkTextLine *line, *next;
2540 GtkTextLine *found_line = NULL;
2542 gint found_byte = 0;
2544 line = _gtk_text_btree_find_line_by_y (_gtk_text_buffer_get_btree (layout->buffer),
2545 layout, y, &line_top);
2549 _gtk_text_btree_get_end_iter_line (_gtk_text_buffer_get_btree (layout->buffer));
2552 _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
2556 while (line && !found_line)
2558 GtkTextLineDisplay *display = gtk_text_layout_get_line_display (layout, line, FALSE);
2559 PangoLayoutIter *layout_iter;
2561 layout_iter = pango_layout_get_iter (display->layout);
2563 line_top += display->top_margin;
2567 gint first_y, last_y;
2568 PangoLayoutLine *layout_line = pango_layout_iter_get_line_readonly (layout_iter);
2570 found_byte = layout_line->start_index;
2578 pango_layout_iter_get_line_yrange (layout_iter, &first_y, &last_y);
2579 line_top += (last_y - first_y) / PANGO_SCALE;
2581 while (pango_layout_iter_next_line (layout_iter));
2583 pango_layout_iter_free (layout_iter);
2585 line_top += display->bottom_margin;
2586 gtk_text_layout_free_line_display (layout, display);
2588 next = _gtk_text_line_next_excluding_last (line);
2595 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
2596 iter, found_line, found_byte);
2599 /* Find the iter for the logical beginning of the last display line whose
2600 * top y is >= y. If none exists, move the iter to the logical beginning
2601 * of the first line in the buffer.
2604 find_display_line_above (GtkTextLayout *layout,
2609 GtkTextLine *found_line = NULL;
2611 gint found_byte = 0;
2613 line = _gtk_text_btree_find_line_by_y (_gtk_text_buffer_get_btree (layout->buffer), layout, y, &line_top);
2616 line = _gtk_text_btree_get_end_iter_line (_gtk_text_buffer_get_btree (layout->buffer));
2618 line_top = _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer), line, layout);
2621 while (line && !found_line)
2623 GtkTextLineDisplay *display = gtk_text_layout_get_line_display (layout, line, FALSE);
2624 PangoRectangle logical_rect;
2625 PangoLayoutIter *layout_iter;
2628 layout_iter = pango_layout_get_iter (display->layout);
2630 line_top -= display->top_margin + display->bottom_margin;
2631 pango_layout_iter_get_layout_extents (layout_iter, NULL, &logical_rect);
2632 line_top -= logical_rect.height / PANGO_SCALE;
2634 tmp_top = line_top + display->top_margin;
2638 gint first_y, last_y;
2639 PangoLayoutLine *layout_line = pango_layout_iter_get_line_readonly (layout_iter);
2641 found_byte = layout_line->start_index;
2643 pango_layout_iter_get_line_yrange (layout_iter, &first_y, &last_y);
2645 tmp_top -= (last_y - first_y) / PANGO_SCALE;
2650 pango_layout_iter_free (layout_iter);
2654 while (pango_layout_iter_next_line (layout_iter));
2656 pango_layout_iter_free (layout_iter);
2658 gtk_text_layout_free_line_display (layout, display);
2660 line = _gtk_text_line_previous (line);
2666 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
2667 iter, found_line, found_byte);
2669 gtk_text_buffer_get_iter_at_offset (layout->buffer, iter, 0);
2673 * gtk_text_layout_clamp_iter_to_vrange:
2674 * @layout: a #GtkTextLayout
2675 * @iter: a #GtkTextIter
2676 * @top: the top of the range
2677 * @bottom: the bottom the range
2679 * If the iterator is not fully in the range @top <= y < @bottom,
2680 * then, if possible, move it the minimum distance so that the
2681 * iterator in this range.
2683 * Returns: %TRUE if the iterator was moved, otherwise %FALSE.
2686 gtk_text_layout_clamp_iter_to_vrange (GtkTextLayout *layout,
2691 GdkRectangle iter_rect;
2693 gtk_text_layout_get_iter_location (layout, iter, &iter_rect);
2695 /* If the iter is at least partially above the range, put the iter
2696 * at the first fully visible line after the range.
2698 if (iter_rect.y < top)
2700 find_display_line_below (layout, iter, top);
2704 /* Otherwise, if the iter is at least partially below the screen, put the
2705 * iter on the last logical position of the last completely visible
2708 else if (iter_rect.y + iter_rect.height > bottom)
2710 find_display_line_above (layout, iter, bottom);
2719 * gtk_text_layout_move_iter_to_previous_line:
2720 * @layout: a #GtkLayout
2721 * @iter: a #GtkTextIter
2723 * Move the iterator to the beginning of the previous line. The lines
2724 * of a wrapped paragraph are treated as distinct for this operation.
2727 gtk_text_layout_move_iter_to_previous_line (GtkTextLayout *layout,
2731 GtkTextLineDisplay *display;
2734 PangoLayoutLine *layout_line;
2736 gboolean update_byte = FALSE;
2738 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), FALSE);
2739 g_return_val_if_fail (iter != NULL, FALSE);
2744 line = _gtk_text_iter_get_text_line (iter);
2745 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2746 line_byte = line_display_iter_to_index (layout, display, iter);
2748 /* If display->height == 0 then the line is invisible, so don't
2751 while (display->height == 0)
2753 GtkTextLine *prev_line;
2755 prev_line = _gtk_text_line_previous (line);
2757 if (prev_line == NULL)
2759 line_display_index_to_iter (layout, display, iter, 0, 0);
2763 gtk_text_layout_free_line_display (layout, display);
2766 display = gtk_text_layout_get_line_display (layout, prev_line, FALSE);
2770 tmp_list = pango_layout_get_lines_readonly (display->layout);
2771 layout_line = tmp_list->data;
2775 line_byte = layout_line->start_index + layout_line->length;
2778 if (line_byte < layout_line->length || !tmp_list->next) /* first line of paragraph */
2780 GtkTextLine *prev_line;
2782 prev_line = _gtk_text_line_previous (line);
2784 /* first line of the whole buffer, do not move the iter and return FALSE */
2785 if (prev_line == NULL)
2790 gtk_text_layout_free_line_display (layout, display);
2792 display = gtk_text_layout_get_line_display (layout, prev_line, FALSE);
2794 if (display->height > 0)
2796 tmp_list = g_slist_last (pango_layout_get_lines_readonly (display->layout));
2797 layout_line = tmp_list->data;
2799 line_display_index_to_iter (layout, display, iter,
2800 layout_line->start_index + layout_line->length, 0);
2804 prev_line = _gtk_text_line_previous (prev_line);
2809 gint prev_offset = layout_line->start_index;
2811 tmp_list = tmp_list->next;
2814 layout_line = tmp_list->data;
2816 if (line_byte < layout_line->start_index + layout_line->length ||
2819 line_display_index_to_iter (layout, display, iter, prev_offset, 0);
2823 prev_offset = layout_line->start_index;
2824 tmp_list = tmp_list->next;
2830 gtk_text_layout_free_line_display (layout, display);
2833 !gtk_text_iter_equal (iter, &orig) &&
2834 !gtk_text_iter_is_end (iter);
2838 * gtk_text_layout_move_iter_to_next_line:
2839 * @layout: a #GtkLayout
2840 * @iter: a #GtkTextIter
2842 * Move the iterator to the beginning of the next line. The
2843 * lines of a wrapped paragraph are treated as distinct for
2847 gtk_text_layout_move_iter_to_next_line (GtkTextLayout *layout,
2851 GtkTextLineDisplay *display;
2854 gboolean found = FALSE;
2855 gboolean found_after = FALSE;
2856 gboolean first = TRUE;
2858 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), FALSE);
2859 g_return_val_if_fail (iter != NULL, FALSE);
2863 line = _gtk_text_iter_get_text_line (iter);
2865 while (line && !found_after)
2869 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2871 if (display->height == 0)
2876 line_byte = line_display_iter_to_index (layout, display, iter);
2882 tmp_list = pango_layout_get_lines_readonly (display->layout);
2883 while (tmp_list && !found_after)
2885 PangoLayoutLine *layout_line = tmp_list->data;
2889 line_display_index_to_iter (layout, display, iter,
2890 layout_line->start_index, 0);
2893 else if (line_byte < layout_line->start_index + layout_line->length || !tmp_list->next)
2896 tmp_list = tmp_list->next;
2901 gtk_text_layout_free_line_display (layout, display);
2903 line = _gtk_text_line_next_excluding_last (line);
2907 gtk_text_buffer_get_end_iter (layout->buffer, iter);
2910 !gtk_text_iter_equal (iter, &orig) &&
2911 !gtk_text_iter_is_end (iter);
2915 * gtk_text_layout_move_iter_to_line_end:
2916 * @layout: a #GtkTextLayout
2917 * @direction: if negative, move to beginning of line, otherwise
2918 move to end of line.
2920 * Move to the beginning or end of a display line.
2923 gtk_text_layout_move_iter_to_line_end (GtkTextLayout *layout,
2928 GtkTextLineDisplay *display;
2933 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), FALSE);
2934 g_return_val_if_fail (iter != NULL, FALSE);
2938 line = _gtk_text_iter_get_text_line (iter);
2939 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2940 line_byte = line_display_iter_to_index (layout, display, iter);
2942 tmp_list = pango_layout_get_lines_readonly (display->layout);
2945 PangoLayoutLine *layout_line = tmp_list->data;
2947 if (line_byte < layout_line->start_index + layout_line->length || !tmp_list->next)
2949 line_display_index_to_iter (layout, display, iter,
2950 direction < 0 ? layout_line->start_index : layout_line->start_index + layout_line->length,
2953 /* FIXME: As a bad hack, we move back one position when we
2954 * are inside a paragraph to avoid going to next line on a
2955 * forced break not at whitespace. Real fix is to keep track
2956 * of whether marks are at leading or trailing edge? */
2957 if (direction > 0 && layout_line->length > 0 &&
2958 !gtk_text_iter_ends_line (iter) &&
2959 !_gtk_text_btree_char_is_invisible (iter))
2960 gtk_text_iter_backward_char (iter);
2964 tmp_list = tmp_list->next;
2967 gtk_text_layout_free_line_display (layout, display);
2970 !gtk_text_iter_equal (iter, &orig) &&
2971 !gtk_text_iter_is_end (iter);
2976 * gtk_text_layout_iter_starts_line:
2977 * @layout: a #GtkTextLayout
2978 * @iter: iterator to test
2980 * Tests whether an iterator is at the start of a display line.
2983 gtk_text_layout_iter_starts_line (GtkTextLayout *layout,
2984 const GtkTextIter *iter)
2987 GtkTextLineDisplay *display;
2991 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), FALSE);
2992 g_return_val_if_fail (iter != NULL, FALSE);
2994 line = _gtk_text_iter_get_text_line (iter);
2995 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2996 line_byte = line_display_iter_to_index (layout, display, iter);
2998 tmp_list = pango_layout_get_lines_readonly (display->layout);
3001 PangoLayoutLine *layout_line = tmp_list->data;
3003 if (line_byte < layout_line->start_index + layout_line->length ||
3006 /* We're located on this line or the para delimiters before
3009 gtk_text_layout_free_line_display (layout, display);
3011 if (line_byte == layout_line->start_index)
3017 tmp_list = tmp_list->next;
3020 g_assert_not_reached ();
3025 gtk_text_layout_get_iter_at_line (GtkTextLayout *layout,
3030 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
3031 iter, line, byte_offset);
3036 * gtk_text_layout_move_iter_to_x:
3037 * @layout: a #GtkTextLayout
3038 * @iter: a #GtkTextIter
3041 * Keeping the iterator on the same line of the layout, move it to the
3042 * specified X coordinate. The lines of a wrapped paragraph are
3043 * treated as distinct for this operation.
3046 gtk_text_layout_move_iter_to_x (GtkTextLayout *layout,
3051 GtkTextLineDisplay *display;
3053 PangoLayoutIter *layout_iter;
3055 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
3056 g_return_if_fail (iter != NULL);
3058 line = _gtk_text_iter_get_text_line (iter);
3060 display = gtk_text_layout_get_line_display (layout, line, FALSE);
3061 line_byte = line_display_iter_to_index (layout, display, iter);
3063 layout_iter = pango_layout_get_iter (display->layout);
3067 PangoLayoutLine *layout_line = pango_layout_iter_get_line_readonly (layout_iter);
3069 if (line_byte < layout_line->start_index + layout_line->length ||
3070 pango_layout_iter_at_last_line (layout_iter))
3072 PangoRectangle logical_rect;
3073 gint byte_index, trailing;
3074 gint x_offset = display->x_offset * PANGO_SCALE;
3076 pango_layout_iter_get_line_extents (layout_iter, NULL, &logical_rect);
3078 pango_layout_line_x_to_index (layout_line,
3079 x * PANGO_SCALE - x_offset - logical_rect.x,
3080 &byte_index, &trailing);
3082 line_display_index_to_iter (layout, display, iter, byte_index, trailing);
3087 while (pango_layout_iter_next_line (layout_iter));
3089 pango_layout_iter_free (layout_iter);
3091 gtk_text_layout_free_line_display (layout, display);
3095 * gtk_text_layout_move_iter_visually:
3096 * @layout: a #GtkTextLayout
3097 * @iter: a #GtkTextIter
3098 * @count: number of characters to move (negative moves left, positive moves right)
3100 * Move the iterator a given number of characters visually, treating
3101 * it as the strong cursor position. If @count is positive, then the
3102 * new strong cursor position will be @count positions to the right of
3103 * the old cursor position. If @count is negative then the new strong
3104 * cursor position will be @count positions to the left of the old
3107 * In the presence of bidirection text, the correspondence
3108 * between logical and visual order will depend on the direction
3109 * of the current run, and there may be jumps when the cursor
3110 * is moved off of the end of a run.
3114 gtk_text_layout_move_iter_visually (GtkTextLayout *layout,
3118 GtkTextLineDisplay *display = NULL;
3120 GtkTextIter lineiter;
3122 g_return_val_if_fail (layout != NULL, FALSE);
3123 g_return_val_if_fail (iter != NULL, FALSE);
3129 GtkTextLine *line = _gtk_text_iter_get_text_line (iter);
3131 gint extra_back = 0;
3134 int byte_count = _gtk_text_line_byte_count (line);
3140 display = gtk_text_layout_get_line_display (layout, line, FALSE);
3142 if (layout->cursor_direction == GTK_TEXT_DIR_NONE)
3145 strong = display->direction == layout->cursor_direction;
3147 line_byte = line_display_iter_to_index (layout, display, iter);
3151 pango_layout_move_cursor_visually (display->layout, strong, line_byte, 0, 1, &new_index, &new_trailing);
3156 pango_layout_move_cursor_visually (display->layout, strong, line_byte, 0, -1, &new_index, &new_trailing);
3160 /* We need to handle the preedit string specially. Well, we don't really need to
3161 * handle it specially, since hopefully calling gtk_im_context_reset() will
3162 * remove the preedit string; but if we start off in front of the preedit
3163 * string (logically) and end up in or on the back edge of the preedit string,
3164 * we should move the iter one place farther.
3166 if (layout->preedit_len > 0 && display->insert_index >= 0)
3168 if (line_byte == display->insert_index + layout->preedit_len &&
3169 new_index < display->insert_index + layout->preedit_len)
3171 line_byte = display->insert_index;
3176 if (new_index < 0 || (new_index == 0 && extra_back))
3180 line = _gtk_text_line_previous (line);
3185 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
3186 &lineiter, line, 0);
3188 while (totally_invisible_line (layout, line, &lineiter));
3190 gtk_text_layout_free_line_display (layout, display);
3191 display = gtk_text_layout_get_line_display (layout, line, FALSE);
3192 new_index = _gtk_text_line_byte_count (line);
3194 else if (new_index > byte_count)
3198 line = _gtk_text_line_next_excluding_last (line);
3202 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
3203 &lineiter, line, 0);
3205 while (totally_invisible_line (layout, line, &lineiter));
3207 gtk_text_layout_free_line_display (layout, display);
3208 display = gtk_text_layout_get_line_display (layout, line, FALSE);
3212 line_display_index_to_iter (layout, display, iter, new_index, new_trailing);
3214 gtk_text_iter_backward_char (iter);
3217 gtk_text_layout_free_line_display (layout, display);
3222 !gtk_text_iter_equal (iter, &orig) &&
3223 !gtk_text_iter_is_end (iter);
3227 gtk_text_layout_spew (GtkTextLayout *layout)
3230 GtkTextDisplayLine *iter;
3232 guint paragraphs = 0;
3233 GtkTextLine *last_line = NULL;
3235 iter = layout->line_list;
3236 while (iter != NULL)
3238 if (iter->line != last_line)
3240 printf ("%5u paragraph (%p)\n", paragraphs, iter->line);
3242 last_line = iter->line;
3245 printf (" %5u y: %d len: %d start: %d bytes: %d\n",
3246 wrapped, iter->y, iter->length, iter->byte_offset,
3253 printf ("Layout %s recompute\n",
3254 layout->need_recompute ? "needs" : "doesn't need");
3256 printf ("Layout pars: %u lines: %u size: %d x %d Screen width: %d\n",
3257 paragraphs, wrapped, layout->width,
3258 layout->height, layout->screen_width);
3262 /* Catch all situations that move the insertion point.
3265 gtk_text_layout_mark_set_handler (GtkTextBuffer *buffer,
3266 const GtkTextIter *location,
3270 GtkTextLayout *layout = GTK_TEXT_LAYOUT (data);
3272 if (mark == gtk_text_buffer_get_insert (buffer))
3273 gtk_text_layout_update_cursor_line (layout);
3277 gtk_text_layout_buffer_insert_text (GtkTextBuffer *textbuffer,
3283 GtkTextLayout *layout = GTK_TEXT_LAYOUT (data);
3285 gtk_text_layout_update_cursor_line (layout);
3289 gtk_text_layout_buffer_delete_range (GtkTextBuffer *textbuffer,
3294 GtkTextLayout *layout = GTK_TEXT_LAYOUT (data);
3296 gtk_text_layout_update_cursor_line (layout);
3299 #define __GTK_TEXT_LAYOUT_C__
3300 #include "gtkaliasdef.c"