]> Pileus Git - ~andy/gtk/blob - gtk/gtktextdisplay.c
gdk/x11: Add gdk_x11_device_manager_lookup()
[~andy/gtk] / gtk / gtktextdisplay.c
1 /* gtktextdisplay.c - display layed-out text
2  *
3  * Copyright (c) 1992-1994 The Regents of the University of California.
4  * Copyright (c) 1994-1997 Sun Microsystems, Inc.
5  * Copyright (c) 2000 Red Hat, Inc.
6  * Tk->Gtk port by Havoc Pennington
7  *
8  * This file can be used under your choice of two licenses, the LGPL
9  * and the original Tk license.
10  *
11  * LGPL:
12  *
13  * This library is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU Lesser General Public
15  * License as published by the Free Software Foundation; either
16  * version 2 of the License, or (at your option) any later version.
17  *
18  * This library is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21  * Lesser General Public License for more details.
22  *
23  * You should have received a copy of the GNU Lesser General Public
24  * License along with this library; if not, write to the Free
25  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26  *
27  * Original Tk license:
28  *
29  * This software is copyrighted by the Regents of the University of
30  * California, Sun Microsystems, Inc., and other parties.  The
31  * following terms apply to all files associated with the software
32  * unless explicitly disclaimed in individual files.
33  *
34  * The authors hereby grant permission to use, copy, modify,
35  * distribute, and license this software and its documentation for any
36  * purpose, provided that existing copyright notices are retained in
37  * all copies and that this notice is included verbatim in any
38  * distributions. No written agreement, license, or royalty fee is
39  * required for any of the authorized uses.  Modifications to this
40  * software may be copyrighted by their authors and need not follow
41  * the licensing terms described here, provided that the new terms are
42  * clearly indicated on the first page of each file where they apply.
43  *
44  * IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY
45  * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
46  * DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION,
47  * OR ANY DERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE BEEN ADVISED
48  * OF THE POSSIBILITY OF SUCH DAMAGE.
49  *
50  * THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES,
51  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
52  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND
53  * NON-INFRINGEMENT.  THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS,
54  * AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO PROVIDE
55  * MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
56  *
57  * GOVERNMENT USE: If you are acquiring this software on behalf of the
58  * U.S. government, the Government shall have only "Restricted Rights"
59  * in the software and related documentation as defined in the Federal
60  * Acquisition Regulations (FARs) in Clause 52.227.19 (c) (2).  If you
61  * are acquiring the software on behalf of the Department of Defense,
62  * the software shall be classified as "Commercial Computer Software"
63  * and the Government shall have only "Restricted Rights" as defined
64  * in Clause 252.227-7013 (c) (1) of DFARs.  Notwithstanding the
65  * foregoing, the authors grant the U.S. Government and others acting
66  * in its behalf permission to use and distribute the software in
67  * accordance with the terms specified in this license.
68  *
69  */
70 /*
71  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
72  * file for a list of people on the GTK+ Team.  See the ChangeLog
73  * files for a list of changes.  These files are distributed with
74  * 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 "gtkwidgetprivate.h"
81 #include "gtkstylecontextprivate.h"
82 #include "gtkintl.h"
83
84 /* DO NOT go putting private headers in here. This file should only
85  * use the semi-public headers, as with gtktextview.c.
86  */
87
88 #define GTK_TYPE_TEXT_RENDERER            (_gtk_text_renderer_get_type())
89 #define GTK_TEXT_RENDERER(object)         (G_TYPE_CHECK_INSTANCE_CAST ((object), GTK_TYPE_TEXT_RENDERER, GtkTextRenderer))
90 #define GTK_IS_TEXT_RENDERER(object)      (G_TYPE_CHECK_INSTANCE_TYPE ((object), GTK_TYPE_TEXT_RENDERER))
91 #define GTK_TEXT_RENDERER_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_TEXT_RENDERER, GtkTextRendererClass))
92 #define GTK_IS_TEXT_RENDERER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_TEXT_RENDERER))
93 #define GTK_TEXT_RENDERER_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_TEXT_RENDERER, GtkTextRendererClass))
94
95 typedef struct _GtkTextRenderer      GtkTextRenderer;
96 typedef struct _GtkTextRendererClass GtkTextRendererClass;
97
98 enum {
99   NORMAL,
100   SELECTED,
101   CURSOR
102 };
103
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   GdkRGBA rgba[4];
115   guint8  rgba_set[4];
116
117   guint state : 2;
118 };
119
120 struct _GtkTextRendererClass
121 {
122   PangoRendererClass parent_class;
123 };
124
125 G_DEFINE_TYPE (GtkTextRenderer, _gtk_text_renderer, PANGO_TYPE_RENDERER)
126
127 static void
128 text_renderer_set_rgba (GtkTextRenderer *text_renderer,
129                         PangoRenderPart  part,
130                         const GdkRGBA   *rgba)
131 {
132   PangoRenderer *renderer = PANGO_RENDERER (text_renderer);
133   PangoColor     dummy = { 0, };
134
135   if (rgba)
136     {
137       text_renderer->rgba[part] = *rgba;
138       pango_renderer_set_color (renderer, part, &dummy);
139     }
140   else
141     pango_renderer_set_color (renderer, part, NULL);
142
143   text_renderer->rgba_set[part] = (rgba != NULL);
144 }
145
146 static GtkTextAppearance *
147 get_item_appearance (PangoItem *item)
148 {
149   GSList *tmp_list = item->analysis.extra_attrs;
150
151   while (tmp_list)
152     {
153       PangoAttribute *attr = tmp_list->data;
154
155       if (attr->klass->type == gtk_text_attr_appearance_type)
156         return &((GtkTextAttrAppearance *)attr)->appearance;
157
158       tmp_list = tmp_list->next;
159     }
160
161   return NULL;
162 }
163
164 static void
165 gtk_text_renderer_prepare_run (PangoRenderer  *renderer,
166                                PangoLayoutRun *run)
167 {
168   GtkStyleContext *context;
169   GtkStateFlags state;
170   GtkTextRenderer *text_renderer = GTK_TEXT_RENDERER (renderer);
171   GdkRGBA *bg_rgba = NULL;
172   GdkRGBA *fg_rgba = NULL;
173   GtkTextAppearance *appearance;
174
175   PANGO_RENDERER_CLASS (_gtk_text_renderer_parent_class)->prepare_run (renderer, run);
176
177   appearance = get_item_appearance (run->item);
178   g_assert (appearance != NULL);
179
180   context = gtk_widget_get_style_context (text_renderer->widget);
181   state   = gtk_widget_get_state_flags (text_renderer->widget);
182
183   if (appearance->draw_bg && text_renderer->state == NORMAL)
184     bg_rgba = appearance->rgba[0];
185   else
186     bg_rgba = NULL;
187   
188   text_renderer_set_rgba (text_renderer, PANGO_RENDER_PART_BACKGROUND, bg_rgba);
189
190   if (text_renderer->state == SELECTED)
191     {
192       state |= GTK_STATE_FLAG_SELECTED;
193
194       gtk_style_context_get (context, state, "color", &fg_rgba, NULL);
195     }
196   else if (text_renderer->state == CURSOR && gtk_widget_has_focus (text_renderer->widget))
197     {
198       gtk_style_context_get (context, state,
199                              "background-color", &fg_rgba,
200                               NULL);
201     }
202   else
203     fg_rgba = appearance->rgba[1];
204
205   text_renderer_set_rgba (text_renderer, PANGO_RENDER_PART_FOREGROUND, fg_rgba);
206   text_renderer_set_rgba (text_renderer, PANGO_RENDER_PART_STRIKETHROUGH, fg_rgba);
207
208   if (appearance->underline == PANGO_UNDERLINE_ERROR)
209     {
210       if (!text_renderer->error_color)
211         {
212           GdkColor *color = NULL;
213
214           gtk_style_context_get_style (context,
215                                        "error-underline-color", &color,
216                                        NULL);
217
218           if (color)
219             {
220               GdkRGBA   rgba;
221
222               rgba.red = color->red / 65535.;
223               rgba.green = color->green / 65535.;
224               rgba.blue = color->blue / 65535.;
225               rgba.alpha = 1;
226               gdk_color_free (color);
227
228               text_renderer->error_color = gdk_rgba_copy (&rgba);
229             }
230           else
231             {
232               static const GdkRGBA red = { 1, 0, 0, 1 };
233               text_renderer->error_color = gdk_rgba_copy (&red);
234             }
235         }
236
237       text_renderer_set_rgba (text_renderer, PANGO_RENDER_PART_UNDERLINE, text_renderer->error_color);
238     }
239   else
240     text_renderer_set_rgba (text_renderer, PANGO_RENDER_PART_UNDERLINE, fg_rgba);
241
242   if (fg_rgba != appearance->rgba[1])
243     gdk_rgba_free (fg_rgba);
244 }
245
246 static void
247 set_color (GtkTextRenderer *text_renderer,
248            PangoRenderPart  part)
249 {
250   cairo_save (text_renderer->cr);
251
252   if (text_renderer->rgba_set[part])
253     gdk_cairo_set_source_rgba (text_renderer->cr, &text_renderer->rgba[part]);
254 }
255
256 static void
257 unset_color (GtkTextRenderer *text_renderer)
258 {
259   cairo_restore (text_renderer->cr);
260 }
261
262 static void
263 gtk_text_renderer_draw_glyphs (PangoRenderer     *renderer,
264                                PangoFont         *font,
265                                PangoGlyphString  *glyphs,
266                                int                x,
267                                int                y)
268 {
269   GtkTextRenderer *text_renderer = GTK_TEXT_RENDERER (renderer);
270
271   set_color (text_renderer, PANGO_RENDER_PART_FOREGROUND);
272
273   cairo_move_to (text_renderer->cr, (double)x / PANGO_SCALE, (double)y / PANGO_SCALE);
274   pango_cairo_show_glyph_string (text_renderer->cr, font, glyphs);
275
276   unset_color (text_renderer);
277 }
278
279 static void
280 gtk_text_renderer_draw_glyph_item (PangoRenderer     *renderer,
281                                    const char        *text,
282                                    PangoGlyphItem    *glyph_item,
283                                    int                x,
284                                    int                y)
285 {
286   GtkTextRenderer *text_renderer = GTK_TEXT_RENDERER (renderer);
287
288   set_color (text_renderer, PANGO_RENDER_PART_FOREGROUND);
289
290   cairo_move_to (text_renderer->cr, (double)x / PANGO_SCALE, (double)y / PANGO_SCALE);
291   pango_cairo_show_glyph_item (text_renderer->cr, text, glyph_item);
292
293   unset_color (text_renderer);
294 }
295
296 static void
297 gtk_text_renderer_draw_rectangle (PangoRenderer     *renderer,
298                                   PangoRenderPart    part,
299                                   int                x,
300                                   int                y,
301                                   int                width,
302                                   int                height)
303 {
304   GtkTextRenderer *text_renderer = GTK_TEXT_RENDERER (renderer);
305
306   set_color (text_renderer, part);
307
308   cairo_rectangle (text_renderer->cr,
309                    (double)x / PANGO_SCALE, (double)y / PANGO_SCALE,
310                    (double)width / PANGO_SCALE, (double)height / PANGO_SCALE);
311   cairo_fill (text_renderer->cr);
312
313   unset_color (text_renderer);
314 }
315
316 static void
317 gtk_text_renderer_draw_trapezoid (PangoRenderer     *renderer,
318                                   PangoRenderPart    part,
319                                   double             y1_,
320                                   double             x11,
321                                   double             x21,
322                                   double             y2,
323                                   double             x12,
324                                   double             x22)
325 {
326   GtkTextRenderer *text_renderer = GTK_TEXT_RENDERER (renderer);
327   cairo_t *cr;
328   cairo_matrix_t matrix;
329
330   set_color (text_renderer, part);
331
332   cr = text_renderer->cr;
333
334   cairo_get_matrix (cr, &matrix);
335   matrix.xx = matrix.yy = 1.0;
336   matrix.xy = matrix.yx = 0.0;
337   cairo_set_matrix (cr, &matrix);
338
339   cairo_move_to (cr, x11, y1_);
340   cairo_line_to (cr, x21, y1_);
341   cairo_line_to (cr, x22, y2);
342   cairo_line_to (cr, x12, y2);
343   cairo_close_path (cr);
344
345   cairo_fill (cr);
346
347   unset_color (text_renderer);
348 }
349
350 static void
351 gtk_text_renderer_draw_error_underline (PangoRenderer *renderer,
352                                         int            x,
353                                         int            y,
354                                         int            width,
355                                         int            height)
356 {
357   GtkTextRenderer *text_renderer = GTK_TEXT_RENDERER (renderer);
358
359   set_color (text_renderer, PANGO_RENDER_PART_UNDERLINE);
360
361   pango_cairo_show_error_underline (text_renderer->cr,
362                                     (double)x / PANGO_SCALE, (double)y / PANGO_SCALE,
363                                     (double)width / PANGO_SCALE, (double)height / PANGO_SCALE);
364
365   unset_color (text_renderer);
366 }
367
368 static void
369 gtk_text_renderer_draw_shape (PangoRenderer   *renderer,
370                               PangoAttrShape  *attr,
371                               int              x,
372                               int              y)
373 {
374   GtkTextRenderer *text_renderer = GTK_TEXT_RENDERER (renderer);
375
376   if (attr->data == NULL)
377     {
378       /* This happens if we have an empty widget anchor. Draw
379        * something empty-looking.
380        */
381       GdkRectangle shape_rect;
382       cairo_t *cr;
383
384       shape_rect.x = PANGO_PIXELS (x);
385       shape_rect.y = PANGO_PIXELS (y + attr->logical_rect.y);
386       shape_rect.width = PANGO_PIXELS (x + attr->logical_rect.width) - shape_rect.x;
387       shape_rect.height = PANGO_PIXELS (y + attr->logical_rect.y + attr->logical_rect.height) - shape_rect.y;
388
389       set_color (text_renderer, PANGO_RENDER_PART_FOREGROUND);
390
391       cr = text_renderer->cr;
392
393       cairo_set_line_width (cr, 1.0);
394
395       cairo_rectangle (cr,
396                        shape_rect.x + 0.5, shape_rect.y + 0.5,
397                        shape_rect.width - 1, shape_rect.height - 1);
398       cairo_move_to (cr, shape_rect.x + 0.5, shape_rect.y + 0.5);
399       cairo_line_to (cr, 
400                      shape_rect.x + shape_rect.width - 0.5,
401                      shape_rect.y + shape_rect.height - 0.5);
402       cairo_move_to (cr, shape_rect.x + 0.5,
403                      shape_rect.y + shape_rect.height - 0.5);
404       cairo_line_to (cr, shape_rect.x + shape_rect.width - 0.5,
405                      shape_rect.y + 0.5);
406
407       cairo_stroke (cr);
408
409       unset_color (text_renderer);
410     }
411   else if (GDK_IS_PIXBUF (attr->data))
412     {
413       cairo_t *cr = text_renderer->cr;
414       GdkPixbuf *pixbuf = GDK_PIXBUF (attr->data);
415       
416       cairo_save (cr);
417
418       gdk_cairo_set_source_pixbuf (cr, pixbuf,
419                                    PANGO_PIXELS (x),
420                                    PANGO_PIXELS (y) -  gdk_pixbuf_get_height (pixbuf));
421       cairo_paint (cr);
422
423       cairo_restore (cr);
424     }
425   else if (GTK_IS_WIDGET (attr->data))
426     {
427       GtkWidget *widget;
428       
429       widget = GTK_WIDGET (attr->data);
430
431       text_renderer->widgets = g_list_prepend (text_renderer->widgets,
432                                                g_object_ref (widget));
433     }
434   else
435     g_assert_not_reached (); /* not a pixbuf or widget */
436 }
437
438 static void
439 gtk_text_renderer_finalize (GObject *object)
440 {
441   G_OBJECT_CLASS (_gtk_text_renderer_parent_class)->finalize (object);
442 }
443
444 static void
445 _gtk_text_renderer_init (GtkTextRenderer *renderer)
446 {
447 }
448
449 static void
450 _gtk_text_renderer_class_init (GtkTextRendererClass *klass)
451 {
452   GObjectClass *object_class = G_OBJECT_CLASS (klass);
453   
454   PangoRendererClass *renderer_class = PANGO_RENDERER_CLASS (klass);
455   
456   renderer_class->prepare_run = gtk_text_renderer_prepare_run;
457   renderer_class->draw_glyphs = gtk_text_renderer_draw_glyphs;
458   renderer_class->draw_glyph_item = gtk_text_renderer_draw_glyph_item;
459   renderer_class->draw_rectangle = gtk_text_renderer_draw_rectangle;
460   renderer_class->draw_trapezoid = gtk_text_renderer_draw_trapezoid;
461   renderer_class->draw_error_underline = gtk_text_renderer_draw_error_underline;
462   renderer_class->draw_shape = gtk_text_renderer_draw_shape;
463
464   object_class->finalize = gtk_text_renderer_finalize;
465 }
466
467 static void
468 text_renderer_set_state (GtkTextRenderer *text_renderer,
469                          int              state)
470 {
471   text_renderer->state = state;
472 }
473
474 static void
475 text_renderer_begin (GtkTextRenderer *text_renderer,
476                      GtkWidget       *widget,
477                      cairo_t         *cr)
478 {
479   GtkStyleContext *context;
480   GtkStateFlags state;
481   GdkRGBA color;
482
483   text_renderer->widget = widget;
484   text_renderer->cr = cr;
485
486   context = gtk_widget_get_style_context (widget);
487
488   gtk_style_context_save (context);
489   gtk_style_context_add_class (context, GTK_STYLE_CLASS_VIEW);
490
491   state = gtk_widget_get_state_flags (widget);
492   gtk_style_context_get_color (context, state, &color);
493
494   cairo_save (cr);
495
496   gdk_cairo_set_source_rgba (cr, &color);
497 }
498
499 /* Returns a GSList of (referenced) widgets encountered while drawing.
500  */
501 static GList *
502 text_renderer_end (GtkTextRenderer *text_renderer)
503 {
504   GtkStyleContext *context;
505   GList *widgets = text_renderer->widgets;
506
507   cairo_restore (text_renderer->cr);
508
509   context = gtk_widget_get_style_context (text_renderer->widget);
510
511   gtk_style_context_restore (context);
512
513   text_renderer->widget = NULL;
514   text_renderer->cr = NULL;
515
516   text_renderer->widgets = NULL;
517
518   if (text_renderer->error_color)
519     {
520       gdk_rgba_free (text_renderer->error_color);
521       text_renderer->error_color = NULL;
522     }
523
524   return widgets;
525 }
526
527 static cairo_region_t *
528 get_selected_clip (GtkTextRenderer    *text_renderer,
529                    PangoLayout        *layout,
530                    PangoLayoutLine    *line,
531                    int                 x,
532                    int                 y,
533                    int                 height,
534                    int                 start_index,
535                    int                 end_index)
536 {
537   gint *ranges;
538   gint n_ranges, i;
539   cairo_region_t *clip_region = cairo_region_create ();
540
541   pango_layout_line_get_x_ranges (line, start_index, end_index, &ranges, &n_ranges);
542
543   for (i=0; i < n_ranges; i++)
544     {
545       GdkRectangle rect;
546
547       rect.x = x + PANGO_PIXELS (ranges[2*i]);
548       rect.y = y;
549       rect.width = PANGO_PIXELS (ranges[2*i + 1]) - PANGO_PIXELS (ranges[2*i]);
550       rect.height = height;
551       
552       cairo_region_union_rectangle (clip_region, &rect);
553     }
554
555   g_free (ranges);
556   return clip_region;
557 }
558
559 static void
560 render_para (GtkTextRenderer    *text_renderer,
561              GtkTextLineDisplay *line_display,
562              int                 selection_start_index,
563              int                 selection_end_index)
564 {
565   GtkStyleContext *context;
566   GtkStateFlags state;
567   PangoLayout *layout = line_display->layout;
568   int byte_offset = 0;
569   PangoLayoutIter *iter;
570   PangoRectangle layout_logical;
571   int screen_width;
572   GdkRGBA selection;
573   gboolean first = TRUE;
574
575   iter = pango_layout_get_iter (layout);
576
577   pango_layout_iter_get_layout_extents (iter, NULL, &layout_logical);
578
579   /* Adjust for margins */
580   
581   layout_logical.x += line_display->x_offset * PANGO_SCALE;
582   layout_logical.y += line_display->top_margin * PANGO_SCALE;
583
584   screen_width = line_display->total_width;
585
586   context = gtk_widget_get_style_context (text_renderer->widget);
587
588   state = GTK_STATE_FLAG_SELECTED;
589   if (gtk_widget_has_focus (text_renderer->widget))
590     state |= GTK_STATE_FLAG_FOCUSED;
591
592   gtk_style_context_get_background_color (context, state, &selection);
593
594   do
595     {
596       PangoLayoutLine *line = pango_layout_iter_get_line_readonly (iter);
597       int selection_y, selection_height;
598       int first_y, last_y;
599       PangoRectangle line_rect;
600       int baseline;
601       gboolean at_last_line;
602       
603       pango_layout_iter_get_line_extents (iter, NULL, &line_rect);
604       baseline = pango_layout_iter_get_baseline (iter);
605       pango_layout_iter_get_line_yrange (iter, &first_y, &last_y);
606       
607       /* Adjust for margins */
608
609       line_rect.x += line_display->x_offset * PANGO_SCALE;
610       line_rect.y += line_display->top_margin * PANGO_SCALE;
611       baseline += line_display->top_margin * PANGO_SCALE;
612
613       /* Selection is the height of the line, plus top/bottom
614        * margin if we're the first/last line
615        */
616       selection_y = PANGO_PIXELS (first_y) + line_display->top_margin;
617       selection_height = PANGO_PIXELS (last_y) - PANGO_PIXELS (first_y);
618
619       if (first)
620         {
621           selection_y -= line_display->top_margin;
622           selection_height += line_display->top_margin;
623         }
624
625       at_last_line = pango_layout_iter_at_last_line (iter);
626       if (at_last_line)
627         selection_height += line_display->bottom_margin;
628       
629       first = FALSE;
630
631       if (selection_start_index < byte_offset &&
632           selection_end_index > line->length + byte_offset) /* All selected */
633         {
634           cairo_t *cr = text_renderer->cr;
635
636           cairo_save (cr);
637           gdk_cairo_set_source_rgba (cr, &selection);
638           cairo_rectangle (cr, 
639                            line_display->left_margin, selection_y,
640                            screen_width, selection_height);
641           cairo_fill (cr);
642           cairo_restore(cr);
643
644           text_renderer_set_state (text_renderer, SELECTED);
645           pango_renderer_draw_layout_line (PANGO_RENDERER (text_renderer),
646                                            line, 
647                                            line_rect.x,
648                                            baseline);
649         }
650       else
651         {
652           if (line_display->pg_bg_rgba)
653             {
654               cairo_t *cr = text_renderer->cr;
655
656               cairo_save (cr);
657  
658               gdk_cairo_set_source_rgba (text_renderer->cr, line_display->pg_bg_rgba);
659               cairo_rectangle (cr, 
660                                line_display->left_margin, selection_y,
661                                screen_width, selection_height);
662               cairo_fill (cr);
663
664               cairo_restore (cr);
665             }
666         
667           text_renderer_set_state (text_renderer, NORMAL);
668           pango_renderer_draw_layout_line (PANGO_RENDERER (text_renderer),
669                                            line, 
670                                            line_rect.x,
671                                            baseline);
672
673           /* Check if some part of the line is selected; the newline
674            * that is after line->length for the last line of the
675            * paragraph counts as part of the line for this
676            */
677           if ((selection_start_index < byte_offset + line->length ||
678                (selection_start_index == byte_offset + line->length && pango_layout_iter_at_last_line (iter))) &&
679               selection_end_index > byte_offset)
680             {
681               cairo_t *cr = text_renderer->cr;
682               cairo_region_t *clip_region = get_selected_clip (text_renderer, layout, line,
683                                                           line_display->x_offset,
684                                                           selection_y,
685                                                           selection_height,
686                                                           selection_start_index, selection_end_index);
687
688               cairo_save (cr);
689               gdk_cairo_region (cr, clip_region);
690               cairo_clip (cr);
691               cairo_region_destroy (clip_region);
692
693               gdk_cairo_set_source_rgba (cr, &selection);
694               cairo_rectangle (cr,
695                                PANGO_PIXELS (line_rect.x),
696                                selection_y,
697                                PANGO_PIXELS (line_rect.width),
698                                selection_height);
699               cairo_fill (cr);
700
701               text_renderer_set_state (text_renderer, SELECTED);
702               pango_renderer_draw_layout_line (PANGO_RENDERER (text_renderer),
703                                                line, 
704                                                line_rect.x,
705                                                baseline);
706
707               cairo_restore (cr);
708
709               /* Paint in the ends of the line */
710               if (line_rect.x > line_display->left_margin * PANGO_SCALE &&
711                   ((line_display->direction == GTK_TEXT_DIR_LTR && selection_start_index < byte_offset) ||
712                    (line_display->direction == GTK_TEXT_DIR_RTL && selection_end_index > byte_offset + line->length)))
713                 {
714                   cairo_save (cr);
715
716                   gdk_cairo_set_source_rgba (cr, &selection);
717                   cairo_rectangle (cr,
718                                    line_display->left_margin,
719                                    selection_y,
720                                    PANGO_PIXELS (line_rect.x) - line_display->left_margin,
721                                    selection_height);
722                   cairo_fill (cr);
723
724                   cairo_restore (cr);
725                 }
726
727               if (line_rect.x + line_rect.width <
728                   (screen_width + line_display->left_margin) * PANGO_SCALE &&
729                   ((line_display->direction == GTK_TEXT_DIR_LTR && selection_end_index > byte_offset + line->length) ||
730                    (line_display->direction == GTK_TEXT_DIR_RTL && selection_start_index < byte_offset)))
731                 {
732                   int nonlayout_width;
733
734                   nonlayout_width =
735                     line_display->left_margin + screen_width -
736                     PANGO_PIXELS (line_rect.x) - PANGO_PIXELS (line_rect.width);
737
738                   cairo_save (cr);
739
740                   gdk_cairo_set_source_rgba (cr, &selection);
741                   cairo_rectangle (cr,
742                                    PANGO_PIXELS (line_rect.x) + PANGO_PIXELS (line_rect.width),
743                                    selection_y,
744                                    nonlayout_width,
745                                    selection_height);
746                   cairo_fill (cr);
747
748                   cairo_restore (cr);
749                 }
750             }
751           else if (line_display->has_block_cursor &&
752                    gtk_widget_has_focus (text_renderer->widget) &&
753                    byte_offset <= line_display->insert_index &&
754                    (line_display->insert_index < byte_offset + line->length ||
755                     (at_last_line && line_display->insert_index == byte_offset + line->length)))
756             {
757               GdkRectangle cursor_rect;
758               GdkRGBA cursor_color;
759               cairo_t *cr = text_renderer->cr;
760
761               /* we draw text using base color on filled cursor rectangle of cursor color
762                * (normally white on black) */
763               _gtk_style_context_get_cursor_color (context, &cursor_color, NULL);
764
765               cursor_rect.x = line_display->x_offset + line_display->block_cursor.x;
766               cursor_rect.y = line_display->block_cursor.y + line_display->top_margin;
767               cursor_rect.width = line_display->block_cursor.width;
768               cursor_rect.height = line_display->block_cursor.height;
769
770               cairo_save (cr);
771
772               gdk_cairo_rectangle (cr, &cursor_rect);
773               cairo_clip (cr);
774
775               gdk_cairo_set_source_rgba (cr, &cursor_color);
776               cairo_paint (cr);
777
778               /* draw text under the cursor if any */
779               if (!line_display->cursor_at_line_end)
780                 {
781                   GdkRGBA color;
782
783                   state = gtk_widget_get_state_flags (text_renderer->widget);
784                   gtk_style_context_get_background_color (context, state, &color);
785
786                   gdk_cairo_set_source_rgba (cr, &color);
787
788                   text_renderer_set_state (text_renderer, CURSOR);
789
790                   pango_renderer_draw_layout_line (PANGO_RENDERER (text_renderer),
791                                                    line,
792                                                    line_rect.x,
793                                                    baseline);
794                 }
795
796               cairo_restore (cr);
797             }
798         }
799
800       byte_offset += line->length;
801     }
802   while (pango_layout_iter_next_line (iter));
803
804   pango_layout_iter_free (iter);
805 }
806
807 static GtkTextRenderer *
808 get_text_renderer (void)
809 {
810   static GtkTextRenderer *text_renderer = NULL;
811
812   if (!text_renderer)
813     text_renderer = g_object_new (GTK_TYPE_TEXT_RENDERER, NULL);
814
815   return text_renderer;
816 }
817
818 void
819 gtk_text_layout_draw (GtkTextLayout *layout,
820                       GtkWidget *widget,
821                       cairo_t *cr,
822                       GList **widgets)
823 {
824   gint offset_y;
825   GSList *cursor_list;
826   GtkTextRenderer *text_renderer;
827   GtkTextIter selection_start, selection_end;
828   gboolean have_selection = FALSE;
829   GSList *line_list;
830   GSList *tmp_list;
831   GList *tmp_widgets;
832   GdkRectangle clip;
833
834   g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
835   g_return_if_fail (layout->default_style != NULL);
836   g_return_if_fail (layout->buffer != NULL);
837   g_return_if_fail (cr != NULL);
838
839   if (!gdk_cairo_get_clip_rectangle (cr, &clip))
840     return;
841
842   line_list = gtk_text_layout_get_lines (layout, clip.y, clip.y + clip.height, &offset_y);
843
844   if (line_list == NULL)
845     return; /* nothing on the screen */
846
847   text_renderer = get_text_renderer ();
848   text_renderer_begin (text_renderer, widget, cr);
849
850   /* text_renderer_begin/end does cairo_save/restore */
851   cairo_translate (cr, 0, offset_y);
852
853   gtk_text_layout_wrap_loop_start (layout);
854
855   if (gtk_text_buffer_get_selection_bounds (layout->buffer,
856                                             &selection_start,
857                                             &selection_end))
858     have_selection = TRUE;
859
860   tmp_list = line_list;
861   while (tmp_list != NULL)
862     {
863       GtkTextLineDisplay *line_display;
864       gint selection_start_index = -1;
865       gint selection_end_index = -1;
866       gboolean have_strong;
867       gboolean have_weak;
868
869       GtkTextLine *line = tmp_list->data;
870
871       line_display = gtk_text_layout_get_line_display (layout, line, FALSE);
872
873       if (line_display->height > 0)
874         {
875           g_assert (line_display->layout != NULL);
876           
877           if (have_selection)
878             {
879               GtkTextIter line_start, line_end;
880               gint byte_count;
881               
882               gtk_text_layout_get_iter_at_line (layout,
883                                                 &line_start,
884                                                 line, 0);
885               line_end = line_start;
886               if (!gtk_text_iter_ends_line (&line_end))
887                 gtk_text_iter_forward_to_line_end (&line_end);
888               byte_count = gtk_text_iter_get_visible_line_index (&line_end);     
889
890               if (gtk_text_iter_compare (&selection_start, &line_end) <= 0 &&
891                   gtk_text_iter_compare (&selection_end, &line_start) >= 0)
892                 {
893                   if (gtk_text_iter_compare (&selection_start, &line_start) >= 0)
894                     selection_start_index = gtk_text_iter_get_visible_line_index (&selection_start);
895                   else
896                     selection_start_index = -1;
897
898                   if (gtk_text_iter_compare (&selection_end, &line_end) <= 0)
899                     selection_end_index = gtk_text_iter_get_visible_line_index (&selection_end);
900                   else
901                     selection_end_index = byte_count + 1; /* + 1 to flag past-the-end */
902                 }
903             }
904
905           render_para (text_renderer, line_display,
906                        selection_start_index, selection_end_index);
907
908           /* We paint the cursors last, because they overlap another chunk
909          and need to appear on top. */
910
911           have_strong = FALSE;
912           have_weak = FALSE;
913           
914           cursor_list = line_display->cursors;
915           while (cursor_list)
916             {
917               GtkTextCursorDisplay *cursor = cursor_list->data;
918               if (cursor->is_strong)
919                 have_strong = TRUE;
920               else
921                 have_weak = TRUE;
922               
923               cursor_list = cursor_list->next;
924             }
925           
926           cursor_list = line_display->cursors;
927           while (cursor_list)
928             {
929               GtkTextCursorDisplay *cursor = cursor_list->data;
930               GtkTextDirection dir;
931               GdkRectangle cursor_location;
932
933               dir = line_display->direction;
934               if (have_strong && have_weak)
935                 {
936                   if (!cursor->is_strong)
937                     dir = (dir == GTK_TEXT_DIR_RTL) ? GTK_TEXT_DIR_LTR : GTK_TEXT_DIR_RTL;
938                 }
939  
940               cursor_location.x = line_display->x_offset + cursor->x;
941               cursor_location.y = line_display->top_margin + cursor->y;
942               cursor_location.width = 0;
943               cursor_location.height = cursor->height;
944
945               gtk_draw_insertion_cursor (widget, cr, &cursor_location,
946                                          cursor->is_strong,
947                                          dir, have_strong && have_weak);
948
949               cursor_list = cursor_list->next;
950             }
951         } /* line_display->height > 0 */
952           
953       cairo_translate (cr, 0, line_display->height);
954       gtk_text_layout_free_line_display (layout, line_display);
955       
956       tmp_list = g_slist_next (tmp_list);
957     }
958
959   gtk_text_layout_wrap_loop_end (layout);
960
961   tmp_widgets = text_renderer_end (text_renderer);
962   if (widgets)
963     *widgets = tmp_widgets;
964   else
965     {
966       g_list_foreach (tmp_widgets, (GFunc)g_object_unref, NULL);
967       g_list_free (tmp_widgets);
968     }
969
970   g_slist_free (line_list);
971 }