gtktextlayout.c 59.9 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/* gtktextlayout.c - calculate the layout of the text
 *
 * Copyright (c) 1992-1994 The Regents of the University of California.
 * Copyright (c) 1994-1997 Sun Microsystems, Inc.
 * Copyright (c) 2000 Red Hat, Inc.
 * Tk->Gtk port by Havoc Pennington
 * Pango support by Owen Taylor
 *
 *
 * This software is copyrighted by the Regents of the University of
 * California, Sun Microsystems, Inc., and other parties.  The
 * following terms apply to all files associated with the software
 * unless explicitly disclaimed in individual files.
 * 
 * The authors hereby grant permission to use, copy, modify,
 * distribute, and license this software and its documentation for any
 * purpose, provided that existing copyright notices are retained in
 * all copies and that this notice is included verbatim in any
 * distributions. No written agreement, license, or royalty fee is
 * required for any of the authorized uses.  Modifications to this
 * software may be copyrighted by their authors and need not follow
 * the licensing terms described here, provided that the new terms are
 * clearly indicated on the first page of each file where they apply.
 * 
 * IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY
 * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
 * DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION,
 * OR ANY DERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE BEEN ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 * THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND
 * NON-INFRINGEMENT.  THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS,
 * AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO PROVIDE
 * MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 *
 * GOVERNMENT USE: If you are acquiring this software on behalf of the
 * U.S. government, the Government shall have only "Restricted Rights"
 * in the software and related documentation as defined in the Federal
 * Acquisition Regulations (FARs) in Clause 52.227.19 (c) (2).  If you
 * are acquiring the software on behalf of the Department of Defense,
 * the software shall be classified as "Commercial Computer Software"
 * and the Government shall have only "Restricted Rights" as defined
 * in Clause 252.227-7013 (c) (1) of DFARs.  Notwithstanding the
 * foregoing, the authors grant the U.S. Government and others acting
 * in its behalf permission to use and distribute the software in
 * accordance with the terms specified in this license.
 * 
 */

#include "gtksignal.h"
#include "gtktextlayout.h"
#include "gtktextbtree.h"
#include "gtktextiterprivate.h"

#include <stdlib.h>
58
#include <string.h>
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192

static GtkTextLineData    *gtk_text_line_data_new                 (GtkTextLayout     *layout,
								   GtkTextLine       *line);

static GtkTextLineData *gtk_text_layout_real_wrap (GtkTextLayout *layout,
                                                     GtkTextLine *line,
                                                     /* may be NULL */
                                                     GtkTextLineData *line_data);

static void gtk_text_layout_real_get_log_attrs (GtkTextLayout  *layout,
						GtkTextLine    *line,
						PangoLogAttr  **attrs,
						gint           *n_attrs);

static void gtk_text_layout_invalidated     (GtkTextLayout     *layout);

static void gtk_text_layout_real_invalidate     (GtkTextLayout     *layout,
						 const GtkTextIter *start,
						 const GtkTextIter *end);
static void gtk_text_layout_real_free_line_data (GtkTextLayout     *layout,
						 GtkTextLine       *line,
						 GtkTextLineData   *line_data);

static void gtk_text_layout_invalidate_all (GtkTextLayout *layout);

static PangoAttribute *gtk_text_attr_appearance_new (const GtkTextAppearance *appearance);

enum {
  INVALIDATED,
  CHANGED,
  LAST_SIGNAL
};

enum {
  ARG_0,
  LAST_ARG
};

static void gtk_text_layout_init (GtkTextLayout *text_layout);
static void gtk_text_layout_class_init (GtkTextLayoutClass *klass);
static void gtk_text_layout_destroy (GtkObject *object);
static void gtk_text_layout_finalize (GObject *object);

void gtk_marshal_NONE__INT_INT_INT_INT (GtkObject  *object,
                                        GtkSignalFunc func,
                                        gpointer func_data,
                                        GtkArg  *args);

static GtkObjectClass *parent_class = NULL;
static guint signals[LAST_SIGNAL] = { 0 };

PangoAttrType gtk_text_attr_appearance_type = 0;

GtkType
gtk_text_layout_get_type (void)
{
  static GtkType our_type = 0;

  if (our_type == 0)
    {
      static const GtkTypeInfo our_info =
      {
        "GtkTextLayout",
        sizeof (GtkTextLayout),
        sizeof (GtkTextLayoutClass),
        (GtkClassInitFunc) gtk_text_layout_class_init,
        (GtkObjectInitFunc) gtk_text_layout_init,
        /* reserved_1 */ NULL,
        /* reserved_2 */ NULL,
        (GtkClassInitFunc) NULL
      };

    our_type = gtk_type_unique (GTK_TYPE_OBJECT, &our_info);
  }

  return our_type;
}

static void
gtk_text_layout_class_init (GtkTextLayoutClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
  GtkObjectClass *object_class = GTK_OBJECT_CLASS (klass);

  parent_class = gtk_type_class (GTK_TYPE_OBJECT);

  signals[INVALIDATED] =
    gtk_signal_new ("invalidated",
                    GTK_RUN_LAST,
                    GTK_CLASS_TYPE (object_class),
                    GTK_SIGNAL_OFFSET (GtkTextLayoutClass, invalidated),
                    gtk_marshal_NONE__NONE,
                    GTK_TYPE_NONE,
                    0);

  signals[CHANGED] =
    gtk_signal_new ("changed",
                    GTK_RUN_LAST,
                    GTK_CLASS_TYPE (object_class),
                    GTK_SIGNAL_OFFSET (GtkTextLayoutClass, changed),
                    gtk_marshal_NONE__INT_INT_INT,
                    GTK_TYPE_NONE,
                    3,
                    GTK_TYPE_INT,
                    GTK_TYPE_INT,
                    GTK_TYPE_INT);

  gtk_object_class_add_signals (object_class, signals, LAST_SIGNAL);
  
  object_class->destroy = gtk_text_layout_destroy;
  gobject_class->finalize = gtk_text_layout_finalize;

  klass->wrap = gtk_text_layout_real_wrap;
  klass->get_log_attrs = gtk_text_layout_real_get_log_attrs;
  klass->invalidate = gtk_text_layout_real_invalidate;
  klass->free_line_data = gtk_text_layout_real_free_line_data;
}

void
gtk_text_layout_init (GtkTextLayout *text_layout)
{
}

GtkTextLayout*
gtk_text_layout_new (void)
{
  return GTK_TEXT_LAYOUT (gtk_type_new (gtk_text_layout_get_type ()));
}

static void
free_style_cache (GtkTextLayout *text_layout)
{
  if (text_layout->one_style_cache)
    {
193
      gtk_text_style_values_unref (text_layout->one_style_cache);
194
195
196
197
198
199
200
201
202
203
204
205
206
207
      text_layout->one_style_cache = NULL;
    }
}

static void
gtk_text_layout_destroy (GtkObject *object)
{
  GtkTextLayout *layout;

  layout = GTK_TEXT_LAYOUT (object);

  gtk_text_layout_set_buffer (layout, NULL);  

  if (layout->default_style)
208
    gtk_text_style_values_unref (layout->default_style);
209
210
211
212
  layout->default_style = NULL;

  if (layout->ltr_context)
    {
213
      g_object_unref (G_OBJECT (layout->ltr_context));
214
215
216
217
      layout->ltr_context = NULL;
    }
  if (layout->rtl_context)
    {
218
      g_object_unref (G_OBJECT (layout->rtl_context));
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
      layout->rtl_context = NULL;
    }
  
  (* parent_class->destroy) (object);
}

static void
gtk_text_layout_finalize (GObject *object)
{
  GtkTextLayout *text_layout;

  text_layout = GTK_TEXT_LAYOUT (object);

  (* G_OBJECT_CLASS (parent_class)->finalize) (object);
}

void
gtk_text_layout_set_buffer (GtkTextLayout *layout,
                            GtkTextBuffer *buffer)
{
  g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
  g_return_if_fail (buffer == NULL || GTK_IS_TEXT_BUFFER (buffer));
  
  if (layout->buffer == buffer)
    return;

  free_style_cache (layout);
  
  if (layout->buffer)
    {
      gtk_text_btree_remove_view (layout->buffer->tree, layout);
      
      gtk_object_unref (GTK_OBJECT (layout->buffer));
      layout->buffer = NULL;
    }

  if (buffer)
    {
      layout->buffer = buffer;

      gtk_object_sink (GTK_OBJECT (buffer));
      gtk_object_ref (GTK_OBJECT (buffer));

      gtk_text_btree_add_view (buffer->tree, layout);
    }
}

