gtktextdisplay.c 30.4 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 "config.h"
79
#include "gtktextdisplay.h"
80
#include "gtkintl.h"
81 82 83
/* DO NOT go putting private headers in here. This file should only
 * use the semi-public headers, as with gtktextview.c.
 */
84

Matthias Clasen's avatar
Matthias Clasen committed
85
#define GTK_TYPE_TEXT_RENDERER            (_gtk_text_renderer_get_type())
86 87 88 89 90
#define GTK_TEXT_RENDERER(object)         (G_TYPE_CHECK_INSTANCE_CAST ((object), GTK_TYPE_TEXT_RENDERER, GtkTextRenderer))
#define GTK_IS_TEXT_RENDERER(object)      (G_TYPE_CHECK_INSTANCE_TYPE ((object), GTK_TYPE_TEXT_RENDERER))
#define GTK_TEXT_RENDERER_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_TEXT_RENDERER, GtkTextRendererClass))
#define GTK_IS_TEXT_RENDERER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_TEXT_RENDERER))
#define GTK_TEXT_RENDERER_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_TEXT_RENDERER, GtkTextRendererClass))
91

92 93
typedef struct _GtkTextRenderer      GtkTextRenderer;
typedef struct _GtkTextRendererClass GtkTextRendererClass;
94

95 96 97 98 99 100
enum {
  NORMAL,
  SELECTED,
  CURSOR
};

101
struct _GtkTextRenderer
102
{
103
  GdkPangoRenderer parent_instance;
104

105 106 107 108
  GdkScreen *screen;

  GtkWidget *widget;
  GdkDrawable *drawable;
109
  GdkRectangle clip_rect;
110 111 112 113
  
  GdkColor *error_color;	/* Error underline color for this widget */
  GList *widgets;		/* widgets encountered when drawing */
  
114
  int state;
115 116
};

117
struct _GtkTextRendererClass
118
{
119 120
  GdkPangoRendererClass parent_class;
};
121

Matthias Clasen's avatar
Matthias Clasen committed
122
G_DEFINE_TYPE (GtkTextRenderer, _gtk_text_renderer, GDK_TYPE_PANGO_RENDERER)
123

124 125 126 127
static GdkColor *
text_renderer_get_error_color (GtkTextRenderer *text_renderer)
{
  static const GdkColor red = { 0, 0xffff, 0, 0 };
128

129 130
  if (!text_renderer->error_color)
    gtk_widget_style_get (text_renderer->widget,
131
			  "error-underline-color", &text_renderer->error_color,
132 133 134 135 136 137
			  NULL);
  
  if (!text_renderer->error_color)
    text_renderer->error_color = gdk_color_copy (&red);

  return text_renderer->error_color;
138 139 140
}

static void
141 142 143
text_renderer_set_gdk_color (GtkTextRenderer *text_renderer,
			     PangoRenderPart  part,
			     GdkColor        *gdk_color)
144
{
145 146 147 148 149
  PangoRenderer *renderer = PANGO_RENDERER (text_renderer);

  if (gdk_color)
    {
      PangoColor color;
150

151 152 153 154 155 156 157 158 159
      color.red = gdk_color->red;
      color.green = gdk_color->green;
      color.blue = gdk_color->blue;
      
      pango_renderer_set_color (renderer, part, &color);
    }
  else
    pango_renderer_set_color (renderer, part, NULL);
	   
160 161
}

162 163
static GtkTextAppearance *
get_item_appearance (PangoItem *item)
164
{
165 166 167 168 169 170 171 172 173 174 175 176 177
  GSList *tmp_list = item->analysis.extra_attrs;

  while (tmp_list)
    {
      PangoAttribute *attr = tmp_list->data;

      if (attr->klass->type == gtk_text_attr_appearance_type)
	return &((GtkTextAttrAppearance *)attr)->appearance;

      tmp_list = tmp_list->next;
    }

  return NULL;
178 179 180
}

static void
181 182
gtk_text_renderer_prepare_run (PangoRenderer  *renderer,
			       PangoLayoutRun *run)
