]> Pileus Git - ~andy/gtk/blob - gtk/gtktextutil.c
Make GtkTextUtil use GtkStyleContext.
[~andy/gtk] / gtk / gtktextutil.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /*
21  * Modified by the GTK+ Team and others 1997-2001.  See the AUTHORS
22  * file for a list of people on the GTK+ Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
25  */
26
27 #include "config.h"
28
29 #include "gtktextview.h"
30 #include "gtktextutil.h"
31
32 #define GTK_TEXT_USE_INTERNAL_UNSUPPORTED_API
33
34 #include "gtktextdisplay.h"
35 #include "gtktextbuffer.h"
36 #include "gtkmenuitem.h"
37 #include "gtkintl.h"
38
39 #define DRAG_ICON_MAX_WIDTH 250
40 #define DRAG_ICON_MAX_HEIGHT 250
41 #define DRAG_ICON_LAYOUT_BORDER 5
42 #define DRAG_ICON_MAX_LINES 7
43 #define ELLIPSIS_CHARACTER "\xe2\x80\xa6"
44
45 typedef struct _GtkUnicodeMenuEntry GtkUnicodeMenuEntry;
46 typedef struct _GtkTextUtilCallbackInfo GtkTextUtilCallbackInfo;
47
48 struct _GtkUnicodeMenuEntry {
49   const char *label;
50   gunichar ch;
51 };
52
53 struct _GtkTextUtilCallbackInfo
54 {
55   GtkTextUtilCharChosenFunc func;
56   gpointer data;
57 };
58
59 static const GtkUnicodeMenuEntry bidi_menu_entries[] = {
60   { N_("LRM _Left-to-right mark"), 0x200E },
61   { N_("RLM _Right-to-left mark"), 0x200F },
62   { N_("LRE Left-to-right _embedding"), 0x202A },
63   { N_("RLE Right-to-left e_mbedding"), 0x202B },
64   { N_("LRO Left-to-right _override"), 0x202D },
65   { N_("RLO Right-to-left o_verride"), 0x202E },
66   { N_("PDF _Pop directional formatting"), 0x202C },
67   { N_("ZWS _Zero width space"), 0x200B },
68   { N_("ZWJ Zero width _joiner"), 0x200D },
69   { N_("ZWNJ Zero width _non-joiner"), 0x200C }
70 };
71
72 static GtkTextUtilCallbackInfo *
73 callback_info_new (GtkTextUtilCharChosenFunc  func,
74                    gpointer                   data)
75 {
76   GtkTextUtilCallbackInfo *info;
77
78   info = g_slice_new (GtkTextUtilCallbackInfo);
79
80   info->func = func;
81   info->data = data;
82
83   return info;
84 }
85
86 static void
87 callback_info_free (GtkTextUtilCallbackInfo *info)
88 {
89   g_slice_free (GtkTextUtilCallbackInfo, info);
90 }
91
92 static void
93 activate_cb (GtkWidget *menu_item,
94              gpointer   data)
95 {
96   GtkUnicodeMenuEntry *entry;
97   GtkTextUtilCallbackInfo *info = data;
98   char buf[7];
99   
100   entry = g_object_get_data (G_OBJECT (menu_item), "gtk-unicode-menu-entry");
101
102   buf[g_unichar_to_utf8 (entry->ch, buf)] = '\0';
103   
104   (* info->func) (buf, info->data);
105 }
106
107 /*
108  * _gtk_text_util_append_special_char_menuitems
109  * @menushell: a #GtkMenuShell
110  * @callback:  call this when an item is chosen
111  * @data: data for callback
112  * 
113  * Add menuitems for various bidi control characters  to a menu;
114  * the menuitems, when selected, will call the given function
115  * with the chosen character.
116  *
117  * This function is private/internal in GTK 2.0, the functionality may
118  * become public sometime, but it probably needs more thought first.
119  * e.g. maybe there should be a way to just get the list of items,
120  * instead of requiring the menu items to be created.
121  */
122 void
123 _gtk_text_util_append_special_char_menuitems (GtkMenuShell              *menushell,
124                                               GtkTextUtilCharChosenFunc  func,
125                                               gpointer                   data)
126 {
127   int i;
128
129   for (i = 0; i < G_N_ELEMENTS (bidi_menu_entries); i++)
130     {
131       GtkWidget *menuitem;
132       GtkTextUtilCallbackInfo *info;
133
134       info = callback_info_new (func, data);
135
136       menuitem = gtk_menu_item_new_with_mnemonic (_(bidi_menu_entries[i].label));
137       g_object_set_data (G_OBJECT (menuitem), I_("gtk-unicode-menu-entry"),
138                          (gpointer)&bidi_menu_entries[i]);
139
140       g_signal_connect_data (menuitem, "activate",
141                              G_CALLBACK (activate_cb),
142                              info, (GClosureNotify) callback_info_free, 0);
143
144       gtk_widget_show (menuitem);
145       gtk_menu_shell_append (menushell, menuitem);
146     }
147 }
148
149 static void
150 append_n_lines (GString *str, const gchar *text, GSList *lines, gint n_lines)
151 {
152   PangoLayoutLine *line;
153   gint i;
154
155   for (i = 0; i < n_lines; i++)
156     {
157       line = lines->data;
158       g_string_append_len (str, &text[line->start_index], line->length);
159       lines = lines->next;
160     }
161 }
162
163 static void
164 limit_layout_lines (PangoLayout *layout)
165 {
166   const gchar *text;
167   GString     *str;
168   GSList      *lines, *elem;
169   gint         n_lines;
170
171   n_lines = pango_layout_get_line_count (layout);
172   
173   if (n_lines >= DRAG_ICON_MAX_LINES)
174     {
175       text  = pango_layout_get_text (layout);
176       str   = g_string_new (NULL);
177       lines = pango_layout_get_lines_readonly (layout);
178
179       /* get first lines */
180       elem = lines;
181       append_n_lines (str, text, elem,
182                       DRAG_ICON_MAX_LINES / 2);
183
184       g_string_append (str, "\n" ELLIPSIS_CHARACTER "\n");
185
186       /* get last lines */
187       elem = g_slist_nth (lines, n_lines - DRAG_ICON_MAX_LINES / 2);
188       append_n_lines (str, text, elem,
189                       DRAG_ICON_MAX_LINES / 2);
190
191       pango_layout_set_text (layout, str->str, -1);
192       g_string_free (str, TRUE);
193     }
194 }
195
196 /*
197  * _gtk_text_util_create_drag_icon
198  * @widget: #GtkWidget to extract the pango context
199  * @text: a #gchar to render the icon
200  * @len: length of @text, or -1 for NUL-terminated text
201  *
202  * Creates a drag and drop icon from @text.
203  *
204  * Returns: a #cairo_surface_t to use as DND icon
205  */
206 cairo_surface_t *
207 _gtk_text_util_create_drag_icon (GtkWidget *widget, 
208                                  gchar     *text,
209                                  gsize      len)
210 {
211   GtkStyleContext *style_context;
212   GtkStateFlags state;
213   cairo_surface_t *surface;
214   PangoContext *context;
215   PangoLayout  *layout;
216   cairo_t      *cr;
217   gint          pixmap_height, pixmap_width;
218   gint          layout_width, layout_height;
219   GdkRGBA       color;
220
221   g_return_val_if_fail (widget != NULL, NULL);
222   g_return_val_if_fail (text != NULL, NULL);
223
224   context = gtk_widget_get_pango_context (widget);
225   layout  = pango_layout_new (context);
226
227   pango_layout_set_text (layout, text, len);
228   pango_layout_set_wrap (layout, PANGO_WRAP_WORD_CHAR);
229   pango_layout_get_size (layout, &layout_width, &layout_height);
230
231   layout_width = MIN (layout_width, DRAG_ICON_MAX_WIDTH * PANGO_SCALE);
232   pango_layout_set_width (layout, layout_width);
233
234   limit_layout_lines (layout);
235
236   /* get again layout extents, they may have changed */
237   pango_layout_get_size (layout, &layout_width, &layout_height);
238
239   pixmap_width  = layout_width  / PANGO_SCALE + DRAG_ICON_LAYOUT_BORDER * 2;
240   pixmap_height = layout_height / PANGO_SCALE + DRAG_ICON_LAYOUT_BORDER * 2;
241
242   style_context = gtk_widget_get_style_context (widget);
243   state = gtk_widget_get_state_flags (widget);
244
245   surface = gdk_window_create_similar_surface (gtk_widget_get_window (widget),
246                                                CAIRO_CONTENT_COLOR,
247                                                pixmap_width  + 2,
248                                                pixmap_height + 2);
249   cr = cairo_create (surface);
250
251   gtk_style_context_save (context);
252   gtk_style_context_add_class (context, GTK_STYLE_CLASS_VIEW);
253
254   gtk_style_context_get_background_color (style_context, state, &color);
255   gdk_cairo_set_source_rgba (cr, &color);
256   cairo_paint (cr);
257
258   gtk_style_context_get_color (style_context, state, &color);
259   gdk_cairo_set_source_rgba (cr, &color);
260   cairo_move_to (cr, 1 + DRAG_ICON_LAYOUT_BORDER, 1 + DRAG_ICON_LAYOUT_BORDER);
261   pango_cairo_show_layout (cr, layout);
262
263   cairo_set_source_rgb (cr, 0, 0, 0);
264   cairo_rectangle (cr, 0.5, 0.5, pixmap_width + 1, pixmap_height + 1);
265   cairo_set_line_width (cr, 1.0);
266   cairo_stroke (cr);
267
268   cairo_destroy (cr);
269   g_object_unref (layout);
270
271   cairo_surface_set_device_offset (surface, 2, 2);
272
273   gtk_style_context_restore (context);
274
275   return surface;
276 }
277
278 static void
279 gtk_text_view_set_attributes_from_style (GtkTextView        *text_view,
280                                          GtkTextAttributes  *values)
281 {
282   GtkStyleContext *context;
283   GdkRGBA bg_color, fg_color;
284   GtkStateFlags state;
285
286   context = gtk_widget_get_style_context (GTK_WIDGET (text_view));
287   state = gtk_widget_get_state_flags (GTK_WIDGET (text_view));
288
289   gtk_style_context_get_background_color (context, state, &bg_color);
290   gtk_style_context_get_color (context, state, &fg_color);
291
292   values->appearance.bg_color.red = CLAMP (bg_color.red * 65535. + 0.5, 0, 65535);
293   values->appearance.bg_color.green = CLAMP (bg_color.green * 65535. + 0.5, 0, 65535);
294   values->appearance.bg_color.blue = CLAMP (bg_color.blue * 65535. + 0.5, 0, 65535);
295
296   values->appearance.fg_color.red = CLAMP (fg_color.red * 65535. + 0.5, 0, 65535);
297   values->appearance.fg_color.green = CLAMP (fg_color.green * 65535. + 0.5, 0, 65535);
298   values->appearance.fg_color.blue = CLAMP (fg_color.blue * 65535. + 0.5, 0, 65535);
299
300   if (values->font)
301     pango_font_description_free (values->font);
302
303   values->font = pango_font_description_copy (gtk_style_context_get_font (context, state));
304 }
305
306 cairo_surface_t *
307 _gtk_text_util_create_rich_drag_icon (GtkWidget     *widget,
308                                       GtkTextBuffer *buffer,
309                                       GtkTextIter   *start,
310                                       GtkTextIter   *end)
311 {
312   GtkAllocation      allocation;
313   cairo_surface_t   *surface;
314   gint               pixmap_height, pixmap_width;
315   gint               layout_width, layout_height;
316   GtkStyleContext   *context;
317   GtkStateFlags      state;
318   GdkRGBA            color;
319   GtkTextBuffer     *new_buffer;
320   GtkTextLayout     *layout;
321   GtkTextAttributes *style;
322   PangoContext      *ltr_context, *rtl_context;
323   GtkTextIter        iter;
324   cairo_t           *cr;
325
326    g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
327    g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
328    g_return_val_if_fail (start != NULL, NULL);
329    g_return_val_if_fail (end != NULL, NULL);
330
331    context = gtk_widget_get_style_context (widget);
332    state = gtk_widget_get_state_flags (widget);
333
334    new_buffer = gtk_text_buffer_new (gtk_text_buffer_get_tag_table (buffer));
335    gtk_text_buffer_get_start_iter (new_buffer, &iter);
336
337    gtk_text_buffer_insert_range (new_buffer, &iter, start, end);
338
339    gtk_text_buffer_get_start_iter (new_buffer, &iter);
340
341    layout = gtk_text_layout_new ();
342
343    ltr_context = gtk_widget_create_pango_context (widget);
344    pango_context_set_base_dir (ltr_context, PANGO_DIRECTION_LTR);
345    rtl_context = gtk_widget_create_pango_context (widget);
346    pango_context_set_base_dir (rtl_context, PANGO_DIRECTION_RTL);
347
348    gtk_text_layout_set_contexts (layout, ltr_context, rtl_context);
349
350    g_object_unref (ltr_context);
351    g_object_unref (rtl_context);
352
353    style = gtk_text_attributes_new ();
354
355    gtk_widget_get_allocation (widget, &allocation);
356    layout_width = allocation.width;
357
358    if (GTK_IS_TEXT_VIEW (widget))
359      {
360        gtk_text_view_set_attributes_from_style (GTK_TEXT_VIEW (widget), style);
361
362        layout_width = layout_width
363          - gtk_text_view_get_border_window_size (GTK_TEXT_VIEW (widget), GTK_TEXT_WINDOW_LEFT)
364          - gtk_text_view_get_border_window_size (GTK_TEXT_VIEW (widget), GTK_TEXT_WINDOW_RIGHT);
365      }
366
367    style->direction = gtk_widget_get_direction (widget);
368    style->wrap_mode = PANGO_WRAP_WORD_CHAR;
369
370    gtk_text_layout_set_default_style (layout, style);
371    gtk_text_attributes_unref (style);
372
373    gtk_text_layout_set_buffer (layout, new_buffer);
374    gtk_text_layout_set_cursor_visible (layout, FALSE);
375    gtk_text_layout_set_screen_width (layout, layout_width);
376
377    gtk_text_layout_validate (layout, DRAG_ICON_MAX_HEIGHT);
378    gtk_text_layout_get_size (layout, &layout_width, &layout_height);
379
380    layout_width = MIN (layout_width, DRAG_ICON_MAX_WIDTH);
381    layout_height = MIN (layout_height, DRAG_ICON_MAX_HEIGHT);
382
383    pixmap_width  = layout_width + DRAG_ICON_LAYOUT_BORDER * 2;
384    pixmap_height = layout_height + DRAG_ICON_LAYOUT_BORDER * 2;
385
386    surface = gdk_window_create_similar_surface (gtk_widget_get_window (widget),
387                                                 CAIRO_CONTENT_COLOR,
388                                                 pixmap_width  + 2,
389                                                 pixmap_height + 2);
390
391    cr = cairo_create (surface);
392
393    gtk_style_context_save (context);
394    gtk_style_context_add_class (context, GTK_STYLE_CLASS_VIEW);
395
396    gtk_style_context_get_background_color (context, state, &color);
397    gdk_cairo_set_source_rgba (cr, &color);
398    cairo_paint (cr);
399
400    cairo_save (cr);
401
402    cairo_translate (cr, 1 + DRAG_ICON_LAYOUT_BORDER, 1 + DRAG_ICON_LAYOUT_BORDER);
403    gtk_text_layout_draw (layout, widget, cr, NULL);
404
405    cairo_restore (cr);
406
407    cairo_set_source_rgb (cr, 0, 0, 0);
408    cairo_rectangle (cr, 0.5, 0.5, pixmap_width + 1, pixmap_height + 1);
409    cairo_set_line_width (cr, 1.0);
410    cairo_stroke (cr);
411
412    cairo_destroy (cr);
413    g_object_unref (layout);
414    g_object_unref (new_buffer);
415
416    cairo_surface_set_device_offset (surface, 2, 2);
417
418    gtk_style_context_restore (context);
419
420    return surface;
421 }
422
423
424 static gint
425 layout_get_char_width (PangoLayout *layout)
426 {
427   gint width;
428   PangoFontMetrics *metrics;
429   const PangoFontDescription *font_desc;
430   PangoContext *context = pango_layout_get_context (layout);
431
432   font_desc = pango_layout_get_font_description (layout);
433   if (!font_desc)
434     font_desc = pango_context_get_font_description (context);
435
436   metrics = pango_context_get_metrics (context, font_desc, NULL);
437   width = pango_font_metrics_get_approximate_char_width (metrics);
438   pango_font_metrics_unref (metrics);
439
440   return width;
441 }
442
443 /*
444  * _gtk_text_util_get_block_cursor_location
445  * @layout: a #PangoLayout
446  * @index: index at which cursor is located
447  * @pos: cursor location
448  * @at_line_end: whether cursor is drawn at line end, not over some
449  * character
450  *
451  * Returns: whether cursor should actually be drawn as a rectangle.
452  *     It may not be the case if character at index is invisible.
453  */
454 gboolean
455 _gtk_text_util_get_block_cursor_location (PangoLayout    *layout,
456                                           gint            index,
457                                           PangoRectangle *pos,
458                                           gboolean       *at_line_end)
459 {
460   PangoRectangle strong_pos, weak_pos;
461   PangoLayoutLine *layout_line;
462   gboolean rtl;
463   gint line_no;
464   const gchar *text;
465
466   g_return_val_if_fail (layout != NULL, FALSE);
467   g_return_val_if_fail (index >= 0, FALSE);
468   g_return_val_if_fail (pos != NULL, FALSE);
469
470   pango_layout_index_to_pos (layout, index, pos);
471
472   if (pos->width != 0)
473     {
474       /* cursor is at some visible character, good */
475       if (at_line_end)
476         *at_line_end = FALSE;
477       if (pos->width < 0)
478         {
479           pos->x += pos->width;
480           pos->width = -pos->width;
481         }
482       return TRUE;
483     }
484
485   pango_layout_index_to_line_x (layout, index, FALSE, &line_no, NULL);
486   layout_line = pango_layout_get_line_readonly (layout, line_no);
487   g_return_val_if_fail (layout_line != NULL, FALSE);
488
489   text = pango_layout_get_text (layout);
490
491   if (index < layout_line->start_index + layout_line->length)
492     {
493       /* this may be a zero-width character in the middle of the line,
494        * or it could be a character where line is wrapped, we do want
495        * block cursor in latter case */
496       if (g_utf8_next_char (text + index) - text !=
497           layout_line->start_index + layout_line->length)
498         {
499           /* zero-width character in the middle of the line, do not
500            * bother with block cursor */
501           return FALSE;
502         }
503     }
504
505   /* Cursor is at the line end. It may be an empty line, or it could
506    * be on the left or on the right depending on text direction, or it
507    * even could be in the middle of visual layout in bidi text. */
508
509   pango_layout_get_cursor_pos (layout, index, &strong_pos, &weak_pos);
510
511   if (strong_pos.x != weak_pos.x)
512     {
513       /* do not show block cursor in this case, since the character typed
514        * in may or may not appear at the cursor position */
515       return FALSE;
516     }
517
518   /* In case when index points to the end of line, pos->x is always most right
519    * pixel of the layout line, so we need to correct it for RTL text. */
520   if (layout_line->length)
521     {
522       if (layout_line->resolved_dir == PANGO_DIRECTION_RTL)
523         {
524           PangoLayoutIter *iter;
525           PangoRectangle line_rect;
526           gint i;
527           gint left, right;
528           const gchar *p;
529
530           p = g_utf8_prev_char (text + index);
531
532           pango_layout_line_index_to_x (layout_line, p - text, FALSE, &left);
533           pango_layout_line_index_to_x (layout_line, p - text, TRUE, &right);
534           pos->x = MIN (left, right);
535
536           iter = pango_layout_get_iter (layout);
537           for (i = 0; i < line_no; i++)
538             pango_layout_iter_next_line (iter);
539           pango_layout_iter_get_line_extents (iter, NULL, &line_rect);
540           pango_layout_iter_free (iter);
541
542           rtl = TRUE;
543           pos->x += line_rect.x;
544         }
545       else
546         rtl = FALSE;
547     }
548   else
549     {
550       PangoContext *context = pango_layout_get_context (layout);
551       rtl = pango_context_get_base_dir (context) == PANGO_DIRECTION_RTL;
552     }
553
554   pos->width = layout_get_char_width (layout);
555
556   if (rtl)
557     pos->x -= pos->width - 1;
558
559   if (at_line_end)
560     *at_line_end = TRUE;
561
562   return pos->width != 0;
563 }