void
gtk_text_layout_default_style_changed (GtkTextLayout *layout)
{
  g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
  
  gtk_text_layout_invalidate_all (layout);
}

void
gtk_text_layout_set_default_style (GtkTextLayout *layout,
                                   GtkTextStyleValues *values)
{
  g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
  g_return_if_fail (values != NULL);

  if (values == layout->default_style)
    return;

284
  gtk_text_style_values_ref (values);
285
286
  
  if (layout->default_style)
287
    gtk_text_style_values_unref (layout->default_style);
288
289
290
291
292
293
294
295
296
297
298
299
300
301

  layout->default_style = values;

  gtk_text_layout_default_style_changed (layout);
}

void
gtk_text_layout_set_contexts (GtkTextLayout *layout,
			      PangoContext  *ltr_context,
			      PangoContext  *rtl_context)
{
  g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));

  if (layout->ltr_context)
302
    g_object_unref (G_OBJECT (ltr_context));
303
304

  layout->ltr_context = ltr_context;
305
  g_object_ref (G_OBJECT (ltr_context));
306
307
  
  if (layout->rtl_context)
308
    g_object_unref (G_OBJECT (rtl_context));
309
310

  layout->rtl_context = rtl_context;
311
  g_object_ref (G_OBJECT (rtl_context));
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
  
  gtk_text_layout_invalidate_all (layout);
}

void
gtk_text_layout_set_screen_width (GtkTextLayout *layout, gint width)
{
  g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
  g_return_if_fail (width >= 0);
  g_return_if_fail (layout->wrap_loop_count == 0);
  
  if (layout->screen_width == width)
    return;
  
  layout->screen_width = width;
  
  gtk_text_layout_invalidate_all (layout);
}

void
gtk_text_layout_get_size (GtkTextLayout *layout,
                          gint *width,
                          gint *height)
{
  gint w, h;
  
  g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
  
  gtk_text_btree_get_view_size (layout->buffer->tree, layout,
                                &w, &h);

  layout->width = w;
  layout->height = h;
  
  if (width)
    *width = layout->width;

  if (height)
    *height = layout->height;
}

static void
gtk_text_layout_invalidated (GtkTextLayout *layout)
{
  gtk_signal_emit (GTK_OBJECT (layout), signals[INVALIDATED]);
}

void
gtk_text_layout_changed (GtkTextLayout *layout,
			 gint           y,
			 gint           old_height,
			 gint           new_height)
{
  gtk_signal_emit (GTK_OBJECT (layout), signals[CHANGED], y, old_height, new_height);
}

void
gtk_text_layout_free_line_data (GtkTextLayout     *layout,
				GtkTextLine       *line,
				GtkTextLineData   *line_data)
{
  (* GTK_TEXT_LAYOUT_GET_CLASS (layout)->free_line_data)
    (layout, line, line_data);
}

void
gtk_text_layout_invalidate (GtkTextLayout *layout,
                            const GtkTextIter *start_index,
                            const GtkTextIter *end_index)
{
  (* GTK_TEXT_LAYOUT_GET_CLASS (layout)->invalidate)
    (layout, start_index, end_index);
}

GtkTextLineData*
gtk_text_layout_wrap (GtkTextLayout *layout,
                       GtkTextLine  *line,
                       /* may be NULL */
                       GtkTextLineData *line_data)
{
  return (* GTK_TEXT_LAYOUT_GET_CLASS (layout)->wrap) (layout, line, line_data);
}

void
gtk_text_layout_get_log_attrs (GtkTextLayout  *layout,
			       GtkTextLine    *line,
			       PangoLogAttr  **attrs,
			       gint           *n_attrs)
{
  (* GTK_TEXT_LAYOUT_GET_CLASS (layout)->get_log_attrs)
    (layout, line, attrs, n_attrs);
}

GSList*
gtk_text_layout_get_lines (GtkTextLayout *layout,
                           /* [top_y, bottom_y) */
                           gint top_y, 
                           gint bottom_y,
                           gint *first_line_y)
{
  GtkTextLine *first_btree_line;
  GtkTextLine *last_btree_line;
  GtkTextLine *line;
  GSList *retval;
  
  g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), NULL);
  g_return_val_if_fail (bottom_y > top_y, NULL);

  retval = NULL;
  
  first_btree_line = gtk_text_btree_find_line_by_y (layout->buffer->tree, layout, top_y, first_line_y);
  if (first_btree_line == NULL)
    {
      g_assert (top_y > 0);
      /* off the bottom */
      return NULL;
    }
  
  /* -1 since bottom_y is one past */
  last_btree_line = gtk_text_btree_find_line_by_y (layout->buffer->tree, layout, bottom_y - 1, NULL);

  if (!last_btree_line)
    last_btree_line = gtk_text_btree_get_line (layout->buffer->tree,
                                               gtk_text_btree_line_count (layout->buffer->tree) - 1, NULL);

  {
    GtkTextLineData *ld = gtk_text_line_get_data (last_btree_line, layout);
    if (ld->height == 0)
      G_BREAKPOINT();
  }
  
  g_assert (last_btree_line != NULL);

  line = first_btree_line;
  while (TRUE)
    {
      retval = g_slist_prepend (retval, line);

      if (line == last_btree_line)
        break;
      
      line = gtk_text_line_next (line);
    }
  
  retval = g_slist_reverse (retval);

  return retval;
}

static void
invalidate_cached_style (GtkTextLayout *layout)
{
  free_style_cache (layout);
}

/* These should be called around a loop which wraps a CONTIGUOUS bunch
 * of display lines. If the lines aren't contiguous you can't call
 * these.
 */
void
gtk_text_layout_wrap_loop_start (GtkTextLayout *layout)
{
  g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
  g_return_if_fail (layout->one_style_cache == NULL);
  
  layout->wrap_loop_count += 1;
}

void
gtk_text_layout_wrap_loop_end (GtkTextLayout *layout)
{
  g_return_if_fail (layout->wrap_loop_count > 0);
  
  layout->wrap_loop_count -= 1;

  if (layout->wrap_loop_count == 0)
    {
      /* We cache a some stuff if we're iterating over some lines wrapping
       * them. This cleans it up.
       */
      /* Nuke our cached style */
      invalidate_cached_style (layout);
      g_assert (layout->one_style_cache == NULL);
    }
}

static void
gtk_text_layout_invalidate_all (GtkTextLayout *layout)
{
  GtkTextIter start;
  GtkTextIter end;
  
  if (layout->buffer == NULL)
    return;

  gtk_text_buffer_get_bounds (layout->buffer, &start, &end);

  gtk_text_layout_invalidate (layout, &start, &end);
}

/* FIXME: This is now completely generic, and we could probably be
 * moved into gtktextbtree.c.
 */
static void
gtk_text_layout_real_invalidate (GtkTextLayout *layout,
                                 const GtkTextIter *start,
                                 const GtkTextIter *end)
{
  GtkTextLine *line;
  GtkTextLine *last_line;
  
  g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
  g_return_if_fail (layout->wrap_loop_count == 0);

#if 0
  gtk_text_view_index_spew (start_index, "invalidate start");
  gtk_text_view_index_spew (end_index, "invalidate end");
#endif
  
  last_line = gtk_text_iter_get_line (end);
  line = gtk_text_iter_get_line (start);

  while (TRUE)
    {
      GtkTextLineData *line_data = gtk_text_line_get_data (line, layout);

      if (line_data &&
	  (line != last_line || !gtk_text_iter_starts_line (end)))
	{
	  if (layout->one_display_cache &&
	      line == layout->one_display_cache->line)
	    {
	      GtkTextLineDisplay *tmp_display = layout->one_display_cache;
	      layout->one_display_cache = NULL;
	      gtk_text_layout_free_line_display (layout, tmp_display);
	    }
	  gtk_text_line_invalidate_wrap (line, line_data);
	}
      
      if (line == last_line)
        break;
      
      line = gtk_text_line_next (line);
    }
  
  gtk_text_layout_invalidated (layout);
}