183
{
184 185 186 187 188
  GtkTextRenderer *text_renderer = GTK_TEXT_RENDERER (renderer);
  GdkPangoRenderer *gdk_renderer = GDK_PANGO_RENDERER (renderer);
  GdkColor *bg_color, *fg_color, *underline_color;
  GtkTextAppearance *appearance;

Matthias Clasen's avatar
Matthias Clasen committed
189
  PANGO_RENDERER_CLASS (_gtk_text_renderer_parent_class)->prepare_run (renderer, run);
190 191 192

  appearance = get_item_appearance (run->item);
  g_assert (appearance != NULL);
193 194

  if (appearance->draw_bg && text_renderer->state == NORMAL)
195 196 197 198 199
    bg_color = &appearance->bg_color;
  else
    bg_color = NULL;
  
  text_renderer_set_gdk_color (text_renderer, PANGO_RENDER_PART_BACKGROUND, bg_color);
200

201
  if (text_renderer->state == SELECTED)
202
    {
203
      if (gtk_widget_has_focus (text_renderer->widget))
204
	fg_color = &text_renderer->widget->style->text[GTK_STATE_SELECTED];
205
      else
206
	fg_color = &text_renderer->widget->style->text[GTK_STATE_ACTIVE];
207
    }
208
  else if (text_renderer->state == CURSOR && gtk_widget_has_focus (text_renderer->widget))
209
    fg_color = &text_renderer->widget->style->base[GTK_STATE_NORMAL];
210 211
  else
    fg_color = &appearance->fg_color;
212

213 214
  text_renderer_set_gdk_color (text_renderer, PANGO_RENDER_PART_FOREGROUND, fg_color);
  text_renderer_set_gdk_color (text_renderer, PANGO_RENDER_PART_STRIKETHROUGH, fg_color);
215

216 217 218 219
  if (appearance->underline == PANGO_UNDERLINE_ERROR)
    underline_color = text_renderer_get_error_color (text_renderer);
  else
    underline_color = fg_color;
220

221
  text_renderer_set_gdk_color (text_renderer, PANGO_RENDER_PART_UNDERLINE, underline_color);
222 223
}

224 225 226 227 228
static void
gtk_text_renderer_draw_shape (PangoRenderer   *renderer,
			      PangoAttrShape  *attr,
			      int              x,
			      int              y)
229
{
230
  GtkTextRenderer *text_renderer = GTK_TEXT_RENDERER (renderer);
231
  GdkColor *fg;
232

233
  if (text_renderer->state == SELECTED)
234
    {
235
      if (gtk_widget_has_focus (text_renderer->widget))
236
	fg = &text_renderer->widget->style->text[GTK_STATE_SELECTED];
237
      else
238
	fg = &text_renderer->widget->style->text[GTK_STATE_SELECTED];
239
    }
240
  else if (text_renderer->state == CURSOR && gtk_widget_has_focus (text_renderer->widget))
241
    fg = &text_renderer->widget->style->base[GTK_STATE_NORMAL];
242
  else
243
    fg = &text_renderer->widget->style->text[GTK_STATE_NORMAL];
244 245
  
  if (attr->data == NULL)
246
    {
247 248 249 250
      /* This happens if we have an empty widget anchor. Draw
       * something empty-looking.
       */
      GdkRectangle shape_rect, draw_rect;
251
      
252 253 254 255
      shape_rect.x = PANGO_PIXELS (x);
      shape_rect.y = PANGO_PIXELS (y + attr->logical_rect.y);
      shape_rect.width = PANGO_PIXELS (x + attr->logical_rect.width) - shape_rect.x;
      shape_rect.height = PANGO_PIXELS (y + attr->logical_rect.y + attr->logical_rect.height) - shape_rect.y;
256
      
257 258
      if (gdk_rectangle_intersect (&shape_rect, &text_renderer->clip_rect,
				   &draw_rect))
259
	{
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
          cairo_t *cr = gdk_cairo_create (text_renderer->drawable);

          cairo_set_line_width (cr, 1.0);
          gdk_cairo_set_source_color (cr, fg);

          cairo_rectangle (cr,
                           shape_rect.x + 0.5, shape_rect.y + 0.5,
			   shape_rect.width - 1, shape_rect.height - 1);
          cairo_move_to (cr, shape_rect.x + 0.5, shape_rect.y + 0.5);
          cairo_line_to (cr, 
			 shape_rect.x + shape_rect.width - 0.5,
			 shape_rect.y + shape_rect.height - 0.5);
          cairo_move_to (cr, shape_rect.x + 0.5,
                         shape_rect.y + shape_rect.height - 0.5);
          cairo_line_to (cr, shape_rect.x + shape_rect.width - 0.5,
			 shape_rect.y + 0.5);

          cairo_stroke (cr);
          
          cairo_destroy (cr);
280
	}
281 282 283 284 285 286
    }
  else if (GDK_IS_PIXBUF (attr->data))
    {
      gint width, height;
      GdkRectangle pixbuf_rect, draw_rect;
      GdkPixbuf *pixbuf;
287
      
288 289 290 291 292 293 294 295 296 297 298 299 300
      pixbuf = GDK_PIXBUF (attr->data);
      
      width = gdk_pixbuf_get_width (pixbuf);
      height = gdk_pixbuf_get_height (pixbuf);
      
      pixbuf_rect.x = PANGO_PIXELS (x);
      pixbuf_rect.y = PANGO_PIXELS (y) - height;
      pixbuf_rect.width = width;
      pixbuf_rect.height = height;
      
      if (gdk_rectangle_intersect (&pixbuf_rect, &text_renderer->clip_rect,
				   &draw_rect))
	{
301 302 303 304 305 306 307
          cairo_t *cr = gdk_cairo_create (text_renderer->drawable);

          gdk_cairo_set_source_pixbuf (cr, pixbuf, pixbuf_rect.x, pixbuf_rect.y);
          gdk_cairo_rectangle (cr, &draw_rect);
          cairo_fill (cr);

          cairo_destroy (cr);
308
	}
309
    }
310 311 312 313 314
  else if (GTK_IS_WIDGET (attr->data))
    {
      GtkWidget *widget;
      
      widget = GTK_WIDGET (attr->data);
315

316 317 318 319 320
      text_renderer->widgets = g_list_prepend (text_renderer->widgets,
					       g_object_ref (widget));
    }
  else
    g_assert_not_reached (); /* not a pixbuf or widget */
321 322
}

