]> Pileus Git - ~andy/gtk/blob - gtk/gtktextutil.c
Don't use GTK_WIDGET_STATE in internal code anymore
[~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 #include "gtkalias.h"
39
40 #define DRAG_ICON_MAX_WIDTH 250
41 #define DRAG_ICON_MAX_HEIGHT 250
42 #define DRAG_ICON_LAYOUT_BORDER 5
43 #define DRAG_ICON_MAX_LINES 7
44 #define ELLIPSIS_CHARACTER "\xe2\x80\xa6"
45
46 typedef struct _GtkUnicodeMenuEntry GtkUnicodeMenuEntry;
47 typedef struct _GtkTextUtilCallbackInfo GtkTextUtilCallbackInfo;
48
49 struct _GtkUnicodeMenuEntry {
50   const char *label;
51   gunichar ch;
52 };
53
54 struct _GtkTextUtilCallbackInfo
55 {
56   GtkTextUtilCharChosenFunc func;
57   gpointer data;
58 };
59
60 static const GtkUnicodeMenuEntry bidi_menu_entries[] = {
61   { N_("LRM _Left-to-right mark"), 0x200E },
62   { N_("RLM _Right-to-left mark"), 0x200F },
63   { N_("LRE Left-to-right _embedding"), 0x202A },
64   { N_("RLE Right-to-left e_mbedding"), 0x202B },
65   { N_("LRO Left-to-right _override"), 0x202D },
66   { N_("RLO Right-to-left o_verride"), 0x202E },
67   { N_("PDF _Pop directional formatting"), 0x202C },
68   { N_("ZWS _Zero width space"), 0x200B },
69   { N_("ZWJ Zero width _joiner"), 0x200D },
70   { N_("ZWNJ Zero width _non-joiner"), 0x200C }
71 };
72
73 static GtkTextUtilCallbackInfo *
74 callback_info_new (GtkTextUtilCharChosenFunc  func,
75                    gpointer                   data)
76 {
77   GtkTextUtilCallbackInfo *info;
78
79   info = g_slice_new (GtkTextUtilCallbackInfo);
80
81   info->func = func;
82   info->data = data;
83
84   return info;
85 }
86
87 static void
88 callback_info_free (GtkTextUtilCallbackInfo *info)
89 {
90   g_slice_free (GtkTextUtilCallbackInfo, info);
91 }
92
93 static void
94 activate_cb (GtkWidget *menu_item,
95              gpointer   data)
96 {
97   GtkUnicodeMenuEntry *entry;
98   GtkTextUtilCallbackInfo *info = data;
99   char buf[7];
100   
101   entry = g_object_get_data (G_OBJECT (menu_item), "gtk-unicode-menu-entry");
102
103   buf[g_unichar_to_utf8 (entry->ch, buf)] = '\0';
104   
105   (* info->func) (buf, info->data);
106 }
107
108 /*
109  * _gtk_text_util_append_special_char_menuitems
110  * @menushell: a #GtkMenuShell
111  * @callback:  call this when an item is chosen
112  * @data: data for callback
113  * 
114  * Add menuitems for various bidi control characters  to a menu;
115  * the menuitems, when selected, will call the given function
116  * with the chosen character.
117  *
118  * This function is private/internal in GTK 2.0, the functionality may
119  * become public sometime, but it probably needs more thought first.
120  * e.g. maybe there should be a way to just get the list of items,
121  * instead of requiring the menu items to be created.
122  */
123 void
124 _gtk_text_util_append_special_char_menuitems (GtkMenuShell              *menushell,
125                                               GtkTextUtilCharChosenFunc  func,
126                                               gpointer                   data)
127 {
128   int i;
129
130   for (i = 0; i < G_N_ELEMENTS (bidi_menu_entries); i++)
131     {
132       GtkWidget *menuitem;
133       GtkTextUtilCallbackInfo *info;
134
135       info = callback_info_new (func, data);
136
137       menuitem = gtk_menu_item_new_with_mnemonic (_(bidi_menu_entries[i].label));
138       g_object_set_data (G_OBJECT (menuitem), I_("gtk-unicode-menu-entry"),
139                          (gpointer)&bidi_menu_entries[i]);
140
141       g_signal_connect_data (menuitem, "activate",
142                              G_CALLBACK (activate_cb),
143                              info, (GClosureNotify) callback_info_free, 0);
144
145       gtk_widget_show (menuitem);
146       gtk_menu_shell_append (menushell, menuitem);
147     }
148 }
149
150 static void
151 append_n_lines (GString *str, const gchar *text, GSList *lines, gint n_lines)
152 {
153   PangoLayoutLine *line;
154   gint i;
155
156   for (i = 0; i < n_lines; i++)
157     {
158       line = lines->data;
159       g_string_append_len (str, &text[line->start_index], line->length);
160       lines = lines->next;
161     }
162 }
163
164 static void
165 limit_layout_lines (PangoLayout *layout)
166 {
167   const gchar *text;
168   GString     *str;
169   GSList      *lines, *elem;
170   gint         n_lines;
171
172   n_lines = pango_layout_get_line_count (layout);
173   
174   if (n_lines >= DRAG_ICON_MAX_LINES)
175     {
176       text  = pango_layout_get_text (layout);
177       str   = g_string_new (NULL);
178       lines = pango_layout_get_lines_readonly (layout);
179
180       /* get first lines */
181       elem = lines;
182       append_n_lines (str, text, elem,
183                       DRAG_ICON_MAX_LINES / 2);
184
185       g_string_append (str, "\n" ELLIPSIS_CHARACTER "\n");
186
187       /* get last lines */
188       elem = g_slist_nth (lines, n_lines - DRAG_ICON_MAX_LINES / 2);
189       append_n_lines (str, text, elem,
190                       DRAG_ICON_MAX_LINES / 2);
191
192       pango_layout_set_text (layout, str->str, -1);
193       g_string_free (str, TRUE);
194     }
195 }
196
197 /*
198  * _gtk_text_util_create_drag_icon
199  * @widget: #GtkWidget to extract the pango context
200  * @text: a #gchar to render the icon
201  * @len: length of @text, or -1 for NUL-terminated text
202  *
203  * Creates a drag and drop icon from @text.
204  *
205  * Returns: a #GdkPixmap to use as DND icon
206  */
207 GdkPixmap *
208 _gtk_text_util_create_drag_icon (GtkWidget *widget, 
209                                  gchar     *text,
210                                  gsize      len)
211 {
212   GdkDrawable  *drawable = NULL;
213   PangoContext *context;
214   PangoLayout  *layout;
215   gint          pixmap_height, pixmap_width;
216   gint          layout_width, layout_height;
217
218   g_return_val_if_fail (widget != NULL, NULL);
219   g_return_val_if_fail (text != NULL, NULL);
220
221   context = gtk_widget_get_pango_context (widget);
222   layout  = pango_layout_new (context);
223
224   pango_layout_set_text (layout, text, len);
225   pango_layout_set_wrap (layout, PANGO_WRAP_WORD_CHAR);
226   pango_layout_get_size (layout, &layout_width, &layout_height);
227
228   layout_width = MIN (layout_width, DRAG_ICON_MAX_WIDTH * PANGO_SCALE);
229   pango_layout_set_width (layout, layout_width);
230
231   limit_layout_lines (layout);
232
233   /* get again layout extents, they may have changed */
234   pango_layout_get_size (layout, &layout_width, &layout_height);
235
236   pixmap_width  = layout_width  / PANGO_SCALE + DRAG_ICON_LAYOUT_BORDER * 2;
237   pixmap_height = layout_height / PANGO_SCALE + DRAG_ICON_LAYOUT_BORDER * 2;
238
239   drawable = gdk_pixmap_new (widget->window,
240                              pixmap_width  + 2,
241                              pixmap_height + 2,
242                              -1);
243
244   gdk_draw_rectangle (drawable,
245                       widget->style->base_gc [gtk_widget_get_state (widget)],
246                       TRUE,
247                       0, 0,
248                       pixmap_width + 1,
249                       pixmap_height + 1);
250
251   gdk_draw_layout (drawable,
252                    widget->style->text_gc [gtk_widget_get_state (widget)],
253                    1 + DRAG_ICON_LAYOUT_BORDER,
254                    1 + DRAG_ICON_LAYOUT_BORDER,
255                    layout);
256
257   gdk_draw_rectangle (drawable,
258                       widget->style->black_gc,
259                       FALSE,
260                       0, 0,
261                       pixmap_width + 1,
262                       pixmap_height + 1);
263
264   g_object_unref (layout);
265
266   return drawable;
267 }
268
269 static void
270 gtk_text_view_set_attributes_from_style (GtkTextView        *text_view,
271                                          GtkTextAttributes  *values,
272                                          GtkStyle           *style)
273 {
274   values->appearance.bg_color = style->base[GTK_STATE_NORMAL];
275   values->appearance.fg_color = style->text[GTK_STATE_NORMAL];
276
277   if (values->font)
278     pango_font_description_free (values->font);
279
280   values->font = pango_font_description_copy (style->font_desc);
281 }
282
283 GdkPixmap *
284 _gtk_text_util_create_rich_drag_icon (GtkWidget     *widget,
285                                       GtkTextBuffer *buffer,
286                                       GtkTextIter   *start,
287                                       GtkTextIter   *end)
288 {
289   GdkDrawable       *drawable = NULL;
290   gint               pixmap_height, pixmap_width;
291   gint               layout_width, layout_height;
292   GtkTextBuffer     *new_buffer;
293   GtkTextLayout     *layout;
294   GtkTextAttributes *style;
295   PangoContext      *ltr_context, *rtl_context;
296   GtkTextIter        iter;
297
298    g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
299    g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
300    g_return_val_if_fail (start != NULL, NULL);
301    g_return_val_if_fail (end != NULL, NULL);
302
303    new_buffer = gtk_text_buffer_new (gtk_text_buffer_get_tag_table (buffer));
304    gtk_text_buffer_get_start_iter (new_buffer, &iter);
305
306    gtk_text_buffer_insert_range (new_buffer, &iter, start, end);
307
308    gtk_text_buffer_get_start_iter (new_buffer, &iter);
309
310    layout = gtk_text_layout_new ();
311
312    ltr_context = gtk_widget_create_pango_context (widget);
313    pango_context_set_base_dir (ltr_context, PANGO_DIRECTION_LTR);
314    rtl_context = gtk_widget_create_pango_context (widget);
315    pango_context_set_base_dir (rtl_context, PANGO_DIRECTION_RTL);
316
317    gtk_text_layout_set_contexts (layout, ltr_context, rtl_context);
318
319    g_object_unref (ltr_context);
320    g_object_unref (rtl_context);
321
322    style = gtk_text_attributes_new ();
323
324    layout_width = widget->allocation.width;
325
326    if (GTK_IS_TEXT_VIEW (widget))
327      {
328        gtk_widget_ensure_style (widget);
329        gtk_text_view_set_attributes_from_style (GTK_TEXT_VIEW (widget),
330                                                 style, widget->style);
331
332        layout_width = layout_width
333          - gtk_text_view_get_border_window_size (GTK_TEXT_VIEW (widget), GTK_TEXT_WINDOW_LEFT)
334          - gtk_text_view_get_border_window_size (GTK_TEXT_VIEW (widget), GTK_TEXT_WINDOW_RIGHT);
335      }
336
337    style->direction = gtk_widget_get_direction (widget);
338    style->wrap_mode = PANGO_WRAP_WORD_CHAR;
339
340    gtk_text_layout_set_default_style (layout, style);
341    gtk_text_attributes_unref (style);
342
343    gtk_text_layout_set_buffer (layout, new_buffer);
344    gtk_text_layout_set_cursor_visible (layout, FALSE);
345    gtk_text_layout_set_screen_width (layout, layout_width);
346
347    gtk_text_layout_validate (layout, DRAG_ICON_MAX_HEIGHT);
348    gtk_text_layout_get_size (layout, &layout_width, &layout_height);
349
350    layout_width = MIN (layout_width, DRAG_ICON_MAX_WIDTH);
351    layout_height = MIN (layout_height, DRAG_ICON_MAX_HEIGHT);
352
353    pixmap_width  = layout_width + DRAG_ICON_LAYOUT_BORDER * 2;
354    pixmap_height = layout_height + DRAG_ICON_LAYOUT_BORDER * 2;
355
356    drawable = gdk_pixmap_new (widget->window,
357                               pixmap_width  + 2, pixmap_height + 2, -1);
358
359    gdk_draw_rectangle (drawable,
360                        widget->style->base_gc [gtk_widget_get_state (widget)],
361                        TRUE,
362                        0, 0,
363                        pixmap_width + 1,
364                        pixmap_height + 1);
365
366    gtk_text_layout_draw (layout, widget, drawable,
367                          widget->style->text_gc [gtk_widget_get_state (widget)],
368                          - (1 + DRAG_ICON_LAYOUT_BORDER),
369                          - (1 + DRAG_ICON_LAYOUT_BORDER),
370                          0, 0,
371                          pixmap_width, pixmap_height, NULL);
372
373    gdk_draw_rectangle (drawable,
374                        widget->style->black_gc,
375                        FALSE,
376                        0, 0,
377                        pixmap_width + 1,
378                        pixmap_height + 1);
379
380    g_object_unref (layout);
381    g_object_unref (new_buffer);
382
383    return drawable;
384 }
385
386
387 static gint
388 layout_get_char_width (PangoLayout *layout)
389 {
390   gint width;
391   PangoFontMetrics *metrics;
392   const PangoFontDescription *font_desc;
393   PangoContext *context = pango_layout_get_context (layout);
394
395   font_desc = pango_layout_get_font_description (layout);
396   if (!font_desc)
397     font_desc = pango_context_get_font_description (context);
398
399   metrics = pango_context_get_metrics (context, font_desc, NULL);
400   width = pango_font_metrics_get_approximate_char_width (metrics);
401   pango_font_metrics_unref (metrics);
402
403   return width;
404 }
405
406 /*
407  * _gtk_text_util_get_block_cursor_location
408  * @layout: a #PangoLayout
409  * @index: index at which cursor is located
410  * @pos: cursor location
411  * @at_line_end: whether cursor is drawn at line end, not over some
412  * character
413  *
414  * Returns: whether cursor should actually be drawn as a rectangle.
415  *     It may not be the case if character at index is invisible.
416  */
417 gboolean
418 _gtk_text_util_get_block_cursor_location (PangoLayout    *layout,
419                                           gint            index,
420                                           PangoRectangle *pos,
421                                           gboolean       *at_line_end)
422 {
423   PangoRectangle strong_pos, weak_pos;
424   PangoLayoutLine *layout_line;
425   gboolean rtl;
426   gint line_no;
427   const gchar *text;
428
429   g_return_val_if_fail (layout != NULL, FALSE);
430   g_return_val_if_fail (index >= 0, FALSE);
431   g_return_val_if_fail (pos != NULL, FALSE);
432
433   pango_layout_index_to_pos (layout, index, pos);
434
435   if (pos->width != 0)
436     {
437       /* cursor is at some visible character, good */
438       if (at_line_end)
439         *at_line_end = FALSE;
440       if (pos->width < 0)
441         {
442           pos->x += pos->width;
443           pos->width = -pos->width;
444         }
445       return TRUE;
446     }
447
448   pango_layout_index_to_line_x (layout, index, FALSE, &line_no, NULL);
449   layout_line = pango_layout_get_line_readonly (layout, line_no);
450   g_return_val_if_fail (layout_line != NULL, FALSE);
451
452   text = pango_layout_get_text (layout);
453
454   if (index < layout_line->start_index + layout_line->length)
455     {
456       /* this may be a zero-width character in the middle of the line,
457        * or it could be a character where line is wrapped, we do want
458        * block cursor in latter case */
459       if (g_utf8_next_char (text + index) - text !=
460           layout_line->start_index + layout_line->length)
461         {
462           /* zero-width character in the middle of the line, do not
463            * bother with block cursor */
464           return FALSE;
465         }
466     }
467
468   /* Cursor is at the line end. It may be an empty line, or it could
469    * be on the left or on the right depending on text direction, or it
470    * even could be in the middle of visual layout in bidi text. */
471
472   pango_layout_get_cursor_pos (layout, index, &strong_pos, &weak_pos);
473
474   if (strong_pos.x != weak_pos.x)
475     {
476       /* do not show block cursor in this case, since the character typed
477        * in may or may not appear at the cursor position */
478       return FALSE;
479     }
480
481   /* In case when index points to the end of line, pos->x is always most right
482    * pixel of the layout line, so we need to correct it for RTL text. */
483   if (layout_line->length)
484     {
485       if (layout_line->resolved_dir == PANGO_DIRECTION_RTL)
486         {
487           PangoLayoutIter *iter;
488           PangoRectangle line_rect;
489           gint i;
490           gint left, right;
491           const gchar *p;
492
493           p = g_utf8_prev_char (text + index);
494
495           pango_layout_line_index_to_x (layout_line, p - text, FALSE, &left);
496           pango_layout_line_index_to_x (layout_line, p - text, TRUE, &right);
497           pos->x = MIN (left, right);
498
499           iter = pango_layout_get_iter (layout);
500           for (i = 0; i < line_no; i++)
501             pango_layout_iter_next_line (iter);
502           pango_layout_iter_get_line_extents (iter, NULL, &line_rect);
503           pango_layout_iter_free (iter);
504
505           rtl = TRUE;
506           pos->x += line_rect.x;
507         }
508       else
509         rtl = FALSE;
510     }
511   else
512     {
513       PangoContext *context = pango_layout_get_context (layout);
514       rtl = pango_context_get_base_dir (context) == PANGO_DIRECTION_RTL;
515     }
516
517   pos->width = layout_get_char_width (layout);
518
519   if (rtl)
520     pos->x -= pos->width - 1;
521
522   if (at_line_end)
523     *at_line_end = TRUE;
524
525   return pos->width != 0;
526 }