static void
gtk_text_layout_real_free_line_data (GtkTextLayout     *layout,
				     GtkTextLine       *line,
				     GtkTextLineData   *line_data)
{
  if (layout->one_display_cache && line == layout->one_display_cache->line)
    {
      GtkTextLineDisplay *tmp_display = layout->one_display_cache;
      layout->one_display_cache = NULL;
      gtk_text_layout_free_line_display (layout, tmp_display);
    }
  
  g_free (line_data);
}



/**
 * gtk_text_layout_is_valid:
 * @layout: a #GtkTextLayout
 * 
 * Check if there are any invalid regions in a #GtkTextLayout's buffer
 * 
 * Return value: #TRUE if any invalid regions were found
 **/
gboolean
gtk_text_layout_is_valid (GtkTextLayout *layout)
{
  g_return_val_if_fail (layout != NULL, FALSE);
  g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), FALSE);
  
  return gtk_text_btree_is_valid (layout->buffer->tree, layout);
}

/**
 * gtk_text_layout_validate_yrange:
 * @layout: a #GtkTextLayout
 * @anchor: iter pointing into a line that will be used as the
 *          coordinate origin
 * @y0: offset from the top of the line pointed to by @anchor at
 *      which to begin validation. (The offset here is in pixels
 *      after validation.)
 * @y1: offset from the top of the line pointed to by @anchor at
 *      which to end validation. (The offset here is in pixels
 *      after validation.)
 * 
 * Ensure that a region of a #GtkTextLayout is valid. The ::changed 
 * signal will be emitted if any lines are validated.
 **/
void
gtk_text_layout_validate_yrange (GtkTextLayout *layout,
				 GtkTextIter   *anchor,
				 gint           y0,
				 gint           y1)
{
  GtkTextLine *line;
  GtkTextLine *first_line = NULL;
  GtkTextLine *last_line = NULL;
  gint seen;
  gint delta_height = 0;
  gint first_line_y = 0;	/* Quiet GCC */
  gint last_line_y = 0;		/* Quiet GCC */
  
  g_return_if_fail (layout != NULL);
  g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
  
  if (y0 > 0)
    y0 = 0;
  if (y1 < 0)
    y1 = 0;
  
  /* Validate backwards from the anchor line to y0
   */
  line = gtk_text_iter_get_line (anchor);
  seen = 0;
  while (line && seen < -y0)
    {
      GtkTextLineData *line_data = gtk_text_line_get_data (line, layout);
      if (!line_data || !line_data->valid)
	{
	  gint old_height = line_data ? line_data->height : 0;
	  
	  gtk_text_btree_validate_line (layout->buffer->tree, line, layout);
	  line_data = gtk_text_line_get_data (line, layout);

	  delta_height += line_data->height - old_height;

	  first_line = line;
	  first_line_y = -seen;
	  if (!last_line)
	    {
	      last_line = line;
	      last_line_y = -seen + line_data->height;
	    }
	}
      
      seen += line_data->height;
      line = gtk_text_line_previous (line);
    }

  /* Validate forwards to y1 */
  line = gtk_text_iter_get_line (anchor);
  seen = 0;
  while (line && seen < y1)
    {
      GtkTextLineData *line_data = gtk_text_line_get_data (line, layout);
      if (!line_data || !line_data->valid)
	{
	  gint old_height = line_data ? line_data->height : 0;
	  
	  gtk_text_btree_validate_line (layout->buffer->tree, line, layout);
	  line_data = gtk_text_line_get_data (line, layout);

	  delta_height += line_data->height - old_height;

	  if (!first_line)
	    {
	      first_line = line;
	      first_line_y = seen;
	    }
	  last_line = line;
	  last_line_y = seen + line_data->height;
	}
      
      seen += line_data->height;
      line = gtk_text_line_next (line);
    }

  /* If we found and validated any invalid lines, emit the changed singal
   */
  if (first_line)
    {
      gint line_top = gtk_text_btree_find_line_top (layout->buffer->tree, first_line, layout);            
      
      gtk_text_layout_changed (layout,
			       line_top,
			       last_line_y - first_line_y - delta_height,
			       last_line_y - first_line_y);
    }
}

/**
 * gtk_text_layout_validate:
 * @tree: a #GtkTextLayout
 * @max_pixels: the maximum number of pixels to validate. (No more
 *              than one paragraph beyond this limit will be validated)
 * 
 * Validate regions of a #GtkTextLayout. The ::changed signal will
 * be emitted for each region validated.
 **/
void
gtk_text_layout_validate (GtkTextLayout *layout,
			  gint           max_pixels)
{
  gint y, old_height, new_height;

  g_return_if_fail (layout != NULL);
  g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
  
  while (max_pixels > 0 &&
	 gtk_text_btree_validate (layout->buffer->tree, layout,  max_pixels,
				  &y, &old_height, &new_height))
    {
      max_pixels -= new_height;
      gtk_text_layout_changed (layout, y, old_height, new_height);
    }
}

static GtkTextLineData*
gtk_text_layout_real_wrap (GtkTextLayout   *layout,
			   GtkTextLine     *line,
                            /* may be NULL */
			   GtkTextLineData *line_data)
{
  GtkTextLineDisplay *display;
  
  g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), NULL);
  
  if (line_data == NULL)
    {
      line_data = gtk_text_line_data_new (layout, line);
      gtk_text_line_add_data (line, line_data);
    }

  display = gtk_text_layout_get_line_display (layout, line, TRUE);
  line_data->width = display->width;
  line_data->height = display->height;
  line_data->valid = TRUE;
  gtk_text_layout_free_line_display (layout, display);

  return line_data;
}

static void
gtk_text_layout_real_get_log_attrs (GtkTextLayout  *layout,
				    GtkTextLine    *line,
				    PangoLogAttr  **attrs,
				    gint           *n_attrs)
{
  GtkTextLineDisplay *display;
  
761
  g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
  
  display = gtk_text_layout_get_line_display (layout, line, TRUE);
  pango_layout_get_log_attrs (display->layout, attrs, n_attrs);
  gtk_text_layout_free_line_display (layout, display);
}

/*
 * Layout utility functions
 */

/* If you get the style with get_style () you need to call
   release_style () to free it. */
static GtkTextStyleValues*
get_style (GtkTextLayout *layout,
	   const GtkTextIter *iter)
{
  GtkTextTag** tags;
  gint tag_count = 0;
  GtkTextStyleValues *style;
  
  /* If we have the one-style cache, then it means
     that we haven't seen a toggle since we filled in the
     one-style cache.
  */
  if (layout->one_style_cache != NULL)
    {
788
      gtk_text_style_values_ref (layout->one_style_cache);
789
790
791
792
793
794
795
796
797
798
799
800
801
      return layout->one_style_cache;
    }

  g_assert (layout->one_style_cache == NULL);
  
  /* Get the tags at this spot */
  tags = gtk_text_btree_get_tags (iter, &tag_count);

  /* No tags, use default style */
  if (tags == NULL || tag_count == 0)
    {
      /* One ref for the return value, one ref for the
         layout->one_style_cache reference */
802
803
      gtk_text_style_values_ref (layout->default_style);
      gtk_text_style_values_ref (layout->default_style);
804
805
806
807
808
809
810
811
812
813
814
      layout->one_style_cache = layout->default_style;

      if (tags)
        g_free (tags);

      return layout->default_style;
    }
  
  /* Sort tags in ascending order of priority */
  gtk_text_tag_array_sort (tags, tag_count);
  
815
  style = gtk_text_style_values_new ();
816
  
817
818
  gtk_text_style_values_copy (layout->default_style,
                              style);
819

820
821
822
  gtk_text_style_values_fill_from_tags (style,
                                        tags,
                                        tag_count);
823
824
825
826
827
828
829

  g_free (tags);

  g_assert (style->refcount == 1);

  /* Leave this style as the last one seen */
  g_assert (layout->one_style_cache == NULL);
830
  gtk_text_style_values_ref (style); /* ref held by layout->one_style_cache */
831
832
833
834
835
836
837
838
839
840
841
842
843
  layout->one_style_cache = style;
  
  /* Returning yet another refcount */
  return style;
}

static void
release_style (GtkTextLayout *layout,
	       GtkTextStyleValues *style)
{
  g_return_if_fail (style != NULL);
  g_return_if_fail (style->refcount > 0);

844
  gtk_text_style_values_unref (style);
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
}

/*
 * Lines
 */

/* This function tries to optimize the case where a line
   is completely invisible */
