]> Pileus Git - ~andy/gtk/blob - gtk/gtktextlayout.c
Fix #56586
[~andy/gtk] / gtk / gtktextlayout.c
1 /* GTK - The GIMP Toolkit
2  * gtktextlayout.c - calculate the layout of the text
3  *
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
9  *
10  * This file can be used under your choice of two licenses, the LGPL
11  * and the original Tk license.
12  *
13  * LGPL:
14  *
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.
19  *
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.
24  *
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.
28  *
29  * Original Tk license:
30  *
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.
35  *
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.
45  *
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.
51  *
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.
58  *
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.
70  *
71  */
72 /*
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/.
77  */
78
79 #include "gtksignal.h"
80 #include "gtktextlayout.h"
81 #include "gtktextbtree.h"
82 #include "gtktextiterprivate.h"
83
84 #include <stdlib.h>
85 #include <string.h>
86
87 static GtkTextLineData    *gtk_text_line_data_new                 (GtkTextLayout     *layout,
88                                                                    GtkTextLine       *line);
89
90 static GtkTextLineData *gtk_text_layout_real_wrap (GtkTextLayout *layout,
91                                                    GtkTextLine *line,
92                                                    /* may be NULL */
93                                                    GtkTextLineData *line_data);
94
95 static void gtk_text_layout_invalidated     (GtkTextLayout     *layout);
96
97 static void gtk_text_layout_real_invalidate        (GtkTextLayout     *layout,
98                                                     const GtkTextIter *start,
99                                                     const GtkTextIter *end);
100 static void gtk_text_layout_invalidate_cache       (GtkTextLayout     *layout,
101                                                     GtkTextLine       *line);
102 static void gtk_text_layout_invalidate_cursor_line (GtkTextLayout     *layout);
103 static void gtk_text_layout_real_free_line_data    (GtkTextLayout     *layout,
104                                                     GtkTextLine       *line,
105                                                     GtkTextLineData   *line_data);
106
107 static void gtk_text_layout_invalidate_all (GtkTextLayout *layout);
108
109 static PangoAttribute *gtk_text_attr_appearance_new (const GtkTextAppearance *appearance);
110
111 enum {
112   INVALIDATED,
113   CHANGED,
114   ALLOCATE_CHILD,
115   LAST_SIGNAL
116 };
117
118 enum {
119   ARG_0,
120   LAST_ARG
121 };
122
123 static void gtk_text_layout_init       (GtkTextLayout      *text_layout);
124 static void gtk_text_layout_class_init (GtkTextLayoutClass *klass);
125 static void gtk_text_layout_finalize   (GObject            *object);
126
127
128 static GtkObjectClass *parent_class = NULL;
129 static guint signals[LAST_SIGNAL] = { 0 };
130
131 PangoAttrType gtk_text_attr_appearance_type = 0;
132
133 GType
134 gtk_text_layout_get_type (void)
135 {
136   static GType our_type = 0;
137
138   if (our_type == 0)
139     {
140       static const GTypeInfo our_info =
141       {
142         sizeof (GtkTextLayoutClass),
143         (GBaseInitFunc) NULL,
144         (GBaseFinalizeFunc) NULL,
145         (GClassInitFunc) gtk_text_layout_class_init,
146         NULL,           /* class_finalize */
147         NULL,           /* class_data */
148         sizeof (GtkTextLayout),
149         0,              /* n_preallocs */
150         (GInstanceInitFunc) gtk_text_layout_init
151       };
152
153       our_type = g_type_register_static (G_TYPE_OBJECT,
154                                          "GtkTextLayout",
155                                          &our_info,
156                                          0);
157     }
158
159   return our_type;
160 }
161
162 static void
163 gtk_text_layout_class_init (GtkTextLayoutClass *klass)
164 {
165   GObjectClass *object_class = G_OBJECT_CLASS (klass);
166
167   parent_class = g_type_class_peek_parent (klass);
168   
169   object_class->finalize = gtk_text_layout_finalize;
170
171   klass->wrap = gtk_text_layout_real_wrap;
172   klass->invalidate = gtk_text_layout_real_invalidate;
173   klass->free_line_data = gtk_text_layout_real_free_line_data;
174
175   signals[INVALIDATED] =
176     g_signal_new ("invalidated",
177                   G_OBJECT_CLASS_TYPE (object_class),
178                   G_SIGNAL_RUN_LAST,
179                   G_STRUCT_OFFSET (GtkTextLayoutClass, invalidated),
180                   NULL, NULL,
181                   gtk_marshal_VOID__VOID,
182                   GTK_TYPE_NONE,
183                   0);
184
185   signals[CHANGED] =
186     g_signal_new ("changed",
187                   G_OBJECT_CLASS_TYPE (object_class),
188                   G_SIGNAL_RUN_LAST,
189                   G_STRUCT_OFFSET (GtkTextLayoutClass, changed),
190                   NULL, NULL,
191                   gtk_marshal_VOID__INT_INT_INT,
192                   GTK_TYPE_NONE,
193                   3,
194                   GTK_TYPE_INT,
195                   GTK_TYPE_INT,
196                   GTK_TYPE_INT);
197
198   signals[ALLOCATE_CHILD] =
199     g_signal_new ("allocate_child",
200                   G_OBJECT_CLASS_TYPE (object_class),
201                   G_SIGNAL_RUN_LAST,
202                   G_STRUCT_OFFSET (GtkTextLayoutClass, allocate_child),
203                   NULL, NULL,
204                   gtk_marshal_VOID__OBJECT_INT_INT,
205                   GTK_TYPE_NONE,
206                   3,
207                   GTK_TYPE_OBJECT,
208                   GTK_TYPE_INT,
209                   GTK_TYPE_INT);
210 }
211
212 void
213 gtk_text_layout_init (GtkTextLayout *text_layout)
214 {
215   text_layout->cursor_visible = TRUE;
216 }
217
218 GtkTextLayout*
219 gtk_text_layout_new (void)
220 {
221   return GTK_TEXT_LAYOUT (g_object_new (gtk_text_layout_get_type (), NULL));
222 }
223
224 static void
225 free_style_cache (GtkTextLayout *text_layout)
226 {
227   if (text_layout->one_style_cache)
228     {
229       gtk_text_attributes_unref (text_layout->one_style_cache);
230       text_layout->one_style_cache = NULL;
231     }
232 }
233
234 static void
235 gtk_text_layout_finalize (GObject *object)
236 {
237   GtkTextLayout *layout;
238
239   layout = GTK_TEXT_LAYOUT (object);
240
241   gtk_text_layout_set_buffer (layout, NULL);
242
243   if (layout->default_style)
244     gtk_text_attributes_unref (layout->default_style);
245   layout->default_style = NULL;
246
247   if (layout->ltr_context)
248     {
249       g_object_unref (G_OBJECT (layout->ltr_context));
250       layout->ltr_context = NULL;
251     }
252   if (layout->rtl_context)
253     {
254       g_object_unref (G_OBJECT (layout->rtl_context));
255       layout->rtl_context = NULL;
256     }
257   
258   (* G_OBJECT_CLASS (parent_class)->finalize) (object);
259 }
260
261 void
262 gtk_text_layout_set_buffer (GtkTextLayout *layout,
263                             GtkTextBuffer *buffer)
264 {
265   g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
266   g_return_if_fail (buffer == NULL || GTK_IS_TEXT_BUFFER (buffer));
267
268   if (layout->buffer == buffer)
269     return;
270
271   free_style_cache (layout);
272
273   if (layout->buffer)
274     {
275       _gtk_text_btree_remove_view (_gtk_text_buffer_get_btree (layout->buffer),
276                                   layout);
277
278       g_object_unref (G_OBJECT (layout->buffer));
279       layout->buffer = NULL;
280     }
281
282   if (buffer)
283     {
284       layout->buffer = buffer;
285
286       g_object_ref (G_OBJECT (buffer));
287
288       _gtk_text_btree_add_view (_gtk_text_buffer_get_btree (buffer), layout);
289     }
290 }
291
292 void
293 gtk_text_layout_default_style_changed (GtkTextLayout *layout)
294 {
295   g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
296
297   gtk_text_layout_invalidate_all (layout);
298 }
299
300 void
301 gtk_text_layout_set_default_style (GtkTextLayout *layout,
302                                    GtkTextAttributes *values)
303 {
304   g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
305   g_return_if_fail (values != NULL);
306
307   if (values == layout->default_style)
308     return;
309
310   gtk_text_attributes_ref (values);
311
312   if (layout->default_style)
313     gtk_text_attributes_unref (layout->default_style);
314
315   layout->default_style = values;
316
317   gtk_text_layout_default_style_changed (layout);
318 }
319
320 void
321 gtk_text_layout_set_contexts (GtkTextLayout *layout,
322                               PangoContext  *ltr_context,
323                               PangoContext  *rtl_context)
324 {
325   g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
326
327   if (layout->ltr_context)
328     g_object_unref (G_OBJECT (ltr_context));
329
330   layout->ltr_context = ltr_context;
331   g_object_ref (G_OBJECT (ltr_context));
332
333   if (layout->rtl_context)
334     g_object_unref (G_OBJECT (rtl_context));
335
336   layout->rtl_context = rtl_context;
337   g_object_ref (G_OBJECT (rtl_context));
338
339   gtk_text_layout_invalidate_all (layout);
340 }
341
342 /**
343  * gtk_text_layout_set_cursor_direction:
344  * @direction: the new direction(s) for which to draw cursors.
345  *             %GTK_TEXT_DIR_NONE means draw cursors for both
346  *             left-to-right insertion and right-to-left insertion.
347  *             (The two cursors will be visually distinguished.)
348  * 
349  * Sets which text directions (left-to-right and/or right-to-left) for
350  * which cursors will be drawn for the insertion point. The visual
351  * point at which new text is inserted depends on whether the new
352  * text is right-to-left or left-to-right, so it may be desired to
353  * make the drawn position of the cursor depend on the keyboard state.
354  **/
355 void
356 gtk_text_layout_set_cursor_direction (GtkTextLayout   *layout,
357                                       GtkTextDirection direction)
358 {
359   if (direction != layout->cursor_direction)
360     {
361       layout->cursor_direction = direction;
362       gtk_text_layout_invalidate_cursor_line (layout);
363     }
364 }
365
366 /**
367  * gtk_text_layout_get_buffer:
368  * @layout: a #GtkTextLayout
369  *
370  * Gets the text buffer used by the layout. See
371  * gtk_text_layout_set_buffer().
372  *
373  * Return value: the text buffer used by the layout.
374  **/
375 GtkTextBuffer *
376 gtk_text_layout_get_buffer (GtkTextLayout *layout)
377 {
378   g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), NULL);
379
380   return layout->buffer;
381 }
382
383 void
384 gtk_text_layout_set_screen_width (GtkTextLayout *layout, gint width)
385 {
386   g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
387   g_return_if_fail (width >= 0);
388   g_return_if_fail (layout->wrap_loop_count == 0);
389
390   if (layout->screen_width == width)
391     return;
392
393   layout->screen_width = width;
394
395   gtk_text_layout_invalidate_all (layout);
396 }
397
398 /**
399  * gtk_text_layout_set_cursor_visible:
400  * @layout: a #GtkTextLayout
401  * @cursor_visible: If %FALSE, then the insertion cursor will not
402  *   be shown, even if the text is editable.
403  *
404  * Sets whether the insertion cursor should be shown. Generally,
405  * widgets using #GtkTextLayout will hide the cursor when the
406  * widget does not have the input focus.
407  **/
408 void
409 gtk_text_layout_set_cursor_visible (GtkTextLayout *layout,
410                                     gboolean       cursor_visible)
411 {
412   cursor_visible = (cursor_visible != FALSE);
413
414   if (layout->cursor_visible != cursor_visible)
415     {
416       GtkTextIter iter;
417       gint y, height;
418
419       layout->cursor_visible = cursor_visible;
420
421       /* Now queue a redraw on the paragraph containing the cursor
422        */
423       gtk_text_buffer_get_iter_at_mark (layout->buffer, &iter,
424                                         gtk_text_buffer_get_mark (layout->buffer, "insert"));
425
426       gtk_text_layout_get_line_yrange (layout, &iter, &y, &height);
427       gtk_text_layout_changed (layout, y, height, height);
428
429       gtk_text_layout_invalidate_cache (layout, _gtk_text_iter_get_text_line (&iter));
430     }
431 }
432
433 /**
434  * gtk_text_layout_get_cursor_visible:
435  * @layout: a #GtkTextLayout
436  *
437  * Returns whether the insertion cursor will be shown.
438  *
439  * Return value: if %FALSE, the insertion cursor will not be
440     shown, even if the text is editable.
441  **/
442 gboolean
443 gtk_text_layout_get_cursor_visible (GtkTextLayout *layout)
444 {
445   return layout->cursor_visible;
446 }
447
448 /**
449  * gtk_text_layout_set_preedit_string:
450  * @layout: a #PangoLayout
451  * @preedit_string: a string to display at the insertion point
452  * @preedit_attrs: a #PangoAttrList of attributes that apply to @preedit_string
453  * @cursor_pos: position of cursor within preedit string in chars
454  * 
455  * Set the preedit string and attributes. The preedit string is a
456  * string showing text that is currently being edited and not
457  * yet committed into the buffer.
458  **/
459 void
460 gtk_text_layout_set_preedit_string (GtkTextLayout *layout,
461                                     const gchar   *preedit_string,
462                                     PangoAttrList *preedit_attrs,
463                                     gint           cursor_pos)
464 {
465   g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
466   g_return_if_fail (preedit_attrs != NULL || preedit_string == NULL);
467
468   if (layout->preedit_string)
469     g_free (layout->preedit_string);
470
471   if (layout->preedit_attrs)
472     pango_attr_list_unref (layout->preedit_attrs);
473
474   if (preedit_string)
475     {
476       layout->preedit_string = g_strdup (preedit_string);
477       layout->preedit_len = strlen (layout->preedit_string);
478       pango_attr_list_ref (preedit_attrs);
479       layout->preedit_attrs = preedit_attrs;
480
481       cursor_pos = CLAMP (cursor_pos, 0, g_utf8_strlen (layout->preedit_string, -1));
482       layout->preedit_cursor = g_utf8_offset_to_pointer (layout->preedit_string, cursor_pos) - layout->preedit_string;
483     }
484   else
485     {
486       layout->preedit_string = NULL;
487       layout->preedit_len = 0;
488       layout->preedit_attrs = NULL;
489       layout->preedit_cursor = 0;
490     }
491
492   gtk_text_layout_invalidate_cursor_line (layout);
493 }
494
495 void
496 gtk_text_layout_get_size (GtkTextLayout *layout,
497                           gint *width,
498                           gint *height)
499 {
500   g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
501
502   if (width)
503     *width = layout->width;
504
505   if (height)
506     *height = layout->height;
507 }
508
509 static void
510 gtk_text_layout_invalidated (GtkTextLayout *layout)
511 {
512   g_signal_emit (G_OBJECT (layout), signals[INVALIDATED], 0);
513 }
514
515 void
516 gtk_text_layout_changed (GtkTextLayout *layout,
517                          gint           y,
518                          gint           old_height,
519                          gint           new_height)
520 {
521   g_signal_emit (G_OBJECT (layout), signals[CHANGED], 0,
522                  y, old_height, new_height);
523 }
524
525 void
526 gtk_text_layout_free_line_data (GtkTextLayout     *layout,
527                                 GtkTextLine       *line,
528                                 GtkTextLineData   *line_data)
529 {
530   (* GTK_TEXT_LAYOUT_GET_CLASS (layout)->free_line_data)
531     (layout, line, line_data);
532 }
533
534 void
535 gtk_text_layout_invalidate (GtkTextLayout *layout,
536                             const GtkTextIter *start_index,
537                             const GtkTextIter *end_index)
538 {
539   (* GTK_TEXT_LAYOUT_GET_CLASS (layout)->invalidate)
540     (layout, start_index, end_index);
541 }
542
543 GtkTextLineData*
544 gtk_text_layout_wrap (GtkTextLayout *layout,
545                       GtkTextLine  *line,
546                       /* may be NULL */
547                       GtkTextLineData *line_data)
548 {
549   return (* GTK_TEXT_LAYOUT_GET_CLASS (layout)->wrap) (layout, line, line_data);
550 }
551
552 GSList*
553 gtk_text_layout_get_lines (GtkTextLayout *layout,
554                            /* [top_y, bottom_y) */
555                            gint top_y,
556                            gint bottom_y,
557                            gint *first_line_y)
558 {
559   GtkTextLine *first_btree_line;
560   GtkTextLine *last_btree_line;
561   GtkTextLine *line;
562   GSList *retval;
563
564   g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), NULL);
565   g_return_val_if_fail (bottom_y > top_y, NULL);
566
567   retval = NULL;
568
569   first_btree_line =
570     _gtk_text_btree_find_line_by_y (_gtk_text_buffer_get_btree (layout->buffer),
571                                    layout, top_y, first_line_y);
572   if (first_btree_line == NULL)
573     {
574       /* off the bottom */
575       return NULL;
576     }
577
578   /* -1 since bottom_y is one past */
579   last_btree_line =
580     _gtk_text_btree_find_line_by_y (_gtk_text_buffer_get_btree (layout->buffer),
581                                     layout, bottom_y - 1, NULL);
582
583   if (!last_btree_line)
584     last_btree_line =
585       _gtk_text_btree_get_end_iter_line (_gtk_text_buffer_get_btree (layout->buffer));
586
587   g_assert (last_btree_line != NULL);
588
589   line = first_btree_line;
590   while (TRUE)
591     {
592       retval = g_slist_prepend (retval, line);
593
594       if (line == last_btree_line)
595         break;
596
597       line = _gtk_text_line_next_excluding_last (line);
598     }
599
600   retval = g_slist_reverse (retval);
601
602   return retval;
603 }
604
605 static void
606 invalidate_cached_style (GtkTextLayout *layout)
607 {
608   free_style_cache (layout);
609 }
610
611 /* These should be called around a loop which wraps a CONTIGUOUS bunch
612  * of display lines. If the lines aren't contiguous you can't call
613  * these.
614  */
615 void
616 gtk_text_layout_wrap_loop_start (GtkTextLayout *layout)
617 {
618   g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
619   g_return_if_fail (layout->one_style_cache == NULL);
620
621   layout->wrap_loop_count += 1;
622 }
623
624 void
625 gtk_text_layout_wrap_loop_end (GtkTextLayout *layout)
626 {
627   g_return_if_fail (layout->wrap_loop_count > 0);
628
629   layout->wrap_loop_count -= 1;
630
631   if (layout->wrap_loop_count == 0)
632     {
633       /* We cache a some stuff if we're iterating over some lines wrapping
634        * them. This cleans it up.
635        */
636       /* Nuke our cached style */
637       invalidate_cached_style (layout);
638       g_assert (layout->one_style_cache == NULL);
639     }
640 }
641
642 static void
643 gtk_text_layout_invalidate_all (GtkTextLayout *layout)
644 {
645   GtkTextIter start;
646   GtkTextIter end;
647
648   if (layout->buffer == NULL)
649     return;
650
651   gtk_text_buffer_get_bounds (layout->buffer, &start, &end);
652
653   gtk_text_layout_invalidate (layout, &start, &end);
654 }
655
656 static void
657 gtk_text_layout_invalidate_cache (GtkTextLayout *layout,
658                                   GtkTextLine   *line)
659 {
660   if (layout->one_display_cache && line == layout->one_display_cache->line)
661     {
662       GtkTextLineDisplay *tmp_display = layout->one_display_cache;
663       layout->one_display_cache = NULL;
664       gtk_text_layout_free_line_display (layout, tmp_display);
665     }
666 }
667
668 /* Now invalidate the paragraph containing the cursor
669  */
670 static void
671 gtk_text_layout_invalidate_cursor_line (GtkTextLayout *layout)
672 {
673   GtkTextIter iter;
674   GtkTextLine *line;
675   GtkTextLineData *line_data;
676
677   gtk_text_buffer_get_iter_at_mark (layout->buffer, &iter,
678                                     gtk_text_buffer_get_mark (layout->buffer, "insert"));
679   
680   line = _gtk_text_iter_get_text_line (&iter);
681   line_data = _gtk_text_line_get_data (line, layout);
682   if (line_data)
683     {
684       gtk_text_layout_invalidate_cache (layout, line);
685       _gtk_text_line_invalidate_wrap (line, line_data);
686       gtk_text_layout_invalidated (layout);
687     }
688 }
689
690 static void
691 gtk_text_layout_real_invalidate (GtkTextLayout *layout,
692                                  const GtkTextIter *start,
693                                  const GtkTextIter *end)
694 {
695   GtkTextLine *line;
696   GtkTextLine *last_line;
697
698   g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
699   g_return_if_fail (layout->wrap_loop_count == 0);
700
701 #if 0
702   gtk_text_view_index_spew (start_index, "invalidate start");
703   gtk_text_view_index_spew (end_index, "invalidate end");
704 #endif
705
706   last_line = _gtk_text_iter_get_text_line (end);
707   line = _gtk_text_iter_get_text_line (start);
708
709   while (TRUE)
710     {
711       GtkTextLineData *line_data = _gtk_text_line_get_data (line, layout);
712
713       if (line_data &&
714           (line != last_line || !gtk_text_iter_starts_line (end)))
715         {
716           gtk_text_layout_invalidate_cache (layout, line);
717           _gtk_text_line_invalidate_wrap (line, line_data);
718         }
719
720       if (line == last_line)
721         break;
722
723       line = _gtk_text_line_next_excluding_last (line);
724     }
725
726   gtk_text_layout_invalidated (layout);
727 }
728
729 static void
730 gtk_text_layout_real_free_line_data (GtkTextLayout     *layout,
731                                      GtkTextLine       *line,
732                                      GtkTextLineData   *line_data)
733 {
734   if (layout->one_display_cache && line == layout->one_display_cache->line)
735     {
736       GtkTextLineDisplay *tmp_display = layout->one_display_cache;
737       layout->one_display_cache = NULL;
738       gtk_text_layout_free_line_display (layout, tmp_display);
739     }
740
741   g_free (line_data);
742 }
743
744
745
746 /**
747  * gtk_text_layout_is_valid:
748  * @layout: a #GtkTextLayout
749  *
750  * Check if there are any invalid regions in a #GtkTextLayout's buffer
751  *
752  * Return value: #TRUE if any invalid regions were found
753  **/
754 gboolean
755 gtk_text_layout_is_valid (GtkTextLayout *layout)
756 {
757   g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), FALSE);
758
759   return _gtk_text_btree_is_valid (_gtk_text_buffer_get_btree (layout->buffer),
760                                   layout);
761 }
762
763 static void
764 update_layout_size (GtkTextLayout *layout)
765 {
766   _gtk_text_btree_get_view_size (_gtk_text_buffer_get_btree (layout->buffer),
767                                 layout,
768                                 &layout->width, &layout->height);
769 }
770
771 /**
772  * gtk_text_layout_validate_yrange:
773  * @layout: a #GtkTextLayout
774  * @anchor: iter pointing into a line that will be used as the
775  *          coordinate origin
776  * @y0: offset from the top of the line pointed to by @anchor at
777  *      which to begin validation. (The offset here is in pixels
778  *      after validation.)
779  * @y1: offset from the top of the line pointed to by @anchor at
780  *      which to end validation. (The offset here is in pixels
781  *      after validation.)
782  *
783  * Ensure that a region of a #GtkTextLayout is valid. The ::changed
784  * signal will be emitted if any lines are validated.
785  **/
786 void
787 gtk_text_layout_validate_yrange (GtkTextLayout *layout,
788                                  GtkTextIter   *anchor,
789                                  gint           y0,
790                                  gint           y1)
791 {
792   GtkTextLine *line;
793   GtkTextLine *first_line = NULL;
794   GtkTextLine *last_line = NULL;
795   gint seen;
796   gint delta_height = 0;
797   gint first_line_y = 0;        /* Quiet GCC */
798   gint last_line_y = 0;         /* Quiet GCC */
799
800   g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
801
802   if (y0 > 0)
803     y0 = 0;
804   if (y1 < 0)
805     y1 = 0;
806   
807   /* Validate backwards from the anchor line to y0
808    */
809   line = _gtk_text_iter_get_text_line (anchor);
810   seen = 0;
811   while (line && seen < -y0)
812     {
813       GtkTextLineData *line_data = _gtk_text_line_get_data (line, layout);
814       if (!line_data || !line_data->valid)
815         {
816           gint old_height = line_data ? line_data->height : 0;
817
818           _gtk_text_btree_validate_line (_gtk_text_buffer_get_btree (layout->buffer),
819                                          line, layout);
820           line_data = _gtk_text_line_get_data (line, layout);
821
822           delta_height += line_data->height - old_height;
823           
824           first_line = line;
825           first_line_y = -seen;
826           if (!last_line)
827             {
828               last_line = line;
829               last_line_y = -seen + line_data->height;
830             }
831         }
832
833       seen += line_data->height;
834       line = _gtk_text_line_previous (line);
835     }
836
837   /* Validate forwards to y1 */
838   line = _gtk_text_iter_get_text_line (anchor);
839   seen = 0;
840   while (line && seen < y1)
841     {
842       GtkTextLineData *line_data = _gtk_text_line_get_data (line, layout);
843       if (!line_data || !line_data->valid)
844         {
845           gint old_height = line_data ? line_data->height : 0;
846
847           _gtk_text_btree_validate_line (_gtk_text_buffer_get_btree (layout->buffer),
848                                          line, layout);
849           line_data = _gtk_text_line_get_data (line, layout);
850
851           delta_height += line_data->height - old_height;
852           
853           if (!first_line)
854             {
855               first_line = line;
856               first_line_y = seen;
857             }
858           last_line = line;
859           last_line_y = seen + line_data->height;
860         }
861
862       seen += line_data->height;
863       line = _gtk_text_line_next_excluding_last (line);
864     }
865
866   /* If we found and validated any invalid lines, update size and
867    * emit the changed signal
868    */
869   if (first_line)
870     {
871       gint line_top;
872
873       update_layout_size (layout);
874
875       line_top = _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
876                                                 first_line, layout);
877
878       gtk_text_layout_changed (layout,
879                                line_top,
880                                last_line_y - first_line_y - delta_height,
881                                last_line_y - first_line_y);
882     }
883 }
884
885 /**
886  * gtk_text_layout_validate:
887  * @tree: a #GtkTextLayout
888  * @max_pixels: the maximum number of pixels to validate. (No more
889  *              than one paragraph beyond this limit will be validated)
890  *
891  * Validate regions of a #GtkTextLayout. The ::changed signal will
892  * be emitted for each region validated.
893  **/
894 void
895 gtk_text_layout_validate (GtkTextLayout *layout,
896                           gint           max_pixels)
897 {
898   gint y, old_height, new_height;
899
900   g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
901
902   while (max_pixels > 0 &&
903          _gtk_text_btree_validate (_gtk_text_buffer_get_btree (layout->buffer),
904                                    layout,  max_pixels,
905                                    &y, &old_height, &new_height))
906     {
907       max_pixels -= new_height;
908
909       update_layout_size (layout);
910       gtk_text_layout_changed (layout, y, old_height, new_height);
911     }
912 }
913
914 static GtkTextLineData*
915 gtk_text_layout_real_wrap (GtkTextLayout   *layout,
916                            GtkTextLine     *line,
917                            /* may be NULL */
918                            GtkTextLineData *line_data)
919 {
920   GtkTextLineDisplay *display;
921
922   g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), NULL);
923
924   if (line_data == NULL)
925     {
926       line_data = gtk_text_line_data_new (layout, line);
927       _gtk_text_line_add_data (line, line_data);
928     }
929
930   display = gtk_text_layout_get_line_display (layout, line, TRUE);
931   line_data->width = display->width;
932   line_data->height = display->height;
933   line_data->valid = TRUE;
934   gtk_text_layout_free_line_display (layout, display);
935
936   return line_data;
937 }
938
939 /*
940  * Layout utility functions
941  */
942
943 /* If you get the style with get_style () you need to call
944    release_style () to free it. */
945 static GtkTextAttributes*
946 get_style (GtkTextLayout *layout,
947            const GtkTextIter *iter)
948 {
949   GtkTextTag** tags;
950   gint tag_count = 0;
951   GtkTextAttributes *style;
952
953   /* If we have the one-style cache, then it means
954      that we haven't seen a toggle since we filled in the
955      one-style cache.
956   */
957   if (layout->one_style_cache != NULL)
958     {
959       gtk_text_attributes_ref (layout->one_style_cache);
960       return layout->one_style_cache;
961     }
962
963   g_assert (layout->one_style_cache == NULL);
964
965   /* Get the tags at this spot */
966   tags = _gtk_text_btree_get_tags (iter, &tag_count);
967
968   /* No tags, use default style */
969   if (tags == NULL || tag_count == 0)
970     {
971       /* One ref for the return value, one ref for the
972          layout->one_style_cache reference */
973       gtk_text_attributes_ref (layout->default_style);
974       gtk_text_attributes_ref (layout->default_style);
975       layout->one_style_cache = layout->default_style;
976
977       if (tags)
978         g_free (tags);
979
980       return layout->default_style;
981     }
982
983   /* Sort tags in ascending order of priority */
984   _gtk_text_tag_array_sort (tags, tag_count);
985
986   style = gtk_text_attributes_new ();
987
988   gtk_text_attributes_copy_values (layout->default_style,
989                                    style);
990
991   _gtk_text_attributes_fill_from_tags (style,
992                                        tags,
993                                        tag_count);
994
995   g_free (tags);
996
997   g_assert (style->refcount == 1);
998
999   /* Leave this style as the last one seen */
1000   g_assert (layout->one_style_cache == NULL);
1001   gtk_text_attributes_ref (style); /* ref held by layout->one_style_cache */
1002   layout->one_style_cache = style;
1003
1004   /* Returning yet another refcount */
1005   return style;
1006 }
1007
1008 static void
1009 release_style (GtkTextLayout *layout,
1010                GtkTextAttributes *style)
1011 {
1012   g_return_if_fail (style != NULL);
1013   g_return_if_fail (style->refcount > 0);
1014
1015   gtk_text_attributes_unref (style);
1016 }
1017
1018 /*
1019  * Lines
1020  */
1021
1022 /* This function tries to optimize the case where a line
1023    is completely invisible */
1024 static gboolean
1025 totally_invisible_line (GtkTextLayout *layout,
1026                         GtkTextLine   *line,
1027                         GtkTextIter   *iter)
1028 {
1029   GtkTextLineSegment *seg;
1030   int bytes = 0;
1031
1032   /* If we have a cached style, then we know it does actually apply
1033    * and we can just see if it is invisible.
1034    */
1035   if (layout->one_style_cache &&
1036       !layout->one_style_cache->invisible)
1037     return FALSE;
1038   /* Without the cache, we check if the first char is visible, if so
1039    * we are partially visible.  Note that we have to check this since
1040    * we don't know the current invisible/noninvisible toggle state; this
1041    * function can use the whole btree to get it right.
1042    */
1043   else
1044     {
1045       _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
1046                                        iter, line, 0);
1047
1048       if (!_gtk_text_btree_char_is_invisible (iter))
1049         return FALSE;
1050     }
1051
1052   bytes = 0;
1053   seg = line->segments;
1054
1055   while (seg != NULL)
1056     {
1057       if (seg->byte_count > 0)
1058         bytes += seg->byte_count;
1059
1060       /* Note that these two tests can cause us to bail out
1061        * when we shouldn't, because a higher-priority tag
1062        * may override these settings. However the important
1063        * thing is to only invisible really-invisible lines, rather
1064        * than to invisible all really-invisible lines.
1065        */
1066
1067       else if (seg->type == &gtk_text_toggle_on_type)
1068         {
1069           invalidate_cached_style (layout);
1070
1071           /* Bail out if an elision-unsetting tag begins */
1072           if (seg->body.toggle.info->tag->invisible_set &&
1073               !seg->body.toggle.info->tag->values->invisible)
1074             break;
1075         }
1076       else if (seg->type == &gtk_text_toggle_off_type)
1077         {
1078           invalidate_cached_style (layout);
1079
1080           /* Bail out if an elision-setting tag ends */
1081           if (seg->body.toggle.info->tag->invisible_set &&
1082               seg->body.toggle.info->tag->values->invisible)
1083             break;
1084         }
1085
1086       seg = seg->next;
1087     }
1088
1089   if (seg != NULL)       /* didn't reach line end */
1090     return FALSE;
1091
1092   return TRUE;
1093 }
1094
1095 static void
1096 set_para_values (GtkTextLayout      *layout,
1097                  GtkTextAttributes *style,
1098                  GtkTextLineDisplay *display)
1099 {
1100   PangoAlignment pango_align = PANGO_ALIGN_LEFT;
1101   int layout_width;
1102
1103   display->direction = style->direction;
1104
1105   if (display->direction == GTK_TEXT_DIR_LTR)
1106     display->layout = pango_layout_new (layout->ltr_context);
1107   else
1108     display->layout = pango_layout_new (layout->rtl_context);
1109
1110   switch (style->justification)
1111     {
1112     case GTK_JUSTIFY_LEFT:
1113       pango_align = (style->direction == GTK_TEXT_DIR_LTR) ? PANGO_ALIGN_LEFT : PANGO_ALIGN_RIGHT;
1114       break;
1115     case GTK_JUSTIFY_RIGHT:
1116       pango_align = (style->direction == GTK_TEXT_DIR_LTR) ? PANGO_ALIGN_RIGHT : PANGO_ALIGN_LEFT;
1117       break;
1118     case GTK_JUSTIFY_CENTER:
1119       pango_align = PANGO_ALIGN_CENTER;
1120       break;
1121     case GTK_JUSTIFY_FILL:
1122       g_warning ("FIXME we don't support GTK_JUSTIFY_FILL yet");
1123       break;
1124     default:
1125       g_assert_not_reached ();
1126       break;
1127     }
1128
1129   pango_layout_set_alignment (display->layout, pango_align);
1130   pango_layout_set_spacing (display->layout,
1131                             style->pixels_inside_wrap * PANGO_SCALE);
1132
1133   if (style->tabs)
1134     pango_layout_set_tabs (display->layout, style->tabs);
1135
1136   display->top_margin = style->pixels_above_lines;
1137   display->height = style->pixels_above_lines + style->pixels_below_lines;
1138   display->bottom_margin = style->pixels_below_lines;
1139   display->left_margin = style->left_margin;
1140   display->right_margin = style->right_margin;
1141   
1142   display->x_offset = display->left_margin;
1143
1144   pango_layout_set_indent (display->layout,
1145                            style->indent * PANGO_SCALE);
1146
1147   switch (style->wrap_mode)
1148     {
1149     case GTK_WRAP_CHAR:
1150       layout_width = layout->screen_width - display->left_margin - display->right_margin;
1151       pango_layout_set_width (display->layout, layout_width * PANGO_SCALE);
1152       pango_layout_set_wrap (display->layout, PANGO_WRAP_CHAR);
1153       break;
1154
1155     case GTK_WRAP_WORD:
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_WORD);
1159       break;
1160
1161     case GTK_WRAP_NONE:
1162       break;
1163     }
1164   
1165   display->total_width = MAX (layout->screen_width, layout->width) - display->left_margin - display->right_margin;
1166 }
1167
1168 static PangoAttribute *
1169 gtk_text_attr_appearance_copy (const PangoAttribute *attr)
1170 {
1171   const GtkTextAttrAppearance *appearance_attr = (const GtkTextAttrAppearance *)attr;
1172
1173   return gtk_text_attr_appearance_new (&appearance_attr->appearance);
1174 }
1175
1176 static void
1177 gtk_text_attr_appearance_destroy (PangoAttribute *attr)
1178 {
1179   GtkTextAppearance *appearance = &((GtkTextAttrAppearance *)attr)->appearance;
1180
1181   if (appearance->bg_stipple)
1182     gdk_drawable_unref (appearance->bg_stipple);
1183   if (appearance->fg_stipple)
1184     gdk_drawable_unref (appearance->fg_stipple);
1185
1186   g_free (attr);
1187 }
1188
1189 static gboolean
1190 gtk_text_attr_appearance_compare (const PangoAttribute *attr1,
1191                                   const PangoAttribute *attr2)
1192 {
1193   const GtkTextAppearance *appearance1 = &((const GtkTextAttrAppearance *)attr1)->appearance;
1194   const GtkTextAppearance *appearance2 = &((const GtkTextAttrAppearance *)attr2)->appearance;
1195
1196   return (gdk_color_equal (&appearance1->fg_color, &appearance2->fg_color) &&
1197           gdk_color_equal (&appearance1->bg_color, &appearance2->bg_color) &&
1198           appearance1->fg_stipple ==  appearance2->fg_stipple &&
1199           appearance1->bg_stipple ==  appearance2->bg_stipple &&
1200           appearance1->underline == appearance2->underline &&
1201           appearance1->strikethrough == appearance2->strikethrough &&
1202           appearance1->draw_bg == appearance2->draw_bg);
1203
1204 }
1205
1206 /**
1207  * gtk_text_attr_appearance_new:
1208  * @desc:
1209  *
1210  * Create a new font description attribute. (This attribute
1211  * allows setting family, style, weight, variant, stretch,
1212  * and size simultaneously.)
1213  *
1214  * Return value:
1215  **/
1216 static PangoAttribute *
1217 gtk_text_attr_appearance_new (const GtkTextAppearance *appearance)
1218 {
1219   static PangoAttrClass klass = {
1220     0,
1221     gtk_text_attr_appearance_copy,
1222     gtk_text_attr_appearance_destroy,
1223     gtk_text_attr_appearance_compare
1224   };
1225
1226   GtkTextAttrAppearance *result;
1227
1228   if (!klass.type)
1229     klass.type = gtk_text_attr_appearance_type =
1230       pango_attr_type_register ("GtkTextAttrAppearance");
1231
1232   result = g_new (GtkTextAttrAppearance, 1);
1233   result->attr.klass = &klass;
1234
1235   result->appearance = *appearance;
1236
1237   if (appearance->bg_stipple)
1238     gdk_drawable_ref (appearance->bg_stipple);
1239   if (appearance->fg_stipple)
1240     gdk_drawable_ref (appearance->fg_stipple);
1241
1242   return (PangoAttribute *)result;
1243 }
1244
1245
1246 static void
1247 add_generic_attrs (GtkTextLayout      *layout,
1248                    GtkTextAppearance  *appearance,
1249                    gint                byte_count,
1250                    PangoAttrList      *attrs,
1251                    gint                start,
1252                    gboolean            size_only,
1253                    gboolean            is_text)
1254 {
1255   PangoAttribute *attr;
1256
1257   if (appearance->underline != PANGO_UNDERLINE_NONE)
1258     {
1259       attr = pango_attr_underline_new (appearance->underline);
1260       
1261       attr->start_index = start;
1262       attr->end_index = start + byte_count;
1263       
1264       pango_attr_list_insert (attrs, attr);
1265     }
1266
1267   if (appearance->rise != 0)
1268     {
1269       attr = pango_attr_rise_new (appearance->rise);
1270       
1271       attr->start_index = start;
1272       attr->end_index = start + byte_count;
1273       
1274       pango_attr_list_insert (attrs, attr);
1275     }
1276   
1277   if (!size_only)
1278     {
1279       attr = gtk_text_attr_appearance_new (appearance);
1280       
1281       attr->start_index = start;
1282       attr->end_index = start + byte_count;
1283
1284       ((GtkTextAttrAppearance *)attr)->appearance.is_text = is_text;
1285       
1286       pango_attr_list_insert (attrs, attr);
1287     }
1288 }
1289
1290 static void
1291 add_text_attrs (GtkTextLayout      *layout,
1292                 GtkTextAttributes  *style,
1293                 gint                byte_count,
1294                 PangoAttrList      *attrs,
1295                 gint                start,
1296                 gboolean            size_only)
1297 {
1298   PangoAttribute *attr;
1299
1300   attr = pango_attr_font_desc_new (style->font);
1301   attr->start_index = start;
1302   attr->end_index = start + byte_count;
1303
1304   pango_attr_list_insert (attrs, attr);
1305
1306   if (style->font_scale != 1.0)
1307     {
1308       attr = pango_attr_scale_new (style->font_scale);
1309
1310       attr->start_index = start;
1311       attr->end_index = start + byte_count;
1312       
1313       pango_attr_list_insert (attrs, attr);
1314     }
1315 }
1316
1317 static void
1318 add_pixbuf_attrs (GtkTextLayout      *layout,
1319                   GtkTextLineDisplay *display,
1320                   GtkTextAttributes  *style,
1321                   GtkTextLineSegment *seg,
1322                   PangoAttrList      *attrs,
1323                   gint                start)
1324 {
1325   PangoAttribute *attr;
1326   PangoRectangle logical_rect;
1327   GtkTextPixbuf *pixbuf = &seg->body.pixbuf;
1328   gint width, height;
1329
1330   width = gdk_pixbuf_get_width (pixbuf->pixbuf);
1331   height = gdk_pixbuf_get_height (pixbuf->pixbuf);
1332
1333   logical_rect.x = 0;
1334   logical_rect.y = -height * PANGO_SCALE;
1335   logical_rect.width = width * PANGO_SCALE;
1336   logical_rect.height = height * PANGO_SCALE;
1337
1338   attr = pango_attr_shape_new (&logical_rect, &logical_rect);
1339   attr->start_index = start;
1340   attr->end_index = start + seg->byte_count;
1341   pango_attr_list_insert (attrs, attr);
1342
1343   display->shaped_objects =
1344     g_slist_append (display->shaped_objects, pixbuf->pixbuf);
1345 }
1346
1347 static void
1348 add_child_attrs (GtkTextLayout      *layout,
1349                  GtkTextLineDisplay *display,
1350                  GtkTextAttributes  *style,
1351                  GtkTextLineSegment *seg,
1352                  PangoAttrList      *attrs,
1353                  gint                start)
1354 {
1355   PangoAttribute *attr;
1356   PangoRectangle logical_rect;
1357   GtkTextChildAnchor *anchor;
1358   gint width, height;
1359   GSList *tmp_list;
1360
1361   width = 1;
1362   height = 1;
1363   
1364   anchor = seg->body.child.obj;
1365
1366   tmp_list = seg->body.child.widgets;
1367   while (tmp_list != NULL)
1368     {
1369       GtkWidget *child = tmp_list->data;
1370
1371       if (_gtk_anchored_child_get_layout (child) == layout)
1372         {
1373           /* Found it */
1374           GtkRequisition req;
1375
1376           gtk_widget_get_child_requisition (child, &req);
1377           
1378           width = req.width;
1379           height = req.height;
1380
1381           display->shaped_objects =
1382             g_slist_append (display->shaped_objects, child);
1383           
1384           break;
1385         }
1386       
1387       tmp_list = g_slist_next (tmp_list);
1388     }
1389
1390   if (tmp_list == NULL)
1391     {
1392       /* If tmp_list == NULL then there is no widget at this anchor in
1393        * this display; not an error. We make up an arbitrary size
1394        * to use, just so the programmer can see the blank spot.
1395        * We also put a NULL in the shaped objects list, to keep
1396        * the correspondence between the list and the shaped chars in
1397        * the layout. A bad hack, yes.
1398        */
1399
1400       width = 30;
1401       height = 20;
1402
1403       display->shaped_objects =
1404         g_slist_append (display->shaped_objects, NULL);
1405     }
1406   
1407   logical_rect.x = 0;
1408   logical_rect.y = -height * PANGO_SCALE;
1409   logical_rect.width = width * PANGO_SCALE;
1410   logical_rect.height = height * PANGO_SCALE;
1411
1412   attr = pango_attr_shape_new (&logical_rect, &logical_rect);
1413   attr->start_index = start;
1414   attr->end_index = start + seg->byte_count;
1415   pango_attr_list_insert (attrs, attr);
1416 }
1417
1418 static void
1419 add_cursor (GtkTextLayout      *layout,
1420             GtkTextLineDisplay *display,
1421             GtkTextLineSegment *seg,
1422             gint                start)
1423 {
1424   PangoRectangle strong_pos, weak_pos;
1425   GtkTextCursorDisplay *cursor = NULL; /* Quiet GCC */
1426   gboolean add_weak = FALSE;
1427   gboolean add_strong = FALSE;
1428   
1429   /* Hide insertion cursor when we have a selection or the layout
1430    * user has hidden the cursor.
1431    */
1432   if (_gtk_text_btree_mark_is_insert (_gtk_text_buffer_get_btree (layout->buffer),
1433                                      seg->body.mark.obj) &&
1434       (!layout->cursor_visible ||
1435        gtk_text_buffer_get_selection_bounds (layout->buffer, NULL, NULL)))
1436     return;
1437
1438   pango_layout_get_cursor_pos (display->layout, start, &strong_pos, &weak_pos);
1439
1440   if (layout->cursor_direction == GTK_TEXT_DIR_NONE)
1441     {
1442       add_strong = TRUE;
1443       add_weak = TRUE;
1444     }
1445   else if (display->direction == layout->cursor_direction)
1446     add_strong = TRUE;
1447   else
1448     add_weak = TRUE;
1449
1450   if (add_strong)
1451     {
1452       cursor = g_new (GtkTextCursorDisplay, 1);
1453
1454       cursor->x = PANGO_PIXELS (strong_pos.x);
1455       cursor->y = PANGO_PIXELS (strong_pos.y);
1456       cursor->height = PANGO_PIXELS (strong_pos.height);
1457       cursor->is_strong = TRUE;
1458       cursor->is_weak = FALSE;
1459       display->cursors = g_slist_prepend (display->cursors, cursor);
1460     }
1461   
1462   if (add_weak)
1463     {
1464       if (weak_pos.x == strong_pos.x && add_strong)
1465         cursor->is_weak = TRUE;
1466       else
1467         {
1468           cursor = g_new (GtkTextCursorDisplay, 1);
1469           
1470           cursor->x = PANGO_PIXELS (weak_pos.x);
1471           cursor->y = PANGO_PIXELS (weak_pos.y);
1472           cursor->height = PANGO_PIXELS (weak_pos.height);
1473           cursor->is_strong = FALSE;
1474           cursor->is_weak = TRUE;
1475           display->cursors = g_slist_prepend (display->cursors, cursor);
1476         }
1477     }
1478 }
1479
1480 static gboolean
1481 is_shape (PangoLayoutRun *run)
1482 {
1483   GSList *tmp_list = run->item->analysis.extra_attrs;
1484     
1485   while (tmp_list)
1486     {
1487       PangoAttribute *attr = tmp_list->data;
1488
1489       if (attr->klass->type == PANGO_ATTR_SHAPE)
1490         return TRUE;
1491
1492       tmp_list = tmp_list->next;
1493     }
1494
1495   return FALSE;
1496 }
1497
1498 static void
1499 allocate_child_widgets (GtkTextLayout      *text_layout,
1500                         GtkTextLineDisplay *display)
1501 {
1502   GSList *shaped = display->shaped_objects;
1503   PangoLayout *layout = display->layout;
1504   PangoLayoutIter *iter;
1505   
1506   iter = pango_layout_get_iter (layout);
1507   
1508   do
1509     {
1510       PangoLayoutRun *run = pango_layout_iter_get_run (iter);
1511
1512       if (run && is_shape (run))
1513         {
1514           GObject *shaped_object = shaped->data;
1515           shaped = shaped->next;
1516
1517           /* shaped_object is NULL for child anchors with no
1518            * widgets stored at them
1519            */
1520           if (shaped_object && GTK_IS_WIDGET (shaped_object))
1521             {
1522               PangoRectangle extents;
1523
1524               /* We emit "allocate_child" with the x,y of
1525                * the widget with respect to the top of the line
1526                * and the left side of the buffer
1527                */
1528               
1529               pango_layout_iter_get_run_extents (iter,
1530                                                  NULL,
1531                                                  &extents);
1532               
1533               g_signal_emit (G_OBJECT (text_layout),
1534                              signals[ALLOCATE_CHILD],
1535                              0,
1536                              shaped_object,
1537                              PANGO_PIXELS (extents.x) + display->x_offset,
1538                              PANGO_PIXELS (extents.y) + display->top_margin);
1539             }
1540         }
1541     }
1542   while (pango_layout_iter_next_run (iter));
1543   
1544   pango_layout_iter_free (iter);
1545 }
1546
1547 static void
1548 convert_color (GdkColor       *result,
1549                PangoAttrColor *attr)
1550 {
1551   result->red = attr->color.red;
1552   result->blue = attr->color.blue;
1553   result->green = attr->color.green;
1554 }
1555
1556 /* This function is used to convert the preedit string attributes, which are
1557  * standard PangoAttributes, into the custom attributes used by the text
1558  * widget and insert them into a attr list with a given offset.
1559  */
1560 static void
1561 add_preedit_attrs (GtkTextLayout     *layout,
1562                    GtkTextAttributes *style,
1563                    PangoAttrList     *attrs,
1564                    gint               offset,
1565                    gboolean           size_only)
1566 {
1567   PangoAttrIterator *iter = pango_attr_list_get_iterator (layout->preedit_attrs);
1568
1569   do
1570     {
1571       GtkTextAppearance appearance = style->appearance;
1572       PangoFontDescription *font_desc = pango_font_description_copy_static (style->font);
1573       PangoAttribute *insert_attr;
1574       GSList *extra_attrs = NULL;
1575       GSList *tmp_list;
1576       PangoLanguage *language;
1577       gint start, end;
1578
1579       pango_attr_iterator_range (iter, &start, &end);
1580
1581       if (end == G_MAXINT)
1582         end = layout->preedit_len;
1583       
1584       pango_attr_iterator_get_font (iter, font_desc, &language, &extra_attrs);
1585       
1586       tmp_list = extra_attrs;
1587       while (tmp_list)
1588         {
1589           PangoAttribute *attr = tmp_list->data;
1590           
1591           switch (attr->klass->type)
1592             {
1593             case PANGO_ATTR_FOREGROUND:
1594               convert_color (&appearance.fg_color, (PangoAttrColor *)attr);
1595               break;
1596             case PANGO_ATTR_BACKGROUND:
1597               convert_color (&appearance.bg_color, (PangoAttrColor *)attr);
1598               appearance.draw_bg = TRUE;
1599               break;
1600             case PANGO_ATTR_UNDERLINE:
1601               appearance.underline = ((PangoAttrInt *)attr)->value;
1602               break;
1603             case PANGO_ATTR_STRIKETHROUGH:
1604               appearance.strikethrough = ((PangoAttrInt *)attr)->value;
1605               break;
1606             case PANGO_ATTR_RISE:
1607               appearance.rise = ((PangoAttrInt *)attr)->value;
1608               break;
1609             default:
1610               break;
1611             }
1612           
1613           pango_attribute_destroy (attr);
1614           tmp_list = tmp_list->next;
1615         }
1616       
1617       g_slist_free (extra_attrs);
1618       
1619       insert_attr = pango_attr_font_desc_new (font_desc);
1620       insert_attr->start_index = start + offset;
1621       insert_attr->end_index = end + offset;
1622       
1623       pango_attr_list_insert (attrs, insert_attr);
1624
1625       if (language)
1626         {
1627           insert_attr = pango_attr_language_new (language);
1628           insert_attr->start_index = start + offset;
1629           insert_attr->end_index = end + offset;
1630           
1631           pango_attr_list_insert (attrs, insert_attr);
1632         }
1633
1634       add_generic_attrs (layout, &appearance, end - start,
1635                          attrs, start + offset,
1636                          size_only, TRUE);
1637       
1638       pango_font_description_free (font_desc);
1639     }
1640   while (pango_attr_iterator_next (iter));
1641
1642   pango_attr_iterator_destroy (iter);
1643 }
1644
1645 GtkTextLineDisplay *
1646 gtk_text_layout_get_line_display (GtkTextLayout *layout,
1647                                   GtkTextLine   *line,
1648                                   gboolean       size_only)
1649 {
1650   GtkTextLineDisplay *display;
1651   GtkTextLineSegment *seg;
1652   GtkTextIter iter;
1653   GtkTextAttributes *style;
1654   gchar *text;
1655   PangoAttrList *attrs;
1656   gint text_allocated, layout_byte_offset, buffer_byte_offset;
1657   PangoRectangle extents;
1658   gboolean para_values_set = FALSE;
1659   GSList *cursor_byte_offsets = NULL;
1660   GSList *cursor_segs = NULL;
1661   GSList *tmp_list1, *tmp_list2;
1662   gboolean saw_widget = FALSE;
1663   
1664   g_return_val_if_fail (line != NULL, NULL);
1665
1666   if (layout->one_display_cache)
1667     {
1668       if (line == layout->one_display_cache->line &&
1669           (size_only || !layout->one_display_cache->size_only))
1670         return layout->one_display_cache;
1671       else
1672         {
1673           GtkTextLineDisplay *tmp_display = layout->one_display_cache;
1674           layout->one_display_cache = NULL;
1675           gtk_text_layout_free_line_display (layout, tmp_display);
1676         }
1677     }
1678
1679   display = g_new0 (GtkTextLineDisplay, 1);
1680
1681   display->size_only = size_only;
1682   display->line = line;
1683   display->insert_index = -1;
1684
1685   _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
1686                                     &iter, line, 0);
1687
1688   /* Special-case optimization for completely
1689    * invisible lines; makes it faster to deal
1690    * with sequences of invisible lines.
1691    */
1692   if (totally_invisible_line (layout, line, &iter))
1693     return display;
1694
1695   /* Allocate space for flat text for buffer
1696    */
1697   text_allocated = _gtk_text_line_byte_count (line);
1698   text = g_malloc (text_allocated);
1699
1700   attrs = pango_attr_list_new ();
1701
1702   /* Iterate over segments, creating display chunks for them. */
1703   layout_byte_offset = 0; /* current length of layout text (includes preedit, does not include invisible text) */
1704   buffer_byte_offset = 0; /* position in the buffer line */
1705   seg = _gtk_text_iter_get_any_segment (&iter);
1706   while (seg != NULL)
1707     {
1708       /* Displayable segments */
1709       if (seg->type == &gtk_text_char_type ||
1710           seg->type == &gtk_text_pixbuf_type ||
1711           seg->type == &gtk_text_child_type)
1712         {
1713           _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
1714                                             &iter, line,
1715                                             buffer_byte_offset);
1716           style = get_style (layout, &iter);
1717
1718           /* We have to delay setting the paragraph values until we
1719            * hit the first pixbuf or text segment because toggles at
1720            * the beginning of the paragraph should affect the
1721            * paragraph-global values
1722            */
1723           if (!para_values_set)
1724             {
1725               set_para_values (layout, style, display);
1726               para_values_set = TRUE;
1727             }
1728
1729           /* First see if the chunk is invisible, and ignore it if so. Tk
1730            * looked at tabs, wrap mode, etc. before doing this, but
1731            * that made no sense to me, so I am just skipping the
1732            * invisible chunks
1733            */
1734           if (!style->invisible)
1735             {
1736               if (seg->type == &gtk_text_char_type)
1737                 {
1738                   /* We don't want to split segments because of marks,
1739                    * so we scan forward for more segments only
1740                    * separated from us by marks. In theory, we should
1741                    * also merge segments with identical styles, even
1742                    * if there are toggles in-between
1743                    */
1744
1745                   gint bytes = 0;
1746                   GtkTextLineSegment *prev_seg = NULL;
1747   
1748                   while (seg)
1749                     {
1750                       if (seg->type == &gtk_text_char_type)
1751                         {
1752                           memcpy (text + layout_byte_offset, seg->body.chars, seg->byte_count);
1753                           layout_byte_offset += seg->byte_count;
1754                           buffer_byte_offset += seg->byte_count;
1755                           bytes += seg->byte_count;
1756                         }
1757                       else if (seg->type == &gtk_text_right_mark_type ||
1758                                seg->type == &gtk_text_left_mark_type)
1759                         {
1760                           /* If we have preedit string, break out of this loop - we'll almost
1761                            * certainly have different attributes on the preedit string
1762                            */
1763
1764                           if (layout->preedit_len > 0 &&
1765                               _gtk_text_btree_mark_is_insert (_gtk_text_buffer_get_btree (layout->buffer),
1766                                                              seg->body.mark.obj))
1767                             break;
1768
1769                           if (seg->body.mark.visible)
1770                             {
1771                               cursor_byte_offsets = g_slist_prepend (cursor_byte_offsets, GINT_TO_POINTER (layout_byte_offset));
1772                               cursor_segs = g_slist_prepend (cursor_segs, seg);
1773                             }
1774                         }
1775                       else
1776                         break;
1777
1778                       prev_seg = seg;
1779                       seg = seg->next;
1780                     }
1781
1782                   seg = prev_seg; /* Back up one */
1783                   add_generic_attrs (layout, &style->appearance,
1784                                      bytes,
1785                                      attrs, layout_byte_offset - bytes,
1786                                      size_only, TRUE);
1787                   add_text_attrs (layout, style, bytes, attrs,
1788                                   layout_byte_offset - bytes, size_only);
1789                 }
1790               else if (seg->type == &gtk_text_pixbuf_type)
1791                 {
1792                   add_generic_attrs (layout,
1793                                      &style->appearance,
1794                                      seg->byte_count,
1795                                      attrs, layout_byte_offset,
1796                                      size_only, FALSE);
1797                   add_pixbuf_attrs (layout, display, style,
1798                                     seg, attrs, layout_byte_offset);
1799                   memcpy (text + layout_byte_offset, gtk_text_unknown_char_utf8,
1800                           seg->byte_count);
1801                   layout_byte_offset += seg->byte_count;
1802                   buffer_byte_offset += seg->byte_count;
1803                 }
1804               else if (seg->type == &gtk_text_child_type)
1805                 {
1806                   saw_widget = TRUE;
1807                   
1808                   add_generic_attrs (layout, &style->appearance,
1809                                      seg->byte_count,
1810                                      attrs, layout_byte_offset,
1811                                      size_only, FALSE);
1812                   add_child_attrs (layout, display, style,
1813                                    seg, attrs, layout_byte_offset);
1814                   memcpy (text + layout_byte_offset, gtk_text_unknown_char_utf8,
1815                           seg->byte_count);
1816                   layout_byte_offset += seg->byte_count;
1817                   buffer_byte_offset += seg->byte_count;
1818                 }
1819               else
1820                 {
1821                   /* We don't know this segment type */
1822                   g_assert_not_reached ();
1823                 }
1824               
1825             } /* if (segment was visible) */
1826           else
1827             {
1828               /* Invisible segment */
1829               buffer_byte_offset += seg->byte_count;
1830             }
1831
1832           release_style (layout, style);
1833         }
1834
1835       /* Toggles */
1836       else if (seg->type == &gtk_text_toggle_on_type ||
1837                seg->type == &gtk_text_toggle_off_type)
1838         {
1839           /* Style may have changed, drop our
1840              current cached style */
1841           invalidate_cached_style (layout);
1842         }
1843
1844       /* Marks */
1845       else if (seg->type == &gtk_text_right_mark_type ||
1846                seg->type == &gtk_text_left_mark_type)
1847         {
1848           gint cursor_offset = 0;
1849           
1850           /* At the insertion point, add the preedit string, if any */
1851           
1852           if (_gtk_text_btree_mark_is_insert (_gtk_text_buffer_get_btree (layout->buffer),
1853                                              seg->body.mark.obj))
1854             {
1855               display->insert_index = layout_byte_offset;
1856               
1857               if (layout->preedit_len > 0)
1858                 {
1859                   text_allocated += layout->preedit_len;
1860                   text = g_realloc (text, text_allocated);
1861
1862                   style = get_style (layout, &iter);
1863                   add_preedit_attrs (layout, style, attrs, layout_byte_offset, size_only);
1864                   release_style (layout, style);
1865                   
1866                   memcpy (text + layout_byte_offset, layout->preedit_string, layout->preedit_len);
1867                   layout_byte_offset += layout->preedit_len;
1868                   /* DO NOT increment the buffer byte offset for preedit */
1869                   
1870                   cursor_offset = layout->preedit_cursor - layout->preedit_len;
1871                 }
1872             }
1873           
1874
1875           /* Display visible marks */
1876
1877           if (seg->body.mark.visible)
1878             {
1879               cursor_byte_offsets = g_slist_prepend (cursor_byte_offsets,
1880                                                      GINT_TO_POINTER (layout_byte_offset + cursor_offset));
1881               cursor_segs = g_slist_prepend (cursor_segs, seg);
1882             }
1883         }
1884
1885       else
1886         g_error ("Unknown segment type: %s", seg->type->name);
1887
1888       seg = seg->next;
1889     }
1890   
1891   if (!para_values_set)
1892     {
1893       style = get_style (layout, &iter);
1894       set_para_values (layout, style, display);
1895       release_style (layout, style);
1896     }
1897   
1898   /* Pango doesn't want the trailing paragraph delimiters */
1899
1900   {
1901     /* Only one character has type G_UNICODE_PARAGRAPH_SEPARATOR in
1902      * Unicode 3.0; update this if that changes.
1903      */
1904 #define PARAGRAPH_SEPARATOR 0x2029
1905     gunichar ch = 0;
1906
1907     if (layout_byte_offset > 0)
1908       {
1909         const char *prev = g_utf8_prev_char (text + layout_byte_offset);
1910         ch = g_utf8_get_char (prev);
1911         if (ch == PARAGRAPH_SEPARATOR || ch == '\r' || ch == '\n')
1912           layout_byte_offset = prev - text; /* chop off */
1913
1914         if (ch == '\n' && layout_byte_offset > 0)
1915           {
1916             /* Possibly chop a CR as well */
1917             prev = g_utf8_prev_char (text + layout_byte_offset);
1918             if (*prev == '\r')
1919               --layout_byte_offset;
1920           }
1921       }
1922   }
1923   
1924   pango_layout_set_text (display->layout, text, layout_byte_offset);
1925   pango_layout_set_attributes (display->layout, attrs);
1926
1927   tmp_list1 = cursor_byte_offsets;
1928   tmp_list2 = cursor_segs;
1929   while (tmp_list1)
1930     {
1931       add_cursor (layout, display, tmp_list2->data,
1932                   GPOINTER_TO_INT (tmp_list1->data));
1933       tmp_list1 = tmp_list1->next;
1934       tmp_list2 = tmp_list2->next;
1935     }
1936   g_slist_free (cursor_byte_offsets);
1937   g_slist_free (cursor_segs);
1938
1939   pango_layout_get_extents (display->layout, NULL, &extents);
1940
1941   display->width = PANGO_PIXELS (extents.width) + display->left_margin + display->right_margin;
1942   display->height += PANGO_PIXELS (extents.height);
1943   
1944   /* Free this if we aren't in a loop */
1945   if (layout->wrap_loop_count == 0)
1946     invalidate_cached_style (layout);
1947
1948   g_free (text);
1949   pango_attr_list_unref (attrs);
1950
1951   layout->one_display_cache = display;
1952
1953   if (saw_widget)
1954     allocate_child_widgets (layout, display);
1955   
1956   return display;
1957 }
1958
1959 void
1960 gtk_text_layout_free_line_display (GtkTextLayout      *layout,
1961                                    GtkTextLineDisplay *display)
1962 {
1963   if (display != layout->one_display_cache)
1964     {
1965       if (display->layout)
1966         g_object_unref (G_OBJECT (display->layout));
1967
1968       if (display->cursors)
1969         {
1970           g_slist_foreach (display->cursors, (GFunc)g_free, NULL);
1971           g_slist_free (display->cursors);
1972         }
1973       g_slist_free (display->shaped_objects);
1974
1975       g_free (display);
1976     }
1977 }
1978
1979 /* Functions to convert iter <=> index for the line of a GtkTextLineDisplay
1980  * taking into account the preedit string and invisible text if necessary.
1981  */
1982 static gint
1983 line_display_iter_to_index (GtkTextLayout      *layout,
1984                             GtkTextLineDisplay *display,
1985                             const GtkTextIter  *iter)
1986 {
1987   gint index;
1988
1989   g_return_val_if_fail (_gtk_text_iter_get_text_line (iter) == display->line, 0);
1990
1991   index = gtk_text_iter_get_visible_line_index (iter);
1992   
1993   if (index >= display->insert_index)
1994     index += layout->preedit_len;
1995
1996   return index;
1997 }
1998
1999 static void
2000 line_display_index_to_iter (GtkTextLayout      *layout,
2001                             GtkTextLineDisplay *display,
2002                             GtkTextIter        *iter,
2003                             gint                index,
2004                             gint                trailing)
2005 {
2006   g_return_if_fail (!_gtk_text_line_is_last (display->line,
2007                                              _gtk_text_buffer_get_btree (layout->buffer)));
2008   
2009   if (index >= display->insert_index + layout->preedit_len)
2010     index -= layout->preedit_len;
2011   else if (index > display->insert_index)
2012     {
2013       index = display->insert_index;
2014       trailing = 0;
2015     }
2016
2017   _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
2018                                     iter, display->line, 0);
2019
2020   gtk_text_iter_set_visible_line_index (iter, index);
2021   
2022   if (_gtk_text_iter_get_text_line (iter) != display->line)
2023     {
2024       /* Clamp to end of line - really this clamping should have been done
2025        * before here, maybe in Pango, this is a broken band-aid I think
2026        */
2027       _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
2028                                         iter, display->line, 0);
2029
2030       if (!gtk_text_iter_ends_line (iter))
2031         gtk_text_iter_forward_to_line_end (iter);
2032     }
2033   
2034   gtk_text_iter_forward_chars (iter, trailing);
2035 }
2036
2037 /* FIXME: This really doesn't belong in this file ... */
2038 static GtkTextLineData*
2039 gtk_text_line_data_new (GtkTextLayout *layout,
2040                         GtkTextLine   *line)
2041 {
2042   GtkTextLineData *line_data;
2043
2044   line_data = g_new (GtkTextLineData, 1);
2045
2046   line_data->view_id = layout;
2047   line_data->next = NULL;
2048   line_data->width = 0;
2049   line_data->height = 0;
2050   line_data->valid = FALSE;
2051
2052   return line_data;
2053 }
2054
2055 static void
2056 get_line_at_y (GtkTextLayout *layout,
2057                gint           y,
2058                GtkTextLine  **line,
2059                gint          *line_top)
2060 {
2061   if (y < 0)
2062     y = 0;
2063   if (y > layout->height)
2064     y = layout->height;
2065
2066   *line = _gtk_text_btree_find_line_by_y (_gtk_text_buffer_get_btree (layout->buffer),
2067                                          layout, y, line_top);
2068   if (*line == NULL)
2069     {
2070       *line = _gtk_text_btree_get_end_iter_line (_gtk_text_buffer_get_btree (layout->buffer));
2071       
2072       if (line_top)
2073         *line_top =
2074           _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
2075                                         *line, layout);
2076     }
2077 }
2078
2079 /**
2080  * gtk_text_layout_get_line_at_y:
2081  * @layout: a #GtkLayout
2082  * @target_iter: the iterator in which the result is stored
2083  * @y: the y positition
2084  * @line_top: location to store the y coordinate of the
2085  *            top of the line. (Can by %NULL.)
2086  *
2087  * Get the iter at the beginning of the line which is displayed
2088  * at the given y.
2089  **/
2090 void
2091 gtk_text_layout_get_line_at_y (GtkTextLayout *layout,
2092                                GtkTextIter   *target_iter,
2093                                gint           y,
2094                                gint          *line_top)
2095 {
2096   GtkTextLine *line;
2097
2098   g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
2099   g_return_if_fail (target_iter != NULL);
2100
2101   get_line_at_y (layout, y, &line, line_top);
2102   _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
2103                                    target_iter, line, 0);
2104 }
2105
2106 void
2107 gtk_text_layout_get_iter_at_pixel (GtkTextLayout *layout,
2108                                    GtkTextIter *target_iter,
2109                                    gint x, gint y)
2110 {
2111   GtkTextLine *line;
2112   gint byte_index, trailing;
2113   gint line_top;
2114   GtkTextLineDisplay *display;
2115
2116   g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
2117   g_return_if_fail (target_iter != NULL);
2118
2119   /* Adjust pixels to be on-screen. This gives nice
2120      behavior if the user is dragging with a pointer grab.
2121   */
2122   if (x < 0)
2123     x = 0;
2124   if (x > layout->width)
2125     x = layout->width;
2126
2127   get_line_at_y (layout, y, &line, &line_top);
2128
2129   display = gtk_text_layout_get_line_display (layout, line, FALSE);
2130
2131   x -= display->x_offset;
2132   y -= line_top + display->top_margin;
2133
2134   /* We clamp y to the area of the actual layout so that the layouts
2135    * hit testing works OK on the space above and below the layout
2136    */
2137   y = CLAMP (y, 0, display->height - display->top_margin - display->bottom_margin - 1);
2138
2139   if (!pango_layout_xy_to_index (display->layout, x * PANGO_SCALE, y * PANGO_SCALE,
2140                                  &byte_index, &trailing))
2141     {
2142       byte_index = _gtk_text_line_byte_count (line);
2143       trailing = 0;
2144     }
2145
2146   line_display_index_to_iter (layout, display, target_iter, byte_index, trailing);
2147
2148   gtk_text_layout_free_line_display (layout, display);
2149 }
2150
2151 /**
2152  * gtk_text_layout_get_cursor_locations
2153  * @layout: a #GtkTextLayout
2154  * @iter: a #GtkTextIter
2155  * @strong_pos: location to store the strong cursor position (may be %NULL)
2156  * @weak_pos: location to store the weak cursor position (may be %NULL)
2157  *
2158  * Given an iterator within a text laout, determine the positions that of the
2159  * strong and weak cursors if the insertion point is at that
2160  * iterator. The position of each cursor is stored as a zero-width
2161  * rectangle. The strong cursor location is the location where
2162  * characters of the directionality equal to the base direction of the
2163  * paragraph are inserted.  The weak cursor location is the location
2164  * where characters of the directionality opposite to the base
2165  * direction of the paragraph are inserted.
2166  **/
2167 void
2168 gtk_text_layout_get_cursor_locations (GtkTextLayout  *layout,
2169                                       GtkTextIter    *iter,
2170                                       GdkRectangle   *strong_pos,
2171                                       GdkRectangle   *weak_pos)
2172 {
2173   GtkTextLine *line;
2174   GtkTextLineDisplay *display;
2175   gint line_top;
2176   gint index;
2177
2178   PangoRectangle pango_strong_pos;
2179   PangoRectangle pango_weak_pos;
2180
2181   g_return_if_fail (layout != NULL);
2182   g_return_if_fail (iter != NULL);
2183
2184   line = _gtk_text_iter_get_text_line (iter);
2185   display = gtk_text_layout_get_line_display (layout, line, FALSE);
2186   index = line_display_iter_to_index (layout, display, iter);
2187   
2188   line_top = _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
2189                                            line, layout);
2190   
2191   pango_layout_get_cursor_pos (display->layout, index,
2192                                strong_pos ? &pango_strong_pos : NULL,
2193                                weak_pos ? &pango_weak_pos : NULL);
2194
2195   if (strong_pos)
2196     {
2197       strong_pos->x = display->x_offset + pango_strong_pos.x / PANGO_SCALE;
2198       strong_pos->y = line_top + display->top_margin + pango_strong_pos.y / PANGO_SCALE;
2199       strong_pos->width = 0;
2200       strong_pos->height = pango_strong_pos.height / PANGO_SCALE;
2201     }
2202
2203   if (weak_pos)
2204     {
2205       weak_pos->x = display->x_offset + pango_weak_pos.x / PANGO_SCALE;
2206       weak_pos->y = line_top + display->top_margin + pango_weak_pos.y / PANGO_SCALE;
2207       weak_pos->width = 0;
2208       weak_pos->height = pango_weak_pos.height / PANGO_SCALE;
2209     }
2210
2211   gtk_text_layout_free_line_display (layout, display);
2212 }
2213
2214 /**
2215  * gtk_text_layout_get_line_yrange:
2216  * @layout: a #GtkTextLayout
2217  * @iter:   a #GtkTextIter
2218  * @y:      location to store the top of the paragraph in pixels,
2219  *          or %NULL.
2220  * @height  location to store the height of the paragraph in pixels,
2221  *          or %NULL.
2222  *
2223  * Find the range of y coordinates for the paragraph containing
2224  * the given iter.
2225  **/
2226 void
2227 gtk_text_layout_get_line_yrange (GtkTextLayout     *layout,
2228                                  const GtkTextIter *iter,
2229                                  gint              *y,
2230                                  gint              *height)
2231 {
2232   GtkTextLine *line;
2233
2234   g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
2235   g_return_if_fail (_gtk_text_iter_get_btree (iter) == _gtk_text_buffer_get_btree (layout->buffer));
2236
2237   line = _gtk_text_iter_get_text_line (iter);
2238
2239   if (y)
2240     *y = _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
2241                                        line, layout);
2242   if (height)
2243     {
2244       GtkTextLineData *line_data = _gtk_text_line_get_data (line, layout);
2245       if (line_data)
2246         *height = line_data->height;
2247       else
2248         *height = 0;
2249     }
2250 }
2251
2252 void
2253 gtk_text_layout_get_iter_location (GtkTextLayout     *layout,
2254                                    const GtkTextIter *iter,
2255                                    GdkRectangle      *rect)
2256 {
2257   PangoRectangle pango_rect;
2258   GtkTextLine *line;
2259   GtkTextBTree *tree;
2260   GtkTextLineDisplay *display;
2261   gint byte_index;
2262   gint x_offset;
2263   
2264   g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
2265   g_return_if_fail (_gtk_text_iter_get_btree (iter) == _gtk_text_buffer_get_btree (layout->buffer));
2266   g_return_if_fail (rect != NULL);
2267
2268   tree = _gtk_text_iter_get_btree (iter);
2269   line = _gtk_text_iter_get_text_line (iter);
2270
2271   display = gtk_text_layout_get_line_display (layout, line, FALSE);
2272
2273   rect->y = _gtk_text_btree_find_line_top (tree, line, layout);
2274
2275   x_offset = display->x_offset * PANGO_SCALE;
2276
2277   byte_index = gtk_text_iter_get_line_index (iter);
2278   
2279   pango_layout_index_to_pos (display->layout, byte_index, &pango_rect);
2280   
2281   rect->x = PANGO_PIXELS (x_offset + pango_rect.x);
2282   rect->y += PANGO_PIXELS (pango_rect.y) + display->top_margin;
2283   rect->width = PANGO_PIXELS (pango_rect.width);
2284   rect->height = PANGO_PIXELS (pango_rect.height);
2285
2286   gtk_text_layout_free_line_display (layout, display);
2287 }
2288
2289 /* FFIXX */
2290
2291 /* Find the iter for the logical beginning of the first display line whose
2292  * top y is >= y. If none exists, move the iter to the logical beginning
2293  * of the last line in the buffer.
2294  */
2295 static void
2296 find_display_line_below (GtkTextLayout *layout,
2297                          GtkTextIter   *iter,
2298                          gint           y)
2299 {
2300   GtkTextLine *line, *next;
2301   GtkTextLine *found_line = NULL;
2302   gint line_top;
2303   gint found_byte = 0;
2304
2305   line = _gtk_text_btree_find_line_by_y (_gtk_text_buffer_get_btree (layout->buffer),
2306                                         layout, y, &line_top);
2307   if (!line)
2308     {
2309       line =
2310         _gtk_text_btree_get_end_iter_line (_gtk_text_buffer_get_btree (layout->buffer));
2311
2312       line_top =
2313         _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer),
2314                                       line, layout);
2315     }
2316
2317   while (line && !found_line)
2318     {
2319       GtkTextLineDisplay *display = gtk_text_layout_get_line_display (layout, line, FALSE);
2320       PangoLayoutIter *layout_iter;
2321
2322       layout_iter = pango_layout_get_iter (display->layout);
2323
2324       line_top += display->top_margin;
2325
2326       do
2327         {
2328           gint first_y, last_y;
2329           PangoLayoutLine *layout_line = pango_layout_iter_get_line (layout_iter);
2330
2331           found_byte = layout_line->start_index;
2332           
2333           if (line_top >= y)
2334             {
2335               found_line = line;
2336               break;
2337             }
2338
2339           pango_layout_iter_get_line_yrange (layout_iter, &first_y, &last_y);
2340           line_top += (last_y - first_y) / PANGO_SCALE;
2341         }
2342       while (pango_layout_iter_next_line (layout_iter));
2343
2344       pango_layout_iter_free (layout_iter);
2345       
2346       line_top += display->bottom_margin;
2347       gtk_text_layout_free_line_display (layout, display);
2348
2349       next = _gtk_text_line_next_excluding_last (line);
2350       if (!next)
2351         found_line = line;
2352
2353       line = next;
2354     }
2355
2356   _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
2357                                    iter, found_line, found_byte);
2358 }
2359
2360 /* Find the iter for the logical beginning of the last display line whose
2361  * top y is >= y. If none exists, move the iter to the logical beginning
2362  * of the first line in the buffer.
2363  */
2364 static void
2365 find_display_line_above (GtkTextLayout *layout,
2366                          GtkTextIter   *iter,
2367                          gint           y)
2368 {
2369   GtkTextLine *line;
2370   GtkTextLine *found_line = NULL;
2371   gint line_top;
2372   gint found_byte = 0;
2373
2374   line = _gtk_text_btree_find_line_by_y (_gtk_text_buffer_get_btree (layout->buffer), layout, y, &line_top);
2375   if (!line)
2376     {
2377       line = _gtk_text_btree_get_end_iter_line (_gtk_text_buffer_get_btree (layout->buffer));
2378       
2379       line_top = _gtk_text_btree_find_line_top (_gtk_text_buffer_get_btree (layout->buffer), line, layout);
2380     }
2381
2382   while (line && !found_line)
2383     {
2384       GtkTextLineDisplay *display = gtk_text_layout_get_line_display (layout, line, FALSE);
2385       PangoRectangle logical_rect;
2386       PangoLayoutIter *layout_iter;
2387       gint tmp_top;
2388
2389       layout_iter = pango_layout_get_iter (display->layout);
2390       
2391       line_top -= display->top_margin + display->bottom_margin;
2392       pango_layout_iter_get_layout_extents (layout_iter, NULL, &logical_rect);
2393       line_top -= logical_rect.height / PANGO_SCALE;
2394
2395       tmp_top = line_top + display->top_margin;
2396
2397       do
2398         {
2399           gint first_y, last_y;
2400           PangoLayoutLine *layout_line = pango_layout_iter_get_line (layout_iter);
2401
2402           found_byte = layout_line->start_index;
2403
2404           pango_layout_iter_get_line_yrange (layout_iter, &first_y, &last_y);
2405           
2406           tmp_top -= (last_y - first_y) / PANGO_SCALE;
2407
2408           if (tmp_top < y)
2409             {
2410               found_line = line;
2411               goto done;
2412             }
2413         }
2414       while (pango_layout_iter_next_line (layout_iter));
2415
2416       pango_layout_iter_free (layout_iter);
2417       
2418       gtk_text_layout_free_line_display (layout, display);
2419
2420       line = _gtk_text_line_previous (line);
2421     }
2422
2423  done:
2424   
2425   if (found_line)
2426     _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
2427                                      iter, found_line, found_byte);
2428   else
2429     gtk_text_buffer_get_iter_at_offset (layout->buffer, iter, 0);
2430 }
2431
2432 /**
2433  * gtk_text_layout_clamp_iter_to_vrange:
2434  * @layout: a #GtkTextLayout
2435  * @iter:   a #GtkTextIter
2436  * @top:    the top of the range
2437  * @bottom: the bottom the range
2438  *
2439  * If the iterator is not fully in the range @top <= y < @bottom,
2440  * then, if possible, move it the minimum distance so that the
2441  * iterator in this range.
2442  *
2443  * Returns: %TRUE if the iterator was moved, otherwise %FALSE.
2444  **/
2445 gboolean
2446 gtk_text_layout_clamp_iter_to_vrange (GtkTextLayout *layout,
2447                                       GtkTextIter   *iter,
2448                                       gint           top,
2449                                       gint           bottom)
2450 {
2451   GdkRectangle iter_rect;
2452
2453   gtk_text_layout_get_iter_location (layout, iter, &iter_rect);
2454
2455   /* If the iter is at least partially above the range, put the iter
2456    * at the first fully visible line after the range.
2457    */
2458   if (iter_rect.y < top)
2459     {
2460       find_display_line_below (layout, iter, top);
2461
2462       return TRUE;
2463     }
2464   /* Otherwise, if the iter is at least partially below the screen, put the
2465    * iter on the last logical position of the last completely visible
2466    * line on screen
2467    */
2468   else if (iter_rect.y + iter_rect.height > bottom)
2469     {
2470       find_display_line_above (layout, iter, bottom);
2471
2472       return TRUE;
2473     }
2474   else
2475     return FALSE;
2476 }
2477
2478 /**
2479  * gtk_text_layout_move_iter_to_previous_line:
2480  * @layout: a #GtkLayout
2481  * @iter:   a #GtkTextIter
2482  *
2483  * Move the iterator to the beginning of the previous line. The lines
2484  * of a wrapped paragraph are treated as distinct for this operation.
2485  **/
2486 gboolean
2487 gtk_text_layout_move_iter_to_previous_line (GtkTextLayout *layout,
2488                                             GtkTextIter   *iter)
2489 {
2490   GtkTextLine *line;
2491   GtkTextLineDisplay *display;
2492   gint line_byte;
2493   GSList *tmp_list;
2494   PangoLayoutLine *layout_line;
2495   GtkTextIter orig;
2496   gboolean update_byte = FALSE;
2497   
2498   g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), FALSE);
2499   g_return_val_if_fail (iter != NULL, FALSE);
2500
2501   orig = *iter;
2502
2503
2504   line = _gtk_text_iter_get_text_line (iter);
2505   display = gtk_text_layout_get_line_display (layout, line, FALSE);
2506   line_byte = line_display_iter_to_index (layout, display, iter);
2507
2508   /* If display->height == 0 then the line is invisible, so don't
2509    * move onto it.
2510    */
2511   while (display->height == 0)
2512     {
2513       GtkTextLine *prev_line;
2514
2515       prev_line = _gtk_text_line_previous (line);
2516
2517       if (prev_line == NULL)
2518         {
2519           line_display_index_to_iter (layout, display, iter, 0, 0);
2520           goto out;
2521         }
2522
2523       gtk_text_layout_free_line_display (layout, display);
2524
2525       line = prev_line;
2526       display = gtk_text_layout_get_line_display (layout, prev_line, FALSE);
2527       update_byte = TRUE;
2528     }
2529   
2530   tmp_list = pango_layout_get_lines (display->layout);
2531   layout_line = tmp_list->data;
2532
2533   if (update_byte)
2534     {
2535       line_byte = layout_line->start_index + layout_line->length;
2536     }
2537   
2538   if (line_byte < layout_line->length || !tmp_list->next) /* first line of paragraph */
2539     {
2540       GtkTextLine *prev_line;
2541
2542       prev_line = _gtk_text_line_previous (line);
2543       while (prev_line)
2544         {
2545           gtk_text_layout_free_line_display (layout, display);
2546
2547           display = gtk_text_layout_get_line_display (layout, prev_line, FALSE);
2548
2549           if (display->height > 0)
2550             {
2551               tmp_list = g_slist_last (pango_layout_get_lines (display->layout));
2552               layout_line = tmp_list->data;
2553
2554               line_display_index_to_iter (layout, display, iter,
2555                                           layout_line->start_index + layout_line->length, 0);
2556               break;
2557             }
2558
2559           prev_line = _gtk_text_line_previous (prev_line);
2560         }
2561
2562       if (prev_line == NULL)
2563         line_display_index_to_iter (layout, display, iter, 0, 0);
2564     }
2565   else
2566     {
2567       gint prev_offset = layout_line->start_index;
2568
2569       tmp_list = tmp_list->next;
2570       while (tmp_list)
2571         {
2572           layout_line = tmp_list->data;
2573
2574           if (line_byte < layout_line->start_index + layout_line->length ||
2575               !tmp_list->next)
2576             {
2577               line_display_index_to_iter (layout, display, iter, prev_offset, 0);
2578               break;
2579             }
2580
2581           prev_offset = layout_line->start_index;
2582           tmp_list = tmp_list->next;
2583         }
2584     }
2585
2586  out:
2587   
2588   gtk_text_layout_free_line_display (layout, display);
2589
2590   return
2591     !gtk_text_iter_equal (iter, &orig) &&
2592     !gtk_text_iter_is_end (iter);
2593 }
2594
2595 /**
2596  * gtk_text_layout_move_iter_to_next_line:
2597  * @layout: a #GtkLayout
2598  * @iter:   a #GtkTextIter
2599  *
2600  * Move the iterator to the beginning of the next line. The
2601  * lines of a wrapped paragraph are treated as distinct for
2602  * this operation.
2603  **/
2604 gboolean
2605 gtk_text_layout_move_iter_to_next_line (GtkTextLayout *layout,
2606                                         GtkTextIter   *iter)
2607 {
2608   GtkTextLine *line;
2609   GtkTextLineDisplay *display;
2610   gint line_byte;
2611   GtkTextIter orig;
2612   gboolean found = FALSE;
2613   gboolean found_after = FALSE;
2614   gboolean first = TRUE;
2615
2616   g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), FALSE);
2617   g_return_val_if_fail (iter != NULL, FALSE);
2618
2619   orig = *iter;
2620   
2621   line = _gtk_text_iter_get_text_line (iter);
2622
2623   while (line && !found_after)
2624     {
2625       GSList *tmp_list;
2626
2627       display = gtk_text_layout_get_line_display (layout, line, FALSE);
2628
2629       if (display->height == 0)
2630         goto next;
2631       
2632       if (first)
2633         {
2634           line_byte = line_display_iter_to_index (layout, display, iter);
2635           first = FALSE;
2636         }
2637       else
2638         line_byte = 0;
2639         
2640       tmp_list = pango_layout_get_lines (display->layout);
2641       while (tmp_list && !found_after)
2642         {
2643           PangoLayoutLine *layout_line = tmp_list->data;
2644
2645           if (found)
2646             {
2647               line_display_index_to_iter (layout, display, iter,
2648                                           layout_line->start_index, 0);
2649               found_after = TRUE;
2650             }
2651           else if (line_byte < layout_line->start_index + layout_line->length || !tmp_list->next)
2652             found = TRUE;
2653           
2654           tmp_list = tmp_list->next;
2655         }
2656
2657     next:
2658       
2659       gtk_text_layout_free_line_display (layout, display);
2660
2661       line = _gtk_text_line_next_excluding_last (line);
2662     }
2663
2664   return
2665     !gtk_text_iter_equal (iter, &orig) &&
2666     !gtk_text_iter_is_end (iter);
2667 }
2668
2669 /**
2670  * gtk_text_layout_move_iter_to_line_end:
2671  * @layout: a #GtkTextLayout
2672  * @direction: if negative, move to beginning of line, otherwise
2673                move to end of line.
2674  *
2675  * Move to the beginning or end of a display line.
2676  **/
2677 gboolean
2678 gtk_text_layout_move_iter_to_line_end (GtkTextLayout *layout,
2679                                        GtkTextIter   *iter,
2680                                        gint           direction)
2681 {
2682   GtkTextLine *line;
2683   GtkTextLineDisplay *display;
2684   gint line_byte;
2685   GSList *tmp_list;
2686   GtkTextIter orig;
2687   
2688   g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), FALSE);
2689   g_return_val_if_fail (iter != NULL, FALSE);
2690
2691   orig = *iter;
2692   
2693   line = _gtk_text_iter_get_text_line (iter);
2694   display = gtk_text_layout_get_line_display (layout, line, FALSE);
2695   line_byte = line_display_iter_to_index (layout, display, iter);
2696
2697   tmp_list = pango_layout_get_lines (display->layout);
2698   while (tmp_list)
2699     {
2700       PangoLayoutLine *layout_line = tmp_list->data;
2701
2702       if (line_byte < layout_line->start_index + layout_line->length || !tmp_list->next)
2703         {
2704           line_display_index_to_iter (layout, display, iter,
2705                                       direction < 0 ? layout_line->start_index : layout_line->start_index + layout_line->length,
2706                                       0);
2707
2708           /* FIXME: As a bad hack, we move back one position when we
2709            * are inside a paragraph to avoid going to next line on a
2710            * forced break not at whitespace. Real fix is to keep track
2711            * of whether marks are at leading or trailing edge?  */
2712           if (direction > 0 && layout_line->length > 0 && !gtk_text_iter_ends_line (iter))
2713             gtk_text_iter_backward_char (iter);
2714
2715           break;
2716         }
2717       
2718       tmp_list = tmp_list->next;
2719     }
2720
2721   gtk_text_layout_free_line_display (layout, display);
2722
2723   return
2724     !gtk_text_iter_equal (iter, &orig) &&
2725     !gtk_text_iter_is_end (iter);
2726 }
2727
2728
2729 /**
2730  * gtk_text_layout_iter_starts_line:
2731  * @layout: a #GtkTextLayout
2732  * @iter: iterator to test
2733  *
2734  * Tests whether an iterator is at the start of a display line.
2735  **/
2736 gboolean
2737 gtk_text_layout_iter_starts_line (GtkTextLayout       *layout,
2738                                   const GtkTextIter   *iter)
2739 {
2740   GtkTextLine *line;
2741   GtkTextLineDisplay *display;
2742   gint line_byte;
2743   GSList *tmp_list;
2744   
2745   g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), FALSE);
2746   g_return_val_if_fail (iter != NULL, FALSE);
2747
2748   line = _gtk_text_iter_get_text_line (iter);
2749   display = gtk_text_layout_get_line_display (layout, line, FALSE);
2750   line_byte = line_display_iter_to_index (layout, display, iter);
2751
2752   tmp_list = pango_layout_get_lines (display->layout);
2753   while (tmp_list)
2754     {
2755       PangoLayoutLine *layout_line = tmp_list->data;
2756
2757       if (line_byte < layout_line->start_index + layout_line->length ||
2758           !tmp_list->next)
2759         {
2760           /* We're located on this line or the para delimiters before
2761            * it
2762            */
2763           gtk_text_layout_free_line_display (layout, display);
2764           
2765           if (line_byte == layout_line->start_index)
2766             return TRUE;
2767           else
2768             return FALSE;
2769         }
2770       
2771       tmp_list = tmp_list->next;
2772     }
2773
2774   g_assert_not_reached ();
2775   return FALSE;
2776 }
2777
2778 void
2779 gtk_text_layout_get_iter_at_line (GtkTextLayout  *layout,
2780                                   GtkTextIter    *iter,
2781                                   GtkTextLine    *line,
2782                                   gint            byte_offset)
2783 {
2784   _gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
2785                                     iter, line, byte_offset);
2786 }
2787
2788
2789 /**
2790  * gtk_text_layout_move_iter_to_x:
2791  * @layout: a #GtkTextLayout
2792  * @iter:   a #GtkTextIter
2793  * @x:      X coordinate
2794  *
2795  * Keeping the iterator on the same line of the layout, move it to the
2796  * specified X coordinate. The lines of a wrapped paragraph are
2797  * treated as distinct for this operation.
2798  **/
2799 void
2800 gtk_text_layout_move_iter_to_x (GtkTextLayout *layout,
2801                                 GtkTextIter   *iter,
2802                                 gint           x)
2803 {
2804   GtkTextLine *line;
2805   GtkTextLineDisplay *display;
2806   gint line_byte;
2807   PangoLayoutIter *layout_iter;
2808   
2809   g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
2810   g_return_if_fail (iter != NULL);
2811
2812   line = _gtk_text_iter_get_text_line (iter);
2813
2814   display = gtk_text_layout_get_line_display (layout, line, FALSE);
2815   line_byte = line_display_iter_to_index (layout, display, iter);
2816
2817   layout_iter = pango_layout_get_iter (display->layout);
2818
2819   do
2820     {
2821       PangoLayoutLine *layout_line = pango_layout_iter_get_line (layout_iter);
2822
2823       if (line_byte < layout_line->start_index + layout_line->length ||
2824           pango_layout_iter_at_last_line (layout_iter))
2825         {
2826           PangoRectangle logical_rect;
2827           gint byte_index, trailing;
2828           gint x_offset = display->x_offset * PANGO_SCALE;
2829
2830           pango_layout_iter_get_line_extents (layout_iter, NULL, &logical_rect);
2831
2832           pango_layout_line_x_to_index (layout_line,
2833                                         x * PANGO_SCALE - x_offset - logical_rect.x,
2834                                         &byte_index, &trailing);
2835
2836           line_display_index_to_iter (layout, display, iter, byte_index, trailing);
2837
2838           break;
2839         }
2840     }
2841   while (pango_layout_iter_next_line (layout_iter));
2842
2843   pango_layout_iter_free (layout_iter);
2844   
2845   gtk_text_layout_free_line_display (layout, display);
2846 }
2847
2848 /**
2849  * gtk_text_layout_move_iter_visually:
2850  * @layout:  a #GtkTextLayout
2851  * @iter:    a #GtkTextIter
2852  * @count:   number of characters to move (negative moves left, positive moves right)
2853  *
2854  * Move the iterator a given number of characters visually, treating
2855  * it as the strong cursor position. If @count is positive, then the
2856  * new strong cursor position will be @count positions to the right of
2857  * the old cursor position. If @count is negative then the new strong
2858  * cursor position will be @count positions to the left of the old
2859  * cursor position.
2860  *
2861  * In the presence of bidirection text, the correspondence
2862  * between logical and visual order will depend on the direction
2863  * of the current run, and there may be jumps when the cursor
2864  * is moved off of the end of a run.
2865  **/
2866
2867 gboolean
2868 gtk_text_layout_move_iter_visually (GtkTextLayout *layout,
2869                                     GtkTextIter   *iter,
2870                                     gint           count)
2871 {
2872   GtkTextLineDisplay *display = NULL;
2873   GtkTextIter orig;
2874   
2875   g_return_val_if_fail (layout != NULL, FALSE);
2876   g_return_val_if_fail (iter != NULL, FALSE);
2877
2878   orig = *iter;
2879   
2880   while (count != 0)
2881     {
2882       GtkTextLine *line = _gtk_text_iter_get_text_line (iter);
2883       gint line_byte;
2884       gint extra_back = 0;
2885       gboolean strong;
2886
2887       int byte_count = _gtk_text_line_byte_count (line);
2888
2889       int new_index;
2890       int new_trailing;
2891
2892       if (!display)
2893         display = gtk_text_layout_get_line_display (layout, line, FALSE);
2894
2895       if (layout->cursor_direction == GTK_TEXT_DIR_NONE)
2896         strong = TRUE;
2897       else
2898         strong = display->direction == layout->cursor_direction;
2899
2900       line_byte = line_display_iter_to_index (layout, display, iter);
2901
2902       if (count > 0)
2903         {
2904           pango_layout_move_cursor_visually (display->layout, strong, line_byte, 0, 1, &new_index, &new_trailing);
2905           count--;
2906         }
2907       else
2908         {
2909           pango_layout_move_cursor_visually (display->layout, strong, line_byte, 0, -1, &new_index, &new_trailing);
2910           count++;
2911         }
2912
2913       /* We need to handle the preedit string specially. Well, we don't really need to
2914        * handle it specially, since hopefully calling gtk_im_context_reset() will
2915        * remove the preedit string; but if we start off in front of the preedit
2916        * string (logically) and end up in or on the back edge of the preedit string,
2917        * we should move the iter one place farther.
2918        */
2919       if (layout->preedit_len > 0 && display->insert_index >= 0)
2920         {
2921           if (line_byte == display->insert_index + layout->preedit_len &&
2922               new_index < display->insert_index + layout->preedit_len)
2923             {
2924               line_byte = display->insert_index;
2925               extra_back = 1;
2926             }
2927         }
2928       
2929       if (new_index < 0 || (new_index == 0 && extra_back))
2930         {
2931           line = _gtk_text_line_previous (line);
2932
2933           if (!line)
2934             goto done;
2935           
2936           gtk_text_layout_free_line_display (layout, display);
2937           display = gtk_text_layout_get_line_display (layout, line, FALSE);
2938           new_index = _gtk_text_line_byte_count (line);
2939         }
2940       else if (new_index > byte_count)
2941         {
2942           line = _gtk_text_line_next_excluding_last (line);
2943           if (!line)
2944             goto done;
2945
2946           gtk_text_layout_free_line_display (layout, display);
2947           display = gtk_text_layout_get_line_display (layout, line, FALSE);
2948           new_index = 0;
2949         }
2950       
2951        line_display_index_to_iter (layout, display, iter, new_index, new_trailing);
2952        if (extra_back)
2953          gtk_text_iter_backward_char (iter);
2954     }
2955
2956   gtk_text_layout_free_line_display (layout, display);
2957
2958  done:
2959   
2960   return
2961     !gtk_text_iter_equal (iter, &orig) &&
2962     !gtk_text_iter_is_end (iter);
2963 }
2964
2965 void
2966 gtk_text_layout_spew (GtkTextLayout *layout)
2967 {
2968 #if 0
2969   GtkTextDisplayLine *iter;
2970   guint wrapped = 0;
2971   guint paragraphs = 0;
2972   GtkTextLine *last_line = NULL;
2973
2974   iter = layout->line_list;
2975   while (iter != NULL)
2976     {
2977       if (iter->line != last_line)
2978         {
2979           printf ("%5u  paragraph (%p)\n", paragraphs, iter->line);
2980           ++paragraphs;
2981           last_line = iter->line;
2982         }
2983
2984       printf ("  %5u  y: %d len: %d start: %d bytes: %d\n",
2985               wrapped, iter->y, iter->length, iter->byte_offset,
2986               iter->byte_count);
2987
2988       ++wrapped;
2989       iter = iter->next;
2990     }
2991
2992   printf ("Layout %s recompute\n",
2993           layout->need_recompute ? "needs" : "doesn't need");
2994
2995   printf ("Layout pars: %u lines: %u size: %d x %d Screen width: %d\n",
2996           paragraphs, wrapped, layout->width,
2997           layout->height, layout->screen_width);
2998 #endif
2999 }