1 #undef GDK_MULTIHEAD_SAFE
2 /* GTK - The GIMP Toolkit
3 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
22 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
23 * file for a list of people on the GTK+ Team. See the ChangeLog
24 * files for a list of changes. These files are distributed with
25 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
32 #include <pango/pango.h>
34 #include <gdk/gdkkeysyms.h>
36 #include "gtkitementry.h"
38 #define MIN_ENTRY_WIDTH 150
39 #define DRAW_TIMEOUT 20
40 #define INNER_BORDER 0
42 /* Initial size of buffer, in bytes */
45 /* Maximum size of text buffer, in bytes */
46 #define MAX_SIZE G_MAXUSHORT
53 /* GObject, GtkObject methods
55 static void gtk_item_entry_class_init (GtkItemEntryClass *klass);
56 static void gtk_item_entry_init (GtkItemEntry *entry);
57 static void gtk_item_entry_editable_init (GtkEditableClass *iface);
61 static void gtk_entry_realize (GtkWidget *widget);
62 static void gtk_entry_size_request (GtkWidget *widget,
63 GtkRequisition *requisition);
64 static void gtk_entry_size_allocate (GtkWidget *widget,
65 GtkAllocation *allocation);
66 static void gtk_entry_draw_frame (GtkWidget *widget);
67 static gint gtk_entry_expose (GtkWidget *widget,
68 GdkEventExpose *event);
69 static void gtk_entry_grab_focus (GtkWidget *widget);
70 static void gtk_entry_style_set (GtkWidget *widget,
71 GtkStyle *previous_style);
72 static void gtk_entry_direction_changed (GtkWidget *widget,
73 GtkTextDirection previous_dir);
74 static void gtk_entry_state_changed (GtkWidget *widget,
75 GtkStateType previous_state);
77 /* GtkEditable method implementations
79 static void gtk_entry_insert_text (GtkEditable *editable,
80 const gchar *new_text,
83 static void gtk_entry_delete_text (GtkEditable *editable,
87 static void gtk_entry_real_set_position (GtkEditable *editable,
89 static gint gtk_entry_get_position (GtkEditable *editable);
91 /* Default signal handlers
93 static void gtk_entry_real_insert_text (GtkEditable *editable,
94 const gchar *new_text,
97 static void gtk_entry_real_delete_text (GtkEditable *editable,
100 static void gtk_entry_move_cursor (GtkEntry *entry,
101 GtkMovementStep step,
103 gboolean extend_selection);
104 static void gtk_entry_insert_at_cursor (GtkEntry *entry,
106 static void gtk_entry_delete_from_cursor (GtkEntry *entry,
110 /* IM Context Callbacks
112 static void gtk_entry_commit_cb (GtkIMContext *context,
115 static void gtk_entry_preedit_changed_cb (GtkIMContext *context,
117 static gboolean gtk_entry_retrieve_surrounding_cb (GtkIMContext *context,
119 static gboolean gtk_entry_delete_surrounding_cb (GtkIMContext *context,
126 static void gtk_entry_enter_text (GtkEntry *entry,
128 static void gtk_entry_set_positions (GtkEntry *entry,
130 gint selection_bound);
131 static void gtk_entry_draw_text (GtkEntry *entry);
132 static void gtk_entry_draw_cursor (GtkEntry *entry,
134 static PangoLayout *gtk_entry_ensure_layout (GtkEntry *entry,
135 gboolean include_preedit);
136 static void gtk_entry_queue_draw (GtkEntry *entry);
137 static void gtk_entry_reset_im_context (GtkEntry *entry);
138 static void gtk_entry_recompute (GtkEntry *entry);
139 static void gtk_entry_get_cursor_locations (GtkEntry *entry,
143 static void gtk_entry_adjust_scroll (GtkEntry *entry);
144 static gint gtk_entry_move_visually (GtkEntry *editable,
147 static gint gtk_entry_move_logically (GtkEntry *entry,
150 static gint gtk_entry_move_forward_word (GtkEntry *entry,
152 static gint gtk_entry_move_backward_word (GtkEntry *entry,
154 static void gtk_entry_delete_whitespace (GtkEntry *entry);
155 static char * gtk_entry_get_public_chars (GtkEntry *entry,
158 static void gtk_entry_update_primary_selection (GtkEntry *entry);
159 static void gtk_entry_state_changed (GtkWidget *widget,
160 GtkStateType previous_state);
161 static void gtk_entry_check_cursor_blink (GtkEntry *entry);
162 static void gtk_entry_pend_cursor_blink (GtkEntry *entry);
163 static void get_text_area_size (GtkEntry *entry,
168 static void get_widget_window_size (GtkEntry *entry,
174 static GtkEntryClass *parent_class = NULL;
177 gtk_item_entry_get_type (void)
179 static GtkType item_entry_type = 0;
181 if (!item_entry_type)
183 static const GtkTypeInfo item_entry_info =
186 sizeof (GtkItemEntry),
187 sizeof (GtkItemEntryClass),
188 (GtkClassInitFunc) gtk_item_entry_class_init,
189 (GtkObjectInitFunc) gtk_item_entry_init,
190 /* reserved_1 */ NULL,
191 /* reserved_2 */ NULL,
192 (GtkClassInitFunc) NULL,
195 static const GInterfaceInfo item_editable_info =
197 (GInterfaceInitFunc) gtk_item_entry_editable_init, /* interface_init */
198 NULL, /* interface_finalize */
199 NULL /* interface_data */
203 item_entry_type = gtk_type_unique (GTK_TYPE_ENTRY, &item_entry_info);
205 g_type_add_interface_static (item_entry_type,
207 &item_editable_info);
211 return item_entry_type;
215 gtk_item_entry_class_init (GtkItemEntryClass *class)
217 GtkObjectClass *object_class;
218 GtkWidgetClass *widget_class;
219 GtkEntryClass *entry_class;
221 object_class = (GtkObjectClass*) class;
222 widget_class = (GtkWidgetClass*) class;
223 parent_class = gtk_type_class (GTK_TYPE_ENTRY);
224 entry_class = (GtkEntryClass *) class;
226 widget_class->realize = gtk_entry_realize;
227 widget_class->size_request = gtk_entry_size_request;
228 widget_class->size_allocate = gtk_entry_size_allocate;
229 widget_class->expose_event = gtk_entry_expose;
230 widget_class->grab_focus = gtk_entry_grab_focus;
231 widget_class->style_set = gtk_entry_style_set;
232 widget_class->direction_changed = gtk_entry_direction_changed;
233 widget_class->state_changed = gtk_entry_state_changed;
235 entry_class->move_cursor = gtk_entry_move_cursor;
236 entry_class->insert_at_cursor = gtk_entry_insert_at_cursor;
237 entry_class->delete_from_cursor = gtk_entry_delete_from_cursor;
242 gtk_item_entry_editable_init (GtkEditableClass *iface)
244 iface->do_insert_text = gtk_entry_insert_text;
245 iface->do_delete_text = gtk_entry_delete_text;
246 iface->insert_text = gtk_entry_real_insert_text;
247 iface->delete_text = gtk_entry_real_delete_text;
248 iface->set_position = gtk_entry_real_set_position;
249 iface->get_position = gtk_entry_get_position;
253 gtk_item_entry_init (GtkItemEntry *entry)
255 entry->justification = GTK_JUSTIFY_LEFT;
256 entry->text_max_size = 0;
257 GTK_ENTRY(entry)->has_frame = FALSE;
259 g_object_unref(G_OBJECT(GTK_ENTRY(entry)->im_context));
261 GTK_ENTRY(entry)->im_context = gtk_im_multicontext_new ();
263 g_signal_connect (G_OBJECT (GTK_ENTRY(entry)->im_context), "commit",
264 G_CALLBACK (gtk_entry_commit_cb), entry);
265 g_signal_connect (G_OBJECT (GTK_ENTRY(entry)->im_context), "preedit_changed",
266 G_CALLBACK (gtk_entry_preedit_changed_cb), entry);
267 g_signal_connect (G_OBJECT (GTK_ENTRY(entry)->im_context), "retrieve_surrounding",
268 G_CALLBACK (gtk_entry_retrieve_surrounding_cb), entry);
269 g_signal_connect (G_OBJECT (GTK_ENTRY(entry)->im_context), "delete_surrounding",
270 G_CALLBACK (gtk_entry_delete_surrounding_cb), entry);
275 gtk_entry_realize (GtkWidget *widget)
278 GtkEditable *editable;
279 GdkWindowAttr attributes;
280 gint attributes_mask;
282 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
283 entry = GTK_ENTRY (widget);
284 editable = GTK_EDITABLE (widget);
286 attributes.window_type = GDK_WINDOW_CHILD;
288 get_widget_window_size (entry, &attributes.x, &attributes.y, &attributes.width, &attributes.height);
290 attributes.wclass = GDK_INPUT_OUTPUT;
291 attributes.visual = gtk_widget_get_visual (widget);
292 attributes.colormap = gtk_widget_get_colormap (widget);
293 attributes.event_mask = gtk_widget_get_events (widget);
294 attributes.event_mask |= (GDK_EXPOSURE_MASK |
295 GDK_BUTTON_PRESS_MASK |
296 GDK_BUTTON_RELEASE_MASK |
297 GDK_BUTTON1_MOTION_MASK |
298 GDK_BUTTON3_MOTION_MASK |
299 GDK_POINTER_MOTION_HINT_MASK |
300 GDK_POINTER_MOTION_MASK |
301 GDK_ENTER_NOTIFY_MASK |
302 GDK_LEAVE_NOTIFY_MASK);
303 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
305 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
306 gdk_window_set_user_data (widget->window, entry);
308 get_text_area_size (entry, &attributes.x, &attributes.y, &attributes.width, &attributes.height);
310 attributes.cursor = gdk_cursor_new (GDK_XTERM);
311 attributes_mask |= GDK_WA_CURSOR;
313 entry->text_area = gdk_window_new (widget->window, &attributes, attributes_mask);
314 gdk_window_set_user_data (entry->text_area, entry);
316 gdk_cursor_unref (attributes.cursor);
318 widget->style = gtk_style_attach (widget->style, widget->window);
320 gdk_window_set_background (widget->window, &widget->style->bg[GTK_WIDGET_STATE(widget)]);
321 gdk_window_set_background (entry->text_area, &widget->style->bg[GTK_WIDGET_STATE (widget)]);
323 gdk_window_show (entry->text_area);
325 gtk_im_context_set_client_window (entry->im_context, entry->text_area);
327 gtk_entry_adjust_scroll (entry);
331 get_borders (GtkEntry *entry,
335 GtkWidget *widget = GTK_WIDGET (entry);
337 gboolean interior_focus;
339 gtk_widget_style_get (widget,
340 "interior-focus", &interior_focus,
341 "focus-line-width", &focus_width,
344 if (entry->has_frame)
346 *xborder = widget->style->xthickness;
347 *yborder = widget->style->ythickness;
357 *xborder += focus_width;
358 *yborder += focus_width;
364 gtk_entry_size_request (GtkWidget *widget,
365 GtkRequisition *requisition)
367 GtkEntry *entry = GTK_ENTRY (widget);
368 PangoFontMetrics *metrics;
369 gint xborder, yborder;
370 PangoContext *context;
372 context = gtk_widget_get_pango_context (widget);
373 metrics = pango_context_get_metrics (context,
374 widget->style->font_desc,
375 pango_context_get_language (context));
377 entry->ascent = pango_font_metrics_get_ascent (metrics);
378 entry->descent = pango_font_metrics_get_descent (metrics);
380 get_borders (entry, &xborder, &yborder);
382 xborder += INNER_BORDER;
383 yborder += INNER_BORDER;
385 if (entry->width_chars < 0)
386 requisition->width = MIN_ENTRY_WIDTH + xborder * 2;
389 gint char_width = pango_font_metrics_get_approximate_char_width (metrics);
390 requisition->width = PANGO_PIXELS (char_width) * entry->width_chars + xborder * 2;
393 requisition->height = PANGO_PIXELS (entry->ascent + entry->descent) + yborder * 2;
395 pango_font_metrics_unref (metrics);
399 get_text_area_size (GtkEntry *entry,
405 gint xborder, yborder;
406 GtkRequisition requisition;
407 GtkWidget *widget = GTK_WIDGET (entry);
409 gtk_widget_get_child_requisition (widget, &requisition);
411 get_borders (entry, &xborder, &yborder);
420 *width = GTK_WIDGET (entry)->allocation.width - xborder * 2;
423 *height = requisition.height - yborder * 2;
427 get_widget_window_size (GtkEntry *entry,
433 GtkRequisition requisition;
434 GtkWidget *widget = GTK_WIDGET (entry);
436 gtk_widget_get_child_requisition (widget, &requisition);
439 *x = widget->allocation.x;
443 if (entry->is_cell_renderer)
444 *y = widget->allocation.y;
446 *y = widget->allocation.y + (widget->allocation.height - requisition.height) / 2;
450 *width = widget->allocation.width;
454 if (entry->is_cell_renderer)
455 *height = widget->allocation.height;
457 *height = requisition.height;
462 gtk_entry_size_allocate (GtkWidget *widget,
463 GtkAllocation *allocation)
465 GtkEntry *entry = GTK_ENTRY (widget);
466 GtkItemEntry *ientry = GTK_ITEM_ENTRY (widget);
468 if(ientry->text_max_size > 0)
469 allocation->width = MIN(ientry->text_max_size, allocation->width);
471 widget->allocation = *allocation;
473 if (GTK_WIDGET_REALIZED (widget))
475 /* We call gtk_widget_get_child_requisition, since we want (for
476 * backwards compatibility reasons) the realization here to
477 * be affected by the usize of the entry, if set
479 gint x, y, width, height;
481 get_widget_window_size (entry, &x, &y, &width, &height);
483 gdk_window_move_resize (widget->window,
484 allocation->x, allocation->y, allocation->width, allocation->height);
486 get_text_area_size (entry, &x, &y, &width, &height);
488 gdk_window_move_resize (entry->text_area,
489 0, allocation->height - height, allocation->width, height);
491 gtk_entry_recompute (entry);
496 gtk_entry_draw_frame (GtkWidget *widget)
501 gtk_entry_expose (GtkWidget *widget,
502 GdkEventExpose *event)
504 GtkEntry *entry = GTK_ENTRY (widget);
506 if (widget->window == event->window)
507 gtk_entry_draw_frame (widget);
508 else if (entry->text_area == event->window)
510 gint area_width, area_height;
512 get_text_area_size (entry, NULL, NULL, &area_width, &area_height);
514 gdk_draw_rectangle (entry->text_area,
515 widget->style->bg_gc[GTK_WIDGET_STATE(widget)],
517 0, 0, area_width, area_height);
519 if ((entry->visible || entry->invisible_char != 0) &&
520 GTK_WIDGET_HAS_FOCUS (widget) &&
521 entry->selection_bound == entry->current_pos && entry->cursor_visible)
522 gtk_entry_draw_cursor (GTK_ENTRY (widget), CURSOR_STANDARD);
524 if (entry->dnd_position != -1)
525 gtk_entry_draw_cursor (GTK_ENTRY (widget), CURSOR_DND);
527 gtk_entry_draw_text (GTK_ENTRY (widget));
534 gtk_entry_grab_focus (GtkWidget *widget)
536 GtkEntry *entry = GTK_ENTRY (widget);
537 gboolean select_on_focus;
539 GTK_WIDGET_CLASS (parent_class)->grab_focus (widget);
541 g_object_get (G_OBJECT (gtk_settings_get_default ()),
542 "gtk-entry-select-on-focus",
546 if (select_on_focus && entry->editable && !entry->in_click)
547 gtk_editable_select_region (GTK_EDITABLE (widget), 0, -1);
551 gtk_entry_direction_changed (GtkWidget *widget,
552 GtkTextDirection previous_dir)
554 GtkEntry *entry = GTK_ENTRY (widget);
556 gtk_entry_recompute (entry);
558 GTK_WIDGET_CLASS (parent_class)->direction_changed (widget, previous_dir);
562 gtk_entry_state_changed (GtkWidget *widget,
563 GtkStateType previous_state)
565 GtkEntry *entry = GTK_ENTRY (widget);
567 if (GTK_WIDGET_REALIZED (widget))
569 gdk_window_set_background (widget->window, &widget->style->bg[GTK_WIDGET_STATE (widget)]);
570 gdk_window_set_background (entry->text_area, &widget->style->bg[GTK_WIDGET_STATE (widget)]);
573 if (!GTK_WIDGET_IS_SENSITIVE (widget))
575 /* Clear any selection */
576 gtk_editable_select_region (GTK_EDITABLE (entry), entry->current_pos, entry->current_pos);
579 gtk_widget_queue_clear (widget);
582 /* GtkEditable method implementations
585 gtk_entry_insert_text (GtkEditable *editable,
586 const gchar *new_text,
587 gint new_text_length,
590 GtkEntry *entry = GTK_ENTRY (editable);
594 if (*position < 0 || *position > entry->text_length)
595 *position = entry->text_length;
597 g_object_ref (G_OBJECT (editable));
599 if (new_text_length <= 63)
602 text = g_new (gchar, new_text_length + 1);
604 text[new_text_length] = '\0';
605 strncpy (text, new_text, new_text_length);
607 g_signal_emit_by_name (editable, "insert_text", text, new_text_length, position);
609 if (new_text_length > 63)
612 g_object_unref (G_OBJECT (editable));
616 gtk_entry_delete_text (GtkEditable *editable,
620 GtkEntry *entry = GTK_ENTRY (editable);
622 if (end_pos < 0 || end_pos > entry->text_length)
623 end_pos = entry->text_length;
626 if (start_pos > end_pos)
629 g_object_ref (G_OBJECT (editable));
631 g_signal_emit_by_name (editable, "delete_text", start_pos, end_pos);
633 g_object_unref (G_OBJECT (editable));
637 gtk_entry_style_set (GtkWidget *widget,
638 GtkStyle *previous_style)
640 GtkEntry *entry = GTK_ENTRY (widget);
642 if (previous_style && GTK_WIDGET_REALIZED (widget))
644 gtk_entry_recompute (entry);
646 gdk_window_set_background (widget->window, &widget->style->bg[GTK_WIDGET_STATE(widget)]);
647 gdk_window_set_background (entry->text_area, &widget->style->bg[GTK_WIDGET_STATE (widget)]);
652 gtk_entry_real_set_position (GtkEditable *editable,
655 GtkEntry *entry = GTK_ENTRY (editable);
657 if (position < 0 || position > entry->text_length)
658 position = entry->text_length;
660 if (position != entry->current_pos ||
661 position != entry->selection_bound)
663 gtk_entry_reset_im_context (entry);
664 gtk_entry_set_positions (entry, position, position);
669 gtk_entry_get_position (GtkEditable *editable)
671 return GTK_ENTRY (editable)->current_pos;
675 /* Default signal handlers
678 gtk_entry_real_insert_text (GtkEditable *editable,
679 const gchar *new_text,
680 gint new_text_length,
686 GtkEntry *entry = GTK_ENTRY (editable);
688 if (new_text_length < 0)
689 new_text_length = strlen (new_text);
691 n_chars = g_utf8_strlen (new_text, new_text_length);
692 if (entry->text_max_length > 0 && n_chars + entry->text_length > entry->text_max_length)
695 n_chars = entry->text_max_length - entry->text_length;
696 new_text_length = g_utf8_offset_to_pointer (new_text, n_chars) - new_text;
699 if (new_text_length + entry->n_bytes + 1 > entry->text_size)
701 while (new_text_length + entry->n_bytes + 1 > entry->text_size)
703 if (entry->text_size == 0)
704 entry->text_size = MIN_SIZE;
707 if (2 * (guint)entry->text_size < MAX_SIZE &&
708 2 * (guint)entry->text_size > entry->text_size)
709 entry->text_size *= 2;
712 entry->text_size = MAX_SIZE;
713 if (new_text_length > (gint)entry->text_size - (gint)entry->n_bytes - 1)
715 new_text_length = (gint)entry->text_size - (gint)entry->n_bytes - 1;
716 new_text_length = g_utf8_find_prev_char (new_text, new_text + new_text_length + 1) - new_text;
717 n_chars = g_utf8_strlen (new_text, new_text_length);
724 entry->text = g_realloc (entry->text, entry->text_size);
727 index = g_utf8_offset_to_pointer (entry->text, *position) - entry->text;
729 g_memmove (entry->text + index + new_text_length, entry->text + index, entry->n_bytes - index);
730 memcpy (entry->text + index, new_text, new_text_length);
732 entry->n_bytes += new_text_length;
733 entry->text_length += n_chars;
735 /* NUL terminate for safety and convenience */
736 entry->text[entry->n_bytes] = '\0';
738 if (entry->current_pos > *position)
739 entry->current_pos += n_chars;
741 if (entry->selection_bound > *position)
742 entry->selection_bound += n_chars;
744 *position += n_chars;
746 gtk_entry_recompute (entry);
748 g_signal_emit_by_name (editable, "changed");
749 g_object_notify (G_OBJECT (editable), "text");
753 gtk_entry_real_delete_text (GtkEditable *editable,
757 GtkEntry *entry = GTK_ENTRY (editable);
761 if (end_pos < 0 || end_pos > entry->text_length)
762 end_pos = entry->text_length;
764 if (start_pos < end_pos)
766 gint start_index = g_utf8_offset_to_pointer (entry->text, start_pos) - entry->text;
767 gint end_index = g_utf8_offset_to_pointer (entry->text, end_pos) - entry->text;
769 g_memmove (entry->text + start_index, entry->text + end_index, entry->n_bytes + 1 - end_index);
770 entry->text_length -= (end_pos - start_pos);
771 entry->n_bytes -= (end_index - start_index);
773 if (entry->current_pos > start_pos)
774 entry->current_pos -= MIN (entry->current_pos, end_pos) - start_pos;
776 if (entry->selection_bound > start_pos)
777 entry->selection_bound -= MIN (entry->selection_bound, end_pos) - start_pos;
778 /* We might have deleted the selection
780 gtk_entry_update_primary_selection (entry);
782 gtk_entry_recompute (entry);
784 g_signal_emit_by_name (editable, "changed");
785 g_object_notify (G_OBJECT (editable), "text");
789 /* Compute the X position for an offset that corresponds to the "more important
790 * cursor position for that offset. We use this when trying to guess to which
791 * end of the selection we should go to when the user hits the left or
795 get_better_cursor_x (GtkEntry *entry,
798 GtkTextDirection keymap_direction =
799 (gdk_keymap_get_direction (gdk_keymap_get_default ()) == PANGO_DIRECTION_LTR) ?
800 GTK_TEXT_DIR_LTR : GTK_TEXT_DIR_RTL;
801 GtkTextDirection widget_direction = gtk_widget_get_direction (GTK_WIDGET (entry));
802 gboolean split_cursor;
804 PangoLayout *layout = gtk_entry_ensure_layout (entry, TRUE);
805 gint index = g_utf8_offset_to_pointer (entry->text, offset) - entry->text;
807 PangoRectangle strong_pos, weak_pos;
809 g_object_get (gtk_widget_get_settings (GTK_WIDGET (entry)),
810 "gtk-split-cursor", &split_cursor,
813 pango_layout_get_cursor_pos (layout, index, &strong_pos, &weak_pos);
816 return strong_pos.x / PANGO_SCALE;
818 return (keymap_direction == widget_direction) ? strong_pos.x / PANGO_SCALE : weak_pos.x / PANGO_SCALE;
822 gtk_entry_move_cursor (GtkEntry *entry,
823 GtkMovementStep step,
825 gboolean extend_selection)
827 gint new_pos = entry->current_pos;
829 gtk_entry_reset_im_context (entry);
831 if (entry->current_pos != entry->selection_bound && !extend_selection)
833 /* If we have a current selection and aren't extending it, move to the
834 * start/or end of the selection as appropriate
838 case GTK_MOVEMENT_VISUAL_POSITIONS:
840 gint current_x = get_better_cursor_x (entry, entry->current_pos);
841 gint bound_x = get_better_cursor_x (entry, entry->selection_bound);
844 new_pos = current_x < bound_x ? entry->current_pos : entry->selection_bound;
846 new_pos = current_x > bound_x ? entry->current_pos : entry->selection_bound;
850 case GTK_MOVEMENT_LOGICAL_POSITIONS:
851 case GTK_MOVEMENT_WORDS:
853 new_pos = MIN (entry->current_pos, entry->selection_bound);
855 new_pos = MAX (entry->current_pos, entry->selection_bound);
857 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
858 case GTK_MOVEMENT_PARAGRAPH_ENDS:
859 case GTK_MOVEMENT_BUFFER_ENDS:
860 new_pos = count < 0 ? 0 : entry->text_length;
862 case GTK_MOVEMENT_DISPLAY_LINES:
863 case GTK_MOVEMENT_PARAGRAPHS:
864 case GTK_MOVEMENT_PAGES:
874 case GTK_MOVEMENT_LOGICAL_POSITIONS:
875 new_pos = gtk_entry_move_logically (entry, new_pos, count);
877 case GTK_MOVEMENT_VISUAL_POSITIONS:
878 new_pos = gtk_entry_move_visually (entry, new_pos, count);
880 case GTK_MOVEMENT_WORDS:
883 new_pos = gtk_entry_move_forward_word (entry, new_pos);
888 new_pos = gtk_entry_move_backward_word (entry, new_pos);
892 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
893 case GTK_MOVEMENT_PARAGRAPH_ENDS:
894 case GTK_MOVEMENT_BUFFER_ENDS:
895 new_pos = count < 0 ? 0 : entry->text_length;
897 case GTK_MOVEMENT_DISPLAY_LINES:
898 case GTK_MOVEMENT_PARAGRAPHS:
899 case GTK_MOVEMENT_PAGES:
906 if (extend_selection)
907 gtk_editable_select_region (GTK_EDITABLE (entry), entry->selection_bound, new_pos);
909 gtk_editable_set_position (GTK_EDITABLE (entry), new_pos);
911 gtk_entry_pend_cursor_blink (entry);
915 gtk_entry_insert_at_cursor (GtkEntry *entry,
918 GtkEditable *editable = GTK_EDITABLE (entry);
919 gint pos = entry->current_pos;
923 gtk_entry_reset_im_context (entry);
925 gtk_editable_insert_text (editable, str, -1, &pos);
926 gtk_editable_set_position (editable, pos);
931 gtk_entry_delete_from_cursor (GtkEntry *entry,
935 GtkEditable *editable = GTK_EDITABLE (entry);
936 gint start_pos = entry->current_pos;
937 gint end_pos = entry->current_pos;
939 gtk_entry_reset_im_context (entry);
941 if (!entry->editable)
944 if (entry->selection_bound != entry->current_pos)
946 gtk_editable_delete_selection (editable);
952 case GTK_DELETE_CHARS:
953 end_pos = gtk_entry_move_logically (entry, entry->current_pos, count);
954 gtk_editable_delete_text (editable, MIN (start_pos, end_pos), MAX (start_pos, end_pos));
956 case GTK_DELETE_WORDS:
959 /* Move to end of current word, or if not on a word, end of previous word */
960 end_pos = gtk_entry_move_backward_word (entry, end_pos);
961 end_pos = gtk_entry_move_forward_word (entry, end_pos);
965 /* Move to beginning of current word, or if not on a word, begining of next word */
966 start_pos = gtk_entry_move_forward_word (entry, start_pos);
967 start_pos = gtk_entry_move_backward_word (entry, start_pos);
971 case GTK_DELETE_WORD_ENDS:
974 start_pos = gtk_entry_move_backward_word (entry, start_pos);
979 end_pos = gtk_entry_move_forward_word (entry, end_pos);
982 gtk_editable_delete_text (editable, start_pos, end_pos);
984 case GTK_DELETE_DISPLAY_LINE_ENDS:
985 case GTK_DELETE_PARAGRAPH_ENDS:
987 gtk_editable_delete_text (editable, 0, entry->current_pos);
989 gtk_editable_delete_text (editable, entry->current_pos, -1);
991 case GTK_DELETE_DISPLAY_LINES:
992 case GTK_DELETE_PARAGRAPHS:
993 gtk_editable_delete_text (editable, 0, -1);
995 case GTK_DELETE_WHITESPACE:
996 gtk_entry_delete_whitespace (entry);
1000 gtk_entry_pend_cursor_blink (entry);
1003 /* IM Context Callbacks
1007 gtk_entry_commit_cb (GtkIMContext *context,
1011 gtk_entry_enter_text (entry, str);
1015 gtk_entry_preedit_changed_cb (GtkIMContext *context,
1018 gchar *preedit_string;
1021 gtk_im_context_get_preedit_string (entry->im_context,
1022 &preedit_string, NULL,
1024 entry->preedit_length = strlen (preedit_string);
1025 cursor_pos = CLAMP (cursor_pos, 0, g_utf8_strlen (preedit_string, -1));
1026 entry->preedit_cursor = cursor_pos;
1027 g_free (preedit_string);
1029 gtk_entry_recompute (entry);
1033 gtk_entry_retrieve_surrounding_cb (GtkIMContext *context,
1036 gtk_im_context_set_surrounding (context,
1039 g_utf8_offset_to_pointer (entry->text, entry->current_pos) - entry->text);
1045 gtk_entry_delete_surrounding_cb (GtkIMContext *slave,
1050 gtk_editable_delete_text (GTK_EDITABLE (entry),
1051 entry->current_pos + offset,
1052 entry->current_pos + offset + n_chars);
1058 /* Internal functions
1061 /* Used for im_commit_cb and inserting Unicode chars */
1063 gtk_entry_enter_text (GtkEntry *entry,
1066 GtkEditable *editable = GTK_EDITABLE (entry);
1069 if (gtk_editable_get_selection_bounds (editable, NULL, NULL))
1070 gtk_editable_delete_selection (editable);
1073 if (entry->overwrite_mode)
1074 gtk_entry_delete_from_cursor (entry, GTK_DELETE_CHARS, 1);
1077 tmp_pos = entry->current_pos;
1078 gtk_editable_insert_text (editable, str, strlen (str), &tmp_pos);
1079 gtk_editable_set_position (editable, tmp_pos);
1082 /* All changes to entry->current_pos and entry->selection_bound
1083 * should go through this function.
1086 gtk_entry_set_positions (GtkEntry *entry,
1088 gint selection_bound)
1090 gboolean changed = FALSE;
1092 g_object_freeze_notify (G_OBJECT (entry));
1094 if (current_pos != -1 &&
1095 entry->current_pos != current_pos)
1097 entry->current_pos = current_pos;
1100 g_object_notify (G_OBJECT (entry), "cursor_position");
1103 if (selection_bound != -1 &&
1104 entry->selection_bound != selection_bound)
1106 entry->selection_bound = selection_bound;
1109 g_object_notify (G_OBJECT (entry), "selection_bound");
1112 g_object_thaw_notify (G_OBJECT (entry));
1115 gtk_entry_recompute (entry);
1119 gtk_entry_reset_layout (GtkEntry *entry)
1121 if (entry->cached_layout)
1123 g_object_unref (G_OBJECT (entry->cached_layout));
1124 entry->cached_layout = NULL;
1129 update_im_cursor_location (GtkEntry *entry)
1133 gint strong_xoffset;
1134 gint x, y, area_width, area_height;
1136 gtk_entry_get_cursor_locations (entry, CURSOR_STANDARD, &strong_x, NULL)
1138 get_text_area_size (entry, &x, &y, &area_width, &area_height);
1140 strong_xoffset = strong_x - entry->scroll_offset;
1141 if (strong_xoffset < 0)
1145 else if (strong_xoffset > area_width)
1147 strong_xoffset = area_width;
1149 area.x = x + strong_xoffset;
1150 area.y = y + area_height;
1151 area.width = area_width;
1152 area.height = area_height;
1154 gtk_im_context_set_cursor_location (entry->im_context, &area);
1158 recompute_idle_func (gpointer data)
1162 GDK_THREADS_ENTER ();
1164 entry = GTK_ENTRY (data);
1166 gtk_entry_adjust_scroll (entry);
1167 gtk_entry_queue_draw (entry);
1169 entry->recompute_idle = FALSE;
1171 update_im_cursor_location (entry);
1173 GDK_THREADS_LEAVE ();
1179 gtk_entry_recompute (GtkEntry *entry)
1181 gtk_entry_reset_layout (entry);
1182 gtk_entry_check_cursor_blink (entry);
1185 if (!entry->recompute_idle)
1187 entry->recompute_idle = g_idle_add_full (G_PRIORITY_HIGH_IDLE + 15, /* between resize and redraw */
1188 recompute_idle_func, entry, NULL);
1193 append_char (GString *str,
1201 char_len = g_unichar_to_utf8 (ch, buf);
1206 g_string_append_len (str, buf, char_len);
1211 static PangoLayout *
1212 gtk_entry_create_layout (GtkEntry *entry,
1213 gboolean include_preedit)
1215 PangoLayout *layout = gtk_widget_create_pango_layout (GTK_WIDGET (entry), NULL);
1216 PangoAttrList *tmp_attrs = pango_attr_list_new ();
1218 gchar *preedit_string = NULL;
1219 gint preedit_length = 0;
1220 PangoAttrList *preedit_attrs = NULL;
1222 pango_layout_set_single_paragraph_mode (layout, TRUE);
1224 if (include_preedit)
1226 gtk_im_context_get_preedit_string (entry->im_context,
1227 &preedit_string, &preedit_attrs, NULL);
1228 preedit_length = entry->preedit_length;
1233 GString *tmp_string = g_string_new (NULL);
1235 gint cursor_index = g_utf8_offset_to_pointer (entry->text, entry->current_pos) - entry->text;
1239 g_string_prepend_len (tmp_string, entry->text, entry->n_bytes);
1240 g_string_insert (tmp_string, cursor_index, preedit_string);
1245 gint preedit_len_chars;
1246 gunichar invisible_char;
1248 ch_len = g_utf8_strlen (entry->text, entry->n_bytes);
1249 preedit_len_chars = g_utf8_strlen (preedit_string, -1);
1250 ch_len += preedit_len_chars;
1252 if (entry->invisible_char != 0)
1253 invisible_char = entry->invisible_char;
1255 invisible_char = ' '; /* just pick a char */
1257 append_char (tmp_string, invisible_char, ch_len);
1259 /* Fix cursor index to point to invisible char corresponding
1260 * to the preedit, fix preedit_length to be the length of
1261 * the invisible chars representing the preedit
1264 g_utf8_offset_to_pointer (tmp_string->str, entry->current_pos) -
1268 g_unichar_to_utf8 (invisible_char, NULL);
1271 pango_layout_set_text (layout, tmp_string->str, tmp_string->len);
1273 pango_attr_list_splice (tmp_attrs, preedit_attrs,
1274 cursor_index, preedit_length);
1276 g_string_free (tmp_string, TRUE);
1282 pango_layout_set_text (layout, entry->text, entry->n_bytes);
1286 GString *str = g_string_new (NULL);
1287 gunichar invisible_char;
1289 if (entry->invisible_char != 0)
1290 invisible_char = entry->invisible_char;
1292 invisible_char = ' '; /* just pick a char */
1294 append_char (str, invisible_char, entry->text_length);
1295 pango_layout_set_text (layout, str->str, str->len);
1296 g_string_free (str, TRUE);
1300 pango_layout_set_attributes (layout, tmp_attrs);
1303 g_free (preedit_string);
1305 pango_attr_list_unref (preedit_attrs);
1307 pango_attr_list_unref (tmp_attrs);
1312 static PangoLayout *
1313 gtk_entry_ensure_layout (GtkEntry *entry,
1314 gboolean include_preedit)
1316 if (entry->preedit_length > 0 &&
1317 !include_preedit != !entry->cache_includes_preedit)
1318 gtk_entry_reset_layout (entry);
1320 if (!entry->cached_layout)
1322 entry->cached_layout = gtk_entry_create_layout (entry, include_preedit);
1323 entry->cache_includes_preedit = include_preedit;
1326 return entry->cached_layout;
1330 get_layout_position (GtkEntry *entry,
1334 PangoLayout *layout;
1335 PangoRectangle logical_rect;
1336 gint area_width, area_height;
1338 PangoLayoutLine *line;
1340 layout = gtk_entry_ensure_layout (entry, TRUE);
1342 get_text_area_size (entry, NULL, NULL, &area_width, &area_height);
1344 area_height = PANGO_SCALE * (area_height);
1346 line = pango_layout_get_lines (layout)->data;
1347 pango_layout_line_get_extents (line, NULL, &logical_rect);
1349 /* Align primarily for locale's ascent/descent */
1351 y_pos = ((area_height - entry->ascent - entry->descent) / 2 +
1352 entry->ascent + logical_rect.y);
1355 /* Now see if we need to adjust to fit in actual drawn string */
1357 if (logical_rect.height > area_height)
1358 y_pos = (area_height - logical_rect.height) / 2;
1361 else if (y_pos + logical_rect.height > area_height)
1362 y_pos = area_height - logical_rect.height;
1364 y_pos = y_pos / PANGO_SCALE;
1367 *x = - entry->scroll_offset;
1374 gtk_entry_draw_text (GtkEntry *entry)
1377 PangoLayoutLine *line;
1379 if (!entry->visible && entry->invisible_char == 0)
1382 if (GTK_WIDGET_DRAWABLE (entry))
1384 PangoLayout *layout = gtk_entry_ensure_layout (entry, TRUE);
1385 gint area_width, area_height;
1388 gint start_pos, end_pos;
1390 widget = GTK_WIDGET (entry);
1392 get_layout_position (entry, &x, &y);
1394 get_text_area_size (entry, NULL, NULL, &area_width, &area_height);
1397 gdk_draw_layout (entry->text_area, widget->style->text_gc [widget->state],
1402 if (gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &start_pos, &end_pos))
1406 PangoRectangle logical_rect;
1407 const gchar *text = pango_layout_get_text (layout);
1408 gint start_index = g_utf8_offset_to_pointer (text, start_pos) - text;
1409 gint end_index = g_utf8_offset_to_pointer (text, end_pos) - text;
1410 GdkRegion *clip_region = gdk_region_new ();
1412 GdkGC *selection_gc;
1414 line = pango_layout_get_lines (layout)->data;
1416 pango_layout_line_get_x_ranges (line, start_index, end_index, &ranges, &n_ranges);
1418 pango_layout_get_extents (layout, NULL, &logical_rect);
1420 if (GTK_WIDGET_HAS_FOCUS (entry))
1422 selection_gc = widget->style->base_gc [GTK_STATE_SELECTED];
1423 text_gc = widget->style->text_gc [GTK_STATE_SELECTED];
1427 selection_gc = widget->style->base_gc [GTK_STATE_ACTIVE];
1428 text_gc = widget->style->text_gc [GTK_STATE_ACTIVE];
1431 for (i=0; i < n_ranges; i++)
1435 rect.x = INNER_BORDER - entry->scroll_offset + ranges[2*i] / PANGO_SCALE;
1437 rect.width = (ranges[2*i + 1] - ranges[2*i]) / PANGO_SCALE;
1438 rect.height = logical_rect.height / PANGO_SCALE;
1440 gdk_draw_rectangle (entry->text_area, selection_gc, TRUE,
1441 rect.x, rect.y, rect.width, rect.height);
1443 gdk_region_union_with_rect (clip_region, &rect);
1446 gdk_gc_set_clip_region (text_gc, clip_region);
1447 gdk_draw_layout (entry->text_area, text_gc,
1450 gdk_gc_set_clip_region (text_gc, NULL);
1452 gdk_region_destroy (clip_region);
1459 * From _gtk_get_insertion_cursor_gc
1462 typedef struct _CursorInfo CursorInfo;
1468 GdkGC *secondary_gc;
1472 make_cursor_gc (GtkWidget *widget,
1473 const gchar *property_name,
1476 GdkGCValues gc_values;
1477 GdkGCValuesMask gc_values_mask;
1478 GdkColor *cursor_color;
1480 gtk_widget_style_get (widget, property_name, &cursor_color, NULL);
1482 gc_values_mask = GDK_GC_FOREGROUND;
1485 gc_values.foreground = *cursor_color;
1486 gdk_color_free (cursor_color);
1489 gc_values.foreground = *fallback;
1491 gdk_rgb_find_color (widget->style->colormap, &gc_values.foreground);
1492 return gtk_gc_get (widget->style->depth, widget->style->colormap,
1493 &gc_values, gc_values_mask);
1497 _gtkextra_get_insertion_cursor_gc (GtkWidget *widget,
1498 gboolean is_primary)
1500 CursorInfo *cursor_info;
1502 cursor_info = g_object_get_data (G_OBJECT (widget->style), "gtk-style-cursor-info");
1505 cursor_info = g_new (CursorInfo, 1);
1506 g_object_set_data (G_OBJECT (widget->style), "gtk-style-cursor-info", cursor_info);
1507 cursor_info->primary_gc = NULL;
1508 cursor_info->secondary_gc = NULL;
1509 cursor_info->for_type = G_TYPE_INVALID;
1512 /* We have to keep track of the type because gtk_widget_style_get()
1513 * can return different results when called on the same property and
1514 * same style but for different widgets. :-(. That is,
1515 * GtkEntry::cursor-color = "red" in a style will modify the cursor
1516 * color for entries but not for text view.
1518 if (cursor_info->for_type != G_OBJECT_TYPE (widget))
1520 cursor_info->for_type = G_OBJECT_TYPE (widget);
1521 if (cursor_info->primary_gc)
1523 gtk_gc_release (cursor_info->primary_gc);
1524 cursor_info->primary_gc = NULL;
1526 if (cursor_info->secondary_gc)
1528 gtk_gc_release (cursor_info->secondary_gc);
1529 cursor_info->secondary_gc = NULL;
1535 if (!cursor_info->primary_gc)
1536 cursor_info->primary_gc = make_cursor_gc (widget,
1538 &widget->style->black);
1540 return g_object_ref (cursor_info->primary_gc);
1544 static GdkColor gray = { 0, 0x8888, 0x8888, 0x8888 };
1546 if (!cursor_info->secondary_gc)
1547 cursor_info->secondary_gc = make_cursor_gc (widget,
1548 "secondary-cursor-color",
1551 return g_object_ref (cursor_info->secondary_gc);
1556 * From _gtk_draw_insertion_cursor
1559 _gtkextra_draw_insertion_cursor (GtkWidget *widget,
1560 GdkDrawable *drawable,
1562 GdkRectangle *location,
1563 GtkTextDirection direction,
1564 gboolean draw_arrow)
1570 gfloat cursor_aspect_ratio;
1573 g_return_if_fail (direction != GTK_TEXT_DIR_NONE);
1575 gtk_widget_style_get (widget, "cursor-aspect-ratio", &cursor_aspect_ratio, NULL);
1577 stem_width = location->height * cursor_aspect_ratio + 1;
1578 arrow_width = stem_width + 1;
1580 /* put (stem_width % 2) on the proper side of the cursor */
1581 if (direction == GTK_TEXT_DIR_LTR)
1582 offset = stem_width / 2;
1584 offset = stem_width - stem_width / 2;
1586 for (i = 0; i < stem_width; i++)
1587 gdk_draw_line (drawable, gc,
1588 location->x + i - offset, location->y,
1589 location->x + i - offset, location->y + location->height - 1);
1593 if (direction == GTK_TEXT_DIR_RTL)
1595 x = location->x - offset - 1;
1596 y = location->y + location->height - arrow_width * 2 - arrow_width + 1;
1598 for (i = 0; i < arrow_width; i++)
1600 gdk_draw_line (drawable, gc,
1602 x, y + 2 * arrow_width - i - 1);
1606 else if (direction == GTK_TEXT_DIR_LTR)
1608 x = location->x + stem_width - offset;
1609 y = location->y + location->height - arrow_width * 2 - arrow_width + 1;
1611 for (i = 0; i < arrow_width; i++)
1613 gdk_draw_line (drawable, gc,
1615 x, y + 2 * arrow_width - i - 1);
1623 gtk_entry_draw_cursor (GtkEntry *entry,
1626 GtkTextDirection keymap_direction =
1627 (gdk_keymap_get_direction (gdk_keymap_get_default ()) == PANGO_DIRECTION_LTR) ?
1628 GTK_TEXT_DIR_LTR : GTK_TEXT_DIR_RTL;
1629 GtkTextDirection widget_direction = gtk_widget_get_direction (GTK_WIDGET (entry));
1631 if (GTK_WIDGET_DRAWABLE (entry) && GTK_ENTRY(entry)->cursor_visible)
1633 GtkWidget *widget = GTK_WIDGET (entry);
1634 GdkRectangle cursor_location;
1635 gboolean split_cursor;
1637 gint xoffset = INNER_BORDER - entry->scroll_offset;
1638 gint strong_x, weak_x;
1639 gint text_area_height;
1640 GtkTextDirection dir1 = GTK_TEXT_DIR_NONE;
1641 GtkTextDirection dir2 = GTK_TEXT_DIR_NONE;
1646 gdk_window_get_size (entry->text_area, NULL, &text_area_height);
1648 gtk_entry_get_cursor_locations (entry, type, &strong_x, &weak_x);
1650 g_object_get (gtk_widget_get_settings (widget),
1651 "gtk-split-cursor", &split_cursor,
1654 dir1 = widget_direction;
1660 if (weak_x != strong_x)
1662 dir2 = (widget_direction == GTK_TEXT_DIR_LTR) ? GTK_TEXT_DIR_RTL : GTK_TEXT_DIR_LTR;
1668 if (keymap_direction == widget_direction)
1674 cursor_location.x = xoffset + x1;
1675 cursor_location.y = INNER_BORDER;
1676 cursor_location.width = 0;
1677 cursor_location.height = text_area_height - 2 * INNER_BORDER ;
1679 gc = _gtkextra_get_insertion_cursor_gc (widget, TRUE);
1680 _gtkextra_draw_insertion_cursor (widget, entry->text_area, gc,
1681 &cursor_location, dir1,
1682 dir2 != GTK_TEXT_DIR_NONE);
1683 g_object_unref (gc);
1685 if (dir2 != GTK_TEXT_DIR_NONE)
1687 cursor_location.x = xoffset + x2;
1688 gc = _gtkextra_get_insertion_cursor_gc (widget, FALSE);
1689 _gtkextra_draw_insertion_cursor (widget, entry->text_area, gc,
1690 &cursor_location, dir2,
1692 g_object_unref (gc);
1698 gtk_entry_queue_draw (GtkEntry *entry)
1700 if (GTK_WIDGET_REALIZED (entry))
1701 gdk_window_invalidate_rect (entry->text_area, NULL, FALSE);
1705 gtk_entry_reset_im_context (GtkEntry *entry)
1707 if (entry->need_im_reset)
1709 entry->need_im_reset = 0;
1710 gtk_im_context_reset (entry->im_context);
1715 gtk_entry_get_cursor_locations (GtkEntry *entry,
1720 PangoLayout *layout = gtk_entry_ensure_layout (entry, TRUE);
1722 PangoRectangle strong_pos, weak_pos;
1725 if (type == CURSOR_STANDARD)
1727 text = pango_layout_get_text (layout);
1728 index = g_utf8_offset_to_pointer (text, entry->current_pos + entry->preedit_cursor) - text;
1730 else /* type == CURSOR_DND */
1732 index = g_utf8_offset_to_pointer (entry->text, entry->dnd_position) - entry->text;
1733 if (entry->dnd_position > entry->current_pos)
1734 index += entry->preedit_length;
1737 pango_layout_get_cursor_pos (layout, index, &strong_pos, &weak_pos);
1740 *strong_x = strong_pos.x / PANGO_SCALE;
1743 *weak_x = weak_pos.x / PANGO_SCALE;
1747 gtk_entry_adjust_scroll (GtkEntry *entry)
1749 gint min_offset, max_offset;
1750 gint text_area_width;
1751 gint strong_x, weak_x;
1752 PangoLayout *layout;
1753 PangoLayoutLine *line;
1754 PangoRectangle logical_rect;
1755 GtkItemEntry *item_entry;
1758 if (!GTK_WIDGET_REALIZED (entry))
1761 item_entry = GTK_ITEM_ENTRY(entry);
1763 gdk_window_get_size (entry->text_area, &text_area_width, NULL);
1764 text_area_width -= 2 * INNER_BORDER;
1766 layout = gtk_entry_ensure_layout (entry, TRUE);
1767 line = pango_layout_get_lines (layout)->data;
1769 pango_layout_line_get_extents (line, NULL, &logical_rect);
1770 text_width = logical_rect.width / PANGO_SCALE + 2; /* 2 for cursor */
1772 gtk_entry_get_cursor_locations (entry, CURSOR_STANDARD, &strong_x, &weak_x);
1774 /* Display as much text as we can */
1776 if (gtk_widget_get_direction (GTK_WIDGET (entry)) == GTK_TEXT_DIR_LTR)
1778 entry->scroll_offset = 0;
1779 switch(item_entry->justification){
1781 case GTK_JUSTIFY_FILL:
1782 case GTK_JUSTIFY_LEFT:
1784 /* LEFT JUSTIFICATION */
1786 strong_x -= entry->scroll_offset;
1788 entry->scroll_offset += strong_x;
1789 else if (strong_x > text_area_width){
1790 if(item_entry->text_max_size != 0 &&
1791 text_area_width + 2 <= item_entry->text_max_size){
1792 GtkAllocation allocation;
1793 allocation = GTK_WIDGET(entry)->allocation;
1794 allocation.width += text_width - text_area_width;
1795 entry->scroll_offset = 0;
1796 gtk_entry_size_allocate(GTK_WIDGET(entry), &allocation);
1798 entry->scroll_offset += (strong_x - text_area_width) + 1;
1804 case GTK_JUSTIFY_RIGHT:
1806 /* RIGHT JUSTIFICATION FOR NUMBERS */
1809 entry->scroll_offset= -(text_area_width - text_width) + 1;
1810 if(entry->scroll_offset > 0){
1811 if(item_entry->text_max_size != 0 &&
1812 text_area_width + 2 <= item_entry->text_max_size){
1813 GtkAllocation allocation;
1814 allocation = GTK_WIDGET(entry)->allocation;
1815 allocation.x -= text_width - text_area_width;
1816 allocation.width += text_width - text_area_width;
1817 entry->scroll_offset = 0;
1818 gtk_entry_size_allocate(GTK_WIDGET(entry), &allocation);
1822 entry->scroll_offset= -(text_area_width - strong_x) + 1;
1823 if(entry->scroll_offset < 0) entry->scroll_offset = 0;
1828 entry->scroll_offset=0;
1831 case GTK_JUSTIFY_CENTER:
1835 entry->scroll_offset= -(text_area_width - text_width)/2;
1836 if(entry->scroll_offset > 0){
1837 if(item_entry->text_max_size != 0 &&
1838 text_area_width+1<=item_entry->text_max_size){
1839 GtkAllocation allocation;
1840 allocation = GTK_WIDGET(entry)->allocation;
1841 allocation.x += (text_area_width/2 - text_width/2);
1842 allocation.width += text_width - text_area_width;
1843 entry->scroll_offset = 0;
1844 gtk_entry_size_allocate(GTK_WIDGET(entry), &allocation);
1848 entry->scroll_offset= -(text_area_width - strong_x) + 1;
1849 if(entry->scroll_offset < 0) entry->scroll_offset = 0;
1854 entry->scroll_offset=0;
1863 max_offset = text_width - text_area_width;
1864 min_offset = MIN (0, max_offset);
1865 entry->scroll_offset = CLAMP (entry->scroll_offset, min_offset, max_offset);
1868 g_object_notify (G_OBJECT (entry), "scroll_offset");
1872 gtk_entry_move_visually (GtkEntry *entry,
1877 PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
1880 text = pango_layout_get_text (layout);
1882 index = g_utf8_offset_to_pointer (text, start) - text;
1886 int new_index, new_trailing;
1887 gboolean split_cursor;
1890 g_object_get (gtk_widget_get_settings (GTK_WIDGET (entry)),
1891 "gtk-split-cursor", &split_cursor,
1898 GtkTextDirection keymap_direction =
1899 (gdk_keymap_get_direction (gdk_keymap_get_default ()) == PANGO_DIRECTION_LTR) ?
1900 GTK_TEXT_DIR_LTR : GTK_TEXT_DIR_RTL;
1902 strong = keymap_direction == gtk_widget_get_direction (GTK_WIDGET (entry));
1907 pango_layout_move_cursor_visually (layout, strong, index, 0, 1, &new_index, &new_trailing);
1912 pango_layout_move_cursor_visually (layout, strong, index, 0, -1, &new_index, &new_trailing);
1916 if (new_index < 0 || new_index == G_MAXINT)
1921 while (new_trailing--)
1922 index = g_utf8_next_char (entry->text + new_index) - entry->text;
1925 return g_utf8_pointer_to_offset (text, text + index);
1929 gtk_entry_move_logically (GtkEntry *entry,
1933 gint new_pos = start;
1935 /* Prevent any leak of information */
1936 if (!entry->visible)
1938 new_pos = CLAMP (start + count, 0, entry->text_length);
1940 else if (entry->text)
1942 PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
1943 PangoLogAttr *log_attrs;
1946 pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
1948 while (count > 0 && new_pos < entry->text_length)
1952 while (new_pos < entry->text_length && !log_attrs[new_pos].is_cursor_position);
1956 while (count < 0 && new_pos > 0)
1960 while (new_pos > 0 && !log_attrs[new_pos].is_cursor_position);
1972 gtk_entry_move_forward_word (GtkEntry *entry,
1975 gint new_pos = start;
1977 /* Prevent any leak of information */
1978 if (!entry->visible)
1980 new_pos = entry->text_length;
1982 else if (entry->text && (new_pos < entry->text_length))
1984 PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
1985 PangoLogAttr *log_attrs;
1988 pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
1990 /* Find the next word end */
1992 while (new_pos < n_attrs && !log_attrs[new_pos].is_word_end)
2003 gtk_entry_move_backward_word (GtkEntry *entry,
2006 gint new_pos = start;
2008 /* Prevent any leak of information */
2009 if (!entry->visible)
2013 else if (entry->text && start > 0)
2015 PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
2016 PangoLogAttr *log_attrs;
2019 pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
2021 new_pos = start - 1;
2023 /* Find the previous word beginning */
2024 while (new_pos > 0 && !log_attrs[new_pos].is_word_start)
2034 gtk_entry_delete_whitespace (GtkEntry *entry)
2036 PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
2037 PangoLogAttr *log_attrs;
2041 pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
2043 start = end = entry->current_pos;
2045 while (start > 0 && log_attrs[start-1].is_white)
2048 while (end < n_attrs && log_attrs[end].is_white)
2054 gtk_editable_delete_text (GTK_EDITABLE (entry), start, end);
2059 * Like gtk_editable_get_chars, but if the editable is not
2060 * visible, return asterisks; also convert result to UTF-8.
2063 gtk_entry_get_public_chars (GtkEntry *entry,
2068 end = entry->text_length;
2071 return gtk_editable_get_chars (GTK_EDITABLE (entry), start, end);
2076 gint n_chars = end - start;
2078 str = g_malloc (n_chars + 1);
2079 for (i = 0; i < n_chars; i++)
2089 primary_get_cb (GtkClipboard *clipboard,
2090 GtkSelectionData *selection_data,
2094 GtkEntry *entry = GTK_ENTRY (data);
2097 if (gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &start, &end))
2099 gchar *str = gtk_entry_get_public_chars (entry, start, end);
2100 gtk_selection_data_set_text (selection_data, str, -1);
2106 primary_clear_cb (GtkClipboard *clipboard,
2109 GtkEntry *entry = GTK_ENTRY (data);
2111 gtk_editable_select_region (GTK_EDITABLE (entry), entry->current_pos, entry->current_pos);
2115 gtk_entry_update_primary_selection (GtkEntry *entry)
2117 static const GtkTargetEntry targets[] = {
2118 { "UTF8_STRING", 0, 0 },
2121 { "COMPOUND_TEXT", 0, 0 }
2124 GtkClipboard *clipboard = gtk_clipboard_get (GDK_SELECTION_PRIMARY);
2127 if (gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &start, &end))
2129 if (!gtk_clipboard_set_with_owner (clipboard, targets, G_N_ELEMENTS (targets),
2130 primary_get_cb, primary_clear_cb, G_OBJECT (entry)))
2131 primary_clear_cb (clipboard, entry);
2135 if (gtk_clipboard_get_owner (clipboard) == G_OBJECT (entry))
2136 gtk_clipboard_clear (clipboard);
2144 gtk_item_entry_new (void)
2148 widget = GTK_WIDGET (gtk_type_new (GTK_TYPE_ITEM_ENTRY));
2153 gtk_item_entry_new_with_max_length (gint max)
2155 GtkItemEntry *entry;
2157 entry = gtk_type_new (GTK_TYPE_ITEM_ENTRY);
2158 gtk_entry_set_max_length(GTK_ENTRY(entry), max);
2160 return GTK_WIDGET (entry);
2164 gtk_item_entry_set_text (GtkItemEntry *entry,
2166 GtkJustification justification)
2170 g_return_if_fail (GTK_IS_ITEM_ENTRY (entry));
2171 g_return_if_fail (text != NULL);
2173 entry->justification = justification;
2175 /* Actually setting the text will affect the cursor and selection;
2176 * if the contents don't actually change, this will look odd to the user.
2178 if (strcmp (GTK_ENTRY(entry)->text, text) == 0)
2181 if (GTK_ENTRY(entry)->recompute_idle){
2182 g_source_remove (GTK_ENTRY(entry)->recompute_idle);
2183 GTK_ENTRY(entry)->recompute_idle = 0;
2185 if (GTK_ENTRY(entry)->blink_timeout){
2186 g_source_remove (GTK_ENTRY(entry)->blink_timeout);
2187 GTK_ENTRY(entry)->blink_timeout = 0;
2190 gtk_editable_delete_text (GTK_EDITABLE (entry), 0, -1);
2193 gtk_editable_insert_text (GTK_EDITABLE (entry), text, strlen (text), &tmp_pos);
2197 * gtk_entry_get_layout_offsets:
2198 * @entry: a #GtkEntry
2199 * @x: location to store X offset of layout, or %NULL
2200 * @y: location to store Y offset of layout, or %NULL
2203 * Obtains the position of the #PangoLayout used to render text
2204 * in the entry, in widget coordinates. Useful if you want to line
2205 * up the text in an entry with some other text, e.g. when using the
2206 * entry to implement editable cells in a sheet widget.
2208 * Also useful to convert mouse events into coordinates inside the
2209 * #PangoLayout, e.g. to take some action if some part of the entry text
2212 * Note that as the user scrolls around in the entry the offsets will
2213 * change; you'll need to connect to the "notify::scroll_offset"
2214 * signal to track this. Remember when using the #PangoLayout
2215 * functions you need to convert to and from pixels using
2216 * PANGO_PIXELS() or #PANGO_SCALE.
2218 * Keep in mind that the layout text may contain a preedit string, so
2219 * gtk_entry_layout_index_to_text_index() and
2220 * gtk_entry_text_index_to_layout_index() are needed to convert byte
2221 * indices in the layout to byte indices in the entry contents.
2225 gtk_item_entry_get_layout_offsets (GtkItemEntry *entry,
2229 gint text_area_x, text_area_y;
2231 g_return_if_fail (GTK_IS_ITEM_ENTRY (entry));
2233 /* this gets coords relative to text area */
2234 get_layout_position (GTK_ENTRY(entry), x, y);
2236 /* convert to widget coords */
2237 get_text_area_size (GTK_ENTRY(entry), &text_area_x, &text_area_y, NULL, NULL);
2247 gtk_item_entry_set_justification(GtkItemEntry *entry, GtkJustification just)
2249 g_return_if_fail (GTK_IS_ITEM_ENTRY (entry));
2251 entry->justification = just;
2255 /* We display the cursor when
2257 * - the selection is empty, AND
2258 * - the widget has focus
2261 #define CURSOR_ON_MULTIPLIER 0.66
2262 #define CURSOR_OFF_MULTIPLIER 0.34
2263 #define CURSOR_PEND_MULTIPLIER 1.0
2266 cursor_blinks (GtkEntry *entry)
2268 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (entry));
2271 if (GTK_WIDGET_HAS_FOCUS (entry) &&
2272 entry->selection_bound == entry->current_pos)
2274 g_object_get (G_OBJECT (settings), "gtk-cursor-blink", &blink, NULL);
2282 get_cursor_time (GtkEntry *entry)
2284 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (entry));
2287 g_object_get (G_OBJECT (settings), "gtk-cursor-blink-time", &time, NULL);
2293 show_cursor (GtkEntry *entry)
2295 if (!entry->cursor_visible)
2297 entry->cursor_visible = TRUE;
2299 if (GTK_WIDGET_HAS_FOCUS (entry) && entry->selection_bound == entry->current_pos)
2300 gtk_widget_queue_draw (GTK_WIDGET (entry));
2305 hide_cursor (GtkEntry *entry)
2307 if (entry->cursor_visible)
2309 entry->cursor_visible = FALSE;
2311 if (GTK_WIDGET_HAS_FOCUS (entry) && entry->selection_bound == entry->current_pos)
2312 gtk_widget_queue_draw (GTK_WIDGET (entry));
2320 blink_cb (gpointer data)
2324 GDK_THREADS_ENTER ();
2326 entry = GTK_ENTRY (data);
2328 g_assert (GTK_WIDGET_HAS_FOCUS (entry));
2329 g_assert (entry->selection_bound == entry->current_pos);
2331 if (entry->cursor_visible)
2333 hide_cursor (entry);
2334 entry->blink_timeout = gtk_timeout_add (get_cursor_time (entry) * CURSOR_OFF_MULTIPLIER,
2340 show_cursor (entry);
2341 entry->blink_timeout = gtk_timeout_add (get_cursor_time (entry) * CURSOR_ON_MULTIPLIER,
2346 GDK_THREADS_LEAVE ();
2348 /* Remove ourselves */
2353 gtk_entry_check_cursor_blink (GtkEntry *entry)
2355 if (cursor_blinks (entry))
2357 if (!entry->blink_timeout)
2359 entry->blink_timeout = gtk_timeout_add (get_cursor_time (entry) * CURSOR_ON_MULTIPLIER,
2362 show_cursor (entry);
2367 if (entry->blink_timeout)
2369 gtk_timeout_remove (entry->blink_timeout);
2370 entry->blink_timeout = 0;
2373 entry->cursor_visible = TRUE;
2379 gtk_entry_pend_cursor_blink (GtkEntry *entry)
2381 if (cursor_blinks (entry))
2383 if (entry->blink_timeout != 0)
2384 gtk_timeout_remove (entry->blink_timeout);
2386 entry->blink_timeout = gtk_timeout_add (get_cursor_time (entry) * CURSOR_PEND_MULTIPLIER,
2389 show_cursor (entry);
2394 gtk_item_entry_set_cursor_visible(GtkItemEntry *entry, gboolean visible)
2396 g_return_if_fail (GTK_IS_ITEM_ENTRY (entry));
2398 GTK_ENTRY(entry)->cursor_visible = visible;
2402 gtk_item_entry_get_cursor_visible(GtkItemEntry *entry)
2404 g_return_val_if_fail (GTK_IS_ITEM_ENTRY (entry), FALSE);
2406 return(GTK_ENTRY(entry)->cursor_visible);