gtktextdisplay.c 31.1 KB
Newer Older
1
2
3
4
5
6
7
/* gtktextdisplay.c - display layed-out 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
 *
8
9
 * This file can be used under your choice of two licenses, the LGPL
 * and the original Tk license.
10
 *
11
 * LGPL:
12
 *
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * Original Tk license:
28
29
30
31
32
 *
 * 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.
33
 *
34
35
36
37
38
39
40
41
42
 * 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.
43
 *
44
45
46
47
48
 * 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.
49
 *
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
 * 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.
68
69
70
71
72
73
74
 *
 */
/*
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
 * file for a list of people on the GTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
75
76
 */

77
#define GTK_TEXT_USE_INTERNAL_UNSUPPORTED_API
78
#include "gtktextdisplay.h"
79
80
81
/* DO NOT go putting private headers in here. This file should only
 * use the semi-public headers, as with gtktextview.c.
 */
Tor Lillqvist's avatar
Tor Lillqvist committed
82
83

#include <pango/pango.h>
84
85
86
87
88
89

typedef struct _GtkTextRenderState GtkTextRenderState;

struct _GtkTextRenderState
{
  GtkWidget *widget;
90

91
  GtkTextAppearance *last_appearance;
92
  GtkTextAppearance *last_bg_appearance;
93
94
95
96
97
98
  GdkGC *fg_gc;
  GdkGC *bg_gc;
  GdkRectangle clip_rect;
};

static void       get_item_properties (PangoItem           *item,
99
                                       GtkTextAppearance  **appearance);
100
static GdkRegion *get_selected_clip   (GtkTextRenderState  *render_state,
101
102
103
104
105
106
107
                                       PangoLayout         *layout,
                                       PangoLayoutLine     *line,
                                       int                  x,
                                       int                  y,
                                       int                  height,
                                       int                  start_index,
                                       int                  end_index);
108
109
110

static GtkTextRenderState *
gtk_text_render_state_new (GtkWidget    *widget,
111
112
                           GdkDrawable  *drawable,
                           GdkRectangle *clip_rect)
113
114
115
116
117
118
119
120
121
122
123
124
125
126
{
  GtkTextRenderState *state = g_new0 (GtkTextRenderState, 1);

  state->widget = widget;
  state->fg_gc = gdk_gc_new (drawable);
  state->bg_gc = gdk_gc_new (drawable);
  state->clip_rect = *clip_rect;

  return state;
}

static void
gtk_text_render_state_destroy (GtkTextRenderState *state)
{
Manish Singh's avatar
Manish Singh committed
127
128
  g_object_unref (state->fg_gc);
  g_object_unref (state->bg_gc);
129

130
131
132
133
134
  g_free (state);
}

static void
gtk_text_render_state_set_color (GtkTextRenderState *state,
135
136
                                 GdkGC              *gc,
                                 GdkColor           *color)
137
138
139
140
141
142
143
{
  gdk_colormap_alloc_color (gtk_widget_get_colormap (state->widget), color, FALSE, TRUE);
  gdk_gc_set_foreground (gc, color);
}

static void
gtk_text_render_state_update (GtkTextRenderState *state,
144
                              GtkTextAppearance  *new_appearance)