static gboolean
totally_invisible_line (GtkTextLayout *layout,
			GtkTextLine   *line,
			GtkTextIter   *iter)
{
  GtkTextLineSegment *seg;
  int bytes = 0;
  
  /* If we have a cached style, then we know it does actually apply
     and we can just see if it is elided. */
  if (layout->one_style_cache &&
      !layout->one_style_cache->elide)
    return FALSE;
  /* Without the cache, we check if the first char is visible, if so
     we are partially visible.  Note that we have to check this since
     we don't know the current elided/nonelided toggle state; this
     function can use the whole btree to get it right. */
  else
    {
      gtk_text_btree_get_iter_at_line(layout->buffer->tree, iter, line, 0);
      
      if (!gtk_text_btree_char_is_invisible (iter))
	return FALSE;
    }

  bytes = 0;
  seg = line->segments;

  while (seg != NULL)
    {
      if (seg->byte_count > 0)
        bytes += seg->byte_count;

      /* Note that these two tests can cause us to bail out
         when we shouldn't, because a higher-priority tag
         may override these settings. However the important
         thing is to only elide really-elided lines, rather
         than to elide all really-elided lines. */
      
      else if (seg->type == &gtk_text_toggle_on_type)
        {
          invalidate_cached_style (layout);
          
          /* Bail out if an elision-unsetting tag begins */
          if (seg->body.toggle.info->tag->elide_set &&
              !seg->body.toggle.info->tag->values->elide)
            break;
        }
      else if (seg->type == &gtk_text_toggle_off_type)
        {
          invalidate_cached_style (layout);
          
          /* Bail out if an elision-setting tag ends */
          if (seg->body.toggle.info->tag->elide_set &&
              seg->body.toggle.info->tag->values->elide)
            break;
        }

      seg = seg->next;
    }

  if (seg != NULL)       /* didn't reach line end */
    return FALSE;

  return TRUE;
}

static void
set_para_values (GtkTextLayout      *layout,
		 GtkTextStyleValues *style,
		 GtkTextLineDisplay *display,
		 gdouble            *align)
{
  PangoAlignment pango_align = PANGO_ALIGN_LEFT;
  int layout_width;

  display->direction = style->direction;

  if (display->direction == GTK_TEXT_DIR_LTR)
    display->layout = pango_layout_new (layout->ltr_context);
  else
    display->layout = pango_layout_new (layout->rtl_context);

  switch (style->justify)
    {
    case GTK_JUSTIFY_LEFT:
      pango_align = (style->direction == GTK_TEXT_DIR_LTR) ? PANGO_ALIGN_LEFT : PANGO_ALIGN_RIGHT;
      break;
    case GTK_JUSTIFY_RIGHT:
      pango_align = (style->direction == GTK_TEXT_DIR_LTR) ? PANGO_ALIGN_RIGHT : PANGO_ALIGN_LEFT;
      break;
    case GTK_JUSTIFY_CENTER:
      pango_align = PANGO_ALIGN_CENTER;
      break;
    case GTK_JUSTIFY_FILL:
      g_warning ("FIXME we don't support GTK_JUSTIFY_FILL yet");
      break;
    default:
      g_assert_not_reached ();
      break;
    }

  switch (pango_align)
    {
      case PANGO_ALIGN_LEFT:
	*align = 0.0;
	break;
      case PANGO_ALIGN_RIGHT:
	*align = 1.0;
	break;
      case PANGO_ALIGN_CENTER:
	*align = 0.5;
	break;
    }

  pango_layout_set_alignment (display->layout, pango_align);
  pango_layout_set_spacing (display->layout, style->pixels_inside_wrap * PANGO_SCALE);

  display->top_margin = style->pixels_above_lines;
  display->height = style->pixels_above_lines + style->pixels_below_lines;
  display->bottom_margin = style->pixels_below_lines;
  display->x_offset = display->left_margin = MIN (style->left_margin, style->left_wrapped_line_margin);
  display->right_margin = style->right_margin;

  pango_layout_set_indent (display->layout, style->left_margin - style->left_wrapped_line_margin);

  switch (style->wrap_mode)
    {
    case GTK_WRAPMODE_CHAR:
      /* FIXME: Handle this; for now, fall-through */
    case GTK_WRAPMODE_WORD:
      display->total_width = -1;
      layout_width = layout->screen_width - display->x_offset - style->right_margin;
      pango_layout_set_width (display->layout, layout_width * PANGO_SCALE);
      break;
    case GTK_WRAPMODE_NONE:
      display->total_width = MAX (layout->screen_width, layout->width) - display->x_offset - style->right_margin;
      break;
    }
}

static PangoAttribute *
gtk_text_attr_appearance_copy (const PangoAttribute *attr)
{
  const GtkTextAttrAppearance *appearance_attr = (const GtkTextAttrAppearance *)attr;
  
  return gtk_text_attr_appearance_new (&appearance_attr->appearance);
}

static void
gtk_text_attr_appearance_destroy (PangoAttribute *attr)
{
  GtkTextAppearance *appearance = &((GtkTextAttrAppearance *)attr)->appearance;

  if (appearance->bg_stipple)
    gdk_drawable_unref (appearance->bg_stipple);
  if (appearance->fg_stipple)
    gdk_drawable_unref (appearance->fg_stipple);
  
  g_free (attr);
}

static gboolean
gtk_text_attr_appearance_compare (const PangoAttribute *attr1,
				  const PangoAttribute *attr2)
{
  const GtkTextAppearance *appearance1 = &((const GtkTextAttrAppearance *)attr1)->appearance;
  const GtkTextAppearance *appearance2 = &((const GtkTextAttrAppearance *)attr2)->appearance;

  return (gdk_color_equal (&appearance1->fg_color, &appearance2->fg_color) &&
	  gdk_color_equal (&appearance1->bg_color, &appearance2->bg_color) &&
	  appearance1->fg_stipple ==  appearance2->fg_stipple &&
	  appearance1->bg_stipple ==  appearance2->bg_stipple &&
	  appearance1->underline == appearance2->underline &&
	  appearance1->overstrike == appearance2->overstrike &&
	  appearance1->draw_bg == appearance2->draw_bg);
	  
}

/**
 * gtk_text_attr_appearance_new:
 * @desc: 
 * 
 * Create a new font description attribute. (This attribute
 * allows setting family, style, weight, variant, stretch,
 * and size simultaneously.)
 * 
 * Return value: 
 **/
static PangoAttribute *
gtk_text_attr_appearance_new (const GtkTextAppearance *appearance)
{
  static PangoAttrClass klass = {
    0,
    gtk_text_attr_appearance_copy,
    gtk_text_attr_appearance_destroy,
    gtk_text_attr_appearance_compare
  };

  GtkTextAttrAppearance *result;

  if (!klass.type)
    klass.type = gtk_text_attr_appearance_type =
      pango_attr_type_register ("GtkTextAttrAppearance");

  result = g_new (GtkTextAttrAppearance, 1);
  result->attr.klass = &klass;

  result->appearance = *appearance;
    
  if (appearance->bg_stipple)
    gdk_drawable_ref (appearance->bg_stipple);
  if (appearance->fg_stipple)
    gdk_drawable_ref (appearance->fg_stipple);

  return (PangoAttribute *)result;
}

static void
add_text_attrs (GtkTextLayout      *layout,
		GtkTextStyleValues *style,
		gint                byte_count,
		PangoAttrList      *attrs,
		gint                start,
		gboolean            size_only)
{
  PangoAttribute *attr;

  attr = pango_attr_font_desc_new (style->font_desc);
  attr->start_index = start;
  attr->end_index = start + byte_count;

  pango_attr_list_insert (attrs, attr);

  if (!size_only)
    {
      attr = gtk_text_attr_appearance_new (&style->appearance);
  
      attr->start_index = start;
      attr->end_index = start + byte_count;

1094
      pango_attr_list_insert (attrs, attr); 
1095
1096
1097
1098
1099
    }
}

