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