]> Pileus Git - ~andy/gtk/blob - gtk/gtktextdisplay.c
Use new PangoAttrShape to reserve space for pixmaps, add GSList *pixmaps
[~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  *
9  * This software is copyrighted by the Regents of the University of
10  * California, Sun Microsystems, Inc., and other parties.  The
11  * following terms apply to all files associated with the software
12  * unless explicitly disclaimed in individual files.
13  * 
14  * The authors hereby grant permission to use, copy, modify,
15  * distribute, and license this software and its documentation for any
16  * purpose, provided that existing copyright notices are retained in
17  * all copies and that this notice is included verbatim in any
18  * distributions. No written agreement, license, or royalty fee is
19  * required for any of the authorized uses.  Modifications to this
20  * software may be copyrighted by their authors and need not follow
21  * the licensing terms described here, provided that the new terms are
22  * clearly indicated on the first page of each file where they apply.
23  * 
24  * IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY
25  * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
26  * DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION,
27  * OR ANY DERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE BEEN ADVISED
28  * OF THE POSSIBILITY OF SUCH DAMAGE.
29  * 
30  * THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES,
31  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
32  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND
33  * NON-INFRINGEMENT.  THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS,
34  * AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO PROVIDE
35  * MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
36  *
37  * GOVERNMENT USE: If you are acquiring this software on behalf of the
38  * U.S. government, the Government shall have only "Restricted Rights"
39  * in the software and related documentation as defined in the Federal
40  * Acquisition Regulations (FARs) in Clause 52.227.19 (c) (2).  If you
41  * are acquiring the software on behalf of the Department of Defense,
42  * the software shall be classified as "Commercial Computer Software"
43  * and the Government shall have only "Restricted Rights" as defined
44  * in Clause 252.227-7013 (c) (1) of DFARs.  Notwithstanding the
45  * foregoing, the authors grant the U.S. Government and others acting
46  * in its behalf permission to use and distribute the software in
47  * accordance with the terms specified in this license.
48  * 
49  */
50
51 #include "gtktextdisplay.h"
52 #include "gtktextiterprivate.h"
53 #include <pango/pangox.h>
54 #include "x11/gdkx.h"
55
56 typedef struct _GtkTextRenderState GtkTextRenderState;
57
58 struct _GtkTextRenderState
59 {
60   GtkWidget *widget;
61   
62   GtkTextAppearance *last_appearance;
63   GdkGC *fg_gc;
64   GdkGC *bg_gc;
65   GdkRectangle clip_rect;
66 };
67
68 static void       get_item_properties (PangoItem           *item,
69                                        GtkTextAppearance  **appearance);
70 static GdkRegion *get_selected_clip   (GtkTextRenderState  *render_state,
71                                        PangoLayout         *layout,
72                                        PangoLayoutLine     *line,
73                                        int                  x,
74                                        int                  y,
75                                        int                  height,
76                                        int                  start_index,
77                                        int                  end_index);
78
79 static GtkTextRenderState *
80 gtk_text_render_state_new (GtkWidget    *widget,
81                            GdkDrawable  *drawable,
82                            GdkRectangle *clip_rect)
83 {
84   GtkTextRenderState *state = g_new0 (GtkTextRenderState, 1);
85
86   state->widget = widget;
87   state->fg_gc = gdk_gc_new (drawable);
88   state->bg_gc = gdk_gc_new (drawable);
89   state->clip_rect = *clip_rect;
90
91   return state;
92 }
93
94 static void
95 gtk_text_render_state_destroy (GtkTextRenderState *state)
96 {
97   gdk_gc_unref (state->fg_gc);
98   gdk_gc_unref (state->bg_gc);
99   
100   g_free (state);
101 }
102
103 static void
104 gtk_text_render_state_set_color (GtkTextRenderState *state,
105                                  GdkGC              *gc,
106                                  GdkColor           *color)
107 {
108   gdk_colormap_alloc_color (gtk_widget_get_colormap (state->widget), color, FALSE, TRUE);
109   gdk_gc_set_foreground (gc, color);
110 }
111
112 static void
113 gtk_text_render_state_update (GtkTextRenderState *state,
114                               GtkTextAppearance  *new_appearance)
115 {
116   if (!state->last_appearance ||
117       !gdk_color_equal (&new_appearance->fg_color, &state->last_appearance->fg_color))
118     gtk_text_render_state_set_color (state, state->fg_gc, &new_appearance->fg_color);
119   
120   if (!state->last_appearance ||
121       new_appearance->fg_stipple != state->last_appearance->fg_stipple)
122     {
123       if (new_appearance->fg_stipple)
124         {
125           gdk_gc_set_fill(state->fg_gc, GDK_STIPPLED);
126           gdk_gc_set_stipple(state->fg_gc, new_appearance->fg_stipple);
127         }
128       else
129         {
130           gdk_gc_set_fill(state->fg_gc, GDK_SOLID);
131         }
132     }
133   
134   if (new_appearance->draw_bg)
135     {
136       if (!state->last_appearance ||
137           !gdk_color_equal (&new_appearance->bg_color, &state->last_appearance->bg_color))
138         gtk_text_render_state_set_color (state, state->bg_gc, &new_appearance->bg_color);
139   
140       if (!state->last_appearance ||
141           new_appearance->bg_stipple != state->last_appearance->bg_stipple)
142         {
143           if (new_appearance->bg_stipple)
144             {
145               gdk_gc_set_fill(state->bg_gc, GDK_STIPPLED);
146               gdk_gc_set_stipple(state->bg_gc, new_appearance->bg_stipple);
147             }
148           else
149             {
150               gdk_gc_set_fill(state->bg_gc, GDK_SOLID);
151             }
152         }
153     }
154
155   state->last_appearance = new_appearance;
156 }
157
158 static void 
159 render_layout_line (GdkDrawable        *drawable,
160                     GtkTextRenderState *render_state,
161                     PangoLayoutLine    *line,
162                     GSList            **pixmap_pointer,
163                     int                 x, 
164                     int                 y,
165                     gboolean            selected)
166 {
167   GSList *tmp_list = line->runs;
168   PangoRectangle overall_rect;
169   PangoRectangle logical_rect;
170   PangoRectangle ink_rect;
171   
172   gint x_off = 0;
173
174   pango_layout_line_get_extents (line, NULL, &overall_rect);
175
176   while (tmp_list)
177     {
178       PangoLayoutRun *run = tmp_list->data;
179       GtkTextAppearance *appearance;
180       
181       tmp_list = tmp_list->next;
182
183       get_item_properties (run->item, &appearance);
184
185       if (appearance)           /* A text segment */
186         {
187           GdkGC *fg_gc;
188           
189           if (selected)
190             {
191               fg_gc = render_state->widget->style->fg_gc[GTK_STATE_SELECTED];
192             }
193           else
194             {
195               gtk_text_render_state_update (render_state, appearance);
196
197               fg_gc = render_state->fg_gc;
198             }
199           
200           if (appearance->underline == PANGO_UNDERLINE_NONE && !appearance->overstrike)
201             pango_glyph_string_extents (run->glyphs, run->item->analysis.font,
202                                         NULL, &logical_rect);
203           else
204             pango_glyph_string_extents (run->glyphs, run->item->analysis.font,
205                                         &ink_rect, &logical_rect);
206
207           if (appearance->draw_bg && !selected)
208             gdk_draw_rectangle (drawable, render_state->bg_gc, TRUE,
209                                 x + (x_off + logical_rect.x) / PANGO_SCALE,
210                                 y + logical_rect.y / PANGO_SCALE,
211                                 logical_rect.width / PANGO_SCALE,
212                                 logical_rect.height / PANGO_SCALE);
213
214           gdk_draw_glyphs (drawable, fg_gc,
215                            run->item->analysis.font, 
216                            x + x_off / PANGO_SCALE, y, run->glyphs);
217
218           switch (appearance->underline)
219             {
220             case PANGO_UNDERLINE_NONE:
221               break;
222             case PANGO_UNDERLINE_DOUBLE:
223               gdk_draw_line (drawable, fg_gc,
224                          x + (x_off + ink_rect.x) / PANGO_SCALE - 1, y + 4,
225                          x + (x_off + ink_rect.x + ink_rect.width) / PANGO_SCALE, y + 4);
226               /* Fall through */
227             case PANGO_UNDERLINE_SINGLE:
228               gdk_draw_line (drawable, fg_gc,
229                          x + (x_off + ink_rect.x) / PANGO_SCALE - 1, y + 2,
230                          x + (x_off + ink_rect.x + ink_rect.width) / PANGO_SCALE, y + 2);
231               break;
232             case PANGO_UNDERLINE_LOW:
233               gdk_draw_line (drawable, fg_gc,
234                          x + (x_off + ink_rect.x) / PANGO_SCALE - 1, y + (ink_rect.y + ink_rect.height) / PANGO_SCALE + 2,
235                          x + (x_off + ink_rect.x + ink_rect.width) / PANGO_SCALE, y + (ink_rect.y + ink_rect.height) / PANGO_SCALE + 2);
236               break;
237             }
238
239           if (appearance->overstrike)
240             {
241               gint overstrike_y = y + (0.3 * logical_rect.y) / PANGO_SCALE;
242               gdk_draw_line (drawable, fg_gc,
243                              x + (x_off + ink_rect.x) / PANGO_SCALE - 1, overstrike_y,
244                              x + (x_off + ink_rect.x + ink_rect.width) / PANGO_SCALE, overstrike_y);
245             }
246
247           x_off += logical_rect.width;
248         }
249       else                      /* Pixmap segment */
250         {
251           GtkTextPixmap *pixmap = (*pixmap_pointer)->data;
252           gint width, height;
253           GdkRectangle pixmap_rect, draw_rect;
254           
255           *pixmap_pointer = (*pixmap_pointer)->next;
256
257           gdk_drawable_get_size (pixmap->pixmap, &width, &height);
258
259           pixmap_rect.x = x + x_off / PANGO_SCALE;
260           pixmap_rect.y = y - height;
261           pixmap_rect.width = width;
262           pixmap_rect.height = height;
263
264           gdk_rectangle_intersect (&pixmap_rect, &render_state->clip_rect, &draw_rect);
265
266           if (pixmap->mask)
267             {
268               gdk_gc_set_clip_mask (render_state->fg_gc, pixmap->mask);
269               gdk_gc_set_clip_origin (render_state->fg_gc,
270                                       pixmap_rect.x, pixmap_rect.y);
271             }
272           
273           gdk_draw_drawable (drawable, render_state->fg_gc, pixmap->pixmap,
274                              draw_rect.x - pixmap_rect.x, draw_rect.y - pixmap_rect.y,
275                              draw_rect.x, draw_rect.y, draw_rect.width, draw_rect.height);
276
277           if (pixmap->mask)
278             gdk_gc_set_clip_rectangle (render_state->fg_gc, &render_state->clip_rect);
279
280           x_off += width * PANGO_SCALE;
281         }
282     }
283 }
284
285 static void 
286 render_para (GdkDrawable        *drawable,
287              GtkTextRenderState *render_state,
288              GtkTextLineDisplay *line_display,
289              int                 x, 
290              int                 y,
291              int                 selection_start_index,
292              int                 selection_end_index)
293 {
294   PangoRectangle logical_rect;
295   GSList *pixmap_pointer = line_display->pixmaps;
296   GSList *tmp_list;
297   PangoAlignment align;
298   PangoLayout *layout = line_display->layout;
299   int indent;
300   int total_width;
301   int y_offset = 0;
302   int byte_offset = 0; 
303
304   gboolean first = TRUE;
305   
306   indent = pango_layout_get_indent (layout);
307   total_width = pango_layout_get_width (layout);
308   align = pango_layout_get_alignment (layout);
309
310   if (total_width < 0)
311     total_width = line_display->total_width * PANGO_SCALE;
312
313   tmp_list = pango_layout_get_lines (layout);
314   while (tmp_list)
315     {
316       PangoLayoutLine *line = tmp_list->data;
317       int x_offset;
318       int selection_y, selection_height;
319       
320       pango_layout_line_get_extents (line, NULL, &logical_rect);
321
322       x_offset = line_display->left_margin * PANGO_SCALE;
323
324       switch (align)
325         {
326         case PANGO_ALIGN_RIGHT:
327           x_offset += total_width - logical_rect.width;
328           break;
329         case PANGO_ALIGN_CENTER:
330           x_offset += (total_width - logical_rect.width) / 2;
331           break;
332         default:
333           break;
334         }
335
336       if (first)
337         {
338           if (indent > 0)
339             {
340               if (align == PANGO_ALIGN_LEFT)
341                 x_offset += indent;
342               else
343                 x_offset -= indent;
344             }
345         }
346       else
347         {
348           if (indent < 0)
349             {
350               if (align == PANGO_ALIGN_LEFT)
351                 x_offset -= indent;
352               else
353                 x_offset += indent;
354             }
355         }
356
357       selection_y = y + y_offset / PANGO_SCALE;
358       selection_height = logical_rect.height / PANGO_SCALE;
359
360       if (first)
361         {
362           selection_y -= line_display->top_margin;
363           selection_height += line_display->top_margin;
364         }
365       if (!tmp_list->next)
366         selection_height += line_display->bottom_margin;
367
368       first = FALSE;
369       
370       if (selection_start_index < byte_offset &&
371           selection_end_index > line->length + byte_offset) /* All selected */
372         {
373           gdk_draw_rectangle (drawable,
374                               render_state->widget->style->bg_gc[GTK_STATE_SELECTED],
375                               TRUE,
376                               x + line_display->left_margin, selection_y,
377                               total_width / PANGO_SCALE, selection_height);
378           render_layout_line (drawable, render_state, line, &pixmap_pointer,
379                               x + x_offset / PANGO_SCALE, y + (y_offset - logical_rect.y) / PANGO_SCALE,
380                               TRUE);
381         }
382       else
383         {
384           GSList *pixmap_pointer_tmp = pixmap_pointer;
385           
386           render_layout_line (drawable, render_state, line, &pixmap_pointer,
387                               x + x_offset / PANGO_SCALE, y + (y_offset - logical_rect.y) / PANGO_SCALE,
388                               FALSE);
389
390           if (selection_start_index < byte_offset + line->length &&
391               selection_end_index > byte_offset) /* Some selected */
392             {
393               GdkRegion *clip_region = get_selected_clip (render_state, layout, line,
394                                                           x + line_display->x_offset,
395                                                           selection_y, selection_height,
396                                                           selection_start_index, selection_end_index);
397
398               gdk_gc_set_clip_region (render_state->widget->style->fg_gc [GTK_STATE_SELECTED], clip_region);
399               gdk_gc_set_clip_region (render_state->widget->style->bg_gc [GTK_STATE_SELECTED], clip_region);
400
401               gdk_draw_rectangle (drawable,
402                                   render_state->widget->style->bg_gc[GTK_STATE_SELECTED],
403                                   TRUE,
404                                   x + x_offset / PANGO_SCALE, selection_y,
405                                   logical_rect.width / PANGO_SCALE,
406                                   selection_height);
407               
408               render_layout_line (drawable, render_state, line, &pixmap_pointer_tmp,
409                                   x + x_offset / PANGO_SCALE, y + (y_offset - logical_rect.y) / PANGO_SCALE,
410                                   TRUE);
411
412               gdk_gc_set_clip_region (render_state->widget->style->fg_gc [GTK_STATE_SELECTED], NULL);
413               gdk_gc_set_clip_region (render_state->widget->style->bg_gc [GTK_STATE_SELECTED], NULL);
414
415               gdk_region_destroy (clip_region);
416
417               /* Paint in the ends of the line */
418               if (x_offset > line_display->left_margin * PANGO_SCALE &&
419                   ((line_display->direction == GTK_TEXT_DIR_LTR && selection_start_index < byte_offset) ||
420                    (line_display->direction == GTK_TEXT_DIR_RTL && selection_end_index > byte_offset + line->length)))
421                 {
422                   gdk_draw_rectangle (drawable,
423                                       render_state->widget->style->bg_gc[GTK_STATE_SELECTED],
424                                       TRUE,
425                                       x + line_display->left_margin, selection_y,
426                                       x + x_offset / PANGO_SCALE - line_display->left_margin,
427                                       selection_height);
428                 }
429
430               if (x_offset + logical_rect.width < line_display->left_margin * PANGO_SCALE + total_width &&
431                   ((line_display->direction == GTK_TEXT_DIR_LTR && selection_end_index > byte_offset + line->length) ||
432                    (line_display->direction == GTK_TEXT_DIR_RTL && selection_start_index < byte_offset)))
433                 {
434                   
435                   
436                   gdk_draw_rectangle (drawable,
437                                       render_state->widget->style->bg_gc[GTK_STATE_SELECTED],
438                                       TRUE,
439                                       x + (x_offset + logical_rect.width) / PANGO_SCALE,
440                                       selection_y,
441                                       x + (line_display->left_margin * PANGO_SCALE + total_width - x_offset - logical_rect.width) / PANGO_SCALE,
442                                       selection_height);
443                 }
444             }
445         }
446
447       byte_offset += line->length;
448       y_offset += logical_rect.height;
449       tmp_list = tmp_list->next;
450     }
451 }
452
453 static GdkRegion *
454 get_selected_clip (GtkTextRenderState *render_state,
455                    PangoLayout        *layout,
456                    PangoLayoutLine    *line,
457                    int                 x,
458                    int                 y,
459                    int                 height,
460                    int                 start_index,
461                    int                 end_index)
462 {
463   gint *ranges;
464   gint n_ranges, i;
465   GdkRegion *clip_region = gdk_region_new ();
466   GdkRegion *tmp_region;
467   PangoRectangle logical_rect;
468
469   pango_layout_line_get_extents (line, NULL, &logical_rect);
470   pango_layout_line_get_x_ranges (line, start_index, end_index, &ranges, &n_ranges);
471
472   for (i=0; i < n_ranges; i++)
473     {
474       GdkRectangle rect;
475
476       rect.x = x + ranges[2*i] / PANGO_SCALE;
477       rect.y = y;
478       rect.width = (ranges[2*i + 1] - ranges[2*i]) / PANGO_SCALE;
479       rect.height = height;
480               
481       gdk_region_union_with_rect (clip_region, &rect);
482     }
483
484   tmp_region = gdk_region_rectangle (&render_state->clip_rect);
485   gdk_region_intersect (clip_region, tmp_region);
486   gdk_region_destroy (tmp_region);
487
488   g_free (ranges);
489   return clip_region;
490 }
491
492 static void
493 get_item_properties (PangoItem          *item,
494                      GtkTextAppearance **appearance)
495 {
496   GSList *tmp_list = item->extra_attrs;
497   
498   *appearance = NULL;
499   
500   while (tmp_list)
501     {
502       PangoAttribute *attr = tmp_list->data;
503
504       if (attr->klass->type == gtk_text_attr_appearance_type)
505         {
506           *appearance = &((GtkTextAttrAppearance *)attr)->appearance;
507         }
508
509       tmp_list = tmp_list->next;
510     }
511 }
512
513 void
514 gtk_text_layout_draw (GtkTextLayout *layout,
515                       GtkWidget *widget,
516                       GdkDrawable *drawable,
517                       /* Location of the layout
518                          in buffer coordinates */
519                       gint x_offset,
520                       gint y_offset,
521                       /* Region of the layout to
522                          render */
523                       gint x,
524                       gint y,
525                       gint width,
526                       gint height)
527 {
528   GdkRectangle clip;
529   gint current_y;
530   GSList *line_list;
531   GSList *tmp_list;
532   GSList *cursor_list;
533   GtkTextRenderState *render_state;
534   GtkTextIter selection_start, selection_end;
535   gboolean have_selection = FALSE;
536   
537   g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
538   g_return_if_fail (layout->default_style != NULL);
539   g_return_if_fail (layout->buffer != NULL);
540   g_return_if_fail (drawable != NULL);
541   g_return_if_fail (x_offset >= 0);
542   g_return_if_fail (y_offset >= 0);
543   g_return_if_fail (width >= 0);
544   g_return_if_fail (height >= 0);
545
546   if (width == 0 || height == 0)
547     return;
548
549   line_list =  gtk_text_layout_get_lines (layout, y + y_offset, y + y_offset + height, &current_y);
550   current_y -= y_offset;
551
552   if (line_list == NULL)
553     return; /* nothing on the screen */
554
555   clip.x = x;
556   clip.y = y;
557   clip.width = width;
558   clip.height = height;
559
560   render_state = gtk_text_render_state_new (widget, drawable, &clip);
561
562   gdk_gc_set_clip_rectangle (render_state->fg_gc, &clip);
563   gdk_gc_set_clip_rectangle (render_state->bg_gc, &clip);
564
565   gtk_text_layout_wrap_loop_start (layout);
566   
567   if (gtk_text_buffer_get_selection_bounds (layout->buffer,
568                                             &selection_start,
569                                             &selection_end))
570     have_selection = TRUE;
571   
572   tmp_list = line_list;
573   while (tmp_list != NULL)
574     {
575       GtkTextLineDisplay *line_display;
576       gint selection_start_index = -1;
577       gint selection_end_index = -1;
578
579       GtkTextLine *line = tmp_list->data;
580      
581       line_display = gtk_text_layout_get_line_display (layout, line, FALSE);
582
583       if (have_selection)
584         {
585           GtkTextIter line_start, line_end;
586           gint byte_count = gtk_text_line_byte_count (line);
587           
588           gtk_text_btree_get_iter_at_line (layout->buffer->tree, &line_start,
589                                            line, 0);
590           gtk_text_btree_get_iter_at_line (layout->buffer->tree, &line_end,
591                                            line, byte_count - 1);
592
593           if (gtk_text_iter_compare (&selection_start, &line_end) < 0 &&
594               gtk_text_iter_compare (&selection_end, &line_start) > 0)
595             {
596               if (gtk_text_iter_compare (&selection_start, &line_start) >= 0)
597                 selection_start_index = gtk_text_iter_get_line_byte (&selection_start);
598               else
599                 selection_start_index = -1;
600
601               if (gtk_text_iter_compare (&selection_end, &line_end) <= 0)
602                 selection_end_index = gtk_text_iter_get_line_byte (&selection_end);
603               else
604                 selection_end_index = byte_count;
605             }
606         }
607   
608       render_para (drawable, render_state, line_display,
609                    - x_offset,
610                    current_y + line_display->top_margin,
611                    selection_start_index, selection_end_index);
612
613       
614       /* We paint the cursors last, because they overlap another chunk
615          and need to appear on top. */
616
617       cursor_list = line_display->cursors;
618       while (cursor_list)
619         {
620           GtkTextCursorDisplay *cursor = cursor_list->data;
621           GdkGC *gc;
622           
623           if (cursor->is_strong)
624             gc = widget->style->bg_gc[GTK_STATE_SELECTED];
625           else
626             gc = widget->style->fg_gc[GTK_STATE_NORMAL];
627           
628           gdk_draw_line (drawable, gc,
629                          line_display->x_offset + cursor->x,
630                          current_y + line_display->top_margin + cursor->y,
631                          line_display->x_offset + cursor->x,
632                          current_y + line_display->top_margin + cursor->y + cursor->height);
633           
634           cursor_list = cursor_list->next;
635         }
636
637       current_y += line_display->height;
638       gtk_text_layout_free_line_display (layout, line_display);
639       
640       tmp_list = g_slist_next (tmp_list);
641     }
642
643   gtk_text_layout_wrap_loop_end (layout);
644   gtk_text_render_state_destroy (render_state);
645
646   g_slist_free (line_list);
647 }