static void
add_pixmap_attrs (GtkTextLayout      *layout,
1100
		  GtkTextLineDisplay *display,
1101
1102
1103
1104
1105
		  GtkTextStyleValues *style,
		  GtkTextLineSegment *seg,
		  PangoAttrList      *attrs,
		  gint                start)
{
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
  PangoAttribute *attr;
  PangoRectangle logical_rect;
  GtkTextPixmap *pixmap = &seg->body.pixmap;
  gint width, height;

  gdk_drawable_get_size (pixmap->pixmap, &width, &height);
  logical_rect.x = 0;
  logical_rect.y = -height * PANGO_SCALE;
  logical_rect.width = width * PANGO_SCALE;
  logical_rect.height = height * PANGO_SCALE;

  attr = pango_attr_shape_new (&logical_rect, &logical_rect);
  attr->start_index = start;
  attr->end_index = start + seg->byte_count;
  pango_attr_list_insert (attrs, attr);

  display->pixmaps = g_slist_append (display->pixmaps, pixmap);
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
}

static void
add_cursor (GtkTextLayout      *layout,
	    GtkTextLineDisplay *display,
	    GtkTextLineSegment *seg,
	    gint                start)
{
  GtkTextIter selection_start, selection_end;
  
  PangoRectangle strong_pos, weak_pos;
  GtkTextCursorDisplay *cursor;

  /* Hide insertion cursor when we have a selection
   */
1138
  if (gtk_text_btree_mark_is_insert (layout->buffer->tree, (GtkTextMark*)seg) &&
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
      gtk_text_buffer_get_selection_bounds (layout->buffer, &selection_start, &selection_end))
    return;
  
  pango_layout_get_cursor_pos (display->layout, start, &strong_pos, &weak_pos);
  
  cursor = g_new (GtkTextCursorDisplay, 1);
  
  cursor->x = strong_pos.x / PANGO_SCALE;
  cursor->y = strong_pos.y / PANGO_SCALE;
  cursor->height = strong_pos.height / PANGO_SCALE;
  cursor->is_strong = TRUE;
  display->cursors = g_slist_prepend (display->cursors, cursor);
  
  if (weak_pos.x == strong_pos.x)
    cursor->is_weak = TRUE;
  else
    {
      cursor->is_weak = FALSE;
      
      cursor = g_new (GtkTextCursorDisplay, 1);
      
      cursor->x = weak_pos.x / PANGO_SCALE;
      cursor->y = weak_pos.y / PANGO_SCALE;
      cursor->height = weak_pos.height / PANGO_SCALE;
      cursor->is_strong = FALSE;
      cursor->is_weak = TRUE;
      display->cursors = g_slist_prepend (display->cursors, cursor);
    }
}

GtkTextLineDisplay *
gtk_text_layout_get_line_display (GtkTextLayout *layout,
				  GtkTextLine   *line,
				  gboolean       size_only)
{
  GtkTextLineDisplay *display;
  GtkTextLineSegment *seg;
  GtkTextIter iter;
  GtkTextStyleValues *style;
  gchar *text;
  PangoAttrList *attrs;
  gint byte_count, byte_offset;
  gdouble align;
  PangoRectangle extents;
  gboolean para_values_set = FALSE;
  GSList *cursor_byte_offsets = NULL;
  GSList *cursor_segs = NULL;
  GSList *tmp_list1, *tmp_list2;

  g_return_val_if_fail (line != NULL, NULL);

  if (layout->one_display_cache)
    {
      if (line == layout->one_display_cache->line &&
	  (size_only || !layout->one_display_cache->size_only))
	return layout->one_display_cache;
      else
	{
	  GtkTextLineDisplay *tmp_display = layout->one_display_cache;
	  layout->one_display_cache = NULL;
	  gtk_text_layout_free_line_display (layout, tmp_display);
	}
    }
  
  display = g_new0 (GtkTextLineDisplay, 1);

  display->size_only = size_only;
  display->line = line;

  gtk_text_btree_get_iter_at_line (layout->buffer->tree, &iter, line, 0);
  
  /* Special-case optimization for completely
   * invisible lines; makes it faster to deal
   * with sequences of invisible lines.
   */
  if (totally_invisible_line (layout, line, &iter))
    return display;

  /* Allocate space for flat text for buffer
   */
  byte_count = gtk_text_line_byte_count (line);
  text = g_malloc (byte_count);

  attrs = pango_attr_list_new ();

  /* Iterate over segments, creating display chunks for them. */
  byte_offset = 0;
  seg = gtk_text_iter_get_any_segment (&iter);
  while (seg != NULL)
    {
      /* Displayable segments */
      if (seg->type == &gtk_text_char_type ||
	  seg->type == &gtk_text_pixmap_type)
        {
	  gtk_text_btree_get_iter_at_line (layout->buffer->tree,
					   &iter, line,
					   byte_offset);
	  style = get_style (layout, &iter);
	  
	  /* We have to delay setting the paragraph values until we
	   * hit the first pixmap or text segment because toggles at
	   * the beginning of the paragraph should affect the
	   * paragraph-global values
	   */
	  if (!para_values_set)
	    {
	      set_para_values (layout, style, display, &align);
	      para_values_set = TRUE;
	    }

          /* First see if the chunk is elided, and ignore it if so. Tk
	   * looked at tabs, wrap mode, etc. before doing this, but
	   * that made no sense to me, so I am just skipping the
	   * elided chunks
	   */
          if (!style->elide)
            {
	      if (seg->type == &gtk_text_char_type)
		{
		  /* We don't want to split segments because of marks, so we scan forward
		   * for more segments only separated from us by marks. In theory, we
		   * should also merge segments with identical styles, even if there
		   * are toggles in-between
		   */

		  gint byte_count = 0;

		  while (TRUE)
		    {
		      if (seg->type == &gtk_text_char_type)
			{
			  memcpy (text + byte_offset, seg->body.chars, seg->byte_count);
			  byte_offset += seg->byte_count;
			  byte_count += seg->byte_count;
			}
		      else if (seg->body.mark.visible)
			{
			  cursor_byte_offsets = g_slist_prepend (cursor_byte_offsets, GINT_TO_POINTER (byte_offset));
			  cursor_segs = g_slist_prepend (cursor_segs, seg);
			}

		      if (!seg->next ||
			  (seg->next->type != &gtk_text_right_mark_type &&
			   seg->next->type != &gtk_text_left_mark_type &&
			   seg->next->type != &gtk_text_char_type))
			break;

		      seg = seg->next;
		    }
		  
		  add_text_attrs (layout, style, byte_count, attrs, byte_offset - byte_count, size_only);
		}
	      else
		{
1293
		  add_pixmap_attrs (layout, display, style, seg, attrs, byte_offset);
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
		  memcpy (text + byte_offset, gtk_text_unknown_char_utf8, seg->byte_count);
		  byte_offset += seg->byte_count;
		}
	    }

	  release_style (layout, style);
        }

      /* Toggles */
      else if (seg->type == &gtk_text_toggle_on_type ||
               seg->type == &gtk_text_toggle_off_type)
        {
          /* Style may have changed, drop our
             current cached style */
          invalidate_cached_style (layout);
        }
      
      /* Marks */
      else if (seg->type == &gtk_text_right_mark_type ||
               seg->type == &gtk_text_left_mark_type)
        {
          /* Display visible marks */

          if (seg->body.mark.visible)
	    {
	      cursor_byte_offsets = g_slist_prepend (cursor_byte_offsets, GINT_TO_POINTER (byte_offset));
	      cursor_segs = g_slist_prepend (cursor_segs, seg);
	    }
        }

      else
        g_error ("Unknown segment type: %s", seg->type->name);

      seg = seg->next;
    }

  if (!para_values_set)
    {
      style = get_style (layout, &iter);
      set_para_values (layout, style, display, &align);
      release_style (layout, style);
    }
	      
  /* Pango doesn't want the trailing new line */
  if (byte_offset > 0 && text[byte_offset - 1] == '\n')
    byte_offset--;
  
  pango_layout_set_text (display->layout, text, byte_offset);
  pango_layout_set_attributes (display->layout, attrs);

  tmp_list1 = cursor_byte_offsets;
  tmp_list2 = cursor_segs;
  while (tmp_list1)
    {
      add_cursor (layout, display, tmp_list2->data, GPOINTER_TO_INT (tmp_list1->data));
      tmp_list1 = tmp_list1->next;
      tmp_list2 = tmp_list2->next;
    }
  g_slist_free (cursor_byte_offsets);
  g_slist_free (cursor_segs);

  pango_layout_get_extents (display->layout, NULL, &extents);

  if (display->total_width >= 0)
    display->x_offset += (display->total_width - extents.width / PANGO_SCALE) * align;

  display->width = extents.width / PANGO_SCALE + display->left_margin + display->right_margin;
  display->height += extents.height / PANGO_SCALE;
 
  /* Free this if we aren't in a loop */
  if (layout->wrap_loop_count == 0)
    invalidate_cached_style (layout);

  g_free (text);
  pango_attr_list_unref (attrs);

  layout->one_display_cache = display;
  
  return display;
}

