gtktextdisplay.c 31.2 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
 * 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
Javier Jardón's avatar
Javier Jardón committed
24
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.Free
25 26
 *
 * Original Tk license:
27 28 29 30 31
 *
 * 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.
32
 *
33 34 35 36 37 38 39 40 41
 * 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.
42
 *
43 44 45 46 47
 * 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.
48
 *
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
 * 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.
67 68 69 70 71 72 73
 *
 */
/*
 * 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/.
74 75
 */

76
#define GTK_TEXT_USE_INTERNAL_UNSUPPORTED_API
77
#include "config.h"
78
#include "gtktextattributesprivate.h"
79
#include "gtktextdisplay.h"
80
#include "gtkwidgetprivate.h"
81
#include "gtkstylecontextprivate.h"
82
#include "gtkintl.h"
83

84 85 86
/* DO NOT go putting private headers in here. This file should only
 * use the semi-public headers, as with gtktextview.c.
 */
87

Matthias Clasen's avatar
Matthias Clasen committed
88
#define GTK_TYPE_TEXT_RENDERER            (_gtk_text_renderer_get_type())
89 90 91 92 93
#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))
94

95 96
typedef struct _GtkTextRenderer      GtkTextRenderer;
typedef struct _GtkTextRendererClass GtkTextRendererClass;
97

98 99 100 101 102 103
enum {
  NORMAL,
  SELECTED,
  CURSOR
};

104
struct _GtkTextRenderer
105
{
106
  PangoRenderer parent_instance;
107 108

  GtkWidget *widget;
109
  cairo_t *cr;
110
  
111
  GdkRGBA *error_color;	/* Error underline color for this widget */
112
  GList *widgets;      	/* widgets encountered when drawing */
113 114 115 116 117

  GdkRGBA rgba[4];
  guint8  rgba_set[4];

  guint state : 2;
118 119
};

120
struct _GtkTextRendererClass
121
{
122
  PangoRendererClass parent_class;
123
};
124

125 126
GType _gtk_text_renderer_get_type (void);

127
G_DEFINE_TYPE (GtkTextRenderer, _gtk_text_renderer, PANGO_TYPE_RENDERER)
128

129 130 131
static void
text_renderer_set_rgba (GtkTextRenderer *text_renderer,
			PangoRenderPart  part,
132
			const GdkRGBA   *rgba)
133 134
{
  PangoRenderer *renderer = PANGO_RENDERER (text_renderer);
135 136
  PangoColor     dummy = { 0, };

137 138
  if (rgba)
    {
139 140
      text_renderer->rgba[part] = *rgba;
      pango_renderer_set_color (renderer, part, &dummy);
141 142
    }
  else
143
    pango_renderer_set_color (renderer, part, NULL);
144

145
  text_renderer->rgba_set[part] = (rgba != NULL);
146 147
}

148 149
static GtkTextAppearance *
get_item_appearance (PangoItem *item)
150
{
151 152 153 154 155 156 157 158 159 160 161 162 163
  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;
164 165 166
}

static void
167 168
gtk_text_renderer_prepare_run (PangoRenderer  *renderer,
			       PangoLayoutRun *run)
