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