void
gtk_text_layout_free_line_display (GtkTextLayout      *layout,
				   GtkTextLineDisplay *display)
{
  if (display != layout->one_display_cache)
    {
1381
      g_object_unref (G_OBJECT (display->layout));
1382
1383
1384
1385
1386

      if (display->cursors)
	{
	  g_slist_foreach (display->cursors, (GFunc)g_free, NULL);
	  g_slist_free (display->cursors);
1387
	  g_slist_free (display->pixmaps);
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
	}
      
      g_free (display);
    }
}

/* FIXME: This really doesn't belong in this file ... */
static GtkTextLineData*
gtk_text_line_data_new (GtkTextLayout *layout,
			GtkTextLine   *line)
{
  GtkTextLineData *line_data;

  line_data = g_new (GtkTextLineData, 1);

  line_data->view_id = layout;
  line_data->next = NULL;
  line_data->width = 0;
  line_data->height = 0;
  line_data->valid = FALSE;
  
  return line_data;
}

static void
get_line_at_y (GtkTextLayout *layout,
	       gint           y,
	       GtkTextLine  **line,
	       gint          *line_top)
{
  if (y < 0)
    y = 0;
  if (y > layout->height)
    y = layout->height;
  
  *line = gtk_text_btree_find_line_by_y (layout->buffer->tree, layout, y, line_top);
  if (*line == NULL)
    {
      *line = gtk_text_btree_get_line (layout->buffer->tree,
				       gtk_text_btree_line_count (layout->buffer->tree) - 1, NULL);
      if (line_top)
	*line_top = gtk_text_btree_find_line_top (layout->buffer->tree, *line, layout);      
    }
}

/**
 * gtk_text_layout_get_line_at_y:
 * @layout: a #GtkLayout
 * @target_iter: the iterator in which the result is stored
 * @y: the y positition
 * @line_top: location to store the y coordinate of the
 *            top of the line. (Can by %NULL.)
 * 
 * Get the iter at the beginning of the line which is displayed
 * at the given y.
 **/
void
gtk_text_layout_get_line_at_y (GtkTextLayout *layout,
			       GtkTextIter   *target_iter,
			       gint           y,
			       gint          *line_top)
{
  GtkTextLine *line;

  g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
  g_return_if_fail (target_iter != NULL);

  get_line_at_y (layout, y, &line, line_top);
  gtk_text_btree_get_iter_at_line (layout->buffer->tree, target_iter, line, 0);
}

void
gtk_text_layout_get_iter_at_pixel (GtkTextLayout *layout,
                                   GtkTextIter *target_iter,
                                   gint x, gint y)
{
  GtkTextLine *line;
  gint byte_index, trailing;
  gint line_top;
  GtkTextLineDisplay *display;
  
  g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
  g_return_if_fail (target_iter != NULL);
  
  /* Adjust pixels to be on-screen. This gives nice
     behavior if the user is dragging with a pointer grab.
  */
  if (x < 0)
    x = 0;
  if (x > layout->width)
    x = layout->width;

  get_line_at_y (layout, y, &line, &line_top);

1482
  display = gtk_text_layout_get_line_display (layout, line, FALSE);
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543

  x -= display->x_offset;
  y -= line_top + display->top_margin;

  /* We clamp y to the area of the actual layout so that the layouts
   * hit testing works OK on the space above and below the layout
   */
  y = CLAMP (y, display->top_margin, display->height - display->top_margin - display->bottom_margin - 1);
  
  if (!pango_layout_xy_to_index (display->layout, x * PANGO_SCALE, y * PANGO_SCALE,
				 &byte_index, &trailing))
    {
      byte_index = gtk_text_line_byte_count (line);
      trailing = 0;
    }

  gtk_text_btree_get_iter_at_line (layout->buffer->tree,
                                   target_iter,
                                   line, byte_index);

  while (trailing--)
    gtk_text_iter_forward_char (target_iter);
  
  gtk_text_layout_free_line_display (layout, display);
}

/**
 * gtk_text_layout_get_cursor_locations
 * @layout: a #GtkTextLayout
 * @iter: a #GtkTextIter
 * @strong_pos: location to store the strong cursor position (may be %NULL)
 * @weak_pos: location to store the weak cursor position (may be %NULL)
 * 
 * Given an iterator within a text laout, determine the positions that of the
 * strong and weak cursors if the insertion point is at that
 * iterator. The position of each cursor is stored as a zero-width
 * rectangle. The strong cursor location is the location where
 * characters of the directionality equal to the base direction of the
 * paragraph are inserted.  The weak cursor location is the location
 * where characters of the directionality opposite to the base
 * direction of the paragraph are inserted.
 **/
void
gtk_text_layout_get_cursor_locations (GtkTextLayout  *layout,
				      GtkTextIter    *iter,
				      GdkRectangle   *strong_pos,
				      GdkRectangle   *weak_pos)
{
  GtkTextLine *line;
  GtkTextLineDisplay *display;
  gint line_top;

  PangoRectangle pango_strong_pos;
  PangoRectangle pango_weak_pos;

  g_return_if_fail (layout != NULL);
  g_return_if_fail (iter != NULL);
  
  line = gtk_text_iter_get_line (iter);
  line_top = gtk_text_btree_find_line_top (layout->buffer->tree, line, layout);

1544
  display = gtk_text_layout_get_line_display (layout, line, FALSE);
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609

  pango_layout_get_cursor_pos (display->layout, gtk_text_iter_get_line_byte (iter),
			       strong_pos ? &pango_strong_pos : NULL,
			       weak_pos ? &pango_weak_pos : NULL);

  if (strong_pos)
    {
      strong_pos->x = display->x_offset + pango_strong_pos.x / PANGO_SCALE;
      strong_pos->y = line_top + display->top_margin + pango_strong_pos.y / PANGO_SCALE;
      strong_pos->width = 0;
      strong_pos->height = pango_strong_pos.height / PANGO_SCALE;
    }

  if (weak_pos)
    {
      weak_pos->x = display->x_offset + pango_weak_pos.x / PANGO_SCALE;
      weak_pos->y = line_top + display->top_margin + pango_weak_pos.y / PANGO_SCALE;
      weak_pos->width = 0;
      weak_pos->height = pango_weak_pos.height / PANGO_SCALE;
    }

  gtk_text_layout_free_line_display (layout, display);
}

/**
 * gtk_text_layout_get_line_y:
 * @layout: a #GtkTextLayout
 * @iter:   a #GtkTextIter
 * 
 * Find the y coordinate of the top of the paragraph containing
 * the given iter.
 * 
 * Return value: the y coordinate, in pixels.
 **/
gint
gtk_text_layout_get_line_y (GtkTextLayout     *layout,
			    const GtkTextIter *iter)
{
  GtkTextLine *line;
  
  g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), 0);
  g_return_val_if_fail (gtk_text_iter_get_btree (iter) == layout->buffer->tree, 0);
  
  line = gtk_text_iter_get_line (iter);
  return gtk_text_btree_find_line_top (layout->buffer->tree, line, layout);
}

void
gtk_text_layout_get_iter_location (GtkTextLayout     *layout,
                                   const GtkTextIter *iter,
                                   GdkRectangle      *rect)
{
  PangoRectangle pango_rect;
  GtkTextLine *line;
  GtkTextBTree *tree;
  GtkTextLineDisplay *display;
  gint byte_index;
  
  g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
  g_return_if_fail (gtk_text_iter_get_btree (iter) == layout->buffer->tree);
  g_return_if_fail (rect != NULL);
  
  tree = gtk_text_iter_get_btree (iter);
  line = gtk_text_iter_get_line (iter);

1610
  display = gtk_text_layout_get_line_display (layout, line, FALSE);
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667

  rect->y = gtk_text_btree_find_line_top (tree, line, layout);

  /* pango_layout_index_to_pos() expects the index of a character within the layout,
   * so we have to special case the last character. FIXME: This should be moved
   * to Pango.
   */
  if (gtk_text_iter_ends_line (iter))
    {
      PangoLayoutLine *last_line = g_slist_last (pango_layout_get_lines (display->layout))->data;

      pango_layout_line_get_extents (last_line, NULL, &pango_rect);
      
      rect->x = display->x_offset + (pango_rect.x + pango_rect.width) / PANGO_SCALE;
      rect->y += display->top_margin;
      rect->width = 0;
      rect->height = pango_rect.height / PANGO_SCALE;
    }
  else
    {
      byte_index = gtk_text_iter_get_line_byte (iter);
  
      pango_layout_index_to_pos (display->layout, byte_index, &pango_rect);
      
      rect->x = display->x_offset + pango_rect.x / PANGO_SCALE;
      rect->y += display->top_margin;
      rect->width = pango_rect.width / PANGO_SCALE;
      rect->height = pango_rect.height / PANGO_SCALE;
    }
  
  gtk_text_layout_free_line_display (layout, display);
}

