]> Pileus Git - ~andy/gtk/blob - gtk/gtktextutil.c
Add infrastructure for copy/paste and DND of rich text for GtkTextBuffer.
[~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 #include "gtktextutil.h"
29 #include "gtktextview.h"
30
31 #define GTK_TEXT_USE_INTERNAL_UNSUPPORTED_API
32
33 #include "gtktextdisplay.h"
34 #include "gtktextbuffer.h"
35 #include "gtkmenuitem.h"
36 #include "gtkintl.h"
37 #include "gtkalias.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 void
73 activate_cb (GtkWidget *menu_item,
74              gpointer   data)
75 {
76   GtkUnicodeMenuEntry *entry;
77   GtkTextUtilCallbackInfo *info = data;
78   char buf[7];
79   
80   entry = g_object_get_data (G_OBJECT (menu_item), "gtk-unicode-menu-entry");
81
82   buf[g_unichar_to_utf8 (entry->ch, buf)] = '\0';
83   
84   (* info->func) (buf, info->data);
85 }
86
87 /**
88  * _gtk_text_util_append_special_char_menuitems
89  * @menushell: a #GtkMenuShell
90  * @callback:  call this when an item is chosen
91  * @data: data for callback
92  * 
93  * Add menuitems for various bidi control characters  to a menu;
94  * the menuitems, when selected, will call the given function
95  * with the chosen character.
96  *
97  * This function is private/internal in GTK 2.0, the functionality may
98  * become public sometime, but it probably needs more thought first.
99  * e.g. maybe there should be a way to just get the list of items,
100  * instead of requiring the menu items to be created.
101  **/
102 void
103 _gtk_text_util_append_special_char_menuitems (GtkMenuShell              *menushell,
104                                               GtkTextUtilCharChosenFunc  func,
105                                               gpointer                   data)
106 {
107   int i;
108   
109   for (i = 0; i < G_N_ELEMENTS (bidi_menu_entries); i++)
110     {
111       GtkWidget *menuitem;
112       GtkTextUtilCallbackInfo *info;
113
114       /* wasteful to have a bunch of copies, but simplifies mem management */
115       info = g_new (GtkTextUtilCallbackInfo, 1);
116       info->func = func;
117       info->data = data;
118       
119       menuitem = gtk_menu_item_new_with_mnemonic (_(bidi_menu_entries[i].label));
120       g_object_set_data (G_OBJECT (menuitem), I_("gtk-unicode-menu-entry"),
121                          (gpointer)&bidi_menu_entries[i]);
122       
123       g_signal_connect_data (menuitem, "activate",
124                              G_CALLBACK (activate_cb),
125                              info, (GClosureNotify) g_free, 0);
126       
127       gtk_widget_show (menuitem);
128       gtk_menu_shell_append (menushell, menuitem);
129     }
130 }
131
132 static void
133 append_n_lines (GString *str, const gchar *text, GSList *lines, gint n_lines)
134 {
135   PangoLayoutLine *line;
136   gint i;
137
138   for (i = 0; i < n_lines; i++)
139     {
140       line = lines->data;
141       g_string_append_len (str, &text[line->start_index], line->length);
142       lines = lines->next;
143     }
144 }
145
146 static void
147 limit_layout_lines (PangoLayout *layout)
148 {
149   const gchar *text;
150   GString     *str;
151   GSList      *lines, *elem;
152   gint         n_lines;
153
154   n_lines = pango_layout_get_line_count (layout);
155   
156   if (n_lines >= DRAG_ICON_MAX_LINES)
157     {
158       text  = pango_layout_get_text (layout);
159       str   = g_string_new (NULL);
160       lines = pango_layout_get_lines (layout);
161
162       /* get first lines */
163       elem = lines;
164       append_n_lines (str, text, elem,
165                       DRAG_ICON_MAX_LINES / 2);
166
167       g_string_append (str, "\n" ELLIPSIS_CHARACTER "\n");
168
169       /* get last lines */
170       elem = g_slist_nth (lines, n_lines - DRAG_ICON_MAX_LINES / 2);
171       append_n_lines (str, text, elem,
172                       DRAG_ICON_MAX_LINES / 2);
173
174       pango_layout_set_text (layout, str->str, -1);
175       g_string_free (str, TRUE);
176     }
177 }
178
179 /**
180  * _gtk_text_util_create_drag_icon
181  * @widget: #GtkWidget to extract the pango context
182  * @text: a #gchar to render the icon
183  * @len: length of @text, or -1 for NUL-terminated text
184  *
185  * Creates a drag and drop icon from @text.
186  **/
187 GdkPixmap *
188 _gtk_text_util_create_drag_icon (GtkWidget *widget, 
189                                  gchar     *text,
190                                  gsize      len)
191 {
192   GdkDrawable  *drawable = NULL;
193   PangoContext *context;
194   PangoLayout  *layout;
195   gint          pixmap_height, pixmap_width;
196   gint          layout_width, layout_height;
197
198   g_return_val_if_fail (widget != NULL, NULL);
199   g_return_val_if_fail (text != NULL, NULL);
200
201   context = gtk_widget_get_pango_context (widget);
202   layout  = pango_layout_new (context);
203
204   pango_layout_set_text (layout, text, len);
205   pango_layout_set_wrap (layout, PANGO_WRAP_WORD_CHAR);
206   pango_layout_get_size (layout, &layout_width, &layout_height);
207
208   layout_width = MIN (layout_width, DRAG_ICON_MAX_WIDTH * PANGO_SCALE);
209   pango_layout_set_width (layout, layout_width);
210
211   limit_layout_lines (layout);
212
213   /* get again layout extents, they may have changed */
214   pango_layout_get_size (layout, &layout_width, &layout_height);
215
216   pixmap_width  = layout_width  / PANGO_SCALE + DRAG_ICON_LAYOUT_BORDER * 2;
217   pixmap_height = layout_height / PANGO_SCALE + DRAG_ICON_LAYOUT_BORDER * 2;
218
219   drawable = gdk_pixmap_new (widget->window,
220                              pixmap_width  + 2,
221                              pixmap_height + 2,
222                              -1);
223
224   gdk_draw_rectangle (drawable,
225                       widget->style->base_gc [GTK_WIDGET_STATE (widget)],
226                       TRUE,
227                       0, 0,
228                       pixmap_width + 1,
229                       pixmap_height + 1);
230
231   gdk_draw_layout (drawable,
232                    widget->style->text_gc [GTK_WIDGET_STATE (widget)],
233                    1 + DRAG_ICON_LAYOUT_BORDER,
234                    1 + DRAG_ICON_LAYOUT_BORDER,
235                    layout);
236
237   gdk_draw_rectangle (drawable,
238                       widget->style->black_gc,
239                       FALSE,
240                       0, 0,
241                       pixmap_width + 1,
242                       pixmap_height + 1);
243
244   g_object_unref (layout);
245
246   return drawable;
247 }
248
249 static void
250 gtk_text_view_set_attributes_from_style (GtkTextView        *text_view,
251                                          GtkTextAttributes  *values,
252                                          GtkStyle           *style)
253 {
254   values->appearance.bg_color = style->base[GTK_STATE_NORMAL];
255   values->appearance.fg_color = style->text[GTK_STATE_NORMAL];
256
257   if (values->font)
258     pango_font_description_free (values->font);
259
260   values->font = pango_font_description_copy (style->font_desc);
261 }
262
263 GdkPixmap *
264 _gtk_text_util_create_rich_drag_icon (GtkWidget     *widget,
265                                       GtkTextBuffer *buffer,
266                                       GtkTextIter   *start,
267                                       GtkTextIter   *end)
268 {
269   GdkDrawable       *drawable = NULL;
270   gint               pixmap_height, pixmap_width;
271   gint               layout_width, layout_height;
272   GtkTextBuffer     *new_buffer;
273   GtkTextLayout     *layout;
274   GtkTextAttributes *style;
275   PangoContext      *ltr_context, *rtl_context;
276   GtkTextIter        iter;
277
278    g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
279    g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
280    g_return_val_if_fail (start != NULL, NULL);
281    g_return_val_if_fail (end != NULL, NULL);
282
283    new_buffer = gtk_text_buffer_new (gtk_text_buffer_get_tag_table (buffer));
284    gtk_text_buffer_get_start_iter (new_buffer, &iter);
285
286    gtk_text_buffer_insert_range (new_buffer, &iter, start, end);
287
288    gtk_text_buffer_get_start_iter (new_buffer, &iter);
289
290    layout = gtk_text_layout_new ();
291
292    ltr_context = gtk_widget_create_pango_context (widget);
293    pango_context_set_base_dir (ltr_context, PANGO_DIRECTION_LTR);
294    rtl_context = gtk_widget_create_pango_context (widget);
295    pango_context_set_base_dir (rtl_context, PANGO_DIRECTION_RTL);
296
297    gtk_text_layout_set_contexts (layout, ltr_context, rtl_context);
298
299    g_object_unref (ltr_context);
300    g_object_unref (rtl_context);
301
302    style = gtk_text_attributes_new ();
303
304    layout_width = widget->allocation.width;
305
306    if (GTK_IS_TEXT_VIEW (widget))
307      {
308        gtk_widget_ensure_style (widget);
309        gtk_text_view_set_attributes_from_style (GTK_TEXT_VIEW (widget),
310                                                 style, widget->style);
311
312        layout_width = layout_width
313          - gtk_text_view_get_border_window_size (GTK_TEXT_VIEW (widget), GTK_TEXT_WINDOW_LEFT)
314          - gtk_text_view_get_border_window_size (GTK_TEXT_VIEW (widget), GTK_TEXT_WINDOW_RIGHT);
315      }
316
317    style->direction = gtk_widget_get_direction (widget);
318    style->wrap_mode = PANGO_WRAP_WORD_CHAR;
319
320    gtk_text_layout_set_default_style (layout, style);
321    gtk_text_attributes_unref (style);
322
323    gtk_text_layout_set_buffer (layout, new_buffer);
324    gtk_text_layout_set_cursor_visible (layout, FALSE);
325    gtk_text_layout_set_screen_width (layout, layout_width);
326
327    gtk_text_layout_validate (layout, DRAG_ICON_MAX_HEIGHT);
328    gtk_text_layout_get_size (layout, &layout_width, &layout_height);
329
330    g_print ("%s: layout size %d %d\n", G_STRFUNC, layout_width, layout_height);
331
332    layout_width = MIN (layout_width, DRAG_ICON_MAX_WIDTH);
333    layout_height = MIN (layout_height, DRAG_ICON_MAX_HEIGHT);
334
335    pixmap_width  = layout_width + DRAG_ICON_LAYOUT_BORDER * 2;
336    pixmap_height = layout_height + DRAG_ICON_LAYOUT_BORDER * 2;
337
338    g_print ("%s: pixmap size %d %d\n", G_STRFUNC, pixmap_width, pixmap_height);
339
340    drawable = gdk_pixmap_new (widget->window,
341                               pixmap_width  + 2, pixmap_height + 2, -1);
342
343    gdk_draw_rectangle (drawable,
344                        widget->style->base_gc [GTK_WIDGET_STATE (widget)],
345                        TRUE,
346                        0, 0,
347                        pixmap_width + 1,
348                        pixmap_height + 1);
349
350    gtk_text_layout_draw (layout, widget, drawable,
351                          widget->style->text_gc [GTK_WIDGET_STATE (widget)],
352                          - (1 + DRAG_ICON_LAYOUT_BORDER),
353                          - (1 + DRAG_ICON_LAYOUT_BORDER),
354                          0, 0,
355                          pixmap_width, pixmap_height, NULL);
356
357    gdk_draw_rectangle (drawable,
358                        widget->style->black_gc,
359                        FALSE,
360                        0, 0,
361                        pixmap_width + 1,
362                        pixmap_height + 1);
363
364    g_object_unref (layout);
365    g_object_unref (new_buffer);
366
367    return drawable;
368 }