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