323
static void
324
gtk_text_renderer_finalize (GObject *object)
325
{
Matthias Clasen's avatar
Matthias Clasen committed
326
  G_OBJECT_CLASS (_gtk_text_renderer_parent_class)->finalize (object);
327
}
328

329
static void
Matthias Clasen's avatar
Matthias Clasen committed
330
_gtk_text_renderer_init (GtkTextRenderer *renderer)
331
{
332 333
}

334
static void
Matthias Clasen's avatar
Matthias Clasen committed
335
_gtk_text_renderer_class_init (GtkTextRendererClass *klass)
336
{
337
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
338
  
339 340 341 342
  PangoRendererClass *renderer_class = PANGO_RENDERER_CLASS (klass);
  
  renderer_class->prepare_run = gtk_text_renderer_prepare_run;
  renderer_class->draw_shape = gtk_text_renderer_draw_shape;
343

344 345
  object_class->finalize = gtk_text_renderer_finalize;
}
346

347
static void
348 349
text_renderer_set_state (GtkTextRenderer *text_renderer,
			 int              state)
350
{
351
  text_renderer->state = state;
352
}
353

354 355 356 357 358 359 360 361 362
static void
text_renderer_begin (GtkTextRenderer *text_renderer,
		     GtkWidget       *widget,
		     GdkDrawable     *drawable,
		     GdkRectangle    *clip_rect)
{
  text_renderer->widget = widget;
  text_renderer->drawable = drawable;
  text_renderer->clip_rect = *clip_rect;
363

364 365 366 367
  gdk_pango_renderer_set_drawable (GDK_PANGO_RENDERER (text_renderer), drawable);
  gdk_pango_renderer_set_gc (GDK_PANGO_RENDERER (text_renderer),
			     widget->style->text_gc[widget->state]);
}
368

369 370 371 372 373 374
/* Returns a GSList of (referenced) widgets encountered while drawing.
 */
static GList *
text_renderer_end (GtkTextRenderer *text_renderer)
{
  GList *widgets = text_renderer->widgets;
375

376 377
  text_renderer->widget = NULL;
  text_renderer->drawable = NULL;
378

379
  text_renderer->widgets = NULL;
380

381 382 383 384 385
  if (text_renderer->error_color)
    {
      gdk_color_free (text_renderer->error_color);
      text_renderer->error_color = NULL;
    }
386

387 388
  gdk_pango_renderer_set_drawable (GDK_PANGO_RENDERER (text_renderer), NULL);
  gdk_pango_renderer_set_gc (GDK_PANGO_RENDERER (text_renderer), NULL);
Havoc Pennington's avatar
Havoc Pennington committed
389

390 391
  return widgets;
}
392