169
{
170 171
  GtkStyleContext *context;
  GtkStateFlags state;
172
  GtkTextRenderer *text_renderer = GTK_TEXT_RENDERER (renderer);
173
  GdkRGBA *bg_rgba = NULL;
174
  GdkRGBA *fg_rgba = NULL;
175 176
  GtkTextAppearance *appearance;

Matthias Clasen's avatar
Matthias Clasen committed
177
  PANGO_RENDERER_CLASS (_gtk_text_renderer_parent_class)->prepare_run (renderer, run);
178 179 180

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

182
  context = gtk_widget_get_style_context (text_renderer->widget);
183
  state   = gtk_widget_get_state_flags (text_renderer->widget);
184

185
  if (appearance->draw_bg && text_renderer->state == NORMAL)
186
    bg_rgba = appearance->rgba[0];
187
  else
188
    bg_rgba = NULL;
189
  
190
  text_renderer_set_rgba (text_renderer, PANGO_RENDER_PART_BACKGROUND, bg_rgba);
191

192
  if (text_renderer->state == SELECTED)
193
    {
194 195
      state |= GTK_STATE_FLAG_SELECTED;

196
      gtk_style_context_get (context, state, "color", &fg_rgba, NULL);
197
    }
198
  else if (text_renderer->state == CURSOR && gtk_widget_has_focus (text_renderer->widget))
199 200 201 202 203
    {
      gtk_style_context_get (context, state,
                             "background-color", &fg_rgba,
                              NULL);
    }
204
  else
205
    fg_rgba = appearance->rgba[1];
206

207
  text_renderer_set_rgba (text_renderer, PANGO_RENDER_PART_FOREGROUND, fg_rgba);
208

209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
  if (GTK_TEXT_APPEARANCE_GET_STRIKETHROUGH_RGBA_SET (appearance))
    {
      GdkRGBA rgba;

      GTK_TEXT_APPEARANCE_GET_STRIKETHROUGH_RGBA (appearance, &rgba);
      text_renderer_set_rgba (text_renderer, PANGO_RENDER_PART_STRIKETHROUGH, &rgba);
    }
  else
    text_renderer_set_rgba (text_renderer, PANGO_RENDER_PART_STRIKETHROUGH, fg_rgba);

  if (GTK_TEXT_APPEARANCE_GET_UNDERLINE_RGBA_SET (appearance))
    {
      GdkRGBA rgba;

      GTK_TEXT_APPEARANCE_GET_UNDERLINE_RGBA (appearance, &rgba);
      text_renderer_set_rgba (text_renderer, PANGO_RENDER_PART_UNDERLINE, &rgba);
    }
  else if (appearance->underline == PANGO_UNDERLINE_ERROR)
227 228 229
    {
      if (!text_renderer->error_color)
        {
230 231
	  GdkColor *color = NULL;

232
          gtk_style_context_get_style (context,
233
                                       "error-underline-color", &color,
234 235
                                       NULL);

236 237 238 239 240 241 242 243
	  if (color)
	    {
	      GdkRGBA   rgba;

	      rgba.red = color->red / 65535.;
	      rgba.green = color->green / 65535.;
	      rgba.blue = color->blue / 65535.;
	      rgba.alpha = 1;
Matthias Clasen's avatar
Matthias Clasen committed
244
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
245
	      gdk_color_free (color);
Matthias Clasen's avatar
Matthias Clasen committed
246
G_GNUC_END_IGNORE_DEPRECATIONS
247 248 249 250 251 252 253 254

	      text_renderer->error_color = gdk_rgba_copy (&rgba);
	    }
	  else
	    {
	      static const GdkRGBA red = { 1, 0, 0, 1 };
	      text_renderer->error_color = gdk_rgba_copy (&red);
	    }
255
        }
256

257
      text_renderer_set_rgba (text_renderer, PANGO_RENDER_PART_UNDERLINE, text_renderer->error_color);
258 259
    }
  else
260 261 262 263
    text_renderer_set_rgba (text_renderer, PANGO_RENDER_PART_UNDERLINE, fg_rgba);

  if (fg_rgba != appearance->rgba[1])
    gdk_rgba_free (fg_rgba);
264 265
}

266 267 268 269 270 271
static void
set_color (GtkTextRenderer *text_renderer,
           PangoRenderPart  part)
{
  cairo_save (text_renderer->cr);

272 273
  if (text_renderer->rgba_set[part])
    gdk_cairo_set_source_rgba (text_renderer->cr, &text_renderer->rgba[part]);
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 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
}

static void
unset_color (GtkTextRenderer *text_renderer)
{
  cairo_restore (text_renderer->cr);
}

static void
gtk_text_renderer_draw_glyphs (PangoRenderer     *renderer,
                               PangoFont         *font,
                               PangoGlyphString  *glyphs,
                               int                x,
                               int                y)
{
  GtkTextRenderer *text_renderer = GTK_TEXT_RENDERER (renderer);

  set_color (text_renderer, PANGO_RENDER_PART_FOREGROUND);

  cairo_move_to (text_renderer->cr, (double)x / PANGO_SCALE, (double)y / PANGO_SCALE);
  pango_cairo_show_glyph_string (text_renderer->cr, font, glyphs);

  unset_color (text_renderer);
}

