]> Pileus Git - ~andy/gtk/blob - gtk/gtktextdisplay.c
gdk: Allow display subclasses to override the type used for windows
[~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 "gtkintl.h"
81 /* DO NOT go putting private headers in here. This file should only
82  * use the semi-public headers, as with gtktextview.c.
83  */
84
85 #define GTK_TYPE_TEXT_RENDERER            (_gtk_text_renderer_get_type())
86 #define GTK_TEXT_RENDERER(object)         (G_TYPE_CHECK_INSTANCE_CAST ((object), GTK_TYPE_TEXT_RENDERER, GtkTextRenderer))
87 #define GTK_IS_TEXT_RENDERER(object)      (G_TYPE_CHECK_INSTANCE_TYPE ((object), GTK_TYPE_TEXT_RENDERER))
88 #define GTK_TEXT_RENDERER_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_TEXT_RENDERER, GtkTextRendererClass))
89 #define GTK_IS_TEXT_RENDERER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_TEXT_RENDERER))
90 #define GTK_TEXT_RENDERER_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_TEXT_RENDERER, GtkTextRendererClass))
91
92 typedef struct _GtkTextRenderer      GtkTextRenderer;
93 typedef struct _GtkTextRendererClass GtkTextRendererClass;
94
95 enum {
96   NORMAL,
97   SELECTED,
98   CURSOR
99 };
100
101 struct _GtkTextRenderer
102 {
103   PangoRenderer parent_instance;
104
105   GtkWidget *widget;
106   cairo_t *cr;
107   
108   GdkColor *error_color;        /* Error underline color for this widget */
109   GList *widgets;               /* widgets encountered when drawing */
110   
111   int state;
112 };
113
114 struct _GtkTextRendererClass
115 {
116   PangoRendererClass parent_class;
117 };
118
119 G_DEFINE_TYPE (GtkTextRenderer, _gtk_text_renderer, PANGO_TYPE_RENDERER)
120
121 static GdkColor *
122 text_renderer_get_error_color (GtkTextRenderer *text_renderer)
123 {
124   static const GdkColor red = { 0, 0xffff, 0, 0 };
125
126   if (!text_renderer->error_color)
127     gtk_widget_style_get (text_renderer->widget,
128                           "error-underline-color", &text_renderer->error_color,
129                           NULL);
130   
131   if (!text_renderer->error_color)
132     text_renderer->error_color = gdk_color_copy (&red);
133
134   return text_renderer->error_color;
135 }
136
137 static void
138 text_renderer_set_gdk_color (GtkTextRenderer *text_renderer,
139                              PangoRenderPart  part,
140                              GdkColor        *gdk_color)
141 {
142   PangoRenderer *renderer = PANGO_RENDERER (text_renderer);
143
144   if (gdk_color)
145     {
146       PangoColor color;
147
148       color.red = gdk_color->red;
149       color.green = gdk_color->green;
150       color.blue = gdk_color->blue;
151       
152       pango_renderer_set_color (renderer, part, &color);
153     }
154   else
155     pango_renderer_set_color (renderer, part, NULL);
156            
157 }
158
159 static GtkTextAppearance *
160 get_item_appearance (PangoItem *item)
161 {
162   GSList *tmp_list = item->analysis.extra_attrs;
163
164   while (tmp_list)
165     {
166       PangoAttribute *attr = tmp_list->data;
167
168       if (attr->klass->type == gtk_text_attr_appearance_type)
169         return &((GtkTextAttrAppearance *)attr)->appearance;
170
171       tmp_list = tmp_list->next;
172     }
173
174   return NULL;
175 }
176
177 static void
178 gtk_text_renderer_prepare_run (PangoRenderer  *renderer,
179                                PangoLayoutRun *run)
180 {
181   GtkTextRenderer *text_renderer = GTK_TEXT_RENDERER (renderer);
182   GtkStyle *style;
183   GdkColor *bg_color, *fg_color, *underline_color;
184   GtkTextAppearance *appearance;
185
186   PANGO_RENDERER_CLASS (_gtk_text_renderer_parent_class)->prepare_run (renderer, run);
187
188   appearance = get_item_appearance (run->item);
189   g_assert (appearance != NULL);
190
191   if (appearance->draw_bg && text_renderer->state == NORMAL)
192     bg_color = &appearance->bg_color;
193   else
194     bg_color = NULL;
195   
196   text_renderer_set_gdk_color (text_renderer, PANGO_RENDER_PART_BACKGROUND, bg_color);
197
198   style = gtk_widget_get_style (text_renderer->widget);
199   if (text_renderer->state == SELECTED)
200     {
201       if (gtk_widget_has_focus (text_renderer->widget))
202         fg_color = &style->text[GTK_STATE_SELECTED];
203       else
204         fg_color = &style->text[GTK_STATE_ACTIVE];
205     }
206   else if (text_renderer->state == CURSOR && gtk_widget_has_focus (text_renderer->widget))
207     fg_color = &style->base[GTK_STATE_NORMAL];
208   else
209     fg_color = &appearance->fg_color;
210
211   text_renderer_set_gdk_color (text_renderer, PANGO_RENDER_PART_FOREGROUND, fg_color);
212   text_renderer_set_gdk_color (text_renderer, PANGO_RENDER_PART_STRIKETHROUGH, fg_color);
213
214   if (appearance->underline == PANGO_UNDERLINE_ERROR)
215     underline_color = text_renderer_get_error_color (text_renderer);
216   else
217     underline_color = fg_color;
218
219   text_renderer_set_gdk_color (text_renderer, PANGO_RENDER_PART_UNDERLINE, underline_color);
220 }
221
222 static void
223 set_color (GtkTextRenderer *text_renderer,
224            PangoRenderPart  part)
225 {
226   PangoColor *color;
227
228   cairo_save (text_renderer->cr);
229
230   color = pango_renderer_get_color (PANGO_RENDERER (text_renderer), part);
231   if (color)
232     cairo_set_source_rgb (text_renderer->cr,
233                           color->red / 65535.,
234                           color->green / 65535.,
235                           color->blue / 65535.);
236 }
237
238 static void
239 unset_color (GtkTextRenderer *text_renderer)
240 {
241   cairo_restore (text_renderer->cr);
242 }
243
244 static void
245 gtk_text_renderer_draw_glyphs (PangoRenderer     *renderer,
246                                PangoFont         *font,
247                                PangoGlyphString  *glyphs,
248                                int                x,
249                                int                y)
250 {
251   GtkTextRenderer *text_renderer = GTK_TEXT_RENDERER (renderer);
252
253   set_color (text_renderer, PANGO_RENDER_PART_FOREGROUND);
254
255   cairo_move_to (text_renderer->cr, (double)x / PANGO_SCALE, (double)y / PANGO_SCALE);
256   pango_cairo_show_glyph_string (text_renderer->cr, font, glyphs);
257
258   unset_color (text_renderer);
259 }
260
261 static void
262 gtk_text_renderer_draw_glyph_item (PangoRenderer     *renderer,
263                                    const char        *text,
264                                    PangoGlyphItem    *glyph_item,
265                                    int                x,
266                                    int                y)
267 {
268   GtkTextRenderer *text_renderer = GTK_TEXT_RENDERER (renderer);
269
270   set_color (text_renderer, PANGO_RENDER_PART_FOREGROUND);
271
272   cairo_move_to (text_renderer->cr, (double)x / PANGO_SCALE, (double)y / PANGO_SCALE);
273   pango_cairo_show_glyph_item (text_renderer->cr, text, glyph_item);
274
275   unset_color (text_renderer);
276 }
277
278 static void
279 gtk_text_renderer_draw_rectangle (PangoRenderer     *renderer,
280                                   PangoRenderPart    part,
281                                   int                x,
282                                   int                y,
283                                   int                width,
284                                   int                height)
285 {
286   GtkTextRenderer *text_renderer = GTK_TEXT_RENDERER (renderer);
287
288   set_color (text_renderer, part);
289
290   cairo_rectangle (text_renderer->cr,
291                    (double)x / PANGO_SCALE, (double)y / PANGO_SCALE,
292                    (double)width / PANGO_SCALE, (double)height / PANGO_SCALE);
293   cairo_fill (text_renderer->cr);
294
295   unset_color (text_renderer);
296 }
297
298 static void
299 gtk_text_renderer_draw_trapezoid (PangoRenderer     *renderer,
300                                   PangoRenderPart    part,
301                                   double             y1_,
302                                   double             x11,
303                                   double             x21,
304                                   double             y2,
305                                   double             x12,
306                                   double             x22)
307 {
308   GtkTextRenderer *text_renderer = GTK_TEXT_RENDERER (renderer);
309   cairo_t *cr;
310   cairo_matrix_t matrix;
311
312   set_color (text_renderer, part);
313
314   cr = text_renderer->cr;
315
316   cairo_get_matrix (cr, &matrix);
317   matrix.xx = matrix.yy = 1.0;
318   matrix.xy = matrix.yx = 0.0;
319   cairo_set_matrix (cr, &matrix);
320
321   cairo_move_to (cr, x11, y1_);
322   cairo_line_to (cr, x21, y1_);
323   cairo_line_to (cr, x22, y2);
324   cairo_line_to (cr, x12, y2);
325   cairo_close_path (cr);
326
327   cairo_fill (cr);
328
329   unset_color (text_renderer);
330 }
331
332 static void
333 gtk_text_renderer_draw_error_underline (PangoRenderer *renderer,
334                                         int            x,
335                                         int            y,
336                                         int            width,
337                                         int            height)
338 {
339   GtkTextRenderer *text_renderer = GTK_TEXT_RENDERER (renderer);
340
341   set_color (text_renderer, PANGO_RENDER_PART_UNDERLINE);
342
343   pango_cairo_show_error_underline (text_renderer->cr,
344                                     (double)x / PANGO_SCALE, (double)y / PANGO_SCALE,
345                                     (double)width / PANGO_SCALE, (double)height / PANGO_SCALE);
346
347   unset_color (text_renderer);
348 }
349
350 static void
351 gtk_text_renderer_draw_shape (PangoRenderer   *renderer,
352                               PangoAttrShape  *attr,
353                               int              x,
354                               int              y)
355 {
356   GtkTextRenderer *text_renderer = GTK_TEXT_RENDERER (renderer);
357   GtkStyle *style;
358   GdkColor *fg;
359
360   style = gtk_widget_get_style (text_renderer->widget);
361   if (text_renderer->state == SELECTED)
362     {
363       if (gtk_widget_has_focus (text_renderer->widget))
364         fg = &style->text[GTK_STATE_SELECTED];
365       else
366         fg = &style->text[GTK_STATE_SELECTED];
367     }
368   else if (text_renderer->state == CURSOR && gtk_widget_has_focus (text_renderer->widget))
369     fg = &style->base[GTK_STATE_NORMAL];
370   else
371     fg = &style->text[GTK_STATE_NORMAL];
372
373   if (attr->data == NULL)
374     {
375       /* This happens if we have an empty widget anchor. Draw
376        * something empty-looking.
377        */
378       GdkRectangle shape_rect;
379       cairo_t *cr;
380       
381       shape_rect.x = PANGO_PIXELS (x);
382       shape_rect.y = PANGO_PIXELS (y + attr->logical_rect.y);
383       shape_rect.width = PANGO_PIXELS (x + attr->logical_rect.width) - shape_rect.x;
384       shape_rect.height = PANGO_PIXELS (y + attr->logical_rect.y + attr->logical_rect.height) - shape_rect.y;
385       
386       cr = text_renderer->cr;
387
388       cairo_save (cr);
389
390       cairo_set_line_width (cr, 1.0);
391       gdk_cairo_set_source_color (cr, fg);
392
393       cairo_rectangle (cr,
394                        shape_rect.x + 0.5, shape_rect.y + 0.5,
395                        shape_rect.width - 1, shape_rect.height - 1);
396       cairo_move_to (cr, shape_rect.x + 0.5, shape_rect.y + 0.5);
397       cairo_line_to (cr, 
398                      shape_rect.x + shape_rect.width - 0.5,
399                      shape_rect.y + shape_rect.height - 0.5);
400       cairo_move_to (cr, shape_rect.x + 0.5,
401                      shape_rect.y + shape_rect.height - 0.5);
402       cairo_line_to (cr, shape_rect.x + shape_rect.width - 0.5,
403                      shape_rect.y + 0.5);
404
405       cairo_stroke (cr);
406       
407       cairo_restore (cr);
408     }
409   else if (GDK_IS_PIXBUF (attr->data))
410     {
411       cairo_t *cr = text_renderer->cr;
412       GdkPixbuf *pixbuf = GDK_PIXBUF (attr->data);
413       
414       cairo_save (cr);
415
416       gdk_cairo_set_source_pixbuf (cr, pixbuf,
417                                    PANGO_PIXELS (x),
418                                    PANGO_PIXELS (y) -  gdk_pixbuf_get_height (pixbuf));
419       cairo_paint (cr);
420
421       cairo_restore (cr);
422     }
423   else if (GTK_IS_WIDGET (attr->data))
424     {
425       GtkWidget *widget;
426       
427       widget = GTK_WIDGET (attr->data);
428
429       text_renderer->widgets = g_list_prepend (text_renderer->widgets,
430                                                g_object_ref (widget));
431     }
432   else
433     g_assert_not_reached (); /* not a pixbuf or widget */
434 }
435
436 static void
437 gtk_text_renderer_finalize (GObject *object)
438 {
439   G_OBJECT_CLASS (_gtk_text_renderer_parent_class)->finalize (object);
440 }
441
442 static void
443 _gtk_text_renderer_init (GtkTextRenderer *renderer)
444 {
445 }
446
447 static void
448 _gtk_text_renderer_class_init (GtkTextRendererClass *klass)
449 {
450   GObjectClass *object_class = G_OBJECT_CLASS (klass);
451   
452   PangoRendererClass *renderer_class = PANGO_RENDERER_CLASS (klass);
453   
454   renderer_class->prepare_run = gtk_text_renderer_prepare_run;
455   renderer_class->draw_glyphs = gtk_text_renderer_draw_glyphs;
456   renderer_class->draw_glyph_item = gtk_text_renderer_draw_glyph_item;
457   renderer_class->draw_rectangle = gtk_text_renderer_draw_rectangle;
458   renderer_class->draw_trapezoid = gtk_text_renderer_draw_trapezoid;
459   renderer_class->draw_error_underline = gtk_text_renderer_draw_error_underline;
460   renderer_class->draw_shape = gtk_text_renderer_draw_shape;
461
462   object_class->finalize = gtk_text_renderer_finalize;
463 }
464
465 static void
466 text_renderer_set_state (GtkTextRenderer *text_renderer,
467                          int              state)
468 {
469   text_renderer->state = state;
470 }
471
472 static void
473 text_renderer_begin (GtkTextRenderer *text_renderer,
474                      GtkWidget       *widget,
475                      cairo_t         *cr)
476 {
477   text_renderer->widget = widget;
478   text_renderer->cr = cr;
479 }
480
481 /* Returns a GSList of (referenced) widgets encountered while drawing.
482  */
483 static GList *
484 text_renderer_end (GtkTextRenderer *text_renderer)
485 {
486   GList *widgets = text_renderer->widgets;
487
488   text_renderer->widget = NULL;
489   text_renderer->cr = NULL;
490
491   text_renderer->widgets = NULL;
492
493   if (text_renderer->error_color)
494     {
495       gdk_color_free (text_renderer->error_color);
496       text_renderer->error_color = NULL;
497     }
498
499   return widgets;
500 }
501
502
503 static cairo_region_t *
504 get_selected_clip (GtkTextRenderer    *text_renderer,
505                    PangoLayout        *layout,
506                    PangoLayoutLine    *line,
507                    int                 x,
508                    int                 y,
509                    int                 height,
510                    int                 start_index,
511                    int                 end_index)
512 {
513   gint *ranges;
514   gint n_ranges, i;
515   cairo_region_t *clip_region = cairo_region_create ();
516
517   pango_layout_line_get_x_ranges (line, start_index, end_index, &ranges, &n_ranges);
518
519   for (i=0; i < n_ranges; i++)
520     {
521       GdkRectangle rect;
522
523       rect.x = x + PANGO_PIXELS (ranges[2*i]);
524       rect.y = y;
525       rect.width = PANGO_PIXELS (ranges[2*i + 1]) - PANGO_PIXELS (ranges[2*i]);
526       rect.height = height;
527       
528       cairo_region_union_rectangle (clip_region, &rect);
529     }
530
531   g_free (ranges);
532   return clip_region;
533 }
534
535 static void
536 render_para (GtkTextRenderer    *text_renderer,
537              GtkTextLineDisplay *line_display,
538              /* Top-left corner of paragraph including all margins */
539              int                 x,
540              int                 y,
541              int                 selection_start_index,
542              int                 selection_end_index)
543 {
544   GtkStyle *style;
545   PangoLayout *layout = line_display->layout;
546   int byte_offset = 0;
547   PangoLayoutIter *iter;
548   PangoRectangle layout_logical;
549   int screen_width;
550   GdkColor *selection;
551   gint state;
552   gboolean first = TRUE;
553
554   style = gtk_widget_get_style (text_renderer->widget);
555
556   iter = pango_layout_get_iter (layout);
557
558   pango_layout_iter_get_layout_extents (iter, NULL, &layout_logical);
559
560   /* Adjust for margins */
561   
562   layout_logical.x += line_display->x_offset * PANGO_SCALE;
563   layout_logical.y += line_display->top_margin * PANGO_SCALE;
564
565   screen_width = line_display->total_width;
566   
567   if (gtk_widget_has_focus (text_renderer->widget))
568     state = GTK_STATE_SELECTED;
569   else
570     state = GTK_STATE_ACTIVE;
571
572   selection = &style->base [state];
573
574   do
575     {
576       PangoLayoutLine *line = pango_layout_iter_get_line_readonly (iter);
577       int selection_y, selection_height;
578       int first_y, last_y;
579       PangoRectangle line_rect;
580       int baseline;
581       gboolean at_last_line;
582       
583       pango_layout_iter_get_line_extents (iter, NULL, &line_rect);
584       baseline = pango_layout_iter_get_baseline (iter);
585       pango_layout_iter_get_line_yrange (iter, &first_y, &last_y);
586       
587       /* Adjust for margins */
588
589       line_rect.x += line_display->x_offset * PANGO_SCALE;
590       line_rect.y += line_display->top_margin * PANGO_SCALE;
591       baseline += line_display->top_margin * PANGO_SCALE;
592
593       /* Selection is the height of the line, plus top/bottom
594        * margin if we're the first/last line
595        */
596       selection_y = y + PANGO_PIXELS (first_y) + line_display->top_margin;
597       selection_height = PANGO_PIXELS (last_y) - PANGO_PIXELS (first_y);
598
599       if (first)
600         {
601           selection_y -= line_display->top_margin;
602           selection_height += line_display->top_margin;
603         }
604
605       at_last_line = pango_layout_iter_at_last_line (iter);
606       if (at_last_line)
607         selection_height += line_display->bottom_margin;
608       
609       first = FALSE;
610
611       if (selection_start_index < byte_offset &&
612           selection_end_index > line->length + byte_offset) /* All selected */
613         {
614           cairo_t *cr = text_renderer->cr;
615
616           cairo_save (cr);
617           gdk_cairo_set_source_color (cr, selection);
618           cairo_rectangle (cr, 
619                            x + line_display->left_margin, selection_y,
620                            screen_width, selection_height);
621           cairo_fill (cr);
622           cairo_restore(cr);
623
624           text_renderer_set_state (text_renderer, SELECTED);
625           pango_renderer_draw_layout_line (PANGO_RENDERER (text_renderer),
626                                            line, 
627                                            PANGO_SCALE * x + line_rect.x,
628                                            PANGO_SCALE * y + baseline);
629         }
630       else
631         {
632           if (line_display->pg_bg_color)
633             {
634               cairo_t *cr = text_renderer->cr;
635
636               cairo_save (cr);
637
638               gdk_cairo_set_source_color (cr, line_display->pg_bg_color);
639               cairo_rectangle (cr, 
640                                x + line_display->left_margin, selection_y,
641                                screen_width, selection_height);
642               cairo_fill (cr);
643
644               cairo_restore (cr);
645             }
646         
647           text_renderer_set_state (text_renderer, NORMAL);
648           pango_renderer_draw_layout_line (PANGO_RENDERER (text_renderer),
649                                            line, 
650                                            PANGO_SCALE * x + line_rect.x,
651                                            PANGO_SCALE * y + baseline);
652
653           /* Check if some part of the line is selected; the newline
654            * that is after line->length for the last line of the
655            * paragraph counts as part of the line for this
656            */
657           if ((selection_start_index < byte_offset + line->length ||
658                (selection_start_index == byte_offset + line->length && pango_layout_iter_at_last_line (iter))) &&
659               selection_end_index > byte_offset)
660             {
661               cairo_t *cr = text_renderer->cr;
662               cairo_region_t *clip_region = get_selected_clip (text_renderer, layout, line,
663                                                           x + line_display->x_offset,
664                                                           selection_y,
665                                                           selection_height,
666                                                           selection_start_index, selection_end_index);
667
668               cairo_save (cr);
669               gdk_cairo_region (cr, clip_region);
670               cairo_clip (cr);
671               cairo_region_destroy (clip_region);
672
673               gdk_cairo_set_source_color (cr, selection);
674               cairo_rectangle (cr,
675                                x + PANGO_PIXELS (line_rect.x),
676                                selection_y,
677                                PANGO_PIXELS (line_rect.width),
678                                selection_height);
679               cairo_fill (cr);
680
681               text_renderer_set_state (text_renderer, SELECTED);
682               pango_renderer_draw_layout_line (PANGO_RENDERER (text_renderer),
683                                                line, 
684                                                PANGO_SCALE * x + line_rect.x,
685                                                PANGO_SCALE * y + baseline);
686
687               cairo_restore (cr);
688
689               /* Paint in the ends of the line */
690               if (line_rect.x > line_display->left_margin * PANGO_SCALE &&
691                   ((line_display->direction == GTK_TEXT_DIR_LTR && selection_start_index < byte_offset) ||
692                    (line_display->direction == GTK_TEXT_DIR_RTL && selection_end_index > byte_offset + line->length)))
693                 {
694                   cairo_save (cr);
695
696                   gdk_cairo_set_source_color (cr, selection);
697                   cairo_rectangle (cr,
698                                    x + line_display->left_margin,
699                                    selection_y,
700                                    PANGO_PIXELS (line_rect.x) - line_display->left_margin,
701                                    selection_height);
702                   cairo_fill (cr);
703
704                   cairo_restore (cr);
705                 }
706
707               if (line_rect.x + line_rect.width <
708                   (screen_width + line_display->left_margin) * PANGO_SCALE &&
709                   ((line_display->direction == GTK_TEXT_DIR_LTR && selection_end_index > byte_offset + line->length) ||
710                    (line_display->direction == GTK_TEXT_DIR_RTL && selection_start_index < byte_offset)))
711                 {
712                   int nonlayout_width;
713
714                   nonlayout_width =
715                     line_display->left_margin + screen_width -
716                     PANGO_PIXELS (line_rect.x) - PANGO_PIXELS (line_rect.width);
717
718                   cairo_save (cr);
719
720                   gdk_cairo_set_source_color (cr, selection);
721                   cairo_rectangle (cr,
722                                    x + PANGO_PIXELS (line_rect.x) + PANGO_PIXELS (line_rect.width),
723                                    selection_y,
724                                    nonlayout_width,
725                                    selection_height);
726                   cairo_fill (cr);
727
728                   cairo_restore (cr);
729                 }
730             }
731           else if (line_display->has_block_cursor &&
732                    gtk_widget_has_focus (text_renderer->widget) &&
733                    byte_offset <= line_display->insert_index &&
734                    (line_display->insert_index < byte_offset + line->length ||
735                     (at_last_line && line_display->insert_index == byte_offset + line->length)))
736             {
737               GdkRectangle cursor_rect;
738               GdkColor cursor_color;
739               cairo_t *cr = text_renderer->cr;
740
741               /* we draw text using base color on filled cursor rectangle of cursor color
742                * (normally white on black) */
743               _gtk_widget_get_cursor_color (text_renderer->widget, &cursor_color);
744
745               cursor_rect.x = x + line_display->x_offset + line_display->block_cursor.x;
746               cursor_rect.y = y + line_display->block_cursor.y + line_display->top_margin;
747               cursor_rect.width = line_display->block_cursor.width;
748               cursor_rect.height = line_display->block_cursor.height;
749
750               cairo_save (cr);
751
752               gdk_cairo_rectangle (cr, &cursor_rect);
753               cairo_clip (cr);
754
755               gdk_cairo_set_source_color (cr, &cursor_color);
756               cairo_paint (cr);
757
758               /* draw text under the cursor if any */
759               if (!line_display->cursor_at_line_end)
760                 {
761                   GtkStateType state;
762                   GtkStyle *style;
763
764                   style = gtk_widget_get_style (text_renderer->widget);
765                   state = gtk_widget_get_state (text_renderer->widget);
766                   gdk_cairo_set_source_color (cr, &style->base[state]);
767
768                   text_renderer_set_state (text_renderer, CURSOR);
769
770                   pango_renderer_draw_layout_line (PANGO_RENDERER (text_renderer),
771                                                    line,
772                                                    PANGO_SCALE * x + line_rect.x,
773                                                    PANGO_SCALE * y + baseline);
774
775                 }
776
777               cairo_restore (cr);
778             }
779         }
780
781       byte_offset += line->length;
782     }
783   while (pango_layout_iter_next_line (iter));
784
785   pango_layout_iter_free (iter);
786 }
787
788 static GtkTextRenderer *
789 get_text_renderer (void)
790 {
791   static GtkTextRenderer *text_renderer = NULL;
792
793   if (!text_renderer)
794     text_renderer = g_object_new (GTK_TYPE_TEXT_RENDERER, NULL);
795
796   return text_renderer;
797 }
798
799 void
800 gtk_text_layout_draw (GtkTextLayout *layout,
801                       GtkWidget *widget,
802                       cairo_t *cr,
803                       GList **widgets)
804 {
805   gint current_y;
806   GSList *cursor_list;
807   GtkTextRenderer *text_renderer;
808   GtkTextIter selection_start, selection_end;
809   gboolean have_selection = FALSE;
810   GSList *line_list;
811   GSList *tmp_list;
812   GList *tmp_widgets;
813   GdkRectangle clip;
814   
815   g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
816   g_return_if_fail (layout->default_style != NULL);
817   g_return_if_fail (layout->buffer != NULL);
818   g_return_if_fail (cr != NULL);
819
820   if (!gdk_cairo_get_clip_rectangle (cr, &clip))
821     return;
822
823   line_list =  gtk_text_layout_get_lines (layout, clip.y, clip.y + clip.height, &current_y);
824
825   if (line_list == NULL)
826     return; /* nothing on the screen */
827
828   cairo_save (cr);
829
830   gdk_cairo_set_source_color (cr, &gtk_widget_get_style (widget)->text[gtk_widget_get_state (widget)]);
831
832   text_renderer = get_text_renderer ();
833   text_renderer_begin (text_renderer, widget, cr);
834
835   gtk_text_layout_wrap_loop_start (layout);
836
837   if (gtk_text_buffer_get_selection_bounds (layout->buffer,
838                                             &selection_start,
839                                             &selection_end))
840     have_selection = TRUE;
841
842   tmp_list = line_list;
843   while (tmp_list != NULL)
844     {
845       GtkTextLineDisplay *line_display;
846       gint selection_start_index = -1;
847       gint selection_end_index = -1;
848       gboolean have_strong;
849       gboolean have_weak;
850
851       GtkTextLine *line = tmp_list->data;
852
853       line_display = gtk_text_layout_get_line_display (layout, line, FALSE);
854
855       if (line_display->height > 0)
856         {
857           g_assert (line_display->layout != NULL);
858           
859           if (have_selection)
860             {
861               GtkTextIter line_start, line_end;
862               gint byte_count;
863               
864               gtk_text_layout_get_iter_at_line (layout,
865                                                 &line_start,
866                                                 line, 0);
867               line_end = line_start;
868               if (!gtk_text_iter_ends_line (&line_end))
869                 gtk_text_iter_forward_to_line_end (&line_end);
870               byte_count = gtk_text_iter_get_visible_line_index (&line_end);     
871
872               if (gtk_text_iter_compare (&selection_start, &line_end) <= 0 &&
873                   gtk_text_iter_compare (&selection_end, &line_start) >= 0)
874                 {
875                   if (gtk_text_iter_compare (&selection_start, &line_start) >= 0)
876                     selection_start_index = gtk_text_iter_get_visible_line_index (&selection_start);
877                   else
878                     selection_start_index = -1;
879
880                   if (gtk_text_iter_compare (&selection_end, &line_end) <= 0)
881                     selection_end_index = gtk_text_iter_get_visible_line_index (&selection_end);
882                   else
883                     selection_end_index = byte_count + 1; /* + 1 to flag past-the-end */
884                 }
885             }
886
887           render_para (text_renderer, line_display,
888                        0, current_y,
889                        selection_start_index, selection_end_index);
890
891           /* We paint the cursors last, because they overlap another chunk
892          and need to appear on top. */
893
894           have_strong = FALSE;
895           have_weak = FALSE;
896           
897           cursor_list = line_display->cursors;
898           while (cursor_list)
899             {
900               GtkTextCursorDisplay *cursor = cursor_list->data;
901               if (cursor->is_strong)
902                 have_strong = TRUE;
903               else
904                 have_weak = TRUE;
905               
906               cursor_list = cursor_list->next;
907             }
908           
909           cursor_list = line_display->cursors;
910           while (cursor_list)
911             {
912               GtkTextCursorDisplay *cursor = cursor_list->data;
913               GtkTextDirection dir;
914               GdkRectangle cursor_location;
915
916               dir = line_display->direction;
917               if (have_strong && have_weak)
918                 {
919                   if (!cursor->is_strong)
920                     dir = (dir == GTK_TEXT_DIR_RTL) ? GTK_TEXT_DIR_LTR : GTK_TEXT_DIR_RTL;
921                 }
922  
923               cursor_location.x = line_display->x_offset + cursor->x;
924               cursor_location.y = current_y + line_display->top_margin + cursor->y;
925               cursor_location.width = 0;
926               cursor_location.height = cursor->height;
927
928               gtk_draw_insertion_cursor (widget, cr, &cursor_location,
929                                          cursor->is_strong,
930                                          dir, have_strong && have_weak);
931
932               cursor_list = cursor_list->next;
933             }
934         } /* line_display->height > 0 */
935           
936       current_y += line_display->height;
937       gtk_text_layout_free_line_display (layout, line_display);
938       
939       tmp_list = g_slist_next (tmp_list);
940     }
941
942   gtk_text_layout_wrap_loop_end (layout);
943
944   tmp_widgets = text_renderer_end (text_renderer);
945   if (widgets)
946     *widgets = tmp_widgets;
947   else
948     {
949       g_list_foreach (tmp_widgets, (GFunc)g_object_unref, NULL);
950       g_list_free (tmp_widgets);
951     }
952
953   cairo_restore (cr);
954
955   g_slist_free (line_list);
956 }