393

394
static cairo_region_t *
395 396 397 398 399 400 401 402 403 404 405
get_selected_clip (GtkTextRenderer    *text_renderer,
                   PangoLayout        *layout,
                   PangoLayoutLine    *line,
                   int                 x,
                   int                 y,
                   int                 height,
                   int                 start_index,
                   int                 end_index)
{
  gint *ranges;
  gint n_ranges, i;
406 407
  cairo_region_t *clip_region = cairo_region_create ();
  cairo_region_t *tmp_region;
408

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

411 412 413
  for (i=0; i < n_ranges; i++)
    {
      GdkRectangle rect;
414

415 416 417 418 419
      rect.x = x + PANGO_PIXELS (ranges[2*i]);
      rect.y = y;
      rect.width = PANGO_PIXELS (ranges[2*i + 1]) - PANGO_PIXELS (ranges[2*i]);
      rect.height = height;
      
420
      cairo_region_union_rectangle (clip_region, &rect);
421
    }
422

423 424 425
  tmp_region = cairo_region_create_rectangle (&text_renderer->clip_rect);
  cairo_region_intersect (clip_region, tmp_region);
  cairo_region_destroy (tmp_region);
426 427 428

  g_free (ranges);
  return clip_region;
429 430
}

431
static void
432
render_para (GtkTextRenderer    *text_renderer,
433
             GtkTextLineDisplay *line_display,
434
             /* Top-left corner of paragraph including all margins */
435 436 437
             int                 x,
             int                 y,
             int                 selection_start_index,
438
             int                 selection_end_index)