static void
gtk_text_renderer_draw_glyph_item (PangoRenderer     *renderer,
                                   const char        *text,
                                   PangoGlyphItem    *glyph_item,
                                   int                x,
                                   int                y)
{
  GtkTextRenderer *text_renderer = GTK_TEXT_RENDERER (renderer);

  set_color (text_renderer, PANGO_RENDER_PART_FOREGROUND);

  cairo_move_to (text_renderer->cr, (double)x / PANGO_SCALE, (double)y / PANGO_SCALE);
  pango_cairo_show_glyph_item (text_renderer->cr, text, glyph_item);

  unset_color (text_renderer);
}

static void
gtk_text_renderer_draw_rectangle (PangoRenderer     *renderer,
				  PangoRenderPart    part,
				  int                x,
				  int                y,
				  int                width,
				  int                height)
{
  GtkTextRenderer *text_renderer = GTK_TEXT_RENDERER (renderer);

  set_color (text_renderer, part);

  cairo_rectangle (text_renderer->cr,
                   (double)x / PANGO_SCALE, (double)y / PANGO_SCALE,
		   (double)width / PANGO_SCALE, (double)height / PANGO_SCALE);
  cairo_fill (text_renderer->cr);

  unset_color (text_renderer);
}

static void
gtk_text_renderer_draw_trapezoid (PangoRenderer     *renderer,
				  PangoRenderPart    part,
				  double             y1_,
				  double             x11,
				  double             x21,
				  double             y2,
				  double             x12,
				  double             x22)
{
  GtkTextRenderer *text_renderer = GTK_TEXT_RENDERER (renderer);
  cairo_t *cr;
  cairo_matrix_t matrix;

  set_color (text_renderer, part);

  cr = text_renderer->cr;

  cairo_get_matrix (cr, &matrix);
  matrix.xx = matrix.yy = 1.0;
  matrix.xy = matrix.yx = 0.0;
  cairo_set_matrix (cr, &matrix);

  cairo_move_to (cr, x11, y1_);
  cairo_line_to (cr, x21, y1_);
  cairo_line_to (cr, x22, y2);
  cairo_line_to (cr, x12, y2);
  cairo_close_path (cr);

  cairo_fill (cr);

  unset_color (text_renderer);
}

static void
gtk_text_renderer_draw_error_underline (PangoRenderer *renderer,
					int            x,
					int            y,
					int            width,
					int            height)
{
  GtkTextRenderer *text_renderer = GTK_TEXT_RENDERER (renderer);

  set_color (text_renderer, PANGO_RENDER_PART_UNDERLINE);

  pango_cairo_show_error_underline (text_renderer->cr,
                                    (double)x / PANGO_SCALE, (double)y / PANGO_SCALE,
                                    (double)width / PANGO_SCALE, (double)height / PANGO_SCALE);

  unset_color (text_renderer);
}

388 389 390 391 392
static void
gtk_text_renderer_draw_shape (PangoRenderer   *renderer,
			      PangoAttrShape  *attr,
			      int              x,
			      int              y)
393
{
394
  GtkTextRenderer *text_renderer = GTK_TEXT_RENDERER (renderer);
395

396
  if (attr->data == NULL)
397
    {
398 399 400
      /* This happens if we have an empty widget anchor. Draw
       * something empty-looking.
       */
401 402
      GdkRectangle shape_rect;
      cairo_t *cr;
403

404 405 406 407
      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;
408

409 410 411
      set_color (text_renderer, PANGO_RENDER_PART_FOREGROUND);

      cr = text_renderer->cr;
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427

      cairo_set_line_width (cr, 1.0);

      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);
428 429

      unset_color (text_renderer);
430 431 432
    }
  else if (GDK_IS_PIXBUF (attr->data))
    {
433
      cairo_t *cr = text_renderer->cr;
434
      GdkPixbuf *pixbuf = GDK_PIXBUF (attr->data);
435
      
436
      cairo_save (cr);
437

438 439 440 441
      gdk_cairo_set_source_pixbuf (cr, pixbuf,
                                   PANGO_PIXELS (x),
                                   PANGO_PIXELS (y) -  gdk_pixbuf_get_height (pixbuf));
      cairo_paint (cr);
442

443
      cairo_restore (cr);
444
    }
445 446
  else if (GTK_IS_WIDGET (attr->data))
    {
447 448 449 450 451 452
      GtkWidget *widget;
      
      widget = GTK_WIDGET (attr->data);

      text_renderer->widgets = g_list_prepend (text_renderer->widgets,
					       g_object_ref (widget));
453 454 455
    }
  else
    g_assert_not_reached (); /* not a pixbuf or widget */