145
{
146
147
  GdkScreen *screen = gtk_widget_get_screen (state->widget);
  
148
149
150
  if (!state->last_appearance ||
      !gdk_color_equal (&new_appearance->fg_color, &state->last_appearance->fg_color))
    gtk_text_render_state_set_color (state, state->fg_gc, &new_appearance->fg_color);
151

152
153
154
155
  if (!state->last_appearance ||
      new_appearance->fg_stipple != state->last_appearance->fg_stipple)
    {
      if (new_appearance->fg_stipple)
156
        {
157
158
159
160
161
162
163
164
165
	  if (screen == gdk_drawable_get_screen (new_appearance->fg_stipple))
	    {
	      gdk_gc_set_fill (state->fg_gc, GDK_STIPPLED);
	      gdk_gc_set_stipple (state->fg_gc, new_appearance->fg_stipple);
	    }
	  else
	    g_warning ("gtk_text_render_state_update:\n"
		       "The foreground stipple bitmap has been created on the wrong screen.\n"
		       "Ignoring the stipple bitmap information.");
166
        }
167
      else
168
169
170
        {
          gdk_gc_set_fill (state->fg_gc, GDK_SOLID);
        }
171
    }
172

173
174
  if (new_appearance->draw_bg)
    {
175
176
      if (!state->last_bg_appearance ||
          !gdk_color_equal (&new_appearance->bg_color, &state->last_bg_appearance->bg_color))
177
178
        gtk_text_render_state_set_color (state, state->bg_gc, &new_appearance->bg_color);

179
180
      if (!state->last_bg_appearance ||
          new_appearance->bg_stipple != state->last_bg_appearance->bg_stipple)
181
182
183
        {
          if (new_appearance->bg_stipple)
            {
184
185
186
187
188
189
190
191
192
193
	      if (screen == gdk_drawable_get_screen (new_appearance->bg_stipple))
		{
		  gdk_gc_set_fill (state->bg_gc, GDK_STIPPLED);
		  gdk_gc_set_stipple (state->bg_gc, new_appearance->bg_stipple);
		}
	      else
		g_warning ("gtk_text_render_state_update:\n"
			   "The background stipple bitmap has been created on the wrong screen.\n"
			   "Ignoring the stipple bitmap information.");

194
195
196
197
198
199
            }
          else
            {
              gdk_gc_set_fill (state->bg_gc, GDK_SOLID);
            }
        }
200
201

      state->last_bg_appearance = new_appearance;
202
203
204
205
206
    }

  state->last_appearance = new_appearance;
}

207
208
209
210
211
static void
get_shape_extents (PangoLayoutRun *run,
                   PangoRectangle *ink_rect,
                   PangoRectangle *logical_rect)
{
212
  GSList *tmp_list = run->item->analysis.extra_attrs;
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
    
  while (tmp_list)
    {
      PangoAttribute *attr = tmp_list->data;

      if (attr->klass->type == PANGO_ATTR_SHAPE)
	{          
	  if (logical_rect)
	    *logical_rect = ((PangoAttrShape *)attr)->logical_rect;

	  if (ink_rect)
	    *ink_rect = ((PangoAttrShape *)attr)->ink_rect;

          return;
        }

      tmp_list = tmp_list->next;
    }

  g_assert_not_reached ();
}

235
static void
236
render_layout_line (GdkDrawable        *drawable,
237
238
                    GtkTextRenderState *render_state,
                    PangoLayoutLine    *line,
239
                    GSList            **shaped_pointer,
240
241
                    int                 x,
                    int                 y,
242
243
                    gboolean            selected,
                    GList             **widgets)