/* Find the iter for the logical beginning of the first display line whose
 * top y is >= y. If none exists, move the iter to the logical beginning
 * of the last line in the buffer.
 */
static void
find_display_line_below (GtkTextLayout *layout,
			 GtkTextIter   *iter,
			 gint           y)
{
  GtkTextLine *line, *next;
  GtkTextLine *found_line = NULL;
  gint line_top;
  gint found_byte = 0;
  
  line = gtk_text_btree_find_line_by_y (layout->buffer->tree, layout, y, &line_top);
  if (!line)
    {
      line = gtk_text_btree_get_line (layout->buffer->tree,
				      gtk_text_btree_line_count (layout->buffer->tree) - 1, NULL);
      line_top = gtk_text_btree_find_line_top (layout->buffer->tree, line, layout);
    }

  while (line && !found_line)
    {
1668
      GtkTextLineDisplay *display = gtk_text_layout_get_line_display (layout, line, FALSE);
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
      gint byte_index = 0;
      GSList *tmp_list =  pango_layout_get_lines (display->layout);

      line_top += display->top_margin;
      
      while (tmp_list)
	{
	  PangoRectangle logical_rect;
	  PangoLayoutLine *layout_line = tmp_list->data;

	  found_byte = byte_index;
	  
	  if (line_top >= y)
	    {
	      found_line = line;
	      break;
	    }
	  
	  pango_layout_line_get_extents (layout_line, NULL, &logical_rect);
	  line_top += logical_rect.height / PANGO_SCALE;
	  
	  tmp_list = tmp_list->next;
	}

      line_top += display->bottom_margin;
      gtk_text_layout_free_line_display (layout, display);

      next = gtk_text_line_next (line);
      if (!next)
	found_line = line;

      line = next;
    }
  
  gtk_text_btree_get_iter_at_line (layout->buffer->tree, iter, found_line, found_byte);
}
  
/* Find the iter for the logical beginning of the last display line whose
 * top y is >= y. If none exists, move the iter to the logical beginning
 * of the first line in the buffer.
 */
static void
find_display_line_above (GtkTextLayout *layout,
			 GtkTextIter   *iter,
			 gint           y)
{
  GtkTextLine *line;
  GtkTextLine *found_line = NULL;
  gint line_top;
  gint found_byte = 0;
  
  line = gtk_text_btree_find_line_by_y (layout->buffer->tree, layout, y, &line_top);
  if (!line)
    {
      line = gtk_text_btree_get_line (layout->buffer->tree,
				      gtk_text_btree_line_count (layout->buffer->tree) - 1, NULL);
      line_top = gtk_text_btree_find_line_top (layout->buffer->tree, line, layout);
    }

  while (line && !found_line)
    {
1730
      GtkTextLineDisplay *display = gtk_text_layout_get_line_display (layout, line, FALSE);
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
      PangoRectangle logical_rect;
      
      gint byte_index = 0;
      GSList *tmp_list;
      gint tmp_top;

      line_top -= display->top_margin + display->bottom_margin;
      pango_layout_get_extents (display->layout, NULL, &logical_rect);
      line_top -= logical_rect.height / PANGO_SCALE;
	  
      tmp_top = line_top + display->top_margin;

      tmp_list =  pango_layout_get_lines (display->layout);
      while (tmp_list)
	{
	  PangoLayoutLine *layout_line = tmp_list->data;

	  found_byte = byte_index;
	  
	  tmp_top += logical_rect.height / PANGO_SCALE;

	  if (tmp_top < y)
	    {
	      found_line = line;
	      found_byte = byte_index;
	    }
	  
	  pango_layout_line_get_extents (layout_line, NULL, &logical_rect);
	  
	  tmp_list = tmp_list->next;
	}

      gtk_text_layout_free_line_display (layout, display);
      
      line = gtk_text_line_previous (line);
    }

  if (found_line)
    gtk_text_btree_get_iter_at_line (layout->buffer->tree, iter, found_line, found_byte);
  else
    gtk_text_buffer_get_iter_at_char (layout->buffer, iter, 0);
}
  
/**
 * gtk_text_layout_clamp_iter_to_vrange:
 * @layout: a #GtkTextLayout
 * @iter:   a #GtkTextIter
 * @top:    the top of the range
 * @bottom: the bottom the range
 * 
 * If the iterator is not fully in the range @top <= y < @bottom,
 * then, if possible, move it the minimum distance so that the
 * iterator in this range.
 *
 * Returns: %TRUE if the iterator was moved, otherwise %FALSE.
 **/
gboolean
gtk_text_layout_clamp_iter_to_vrange (GtkTextLayout *layout,
				      GtkTextIter   *iter,
				      gint           top,
				      gint           bottom)
{
  GdkRectangle iter_rect;

  gtk_text_layout_get_iter_location (layout, iter, &iter_rect);

  /* If the iter is at least partially above the range, put the iter
   * at the first fully visible line after the range.
   */
  if (iter_rect.y < top)
    {
      find_display_line_below (layout, iter, top);
      
      return TRUE;
    }
  /* Otherwise, if the iter is at least partially below the screen, put the
   * iter on the last logical position of the last completely visible
   * line on screen
   */
  else if (iter_rect.y + iter_rect.height > bottom)
    {
      find_display_line_above (layout, iter, bottom);
      
      return TRUE;
    }
  else
    return FALSE;
}

/**
 * gtk_text_layout_move_iter_to_next_line:
 * @layout: a #GtkLayout
 * @iter:   a #GtkTextIter
 * 
 * Move the iterator to the beginning of the previous line. The lines
 * of a wrapped paragraph are treated as distinct for this operation.
 **/
void
gtk_text_layout_move_iter_to_previous_line (GtkTextLayout *layout,
					    GtkTextIter   *iter)
{
  GtkTextLine *line;
  GtkTextLineDisplay *display;
  gint line_byte;
  GSList *tmp_list;
  PangoLayoutLine *layout_line;

  g_return_if_fail (layout != NULL);
  g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
  g_return_if_fail (iter != NULL);
  
  line = gtk_text_iter_get_line (iter);
  line_byte = gtk_text_iter_get_line_byte (iter);
  
1845
  display = gtk_text_layout_get_line_display (layout, line, FALSE);
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858

  tmp_list = pango_layout_get_lines (display->layout);
  layout_line = tmp_list->data;

  if (line_byte < layout_line->length || !tmp_list->next) /* first line of paragraph */
    {
      GtkTextLine *prev_line = gtk_text_line_previous (line);

      if (prev_line)
	{
	  gint byte_offset = 0;
	  
	  gtk_text_layout_free_line_display (layout, display);
1859
	  display = gtk_text_layout_get_line_display (layout, prev_line, FALSE);
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
	  
	  tmp_list =  pango_layout_get_lines (display->layout);

	  while (tmp_list->next)
	    {
	      layout_line = tmp_list->data;
	      tmp_list = tmp_list->next;

	      byte_offset += layout_line->length;
	    }

	  gtk_text_btree_get_iter_at_line (layout->buffer->tree,
					   iter, prev_line, byte_offset);
	}
      else
	gtk_text_btree_get_iter_at_line (layout->buffer->tree,
					 iter, line, 0);
    }
  else
    {
      gint prev_offset = 0;
      gint byte_offset = layout_line->length;

      tmp_list = tmp_list->next;
      while (tmp_list)
	{
	  layout_line = tmp_list->data;

	  if (line_byte < byte_offset + layout_line->length || !tmp_list->next)
	    {
	      gtk_text_btree_get_iter_at_line (layout->buffer->tree,
					       iter, line, prev_offset);
	      break;
	    }
	  
	  prev_offset = byte_offset;
	  byte_offset += layout_line->length;
	  tmp_list = tmp_list->next;
	}
    }

  gtk_text_layout_free_line_display (layout, display);
}