456 457
}

458
static void
459
gtk_text_renderer_finalize (GObject *object)
460
{
Matthias Clasen's avatar
Matthias Clasen committed
461
  G_OBJECT_CLASS (_gtk_text_renderer_parent_class)->finalize (object);
462
}
463

464
static void
Matthias Clasen's avatar
Matthias Clasen committed
465
_gtk_text_renderer_init (GtkTextRenderer *renderer)
466
{
467 468
}

469
static void
Matthias Clasen's avatar
Matthias Clasen committed
470
_gtk_text_renderer_class_init (GtkTextRendererClass *klass)
471
{
472
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
473
  
474 475 476
  PangoRendererClass *renderer_class = PANGO_RENDERER_CLASS (klass);
  
  renderer_class->prepare_run = gtk_text_renderer_prepare_run;
477 478 479 480 481
  renderer_class->draw_glyphs = gtk_text_renderer_draw_glyphs;
  renderer_class->draw_glyph_item = gtk_text_renderer_draw_glyph_item;
  renderer_class->draw_rectangle = gtk_text_renderer_draw_rectangle;
  renderer_class->draw_trapezoid = gtk_text_renderer_draw_trapezoid;
  renderer_class->draw_error_underline = gtk_text_renderer_draw_error_underline;
482
  renderer_class->draw_shape = gtk_text_renderer_draw_shape;
483

484 485
  object_class->finalize = gtk_text_renderer_finalize;
}
486

487
static void
488 489
text_renderer_set_state (GtkTextRenderer *text_renderer,
			 int              state)
490
{
491
  text_renderer->state = state;
492
}
493

494 495
static void
text_renderer_begin (GtkTextRenderer *text_renderer,
496 497
                     GtkWidget       *widget,
                     cairo_t         *cr)
498
{
499 500 501 502
  GtkStyleContext *context;
  GtkStateFlags state;
  GdkRGBA color;

503
  text_renderer->widget = widget;
504
  text_renderer->cr = cr;
505 506 507 508 509 510 511 512 513 514 515 516

  context = gtk_widget_get_style_context (widget);

  gtk_style_context_save (context);
  gtk_style_context_add_class (context, GTK_STYLE_CLASS_VIEW);

  state = gtk_widget_get_state_flags (widget);
  gtk_style_context_get_color (context, state, &color);

  cairo_save (cr);

  gdk_cairo_set_source_rgba (cr, &color);
517
}
518

519 520
/* Returns a GSList of (referenced) widgets encountered while drawing.
 */
521
static GList *
522 523
text_renderer_end (GtkTextRenderer *text_renderer)
{
524
  GtkStyleContext *context;
525
  GList *widgets = text_renderer->widgets;
526

527 528 529 530 531 532
  cairo_restore (text_renderer->cr);

  context = gtk_widget_get_style_context (text_renderer->widget);

  gtk_style_context_restore (context);

533
  text_renderer->widget = NULL;
534
  text_renderer->cr = NULL;
535

536 537
  text_renderer->widgets = NULL;

538 539
  if (text_renderer->error_color)
    {
540
      gdk_rgba_free (text_renderer->error_color);
541 542
      text_renderer->error_color = NULL;
    }
543 544

  return widgets;
545
}
546

547
static cairo_region_t *
548 549 550 551 552 553 554 555 556 557 558
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;
559
  cairo_region_t *clip_region = cairo_region_create ();
560

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

563 564 565
  for (i=0; i < n_ranges; i++)
    {
      GdkRectangle rect;
566

567 568 569 570 571
      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;
      
572
      cairo_region_union_rectangle (clip_region, &rect);
573
    }
574 575 576

  g_free (ranges);
  return clip_region;
577 578
}

579
static void
580
render_para (GtkTextRenderer    *text_renderer,
581 582
             GtkTextLineDisplay *line_display,
             int                 selection_start_index,
583
             int                 selection_end_index)
