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