]> Pileus Git - ~andy/gtk/blob - gtk/gtktextdisplay.c
Use gunichar instead of gint when appropriate in the interfaces
[~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                     int                 x, 
163                     int                 y,
164                     gboolean            selected)
165 {
166   GSList *tmp_list = line->runs;
167   PangoRectangle overall_rect;
168   PangoRectangle logical_rect;
169   PangoRectangle ink_rect;
170   
171   gint x_off = 0;
172
173   pango_layout_line_get_extents (line, NULL, &overall_rect);
174
175   while (tmp_list)
176     {
177       PangoLayoutRun *run = tmp_list->data;
178       GtkTextAppearance *appearance;
179       
180       tmp_list = tmp_list->next;
181
182       get_item_properties (run->item, &appearance);
183
184       if (appearance)           /* A text segment */
185         {
186           GdkGC *fg_gc;
187           
188           if (selected)
189             {
190               fg_gc = render_state->widget->style->fg_gc[GTK_STATE_SELECTED];
191             }
192           else
193             {
194               gtk_text_render_state_update (render_state, appearance);
195
196               fg_gc = render_state->fg_gc;
197             }
198           
199           if (appearance->underline == PANGO_UNDERLINE_NONE && !appearance->overstrike)
200             pango_glyph_string_extents (run->glyphs, run->item->analysis.font,
201                                         NULL, &logical_rect);
202           else
203             pango_glyph_string_extents (run->glyphs, run->item->analysis.font,
204                                         &ink_rect, &logical_rect);
205
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);
212
213           gdk_draw_glyphs (drawable, fg_gc,
214                            run->item->analysis.font, 
215                            x + x_off / PANGO_SCALE, y, run->glyphs);
216
217           switch (appearance->underline)
218             {
219             case PANGO_UNDERLINE_NONE:
220               break;
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);
225               /* Fall through */
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);
230               break;
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);
235               break;
236             }
237
238           if (appearance->overstrike)
239             {
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);
244             }
245
246           x_off += logical_rect.width;
247         }
248     }
249 }
250
251 static void 
252 render_para (GdkDrawable        *drawable,
253              GtkTextRenderState *render_state,
254              GtkTextLineDisplay *line_display,
255              int                 x, 
256              int                 y,
257              int                 selection_start_index,
258              int                 selection_end_index)
259 {
260   PangoRectangle logical_rect;
261   GSList *tmp_list;
262   PangoAlignment align;
263   PangoLayout *layout = line_display->layout;
264   int indent;
265   int total_width;
266   int y_offset = 0;
267   int byte_offset = 0; 
268
269   gboolean first = TRUE;
270   
271   indent = pango_layout_get_indent (layout);
272   total_width = pango_layout_get_width (layout);
273   align = pango_layout_get_alignment (layout);
274
275   if (total_width < 0)
276     total_width = line_display->total_width * PANGO_SCALE;
277
278   tmp_list = pango_layout_get_lines (layout);
279   while (tmp_list)
280     {
281       PangoLayoutLine *line = tmp_list->data;
282       int x_offset;
283       int selection_y, selection_height;
284       
285       pango_layout_line_get_extents (line, NULL, &logical_rect);
286
287       x_offset = line_display->left_margin * PANGO_SCALE;
288
289       switch (align)
290         {
291         case PANGO_ALIGN_RIGHT:
292           x_offset += total_width - logical_rect.width;
293           break;
294         case PANGO_ALIGN_CENTER:
295           x_offset += (total_width - logical_rect.width) / 2;
296           break;
297         default:
298           break;
299         }
300
301       if (first)
302         {
303           if (indent > 0)
304             {
305               if (align == PANGO_ALIGN_LEFT)
306                 x_offset += indent;
307               else
308                 x_offset -= indent;
309             }
310         }
311       else
312         {
313           if (indent < 0)
314             {
315               if (align == PANGO_ALIGN_LEFT)
316                 x_offset -= indent;
317               else
318                 x_offset += indent;
319             }
320         }
321
322       selection_y = y + y_offset / PANGO_SCALE;
323       selection_height = logical_rect.height / PANGO_SCALE;
324
325       if (first)
326         {
327           selection_y -= line_display->top_margin;
328           selection_height += line_display->top_margin;
329         }
330       if (!tmp_list->next)
331         selection_height += line_display->bottom_margin;
332
333       first = FALSE;
334       
335       if (selection_start_index < byte_offset &&
336           selection_end_index > line->length + byte_offset) /* All selected */
337         {
338           gdk_draw_rectangle (drawable,
339                               render_state->widget->style->bg_gc[GTK_STATE_SELECTED],
340                               TRUE,
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,
345                               TRUE);
346         }
347       else
348         {
349           render_layout_line (drawable, render_state,
350                               line, x + x_offset / PANGO_SCALE, y + (y_offset - logical_rect.y) / PANGO_SCALE,
351                               FALSE);
352
353           if (selection_start_index < byte_offset + line->length &&
354               selection_end_index > byte_offset) /* Some selected */
355             {
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);
360
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);
363
364               gdk_draw_rectangle (drawable,
365                                   render_state->widget->style->bg_gc[GTK_STATE_SELECTED],
366                                   TRUE,
367                                   x + x_offset / PANGO_SCALE, selection_y,
368                                   logical_rect.width / PANGO_SCALE,
369                                   selection_height);
370               
371               render_layout_line (drawable, render_state,
372                                   line, x + x_offset / PANGO_SCALE, y + (y_offset - logical_rect.y) / PANGO_SCALE,
373                                   TRUE);
374
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);
377
378               gdk_region_destroy (clip_region);
379
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)))
384                 {
385                   gdk_draw_rectangle (drawable,
386                                       render_state->widget->style->bg_gc[GTK_STATE_SELECTED],
387                                       TRUE,
388                                       x + line_display->left_margin, selection_y,
389                                       x + x_offset / PANGO_SCALE - line_display->left_margin,
390                                       selection_height);
391                 }
392
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)))
396                 {
397                   
398                   
399                   gdk_draw_rectangle (drawable,
400                                       render_state->widget->style->bg_gc[GTK_STATE_SELECTED],
401                                       TRUE,
402                                       x + (x_offset + logical_rect.width) / PANGO_SCALE,
403                                       selection_y,
404                                       x + (line_display->left_margin * PANGO_SCALE + total_width - x_offset - logical_rect.width) / PANGO_SCALE,
405                                       selection_height);
406                 }
407             }
408         }
409
410       byte_offset += line->length;
411       y_offset += logical_rect.height;
412       tmp_list = tmp_list->next;
413     }
414 }
415
416 static GdkRegion *
417 get_selected_clip (GtkTextRenderState *render_state,
418                    PangoLayout        *layout,
419                    PangoLayoutLine    *line,
420                    int                 x,
421                    int                 y,
422                    int                 height,
423                    int                 start_index,
424                    int                 end_index)
425 {
426   gint *ranges;
427   gint n_ranges, i;
428   GdkRegion *clip_region = gdk_region_new ();
429   GdkRegion *tmp_region;
430   PangoRectangle logical_rect;
431
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);
434
435   for (i=0; i < n_ranges; i++)
436     {
437       GdkRectangle rect;
438
439       rect.x = x + ranges[2*i] / PANGO_SCALE;
440       rect.y = y;
441       rect.width = (ranges[2*i + 1] - ranges[2*i]) / PANGO_SCALE;
442       rect.height = height;
443               
444       gdk_region_union_with_rect (clip_region, &rect);
445     }
446
447   tmp_region = gdk_region_rectangle (&render_state->clip_rect);
448   gdk_region_intersect (clip_region, tmp_region);
449   gdk_region_destroy (tmp_region);
450
451   g_free (ranges);
452   return clip_region;
453 }
454
455 static void
456 get_item_properties (PangoItem          *item,
457                      GtkTextAppearance **appearance)
458 {
459   GSList *tmp_list = item->extra_attrs;
460   
461   *appearance = NULL;
462   
463   while (tmp_list)
464     {
465       PangoAttribute *attr = tmp_list->data;
466
467       if (attr->klass->type == gtk_text_attr_appearance_type)
468         {
469           *appearance = &((GtkTextAttrAppearance *)attr)->appearance;
470           return;
471         }
472     }
473 }
474
475 void
476 gtk_text_layout_draw (GtkTextLayout *layout,
477                       GtkWidget *widget,
478                       GdkDrawable *drawable,
479                       /* Location of the layout
480                          in buffer coordinates */
481                       gint x_offset,
482                       gint y_offset,
483                       /* Region of the layout to
484                          render */
485                       gint x,
486                       gint y,
487                       gint width,
488                       gint height)
489 {
490   GdkRectangle clip;
491   gint current_y;
492   GSList *line_list;
493   GSList *tmp_list;
494   GSList *cursor_list;
495   GtkTextRenderState *render_state;
496   GtkTextIter selection_start, selection_end;
497   gboolean have_selection = FALSE;
498   
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);
507
508   if (width == 0 || height == 0)
509     return;
510
511   line_list =  gtk_text_layout_get_lines (layout, y + y_offset, y + y_offset + height, &current_y);
512   current_y -= y_offset;
513
514   if (line_list == NULL)
515     return; /* nothing on the screen */
516
517   clip.x = x;
518   clip.y = y;
519   clip.width = width;
520   clip.height = height;
521
522   render_state = gtk_text_render_state_new (widget, drawable, &clip);
523
524   gdk_gc_set_clip_rectangle (render_state->fg_gc, &clip);
525   gdk_gc_set_clip_rectangle (render_state->bg_gc, &clip);
526
527   gtk_text_layout_wrap_loop_start (layout);
528   
529   if (gtk_text_buffer_get_selection_bounds (layout->buffer,
530                                             &selection_start,
531                                             &selection_end))
532     have_selection = TRUE;
533   
534   tmp_list = line_list;
535   while (tmp_list != NULL)
536     {
537       GtkTextLineDisplay *line_display;
538       gint selection_start_index = -1;
539       gint selection_end_index = -1;
540
541       GtkTextLine *line = tmp_list->data;
542      
543       line_display = gtk_text_layout_get_line_display (layout, line, FALSE);
544
545       if (have_selection)
546         {
547           GtkTextIter line_start, line_end;
548           gint byte_count = gtk_text_line_byte_count (line);
549           
550           gtk_text_btree_get_iter_at_line (layout->buffer->tree, &line_start,
551                                            line, 0);
552           gtk_text_btree_get_iter_at_line (layout->buffer->tree, &line_end,
553                                            line, byte_count - 1);
554
555           if (gtk_text_iter_compare (&selection_start, &line_end) < 0 &&
556               gtk_text_iter_compare (&selection_end, &line_start) > 0)
557             {
558               if (gtk_text_iter_compare (&selection_start, &line_start) >= 0)
559                 selection_start_index = gtk_text_iter_get_line_byte (&selection_start);
560               else
561                 selection_start_index = -1;
562
563               if (gtk_text_iter_compare (&selection_end, &line_end) <= 0)
564                 selection_end_index = gtk_text_iter_get_line_byte (&selection_end);
565               else
566                 selection_end_index = byte_count;
567             }
568         }
569   
570       render_para (drawable, render_state, line_display,
571                    - x_offset,
572                    current_y + line_display->top_margin,
573                    selection_start_index, selection_end_index);
574
575       
576       /* We paint the cursors last, because they overlap another chunk
577          and need to appear on top. */
578
579       cursor_list = line_display->cursors;
580       while (cursor_list)
581         {
582           GtkTextCursorDisplay *cursor = cursor_list->data;
583           GdkGC *gc;
584           
585           if (cursor->is_strong)
586             gc = widget->style->bg_gc[GTK_STATE_SELECTED];
587           else
588             gc = widget->style->fg_gc[GTK_STATE_NORMAL];
589           
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);
595           
596           cursor_list = cursor_list->next;
597         }
598
599       current_y += line_display->height;
600       gtk_text_layout_free_line_display (layout, line_display);
601       
602       tmp_list = g_slist_next (tmp_list);
603     }
604
605   gtk_text_layout_wrap_loop_end (layout);
606   gtk_text_render_state_destroy (render_state);
607
608   g_slist_free (line_list);
609 }