/**
 * gtk_text_layout_move_iter_to_next_line:
 * @layout: a #GtkLayout
 * @iter:   a #GtkTextIter
 * 
 * Move the iterator to the beginning of the next line. The
 * lines of a wrapped paragraph are treated as distinct for
 * this operation.
 **/
void
gtk_text_layout_move_iter_to_next_line (GtkTextLayout *layout,
					GtkTextIter   *iter)
{
  GtkTextLine *line;
  GtkTextLineDisplay *display;
  gint line_byte;
  
  gboolean found = FALSE;
  gboolean found_after = FALSE;
  
  g_return_if_fail (layout != NULL);
  g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
  g_return_if_fail (iter != NULL);
  
  line = gtk_text_iter_get_line (iter);
  line_byte = gtk_text_iter_get_line_byte (iter);
  
  while (line && !found_after)
    {
      gint byte_offset = 0;
      GSList *tmp_list;
      
1936
      display = gtk_text_layout_get_line_display (layout, line, FALSE);
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990

      tmp_list = pango_layout_get_lines (display->layout);
      while (tmp_list && !found_after)
	{
	  PangoLayoutLine *layout_line = tmp_list->data;

	  if (found)
	    {
	      gtk_text_btree_get_iter_at_line (layout->buffer->tree,
					       iter, line,
					       byte_offset);
	      found_after = TRUE;
	    }
	  else if (line_byte < byte_offset + layout_line->length || !tmp_list->next)
	    found = TRUE;

	  byte_offset += layout_line->length;
	  tmp_list = tmp_list->next;
	}
      
      gtk_text_layout_free_line_display (layout, display);

      line = gtk_text_line_next (line);
    }
}

/**
 * gtk_text_layout_move_iter_to_x:
 * @layout: a #GtkTextLayout
 * @iter:   a #GtkTextIter
 * @x:      X coordinate
 *
 * Keeping the iterator on the same line of the layout, move it to the
 * specified X coordinate. The lines of a wrapped paragraph are
 * treated as distinct for this operation.
 **/
void
gtk_text_layout_move_iter_to_x (GtkTextLayout *layout,
				GtkTextIter   *iter,
				gint           x)
{
  GtkTextLine *line;
  GtkTextLineDisplay *display;
  gint line_byte;
  gint byte_offset = 0;
  GSList *tmp_list;
  
  g_return_if_fail (layout != NULL);
  g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
  g_return_if_fail (iter != NULL);
  
  line = gtk_text_iter_get_line (iter);
  line_byte = gtk_text_iter_get_line_byte (iter);
  
1991
  display = gtk_text_layout_get_line_display (layout, line, FALSE);
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074

  tmp_list = pango_layout_get_lines (display->layout);
  while (tmp_list)
    {
      PangoLayoutLine *layout_line = tmp_list->data;

      if (line_byte < byte_offset + layout_line->length || !tmp_list->next)
	{
	  PangoRectangle logical_rect;
	  gint byte_index, trailing;
	  gint align = pango_layout_get_alignment (display->layout);
	  gint x_offset = display->left_margin * PANGO_SCALE;
	  gint width = pango_layout_get_width (display->layout);

	  if (width < 0)
	    width = display->total_width * PANGO_SCALE;

	  pango_layout_line_get_extents (layout_line, NULL, &logical_rect);

	  switch (align)
	    {
	    case PANGO_ALIGN_RIGHT:
	      x_offset += width - logical_rect.width;
	      break;
	    case PANGO_ALIGN_CENTER:
	      x_offset += (width - logical_rect.width) / 2;
	      break;
	    default:
	      break;
	    }

	  pango_layout_line_x_to_index (layout_line,
					x * PANGO_SCALE - x_offset,
					&byte_index, &trailing);

	  gtk_text_btree_get_iter_at_line (layout->buffer->tree,
					   iter,
					   line, byte_index);
	  
	  while (trailing--)
	    gtk_text_iter_forward_char (iter);

	  break;
	}
      
      byte_offset += layout_line->length;
      tmp_list = tmp_list->next;
    }
  
  gtk_text_layout_free_line_display (layout, display);
}

/**
 * gtk_text_layout_move_iter_visually:
 * @layout:  a #GtkTextLayout
 * @iter:    a #GtkTextIter
 * @count:   number of characters to move (negative moves left, positive moves right)
 * 
 * Move the iterator a given number of characters visually, treating
 * it as the strong cursor position. If @count is positive, then the
 * new strong cursor position will be @count positions to the right of
 * the old cursor position. If @count is negative then the new strong
 * cursor position will be @count positions to the left of the old
 * cursor position.
 *
 * In the presence of bidirection text, the correspondence
 * between logical and visual order will depend on the direction
 * of the current run, and there may be jumps when the cursor
 * is moved off of the end of a run.
 **/
 
void
gtk_text_layout_move_iter_visually (GtkTextLayout *layout,
				    GtkTextIter   *iter,
				    gint           count)
{
  g_return_if_fail (layout != NULL);
  g_return_if_fail (iter != NULL);
  
  while (count != 0)
    {
      GtkTextLine *line = gtk_text_iter_get_line (iter);
      gint line_byte = gtk_text_iter_get_line_byte (iter);
2075
      GtkTextLineDisplay *display = gtk_text_layout_get_line_display (layout, line, FALSE);
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
      int byte_count = gtk_text_line_byte_count (line);
      
      int new_index;
      int new_trailing;
      
      if (count > 0)
	{
	  pango_layout_move_cursor_visually (display->layout, line_byte, 0, 1, &new_index, &new_trailing);
	  count--;
	}
      else
	{
	  pango_layout_move_cursor_visually (display->layout, line_byte, 0, -1, &new_index, &new_trailing);
	  count++;
	}

      gtk_text_layout_free_line_display (layout, display);

      if (new_index < 0)
	{
	  line = gtk_text_line_previous (line);
	  if (!line)
	    return;
	  
	  new_index = gtk_text_line_byte_count (line);
	  
	}
      else if (new_index > byte_count)
	{
	  line = gtk_text_line_next (line);
	  if (!line)
	    return;
	  
	  new_index = 0;
	}

      gtk_text_btree_get_iter_at_line (layout->buffer->tree,
				       iter,
				       line, new_index);
      while (new_trailing--)
	gtk_text_iter_forward_char (iter);
    }

}

typedef void (*GtkSignal_NONE__INT_INT_INT_INT) (GtkObject  *object,
                                                 gint x, gint y,
                                                 gint width, gint height,
                                                 gpointer user_data);

void
gtk_marshal_NONE__INT_INT_INT_INT (GtkObject  *object,
                                   GtkSignalFunc func,
                                   gpointer func_data,
                                   GtkArg  *args)
{
  GtkSignal_NONE__INT_INT_INT_INT rfunc;

  rfunc = (GtkSignal_NONE__INT_INT_INT_INT) func;
  (*rfunc) (object,
            GTK_VALUE_INT (args[0]),
            GTK_VALUE_INT (args[1]),
            GTK_VALUE_INT (args[2]),
            GTK_VALUE_INT (args[3]),
            func_data);
}

void
gtk_text_layout_spew (GtkTextLayout *layout)
{
#if 0
  GtkTextDisplayLine *iter;
  guint wrapped = 0;
  guint paragraphs = 0;
  GtkTextLine *last_line = NULL;
  
  iter = layout->line_list;
   while (iter != NULL)
    {
      if (iter->line != last_line)
        {
          printf ("%5u  paragraph (%p)\n", paragraphs, iter->line);
          ++paragraphs;
          last_line = iter->line;
        }
      
      printf ("  %5u  y: %d len: %d start: %d bytes: %d\n",
             wrapped, iter->y, iter->length, iter->byte_offset,
             iter->byte_count);

      ++wrapped;
      iter = iter->next;
    }

  printf ("Layout %s recompute\n",
         layout->need_recompute ? "needs" : "doesn't need");

  printf ("Layout pars: %u lines: %u size: %d x %d Screen width: %d\n",
         paragraphs, wrapped, layout->width,
         layout->height, layout->screen_width);
#endif
}