1 /* gtktextdisplay.c - display layed-out text
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
8 * This file can be used under your choice of two licenses, the LGPL
9 * and the original Tk license.
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.
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.
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.
27 * Original Tk license:
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.
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.
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.
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.
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.
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/.
77 #include "gtktextdisplay.h"
78 #include "gtktextiterprivate.h"
80 #include <pango/pango.h>
82 typedef struct _GtkTextRenderState GtkTextRenderState;
84 struct _GtkTextRenderState
88 GtkTextAppearance *last_appearance;
89 GtkTextAppearance *last_bg_appearance;
92 GdkRectangle clip_rect;
95 static void get_item_properties (PangoItem *item,
96 GtkTextAppearance **appearance);
97 static GdkRegion *get_selected_clip (GtkTextRenderState *render_state,
99 PangoLayoutLine *line,
106 static GtkTextRenderState *
107 gtk_text_render_state_new (GtkWidget *widget,
108 GdkDrawable *drawable,
109 GdkRectangle *clip_rect)
111 GtkTextRenderState *state = g_new0 (GtkTextRenderState, 1);
113 state->widget = widget;
114 state->fg_gc = gdk_gc_new (drawable);
115 state->bg_gc = gdk_gc_new (drawable);
116 state->clip_rect = *clip_rect;
122 gtk_text_render_state_destroy (GtkTextRenderState *state)
124 gdk_gc_unref (state->fg_gc);
125 gdk_gc_unref (state->bg_gc);
131 gtk_text_render_state_set_color (GtkTextRenderState *state,
135 gdk_colormap_alloc_color (gtk_widget_get_colormap (state->widget), color, FALSE, TRUE);
136 gdk_gc_set_foreground (gc, color);
140 gtk_text_render_state_update (GtkTextRenderState *state,
141 GtkTextAppearance *new_appearance)
143 if (!state->last_appearance ||
144 !gdk_color_equal (&new_appearance->fg_color, &state->last_appearance->fg_color))
145 gtk_text_render_state_set_color (state, state->fg_gc, &new_appearance->fg_color);
147 if (!state->last_appearance ||
148 new_appearance->fg_stipple != state->last_appearance->fg_stipple)
150 if (new_appearance->fg_stipple)
152 gdk_gc_set_fill (state->fg_gc, GDK_STIPPLED);
153 gdk_gc_set_stipple (state->fg_gc, new_appearance->fg_stipple);
157 gdk_gc_set_fill (state->fg_gc, GDK_SOLID);
161 if (new_appearance->draw_bg)
163 if (!state->last_bg_appearance ||
164 !gdk_color_equal (&new_appearance->bg_color, &state->last_bg_appearance->bg_color))
165 gtk_text_render_state_set_color (state, state->bg_gc, &new_appearance->bg_color);
167 if (!state->last_bg_appearance ||
168 new_appearance->bg_stipple != state->last_bg_appearance->bg_stipple)
170 if (new_appearance->bg_stipple)
172 gdk_gc_set_fill (state->bg_gc, GDK_STIPPLED);
173 gdk_gc_set_stipple (state->bg_gc, new_appearance->bg_stipple);
177 gdk_gc_set_fill (state->bg_gc, GDK_SOLID);
181 state->last_bg_appearance = new_appearance;
184 state->last_appearance = new_appearance;
188 get_shape_extents (PangoLayoutRun *run,
189 PangoRectangle *ink_rect,
190 PangoRectangle *logical_rect)
192 GSList *tmp_list = run->item->extra_attrs;
196 PangoAttribute *attr = tmp_list->data;
198 if (attr->klass->type == PANGO_ATTR_SHAPE)
201 *logical_rect = ((PangoAttrShape *)attr)->logical_rect;
204 *ink_rect = ((PangoAttrShape *)attr)->ink_rect;
209 tmp_list = tmp_list->next;
212 g_assert_not_reached ();
216 render_layout_line (GdkDrawable *drawable,
217 GtkTextRenderState *render_state,
218 PangoLayoutLine *line,
219 GSList **shaped_pointer,
224 GSList *tmp_list = line->runs;
225 PangoRectangle overall_rect;
226 PangoRectangle logical_rect;
227 PangoRectangle ink_rect;
231 pango_layout_line_get_extents (line, NULL, &overall_rect);
235 PangoLayoutRun *run = tmp_list->data;
236 GtkTextAppearance *appearance;
238 gint shaped_width_pixels = 0;
239 gboolean need_ink = FALSE;
241 tmp_list = tmp_list->next;
243 get_item_properties (run->item, &appearance);
245 g_assert (appearance != NULL);
247 risen_y = y - PANGO_PIXELS (appearance->rise);
251 fg_gc = render_state->widget->style->fg_gc[GTK_STATE_SELECTED];
255 gtk_text_render_state_update (render_state, appearance);
257 fg_gc = render_state->fg_gc;
260 if (appearance->underline != PANGO_UNDERLINE_NONE ||
261 appearance->strikethrough)
264 if (appearance->is_text)
267 pango_glyph_string_extents (run->glyphs, run->item->analysis.font,
268 &ink_rect, &logical_rect);
270 pango_glyph_string_extents (run->glyphs, run->item->analysis.font,
271 NULL, &logical_rect);
276 get_shape_extents (run, &ink_rect, &logical_rect);
278 get_shape_extents (run, NULL, &logical_rect);
281 if (appearance->draw_bg && !selected)
282 gdk_draw_rectangle (drawable, render_state->bg_gc, TRUE,
283 x + PANGO_PIXELS (x_off) + PANGO_PIXELS (logical_rect.x),
284 risen_y + PANGO_PIXELS (logical_rect.y),
285 PANGO_PIXELS (logical_rect.width),
286 PANGO_PIXELS (logical_rect.height));
288 if (appearance->is_text)
289 gdk_draw_glyphs (drawable, fg_gc,
290 run->item->analysis.font,
291 x + PANGO_PIXELS (x_off),
292 risen_y, run->glyphs);
295 GObject *shaped = (*shaped_pointer)->data;
297 *shaped_pointer = (*shaped_pointer)->next;
299 if (GDK_IS_PIXBUF (shaped))
302 GdkRectangle pixbuf_rect, draw_rect;
305 pixbuf = GDK_PIXBUF (shaped);
307 width = gdk_pixbuf_get_width (pixbuf);
308 height = gdk_pixbuf_get_height (pixbuf);
310 pixbuf_rect.x = x + x_off / PANGO_SCALE;
311 pixbuf_rect.y = risen_y - height;
312 pixbuf_rect.width = width;
313 pixbuf_rect.height = height;
315 if (gdk_rectangle_intersect (&pixbuf_rect, &render_state->clip_rect,
318 GdkBitmap *mask = NULL;
320 if (gdk_pixbuf_get_has_alpha (pixbuf))
322 mask = gdk_pixmap_new (drawable,
323 gdk_pixbuf_get_width (pixbuf),
324 gdk_pixbuf_get_height (pixbuf),
327 gdk_pixbuf_render_threshold_alpha (pixbuf, mask,
329 gdk_pixbuf_get_width (pixbuf),
330 gdk_pixbuf_get_height (pixbuf),
337 gdk_gc_set_clip_mask (render_state->fg_gc, mask);
338 gdk_gc_set_clip_origin (render_state->fg_gc,
339 pixbuf_rect.x, pixbuf_rect.y);
342 gdk_pixbuf_render_to_drawable (pixbuf,
345 draw_rect.x - pixbuf_rect.x,
346 draw_rect.y - pixbuf_rect.y,
347 draw_rect.x, draw_rect.y,
350 GDK_RGB_DITHER_NORMAL,
355 gdk_gc_set_clip_rectangle (render_state->fg_gc,
356 &render_state->clip_rect);
357 g_object_unref (G_OBJECT (mask));
361 shaped_width_pixels = width;
363 else if (GTK_IS_WIDGET (shaped))
366 GdkRectangle draw_rect;
369 widget = GTK_WIDGET (shaped);
371 width = widget->allocation.width;
372 height = widget->allocation.height;
374 printf ("widget allocation at %d,%d %d x %d\n",
375 widget->allocation.x,
376 widget->allocation.y,
377 widget->allocation.width,
378 widget->allocation.height);
380 if (GTK_WIDGET_DRAWABLE (widget) &&
381 gdk_rectangle_intersect (&widget->allocation,
382 &render_state->clip_rect,
386 printf ("drawing widget area %d,%d %d x %d\n",
392 gtk_widget_draw (widget, &draw_rect);
395 shaped_width_pixels = width;
398 g_assert_not_reached (); /* not a pixbuf or widget */
401 switch (appearance->underline)
403 case PANGO_UNDERLINE_NONE:
405 case PANGO_UNDERLINE_DOUBLE:
407 gdk_draw_line (drawable, fg_gc,
408 x + (x_off + ink_rect.x) / PANGO_SCALE - 1,
410 x + (x_off + ink_rect.x + ink_rect.width) / PANGO_SCALE,
413 case PANGO_UNDERLINE_SINGLE:
415 gdk_draw_line (drawable, fg_gc,
416 x + (x_off + ink_rect.x) / PANGO_SCALE - 1,
418 x + (x_off + ink_rect.x + ink_rect.width) / PANGO_SCALE,
421 case PANGO_UNDERLINE_LOW:
423 gdk_draw_line (drawable, fg_gc,
424 x + (x_off + ink_rect.x) / PANGO_SCALE - 1,
425 risen_y + (ink_rect.y + ink_rect.height) / PANGO_SCALE + 1,
426 x + (x_off + ink_rect.x + ink_rect.width) / PANGO_SCALE,
427 risen_y + (ink_rect.y + ink_rect.height) / PANGO_SCALE + 1);
431 if (appearance->strikethrough)
433 gint strikethrough_y = risen_y + (0.3 * logical_rect.y) / PANGO_SCALE;
437 gdk_draw_line (drawable, fg_gc,
438 x + (x_off + ink_rect.x) / PANGO_SCALE - 1, strikethrough_y,
439 x + (x_off + ink_rect.x + ink_rect.width) / PANGO_SCALE, strikethrough_y);
442 if (appearance->is_text)
443 x_off += logical_rect.width;
445 x_off += shaped_width_pixels * PANGO_SCALE;
450 render_para (GdkDrawable *drawable,
451 GtkTextRenderState *render_state,
452 GtkTextLineDisplay *line_display,
453 /* Top-left corner of paragraph including all margins */
456 int selection_start_index,
457 int selection_end_index)
459 GSList *shaped_pointer = line_display->shaped_objects;
460 PangoLayout *layout = line_display->layout;
462 PangoLayoutIter *iter;
463 PangoRectangle layout_logical;
466 gboolean first = TRUE;
468 iter = pango_layout_get_iter (layout);
470 pango_layout_iter_get_layout_extents (iter, NULL, &layout_logical);
472 /* Adjust for margins */
474 layout_logical.x += line_display->x_offset * PANGO_SCALE;
475 layout_logical.y += line_display->top_margin * PANGO_SCALE;
477 screen_width = line_display->total_width;
481 PangoLayoutLine *line = pango_layout_iter_get_line (iter);
482 int selection_y, selection_height;
484 PangoRectangle line_rect;
487 pango_layout_iter_get_line_extents (iter, NULL, &line_rect);
488 baseline = pango_layout_iter_get_baseline (iter);
489 pango_layout_iter_get_line_yrange (iter, &first_y, &last_y);
491 /* Adjust for margins */
493 line_rect.x += line_display->x_offset * PANGO_SCALE;
494 line_rect.y += line_display->top_margin * PANGO_SCALE;
495 baseline += line_display->top_margin * PANGO_SCALE;
497 /* Selection is the height of the line, plus top/bottom
498 * margin if we're the first/last line
500 selection_y = y + PANGO_PIXELS (first_y) + line_display->top_margin;
501 selection_height = PANGO_PIXELS (last_y) - PANGO_PIXELS (first_y);
505 selection_y -= line_display->top_margin;
506 selection_height += line_display->top_margin;
509 if (pango_layout_iter_at_last_line (iter))
510 selection_height += line_display->bottom_margin;
514 if (selection_start_index < byte_offset &&
515 selection_end_index > line->length + byte_offset) /* All selected */
517 gdk_draw_rectangle (drawable,
518 render_state->widget->style->bg_gc[GTK_STATE_SELECTED],
520 x + line_display->left_margin,
525 render_layout_line (drawable, render_state, line, &shaped_pointer,
526 x + PANGO_PIXELS (line_rect.x),
527 y + PANGO_PIXELS (baseline),
532 GSList *shaped_pointer_tmp = shaped_pointer;
534 render_layout_line (drawable, render_state,
535 line, &shaped_pointer,
536 x + PANGO_PIXELS (line_rect.x),
537 y + PANGO_PIXELS (baseline),
540 if (selection_start_index < byte_offset + line->length &&
541 selection_end_index > byte_offset) /* Some selected */
543 GdkRegion *clip_region = get_selected_clip (render_state, layout, line,
544 x + line_display->x_offset,
547 selection_start_index, selection_end_index);
549 gdk_gc_set_clip_region (render_state->widget->style->fg_gc [GTK_STATE_SELECTED], clip_region);
550 gdk_gc_set_clip_region (render_state->widget->style->bg_gc [GTK_STATE_SELECTED], clip_region);
552 gdk_draw_rectangle (drawable,
553 render_state->widget->style->bg_gc[GTK_STATE_SELECTED],
555 x + PANGO_PIXELS (line_rect.x),
557 PANGO_PIXELS (line_rect.width),
560 render_layout_line (drawable, render_state, line, &shaped_pointer_tmp,
561 x + PANGO_PIXELS (line_rect.x),
562 y + PANGO_PIXELS (baseline),
565 gdk_gc_set_clip_region (render_state->widget->style->fg_gc [GTK_STATE_SELECTED], NULL);
566 gdk_gc_set_clip_region (render_state->widget->style->bg_gc [GTK_STATE_SELECTED], NULL);
568 gdk_region_destroy (clip_region);
570 /* Paint in the ends of the line */
571 if (line_rect.x > line_display->left_margin * PANGO_SCALE &&
572 ((line_display->direction == GTK_TEXT_DIR_LTR && selection_start_index < byte_offset) ||
573 (line_display->direction == GTK_TEXT_DIR_RTL && selection_end_index > byte_offset + line->length)))
575 gdk_draw_rectangle (drawable,
576 render_state->widget->style->bg_gc[GTK_STATE_SELECTED],
578 x + line_display->left_margin,
580 PANGO_PIXELS (line_rect.x) - line_display->left_margin,
584 if (line_rect.x + line_rect.width <
585 (screen_width + line_display->left_margin) * PANGO_SCALE &&
586 ((line_display->direction == GTK_TEXT_DIR_LTR && selection_end_index > byte_offset + line->length) ||
587 (line_display->direction == GTK_TEXT_DIR_RTL && selection_start_index < byte_offset)))
592 line_display->left_margin + screen_width -
593 PANGO_PIXELS (line_rect.x) - PANGO_PIXELS (line_rect.width);
595 gdk_draw_rectangle (drawable,
596 render_state->widget->style->bg_gc[GTK_STATE_SELECTED],
598 x + PANGO_PIXELS (line_rect.x) + PANGO_PIXELS (line_rect.width),
606 byte_offset += line->length;
608 while (pango_layout_iter_next_line (iter));
610 pango_layout_iter_free (iter);
614 get_selected_clip (GtkTextRenderState *render_state,
616 PangoLayoutLine *line,
625 GdkRegion *clip_region = gdk_region_new ();
626 GdkRegion *tmp_region;
628 pango_layout_line_get_x_ranges (line, start_index, end_index, &ranges, &n_ranges);
630 for (i=0; i < n_ranges; i++)
634 rect.x = x + PANGO_PIXELS (ranges[2*i]);
636 rect.width = PANGO_PIXELS (ranges[2*i + 1]) - PANGO_PIXELS (ranges[2*i]);
637 rect.height = height;
639 gdk_region_union_with_rect (clip_region, &rect);
642 tmp_region = gdk_region_rectangle (&render_state->clip_rect);
643 gdk_region_intersect (clip_region, tmp_region);
644 gdk_region_destroy (tmp_region);
651 get_item_properties (PangoItem *item,
652 GtkTextAppearance **appearance)
654 GSList *tmp_list = item->extra_attrs;
660 PangoAttribute *attr = tmp_list->data;
662 if (attr->klass->type == gtk_text_attr_appearance_type)
664 *appearance = &((GtkTextAttrAppearance *)attr)->appearance;
667 tmp_list = tmp_list->next;
672 gtk_text_layout_draw (GtkTextLayout *layout,
674 GdkDrawable *drawable,
675 /* Location of the drawable
676 in layout coordinates */
679 /* Region of the layout to
689 GtkTextRenderState *render_state;
690 GtkTextIter selection_start, selection_end;
691 gboolean have_selection = FALSE;
695 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
696 g_return_if_fail (layout->default_style != NULL);
697 g_return_if_fail (layout->buffer != NULL);
698 g_return_if_fail (drawable != NULL);
699 g_return_if_fail (x_offset >= 0);
700 g_return_if_fail (y_offset >= 0);
701 g_return_if_fail (width >= 0);
702 g_return_if_fail (height >= 0);
704 if (width == 0 || height == 0)
707 line_list = gtk_text_layout_get_lines (layout, y + y_offset, y + y_offset + height, ¤t_y);
708 current_y -= y_offset;
710 if (line_list == NULL)
711 return; /* nothing on the screen */
716 clip.height = height;
718 render_state = gtk_text_render_state_new (widget, drawable, &clip);
720 gdk_gc_set_clip_rectangle (render_state->fg_gc, &clip);
721 gdk_gc_set_clip_rectangle (render_state->bg_gc, &clip);
723 gtk_text_layout_wrap_loop_start (layout);
725 if (gtk_text_buffer_get_selection_bounds (layout->buffer,
728 have_selection = TRUE;
730 tmp_list = line_list;
731 while (tmp_list != NULL)
733 GtkTextLineDisplay *line_display;
734 gint selection_start_index = -1;
735 gint selection_end_index = -1;
737 GtkTextLine *line = tmp_list->data;
739 line_display = gtk_text_layout_get_line_display (layout, line, FALSE);
743 GtkTextIter line_start, line_end;
744 gint byte_count = gtk_text_line_byte_count (line);
746 gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
749 gtk_text_btree_get_iter_at_line (_gtk_text_buffer_get_btree (layout->buffer),
751 line, byte_count - 1);
753 if (gtk_text_iter_compare (&selection_start, &line_end) < 0 &&
754 gtk_text_iter_compare (&selection_end, &line_start) > 0)
756 if (gtk_text_iter_compare (&selection_start, &line_start) >= 0)
757 selection_start_index = gtk_text_iter_get_line_index (&selection_start);
759 selection_start_index = -1;
761 if (gtk_text_iter_compare (&selection_end, &line_end) <= 0)
762 selection_end_index = gtk_text_iter_get_line_index (&selection_end);
764 selection_end_index = byte_count;
768 render_para (drawable, render_state, line_display,
771 selection_start_index, selection_end_index);
774 /* We paint the cursors last, because they overlap another chunk
775 and need to appear on top. */
777 cursor_list = line_display->cursors;
780 GtkTextCursorDisplay *cursor = cursor_list->data;
783 if (cursor->is_strong)
784 gc = widget->style->bg_gc[GTK_STATE_SELECTED];
786 gc = widget->style->fg_gc[GTK_STATE_NORMAL];
788 gdk_draw_line (drawable, gc,
789 line_display->x_offset + cursor->x - x_offset,
790 current_y + line_display->top_margin + cursor->y,
791 line_display->x_offset + cursor->x - x_offset,
792 current_y + line_display->top_margin + cursor->y + cursor->height - 1);
794 cursor_list = cursor_list->next;
797 current_y += line_display->height;
798 gtk_text_layout_free_line_display (layout, line_display);
800 tmp_list = g_slist_next (tmp_list);
803 gtk_text_layout_wrap_loop_end (layout);
804 gtk_text_render_state_destroy (render_state);
806 g_slist_free (line_list);