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
80 #include "gtkmarshalers.h"
81 #include "gtktextlayout.h"
82 #include "gtktextbtree.h"
83 #include "gtktextiterprivate.h"
88 static GtkTextLineData *gtk_text_layout_real_wrap (GtkTextLayout *layout,
91 GtkTextLineData *line_data);
93 static void gtk_text_layout_invalidated (GtkTextLayout *layout);
95 static void gtk_text_layout_real_invalidate (GtkTextLayout *layout,
96 const GtkTextIter *start,
97 const GtkTextIter *end);
98 static void gtk_text_layout_invalidate_cache (GtkTextLayout *layout,
100 static void gtk_text_layout_invalidate_cursor_line (GtkTextLayout *layout);
101 static void gtk_text_layout_real_free_line_data (GtkTextLayout *layout,
103 GtkTextLineData *line_data);
105 static void gtk_text_layout_invalidate_all (GtkTextLayout *layout);
107 static PangoAttribute *gtk_text_attr_appearance_new (const GtkTextAppearance *appearance);
121 static void gtk_text_layout_init (GtkTextLayout *text_layout);
122 static void gtk_text_layout_class_init (GtkTextLayoutClass *klass);
123 static void gtk_text_layout_finalize (GObject *object);
126 static GtkObjectClass *parent_class = NULL;
127 static guint signals[LAST_SIGNAL] = { 0 };
129 PangoAttrType gtk_text_attr_appearance_type = 0;
132 gtk_text_layout_get_type (void)
134 static GType our_type = 0;
138 static const GTypeInfo our_info =
140 sizeof (GtkTextLayoutClass),
141 (GBaseInitFunc) NULL,
142 (GBaseFinalizeFunc) NULL,
143 (GClassInitFunc) gtk_text_layout_class_init,
144 NULL, /* class_finalize */
145 NULL, /* class_data */
146 sizeof (GtkTextLayout),
148 (GInstanceInitFunc) gtk_text_layout_init
151 our_type = g_type_register_static (G_TYPE_OBJECT, "GtkTextLayout",
159 gtk_text_layout_class_init (GtkTextLayoutClass *klass)
161 GObjectClass *object_class = G_OBJECT_CLASS (klass);
163 parent_class = g_type_class_peek_parent (klass);
165 object_class->finalize = gtk_text_layout_finalize;
167 klass->wrap = gtk_text_layout_real_wrap;
168 klass->invalidate = gtk_text_layout_real_invalidate;
169 klass->free_line_data = gtk_text_layout_real_free_line_data;
171 signals[INVALIDATED] =
172 g_signal_new ("invalidated",
173 G_OBJECT_CLASS_TYPE (object_class),
175 G_STRUCT_OFFSET (GtkTextLayoutClass, invalidated),
177 _gtk_marshal_VOID__VOID,
182 g_signal_new ("changed",
183 G_OBJECT_CLASS_TYPE (object_class),
185 G_STRUCT_OFFSET (GtkTextLayoutClass, changed),
187 _gtk_marshal_VOID__INT_INT_INT,
194 signals[ALLOCATE_CHILD] =
195 g_signal_new ("allocate_child",
196 G_OBJECT_CLASS_TYPE (object_class),
198 G_STRUCT_OFFSET (GtkTextLayoutClass, allocate_child),
200 _gtk_marshal_VOID__OBJECT_INT_INT,
209 gtk_text_layout_init (GtkTextLayout *text_layout)
211 text_layout->cursor_visible = TRUE;
215 gtk_text_layout_new (void)
217 return g_object_new (GTK_TYPE_TEXT_LAYOUT, NULL);
221 free_style_cache (GtkTextLayout *text_layout)
223 if (text_layout->one_style_cache)
225 gtk_text_attributes_unref (text_layout->one_style_cache);
226 text_layout->one_style_cache = NULL;
231 gtk_text_layout_finalize (GObject *object)
233 GtkTextLayout *layout;
235 layout = GTK_TEXT_LAYOUT (object);
237 gtk_text_layout_set_buffer (layout, NULL);
239 if (layout->default_style)
240 gtk_text_attributes_unref (layout->default_style);
241 layout->default_style = NULL;
243 if (layout->ltr_context)
245 g_object_unref (layout->ltr_context);
246 layout->ltr_context = NULL;
248 if (layout->rtl_context)
250 g_object_unref (layout->rtl_context);
251 layout->rtl_context = NULL;
254 if (layout->one_display_cache)
256 GtkTextLineDisplay *tmp_display = layout->one_display_cache;
257 layout->one_display_cache = NULL;
258 gtk_text_layout_free_line_display (layout, tmp_display);
261 (* G_OBJECT_CLASS (parent_class)->finalize) (object);
265 gtk_text_layout_set_buffer (GtkTextLayout *layout,
266 GtkTextBuffer *buffer)
268 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
269 g_return_if_fail (buffer == NULL || GTK_IS_TEXT_BUFFER (buffer));
271 if (layout->buffer == buffer)
274 free_style_cache (layout);
278 _gtk_text_btree_remove_view (_gtk_text_buffer_get_btree (layout->buffer),
281 g_object_unref (layout->buffer);
282 layout->buffer = NULL;
287 layout->buffer = buffer;
289 g_object_ref (buffer);
291 _gtk_text_btree_add_view (_gtk_text_buffer_get_btree (buffer), layout);
296 gtk_text_layout_default_style_changed (GtkTextLayout *layout)
298 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
300 DV (g_print ("invalidating all due to default style change (%s)\n", G_STRLOC));
301 gtk_text_layout_invalidate_all (layout);
305 gtk_text_layout_set_default_style (GtkTextLayout *layout,
306 GtkTextAttributes *values)
308 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
309 g_return_if_fail (values != NULL);
311 if (values == layout->default_style)
314 gtk_text_attributes_ref (values);
316 if (layout->default_style)
317 gtk_text_attributes_unref (layout->default_style);
319 layout->default_style = values;
321 gtk_text_layout_default_style_changed (layout);
325 gtk_text_layout_set_contexts (GtkTextLayout *layout,
326 PangoContext *ltr_context,
327 PangoContext *rtl_context)
329 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
331 if (layout->ltr_context)
332 g_object_unref (ltr_context);
334 layout->ltr_context = ltr_context;
335 g_object_ref (ltr_context);
337 if (layout->rtl_context)
338 g_object_unref (rtl_context);
340 layout->rtl_context = rtl_context;
341 g_object_ref (rtl_context);
343 DV (g_print ("invalidating all due to new pango contexts (%s)\n", G_STRLOC));
344 gtk_text_layout_invalidate_all (layout);
348 * gtk_text_layout_set_cursor_direction:
349 * @direction: the new direction(s) for which to draw cursors.
350 * %GTK_TEXT_DIR_NONE means draw cursors for both
351 * left-to-right insertion and right-to-left insertion.
352 * (The two cursors will be visually distinguished.)
354 * Sets which text directions (left-to-right and/or right-to-left) for
355 * which cursors will be drawn for the insertion point. The visual
356 * point at which new text is inserted depends on whether the new
357 * text is right-to-left or left-to-right, so it may be desired to
358 * make the drawn position of the cursor depend on the keyboard state.
361 gtk_text_layout_set_cursor_direction (GtkTextLayout *layout,
362 GtkTextDirection direction)
364 if (direction != layout->cursor_direction)
366 layout->cursor_direction = direction;
367 gtk_text_layout_invalidate_cursor_line (layout);
372 * gtk_text_layout_get_buffer:
373 * @layout: a #GtkTextLayout
375 * Gets the text buffer used by the layout. See
376 * gtk_text_layout_set_buffer().
378 * Return value: the text buffer used by the layout.
381 gtk_text_layout_get_buffer (GtkTextLayout *layout)
383 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), NULL);
385 return layout->buffer;
389 gtk_text_layout_set_screen_width (GtkTextLayout *layout, gint width)
391 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
392 g_return_if_fail (width >= 0);
393 g_return_if_fail (layout->wrap_loop_count == 0);
395 if (layout->screen_width == width)
398 layout->screen_width = width;
400 DV (g_print ("invalidating all due to new screen width (%s)\n", G_STRLOC));
401 gtk_text_layout_invalidate_all (layout);
405 * gtk_text_layout_set_cursor_visible:
406 * @layout: a #GtkTextLayout
407 * @cursor_visible: If %FALSE, then the insertion cursor will not
408 * be shown, even if the text is editable.
410 * Sets whether the insertion cursor should be shown. Generally,
411 * widgets using #GtkTextLayout will hide the cursor when the
412 * widget does not have the input focus.
415 gtk_text_layout_set_cursor_visible (GtkTextLayout *layout,
416 gboolean cursor_visible)
418 cursor_visible = (cursor_visible != FALSE);
420 if (layout->cursor_visible != cursor_visible)
425 layout->cursor_visible = cursor_visible;
427 /* Now queue a redraw on the paragraph containing the cursor
429 gtk_text_buffer_get_iter_at_mark (layout->buffer, &iter,
430 gtk_text_buffer_get_mark (layout->buffer, "insert"));
432 gtk_text_layout_get_line_yrange (layout, &iter, &y, &height);
433 gtk_text_layout_changed (layout, y, height, height);
435 gtk_text_layout_invalidate_cache (layout, _gtk_text_iter_get_text_line (&iter));
440 * gtk_text_layout_get_cursor_visible:
441 * @layout: a #GtkTextLayout
443 * Returns whether the insertion cursor will be shown.
445 * Return value: if %FALSE, the insertion cursor will not be
446 shown, even if the text is editable.
449 gtk_text_layout_get_cursor_visible (GtkTextLayout *layout)
451 return layout->cursor_visible;
455 * gtk_text_layout_set_preedit_string:
456 * @layout: a #PangoLayout
457 * @preedit_string: a string to display at the insertion point
458 * @preedit_attrs: a #PangoAttrList of attributes that apply to @preedit_string
459 * @cursor_pos: position of cursor within preedit string in chars
461 * Set the preedit string and attributes. The preedit string is a
462 * string showing text that is currently being edited and not
463 * yet committed into the buffer.
466 gtk_text_layout_set_preedit_string (GtkTextLayout *layout,
467 const gchar *preedit_string,
468 PangoAttrList *preedit_attrs,
471 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
472 g_return_if_fail (preedit_attrs != NULL || preedit_string == NULL);
474 if (layout->preedit_string)
475 g_free (layout->preedit_string);
477 if (layout->preedit_attrs)
478 pango_attr_list_unref (layout->preedit_attrs);
482 layout->preedit_string = g_strdup (preedit_string);
483 layout->preedit_len = strlen (layout->preedit_string);
484 pango_attr_list_ref (preedit_attrs);
485 layout->preedit_attrs = preedit_attrs;
487 cursor_pos = CLAMP (cursor_pos, 0, g_utf8_strlen (layout->preedit_string, -1));
488 layout->preedit_cursor = g_utf8_offset_to_pointer (layout->preedit_string, cursor_pos) - layout->preedit_string;
492 layout->preedit_string = NULL;
493 layout->preedit_len = 0;
494 layout->preedit_attrs = NULL;
495 layout->preedit_cursor = 0;
498 gtk_text_layout_invalidate_cursor_line (layout);
502 gtk_text_layout_get_size (GtkTextLayout *layout,
506 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
509 *width = layout->width;
512 *height = layout->height;
516 gtk_text_layout_invalidated (GtkTextLayout *layout)
518 g_signal_emit (layout, signals[INVALIDATED], 0);
522 gtk_text_layout_changed (GtkTextLayout *layout,
527 g_signal_emit (layout, signals[CHANGED], 0, y, old_height, new_height);
531 gtk_text_layout_free_line_data (GtkTextLayout *layout,
533 GtkTextLineData *line_data)
535 (* GTK_TEXT_LAYOUT_GET_CLASS (layout)->free_line_data)
536 (layout, line, line_data);
540 gtk_text_layout_invalidate (GtkTextLayout *layout,
541 const GtkTextIter *start_index,
542 const GtkTextIter *end_index)
544 (* GTK_TEXT_LAYOUT_GET_CLASS (layout)->invalidate)
545 (layout, start_index, end_index);
549 gtk_text_layout_wrap (GtkTextLayout *layout,
552 GtkTextLineData *line_data)
554 return (* GTK_TEXT_LAYOUT_GET_CLASS (layout)->wrap) (layout, line, line_data);
558 gtk_text_layout_get_lines (GtkTextLayout *layout,
559 /* [top_y, bottom_y) */
564 GtkTextLine *first_btree_line;
565 GtkTextLine *last_btree_line;
569 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), NULL);
570 g_return_val_if_fail (bottom_y > top_y, NULL);
575 _gtk_text_btree_find_line_by_y (_gtk_text_buffer_get_btree (layout->buffer),
576 layout, top_y, first_line_y);
577 if (first_btree_line == NULL)
583 /* -1 since bottom_y is one past */
585 _gtk_text_btree_find_line_by_y (_gtk_text_buffer_get_btree (layout->buffer),
586 layout, bottom_y - 1, NULL);
588 if (!last_btree_line)
590 _gtk_text_btree_get_end_iter_line (_gtk_text_buffer_get_btree (layout->buffer));
592 g_assert (last_btree_line != NULL);
594 line = first_btree_line;
597 retval = g_slist_prepend (retval, line);
599 if (line == last_btree_line)
602 line = _gtk_text_line_next_excluding_last (line);
605 retval = g_slist_reverse (retval);
611 invalidate_cached_style (GtkTextLayout *layout)
613 free_style_cache (layout);
616 /* These should be called around a loop which wraps a CONTIGUOUS bunch
617 * of display lines. If the lines aren't contiguous you can't call
621 gtk_text_layout_wrap_loop_start (GtkTextLayout *layout)
623 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
624 g_return_if_fail (layout->one_style_cache == NULL);
626 layout->wrap_loop_count += 1;
630 gtk_text_layout_wrap_loop_end (GtkTextLayout *layout)
632 g_return_if_fail (layout->wrap_loop_count > 0);
634 layout->wrap_loop_count -= 1;
636 if (layout->wrap_loop_count == 0)
638 /* We cache a some stuff if we're iterating over some lines wrapping
639 * them. This cleans it up.
641 /* Nuke our cached style */
642 invalidate_cached_style (layout);
643 g_assert (layout->one_style_cache == NULL);
648 gtk_text_layout_invalidate_all (GtkTextLayout *layout)
653 if (layout->buffer == NULL)
656 gtk_text_buffer_get_bounds (layout->buffer, &start, &end);
658 gtk_text_layout_invalidate (layout, &start, &end);
662 gtk_text_layout_invalidate_cache (GtkTextLayout *layout,
665 if (layout->one_display_cache && line == layout->one_display_cache->line)
667 GtkTextLineDisplay *tmp_display = layout->one_display_cache;
668 layout->one_display_cache = NULL;
669 gtk_text_layout_free_line_display (layout, tmp_display);
673 /* Now invalidate the paragraph containing the cursor
676 gtk_text_layout_invalidate_cursor_line (GtkTextLayout *layout)
680 GtkTextLineData *line_data;
682 gtk_text_buffer_get_iter_at_mark (layout->buffer, &iter,
683 gtk_text_buffer_get_mark (layout->buffer, "insert"));
685 line = _gtk_text_iter_get_text_line (&iter);
686 line_data = _gtk_text_line_get_data (line, layout);
689 gtk_text_layout_invalidate_cache (layout, line);
690 _gtk_text_line_invalidate_wrap (line, line_data);
691 gtk_text_layout_invalidated (layout);
696 gtk_text_layout_real_invalidate (GtkTextLayout *layout,
697 const GtkTextIter *start,
698 const GtkTextIter *end)
701 GtkTextLine *last_line;
703 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
704 g_return_if_fail (layout->wrap_loop_count == 0);
706 /* Because we may be invalidating a mark, it's entirely possible
707 * that gtk_text_iter_equal (start, end) in which case we
708 * should still invalidate the line they are both on. i.e.
709 * we always invalidate the line with "start" even
710 * if there's an empty range.
714 gtk_text_view_index_spew (start_index, "invalidate start");
715 gtk_text_view_index_spew (end_index, "invalidate end");
718 last_line = _gtk_text_iter_get_text_line (end);
719 line = _gtk_text_iter_get_text_line (start);
723 GtkTextLineData *line_data = _gtk_text_line_get_data (line, layout);
725 gtk_text_layout_invalidate_cache (layout, line);
728 _gtk_text_line_invalidate_wrap (line, line_data);
730 if (line == last_line)
733 line = _gtk_text_line_next_excluding_last (line);
736 gtk_text_layout_invalidated (layout);
740 gtk_text_layout_real_free_line_data (GtkTextLayout *layout,
742 GtkTextLineData *line_data)
744 gtk_text_layout_invalidate_cache (layout, line);
752 * gtk_text_layout_is_valid:
753 * @layout: a #GtkTextLayout
755 * Check if there are any invalid regions in a #GtkTextLayout's buffer
757 * Return value: %TRUE if any invalid regions were found
760 gtk_text_layout_is_valid (GtkTextLayout *layout)
762 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), FALSE);
764 return _gtk_text_btree_is_valid (_gtk_text_buffer_get_btree (layout->buffer),
769 update_layout_size (GtkTextLayout *layout)
771 _gtk_text_btree_get_view_size (_gtk_text_buffer_get_btree (layout->buffer),
773 &layout->width, &layout->height);
777 * gtk_text_layout_validate_yrange:
778 * @layout: a #GtkTextLayout
779 * @anchor: iter pointing into a line that will be used as the
781 * @y0_: offset from the top of the line pointed to by @anchor at
782 * which to begin validation. (The offset here is in pixels
784 * @y1_: offset from the top of the line pointed to by @anchor at
785 * which to end validation. (The offset here is in pixels
788 * Ensure that a region of a #GtkTextLayout is valid. The ::changed
789 * signal will be emitted if any lines are validated.
792 gtk_text_layout_validate_yrange (GtkTextLayout *layout,
798 GtkTextLine *first_line = NULL;
799 GtkTextLine *last_line = NULL;
801 gint delta_height = 0;
802 gint first_line_y = 0; /* Quiet GCC */
803 gint last_line_y = 0; /* Quiet GCC */
805 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
812 /* Validate backwards from the anchor line to y0
814 line = _gtk_text_iter_get_text_line (anchor);
816 while (line && seen < -y0)
818 GtkTextLineData *line_data = _gtk_text_line_get_data (line, layout);
819 if (!line_data || !line_data->valid)
821 gint old_height = line_data ? line_data->height : 0;
823 _gtk_text_btree_validate_line (_gtk_text_buffer_get_btree (layout->buffer),
825 line_data = _gtk_text_line_get_data (line, layout);
827 delta_height += line_data->height - old_height;
830 first_line_y = -seen;
834 last_line_y = -seen + line_data->height;
838 seen += line_data->height;
839 line = _gtk_text_line_previous (line);
842 /* Validate forwards to y1 */
843 line = _gtk_text_iter_get_text_line (anchor);
845 while (line && seen < y1)
847 GtkTextLineData *line_data = _gtk_text_line_get_data (line, layout);
848 if (!line_data || !line_data->valid)
850 gint old_height = line_data ? line_data->height : 0;
852 _gtk_text_btree_validate_line (_gtk_text_buffer_get_btree (layout->buffer),
854 line_data = _gtk_text_line_get_data (line, layout);
856 delta_height += line_data->height - old_height;
864 last_line_y = seen + line_data->height;
867 seen += line_data->height;
868 line = _gtk_text_line_next_excluding_last (line);
871 /* If we found and validated any invalid lines, update size and
872 * emit the changed signal
878 update_layout_size (layout);
880 line_top = _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
883 gtk_text_layout_changed (layout,
885 last_line_y - first_line_y - delta_height,
886 last_line_y - first_line_y);
891 * gtk_text_layout_validate:
892 * @tree: a #GtkTextLayout
893 * @max_pixels: the maximum number of pixels to validate. (No more
894 * than one paragraph beyond this limit will be validated)
896 * Validate regions of a #GtkTextLayout. The ::changed signal will
897 * be emitted for each region validated.
900 gtk_text_layout_validate (GtkTextLayout *layout,
903 gint y, old_height, new_height;
905 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
907 while (max_pixels > 0 &&
908 _gtk_text_btree_validate (_gtk_text_buffer_get_btree (layout->buffer),
910 &y, &old_height, &new_height))
912 max_pixels -= new_height;
914 update_layout_size (layout);
915 gtk_text_layout_changed (layout, y, old_height, new_height);
919 static GtkTextLineData*
920 gtk_text_layout_real_wrap (GtkTextLayout *layout,
923 GtkTextLineData *line_data)
925 GtkTextLineDisplay *display;
927 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), NULL);
928 g_return_val_if_fail (line != NULL, NULL);
930 if (line_data == NULL)
932 line_data = _gtk_text_line_data_new (layout, line);
933 _gtk_text_line_add_data (line, line_data);
936 display = gtk_text_layout_get_line_display (layout, line, TRUE);
937 line_data->width = display->width;
938 line_data->height = display->height;
939 line_data->valid = TRUE;
940 gtk_text_layout_free_line_display (layout, display);
946 * Layout utility functions
949 /* If you get the style with get_style () you need to call
950 release_style () to free it. */
951 static GtkTextAttributes*
952 get_style (GtkTextLayout *layout,
953 const GtkTextIter *iter)
957 GtkTextAttributes *style;
959 /* If we have the one-style cache, then it means
960 that we haven't seen a toggle since we filled in the
963 if (layout->one_style_cache != NULL)
965 gtk_text_attributes_ref (layout->one_style_cache);
966 return layout->one_style_cache;
969 g_assert (layout->one_style_cache == NULL);
971 /* Get the tags at this spot */
972 tags = _gtk_text_btree_get_tags (iter, &tag_count);
974 /* No tags, use default style */
975 if (tags == NULL || tag_count == 0)
977 /* One ref for the return value, one ref for the
978 layout->one_style_cache reference */
979 gtk_text_attributes_ref (layout->default_style);
980 gtk_text_attributes_ref (layout->default_style);
981 layout->one_style_cache = layout->default_style;
986 return layout->default_style;
989 /* Sort tags in ascending order of priority */
990 _gtk_text_tag_array_sort (tags, tag_count);
992 style = gtk_text_attributes_new ();
994 gtk_text_attributes_copy_values (layout->default_style,
997 _gtk_text_attributes_fill_from_tags (style,
1003 g_assert (style->refcount == 1);
1005 /* Leave this style as the last one seen */
1006 g_assert (layout->one_style_cache == NULL);
1007 gtk_text_attributes_ref (style); /* ref held by layout->one_style_cache */
1008 layout->one_style_cache = style;
1010 /* Returning yet another refcount */
1015 release_style (GtkTextLayout *layout,
1016 GtkTextAttributes *style)
1018 g_return_if_fail (style != NULL);
1019 g_return_if_fail (style->refcount > 0);
1021 gtk_text_attributes_unref (style);
1028 /* This function tries to optimize the case where a line
1029 is completely invisible */
1031 totally_invisible_line (GtkTextLayout *layout,
1035 GtkTextLineSegment *seg;
1038 /* If we have a cached style, then we know it does actually apply
1039 * and we can just see if it is invisible.
1041 if (layout->one_style_cache &&
1042 !layout->one_style_cache->invisible)
1044 /* Without the cache, we check if the first char is visible, if so
1045 * we are partially visible. Note that we have to check this since
1046 * we don't know the current invisible/noninvisible toggle state; this
1047 * function can use the whole btree to get it right.
1051 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
1054 if (!_gtk_text_btree_char_is_invisible (iter))
1059 seg = line->segments;
1063 if (seg->byte_count > 0)
1064 bytes += seg->byte_count;
1066 /* Note that these two tests can cause us to bail out
1067 * when we shouldn't, because a higher-priority tag
1068 * may override these settings. However the important
1069 * thing is to only invisible really-invisible lines, rather
1070 * than to invisible all really-invisible lines.
1073 else if (seg->type == >k_text_toggle_on_type)
1075 invalidate_cached_style (layout);
1077 /* Bail out if an elision-unsetting tag begins */
1078 if (seg->body.toggle.info->tag->invisible_set &&
1079 !seg->body.toggle.info->tag->values->invisible)
1082 else if (seg->type == >k_text_toggle_off_type)
1084 invalidate_cached_style (layout);
1086 /* Bail out if an elision-setting tag ends */
1087 if (seg->body.toggle.info->tag->invisible_set &&
1088 seg->body.toggle.info->tag->values->invisible)
1095 if (seg != NULL) /* didn't reach line end */
1102 set_para_values (GtkTextLayout *layout,
1103 GtkTextAttributes *style,
1104 GtkTextLineDisplay *display)
1106 PangoAlignment pango_align = PANGO_ALIGN_LEFT;
1109 display->direction = style->direction;
1111 if (display->direction == GTK_TEXT_DIR_LTR)
1112 display->layout = pango_layout_new (layout->ltr_context);
1114 display->layout = pango_layout_new (layout->rtl_context);
1116 switch (style->justification)
1118 case GTK_JUSTIFY_LEFT:
1119 pango_align = (style->direction == GTK_TEXT_DIR_LTR) ? PANGO_ALIGN_LEFT : PANGO_ALIGN_RIGHT;
1121 case GTK_JUSTIFY_RIGHT:
1122 pango_align = (style->direction == GTK_TEXT_DIR_LTR) ? PANGO_ALIGN_RIGHT : PANGO_ALIGN_LEFT;
1124 case GTK_JUSTIFY_CENTER:
1125 pango_align = PANGO_ALIGN_CENTER;
1127 case GTK_JUSTIFY_FILL:
1128 g_warning ("FIXME we don't support GTK_JUSTIFY_FILL yet");
1131 g_assert_not_reached ();
1135 pango_layout_set_alignment (display->layout, pango_align);
1136 pango_layout_set_spacing (display->layout,
1137 style->pixels_inside_wrap * PANGO_SCALE);
1140 pango_layout_set_tabs (display->layout, style->tabs);
1142 display->top_margin = style->pixels_above_lines;
1143 display->height = style->pixels_above_lines + style->pixels_below_lines;
1144 display->bottom_margin = style->pixels_below_lines;
1145 display->left_margin = style->left_margin;
1146 display->right_margin = style->right_margin;
1148 display->x_offset = display->left_margin;
1150 pango_layout_set_indent (display->layout,
1151 style->indent * PANGO_SCALE);
1153 switch (style->wrap_mode)
1156 layout_width = layout->screen_width - display->left_margin - display->right_margin;
1157 pango_layout_set_width (display->layout, layout_width * PANGO_SCALE);
1158 pango_layout_set_wrap (display->layout, PANGO_WRAP_CHAR);
1162 layout_width = layout->screen_width - display->left_margin - display->right_margin;
1163 pango_layout_set_width (display->layout, layout_width * PANGO_SCALE);
1164 pango_layout_set_wrap (display->layout, PANGO_WRAP_WORD);
1171 display->total_width = MAX (layout->screen_width, layout->width) - display->left_margin - display->right_margin;
1174 static PangoAttribute *
1175 gtk_text_attr_appearance_copy (const PangoAttribute *attr)
1177 const GtkTextAttrAppearance *appearance_attr = (const GtkTextAttrAppearance *)attr;
1179 return gtk_text_attr_appearance_new (&appearance_attr->appearance);
1183 gtk_text_attr_appearance_destroy (PangoAttribute *attr)
1185 GtkTextAppearance *appearance = &((GtkTextAttrAppearance *)attr)->appearance;
1187 if (appearance->bg_stipple)
1188 g_object_unref (appearance->bg_stipple);
1189 if (appearance->fg_stipple)
1190 g_object_unref (appearance->fg_stipple);
1196 gtk_text_attr_appearance_compare (const PangoAttribute *attr1,
1197 const PangoAttribute *attr2)
1199 const GtkTextAppearance *appearance1 = &((const GtkTextAttrAppearance *)attr1)->appearance;
1200 const GtkTextAppearance *appearance2 = &((const GtkTextAttrAppearance *)attr2)->appearance;
1202 return (gdk_color_equal (&appearance1->fg_color, &appearance2->fg_color) &&
1203 gdk_color_equal (&appearance1->bg_color, &appearance2->bg_color) &&
1204 appearance1->fg_stipple == appearance2->fg_stipple &&
1205 appearance1->bg_stipple == appearance2->bg_stipple &&
1206 appearance1->underline == appearance2->underline &&
1207 appearance1->strikethrough == appearance2->strikethrough &&
1208 appearance1->draw_bg == appearance2->draw_bg);
1213 * gtk_text_attr_appearance_new:
1216 * Create a new font description attribute. (This attribute
1217 * allows setting family, style, weight, variant, stretch,
1218 * and size simultaneously.)
1222 static PangoAttribute *
1223 gtk_text_attr_appearance_new (const GtkTextAppearance *appearance)
1225 static PangoAttrClass klass = {
1227 gtk_text_attr_appearance_copy,
1228 gtk_text_attr_appearance_destroy,
1229 gtk_text_attr_appearance_compare
1232 GtkTextAttrAppearance *result;
1235 klass.type = gtk_text_attr_appearance_type =
1236 pango_attr_type_register ("GtkTextAttrAppearance");
1238 result = g_new (GtkTextAttrAppearance, 1);
1239 result->attr.klass = &klass;
1241 result->appearance = *appearance;
1243 if (appearance->bg_stipple)
1244 g_object_ref (appearance->bg_stipple);
1245 if (appearance->fg_stipple)
1246 g_object_ref (appearance->fg_stipple);
1248 return (PangoAttribute *)result;
1253 add_generic_attrs (GtkTextLayout *layout,
1254 GtkTextAppearance *appearance,
1256 PangoAttrList *attrs,
1261 PangoAttribute *attr;
1263 if (appearance->underline != PANGO_UNDERLINE_NONE)
1265 attr = pango_attr_underline_new (appearance->underline);
1267 attr->start_index = start;
1268 attr->end_index = start + byte_count;
1270 pango_attr_list_insert (attrs, attr);
1273 if (appearance->rise != 0)
1275 attr = pango_attr_rise_new (appearance->rise);
1277 attr->start_index = start;
1278 attr->end_index = start + byte_count;
1280 pango_attr_list_insert (attrs, attr);
1285 attr = gtk_text_attr_appearance_new (appearance);
1287 attr->start_index = start;
1288 attr->end_index = start + byte_count;
1290 ((GtkTextAttrAppearance *)attr)->appearance.is_text = is_text;
1292 pango_attr_list_insert (attrs, attr);
1297 add_text_attrs (GtkTextLayout *layout,
1298 GtkTextAttributes *style,
1300 PangoAttrList *attrs,
1304 PangoAttribute *attr;
1306 attr = pango_attr_font_desc_new (style->font);
1307 attr->start_index = start;
1308 attr->end_index = start + byte_count;
1310 pango_attr_list_insert (attrs, attr);
1312 if (style->font_scale != 1.0)
1314 attr = pango_attr_scale_new (style->font_scale);
1316 attr->start_index = start;
1317 attr->end_index = start + byte_count;
1319 pango_attr_list_insert (attrs, attr);
1324 add_pixbuf_attrs (GtkTextLayout *layout,
1325 GtkTextLineDisplay *display,
1326 GtkTextAttributes *style,
1327 GtkTextLineSegment *seg,
1328 PangoAttrList *attrs,
1331 PangoAttribute *attr;
1332 PangoRectangle logical_rect;
1333 GtkTextPixbuf *pixbuf = &seg->body.pixbuf;
1336 width = gdk_pixbuf_get_width (pixbuf->pixbuf);
1337 height = gdk_pixbuf_get_height (pixbuf->pixbuf);
1340 logical_rect.y = -height * PANGO_SCALE;
1341 logical_rect.width = width * PANGO_SCALE;
1342 logical_rect.height = height * PANGO_SCALE;
1344 attr = pango_attr_shape_new (&logical_rect, &logical_rect);
1345 attr->start_index = start;
1346 attr->end_index = start + seg->byte_count;
1347 pango_attr_list_insert (attrs, attr);
1349 display->shaped_objects =
1350 g_slist_append (display->shaped_objects, pixbuf->pixbuf);
1354 add_child_attrs (GtkTextLayout *layout,
1355 GtkTextLineDisplay *display,
1356 GtkTextAttributes *style,
1357 GtkTextLineSegment *seg,
1358 PangoAttrList *attrs,
1361 PangoAttribute *attr;
1362 PangoRectangle logical_rect;
1363 GtkTextChildAnchor *anchor;
1370 anchor = seg->body.child.obj;
1372 tmp_list = seg->body.child.widgets;
1373 while (tmp_list != NULL)
1375 GtkWidget *child = tmp_list->data;
1377 if (_gtk_anchored_child_get_layout (child) == layout)
1382 gtk_widget_get_child_requisition (child, &req);
1385 height = req.height;
1387 display->shaped_objects =
1388 g_slist_append (display->shaped_objects, child);
1393 tmp_list = g_slist_next (tmp_list);
1396 if (tmp_list == NULL)
1398 /* If tmp_list == NULL then there is no widget at this anchor in
1399 * this display; not an error. We make up an arbitrary size
1400 * to use, just so the programmer can see the blank spot.
1401 * We also put a NULL in the shaped objects list, to keep
1402 * the correspondence between the list and the shaped chars in
1403 * the layout. A bad hack, yes.
1409 display->shaped_objects =
1410 g_slist_append (display->shaped_objects, NULL);
1414 logical_rect.y = -height * PANGO_SCALE;
1415 logical_rect.width = width * PANGO_SCALE;
1416 logical_rect.height = height * PANGO_SCALE;
1418 attr = pango_attr_shape_new (&logical_rect, &logical_rect);
1419 attr->start_index = start;
1420 attr->end_index = start + seg->byte_count;
1421 pango_attr_list_insert (attrs, attr);
1425 add_cursor (GtkTextLayout *layout,
1426 GtkTextLineDisplay *display,
1427 GtkTextLineSegment *seg,
1430 PangoRectangle strong_pos, weak_pos;
1431 GtkTextCursorDisplay *cursor = NULL; /* Quiet GCC */
1432 gboolean add_weak = FALSE;
1433 gboolean add_strong = FALSE;
1435 /* Hide insertion cursor when we have a selection or the layout
1436 * user has hidden the cursor.
1438 if (_gtk_text_btree_mark_is_insert (_gtk_text_buffer_get_btree (layout->buffer),
1439 seg->body.mark.obj) &&
1440 (!layout->cursor_visible ||
1441 gtk_text_buffer_get_selection_bounds (layout->buffer, NULL, NULL)))
1444 pango_layout_get_cursor_pos (display->layout, start, &strong_pos, &weak_pos);
1446 if (layout->cursor_direction == GTK_TEXT_DIR_NONE)
1451 else if (display->direction == layout->cursor_direction)
1458 cursor = g_new (GtkTextCursorDisplay, 1);
1460 cursor->x = PANGO_PIXELS (strong_pos.x);
1461 cursor->y = PANGO_PIXELS (strong_pos.y);
1462 cursor->height = PANGO_PIXELS (strong_pos.height);
1463 cursor->is_strong = TRUE;
1464 cursor->is_weak = (layout->cursor_direction == GTK_TEXT_DIR_NONE) ? FALSE : TRUE;
1465 display->cursors = g_slist_prepend (display->cursors, cursor);
1470 if (weak_pos.x == strong_pos.x && add_strong)
1471 cursor->is_weak = TRUE;
1474 cursor = g_new (GtkTextCursorDisplay, 1);
1476 cursor->x = PANGO_PIXELS (weak_pos.x);
1477 cursor->y = PANGO_PIXELS (weak_pos.y);
1478 cursor->height = PANGO_PIXELS (weak_pos.height);
1479 cursor->is_strong = (layout->cursor_direction == GTK_TEXT_DIR_NONE) ? FALSE : TRUE;
1480 cursor->is_weak = TRUE;
1481 display->cursors = g_slist_prepend (display->cursors, cursor);
1487 is_shape (PangoLayoutRun *run)
1489 GSList *tmp_list = run->item->analysis.extra_attrs;
1493 PangoAttribute *attr = tmp_list->data;
1495 if (attr->klass->type == PANGO_ATTR_SHAPE)
1498 tmp_list = tmp_list->next;
1505 allocate_child_widgets (GtkTextLayout *text_layout,
1506 GtkTextLineDisplay *display)
1508 GSList *shaped = display->shaped_objects;
1509 PangoLayout *layout = display->layout;
1510 PangoLayoutIter *iter;
1512 iter = pango_layout_get_iter (layout);
1516 PangoLayoutRun *run = pango_layout_iter_get_run (iter);
1518 if (run && is_shape (run))
1520 GObject *shaped_object = shaped->data;
1521 shaped = shaped->next;
1523 /* shaped_object is NULL for child anchors with no
1524 * widgets stored at them
1526 if (shaped_object && GTK_IS_WIDGET (shaped_object))
1528 PangoRectangle extents;
1530 /* We emit "allocate_child" with the x,y of
1531 * the widget with respect to the top of the line
1532 * and the left side of the buffer
1535 pango_layout_iter_get_run_extents (iter,
1539 g_signal_emit (text_layout,
1540 signals[ALLOCATE_CHILD],
1543 PANGO_PIXELS (extents.x) + display->x_offset,
1544 PANGO_PIXELS (extents.y) + display->top_margin);
1548 while (pango_layout_iter_next_run (iter));
1550 pango_layout_iter_free (iter);
1554 convert_color (GdkColor *result,
1555 PangoAttrColor *attr)
1557 result->red = attr->color.red;
1558 result->blue = attr->color.blue;
1559 result->green = attr->color.green;
1562 /* This function is used to convert the preedit string attributes, which are
1563 * standard PangoAttributes, into the custom attributes used by the text
1564 * widget and insert them into a attr list with a given offset.
1567 add_preedit_attrs (GtkTextLayout *layout,
1568 GtkTextAttributes *style,
1569 PangoAttrList *attrs,
1573 PangoAttrIterator *iter = pango_attr_list_get_iterator (layout->preedit_attrs);
1577 GtkTextAppearance appearance = style->appearance;
1578 PangoFontDescription *font_desc = pango_font_description_copy_static (style->font);
1579 PangoAttribute *insert_attr;
1580 GSList *extra_attrs = NULL;
1582 PangoLanguage *language;
1585 pango_attr_iterator_range (iter, &start, &end);
1587 if (end == G_MAXINT)
1588 end = layout->preedit_len;
1590 pango_attr_iterator_get_font (iter, font_desc, &language, &extra_attrs);
1592 tmp_list = extra_attrs;
1595 PangoAttribute *attr = tmp_list->data;
1597 switch (attr->klass->type)
1599 case PANGO_ATTR_FOREGROUND:
1600 convert_color (&appearance.fg_color, (PangoAttrColor *)attr);
1602 case PANGO_ATTR_BACKGROUND:
1603 convert_color (&appearance.bg_color, (PangoAttrColor *)attr);
1604 appearance.draw_bg = TRUE;
1606 case PANGO_ATTR_UNDERLINE:
1607 appearance.underline = ((PangoAttrInt *)attr)->value;
1609 case PANGO_ATTR_STRIKETHROUGH:
1610 appearance.strikethrough = ((PangoAttrInt *)attr)->value;
1612 case PANGO_ATTR_RISE:
1613 appearance.rise = ((PangoAttrInt *)attr)->value;
1619 pango_attribute_destroy (attr);
1620 tmp_list = tmp_list->next;
1623 g_slist_free (extra_attrs);
1625 insert_attr = pango_attr_font_desc_new (font_desc);
1626 insert_attr->start_index = start + offset;
1627 insert_attr->end_index = end + offset;
1629 pango_attr_list_insert (attrs, insert_attr);
1633 insert_attr = pango_attr_language_new (language);
1634 insert_attr->start_index = start + offset;
1635 insert_attr->end_index = end + offset;
1637 pango_attr_list_insert (attrs, insert_attr);
1640 add_generic_attrs (layout, &appearance, end - start,
1641 attrs, start + offset,
1644 pango_font_description_free (font_desc);
1646 while (pango_attr_iterator_next (iter));
1648 pango_attr_iterator_destroy (iter);
1651 GtkTextLineDisplay *
1652 gtk_text_layout_get_line_display (GtkTextLayout *layout,
1656 GtkTextLineDisplay *display;
1657 GtkTextLineSegment *seg;
1659 GtkTextAttributes *style;
1661 PangoAttrList *attrs;
1662 gint text_allocated, layout_byte_offset, buffer_byte_offset;
1663 PangoRectangle extents;
1664 gboolean para_values_set = FALSE;
1665 GSList *cursor_byte_offsets = NULL;
1666 GSList *cursor_segs = NULL;
1667 GSList *tmp_list1, *tmp_list2;
1668 gboolean saw_widget = FALSE;
1670 g_return_val_if_fail (line != NULL, NULL);
1672 if (layout->one_display_cache)
1674 if (line == layout->one_display_cache->line &&
1675 (size_only || !layout->one_display_cache->size_only))
1676 return layout->one_display_cache;
1679 GtkTextLineDisplay *tmp_display = layout->one_display_cache;
1680 layout->one_display_cache = NULL;
1681 gtk_text_layout_free_line_display (layout, tmp_display);
1685 display = g_new0 (GtkTextLineDisplay, 1);
1687 display->size_only = size_only;
1688 display->line = line;
1689 display->insert_index = -1;
1691 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
1694 /* Special-case optimization for completely
1695 * invisible lines; makes it faster to deal
1696 * with sequences of invisible lines.
1698 if (totally_invisible_line (layout, line, &iter))
1701 /* Allocate space for flat text for buffer
1703 text_allocated = _gtk_text_line_byte_count (line);
1704 text = g_malloc (text_allocated);
1706 attrs = pango_attr_list_new ();
1708 /* Iterate over segments, creating display chunks for them. */
1709 layout_byte_offset = 0; /* current length of layout text (includes preedit, does not include invisible text) */
1710 buffer_byte_offset = 0; /* position in the buffer line */
1711 seg = _gtk_text_iter_get_any_segment (&iter);
1714 /* Displayable segments */
1715 if (seg->type == >k_text_char_type ||
1716 seg->type == >k_text_pixbuf_type ||
1717 seg->type == >k_text_child_type)
1719 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
1721 buffer_byte_offset);
1722 style = get_style (layout, &iter);
1724 /* We have to delay setting the paragraph values until we
1725 * hit the first pixbuf or text segment because toggles at
1726 * the beginning of the paragraph should affect the
1727 * paragraph-global values
1729 if (!para_values_set)
1731 set_para_values (layout, style, display);
1732 para_values_set = TRUE;
1735 /* First see if the chunk is invisible, and ignore it if so. Tk
1736 * looked at tabs, wrap mode, etc. before doing this, but
1737 * that made no sense to me, so I am just skipping the
1740 if (!style->invisible)
1742 if (seg->type == >k_text_char_type)
1744 /* We don't want to split segments because of marks,
1745 * so we scan forward for more segments only
1746 * separated from us by marks. In theory, we should
1747 * also merge segments with identical styles, even
1748 * if there are toggles in-between
1752 GtkTextLineSegment *prev_seg = NULL;
1756 if (seg->type == >k_text_char_type)
1758 memcpy (text + layout_byte_offset, seg->body.chars, seg->byte_count);
1759 layout_byte_offset += seg->byte_count;
1760 buffer_byte_offset += seg->byte_count;
1761 bytes += seg->byte_count;
1763 else if (seg->type == >k_text_right_mark_type ||
1764 seg->type == >k_text_left_mark_type)
1766 /* If we have preedit string, break out of this loop - we'll almost
1767 * certainly have different attributes on the preedit string
1770 if (layout->preedit_len > 0 &&
1771 _gtk_text_btree_mark_is_insert (_gtk_text_buffer_get_btree (layout->buffer),
1772 seg->body.mark.obj))
1775 if (seg->body.mark.visible)
1777 cursor_byte_offsets = g_slist_prepend (cursor_byte_offsets, GINT_TO_POINTER (layout_byte_offset));
1778 cursor_segs = g_slist_prepend (cursor_segs, seg);
1788 seg = prev_seg; /* Back up one */
1789 add_generic_attrs (layout, &style->appearance,
1791 attrs, layout_byte_offset - bytes,
1793 add_text_attrs (layout, style, bytes, attrs,
1794 layout_byte_offset - bytes, size_only);
1796 else if (seg->type == >k_text_pixbuf_type)
1798 add_generic_attrs (layout,
1801 attrs, layout_byte_offset,
1803 add_pixbuf_attrs (layout, display, style,
1804 seg, attrs, layout_byte_offset);
1805 memcpy (text + layout_byte_offset, gtk_text_unknown_char_utf8,
1807 layout_byte_offset += seg->byte_count;
1808 buffer_byte_offset += seg->byte_count;
1810 else if (seg->type == >k_text_child_type)
1814 add_generic_attrs (layout, &style->appearance,
1816 attrs, layout_byte_offset,
1818 add_child_attrs (layout, display, style,
1819 seg, attrs, layout_byte_offset);
1820 memcpy (text + layout_byte_offset, gtk_text_unknown_char_utf8,
1822 layout_byte_offset += seg->byte_count;
1823 buffer_byte_offset += seg->byte_count;
1827 /* We don't know this segment type */
1828 g_assert_not_reached ();
1831 } /* if (segment was visible) */
1834 /* Invisible segment */
1835 buffer_byte_offset += seg->byte_count;
1838 release_style (layout, style);
1842 else if (seg->type == >k_text_toggle_on_type ||
1843 seg->type == >k_text_toggle_off_type)
1845 /* Style may have changed, drop our
1846 current cached style */
1847 invalidate_cached_style (layout);
1851 else if (seg->type == >k_text_right_mark_type ||
1852 seg->type == >k_text_left_mark_type)
1854 gint cursor_offset = 0;
1856 /* At the insertion point, add the preedit string, if any */
1858 if (_gtk_text_btree_mark_is_insert (_gtk_text_buffer_get_btree (layout->buffer),
1859 seg->body.mark.obj))
1861 display->insert_index = layout_byte_offset;
1863 if (layout->preedit_len > 0)
1865 text_allocated += layout->preedit_len;
1866 text = g_realloc (text, text_allocated);
1868 style = get_style (layout, &iter);
1869 add_preedit_attrs (layout, style, attrs, layout_byte_offset, size_only);
1870 release_style (layout, style);
1872 memcpy (text + layout_byte_offset, layout->preedit_string, layout->preedit_len);
1873 layout_byte_offset += layout->preedit_len;
1874 /* DO NOT increment the buffer byte offset for preedit */
1876 cursor_offset = layout->preedit_cursor - layout->preedit_len;
1881 /* Display visible marks */
1883 if (seg->body.mark.visible)
1885 cursor_byte_offsets = g_slist_prepend (cursor_byte_offsets,
1886 GINT_TO_POINTER (layout_byte_offset + cursor_offset));
1887 cursor_segs = g_slist_prepend (cursor_segs, seg);
1892 g_error ("Unknown segment type: %s", seg->type->name);
1897 if (!para_values_set)
1899 style = get_style (layout, &iter);
1900 set_para_values (layout, style, display);
1901 release_style (layout, style);
1904 /* Pango doesn't want the trailing paragraph delimiters */
1907 /* Only one character has type G_UNICODE_PARAGRAPH_SEPARATOR in
1908 * Unicode 3.0; update this if that changes.
1910 #define PARAGRAPH_SEPARATOR 0x2029
1913 if (layout_byte_offset > 0)
1915 const char *prev = g_utf8_prev_char (text + layout_byte_offset);
1916 ch = g_utf8_get_char (prev);
1917 if (ch == PARAGRAPH_SEPARATOR || ch == '\r' || ch == '\n')
1918 layout_byte_offset = prev - text; /* chop off */
1920 if (ch == '\n' && layout_byte_offset > 0)
1922 /* Possibly chop a CR as well */
1923 prev = g_utf8_prev_char (text + layout_byte_offset);
1925 --layout_byte_offset;
1930 pango_layout_set_text (display->layout, text, layout_byte_offset);
1931 pango_layout_set_attributes (display->layout, attrs);
1933 tmp_list1 = cursor_byte_offsets;
1934 tmp_list2 = cursor_segs;
1937 add_cursor (layout, display, tmp_list2->data,
1938 GPOINTER_TO_INT (tmp_list1->data));
1939 tmp_list1 = tmp_list1->next;
1940 tmp_list2 = tmp_list2->next;
1942 g_slist_free (cursor_byte_offsets);
1943 g_slist_free (cursor_segs);
1945 pango_layout_get_extents (display->layout, NULL, &extents);
1947 display->width = PANGO_PIXELS (extents.width) + display->left_margin + display->right_margin;
1948 display->height += PANGO_PIXELS (extents.height);
1950 /* Free this if we aren't in a loop */
1951 if (layout->wrap_loop_count == 0)
1952 invalidate_cached_style (layout);
1955 pango_attr_list_unref (attrs);
1957 layout->one_display_cache = display;
1960 allocate_child_widgets (layout, display);
1966 gtk_text_layout_free_line_display (GtkTextLayout *layout,
1967 GtkTextLineDisplay *display)
1969 if (display != layout->one_display_cache)
1971 if (display->layout)
1972 g_object_unref (display->layout);
1974 if (display->cursors)
1976 g_slist_foreach (display->cursors, (GFunc)g_free, NULL);
1977 g_slist_free (display->cursors);
1979 g_slist_free (display->shaped_objects);
1985 /* Functions to convert iter <=> index for the line of a GtkTextLineDisplay
1986 * taking into account the preedit string and invisible text if necessary.
1989 line_display_iter_to_index (GtkTextLayout *layout,
1990 GtkTextLineDisplay *display,
1991 const GtkTextIter *iter)
1995 g_return_val_if_fail (_gtk_text_iter_get_text_line (iter) == display->line, 0);
1997 index = gtk_text_iter_get_visible_line_index (iter);
1999 if (index >= display->insert_index)
2000 index += layout->preedit_len;
2006 line_display_index_to_iter (GtkTextLayout *layout,
2007 GtkTextLineDisplay *display,
2012 g_return_if_fail (!_gtk_text_line_is_last (display->line,
2013 _gtk_text_buffer_get_btree (layout->buffer)));
2015 if (index >= display->insert_index + layout->preedit_len)
2016 index -= layout->preedit_len;
2017 else if (index > display->insert_index)
2019 index = display->insert_index;
2023 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
2024 iter, display->line, 0);
2026 gtk_text_iter_set_visible_line_index (iter, index);
2028 if (_gtk_text_iter_get_text_line (iter) != display->line)
2030 /* Clamp to end of line - really this clamping should have been done
2031 * before here, maybe in Pango, this is a broken band-aid I think
2033 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
2034 iter, display->line, 0);
2036 if (!gtk_text_iter_ends_line (iter))
2037 gtk_text_iter_forward_to_line_end (iter);
2040 gtk_text_iter_forward_chars (iter, trailing);
2044 get_line_at_y (GtkTextLayout *layout,
2051 if (y > layout->height)
2054 *line = _gtk_text_btree_find_line_by_y (_gtk_text_buffer_get_btree (layout->buffer),
2055 layout, y, line_top);
2058 *line = _gtk_text_btree_get_end_iter_line (_gtk_text_buffer_get_btree (layout->buffer));
2062 _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
2068 * gtk_text_layout_get_line_at_y:
2069 * @layout: a #GtkLayout
2070 * @target_iter: the iterator in which the result is stored
2071 * @y: the y positition
2072 * @line_top: location to store the y coordinate of the
2073 * top of the line. (Can by %NULL.)
2075 * Get the iter at the beginning of the line which is displayed
2079 gtk_text_layout_get_line_at_y (GtkTextLayout *layout,
2080 GtkTextIter *target_iter,
2086 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
2087 g_return_if_fail (target_iter != NULL);
2089 get_line_at_y (layout, y, &line, line_top);
2090 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
2091 target_iter, line, 0);
2095 gtk_text_layout_get_iter_at_pixel (GtkTextLayout *layout,
2096 GtkTextIter *target_iter,
2100 gint byte_index, trailing;
2102 GtkTextLineDisplay *display;
2104 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
2105 g_return_if_fail (target_iter != NULL);
2107 get_line_at_y (layout, y, &line, &line_top);
2109 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2111 x -= display->x_offset;
2112 y -= line_top + display->top_margin;
2114 /* If we are below the layout, position the cursor at the last character
2117 if (y > display->height - display->top_margin - display->bottom_margin)
2119 byte_index = _gtk_text_line_byte_count (line);
2124 /* Ignore the "outside" return value from pango. Pango is doing
2125 * the right thing even if we are outside the layout in the
2128 pango_layout_xy_to_index (display->layout, x * PANGO_SCALE, y * PANGO_SCALE,
2129 &byte_index, &trailing);
2132 line_display_index_to_iter (layout, display, target_iter, byte_index, trailing);
2134 gtk_text_layout_free_line_display (layout, display);
2138 * gtk_text_layout_get_cursor_locations
2139 * @layout: a #GtkTextLayout
2140 * @iter: a #GtkTextIter
2141 * @strong_pos: location to store the strong cursor position (may be %NULL)
2142 * @weak_pos: location to store the weak cursor position (may be %NULL)
2144 * Given an iterator within a text laout, determine the positions that of the
2145 * strong and weak cursors if the insertion point is at that
2146 * iterator. The position of each cursor is stored as a zero-width
2147 * rectangle. The strong cursor location is the location where
2148 * characters of the directionality equal to the base direction of the
2149 * paragraph are inserted. The weak cursor location is the location
2150 * where characters of the directionality opposite to the base
2151 * direction of the paragraph are inserted.
2154 gtk_text_layout_get_cursor_locations (GtkTextLayout *layout,
2156 GdkRectangle *strong_pos,
2157 GdkRectangle *weak_pos)
2160 GtkTextLineDisplay *display;
2164 PangoRectangle pango_strong_pos;
2165 PangoRectangle pango_weak_pos;
2167 g_return_if_fail (layout != NULL);
2168 g_return_if_fail (iter != NULL);
2170 line = _gtk_text_iter_get_text_line (iter);
2171 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2172 index = line_display_iter_to_index (layout, display, iter);
2174 line_top = _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
2177 pango_layout_get_cursor_pos (display->layout, index,
2178 strong_pos ? &pango_strong_pos : NULL,
2179 weak_pos ? &pango_weak_pos : NULL);
2183 strong_pos->x = display->x_offset + pango_strong_pos.x / PANGO_SCALE;
2184 strong_pos->y = line_top + display->top_margin + pango_strong_pos.y / PANGO_SCALE;
2185 strong_pos->width = 0;
2186 strong_pos->height = pango_strong_pos.height / PANGO_SCALE;
2191 weak_pos->x = display->x_offset + pango_weak_pos.x / PANGO_SCALE;
2192 weak_pos->y = line_top + display->top_margin + pango_weak_pos.y / PANGO_SCALE;
2193 weak_pos->width = 0;
2194 weak_pos->height = pango_weak_pos.height / PANGO_SCALE;
2197 gtk_text_layout_free_line_display (layout, display);
2201 * gtk_text_layout_get_line_yrange:
2202 * @layout: a #GtkTextLayout
2203 * @iter: a #GtkTextIter
2204 * @y: location to store the top of the paragraph in pixels,
2206 * @height location to store the height of the paragraph in pixels,
2209 * Find the range of y coordinates for the paragraph containing
2213 gtk_text_layout_get_line_yrange (GtkTextLayout *layout,
2214 const GtkTextIter *iter,
2220 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
2221 g_return_if_fail (_gtk_text_iter_get_btree (iter) == _gtk_text_buffer_get_btree (layout->buffer));
2223 line = _gtk_text_iter_get_text_line (iter);
2226 *y = _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
2230 GtkTextLineData *line_data = _gtk_text_line_get_data (line, layout);
2232 *height = line_data->height;
2239 * _gtk_text_layout_get_line_xrange:
2240 * @layout: a #GtkTextLayout
2241 * @iter: a #GtkTextIter
2242 * @x: location to store the top of the paragraph in pixels,
2244 * @width location to store the height of the paragraph in pixels,
2247 * Find the range of X coordinates for the paragraph containing
2248 * the given iter. Private for 2.0 due to API freeze, could
2249 * be made public for 2.2.
2252 _gtk_text_layout_get_line_xrange (GtkTextLayout *layout,
2253 const GtkTextIter *iter,
2259 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
2260 g_return_if_fail (_gtk_text_iter_get_btree (iter) == _gtk_text_buffer_get_btree (layout->buffer));
2262 line = _gtk_text_iter_get_text_line (iter);
2265 *x = 0; /* FIXME This is wrong; should represent the first available cursor position */
2269 GtkTextLineData *line_data = _gtk_text_line_get_data (line, layout);
2271 *width = line_data->width;
2278 gtk_text_layout_get_iter_location (GtkTextLayout *layout,
2279 const GtkTextIter *iter,
2282 PangoRectangle pango_rect;
2285 GtkTextLineDisplay *display;
2289 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
2290 g_return_if_fail (_gtk_text_iter_get_btree (iter) == _gtk_text_buffer_get_btree (layout->buffer));
2291 g_return_if_fail (rect != NULL);
2293 tree = _gtk_text_iter_get_btree (iter);
2294 line = _gtk_text_iter_get_text_line (iter);
2296 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2298 rect->y = _gtk_text_btree_find_line_top (tree, line, layout);
2300 x_offset = display->x_offset * PANGO_SCALE;
2302 byte_index = gtk_text_iter_get_line_index (iter);
2304 pango_layout_index_to_pos (display->layout, byte_index, &pango_rect);
2306 rect->x = PANGO_PIXELS (x_offset + pango_rect.x);
2307 rect->y += PANGO_PIXELS (pango_rect.y) + display->top_margin;
2308 rect->width = PANGO_PIXELS (pango_rect.width);
2309 rect->height = PANGO_PIXELS (pango_rect.height);
2311 gtk_text_layout_free_line_display (layout, display);
2316 /* Find the iter for the logical beginning of the first display line whose
2317 * top y is >= y. If none exists, move the iter to the logical beginning
2318 * of the last line in the buffer.
2321 find_display_line_below (GtkTextLayout *layout,
2325 GtkTextLine *line, *next;
2326 GtkTextLine *found_line = NULL;
2328 gint found_byte = 0;
2330 line = _gtk_text_btree_find_line_by_y (_gtk_text_buffer_get_btree (layout->buffer),
2331 layout, y, &line_top);
2335 _gtk_text_btree_get_end_iter_line (_gtk_text_buffer_get_btree (layout->buffer));
2338 _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
2342 while (line && !found_line)
2344 GtkTextLineDisplay *display = gtk_text_layout_get_line_display (layout, line, FALSE);
2345 PangoLayoutIter *layout_iter;
2347 layout_iter = pango_layout_get_iter (display->layout);
2349 line_top += display->top_margin;
2353 gint first_y, last_y;
2354 PangoLayoutLine *layout_line = pango_layout_iter_get_line (layout_iter);
2356 found_byte = layout_line->start_index;
2364 pango_layout_iter_get_line_yrange (layout_iter, &first_y, &last_y);
2365 line_top += (last_y - first_y) / PANGO_SCALE;
2367 while (pango_layout_iter_next_line (layout_iter));
2369 pango_layout_iter_free (layout_iter);
2371 line_top += display->bottom_margin;
2372 gtk_text_layout_free_line_display (layout, display);
2374 next = _gtk_text_line_next_excluding_last (line);
2381 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
2382 iter, found_line, found_byte);
2385 /* Find the iter for the logical beginning of the last display line whose
2386 * top y is >= y. If none exists, move the iter to the logical beginning
2387 * of the first line in the buffer.
2390 find_display_line_above (GtkTextLayout *layout,
2395 GtkTextLine *found_line = NULL;
2397 gint found_byte = 0;
2399 line = _gtk_text_btree_find_line_by_y (_gtk_text_buffer_get_btree (layout->buffer), layout, y, &line_top);
2402 line = _gtk_text_btree_get_end_iter_line (_gtk_text_buffer_get_btree (layout->buffer));
2404 line_top = _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer), line, layout);
2407 while (line && !found_line)
2409 GtkTextLineDisplay *display = gtk_text_layout_get_line_display (layout, line, FALSE);
2410 PangoRectangle logical_rect;
2411 PangoLayoutIter *layout_iter;
2414 layout_iter = pango_layout_get_iter (display->layout);
2416 line_top -= display->top_margin + display->bottom_margin;
2417 pango_layout_iter_get_layout_extents (layout_iter, NULL, &logical_rect);
2418 line_top -= logical_rect.height / PANGO_SCALE;
2420 tmp_top = line_top + display->top_margin;
2424 gint first_y, last_y;
2425 PangoLayoutLine *layout_line = pango_layout_iter_get_line (layout_iter);
2427 found_byte = layout_line->start_index;
2429 pango_layout_iter_get_line_yrange (layout_iter, &first_y, &last_y);
2431 tmp_top -= (last_y - first_y) / PANGO_SCALE;
2436 pango_layout_iter_free (layout_iter);
2440 while (pango_layout_iter_next_line (layout_iter));
2442 pango_layout_iter_free (layout_iter);
2444 gtk_text_layout_free_line_display (layout, display);
2446 line = _gtk_text_line_previous (line);
2452 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
2453 iter, found_line, found_byte);
2455 gtk_text_buffer_get_iter_at_offset (layout->buffer, iter, 0);
2459 * gtk_text_layout_clamp_iter_to_vrange:
2460 * @layout: a #GtkTextLayout
2461 * @iter: a #GtkTextIter
2462 * @top: the top of the range
2463 * @bottom: the bottom the range
2465 * If the iterator is not fully in the range @top <= y < @bottom,
2466 * then, if possible, move it the minimum distance so that the
2467 * iterator in this range.
2469 * Returns: %TRUE if the iterator was moved, otherwise %FALSE.
2472 gtk_text_layout_clamp_iter_to_vrange (GtkTextLayout *layout,
2477 GdkRectangle iter_rect;
2479 gtk_text_layout_get_iter_location (layout, iter, &iter_rect);
2481 /* If the iter is at least partially above the range, put the iter
2482 * at the first fully visible line after the range.
2484 if (iter_rect.y < top)
2486 find_display_line_below (layout, iter, top);
2490 /* Otherwise, if the iter is at least partially below the screen, put the
2491 * iter on the last logical position of the last completely visible
2494 else if (iter_rect.y + iter_rect.height > bottom)
2496 find_display_line_above (layout, iter, bottom);
2505 * gtk_text_layout_move_iter_to_previous_line:
2506 * @layout: a #GtkLayout
2507 * @iter: a #GtkTextIter
2509 * Move the iterator to the beginning of the previous line. The lines
2510 * of a wrapped paragraph are treated as distinct for this operation.
2513 gtk_text_layout_move_iter_to_previous_line (GtkTextLayout *layout,
2517 GtkTextLineDisplay *display;
2520 PangoLayoutLine *layout_line;
2522 gboolean update_byte = FALSE;
2524 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), FALSE);
2525 g_return_val_if_fail (iter != NULL, FALSE);
2530 line = _gtk_text_iter_get_text_line (iter);
2531 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2532 line_byte = line_display_iter_to_index (layout, display, iter);
2534 /* If display->height == 0 then the line is invisible, so don't
2537 while (display->height == 0)
2539 GtkTextLine *prev_line;
2541 prev_line = _gtk_text_line_previous (line);
2543 if (prev_line == NULL)
2545 line_display_index_to_iter (layout, display, iter, 0, 0);
2549 gtk_text_layout_free_line_display (layout, display);
2552 display = gtk_text_layout_get_line_display (layout, prev_line, FALSE);
2556 tmp_list = pango_layout_get_lines (display->layout);
2557 layout_line = tmp_list->data;
2561 line_byte = layout_line->start_index + layout_line->length;
2564 if (line_byte < layout_line->length || !tmp_list->next) /* first line of paragraph */
2566 GtkTextLine *prev_line;
2568 prev_line = _gtk_text_line_previous (line);
2571 gtk_text_layout_free_line_display (layout, display);
2573 display = gtk_text_layout_get_line_display (layout, prev_line, FALSE);
2575 if (display->height > 0)
2577 tmp_list = g_slist_last (pango_layout_get_lines (display->layout));
2578 layout_line = tmp_list->data;
2580 line_display_index_to_iter (layout, display, iter,
2581 layout_line->start_index + layout_line->length, 0);
2585 prev_line = _gtk_text_line_previous (prev_line);
2588 if (prev_line == NULL)
2589 line_display_index_to_iter (layout, display, iter, 0, 0);
2593 gint prev_offset = layout_line->start_index;
2595 tmp_list = tmp_list->next;
2598 layout_line = tmp_list->data;
2600 if (line_byte < layout_line->start_index + layout_line->length ||
2603 line_display_index_to_iter (layout, display, iter, prev_offset, 0);
2607 prev_offset = layout_line->start_index;
2608 tmp_list = tmp_list->next;
2614 gtk_text_layout_free_line_display (layout, display);
2617 !gtk_text_iter_equal (iter, &orig) &&
2618 !gtk_text_iter_is_end (iter);
2622 * gtk_text_layout_move_iter_to_next_line:
2623 * @layout: a #GtkLayout
2624 * @iter: a #GtkTextIter
2626 * Move the iterator to the beginning of the next line. The
2627 * lines of a wrapped paragraph are treated as distinct for
2631 gtk_text_layout_move_iter_to_next_line (GtkTextLayout *layout,
2635 GtkTextLineDisplay *display;
2638 gboolean found = FALSE;
2639 gboolean found_after = FALSE;
2640 gboolean first = TRUE;
2642 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), FALSE);
2643 g_return_val_if_fail (iter != NULL, FALSE);
2647 line = _gtk_text_iter_get_text_line (iter);
2649 while (line && !found_after)
2653 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2655 if (display->height == 0)
2660 line_byte = line_display_iter_to_index (layout, display, iter);
2666 tmp_list = pango_layout_get_lines (display->layout);
2667 while (tmp_list && !found_after)
2669 PangoLayoutLine *layout_line = tmp_list->data;
2673 line_display_index_to_iter (layout, display, iter,
2674 layout_line->start_index, 0);
2677 else if (line_byte < layout_line->start_index + layout_line->length || !tmp_list->next)
2680 tmp_list = tmp_list->next;
2685 gtk_text_layout_free_line_display (layout, display);
2687 line = _gtk_text_line_next_excluding_last (line);
2691 !gtk_text_iter_equal (iter, &orig) &&
2692 !gtk_text_iter_is_end (iter);
2696 * gtk_text_layout_move_iter_to_line_end:
2697 * @layout: a #GtkTextLayout
2698 * @direction: if negative, move to beginning of line, otherwise
2699 move to end of line.
2701 * Move to the beginning or end of a display line.
2704 gtk_text_layout_move_iter_to_line_end (GtkTextLayout *layout,
2709 GtkTextLineDisplay *display;
2714 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), FALSE);
2715 g_return_val_if_fail (iter != NULL, FALSE);
2719 line = _gtk_text_iter_get_text_line (iter);
2720 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2721 line_byte = line_display_iter_to_index (layout, display, iter);
2723 tmp_list = pango_layout_get_lines (display->layout);
2726 PangoLayoutLine *layout_line = tmp_list->data;
2728 if (line_byte < layout_line->start_index + layout_line->length || !tmp_list->next)
2730 line_display_index_to_iter (layout, display, iter,
2731 direction < 0 ? layout_line->start_index : layout_line->start_index + layout_line->length,
2734 /* FIXME: As a bad hack, we move back one position when we
2735 * are inside a paragraph to avoid going to next line on a
2736 * forced break not at whitespace. Real fix is to keep track
2737 * of whether marks are at leading or trailing edge? */
2738 if (direction > 0 && layout_line->length > 0 && !gtk_text_iter_ends_line (iter))
2739 gtk_text_iter_backward_char (iter);
2744 tmp_list = tmp_list->next;
2747 gtk_text_layout_free_line_display (layout, display);
2750 !gtk_text_iter_equal (iter, &orig) &&
2751 !gtk_text_iter_is_end (iter);
2756 * gtk_text_layout_iter_starts_line:
2757 * @layout: a #GtkTextLayout
2758 * @iter: iterator to test
2760 * Tests whether an iterator is at the start of a display line.
2763 gtk_text_layout_iter_starts_line (GtkTextLayout *layout,
2764 const GtkTextIter *iter)
2767 GtkTextLineDisplay *display;
2771 g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), FALSE);
2772 g_return_val_if_fail (iter != NULL, FALSE);
2774 line = _gtk_text_iter_get_text_line (iter);
2775 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2776 line_byte = line_display_iter_to_index (layout, display, iter);
2778 tmp_list = pango_layout_get_lines (display->layout);
2781 PangoLayoutLine *layout_line = tmp_list->data;
2783 if (line_byte < layout_line->start_index + layout_line->length ||
2786 /* We're located on this line or the para delimiters before
2789 gtk_text_layout_free_line_display (layout, display);
2791 if (line_byte == layout_line->start_index)
2797 tmp_list = tmp_list->next;
2800 g_assert_not_reached ();
2805 gtk_text_layout_get_iter_at_line (GtkTextLayout *layout,
2810 _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
2811 iter, line, byte_offset);
2816 * gtk_text_layout_move_iter_to_x:
2817 * @layout: a #GtkTextLayout
2818 * @iter: a #GtkTextIter
2821 * Keeping the iterator on the same line of the layout, move it to the
2822 * specified X coordinate. The lines of a wrapped paragraph are
2823 * treated as distinct for this operation.
2826 gtk_text_layout_move_iter_to_x (GtkTextLayout *layout,
2831 GtkTextLineDisplay *display;
2833 PangoLayoutIter *layout_iter;
2835 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
2836 g_return_if_fail (iter != NULL);
2838 line = _gtk_text_iter_get_text_line (iter);
2840 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2841 line_byte = line_display_iter_to_index (layout, display, iter);
2843 layout_iter = pango_layout_get_iter (display->layout);
2847 PangoLayoutLine *layout_line = pango_layout_iter_get_line (layout_iter);
2849 if (line_byte < layout_line->start_index + layout_line->length ||
2850 pango_layout_iter_at_last_line (layout_iter))
2852 PangoRectangle logical_rect;
2853 gint byte_index, trailing;
2854 gint x_offset = display->x_offset * PANGO_SCALE;
2856 pango_layout_iter_get_line_extents (layout_iter, NULL, &logical_rect);
2858 pango_layout_line_x_to_index (layout_line,
2859 x * PANGO_SCALE - x_offset - logical_rect.x,
2860 &byte_index, &trailing);
2862 line_display_index_to_iter (layout, display, iter, byte_index, trailing);
2867 while (pango_layout_iter_next_line (layout_iter));
2869 pango_layout_iter_free (layout_iter);
2871 gtk_text_layout_free_line_display (layout, display);
2875 * gtk_text_layout_move_iter_visually:
2876 * @layout: a #GtkTextLayout
2877 * @iter: a #GtkTextIter
2878 * @count: number of characters to move (negative moves left, positive moves right)
2880 * Move the iterator a given number of characters visually, treating
2881 * it as the strong cursor position. If @count is positive, then the
2882 * new strong cursor position will be @count positions to the right of
2883 * the old cursor position. If @count is negative then the new strong
2884 * cursor position will be @count positions to the left of the old
2887 * In the presence of bidirection text, the correspondence
2888 * between logical and visual order will depend on the direction
2889 * of the current run, and there may be jumps when the cursor
2890 * is moved off of the end of a run.
2894 gtk_text_layout_move_iter_visually (GtkTextLayout *layout,
2898 GtkTextLineDisplay *display = NULL;
2901 g_return_val_if_fail (layout != NULL, FALSE);
2902 g_return_val_if_fail (iter != NULL, FALSE);
2908 GtkTextLine *line = _gtk_text_iter_get_text_line (iter);
2910 gint extra_back = 0;
2913 int byte_count = _gtk_text_line_byte_count (line);
2919 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2921 if (layout->cursor_direction == GTK_TEXT_DIR_NONE)
2924 strong = display->direction == layout->cursor_direction;
2926 line_byte = line_display_iter_to_index (layout, display, iter);
2930 pango_layout_move_cursor_visually (display->layout, strong, line_byte, 0, 1, &new_index, &new_trailing);
2935 pango_layout_move_cursor_visually (display->layout, strong, line_byte, 0, -1, &new_index, &new_trailing);
2939 /* We need to handle the preedit string specially. Well, we don't really need to
2940 * handle it specially, since hopefully calling gtk_im_context_reset() will
2941 * remove the preedit string; but if we start off in front of the preedit
2942 * string (logically) and end up in or on the back edge of the preedit string,
2943 * we should move the iter one place farther.
2945 if (layout->preedit_len > 0 && display->insert_index >= 0)
2947 if (line_byte == display->insert_index + layout->preedit_len &&
2948 new_index < display->insert_index + layout->preedit_len)
2950 line_byte = display->insert_index;
2955 if (new_index < 0 || (new_index == 0 && extra_back))
2957 line = _gtk_text_line_previous (line);
2962 gtk_text_layout_free_line_display (layout, display);
2963 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2964 new_index = _gtk_text_line_byte_count (line);
2966 else if (new_index > byte_count)
2968 line = _gtk_text_line_next_excluding_last (line);
2972 gtk_text_layout_free_line_display (layout, display);
2973 display = gtk_text_layout_get_line_display (layout, line, FALSE);
2977 line_display_index_to_iter (layout, display, iter, new_index, new_trailing);
2979 gtk_text_iter_backward_char (iter);
2982 gtk_text_layout_free_line_display (layout, display);
2987 !gtk_text_iter_equal (iter, &orig) &&
2988 !gtk_text_iter_is_end (iter);
2992 gtk_text_layout_spew (GtkTextLayout *layout)
2995 GtkTextDisplayLine *iter;
2997 guint paragraphs = 0;
2998 GtkTextLine *last_line = NULL;
3000 iter = layout->line_list;
3001 while (iter != NULL)
3003 if (iter->line != last_line)
3005 printf ("%5u paragraph (%p)\n", paragraphs, iter->line);
3007 last_line = iter->line;
3010 printf (" %5u y: %d len: %d start: %d bytes: %d\n",
3011 wrapped, iter->y, iter->length, iter->byte_offset,
3018 printf ("Layout %s recompute\n",
3019 layout->need_recompute ? "needs" : "doesn't need");
3021 printf ("Layout pars: %u lines: %u size: %d x %d Screen width: %d\n",
3022 paragraphs, wrapped, layout->width,
3023 layout->height, layout->screen_width);