244
245
246
247
248
249
{
  GSList *tmp_list = line->runs;
  PangoRectangle overall_rect;
  PangoRectangle logical_rect;
  PangoRectangle ink_rect;
  gint x_off = 0;
250
251
  GdkGC *fg_gc;
  
252
253
254
255
256
257
  pango_layout_line_get_extents (line, NULL, &overall_rect);

  while (tmp_list)
    {
      PangoLayoutRun *run = tmp_list->data;
      GtkTextAppearance *appearance;
258
259
260
261
      gint risen_y;
      gint shaped_width_pixels = 0;
      gboolean need_ink = FALSE;
      
262
263
264
265
      tmp_list = tmp_list->next;

      get_item_properties (run->item, &appearance);

266
267
268
269
270
      g_assert (appearance != NULL);
      
      risen_y = y - PANGO_PIXELS (appearance->rise);
      
      if (selected)
271
        {
272
273
274
275
	  if (GTK_WIDGET_HAS_FOCUS (render_state->widget))
	    fg_gc = render_state->widget->style->text_gc[GTK_STATE_SELECTED];
	  else
	    fg_gc = render_state->widget->style->text_gc [GTK_STATE_ACTIVE];
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
        }
      else
        {
          gtk_text_render_state_update (render_state, appearance);
          
          fg_gc = render_state->fg_gc;
        }
      
      if (appearance->underline != PANGO_UNDERLINE_NONE ||
          appearance->strikethrough)
        need_ink = TRUE;
      
      if (appearance->is_text)
        {
          if (need_ink)
291
            pango_glyph_string_extents (run->glyphs, run->item->analysis.font,
292
                                        &ink_rect, &logical_rect);
293
294
          else
            pango_glyph_string_extents (run->glyphs, run->item->analysis.font,
295
                                        NULL, &logical_rect);
296
        }
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
      else
        {
          if (need_ink)
            get_shape_extents (run, &ink_rect, &logical_rect);
          else
            get_shape_extents (run, NULL, &logical_rect);
        }
      
      if (appearance->draw_bg && !selected)
        gdk_draw_rectangle (drawable, render_state->bg_gc, TRUE,
                            x + PANGO_PIXELS (x_off) + PANGO_PIXELS (logical_rect.x),
                            risen_y + PANGO_PIXELS (logical_rect.y),
                            PANGO_PIXELS (logical_rect.width),
                            PANGO_PIXELS (logical_rect.height));

      if (appearance->is_text)
        gdk_draw_glyphs (drawable, fg_gc,
                         run->item->analysis.font,
                         x + PANGO_PIXELS (x_off),
                         risen_y, run->glyphs);
      else
318
        {
319
          GObject *shaped = (*shaped_pointer)->data;
320

321
          *shaped_pointer = (*shaped_pointer)->next;
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

          if (shaped == NULL)
            {
              /* This happens if we have an empty widget anchor. Draw
               * something empty-looking.
               */
              GdkRectangle shape_rect, draw_rect;

              shape_rect.x = x + x_off / PANGO_SCALE;
              shape_rect.y = risen_y - PANGO_PIXELS (logical_rect.height);
              shape_rect.width = PANGO_PIXELS (logical_rect.width);
              shape_rect.height = PANGO_PIXELS (logical_rect.height);

              if (gdk_rectangle_intersect (&shape_rect, &render_state->clip_rect,
                                           &draw_rect))
                {
                  gdk_draw_rectangle (drawable, render_state->fg_gc,
                                      FALSE, shape_rect.x, shape_rect.y,
                                      shape_rect.width, shape_rect.height);

                  gdk_draw_line (drawable, render_state->fg_gc,
                                 shape_rect.x, shape_rect.y,
                                 shape_rect.x + shape_rect.width,
                                 shape_rect.y + shape_rect.height);

                  gdk_draw_line (drawable, render_state->fg_gc,
                                 shape_rect.x + shape_rect.width, shape_rect.y,
                                 shape_rect.x,
                                 shape_rect.y + shape_rect.height);
                }
352
353

              shaped_width_pixels = shape_rect.width;
354
355
            }
          else if (GDK_IS_PIXBUF (shaped))
Havoc Pennington's avatar
Havoc Pennington committed
356
            {
357
358
359
360
361
362
363
364
365
366
              gint width, height;
              GdkRectangle pixbuf_rect, draw_rect;
              GdkPixbuf *pixbuf;

              pixbuf = GDK_PIXBUF (shaped);
              
              width = gdk_pixbuf_get_width (pixbuf);
              height = gdk_pixbuf_get_height (pixbuf);

              pixbuf_rect.x = x + x_off / PANGO_SCALE;
367
              pixbuf_rect.y = risen_y - height;
368
369
370
371
372
              pixbuf_rect.width = width;
              pixbuf_rect.height = height;

              if (gdk_rectangle_intersect (&pixbuf_rect, &render_state->clip_rect,
                                           &draw_rect))
Havoc Pennington's avatar
Havoc Pennington committed
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
                  GdkBitmap *mask = NULL;
              
                  if (gdk_pixbuf_get_has_alpha (pixbuf))
                    {
                      mask = gdk_pixmap_new (drawable,
                                             gdk_pixbuf_get_width (pixbuf),
                                             gdk_pixbuf_get_height (pixbuf),
                                             1);

                      gdk_pixbuf_render_threshold_alpha (pixbuf, mask,
                                                         0, 0, 0, 0,
                                                         gdk_pixbuf_get_width (pixbuf),
                                                         gdk_pixbuf_get_height (pixbuf),
                                                         128);

                    }

                  if (mask)
                    {
                      gdk_gc_set_clip_mask (render_state->fg_gc, mask);
                      gdk_gc_set_clip_origin (render_state->fg_gc,
                                              pixbuf_rect.x, pixbuf_rect.y);
                    }

398
399
400
401
402
403
404
405
406
407
                  gdk_draw_pixbuf (drawable,
				   render_state->fg_gc,
				   pixbuf,
				   draw_rect.x - pixbuf_rect.x,
				   draw_rect.y - pixbuf_rect.y,
				   draw_rect.x, draw_rect.y,
				   draw_rect.width,
				   draw_rect.height,
				   GDK_RGB_DITHER_NORMAL,
				   0, 0);
408
409
410
411
412

                  if (mask)
                    {
                      gdk_gc_set_clip_rectangle (render_state->fg_gc,
                                                 &render_state->clip_rect);
Manish Singh's avatar
Manish Singh committed
413
                      g_object_unref (mask);
414
                    }
Havoc Pennington's avatar
Havoc Pennington committed
415
416
                }

417
              shaped_width_pixels = width;
418
419
420
421
            }
          else if (GTK_IS_WIDGET (shaped))
            {
              GtkWidget *widget;
422
              
423
424
              widget = GTK_WIDGET (shaped);
              
425
              shaped_width_pixels = widget->allocation.width;
426

427
              if (widgets)
Havoc Pennington's avatar
Havoc Pennington committed
428
                {
Manish Singh's avatar
Manish Singh committed
429
                  g_object_ref (widget);
430
                  *widgets = g_list_prepend (*widgets, widget);
Havoc Pennington's avatar
Havoc Pennington committed
431
432
                }
            }
433
434
          else
            g_assert_not_reached (); /* not a pixbuf or widget */
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

      switch (appearance->underline)
        {
        case PANGO_UNDERLINE_NONE:
          break;
        case PANGO_UNDERLINE_DOUBLE:
          g_assert (need_ink);
          gdk_draw_line (drawable, fg_gc,
                         x + (x_off + ink_rect.x) / PANGO_SCALE - 1,
                         risen_y + 3,
                         x + (x_off + ink_rect.x + ink_rect.width) / PANGO_SCALE,
                         risen_y + 3);
          /* Fall through */
        case PANGO_UNDERLINE_SINGLE:
          g_assert (need_ink);
          gdk_draw_line (drawable, fg_gc,
                         x + (x_off + ink_rect.x) / PANGO_SCALE - 1,
                         risen_y + 1,
                         x + (x_off + ink_rect.x + ink_rect.width) / PANGO_SCALE,
                         risen_y + 1);
          break;
        case PANGO_UNDERLINE_LOW:
          g_assert (need_ink);
          gdk_draw_line (drawable, fg_gc,
                         x + (x_off + ink_rect.x) / PANGO_SCALE - 1,
                         risen_y + (ink_rect.y + ink_rect.height) / PANGO_SCALE + 1,
                         x + (x_off + ink_rect.x + ink_rect.width) / PANGO_SCALE,
                         risen_y + (ink_rect.y + ink_rect.height) / PANGO_SCALE + 1);
          break;
        }

      if (appearance->strikethrough)
        {          
          gint strikethrough_y = risen_y + (0.3 * logical_rect.y) / PANGO_SCALE;

          g_assert (need_ink);
          
          gdk_draw_line (drawable, fg_gc,
                         x + (x_off + ink_rect.x) / PANGO_SCALE - 1, strikethrough_y,
                         x + (x_off + ink_rect.x + ink_rect.width) / PANGO_SCALE, strikethrough_y);
        }

      if (appearance->is_text)
        x_off += logical_rect.width;
      else
        x_off += shaped_width_pixels * PANGO_SCALE;
482
483
484
    }
}