439 440
{
  PangoLayout *layout = line_display->layout;
441
  int byte_offset = 0;
442 443 444
  PangoLayoutIter *iter;
  PangoRectangle layout_logical;
  int screen_width;
445 446
  GdkColor *selection;
  GdkGC *fg_gc;
447
  gint state;
448
  
449
  gboolean first = TRUE;
450

451
  iter = pango_layout_get_iter (layout);
452

453
  pango_layout_iter_get_layout_extents (iter, NULL, &layout_logical);
454

455 456 457 458 459 460 461
  /* 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;
  
462
  if (gtk_widget_has_focus (text_renderer->widget))
463 464 465 466
    state = GTK_STATE_SELECTED;
  else
    state = GTK_STATE_ACTIVE;

467
  selection = &text_renderer->widget->style->base [state];
468
  fg_gc = text_renderer->widget->style->text_gc[text_renderer->widget->state];
469

470 471
  do
    {
472
      PangoLayoutLine *line = pango_layout_iter_get_line_readonly (iter);
473
      int selection_y, selection_height;
474 475 476
      int first_y, last_y;
      PangoRectangle line_rect;
      int baseline;
477
      gboolean at_last_line;
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
      
      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);
494 495

      if (first)
496 497 498 499
        {
          selection_y -= line_display->top_margin;
          selection_height += line_display->top_margin;
        }
500 501 502

      at_last_line = pango_layout_iter_at_last_line (iter);
      if (at_last_line)
503
        selection_height += line_display->bottom_margin;
504
      
505
      first = FALSE;
506

507
      if (selection_start_index < byte_offset &&
508 509
          selection_end_index > line->length + byte_offset) /* All selected */
        {
510 511 512 513 514 515 516
          cairo_t *cr = gdk_cairo_create (text_renderer->drawable);
          gdk_cairo_set_source_color (cr, selection);
          cairo_rectangle (cr, 
                           x + line_display->left_margin, selection_y,
                           screen_width, selection_height);
          cairo_fill (cr);
          cairo_destroy (cr);
517

518
	  text_renderer_set_state (text_renderer, SELECTED);
519 520 521 522
	  pango_renderer_draw_layout_line (PANGO_RENDERER (text_renderer),
					   line, 
					   PANGO_SCALE * x + line_rect.x,
					   PANGO_SCALE * y + baseline);
523
        }
524
      else
525
        {
526 527
          if (line_display->pg_bg_color)
            {
528 529 530 531 532 533 534 535 536
              cairo_t *cr = gdk_cairo_create (text_renderer->drawable);

              gdk_cairo_set_source_color (cr, line_display->pg_bg_color);
              cairo_rectangle (cr, 
                               x + line_display->left_margin, selection_y,
                               screen_width, selection_height);
              cairo_fill (cr);

              cairo_destroy (cr);
537 538
            }
        
539
	  text_renderer_set_state (text_renderer, NORMAL);
540 541 542 543
	  pango_renderer_draw_layout_line (PANGO_RENDERER (text_renderer),
					   line, 
					   PANGO_SCALE * x + line_rect.x,
					   PANGO_SCALE * y + baseline);
544

545 546 547 548 549 550 551
	  /* Check if some part of the line is selected; the newline
	   * that is after line->length for the last line of the
	   * paragraph counts as part of the line for this
	   */
          if ((selection_start_index < byte_offset + line->length ||
	       (selection_start_index == byte_offset + line->length && pango_layout_iter_at_last_line (iter))) &&
	      selection_end_index > byte_offset)
552
            {
553
              cairo_t *cr;
554
              cairo_region_t *clip_region = get_selected_clip (text_renderer, layout, line,
555
                                                          x + line_display->x_offset,
556 557
                                                          selection_y,
                                                          selection_height,
558
                                                          selection_start_index, selection_end_index);
559

560 561 562 563 564 565 566 567 568 569 570 571 572 573
              cr = gdk_cairo_create (text_renderer->drawable);
              gdk_cairo_region (cr, clip_region);
              cairo_clip (cr);

              gdk_cairo_set_source_color (cr, selection);
              cairo_rectangle (cr,
                               x + PANGO_PIXELS (line_rect.x),
                               selection_y,
                               PANGO_PIXELS (line_rect.width),
                               selection_height);
              cairo_fill (cr);

              cairo_destroy (cr);

574 575 576 577 578
	      /* When we change the clip on the foreground GC, we have to set
	       * it on the rendererer again, since the rendererer might have
	       * copied the GC to change attributes.
	       */
	      gdk_pango_renderer_set_gc (GDK_PANGO_RENDERER (text_renderer), NULL);
579
              gdk_gc_set_clip_region (fg_gc, clip_region);
580
	      gdk_pango_renderer_set_gc (GDK_PANGO_RENDERER (text_renderer), fg_gc);
581

582
	      text_renderer_set_state (text_renderer, SELECTED);
583 584 585 586
	      pango_renderer_draw_layout_line (PANGO_RENDERER (text_renderer),
					       line, 
					       PANGO_SCALE * x + line_rect.x,
					       PANGO_SCALE * y + baseline);
587

588
	      gdk_pango_renderer_set_gc (GDK_PANGO_RENDERER (text_renderer), NULL);
589
              gdk_gc_set_clip_region (fg_gc, NULL);
590 591
	      gdk_pango_renderer_set_gc (GDK_PANGO_RENDERER (text_renderer), fg_gc);
	      
592
              cairo_region_destroy (clip_region);
593 594

              /* Paint in the ends of the line */
595
              if (line_rect.x > line_display->left_margin * PANGO_SCALE &&
596 597 598
                  ((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)))
                {
599 600 601 602 603 604 605 606 607 608
                  cairo_t *cr = gdk_cairo_create (text_renderer->drawable);

                  gdk_cairo_set_source_color (cr, selection);
                  cairo_rectangle (cr,
                                   x + line_display->left_margin,
                                   selection_y,
                                   PANGO_PIXELS (line_rect.x) - line_display->left_margin,
                                   selection_height);
                  cairo_fill (cr);
                  cairo_destroy (cr);
609 610
                }

611 612
              if (line_rect.x + line_rect.width <
                  (screen_width + line_display->left_margin) * PANGO_SCALE &&
613 614 615
                  ((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)))
                {
616
                  cairo_t *cr = gdk_cairo_create (text_renderer->drawable);
617
                  int nonlayout_width;
618

619 620 621
                  nonlayout_width =
                    line_display->left_margin + screen_width -
                    PANGO_PIXELS (line_rect.x) - PANGO_PIXELS (line_rect.width);
622

623 624 625 626 627 628 629 630
                  gdk_cairo_set_source_color (cr, selection);
                  cairo_rectangle (cr,
                                   x + PANGO_PIXELS (line_rect.x) + PANGO_PIXELS (line_rect.width),
                                   selection_y,
                                   nonlayout_width,
                                   selection_height);
                  cairo_fill (cr);
                  cairo_destroy (cr);
631 632
                }
            }
633
	  else if (line_display->has_block_cursor &&
634
		   gtk_widget_has_focus (text_renderer->widget) &&
635 636 637 638 639
		   byte_offset <= line_display->insert_index &&
		   (line_display->insert_index < byte_offset + line->length ||
		    (at_last_line && line_display->insert_index == byte_offset + line->length)))
	    {
	      GdkRectangle cursor_rect;
640 641
              GdkColor cursor_color;
              cairo_t *cr;
642 643 644

	      /* we draw text using base color on filled cursor rectangle of cursor color
	       * (normally white on black) */
645
	      _gtk_widget_get_cursor_color (text_renderer->widget, &cursor_color);
646 647

	      cursor_rect.x = x + line_display->x_offset + line_display->block_cursor.x;
648
	      cursor_rect.y = y + line_display->block_cursor.y + line_display->top_margin;
649
	      cursor_rect.width = line_display->block_cursor.width;
650
	      cursor_rect.height = line_display->block_cursor.height;
651

652 653 654
              cr = gdk_cairo_create (text_renderer->drawable);
              gdk_cairo_rectangle (cr, &cursor_rect);
              cairo_clip (cr);
655

656 657
              gdk_cairo_set_source_color (cr, &cursor_color);
              cairo_paint (cr);
658

659
              cairo_destroy (cr);
660

661 662 663 664 665 666 667
	      /* draw text under the cursor if any */
	      if (!line_display->cursor_at_line_end)
		{
		  GdkGC *cursor_text_gc;

		  cursor_text_gc = text_renderer->widget->style->base_gc[text_renderer->widget->state];
		  gdk_gc_set_clip_rectangle (cursor_text_gc, &cursor_rect);
668

669 670 671 672 673 674 675 676 677 678 679 680
		  gdk_pango_renderer_set_gc (GDK_PANGO_RENDERER (text_renderer), cursor_text_gc);
		  text_renderer_set_state (text_renderer, CURSOR);

		  pango_renderer_draw_layout_line (PANGO_RENDERER (text_renderer),
						   line,
						   PANGO_SCALE * x + line_rect.x,
						   PANGO_SCALE * y + baseline);

		  gdk_pango_renderer_set_gc (GDK_PANGO_RENDERER (text_renderer), fg_gc);
		  gdk_gc_set_clip_region (cursor_text_gc, NULL);
		}
	    }
681
        }
682 683 684

      byte_offset += line->length;
    }
