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