485
static void
486
render_para (GdkDrawable        *drawable,
487
488
             GtkTextRenderState *render_state,
             GtkTextLineDisplay *line_display,
489
             /* Top-left corner of paragraph including all margins */
490
491
492
             int                 x,
             int                 y,
             int                 selection_start_index,
493
494
             int                 selection_end_index,
             GList             **widgets)
495
{
496
  GSList *shaped_pointer = line_display->shaped_objects;
497
  PangoLayout *layout = line_display->layout;
498
  int byte_offset = 0;
499
500
501
  PangoLayoutIter *iter;
  PangoRectangle layout_logical;
  int screen_width;
502
503
  GdkGC *fg_gc, *bg_gc;
  gint state;
504
  
505
  gboolean first = TRUE;
506

507
  iter = pango_layout_get_iter (layout);
508

509
  pango_layout_iter_get_layout_extents (iter, NULL, &layout_logical);
510

511
512
513
514
515
516
517
  /* Adjust for margins */
  
  layout_logical.x += line_display->x_offset * PANGO_SCALE;
  layout_logical.y += line_display->top_margin * PANGO_SCALE;

  screen_width = line_display->total_width;
  
518
519
520
521
522
523
524
525
  if (GTK_WIDGET_HAS_FOCUS (render_state->widget))
    state = GTK_STATE_SELECTED;
  else
    state = GTK_STATE_ACTIVE;

  fg_gc = render_state->widget->style->text_gc [state];
  bg_gc = render_state->widget->style->base_gc [state];

526
527
528
  do
    {
      PangoLayoutLine *line = pango_layout_iter_get_line (iter);
529
      int selection_y, selection_height;
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
      int first_y, last_y;
      PangoRectangle line_rect;
      int baseline;
      
      pango_layout_iter_get_line_extents (iter, NULL, &line_rect);
      baseline = pango_layout_iter_get_baseline (iter);
      pango_layout_iter_get_line_yrange (iter, &first_y, &last_y);
      
      /* Adjust for margins */

      line_rect.x += line_display->x_offset * PANGO_SCALE;
      line_rect.y += line_display->top_margin * PANGO_SCALE;
      baseline += line_display->top_margin * PANGO_SCALE;

      /* Selection is the height of the line, plus top/bottom
       * margin if we're the first/last line
       */
      selection_y = y + PANGO_PIXELS (first_y) + line_display->top_margin;
      selection_height = PANGO_PIXELS (last_y) - PANGO_PIXELS (first_y);
549
550

      if (first)
551
552
553
554
        {
          selection_y -= line_display->top_margin;
          selection_height += line_display->top_margin;
        }
555
556
      
      if (pango_layout_iter_at_last_line (iter))
557
        selection_height += line_display->bottom_margin;
558
      
559
      first = FALSE;
560

561
      if (selection_start_index < byte_offset &&
562
563
564
          selection_end_index > line->length + byte_offset) /* All selected */
        {
          gdk_draw_rectangle (drawable,
565
                              bg_gc,
566
                              TRUE,
567
568
569
570
571
                              x + line_display->left_margin,
                              selection_y,
                              screen_width,
                              selection_height);

572
          render_layout_line (drawable, render_state, line, &shaped_pointer,
573
574
                              x + PANGO_PIXELS (line_rect.x),
                              y + PANGO_PIXELS (baseline),
575
576
                              TRUE,
                              widgets);
577
        }
578
      else
579
        {
580
          GSList *shaped_pointer_tmp = shaped_pointer;
581

582
583
584
585
          render_layout_line (drawable, render_state,
                              line, &shaped_pointer,
                              x + PANGO_PIXELS (line_rect.x),
                              y + PANGO_PIXELS (baseline),
586
587
                              FALSE,
                              widgets);
588

589
          if (selection_start_index <= byte_offset + line->length &&
590
591
592
593
              selection_end_index > byte_offset) /* Some selected */
            {
              GdkRegion *clip_region = get_selected_clip (render_state, layout, line,
                                                          x + line_display->x_offset,
594
595
                                                          selection_y,
                                                          selection_height,
596
                                                          selection_start_index, selection_end_index);
597
598
              gdk_gc_set_clip_region (fg_gc, clip_region);
              gdk_gc_set_clip_region (bg_gc, clip_region);
599
600

              gdk_draw_rectangle (drawable,
601
                                  bg_gc,
602
                                  TRUE,
603
604
605
                                  x + PANGO_PIXELS (line_rect.x),
                                  selection_y,
                                  PANGO_PIXELS (line_rect.width),
606
607
                                  selection_height);

608
              render_layout_line (drawable, render_state, line, &shaped_pointer_tmp,
609
610
                                  x + PANGO_PIXELS (line_rect.x),
                                  y + PANGO_PIXELS (baseline),
611
612
                                  TRUE,
                                  widgets);
613

614
615
              gdk_gc_set_clip_region (fg_gc, NULL);
              gdk_gc_set_clip_region (bg_gc, NULL);
616
617
618
619

              gdk_region_destroy (clip_region);

              /* Paint in the ends of the line */
620
              if (line_rect.x > line_display->left_margin * PANGO_SCALE &&
621
622
623
624
                  ((line_display->direction == GTK_TEXT_DIR_LTR && selection_start_index < byte_offset) ||
                   (line_display->direction == GTK_TEXT_DIR_RTL && selection_end_index > byte_offset + line->length)))
                {
                  gdk_draw_rectangle (drawable,
625
                                      bg_gc,
626
                                      TRUE,
627
628
629
                                      x + line_display->left_margin,
                                      selection_y,
                                      PANGO_PIXELS (line_rect.x) - line_display->left_margin,
630
631
632
                                      selection_height);
                }

633
634
              if (line_rect.x + line_rect.width <
                  (screen_width + line_display->left_margin) * PANGO_SCALE &&
635
636
637
                  ((line_display->direction == GTK_TEXT_DIR_LTR && selection_end_index > byte_offset + line->length) ||
                   (line_display->direction == GTK_TEXT_DIR_RTL && selection_start_index < byte_offset)))
                {
638
                  int nonlayout_width;
639

640
641
642
                  nonlayout_width =
                    line_display->left_margin + screen_width -
                    PANGO_PIXELS (line_rect.x) - PANGO_PIXELS (line_rect.width);
643
644

                  gdk_draw_rectangle (drawable,
645
                                      bg_gc,
646
                                      TRUE,
647
                                      x + PANGO_PIXELS (line_rect.x) + PANGO_PIXELS (line_rect.width),
648
                                      selection_y,
649
                                      nonlayout_width,
650
651
652
653
                                      selection_height);
                }
            }
        }
654
655
656

      byte_offset += line->length;
    }
657
658
659
  while (pango_layout_iter_next_line (iter));

  pango_layout_iter_free (iter);
660
661
662
663
}