685 686 687
  while (pango_layout_iter_next_line (iter));

  pango_layout_iter_free (iter);
688 689
}

690 691
static void
on_renderer_display_closed (GdkDisplay       *display,
692
                            gboolean          is_error,
693
			    GtkTextRenderer  *text_renderer)
694
{
695 696 697
  g_signal_handlers_disconnect_by_func (text_renderer->screen,
					(gpointer)on_renderer_display_closed,
					text_renderer);
698
  g_object_set_data (G_OBJECT (text_renderer->screen), I_("gtk-text-renderer"), NULL);
699 700
}

701 702
static GtkTextRenderer *
get_text_renderer (GdkScreen *screen)
703
{
704
  GtkTextRenderer *text_renderer;
705

706 707 708 709
  g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
  
  text_renderer = g_object_get_data (G_OBJECT (screen), "gtk-text-renderer");
  if (!text_renderer)
710
    {
711 712 713
      text_renderer = g_object_new (GTK_TYPE_TEXT_RENDERER, "screen", screen, NULL);
      text_renderer->screen = screen;
      
714
      g_object_set_data_full (G_OBJECT (screen), I_("gtk-text-renderer"), text_renderer,
715
			      (GDestroyNotify)g_object_unref);
716

717 718 719
      g_signal_connect_object (gdk_screen_get_display (screen), "closed",
                               G_CALLBACK (on_renderer_display_closed),
                               text_renderer, 0);
720
    }
721 722

  return text_renderer;
723 724 725 726
}

