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