584
{
585 586
  GtkStyleContext *context;
  GtkStateFlags state;
587
  PangoLayout *layout = line_display->layout;
588
  int byte_offset = 0;
589 590 591
  PangoLayoutIter *iter;
  PangoRectangle layout_logical;
  int screen_width;
592
  GdkRGBA selection;
593
  gboolean first = TRUE;
594

595
  iter = pango_layout_get_iter (layout);
596

597
  pango_layout_iter_get_layout_extents (iter, NULL, &layout_logical);
598

599 600 601 602 603 604
  /* 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;
605 606

  context = gtk_widget_get_style_context (text_renderer->widget);
607
  gtk_style_context_save (context);
608

609
  state = gtk_style_context_get_state (context);
610
  state |= GTK_STATE_FLAG_SELECTED;
611
  gtk_style_context_set_state (context, state);
612

613
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
614
  gtk_style_context_get_background_color (context, state, &selection);
615
G_GNUC_END_IGNORE_DEPRECATIONS
616

617 618
 gtk_style_context_restore (context);

619 620
  do
    {
621
      PangoLayoutLine *line = pango_layout_iter_get_line_readonly (iter);
622
      int selection_y, selection_height;
623 624 625
      int first_y, last_y;
      PangoRectangle line_rect;
      int baseline;
626
      gboolean at_last_line;
627 628 629 630 631 632 633 634 635 636 637 638 639 640
      
      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
       */
641
      selection_y = PANGO_PIXELS (first_y) + line_display->top_margin;
642
      selection_height = PANGO_PIXELS (last_y) - PANGO_PIXELS (first_y);
643 644

      if (first)
645 646 647 648
        {
          selection_y -= line_display->top_margin;
          selection_height += line_display->top_margin;
        }
649 650 651

      at_last_line = pango_layout_iter_at_last_line (iter);
      if (at_last_line)
652
        selection_height += line_display->bottom_margin;
653
      
654
      first = FALSE;
655

656
      if (selection_start_index < byte_offset &&
657 658
          selection_end_index > line->length + byte_offset) /* All selected */
        {
659 660 661
          cairo_t *cr = text_renderer->cr;

          cairo_save (cr);
662
          gdk_cairo_set_source_rgba (cr, &selection);
663
          cairo_rectangle (cr, 
664
                           line_display->left_margin, selection_y,
665 666
                           screen_width, selection_height);
          cairo_fill (cr);
667
          cairo_restore(cr);
668

669
	  text_renderer_set_state (text_renderer, SELECTED);
670 671
	  pango_renderer_draw_layout_line (PANGO_RENDERER (text_renderer),
					   line, 
672 673
					   line_rect.x,
					   baseline);
674
        }
675
      else
676
        {
677
          if (line_display->pg_bg_rgba)
678
            {
679 680 681
              cairo_t *cr = text_renderer->cr;

              cairo_save (cr);
682 683
 
	      gdk_cairo_set_source_rgba (text_renderer->cr, line_display->pg_bg_rgba);
684
              cairo_rectangle (cr, 
685
                               line_display->left_margin, selection_y,
686 687 688
                               screen_width, selection_height);
              cairo_fill (cr);

689
              cairo_restore (cr);
690 691
            }
        
692
	  text_renderer_set_state (text_renderer, NORMAL);
693 694
	  pango_renderer_draw_layout_line (PANGO_RENDERER (text_renderer),
					   line, 
695 696
					   line_rect.x,
					   baseline);
697

698 699 700 701 702 703 704
	  /* 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)
705
            {
706
              cairo_t *cr = text_renderer->cr;
707
              cairo_region_t *clip_region = get_selected_clip (text_renderer, layout, line,
708
                                                          line_display->x_offset,
709 710
                                                          selection_y,
                                                          selection_height,
711
                                                          selection_start_index, selection_end_index);
712

713
              cairo_save (cr);
714 715
              gdk_cairo_region (cr, clip_region);
              cairo_clip (cr);
716
              cairo_region_destroy (clip_region);
717

718
              gdk_cairo_set_source_rgba (cr, &selection);
719
              cairo_rectangle (cr,
720
                               PANGO_PIXELS (line_rect.x),
721 722 723 724 725
                               selection_y,
                               PANGO_PIXELS (line_rect.width),
                               selection_height);
              cairo_fill (cr);

726
	      text_renderer_set_state (text_renderer, SELECTED);
727 728
	      pango_renderer_draw_layout_line (PANGO_RENDERER (text_renderer),
					       line, 
729 730
					       line_rect.x,
					       baseline);
731

732
              cairo_restore (cr);
733 734

              /* Paint in the ends of the line */
735
              if (line_rect.x > line_display->left_margin * PANGO_SCALE &&
736 737 738
                  ((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)))
                {
739
                  cairo_save (cr);
740

741
                  gdk_cairo_set_source_rgba (cr, &selection);
742
                  cairo_rectangle (cr,
743
                                   line_display->left_margin,
744 745 746 747
                                   selection_y,
                                   PANGO_PIXELS (line_rect.x) - line_display->left_margin,
                                   selection_height);
                  cairo_fill (cr);
748 749

                  cairo_restore (cr);
750 751
                }

752 753
              if (line_rect.x + line_rect.width <
                  (screen_width + line_display->left_margin) * PANGO_SCALE &&
754 755 756
                  ((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)))
                {
757
                  int nonlayout_width;
758

759 760 761
                  nonlayout_width =
                    line_display->left_margin + screen_width -
                    PANGO_PIXELS (line_rect.x) - PANGO_PIXELS (line_rect.width);
762

763 764
                  cairo_save (cr);

765
                  gdk_cairo_set_source_rgba (cr, &selection);
766
                  cairo_rectangle (cr,
767
                                   PANGO_PIXELS (line_rect.x) + PANGO_PIXELS (line_rect.width),
768 769 770 771
                                   selection_y,
                                   nonlayout_width,
                                   selection_height);
                  cairo_fill (cr);
772 773

                  cairo_restore (cr);
774 775
                }
            }
776
	  else if (line_display->has_block_cursor &&
777
		   gtk_widget_has_focus (text_renderer->widget) &&
778 779 780 781 782
		   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;
783
              GdkRGBA cursor_color;
784
              cairo_t *cr = text_renderer->cr;
785

786 787 788
              /* we draw text using base color on filled cursor rectangle of cursor color
               * (normally white on black) */
              _gtk_style_context_get_cursor_color (context, &cursor_color, NULL);
789

790 791
	      cursor_rect.x = line_display->x_offset + line_display->block_cursor.x;
	      cursor_rect.y = line_display->block_cursor.y + line_display->top_margin;
792
	      cursor_rect.width = line_display->block_cursor.width;
793
	      cursor_rect.height = line_display->block_cursor.height;
794

795 796
              cairo_save (cr);

797 798
              gdk_cairo_rectangle (cr, &cursor_rect);
              cairo_clip (cr);
799

800
              gdk_cairo_set_source_rgba (cr, &cursor_color);
801
              cairo_paint (cr);
802

803 804 805 806 807 808
              /* draw text under the cursor if any */
              if (!line_display->cursor_at_line_end)
                {
                  GdkRGBA color;

                  state = gtk_widget_get_state_flags (text_renderer->widget);
809
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
810
                  gtk_style_context_get_background_color (context, state, &color);
811
G_GNUC_END_IGNORE_DEPRECATIONS
812

813
                  gdk_cairo_set_source_rgba (cr, &color);
814 815 816 817 818

		  text_renderer_set_state (text_renderer, CURSOR);

		  pango_renderer_draw_layout_line (PANGO_RENDERER (text_renderer),
						   line,
819 820
						   line_rect.x,
						   baseline);
821
                }
822 823

              cairo_restore (cr);
824
	    }
825
        }
