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"
53 #include <pango/pangox.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,
166 GSList *tmp_list = line->runs;
167 PangoRectangle overall_rect;
168 PangoRectangle logical_rect;
169 PangoRectangle ink_rect;
173 pango_layout_line_get_extents (line, NULL, &overall_rect);
177 PangoLayoutRun *run = tmp_list->data;
178 GtkTextAppearance *appearance;
180 tmp_list = tmp_list->next;
182 get_item_properties (run->item, &appearance);
184 if (appearance) /* A text segment */
190 fg_gc = render_state->widget->style->fg_gc[GTK_STATE_SELECTED];
194 gtk_text_render_state_update (render_state, appearance);
196 fg_gc = render_state->fg_gc;
199 if (appearance->underline == PANGO_UNDERLINE_NONE && !appearance->overstrike)
200 pango_glyph_string_extents (run->glyphs, run->item->analysis.font,
201 NULL, &logical_rect);
203 pango_glyph_string_extents (run->glyphs, run->item->analysis.font,
204 &ink_rect, &logical_rect);
206 if (appearance->draw_bg && !selected)
207 gdk_draw_rectangle (drawable, render_state->bg_gc, TRUE,
208 x + (x_off + logical_rect.x) / PANGO_SCALE,
209 y + logical_rect.y / PANGO_SCALE,
210 logical_rect.width / PANGO_SCALE,
211 logical_rect.height / PANGO_SCALE);
213 gdk_draw_glyphs (drawable, fg_gc,
214 run->item->analysis.font,
215 x + x_off / PANGO_SCALE, y, run->glyphs);
217 switch (appearance->underline)
219 case PANGO_UNDERLINE_NONE:
221 case PANGO_UNDERLINE_DOUBLE:
222 gdk_draw_line (drawable, fg_gc,
223 x + (x_off + ink_rect.x) / PANGO_SCALE - 1, y + 4,
224 x + (x_off + ink_rect.x + ink_rect.width) / PANGO_SCALE, y + 4);
226 case PANGO_UNDERLINE_SINGLE:
227 gdk_draw_line (drawable, fg_gc,
228 x + (x_off + ink_rect.x) / PANGO_SCALE - 1, y + 2,
229 x + (x_off + ink_rect.x + ink_rect.width) / PANGO_SCALE, y + 2);
231 case PANGO_UNDERLINE_LOW:
232 gdk_draw_line (drawable, fg_gc,
233 x + (x_off + ink_rect.x) / PANGO_SCALE - 1, y + (ink_rect.y + ink_rect.height) / PANGO_SCALE + 2,
234 x + (x_off + ink_rect.x + ink_rect.width) / PANGO_SCALE, y + (ink_rect.y + ink_rect.height) / PANGO_SCALE + 2);
238 if (appearance->overstrike)
240 gint overstrike_y = y + (0.3 * logical_rect.y) / PANGO_SCALE;
241 gdk_draw_line (drawable, fg_gc,
242 x + (x_off + ink_rect.x) / PANGO_SCALE - 1, overstrike_y,
243 x + (x_off + ink_rect.x + ink_rect.width) / PANGO_SCALE, overstrike_y);
246 x_off += logical_rect.width;
252 render_para (GdkDrawable *drawable,
253 GtkTextRenderState *render_state,
254 GtkTextLineDisplay *line_display,
257 int selection_start_index,
258 int selection_end_index)
260 PangoRectangle logical_rect;
262 PangoAlignment align;
263 PangoLayout *layout = line_display->layout;
269 gboolean first = TRUE;
271 indent = pango_layout_get_indent (layout);
272 total_width = pango_layout_get_width (layout);
273 align = pango_layout_get_alignment (layout);
276 total_width = line_display->total_width * PANGO_SCALE;
278 tmp_list = pango_layout_get_lines (layout);
281 PangoLayoutLine *line = tmp_list->data;
283 int selection_y, selection_height;
285 pango_layout_line_get_extents (line, NULL, &logical_rect);
287 x_offset = line_display->left_margin * PANGO_SCALE;
291 case PANGO_ALIGN_RIGHT:
292 x_offset += total_width - logical_rect.width;
294 case PANGO_ALIGN_CENTER:
295 x_offset += (total_width - logical_rect.width) / 2;
305 if (align == PANGO_ALIGN_LEFT)
315 if (align == PANGO_ALIGN_LEFT)
322 selection_y = y + y_offset / PANGO_SCALE;
323 selection_height = logical_rect.height / PANGO_SCALE;
327 selection_y -= line_display->top_margin;
328 selection_height += line_display->top_margin;
331 selection_height += line_display->bottom_margin;
335 if (selection_start_index < byte_offset &&
336 selection_end_index > line->length + byte_offset) /* All selected */
338 gdk_draw_rectangle (drawable,
339 render_state->widget->style->bg_gc[GTK_STATE_SELECTED],
341 x + line_display->left_margin, selection_y,
342 total_width / PANGO_SCALE, selection_height);
343 render_layout_line (drawable, render_state,
344 line, x + x_offset / PANGO_SCALE, y + (y_offset - logical_rect.y) / PANGO_SCALE,
349 render_layout_line (drawable, render_state,
350 line, x + x_offset / PANGO_SCALE, y + (y_offset - logical_rect.y) / PANGO_SCALE,
353 if (selection_start_index < byte_offset + line->length &&
354 selection_end_index > byte_offset) /* Some selected */
356 GdkRegion *clip_region = get_selected_clip (render_state, layout, line,
357 x + line_display->x_offset,
358 selection_y, selection_height,
359 selection_start_index, selection_end_index);
361 gdk_gc_set_clip_region (render_state->widget->style->fg_gc [GTK_STATE_SELECTED], clip_region);
362 gdk_gc_set_clip_region (render_state->widget->style->bg_gc [GTK_STATE_SELECTED], clip_region);
364 gdk_draw_rectangle (drawable,
365 render_state->widget->style->bg_gc[GTK_STATE_SELECTED],
367 x + x_offset / PANGO_SCALE, selection_y,
368 logical_rect.width / PANGO_SCALE,
371 render_layout_line (drawable, render_state,
372 line, x + x_offset / PANGO_SCALE, y + (y_offset - logical_rect.y) / PANGO_SCALE,
375 gdk_gc_set_clip_region (render_state->widget->style->fg_gc [GTK_STATE_SELECTED], NULL);
376 gdk_gc_set_clip_region (render_state->widget->style->bg_gc [GTK_STATE_SELECTED], NULL);
378 gdk_region_destroy (clip_region);
380 /* Paint in the ends of the line */
381 if (x_offset > line_display->left_margin * PANGO_SCALE &&
382 ((line_display->direction == GTK_TEXT_DIR_LTR && selection_start_index < byte_offset) ||
383 (line_display->direction == GTK_TEXT_DIR_RTL && selection_end_index > byte_offset + line->length)))
385 gdk_draw_rectangle (drawable,
386 render_state->widget->style->bg_gc[GTK_STATE_SELECTED],
388 x + line_display->left_margin, selection_y,
389 x + x_offset / PANGO_SCALE - line_display->left_margin,
393 if (x_offset + logical_rect.width < line_display->left_margin * PANGO_SCALE + total_width &&
394 ((line_display->direction == GTK_TEXT_DIR_LTR && selection_end_index > byte_offset + line->length) ||
395 (line_display->direction == GTK_TEXT_DIR_RTL && selection_start_index < byte_offset)))
399 gdk_draw_rectangle (drawable,
400 render_state->widget->style->bg_gc[GTK_STATE_SELECTED],
402 x + (x_offset + logical_rect.width) / PANGO_SCALE,
404 x + (line_display->left_margin * PANGO_SCALE + total_width - x_offset - logical_rect.width) / PANGO_SCALE,
410 byte_offset += line->length;
411 y_offset += logical_rect.height;
412 tmp_list = tmp_list->next;
417 get_selected_clip (GtkTextRenderState *render_state,
419 PangoLayoutLine *line,
428 GdkRegion *clip_region = gdk_region_new ();
429 GdkRegion *tmp_region;
430 PangoRectangle logical_rect;
432 pango_layout_line_get_extents (line, NULL, &logical_rect);
433 pango_layout_line_get_x_ranges (line, start_index, end_index, &ranges, &n_ranges);
435 for (i=0; i < n_ranges; i++)
439 rect.x = x + ranges[2*i] / PANGO_SCALE;
441 rect.width = (ranges[2*i + 1] - ranges[2*i]) / PANGO_SCALE;
442 rect.height = height;
444 gdk_region_union_with_rect (clip_region, &rect);
447 tmp_region = gdk_region_rectangle (&render_state->clip_rect);
448 gdk_region_intersect (clip_region, tmp_region);
449 gdk_region_destroy (tmp_region);
456 get_item_properties (PangoItem *item,
457 GtkTextAppearance **appearance)
459 GSList *tmp_list = item->extra_attrs;
465 PangoAttribute *attr = tmp_list->data;
467 if (attr->klass->type == gtk_text_attr_appearance_type)
469 *appearance = &((GtkTextAttrAppearance *)attr)->appearance;
476 gtk_text_layout_draw (GtkTextLayout *layout,
478 GdkDrawable *drawable,
479 /* Location of the layout
480 in buffer coordinates */
483 /* Region of the layout to
495 GtkTextRenderState *render_state;
496 GtkTextIter selection_start, selection_end;
497 gboolean have_selection = FALSE;
499 g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
500 g_return_if_fail (layout->default_style != NULL);
501 g_return_if_fail (layout->buffer != NULL);
502 g_return_if_fail (drawable != NULL);
503 g_return_if_fail (x_offset >= 0);
504 g_return_if_fail (y_offset >= 0);
505 g_return_if_fail (width >= 0);
506 g_return_if_fail (height >= 0);
508 if (width == 0 || height == 0)
511 line_list = gtk_text_layout_get_lines (layout, y + y_offset, y + y_offset + height, ¤t_y);
512 current_y -= y_offset;
514 if (line_list == NULL)
515 return; /* nothing on the screen */
520 clip.height = height;
522 render_state = gtk_text_render_state_new (widget, drawable, &clip);
524 gdk_gc_set_clip_rectangle (render_state->fg_gc, &clip);
525 gdk_gc_set_clip_rectangle (render_state->bg_gc, &clip);
527 gtk_text_layout_wrap_loop_start (layout);
529 if (gtk_text_buffer_get_selection_bounds (layout->buffer,
532 have_selection = TRUE;
534 tmp_list = line_list;
535 while (tmp_list != NULL)
537 GtkTextLineDisplay *line_display;
538 gint selection_start_index = -1;
539 gint selection_end_index = -1;
541 GtkTextLine *line = tmp_list->data;
543 line_display = gtk_text_layout_get_line_display (layout, line, FALSE);
547 GtkTextIter line_start, line_end;
548 gint byte_count = gtk_text_line_byte_count (line);
550 gtk_text_btree_get_iter_at_line (layout->buffer->tree, &line_start,
552 gtk_text_btree_get_iter_at_line (layout->buffer->tree, &line_end,
553 line, byte_count - 1);
555 if (gtk_text_iter_compare (&selection_start, &line_end) < 0 &&
556 gtk_text_iter_compare (&selection_end, &line_start) > 0)
558 if (gtk_text_iter_compare (&selection_start, &line_start) >= 0)
559 selection_start_index = gtk_text_iter_get_line_byte (&selection_start);
561 selection_start_index = -1;
563 if (gtk_text_iter_compare (&selection_end, &line_end) <= 0)
564 selection_end_index = gtk_text_iter_get_line_byte (&selection_end);
566 selection_end_index = byte_count;
570 render_para (drawable, render_state, line_display,
572 current_y + line_display->top_margin,
573 selection_start_index, selection_end_index);
576 /* We paint the cursors last, because they overlap another chunk
577 and need to appear on top. */
579 cursor_list = line_display->cursors;
582 GtkTextCursorDisplay *cursor = cursor_list->data;
585 if (cursor->is_strong)
586 gc = widget->style->bg_gc[GTK_STATE_SELECTED];
588 gc = widget->style->fg_gc[GTK_STATE_NORMAL];
590 gdk_draw_line (drawable, gc,
591 line_display->x_offset + cursor->x,
592 current_y + line_display->top_margin + cursor->y,
593 line_display->x_offset + cursor->x,
594 current_y + line_display->top_margin + cursor->y + cursor->height);
596 cursor_list = cursor_list->next;
599 current_y += line_display->height;
600 gtk_text_layout_free_line_display (layout, line_display);
602 tmp_list = g_slist_next (tmp_list);
605 gtk_text_layout_wrap_loop_end (layout);
606 gtk_text_render_state_destroy (render_state);
608 g_slist_free (line_list);