static GdkRegion *
get_selected_clip (GtkTextRenderState *render_state,
664
665
666
667
668
669
670
                   PangoLayout        *layout,
                   PangoLayoutLine    *line,
                   int                 x,
                   int                 y,
                   int                 height,
                   int                 start_index,
                   int                 end_index)
671
672
673
674
675
676
677
678
679
680
681
682
{
  gint *ranges;
  gint n_ranges, i;
  GdkRegion *clip_region = gdk_region_new ();
  GdkRegion *tmp_region;

  pango_layout_line_get_x_ranges (line, start_index, end_index, &ranges, &n_ranges);

  for (i=0; i < n_ranges; i++)
    {
      GdkRectangle rect;

683
      rect.x = x + PANGO_PIXELS (ranges[2*i]);
684
      rect.y = y;
685
      rect.width = PANGO_PIXELS (ranges[2*i + 1]) - PANGO_PIXELS (ranges[2*i]);
686
      rect.height = height;
687
      
688
689
690
691
692
693
694
695
696
697
698
699
700
      gdk_region_union_with_rect (clip_region, &rect);
    }

  tmp_region = gdk_region_rectangle (&render_state->clip_rect);
  gdk_region_intersect (clip_region, tmp_region);
  gdk_region_destroy (tmp_region);

  g_free (ranges);
  return clip_region;
}

