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