1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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.
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.
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.
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/.
29 #include "gtktextview.h"
30 #include "gtktextutil.h"
32 #define GTK_TEXT_USE_INTERNAL_UNSUPPORTED_API
34 #include "gtktextdisplay.h"
35 #include "gtktextbuffer.h"
36 #include "gtkmenuitem.h"
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"
45 typedef struct _GtkUnicodeMenuEntry GtkUnicodeMenuEntry;
46 typedef struct _GtkTextUtilCallbackInfo GtkTextUtilCallbackInfo;
48 struct _GtkUnicodeMenuEntry {
53 struct _GtkTextUtilCallbackInfo
55 GtkTextUtilCharChosenFunc func;
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 }
72 static GtkTextUtilCallbackInfo *
73 callback_info_new (GtkTextUtilCharChosenFunc func,
76 GtkTextUtilCallbackInfo *info;
78 info = g_slice_new (GtkTextUtilCallbackInfo);
87 callback_info_free (GtkTextUtilCallbackInfo *info)
89 g_slice_free (GtkTextUtilCallbackInfo, info);
93 activate_cb (GtkWidget *menu_item,
96 GtkUnicodeMenuEntry *entry;
97 GtkTextUtilCallbackInfo *info = data;
100 entry = g_object_get_data (G_OBJECT (menu_item), "gtk-unicode-menu-entry");
102 buf[g_unichar_to_utf8 (entry->ch, buf)] = '\0';
104 (* info->func) (buf, info->data);
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
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.
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.
123 _gtk_text_util_append_special_char_menuitems (GtkMenuShell *menushell,
124 GtkTextUtilCharChosenFunc func,
129 for (i = 0; i < G_N_ELEMENTS (bidi_menu_entries); i++)
132 GtkTextUtilCallbackInfo *info;
134 info = callback_info_new (func, data);
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]);
140 g_signal_connect_data (menuitem, "activate",
141 G_CALLBACK (activate_cb),
142 info, (GClosureNotify) callback_info_free, 0);
144 gtk_widget_show (menuitem);
145 gtk_menu_shell_append (menushell, menuitem);
150 append_n_lines (GString *str, const gchar *text, GSList *lines, gint n_lines)
152 PangoLayoutLine *line;
155 for (i = 0; i < n_lines; i++)
158 g_string_append_len (str, &text[line->start_index], line->length);
164 limit_layout_lines (PangoLayout *layout)
168 GSList *lines, *elem;
171 n_lines = pango_layout_get_line_count (layout);
173 if (n_lines >= DRAG_ICON_MAX_LINES)
175 text = pango_layout_get_text (layout);
176 str = g_string_new (NULL);
177 lines = pango_layout_get_lines_readonly (layout);
179 /* get first lines */
181 append_n_lines (str, text, elem,
182 DRAG_ICON_MAX_LINES / 2);
184 g_string_append (str, "\n" ELLIPSIS_CHARACTER "\n");
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);
191 pango_layout_set_text (layout, str->str, -1);
192 g_string_free (str, TRUE);
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
202 * Creates a drag and drop icon from @text.
204 * Returns: a #GdkPixmap to use as DND icon
207 _gtk_text_util_create_drag_icon (GtkWidget *widget,
211 GdkDrawable *drawable = NULL;
212 PangoContext *context;
214 gint pixmap_height, pixmap_width;
215 gint layout_width, layout_height;
217 g_return_val_if_fail (widget != NULL, NULL);
218 g_return_val_if_fail (text != NULL, NULL);
220 context = gtk_widget_get_pango_context (widget);
221 layout = pango_layout_new (context);
223 pango_layout_set_text (layout, text, len);
224 pango_layout_set_wrap (layout, PANGO_WRAP_WORD_CHAR);
225 pango_layout_get_size (layout, &layout_width, &layout_height);
227 layout_width = MIN (layout_width, DRAG_ICON_MAX_WIDTH * PANGO_SCALE);
228 pango_layout_set_width (layout, layout_width);
230 limit_layout_lines (layout);
232 /* get again layout extents, they may have changed */
233 pango_layout_get_size (layout, &layout_width, &layout_height);
235 pixmap_width = layout_width / PANGO_SCALE + DRAG_ICON_LAYOUT_BORDER * 2;
236 pixmap_height = layout_height / PANGO_SCALE + DRAG_ICON_LAYOUT_BORDER * 2;
238 drawable = gdk_pixmap_new (widget->window,
243 gdk_draw_rectangle (drawable,
244 widget->style->base_gc [gtk_widget_get_state (widget)],
250 gdk_draw_layout (drawable,
251 widget->style->text_gc [gtk_widget_get_state (widget)],
252 1 + DRAG_ICON_LAYOUT_BORDER,
253 1 + DRAG_ICON_LAYOUT_BORDER,
256 gdk_draw_rectangle (drawable,
257 widget->style->black_gc,
263 g_object_unref (layout);
269 gtk_text_view_set_attributes_from_style (GtkTextView *text_view,
270 GtkTextAttributes *values,
273 values->appearance.bg_color = style->base[GTK_STATE_NORMAL];
274 values->appearance.fg_color = style->text[GTK_STATE_NORMAL];
277 pango_font_description_free (values->font);
279 values->font = pango_font_description_copy (style->font_desc);
283 _gtk_text_util_create_rich_drag_icon (GtkWidget *widget,
284 GtkTextBuffer *buffer,
288 GdkDrawable *drawable = NULL;
289 gint pixmap_height, pixmap_width;
290 gint layout_width, layout_height;
291 GtkTextBuffer *new_buffer;
292 GtkTextLayout *layout;
293 GtkTextAttributes *style;
294 PangoContext *ltr_context, *rtl_context;
297 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
298 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
299 g_return_val_if_fail (start != NULL, NULL);
300 g_return_val_if_fail (end != NULL, NULL);
302 new_buffer = gtk_text_buffer_new (gtk_text_buffer_get_tag_table (buffer));
303 gtk_text_buffer_get_start_iter (new_buffer, &iter);
305 gtk_text_buffer_insert_range (new_buffer, &iter, start, end);
307 gtk_text_buffer_get_start_iter (new_buffer, &iter);
309 layout = gtk_text_layout_new ();
311 ltr_context = gtk_widget_create_pango_context (widget);
312 pango_context_set_base_dir (ltr_context, PANGO_DIRECTION_LTR);
313 rtl_context = gtk_widget_create_pango_context (widget);
314 pango_context_set_base_dir (rtl_context, PANGO_DIRECTION_RTL);
316 gtk_text_layout_set_contexts (layout, ltr_context, rtl_context);
318 g_object_unref (ltr_context);
319 g_object_unref (rtl_context);
321 style = gtk_text_attributes_new ();
323 layout_width = widget->allocation.width;
325 if (GTK_IS_TEXT_VIEW (widget))
327 gtk_widget_ensure_style (widget);
328 gtk_text_view_set_attributes_from_style (GTK_TEXT_VIEW (widget),
329 style, widget->style);
331 layout_width = layout_width
332 - gtk_text_view_get_border_window_size (GTK_TEXT_VIEW (widget), GTK_TEXT_WINDOW_LEFT)
333 - gtk_text_view_get_border_window_size (GTK_TEXT_VIEW (widget), GTK_TEXT_WINDOW_RIGHT);
336 style->direction = gtk_widget_get_direction (widget);
337 style->wrap_mode = PANGO_WRAP_WORD_CHAR;
339 gtk_text_layout_set_default_style (layout, style);
340 gtk_text_attributes_unref (style);
342 gtk_text_layout_set_buffer (layout, new_buffer);
343 gtk_text_layout_set_cursor_visible (layout, FALSE);
344 gtk_text_layout_set_screen_width (layout, layout_width);
346 gtk_text_layout_validate (layout, DRAG_ICON_MAX_HEIGHT);
347 gtk_text_layout_get_size (layout, &layout_width, &layout_height);
349 layout_width = MIN (layout_width, DRAG_ICON_MAX_WIDTH);
350 layout_height = MIN (layout_height, DRAG_ICON_MAX_HEIGHT);
352 pixmap_width = layout_width + DRAG_ICON_LAYOUT_BORDER * 2;
353 pixmap_height = layout_height + DRAG_ICON_LAYOUT_BORDER * 2;
355 drawable = gdk_pixmap_new (widget->window,
356 pixmap_width + 2, pixmap_height + 2, -1);
358 gdk_draw_rectangle (drawable,
359 widget->style->base_gc [gtk_widget_get_state (widget)],
365 gtk_text_layout_draw (layout, widget, drawable,
366 widget->style->text_gc [gtk_widget_get_state (widget)],
367 - (1 + DRAG_ICON_LAYOUT_BORDER),
368 - (1 + DRAG_ICON_LAYOUT_BORDER),
370 pixmap_width, pixmap_height, NULL);
372 gdk_draw_rectangle (drawable,
373 widget->style->black_gc,
379 g_object_unref (layout);
380 g_object_unref (new_buffer);
387 layout_get_char_width (PangoLayout *layout)
390 PangoFontMetrics *metrics;
391 const PangoFontDescription *font_desc;
392 PangoContext *context = pango_layout_get_context (layout);
394 font_desc = pango_layout_get_font_description (layout);
396 font_desc = pango_context_get_font_description (context);
398 metrics = pango_context_get_metrics (context, font_desc, NULL);
399 width = pango_font_metrics_get_approximate_char_width (metrics);
400 pango_font_metrics_unref (metrics);
406 * _gtk_text_util_get_block_cursor_location
407 * @layout: a #PangoLayout
408 * @index: index at which cursor is located
409 * @pos: cursor location
410 * @at_line_end: whether cursor is drawn at line end, not over some
413 * Returns: whether cursor should actually be drawn as a rectangle.
414 * It may not be the case if character at index is invisible.
417 _gtk_text_util_get_block_cursor_location (PangoLayout *layout,
420 gboolean *at_line_end)
422 PangoRectangle strong_pos, weak_pos;
423 PangoLayoutLine *layout_line;
428 g_return_val_if_fail (layout != NULL, FALSE);
429 g_return_val_if_fail (index >= 0, FALSE);
430 g_return_val_if_fail (pos != NULL, FALSE);
432 pango_layout_index_to_pos (layout, index, pos);
436 /* cursor is at some visible character, good */
438 *at_line_end = FALSE;
441 pos->x += pos->width;
442 pos->width = -pos->width;
447 pango_layout_index_to_line_x (layout, index, FALSE, &line_no, NULL);
448 layout_line = pango_layout_get_line_readonly (layout, line_no);
449 g_return_val_if_fail (layout_line != NULL, FALSE);
451 text = pango_layout_get_text (layout);
453 if (index < layout_line->start_index + layout_line->length)
455 /* this may be a zero-width character in the middle of the line,
456 * or it could be a character where line is wrapped, we do want
457 * block cursor in latter case */
458 if (g_utf8_next_char (text + index) - text !=
459 layout_line->start_index + layout_line->length)
461 /* zero-width character in the middle of the line, do not
462 * bother with block cursor */
467 /* Cursor is at the line end. It may be an empty line, or it could
468 * be on the left or on the right depending on text direction, or it
469 * even could be in the middle of visual layout in bidi text. */
471 pango_layout_get_cursor_pos (layout, index, &strong_pos, &weak_pos);
473 if (strong_pos.x != weak_pos.x)
475 /* do not show block cursor in this case, since the character typed
476 * in may or may not appear at the cursor position */
480 /* In case when index points to the end of line, pos->x is always most right
481 * pixel of the layout line, so we need to correct it for RTL text. */
482 if (layout_line->length)
484 if (layout_line->resolved_dir == PANGO_DIRECTION_RTL)
486 PangoLayoutIter *iter;
487 PangoRectangle line_rect;
492 p = g_utf8_prev_char (text + index);
494 pango_layout_line_index_to_x (layout_line, p - text, FALSE, &left);
495 pango_layout_line_index_to_x (layout_line, p - text, TRUE, &right);
496 pos->x = MIN (left, right);
498 iter = pango_layout_get_iter (layout);
499 for (i = 0; i < line_no; i++)
500 pango_layout_iter_next_line (iter);
501 pango_layout_iter_get_line_extents (iter, NULL, &line_rect);
502 pango_layout_iter_free (iter);
505 pos->x += line_rect.x;
512 PangoContext *context = pango_layout_get_context (layout);
513 rtl = pango_context_get_base_dir (context) == PANGO_DIRECTION_RTL;
516 pos->width = layout_get_char_width (layout);
519 pos->x -= pos->width - 1;
524 return pos->width != 0;