static void
get_item_properties (PangoItem          *item,
701
                     GtkTextAppearance **appearance)
702
{
703
  GSList *tmp_list = item->analysis.extra_attrs;
704

705
  *appearance = NULL;
706

707
708
709
710
711
  while (tmp_list)
    {
      PangoAttribute *attr = tmp_list->data;

      if (attr->klass->type == gtk_text_attr_appearance_type)
712
713
714
        {
          *appearance = &((GtkTextAttrAppearance *)attr)->appearance;
        }
715
716

      tmp_list = tmp_list->next;
717
718
719
720
721
    }
}

void
gtk_text_layout_draw (GtkTextLayout *layout,
722
723
                      GtkWidget *widget,
                      GdkDrawable *drawable,
724
		      GdkGC       *cursor_gc,
725
726
                      /* Location of the drawable
                         in layout coordinates */
727
728
729
730
731
732
733
                      gint x_offset,
                      gint y_offset,
                      /* Region of the layout to
                         render */
                      gint x,
                      gint y,
                      gint width,
734
735
736
                      gint height,
                      /* widgets to expose */
                      GList **widgets)
737
738
739
740
741
742
743
{
  GdkRectangle clip;
  gint current_y;
  GSList *cursor_list;
  GtkTextRenderState *render_state;
  GtkTextIter selection_start, selection_end;
  gboolean have_selection = FALSE;
744
745
746
  GSList *line_list;
  GSList *tmp_list;
  
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
  g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
  g_return_if_fail (layout->default_style != NULL);
  g_return_if_fail (layout->buffer != NULL);
  g_return_if_fail (drawable != NULL);
  g_return_if_fail (width >= 0);
  g_return_if_fail (height >= 0);

  if (width == 0 || height == 0)
    return;

  line_list =  gtk_text_layout_get_lines (layout, y + y_offset, y + y_offset + height, &current_y);
  current_y -= y_offset;

  if (line_list == NULL)
    return; /* nothing on the screen */

  clip.x = x;
  clip.y = y;
  clip.width = width;
  clip.height = height;

  render_state = gtk_text_render_state_new (widget, drawable, &clip);

  gdk_gc_set_clip_rectangle (render_state->fg_gc, &clip);
  gdk_gc_set_clip_rectangle (render_state->bg_gc, &clip);

  gtk_text_layout_wrap_loop_start (layout);
774

775
  if (gtk_text_buffer_get_selection_bounds (layout->buffer,
776
777
                                            &selection_start,
                                            &selection_end))
778
    have_selection = TRUE;
779

780
781
782
783
784
785
  tmp_list = line_list;
  while (tmp_list != NULL)
    {
      GtkTextLineDisplay *line_display;
      gint selection_start_index = -1;
      gint selection_end_index = -1;
786
787
      gboolean have_strong;
      gboolean have_weak;
788
789

      GtkTextLine *line = tmp_list->data;
790

791
      line_display = gtk_text_layout_get_line_display (layout, line, FALSE);
792

793
794
795
      if (line_display->height > 0)
        {
          g_assert (line_display->layout != NULL);
796
          
797
          if (have_selection)
798
            {
799
800
              GtkTextIter line_start, line_end;
              gint byte_count;
801
              
802
803
804
              gtk_text_layout_get_iter_at_line (layout,
                                                &line_start,
                                                line, 0);
805
806
807
              line_end = line_start;
              gtk_text_iter_forward_to_line_end (&line_end);
              byte_count = gtk_text_iter_get_line_index (&line_end);     
808

809
810
              if (gtk_text_iter_compare (&selection_start, &line_end) <= 0 &&
                  gtk_text_iter_compare (&selection_end, &line_start) >= 0)
811
812
813
814
815
816
817
818
819
820
821
                {
                  if (gtk_text_iter_compare (&selection_start, &line_start) >= 0)
                    selection_start_index = gtk_text_iter_get_line_index (&selection_start);
                  else
                    selection_start_index = -1;

                  if (gtk_text_iter_compare (&selection_end, &line_end) <= 0)
                    selection_end_index = gtk_text_iter_get_line_index (&selection_end);
                  else
                    selection_end_index = byte_count;
                }
822
823
            }

824
825
826
          render_para (drawable, render_state, line_display,
                       - x_offset,
                       current_y,
827
828
                       selection_start_index, selection_end_index,
                       widgets);
829

830
          /* We paint the cursors last, because they overlap another chunk
831
832
         and need to appear on top. */

833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
 	  have_strong = FALSE;
 	  have_weak = FALSE;
	  
	  cursor_list = line_display->cursors;
	  while (cursor_list)
	    {
	      GtkTextCursorDisplay *cursor = cursor_list->data;
 	      if (cursor->is_strong)
 		have_strong = TRUE;
 	      else
 		have_weak = TRUE;
	      
	      cursor_list = cursor_list->next;
 	    }
	  
848
849
850
851
          cursor_list = line_display->cursors;
          while (cursor_list)
            {
              GtkTextCursorDisplay *cursor = cursor_list->data;
852
853
	      GtkTextDirection dir;
 	      GdkRectangle cursor_location;
854
              GdkGC *gc;
855

856
              dir = line_display->direction;
857
858
859
860
861
862
863
864
865
866
867
 	      if (have_strong && have_weak)
 		{
 		  if (!cursor->is_strong)
 		    dir = (dir == GTK_TEXT_DIR_RTL) ? GTK_TEXT_DIR_LTR : GTK_TEXT_DIR_RTL;
 		}
 
 	      cursor_location.x = line_display->x_offset + cursor->x - x_offset;
 	      cursor_location.y = current_y + line_display->top_margin + cursor->y;
 	      cursor_location.width = 0;
 	      cursor_location.height = cursor->height;
 
868
	      gc = _gtk_get_insertion_cursor_gc (widget, cursor->is_strong);
869
	      gdk_gc_set_clip_rectangle(gc, &clip);
870
871
 	      _gtk_draw_insertion_cursor (widget, drawable, gc, &cursor_location,
                                          dir, have_strong && have_weak);
872
              gdk_gc_set_clip_rectangle (gc, NULL);
873

874
875
	      g_object_unref (gc);

876
877
878
879
              cursor_list = cursor_list->next;
            }
        } /* line_display->height > 0 */
          
880
881
      current_y += line_display->height;
      gtk_text_layout_free_line_display (layout, line_display);
882
883
884
      render_state->last_appearance = NULL;
      render_state->last_bg_appearance = NULL;
      
885
886
887
888
889
890
891
892
      tmp_list = g_slist_next (tmp_list);
    }

  gtk_text_layout_wrap_loop_end (layout);
  gtk_text_render_state_destroy (render_state);

  g_slist_free (line_list);
}