]> Pileus Git - ~andy/gtk/blob - gtk/gtktextdisplay.c
e531a4652653851fdb71222c9d178cdd64f40e5b
[~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 "gtktextdisplay.h"
79 /* DO NOT go putting private headers in here. This file should only
80  * use the semi-public headers, as with gtktextview.c.
81  */
82
83 #include <pango/pango.h>
84
85 typedef struct _GtkTextRenderState GtkTextRenderState;
86
87 struct _GtkTextRenderState
88 {
89   GtkWidget *widget;
90
91   GtkTextAppearance *last_appearance;
92   GtkTextAppearance *last_bg_appearance;
93   GdkGC *fg_gc;
94   GdkGC *bg_gc;
95   GdkRectangle clip_rect;
96 };
97
98 static void       get_item_properties (PangoItem           *item,
99                                        GtkTextAppearance  **appearance);
100 static GdkRegion *get_selected_clip   (GtkTextRenderState  *render_state,
101                                        PangoLayout         *layout,
102                                        PangoLayoutLine     *line,
103                                        int                  x,
104                                        int                  y,
105                                        int                  height,
106                                        int                  start_index,
107                                        int                  end_index);
108
109 static GtkTextRenderState *
110 gtk_text_render_state_new (GtkWidget    *widget,
111                            GdkDrawable  *drawable,
112                            GdkRectangle *clip_rect)
113 {
114   GtkTextRenderState *state = g_new0 (GtkTextRenderState, 1);
115
116   state->widget = widget;
117   state->fg_gc = gdk_gc_new (drawable);
118   state->bg_gc = gdk_gc_new (drawable);
119   state->clip_rect = *clip_rect;
120
121   return state;
122 }
123
124 static void
125 gtk_text_render_state_destroy (GtkTextRenderState *state)
126 {
127   g_object_unref (state->fg_gc);
128   g_object_unref (state->bg_gc);
129
130   g_free (state);
131 }
132
133 static void
134 gtk_text_render_state_set_color (GtkTextRenderState *state,
135                                  GdkGC              *gc,
136                                  GdkColor           *color)
137 {
138   gdk_colormap_alloc_color (gtk_widget_get_colormap (state->widget), color, FALSE, TRUE);
139   gdk_gc_set_foreground (gc, color);
140 }
141
142 static void
143 gtk_text_render_state_update (GtkTextRenderState *state,
144                               GtkTextAppearance  *new_appearance)
145 {
146   GdkScreen *screen = gtk_widget_get_screen (state->widget);
147   
148   if (!state->last_appearance ||
149       !gdk_color_equal (&new_appearance->fg_color, &state->last_appearance->fg_color))
150     gtk_text_render_state_set_color (state, state->fg_gc, &new_appearance->fg_color);
151
152   if (!state->last_appearance ||
153       new_appearance->fg_stipple != state->last_appearance->fg_stipple)
154     {
155       if (new_appearance->fg_stipple)
156         {
157           if (screen == gdk_drawable_get_screen (new_appearance->fg_stipple))
158             {
159               gdk_gc_set_fill (state->fg_gc, GDK_STIPPLED);
160               gdk_gc_set_stipple (state->fg_gc, new_appearance->fg_stipple);
161             }
162           else
163             g_warning ("gtk_text_render_state_update:\n"
164                        "The foreground stipple bitmap has been created on the wrong screen.\n"
165                        "Ignoring the stipple bitmap information.");
166         }
167       else
168         {
169           gdk_gc_set_fill (state->fg_gc, GDK_SOLID);
170         }
171     }
172
173   if (new_appearance->draw_bg)
174     {
175       if (!state->last_bg_appearance ||
176           !gdk_color_equal (&new_appearance->bg_color, &state->last_bg_appearance->bg_color))
177         gtk_text_render_state_set_color (state, state->bg_gc, &new_appearance->bg_color);
178
179       if (!state->last_bg_appearance ||
180           new_appearance->bg_stipple != state->last_bg_appearance->bg_stipple)
181         {
182           if (new_appearance->bg_stipple)
183             {
184               if (screen == gdk_drawable_get_screen (new_appearance->bg_stipple))
185                 {
186                   gdk_gc_set_fill (state->bg_gc, GDK_STIPPLED);
187                   gdk_gc_set_stipple (state->bg_gc, new_appearance->bg_stipple);
188                 }
189               else
190                 g_warning ("gtk_text_render_state_update:\n"
191                            "The background stipple bitmap has been created on the wrong screen.\n"
192                            "Ignoring the stipple bitmap information.");
193
194             }
195           else
196             {
197               gdk_gc_set_fill (state->bg_gc, GDK_SOLID);
198             }
199         }
200
201       state->last_bg_appearance = new_appearance;
202     }
203
204   state->last_appearance = new_appearance;
205 }
206
207 static void
208 get_shape_extents (PangoLayoutRun *run,
209                    PangoRectangle *ink_rect,
210                    PangoRectangle *logical_rect)
211 {
212   GSList *tmp_list = run->item->analysis.extra_attrs;
213     
214   while (tmp_list)
215     {
216       PangoAttribute *attr = tmp_list->data;
217
218       if (attr->klass->type == PANGO_ATTR_SHAPE)
219         {          
220           if (logical_rect)
221             *logical_rect = ((PangoAttrShape *)attr)->logical_rect;
222
223           if (ink_rect)
224             *ink_rect = ((PangoAttrShape *)attr)->ink_rect;
225
226           return;
227         }
228
229       tmp_list = tmp_list->next;
230     }
231
232   g_assert_not_reached ();
233 }
234
235 static void
236 render_layout_line (GdkDrawable        *drawable,
237                     GtkTextRenderState *render_state,
238                     PangoLayoutLine    *line,
239                     GSList            **shaped_pointer,
240                     int                 x,
241                     int                 y,
242                     gboolean            selected,
243                     GList             **widgets)
244 {
245   GSList *tmp_list = line->runs;
246   PangoRectangle overall_rect;
247   PangoRectangle logical_rect;
248   PangoRectangle ink_rect;
249   gint x_off = 0;
250   GdkGC *fg_gc;
251   
252   pango_layout_line_get_extents (line, NULL, &overall_rect);
253
254   while (tmp_list)
255     {
256       PangoLayoutRun *run = tmp_list->data;
257       GtkTextAppearance *appearance;
258       gint risen_y;
259       gint shaped_width_pixels = 0;
260       gboolean need_ink = FALSE;
261       
262       tmp_list = tmp_list->next;
263
264       get_item_properties (run->item, &appearance);
265
266       g_assert (appearance != NULL);
267       
268       risen_y = y - PANGO_PIXELS (appearance->rise);
269       
270       if (selected)
271         {
272           if (GTK_WIDGET_HAS_FOCUS (render_state->widget))
273             fg_gc = render_state->widget->style->text_gc[GTK_STATE_SELECTED];
274           else
275             fg_gc = render_state->widget->style->text_gc [GTK_STATE_ACTIVE];
276         }
277       else
278         {
279           gtk_text_render_state_update (render_state, appearance);
280           
281           fg_gc = render_state->fg_gc;
282         }
283       
284       if (appearance->underline != PANGO_UNDERLINE_NONE ||
285           appearance->strikethrough)
286         need_ink = TRUE;
287       
288       if (appearance->is_text)
289         {
290           if (need_ink)
291             pango_glyph_string_extents (run->glyphs, run->item->analysis.font,
292                                         &ink_rect, &logical_rect);
293           else
294             pango_glyph_string_extents (run->glyphs, run->item->analysis.font,
295                                         NULL, &logical_rect);
296         }
297       else
298         {
299           if (need_ink)
300             get_shape_extents (run, &ink_rect, &logical_rect);
301           else
302             get_shape_extents (run, NULL, &logical_rect);
303         }
304       
305       if (appearance->draw_bg && !selected)
306         gdk_draw_rectangle (drawable, render_state->bg_gc, TRUE,
307                             x + PANGO_PIXELS (x_off) + PANGO_PIXELS (logical_rect.x),
308                             risen_y + PANGO_PIXELS (logical_rect.y),
309                             PANGO_PIXELS (logical_rect.width),
310                             PANGO_PIXELS (logical_rect.height));
311
312       if (appearance->is_text)
313         gdk_draw_glyphs (drawable, fg_gc,
314                          run->item->analysis.font,
315                          x + PANGO_PIXELS (x_off),
316                          risen_y, run->glyphs);
317       else
318         {
319           GObject *shaped = (*shaped_pointer)->data;
320
321           *shaped_pointer = (*shaped_pointer)->next;
322
323           if (shaped == NULL)
324             {
325               /* This happens if we have an empty widget anchor. Draw
326                * something empty-looking.
327                */
328               GdkRectangle shape_rect, draw_rect;
329
330               shape_rect.x = x + x_off / PANGO_SCALE;
331               shape_rect.y = risen_y - PANGO_PIXELS (logical_rect.height);
332               shape_rect.width = PANGO_PIXELS (logical_rect.width);
333               shape_rect.height = PANGO_PIXELS (logical_rect.height);
334
335               if (gdk_rectangle_intersect (&shape_rect, &render_state->clip_rect,
336                                            &draw_rect))
337                 {
338                   gdk_draw_rectangle (drawable, render_state->fg_gc,
339                                       FALSE, shape_rect.x, shape_rect.y,
340                                       shape_rect.width, shape_rect.height);
341
342                   gdk_draw_line (drawable, render_state->fg_gc,
343                                  shape_rect.x, shape_rect.y,
344                                  shape_rect.x + shape_rect.width,
345                                  shape_rect.y + shape_rect.height);
346
347                   gdk_draw_line (drawable, render_state->fg_gc,
348                                  shape_rect.x + shape_rect.width, shape_rect.y,
349                                  shape_rect.x,
350                                  shape_rect.y + shape_rect.height);
351                 }
352
353               shaped_width_pixels = shape_rect.width;
354             }
355           else if (GDK_IS_PIXBUF (shaped))
356             {
357               gint width, height;
358               GdkRectangle pixbuf_rect, draw_rect;
359               GdkPixbuf *pixbuf;
360
361               pixbuf = GDK_PIXBUF (shaped);
362               
363               width = gdk_pixbuf_get_width (pixbuf);
364               height = gdk_pixbuf_get_height (pixbuf);
365
366               pixbuf_rect.x = x + x_off / PANGO_SCALE;
367               pixbuf_rect.y = risen_y - height;
368               pixbuf_rect.width = width;
369               pixbuf_rect.height = height;
370
371               if (gdk_rectangle_intersect (&pixbuf_rect, &render_state->clip_rect,
372                                            &draw_rect))
373                 {
374                   GdkBitmap *mask = NULL;
375               
376                   if (gdk_pixbuf_get_has_alpha (pixbuf))
377                     {
378                       mask = gdk_pixmap_new (drawable,
379                                              gdk_pixbuf_get_width (pixbuf),
380                                              gdk_pixbuf_get_height (pixbuf),
381                                              1);
382
383                       gdk_pixbuf_render_threshold_alpha (pixbuf, mask,
384                                                          0, 0, 0, 0,
385                                                          gdk_pixbuf_get_width (pixbuf),
386                                                          gdk_pixbuf_get_height (pixbuf),
387                                                          128);
388
389                     }
390
391                   if (mask)
392                     {
393                       gdk_gc_set_clip_mask (render_state->fg_gc, mask);
394                       gdk_gc_set_clip_origin (render_state->fg_gc,
395                                               pixbuf_rect.x, pixbuf_rect.y);
396                     }
397
398                   gdk_draw_pixbuf (drawable,
399                                    render_state->fg_gc,
400                                    pixbuf,
401                                    draw_rect.x - pixbuf_rect.x,
402                                    draw_rect.y - pixbuf_rect.y,
403                                    draw_rect.x, draw_rect.y,
404                                    draw_rect.width,
405                                    draw_rect.height,
406                                    GDK_RGB_DITHER_NORMAL,
407                                    0, 0);
408
409                   if (mask)
410                     {
411                       gdk_gc_set_clip_rectangle (render_state->fg_gc,
412                                                  &render_state->clip_rect);
413                       g_object_unref (mask);
414                     }
415                 }
416
417               shaped_width_pixels = width;
418             }
419           else if (GTK_IS_WIDGET (shaped))
420             {
421               GtkWidget *widget;
422               
423               widget = GTK_WIDGET (shaped);
424               
425               shaped_width_pixels = widget->allocation.width;
426
427               if (widgets)
428                 {
429                   g_object_ref (widget);
430                   *widgets = g_list_prepend (*widgets, widget);
431                 }
432             }
433           else
434             g_assert_not_reached (); /* not a pixbuf or widget */
435         }
436
437       switch (appearance->underline)
438         {
439         case PANGO_UNDERLINE_NONE:
440           break;
441         case PANGO_UNDERLINE_DOUBLE:
442           g_assert (need_ink);
443           gdk_draw_line (drawable, fg_gc,
444                          x + (x_off + ink_rect.x) / PANGO_SCALE - 1,
445                          risen_y + 3,
446                          x + (x_off + ink_rect.x + ink_rect.width) / PANGO_SCALE,
447                          risen_y + 3);
448           /* Fall through */
449         case PANGO_UNDERLINE_SINGLE:
450           g_assert (need_ink);
451           gdk_draw_line (drawable, fg_gc,
452                          x + (x_off + ink_rect.x) / PANGO_SCALE - 1,
453                          risen_y + 1,
454                          x + (x_off + ink_rect.x + ink_rect.width) / PANGO_SCALE,
455                          risen_y + 1);
456           break;
457         case PANGO_UNDERLINE_LOW:
458           g_assert (need_ink);
459           gdk_draw_line (drawable, fg_gc,
460                          x + (x_off + ink_rect.x) / PANGO_SCALE - 1,
461                          risen_y + (ink_rect.y + ink_rect.height) / PANGO_SCALE + 1,
462                          x + (x_off + ink_rect.x + ink_rect.width) / PANGO_SCALE,
463                          risen_y + (ink_rect.y + ink_rect.height) / PANGO_SCALE + 1);
464           break;
465         }
466
467       if (appearance->strikethrough)
468         {          
469           gint strikethrough_y = risen_y + (0.3 * logical_rect.y) / PANGO_SCALE;
470
471           g_assert (need_ink);
472           
473           gdk_draw_line (drawable, fg_gc,
474                          x + (x_off + ink_rect.x) / PANGO_SCALE - 1, strikethrough_y,
475                          x + (x_off + ink_rect.x + ink_rect.width) / PANGO_SCALE, strikethrough_y);
476         }
477
478       if (appearance->is_text)
479         x_off += logical_rect.width;
480       else
481         x_off += shaped_width_pixels * PANGO_SCALE;
482     }
483 }
484
485 static void
486 render_para (GdkDrawable        *drawable,
487              GtkTextRenderState *render_state,
488              GtkTextLineDisplay *line_display,
489              /* Top-left corner of paragraph including all margins */
490              int                 x,
491              int                 y,
492              int                 selection_start_index,
493              int                 selection_end_index,
494              GList             **widgets)
495 {
496   GSList *shaped_pointer = line_display->shaped_objects;
497   PangoLayout *layout = line_display->layout;
498   int byte_offset = 0;
499   PangoLayoutIter *iter;
500   PangoRectangle layout_logical;
501   int screen_width;
502   GdkGC *fg_gc, *bg_gc;
503   gint state;
504   
505   gboolean first = TRUE;
506
507   iter = pango_layout_get_iter (layout);
508
509   pango_layout_iter_get_layout_extents (iter, NULL, &layout_logical);
510
511   /* Adjust for margins */
512   
513   layout_logical.x += line_display->x_offset * PANGO_SCALE;
514   layout_logical.y += line_display->top_margin * PANGO_SCALE;
515
516   screen_width = line_display->total_width;
517   
518   if (GTK_WIDGET_HAS_FOCUS (render_state->widget))
519     state = GTK_STATE_SELECTED;
520   else
521     state = GTK_STATE_ACTIVE;
522
523   fg_gc = render_state->widget->style->text_gc [state];
524   bg_gc = render_state->widget->style->base_gc [state];
525
526   do
527     {
528       PangoLayoutLine *line = pango_layout_iter_get_line (iter);
529       int selection_y, selection_height;
530       int first_y, last_y;
531       PangoRectangle line_rect;
532       int baseline;
533       
534       pango_layout_iter_get_line_extents (iter, NULL, &line_rect);
535       baseline = pango_layout_iter_get_baseline (iter);
536       pango_layout_iter_get_line_yrange (iter, &first_y, &last_y);
537       
538       /* Adjust for margins */
539
540       line_rect.x += line_display->x_offset * PANGO_SCALE;
541       line_rect.y += line_display->top_margin * PANGO_SCALE;
542       baseline += line_display->top_margin * PANGO_SCALE;
543
544       /* Selection is the height of the line, plus top/bottom
545        * margin if we're the first/last line
546        */
547       selection_y = y + PANGO_PIXELS (first_y) + line_display->top_margin;
548       selection_height = PANGO_PIXELS (last_y) - PANGO_PIXELS (first_y);
549
550       if (first)
551         {
552           selection_y -= line_display->top_margin;
553           selection_height += line_display->top_margin;
554         }
555       
556       if (pango_layout_iter_at_last_line (iter))
557         selection_height += line_display->bottom_margin;
558       
559       first = FALSE;
560
561       if (selection_start_index < byte_offset &&
562           selection_end_index > line->length + byte_offset) /* All selected */
563         {
564           gdk_draw_rectangle (drawable,
565                               bg_gc,
566                               TRUE,
567                               x + line_display->left_margin,
568                               selection_y,
569                               screen_width,
570                               selection_height);
571
572           render_layout_line (drawable, render_state, line, &shaped_pointer,
573                               x + PANGO_PIXELS (line_rect.x),
574                               y + PANGO_PIXELS (baseline),
575                               TRUE,
576                               widgets);
577         }
578       else
579         {
580           GSList *shaped_pointer_tmp = shaped_pointer;
581
582           render_layout_line (drawable, render_state,
583                               line, &shaped_pointer,
584                               x + PANGO_PIXELS (line_rect.x),
585                               y + PANGO_PIXELS (baseline),
586                               FALSE,
587                               widgets);
588
589           if (selection_start_index <= byte_offset + line->length &&
590               selection_end_index > byte_offset) /* Some selected */
591             {
592               GdkRegion *clip_region = get_selected_clip (render_state, layout, line,
593                                                           x + line_display->x_offset,
594                                                           selection_y,
595                                                           selection_height,
596                                                           selection_start_index, selection_end_index);
597               gdk_gc_set_clip_region (fg_gc, clip_region);
598               gdk_gc_set_clip_region (bg_gc, clip_region);
599
600               gdk_draw_rectangle (drawable,
601                                   bg_gc,
602                                   TRUE,
603                                   x + PANGO_PIXELS (line_rect.x),
604                                   selection_y,
605                                   PANGO_PIXELS (line_rect.width),
606                                   selection_height);
607
608               render_layout_line (drawable, render_state, line, &shaped_pointer_tmp,
609                                   x + PANGO_PIXELS (line_rect.x),
610                                   y + PANGO_PIXELS (baseline),
611                                   TRUE,
612                                   widgets);
613
614               gdk_gc_set_clip_region (fg_gc, NULL);
615               gdk_gc_set_clip_region (bg_gc, NULL);
616
617               gdk_region_destroy (clip_region);
618
619               /* Paint in the ends of the line */
620               if (line_rect.x > line_display->left_margin * PANGO_SCALE &&
621                   ((line_display->direction == GTK_TEXT_DIR_LTR && selection_start_index < byte_offset) ||
622                    (line_display->direction == GTK_TEXT_DIR_RTL && selection_end_index > byte_offset + line->length)))
623                 {
624                   gdk_draw_rectangle (drawable,
625                                       bg_gc,
626                                       TRUE,
627                                       x + line_display->left_margin,
628                                       selection_y,
629                                       PANGO_PIXELS (line_rect.x) - line_display->left_margin,
630                                       selection_height);
631                 }
632
633               if (line_rect.x + line_rect.width <
634                   (screen_width + line_display->left_margin) * PANGO_SCALE &&
635                   ((line_display->direction == GTK_TEXT_DIR_LTR && selection_end_index > byte_offset + line->length) ||
636                    (line_display->direction == GTK_TEXT_DIR_RTL && selection_start_index < byte_offset)))
637                 {
638                   int nonlayout_width;
639
640                   nonlayout_width =
641                     line_display->left_margin + screen_width -
642                     PANGO_PIXELS (line_rect.x) - PANGO_PIXELS (line_rect.width);
643
644                   gdk_draw_rectangle (drawable,
645                                       bg_gc,
646                                       TRUE,
647                                       x + PANGO_PIXELS (line_rect.x) + PANGO_PIXELS (line_rect.width),
648                                       selection_y,
649                                       nonlayout_width,
650                                       selection_height);
651                 }
652             }
653         }
654
655       byte_offset += line->length;
656     }
657   while (pango_layout_iter_next_line (iter));
658
659   pango_layout_iter_free (iter);
660 }
661
662 static GdkRegion *
663 get_selected_clip (GtkTextRenderState *render_state,
664                    PangoLayout        *layout,
665                    PangoLayoutLine    *line,
666                    int                 x,
667                    int                 y,
668                    int                 height,
669                    int                 start_index,
670                    int                 end_index)
671 {
672   gint *ranges;
673   gint n_ranges, i;
674   GdkRegion *clip_region = gdk_region_new ();
675   GdkRegion *tmp_region;
676
677   pango_layout_line_get_x_ranges (line, start_index, end_index, &ranges, &n_ranges);
678
679   for (i=0; i < n_ranges; i++)
680     {
681       GdkRectangle rect;
682
683       rect.x = x + PANGO_PIXELS (ranges[2*i]);
684       rect.y = y;
685       rect.width = PANGO_PIXELS (ranges[2*i + 1]) - PANGO_PIXELS (ranges[2*i]);
686       rect.height = height;
687       
688       gdk_region_union_with_rect (clip_region, &rect);
689     }
690
691   tmp_region = gdk_region_rectangle (&render_state->clip_rect);
692   gdk_region_intersect (clip_region, tmp_region);
693   gdk_region_destroy (tmp_region);
694
695   g_free (ranges);
696   return clip_region;
697 }
698
699 static void
700 get_item_properties (PangoItem          *item,
701                      GtkTextAppearance **appearance)
702 {
703   GSList *tmp_list = item->analysis.extra_attrs;
704
705   *appearance = NULL;
706
707   while (tmp_list)
708     {
709       PangoAttribute *attr = tmp_list->data;
710
711       if (attr->klass->type == gtk_text_attr_appearance_type)
712         {
713           *appearance = &((GtkTextAttrAppearance *)attr)->appearance;
714         }
715
716       tmp_list = tmp_list->next;
717     }
718 }
719
720 void
721 gtk_text_layout_draw (GtkTextLayout *layout,
722                       GtkWidget *widget,
723                       GdkDrawable *drawable,
724                       GdkGC       *cursor_gc,
725                       /* Location of the drawable
726                          in layout coordinates */
727                       gint x_offset,
728                       gint y_offset,
729                       /* Region of the layout to
730                          render */
731                       gint x,
732                       gint y,
733                       gint width,
734                       gint height,
735                       /* widgets to expose */
736                       GList **widgets)
737 {
738   GdkRectangle clip;
739   gint current_y;
740   GSList *cursor_list;
741   GtkTextRenderState *render_state;
742   GtkTextIter selection_start, selection_end;
743   gboolean have_selection = FALSE;
744   GSList *line_list;
745   GSList *tmp_list;
746   
747   g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
748   g_return_if_fail (layout->default_style != NULL);
749   g_return_if_fail (layout->buffer != NULL);
750   g_return_if_fail (drawable != NULL);
751   g_return_if_fail (width >= 0);
752   g_return_if_fail (height >= 0);
753
754   if (width == 0 || height == 0)
755     return;
756
757   line_list =  gtk_text_layout_get_lines (layout, y + y_offset, y + y_offset + height, &current_y);
758   current_y -= y_offset;
759
760   if (line_list == NULL)
761     return; /* nothing on the screen */
762
763   clip.x = x;
764   clip.y = y;
765   clip.width = width;
766   clip.height = height;
767
768   render_state = gtk_text_render_state_new (widget, drawable, &clip);
769
770   gdk_gc_set_clip_rectangle (render_state->fg_gc, &clip);
771   gdk_gc_set_clip_rectangle (render_state->bg_gc, &clip);
772
773   gtk_text_layout_wrap_loop_start (layout);
774
775   if (gtk_text_buffer_get_selection_bounds (layout->buffer,
776                                             &selection_start,
777                                             &selection_end))
778     have_selection = TRUE;
779
780   tmp_list = line_list;
781   while (tmp_list != NULL)
782     {
783       GtkTextLineDisplay *line_display;
784       gint selection_start_index = -1;
785       gint selection_end_index = -1;
786       gboolean have_strong;
787       gboolean have_weak;
788
789       GtkTextLine *line = tmp_list->data;
790
791       line_display = gtk_text_layout_get_line_display (layout, line, FALSE);
792
793       if (line_display->height > 0)
794         {
795           g_assert (line_display->layout != NULL);
796           
797           if (have_selection)
798             {
799               GtkTextIter line_start, line_end;
800               gint byte_count;
801               
802               gtk_text_layout_get_iter_at_line (layout,
803                                                 &line_start,
804                                                 line, 0);
805               line_end = line_start;
806               gtk_text_iter_forward_to_line_end (&line_end);
807               byte_count = gtk_text_iter_get_line_index (&line_end);     
808
809               if (gtk_text_iter_compare (&selection_start, &line_end) <= 0 &&
810                   gtk_text_iter_compare (&selection_end, &line_start) >= 0)
811                 {
812                   if (gtk_text_iter_compare (&selection_start, &line_start) >= 0)
813                     selection_start_index = gtk_text_iter_get_line_index (&selection_start);
814                   else
815                     selection_start_index = -1;
816
817                   if (gtk_text_iter_compare (&selection_end, &line_end) <= 0)
818                     selection_end_index = gtk_text_iter_get_line_index (&selection_end);
819                   else
820                     selection_end_index = byte_count;
821                 }
822             }
823
824           render_para (drawable, render_state, line_display,
825                        - x_offset,
826                        current_y,
827                        selection_start_index, selection_end_index,
828                        widgets);
829
830           /* We paint the cursors last, because they overlap another chunk
831          and need to appear on top. */
832
833           have_strong = FALSE;
834           have_weak = FALSE;
835           
836           cursor_list = line_display->cursors;
837           while (cursor_list)
838             {
839               GtkTextCursorDisplay *cursor = cursor_list->data;
840               if (cursor->is_strong)
841                 have_strong = TRUE;
842               else
843                 have_weak = TRUE;
844               
845               cursor_list = cursor_list->next;
846             }
847           
848           cursor_list = line_display->cursors;
849           while (cursor_list)
850             {
851               GtkTextCursorDisplay *cursor = cursor_list->data;
852               GtkTextDirection dir;
853               GdkRectangle cursor_location;
854               GdkGC *gc;
855
856               dir = line_display->direction;
857               if (have_strong && have_weak)
858                 {
859                   if (!cursor->is_strong)
860                     dir = (dir == GTK_TEXT_DIR_RTL) ? GTK_TEXT_DIR_LTR : GTK_TEXT_DIR_RTL;
861                 }
862  
863               cursor_location.x = line_display->x_offset + cursor->x - x_offset;
864               cursor_location.y = current_y + line_display->top_margin + cursor->y;
865               cursor_location.width = 0;
866               cursor_location.height = cursor->height;
867  
868               gc = _gtk_get_insertion_cursor_gc (widget, cursor->is_strong);
869               gdk_gc_set_clip_rectangle(gc, &clip);
870               _gtk_draw_insertion_cursor (widget, drawable, gc, &cursor_location,
871                                           dir, have_strong && have_weak);
872               gdk_gc_set_clip_rectangle (gc, NULL);
873
874               g_object_unref (gc);
875
876               cursor_list = cursor_list->next;
877             }
878         } /* line_display->height > 0 */
879           
880       current_y += line_display->height;
881       gtk_text_layout_free_line_display (layout, line_display);
882       render_state->last_appearance = NULL;
883       render_state->last_bg_appearance = NULL;
884       
885       tmp_list = g_slist_next (tmp_list);
886     }
887
888   gtk_text_layout_wrap_loop_end (layout);
889   gtk_text_render_state_destroy (render_state);
890
891   g_slist_free (line_list);
892 }