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-2000. 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/.
31 #include <pango/pango.h>
33 #include <gdk/gdkkeysyms.h>
35 #include "gtkitementry.h"
37 #if GTK_CHECK_VERSION (2,17,4)
38 #define HAVE_GTKENTRYBUFFER 1
41 #if !GTK_CHECK_VERSION (2,14,0)
43 gtk_entry_get_text_length (GtkEntry *entry)
45 return entry->text_length;
49 #define MIN_ENTRY_WIDTH 150
50 #define DRAW_TIMEOUT 20
51 #define INNER_BORDER 0
53 /* Initial size of buffer, in bytes */
56 /* Maximum size of text buffer, in bytes */
57 #define MAX_SIZE G_MAXUSHORT
64 /* GObject, GtkObject methods
66 static void gtk_item_entry_class_init (GtkItemEntryClass *klass);
67 static void gtk_item_entry_init (GtkItemEntry *entry);
68 static void gtk_item_entry_editable_init (GtkEditableClass *iface);
72 static void gtk_entry_realize (GtkWidget *widget);
73 static void gtk_entry_size_request (GtkWidget *widget,
74 GtkRequisition *requisition);
75 static void gtk_entry_size_allocate (GtkWidget *widget,
76 GtkAllocation *allocation);
77 static void gtk_entry_draw_frame (GtkWidget *widget);
78 static gint gtk_entry_expose (GtkWidget *widget,
79 GdkEventExpose *event);
80 static void gtk_entry_grab_focus (GtkWidget *widget);
81 static void gtk_entry_style_set (GtkWidget *widget,
82 GtkStyle *previous_style);
83 static void gtk_entry_direction_changed (GtkWidget *widget,
84 GtkTextDirection previous_dir);
85 static void gtk_entry_state_changed (GtkWidget *widget,
86 GtkStateType previous_state);
88 /* GtkEditable method implementations
90 static void gtk_entry_insert_text (GtkEditable *editable,
91 const gchar *new_text,
94 static void gtk_entry_delete_text (GtkEditable *editable,
98 static void gtk_entry_real_set_position (GtkEditable *editable,
100 static gint gtk_entry_get_position (GtkEditable *editable);
102 /* Default signal handlers
104 static void gtk_entry_real_insert_text (GtkEditable *editable,
105 const gchar *new_text,
106 gint new_text_length,
108 static void gtk_entry_real_delete_text (GtkEditable *editable,
111 static void gtk_entry_move_cursor (GtkEntry *entry,
112 GtkMovementStep step,
114 gboolean extend_selection);
115 static void gtk_entry_insert_at_cursor (GtkEntry *entry,
117 static void gtk_entry_delete_from_cursor (GtkEntry *entry,
121 /* IM Context Callbacks
123 static void gtk_entry_commit_cb (GtkIMContext *context,
126 static void gtk_entry_preedit_changed_cb (GtkIMContext *context,
128 static gboolean gtk_entry_retrieve_surrounding_cb (GtkIMContext *context,
130 static gboolean gtk_entry_delete_surrounding_cb (GtkIMContext *context,
137 static void gtk_entry_enter_text (GtkEntry *entry,
139 static void gtk_entry_set_positions (GtkEntry *entry,
141 gint selection_bound);
142 static void gtk_entry_draw_text (GtkEntry *entry);
143 static void gtk_entry_draw_cursor (GtkEntry *entry,
145 static PangoLayout *gtk_entry_ensure_layout (GtkEntry *entry,
146 gboolean include_preedit);
147 static void gtk_entry_queue_draw (GtkEntry *entry);
148 static void gtk_entry_reset_im_context (GtkEntry *entry);
149 static void gtk_entry_recompute (GtkEntry *entry);
150 static void gtk_entry_get_cursor_locations (GtkEntry *entry,
154 static void gtk_entry_adjust_scroll (GtkEntry *entry);
155 static gint gtk_entry_move_visually (GtkEntry *editable,
158 static gint gtk_entry_move_logically (GtkEntry *entry,
161 static gint gtk_entry_move_forward_word (GtkEntry *entry,
163 static gint gtk_entry_move_backward_word (GtkEntry *entry,
165 static void gtk_entry_delete_whitespace (GtkEntry *entry);
166 static char * gtk_entry_get_public_chars (GtkEntry *entry,
169 static void gtk_entry_update_primary_selection (GtkEntry *entry);
170 static void gtk_entry_state_changed (GtkWidget *widget,
171 GtkStateType previous_state);
172 static void gtk_entry_check_cursor_blink (GtkEntry *entry);
173 static void gtk_entry_pend_cursor_blink (GtkEntry *entry);
174 static void get_text_area_size (GtkEntry *entry,
179 static void get_widget_window_size (GtkEntry *entry,
185 static GtkEntryClass *parent_class = NULL;
188 gtk_item_entry_get_type (void)
190 static GtkType item_entry_type = 0;
192 if (!item_entry_type)
194 static const GtkTypeInfo item_entry_info =
197 sizeof (GtkItemEntry),
198 sizeof (GtkItemEntryClass),
199 (GtkClassInitFunc) gtk_item_entry_class_init,
200 (GtkObjectInitFunc) gtk_item_entry_init,
201 /* reserved_1 */ NULL,
202 /* reserved_2 */ NULL,
203 (GtkClassInitFunc) NULL,
206 static const GInterfaceInfo item_editable_info =
208 (GInterfaceInitFunc) gtk_item_entry_editable_init, /* interface_init */
209 NULL, /* interface_finalize */
210 NULL /* interface_data */
214 item_entry_type = gtk_type_unique (GTK_TYPE_ENTRY, &item_entry_info);
216 g_type_add_interface_static (item_entry_type,
218 &item_editable_info);
222 return item_entry_type;
226 gtk_item_entry_class_init (GtkItemEntryClass *class)
228 GtkObjectClass *object_class;
229 GtkWidgetClass *widget_class;
230 GtkEntryClass *entry_class;
232 object_class = (GtkObjectClass*) class;
233 widget_class = (GtkWidgetClass*) class;
234 parent_class = gtk_type_class (GTK_TYPE_ENTRY);
235 entry_class = (GtkEntryClass *) class;
237 widget_class->realize = gtk_entry_realize;
238 widget_class->size_request = gtk_entry_size_request;
239 widget_class->size_allocate = gtk_entry_size_allocate;
240 widget_class->expose_event = gtk_entry_expose;
241 widget_class->grab_focus = gtk_entry_grab_focus;
242 widget_class->style_set = gtk_entry_style_set;
243 widget_class->direction_changed = gtk_entry_direction_changed;
244 widget_class->state_changed = gtk_entry_state_changed;
246 entry_class->move_cursor = gtk_entry_move_cursor;
247 entry_class->insert_at_cursor = gtk_entry_insert_at_cursor;
248 entry_class->delete_from_cursor = gtk_entry_delete_from_cursor;
253 gtk_item_entry_editable_init (GtkEditableClass *iface)
255 iface->do_insert_text = gtk_entry_insert_text;
256 iface->do_delete_text = gtk_entry_delete_text;
257 iface->insert_text = gtk_entry_real_insert_text;
258 iface->delete_text = gtk_entry_real_delete_text;
259 iface->set_position = gtk_entry_real_set_position;
260 iface->get_position = gtk_entry_get_position;
264 gtk_item_entry_init (GtkItemEntry *entry)
266 entry->justification = GTK_JUSTIFY_LEFT;
267 entry->text_max_size = 0;
268 GTK_ENTRY(entry)->has_frame = FALSE;
270 g_object_unref(G_OBJECT(GTK_ENTRY(entry)->im_context));
272 GTK_ENTRY(entry)->im_context = gtk_im_multicontext_new ();
274 g_signal_connect (G_OBJECT (GTK_ENTRY(entry)->im_context), "commit",
275 G_CALLBACK (gtk_entry_commit_cb), entry);
276 g_signal_connect (G_OBJECT (GTK_ENTRY(entry)->im_context), "preedit_changed",
277 G_CALLBACK (gtk_entry_preedit_changed_cb), entry);
278 g_signal_connect (G_OBJECT (GTK_ENTRY(entry)->im_context), "retrieve_surrounding",
279 G_CALLBACK (gtk_entry_retrieve_surrounding_cb), entry);
280 g_signal_connect (G_OBJECT (GTK_ENTRY(entry)->im_context), "delete_surrounding",
281 G_CALLBACK (gtk_entry_delete_surrounding_cb), entry);
286 gtk_entry_realize (GtkWidget *widget)
289 GtkEditable *editable;
290 GdkWindowAttr attributes;
291 gint attributes_mask;
293 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
294 entry = GTK_ENTRY (widget);
295 editable = GTK_EDITABLE (widget);
297 attributes.window_type = GDK_WINDOW_CHILD;
299 get_widget_window_size (entry, &attributes.x, &attributes.y, &attributes.width, &attributes.height);
301 attributes.wclass = GDK_INPUT_OUTPUT;
302 attributes.visual = gtk_widget_get_visual (widget);
303 attributes.colormap = gtk_widget_get_colormap (widget);
304 attributes.event_mask = gtk_widget_get_events (widget);
305 attributes.event_mask |= (GDK_EXPOSURE_MASK |
306 GDK_BUTTON_PRESS_MASK |
307 GDK_BUTTON_RELEASE_MASK |
308 GDK_BUTTON1_MOTION_MASK |
309 GDK_BUTTON3_MOTION_MASK |
310 GDK_POINTER_MOTION_HINT_MASK |
311 GDK_POINTER_MOTION_MASK |
312 GDK_ENTER_NOTIFY_MASK |
313 GDK_LEAVE_NOTIFY_MASK);
314 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
316 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
317 gdk_window_set_user_data (widget->window, entry);
319 get_text_area_size (entry, &attributes.x, &attributes.y, &attributes.width, &attributes.height);
321 attributes.cursor = gdk_cursor_new (GDK_XTERM);
322 attributes_mask |= GDK_WA_CURSOR;
324 entry->text_area = gdk_window_new (widget->window, &attributes, attributes_mask);
325 gdk_window_set_user_data (entry->text_area, entry);
327 gdk_cursor_unref (attributes.cursor);
329 widget->style = gtk_style_attach (widget->style, widget->window);
331 gdk_window_set_background (widget->window, &widget->style->bg[GTK_WIDGET_STATE(widget)]);
332 gdk_window_set_background (entry->text_area, &widget->style->bg[GTK_WIDGET_STATE (widget)]);
334 gdk_window_show (entry->text_area);
336 gtk_im_context_set_client_window (entry->im_context, entry->text_area);
338 gtk_entry_adjust_scroll (entry);
342 get_borders (GtkEntry *entry,
346 GtkWidget *widget = GTK_WIDGET (entry);
348 gboolean interior_focus;
350 gtk_widget_style_get (widget,
351 "interior-focus", &interior_focus,
352 "focus-line-width", &focus_width,
355 if (entry->has_frame)
357 *xborder = widget->style->xthickness;
358 *yborder = widget->style->ythickness;
368 *xborder += focus_width;
369 *yborder += focus_width;
375 gtk_entry_size_request (GtkWidget *widget,
376 GtkRequisition *requisition)
378 GtkEntry *entry = GTK_ENTRY (widget);
379 PangoFontMetrics *metrics;
380 gint xborder, yborder;
381 PangoContext *context;
383 context = gtk_widget_get_pango_context (widget);
384 metrics = pango_context_get_metrics (context,
385 widget->style->font_desc,
386 pango_context_get_language (context));
388 entry->ascent = pango_font_metrics_get_ascent (metrics);
389 entry->descent = pango_font_metrics_get_descent (metrics);
391 get_borders (entry, &xborder, &yborder);
393 xborder += INNER_BORDER;
394 yborder += INNER_BORDER;
396 if (entry->width_chars < 0)
397 requisition->width = MIN_ENTRY_WIDTH + xborder * 2;
400 gint char_width = pango_font_metrics_get_approximate_char_width (metrics);
401 requisition->width = PANGO_PIXELS (char_width) * entry->width_chars + xborder * 2;
404 requisition->height = PANGO_PIXELS (entry->ascent + entry->descent) + yborder * 2;
406 pango_font_metrics_unref (metrics);
410 get_text_area_size (GtkEntry *entry,
416 gint xborder, yborder;
417 GtkRequisition requisition;
418 GtkWidget *widget = GTK_WIDGET (entry);
420 gtk_widget_get_child_requisition (widget, &requisition);
422 get_borders (entry, &xborder, &yborder);
431 *width = GTK_WIDGET (entry)->allocation.width - xborder * 2;
434 *height = requisition.height - yborder * 2;
438 get_widget_window_size (GtkEntry *entry,
444 GtkRequisition requisition;
445 GtkWidget *widget = GTK_WIDGET (entry);
447 gtk_widget_get_child_requisition (widget, &requisition);
450 *x = widget->allocation.x;
454 if (entry->is_cell_renderer)
455 *y = widget->allocation.y;
457 *y = widget->allocation.y + (widget->allocation.height - requisition.height) / 2;
461 *width = widget->allocation.width;
465 if (entry->is_cell_renderer)
466 *height = widget->allocation.height;
468 *height = requisition.height;
473 gtk_entry_size_allocate (GtkWidget *widget,
474 GtkAllocation *allocation)
476 GtkEntry *entry = GTK_ENTRY (widget);
477 GtkItemEntry *ientry = GTK_ITEM_ENTRY (widget);
479 if(ientry->text_max_size > 0)
480 allocation->width = MIN(ientry->text_max_size, allocation->width);
482 widget->allocation = *allocation;
484 if (GTK_WIDGET_REALIZED (widget))
486 /* We call gtk_widget_get_child_requisition, since we want (for
487 * backwards compatibility reasons) the realization here to
488 * be affected by the usize of the entry, if set
490 gint x, y, width, height;
492 get_widget_window_size (entry, &x, &y, &width, &height);
494 gdk_window_move_resize (widget->window,
495 allocation->x, allocation->y, allocation->width, allocation->height);
497 get_text_area_size (entry, &x, &y, &width, &height);
499 gdk_window_move_resize (entry->text_area,
500 0, allocation->height - height, allocation->width, height);
502 gtk_entry_recompute (entry);
507 gtk_entry_draw_frame (GtkWidget *widget)
512 gtk_entry_expose (GtkWidget *widget,
513 GdkEventExpose *event)
515 GtkEntry *entry = GTK_ENTRY (widget);
517 if (widget->window == event->window)
518 gtk_entry_draw_frame (widget);
519 else if (entry->text_area == event->window)
521 gint area_width, area_height;
523 get_text_area_size (entry, NULL, NULL, &area_width, &area_height);
525 gdk_draw_rectangle (entry->text_area,
526 widget->style->bg_gc[GTK_WIDGET_STATE(widget)],
528 0, 0, area_width, area_height);
530 if ((entry->visible || entry->invisible_char != 0) &&
531 GTK_WIDGET_HAS_FOCUS (widget) &&
532 entry->selection_bound == entry->current_pos && entry->cursor_visible)
533 gtk_entry_draw_cursor (GTK_ENTRY (widget), CURSOR_STANDARD);
535 if (entry->dnd_position != -1)
536 gtk_entry_draw_cursor (GTK_ENTRY (widget), CURSOR_DND);
538 gtk_entry_draw_text (GTK_ENTRY (widget));
545 gtk_entry_grab_focus (GtkWidget *widget)
547 GtkEntry *entry = GTK_ENTRY (widget);
548 gboolean select_on_focus;
550 GTK_WIDGET_CLASS (parent_class)->grab_focus (widget);
552 g_object_get (G_OBJECT (gtk_settings_get_default ()),
553 "gtk-entry-select-on-focus",
557 if (select_on_focus && entry->editable && !entry->in_click)
558 gtk_editable_select_region (GTK_EDITABLE (widget), 0, -1);
562 gtk_entry_direction_changed (GtkWidget *widget,
563 GtkTextDirection previous_dir)
565 GtkEntry *entry = GTK_ENTRY (widget);
567 gtk_entry_recompute (entry);
569 GTK_WIDGET_CLASS (parent_class)->direction_changed (widget, previous_dir);
573 gtk_entry_state_changed (GtkWidget *widget,
574 GtkStateType previous_state)
576 GtkEntry *entry = GTK_ENTRY (widget);
578 if (GTK_WIDGET_REALIZED (widget))
580 gdk_window_set_background (widget->window, &widget->style->bg[GTK_WIDGET_STATE (widget)]);
581 gdk_window_set_background (entry->text_area, &widget->style->bg[GTK_WIDGET_STATE (widget)]);
584 if (!GTK_WIDGET_IS_SENSITIVE (widget))
586 /* Clear any selection */
587 gtk_editable_select_region (GTK_EDITABLE (entry), entry->current_pos, entry->current_pos);
590 gtk_widget_queue_clear (widget);
593 /* GtkEditable method implementations
596 gtk_entry_insert_text (GtkEditable *editable,
597 const gchar *new_text,
598 gint new_text_length,
601 #if HAVE_GTKENTRYBUFFER
602 g_object_ref (editable);
605 * The incoming text may a password or other secret. We make sure
606 * not to copy it into temporary buffers.
609 g_signal_emit_by_name (editable, "insert-text", new_text, new_text_length, position);
611 g_object_unref (editable);
612 #else /* !HAVE_GTKENTRYBUFFER */
613 GtkEntry *entry = GTK_ENTRY (editable);
617 if (*position < 0 || *position > entry->text_length)
618 *position = entry->text_length;
620 g_object_ref (G_OBJECT (editable));
622 if (new_text_length <= 63)
625 text = g_new (gchar, new_text_length + 1);
627 text[new_text_length] = '\0';
628 strncpy (text, new_text, new_text_length);
630 g_signal_emit_by_name (editable, "insert_text", text, new_text_length, position);
632 if (new_text_length > 63)
635 g_object_unref (G_OBJECT (editable));
636 #endif /* !HAVE_GTKENTRYBUFFER */
640 gtk_entry_delete_text (GtkEditable *editable,
644 #if HAVE_GTKENTRYBUFFER
645 g_object_ref (editable);
647 g_signal_emit_by_name (editable, "delete-text", start_pos, end_pos);
649 g_object_unref (editable);
650 #else /* !HAVE_GTKENTRYBUFFER */
651 GtkEntry *entry = GTK_ENTRY (editable);
653 if (end_pos < 0 || end_pos > entry->text_length)
654 end_pos = entry->text_length;
657 if (start_pos > end_pos)
660 g_object_ref (G_OBJECT (editable));
662 g_signal_emit_by_name (editable, "delete_text", start_pos, end_pos);
664 g_object_unref (G_OBJECT (editable));
665 #endif /* !HAVE_GTKENTRYBUFFER */
669 gtk_entry_style_set (GtkWidget *widget,
670 GtkStyle *previous_style)
672 GtkEntry *entry = GTK_ENTRY (widget);
674 if (previous_style && GTK_WIDGET_REALIZED (widget))
676 gtk_entry_recompute (entry);
678 gdk_window_set_background (widget->window, &widget->style->bg[GTK_WIDGET_STATE(widget)]);
679 gdk_window_set_background (entry->text_area, &widget->style->bg[GTK_WIDGET_STATE (widget)]);
684 gtk_entry_real_set_position (GtkEditable *editable,
687 GtkEntry *entry = GTK_ENTRY (editable);
690 length = gtk_entry_get_text_length (entry);
691 if (position < 0 || position > length)
694 if (position != entry->current_pos ||
695 position != entry->selection_bound)
697 gtk_entry_reset_im_context (entry);
698 gtk_entry_set_positions (entry, position, position);
703 gtk_entry_get_position (GtkEditable *editable)
705 return GTK_ENTRY (editable)->current_pos;
709 /* Default signal handlers
712 gtk_entry_real_insert_text (GtkEditable *editable,
713 const gchar *new_text,
714 gint new_text_length,
717 #if HAVE_GTKENTRYBUFFER
718 GtkEntry *entry = GTK_ENTRY (editable);
719 GtkEntryBuffer *buffer = gtk_entry_get_buffer (entry);
724 n_chars = g_utf8_strlen (new_text, new_text_length);
727 * The actual insertion into the buffer. This will end up firing the
728 * following signal handlers: buffer_inserted_text(), buffer_notify_display_text(),
729 * buffer_notify_text(), buffer_notify_length()
731 n_inserted = gtk_entry_buffer_insert_text (buffer, *position, new_text, n_chars);
733 if (n_inserted != n_chars)
734 gtk_widget_error_bell (GTK_WIDGET (editable));
736 *position += n_inserted;
737 #else /* !HAVE_GTKENTRYBUFFER */
741 GtkEntry *entry = GTK_ENTRY (editable);
743 if (new_text_length < 0)
744 new_text_length = strlen (new_text);
746 n_chars = g_utf8_strlen (new_text, new_text_length);
747 if (entry->text_max_length > 0 && n_chars + entry->text_length > entry->text_max_length)
750 n_chars = entry->text_max_length - entry->text_length;
751 new_text_length = g_utf8_offset_to_pointer (new_text, n_chars) - new_text;
754 if (new_text_length + entry->n_bytes + 1 > entry->text_size)
756 while (new_text_length + entry->n_bytes + 1 > entry->text_size)
758 if (entry->text_size == 0)
759 entry->text_size = MIN_SIZE;
762 if (2 * (guint)entry->text_size < MAX_SIZE &&
763 2 * (guint)entry->text_size > entry->text_size)
764 entry->text_size *= 2;
767 entry->text_size = MAX_SIZE;
768 if (new_text_length > (gint)entry->text_size - (gint)entry->n_bytes - 1)
770 new_text_length = (gint)entry->text_size - (gint)entry->n_bytes - 1;
771 new_text_length = g_utf8_find_prev_char (new_text, new_text + new_text_length + 1) - new_text;
772 n_chars = g_utf8_strlen (new_text, new_text_length);
779 entry->text = g_realloc (entry->text, entry->text_size);
782 index = g_utf8_offset_to_pointer (entry->text, *position) - entry->text;
784 g_memmove (entry->text + index + new_text_length, entry->text + index, entry->n_bytes - index);
785 memcpy (entry->text + index, new_text, new_text_length);
787 entry->n_bytes += new_text_length;
788 entry->text_length += n_chars;
790 /* NUL terminate for safety and convenience */
791 entry->text[entry->n_bytes] = '\0';
793 if (entry->current_pos > *position)
794 entry->current_pos += n_chars;
796 if (entry->selection_bound > *position)
797 entry->selection_bound += n_chars;
799 *position += n_chars;
801 gtk_entry_recompute (entry);
803 g_signal_emit_by_name (editable, "changed");
804 g_object_notify (G_OBJECT (editable), "text");
805 #endif /* !HAVE_GTKENTRYBUFFER */
809 gtk_entry_real_delete_text (GtkEditable *editable,
813 #ifdef HAVE_GTKENTRYBUFFER
814 GtkEntry *entry = GTK_ENTRY (editable);
815 GtkEntryBuffer *buffer = gtk_entry_get_buffer (entry);
817 * The actual deletion from the buffer. This will end up firing the
818 * following signal handlers: buffer_deleted_text(), buffer_notify_display_text(),
819 * buffer_notify_text(), buffer_notify_length()
822 gtk_entry_buffer_delete_text (buffer, start_pos, end_pos - start_pos);
823 #else /* !HAVE_GTKENTRYBUFFER */
824 GtkEntry *entry = GTK_ENTRY (editable);
828 if (end_pos < 0 || end_pos > entry->text_length)
829 end_pos = entry->text_length;
831 if (start_pos < end_pos)
833 gint start_index = g_utf8_offset_to_pointer (entry->text, start_pos) - entry->text;
834 gint end_index = g_utf8_offset_to_pointer (entry->text, end_pos) - entry->text;
836 g_memmove (entry->text + start_index, entry->text + end_index, entry->n_bytes + 1 - end_index);
837 entry->text_length -= (end_pos - start_pos);
838 entry->n_bytes -= (end_index - start_index);
840 if (entry->current_pos > start_pos)
841 entry->current_pos -= MIN (entry->current_pos, end_pos) - start_pos;
843 if (entry->selection_bound > start_pos)
844 entry->selection_bound -= MIN (entry->selection_bound, end_pos) - start_pos;
845 /* We might have deleted the selection
847 gtk_entry_update_primary_selection (entry);
849 gtk_entry_recompute (entry);
851 g_signal_emit_by_name (editable, "changed");
852 g_object_notify (G_OBJECT (editable), "text");
854 #endif /* !HAVE_GTKENTRYBUFFER */
857 /* Compute the X position for an offset that corresponds to the "more important
858 * cursor position for that offset. We use this when trying to guess to which
859 * end of the selection we should go to when the user hits the left or
863 get_better_cursor_x (GtkEntry *entry,
866 GtkTextDirection keymap_direction =
867 (gdk_keymap_get_direction (gdk_keymap_get_default ()) == PANGO_DIRECTION_LTR) ?
868 GTK_TEXT_DIR_LTR : GTK_TEXT_DIR_RTL;
869 GtkTextDirection widget_direction = gtk_widget_get_direction (GTK_WIDGET (entry));
870 gboolean split_cursor;
872 PangoLayout *layout = gtk_entry_ensure_layout (entry, TRUE);
873 const char *text = gtk_entry_get_text (entry);
874 gint index = g_utf8_offset_to_pointer (text, offset) - text;
876 PangoRectangle strong_pos, weak_pos;
878 g_object_get (gtk_widget_get_settings (GTK_WIDGET (entry)),
879 "gtk-split-cursor", &split_cursor,
882 pango_layout_get_cursor_pos (layout, index, &strong_pos, &weak_pos);
885 return strong_pos.x / PANGO_SCALE;
887 return (keymap_direction == widget_direction) ? strong_pos.x / PANGO_SCALE : weak_pos.x / PANGO_SCALE;
891 gtk_entry_move_cursor (GtkEntry *entry,
892 GtkMovementStep step,
894 gboolean extend_selection)
896 gint new_pos = entry->current_pos;
898 gtk_entry_reset_im_context (entry);
900 if (entry->current_pos != entry->selection_bound && !extend_selection)
902 /* If we have a current selection and aren't extending it, move to the
903 * start/or end of the selection as appropriate
907 case GTK_MOVEMENT_VISUAL_POSITIONS:
909 gint current_x = get_better_cursor_x (entry, entry->current_pos);
910 gint bound_x = get_better_cursor_x (entry, entry->selection_bound);
913 new_pos = current_x < bound_x ? entry->current_pos : entry->selection_bound;
915 new_pos = current_x > bound_x ? entry->current_pos : entry->selection_bound;
919 case GTK_MOVEMENT_LOGICAL_POSITIONS:
920 case GTK_MOVEMENT_WORDS:
922 new_pos = MIN (entry->current_pos, entry->selection_bound);
924 new_pos = MAX (entry->current_pos, entry->selection_bound);
926 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
927 case GTK_MOVEMENT_PARAGRAPH_ENDS:
928 case GTK_MOVEMENT_BUFFER_ENDS:
929 new_pos = count < 0 ? 0 : gtk_entry_get_text_length (entry);
931 case GTK_MOVEMENT_DISPLAY_LINES:
932 case GTK_MOVEMENT_PARAGRAPHS:
933 case GTK_MOVEMENT_PAGES:
943 case GTK_MOVEMENT_LOGICAL_POSITIONS:
944 new_pos = gtk_entry_move_logically (entry, new_pos, count);
946 case GTK_MOVEMENT_VISUAL_POSITIONS:
947 new_pos = gtk_entry_move_visually (entry, new_pos, count);
949 case GTK_MOVEMENT_WORDS:
952 new_pos = gtk_entry_move_forward_word (entry, new_pos);
957 new_pos = gtk_entry_move_backward_word (entry, new_pos);
961 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
962 case GTK_MOVEMENT_PARAGRAPH_ENDS:
963 case GTK_MOVEMENT_BUFFER_ENDS:
964 new_pos = count < 0 ? 0 : gtk_entry_get_text_length (entry);
966 case GTK_MOVEMENT_DISPLAY_LINES:
967 case GTK_MOVEMENT_PARAGRAPHS:
968 case GTK_MOVEMENT_PAGES:
975 if (extend_selection)
976 gtk_editable_select_region (GTK_EDITABLE (entry), entry->selection_bound, new_pos);
978 gtk_editable_set_position (GTK_EDITABLE (entry), new_pos);
980 gtk_entry_pend_cursor_blink (entry);
984 gtk_entry_insert_at_cursor (GtkEntry *entry,
987 GtkEditable *editable = GTK_EDITABLE (entry);
988 gint pos = entry->current_pos;
992 gtk_entry_reset_im_context (entry);
994 gtk_editable_insert_text (editable, str, -1, &pos);
995 gtk_editable_set_position (editable, pos);
1000 gtk_entry_delete_from_cursor (GtkEntry *entry,
1004 GtkEditable *editable = GTK_EDITABLE (entry);
1005 gint start_pos = entry->current_pos;
1006 gint end_pos = entry->current_pos;
1008 gtk_entry_reset_im_context (entry);
1010 if (!entry->editable)
1013 if (entry->selection_bound != entry->current_pos)
1015 gtk_editable_delete_selection (editable);
1021 case GTK_DELETE_CHARS:
1022 end_pos = gtk_entry_move_logically (entry, entry->current_pos, count);
1023 gtk_editable_delete_text (editable, MIN (start_pos, end_pos), MAX (start_pos, end_pos));
1025 case GTK_DELETE_WORDS:
1028 /* Move to end of current word, or if not on a word, end of previous word */
1029 end_pos = gtk_entry_move_backward_word (entry, end_pos);
1030 end_pos = gtk_entry_move_forward_word (entry, end_pos);
1034 /* Move to beginning of current word, or if not on a word, begining of next word */
1035 start_pos = gtk_entry_move_forward_word (entry, start_pos);
1036 start_pos = gtk_entry_move_backward_word (entry, start_pos);
1040 case GTK_DELETE_WORD_ENDS:
1043 start_pos = gtk_entry_move_backward_word (entry, start_pos);
1048 end_pos = gtk_entry_move_forward_word (entry, end_pos);
1051 gtk_editable_delete_text (editable, start_pos, end_pos);
1053 case GTK_DELETE_DISPLAY_LINE_ENDS:
1054 case GTK_DELETE_PARAGRAPH_ENDS:
1056 gtk_editable_delete_text (editable, 0, entry->current_pos);
1058 gtk_editable_delete_text (editable, entry->current_pos, -1);
1060 case GTK_DELETE_DISPLAY_LINES:
1061 case GTK_DELETE_PARAGRAPHS:
1062 gtk_editable_delete_text (editable, 0, -1);
1064 case GTK_DELETE_WHITESPACE:
1065 gtk_entry_delete_whitespace (entry);
1069 gtk_entry_pend_cursor_blink (entry);
1072 /* IM Context Callbacks
1076 gtk_entry_commit_cb (GtkIMContext *context,
1080 gtk_entry_enter_text (entry, str);
1084 gtk_entry_preedit_changed_cb (GtkIMContext *context,
1087 gchar *preedit_string;
1090 gtk_im_context_get_preedit_string (entry->im_context,
1091 &preedit_string, NULL,
1093 entry->preedit_length = strlen (preedit_string);
1094 cursor_pos = CLAMP (cursor_pos, 0, g_utf8_strlen (preedit_string, -1));
1095 entry->preedit_cursor = cursor_pos;
1096 g_free (preedit_string);
1098 gtk_entry_recompute (entry);
1102 gtk_entry_retrieve_surrounding_cb (GtkIMContext *context,
1105 const char *text = gtk_entry_get_text (entry);
1106 size_t n_bytes = strlen (text);
1108 gtk_im_context_set_surrounding (context,
1111 g_utf8_offset_to_pointer (text, entry->current_pos) - text);
1117 gtk_entry_delete_surrounding_cb (GtkIMContext *slave,
1122 gtk_editable_delete_text (GTK_EDITABLE (entry),
1123 entry->current_pos + offset,
1124 entry->current_pos + offset + n_chars);
1130 /* Internal functions
1133 /* Used for im_commit_cb and inserting Unicode chars */
1135 gtk_entry_enter_text (GtkEntry *entry,
1138 GtkEditable *editable = GTK_EDITABLE (entry);
1141 if (gtk_editable_get_selection_bounds (editable, NULL, NULL))
1142 gtk_editable_delete_selection (editable);
1145 if (entry->overwrite_mode)
1146 gtk_entry_delete_from_cursor (entry, GTK_DELETE_CHARS, 1);
1149 tmp_pos = entry->current_pos;
1150 gtk_editable_insert_text (editable, str, strlen (str), &tmp_pos);
1151 gtk_editable_set_position (editable, tmp_pos);
1154 /* All changes to entry->current_pos and entry->selection_bound
1155 * should go through this function.
1158 gtk_entry_set_positions (GtkEntry *entry,
1160 gint selection_bound)
1162 gboolean changed = FALSE;
1164 g_object_freeze_notify (G_OBJECT (entry));
1166 if (current_pos != -1 &&
1167 entry->current_pos != current_pos)
1169 entry->current_pos = current_pos;
1172 g_object_notify (G_OBJECT (entry), "cursor_position");
1175 if (selection_bound != -1 &&
1176 entry->selection_bound != selection_bound)
1178 entry->selection_bound = selection_bound;
1181 g_object_notify (G_OBJECT (entry), "selection_bound");
1184 g_object_thaw_notify (G_OBJECT (entry));
1187 gtk_entry_recompute (entry);
1191 gtk_entry_reset_layout (GtkEntry *entry)
1193 if (entry->cached_layout)
1195 g_object_unref (G_OBJECT (entry->cached_layout));
1196 entry->cached_layout = NULL;
1201 update_im_cursor_location (GtkEntry *entry)
1205 gint strong_xoffset;
1206 gint x, y, area_width, area_height;
1208 gtk_entry_get_cursor_locations (entry, CURSOR_STANDARD, &strong_x, NULL)
1210 get_text_area_size (entry, &x, &y, &area_width, &area_height);
1212 strong_xoffset = strong_x - entry->scroll_offset;
1213 if (strong_xoffset < 0)
1217 else if (strong_xoffset > area_width)
1219 strong_xoffset = area_width;
1221 area.x = x + strong_xoffset;
1222 area.y = y + area_height;
1223 area.width = area_width;
1224 area.height = area_height;
1226 gtk_im_context_set_cursor_location (entry->im_context, &area);
1230 recompute_idle_func (gpointer data)
1234 GDK_THREADS_ENTER ();
1236 entry = GTK_ENTRY (data);
1238 gtk_entry_adjust_scroll (entry);
1239 gtk_entry_queue_draw (entry);
1241 entry->recompute_idle = FALSE;
1243 update_im_cursor_location (entry);
1245 GDK_THREADS_LEAVE ();
1251 gtk_entry_recompute (GtkEntry *entry)
1253 gtk_entry_reset_layout (entry);
1254 gtk_entry_check_cursor_blink (entry);
1257 if (!entry->recompute_idle)
1259 entry->recompute_idle = g_idle_add_full (G_PRIORITY_HIGH_IDLE + 15, /* between resize and redraw */
1260 recompute_idle_func, entry, NULL);
1265 append_char (GString *str,
1273 char_len = g_unichar_to_utf8 (ch, buf);
1278 g_string_append_len (str, buf, char_len);
1283 static PangoLayout *
1284 gtk_entry_create_layout (GtkEntry *entry,
1285 gboolean include_preedit)
1287 PangoLayout *layout = gtk_widget_create_pango_layout (GTK_WIDGET (entry), NULL);
1288 PangoAttrList *tmp_attrs = pango_attr_list_new ();
1290 gchar *preedit_string = NULL;
1291 gint preedit_length = 0;
1292 PangoAttrList *preedit_attrs = NULL;
1294 const char *text = gtk_entry_get_text (entry);
1295 size_t n_bytes = strlen (text);
1297 pango_layout_set_single_paragraph_mode (layout, TRUE);
1299 if (include_preedit)
1301 gtk_im_context_get_preedit_string (entry->im_context,
1302 &preedit_string, &preedit_attrs, NULL);
1303 preedit_length = entry->preedit_length;
1308 GString *tmp_string = g_string_new (NULL);
1310 gint cursor_index = g_utf8_offset_to_pointer (text, entry->current_pos) - text;
1314 g_string_prepend_len (tmp_string, text, n_bytes);
1315 g_string_insert (tmp_string, cursor_index, preedit_string);
1320 gint preedit_len_chars;
1321 gunichar invisible_char;
1323 ch_len = g_utf8_strlen (text, n_bytes);
1324 preedit_len_chars = g_utf8_strlen (preedit_string, -1);
1325 ch_len += preedit_len_chars;
1327 if (entry->invisible_char != 0)
1328 invisible_char = entry->invisible_char;
1330 invisible_char = ' '; /* just pick a char */
1332 append_char (tmp_string, invisible_char, ch_len);
1334 /* Fix cursor index to point to invisible char corresponding
1335 * to the preedit, fix preedit_length to be the length of
1336 * the invisible chars representing the preedit
1339 g_utf8_offset_to_pointer (tmp_string->str, entry->current_pos) -
1343 g_unichar_to_utf8 (invisible_char, NULL);
1346 pango_layout_set_text (layout, tmp_string->str, tmp_string->len);
1348 pango_attr_list_splice (tmp_attrs, preedit_attrs,
1349 cursor_index, preedit_length);
1351 g_string_free (tmp_string, TRUE);
1357 pango_layout_set_text (layout, text, n_bytes);
1361 GString *str = g_string_new (NULL);
1362 gunichar invisible_char;
1364 if (entry->invisible_char != 0)
1365 invisible_char = entry->invisible_char;
1367 invisible_char = ' '; /* just pick a char */
1369 append_char (str, invisible_char, gtk_entry_get_text_length (entry));
1370 pango_layout_set_text (layout, str->str, str->len);
1371 g_string_free (str, TRUE);
1375 pango_layout_set_attributes (layout, tmp_attrs);
1378 g_free (preedit_string);
1380 pango_attr_list_unref (preedit_attrs);
1382 pango_attr_list_unref (tmp_attrs);
1387 static PangoLayout *
1388 gtk_entry_ensure_layout (GtkEntry *entry,
1389 gboolean include_preedit)
1391 if (entry->preedit_length > 0 &&
1392 !include_preedit != !entry->cache_includes_preedit)
1393 gtk_entry_reset_layout (entry);
1395 if (!entry->cached_layout)
1397 entry->cached_layout = gtk_entry_create_layout (entry, include_preedit);
1398 entry->cache_includes_preedit = include_preedit;
1401 return entry->cached_layout;
1405 get_layout_position (GtkEntry *entry,
1409 PangoLayout *layout;
1410 PangoRectangle logical_rect;
1411 gint area_width, area_height;
1413 PangoLayoutLine *line;
1415 layout = gtk_entry_ensure_layout (entry, TRUE);
1417 get_text_area_size (entry, NULL, NULL, &area_width, &area_height);
1419 area_height = PANGO_SCALE * (area_height);
1421 line = pango_layout_get_lines (layout)->data;
1422 pango_layout_line_get_extents (line, NULL, &logical_rect);
1424 /* Align primarily for locale's ascent/descent */
1426 y_pos = ((area_height - entry->ascent - entry->descent) / 2 +
1427 entry->ascent + logical_rect.y);
1430 /* Now see if we need to adjust to fit in actual drawn string */
1432 if (logical_rect.height > area_height)
1433 y_pos = (area_height - logical_rect.height) / 2;
1436 else if (y_pos + logical_rect.height > area_height)
1437 y_pos = area_height - logical_rect.height;
1439 y_pos = y_pos / PANGO_SCALE;
1442 *x = - entry->scroll_offset;
1449 gtk_entry_draw_text (GtkEntry *entry)
1452 PangoLayoutLine *line;
1454 if (!entry->visible && entry->invisible_char == 0)
1457 if (GTK_WIDGET_DRAWABLE (entry))
1459 PangoLayout *layout = gtk_entry_ensure_layout (entry, TRUE);
1460 gint area_width, area_height;
1463 gint start_pos, end_pos;
1465 widget = GTK_WIDGET (entry);
1467 get_layout_position (entry, &x, &y);
1469 get_text_area_size (entry, NULL, NULL, &area_width, &area_height);
1472 gdk_draw_layout (entry->text_area, widget->style->text_gc [widget->state],
1477 if (gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &start_pos, &end_pos))
1481 PangoRectangle logical_rect;
1482 const gchar *text = pango_layout_get_text (layout);
1483 gint start_index = g_utf8_offset_to_pointer (text, start_pos) - text;
1484 gint end_index = g_utf8_offset_to_pointer (text, end_pos) - text;
1485 GdkRegion *clip_region = gdk_region_new ();
1487 GdkGC *selection_gc;
1489 line = pango_layout_get_lines (layout)->data;
1491 pango_layout_line_get_x_ranges (line, start_index, end_index, &ranges, &n_ranges);
1493 pango_layout_get_extents (layout, NULL, &logical_rect);
1495 if (GTK_WIDGET_HAS_FOCUS (entry))
1497 selection_gc = widget->style->base_gc [GTK_STATE_SELECTED];
1498 text_gc = widget->style->text_gc [GTK_STATE_SELECTED];
1502 selection_gc = widget->style->base_gc [GTK_STATE_ACTIVE];
1503 text_gc = widget->style->text_gc [GTK_STATE_ACTIVE];
1506 for (i=0; i < n_ranges; i++)
1510 rect.x = INNER_BORDER - entry->scroll_offset + ranges[2*i] / PANGO_SCALE;
1512 rect.width = (ranges[2*i + 1] - ranges[2*i]) / PANGO_SCALE;
1513 rect.height = logical_rect.height / PANGO_SCALE;
1515 gdk_draw_rectangle (entry->text_area, selection_gc, TRUE,
1516 rect.x, rect.y, rect.width, rect.height);
1518 gdk_region_union_with_rect (clip_region, &rect);
1521 gdk_gc_set_clip_region (text_gc, clip_region);
1522 gdk_draw_layout (entry->text_area, text_gc,
1525 gdk_gc_set_clip_region (text_gc, NULL);
1527 gdk_region_destroy (clip_region);
1534 * From _gtk_get_insertion_cursor_gc
1537 typedef struct _CursorInfo CursorInfo;
1543 GdkGC *secondary_gc;
1547 make_cursor_gc (GtkWidget *widget,
1548 const gchar *property_name,
1551 GdkGCValues gc_values;
1552 GdkGCValuesMask gc_values_mask;
1553 GdkColor *cursor_color;
1555 gtk_widget_style_get (widget, property_name, &cursor_color, NULL);
1557 gc_values_mask = GDK_GC_FOREGROUND;
1560 gc_values.foreground = *cursor_color;
1561 gdk_color_free (cursor_color);
1564 gc_values.foreground = *fallback;
1566 gdk_rgb_find_color (widget->style->colormap, &gc_values.foreground);
1567 return gtk_gc_get (widget->style->depth, widget->style->colormap,
1568 &gc_values, gc_values_mask);
1572 _gtkextra_get_insertion_cursor_gc (GtkWidget *widget,
1573 gboolean is_primary)
1575 CursorInfo *cursor_info;
1577 cursor_info = g_object_get_data (G_OBJECT (widget->style), "gtk-style-cursor-info");
1580 cursor_info = g_new (CursorInfo, 1);
1581 g_object_set_data (G_OBJECT (widget->style), "gtk-style-cursor-info", cursor_info);
1582 cursor_info->primary_gc = NULL;
1583 cursor_info->secondary_gc = NULL;
1584 cursor_info->for_type = G_TYPE_INVALID;
1587 /* We have to keep track of the type because gtk_widget_style_get()
1588 * can return different results when called on the same property and
1589 * same style but for different widgets. :-(. That is,
1590 * GtkEntry::cursor-color = "red" in a style will modify the cursor
1591 * color for entries but not for text view.
1593 if (cursor_info->for_type != G_OBJECT_TYPE (widget))
1595 cursor_info->for_type = G_OBJECT_TYPE (widget);
1596 if (cursor_info->primary_gc)
1598 gtk_gc_release (cursor_info->primary_gc);
1599 cursor_info->primary_gc = NULL;
1601 if (cursor_info->secondary_gc)
1603 gtk_gc_release (cursor_info->secondary_gc);
1604 cursor_info->secondary_gc = NULL;
1610 if (!cursor_info->primary_gc)
1611 cursor_info->primary_gc = make_cursor_gc (widget,
1613 &widget->style->black);
1615 return g_object_ref (cursor_info->primary_gc);
1619 static GdkColor gray = { 0, 0x8888, 0x8888, 0x8888 };
1621 if (!cursor_info->secondary_gc)
1622 cursor_info->secondary_gc = make_cursor_gc (widget,
1623 "secondary-cursor-color",
1626 return g_object_ref (cursor_info->secondary_gc);
1631 * From _gtk_draw_insertion_cursor
1634 _gtkextra_draw_insertion_cursor (GtkWidget *widget,
1635 GdkDrawable *drawable,
1637 GdkRectangle *location,
1638 GtkTextDirection direction,
1639 gboolean draw_arrow)
1645 gfloat cursor_aspect_ratio;
1648 g_return_if_fail (direction != GTK_TEXT_DIR_NONE);
1650 gtk_widget_style_get (widget, "cursor-aspect-ratio", &cursor_aspect_ratio, NULL);
1652 stem_width = location->height * cursor_aspect_ratio + 1;
1653 arrow_width = stem_width + 1;
1655 /* put (stem_width % 2) on the proper side of the cursor */
1656 if (direction == GTK_TEXT_DIR_LTR)
1657 offset = stem_width / 2;
1659 offset = stem_width - stem_width / 2;
1661 for (i = 0; i < stem_width; i++)
1662 gdk_draw_line (drawable, gc,
1663 location->x + i - offset, location->y,
1664 location->x + i - offset, location->y + location->height - 1);
1668 if (direction == GTK_TEXT_DIR_RTL)
1670 x = location->x - offset - 1;
1671 y = location->y + location->height - arrow_width * 2 - arrow_width + 1;
1673 for (i = 0; i < arrow_width; i++)
1675 gdk_draw_line (drawable, gc,
1677 x, y + 2 * arrow_width - i - 1);
1681 else if (direction == GTK_TEXT_DIR_LTR)
1683 x = location->x + stem_width - offset;
1684 y = location->y + location->height - arrow_width * 2 - arrow_width + 1;
1686 for (i = 0; i < arrow_width; i++)
1688 gdk_draw_line (drawable, gc,
1690 x, y + 2 * arrow_width - i - 1);
1698 gtk_entry_draw_cursor (GtkEntry *entry,
1701 GtkTextDirection keymap_direction =
1702 (gdk_keymap_get_direction (gdk_keymap_get_default ()) == PANGO_DIRECTION_LTR) ?
1703 GTK_TEXT_DIR_LTR : GTK_TEXT_DIR_RTL;
1704 GtkTextDirection widget_direction = gtk_widget_get_direction (GTK_WIDGET (entry));
1706 if (GTK_WIDGET_DRAWABLE (entry) && GTK_ENTRY(entry)->cursor_visible)
1708 GtkWidget *widget = GTK_WIDGET (entry);
1709 GdkRectangle cursor_location;
1710 gboolean split_cursor;
1712 gint xoffset = INNER_BORDER - entry->scroll_offset;
1713 gint strong_x, weak_x;
1714 gint text_area_height;
1715 GtkTextDirection dir1 = GTK_TEXT_DIR_NONE;
1716 GtkTextDirection dir2 = GTK_TEXT_DIR_NONE;
1721 gdk_window_get_size (entry->text_area, NULL, &text_area_height);
1723 gtk_entry_get_cursor_locations (entry, type, &strong_x, &weak_x);
1725 g_object_get (gtk_widget_get_settings (widget),
1726 "gtk-split-cursor", &split_cursor,
1729 dir1 = widget_direction;
1735 if (weak_x != strong_x)
1737 dir2 = (widget_direction == GTK_TEXT_DIR_LTR) ? GTK_TEXT_DIR_RTL : GTK_TEXT_DIR_LTR;
1743 if (keymap_direction == widget_direction)
1749 cursor_location.x = xoffset + x1;
1750 cursor_location.y = INNER_BORDER;
1751 cursor_location.width = 0;
1752 cursor_location.height = text_area_height - 2 * INNER_BORDER ;
1754 gc = _gtkextra_get_insertion_cursor_gc (widget, TRUE);
1755 _gtkextra_draw_insertion_cursor (widget, entry->text_area, gc,
1756 &cursor_location, dir1,
1757 dir2 != GTK_TEXT_DIR_NONE);
1758 g_object_unref (gc);
1760 if (dir2 != GTK_TEXT_DIR_NONE)
1762 cursor_location.x = xoffset + x2;
1763 gc = _gtkextra_get_insertion_cursor_gc (widget, FALSE);
1764 _gtkextra_draw_insertion_cursor (widget, entry->text_area, gc,
1765 &cursor_location, dir2,
1767 g_object_unref (gc);
1773 gtk_entry_queue_draw (GtkEntry *entry)
1775 if (GTK_WIDGET_REALIZED (entry))
1776 gdk_window_invalidate_rect (entry->text_area, NULL, FALSE);
1780 gtk_entry_reset_im_context (GtkEntry *entry)
1782 if (entry->need_im_reset)
1784 entry->need_im_reset = 0;
1785 gtk_im_context_reset (entry->im_context);
1790 gtk_entry_get_cursor_locations (GtkEntry *entry,
1795 PangoLayout *layout = gtk_entry_ensure_layout (entry, TRUE);
1797 PangoRectangle strong_pos, weak_pos;
1800 if (type == CURSOR_STANDARD)
1802 text = pango_layout_get_text (layout);
1803 index = g_utf8_offset_to_pointer (text, entry->current_pos + entry->preedit_cursor) - text;
1805 else /* type == CURSOR_DND */
1807 const char *text = gtk_entry_get_text (entry);
1808 index = g_utf8_offset_to_pointer (text, entry->dnd_position) - text;
1809 if (entry->dnd_position > entry->current_pos)
1810 index += entry->preedit_length;
1813 pango_layout_get_cursor_pos (layout, index, &strong_pos, &weak_pos);
1816 *strong_x = strong_pos.x / PANGO_SCALE;
1819 *weak_x = weak_pos.x / PANGO_SCALE;
1823 gtk_entry_adjust_scroll (GtkEntry *entry)
1825 gint min_offset, max_offset;
1826 gint text_area_width;
1827 gint strong_x, weak_x;
1828 PangoLayout *layout;
1829 PangoLayoutLine *line;
1830 PangoRectangle logical_rect;
1831 GtkItemEntry *item_entry;
1834 if (!GTK_WIDGET_REALIZED (entry))
1837 item_entry = GTK_ITEM_ENTRY(entry);
1839 gdk_window_get_size (entry->text_area, &text_area_width, NULL);
1840 text_area_width -= 2 * INNER_BORDER;
1842 layout = gtk_entry_ensure_layout (entry, TRUE);
1843 line = pango_layout_get_lines (layout)->data;
1845 pango_layout_line_get_extents (line, NULL, &logical_rect);
1846 text_width = logical_rect.width / PANGO_SCALE + 2; /* 2 for cursor */
1848 gtk_entry_get_cursor_locations (entry, CURSOR_STANDARD, &strong_x, &weak_x);
1850 /* Display as much text as we can */
1852 if (gtk_widget_get_direction (GTK_WIDGET (entry)) == GTK_TEXT_DIR_LTR)
1854 entry->scroll_offset = 0;
1855 switch(item_entry->justification){
1857 case GTK_JUSTIFY_FILL:
1858 case GTK_JUSTIFY_LEFT:
1860 /* LEFT JUSTIFICATION */
1862 strong_x -= entry->scroll_offset;
1864 entry->scroll_offset += strong_x;
1865 else if (strong_x > text_area_width){
1866 if(item_entry->text_max_size != 0 &&
1867 text_area_width + 2 <= item_entry->text_max_size){
1868 GtkAllocation allocation;
1869 allocation = GTK_WIDGET(entry)->allocation;
1870 allocation.width += text_width - text_area_width;
1871 entry->scroll_offset = 0;
1872 gtk_entry_size_allocate(GTK_WIDGET(entry), &allocation);
1874 entry->scroll_offset += (strong_x - text_area_width) + 1;
1880 case GTK_JUSTIFY_RIGHT:
1882 /* RIGHT JUSTIFICATION FOR NUMBERS */
1883 if(gtk_entry_get_text (entry)){
1885 entry->scroll_offset= -(text_area_width - text_width) + 1;
1886 if(entry->scroll_offset > 0){
1887 if(item_entry->text_max_size != 0 &&
1888 text_area_width + 2 <= item_entry->text_max_size){
1889 GtkAllocation allocation;
1890 allocation = GTK_WIDGET(entry)->allocation;
1891 allocation.x -= text_width - text_area_width;
1892 allocation.width += text_width - text_area_width;
1893 entry->scroll_offset = 0;
1894 gtk_entry_size_allocate(GTK_WIDGET(entry), &allocation);
1898 entry->scroll_offset= -(text_area_width - strong_x) + 1;
1899 if(entry->scroll_offset < 0) entry->scroll_offset = 0;
1904 entry->scroll_offset=0;
1907 case GTK_JUSTIFY_CENTER:
1909 if(gtk_entry_get_text (entry)){
1911 entry->scroll_offset= -(text_area_width - text_width)/2;
1912 if(entry->scroll_offset > 0){
1913 if(item_entry->text_max_size != 0 &&
1914 text_area_width+1<=item_entry->text_max_size){
1915 GtkAllocation allocation;
1916 allocation = GTK_WIDGET(entry)->allocation;
1917 allocation.x += (text_area_width/2 - text_width/2);
1918 allocation.width += text_width - text_area_width;
1919 entry->scroll_offset = 0;
1920 gtk_entry_size_allocate(GTK_WIDGET(entry), &allocation);
1924 entry->scroll_offset= -(text_area_width - strong_x) + 1;
1925 if(entry->scroll_offset < 0) entry->scroll_offset = 0;
1930 entry->scroll_offset=0;
1939 max_offset = text_width - text_area_width;
1940 min_offset = MIN (0, max_offset);
1941 entry->scroll_offset = CLAMP (entry->scroll_offset, min_offset, max_offset);
1944 g_object_notify (G_OBJECT (entry), "scroll_offset");
1948 gtk_entry_move_visually (GtkEntry *entry,
1953 PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
1956 text = pango_layout_get_text (layout);
1958 index = g_utf8_offset_to_pointer (text, start) - text;
1962 const char *text = gtk_entry_get_text (entry);
1963 int new_index, new_trailing;
1964 gboolean split_cursor;
1967 g_object_get (gtk_widget_get_settings (GTK_WIDGET (entry)),
1968 "gtk-split-cursor", &split_cursor,
1975 GtkTextDirection keymap_direction =
1976 (gdk_keymap_get_direction (gdk_keymap_get_default ()) == PANGO_DIRECTION_LTR) ?
1977 GTK_TEXT_DIR_LTR : GTK_TEXT_DIR_RTL;
1979 strong = keymap_direction == gtk_widget_get_direction (GTK_WIDGET (entry));
1984 pango_layout_move_cursor_visually (layout, strong, index, 0, 1, &new_index, &new_trailing);
1989 pango_layout_move_cursor_visually (layout, strong, index, 0, -1, &new_index, &new_trailing);
1993 if (new_index < 0 || new_index == G_MAXINT)
1998 while (new_trailing--)
1999 index = g_utf8_next_char (text + new_index) - text;
2002 return g_utf8_pointer_to_offset (text, text + index);
2006 gtk_entry_move_logically (GtkEntry *entry,
2010 const char *text = gtk_entry_get_text (entry);
2011 guint16 text_length = gtk_entry_get_text_length (entry);
2012 gint new_pos = start;
2014 /* Prevent any leak of information */
2015 if (!entry->visible)
2017 new_pos = CLAMP (start + count, 0, text_length);
2021 PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
2022 PangoLogAttr *log_attrs;
2025 pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
2027 while (count > 0 && new_pos < text_length)
2031 while (new_pos < text_length && !log_attrs[new_pos].is_cursor_position);
2035 while (count < 0 && new_pos > 0)
2039 while (new_pos > 0 && !log_attrs[new_pos].is_cursor_position);
2051 gtk_entry_move_forward_word (GtkEntry *entry,
2054 const char *text = gtk_entry_get_text (entry);
2055 guint16 text_length = gtk_entry_get_text_length (entry);
2056 gint new_pos = start;
2058 /* Prevent any leak of information */
2059 if (!entry->visible)
2061 new_pos = text_length;
2063 else if (text && (new_pos < text_length))
2065 PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
2066 PangoLogAttr *log_attrs;
2069 pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
2071 /* Find the next word end */
2073 while (new_pos < n_attrs && !log_attrs[new_pos].is_word_end)
2084 gtk_entry_move_backward_word (GtkEntry *entry,
2087 gint new_pos = start;
2089 /* Prevent any leak of information */
2090 if (!entry->visible)
2094 else if (gtk_entry_get_text (entry) && start > 0)
2096 PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
2097 PangoLogAttr *log_attrs;
2100 pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
2102 new_pos = start - 1;
2104 /* Find the previous word beginning */
2105 while (new_pos > 0 && !log_attrs[new_pos].is_word_start)
2115 gtk_entry_delete_whitespace (GtkEntry *entry)
2117 PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
2118 PangoLogAttr *log_attrs;
2122 pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
2124 start = end = entry->current_pos;
2126 while (start > 0 && log_attrs[start-1].is_white)
2129 while (end < n_attrs && log_attrs[end].is_white)
2135 gtk_editable_delete_text (GTK_EDITABLE (entry), start, end);
2140 * Like gtk_editable_get_chars, but if the editable is not
2141 * visible, return asterisks; also convert result to UTF-8.
2144 gtk_entry_get_public_chars (GtkEntry *entry,
2149 end = gtk_entry_get_text_length (entry);
2152 return gtk_editable_get_chars (GTK_EDITABLE (entry), start, end);
2157 gint n_chars = end - start;
2159 str = g_malloc (n_chars + 1);
2160 for (i = 0; i < n_chars; i++)
2170 primary_get_cb (GtkClipboard *clipboard,
2171 GtkSelectionData *selection_data,
2175 GtkEntry *entry = GTK_ENTRY (data);
2178 if (gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &start, &end))
2180 gchar *str = gtk_entry_get_public_chars (entry, start, end);
2181 gtk_selection_data_set_text (selection_data, str, -1);
2187 primary_clear_cb (GtkClipboard *clipboard,
2190 GtkEntry *entry = GTK_ENTRY (data);
2192 gtk_editable_select_region (GTK_EDITABLE (entry), entry->current_pos, entry->current_pos);
2196 gtk_entry_update_primary_selection (GtkEntry *entry)
2198 static const GtkTargetEntry targets[] = {
2199 { "UTF8_STRING", 0, 0 },
2202 { "COMPOUND_TEXT", 0, 0 }
2205 GtkClipboard *clipboard = gtk_clipboard_get (GDK_SELECTION_PRIMARY);
2208 if (gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &start, &end))
2210 if (!gtk_clipboard_set_with_owner (clipboard, targets, G_N_ELEMENTS (targets),
2211 primary_get_cb, primary_clear_cb, G_OBJECT (entry)))
2212 primary_clear_cb (clipboard, entry);
2216 if (gtk_clipboard_get_owner (clipboard) == G_OBJECT (entry))
2217 gtk_clipboard_clear (clipboard);
2225 gtk_item_entry_new (void)
2229 widget = GTK_WIDGET (gtk_type_new (GTK_TYPE_ITEM_ENTRY));
2234 gtk_item_entry_new_with_max_length (gint max)
2236 GtkItemEntry *entry;
2238 entry = gtk_type_new (GTK_TYPE_ITEM_ENTRY);
2239 gtk_entry_set_max_length(GTK_ENTRY(entry), max);
2241 return GTK_WIDGET (entry);
2245 gtk_item_entry_set_text (GtkItemEntry *entry,
2247 GtkJustification justification)
2251 g_return_if_fail (GTK_IS_ITEM_ENTRY (entry));
2252 g_return_if_fail (text != NULL);
2254 entry->justification = justification;
2256 /* Actually setting the text will affect the cursor and selection;
2257 * if the contents don't actually change, this will look odd to the user.
2259 if (strcmp (gtk_entry_get_text (GTK_ENTRY(entry)), text) == 0)
2262 if (GTK_ENTRY(entry)->recompute_idle){
2263 g_source_remove (GTK_ENTRY(entry)->recompute_idle);
2264 GTK_ENTRY(entry)->recompute_idle = 0;
2266 if (GTK_ENTRY(entry)->blink_timeout){
2267 g_source_remove (GTK_ENTRY(entry)->blink_timeout);
2268 GTK_ENTRY(entry)->blink_timeout = 0;
2271 gtk_editable_delete_text (GTK_EDITABLE (entry), 0, -1);
2274 gtk_editable_insert_text (GTK_EDITABLE (entry), text, strlen (text), &tmp_pos);
2278 * gtk_entry_get_layout_offsets:
2279 * @entry: a #GtkEntry
2280 * @x: location to store X offset of layout, or %NULL
2281 * @y: location to store Y offset of layout, or %NULL
2284 * Obtains the position of the #PangoLayout used to render text
2285 * in the entry, in widget coordinates. Useful if you want to line
2286 * up the text in an entry with some other text, e.g. when using the
2287 * entry to implement editable cells in a sheet widget.
2289 * Also useful to convert mouse events into coordinates inside the
2290 * #PangoLayout, e.g. to take some action if some part of the entry text
2293 * Note that as the user scrolls around in the entry the offsets will
2294 * change; you'll need to connect to the "notify::scroll_offset"
2295 * signal to track this. Remember when using the #PangoLayout
2296 * functions you need to convert to and from pixels using
2297 * PANGO_PIXELS() or #PANGO_SCALE.
2299 * Keep in mind that the layout text may contain a preedit string, so
2300 * gtk_entry_layout_index_to_text_index() and
2301 * gtk_entry_text_index_to_layout_index() are needed to convert byte
2302 * indices in the layout to byte indices in the entry contents.
2306 gtk_item_entry_get_layout_offsets (GtkItemEntry *entry,
2310 gint text_area_x, text_area_y;
2312 g_return_if_fail (GTK_IS_ITEM_ENTRY (entry));
2314 /* this gets coords relative to text area */
2315 get_layout_position (GTK_ENTRY(entry), x, y);
2317 /* convert to widget coords */
2318 get_text_area_size (GTK_ENTRY(entry), &text_area_x, &text_area_y, NULL, NULL);
2328 gtk_item_entry_set_justification(GtkItemEntry *entry, GtkJustification just)
2330 g_return_if_fail (GTK_IS_ITEM_ENTRY (entry));
2332 entry->justification = just;
2336 /* We display the cursor when
2338 * - the selection is empty, AND
2339 * - the widget has focus
2342 #define CURSOR_ON_MULTIPLIER 0.66
2343 #define CURSOR_OFF_MULTIPLIER 0.34
2344 #define CURSOR_PEND_MULTIPLIER 1.0
2347 cursor_blinks (GtkEntry *entry)
2349 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (entry));
2352 if (GTK_WIDGET_HAS_FOCUS (entry) &&
2353 entry->selection_bound == entry->current_pos)
2355 g_object_get (G_OBJECT (settings), "gtk-cursor-blink", &blink, NULL);
2363 get_cursor_time (GtkEntry *entry)
2365 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (entry));
2368 g_object_get (G_OBJECT (settings), "gtk-cursor-blink-time", &time, NULL);
2374 show_cursor (GtkEntry *entry)
2376 if (!entry->cursor_visible)
2378 entry->cursor_visible = TRUE;
2380 if (GTK_WIDGET_HAS_FOCUS (entry) && entry->selection_bound == entry->current_pos)
2381 gtk_widget_queue_draw (GTK_WIDGET (entry));
2386 hide_cursor (GtkEntry *entry)
2388 if (entry->cursor_visible)
2390 entry->cursor_visible = FALSE;
2392 if (GTK_WIDGET_HAS_FOCUS (entry) && entry->selection_bound == entry->current_pos)
2393 gtk_widget_queue_draw (GTK_WIDGET (entry));
2401 blink_cb (gpointer data)
2405 GDK_THREADS_ENTER ();
2407 entry = GTK_ENTRY (data);
2409 g_assert (GTK_WIDGET_HAS_FOCUS (entry));
2410 g_assert (entry->selection_bound == entry->current_pos);
2412 if (entry->cursor_visible)
2414 hide_cursor (entry);
2415 entry->blink_timeout = gtk_timeout_add (get_cursor_time (entry) * CURSOR_OFF_MULTIPLIER,
2421 show_cursor (entry);
2422 entry->blink_timeout = gtk_timeout_add (get_cursor_time (entry) * CURSOR_ON_MULTIPLIER,
2427 GDK_THREADS_LEAVE ();
2429 /* Remove ourselves */
2434 gtk_entry_check_cursor_blink (GtkEntry *entry)
2436 if (cursor_blinks (entry))
2438 if (!entry->blink_timeout)
2440 entry->blink_timeout = gtk_timeout_add (get_cursor_time (entry) * CURSOR_ON_MULTIPLIER,
2443 show_cursor (entry);
2448 if (entry->blink_timeout)
2450 gtk_timeout_remove (entry->blink_timeout);
2451 entry->blink_timeout = 0;
2454 entry->cursor_visible = TRUE;
2460 gtk_entry_pend_cursor_blink (GtkEntry *entry)
2462 if (cursor_blinks (entry))
2464 if (entry->blink_timeout != 0)
2465 gtk_timeout_remove (entry->blink_timeout);
2467 entry->blink_timeout = gtk_timeout_add (get_cursor_time (entry) * CURSOR_PEND_MULTIPLIER,
2470 show_cursor (entry);
2475 gtk_item_entry_set_cursor_visible(GtkItemEntry *entry, gboolean visible)
2477 g_return_if_fail (GTK_IS_ITEM_ENTRY (entry));
2479 GTK_ENTRY(entry)->cursor_visible = visible;
2483 gtk_item_entry_get_cursor_visible(GtkItemEntry *entry)
2485 g_return_val_if_fail (GTK_IS_ITEM_ENTRY (entry), FALSE);
2487 return(GTK_ENTRY(entry)->cursor_visible);