826 827 828

      byte_offset += line->length;
    }
829 830 831
  while (pango_layout_iter_next_line (iter));

  pango_layout_iter_free (iter);
832 833
}

834
static GtkTextRenderer *
835
get_text_renderer (void)
836
{
837
  static GtkTextRenderer *text_renderer = NULL;
838

839
  if (!text_renderer)
840
    text_renderer = g_object_new (GTK_TYPE_TEXT_RENDERER, NULL);
841 842

  return text_renderer;
843 844 845 846
}

void
gtk_text_layout_draw (GtkTextLayout *layout,
847
                      GtkWidget *widget,
848 849
                      cairo_t *cr,
                      GList **widgets)
850
{
851
  GtkStyleContext *context;
852
  gint offset_y;
853
  GtkTextRenderer *text_renderer;
854
  GtkTextIter selection_start, selection_end;
Paolo Borelli's avatar
Paolo Borelli committed
855
  gboolean have_selection;
856 857
  GSList *line_list;
  GSList *tmp_list;
858
  GList *tmp_widgets;
José Aliste's avatar
José Aliste committed
859
  GdkRectangle clip;
860

861 862 863
  g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
  g_return_if_fail (layout->default_style != NULL);
  g_return_if_fail (layout->buffer != NULL);
864
  g_return_if_fail (cr != NULL);
865

José Aliste's avatar
José Aliste committed
866 867
  if (!gdk_cairo_get_clip_rectangle (cr, &clip))
    return;
868

869 870
  context = gtk_widget_get_style_context (widget);

871
  line_list = gtk_text_layout_get_lines (layout, clip.y, clip.y + clip.height, &offset_y);
872 873 874 875

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

876 877
  text_renderer = get_text_renderer ();
  text_renderer_begin (text_renderer, widget, cr);
878

879 880 881
  /* text_renderer_begin/end does cairo_save/restore */
  cairo_translate (cr, 0, offset_y);

882
  gtk_text_layout_wrap_loop_start (layout);
883

Paolo Borelli's avatar
Paolo Borelli committed
884 885 886
  have_selection = gtk_text_buffer_get_selection_bounds (layout->buffer,
                                                         &selection_start,
                                                         &selection_end);
887

888 889 890 891 892 893 894 895
  tmp_list = line_list;
  while (tmp_list != NULL)
    {
      GtkTextLineDisplay *line_display;
      gint selection_start_index = -1;
      gint selection_end_index = -1;

      GtkTextLine *line = tmp_list->data;
896

897
      line_display = gtk_text_layout_get_line_display (layout, line, FALSE);
898

899 900 901
      if (line_display->height > 0)
        {
          g_assert (line_display->layout != NULL);
902
          
903
          if (have_selection)
904
            {
905 906
              GtkTextIter line_start, line_end;
              gint byte_count;
907
              
908 909 910
              gtk_text_layout_get_iter_at_line (layout,
                                                &line_start,
                                                line, 0);
911
              line_end = line_start;
912 913
	      if (!gtk_text_iter_ends_line (&line_end))
		gtk_text_iter_forward_to_line_end (&line_end);
914
              byte_count = gtk_text_iter_get_visible_line_index (&line_end);     
915

916 917
              if (gtk_text_iter_compare (&selection_start, &line_end) <= 0 &&
                  gtk_text_iter_compare (&selection_end, &line_start) >= 0)
918 919
                {
                  if (gtk_text_iter_compare (&selection_start, &line_start) >= 0)
920
                    selection_start_index = gtk_text_iter_get_visible_line_index (&selection_start);
921 922 923 924
                  else
                    selection_start_index = -1;

                  if (gtk_text_iter_compare (&selection_end, &line_end) <= 0)
925
                    selection_end_index = gtk_text_iter_get_visible_line_index (&selection_end);
926
                  else
927
                    selection_end_index = byte_count + 1; /* + 1 to flag past-the-end */
928
                }
929 930
            }

931 932
          render_para (text_renderer, line_display,
                       selection_start_index, selection_end_index);
933

934
          /* We paint the cursors last, because they overlap another chunk
935 936 937
           * and need to appear on top.
           */
          if (line_display->cursors != NULL)
938
            {
939
              int i;
940

941 942 943
              for (i = 0; i < line_display->cursors->len; i++)
                {
                  int index;
944
                  PangoDirection dir;
945 946

                  index = g_array_index(line_display->cursors, int, i);
947 948 949 950
                  dir = (line_display->direction == GTK_TEXT_DIR_RTL) ? PANGO_DIRECTION_RTL : PANGO_DIRECTION_LTR;
                  gtk_render_insertion_cursor (context, cr,
                                               line_display->x_offset, line_display->top_margin,
                                               line_display->layout, index, dir);
951
                }
952 953
            }
        } /* line_display->height > 0 */
954

955
      cairo_translate (cr, 0, line_display->height);
956
      gtk_text_layout_free_line_display (layout, line_display);
957
      
958 959 960 961
      tmp_list = g_slist_next (tmp_list);
    }

  gtk_text_layout_wrap_loop_end (layout);
962

963 964 965 966 967
  tmp_widgets = text_renderer_end (text_renderer);
  if (widgets)
    *widgets = tmp_widgets;
  else
    g_list_free_full (tmp_widgets, g_object_unref);
968 969 970

  g_slist_free (line_list);
}