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
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.
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.
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.
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.
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.
51 #include "gtktextdisplay.h"
52 #include "gtktextiterprivate.h"
54 #include <pango/pango.h>
56 typedef struct _GtkTextRenderState GtkTextRenderState;
58 struct _GtkTextRenderState
62 GtkTextAppearance *last_appearance;
65 GdkRectangle clip_rect;
68 static void get_item_properties (PangoItem *item,
69 GtkTextAppearance **appearance);
70 static GdkRegion *get_selected_clip (GtkTextRenderState *render_state,
72 PangoLayoutLine *line,
79 static GtkTextRenderState *
80 gtk_text_render_state_new (GtkWidget *widget,
81 GdkDrawable *drawable,
82 GdkRectangle *clip_rect)
84 GtkTextRenderState *state = g_new0 (GtkTextRenderState, 1);
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;
95 gtk_text_render_state_destroy (GtkTextRenderState *state)
97 gdk_gc_unref (state->fg_gc);
98 gdk_gc_unref (state->bg_gc);
104 gtk_text_render_state_set_color (GtkTextRenderState *state,
108 gdk_colormap_alloc_color (gtk_widget_get_colormap (state->widget), color, FALSE, TRUE);
109 gdk_gc_set_foreground (gc, color);
113 gtk_text_render_state_update (GtkTextRenderState *state,
114 GtkTextAppearance *new_appearance)
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);
120 if (!state->last_appearance ||
121 new_appearance->fg_stipple != state->last_appearance->fg_stipple)
123 if (new_appearance->fg_stipple)
125 gdk_gc_set_fill(state->fg_gc, GDK_STIPPLED);
126 gdk_gc_set_stipple(state->fg_gc, new_appearance->fg_stipple);
130 gdk_gc_set_fill(state->fg_gc, GDK_SOLID);
134 if (new_appearance->draw_bg)
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);
140 if (!state->last_appearance ||
141 new_appearance->bg_stipple != state->last_appearance->bg_stipple)
143 if (new_appearance->bg_stipple)
145 gdk_gc_set_fill(state->bg_gc, GDK_STIPPLED);
146 gdk_gc_set_stipple(state->bg_gc, new_appearance->bg_stipple);
150 gdk_gc_set_fill(state->bg_gc, GDK_SOLID);
155 state->last_appearance = new_appearance;
159 render_layout_line (GdkDrawable *drawable,
160 GtkTextRenderState *render_state,
161 PangoLayoutLine *line,
162 GSList **pixmap_pointer,
167 GSList *tmp_list = line->runs;
168 PangoRectangle overall_rect;
169 PangoRectangle logical_rect;
170 PangoRectangle ink_rect;
174 pango_layout_line_get_extents (line, NULL, &overall_rect);
178 PangoLayoutRun *run = tmp_list->data;
179 GtkTextAppearance *appearance;
181 tmp_list = tmp_list->next;
183 get_item_properties (run->item, &appearance);
185 if (appearance) /* A text segment */
191 fg_gc = render_state->widget->style->fg_gc[GTK_STATE_SELECTED];
195 gtk_text_render_state_update (render_state, appearance);
197 fg_gc = render_state->fg_gc;
200 if (appearance->underline == PANGO_UNDERLINE_NONE && !appearance->overstrike)
201 pango_glyph_string_extents (run->glyphs, run->item->analysis.font,
202 NULL, &logical_rect);
204 pango_glyph_string_extents (run->glyphs, run->item->analysis.font,
205 &ink_rect, &logical_rect);
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);
214 gdk_draw_glyphs (drawable, fg_gc,
215 run->item->analysis.font,
216 x + x_off / PANGO_SCALE, y, run->glyphs);
218 switch (appearance->underline)
220 case PANGO_UNDERLINE_NONE:
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);
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);
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);
239 if (appearance->overstrike)
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);
247 x_off += logical_rect.width;
249 else /* Pixmap segment */
251 GtkTextPixmap *pixmap = (*pixmap_pointer)->data;
253 GdkRectangle pixmap_rect, draw_rect;
255 *pixmap_pointer = (*pixmap_pointer)->next;
257 gdk_drawable_get_size (pixmap->pixmap, &width, &height);
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;
264 gdk_rectangle_intersect (&pixmap_rect, &render_state->clip_rect, &draw_rect);
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);
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);
278 gdk_gc_set_clip_rectangle (render_state->fg_gc, &render_state->clip_rect);
280 x_off += width * PANGO_SCALE;
286 render_para (GdkDrawable *drawable,
287 GtkTextRenderState *render_state,
288 GtkTextLineDisplay *line_display,
291 int selection_start_index,
292 int selection_end_index)
294 PangoRectangle logical_rect;
295 GSList *pixmap_pointer = line_display->pixmaps;
297 PangoAlignment align;
298 PangoLayout *layout = line_display->layout;
304 gboolean first = TRUE;
306 indent = pango_layout_get_indent (layout);
307 total_width = pango_layout_get_width (layout);
308 align = pango_layout_get_alignment (layout);
311 total_width = line_display->total_width * PANGO_SCALE;
313 tmp_list = pango_layout_get_lines (layout);
316 PangoLayoutLine *line = tmp_list->data;
318 int selection_y, selection_height;
320 pango_layout_line_get_extents (line, NULL, &logical_rect);
322 x_offset = line_display->left_margin * PANGO_SCALE;
326 case PANGO_ALIGN_RIGHT:
327 x_offset += total_width - logical_rect.width;
329 case PANGO_ALIGN_CENTER:
330 x_offset += (total_width - logical_rect.width) / 2;
340 if (align == PANGO_ALIGN_LEFT)
350 if (align == PANGO_ALIGN_LEFT)
357 selection_y = y + y_offset / PANGO_SCALE;
358 selection_height = logical_rect.height / PANGO_SCALE;
362 selection_y -= line_display->top_margin;
363 selection_height += line_display->top_margin;
366 selection_height += line_display->bottom_margin;
370 if (selection_start_index < byte_offset &&
371 selection_end_index > line->length + byte_offset) /* All selected */
373 gdk_draw_rectangle (drawable,
374 render_state->widget->style->bg_gc[GTK_STATE_SELECTED],
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,
384 GSList *pixmap_pointer_tmp = pixmap_pointer;
386 render_layout_line (drawable, render_state, line, &pixmap_pointer,
387 x + x_offset / PANGO_SCALE, y + (y_offset - logical_rect.y) / PANGO_SCALE,
390 if (selection_start_index < byte_offset + line->length &&
391 selection_end_index > byte_offset) /* Some selected */
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);
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);
401 gdk_draw_rectangle (drawable,
402 render_state->widget->style->bg_gc[GTK_STATE_SELECTED],
404 x + x_offset / PANGO_SCALE, selection_y,
405 logical_rect.width / PANGO_SCALE,
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,
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);
415 gdk_region_destroy (clip_region);
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)))
422 gdk_draw_rectangle (drawable,
423 render_state->widget->style->bg_gc[GTK_STATE_SELECTED],
425 x + line_display->left_margin, selection_y,
426 x + x_offset / PANGO_SCALE - line_display->left_margin,
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)))
436 gdk_draw_rectangle (drawable,
437 render_state->widget->style->bg_gc[GTK_STATE_SELECTED],
439 x + (x_offset + logical_rect.width) / PANGO_SCALE,
441 x + (line_display->left_margin * PANGO_SCALE + total_width - x_offset - logical_rect.width) / PANGO_SCALE,
447 byte_offset += line->length;
448 y_offset += logical_rect.height;
449 tmp_list = tmp_list->next;
454 get_selected_clip (GtkTextRenderState *render_state,
456 PangoLayoutLine *line,
465 GdkRegion *clip_region = gdk_region_new ();
466 GdkRegion *tmp_region;
467 PangoRectangle logical_rect;
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);
472 for (i=0; i < n_ranges; i++)
476 rect.x = x + ranges[2*i] / PANGO_SCALE;
478 rect.width = (ranges[2*i + 1] - ranges[2*i]) / PANGO_SCALE;
479 rect.height = height;
481 gdk_region_union_with_rect (clip_region, &rect);
484 tmp_region = gdk_region_rectangle (&render_state->clip_rect);
485 gdk_region_intersect (clip_region, tmp_region);
486 gdk_region_destroy (tmp_region);
493 get_item_properties (PangoItem *item,
494 GtkTextAppearance **appearance)
496 GSList *tmp_list = item->extra_attrs;
502 PangoAttribute *attr = tmp_list->data;
504 if (attr->klass->type == gtk_text_attr_appearance_type)
506 *appearance = &((GtkTextAttrAppearance *)attr)->appearance;
509 tmp_list = tmp_list->next;
514 gtk_text_layout_draw (GtkTextLayout *layout,
516 GdkDrawable *drawable,
517 /* Location of the layout
518 in buffer coordinates */
521 /* Region of the layout to
533 GtkTextRenderState *render_state;
534 GtkTextIter selection_start, selection_end;
535 gboolean have_selection = FALSE;
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);
546 if (width == 0 || height == 0)
549 line_list = gtk_text_layout_get_lines (layout, y + y_offset, y + y_offset + height, ¤t_y);
550 current_y -= y_offset;
552 if (line_list == NULL)
553 return; /* nothing on the screen */
558 clip.height = height;
560 render_state = gtk_text_render_state_new (widget, drawable, &clip);
562 gdk_gc_set_clip_rectangle (render_state->fg_gc, &clip);
563 gdk_gc_set_clip_rectangle (render_state->bg_gc, &clip);
565 gtk_text_layout_wrap_loop_start (layout);
567 if (gtk_text_buffer_get_selection_bounds (layout->buffer,
570 have_selection = TRUE;
572 tmp_list = line_list;
573 while (tmp_list != NULL)
575 GtkTextLineDisplay *line_display;
576 gint selection_start_index = -1;
577 gint selection_end_index = -1;
579 GtkTextLine *line = tmp_list->data;
581 line_display = gtk_text_layout_get_line_display (layout, line, FALSE);
585 GtkTextIter line_start, line_end;
586 gint byte_count = gtk_text_line_byte_count (line);
588 gtk_text_btree_get_iter_at_line (layout->buffer->tree, &line_start,
590 gtk_text_btree_get_iter_at_line (layout->buffer->tree, &line_end,
591 line, byte_count - 1);
593 if (gtk_text_iter_compare (&selection_start, &line_end) < 0 &&
594 gtk_text_iter_compare (&selection_end, &line_start) > 0)
596 if (gtk_text_iter_compare (&selection_start, &line_start) >= 0)
597 selection_start_index = gtk_text_iter_get_line_byte (&selection_start);
599 selection_start_index = -1;
601 if (gtk_text_iter_compare (&selection_end, &line_end) <= 0)
602 selection_end_index = gtk_text_iter_get_line_byte (&selection_end);
604 selection_end_index = byte_count;
608 render_para (drawable, render_state, line_display,
610 current_y + line_display->top_margin,
611 selection_start_index, selection_end_index);
614 /* We paint the cursors last, because they overlap another chunk
615 and need to appear on top. */
617 cursor_list = line_display->cursors;
620 GtkTextCursorDisplay *cursor = cursor_list->data;
623 if (cursor->is_strong)
624 gc = widget->style->bg_gc[GTK_STATE_SELECTED];
626 gc = widget->style->fg_gc[GTK_STATE_NORMAL];
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);
634 cursor_list = cursor_list->next;
637 current_y += line_display->height;
638 gtk_text_layout_free_line_display (layout, line_display);
640 tmp_list = g_slist_next (tmp_list);
643 gtk_text_layout_wrap_loop_end (layout);
644 gtk_text_render_state_destroy (render_state);
646 g_slist_free (line_list);