void
gtk_text_layout_draw (GtkTextLayout *layout,
727 728
                      GtkWidget *widget,
                      GdkDrawable *drawable,
729
		      GdkGC       *cursor_gc,
730 731
                      /* Location of the drawable
                         in layout coordinates */
732 733 734 735 736 737 738
                      gint x_offset,
                      gint y_offset,
                      /* Region of the layout to
                         render */
                      gint x,
                      gint y,
                      gint width,
739 740 741
                      gint height,
                      /* widgets to expose */
                      GList **widgets)
742 743 744 745
{
  GdkRectangle clip;
  gint current_y;
  GSList *cursor_list;
746
  GtkTextRenderer *text_renderer;
747 748
  GtkTextIter selection_start, selection_end;
  gboolean have_selection = FALSE;
749 750
  GSList *line_list;
  GSList *tmp_list;
751
  GList *tmp_widgets;
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;

774
  text_renderer = get_text_renderer (gdk_drawable_get_screen (drawable));
775

776
  text_renderer_begin (text_renderer, widget, drawable, &clip);
777 778

  gtk_text_layout_wrap_loop_start (layout);
779

780
  if (gtk_text_buffer_get_selection_bounds (layout->buffer,
781 782
                                            &selection_start,
                                            &selection_end))
783
    have_selection = TRUE;
784

785 786 787 788 789 790
  tmp_list = line_list;
  while (tmp_list != NULL)
    {
      GtkTextLineDisplay *line_display;
      gint selection_start_index = -1;
      gint selection_end_index = -1;
791 792
      gboolean have_strong;
      gboolean have_weak;
793 794

      GtkTextLine *line = tmp_list->data;
795

796
      line_display = gtk_text_layout_get_line_display (layout, line, FALSE);
797

798 799 800
      if (line_display->height > 0)
        {
          g_assert (line_display->layout != NULL);
801
          
802
          if (have_selection)
803
            {
804 805
              GtkTextIter line_start, line_end;
              gint byte_count;
806
              
807 808 809
              gtk_text_layout_get_iter_at_line (layout,
                                                &line_start,
                                                line, 0);
810
              line_end = line_start;
811 812
	      if (!gtk_text_iter_ends_line (&line_end))
		gtk_text_iter_forward_to_line_end (&line_end);
813
              byte_count = gtk_text_iter_get_visible_line_index (&line_end);     
814

815 816
              if (gtk_text_iter_compare (&selection_start, &line_end) <= 0 &&
                  gtk_text_iter_compare (&selection_end, &line_start) >= 0)
817 818
                {
                  if (gtk_text_iter_compare (&selection_start, &line_start) >= 0)
819
                    selection_start_index = gtk_text_iter_get_visible_line_index (&selection_start);
820 821 822 823
                  else
                    selection_start_index = -1;

                  if (gtk_text_iter_compare (&selection_end, &line_end) <= 0)
824
                    selection_end_index = gtk_text_iter_get_visible_line_index (&selection_end);
825
                  else
826
                    selection_end_index = byte_count + 1; /* + 1 to flag past-the-end */
827
                }
828 829
            }

830
          render_para (text_renderer, line_display,
831 832
                       - x_offset,
                       current_y,
833
                       selection_start_index, selection_end_index);
834

835
          /* We paint the cursors last, because they overlap another chunk
836 837
         and need to appear on top. */

838 839 840 841 842 843 844 845 846 847 848 849 850 851 852
 	  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;
 	    }
	  
853 854 855 856
          cursor_list = line_display->cursors;
          while (cursor_list)
            {
              GtkTextCursorDisplay *cursor = cursor_list->data;
857 858
	      GtkTextDirection dir;
 	      GdkRectangle cursor_location;
859

860
              dir = line_display->direction;
861 862 863 864 865 866 867 868 869 870
 	      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;
871

872 873 874
	      gtk_draw_insertion_cursor (widget, drawable, &clip, &cursor_location,
					 cursor->is_strong,
					 dir, have_strong && have_weak);
875

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 885 886
      tmp_list = g_slist_next (tmp_list);
    }

  gtk_text_layout_wrap_loop_end (layout);
887 888 889 890 891 892 893 894 895

  tmp_widgets = text_renderer_end (text_renderer);
  if (widgets)
    *widgets = tmp_widgets;
  else
    {
      g_list_foreach (tmp_widgets, (GFunc)g_object_unref, NULL);
      g_list_free (tmp_widgets);
    }
896 897 898

  g_slist_free (line_list);
}