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