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