1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
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/.
31 #include <pango/pango.h>
33 #include "gdk/gdkkeysyms.h"
34 #include "gtkbindings.h"
35 #include "gtkcelleditable.h"
36 #include "gtkclipboard.h"
39 #include "gtkimagemenuitem.h"
40 #include "gtkimcontextsimple.h"
41 #include "gtkimmulticontext.h"
44 #include "gtkmarshalers.h"
46 #include "gtkmenuitem.h"
47 #include "gtkseparatormenuitem.h"
48 #include "gtkselection.h"
49 #include "gtksettings.h"
51 #include "gtktextutil.h"
52 #include "gtkwindow.h"
53 #include "gtktreeview.h"
54 #include "gtktreeselection.h"
55 #include "gtkprivate.h"
56 #include "gtkentryprivate.h"
57 #include "gtkcelllayout.h"
60 #define GTK_ENTRY_COMPLETION_KEY "gtk-entry-completion-key"
62 #define MIN_ENTRY_WIDTH 150
63 #define DRAW_TIMEOUT 20
64 #define COMPLETION_TIMEOUT 300
65 #define PASSWORD_HINT_MAX 8
67 /* Initial size of buffer, in bytes */
70 /* Maximum size of text buffer, in bytes */
71 #define MAX_SIZE G_MAXUSHORT
73 static const GtkBorder default_inner_border = { 2, 2, 2, 2 };
74 static GQuark quark_inner_border = 0;
75 static GQuark quark_password_hint = 0;
76 static GQuark quark_cursor_hadjustment = 0;
78 typedef struct _GtkEntryPrivate GtkEntryPrivate;
80 #define GTK_ENTRY_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_ENTRY, GtkEntryPrivate))
82 struct _GtkEntryPrivate
86 guint blink_time; /* time in msec the cursor has blinked since last user event */
87 guint interior_focus : 1;
88 guint real_changed : 1;
89 guint change_count : 8;
92 GtkShadowType shadow_type;
95 typedef struct _GtkEntryPasswordHint GtkEntryPasswordHint;
97 struct _GtkEntryPasswordHint
99 gchar password_hint[PASSWORD_HINT_MAX];
100 guint password_hint_timeout_id;
101 gint password_hint_length;
102 gint password_hint_position;
121 PROP_CURSOR_POSITION,
122 PROP_SELECTION_BOUND,
129 PROP_ACTIVATES_DEFAULT,
134 PROP_TRUNCATE_MULTILINE,
138 static guint signals[LAST_SIGNAL] = { 0 };
145 /* GObject, GtkObject methods
147 static void gtk_entry_editable_init (GtkEditableClass *iface);
148 static void gtk_entry_cell_editable_init (GtkCellEditableIface *iface);
149 static void gtk_entry_set_property (GObject *object,
153 static void gtk_entry_get_property (GObject *object,
157 static void gtk_entry_finalize (GObject *object);
158 static void gtk_entry_destroy (GtkObject *object);
162 static void gtk_entry_realize (GtkWidget *widget);
163 static void gtk_entry_unrealize (GtkWidget *widget);
164 static void gtk_entry_size_request (GtkWidget *widget,
165 GtkRequisition *requisition);
166 static void gtk_entry_size_allocate (GtkWidget *widget,
167 GtkAllocation *allocation);
168 static void gtk_entry_draw_frame (GtkWidget *widget,
170 static gint gtk_entry_expose (GtkWidget *widget,
171 GdkEventExpose *event);
172 static gint gtk_entry_button_press (GtkWidget *widget,
173 GdkEventButton *event);
174 static gint gtk_entry_button_release (GtkWidget *widget,
175 GdkEventButton *event);
176 static gint gtk_entry_motion_notify (GtkWidget *widget,
177 GdkEventMotion *event);
178 static gint gtk_entry_key_press (GtkWidget *widget,
180 static gint gtk_entry_key_release (GtkWidget *widget,
182 static gint gtk_entry_focus_in (GtkWidget *widget,
183 GdkEventFocus *event);
184 static gint gtk_entry_focus_out (GtkWidget *widget,
185 GdkEventFocus *event);
186 static void gtk_entry_grab_focus (GtkWidget *widget);
187 static void gtk_entry_style_set (GtkWidget *widget,
188 GtkStyle *previous_style);
189 static void gtk_entry_direction_changed (GtkWidget *widget,
190 GtkTextDirection previous_dir);
191 static void gtk_entry_state_changed (GtkWidget *widget,
192 GtkStateType previous_state);
193 static void gtk_entry_screen_changed (GtkWidget *widget,
194 GdkScreen *old_screen);
196 static gboolean gtk_entry_drag_drop (GtkWidget *widget,
197 GdkDragContext *context,
201 static gboolean gtk_entry_drag_motion (GtkWidget *widget,
202 GdkDragContext *context,
206 static void gtk_entry_drag_leave (GtkWidget *widget,
207 GdkDragContext *context,
209 static void gtk_entry_drag_data_received (GtkWidget *widget,
210 GdkDragContext *context,
213 GtkSelectionData *selection_data,
216 static void gtk_entry_drag_data_get (GtkWidget *widget,
217 GdkDragContext *context,
218 GtkSelectionData *selection_data,
221 static void gtk_entry_drag_data_delete (GtkWidget *widget,
222 GdkDragContext *context);
224 /* GtkEditable method implementations
226 static void gtk_entry_insert_text (GtkEditable *editable,
227 const gchar *new_text,
228 gint new_text_length,
230 static void gtk_entry_delete_text (GtkEditable *editable,
233 static gchar * gtk_entry_get_chars (GtkEditable *editable,
236 static void gtk_entry_real_set_position (GtkEditable *editable,
238 static gint gtk_entry_get_position (GtkEditable *editable);
239 static void gtk_entry_set_selection_bounds (GtkEditable *editable,
242 static gboolean gtk_entry_get_selection_bounds (GtkEditable *editable,
246 /* GtkCellEditable method implementations
248 static void gtk_entry_start_editing (GtkCellEditable *cell_editable,
251 /* Default signal handlers
253 static void gtk_entry_real_insert_text (GtkEditable *editable,
254 const gchar *new_text,
255 gint new_text_length,
257 static void gtk_entry_real_delete_text (GtkEditable *editable,
260 static void gtk_entry_move_cursor (GtkEntry *entry,
261 GtkMovementStep step,
263 gboolean extend_selection);
264 static void gtk_entry_insert_at_cursor (GtkEntry *entry,
266 static void gtk_entry_delete_from_cursor (GtkEntry *entry,
269 static void gtk_entry_backspace (GtkEntry *entry);
270 static void gtk_entry_cut_clipboard (GtkEntry *entry);
271 static void gtk_entry_copy_clipboard (GtkEntry *entry);
272 static void gtk_entry_paste_clipboard (GtkEntry *entry);
273 static void gtk_entry_toggle_overwrite (GtkEntry *entry);
274 static void gtk_entry_select_all (GtkEntry *entry);
275 static void gtk_entry_real_activate (GtkEntry *entry);
276 static gboolean gtk_entry_popup_menu (GtkWidget *widget);
278 static void gtk_entry_keymap_direction_changed (GdkKeymap *keymap,
280 /* IM Context Callbacks
282 static void gtk_entry_commit_cb (GtkIMContext *context,
285 static void gtk_entry_preedit_changed_cb (GtkIMContext *context,
287 static gboolean gtk_entry_retrieve_surrounding_cb (GtkIMContext *context,
289 static gboolean gtk_entry_delete_surrounding_cb (GtkIMContext *context,
296 static void gtk_entry_enter_text (GtkEntry *entry,
298 static void gtk_entry_set_positions (GtkEntry *entry,
300 gint selection_bound);
301 static void gtk_entry_draw_text (GtkEntry *entry);
302 static void gtk_entry_draw_cursor (GtkEntry *entry,
304 static PangoLayout *gtk_entry_ensure_layout (GtkEntry *entry,
305 gboolean include_preedit);
306 static void gtk_entry_reset_layout (GtkEntry *entry);
307 static void gtk_entry_queue_draw (GtkEntry *entry);
308 static void gtk_entry_recompute (GtkEntry *entry);
309 static gint gtk_entry_find_position (GtkEntry *entry,
311 static void gtk_entry_get_cursor_locations (GtkEntry *entry,
315 static void gtk_entry_adjust_scroll (GtkEntry *entry);
316 static gint gtk_entry_move_visually (GtkEntry *editable,
319 static gint gtk_entry_move_logically (GtkEntry *entry,
322 static gint gtk_entry_move_forward_word (GtkEntry *entry,
324 gboolean allow_whitespace);
325 static gint gtk_entry_move_backward_word (GtkEntry *entry,
327 gboolean allow_whitespace);
328 static void gtk_entry_delete_whitespace (GtkEntry *entry);
329 static void gtk_entry_select_word (GtkEntry *entry);
330 static void gtk_entry_select_line (GtkEntry *entry);
331 static char * gtk_entry_get_public_chars (GtkEntry *entry,
334 static void gtk_entry_paste (GtkEntry *entry,
336 static void gtk_entry_update_primary_selection (GtkEntry *entry);
337 static void gtk_entry_do_popup (GtkEntry *entry,
338 GdkEventButton *event);
339 static gboolean gtk_entry_mnemonic_activate (GtkWidget *widget,
340 gboolean group_cycling);
341 static void gtk_entry_state_changed (GtkWidget *widget,
342 GtkStateType previous_state);
343 static void gtk_entry_check_cursor_blink (GtkEntry *entry);
344 static void gtk_entry_pend_cursor_blink (GtkEntry *entry);
345 static void gtk_entry_reset_blink_time (GtkEntry *entry);
346 static void get_text_area_size (GtkEntry *entry,
351 static void get_widget_window_size (GtkEntry *entry,
356 static void gtk_entry_move_adjustments (GtkEntry *entry);
359 static gint gtk_entry_completion_timeout (gpointer data);
360 static gboolean gtk_entry_completion_key_press (GtkWidget *widget,
363 static void gtk_entry_completion_changed (GtkWidget *entry,
365 static gboolean check_completion_callback (GtkEntryCompletion *completion);
366 static void clear_completion_callback (GtkEntry *entry,
368 static gboolean accept_completion_callback (GtkEntry *entry);
369 static void completion_insert_text_callback (GtkEntry *entry,
373 GtkEntryCompletion *completion);
374 static void completion_changed (GtkEntryCompletion *completion,
377 static void disconnect_completion_signals (GtkEntry *entry,
378 GtkEntryCompletion *completion);
379 static void connect_completion_signals (GtkEntry *entry,
380 GtkEntryCompletion *completion);
382 static void begin_change (GtkEntry *entry);
383 static void end_change (GtkEntry *entry);
384 static void emit_changed (GtkEntry *entry);
386 G_DEFINE_TYPE_WITH_CODE (GtkEntry, gtk_entry, GTK_TYPE_WIDGET,
387 G_IMPLEMENT_INTERFACE (GTK_TYPE_EDITABLE,
388 gtk_entry_editable_init)
389 G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_EDITABLE,
390 gtk_entry_cell_editable_init))
393 add_move_binding (GtkBindingSet *binding_set,
396 GtkMovementStep step,
399 g_return_if_fail ((modmask & GDK_SHIFT_MASK) == 0);
401 gtk_binding_entry_add_signal (binding_set, keyval, modmask,
405 G_TYPE_BOOLEAN, FALSE);
407 /* Selection-extending version */
408 gtk_binding_entry_add_signal (binding_set, keyval, modmask | GDK_SHIFT_MASK,
412 G_TYPE_BOOLEAN, TRUE);
416 gtk_entry_class_init (GtkEntryClass *class)
418 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
419 GtkWidgetClass *widget_class;
420 GtkObjectClass *gtk_object_class;
421 GtkBindingSet *binding_set;
423 widget_class = (GtkWidgetClass*) class;
424 gtk_object_class = (GtkObjectClass *)class;
426 gobject_class->finalize = gtk_entry_finalize;
427 gobject_class->set_property = gtk_entry_set_property;
428 gobject_class->get_property = gtk_entry_get_property;
430 widget_class->realize = gtk_entry_realize;
431 widget_class->unrealize = gtk_entry_unrealize;
432 widget_class->size_request = gtk_entry_size_request;
433 widget_class->size_allocate = gtk_entry_size_allocate;
434 widget_class->expose_event = gtk_entry_expose;
435 widget_class->button_press_event = gtk_entry_button_press;
436 widget_class->button_release_event = gtk_entry_button_release;
437 widget_class->motion_notify_event = gtk_entry_motion_notify;
438 widget_class->key_press_event = gtk_entry_key_press;
439 widget_class->key_release_event = gtk_entry_key_release;
440 widget_class->focus_in_event = gtk_entry_focus_in;
441 widget_class->focus_out_event = gtk_entry_focus_out;
442 widget_class->grab_focus = gtk_entry_grab_focus;
443 widget_class->style_set = gtk_entry_style_set;
444 widget_class->direction_changed = gtk_entry_direction_changed;
445 widget_class->state_changed = gtk_entry_state_changed;
446 widget_class->screen_changed = gtk_entry_screen_changed;
447 widget_class->mnemonic_activate = gtk_entry_mnemonic_activate;
449 widget_class->drag_drop = gtk_entry_drag_drop;
450 widget_class->drag_motion = gtk_entry_drag_motion;
451 widget_class->drag_leave = gtk_entry_drag_leave;
452 widget_class->drag_data_received = gtk_entry_drag_data_received;
453 widget_class->drag_data_get = gtk_entry_drag_data_get;
454 widget_class->drag_data_delete = gtk_entry_drag_data_delete;
456 widget_class->popup_menu = gtk_entry_popup_menu;
458 gtk_object_class->destroy = gtk_entry_destroy;
460 class->move_cursor = gtk_entry_move_cursor;
461 class->insert_at_cursor = gtk_entry_insert_at_cursor;
462 class->delete_from_cursor = gtk_entry_delete_from_cursor;
463 class->backspace = gtk_entry_backspace;
464 class->cut_clipboard = gtk_entry_cut_clipboard;
465 class->copy_clipboard = gtk_entry_copy_clipboard;
466 class->paste_clipboard = gtk_entry_paste_clipboard;
467 class->toggle_overwrite = gtk_entry_toggle_overwrite;
468 class->activate = gtk_entry_real_activate;
470 quark_inner_border = g_quark_from_static_string ("gtk-entry-inner-border");
471 quark_password_hint = g_quark_from_static_string ("gtk-entry-password-hint");
472 quark_cursor_hadjustment = g_quark_from_static_string ("gtk-hadjustment");
474 g_object_class_install_property (gobject_class,
475 PROP_CURSOR_POSITION,
476 g_param_spec_int ("cursor-position",
477 P_("Cursor Position"),
478 P_("The current position of the insertion cursor in chars"),
482 GTK_PARAM_READABLE));
484 g_object_class_install_property (gobject_class,
485 PROP_SELECTION_BOUND,
486 g_param_spec_int ("selection-bound",
487 P_("Selection Bound"),
488 P_("The position of the opposite end of the selection from the cursor in chars"),
492 GTK_PARAM_READABLE));
494 g_object_class_install_property (gobject_class,
496 g_param_spec_boolean ("editable",
498 P_("Whether the entry contents can be edited"),
500 GTK_PARAM_READWRITE));
502 g_object_class_install_property (gobject_class,
504 g_param_spec_int ("max-length",
505 P_("Maximum length"),
506 P_("Maximum number of characters for this entry. Zero if no maximum"),
510 GTK_PARAM_READWRITE));
511 g_object_class_install_property (gobject_class,
513 g_param_spec_boolean ("visibility",
515 P_("FALSE displays the \"invisible char\" instead of the actual text (password mode)"),
517 GTK_PARAM_READWRITE));
519 g_object_class_install_property (gobject_class,
521 g_param_spec_boolean ("has-frame",
523 P_("FALSE removes outside bevel from entry"),
525 GTK_PARAM_READWRITE));
527 g_object_class_install_property (gobject_class,
529 g_param_spec_boxed ("inner-border",
531 P_("Border between text and frame. Overrides the inner-border style property"),
533 GTK_PARAM_READWRITE));
535 g_object_class_install_property (gobject_class,
537 g_param_spec_unichar ("invisible-char",
538 P_("Invisible character"),
539 P_("The character to use when masking entry contents (in \"password mode\")"),
541 GTK_PARAM_READWRITE));
543 g_object_class_install_property (gobject_class,
544 PROP_ACTIVATES_DEFAULT,
545 g_param_spec_boolean ("activates-default",
546 P_("Activates default"),
547 P_("Whether to activate the default widget (such as the default button in a dialog) when Enter is pressed"),
549 GTK_PARAM_READWRITE));
550 g_object_class_install_property (gobject_class,
552 g_param_spec_int ("width-chars",
553 P_("Width in chars"),
554 P_("Number of characters to leave space for in the entry"),
558 GTK_PARAM_READWRITE));
560 g_object_class_install_property (gobject_class,
562 g_param_spec_int ("scroll-offset",
564 P_("Number of pixels of the entry scrolled off the screen to the left"),
568 GTK_PARAM_READABLE));
570 g_object_class_install_property (gobject_class,
572 g_param_spec_string ("text",
574 P_("The contents of the entry"),
576 GTK_PARAM_READWRITE));
581 * The horizontal alignment, from 0 (left) to 1 (right).
582 * Reversed for RTL layouts.
586 g_object_class_install_property (gobject_class,
588 g_param_spec_float ("xalign",
590 P_("The horizontal alignment, from 0 (left) to 1 (right). Reversed for RTL layouts."),
594 GTK_PARAM_READWRITE));
597 * GtkEntry:truncate-multiline:
599 * When %TRUE, pasted multi-line text is truncated to the first line.
603 g_object_class_install_property (gobject_class,
604 PROP_TRUNCATE_MULTILINE,
605 g_param_spec_boolean ("truncate-multiline",
606 P_("Truncate multiline"),
607 P_("Whether to truncate multiline pastes to one line."),
609 GTK_PARAM_READWRITE));
612 * GtkEntry:shadow-type:
614 * Which kind of shadow to draw around the entry when
615 * #GtkEntry:has-frame is set to %TRUE.
619 g_object_class_install_property (gobject_class,
621 g_param_spec_enum ("shadow-type",
623 P_("Which kind of shadow to draw around the entry when has-frame is set"),
624 GTK_TYPE_SHADOW_TYPE,
626 GTK_PARAM_READWRITE));
628 signals[POPULATE_POPUP] =
629 g_signal_new (I_("populate_popup"),
630 G_OBJECT_CLASS_TYPE (gobject_class),
632 G_STRUCT_OFFSET (GtkEntryClass, populate_popup),
634 _gtk_marshal_VOID__OBJECT,
641 g_signal_new (I_("activate"),
642 G_OBJECT_CLASS_TYPE (gobject_class),
643 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
644 G_STRUCT_OFFSET (GtkEntryClass, activate),
646 _gtk_marshal_VOID__VOID,
648 widget_class->activate_signal = signals[ACTIVATE];
650 signals[MOVE_CURSOR] =
651 g_signal_new (I_("move_cursor"),
652 G_OBJECT_CLASS_TYPE (gobject_class),
653 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
654 G_STRUCT_OFFSET (GtkEntryClass, move_cursor),
656 _gtk_marshal_VOID__ENUM_INT_BOOLEAN,
658 GTK_TYPE_MOVEMENT_STEP,
662 signals[INSERT_AT_CURSOR] =
663 g_signal_new (I_("insert_at_cursor"),
664 G_OBJECT_CLASS_TYPE (gobject_class),
665 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
666 G_STRUCT_OFFSET (GtkEntryClass, insert_at_cursor),
668 _gtk_marshal_VOID__STRING,
672 signals[DELETE_FROM_CURSOR] =
673 g_signal_new (I_("delete_from_cursor"),
674 G_OBJECT_CLASS_TYPE (gobject_class),
675 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
676 G_STRUCT_OFFSET (GtkEntryClass, delete_from_cursor),
678 _gtk_marshal_VOID__ENUM_INT,
680 GTK_TYPE_DELETE_TYPE,
684 g_signal_new (I_("backspace"),
685 G_OBJECT_CLASS_TYPE (gobject_class),
686 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
687 G_STRUCT_OFFSET (GtkEntryClass, backspace),
689 _gtk_marshal_VOID__VOID,
692 signals[CUT_CLIPBOARD] =
693 g_signal_new (I_("cut_clipboard"),
694 G_OBJECT_CLASS_TYPE (gobject_class),
695 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
696 G_STRUCT_OFFSET (GtkEntryClass, cut_clipboard),
698 _gtk_marshal_VOID__VOID,
701 signals[COPY_CLIPBOARD] =
702 g_signal_new (I_("copy_clipboard"),
703 G_OBJECT_CLASS_TYPE (gobject_class),
704 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
705 G_STRUCT_OFFSET (GtkEntryClass, copy_clipboard),
707 _gtk_marshal_VOID__VOID,
710 signals[PASTE_CLIPBOARD] =
711 g_signal_new (I_("paste_clipboard"),
712 G_OBJECT_CLASS_TYPE (gobject_class),
713 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
714 G_STRUCT_OFFSET (GtkEntryClass, paste_clipboard),
716 _gtk_marshal_VOID__VOID,
719 signals[TOGGLE_OVERWRITE] =
720 g_signal_new (I_("toggle_overwrite"),
721 G_OBJECT_CLASS_TYPE (gobject_class),
722 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
723 G_STRUCT_OFFSET (GtkEntryClass, toggle_overwrite),
725 _gtk_marshal_VOID__VOID,
732 binding_set = gtk_binding_set_by_class (class);
734 /* Moving the insertion point */
735 add_move_binding (binding_set, GDK_Right, 0,
736 GTK_MOVEMENT_VISUAL_POSITIONS, 1);
738 add_move_binding (binding_set, GDK_Left, 0,
739 GTK_MOVEMENT_VISUAL_POSITIONS, -1);
741 add_move_binding (binding_set, GDK_KP_Right, 0,
742 GTK_MOVEMENT_VISUAL_POSITIONS, 1);
744 add_move_binding (binding_set, GDK_KP_Left, 0,
745 GTK_MOVEMENT_VISUAL_POSITIONS, -1);
747 add_move_binding (binding_set, GDK_Right, GDK_CONTROL_MASK,
748 GTK_MOVEMENT_WORDS, 1);
750 add_move_binding (binding_set, GDK_Left, GDK_CONTROL_MASK,
751 GTK_MOVEMENT_WORDS, -1);
753 add_move_binding (binding_set, GDK_KP_Right, GDK_CONTROL_MASK,
754 GTK_MOVEMENT_WORDS, 1);
756 add_move_binding (binding_set, GDK_KP_Left, GDK_CONTROL_MASK,
757 GTK_MOVEMENT_WORDS, -1);
759 add_move_binding (binding_set, GDK_Home, 0,
760 GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
762 add_move_binding (binding_set, GDK_End, 0,
763 GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
765 add_move_binding (binding_set, GDK_KP_Home, 0,
766 GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
768 add_move_binding (binding_set, GDK_KP_End, 0,
769 GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
771 add_move_binding (binding_set, GDK_Home, GDK_CONTROL_MASK,
772 GTK_MOVEMENT_BUFFER_ENDS, -1);
774 add_move_binding (binding_set, GDK_End, GDK_CONTROL_MASK,
775 GTK_MOVEMENT_BUFFER_ENDS, 1);
777 add_move_binding (binding_set, GDK_KP_Home, GDK_CONTROL_MASK,
778 GTK_MOVEMENT_BUFFER_ENDS, -1);
780 add_move_binding (binding_set, GDK_KP_End, GDK_CONTROL_MASK,
781 GTK_MOVEMENT_BUFFER_ENDS, 1);
785 gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_CONTROL_MASK,
787 GTK_TYPE_MOVEMENT_STEP, GTK_MOVEMENT_BUFFER_ENDS,
789 G_TYPE_BOOLEAN, FALSE);
790 gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_CONTROL_MASK,
792 GTK_TYPE_MOVEMENT_STEP, GTK_MOVEMENT_BUFFER_ENDS,
794 G_TYPE_BOOLEAN, TRUE);
796 gtk_binding_entry_add_signal (binding_set, GDK_slash, GDK_CONTROL_MASK,
798 GTK_TYPE_MOVEMENT_STEP, GTK_MOVEMENT_BUFFER_ENDS,
800 G_TYPE_BOOLEAN, FALSE);
801 gtk_binding_entry_add_signal (binding_set, GDK_slash, GDK_CONTROL_MASK,
803 GTK_TYPE_MOVEMENT_STEP, GTK_MOVEMENT_BUFFER_ENDS,
805 G_TYPE_BOOLEAN, TRUE);
808 gtk_binding_entry_add_signal (binding_set, GDK_backslash, GDK_CONTROL_MASK,
810 GTK_TYPE_MOVEMENT_STEP, GTK_MOVEMENT_VISUAL_POSITIONS,
812 G_TYPE_BOOLEAN, FALSE);
813 gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
815 GTK_TYPE_MOVEMENT_STEP, GTK_MOVEMENT_VISUAL_POSITIONS,
817 G_TYPE_BOOLEAN, FALSE);
821 gtk_binding_entry_add_signal (binding_set, GDK_Return, 0,
823 gtk_binding_entry_add_signal (binding_set, GDK_ISO_Enter, 0,
825 gtk_binding_entry_add_signal (binding_set, GDK_KP_Enter, 0,
829 gtk_binding_entry_add_signal (binding_set, GDK_Delete, 0,
830 "delete_from_cursor", 2,
831 G_TYPE_ENUM, GTK_DELETE_CHARS,
834 gtk_binding_entry_add_signal (binding_set, GDK_KP_Delete, 0,
835 "delete_from_cursor", 2,
836 G_TYPE_ENUM, GTK_DELETE_CHARS,
839 gtk_binding_entry_add_signal (binding_set, GDK_BackSpace, 0,
842 /* Make this do the same as Backspace, to help with mis-typing */
843 gtk_binding_entry_add_signal (binding_set, GDK_BackSpace, GDK_SHIFT_MASK,
846 gtk_binding_entry_add_signal (binding_set, GDK_Delete, GDK_CONTROL_MASK,
847 "delete_from_cursor", 2,
848 G_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
851 gtk_binding_entry_add_signal (binding_set, GDK_KP_Delete, GDK_CONTROL_MASK,
852 "delete_from_cursor", 2,
853 G_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
856 gtk_binding_entry_add_signal (binding_set, GDK_BackSpace, GDK_CONTROL_MASK,
857 "delete_from_cursor", 2,
858 G_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
863 gtk_binding_entry_add_signal (binding_set, GDK_x, GDK_CONTROL_MASK,
865 gtk_binding_entry_add_signal (binding_set, GDK_c, GDK_CONTROL_MASK,
866 "copy_clipboard", 0);
867 gtk_binding_entry_add_signal (binding_set, GDK_v, GDK_CONTROL_MASK,
868 "paste_clipboard", 0);
870 gtk_binding_entry_add_signal (binding_set, GDK_Delete, GDK_SHIFT_MASK,
872 gtk_binding_entry_add_signal (binding_set, GDK_Insert, GDK_CONTROL_MASK,
873 "copy_clipboard", 0);
874 gtk_binding_entry_add_signal (binding_set, GDK_Insert, GDK_SHIFT_MASK,
875 "paste_clipboard", 0);
878 gtk_binding_entry_add_signal (binding_set, GDK_Insert, 0,
879 "toggle_overwrite", 0);
880 gtk_binding_entry_add_signal (binding_set, GDK_KP_Insert, 0,
881 "toggle_overwrite", 0);
884 * GtkEntry:inner-border:
886 * Sets the text area's border between the text and the frame.
890 gtk_widget_class_install_style_property (widget_class,
891 g_param_spec_boxed ("inner-border",
893 P_("Border between text and frame."),
895 GTK_PARAM_READABLE));
897 gtk_settings_install_property (g_param_spec_boolean ("gtk-entry-select-on-focus",
898 P_("Select on focus"),
899 P_("Whether to select the contents of an entry when it is focused"),
901 GTK_PARAM_READWRITE));
904 * GtkSettings:gtk-entry-password-hint-timeout:
906 * How long to show the last input character in hidden
907 * entries. This value is in milliseconds. 0 disables showing the
908 * last char. 600 is a good value for enabling it.
912 gtk_settings_install_property (g_param_spec_uint ("gtk-entry-password-hint-timeout",
913 P_("Password Hint Timeout"),
914 P_("How long to show the last input character in hidden entries"),
916 GTK_PARAM_READWRITE));
918 g_type_class_add_private (gobject_class, sizeof (GtkEntryPrivate));
922 gtk_entry_editable_init (GtkEditableClass *iface)
924 iface->do_insert_text = gtk_entry_insert_text;
925 iface->do_delete_text = gtk_entry_delete_text;
926 iface->insert_text = gtk_entry_real_insert_text;
927 iface->delete_text = gtk_entry_real_delete_text;
928 iface->get_chars = gtk_entry_get_chars;
929 iface->set_selection_bounds = gtk_entry_set_selection_bounds;
930 iface->get_selection_bounds = gtk_entry_get_selection_bounds;
931 iface->set_position = gtk_entry_real_set_position;
932 iface->get_position = gtk_entry_get_position;
936 gtk_entry_cell_editable_init (GtkCellEditableIface *iface)
938 iface->start_editing = gtk_entry_start_editing;
942 gtk_entry_set_property (GObject *object,
947 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (object);
948 GtkEntry *entry = GTK_ENTRY (object);
954 gboolean new_value = g_value_get_boolean (value);
956 if (new_value != entry->editable)
960 _gtk_entry_reset_im_context (entry);
961 if (GTK_WIDGET_HAS_FOCUS (entry))
962 gtk_im_context_focus_out (entry->im_context);
964 entry->preedit_length = 0;
965 entry->preedit_cursor = 0;
968 entry->editable = new_value;
970 if (new_value && GTK_WIDGET_HAS_FOCUS (entry))
971 gtk_im_context_focus_in (entry->im_context);
973 gtk_entry_queue_draw (entry);
978 case PROP_MAX_LENGTH:
979 gtk_entry_set_max_length (entry, g_value_get_int (value));
982 case PROP_VISIBILITY:
983 gtk_entry_set_visibility (entry, g_value_get_boolean (value));
987 gtk_entry_set_has_frame (entry, g_value_get_boolean (value));
990 case PROP_INNER_BORDER:
991 gtk_entry_set_inner_border (entry, g_value_get_boxed (value));
994 case PROP_INVISIBLE_CHAR:
995 gtk_entry_set_invisible_char (entry, g_value_get_uint (value));
998 case PROP_ACTIVATES_DEFAULT:
999 gtk_entry_set_activates_default (entry, g_value_get_boolean (value));
1002 case PROP_WIDTH_CHARS:
1003 gtk_entry_set_width_chars (entry, g_value_get_int (value));
1007 gtk_entry_set_text (entry, g_value_get_string (value));
1011 gtk_entry_set_alignment (entry, g_value_get_float (value));
1014 case PROP_TRUNCATE_MULTILINE:
1015 entry->truncate_multiline = g_value_get_boolean (value);
1018 case PROP_SHADOW_TYPE:
1019 priv->shadow_type = g_value_get_enum (value);
1022 case PROP_SCROLL_OFFSET:
1023 case PROP_CURSOR_POSITION:
1025 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1031 gtk_entry_get_property (GObject *object,
1036 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (object);
1037 GtkEntry *entry = GTK_ENTRY (object);
1041 case PROP_CURSOR_POSITION:
1042 g_value_set_int (value, entry->current_pos);
1044 case PROP_SELECTION_BOUND:
1045 g_value_set_int (value, entry->selection_bound);
1048 g_value_set_boolean (value, entry->editable);
1050 case PROP_MAX_LENGTH:
1051 g_value_set_int (value, entry->text_max_length);
1053 case PROP_VISIBILITY:
1054 g_value_set_boolean (value, entry->visible);
1056 case PROP_HAS_FRAME:
1057 g_value_set_boolean (value, entry->has_frame);
1059 case PROP_INNER_BORDER:
1060 g_value_set_boxed (value, gtk_entry_get_inner_border (entry));
1062 case PROP_INVISIBLE_CHAR:
1063 g_value_set_uint (value, entry->invisible_char);
1065 case PROP_ACTIVATES_DEFAULT:
1066 g_value_set_boolean (value, entry->activates_default);
1068 case PROP_WIDTH_CHARS:
1069 g_value_set_int (value, entry->width_chars);
1071 case PROP_SCROLL_OFFSET:
1072 g_value_set_int (value, entry->scroll_offset);
1075 g_value_set_string (value, gtk_entry_get_text (entry));
1078 g_value_set_float (value, gtk_entry_get_alignment (entry));
1080 case PROP_TRUNCATE_MULTILINE:
1081 g_value_set_boolean (value, entry->truncate_multiline);
1083 case PROP_SHADOW_TYPE:
1084 g_value_set_enum (value, priv->shadow_type);
1088 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1094 gtk_entry_init (GtkEntry *entry)
1096 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
1098 GTK_WIDGET_SET_FLAGS (entry, GTK_CAN_FOCUS);
1100 entry->text_size = MIN_SIZE;
1101 entry->text = g_malloc (entry->text_size);
1102 entry->text[0] = '\0';
1104 entry->editable = TRUE;
1105 entry->visible = TRUE;
1106 entry->invisible_char = '*';
1107 entry->dnd_position = -1;
1108 entry->width_chars = -1;
1109 entry->is_cell_renderer = FALSE;
1110 entry->editing_canceled = FALSE;
1111 entry->has_frame = TRUE;
1112 entry->truncate_multiline = FALSE;
1113 priv->shadow_type = GTK_SHADOW_IN;
1116 gtk_drag_dest_set (GTK_WIDGET (entry),
1117 GTK_DEST_DEFAULT_HIGHLIGHT,
1119 GDK_ACTION_COPY | GDK_ACTION_MOVE);
1120 gtk_drag_dest_add_text_targets (GTK_WIDGET (entry));
1122 /* This object is completely private. No external entity can gain a reference
1123 * to it; so we create it here and destroy it in finalize().
1125 entry->im_context = gtk_im_multicontext_new ();
1127 g_signal_connect (entry->im_context, "commit",
1128 G_CALLBACK (gtk_entry_commit_cb), entry);
1129 g_signal_connect (entry->im_context, "preedit_changed",
1130 G_CALLBACK (gtk_entry_preedit_changed_cb), entry);
1131 g_signal_connect (entry->im_context, "retrieve_surrounding",
1132 G_CALLBACK (gtk_entry_retrieve_surrounding_cb), entry);
1133 g_signal_connect (entry->im_context, "delete_surrounding",
1134 G_CALLBACK (gtk_entry_delete_surrounding_cb), entry);
1138 begin_change (GtkEntry *entry)
1140 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
1142 priv->change_count++;
1146 end_change (GtkEntry *entry)
1148 GtkEditable *editable = GTK_EDITABLE (entry);
1149 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
1151 g_return_if_fail (priv->change_count > 0);
1153 priv->change_count--;
1155 if (priv->change_count == 0)
1157 if (priv->real_changed)
1159 g_signal_emit_by_name (editable, "changed");
1160 priv->real_changed = FALSE;
1166 emit_changed (GtkEntry *entry)
1168 GtkEditable *editable = GTK_EDITABLE (entry);
1169 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
1171 if (priv->change_count == 0)
1172 g_signal_emit_by_name (editable, "changed");
1174 priv->real_changed = TRUE;
1178 * Overwrite a memory that might contain sensitive information.
1181 trash_area (gchar *area, gsize len)
1183 volatile gchar *varea = (volatile gchar *)area;
1189 gtk_entry_destroy (GtkObject *object)
1191 GtkEntry *entry = GTK_ENTRY (object);
1194 entry->current_pos = entry->selection_bound = entry->text_length = 0;
1195 _gtk_entry_reset_im_context (entry);
1196 gtk_entry_reset_layout (entry);
1198 if (entry->blink_timeout)
1200 g_source_remove (entry->blink_timeout);
1201 entry->blink_timeout = 0;
1204 if (entry->recompute_idle)
1206 g_source_remove (entry->recompute_idle);
1207 entry->recompute_idle = 0;
1210 if (!entry->visible)
1212 /* We want to trash the text here because the entry might be leaked. */
1213 trash_area (entry->text, strlen (entry->text));
1216 GTK_OBJECT_CLASS (gtk_entry_parent_class)->destroy (object);
1220 gtk_entry_finalize (GObject *object)
1222 GtkEntry *entry = GTK_ENTRY (object);
1224 gtk_entry_set_completion (entry, NULL);
1226 if (entry->cached_layout)
1227 g_object_unref (entry->cached_layout);
1229 g_object_unref (entry->im_context);
1231 if (entry->blink_timeout)
1232 g_source_remove (entry->blink_timeout);
1234 if (entry->recompute_idle)
1235 g_source_remove (entry->recompute_idle);
1237 entry->text_size = 0;
1241 if (!entry->visible)
1242 trash_area (entry->text, strlen (entry->text));
1243 g_free (entry->text);
1247 G_OBJECT_CLASS (gtk_entry_parent_class)->finalize (object);
1251 gtk_entry_realize (GtkWidget *widget)
1254 GdkWindowAttr attributes;
1255 gint attributes_mask;
1257 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
1258 entry = GTK_ENTRY (widget);
1260 attributes.window_type = GDK_WINDOW_CHILD;
1262 get_widget_window_size (entry, &attributes.x, &attributes.y, &attributes.width, &attributes.height);
1264 attributes.wclass = GDK_INPUT_OUTPUT;
1265 attributes.visual = gtk_widget_get_visual (widget);
1266 attributes.colormap = gtk_widget_get_colormap (widget);
1267 attributes.event_mask = gtk_widget_get_events (widget);
1268 attributes.event_mask |= (GDK_EXPOSURE_MASK |
1269 GDK_BUTTON_PRESS_MASK |
1270 GDK_BUTTON_RELEASE_MASK |
1271 GDK_BUTTON1_MOTION_MASK |
1272 GDK_BUTTON3_MOTION_MASK |
1273 GDK_POINTER_MOTION_HINT_MASK |
1274 GDK_POINTER_MOTION_MASK |
1275 GDK_ENTER_NOTIFY_MASK |
1276 GDK_LEAVE_NOTIFY_MASK);
1277 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
1279 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
1280 gdk_window_set_user_data (widget->window, entry);
1282 get_text_area_size (entry, &attributes.x, &attributes.y, &attributes.width, &attributes.height);
1284 if (GTK_WIDGET_IS_SENSITIVE (widget))
1286 attributes.cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget), GDK_XTERM);
1287 attributes_mask |= GDK_WA_CURSOR;
1290 entry->text_area = gdk_window_new (widget->window, &attributes, attributes_mask);
1292 gdk_window_set_user_data (entry->text_area, entry);
1294 if (attributes_mask & GDK_WA_CURSOR)
1295 gdk_cursor_unref (attributes.cursor);
1297 widget->style = gtk_style_attach (widget->style, widget->window);
1299 gdk_window_set_background (widget->window, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1300 gdk_window_set_background (entry->text_area, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1302 gdk_window_show (entry->text_area);
1304 gtk_im_context_set_client_window (entry->im_context, entry->text_area);
1306 gtk_entry_adjust_scroll (entry);
1307 gtk_entry_update_primary_selection (entry);
1311 gtk_entry_unrealize (GtkWidget *widget)
1313 GtkEntry *entry = GTK_ENTRY (widget);
1314 GtkClipboard *clipboard;
1316 gtk_entry_reset_layout (entry);
1318 gtk_im_context_set_client_window (entry->im_context, NULL);
1320 clipboard = gtk_widget_get_clipboard (widget, GDK_SELECTION_PRIMARY);
1321 if (gtk_clipboard_get_owner (clipboard) == G_OBJECT (entry))
1322 gtk_clipboard_clear (clipboard);
1324 if (entry->text_area)
1326 gdk_window_set_user_data (entry->text_area, NULL);
1327 gdk_window_destroy (entry->text_area);
1328 entry->text_area = NULL;
1331 if (entry->popup_menu)
1333 gtk_widget_destroy (entry->popup_menu);
1334 entry->popup_menu = NULL;
1337 if (GTK_WIDGET_CLASS (gtk_entry_parent_class)->unrealize)
1338 (* GTK_WIDGET_CLASS (gtk_entry_parent_class)->unrealize) (widget);
1342 _gtk_entry_get_borders (GtkEntry *entry,
1346 GtkWidget *widget = GTK_WIDGET (entry);
1347 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (widget);
1349 if (entry->has_frame)
1351 *xborder = widget->style->xthickness;
1352 *yborder = widget->style->ythickness;
1360 if (!priv->interior_focus)
1362 *xborder += priv->focus_width;
1363 *yborder += priv->focus_width;
1368 gtk_entry_size_request (GtkWidget *widget,
1369 GtkRequisition *requisition)
1371 GtkEntry *entry = GTK_ENTRY (widget);
1372 PangoFontMetrics *metrics;
1373 gint xborder, yborder;
1374 GtkBorder inner_border;
1375 PangoContext *context;
1377 gtk_widget_ensure_style (widget);
1378 context = gtk_widget_get_pango_context (widget);
1379 metrics = pango_context_get_metrics (context,
1380 widget->style->font_desc,
1381 pango_context_get_language (context));
1383 entry->ascent = pango_font_metrics_get_ascent (metrics);
1384 entry->descent = pango_font_metrics_get_descent (metrics);
1386 _gtk_entry_get_borders (entry, &xborder, &yborder);
1387 _gtk_entry_effective_inner_border (entry, &inner_border);
1389 if (entry->width_chars < 0)
1390 requisition->width = MIN_ENTRY_WIDTH + xborder * 2 + inner_border.left + inner_border.right;
1393 gint char_width = pango_font_metrics_get_approximate_char_width (metrics);
1394 gint digit_width = pango_font_metrics_get_approximate_digit_width (metrics);
1395 gint char_pixels = (MAX (char_width, digit_width) + PANGO_SCALE - 1) / PANGO_SCALE;
1397 requisition->width = char_pixels * entry->width_chars + xborder * 2 + inner_border.left + inner_border.right;
1400 requisition->height = PANGO_PIXELS (entry->ascent + entry->descent) + yborder * 2 + inner_border.top + inner_border.bottom;
1402 pango_font_metrics_unref (metrics);
1406 get_text_area_size (GtkEntry *entry,
1413 gint xborder, yborder;
1414 GtkRequisition requisition;
1415 GtkWidget *widget = GTK_WIDGET (entry);
1416 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (widget);
1418 gtk_widget_get_child_requisition (widget, &requisition);
1419 _gtk_entry_get_borders (entry, &xborder, &yborder);
1421 if (GTK_WIDGET_REALIZED (widget))
1422 gdk_drawable_get_size (widget->window, NULL, &frame_height);
1424 frame_height = requisition.height;
1426 if (GTK_WIDGET_HAS_FOCUS (widget) && !priv->interior_focus)
1427 frame_height -= 2 * priv->focus_width;
1433 *y = frame_height / 2 - (requisition.height - yborder * 2) / 2;
1436 *width = GTK_WIDGET (entry)->allocation.width - xborder * 2;
1439 *height = requisition.height - yborder * 2;
1443 get_widget_window_size (GtkEntry *entry,
1449 GtkRequisition requisition;
1450 GtkWidget *widget = GTK_WIDGET (entry);
1452 gtk_widget_get_child_requisition (widget, &requisition);
1455 *x = widget->allocation.x;
1459 if (entry->is_cell_renderer)
1460 *y = widget->allocation.y;
1462 *y = widget->allocation.y + (widget->allocation.height - requisition.height) / 2;
1466 *width = widget->allocation.width;
1470 if (entry->is_cell_renderer)
1471 *height = widget->allocation.height;
1473 *height = requisition.height;
1478 _gtk_entry_effective_inner_border (GtkEntry *entry,
1481 GtkBorder *tmp_border;
1483 tmp_border = g_object_get_qdata (G_OBJECT (entry), quark_inner_border);
1487 *border = *tmp_border;
1491 gtk_widget_style_get (GTK_WIDGET (entry), "inner-border", &tmp_border, NULL);
1495 *border = *tmp_border;
1496 gtk_border_free (tmp_border);
1500 *border = default_inner_border;
1504 gtk_entry_size_allocate (GtkWidget *widget,
1505 GtkAllocation *allocation)
1507 GtkEntry *entry = GTK_ENTRY (widget);
1509 widget->allocation = *allocation;
1511 if (GTK_WIDGET_REALIZED (widget))
1513 /* We call gtk_widget_get_child_requisition, since we want (for
1514 * backwards compatibility reasons) the realization here to
1515 * be affected by the usize of the entry, if set
1517 gint x, y, width, height;
1519 get_widget_window_size (entry, &x, &y, &width, &height);
1521 gdk_window_move_resize (widget->window,
1522 x, y, width, height);
1524 get_text_area_size (entry, &x, &y, &width, &height);
1526 gdk_window_move_resize (entry->text_area,
1527 x, y, width, height);
1529 gtk_entry_recompute (entry);
1534 gtk_entry_draw_frame (GtkWidget *widget,
1537 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (widget);
1538 gint x = 0, y = 0, width, height;
1540 gdk_drawable_get_size (widget->window, &width, &height);
1542 if (GTK_WIDGET_HAS_FOCUS (widget) && !priv->interior_focus)
1544 x += priv->focus_width;
1545 y += priv->focus_width;
1546 width -= 2 * priv->focus_width;
1547 height -= 2 * priv->focus_width;
1550 gtk_paint_shadow (widget->style, widget->window,
1551 GTK_STATE_NORMAL, priv->shadow_type,
1552 area, widget, "entry", x, y, width, height);
1554 if (GTK_WIDGET_HAS_FOCUS (widget) && !priv->interior_focus)
1556 x -= priv->focus_width;
1557 y -= priv->focus_width;
1558 width += 2 * priv->focus_width;
1559 height += 2 * priv->focus_width;
1561 gtk_paint_focus (widget->style, widget->window, GTK_WIDGET_STATE (widget),
1562 area, widget, "entry",
1563 0, 0, width, height);
1568 gtk_entry_expose (GtkWidget *widget,
1569 GdkEventExpose *event)
1571 GtkEntry *entry = GTK_ENTRY (widget);
1573 if (widget->window == event->window)
1574 gtk_entry_draw_frame (widget, &event->area);
1575 else if (entry->text_area == event->window)
1577 gint area_width, area_height;
1579 get_text_area_size (entry, NULL, NULL, &area_width, &area_height);
1581 gtk_paint_flat_box (widget->style, entry->text_area,
1582 GTK_WIDGET_STATE(widget), GTK_SHADOW_NONE,
1583 &event->area, widget, "entry_bg",
1584 0, 0, area_width, area_height);
1586 if (entry->dnd_position != -1)
1587 gtk_entry_draw_cursor (GTK_ENTRY (widget), CURSOR_DND);
1589 gtk_entry_draw_text (GTK_ENTRY (widget));
1591 if ((entry->visible || entry->invisible_char != 0) &&
1592 GTK_WIDGET_HAS_FOCUS (widget) &&
1593 entry->selection_bound == entry->current_pos && entry->cursor_visible)
1594 gtk_entry_draw_cursor (GTK_ENTRY (widget), CURSOR_STANDARD);
1601 gtk_entry_get_pixel_ranges (GtkEntry *entry,
1605 gint start_char, end_char;
1607 if (gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &start_char, &end_char))
1609 PangoLayout *layout = gtk_entry_ensure_layout (entry, TRUE);
1610 PangoLayoutLine *line = pango_layout_get_lines_readonly (layout)->data;
1611 const char *text = pango_layout_get_text (layout);
1612 gint start_index = g_utf8_offset_to_pointer (text, start_char) - text;
1613 gint end_index = g_utf8_offset_to_pointer (text, end_char) - text;
1614 gint real_n_ranges, i;
1616 pango_layout_line_get_x_ranges (line, start_index, end_index, ranges, &real_n_ranges);
1622 for (i = 0; i < real_n_ranges; ++i)
1624 r[2 * i + 1] = (r[2 * i + 1] - r[2 * i]) / PANGO_SCALE;
1625 r[2 * i] = r[2 * i] / PANGO_SCALE;
1630 *n_ranges = real_n_ranges;
1642 in_selection (GtkEntry *entry,
1647 gint retval = FALSE;
1649 gtk_entry_get_pixel_ranges (entry, &ranges, &n_ranges);
1651 for (i = 0; i < n_ranges; ++i)
1653 if (x >= ranges[2 * i] && x < ranges[2 * i] + ranges[2 * i + 1])
1665 gtk_entry_button_press (GtkWidget *widget,
1666 GdkEventButton *event)
1668 GtkEntry *entry = GTK_ENTRY (widget);
1669 GtkEditable *editable = GTK_EDITABLE (widget);
1670 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
1672 gint sel_start, sel_end;
1674 if (event->window != entry->text_area ||
1675 (entry->button && event->button != entry->button))
1678 gtk_entry_reset_blink_time (entry);
1680 entry->button = event->button;
1682 if (!GTK_WIDGET_HAS_FOCUS (widget))
1684 entry->in_click = TRUE;
1685 gtk_widget_grab_focus (widget);
1686 entry->in_click = FALSE;
1689 tmp_pos = gtk_entry_find_position (entry, event->x + entry->scroll_offset);
1691 if (event->button == 1)
1693 gboolean have_selection = gtk_editable_get_selection_bounds (editable, &sel_start, &sel_end);
1695 entry->select_words = FALSE;
1696 entry->select_lines = FALSE;
1698 if (event->state & GDK_SHIFT_MASK)
1700 _gtk_entry_reset_im_context (entry);
1702 if (!have_selection) /* select from the current position to the clicked position */
1703 sel_start = sel_end = entry->current_pos;
1705 if (tmp_pos > sel_start && tmp_pos < sel_end)
1707 /* Truncate current selection, but keep it as big as possible */
1708 if (tmp_pos - sel_start > sel_end - tmp_pos)
1709 gtk_entry_set_positions (entry, sel_start, tmp_pos);
1711 gtk_entry_set_positions (entry, tmp_pos, sel_end);
1715 gboolean extend_to_left;
1718 /* Figure out what click selects and extend current selection */
1719 switch (event->type)
1721 case GDK_BUTTON_PRESS:
1722 gtk_entry_set_positions (entry, tmp_pos, tmp_pos);
1725 case GDK_2BUTTON_PRESS:
1726 entry->select_words = TRUE;
1727 gtk_entry_select_word (entry);
1730 case GDK_3BUTTON_PRESS:
1731 entry->select_lines = TRUE;
1732 gtk_entry_select_line (entry);
1739 start = MIN (entry->current_pos, entry->selection_bound);
1740 start = MIN (sel_start, start);
1742 end = MAX (entry->current_pos, entry->selection_bound);
1743 end = MAX (sel_end, end);
1745 if (tmp_pos == sel_start || tmp_pos == sel_end)
1746 extend_to_left = (tmp_pos == start);
1748 extend_to_left = (end == sel_end);
1751 gtk_entry_set_positions (entry, start, end);
1753 gtk_entry_set_positions (entry, end, start);
1756 else /* no shift key */
1757 switch (event->type)
1759 case GDK_BUTTON_PRESS:
1760 if (in_selection (entry, event->x + entry->scroll_offset))
1762 /* Click inside the selection - we'll either start a drag, or
1763 * clear the selection
1765 entry->in_drag = TRUE;
1766 entry->drag_start_x = event->x + entry->scroll_offset;
1767 entry->drag_start_y = event->y;
1770 gtk_editable_set_position (editable, tmp_pos);
1773 case GDK_2BUTTON_PRESS:
1774 /* We ALWAYS receive a GDK_BUTTON_PRESS immediately before
1775 * receiving a GDK_2BUTTON_PRESS so we need to reset
1776 * entry->in_drag which may have been set above
1778 entry->in_drag = FALSE;
1779 entry->select_words = TRUE;
1780 gtk_entry_select_word (entry);
1783 case GDK_3BUTTON_PRESS:
1784 /* We ALWAYS receive a GDK_BUTTON_PRESS immediately before
1785 * receiving a GDK_3BUTTON_PRESS so we need to reset
1786 * entry->in_drag which may have been set above
1788 entry->in_drag = FALSE;
1789 entry->select_lines = TRUE;
1790 gtk_entry_select_line (entry);
1799 else if (event->button == 2 && event->type == GDK_BUTTON_PRESS)
1801 if (entry->editable)
1803 priv->insert_pos = tmp_pos;
1804 gtk_entry_paste (entry, GDK_SELECTION_PRIMARY);
1809 gtk_widget_error_bell (widget);
1812 else if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
1814 gtk_entry_do_popup (entry, event);
1815 entry->button = 0; /* Don't wait for release, since the menu will gtk_grab_add */
1824 gtk_entry_button_release (GtkWidget *widget,
1825 GdkEventButton *event)
1827 GtkEntry *entry = GTK_ENTRY (widget);
1829 if (event->window != entry->text_area || entry->button != event->button)
1834 gint tmp_pos = gtk_entry_find_position (entry, entry->drag_start_x);
1836 gtk_editable_set_position (GTK_EDITABLE (entry), tmp_pos);
1843 gtk_entry_update_primary_selection (entry);
1849 _gtk_entry_get_selected_text (GtkEntry *entry)
1851 GtkEditable *editable = GTK_EDITABLE (entry);
1852 gint start_text, end_text;
1855 if (gtk_editable_get_selection_bounds (editable, &start_text, &end_text))
1856 text = gtk_editable_get_chars (editable, start_text, end_text);
1862 drag_begin_cb (GtkWidget *widget,
1863 GdkDragContext *context,
1866 g_signal_handlers_disconnect_by_func (widget, drag_begin_cb, NULL);
1870 gtk_entry_motion_notify (GtkWidget *widget,
1871 GdkEventMotion *event)
1873 GtkEntry *entry = GTK_ENTRY (widget);
1876 if (entry->mouse_cursor_obscured)
1880 cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget), GDK_XTERM);
1881 gdk_window_set_cursor (entry->text_area, cursor);
1882 gdk_cursor_unref (cursor);
1883 entry->mouse_cursor_obscured = FALSE;
1886 if (event->window != entry->text_area || entry->button != 1)
1889 if (entry->select_lines)
1892 gdk_event_request_motions (event);
1896 if (gtk_drag_check_threshold (widget,
1897 entry->drag_start_x, entry->drag_start_y,
1898 event->x + entry->scroll_offset, event->y))
1900 GdkDragContext *context;
1901 GtkTargetList *target_list = gtk_target_list_new (NULL, 0);
1902 guint actions = entry->editable ? GDK_ACTION_COPY | GDK_ACTION_MOVE : GDK_ACTION_COPY;
1904 GdkPixmap *pixmap = NULL;
1906 gtk_target_list_add_text_targets (target_list, 0);
1910 text = _gtk_entry_get_selected_text (entry);
1911 pixmap = _gtk_text_util_create_drag_icon (widget, text, -1);
1914 context = gtk_drag_begin (widget, target_list, actions,
1915 entry->button, (GdkEvent *)event);
1918 gtk_drag_set_icon_pixmap (context,
1919 gdk_drawable_get_colormap (pixmap),
1924 gtk_drag_set_icon_default (context);
1927 g_object_unref (pixmap);
1930 entry->in_drag = FALSE;
1933 gtk_target_list_unref (target_list);
1939 gdk_drawable_get_size (entry->text_area, NULL, &height);
1943 else if (event->y >= height)
1944 tmp_pos = entry->text_length;
1946 tmp_pos = gtk_entry_find_position (entry, event->x + entry->scroll_offset);
1948 if (entry->select_words)
1951 gint old_min, old_max;
1954 min = gtk_entry_move_backward_word (entry, tmp_pos, TRUE);
1955 max = gtk_entry_move_forward_word (entry, tmp_pos, TRUE);
1957 pos = entry->current_pos;
1958 bound = entry->selection_bound;
1960 old_min = MIN(entry->current_pos, entry->selection_bound);
1961 old_max = MAX(entry->current_pos, entry->selection_bound);
1968 else if (old_max < max)
1973 else if (pos == old_min)
1975 if (entry->current_pos != min)
1980 if (entry->current_pos != max)
1984 gtk_entry_set_positions (entry, pos, bound);
1987 gtk_entry_set_positions (entry, tmp_pos, -1);
1994 set_invisible_cursor (GdkWindow *window)
1996 GdkBitmap *empty_bitmap;
1999 char invisible_cursor_bits[] = { 0x0 };
2001 useless.red = useless.green = useless.blue = 0;
2004 empty_bitmap = gdk_bitmap_create_from_data (window,
2005 invisible_cursor_bits,
2008 cursor = gdk_cursor_new_from_pixmap (empty_bitmap,
2013 gdk_window_set_cursor (window, cursor);
2015 gdk_cursor_unref (cursor);
2017 g_object_unref (empty_bitmap);
2021 gtk_entry_obscure_mouse_cursor (GtkEntry *entry)
2023 if (entry->mouse_cursor_obscured)
2026 set_invisible_cursor (entry->text_area);
2028 entry->mouse_cursor_obscured = TRUE;
2032 gtk_entry_key_press (GtkWidget *widget,
2035 GtkEntry *entry = GTK_ENTRY (widget);
2037 gtk_entry_reset_blink_time (entry);
2038 gtk_entry_pend_cursor_blink (entry);
2040 if (entry->editable)
2042 if (gtk_im_context_filter_keypress (entry->im_context, event))
2044 gtk_entry_obscure_mouse_cursor (entry);
2045 entry->need_im_reset = TRUE;
2050 if (event->keyval == GDK_Return ||
2051 event->keyval == GDK_KP_Enter ||
2052 event->keyval == GDK_ISO_Enter ||
2053 event->keyval == GDK_Escape)
2055 GtkEntryCompletion *completion = gtk_entry_get_completion (entry);
2057 if (completion && completion->priv->completion_timeout)
2059 g_source_remove (completion->priv->completion_timeout);
2060 completion->priv->completion_timeout = 0;
2063 _gtk_entry_reset_im_context (entry);
2066 if (GTK_WIDGET_CLASS (gtk_entry_parent_class)->key_press_event (widget, event))
2067 /* Activate key bindings
2071 if (!entry->editable && event->length)
2072 gtk_widget_error_bell (widget);
2078 gtk_entry_key_release (GtkWidget *widget,
2081 GtkEntry *entry = GTK_ENTRY (widget);
2083 if (entry->editable)
2085 if (gtk_im_context_filter_keypress (entry->im_context, event))
2087 entry->need_im_reset = TRUE;
2092 return GTK_WIDGET_CLASS (gtk_entry_parent_class)->key_release_event (widget, event);
2096 gtk_entry_focus_in (GtkWidget *widget,
2097 GdkEventFocus *event)
2099 GtkEntry *entry = GTK_ENTRY (widget);
2101 gtk_widget_queue_draw (widget);
2103 if (entry->editable)
2105 entry->need_im_reset = TRUE;
2106 gtk_im_context_focus_in (entry->im_context);
2109 g_signal_connect (gdk_keymap_get_for_display (gtk_widget_get_display (widget)),
2110 "direction_changed",
2111 G_CALLBACK (gtk_entry_keymap_direction_changed), entry);
2113 gtk_entry_reset_blink_time (entry);
2114 gtk_entry_check_cursor_blink (entry);
2120 gtk_entry_focus_out (GtkWidget *widget,
2121 GdkEventFocus *event)
2123 GtkEntry *entry = GTK_ENTRY (widget);
2124 GtkEntryCompletion *completion;
2126 gtk_widget_queue_draw (widget);
2128 if (entry->editable)
2130 entry->need_im_reset = TRUE;
2131 gtk_im_context_focus_out (entry->im_context);
2134 gtk_entry_check_cursor_blink (entry);
2136 g_signal_handlers_disconnect_by_func (gdk_keymap_get_for_display (gtk_widget_get_display (widget)),
2137 gtk_entry_keymap_direction_changed,
2140 completion = gtk_entry_get_completion (entry);
2142 _gtk_entry_completion_popdown (completion);
2148 gtk_entry_grab_focus (GtkWidget *widget)
2150 GtkEntry *entry = GTK_ENTRY (widget);
2151 gboolean select_on_focus;
2153 GTK_WIDGET_CLASS (gtk_entry_parent_class)->grab_focus (widget);
2155 if (entry->editable && !entry->in_click)
2157 g_object_get (gtk_widget_get_settings (widget),
2158 "gtk-entry-select-on-focus",
2162 if (select_on_focus)
2163 gtk_editable_select_region (GTK_EDITABLE (widget), 0, -1);
2168 gtk_entry_direction_changed (GtkWidget *widget,
2169 GtkTextDirection previous_dir)
2171 GtkEntry *entry = GTK_ENTRY (widget);
2173 gtk_entry_recompute (entry);
2175 GTK_WIDGET_CLASS (gtk_entry_parent_class)->direction_changed (widget, previous_dir);
2179 gtk_entry_state_changed (GtkWidget *widget,
2180 GtkStateType previous_state)
2182 GtkEntry *entry = GTK_ENTRY (widget);
2185 if (GTK_WIDGET_REALIZED (widget))
2187 gdk_window_set_background (widget->window, &widget->style->base[GTK_WIDGET_STATE (widget)]);
2188 gdk_window_set_background (entry->text_area, &widget->style->base[GTK_WIDGET_STATE (widget)]);
2190 if (GTK_WIDGET_IS_SENSITIVE (widget))
2191 cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget), GDK_XTERM);
2195 gdk_window_set_cursor (entry->text_area, cursor);
2198 gdk_cursor_unref (cursor);
2200 entry->mouse_cursor_obscured = FALSE;
2203 if (!GTK_WIDGET_IS_SENSITIVE (widget))
2205 /* Clear any selection */
2206 gtk_editable_select_region (GTK_EDITABLE (entry), entry->current_pos, entry->current_pos);
2209 gtk_widget_queue_draw (widget);
2213 gtk_entry_screen_changed (GtkWidget *widget,
2214 GdkScreen *old_screen)
2216 gtk_entry_recompute (GTK_ENTRY (widget));
2219 /* GtkEditable method implementations
2222 gtk_entry_insert_text (GtkEditable *editable,
2223 const gchar *new_text,
2224 gint new_text_length,
2227 GtkEntry *entry = GTK_ENTRY (editable);
2231 if (*position < 0 || *position > entry->text_length)
2232 *position = entry->text_length;
2234 g_object_ref (editable);
2236 if (new_text_length <= 63)
2239 text = g_new (gchar, new_text_length + 1);
2241 text[new_text_length] = '\0';
2242 strncpy (text, new_text, new_text_length);
2244 g_signal_emit_by_name (editable, "insert_text", text, new_text_length, position);
2246 if (!entry->visible)
2247 trash_area (text, new_text_length);
2249 if (new_text_length > 63)
2252 g_object_unref (editable);
2256 gtk_entry_delete_text (GtkEditable *editable,
2260 GtkEntry *entry = GTK_ENTRY (editable);
2262 if (end_pos < 0 || end_pos > entry->text_length)
2263 end_pos = entry->text_length;
2266 if (start_pos > end_pos)
2267 start_pos = end_pos;
2269 g_object_ref (editable);
2271 g_signal_emit_by_name (editable, "delete_text", start_pos, end_pos);
2273 g_object_unref (editable);
2277 gtk_entry_get_chars (GtkEditable *editable,
2281 GtkEntry *entry = GTK_ENTRY (editable);
2282 gint start_index, end_index;
2285 end_pos = entry->text_length;
2287 start_pos = MIN (entry->text_length, start_pos);
2288 end_pos = MIN (entry->text_length, end_pos);
2290 start_index = g_utf8_offset_to_pointer (entry->text, start_pos) - entry->text;
2291 end_index = g_utf8_offset_to_pointer (entry->text, end_pos) - entry->text;
2293 return g_strndup (entry->text + start_index, end_index - start_index);
2297 gtk_entry_real_set_position (GtkEditable *editable,
2300 GtkEntry *entry = GTK_ENTRY (editable);
2302 if (position < 0 || position > entry->text_length)
2303 position = entry->text_length;
2305 if (position != entry->current_pos ||
2306 position != entry->selection_bound)
2308 _gtk_entry_reset_im_context (entry);
2309 gtk_entry_set_positions (entry, position, position);
2314 gtk_entry_get_position (GtkEditable *editable)
2316 return GTK_ENTRY (editable)->current_pos;
2320 gtk_entry_set_selection_bounds (GtkEditable *editable,
2324 GtkEntry *entry = GTK_ENTRY (editable);
2327 start = entry->text_length;
2329 end = entry->text_length;
2331 _gtk_entry_reset_im_context (entry);
2333 gtk_entry_set_positions (entry,
2334 MIN (end, entry->text_length),
2335 MIN (start, entry->text_length));
2337 gtk_entry_update_primary_selection (entry);
2341 gtk_entry_get_selection_bounds (GtkEditable *editable,
2345 GtkEntry *entry = GTK_ENTRY (editable);
2347 *start = entry->selection_bound;
2348 *end = entry->current_pos;
2350 return (entry->selection_bound != entry->current_pos);
2354 gtk_entry_style_set (GtkWidget *widget,
2355 GtkStyle *previous_style)
2357 GtkEntry *entry = GTK_ENTRY (widget);
2358 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
2360 gboolean interior_focus;
2362 gtk_widget_style_get (widget,
2363 "focus-line-width", &focus_width,
2364 "interior-focus", &interior_focus,
2367 priv->focus_width = focus_width;
2368 priv->interior_focus = interior_focus;
2370 gtk_entry_recompute (entry);
2372 if (previous_style && GTK_WIDGET_REALIZED (widget))
2374 gdk_window_set_background (widget->window, &widget->style->base[GTK_WIDGET_STATE (widget)]);
2375 gdk_window_set_background (entry->text_area, &widget->style->base[GTK_WIDGET_STATE (widget)]);
2379 /* GtkCellEditable method implementations
2382 gtk_cell_editable_entry_activated (GtkEntry *entry, gpointer data)
2384 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (entry));
2385 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (entry));
2389 gtk_cell_editable_key_press_event (GtkEntry *entry,
2390 GdkEventKey *key_event,
2393 if (key_event->keyval == GDK_Escape)
2395 entry->editing_canceled = TRUE;
2396 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (entry));
2397 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (entry));
2402 /* override focus */
2403 if (key_event->keyval == GDK_Up || key_event->keyval == GDK_Down)
2405 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (entry));
2406 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (entry));
2415 gtk_entry_start_editing (GtkCellEditable *cell_editable,
2418 GTK_ENTRY (cell_editable)->is_cell_renderer = TRUE;
2420 g_signal_connect (cell_editable, "activate",
2421 G_CALLBACK (gtk_cell_editable_entry_activated), NULL);
2422 g_signal_connect (cell_editable, "key_press_event",
2423 G_CALLBACK (gtk_cell_editable_key_press_event), NULL);
2427 gtk_entry_password_hint_free (GtkEntryPasswordHint *password_hint)
2429 if (password_hint->password_hint_timeout_id)
2430 g_source_remove (password_hint->password_hint_timeout_id);
2432 g_free (password_hint);
2435 /* Default signal handlers
2438 gtk_entry_real_insert_text (GtkEditable *editable,
2439 const gchar *new_text,
2440 gint new_text_length,
2443 GtkEntry *entry = GTK_ENTRY (editable);
2447 if (new_text_length < 0)
2448 new_text_length = strlen (new_text);
2450 n_chars = g_utf8_strlen (new_text, new_text_length);
2451 if (entry->text_max_length > 0 && n_chars + entry->text_length > entry->text_max_length)
2453 gtk_widget_error_bell (GTK_WIDGET (entry));
2454 n_chars = entry->text_max_length - entry->text_length;
2455 new_text_length = g_utf8_offset_to_pointer (new_text, n_chars) - new_text;
2458 if (new_text_length + entry->n_bytes + 1 > entry->text_size)
2460 gsize prev_size = entry->text_size;
2462 while (new_text_length + entry->n_bytes + 1 > entry->text_size)
2464 if (entry->text_size == 0)
2465 entry->text_size = MIN_SIZE;
2468 if (2 * (guint)entry->text_size < MAX_SIZE &&
2469 2 * (guint)entry->text_size > entry->text_size)
2470 entry->text_size *= 2;
2473 entry->text_size = MAX_SIZE;
2474 if (new_text_length > (gint)entry->text_size - (gint)entry->n_bytes - 1)
2476 new_text_length = (gint)entry->text_size - (gint)entry->n_bytes - 1;
2477 new_text_length = g_utf8_find_prev_char (new_text, new_text + new_text_length + 1) - new_text;
2478 n_chars = g_utf8_strlen (new_text, new_text_length);
2486 entry->text = g_realloc (entry->text, entry->text_size);
2489 /* Same thing, just slower and without leaving stuff in memory. */
2490 gchar *et_new = g_malloc (entry->text_size);
2491 memcpy (et_new, entry->text, MIN (prev_size, entry->text_size));
2492 trash_area (entry->text, prev_size);
2493 g_free (entry->text);
2494 entry->text = et_new;
2498 index = g_utf8_offset_to_pointer (entry->text, *position) - entry->text;
2500 g_memmove (entry->text + index + new_text_length, entry->text + index, entry->n_bytes - index);
2501 memcpy (entry->text + index, new_text, new_text_length);
2503 entry->n_bytes += new_text_length;
2504 entry->text_length += n_chars;
2506 /* NUL terminate for safety and convenience */
2507 entry->text[entry->n_bytes] = '\0';
2509 if (entry->current_pos > *position)
2510 entry->current_pos += n_chars;
2512 if (entry->selection_bound > *position)
2513 entry->selection_bound += n_chars;
2515 if (n_chars == 1 && !entry->visible && (new_text_length < PASSWORD_HINT_MAX))
2517 guint password_hint_timeout;
2519 g_object_get (gtk_widget_get_settings (GTK_WIDGET (entry)),
2520 "gtk-entry-password-hint-timeout", &password_hint_timeout,
2523 if (password_hint_timeout > 0)
2525 GtkEntryPasswordHint *password_hint = g_object_get_qdata (G_OBJECT (entry),
2526 quark_password_hint);
2530 password_hint = g_new0 (GtkEntryPasswordHint, 1);
2531 g_object_set_qdata_full (G_OBJECT (entry), quark_password_hint,
2533 (GDestroyNotify) gtk_entry_password_hint_free);
2536 memset (&password_hint->password_hint, 0x0, PASSWORD_HINT_MAX);
2537 password_hint->password_hint_length = new_text_length;
2538 memcpy (&password_hint->password_hint, new_text, new_text_length);
2539 password_hint->password_hint_position = *position + n_chars;
2544 g_object_set_qdata (G_OBJECT (entry), quark_password_hint, NULL);
2547 *position += n_chars;
2549 gtk_entry_recompute (entry);
2551 emit_changed (entry);
2552 g_object_notify (G_OBJECT (editable), "text");
2556 gtk_entry_real_delete_text (GtkEditable *editable,
2560 GtkEntry *entry = GTK_ENTRY (editable);
2564 if (end_pos < 0 || end_pos > entry->text_length)
2565 end_pos = entry->text_length;
2567 if (start_pos < end_pos)
2569 gint start_index = g_utf8_offset_to_pointer (entry->text, start_pos) - entry->text;
2570 gint end_index = g_utf8_offset_to_pointer (entry->text, end_pos) - entry->text;
2572 gint selection_bound;
2574 g_memmove (entry->text + start_index, entry->text + end_index, entry->n_bytes + 1 - end_index);
2575 entry->text_length -= (end_pos - start_pos);
2576 entry->n_bytes -= (end_index - start_index);
2578 /* In password-mode, make sure we don't leave anything sensitive after
2579 * the terminating zero. Note, that the terminating zero already trashed
2582 if (!entry->visible)
2583 trash_area (entry->text + entry->n_bytes + 1, end_index - start_index - 1);
2585 current_pos = entry->current_pos;
2586 if (current_pos > start_pos)
2587 current_pos -= MIN (current_pos, end_pos) - start_pos;
2589 selection_bound = entry->selection_bound;
2590 if (selection_bound > start_pos)
2591 selection_bound -= MIN (selection_bound, end_pos) - start_pos;
2593 gtk_entry_set_positions (entry, current_pos, selection_bound);
2595 /* We might have deleted the selection
2597 gtk_entry_update_primary_selection (entry);
2599 gtk_entry_recompute (entry);
2601 emit_changed (entry);
2602 g_object_notify (G_OBJECT (editable), "text");
2606 /* Compute the X position for an offset that corresponds to the "more important
2607 * cursor position for that offset. We use this when trying to guess to which
2608 * end of the selection we should go to when the user hits the left or
2612 get_better_cursor_x (GtkEntry *entry,
2615 GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (entry)));
2616 PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
2617 gboolean split_cursor;
2619 PangoLayout *layout = gtk_entry_ensure_layout (entry, TRUE);
2620 const gchar *text = pango_layout_get_text (layout);
2621 gint index = g_utf8_offset_to_pointer (text, offset) - text;
2623 PangoRectangle strong_pos, weak_pos;
2625 g_object_get (gtk_widget_get_settings (GTK_WIDGET (entry)),
2626 "gtk-split-cursor", &split_cursor,
2629 pango_layout_get_cursor_pos (layout, index, &strong_pos, &weak_pos);
2632 return strong_pos.x / PANGO_SCALE;
2634 return (keymap_direction == entry->resolved_dir) ? strong_pos.x / PANGO_SCALE : weak_pos.x / PANGO_SCALE;
2638 gtk_entry_move_cursor (GtkEntry *entry,
2639 GtkMovementStep step,
2641 gboolean extend_selection)
2643 gint new_pos = entry->current_pos;
2645 _gtk_entry_reset_im_context (entry);
2647 if (entry->current_pos != entry->selection_bound && !extend_selection)
2649 /* If we have a current selection and aren't extending it, move to the
2650 * start/or end of the selection as appropriate
2654 case GTK_MOVEMENT_VISUAL_POSITIONS:
2656 gint current_x = get_better_cursor_x (entry, entry->current_pos);
2657 gint bound_x = get_better_cursor_x (entry, entry->selection_bound);
2660 new_pos = current_x < bound_x ? entry->current_pos : entry->selection_bound;
2662 new_pos = current_x > bound_x ? entry->current_pos : entry->selection_bound;
2665 case GTK_MOVEMENT_LOGICAL_POSITIONS:
2666 case GTK_MOVEMENT_WORDS:
2668 new_pos = MIN (entry->current_pos, entry->selection_bound);
2670 new_pos = MAX (entry->current_pos, entry->selection_bound);
2672 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
2673 case GTK_MOVEMENT_PARAGRAPH_ENDS:
2674 case GTK_MOVEMENT_BUFFER_ENDS:
2675 new_pos = count < 0 ? 0 : entry->text_length;
2677 case GTK_MOVEMENT_DISPLAY_LINES:
2678 case GTK_MOVEMENT_PARAGRAPHS:
2679 case GTK_MOVEMENT_PAGES:
2680 case GTK_MOVEMENT_HORIZONTAL_PAGES:
2688 case GTK_MOVEMENT_LOGICAL_POSITIONS:
2689 new_pos = gtk_entry_move_logically (entry, new_pos, count);
2691 case GTK_MOVEMENT_VISUAL_POSITIONS:
2692 new_pos = gtk_entry_move_visually (entry, new_pos, count);
2693 if (entry->current_pos == new_pos)
2695 if (!extend_selection)
2697 if (!gtk_widget_keynav_failed (GTK_WIDGET (entry),
2699 GTK_DIR_RIGHT : GTK_DIR_LEFT))
2701 GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (entry));
2704 gtk_widget_child_focus (toplevel,
2706 GTK_DIR_RIGHT : GTK_DIR_LEFT);
2711 gtk_widget_error_bell (GTK_WIDGET (entry));
2715 case GTK_MOVEMENT_WORDS:
2718 new_pos = gtk_entry_move_forward_word (entry, new_pos, FALSE);
2723 new_pos = gtk_entry_move_backward_word (entry, new_pos, FALSE);
2726 if (entry->current_pos == new_pos)
2727 gtk_widget_error_bell (GTK_WIDGET (entry));
2729 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
2730 case GTK_MOVEMENT_PARAGRAPH_ENDS:
2731 case GTK_MOVEMENT_BUFFER_ENDS:
2732 new_pos = count < 0 ? 0 : entry->text_length;
2733 if (entry->current_pos == new_pos)
2734 gtk_widget_error_bell (GTK_WIDGET (entry));
2736 case GTK_MOVEMENT_DISPLAY_LINES:
2737 case GTK_MOVEMENT_PARAGRAPHS:
2738 case GTK_MOVEMENT_PAGES:
2739 case GTK_MOVEMENT_HORIZONTAL_PAGES:
2744 if (extend_selection)
2745 gtk_editable_select_region (GTK_EDITABLE (entry), entry->selection_bound, new_pos);
2747 gtk_editable_set_position (GTK_EDITABLE (entry), new_pos);
2749 gtk_entry_pend_cursor_blink (entry);
2753 gtk_entry_insert_at_cursor (GtkEntry *entry,
2756 GtkEditable *editable = GTK_EDITABLE (entry);
2757 gint pos = entry->current_pos;
2759 if (entry->editable)
2761 _gtk_entry_reset_im_context (entry);
2763 gtk_editable_insert_text (editable, str, -1, &pos);
2764 gtk_editable_set_position (editable, pos);
2769 gtk_entry_delete_from_cursor (GtkEntry *entry,
2773 GtkEditable *editable = GTK_EDITABLE (entry);
2774 gint start_pos = entry->current_pos;
2775 gint end_pos = entry->current_pos;
2776 gint old_n_bytes = entry->n_bytes;
2778 _gtk_entry_reset_im_context (entry);
2780 if (!entry->editable)
2782 gtk_widget_error_bell (GTK_WIDGET (entry));
2786 if (entry->selection_bound != entry->current_pos)
2788 gtk_editable_delete_selection (editable);
2794 case GTK_DELETE_CHARS:
2795 end_pos = gtk_entry_move_logically (entry, entry->current_pos, count);
2796 gtk_editable_delete_text (editable, MIN (start_pos, end_pos), MAX (start_pos, end_pos));
2798 case GTK_DELETE_WORDS:
2801 /* Move to end of current word, or if not on a word, end of previous word */
2802 end_pos = gtk_entry_move_backward_word (entry, end_pos, FALSE);
2803 end_pos = gtk_entry_move_forward_word (entry, end_pos, FALSE);
2807 /* Move to beginning of current word, or if not on a word, begining of next word */
2808 start_pos = gtk_entry_move_forward_word (entry, start_pos, FALSE);
2809 start_pos = gtk_entry_move_backward_word (entry, start_pos, FALSE);
2813 case GTK_DELETE_WORD_ENDS:
2816 start_pos = gtk_entry_move_backward_word (entry, start_pos, FALSE);
2821 end_pos = gtk_entry_move_forward_word (entry, end_pos, FALSE);
2824 gtk_editable_delete_text (editable, start_pos, end_pos);
2826 case GTK_DELETE_DISPLAY_LINE_ENDS:
2827 case GTK_DELETE_PARAGRAPH_ENDS:
2829 gtk_editable_delete_text (editable, 0, entry->current_pos);
2831 gtk_editable_delete_text (editable, entry->current_pos, -1);
2833 case GTK_DELETE_DISPLAY_LINES:
2834 case GTK_DELETE_PARAGRAPHS:
2835 gtk_editable_delete_text (editable, 0, -1);
2837 case GTK_DELETE_WHITESPACE:
2838 gtk_entry_delete_whitespace (entry);
2842 if (entry->n_bytes == old_n_bytes)
2843 gtk_widget_error_bell (GTK_WIDGET (entry));
2845 gtk_entry_pend_cursor_blink (entry);
2849 gtk_entry_backspace (GtkEntry *entry)
2851 GtkEditable *editable = GTK_EDITABLE (entry);
2854 _gtk_entry_reset_im_context (entry);
2856 if (!entry->editable || !entry->text)
2858 gtk_widget_error_bell (GTK_WIDGET (entry));
2862 if (entry->selection_bound != entry->current_pos)
2864 gtk_editable_delete_selection (editable);
2868 prev_pos = gtk_entry_move_logically(entry, entry->current_pos, -1);
2870 if (prev_pos < entry->current_pos)
2872 PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
2873 PangoLogAttr *log_attrs;
2876 pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
2878 if (log_attrs[entry->current_pos].backspace_deletes_character)
2880 gchar *cluster_text;
2881 gchar *normalized_text;
2884 cluster_text = gtk_editable_get_chars (editable,
2886 entry->current_pos);
2887 normalized_text = g_utf8_normalize (cluster_text,
2888 strlen (cluster_text),
2890 len = g_utf8_strlen (normalized_text, -1);
2892 gtk_editable_delete_text (editable, prev_pos, entry->current_pos);
2895 gint pos = entry->current_pos;
2897 gtk_editable_insert_text (editable, normalized_text,
2898 g_utf8_offset_to_pointer (normalized_text, len - 1) - normalized_text,
2900 gtk_editable_set_position (editable, pos);
2903 g_free (normalized_text);
2904 g_free (cluster_text);
2908 gtk_editable_delete_text (editable, prev_pos, entry->current_pos);
2915 gtk_widget_error_bell (GTK_WIDGET (entry));
2918 gtk_entry_pend_cursor_blink (entry);
2922 gtk_entry_copy_clipboard (GtkEntry *entry)
2924 GtkEditable *editable = GTK_EDITABLE (entry);
2927 if (gtk_editable_get_selection_bounds (editable, &start, &end))
2929 gchar *str = gtk_entry_get_public_chars (entry, start, end);
2930 gtk_clipboard_set_text (gtk_widget_get_clipboard (GTK_WIDGET (entry),
2931 GDK_SELECTION_CLIPBOARD),
2938 gtk_entry_cut_clipboard (GtkEntry *entry)
2940 GtkEditable *editable = GTK_EDITABLE (entry);
2943 gtk_entry_copy_clipboard (entry);
2945 if (entry->editable)
2947 if (gtk_editable_get_selection_bounds (editable, &start, &end))
2948 gtk_editable_delete_text (editable, start, end);
2952 gtk_widget_error_bell (GTK_WIDGET (entry));
2957 gtk_entry_paste_clipboard (GtkEntry *entry)
2959 if (entry->editable)
2960 gtk_entry_paste (entry, GDK_NONE);
2962 gtk_widget_error_bell (GTK_WIDGET (entry));
2966 gtk_entry_delete_cb (GtkEntry *entry)
2968 GtkEditable *editable = GTK_EDITABLE (entry);
2971 if (entry->editable)
2973 if (gtk_editable_get_selection_bounds (editable, &start, &end))
2974 gtk_editable_delete_text (editable, start, end);
2979 gtk_entry_toggle_overwrite (GtkEntry *entry)
2981 entry->overwrite_mode = !entry->overwrite_mode;
2982 gtk_entry_pend_cursor_blink (entry);
2983 gtk_widget_queue_draw (GTK_WIDGET (entry));
2987 gtk_entry_select_all (GtkEntry *entry)
2989 gtk_entry_select_line (entry);
2993 gtk_entry_real_activate (GtkEntry *entry)
2996 GtkWidget *toplevel;
2999 widget = GTK_WIDGET (entry);
3001 if (entry->activates_default)
3003 toplevel = gtk_widget_get_toplevel (widget);
3004 if (GTK_IS_WINDOW (toplevel))
3006 window = GTK_WINDOW (toplevel);
3009 widget != window->default_widget &&
3010 !(widget == window->focus_widget &&
3011 (!window->default_widget || !GTK_WIDGET_SENSITIVE (window->default_widget))))
3012 gtk_window_activate_default (window);
3018 gtk_entry_keymap_direction_changed (GdkKeymap *keymap,
3021 gtk_entry_recompute (entry);
3024 /* IM Context Callbacks
3028 gtk_entry_commit_cb (GtkIMContext *context,
3032 if (entry->editable)
3033 gtk_entry_enter_text (entry, str);
3037 gtk_entry_preedit_changed_cb (GtkIMContext *context,
3040 if (entry->editable)
3042 gchar *preedit_string;
3045 gtk_im_context_get_preedit_string (entry->im_context,
3046 &preedit_string, NULL,
3048 entry->preedit_length = strlen (preedit_string);
3049 cursor_pos = CLAMP (cursor_pos, 0, g_utf8_strlen (preedit_string, -1));
3050 entry->preedit_cursor = cursor_pos;
3051 g_free (preedit_string);
3053 gtk_entry_recompute (entry);
3058 gtk_entry_retrieve_surrounding_cb (GtkIMContext *context,
3061 gtk_im_context_set_surrounding (context,
3064 g_utf8_offset_to_pointer (entry->text, entry->current_pos) - entry->text);
3070 gtk_entry_delete_surrounding_cb (GtkIMContext *slave,
3075 if (entry->editable)
3076 gtk_editable_delete_text (GTK_EDITABLE (entry),
3077 entry->current_pos + offset,
3078 entry->current_pos + offset + n_chars);
3083 /* Internal functions
3086 /* Used for im_commit_cb and inserting Unicode chars */
3088 gtk_entry_enter_text (GtkEntry *entry,
3091 GtkEditable *editable = GTK_EDITABLE (entry);
3093 gboolean old_need_im_reset;
3095 old_need_im_reset = entry->need_im_reset;
3096 entry->need_im_reset = FALSE;
3098 if (gtk_editable_get_selection_bounds (editable, NULL, NULL))
3099 gtk_editable_delete_selection (editable);
3102 if (entry->overwrite_mode)
3103 gtk_entry_delete_from_cursor (entry, GTK_DELETE_CHARS, 1);
3106 tmp_pos = entry->current_pos;
3107 gtk_editable_insert_text (editable, str, strlen (str), &tmp_pos);
3108 gtk_editable_set_position (editable, tmp_pos);
3110 entry->need_im_reset = old_need_im_reset;
3113 /* All changes to entry->current_pos and entry->selection_bound
3114 * should go through this function.
3117 gtk_entry_set_positions (GtkEntry *entry,
3119 gint selection_bound)
3121 gboolean changed = FALSE;
3123 g_object_freeze_notify (G_OBJECT (entry));
3125 if (current_pos != -1 &&
3126 entry->current_pos != current_pos)
3128 entry->current_pos = current_pos;
3131 g_object_notify (G_OBJECT (entry), "cursor-position");
3134 if (selection_bound != -1 &&
3135 entry->selection_bound != selection_bound)
3137 entry->selection_bound = selection_bound;
3140 g_object_notify (G_OBJECT (entry), "selection-bound");
3143 g_object_thaw_notify (G_OBJECT (entry));
3147 gtk_entry_move_adjustments (entry);
3148 gtk_entry_recompute (entry);
3153 gtk_entry_reset_layout (GtkEntry *entry)
3155 if (entry->cached_layout)
3157 g_object_unref (entry->cached_layout);
3158 entry->cached_layout = NULL;
3163 update_im_cursor_location (GtkEntry *entry)
3167 gint strong_xoffset;
3168 gint area_width, area_height;
3170 gtk_entry_get_cursor_locations (entry, CURSOR_STANDARD, &strong_x, NULL)
3172 get_text_area_size (entry, NULL, NULL, &area_width, &area_height);
3174 strong_xoffset = strong_x - entry->scroll_offset;
3175 if (strong_xoffset < 0)
3179 else if (strong_xoffset > area_width)
3181 strong_xoffset = area_width;
3183 area.x = strong_xoffset;
3186 area.height = area_height;
3188 gtk_im_context_set_cursor_location (entry->im_context, &area);
3192 recompute_idle_func (gpointer data)
3196 entry = GTK_ENTRY (data);
3198 entry->recompute_idle = 0;
3200 if (gtk_widget_has_screen (GTK_WIDGET (entry)))
3202 gtk_entry_adjust_scroll (entry);
3203 gtk_entry_queue_draw (entry);
3205 update_im_cursor_location (entry);
3212 gtk_entry_recompute (GtkEntry *entry)
3214 gtk_entry_reset_layout (entry);
3215 gtk_entry_check_cursor_blink (entry);
3217 if (!entry->recompute_idle)
3219 entry->recompute_idle = gdk_threads_add_idle_full (G_PRIORITY_HIGH_IDLE + 15, /* between resize and redraw */
3220 recompute_idle_func, entry, NULL);
3225 append_char (GString *str,
3233 char_len = g_unichar_to_utf8 (ch, buf);
3238 g_string_append_len (str, buf, char_len);
3244 gtk_entry_remove_password_hint (gpointer data)
3246 /* Force the string to be redrawn, but now without a visible character */
3247 gtk_entry_recompute (GTK_ENTRY (data));
3252 static PangoLayout *
3253 gtk_entry_create_layout (GtkEntry *entry,
3254 gboolean include_preedit)
3256 GtkWidget *widget = GTK_WIDGET (entry);
3257 PangoLayout *layout = gtk_widget_create_pango_layout (widget, NULL);
3258 PangoAttrList *tmp_attrs = pango_attr_list_new ();
3260 gchar *preedit_string = NULL;
3261 gint preedit_length = 0;
3262 PangoAttrList *preedit_attrs = NULL;
3264 pango_layout_set_single_paragraph_mode (layout, TRUE);
3266 if (include_preedit)
3268 gtk_im_context_get_preedit_string (entry->im_context,
3269 &preedit_string, &preedit_attrs, NULL);
3270 preedit_length = entry->preedit_length;
3275 GString *tmp_string = g_string_new (NULL);
3277 gint cursor_index = g_utf8_offset_to_pointer (entry->text, entry->current_pos) - entry->text;
3281 g_string_prepend_len (tmp_string, entry->text, entry->n_bytes);
3282 g_string_insert (tmp_string, cursor_index, preedit_string);
3287 gint preedit_len_chars;
3288 gunichar invisible_char;
3290 ch_len = g_utf8_strlen (entry->text, entry->n_bytes);
3291 preedit_len_chars = g_utf8_strlen (preedit_string, -1);
3292 ch_len += preedit_len_chars;
3294 if (entry->invisible_char != 0)
3295 invisible_char = entry->invisible_char;
3297 invisible_char = ' '; /* just pick a char */
3299 append_char (tmp_string, invisible_char, ch_len);
3301 /* Fix cursor index to point to invisible char corresponding
3302 * to the preedit, fix preedit_length to be the length of
3303 * the invisible chars representing the preedit
3306 g_utf8_offset_to_pointer (tmp_string->str, entry->current_pos) -
3310 g_unichar_to_utf8 (invisible_char, NULL);
3313 pango_layout_set_text (layout, tmp_string->str, tmp_string->len);
3315 pango_attr_list_splice (tmp_attrs, preedit_attrs,
3316 cursor_index, preedit_length);
3318 g_string_free (tmp_string, TRUE);
3322 PangoDirection pango_dir;
3325 pango_dir = pango_find_base_dir (entry->text, entry->n_bytes);
3328 pango_dir = PANGO_DIRECTION_NEUTRAL;
3330 if (pango_dir == PANGO_DIRECTION_NEUTRAL)
3332 if (GTK_WIDGET_HAS_FOCUS (widget))
3334 GdkDisplay *display = gtk_widget_get_display (widget);
3335 GdkKeymap *keymap = gdk_keymap_get_for_display (display);
3336 if (gdk_keymap_get_direction (keymap) == PANGO_DIRECTION_RTL)
3337 pango_dir = PANGO_DIRECTION_RTL;
3339 pango_dir = PANGO_DIRECTION_LTR;
3343 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
3344 pango_dir = PANGO_DIRECTION_RTL;
3346 pango_dir = PANGO_DIRECTION_LTR;
3350 pango_context_set_base_dir (gtk_widget_get_pango_context (widget),
3353 pango_layout_set_alignment (layout, pango_dir);
3355 entry->resolved_dir = pango_dir;
3359 pango_layout_set_text (layout, entry->text, entry->n_bytes);
3363 GString *str = g_string_new (NULL);
3364 gunichar invisible_char;
3365 guint password_hint_timeout;
3366 GtkEntryPasswordHint *password_hint;
3368 g_object_get (gtk_widget_get_settings (widget),
3369 "gtk-entry-password-hint-timeout", &password_hint_timeout,
3372 if (entry->invisible_char != 0)
3373 invisible_char = entry->invisible_char;
3375 invisible_char = ' '; /* just pick a char */
3377 password_hint = g_object_get_qdata (G_OBJECT (entry),
3378 quark_password_hint);
3380 if (password_hint && password_hint->password_hint_timeout_id)
3382 g_source_remove (password_hint->password_hint_timeout_id);
3383 password_hint->password_hint_timeout_id = 0;
3386 if (password_hint_timeout == 0 || password_hint == NULL ||
3387 (password_hint && password_hint->password_hint_length == 0))
3389 append_char (str, invisible_char, entry->text_length);
3391 else if (password_hint)
3393 /* Draw hidden characters upto the inserted position,
3394 * then the real thing, pad up to full length
3396 if (password_hint->password_hint_position > 1)
3397 append_char (str, invisible_char,
3398 password_hint->password_hint_position - 1);
3400 g_string_append_len (str, password_hint->password_hint,
3401 password_hint->password_hint_length);
3403 if (password_hint->password_hint_position < entry->text_length)
3404 append_char (str, invisible_char,
3405 entry->text_length -
3406 password_hint->password_hint_position);
3408 /* Now remove this last input character, don't need
3411 memset (password_hint->password_hint, 0, PASSWORD_HINT_MAX);
3412 password_hint->password_hint_length = 0;
3414 password_hint->password_hint_timeout_id =
3415 gdk_threads_add_timeout (password_hint_timeout,
3416 (GSourceFunc) gtk_entry_remove_password_hint,
3420 pango_layout_set_text (layout, str->str, str->len);
3421 g_string_free (str, TRUE);
3425 pango_layout_set_attributes (layout, tmp_attrs);
3427 g_free (preedit_string);
3429 pango_attr_list_unref (preedit_attrs);
3431 pango_attr_list_unref (tmp_attrs);
3436 static PangoLayout *
3437 gtk_entry_ensure_layout (GtkEntry *entry,
3438 gboolean include_preedit)
3440 if (entry->preedit_length > 0 &&
3441 !include_preedit != !entry->cache_includes_preedit)
3442 gtk_entry_reset_layout (entry);
3444 if (!entry->cached_layout)
3446 entry->cached_layout = gtk_entry_create_layout (entry, include_preedit);
3447 entry->cache_includes_preedit = include_preedit;
3450 return entry->cached_layout;
3454 get_layout_position (GtkEntry *entry,
3458 PangoLayout *layout;
3459 PangoRectangle logical_rect;
3460 gint area_width, area_height;
3461 GtkBorder inner_border;
3463 PangoLayoutLine *line;
3465 layout = gtk_entry_ensure_layout (entry, TRUE);
3467 get_text_area_size (entry, NULL, NULL, &area_width, &area_height);
3468 _gtk_entry_effective_inner_border (entry, &inner_border);
3470 area_height = PANGO_SCALE * (area_height - inner_border.top - inner_border.bottom);
3472 line = pango_layout_get_lines_readonly (layout)->data;
3473 pango_layout_line_get_extents (line, NULL, &logical_rect);
3475 /* Align primarily for locale's ascent/descent */
3476 y_pos = ((area_height - entry->ascent - entry->descent) / 2 +
3477 entry->ascent + logical_rect.y);
3479 /* Now see if we need to adjust to fit in actual drawn string */
3480 if (logical_rect.height > area_height)
3481 y_pos = (area_height - logical_rect.height) / 2;
3484 else if (y_pos + logical_rect.height > area_height)
3485 y_pos = area_height - logical_rect.height;
3487 y_pos = inner_border.top + y_pos / PANGO_SCALE;
3490 *x = inner_border.left - entry->scroll_offset;
3497 gtk_entry_draw_text (GtkEntry *entry)
3501 if (!entry->visible && entry->invisible_char == 0)
3504 if (GTK_WIDGET_DRAWABLE (entry))
3506 PangoLayout *layout = gtk_entry_ensure_layout (entry, TRUE);
3509 gint start_pos, end_pos;
3511 widget = GTK_WIDGET (entry);
3513 get_layout_position (entry, &x, &y);
3515 cr = gdk_cairo_create (entry->text_area);
3517 cairo_move_to (cr, x, y);
3518 gdk_cairo_set_source_color (cr, &widget->style->text [widget->state]);
3519 pango_cairo_show_layout (cr, layout);
3521 if (gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &start_pos, &end_pos))
3525 PangoRectangle logical_rect;
3526 GdkColor *selection_color, *text_color;
3527 GtkBorder inner_border;
3529 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
3530 gtk_entry_get_pixel_ranges (entry, &ranges, &n_ranges);
3532 if (GTK_WIDGET_HAS_FOCUS (entry))
3534 selection_color = &widget->style->base [GTK_STATE_SELECTED];
3535 text_color = &widget->style->text [GTK_STATE_SELECTED];
3539 selection_color = &widget->style->base [GTK_STATE_ACTIVE];
3540 text_color = &widget->style->text [GTK_STATE_ACTIVE];
3543 _gtk_entry_effective_inner_border (entry, &inner_border);
3545 for (i = 0; i < n_ranges; ++i)
3546 cairo_rectangle (cr,
3547 inner_border.left - entry->scroll_offset + ranges[2 * i],
3550 logical_rect.height);
3554 gdk_cairo_set_source_color (cr, selection_color);
3557 cairo_move_to (cr, x, y);
3558 gdk_cairo_set_source_color (cr, text_color);
3559 pango_cairo_show_layout (cr, layout);
3569 draw_insertion_cursor (GtkEntry *entry,
3570 GdkRectangle *cursor_location,
3571 gboolean is_primary,
3572 PangoDirection direction,
3573 gboolean draw_arrow)
3575 GtkWidget *widget = GTK_WIDGET (entry);
3576 GtkTextDirection text_dir;
3578 if (direction == PANGO_DIRECTION_LTR)
3579 text_dir = GTK_TEXT_DIR_LTR;
3581 text_dir = GTK_TEXT_DIR_RTL;
3583 gtk_draw_insertion_cursor (widget, entry->text_area, NULL,
3585 is_primary, text_dir, draw_arrow);
3589 gtk_entry_draw_cursor (GtkEntry *entry,
3592 GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (entry)));
3593 PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
3595 if (GTK_WIDGET_DRAWABLE (entry))
3597 GtkWidget *widget = GTK_WIDGET (entry);
3598 GdkRectangle cursor_location;
3599 gboolean split_cursor;
3600 PangoRectangle cursor_rect;
3601 GtkBorder inner_border;
3603 gint text_area_height;
3606 gboolean block_at_line_end;
3608 _gtk_entry_effective_inner_border (entry, &inner_border);
3610 xoffset = inner_border.left - entry->scroll_offset;
3612 gdk_drawable_get_size (entry->text_area, NULL, &text_area_height);
3614 cursor_index = g_utf8_offset_to_pointer (entry->text, entry->current_pos + entry->preedit_cursor) - entry->text;
3615 if (!entry->overwrite_mode)
3618 block = _gtk_text_util_get_block_cursor_location (gtk_entry_ensure_layout (entry, TRUE),
3619 cursor_index, &cursor_rect, &block_at_line_end);
3623 gint strong_x, weak_x;
3624 PangoDirection dir1 = PANGO_DIRECTION_NEUTRAL;
3625 PangoDirection dir2 = PANGO_DIRECTION_NEUTRAL;
3629 gtk_entry_get_cursor_locations (entry, type, &strong_x, &weak_x);
3631 g_object_get (gtk_widget_get_settings (widget),
3632 "gtk-split-cursor", &split_cursor,
3635 dir1 = entry->resolved_dir;
3641 if (weak_x != strong_x)
3643 dir2 = (entry->resolved_dir == PANGO_DIRECTION_LTR) ? PANGO_DIRECTION_RTL : PANGO_DIRECTION_LTR;
3649 if (keymap_direction == entry->resolved_dir)
3655 cursor_location.x = xoffset + x1;
3656 cursor_location.y = inner_border.top;
3657 cursor_location.width = 0;
3658 cursor_location.height = text_area_height - inner_border.top - inner_border.bottom;
3660 draw_insertion_cursor (entry,
3661 &cursor_location, TRUE, dir1,
3662 dir2 != PANGO_DIRECTION_NEUTRAL);
3664 if (dir2 != PANGO_DIRECTION_NEUTRAL)
3666 cursor_location.x = xoffset + x2;
3667 draw_insertion_cursor (entry,
3668 &cursor_location, FALSE, dir2,
3672 else /* overwrite_mode */
3674 PangoLayout *layout = gtk_entry_ensure_layout (entry, TRUE);
3675 GdkColor cursor_color;
3680 get_layout_position (entry, &x, &y);
3682 rect.x = PANGO_PIXELS (cursor_rect.x) + x;
3683 rect.y = PANGO_PIXELS (cursor_rect.y) + y;
3684 rect.width = PANGO_PIXELS (cursor_rect.width);
3685 rect.height = PANGO_PIXELS (cursor_rect.height);
3687 cr = gdk_cairo_create (entry->text_area);
3689 _gtk_widget_get_cursor_color (widget, &cursor_color);
3690 gdk_cairo_set_source_color (cr, &cursor_color);
3691 gdk_cairo_rectangle (cr, &rect);
3694 if (!block_at_line_end)
3696 gdk_cairo_rectangle (cr, &rect);
3698 cairo_move_to (cr, x, y);
3699 gdk_cairo_set_source_color (cr, &widget->style->base[widget->state]);
3700 pango_cairo_show_layout (cr, layout);
3709 gtk_entry_queue_draw (GtkEntry *entry)
3711 if (GTK_WIDGET_DRAWABLE (entry))
3712 gdk_window_invalidate_rect (entry->text_area, NULL, FALSE);
3716 _gtk_entry_reset_im_context (GtkEntry *entry)
3718 if (entry->need_im_reset)
3720 entry->need_im_reset = 0;
3721 gtk_im_context_reset (entry->im_context);
3726 gtk_entry_find_position (GtkEntry *entry,
3729 PangoLayout *layout;
3730 PangoLayoutLine *line;
3737 layout = gtk_entry_ensure_layout (entry, TRUE);
3738 text = pango_layout_get_text (layout);
3739 cursor_index = g_utf8_offset_to_pointer (text, entry->current_pos) - text;
3741 line = pango_layout_get_lines_readonly (layout)->data;
3742 pango_layout_line_x_to_index (line, x * PANGO_SCALE, &index, &trailing);
3744 if (index >= cursor_index && entry->preedit_length)
3746 if (index >= cursor_index + entry->preedit_length)
3747 index -= entry->preedit_length;
3750 index = cursor_index;
3755 pos = g_utf8_pointer_to_offset (text, text + index);
3762 gtk_entry_get_cursor_locations (GtkEntry *entry,
3767 if (!entry->visible && !entry->invisible_char)
3777 PangoLayout *layout = gtk_entry_ensure_layout (entry, TRUE);
3778 const gchar *text = pango_layout_get_text (layout);
3779 PangoRectangle strong_pos, weak_pos;
3782 if (type == CURSOR_STANDARD)
3784 index = g_utf8_offset_to_pointer (text, entry->current_pos + entry->preedit_cursor) - text;
3786 else /* type == CURSOR_DND */
3788 index = g_utf8_offset_to_pointer (text, entry->dnd_position) - text;
3790 if (entry->dnd_position > entry->current_pos)
3793 index += entry->preedit_length;
3796 gint preedit_len_chars = g_utf8_strlen (text, -1) - entry->text_length;
3797 index += preedit_len_chars * g_unichar_to_utf8 (entry->invisible_char, NULL);
3802 pango_layout_get_cursor_pos (layout, index, &strong_pos, &weak_pos);
3805 *strong_x = strong_pos.x / PANGO_SCALE;
3808 *weak_x = weak_pos.x / PANGO_SCALE;
3813 gtk_entry_adjust_scroll (GtkEntry *entry)
3815 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
3816 gint min_offset, max_offset;
3817 gint text_area_width, text_width;
3818 GtkBorder inner_border;
3819 gint strong_x, weak_x;
3820 gint strong_xoffset, weak_xoffset;
3822 PangoLayout *layout;
3823 PangoLayoutLine *line;
3824 PangoRectangle logical_rect;
3826 if (!GTK_WIDGET_REALIZED (entry))
3829 _gtk_entry_effective_inner_border (entry, &inner_border);
3831 gdk_drawable_get_size (entry->text_area, &text_area_width, NULL);
3832 text_area_width -= inner_border.left + inner_border.right;
3833 if (text_area_width < 0)
3834 text_area_width = 0;
3836 layout = gtk_entry_ensure_layout (entry, TRUE);
3837 line = pango_layout_get_lines_readonly (layout)->data;
3839 pango_layout_line_get_extents (line, NULL, &logical_rect);
3841 /* Display as much text as we can */
3843 if (entry->resolved_dir == PANGO_DIRECTION_LTR)
3844 xalign = priv->xalign;
3846 xalign = 1.0 - priv->xalign;
3848 text_width = PANGO_PIXELS(logical_rect.width);
3850 if (text_width > text_area_width)
3853 max_offset = text_width - text_area_width;
3857 min_offset = (text_width - text_area_width) * xalign;
3858 max_offset = min_offset;
3861 entry->scroll_offset = CLAMP (entry->scroll_offset, min_offset, max_offset);
3863 /* And make sure cursors are on screen. Note that the cursor is
3864 * actually drawn one pixel into the INNER_BORDER space on
3865 * the right, when the scroll is at the utmost right. This
3866 * looks better to to me than confining the cursor inside the
3867 * border entirely, though it means that the cursor gets one
3868 * pixel closer to the edge of the widget on the right than
3869 * on the left. This might need changing if one changed
3870 * INNER_BORDER from 2 to 1, as one would do on a
3871 * small-screen-real-estate display.
3873 * We always make sure that the strong cursor is on screen, and
3874 * put the weak cursor on screen if possible.
3877 gtk_entry_get_cursor_locations (entry, CURSOR_STANDARD, &strong_x, &weak_x);
3879 strong_xoffset = strong_x - entry->scroll_offset;
3881 if (strong_xoffset < 0)
3883 entry->scroll_offset += strong_xoffset;
3886 else if (strong_xoffset > text_area_width)
3888 entry->scroll_offset += strong_xoffset - text_area_width;
3889 strong_xoffset = text_area_width;
3892 weak_xoffset = weak_x - entry->scroll_offset;
3894 if (weak_xoffset < 0 && strong_xoffset - weak_xoffset <= text_area_width)
3896 entry->scroll_offset += weak_xoffset;
3898 else if (weak_xoffset > text_area_width &&
3899 strong_xoffset - (weak_xoffset - text_area_width) >= 0)
3901 entry->scroll_offset += weak_xoffset - text_area_width;
3904 g_object_notify (G_OBJECT (entry), "scroll-offset");
3908 gtk_entry_move_adjustments (GtkEntry *entry)
3910 PangoContext *context;
3911 PangoFontMetrics *metrics;
3912 gint x, layout_x, border_x, border_y;
3914 GtkAdjustment *adjustment;
3916 adjustment = g_object_get_qdata (G_OBJECT (entry), quark_cursor_hadjustment);
3920 /* Cursor position, layout offset, border width, and widget allocation */
3921 gtk_entry_get_cursor_locations (entry, CURSOR_STANDARD, &x, NULL);
3922 get_layout_position (entry, &layout_x, NULL);
3923 _gtk_entry_get_borders (entry, &border_x, &border_y);
3924 x += entry->widget.allocation.x + layout_x + border_x;
3926 /* Approximate width of a char, so user can see what is ahead/behind */
3927 context = gtk_widget_get_pango_context (GTK_WIDGET (entry));
3928 metrics = pango_context_get_metrics (context,
3929 entry->widget.style->font_desc,
3930 pango_context_get_language (context));
3931 char_width = pango_font_metrics_get_approximate_char_width (metrics) / PANGO_SCALE;
3934 gtk_adjustment_clamp_page (adjustment,
3935 x - (char_width + 1), /* one char + one pixel before */
3936 x + (char_width + 2)); /* one char + cursor + one pixel after */
3940 gtk_entry_move_visually (GtkEntry *entry,
3945 PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
3948 text = pango_layout_get_text (layout);
3950 index = g_utf8_offset_to_pointer (text, start) - text;
3954 int new_index, new_trailing;
3955 gboolean split_cursor;
3958 g_object_get (gtk_widget_get_settings (GTK_WIDGET (entry)),
3959 "gtk-split-cursor", &split_cursor,
3966 GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (entry)));
3967 PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
3969 strong = keymap_direction == entry->resolved_dir;
3974 pango_layout_move_cursor_visually (layout, strong, index, 0, 1, &new_index, &new_trailing);
3979 pango_layout_move_cursor_visually (layout, strong, index, 0, -1, &new_index, &new_trailing);
3985 else if (new_index != G_MAXINT)
3988 while (new_trailing--)
3989 index = g_utf8_next_char (text + index) - text;
3992 return g_utf8_pointer_to_offset (text, text + index);
3996 gtk_entry_move_logically (GtkEntry *entry,
4000 gint new_pos = start;
4002 /* Prevent any leak of information */
4003 if (!entry->visible)
4005 new_pos = CLAMP (start + count, 0, entry->text_length);
4007 else if (entry->text)
4009 PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
4010 PangoLogAttr *log_attrs;
4013 pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
4015 while (count > 0 && new_pos < entry->text_length)
4019 while (new_pos < entry->text_length && !log_attrs[new_pos].is_cursor_position);
4023 while (count < 0 && new_pos > 0)
4027 while (new_pos > 0 && !log_attrs[new_pos].is_cursor_position);
4039 gtk_entry_move_forward_word (GtkEntry *entry,
4041 gboolean allow_whitespace)
4043 gint new_pos = start;
4045 /* Prevent any leak of information */
4046 if (!entry->visible)
4048 new_pos = entry->text_length;
4050 else if (entry->text && (new_pos < entry->text_length))
4052 PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
4053 PangoLogAttr *log_attrs;
4056 pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
4058 /* Find the next word boundary */
4060 while (new_pos < n_attrs - 1 && !(log_attrs[new_pos].is_word_end ||
4061 (log_attrs[new_pos].is_word_start && allow_whitespace)))
4072 gtk_entry_move_backward_word (GtkEntry *entry,
4074 gboolean allow_whitespace)
4076 gint new_pos = start;
4078 /* Prevent any leak of information */
4079 if (!entry->visible)
4083 else if (entry->text && start > 0)
4085 PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
4086 PangoLogAttr *log_attrs;
4089 pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
4091 new_pos = start - 1;
4093 /* Find the previous word boundary */
4094 while (new_pos > 0 && !(log_attrs[new_pos].is_word_start ||
4095 (log_attrs[new_pos].is_word_end && allow_whitespace)))
4105 gtk_entry_delete_whitespace (GtkEntry *entry)
4107 PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
4108 PangoLogAttr *log_attrs;
4112 pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
4114 start = end = entry->current_pos;
4116 while (start > 0 && log_attrs[start-1].is_white)
4119 while (end < n_attrs && log_attrs[end].is_white)
4125 gtk_editable_delete_text (GTK_EDITABLE (entry), start, end);
4130 gtk_entry_select_word (GtkEntry *entry)
4132 gint start_pos = gtk_entry_move_backward_word (entry, entry->current_pos, TRUE);
4133 gint end_pos = gtk_entry_move_forward_word (entry, entry->current_pos, TRUE);
4135 gtk_editable_select_region (GTK_EDITABLE (entry), start_pos, end_pos);
4139 gtk_entry_select_line (GtkEntry *entry)
4141 gtk_editable_select_region (GTK_EDITABLE (entry), 0, -1);
4145 * Like gtk_editable_get_chars, but handle not-visible entries
4149 gtk_entry_get_public_chars (GtkEntry *entry,
4154 end = entry->text_length;
4157 return gtk_editable_get_chars (GTK_EDITABLE (entry), start, end);
4158 else if (!entry->invisible_char)
4159 return g_strdup ("");
4162 GString *str = g_string_new (NULL);
4163 append_char (str, entry->invisible_char, end - start);
4164 return g_string_free (str, FALSE);
4169 truncate_multiline (const gchar *text)
4174 text[length] && text[length] != '\n' && text[length] != '\r';
4181 paste_received (GtkClipboard *clipboard,
4185 GtkEntry *entry = GTK_ENTRY (data);
4186 GtkEditable *editable = GTK_EDITABLE (entry);
4187 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
4189 if (entry->button == 2)
4191 gint pos, start, end;
4192 pos = priv->insert_pos;
4193 gtk_editable_get_selection_bounds (editable, &start, &end);
4194 if (!((start <= pos && pos <= end) || (end <= pos && pos <= start)))
4195 gtk_editable_select_region (editable, pos, pos);
4200 gint pos, start, end;
4202 gboolean popup_completion;
4203 GtkEntryCompletion *completion;
4205 completion = gtk_entry_get_completion (entry);
4207 if (entry->truncate_multiline)
4208 length = truncate_multiline (text);
4210 /* only complete if the selection is at the end */
4211 popup_completion = (entry->text_length == MAX (entry->current_pos, entry->selection_bound));
4215 if (GTK_WIDGET_MAPPED (completion->priv->popup_window))
4216 _gtk_entry_completion_popdown (completion);
4218 if (!popup_completion && completion->priv->changed_id > 0)
4219 g_signal_handler_block (entry, completion->priv->changed_id);
4222 begin_change (entry);
4223 g_object_freeze_notify (G_OBJECT (entry));
4224 if (gtk_editable_get_selection_bounds (editable, &start, &end))
4225 gtk_editable_delete_text (editable, start, end);
4227 pos = entry->current_pos;
4228 gtk_editable_insert_text (editable, text, length, &pos);
4229 gtk_editable_set_position (editable, pos);
4230 g_object_thaw_notify (G_OBJECT (entry));
4234 !popup_completion && completion->priv->changed_id > 0)
4235 g_signal_handler_unblock (entry, completion->priv->changed_id);
4238 g_object_unref (entry);
4242 gtk_entry_paste (GtkEntry *entry,
4245 g_object_ref (entry);
4246 gtk_clipboard_request_text (gtk_widget_get_clipboard (GTK_WIDGET (entry), selection),
4247 paste_received, entry);
4251 primary_get_cb (GtkClipboard *clipboard,
4252 GtkSelectionData *selection_data,
4256 GtkEntry *entry = GTK_ENTRY (data);
4259 if (gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &start, &end))
4261 gchar *str = gtk_entry_get_public_chars (entry, start, end);
4262 gtk_selection_data_set_text (selection_data, str, -1);
4268 primary_clear_cb (GtkClipboard *clipboard,
4271 GtkEntry *entry = GTK_ENTRY (data);
4273 gtk_editable_select_region (GTK_EDITABLE (entry), entry->current_pos, entry->current_pos);
4277 gtk_entry_update_primary_selection (GtkEntry *entry)
4279 static GtkTargetEntry targets[] = {
4280 { "UTF8_STRING", 0, 0 },
4283 { "COMPOUND_TEXT", 0, 0 },
4284 { "text/plain;charset=utf-8", 0, 0 },
4286 { "text/plain", 0, 0 }
4289 GtkClipboard *clipboard;
4292 if (targets[5].target == NULL)
4294 const gchar *charset;
4296 g_get_charset (&charset);
4297 targets[5].target = g_strdup_printf ("text/plain;charset=%s", charset);
4300 if (!GTK_WIDGET_REALIZED (entry))
4303 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (entry), GDK_SELECTION_PRIMARY);
4305 if (gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &start, &end))
4307 if (!gtk_clipboard_set_with_owner (clipboard, targets, G_N_ELEMENTS (targets),
4308 primary_get_cb, primary_clear_cb, G_OBJECT (entry)))
4309 primary_clear_cb (clipboard, entry);
4313 if (gtk_clipboard_get_owner (clipboard) == G_OBJECT (entry))
4314 gtk_clipboard_clear (clipboard);
4324 * Creates a new entry.
4326 * Return value: a new #GtkEntry.
4329 gtk_entry_new (void)
4331 return g_object_new (GTK_TYPE_ENTRY, NULL);
4335 * gtk_entry_new_with_max_length:
4336 * @max: the maximum length of the entry, or 0 for no maximum.
4337 * (other than the maximum length of entries.) The value passed in will
4338 * be clamped to the range 0-65536.
4340 * Creates a new #GtkEntry widget with the given maximum length.
4342 * Note: the existence of this function is inconsistent
4343 * with the rest of the GTK+ API. The normal setup would
4344 * be to just require the user to make an extra call
4345 * to gtk_entry_set_max_length() instead. It is not
4346 * expected that this function will be removed, but
4347 * it would be better practice not to use it.
4349 * Return value: a new #GtkEntry.
4352 gtk_entry_new_with_max_length (gint max)
4356 max = CLAMP (max, 0, MAX_SIZE);
4358 entry = g_object_new (GTK_TYPE_ENTRY, NULL);
4359 entry->text_max_length = max;
4361 return GTK_WIDGET (entry);
4365 * gtk_entry_set_text:
4366 * @entry: a #GtkEntry
4367 * @text: the new text
4369 * Sets the text in the widget to the given
4370 * value, replacing the current contents.
4373 gtk_entry_set_text (GtkEntry *entry,
4377 GtkEntryCompletion *completion;
4379 g_return_if_fail (GTK_IS_ENTRY (entry));
4380 g_return_if_fail (text != NULL);
4382 /* Actually setting the text will affect the cursor and selection;
4383 * if the contents don't actually change, this will look odd to the user.
4385 if (strcmp (entry->text, text) == 0)
4388 completion = gtk_entry_get_completion (entry);
4389 if (completion && completion->priv->changed_id > 0)
4390 g_signal_handler_block (entry, completion->priv->changed_id);
4392 begin_change (entry);
4393 g_object_freeze_notify (G_OBJECT (entry));
4394 gtk_editable_delete_text (GTK_EDITABLE (entry), 0, -1);
4397 gtk_editable_insert_text (GTK_EDITABLE (entry), text, strlen (text), &tmp_pos);
4398 g_object_thaw_notify (G_OBJECT (entry));
4401 if (completion && completion->priv->changed_id > 0)
4402 g_signal_handler_unblock (entry, completion->priv->changed_id);
4406 * gtk_entry_append_text:
4407 * @entry: a #GtkEntry
4408 * @text: the text to append
4410 * Appends the given text to the contents of the widget.
4412 * Deprecated: gtk_entry_append_text() is deprecated and should not
4413 * be used in newly-written code. Use gtk_editable_insert_text()
4417 gtk_entry_append_text (GtkEntry *entry,
4422 g_return_if_fail (GTK_IS_ENTRY (entry));
4423 g_return_if_fail (text != NULL);
4425 tmp_pos = entry->text_length;
4426 gtk_editable_insert_text (GTK_EDITABLE (entry), text, -1, &tmp_pos);
4430 * gtk_entry_prepend_text:
4431 * @entry: a #GtkEntry
4432 * @text: the text to prepend
4434 * Prepends the given text to the contents of the widget.
4436 * Deprecated: gtk_entry_prepend_text() is deprecated and should not
4437 * be used in newly-written code. Use gtk_editable_insert_text()
4441 gtk_entry_prepend_text (GtkEntry *entry,
4446 g_return_if_fail (GTK_IS_ENTRY (entry));
4447 g_return_if_fail (text != NULL);
4450 gtk_editable_insert_text (GTK_EDITABLE (entry), text, -1, &tmp_pos);
4454 * gtk_entry_set_position:
4455 * @entry: a #GtkEntry
4456 * @position: the position of the cursor. The cursor is displayed
4457 * before the character with the given (base 0) index in the widget.
4458 * The value must be less than or equal to the number of characters
4459 * in the widget. A value of -1 indicates that the position should
4460 * be set after the last character in the entry. Note that this
4461 * position is in characters, not in bytes.
4463 * Sets the cursor position in an entry to the given value.
4465 * Deprecated: Use gtk_editable_set_position() instead.
4468 gtk_entry_set_position (GtkEntry *entry,
4471 g_return_if_fail (GTK_IS_ENTRY (entry));
4473 gtk_editable_set_position (GTK_EDITABLE (entry), position);
4477 * gtk_entry_set_visibility:
4478 * @entry: a #GtkEntry
4479 * @visible: %TRUE if the contents of the entry are displayed
4482 * Sets whether the contents of the entry are visible or not.
4483 * When visibility is set to %FALSE, characters are displayed
4484 * as the invisible char, and will also appear that way when
4485 * the text in the entry widget is copied elsewhere.
4487 * The default invisible char is the asterisk '*', but it can
4488 * be changed with gtk_entry_set_invisible_char().
4491 gtk_entry_set_visibility (GtkEntry *entry,
4494 g_return_if_fail (GTK_IS_ENTRY (entry));
4496 visible = visible != FALSE;
4498 if (entry->visible != visible)
4500 if (GTK_WIDGET_HAS_FOCUS (entry) && !visible)
4501 gtk_im_context_focus_out (entry->im_context);
4503 g_object_unref (entry->im_context);
4506 entry->im_context = gtk_im_multicontext_new ();
4508 entry->im_context = gtk_im_context_simple_new ();
4510 g_signal_connect (entry->im_context, "commit",
4511 G_CALLBACK (gtk_entry_commit_cb), entry);
4512 g_signal_connect (entry->im_context, "preedit_changed",
4513 G_CALLBACK (gtk_entry_preedit_changed_cb), entry);
4514 g_signal_connect (entry->im_context, "retrieve_surrounding",
4515 G_CALLBACK (gtk_entry_retrieve_surrounding_cb), entry);
4516 g_signal_connect (entry->im_context, "delete_surrounding",
4517 G_CALLBACK (gtk_entry_delete_surrounding_cb), entry);
4519 if (GTK_WIDGET_HAS_FOCUS (entry) && visible)
4520 gtk_im_context_focus_in (entry->im_context);
4522 entry->visible = visible;
4524 g_object_notify (G_OBJECT (entry), "visibility");
4525 gtk_entry_recompute (entry);
4530 * gtk_entry_get_visibility:
4531 * @entry: a #GtkEntry
4533 * Retrieves whether the text in @entry is visible. See
4534 * gtk_entry_set_visibility().
4536 * Return value: %TRUE if the text is currently visible
4539 gtk_entry_get_visibility (GtkEntry *entry)
4541 g_return_val_if_fail (GTK_IS_ENTRY (entry), FALSE);
4543 return entry->visible;
4547 * gtk_entry_set_invisible_char:
4548 * @entry: a #GtkEntry
4549 * @ch: a Unicode character
4551 * Sets the character to use in place of the actual text when
4552 * gtk_entry_set_visibility() has been called to set text visibility
4553 * to %FALSE. i.e. this is the character used in "password mode" to
4554 * show the user how many characters have been typed. The default
4555 * invisible char is an asterisk ('*'). If you set the invisible char
4556 * to 0, then the user will get no feedback at all; there will be
4557 * no text on the screen as they type.
4560 gtk_entry_set_invisible_char (GtkEntry *entry,
4563 g_return_if_fail (GTK_IS_ENTRY (entry));
4565 if (ch == entry->invisible_char)
4568 entry->invisible_char = ch;
4569 g_object_notify (G_OBJECT (entry), "invisible-char");
4570 gtk_entry_recompute (entry);
4574 * gtk_entry_get_invisible_char:
4575 * @entry: a #GtkEntry
4577 * Retrieves the character displayed in place of the real characters
4578 * for entries with visibility set to false. See gtk_entry_set_invisible_char().
4580 * Return value: the current invisible char, or 0, if the entry does not
4581 * show invisible text at all.
4584 gtk_entry_get_invisible_char (GtkEntry *entry)
4586 g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
4588 return entry->invisible_char;
4592 * gtk_entry_set_editable:
4593 * @entry: a #GtkEntry
4594 * @editable: %TRUE if the user is allowed to edit the text
4597 * Determines if the user can edit the text in the editable
4600 * Deprecated: Use gtk_editable_set_editable() instead.
4603 gtk_entry_set_editable (GtkEntry *entry,
4606 g_return_if_fail (GTK_IS_ENTRY (entry));
4608 gtk_editable_set_editable (GTK_EDITABLE (entry), editable);
4612 * gtk_entry_get_text:
4613 * @entry: a #GtkEntry
4615 * Retrieves the contents of the entry widget.
4616 * See also gtk_editable_get_chars().
4618 * Return value: a pointer to the contents of the widget as a
4619 * string. This string points to internally allocated
4620 * storage in the widget and must not be freed, modified or
4623 G_CONST_RETURN gchar*
4624 gtk_entry_get_text (GtkEntry *entry)
4626 g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
4632 * gtk_entry_select_region:
4633 * @entry: a #GtkEntry
4634 * @start: the starting position
4635 * @end: the end position
4637 * Selects a region of text. The characters that are selected are
4638 * those characters at positions from @start_pos up to, but not
4639 * including @end_pos. If @end_pos is negative, then the the characters
4640 * selected will be those characters from @start_pos to the end of
4643 * Deprecated: Use gtk_editable_select_region() instead.
4646 gtk_entry_select_region (GtkEntry *entry,
4650 gtk_editable_select_region (GTK_EDITABLE (entry), start, end);
4654 * gtk_entry_set_max_length:
4655 * @entry: a #GtkEntry
4656 * @max: the maximum length of the entry, or 0 for no maximum.
4657 * (other than the maximum length of entries.) The value passed in will
4658 * be clamped to the range 0-65536.
4660 * Sets the maximum allowed length of the contents of the widget. If
4661 * the current contents are longer than the given length, then they
4662 * will be truncated to fit.
4665 gtk_entry_set_max_length (GtkEntry *entry,
4668 g_return_if_fail (GTK_IS_ENTRY (entry));
4670 max = CLAMP (max, 0, MAX_SIZE);
4672 if (max > 0 && entry->text_length > max)
4673 gtk_editable_delete_text (GTK_EDITABLE (entry), max, -1);
4675 entry->text_max_length = max;
4676 g_object_notify (G_OBJECT (entry), "max-length");
4680 * gtk_entry_get_max_length:
4681 * @entry: a #GtkEntry
4683 * Retrieves the maximum allowed length of the text in
4684 * @entry. See gtk_entry_set_max_length().
4686 * Return value: the maximum allowed number of characters
4687 * in #GtkEntry, or 0 if there is no maximum.
4690 gtk_entry_get_max_length (GtkEntry *entry)
4692 g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
4694 return entry->text_max_length;
4698 * gtk_entry_set_activates_default:
4699 * @entry: a #GtkEntry
4700 * @setting: %TRUE to activate window's default widget on Enter keypress
4702 * If @setting is %TRUE, pressing Enter in the @entry will activate the default
4703 * widget for the window containing the entry. This usually means that
4704 * the dialog box containing the entry will be closed, since the default
4705 * widget is usually one of the dialog buttons.
4707 * (For experts: if @setting is %TRUE, the entry calls
4708 * gtk_window_activate_default() on the window containing the entry, in
4709 * the default handler for the #GtkWidget::activate signal.)
4712 gtk_entry_set_activates_default (GtkEntry *entry,
4715 g_return_if_fail (GTK_IS_ENTRY (entry));
4716 setting = setting != FALSE;
4718 if (setting != entry->activates_default)
4720 entry->activates_default = setting;
4721 g_object_notify (G_OBJECT (entry), "activates-default");
4726 * gtk_entry_get_activates_default:
4727 * @entry: a #GtkEntry
4729 * Retrieves the value set by gtk_entry_set_activates_default().
4731 * Return value: %TRUE if the entry will activate the default widget
4734 gtk_entry_get_activates_default (GtkEntry *entry)
4736 g_return_val_if_fail (GTK_IS_ENTRY (entry), FALSE);
4738 return entry->activates_default;
4742 * gtk_entry_set_width_chars:
4743 * @entry: a #GtkEntry
4744 * @n_chars: width in chars
4746 * Changes the size request of the entry to be about the right size
4747 * for @n_chars characters. Note that it changes the size
4748 * <emphasis>request</emphasis>, the size can still be affected by
4749 * how you pack the widget into containers. If @n_chars is -1, the
4750 * size reverts to the default entry size.
4753 gtk_entry_set_width_chars (GtkEntry *entry,
4756 g_return_if_fail (GTK_IS_ENTRY (entry));
4758 if (entry->width_chars != n_chars)
4760 entry->width_chars = n_chars;
4761 g_object_notify (G_OBJECT (entry), "width-chars");
4762 gtk_widget_queue_resize (GTK_WIDGET (entry));
4767 * gtk_entry_get_width_chars:
4768 * @entry: a #GtkEntry
4770 * Gets the value set by gtk_entry_set_width_chars().
4772 * Return value: number of chars to request space for, or negative if unset
4775 gtk_entry_get_width_chars (GtkEntry *entry)
4777 g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
4779 return entry->width_chars;
4783 * gtk_entry_set_has_frame:
4784 * @entry: a #GtkEntry
4785 * @setting: new value
4787 * Sets whether the entry has a beveled frame around it.
4790 gtk_entry_set_has_frame (GtkEntry *entry,
4793 g_return_if_fail (GTK_IS_ENTRY (entry));
4795 setting = (setting != FALSE);
4797 if (entry->has_frame == setting)
4800 gtk_widget_queue_resize (GTK_WIDGET (entry));
4801 entry->has_frame = setting;
4802 g_object_notify (G_OBJECT (entry), "has-frame");
4806 * gtk_entry_get_has_frame:
4807 * @entry: a #GtkEntry
4809 * Gets the value set by gtk_entry_set_has_frame().
4811 * Return value: whether the entry has a beveled frame
4814 gtk_entry_get_has_frame (GtkEntry *entry)
4816 g_return_val_if_fail (GTK_IS_ENTRY (entry), FALSE);
4818 return entry->has_frame;
4822 * gtk_entry_set_inner_border:
4823 * @entry: a #GtkEntry
4824 * @border: a #GtkBorder, or %NULL
4826 * Sets %entry's inner-border property to %border, or clears it if %NULL
4827 * is passed. The inner-border is the area around the entry's text, but
4830 * If set, this property overrides the inner-border style property.
4831 * Overriding the style-provided border is useful when you want to do
4832 * in-place editing of some text in a canvas or list widget, where
4833 * pixel-exact positioning of the entry is important.
4838 gtk_entry_set_inner_border (GtkEntry *entry,
4839 const GtkBorder *border)
4841 g_return_if_fail (GTK_IS_ENTRY (entry));
4843 gtk_widget_queue_resize (GTK_WIDGET (entry));
4846 g_object_set_qdata_full (G_OBJECT (entry), quark_inner_border,
4847 gtk_border_copy (border),
4848 (GDestroyNotify) gtk_border_free);
4850 g_object_set_qdata (G_OBJECT (entry), quark_inner_border, NULL);
4852 g_object_notify (G_OBJECT (entry), "inner-border");
4856 * gtk_entry_get_inner_border:
4857 * @entry: a #GtkEntry
4859 * This function returns the entry's #GtkEntry:inner-border property. See
4860 * gtk_entry_set_inner_border() for more information.
4862 * Return value: the entry's #GtkBorder, or %NULL if none was set.
4866 G_CONST_RETURN GtkBorder *
4867 gtk_entry_get_inner_border (GtkEntry *entry)
4869 g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
4871 return g_object_get_qdata (G_OBJECT (entry), quark_inner_border);
4875 * gtk_entry_get_layout:
4876 * @entry: a #GtkEntry
4878 * Gets the #PangoLayout used to display the entry.
4879 * The layout is useful to e.g. convert text positions to
4880 * pixel positions, in combination with gtk_entry_get_layout_offsets().
4881 * The returned layout is owned by the entry and must not be
4882 * modified or freed by the caller.
4884 * Keep in mind that the layout text may contain a preedit string, so
4885 * gtk_entry_layout_index_to_text_index() and
4886 * gtk_entry_text_index_to_layout_index() are needed to convert byte
4887 * indices in the layout to byte indices in the entry contents.
4889 * Return value: the #PangoLayout for this entry
4892 gtk_entry_get_layout (GtkEntry *entry)
4894 PangoLayout *layout;
4896 g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
4898 layout = gtk_entry_ensure_layout (entry, TRUE);
4905 * gtk_entry_layout_index_to_text_index:
4906 * @entry: a #GtkEntry
4907 * @layout_index: byte index into the entry layout text
4909 * Converts from a position in the entry contents (returned
4910 * by gtk_entry_get_text()) to a position in the
4911 * entry's #PangoLayout (returned by gtk_entry_get_layout(),
4912 * with text retrieved via pango_layout_get_text()).
4914 * Return value: byte index into the entry contents
4917 gtk_entry_layout_index_to_text_index (GtkEntry *entry,
4920 PangoLayout *layout;
4924 g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
4926 layout = gtk_entry_ensure_layout (entry, TRUE);
4927 text = pango_layout_get_text (layout);
4928 cursor_index = g_utf8_offset_to_pointer (text, entry->current_pos) - text;
4930 if (layout_index >= cursor_index && entry->preedit_length)
4932 if (layout_index >= cursor_index + entry->preedit_length)
4933 layout_index -= entry->preedit_length;
4935 layout_index = cursor_index;
4938 return layout_index;
4942 * gtk_entry_text_index_to_layout_index:
4943 * @entry: a #GtkEntry
4944 * @text_index: byte index into the entry contents
4946 * Converts from a position in the entry's #PangoLayout (returned by
4947 * gtk_entry_get_layout()) to a position in the entry contents
4948 * (returned by gtk_entry_get_text()).
4950 * Return value: byte index into the entry layout text
4953 gtk_entry_text_index_to_layout_index (GtkEntry *entry,
4956 PangoLayout *layout;
4959 g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
4961 layout = gtk_entry_ensure_layout (entry, TRUE);
4962 text = pango_layout_get_text (layout);
4963 cursor_index = g_utf8_offset_to_pointer (text, entry->current_pos) - text;
4965 if (text_index > cursor_index)
4966 text_index += entry->preedit_length;
4972 * gtk_entry_get_layout_offsets:
4973 * @entry: a #GtkEntry
4974 * @x: location to store X offset of layout, or %NULL
4975 * @y: location to store Y offset of layout, or %NULL
4978 * Obtains the position of the #PangoLayout used to render text
4979 * in the entry, in widget coordinates. Useful if you want to line
4980 * up the text in an entry with some other text, e.g. when using the
4981 * entry to implement editable cells in a sheet widget.
4983 * Also useful to convert mouse events into coordinates inside the
4984 * #PangoLayout, e.g. to take some action if some part of the entry text
4987 * Note that as the user scrolls around in the entry the offsets will
4988 * change; you'll need to connect to the "notify::scroll-offset"
4989 * signal to track this. Remember when using the #PangoLayout
4990 * functions you need to convert to and from pixels using
4991 * PANGO_PIXELS() or #PANGO_SCALE.
4993 * Keep in mind that the layout text may contain a preedit string, so
4994 * gtk_entry_layout_index_to_text_index() and
4995 * gtk_entry_text_index_to_layout_index() are needed to convert byte
4996 * indices in the layout to byte indices in the entry contents.
4999 gtk_entry_get_layout_offsets (GtkEntry *entry,
5003 gint text_area_x, text_area_y;
5005 g_return_if_fail (GTK_IS_ENTRY (entry));
5007 /* this gets coords relative to text area */
5008 get_layout_position (entry, x, y);
5010 /* convert to widget coords */
5011 get_text_area_size (entry, &text_area_x, &text_area_y, NULL, NULL);
5022 * gtk_entry_set_alignment:
5023 * @entry: a #GtkEntry
5024 * @xalign: The horizontal alignment, from 0 (left) to 1 (right).
5025 * Reversed for RTL layouts
5027 * Sets the alignment for the contents of the entry. This controls
5028 * the horizontal positioning of the contents when the displayed
5029 * text is shorter than the width of the entry.
5034 gtk_entry_set_alignment (GtkEntry *entry, gfloat xalign)
5036 GtkEntryPrivate *priv;
5038 g_return_if_fail (GTK_IS_ENTRY (entry));
5040 priv = GTK_ENTRY_GET_PRIVATE (entry);
5044 else if (xalign > 1.0)
5047 if (xalign != priv->xalign)
5049 priv->xalign = xalign;
5051 gtk_entry_recompute (entry);
5053 g_object_notify (G_OBJECT (entry), "xalign");
5058 * gtk_entry_get_alignment:
5059 * @entry: a #GtkEntry
5061 * Gets the value set by gtk_entry_set_alignment().
5063 * Return value: the alignment
5068 gtk_entry_get_alignment (GtkEntry *entry)
5070 GtkEntryPrivate *priv;
5072 g_return_val_if_fail (GTK_IS_ENTRY (entry), 0.0);
5074 priv = GTK_ENTRY_GET_PRIVATE (entry);
5076 return priv->xalign;
5079 /* Quick hack of a popup menu
5082 activate_cb (GtkWidget *menuitem,
5085 const gchar *signal = g_object_get_data (G_OBJECT (menuitem), "gtk-signal");
5086 g_signal_emit_by_name (entry, signal);
5091 gtk_entry_mnemonic_activate (GtkWidget *widget,
5092 gboolean group_cycling)
5094 gtk_widget_grab_focus (widget);
5099 append_action_signal (GtkEntry *entry,
5101 const gchar *stock_id,
5102 const gchar *signal,
5105 GtkWidget *menuitem = gtk_image_menu_item_new_from_stock (stock_id, NULL);
5107 g_object_set_data (G_OBJECT (menuitem), I_("gtk-signal"), (char *)signal);
5108 g_signal_connect (menuitem, "activate",
5109 G_CALLBACK (activate_cb), entry);
5111 gtk_widget_set_sensitive (menuitem, sensitive);
5113 gtk_widget_show (menuitem);
5114 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
5118 popup_menu_detach (GtkWidget *attach_widget,
5121 GTK_ENTRY (attach_widget)->popup_menu = NULL;
5125 popup_position_func (GtkMenu *menu,
5131 GtkEntry *entry = GTK_ENTRY (user_data);
5132 GtkWidget *widget = GTK_WIDGET (entry);
5134 GtkRequisition menu_req;
5135 GdkRectangle monitor;
5136 GtkBorder inner_border;
5137 gint monitor_num, strong_x, height;
5139 g_return_if_fail (GTK_WIDGET_REALIZED (entry));
5141 gdk_window_get_origin (entry->text_area, x, y);
5143 screen = gtk_widget_get_screen (widget);
5144 monitor_num = gdk_screen_get_monitor_at_window (screen, entry->text_area);
5145 if (monitor_num < 0)
5147 gtk_menu_set_monitor (menu, monitor_num);
5149 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
5150 gtk_widget_size_request (entry->popup_menu, &menu_req);
5151 gdk_drawable_get_size (entry->text_area, NULL, &height);
5152 gtk_entry_get_cursor_locations (entry, CURSOR_STANDARD, &strong_x, NULL);
5153 _gtk_entry_effective_inner_border (entry, &inner_border);
5155 *x += inner_border.left + strong_x - entry->scroll_offset;
5156 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
5157 *x -= menu_req.width;
5159 if ((*y + height + menu_req.height) <= monitor.y + monitor.height)
5161 else if ((*y - menu_req.height) >= monitor.y)
5162 *y -= menu_req.height;
5163 else if (monitor.y + monitor.height - (*y + height) > *y)
5166 *y -= menu_req.height;
5172 unichar_chosen_func (const char *text,
5175 GtkEntry *entry = GTK_ENTRY (data);
5177 if (entry->editable)
5178 gtk_entry_enter_text (entry, text);
5189 popup_targets_received (GtkClipboard *clipboard,
5190 GtkSelectionData *data,
5193 PopupInfo *info = user_data;
5194 GtkEntry *entry = info->entry;
5196 if (GTK_WIDGET_REALIZED (entry))
5198 gboolean clipboard_contains_text;
5199 GtkWidget *menuitem;
5201 gboolean show_input_method_menu;
5202 gboolean show_unicode_menu;
5204 clipboard_contains_text = gtk_selection_data_targets_include_text (data);
5205 if (entry->popup_menu)
5206 gtk_widget_destroy (entry->popup_menu);
5208 entry->popup_menu = gtk_menu_new ();
5210 gtk_menu_attach_to_widget (GTK_MENU (entry->popup_menu),
5214 append_action_signal (entry, entry->popup_menu, GTK_STOCK_CUT, "cut_clipboard",
5215 entry->editable && entry->current_pos != entry->selection_bound);
5216 append_action_signal (entry, entry->popup_menu, GTK_STOCK_COPY, "copy_clipboard",
5217 entry->current_pos != entry->selection_bound);
5218 append_action_signal (entry, entry->popup_menu, GTK_STOCK_PASTE, "paste_clipboard",
5219 entry->editable && clipboard_contains_text);
5221 menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_DELETE, NULL);
5222 gtk_widget_set_sensitive (menuitem, entry->editable && entry->current_pos != entry->selection_bound);
5223 g_signal_connect_swapped (menuitem, "activate",
5224 G_CALLBACK (gtk_entry_delete_cb), entry);
5225 gtk_widget_show (menuitem);
5226 gtk_menu_shell_append (GTK_MENU_SHELL (entry->popup_menu), menuitem);
5228 menuitem = gtk_separator_menu_item_new ();
5229 gtk_widget_show (menuitem);
5230 gtk_menu_shell_append (GTK_MENU_SHELL (entry->popup_menu), menuitem);
5232 menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_SELECT_ALL, NULL);
5233 g_signal_connect_swapped (menuitem, "activate",
5234 G_CALLBACK (gtk_entry_select_all), entry);
5235 gtk_widget_show (menuitem);
5236 gtk_menu_shell_append (GTK_MENU_SHELL (entry->popup_menu), menuitem);
5238 g_object_get (gtk_widget_get_settings (GTK_WIDGET (entry)),
5239 "gtk-show-input-method-menu", &show_input_method_menu,
5240 "gtk-show-unicode-menu", &show_unicode_menu,
5243 if (!entry->visible)
5244 show_input_method_menu = FALSE;
5246 if (show_input_method_menu || show_unicode_menu)
5248 menuitem = gtk_separator_menu_item_new ();
5249 gtk_widget_show (menuitem);
5250 gtk_menu_shell_append (GTK_MENU_SHELL (entry->popup_menu), menuitem);
5253 if (show_input_method_menu)
5255 menuitem = gtk_menu_item_new_with_mnemonic (_("Input _Methods"));
5256 gtk_widget_set_sensitive (menuitem, entry->editable);
5257 gtk_widget_show (menuitem);
5258 submenu = gtk_menu_new ();
5259 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
5261 gtk_menu_shell_append (GTK_MENU_SHELL (entry->popup_menu), menuitem);
5263 gtk_im_multicontext_append_menuitems (GTK_IM_MULTICONTEXT (entry->im_context),
5264 GTK_MENU_SHELL (submenu));
5267 if (show_unicode_menu)
5269 menuitem = gtk_menu_item_new_with_mnemonic (_("_Insert Unicode Control Character"));
5270 gtk_widget_set_sensitive (menuitem, entry->editable);
5271 gtk_widget_show (menuitem);
5273 submenu = gtk_menu_new ();
5274 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
5275 gtk_menu_shell_append (GTK_MENU_SHELL (entry->popup_menu), menuitem);
5277 _gtk_text_util_append_special_char_menuitems (GTK_MENU_SHELL (submenu),
5278 unichar_chosen_func,
5282 g_signal_emit (entry,
5283 signals[POPULATE_POPUP],
5289 gtk_menu_popup (GTK_MENU (entry->popup_menu), NULL, NULL,
5291 info->button, info->time);
5294 gtk_menu_popup (GTK_MENU (entry->popup_menu), NULL, NULL,
5295 popup_position_func, entry,
5296 info->button, info->time);
5297 gtk_menu_shell_select_first (GTK_MENU_SHELL (entry->popup_menu), FALSE);
5301 g_object_unref (entry);
5306 gtk_entry_do_popup (GtkEntry *entry,
5307 GdkEventButton *event)
5309 PopupInfo *info = g_new (PopupInfo, 1);
5311 /* In order to know what entries we should make sensitive, we
5312 * ask for the current targets of the clipboard, and when
5313 * we get them, then we actually pop up the menu.
5315 info->entry = g_object_ref (entry);
5319 info->button = event->button;
5320 info->time = event->time;
5325 info->time = gtk_get_current_event_time ();
5328 gtk_clipboard_request_contents (gtk_widget_get_clipboard (GTK_WIDGET (entry), GDK_SELECTION_CLIPBOARD),
5329 gdk_atom_intern_static_string ("TARGETS"),
5330 popup_targets_received,
5335 gtk_entry_popup_menu (GtkWidget *widget)
5337 gtk_entry_do_popup (GTK_ENTRY (widget), NULL);
5342 gtk_entry_drag_leave (GtkWidget *widget,
5343 GdkDragContext *context,
5346 GtkEntry *entry = GTK_ENTRY (widget);
5348 entry->dnd_position = -1;
5349 gtk_widget_queue_draw (widget);
5353 gtk_entry_drag_drop (GtkWidget *widget,
5354 GdkDragContext *context,
5359 GtkEntry *entry = GTK_ENTRY (widget);
5360 GdkAtom target = GDK_NONE;
5362 if (entry->editable)
5363 target = gtk_drag_dest_find_target (widget, context, NULL);
5365 if (target != GDK_NONE)
5366 gtk_drag_get_data (widget, context, target, time);
5368 gtk_drag_finish (context, FALSE, FALSE, time);
5374 gtk_entry_drag_motion (GtkWidget *widget,
5375 GdkDragContext *context,
5380 GtkEntry *entry = GTK_ENTRY (widget);
5381 GtkWidget *source_widget;
5382 GdkDragAction suggested_action;
5383 gint new_position, old_position;
5386 x -= widget->style->xthickness;
5387 y -= widget->style->ythickness;
5389 old_position = entry->dnd_position;
5390 new_position = gtk_entry_find_position (entry, x + entry->scroll_offset);
5392 if (entry->editable &&
5393 gtk_drag_dest_find_target (widget, context, NULL) != GDK_NONE)
5395 source_widget = gtk_drag_get_source_widget (context);
5396 suggested_action = context->suggested_action;
5398 if (!gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &sel1, &sel2) ||
5399 new_position < sel1 || new_position > sel2)
5401 if (source_widget == widget)
5403 /* Default to MOVE, unless the user has
5404 * pressed ctrl or alt to affect available actions
5406 if ((context->actions & GDK_ACTION_MOVE) != 0)
5407 suggested_action = GDK_ACTION_MOVE;
5410 entry->dnd_position = new_position;
5414 if (source_widget == widget)
5415 suggested_action = 0; /* Can't drop in selection where drag started */
5417 entry->dnd_position = -1;
5422 /* Entry not editable, or no text */
5423 suggested_action = 0;
5424 entry->dnd_position = -1;
5427 gdk_drag_status (context, suggested_action, time);
5429 if (entry->dnd_position != old_position)
5430 gtk_widget_queue_draw (widget);
5436 gtk_entry_drag_data_received (GtkWidget *widget,
5437 GdkDragContext *context,
5440 GtkSelectionData *selection_data,
5444 GtkEntry *entry = GTK_ENTRY (widget);
5445 GtkEditable *editable = GTK_EDITABLE (widget);
5448 str = (gchar *) gtk_selection_data_get_text (selection_data);
5450 x -= widget->style->xthickness;
5451 y -= widget->style->ythickness;
5453 if (str && entry->editable)
5459 if (entry->truncate_multiline)
5460 length = truncate_multiline (str);
5462 new_position = gtk_entry_find_position (entry, x + entry->scroll_offset);
5464 if (!gtk_editable_get_selection_bounds (editable, &sel1, &sel2) ||
5465 new_position < sel1 || new_position > sel2)
5467 gtk_editable_insert_text (editable, str, length, &new_position);
5471 /* Replacing selection */
5472 begin_change (entry);
5473 g_object_freeze_notify (G_OBJECT (entry));
5474 gtk_editable_delete_text (editable, sel1, sel2);
5475 gtk_editable_insert_text (editable, str, length, &sel1);
5476 g_object_thaw_notify (G_OBJECT (entry));
5480 gtk_drag_finish (context, TRUE, context->action == GDK_ACTION_MOVE, time);
5484 /* Drag and drop didn't happen! */
5485 gtk_drag_finish (context, FALSE, FALSE, time);
5492 gtk_entry_drag_data_get (GtkWidget *widget,
5493 GdkDragContext *context,
5494 GtkSelectionData *selection_data,
5498 gint sel_start, sel_end;
5500 GtkEditable *editable = GTK_EDITABLE (widget);
5502 if (gtk_editable_get_selection_bounds (editable, &sel_start, &sel_end))
5504 gchar *str = gtk_entry_get_public_chars (GTK_ENTRY (widget), sel_start, sel_end);
5506 gtk_selection_data_set_text (selection_data, str, -1);
5514 gtk_entry_drag_data_delete (GtkWidget *widget,
5515 GdkDragContext *context)
5517 gint sel_start, sel_end;
5519 GtkEditable *editable = GTK_EDITABLE (widget);
5521 if (GTK_ENTRY (widget)->editable &&
5522 gtk_editable_get_selection_bounds (editable, &sel_start, &sel_end))
5523 gtk_editable_delete_text (editable, sel_start, sel_end);
5526 /* We display the cursor when
5528 * - the selection is empty, AND
5529 * - the widget has focus
5532 #define CURSOR_ON_MULTIPLIER 2
5533 #define CURSOR_OFF_MULTIPLIER 1
5534 #define CURSOR_PEND_MULTIPLIER 3
5535 #define CURSOR_DIVIDER 3
5538 cursor_blinks (GtkEntry *entry)
5540 if (GTK_WIDGET_HAS_FOCUS (entry) &&
5542 entry->selection_bound == entry->current_pos)
5544 GtkSettings *settings;
5547 settings = gtk_widget_get_settings (GTK_WIDGET (entry));
5548 g_object_get (settings, "gtk-cursor-blink", &blink, NULL);
5557 get_cursor_time (GtkEntry *entry)
5559 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (entry));
5562 g_object_get (settings, "gtk-cursor-blink-time", &time, NULL);
5568 get_cursor_blink_timeout (GtkEntry *entry)
5570 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (entry));
5573 g_object_get (settings, "gtk-cursor-blink-timeout", &timeout, NULL);
5579 show_cursor (GtkEntry *entry)
5581 if (!entry->cursor_visible)
5583 entry->cursor_visible = TRUE;
5585 if (GTK_WIDGET_HAS_FOCUS (entry) && entry->selection_bound == entry->current_pos)
5586 gtk_widget_queue_draw (GTK_WIDGET (entry));
5591 hide_cursor (GtkEntry *entry)
5593 if (entry->cursor_visible)
5595 entry->cursor_visible = FALSE;
5597 if (GTK_WIDGET_HAS_FOCUS (entry) && entry->selection_bound == entry->current_pos)
5598 gtk_widget_queue_draw (GTK_WIDGET (entry));
5606 blink_cb (gpointer data)
5609 GtkEntryPrivate *priv;
5612 entry = GTK_ENTRY (data);
5613 priv = GTK_ENTRY_GET_PRIVATE (entry);
5615 if (!GTK_WIDGET_HAS_FOCUS (entry))
5617 g_warning ("GtkEntry - did not receive focus-out-event. If you\n"
5618 "connect a handler to this signal, it must return\n"
5619 "FALSE so the entry gets the event as well");
5621 gtk_entry_check_cursor_blink (entry);
5626 g_assert (entry->selection_bound == entry->current_pos);
5628 blink_timeout = get_cursor_blink_timeout (entry);
5629 if (priv->blink_time > 1000 * blink_timeout &&
5630 blink_timeout < G_MAXINT/1000)
5632 /* we've blinked enough without the user doing anything, stop blinking */
5633 show_cursor (entry);
5634 entry->blink_timeout = 0;
5636 else if (entry->cursor_visible)
5638 hide_cursor (entry);
5639 entry->blink_timeout = gdk_threads_add_timeout (get_cursor_time (entry) * CURSOR_OFF_MULTIPLIER / CURSOR_DIVIDER,
5645 show_cursor (entry);
5646 priv->blink_time += get_cursor_time (entry);
5647 entry->blink_timeout = gdk_threads_add_timeout (get_cursor_time (entry) * CURSOR_ON_MULTIPLIER / CURSOR_DIVIDER,
5652 /* Remove ourselves */
5657 gtk_entry_check_cursor_blink (GtkEntry *entry)
5659 GtkEntryPrivate *priv;
5661 priv = GTK_ENTRY_GET_PRIVATE (entry);
5663 if (cursor_blinks (entry))
5665 if (!entry->blink_timeout)
5667 show_cursor (entry);
5668 entry->blink_timeout = gdk_threads_add_timeout (get_cursor_time (entry) * CURSOR_ON_MULTIPLIER / CURSOR_DIVIDER,
5675 if (entry->blink_timeout)
5677 g_source_remove (entry->blink_timeout);
5678 entry->blink_timeout = 0;
5681 entry->cursor_visible = TRUE;
5687 gtk_entry_pend_cursor_blink (GtkEntry *entry)
5689 if (cursor_blinks (entry))
5691 if (entry->blink_timeout != 0)
5692 g_source_remove (entry->blink_timeout);
5694 entry->blink_timeout = gdk_threads_add_timeout (get_cursor_time (entry) * CURSOR_PEND_MULTIPLIER / CURSOR_DIVIDER,
5697 show_cursor (entry);
5702 gtk_entry_reset_blink_time (GtkEntry *entry)
5704 GtkEntryPrivate *priv;
5706 priv = GTK_ENTRY_GET_PRIVATE (entry);
5708 priv->blink_time = 0;
5714 gtk_entry_completion_timeout (gpointer data)
5716 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (data);
5718 completion->priv->completion_timeout = 0;
5720 if (completion->priv->filter_model &&
5721 g_utf8_strlen (gtk_entry_get_text (GTK_ENTRY (completion->priv->entry)), -1)
5722 >= completion->priv->minimum_key_length)
5726 GtkTreeSelection *s;
5727 gboolean popup_single;
5729 gtk_entry_completion_complete (completion);
5730 matches = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->filter_model), NULL);
5732 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view)));
5734 s = gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->action_view));
5736 gtk_tree_selection_unselect_all (s);
5738 actions = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->actions), NULL);
5740 g_object_get (completion, "popup-single-match", &popup_single, NULL);
5741 if ((matches > (popup_single ? 0: 1)) || actions > 0)
5743 if (GTK_WIDGET_VISIBLE (completion->priv->popup_window))
5744 _gtk_entry_completion_resize_popup (completion);
5746 _gtk_entry_completion_popup (completion);
5749 _gtk_entry_completion_popdown (completion);
5751 else if (GTK_WIDGET_VISIBLE (completion->priv->popup_window))
5752 _gtk_entry_completion_popdown (completion);
5757 static inline gboolean
5758 keyval_is_cursor_move (guint keyval)
5760 if (keyval == GDK_Up || keyval == GDK_KP_Up)
5763 if (keyval == GDK_Down || keyval == GDK_KP_Down)
5766 if (keyval == GDK_Page_Up)
5769 if (keyval == GDK_Page_Down)
5776 gtk_entry_completion_key_press (GtkWidget *widget,
5780 gint matches, actions = 0;
5781 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
5783 if (!GTK_WIDGET_MAPPED (completion->priv->popup_window))
5786 matches = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->filter_model), NULL);
5788 if (completion->priv->actions)
5789 actions = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->actions), NULL);
5791 if (keyval_is_cursor_move (event->keyval))
5793 GtkTreePath *path = NULL;
5795 if (event->keyval == GDK_Up || event->keyval == GDK_KP_Up)
5797 if (completion->priv->current_selected < 0)
5798 completion->priv->current_selected = matches + actions - 1;
5800 completion->priv->current_selected--;
5802 else if (event->keyval == GDK_Down || event->keyval == GDK_KP_Down)
5804 if (completion->priv->current_selected < matches + actions - 1)
5805 completion->priv->current_selected++;
5807 completion->priv->current_selected = -1;
5809 else if (event->keyval == GDK_Page_Up)
5811 if (completion->priv->current_selected < 0)
5812 completion->priv->current_selected = matches + actions - 1;
5813 else if (completion->priv->current_selected == 0)
5814 completion->priv->current_selected = -1;
5815 else if (completion->priv->current_selected < matches)
5817 completion->priv->current_selected -= 14;
5818 if (completion->priv->current_selected < 0)
5819 completion->priv->current_selected = 0;
5823 completion->priv->current_selected -= 14;
5824 if (completion->priv->current_selected < matches - 1)
5825 completion->priv->current_selected = matches - 1;
5828 else if (event->keyval == GDK_Page_Down)
5830 if (completion->priv->current_selected < 0)
5831 completion->priv->current_selected = 0;
5832 else if (completion->priv->current_selected < matches - 1)
5834 completion->priv->current_selected += 14;
5835 if (completion->priv->current_selected > matches - 1)
5836 completion->priv->current_selected = matches - 1;
5838 else if (completion->priv->current_selected == matches + actions - 1)
5840 completion->priv->current_selected = -1;
5844 completion->priv->current_selected += 14;
5845 if (completion->priv->current_selected > matches + actions - 1)
5846 completion->priv->current_selected = matches + actions - 1;
5850 if (completion->priv->current_selected < 0)
5852 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view)));
5853 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->action_view)));
5855 if (completion->priv->inline_selection &&
5856 completion->priv->completion_prefix)
5858 gtk_entry_set_text (GTK_ENTRY (completion->priv->entry),
5859 completion->priv->completion_prefix);
5860 gtk_editable_set_position (GTK_EDITABLE (widget), -1);
5863 else if (completion->priv->current_selected < matches)
5865 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->action_view)));
5867 path = gtk_tree_path_new_from_indices (completion->priv->current_selected, -1);
5868 gtk_tree_view_set_cursor (GTK_TREE_VIEW (completion->priv->tree_view),
5871 if (completion->priv->inline_selection)
5875 GtkTreeModel *model = NULL;
5876 GtkTreeSelection *sel;
5879 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view));
5880 if (!gtk_tree_selection_get_selected (sel, &model, &iter))
5883 if (completion->priv->completion_prefix == NULL)
5884 completion->priv->completion_prefix = g_strdup (gtk_entry_get_text (GTK_ENTRY (completion->priv->entry)));
5886 g_signal_emit_by_name (completion, "cursor_on_match", model,
5890 else if (completion->priv->current_selected - matches >= 0)
5892 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view)));
5894 path = gtk_tree_path_new_from_indices (completion->priv->current_selected - matches, -1);
5895 gtk_tree_view_set_cursor (GTK_TREE_VIEW (completion->priv->action_view),
5898 if (completion->priv->inline_selection &&
5899 completion->priv->completion_prefix)
5901 gtk_entry_set_text (GTK_ENTRY (completion->priv->entry),
5902 completion->priv->completion_prefix);
5903 gtk_editable_set_position (GTK_EDITABLE (widget), -1);
5907 gtk_tree_path_free (path);
5911 else if (event->keyval == GDK_Escape ||
5912 event->keyval == GDK_Left ||
5913 event->keyval == GDK_KP_Left ||
5914 event->keyval == GDK_Right ||
5915 event->keyval == GDK_KP_Right)
5917 gboolean retval = TRUE;
5919 _gtk_entry_reset_im_context (GTK_ENTRY (widget));
5920 _gtk_entry_completion_popdown (completion);
5922 if (completion->priv->current_selected < 0)
5925 goto keypress_completion_out;
5927 else if (completion->priv->inline_selection)
5929 /* Escape rejects the tentative completion */
5930 if (event->keyval == GDK_Escape)
5932 if (completion->priv->completion_prefix)
5933 gtk_entry_set_text (GTK_ENTRY (completion->priv->entry),
5934 completion->priv->completion_prefix);
5936 gtk_entry_set_text (GTK_ENTRY (completion->priv->entry), "");
5939 /* Move the cursor to the end for Right/Esc, to the
5940 beginning for Left */
5941 if (event->keyval == GDK_Right ||
5942 event->keyval == GDK_KP_Right ||
5943 event->keyval == GDK_Escape)
5944 gtk_editable_set_position (GTK_EDITABLE (widget), -1);
5946 gtk_editable_set_position (GTK_EDITABLE (widget), 0);
5949 keypress_completion_out:
5950 if (completion->priv->inline_selection)
5952 g_free (completion->priv->completion_prefix);
5953 completion->priv->completion_prefix = NULL;
5958 else if (event->keyval == GDK_Tab ||
5959 event->keyval == GDK_KP_Tab ||
5960 event->keyval == GDK_ISO_Left_Tab)
5962 GtkDirectionType dir = event->keyval == GDK_ISO_Left_Tab ?
5963 GTK_DIR_TAB_BACKWARD : GTK_DIR_TAB_FORWARD;
5965 _gtk_entry_reset_im_context (GTK_ENTRY (widget));
5966 _gtk_entry_completion_popdown (completion);
5968 g_free (completion->priv->completion_prefix);
5969 completion->priv->completion_prefix = NULL;
5971 gtk_widget_child_focus (gtk_widget_get_toplevel (widget), dir);
5975 else if (event->keyval == GDK_ISO_Enter ||
5976 event->keyval == GDK_KP_Enter ||
5977 event->keyval == GDK_Return)
5979 gboolean retval = TRUE;
5981 _gtk_entry_reset_im_context (GTK_ENTRY (widget));
5982 _gtk_entry_completion_popdown (completion);
5984 if (completion->priv->current_selected < matches)
5987 GtkTreeModel *model = NULL;
5988 GtkTreeSelection *sel;
5991 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view));
5992 if (gtk_tree_selection_get_selected (sel, &model, &iter))
5994 g_signal_handler_block (widget, completion->priv->changed_id);
5995 g_signal_emit_by_name (completion, "match_selected",
5996 model, &iter, &entry_set);
5997 g_signal_handler_unblock (widget, completion->priv->changed_id);
6003 gtk_tree_model_get (model, &iter,
6004 completion->priv->text_column, &str,
6007 gtk_entry_set_text (GTK_ENTRY (widget), str);
6009 /* move the cursor to the end */
6010 gtk_editable_set_position (GTK_EDITABLE (widget), -1);
6018 else if (completion->priv->current_selected - matches >= 0)
6022 _gtk_entry_reset_im_context (GTK_ENTRY (widget));
6024 path = gtk_tree_path_new_from_indices (completion->priv->current_selected - matches, -1);
6026 g_signal_emit_by_name (completion, "action_activated",
6027 gtk_tree_path_get_indices (path)[0]);
6028 gtk_tree_path_free (path);
6031 g_free (completion->priv->completion_prefix);
6032 completion->priv->completion_prefix = NULL;
6041 gtk_entry_completion_changed (GtkWidget *entry,
6044 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
6046 /* (re)install completion timeout */
6047 if (completion->priv->completion_timeout)
6048 g_source_remove (completion->priv->completion_timeout);
6050 if (!gtk_entry_get_text (GTK_ENTRY (entry)))
6053 /* no need to normalize for this test */
6054 if (completion->priv->minimum_key_length > 0 &&
6055 strcmp ("", gtk_entry_get_text (GTK_ENTRY (entry))) == 0)
6057 if (GTK_WIDGET_VISIBLE (completion->priv->popup_window))
6058 _gtk_entry_completion_popdown (completion);
6062 completion->priv->completion_timeout =
6063 gdk_threads_add_timeout (COMPLETION_TIMEOUT,
6064 gtk_entry_completion_timeout,
6069 check_completion_callback (GtkEntryCompletion *completion)
6071 completion->priv->check_completion_idle = NULL;
6073 gtk_entry_completion_complete (completion);
6074 gtk_entry_completion_insert_prefix (completion);
6080 clear_completion_callback (GtkEntry *entry,
6083 if (pspec->name == I_("cursor-position") ||
6084 pspec->name == I_("selection-bound"))
6086 GtkEntryCompletion *completion = gtk_entry_get_completion (entry);
6088 completion->priv->has_completion = FALSE;
6093 accept_completion_callback (GtkEntry *entry)
6095 GtkEntryCompletion *completion = gtk_entry_get_completion (entry);
6097 if (completion->priv->has_completion)
6098 gtk_editable_set_position (GTK_EDITABLE (entry),
6099 entry->text_length);
6105 completion_insert_text_callback (GtkEntry *entry,
6109 GtkEntryCompletion *completion)
6111 /* idle to update the selection based on the file list */
6112 if (completion->priv->check_completion_idle == NULL)
6114 completion->priv->check_completion_idle = g_idle_source_new ();
6115 g_source_set_priority (completion->priv->check_completion_idle, G_PRIORITY_HIGH);
6116 g_source_set_closure (completion->priv->check_completion_idle,
6117 g_cclosure_new_object (G_CALLBACK (check_completion_callback),
6118 G_OBJECT (completion)));
6119 g_source_attach (completion->priv->check_completion_idle, NULL);
6124 completion_changed (GtkEntryCompletion *completion,
6128 GtkEntry *entry = GTK_ENTRY (data);
6130 if (pspec->name == I_("popup-completion") ||
6131 pspec->name == I_("inline-completion"))
6133 disconnect_completion_signals (entry, completion);
6134 connect_completion_signals (entry, completion);
6139 disconnect_completion_signals (GtkEntry *entry,
6140 GtkEntryCompletion *completion)
6142 g_signal_handlers_disconnect_by_func (completion,
6143 G_CALLBACK (completion_changed), entry);
6144 if (completion->priv->changed_id > 0 &&
6145 g_signal_handler_is_connected (entry, completion->priv->changed_id))
6147 g_signal_handler_disconnect (entry, completion->priv->changed_id);
6148 completion->priv->changed_id = 0;
6150 g_signal_handlers_disconnect_by_func (entry,
6151 G_CALLBACK (gtk_entry_completion_key_press), completion);
6152 if (completion->priv->insert_text_id > 0 &&
6153 g_signal_handler_is_connected (entry, completion->priv->insert_text_id))
6155 g_signal_handler_disconnect (entry, completion->priv->insert_text_id);
6156 completion->priv->insert_text_id = 0;
6158 g_signal_handlers_disconnect_by_func (entry,
6159 G_CALLBACK (completion_insert_text_callback), completion);
6160 g_signal_handlers_disconnect_by_func (entry,
6161 G_CALLBACK (clear_completion_callback), completion);
6162 g_signal_handlers_disconnect_by_func (entry,
6163 G_CALLBACK (accept_completion_callback), completion);
6167 connect_completion_signals (GtkEntry *entry,
6168 GtkEntryCompletion *completion)
6170 if (completion->priv->popup_completion)
6172 completion->priv->changed_id =
6173 g_signal_connect (entry, "changed",
6174 G_CALLBACK (gtk_entry_completion_changed), completion);
6175 g_signal_connect (entry, "key_press_event",
6176 G_CALLBACK (gtk_entry_completion_key_press), completion);
6179 if (completion->priv->inline_completion)
6181 completion->priv->insert_text_id =
6182 g_signal_connect (entry, "insert_text",
6183 G_CALLBACK (completion_insert_text_callback), completion);
6184 g_signal_connect (entry, "notify",
6185 G_CALLBACK (clear_completion_callback), completion);
6186 g_signal_connect (entry, "activate",
6187 G_CALLBACK (accept_completion_callback), completion);
6188 g_signal_connect (entry, "focus_out_event",
6189 G_CALLBACK (accept_completion_callback), completion);
6192 g_signal_connect (completion, "notify",
6193 G_CALLBACK (completion_changed), entry);
6197 * gtk_entry_set_completion:
6198 * @entry: A #GtkEntry
6199 * @completion: The #GtkEntryCompletion or %NULL
6201 * Sets @completion to be the auxiliary completion object to use with @entry.
6202 * All further configuration of the completion mechanism is done on
6203 * @completion using the #GtkEntryCompletion API. Completion is disabled if
6204 * @completion is set to %NULL.
6209 gtk_entry_set_completion (GtkEntry *entry,
6210 GtkEntryCompletion *completion)
6212 GtkEntryCompletion *old;
6214 g_return_if_fail (GTK_IS_ENTRY (entry));
6215 g_return_if_fail (!completion || GTK_IS_ENTRY_COMPLETION (completion));
6217 old = gtk_entry_get_completion (entry);
6219 if (old == completion)
6224 if (old->priv->completion_timeout)
6226 g_source_remove (old->priv->completion_timeout);
6227 old->priv->completion_timeout = 0;
6230 if (GTK_WIDGET_MAPPED (old->priv->popup_window))
6231 _gtk_entry_completion_popdown (old);
6233 disconnect_completion_signals (entry, old);
6234 old->priv->entry = NULL;
6236 g_object_unref (old);
6241 g_object_set_data (G_OBJECT (entry), I_(GTK_ENTRY_COMPLETION_KEY), NULL);
6245 /* hook into the entry */
6246 g_object_ref (completion);
6248 connect_completion_signals (entry, completion);
6249 completion->priv->entry = GTK_WIDGET (entry);
6250 g_object_set_data (G_OBJECT (entry), I_(GTK_ENTRY_COMPLETION_KEY), completion);
6254 * gtk_entry_get_completion:
6255 * @entry: A #GtkEntry
6257 * Returns the auxiliary completion object currently in use by @entry.
6259 * Return value: The auxiliary completion object currently in use by @entry.
6263 GtkEntryCompletion *
6264 gtk_entry_get_completion (GtkEntry *entry)
6266 GtkEntryCompletion *completion;
6268 g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
6270 completion = GTK_ENTRY_COMPLETION (g_object_get_data (G_OBJECT (entry),
6271 GTK_ENTRY_COMPLETION_KEY));
6277 * gtk_entry_set_cursor_hadjustment:
6278 * @entry: a #GtkEntry
6279 * @adjustment: an adjustment which should be adjusted when the cursor
6280 * is moved, or %NULL
6282 * Hooks up an adjustment to the cursor position in an entry, so that when
6283 * the cursor is moved, the adjustment is scrolled to show that position.
6284 * See gtk_scrolled_window_get_hadjustment() for a typical way of obtaining
6287 * The adjustment has to be in pixel units and in the same coordinate system
6293 gtk_entry_set_cursor_hadjustment (GtkEntry *entry,
6294 GtkAdjustment *adjustment)
6296 g_return_if_fail (GTK_IS_ENTRY (entry));
6298 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
6301 g_object_ref (adjustment);
6303 g_object_set_qdata_full (G_OBJECT (entry),
6304 quark_cursor_hadjustment,
6310 * gtk_entry_get_cursor_hadjustment:
6311 * @entry: a #GtkEntry
6313 * Retrieves the horizontal cursor adjustment for the entry.
6314 * See gtk_entry_set_cursor_hadjustment().
6316 * Return value: the horizontal cursor adjustment, or %NULL
6317 * if none has been set.
6322 gtk_entry_get_cursor_hadjustment (GtkEntry *entry)
6324 g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
6326 return g_object_get_qdata (G_OBJECT (entry), quark_cursor_hadjustment);
6329 #define __GTK_ENTRY_C__
6330 #include "gtkaliasdef.c"