]> Pileus Git - ~andy/gtk/blob - gtk/gtktextdisplay.c
handle NULL shaped object from the empty-child-anchor case by drawing a
[~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           fg_gc = render_state->widget->style->text_gc[GTK_STATE_SELECTED];
254         }
255       else
256         {
257           gtk_text_render_state_update (render_state, appearance);
258           
259           fg_gc = render_state->fg_gc;
260         }
261       
262       if (appearance->underline != PANGO_UNDERLINE_NONE ||
263           appearance->strikethrough)
264         need_ink = TRUE;
265       
266       if (appearance->is_text)
267         {
268           if (need_ink)
269             pango_glyph_string_extents (run->glyphs, run->item->analysis.font,
270                                         &ink_rect, &logical_rect);
271           else
272             pango_glyph_string_extents (run->glyphs, run->item->analysis.font,
273                                         NULL, &logical_rect);
274         }
275       else
276         {
277           if (need_ink)
278             get_shape_extents (run, &ink_rect, &logical_rect);
279           else
280             get_shape_extents (run, NULL, &logical_rect);
281         }
282       
283       if (appearance->draw_bg && !selected)
284         gdk_draw_rectangle (drawable, render_state->bg_gc, TRUE,
285                             x + PANGO_PIXELS (x_off) + PANGO_PIXELS (logical_rect.x),
286                             risen_y + PANGO_PIXELS (logical_rect.y),
287                             PANGO_PIXELS (logical_rect.width),
288                             PANGO_PIXELS (logical_rect.height));
289
290       if (appearance->is_text)
291         gdk_draw_glyphs (drawable, fg_gc,
292                          run->item->analysis.font,
293                          x + PANGO_PIXELS (x_off),
294                          risen_y, run->glyphs);
295       else
296         {
297           GObject *shaped = (*shaped_pointer)->data;
298
299           *shaped_pointer = (*shaped_pointer)->next;
300
301           if (shaped == NULL)
302             {
303               /* This happens if we have an empty widget anchor. Draw
304                * something empty-looking.
305                */
306               GdkRectangle shape_rect, draw_rect;
307
308               shape_rect.x = x + x_off / PANGO_SCALE;
309               shape_rect.y = risen_y - PANGO_PIXELS (logical_rect.height);
310               shape_rect.width = PANGO_PIXELS (logical_rect.width);
311               shape_rect.height = PANGO_PIXELS (logical_rect.height);
312
313               if (gdk_rectangle_intersect (&shape_rect, &render_state->clip_rect,
314                                            &draw_rect))
315                 {
316                   gdk_draw_rectangle (drawable, render_state->fg_gc,
317                                       FALSE, shape_rect.x, shape_rect.y,
318                                       shape_rect.width, shape_rect.height);
319
320                   gdk_draw_line (drawable, render_state->fg_gc,
321                                  shape_rect.x, shape_rect.y,
322                                  shape_rect.x + shape_rect.width,
323                                  shape_rect.y + shape_rect.height);
324
325                   gdk_draw_line (drawable, render_state->fg_gc,
326                                  shape_rect.x + shape_rect.width, shape_rect.y,
327                                  shape_rect.x,
328                                  shape_rect.y + shape_rect.height);
329                 }
330             }
331           else if (GDK_IS_PIXBUF (shaped))
332             {
333               gint width, height;
334               GdkRectangle pixbuf_rect, draw_rect;
335               GdkPixbuf *pixbuf;
336
337               pixbuf = GDK_PIXBUF (shaped);
338               
339               width = gdk_pixbuf_get_width (pixbuf);
340               height = gdk_pixbuf_get_height (pixbuf);
341
342               pixbuf_rect.x = x + x_off / PANGO_SCALE;
343               pixbuf_rect.y = risen_y - height;
344               pixbuf_rect.width = width;
345               pixbuf_rect.height = height;
346
347               if (gdk_rectangle_intersect (&pixbuf_rect, &render_state->clip_rect,
348                                            &draw_rect))
349                 {
350                   GdkBitmap *mask = NULL;
351               
352                   if (gdk_pixbuf_get_has_alpha (pixbuf))
353                     {
354                       mask = gdk_pixmap_new (drawable,
355                                              gdk_pixbuf_get_width (pixbuf),
356                                              gdk_pixbuf_get_height (pixbuf),
357                                              1);
358
359                       gdk_pixbuf_render_threshold_alpha (pixbuf, mask,
360                                                          0, 0, 0, 0,
361                                                          gdk_pixbuf_get_width (pixbuf),
362                                                          gdk_pixbuf_get_height (pixbuf),
363                                                          128);
364
365                     }
366
367                   if (mask)
368                     {
369                       gdk_gc_set_clip_mask (render_state->fg_gc, mask);
370                       gdk_gc_set_clip_origin (render_state->fg_gc,
371                                               pixbuf_rect.x, pixbuf_rect.y);
372                     }
373
374                   gdk_pixbuf_render_to_drawable (pixbuf,
375                                                  drawable,
376                                                  render_state->fg_gc,
377                                                  draw_rect.x - pixbuf_rect.x,
378                                                  draw_rect.y - pixbuf_rect.y,
379                                                  draw_rect.x, draw_rect.y,
380                                                  draw_rect.width,
381                                                  draw_rect.height,
382                                                  GDK_RGB_DITHER_NORMAL,
383                                                  0, 0);
384
385                   if (mask)
386                     {
387                       gdk_gc_set_clip_rectangle (render_state->fg_gc,
388                                                  &render_state->clip_rect);
389                       g_object_unref (G_OBJECT (mask));
390                     }
391                 }
392
393               shaped_width_pixels = width;
394             }
395           else if (GTK_IS_WIDGET (shaped))
396             {
397               gint width, height;
398               GdkRectangle draw_rect;
399               GtkWidget *widget;
400               
401               widget = GTK_WIDGET (shaped);
402               
403               width = widget->allocation.width;
404               height = widget->allocation.height;
405
406               g_print ("widget allocation at %d,%d %d x %d\n",
407                        widget->allocation.x,
408                        widget->allocation.y,
409                        widget->allocation.width,
410                        widget->allocation.height);
411               
412               if (GTK_WIDGET_DRAWABLE (widget) &&
413                   gdk_rectangle_intersect (&widget->allocation,
414                                            &render_state->clip_rect,
415                                            &draw_rect))
416
417                 {
418                   g_print ("drawing widget area %d,%d %d x %d\n",
419                            draw_rect.x,
420                            draw_rect.y,
421                            draw_rect.width,
422                            draw_rect.height);
423
424                   gtk_widget_draw (widget, &draw_rect);
425                 }
426
427               shaped_width_pixels = width;
428             }
429           else
430             g_assert_not_reached (); /* not a pixbuf or widget */
431         }
432
433       switch (appearance->underline)
434         {
435         case PANGO_UNDERLINE_NONE:
436           break;
437         case PANGO_UNDERLINE_DOUBLE:
438           g_assert (need_ink);
439           gdk_draw_line (drawable, fg_gc,
440                          x + (x_off + ink_rect.x) / PANGO_SCALE - 1,
441                          risen_y + 3,
442                          x + (x_off + ink_rect.x + ink_rect.width) / PANGO_SCALE,
443                          risen_y + 3);
444           /* Fall through */
445         case PANGO_UNDERLINE_SINGLE:
446           g_assert (need_ink);
447           gdk_draw_line (drawable, fg_gc,
448                          x + (x_off + ink_rect.x) / PANGO_SCALE - 1,
449                          risen_y + 1,
450                          x + (x_off + ink_rect.x + ink_rect.width) / PANGO_SCALE,
451                          risen_y + 1);
452           break;
453         case PANGO_UNDERLINE_LOW:
454           g_assert (need_ink);
455           gdk_draw_line (drawable, fg_gc,
456                          x + (x_off + ink_rect.x) / PANGO_SCALE - 1,
457                          risen_y + (ink_rect.y + ink_rect.height) / PANGO_SCALE + 1,
458                          x + (x_off + ink_rect.x + ink_rect.width) / PANGO_SCALE,
459                          risen_y + (ink_rect.y + ink_rect.height) / PANGO_SCALE + 1);
460           break;
461         }
462
463       if (appearance->strikethrough)
464         {          
465           gint strikethrough_y = risen_y + (0.3 * logical_rect.y) / PANGO_SCALE;
466
467           g_assert (need_ink);
468           
469           gdk_draw_line (drawable, fg_gc,
470                          x + (x_off + ink_rect.x) / PANGO_SCALE - 1, strikethrough_y,
471                          x + (x_off + ink_rect.x + ink_rect.width) / PANGO_SCALE, strikethrough_y);
472         }
473
474       if (appearance->is_text)
475         x_off += logical_rect.width;
476       else
477         x_off += shaped_width_pixels * PANGO_SCALE;
478     }
479 }
480
481 static void
482 render_para (GdkDrawable        *drawable,
483              GtkTextRenderState *render_state,
484              GtkTextLineDisplay *line_display,
485              /* Top-left corner of paragraph including all margins */
486              int                 x,
487              int                 y,
488              int                 selection_start_index,
489              int                 selection_end_index)
490 {
491   GSList *shaped_pointer = line_display->shaped_objects;
492   PangoLayout *layout = line_display->layout;
493   int byte_offset = 0;
494   PangoLayoutIter *iter;
495   PangoRectangle layout_logical;
496   int screen_width;
497   
498   gboolean first = TRUE;
499
500   iter = pango_layout_get_iter (layout);
501
502   pango_layout_iter_get_layout_extents (iter, NULL, &layout_logical);
503
504   /* Adjust for margins */
505   
506   layout_logical.x += line_display->x_offset * PANGO_SCALE;
507   layout_logical.y += line_display->top_margin * PANGO_SCALE;
508
509   screen_width = line_display->total_width;
510   
511   do
512     {
513       PangoLayoutLine *line = pango_layout_iter_get_line (iter);
514       int selection_y, selection_height;
515       int first_y, last_y;
516       PangoRectangle line_rect;
517       int baseline;
518       
519       pango_layout_iter_get_line_extents (iter, NULL, &line_rect);
520       baseline = pango_layout_iter_get_baseline (iter);
521       pango_layout_iter_get_line_yrange (iter, &first_y, &last_y);
522       
523       /* Adjust for margins */
524
525       line_rect.x += line_display->x_offset * PANGO_SCALE;
526       line_rect.y += line_display->top_margin * PANGO_SCALE;
527       baseline += line_display->top_margin * PANGO_SCALE;
528
529       /* Selection is the height of the line, plus top/bottom
530        * margin if we're the first/last line
531        */
532       selection_y = y + PANGO_PIXELS (first_y) + line_display->top_margin;
533       selection_height = PANGO_PIXELS (last_y) - PANGO_PIXELS (first_y);
534
535       if (first)
536         {
537           selection_y -= line_display->top_margin;
538           selection_height += line_display->top_margin;
539         }
540       
541       if (pango_layout_iter_at_last_line (iter))
542         selection_height += line_display->bottom_margin;
543       
544       first = FALSE;
545
546       if (selection_start_index < byte_offset &&
547           selection_end_index > line->length + byte_offset) /* All selected */
548         {
549           gdk_draw_rectangle (drawable,
550                               render_state->widget->style->base_gc[GTK_STATE_SELECTED],
551                               TRUE,
552                               x + line_display->left_margin,
553                               selection_y,
554                               screen_width,
555                               selection_height);
556
557           render_layout_line (drawable, render_state, line, &shaped_pointer,
558                               x + PANGO_PIXELS (line_rect.x),
559                               y + PANGO_PIXELS (baseline),
560                               TRUE);
561         }
562       else
563         {
564           GSList *shaped_pointer_tmp = shaped_pointer;
565
566           render_layout_line (drawable, render_state,
567                               line, &shaped_pointer,
568                               x + PANGO_PIXELS (line_rect.x),
569                               y + PANGO_PIXELS (baseline),
570                               FALSE);
571
572           if (selection_start_index <= byte_offset + line->length &&
573               selection_end_index > byte_offset) /* Some selected */
574             {
575               GdkRegion *clip_region = get_selected_clip (render_state, layout, line,
576                                                           x + line_display->x_offset,
577                                                           selection_y,
578                                                           selection_height,
579                                                           selection_start_index, selection_end_index);
580
581               gdk_gc_set_clip_region (render_state->widget->style->text_gc [GTK_STATE_SELECTED], clip_region);
582               gdk_gc_set_clip_region (render_state->widget->style->base_gc [GTK_STATE_SELECTED], clip_region);
583
584               gdk_draw_rectangle (drawable,
585                                   render_state->widget->style->base_gc[GTK_STATE_SELECTED],
586                                   TRUE,
587                                   x + PANGO_PIXELS (line_rect.x),
588                                   selection_y,
589                                   PANGO_PIXELS (line_rect.width),
590                                   selection_height);
591
592               render_layout_line (drawable, render_state, line, &shaped_pointer_tmp,
593                                   x + PANGO_PIXELS (line_rect.x),
594                                   y + PANGO_PIXELS (baseline),
595                                   TRUE);
596
597               gdk_gc_set_clip_region (render_state->widget->style->text_gc [GTK_STATE_SELECTED], NULL);
598               gdk_gc_set_clip_region (render_state->widget->style->base_gc [GTK_STATE_SELECTED], NULL);
599
600               gdk_region_destroy (clip_region);
601
602               /* Paint in the ends of the line */
603               if (line_rect.x > line_display->left_margin * PANGO_SCALE &&
604                   ((line_display->direction == GTK_TEXT_DIR_LTR && selection_start_index < byte_offset) ||
605                    (line_display->direction == GTK_TEXT_DIR_RTL && selection_end_index > byte_offset + line->length)))
606                 {
607                   gdk_draw_rectangle (drawable,
608                                       render_state->widget->style->base_gc[GTK_STATE_SELECTED],
609                                       TRUE,
610                                       x + line_display->left_margin,
611                                       selection_y,
612                                       PANGO_PIXELS (line_rect.x) - line_display->left_margin,
613                                       selection_height);
614                 }
615
616               if (line_rect.x + line_rect.width <
617                   (screen_width + line_display->left_margin) * PANGO_SCALE &&
618                   ((line_display->direction == GTK_TEXT_DIR_LTR && selection_end_index > byte_offset + line->length) ||
619                    (line_display->direction == GTK_TEXT_DIR_RTL && selection_start_index < byte_offset)))
620                 {
621                   int nonlayout_width;
622
623                   nonlayout_width =
624                     line_display->left_margin + screen_width -
625                     PANGO_PIXELS (line_rect.x) - PANGO_PIXELS (line_rect.width);
626
627                   gdk_draw_rectangle (drawable,
628                                       render_state->widget->style->base_gc[GTK_STATE_SELECTED],
629                                       TRUE,
630                                       x + PANGO_PIXELS (line_rect.x) + PANGO_PIXELS (line_rect.width),
631                                       selection_y,
632                                       nonlayout_width,
633                                       selection_height);
634                 }
635             }
636         }
637
638       byte_offset += line->length;
639     }
640   while (pango_layout_iter_next_line (iter));
641
642   pango_layout_iter_free (iter);
643 }
644
645 static GdkRegion *
646 get_selected_clip (GtkTextRenderState *render_state,
647                    PangoLayout        *layout,
648                    PangoLayoutLine    *line,
649                    int                 x,
650                    int                 y,
651                    int                 height,
652                    int                 start_index,
653                    int                 end_index)
654 {
655   gint *ranges;
656   gint n_ranges, i;
657   GdkRegion *clip_region = gdk_region_new ();
658   GdkRegion *tmp_region;
659
660   pango_layout_line_get_x_ranges (line, start_index, end_index, &ranges, &n_ranges);
661
662   for (i=0; i < n_ranges; i++)
663     {
664       GdkRectangle rect;
665
666       rect.x = x + PANGO_PIXELS (ranges[2*i]);
667       rect.y = y;
668       rect.width = PANGO_PIXELS (ranges[2*i + 1]) - PANGO_PIXELS (ranges[2*i]);
669       rect.height = height;
670       
671       gdk_region_union_with_rect (clip_region, &rect);
672     }
673
674   tmp_region = gdk_region_rectangle (&render_state->clip_rect);
675   gdk_region_intersect (clip_region, tmp_region);
676   gdk_region_destroy (tmp_region);
677
678   g_free (ranges);
679   return clip_region;
680 }
681
682 static void
683 get_item_properties (PangoItem          *item,
684                      GtkTextAppearance **appearance)
685 {
686   GSList *tmp_list = item->analysis.extra_attrs;
687
688   *appearance = NULL;
689
690   while (tmp_list)
691     {
692       PangoAttribute *attr = tmp_list->data;
693
694       if (attr->klass->type == gtk_text_attr_appearance_type)
695         {
696           *appearance = &((GtkTextAttrAppearance *)attr)->appearance;
697         }
698
699       tmp_list = tmp_list->next;
700     }
701 }
702
703 void
704 gtk_text_layout_draw (GtkTextLayout *layout,
705                       GtkWidget *widget,
706                       GdkDrawable *drawable,
707                       /* Location of the drawable
708                          in layout coordinates */
709                       gint x_offset,
710                       gint y_offset,
711                       /* Region of the layout to
712                          render */
713                       gint x,
714                       gint y,
715                       gint width,
716                       gint height)
717 {
718   GdkRectangle clip;
719   gint current_y;
720   GSList *cursor_list;
721   GtkTextRenderState *render_state;
722   GtkTextIter selection_start, selection_end;
723   gboolean have_selection = FALSE;
724   GSList *line_list;
725   GSList *tmp_list;
726   
727   g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
728   g_return_if_fail (layout->default_style != NULL);
729   g_return_if_fail (layout->buffer != NULL);
730   g_return_if_fail (drawable != NULL);
731   g_return_if_fail (width >= 0);
732   g_return_if_fail (height >= 0);
733
734   if (width == 0 || height == 0)
735     return;
736
737   line_list =  gtk_text_layout_get_lines (layout, y + y_offset, y + y_offset + height, &current_y);
738   current_y -= y_offset;
739
740   if (line_list == NULL)
741     return; /* nothing on the screen */
742
743   clip.x = x;
744   clip.y = y;
745   clip.width = width;
746   clip.height = height;
747
748   render_state = gtk_text_render_state_new (widget, drawable, &clip);
749
750   gdk_gc_set_clip_rectangle (render_state->fg_gc, &clip);
751   gdk_gc_set_clip_rectangle (render_state->bg_gc, &clip);
752
753   gtk_text_layout_wrap_loop_start (layout);
754
755   if (gtk_text_buffer_get_selection_bounds (layout->buffer,
756                                             &selection_start,
757                                             &selection_end))
758     have_selection = TRUE;
759
760   tmp_list = line_list;
761   while (tmp_list != NULL)
762     {
763       GtkTextLineDisplay *line_display;
764       gint selection_start_index = -1;
765       gint selection_end_index = -1;
766
767       GtkTextLine *line = tmp_list->data;
768
769       line_display = gtk_text_layout_get_line_display (layout, line, FALSE);
770
771       if (line_display->height > 0)
772         {
773           g_assert (line_display->layout != NULL);
774           
775           if (have_selection)
776             {
777               GtkTextIter line_start, line_end;
778               gint byte_count;
779               
780               gtk_text_layout_get_iter_at_line (layout,
781                                                 &line_start,
782                                                 line, 0);
783               byte_count = gtk_text_iter_get_bytes_in_line (&line_start);
784           
785               /* FIXME the -1 assumes a newline I think */
786               gtk_text_layout_get_iter_at_line (layout,
787                                                 &line_end,
788                                                 line, byte_count - 1);
789
790               if (gtk_text_iter_compare (&selection_start, &line_end) <= 0 &&
791                   gtk_text_iter_compare (&selection_end, &line_start) >= 0)
792                 {
793                   if (gtk_text_iter_compare (&selection_start, &line_start) >= 0)
794                     selection_start_index = gtk_text_iter_get_line_index (&selection_start);
795                   else
796                     selection_start_index = -1;
797
798                   if (gtk_text_iter_compare (&selection_end, &line_end) <= 0)
799                     selection_end_index = gtk_text_iter_get_line_index (&selection_end);
800                   else
801                     selection_end_index = byte_count;
802                 }
803             }
804
805           render_para (drawable, render_state, line_display,
806                        - x_offset,
807                        current_y,
808                        selection_start_index, selection_end_index);
809
810
811           /* We paint the cursors last, because they overlap another chunk
812          and need to appear on top. */
813
814           cursor_list = line_display->cursors;
815           while (cursor_list)
816             {
817               GtkTextCursorDisplay *cursor = cursor_list->data;
818               GdkGC *gc;
819
820               if (cursor->is_strong)
821                 gc = widget->style->base_gc[GTK_STATE_SELECTED];
822               else
823                 gc = widget->style->text_gc[GTK_STATE_NORMAL];
824
825               gdk_gc_set_clip_rectangle (gc, &clip);
826               gdk_draw_line (drawable, gc,
827                              line_display->x_offset + cursor->x - x_offset,
828                              current_y + line_display->top_margin + cursor->y,
829                              line_display->x_offset + cursor->x - x_offset,
830                              current_y + line_display->top_margin + cursor->y + cursor->height - 1);
831               gdk_gc_set_clip_rectangle (gc, NULL);
832
833               cursor_list = cursor_list->next;
834             }
835         } /* line_display->height > 0 */
836           
837       current_y += line_display->height;
838       gtk_text_layout_free_line_display (layout, line_display);
839       render_state->last_appearance = NULL;
840       render_state->last_bg_appearance = NULL;
841       
842       tmp_list = g_slist_next (tmp_list);
843     }
844
845   gtk_text_layout_wrap_loop_end (layout);
846   gtk_text_render_state_destroy (render_state);
847
848   g_slist_free (line_list);
849 }