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;
77 typedef struct _GtkEntryPrivate GtkEntryPrivate;
79 #define GTK_ENTRY_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_ENTRY, GtkEntryPrivate))
81 struct _GtkEntryPrivate
85 guint blink_time; /* time in msec the cursor has blinked since last user event */
86 guint real_changed : 1;
87 guint change_count : 8;
90 gboolean interior_focus;
91 GtkShadowType shadow_type;
93 GtkAdjustment *cursor_hadjustment;
96 typedef struct _GtkEntryPasswordHint GtkEntryPasswordHint;
98 struct _GtkEntryPasswordHint
100 gchar password_hint[PASSWORD_HINT_MAX];
101 guint password_hint_timeout_id;
102 gint password_hint_length;
103 gint password_hint_position;
122 PROP_CURSOR_POSITION,
123 PROP_SELECTION_BOUND,
130 PROP_ACTIVATES_DEFAULT,
135 PROP_TRUNCATE_MULTILINE,
139 static guint signals[LAST_SIGNAL] = { 0 };
146 /* GObject, GtkObject methods
148 static void gtk_entry_editable_init (GtkEditableClass *iface);
149 static void gtk_entry_cell_editable_init (GtkCellEditableIface *iface);
150 static void gtk_entry_set_property (GObject *object,
154 static void gtk_entry_get_property (GObject *object,
158 static void gtk_entry_finalize (GObject *object);
159 static void gtk_entry_destroy (GtkObject *object);
163 static void gtk_entry_realize (GtkWidget *widget);
164 static void gtk_entry_unrealize (GtkWidget *widget);
165 static void gtk_entry_size_request (GtkWidget *widget,
166 GtkRequisition *requisition);
167 static void gtk_entry_size_allocate (GtkWidget *widget,
168 GtkAllocation *allocation);
169 static void gtk_entry_draw_frame (GtkWidget *widget,
171 static gint gtk_entry_expose (GtkWidget *widget,
172 GdkEventExpose *event);
173 static gint gtk_entry_button_press (GtkWidget *widget,
174 GdkEventButton *event);
175 static gint gtk_entry_button_release (GtkWidget *widget,
176 GdkEventButton *event);
177 static gint gtk_entry_motion_notify (GtkWidget *widget,
178 GdkEventMotion *event);
179 static gint gtk_entry_key_press (GtkWidget *widget,
181 static gint gtk_entry_key_release (GtkWidget *widget,
183 static gint gtk_entry_focus_in (GtkWidget *widget,
184 GdkEventFocus *event);
185 static gint gtk_entry_focus_out (GtkWidget *widget,
186 GdkEventFocus *event);
187 static void gtk_entry_grab_focus (GtkWidget *widget);
188 static void gtk_entry_style_set (GtkWidget *widget,
189 GtkStyle *previous_style);
190 static void gtk_entry_direction_changed (GtkWidget *widget,
191 GtkTextDirection previous_dir);
192 static void gtk_entry_state_changed (GtkWidget *widget,
193 GtkStateType previous_state);
194 static void gtk_entry_screen_changed (GtkWidget *widget,
195 GdkScreen *old_screen);
197 static gboolean gtk_entry_drag_drop (GtkWidget *widget,
198 GdkDragContext *context,
202 static gboolean gtk_entry_drag_motion (GtkWidget *widget,
203 GdkDragContext *context,
207 static void gtk_entry_drag_leave (GtkWidget *widget,
208 GdkDragContext *context,
210 static void gtk_entry_drag_data_received (GtkWidget *widget,
211 GdkDragContext *context,
214 GtkSelectionData *selection_data,
217 static void gtk_entry_drag_data_get (GtkWidget *widget,
218 GdkDragContext *context,
219 GtkSelectionData *selection_data,
222 static void gtk_entry_drag_data_delete (GtkWidget *widget,
223 GdkDragContext *context);
225 /* GtkEditable method implementations
227 static void gtk_entry_insert_text (GtkEditable *editable,
228 const gchar *new_text,
229 gint new_text_length,
231 static void gtk_entry_delete_text (GtkEditable *editable,
234 static gchar * gtk_entry_get_chars (GtkEditable *editable,
237 static void gtk_entry_real_set_position (GtkEditable *editable,
239 static gint gtk_entry_get_position (GtkEditable *editable);
240 static void gtk_entry_set_selection_bounds (GtkEditable *editable,
243 static gboolean gtk_entry_get_selection_bounds (GtkEditable *editable,
247 /* GtkCellEditable method implementations
249 static void gtk_entry_start_editing (GtkCellEditable *cell_editable,
252 /* Default signal handlers
254 static void gtk_entry_real_insert_text (GtkEditable *editable,
255 const gchar *new_text,
256 gint new_text_length,
258 static void gtk_entry_real_delete_text (GtkEditable *editable,
261 static void gtk_entry_move_cursor (GtkEntry *entry,
262 GtkMovementStep step,
264 gboolean extend_selection);
265 static void gtk_entry_insert_at_cursor (GtkEntry *entry,
267 static void gtk_entry_delete_from_cursor (GtkEntry *entry,
270 static void gtk_entry_backspace (GtkEntry *entry);
271 static void gtk_entry_cut_clipboard (GtkEntry *entry);
272 static void gtk_entry_copy_clipboard (GtkEntry *entry);
273 static void gtk_entry_paste_clipboard (GtkEntry *entry);
274 static void gtk_entry_toggle_overwrite (GtkEntry *entry);
275 static void gtk_entry_select_all (GtkEntry *entry);
276 static void gtk_entry_real_activate (GtkEntry *entry);
277 static gboolean gtk_entry_popup_menu (GtkWidget *widget);
279 static void gtk_entry_keymap_direction_changed (GdkKeymap *keymap,
281 /* IM Context Callbacks
283 static void gtk_entry_commit_cb (GtkIMContext *context,
286 static void gtk_entry_preedit_changed_cb (GtkIMContext *context,
288 static gboolean gtk_entry_retrieve_surrounding_cb (GtkIMContext *context,
290 static gboolean gtk_entry_delete_surrounding_cb (GtkIMContext *context,
297 static void gtk_entry_enter_text (GtkEntry *entry,
299 static void gtk_entry_set_positions (GtkEntry *entry,
301 gint selection_bound);
302 static void gtk_entry_draw_text (GtkEntry *entry);
303 static void gtk_entry_draw_cursor (GtkEntry *entry,
305 static PangoLayout *gtk_entry_ensure_layout (GtkEntry *entry,
306 gboolean include_preedit);
307 static void gtk_entry_reset_layout (GtkEntry *entry);
308 static void gtk_entry_queue_draw (GtkEntry *entry);
309 static void gtk_entry_recompute (GtkEntry *entry);
310 static gint gtk_entry_find_position (GtkEntry *entry,
312 static void gtk_entry_get_cursor_locations (GtkEntry *entry,
316 static void gtk_entry_adjust_scroll (GtkEntry *entry);
317 static gint gtk_entry_move_visually (GtkEntry *editable,
320 static gint gtk_entry_move_logically (GtkEntry *entry,
323 static gint gtk_entry_move_forward_word (GtkEntry *entry,
325 gboolean allow_whitespace);
326 static gint gtk_entry_move_backward_word (GtkEntry *entry,
328 gboolean allow_whitespace);
329 static void gtk_entry_delete_whitespace (GtkEntry *entry);
330 static void gtk_entry_select_word (GtkEntry *entry);
331 static void gtk_entry_select_line (GtkEntry *entry);
332 static char * gtk_entry_get_public_chars (GtkEntry *entry,
335 static void gtk_entry_paste (GtkEntry *entry,
337 static void gtk_entry_update_primary_selection (GtkEntry *entry);
338 static void gtk_entry_do_popup (GtkEntry *entry,
339 GdkEventButton *event);
340 static gboolean gtk_entry_mnemonic_activate (GtkWidget *widget,
341 gboolean group_cycling);
342 static void gtk_entry_state_changed (GtkWidget *widget,
343 GtkStateType previous_state);
344 static void gtk_entry_check_cursor_blink (GtkEntry *entry);
345 static void gtk_entry_pend_cursor_blink (GtkEntry *entry);
346 static void gtk_entry_reset_blink_time (GtkEntry *entry);
347 static void get_text_area_size (GtkEntry *entry,
352 static void get_widget_window_size (GtkEntry *entry,
357 static void gtk_entry_move_adjustments (GtkEntry *entry);
360 static gint gtk_entry_completion_timeout (gpointer data);
361 static gboolean gtk_entry_completion_key_press (GtkWidget *widget,
364 static void gtk_entry_completion_changed (GtkWidget *entry,
366 static gboolean check_completion_callback (GtkEntryCompletion *completion);
367 static void clear_completion_callback (GtkEntry *entry,
369 static gboolean accept_completion_callback (GtkEntry *entry);
370 static void completion_insert_text_callback (GtkEntry *entry,
374 GtkEntryCompletion *completion);
375 static void completion_changed (GtkEntryCompletion *completion,
378 static void disconnect_completion_signals (GtkEntry *entry,
379 GtkEntryCompletion *completion);
380 static void connect_completion_signals (GtkEntry *entry,
381 GtkEntryCompletion *completion);
383 static void begin_change (GtkEntry *entry);
384 static void end_change (GtkEntry *entry);
385 static void emit_changed (GtkEntry *entry);
387 G_DEFINE_TYPE_WITH_CODE (GtkEntry, gtk_entry, GTK_TYPE_WIDGET,
388 G_IMPLEMENT_INTERFACE (GTK_TYPE_EDITABLE,
389 gtk_entry_editable_init)
390 G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_EDITABLE,
391 gtk_entry_cell_editable_init))
394 add_move_binding (GtkBindingSet *binding_set,
397 GtkMovementStep step,
400 g_return_if_fail ((modmask & GDK_SHIFT_MASK) == 0);
402 gtk_binding_entry_add_signal (binding_set, keyval, modmask,
406 G_TYPE_BOOLEAN, FALSE);
408 /* Selection-extending version */
409 gtk_binding_entry_add_signal (binding_set, keyval, modmask | GDK_SHIFT_MASK,
413 G_TYPE_BOOLEAN, TRUE);
417 gtk_entry_class_init (GtkEntryClass *class)
419 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
420 GtkWidgetClass *widget_class;
421 GtkObjectClass *gtk_object_class;
422 GtkBindingSet *binding_set;
424 widget_class = (GtkWidgetClass*) class;
425 gtk_object_class = (GtkObjectClass *)class;
427 gobject_class->finalize = gtk_entry_finalize;
428 gobject_class->set_property = gtk_entry_set_property;
429 gobject_class->get_property = gtk_entry_get_property;
431 widget_class->realize = gtk_entry_realize;
432 widget_class->unrealize = gtk_entry_unrealize;
433 widget_class->size_request = gtk_entry_size_request;
434 widget_class->size_allocate = gtk_entry_size_allocate;
435 widget_class->expose_event = gtk_entry_expose;
436 widget_class->button_press_event = gtk_entry_button_press;
437 widget_class->button_release_event = gtk_entry_button_release;
438 widget_class->motion_notify_event = gtk_entry_motion_notify;
439 widget_class->key_press_event = gtk_entry_key_press;
440 widget_class->key_release_event = gtk_entry_key_release;
441 widget_class->focus_in_event = gtk_entry_focus_in;
442 widget_class->focus_out_event = gtk_entry_focus_out;
443 widget_class->grab_focus = gtk_entry_grab_focus;
444 widget_class->style_set = gtk_entry_style_set;
445 widget_class->direction_changed = gtk_entry_direction_changed;
446 widget_class->state_changed = gtk_entry_state_changed;
447 widget_class->screen_changed = gtk_entry_screen_changed;
448 widget_class->mnemonic_activate = gtk_entry_mnemonic_activate;
450 widget_class->drag_drop = gtk_entry_drag_drop;
451 widget_class->drag_motion = gtk_entry_drag_motion;
452 widget_class->drag_leave = gtk_entry_drag_leave;
453 widget_class->drag_data_received = gtk_entry_drag_data_received;
454 widget_class->drag_data_get = gtk_entry_drag_data_get;
455 widget_class->drag_data_delete = gtk_entry_drag_data_delete;
457 widget_class->popup_menu = gtk_entry_popup_menu;
459 gtk_object_class->destroy = gtk_entry_destroy;
461 class->move_cursor = gtk_entry_move_cursor;
462 class->insert_at_cursor = gtk_entry_insert_at_cursor;
463 class->delete_from_cursor = gtk_entry_delete_from_cursor;
464 class->backspace = gtk_entry_backspace;
465 class->cut_clipboard = gtk_entry_cut_clipboard;
466 class->copy_clipboard = gtk_entry_copy_clipboard;
467 class->paste_clipboard = gtk_entry_paste_clipboard;
468 class->toggle_overwrite = gtk_entry_toggle_overwrite;
469 class->activate = gtk_entry_real_activate;
471 quark_inner_border = g_quark_from_static_string ("gtk-entry-inner-border");
472 quark_password_hint = g_quark_from_static_string ("gtk-entry-password-hint");
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_KP_Enter, 0,
827 gtk_binding_entry_add_signal (binding_set, GDK_Delete, 0,
828 "delete_from_cursor", 2,
829 G_TYPE_ENUM, GTK_DELETE_CHARS,
832 gtk_binding_entry_add_signal (binding_set, GDK_KP_Delete, 0,
833 "delete_from_cursor", 2,
834 G_TYPE_ENUM, GTK_DELETE_CHARS,
837 gtk_binding_entry_add_signal (binding_set, GDK_BackSpace, 0,
840 /* Make this do the same as Backspace, to help with mis-typing */
841 gtk_binding_entry_add_signal (binding_set, GDK_BackSpace, GDK_SHIFT_MASK,
844 gtk_binding_entry_add_signal (binding_set, GDK_Delete, GDK_CONTROL_MASK,
845 "delete_from_cursor", 2,
846 G_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
849 gtk_binding_entry_add_signal (binding_set, GDK_KP_Delete, GDK_CONTROL_MASK,
850 "delete_from_cursor", 2,
851 G_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
854 gtk_binding_entry_add_signal (binding_set, GDK_BackSpace, GDK_CONTROL_MASK,
855 "delete_from_cursor", 2,
856 G_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
861 gtk_binding_entry_add_signal (binding_set, GDK_x, GDK_CONTROL_MASK,
863 gtk_binding_entry_add_signal (binding_set, GDK_c, GDK_CONTROL_MASK,
864 "copy_clipboard", 0);
865 gtk_binding_entry_add_signal (binding_set, GDK_v, GDK_CONTROL_MASK,
866 "paste_clipboard", 0);
868 gtk_binding_entry_add_signal (binding_set, GDK_Delete, GDK_SHIFT_MASK,
870 gtk_binding_entry_add_signal (binding_set, GDK_Insert, GDK_CONTROL_MASK,
871 "copy_clipboard", 0);
872 gtk_binding_entry_add_signal (binding_set, GDK_Insert, GDK_SHIFT_MASK,
873 "paste_clipboard", 0);
876 gtk_binding_entry_add_signal (binding_set, GDK_Insert, 0,
877 "toggle_overwrite", 0);
878 gtk_binding_entry_add_signal (binding_set, GDK_KP_Insert, 0,
879 "toggle_overwrite", 0);
882 * GtkEntry:inner-border:
884 * Sets the text area's border between the text and the frame.
888 gtk_widget_class_install_style_property (widget_class,
889 g_param_spec_boxed ("inner-border",
891 P_("Border between text and frame."),
893 GTK_PARAM_READABLE));
895 gtk_settings_install_property (g_param_spec_boolean ("gtk-entry-select-on-focus",
896 P_("Select on focus"),
897 P_("Whether to select the contents of an entry when it is focused"),
899 GTK_PARAM_READWRITE));
902 * GtkSettings:gtk-entry-password-hint-timeout:
904 * How long to show the last inputted character in hidden
905 * entries. This value is in milliseconds. 0 disables showing the
906 * last char. 600 is a good value for enabling it.
910 gtk_settings_install_property (g_param_spec_uint ("gtk-entry-password-hint-timeout",
911 P_("Password Hint Timeout"),
912 P_("How long to show the last inputted character in hidden entries"),
914 GTK_PARAM_READWRITE));
916 g_type_class_add_private (gobject_class, sizeof (GtkEntryPrivate));
920 gtk_entry_editable_init (GtkEditableClass *iface)
922 iface->do_insert_text = gtk_entry_insert_text;
923 iface->do_delete_text = gtk_entry_delete_text;
924 iface->insert_text = gtk_entry_real_insert_text;
925 iface->delete_text = gtk_entry_real_delete_text;
926 iface->get_chars = gtk_entry_get_chars;
927 iface->set_selection_bounds = gtk_entry_set_selection_bounds;
928 iface->get_selection_bounds = gtk_entry_get_selection_bounds;
929 iface->set_position = gtk_entry_real_set_position;
930 iface->get_position = gtk_entry_get_position;
934 gtk_entry_cell_editable_init (GtkCellEditableIface *iface)
936 iface->start_editing = gtk_entry_start_editing;
940 gtk_entry_set_property (GObject *object,
945 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (object);
946 GtkEntry *entry = GTK_ENTRY (object);
952 gboolean new_value = g_value_get_boolean (value);
954 if (new_value != entry->editable)
958 _gtk_entry_reset_im_context (entry);
959 if (GTK_WIDGET_HAS_FOCUS (entry))
960 gtk_im_context_focus_out (entry->im_context);
962 entry->preedit_length = 0;
963 entry->preedit_cursor = 0;
966 entry->editable = new_value;
968 if (new_value && GTK_WIDGET_HAS_FOCUS (entry))
969 gtk_im_context_focus_in (entry->im_context);
971 gtk_entry_queue_draw (entry);
976 case PROP_MAX_LENGTH:
977 gtk_entry_set_max_length (entry, g_value_get_int (value));
980 case PROP_VISIBILITY:
981 gtk_entry_set_visibility (entry, g_value_get_boolean (value));
985 gtk_entry_set_has_frame (entry, g_value_get_boolean (value));
988 case PROP_INNER_BORDER:
989 gtk_entry_set_inner_border (entry, g_value_get_boxed (value));
992 case PROP_INVISIBLE_CHAR:
993 gtk_entry_set_invisible_char (entry, g_value_get_uint (value));
996 case PROP_ACTIVATES_DEFAULT:
997 gtk_entry_set_activates_default (entry, g_value_get_boolean (value));
1000 case PROP_WIDTH_CHARS:
1001 gtk_entry_set_width_chars (entry, g_value_get_int (value));
1005 gtk_entry_set_text (entry, g_value_get_string (value));
1009 gtk_entry_set_alignment (entry, g_value_get_float (value));
1012 case PROP_TRUNCATE_MULTILINE:
1013 entry->truncate_multiline = g_value_get_boolean (value);
1016 case PROP_SHADOW_TYPE:
1017 priv->shadow_type = g_value_get_enum (value);
1020 case PROP_SCROLL_OFFSET:
1021 case PROP_CURSOR_POSITION:
1023 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1029 gtk_entry_get_property (GObject *object,
1034 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (object);
1035 GtkEntry *entry = GTK_ENTRY (object);
1039 case PROP_CURSOR_POSITION:
1040 g_value_set_int (value, entry->current_pos);
1042 case PROP_SELECTION_BOUND:
1043 g_value_set_int (value, entry->selection_bound);
1046 g_value_set_boolean (value, entry->editable);
1048 case PROP_MAX_LENGTH:
1049 g_value_set_int (value, entry->text_max_length);
1051 case PROP_VISIBILITY:
1052 g_value_set_boolean (value, entry->visible);
1054 case PROP_HAS_FRAME:
1055 g_value_set_boolean (value, entry->has_frame);
1057 case PROP_INNER_BORDER:
1058 g_value_set_boxed (value, gtk_entry_get_inner_border (entry));
1060 case PROP_INVISIBLE_CHAR:
1061 g_value_set_uint (value, entry->invisible_char);
1063 case PROP_ACTIVATES_DEFAULT:
1064 g_value_set_boolean (value, entry->activates_default);
1066 case PROP_WIDTH_CHARS:
1067 g_value_set_int (value, entry->width_chars);
1069 case PROP_SCROLL_OFFSET:
1070 g_value_set_int (value, entry->scroll_offset);
1073 g_value_set_string (value, gtk_entry_get_text (entry));
1076 g_value_set_float (value, gtk_entry_get_alignment (entry));
1078 case PROP_TRUNCATE_MULTILINE:
1079 g_value_set_boolean (value, entry->truncate_multiline);
1081 case PROP_SHADOW_TYPE:
1082 g_value_set_enum (value, priv->shadow_type);
1086 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1092 gtk_entry_init (GtkEntry *entry)
1094 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
1096 GTK_WIDGET_SET_FLAGS (entry, GTK_CAN_FOCUS);
1098 entry->text_size = MIN_SIZE;
1099 entry->text = g_malloc (entry->text_size);
1100 entry->text[0] = '\0';
1102 entry->editable = TRUE;
1103 entry->visible = TRUE;
1104 entry->invisible_char = '*';
1105 entry->dnd_position = -1;
1106 entry->width_chars = -1;
1107 entry->is_cell_renderer = FALSE;
1108 entry->editing_canceled = FALSE;
1109 entry->has_frame = TRUE;
1110 entry->truncate_multiline = FALSE;
1111 priv->shadow_type = GTK_SHADOW_IN;
1114 gtk_drag_dest_set (GTK_WIDGET (entry),
1115 GTK_DEST_DEFAULT_HIGHLIGHT,
1117 GDK_ACTION_COPY | GDK_ACTION_MOVE);
1118 gtk_drag_dest_add_text_targets (GTK_WIDGET (entry));
1120 /* This object is completely private. No external entity can gain a reference
1121 * to it; so we create it here and destroy it in finalize().
1123 entry->im_context = gtk_im_multicontext_new ();
1125 g_signal_connect (entry->im_context, "commit",
1126 G_CALLBACK (gtk_entry_commit_cb), entry);
1127 g_signal_connect (entry->im_context, "preedit_changed",
1128 G_CALLBACK (gtk_entry_preedit_changed_cb), entry);
1129 g_signal_connect (entry->im_context, "retrieve_surrounding",
1130 G_CALLBACK (gtk_entry_retrieve_surrounding_cb), entry);
1131 g_signal_connect (entry->im_context, "delete_surrounding",
1132 G_CALLBACK (gtk_entry_delete_surrounding_cb), entry);
1136 begin_change (GtkEntry *entry)
1138 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
1140 priv->change_count++;
1144 end_change (GtkEntry *entry)
1146 GtkEditable *editable = GTK_EDITABLE (entry);
1147 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
1149 g_return_if_fail (priv->change_count > 0);
1151 priv->change_count--;
1153 if (priv->change_count == 0)
1155 if (priv->real_changed)
1157 g_signal_emit_by_name (editable, "changed");
1158 priv->real_changed = FALSE;
1164 emit_changed (GtkEntry *entry)
1166 GtkEditable *editable = GTK_EDITABLE (entry);
1167 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
1169 if (priv->change_count == 0)
1170 g_signal_emit_by_name (editable, "changed");
1172 priv->real_changed = TRUE;
1176 * Overwrite a memory that might contain sensitive information.
1179 trash_area (gchar *area, gsize len)
1181 volatile gchar *varea = (volatile gchar *)area;
1187 gtk_entry_destroy (GtkObject *object)
1189 GtkEntry *entry = GTK_ENTRY (object);
1192 entry->current_pos = entry->selection_bound = entry->text_length = 0;
1193 _gtk_entry_reset_im_context (entry);
1194 gtk_entry_reset_layout (entry);
1196 if (entry->blink_timeout)
1198 g_source_remove (entry->blink_timeout);
1199 entry->blink_timeout = 0;
1202 if (entry->recompute_idle)
1204 g_source_remove (entry->recompute_idle);
1205 entry->recompute_idle = 0;
1208 if (!entry->visible)
1210 /* We want to trash the text here because the entry might be leaked. */
1211 trash_area (entry->text, strlen (entry->text));
1214 GTK_OBJECT_CLASS (gtk_entry_parent_class)->destroy (object);
1218 gtk_entry_finalize (GObject *object)
1220 GtkEntry *entry = GTK_ENTRY (object);
1222 gtk_entry_set_completion (entry, NULL);
1224 if (entry->cached_layout)
1225 g_object_unref (entry->cached_layout);
1227 g_object_unref (entry->im_context);
1229 if (entry->blink_timeout)
1230 g_source_remove (entry->blink_timeout);
1232 if (entry->recompute_idle)
1233 g_source_remove (entry->recompute_idle);
1235 entry->text_size = 0;
1239 if (!entry->visible)
1240 trash_area (entry->text, strlen (entry->text));
1241 g_free (entry->text);
1245 G_OBJECT_CLASS (gtk_entry_parent_class)->finalize (object);
1249 gtk_entry_realize (GtkWidget *widget)
1252 GdkWindowAttr attributes;
1253 gint attributes_mask;
1255 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
1256 entry = GTK_ENTRY (widget);
1258 attributes.window_type = GDK_WINDOW_CHILD;
1260 get_widget_window_size (entry, &attributes.x, &attributes.y, &attributes.width, &attributes.height);
1262 attributes.wclass = GDK_INPUT_OUTPUT;
1263 attributes.visual = gtk_widget_get_visual (widget);
1264 attributes.colormap = gtk_widget_get_colormap (widget);
1265 attributes.event_mask = gtk_widget_get_events (widget);
1266 attributes.event_mask |= (GDK_EXPOSURE_MASK |
1267 GDK_BUTTON_PRESS_MASK |
1268 GDK_BUTTON_RELEASE_MASK |
1269 GDK_BUTTON1_MOTION_MASK |
1270 GDK_BUTTON3_MOTION_MASK |
1271 GDK_POINTER_MOTION_HINT_MASK |
1272 GDK_POINTER_MOTION_MASK |
1273 GDK_ENTER_NOTIFY_MASK |
1274 GDK_LEAVE_NOTIFY_MASK);
1275 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
1277 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
1278 gdk_window_set_user_data (widget->window, entry);
1280 get_text_area_size (entry, &attributes.x, &attributes.y, &attributes.width, &attributes.height);
1282 if (GTK_WIDGET_IS_SENSITIVE (widget))
1284 attributes.cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget), GDK_XTERM);
1285 attributes_mask |= GDK_WA_CURSOR;
1288 entry->text_area = gdk_window_new (widget->window, &attributes, attributes_mask);
1290 gdk_window_set_user_data (entry->text_area, entry);
1292 if (attributes_mask & GDK_WA_CURSOR)
1293 gdk_cursor_unref (attributes.cursor);
1295 widget->style = gtk_style_attach (widget->style, widget->window);
1297 gdk_window_set_background (widget->window, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1298 gdk_window_set_background (entry->text_area, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1300 gdk_window_show (entry->text_area);
1302 gtk_im_context_set_client_window (entry->im_context, entry->text_area);
1304 gtk_entry_adjust_scroll (entry);
1305 gtk_entry_update_primary_selection (entry);
1309 gtk_entry_unrealize (GtkWidget *widget)
1311 GtkEntry *entry = GTK_ENTRY (widget);
1312 GtkClipboard *clipboard;
1314 gtk_entry_reset_layout (entry);
1316 gtk_im_context_set_client_window (entry->im_context, NULL);
1318 clipboard = gtk_widget_get_clipboard (widget, GDK_SELECTION_PRIMARY);
1319 if (gtk_clipboard_get_owner (clipboard) == G_OBJECT (entry))
1320 gtk_clipboard_clear (clipboard);
1322 if (entry->text_area)
1324 gdk_window_set_user_data (entry->text_area, NULL);
1325 gdk_window_destroy (entry->text_area);
1326 entry->text_area = NULL;
1329 if (entry->popup_menu)
1331 gtk_widget_destroy (entry->popup_menu);
1332 entry->popup_menu = NULL;
1335 if (GTK_WIDGET_CLASS (gtk_entry_parent_class)->unrealize)
1336 (* GTK_WIDGET_CLASS (gtk_entry_parent_class)->unrealize) (widget);
1340 _gtk_entry_get_borders (GtkEntry *entry,
1344 GtkWidget *widget = GTK_WIDGET (entry);
1345 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (widget);
1347 if (entry->has_frame)
1349 *xborder = widget->style->xthickness;
1350 *yborder = widget->style->ythickness;
1358 if (!priv->interior_focus)
1360 *xborder += priv->focus_width;
1361 *yborder += priv->focus_width;
1366 gtk_entry_size_request (GtkWidget *widget,
1367 GtkRequisition *requisition)
1369 GtkEntry *entry = GTK_ENTRY (widget);
1370 PangoFontMetrics *metrics;
1371 gint xborder, yborder;
1372 GtkBorder inner_border;
1373 PangoContext *context;
1375 gtk_widget_ensure_style (widget);
1376 context = gtk_widget_get_pango_context (widget);
1377 metrics = pango_context_get_metrics (context,
1378 widget->style->font_desc,
1379 pango_context_get_language (context));
1381 entry->ascent = pango_font_metrics_get_ascent (metrics);
1382 entry->descent = pango_font_metrics_get_descent (metrics);
1384 _gtk_entry_get_borders (entry, &xborder, &yborder);
1385 _gtk_entry_effective_inner_border (entry, &inner_border);
1387 if (entry->width_chars < 0)
1388 requisition->width = MIN_ENTRY_WIDTH + xborder * 2 + inner_border.left + inner_border.right;
1391 gint char_width = pango_font_metrics_get_approximate_char_width (metrics);
1392 gint digit_width = pango_font_metrics_get_approximate_digit_width (metrics);
1393 gint char_pixels = (MAX (char_width, digit_width) + PANGO_SCALE - 1) / PANGO_SCALE;
1395 requisition->width = char_pixels * entry->width_chars + xborder * 2 + inner_border.left + inner_border.right;
1398 requisition->height = PANGO_PIXELS (entry->ascent + entry->descent) + yborder * 2 + inner_border.top + inner_border.bottom;
1400 pango_font_metrics_unref (metrics);
1404 get_text_area_size (GtkEntry *entry,
1411 gint xborder, yborder;
1412 GtkRequisition requisition;
1413 GtkWidget *widget = GTK_WIDGET (entry);
1414 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (widget);
1416 gtk_widget_get_child_requisition (widget, &requisition);
1417 _gtk_entry_get_borders (entry, &xborder, &yborder);
1419 if (GTK_WIDGET_REALIZED (widget))
1420 gdk_drawable_get_size (widget->window, NULL, &frame_height);
1422 frame_height = requisition.height;
1424 if (GTK_WIDGET_HAS_FOCUS (widget) && !priv->interior_focus)
1425 frame_height -= 2 * priv->focus_width;
1431 *y = frame_height / 2 - (requisition.height - yborder * 2) / 2;
1434 *width = GTK_WIDGET (entry)->allocation.width - xborder * 2;
1437 *height = requisition.height - yborder * 2;
1441 get_widget_window_size (GtkEntry *entry,
1447 GtkRequisition requisition;
1448 GtkWidget *widget = GTK_WIDGET (entry);
1450 gtk_widget_get_child_requisition (widget, &requisition);
1453 *x = widget->allocation.x;
1457 if (entry->is_cell_renderer)
1458 *y = widget->allocation.y;
1460 *y = widget->allocation.y + (widget->allocation.height - requisition.height) / 2;
1464 *width = widget->allocation.width;
1468 if (entry->is_cell_renderer)
1469 *height = widget->allocation.height;
1471 *height = requisition.height;
1476 _gtk_entry_effective_inner_border (GtkEntry *entry,
1479 GtkBorder *tmp_border;
1481 tmp_border = g_object_get_qdata (G_OBJECT (entry), quark_inner_border);
1485 *border = *tmp_border;
1489 gtk_widget_style_get (GTK_WIDGET (entry), "inner-border", &tmp_border, NULL);
1493 *border = *tmp_border;
1494 gtk_border_free (tmp_border);
1498 *border = default_inner_border;
1502 gtk_entry_size_allocate (GtkWidget *widget,
1503 GtkAllocation *allocation)
1505 GtkEntry *entry = GTK_ENTRY (widget);
1507 widget->allocation = *allocation;
1509 if (GTK_WIDGET_REALIZED (widget))
1511 /* We call gtk_widget_get_child_requisition, since we want (for
1512 * backwards compatibility reasons) the realization here to
1513 * be affected by the usize of the entry, if set
1515 gint x, y, width, height;
1517 get_widget_window_size (entry, &x, &y, &width, &height);
1519 gdk_window_move_resize (widget->window,
1520 x, y, width, height);
1522 get_text_area_size (entry, &x, &y, &width, &height);
1524 gdk_window_move_resize (entry->text_area,
1525 x, y, width, height);
1527 gtk_entry_recompute (entry);
1532 gtk_entry_draw_frame (GtkWidget *widget,
1535 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (widget);
1536 gint x = 0, y = 0, width, height;
1538 gdk_drawable_get_size (widget->window, &width, &height);
1540 if (GTK_WIDGET_HAS_FOCUS (widget) && !priv->interior_focus)
1542 x += priv->focus_width;
1543 y += priv->focus_width;
1544 width -= 2 * priv->focus_width;
1545 height -= 2 * priv->focus_width;
1548 gtk_paint_shadow (widget->style, widget->window,
1549 GTK_STATE_NORMAL, priv->shadow_type,
1550 area, widget, "entry", x, y, width, height);
1552 if (GTK_WIDGET_HAS_FOCUS (widget) && !priv->interior_focus)
1554 x -= priv->focus_width;
1555 y -= priv->focus_width;
1556 width += 2 * priv->focus_width;
1557 height += 2 * priv->focus_width;
1559 gtk_paint_focus (widget->style, widget->window, GTK_WIDGET_STATE (widget),
1560 area, widget, "entry",
1561 0, 0, width, height);
1566 gtk_entry_expose (GtkWidget *widget,
1567 GdkEventExpose *event)
1569 GtkEntry *entry = GTK_ENTRY (widget);
1571 if (widget->window == event->window)
1572 gtk_entry_draw_frame (widget, &event->area);
1573 else if (entry->text_area == event->window)
1575 gint area_width, area_height;
1577 get_text_area_size (entry, NULL, NULL, &area_width, &area_height);
1579 gtk_paint_flat_box (widget->style, entry->text_area,
1580 GTK_WIDGET_STATE(widget), GTK_SHADOW_NONE,
1581 &event->area, widget, "entry_bg",
1582 0, 0, area_width, area_height);
1584 if ((entry->visible || entry->invisible_char != 0) &&
1585 GTK_WIDGET_HAS_FOCUS (widget) &&
1586 entry->selection_bound == entry->current_pos && entry->cursor_visible)
1587 gtk_entry_draw_cursor (GTK_ENTRY (widget), CURSOR_STANDARD);
1589 if (entry->dnd_position != -1)
1590 gtk_entry_draw_cursor (GTK_ENTRY (widget), CURSOR_DND);
1592 gtk_entry_draw_text (GTK_ENTRY (widget));
1599 gtk_entry_get_pixel_ranges (GtkEntry *entry,
1603 gint start_char, end_char;
1605 if (gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &start_char, &end_char))
1607 PangoLayout *layout = gtk_entry_ensure_layout (entry, TRUE);
1608 PangoLayoutLine *line = pango_layout_get_lines_readonly (layout)->data;
1609 const char *text = pango_layout_get_text (layout);
1610 gint start_index = g_utf8_offset_to_pointer (text, start_char) - text;
1611 gint end_index = g_utf8_offset_to_pointer (text, end_char) - text;
1612 gint real_n_ranges, i;
1614 pango_layout_line_get_x_ranges (line, start_index, end_index, ranges, &real_n_ranges);
1620 for (i = 0; i < real_n_ranges; ++i)
1622 r[2 * i + 1] = (r[2 * i + 1] - r[2 * i]) / PANGO_SCALE;
1623 r[2 * i] = r[2 * i] / PANGO_SCALE;
1628 *n_ranges = real_n_ranges;
1640 in_selection (GtkEntry *entry,
1645 gint retval = FALSE;
1647 gtk_entry_get_pixel_ranges (entry, &ranges, &n_ranges);
1649 for (i = 0; i < n_ranges; ++i)
1651 if (x >= ranges[2 * i] && x < ranges[2 * i] + ranges[2 * i + 1])
1663 gtk_entry_button_press (GtkWidget *widget,
1664 GdkEventButton *event)
1666 GtkEntry *entry = GTK_ENTRY (widget);
1667 GtkEditable *editable = GTK_EDITABLE (widget);
1668 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
1670 gint sel_start, sel_end;
1672 if (event->window != entry->text_area ||
1673 (entry->button && event->button != entry->button))
1676 gtk_entry_reset_blink_time (entry);
1678 entry->button = event->button;
1680 if (!GTK_WIDGET_HAS_FOCUS (widget))
1682 entry->in_click = TRUE;
1683 gtk_widget_grab_focus (widget);
1684 entry->in_click = FALSE;
1687 tmp_pos = gtk_entry_find_position (entry, event->x + entry->scroll_offset);
1689 if (event->button == 1)
1691 gboolean have_selection = gtk_editable_get_selection_bounds (editable, &sel_start, &sel_end);
1693 entry->select_words = FALSE;
1694 entry->select_lines = FALSE;
1696 if (event->state & GDK_SHIFT_MASK)
1698 _gtk_entry_reset_im_context (entry);
1700 if (!have_selection) /* select from the current position to the clicked position */
1701 sel_start = sel_end = entry->current_pos;
1703 if (tmp_pos > sel_start && tmp_pos < sel_end)
1705 /* Truncate current selection, but keep it as big as possible */
1706 if (tmp_pos - sel_start > sel_end - tmp_pos)
1707 gtk_entry_set_positions (entry, sel_start, tmp_pos);
1709 gtk_entry_set_positions (entry, tmp_pos, sel_end);
1713 gboolean extend_to_left;
1716 /* Figure out what click selects and extend current selection */
1717 switch (event->type)
1719 case GDK_BUTTON_PRESS:
1720 gtk_entry_set_positions (entry, tmp_pos, tmp_pos);
1723 case GDK_2BUTTON_PRESS:
1724 entry->select_words = TRUE;
1725 gtk_entry_select_word (entry);
1728 case GDK_3BUTTON_PRESS:
1729 entry->select_lines = TRUE;
1730 gtk_entry_select_line (entry);
1737 start = MIN (entry->current_pos, entry->selection_bound);
1738 start = MIN (sel_start, start);
1740 end = MAX (entry->current_pos, entry->selection_bound);
1741 end = MAX (sel_end, end);
1743 if (tmp_pos == sel_start || tmp_pos == sel_end)
1744 extend_to_left = (tmp_pos == start);
1746 extend_to_left = (end == sel_end);
1749 gtk_entry_set_positions (entry, start, end);
1751 gtk_entry_set_positions (entry, end, start);
1754 else /* no shift key */
1755 switch (event->type)
1757 case GDK_BUTTON_PRESS:
1758 if (in_selection (entry, event->x + entry->scroll_offset))
1760 /* Click inside the selection - we'll either start a drag, or
1761 * clear the selection
1763 entry->in_drag = TRUE;
1764 entry->drag_start_x = event->x + entry->scroll_offset;
1765 entry->drag_start_y = event->y;
1768 gtk_editable_set_position (editable, tmp_pos);
1771 case GDK_2BUTTON_PRESS:
1772 /* We ALWAYS receive a GDK_BUTTON_PRESS immediately before
1773 * receiving a GDK_2BUTTON_PRESS so we need to reset
1774 * entry->in_drag which may have been set above
1776 entry->in_drag = FALSE;
1777 entry->select_words = TRUE;
1778 gtk_entry_select_word (entry);
1781 case GDK_3BUTTON_PRESS:
1782 /* We ALWAYS receive a GDK_BUTTON_PRESS immediately before
1783 * receiving a GDK_3BUTTON_PRESS so we need to reset
1784 * entry->in_drag which may have been set above
1786 entry->in_drag = FALSE;
1787 entry->select_lines = TRUE;
1788 gtk_entry_select_line (entry);
1797 else if (event->button == 2 && event->type == GDK_BUTTON_PRESS)
1799 if (entry->editable)
1801 priv->insert_pos = tmp_pos;
1802 gtk_entry_paste (entry, GDK_SELECTION_PRIMARY);
1807 gtk_widget_error_bell (widget);
1810 else if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
1812 gtk_entry_do_popup (entry, event);
1813 entry->button = 0; /* Don't wait for release, since the menu will gtk_grab_add */
1822 gtk_entry_button_release (GtkWidget *widget,
1823 GdkEventButton *event)
1825 GtkEntry *entry = GTK_ENTRY (widget);
1827 if (event->window != entry->text_area || entry->button != event->button)
1832 gint tmp_pos = gtk_entry_find_position (entry, entry->drag_start_x);
1834 gtk_editable_set_position (GTK_EDITABLE (entry), tmp_pos);
1841 gtk_entry_update_primary_selection (entry);
1847 _gtk_entry_get_selected_text (GtkEntry *entry)
1849 GtkEditable *editable = GTK_EDITABLE (entry);
1850 gint start_text, end_text;
1853 if (gtk_editable_get_selection_bounds (editable, &start_text, &end_text))
1854 text = gtk_editable_get_chars (editable, start_text, end_text);
1860 drag_begin_cb (GtkWidget *widget,
1861 GdkDragContext *context,
1864 g_signal_handlers_disconnect_by_func (widget, drag_begin_cb, NULL);
1868 gtk_entry_motion_notify (GtkWidget *widget,
1869 GdkEventMotion *event)
1871 GtkEntry *entry = GTK_ENTRY (widget);
1874 if (entry->mouse_cursor_obscured)
1878 cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget), GDK_XTERM);
1879 gdk_window_set_cursor (entry->text_area, cursor);
1880 gdk_cursor_unref (cursor);
1881 entry->mouse_cursor_obscured = FALSE;
1884 if (event->window != entry->text_area || entry->button != 1)
1887 if (entry->select_lines)
1890 gdk_event_request_motions (event);
1894 if (gtk_drag_check_threshold (widget,
1895 entry->drag_start_x, entry->drag_start_y,
1896 event->x + entry->scroll_offset, event->y))
1898 GdkDragContext *context;
1899 GtkTargetList *target_list = gtk_target_list_new (NULL, 0);
1900 guint actions = entry->editable ? GDK_ACTION_COPY | GDK_ACTION_MOVE : GDK_ACTION_COPY;
1902 GdkPixmap *pixmap = NULL;
1904 gtk_target_list_add_text_targets (target_list, 0);
1908 text = _gtk_entry_get_selected_text (entry);
1909 pixmap = _gtk_text_util_create_drag_icon (widget, text, -1);
1912 context = gtk_drag_begin (widget, target_list, actions,
1913 entry->button, (GdkEvent *)event);
1916 gtk_drag_set_icon_pixmap (context,
1917 gdk_drawable_get_colormap (pixmap),
1922 gtk_drag_set_icon_default (context);
1925 g_object_unref (pixmap);
1928 entry->in_drag = FALSE;
1931 gtk_target_list_unref (target_list);
1937 gdk_drawable_get_size (entry->text_area, NULL, &height);
1941 else if (event->y >= height)
1942 tmp_pos = entry->text_length;
1944 tmp_pos = gtk_entry_find_position (entry, event->x + entry->scroll_offset);
1946 if (entry->select_words)
1949 gint old_min, old_max;
1952 min = gtk_entry_move_backward_word (entry, tmp_pos, TRUE);
1953 max = gtk_entry_move_forward_word (entry, tmp_pos, TRUE);
1955 pos = entry->current_pos;
1956 bound = entry->selection_bound;
1958 old_min = MIN(entry->current_pos, entry->selection_bound);
1959 old_max = MAX(entry->current_pos, entry->selection_bound);
1966 else if (old_max < max)
1971 else if (pos == old_min)
1973 if (entry->current_pos != min)
1978 if (entry->current_pos != max)
1982 gtk_entry_set_positions (entry, pos, bound);
1985 gtk_entry_set_positions (entry, tmp_pos, -1);
1992 set_invisible_cursor (GdkWindow *window)
1994 GdkBitmap *empty_bitmap;
1997 char invisible_cursor_bits[] = { 0x0 };
1999 useless.red = useless.green = useless.blue = 0;
2002 empty_bitmap = gdk_bitmap_create_from_data (window,
2003 invisible_cursor_bits,
2006 cursor = gdk_cursor_new_from_pixmap (empty_bitmap,
2011 gdk_window_set_cursor (window, cursor);
2013 gdk_cursor_unref (cursor);
2015 g_object_unref (empty_bitmap);
2019 gtk_entry_obscure_mouse_cursor (GtkEntry *entry)
2021 if (entry->mouse_cursor_obscured)
2024 set_invisible_cursor (entry->text_area);
2026 entry->mouse_cursor_obscured = TRUE;
2030 gtk_entry_key_press (GtkWidget *widget,
2033 GtkEntry *entry = GTK_ENTRY (widget);
2035 gtk_entry_reset_blink_time (entry);
2036 gtk_entry_pend_cursor_blink (entry);
2038 if (entry->editable)
2040 if (gtk_im_context_filter_keypress (entry->im_context, event))
2042 gtk_entry_obscure_mouse_cursor (entry);
2043 entry->need_im_reset = TRUE;
2048 if (event->keyval == GDK_Return ||
2049 event->keyval == GDK_KP_Enter ||
2050 event->keyval == GDK_ISO_Enter ||
2051 event->keyval == GDK_Escape)
2053 GtkEntryCompletion *completion = gtk_entry_get_completion (entry);
2055 if (completion && completion->priv->completion_timeout)
2057 g_source_remove (completion->priv->completion_timeout);
2058 completion->priv->completion_timeout = 0;
2061 _gtk_entry_reset_im_context (entry);
2064 if (GTK_WIDGET_CLASS (gtk_entry_parent_class)->key_press_event (widget, event))
2065 /* Activate key bindings
2069 if (!entry->editable && event->length)
2070 gtk_widget_error_bell (widget);
2076 gtk_entry_key_release (GtkWidget *widget,
2079 GtkEntry *entry = GTK_ENTRY (widget);
2081 if (entry->editable)
2083 if (gtk_im_context_filter_keypress (entry->im_context, event))
2085 entry->need_im_reset = TRUE;
2090 return GTK_WIDGET_CLASS (gtk_entry_parent_class)->key_release_event (widget, event);
2094 gtk_entry_focus_in (GtkWidget *widget,
2095 GdkEventFocus *event)
2097 GtkEntry *entry = GTK_ENTRY (widget);
2099 gtk_widget_queue_draw (widget);
2101 if (entry->editable)
2103 entry->need_im_reset = TRUE;
2104 gtk_im_context_focus_in (entry->im_context);
2107 g_signal_connect (gdk_keymap_get_for_display (gtk_widget_get_display (widget)),
2108 "direction_changed",
2109 G_CALLBACK (gtk_entry_keymap_direction_changed), entry);
2111 gtk_entry_reset_blink_time (entry);
2112 gtk_entry_check_cursor_blink (entry);
2118 gtk_entry_focus_out (GtkWidget *widget,
2119 GdkEventFocus *event)
2121 GtkEntry *entry = GTK_ENTRY (widget);
2122 GtkEntryCompletion *completion;
2124 gtk_widget_queue_draw (widget);
2126 if (entry->editable)
2128 entry->need_im_reset = TRUE;
2129 gtk_im_context_focus_out (entry->im_context);
2132 gtk_entry_check_cursor_blink (entry);
2134 g_signal_handlers_disconnect_by_func (gdk_keymap_get_for_display (gtk_widget_get_display (widget)),
2135 gtk_entry_keymap_direction_changed,
2138 completion = gtk_entry_get_completion (entry);
2140 _gtk_entry_completion_popdown (completion);
2146 gtk_entry_grab_focus (GtkWidget *widget)
2148 GtkEntry *entry = GTK_ENTRY (widget);
2149 gboolean select_on_focus;
2151 GTK_WIDGET_CLASS (gtk_entry_parent_class)->grab_focus (widget);
2153 if (entry->editable && !entry->in_click)
2155 g_object_get (gtk_widget_get_settings (widget),
2156 "gtk-entry-select-on-focus",
2160 if (select_on_focus)
2161 gtk_editable_select_region (GTK_EDITABLE (widget), 0, -1);
2166 gtk_entry_direction_changed (GtkWidget *widget,
2167 GtkTextDirection previous_dir)
2169 GtkEntry *entry = GTK_ENTRY (widget);
2171 gtk_entry_recompute (entry);
2173 GTK_WIDGET_CLASS (gtk_entry_parent_class)->direction_changed (widget, previous_dir);
2177 gtk_entry_state_changed (GtkWidget *widget,
2178 GtkStateType previous_state)
2180 GtkEntry *entry = GTK_ENTRY (widget);
2183 if (GTK_WIDGET_REALIZED (widget))
2185 gdk_window_set_background (widget->window, &widget->style->base[GTK_WIDGET_STATE (widget)]);
2186 gdk_window_set_background (entry->text_area, &widget->style->base[GTK_WIDGET_STATE (widget)]);
2188 if (GTK_WIDGET_IS_SENSITIVE (widget))
2189 cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget), GDK_XTERM);
2193 gdk_window_set_cursor (entry->text_area, cursor);
2196 gdk_cursor_unref (cursor);
2198 entry->mouse_cursor_obscured = FALSE;
2201 if (!GTK_WIDGET_IS_SENSITIVE (widget))
2203 /* Clear any selection */
2204 gtk_editable_select_region (GTK_EDITABLE (entry), entry->current_pos, entry->current_pos);
2207 gtk_widget_queue_draw (widget);
2211 gtk_entry_screen_changed (GtkWidget *widget,
2212 GdkScreen *old_screen)
2214 gtk_entry_recompute (GTK_ENTRY (widget));
2217 /* GtkEditable method implementations
2220 gtk_entry_insert_text (GtkEditable *editable,
2221 const gchar *new_text,
2222 gint new_text_length,
2225 GtkEntry *entry = GTK_ENTRY (editable);
2229 if (*position < 0 || *position > entry->text_length)
2230 *position = entry->text_length;
2232 g_object_ref (editable);
2234 if (new_text_length <= 63)
2237 text = g_new (gchar, new_text_length + 1);
2239 text[new_text_length] = '\0';
2240 strncpy (text, new_text, new_text_length);
2242 g_signal_emit_by_name (editable, "insert_text", text, new_text_length, position);
2244 if (!entry->visible)
2245 trash_area (text, new_text_length);
2247 if (new_text_length > 63)
2250 g_object_unref (editable);
2254 gtk_entry_delete_text (GtkEditable *editable,
2258 GtkEntry *entry = GTK_ENTRY (editable);
2260 if (end_pos < 0 || end_pos > entry->text_length)
2261 end_pos = entry->text_length;
2264 if (start_pos > end_pos)
2265 start_pos = end_pos;
2267 g_object_ref (editable);
2269 g_signal_emit_by_name (editable, "delete_text", start_pos, end_pos);
2271 g_object_unref (editable);
2275 gtk_entry_get_chars (GtkEditable *editable,
2279 GtkEntry *entry = GTK_ENTRY (editable);
2280 gint start_index, end_index;
2283 end_pos = entry->text_length;
2285 start_pos = MIN (entry->text_length, start_pos);
2286 end_pos = MIN (entry->text_length, end_pos);
2288 start_index = g_utf8_offset_to_pointer (entry->text, start_pos) - entry->text;
2289 end_index = g_utf8_offset_to_pointer (entry->text, end_pos) - entry->text;
2291 return g_strndup (entry->text + start_index, end_index - start_index);
2295 gtk_entry_real_set_position (GtkEditable *editable,
2298 GtkEntry *entry = GTK_ENTRY (editable);
2300 if (position < 0 || position > entry->text_length)
2301 position = entry->text_length;
2303 if (position != entry->current_pos ||
2304 position != entry->selection_bound)
2306 _gtk_entry_reset_im_context (entry);
2307 gtk_entry_set_positions (entry, position, position);
2312 gtk_entry_get_position (GtkEditable *editable)
2314 return GTK_ENTRY (editable)->current_pos;
2318 gtk_entry_set_selection_bounds (GtkEditable *editable,
2322 GtkEntry *entry = GTK_ENTRY (editable);
2325 start = entry->text_length;
2327 end = entry->text_length;
2329 _gtk_entry_reset_im_context (entry);
2331 gtk_entry_set_positions (entry,
2332 MIN (end, entry->text_length),
2333 MIN (start, entry->text_length));
2335 gtk_entry_update_primary_selection (entry);
2339 gtk_entry_get_selection_bounds (GtkEditable *editable,
2343 GtkEntry *entry = GTK_ENTRY (editable);
2345 *start = entry->selection_bound;
2346 *end = entry->current_pos;
2348 return (entry->selection_bound != entry->current_pos);
2352 gtk_entry_style_set (GtkWidget *widget,
2353 GtkStyle *previous_style)
2355 GtkEntry *entry = GTK_ENTRY (widget);
2356 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
2358 gtk_widget_style_get (widget,
2359 "focus-line-width", &priv->focus_width,
2360 "interior-focus", &priv->interior_focus,
2363 gtk_entry_recompute (entry);
2365 if (previous_style && GTK_WIDGET_REALIZED (widget))
2367 gdk_window_set_background (widget->window, &widget->style->base[GTK_WIDGET_STATE (widget)]);
2368 gdk_window_set_background (entry->text_area, &widget->style->base[GTK_WIDGET_STATE (widget)]);
2372 /* GtkCellEditable method implementations
2375 gtk_cell_editable_entry_activated (GtkEntry *entry, gpointer data)
2377 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (entry));
2378 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (entry));
2382 gtk_cell_editable_key_press_event (GtkEntry *entry,
2383 GdkEventKey *key_event,
2386 if (key_event->keyval == GDK_Escape)
2388 entry->editing_canceled = TRUE;
2389 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (entry));
2390 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (entry));
2395 /* override focus */
2396 if (key_event->keyval == GDK_Up || key_event->keyval == GDK_Down)
2398 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (entry));
2399 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (entry));
2408 gtk_entry_start_editing (GtkCellEditable *cell_editable,
2411 GTK_ENTRY (cell_editable)->is_cell_renderer = TRUE;
2413 g_signal_connect (cell_editable, "activate",
2414 G_CALLBACK (gtk_cell_editable_entry_activated), NULL);
2415 g_signal_connect (cell_editable, "key_press_event",
2416 G_CALLBACK (gtk_cell_editable_key_press_event), NULL);
2420 gtk_entry_password_hint_free (GtkEntryPasswordHint *password_hint)
2422 if (password_hint->password_hint_timeout_id)
2423 g_source_remove (password_hint->password_hint_timeout_id);
2425 g_free (password_hint);
2428 /* Default signal handlers
2431 gtk_entry_real_insert_text (GtkEditable *editable,
2432 const gchar *new_text,
2433 gint new_text_length,
2436 GtkEntry *entry = GTK_ENTRY (editable);
2440 if (new_text_length < 0)
2441 new_text_length = strlen (new_text);
2443 n_chars = g_utf8_strlen (new_text, new_text_length);
2444 if (entry->text_max_length > 0 && n_chars + entry->text_length > entry->text_max_length)
2446 gtk_widget_error_bell (GTK_WIDGET (entry));
2447 n_chars = entry->text_max_length - entry->text_length;
2448 new_text_length = g_utf8_offset_to_pointer (new_text, n_chars) - new_text;
2451 if (new_text_length + entry->n_bytes + 1 > entry->text_size)
2453 gsize prev_size = entry->text_size;
2455 while (new_text_length + entry->n_bytes + 1 > entry->text_size)
2457 if (entry->text_size == 0)
2458 entry->text_size = MIN_SIZE;
2461 if (2 * (guint)entry->text_size < MAX_SIZE &&
2462 2 * (guint)entry->text_size > entry->text_size)
2463 entry->text_size *= 2;
2466 entry->text_size = MAX_SIZE;
2467 if (new_text_length > (gint)entry->text_size - (gint)entry->n_bytes - 1)
2469 new_text_length = (gint)entry->text_size - (gint)entry->n_bytes - 1;
2470 new_text_length = g_utf8_find_prev_char (new_text, new_text + new_text_length + 1) - new_text;
2471 n_chars = g_utf8_strlen (new_text, new_text_length);
2479 entry->text = g_realloc (entry->text, entry->text_size);
2482 /* Same thing, just slower and without leaving stuff in memory. */
2483 gchar *et_new = g_malloc (entry->text_size);
2484 memcpy (et_new, entry->text, MIN (prev_size, entry->text_size));
2485 trash_area (entry->text, prev_size);
2486 g_free (entry->text);
2487 entry->text = et_new;
2491 index = g_utf8_offset_to_pointer (entry->text, *position) - entry->text;
2493 g_memmove (entry->text + index + new_text_length, entry->text + index, entry->n_bytes - index);
2494 memcpy (entry->text + index, new_text, new_text_length);
2496 entry->n_bytes += new_text_length;
2497 entry->text_length += n_chars;
2499 /* NUL terminate for safety and convenience */
2500 entry->text[entry->n_bytes] = '\0';
2502 if (entry->current_pos > *position)
2503 entry->current_pos += n_chars;
2505 if (entry->selection_bound > *position)
2506 entry->selection_bound += n_chars;
2508 if (n_chars == 1 && !entry->visible && (new_text_length < PASSWORD_HINT_MAX))
2510 guint password_hint_timeout;
2512 g_object_get (gtk_widget_get_settings (GTK_WIDGET (entry)),
2513 "gtk-entry-password-hint-timeout", &password_hint_timeout,
2516 if (password_hint_timeout > 0)
2518 GtkEntryPasswordHint *password_hint = g_object_get_qdata (G_OBJECT (entry),
2519 quark_password_hint);
2523 password_hint = g_new0 (GtkEntryPasswordHint, 1);
2524 g_object_set_qdata_full (G_OBJECT (entry), quark_password_hint,
2526 (GDestroyNotify) gtk_entry_password_hint_free);
2529 memset (&password_hint->password_hint, 0x0, PASSWORD_HINT_MAX);
2530 password_hint->password_hint_length = new_text_length;
2531 memcpy (&password_hint->password_hint, new_text, new_text_length);
2532 password_hint->password_hint_position = *position + n_chars;
2537 g_object_set_qdata (G_OBJECT (entry), quark_password_hint, NULL);
2540 *position += n_chars;
2542 gtk_entry_recompute (entry);
2544 emit_changed (entry);
2545 g_object_notify (G_OBJECT (editable), "text");
2549 gtk_entry_real_delete_text (GtkEditable *editable,
2553 GtkEntry *entry = GTK_ENTRY (editable);
2557 if (end_pos < 0 || end_pos > entry->text_length)
2558 end_pos = entry->text_length;
2560 if (start_pos < end_pos)
2562 gint start_index = g_utf8_offset_to_pointer (entry->text, start_pos) - entry->text;
2563 gint end_index = g_utf8_offset_to_pointer (entry->text, end_pos) - entry->text;
2565 gint selection_bound;
2567 g_memmove (entry->text + start_index, entry->text + end_index, entry->n_bytes + 1 - end_index);
2568 entry->text_length -= (end_pos - start_pos);
2569 entry->n_bytes -= (end_index - start_index);
2571 /* In password-mode, make sure we don't leave anything sensitive after
2572 * the terminating zero. Note, that the terminating zero already trashed
2575 if (!entry->visible)
2576 trash_area (entry->text + entry->n_bytes + 1, end_index - start_index - 1);
2578 current_pos = entry->current_pos;
2579 if (current_pos > start_pos)
2580 current_pos -= MIN (current_pos, end_pos) - start_pos;
2582 selection_bound = entry->selection_bound;
2583 if (selection_bound > start_pos)
2584 selection_bound -= MIN (selection_bound, end_pos) - start_pos;
2586 gtk_entry_set_positions (entry, current_pos, selection_bound);
2588 /* We might have deleted the selection
2590 gtk_entry_update_primary_selection (entry);
2592 gtk_entry_recompute (entry);
2594 emit_changed (entry);
2595 g_object_notify (G_OBJECT (editable), "text");
2599 /* Compute the X position for an offset that corresponds to the "more important
2600 * cursor position for that offset. We use this when trying to guess to which
2601 * end of the selection we should go to when the user hits the left or
2605 get_better_cursor_x (GtkEntry *entry,
2608 GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (entry)));
2609 PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
2610 gboolean split_cursor;
2612 PangoLayout *layout = gtk_entry_ensure_layout (entry, TRUE);
2613 const gchar *text = pango_layout_get_text (layout);
2614 gint index = g_utf8_offset_to_pointer (text, offset) - text;
2616 PangoRectangle strong_pos, weak_pos;
2618 g_object_get (gtk_widget_get_settings (GTK_WIDGET (entry)),
2619 "gtk-split-cursor", &split_cursor,
2622 pango_layout_get_cursor_pos (layout, index, &strong_pos, &weak_pos);
2625 return strong_pos.x / PANGO_SCALE;
2627 return (keymap_direction == entry->resolved_dir) ? strong_pos.x / PANGO_SCALE : weak_pos.x / PANGO_SCALE;
2631 gtk_entry_move_cursor (GtkEntry *entry,
2632 GtkMovementStep step,
2634 gboolean extend_selection)
2636 gint new_pos = entry->current_pos;
2638 _gtk_entry_reset_im_context (entry);
2640 if (entry->current_pos != entry->selection_bound && !extend_selection)
2642 /* If we have a current selection and aren't extending it, move to the
2643 * start/or end of the selection as appropriate
2647 case GTK_MOVEMENT_VISUAL_POSITIONS:
2649 gint current_x = get_better_cursor_x (entry, entry->current_pos);
2650 gint bound_x = get_better_cursor_x (entry, entry->selection_bound);
2653 new_pos = current_x < bound_x ? entry->current_pos : entry->selection_bound;
2655 new_pos = current_x > bound_x ? entry->current_pos : entry->selection_bound;
2658 case GTK_MOVEMENT_LOGICAL_POSITIONS:
2659 case GTK_MOVEMENT_WORDS:
2661 new_pos = MIN (entry->current_pos, entry->selection_bound);
2663 new_pos = MAX (entry->current_pos, entry->selection_bound);
2665 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
2666 case GTK_MOVEMENT_PARAGRAPH_ENDS:
2667 case GTK_MOVEMENT_BUFFER_ENDS:
2668 new_pos = count < 0 ? 0 : entry->text_length;
2670 case GTK_MOVEMENT_DISPLAY_LINES:
2671 case GTK_MOVEMENT_PARAGRAPHS:
2672 case GTK_MOVEMENT_PAGES:
2673 case GTK_MOVEMENT_HORIZONTAL_PAGES:
2681 case GTK_MOVEMENT_LOGICAL_POSITIONS:
2682 new_pos = gtk_entry_move_logically (entry, new_pos, count);
2684 case GTK_MOVEMENT_VISUAL_POSITIONS:
2685 new_pos = gtk_entry_move_visually (entry, new_pos, count);
2686 if (entry->current_pos == new_pos)
2688 if (!extend_selection)
2690 if (!gtk_widget_keynav_failed (GTK_WIDGET (entry),
2692 GTK_DIR_RIGHT : GTK_DIR_LEFT))
2694 GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (entry));
2697 gtk_widget_child_focus (toplevel,
2699 GTK_DIR_RIGHT : GTK_DIR_LEFT);
2704 gtk_widget_error_bell (GTK_WIDGET (entry));
2708 case GTK_MOVEMENT_WORDS:
2711 new_pos = gtk_entry_move_forward_word (entry, new_pos, FALSE);
2716 new_pos = gtk_entry_move_backward_word (entry, new_pos, FALSE);
2719 if (entry->current_pos == new_pos)
2720 gtk_widget_error_bell (GTK_WIDGET (entry));
2722 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
2723 case GTK_MOVEMENT_PARAGRAPH_ENDS:
2724 case GTK_MOVEMENT_BUFFER_ENDS:
2725 new_pos = count < 0 ? 0 : entry->text_length;
2726 if (entry->current_pos == new_pos)
2727 gtk_widget_error_bell (GTK_WIDGET (entry));
2729 case GTK_MOVEMENT_DISPLAY_LINES:
2730 case GTK_MOVEMENT_PARAGRAPHS:
2731 case GTK_MOVEMENT_PAGES:
2732 case GTK_MOVEMENT_HORIZONTAL_PAGES:
2737 if (extend_selection)
2738 gtk_editable_select_region (GTK_EDITABLE (entry), entry->selection_bound, new_pos);
2740 gtk_editable_set_position (GTK_EDITABLE (entry), new_pos);
2742 gtk_entry_pend_cursor_blink (entry);
2746 gtk_entry_insert_at_cursor (GtkEntry *entry,
2749 GtkEditable *editable = GTK_EDITABLE (entry);
2750 gint pos = entry->current_pos;
2752 if (entry->editable)
2754 _gtk_entry_reset_im_context (entry);
2756 gtk_editable_insert_text (editable, str, -1, &pos);
2757 gtk_editable_set_position (editable, pos);
2762 gtk_entry_delete_from_cursor (GtkEntry *entry,
2766 GtkEditable *editable = GTK_EDITABLE (entry);
2767 gint start_pos = entry->current_pos;
2768 gint end_pos = entry->current_pos;
2769 gint old_n_bytes = entry->n_bytes;
2771 _gtk_entry_reset_im_context (entry);
2773 if (!entry->editable)
2775 gtk_widget_error_bell (GTK_WIDGET (entry));
2779 if (entry->selection_bound != entry->current_pos)
2781 gtk_editable_delete_selection (editable);
2787 case GTK_DELETE_CHARS:
2788 end_pos = gtk_entry_move_logically (entry, entry->current_pos, count);
2789 gtk_editable_delete_text (editable, MIN (start_pos, end_pos), MAX (start_pos, end_pos));
2791 case GTK_DELETE_WORDS:
2794 /* Move to end of current word, or if not on a word, end of previous word */
2795 end_pos = gtk_entry_move_backward_word (entry, end_pos, FALSE);
2796 end_pos = gtk_entry_move_forward_word (entry, end_pos, FALSE);
2800 /* Move to beginning of current word, or if not on a word, begining of next word */
2801 start_pos = gtk_entry_move_forward_word (entry, start_pos, FALSE);
2802 start_pos = gtk_entry_move_backward_word (entry, start_pos, FALSE);
2806 case GTK_DELETE_WORD_ENDS:
2809 start_pos = gtk_entry_move_backward_word (entry, start_pos, FALSE);
2814 end_pos = gtk_entry_move_forward_word (entry, end_pos, FALSE);
2817 gtk_editable_delete_text (editable, start_pos, end_pos);
2819 case GTK_DELETE_DISPLAY_LINE_ENDS:
2820 case GTK_DELETE_PARAGRAPH_ENDS:
2822 gtk_editable_delete_text (editable, 0, entry->current_pos);
2824 gtk_editable_delete_text (editable, entry->current_pos, -1);
2826 case GTK_DELETE_DISPLAY_LINES:
2827 case GTK_DELETE_PARAGRAPHS:
2828 gtk_editable_delete_text (editable, 0, -1);
2830 case GTK_DELETE_WHITESPACE:
2831 gtk_entry_delete_whitespace (entry);
2835 if (entry->n_bytes == old_n_bytes)
2836 gtk_widget_error_bell (GTK_WIDGET (entry));
2838 gtk_entry_pend_cursor_blink (entry);
2842 gtk_entry_backspace (GtkEntry *entry)
2844 GtkEditable *editable = GTK_EDITABLE (entry);
2847 _gtk_entry_reset_im_context (entry);
2849 if (!entry->editable || !entry->text)
2851 gtk_widget_error_bell (GTK_WIDGET (entry));
2855 if (entry->selection_bound != entry->current_pos)
2857 gtk_editable_delete_selection (editable);
2861 prev_pos = gtk_entry_move_logically(entry, entry->current_pos, -1);
2863 if (prev_pos < entry->current_pos)
2865 PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
2866 PangoLogAttr *log_attrs;
2869 pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
2871 if (log_attrs[entry->current_pos].backspace_deletes_character)
2873 gchar *cluster_text;
2874 gchar *normalized_text;
2877 cluster_text = gtk_editable_get_chars (editable,
2879 entry->current_pos);
2880 normalized_text = g_utf8_normalize (cluster_text,
2881 strlen (cluster_text),
2883 len = g_utf8_strlen (normalized_text, -1);
2885 gtk_editable_delete_text (editable, prev_pos, entry->current_pos);
2888 gint pos = entry->current_pos;
2890 gtk_editable_insert_text (editable, normalized_text,
2891 g_utf8_offset_to_pointer (normalized_text, len - 1) - normalized_text,
2893 gtk_editable_set_position (editable, pos);
2896 g_free (normalized_text);
2897 g_free (cluster_text);
2901 gtk_editable_delete_text (editable, prev_pos, entry->current_pos);
2908 gtk_widget_error_bell (GTK_WIDGET (entry));
2911 gtk_entry_pend_cursor_blink (entry);
2915 gtk_entry_copy_clipboard (GtkEntry *entry)
2917 GtkEditable *editable = GTK_EDITABLE (entry);
2920 if (gtk_editable_get_selection_bounds (editable, &start, &end))
2922 gchar *str = gtk_entry_get_public_chars (entry, start, end);
2923 gtk_clipboard_set_text (gtk_widget_get_clipboard (GTK_WIDGET (entry),
2924 GDK_SELECTION_CLIPBOARD),
2931 gtk_entry_cut_clipboard (GtkEntry *entry)
2933 GtkEditable *editable = GTK_EDITABLE (entry);
2936 gtk_entry_copy_clipboard (entry);
2938 if (entry->editable)
2940 if (gtk_editable_get_selection_bounds (editable, &start, &end))
2941 gtk_editable_delete_text (editable, start, end);
2945 gtk_widget_error_bell (GTK_WIDGET (entry));
2950 gtk_entry_paste_clipboard (GtkEntry *entry)
2952 if (entry->editable)
2953 gtk_entry_paste (entry, GDK_NONE);
2955 gtk_widget_error_bell (GTK_WIDGET (entry));
2959 gtk_entry_delete_cb (GtkEntry *entry)
2961 GtkEditable *editable = GTK_EDITABLE (entry);
2964 if (entry->editable)
2966 if (gtk_editable_get_selection_bounds (editable, &start, &end))
2967 gtk_editable_delete_text (editable, start, end);
2972 gtk_entry_toggle_overwrite (GtkEntry *entry)
2974 entry->overwrite_mode = !entry->overwrite_mode;
2978 gtk_entry_select_all (GtkEntry *entry)
2980 gtk_entry_select_line (entry);
2984 gtk_entry_real_activate (GtkEntry *entry)
2987 GtkWidget *toplevel;
2990 widget = GTK_WIDGET (entry);
2992 if (entry->activates_default)
2994 toplevel = gtk_widget_get_toplevel (widget);
2995 if (GTK_IS_WINDOW (toplevel))
2997 window = GTK_WINDOW (toplevel);
3000 widget != window->default_widget &&
3001 !(widget == window->focus_widget &&
3002 (!window->default_widget || !GTK_WIDGET_SENSITIVE (window->default_widget))))
3003 gtk_window_activate_default (window);
3009 gtk_entry_keymap_direction_changed (GdkKeymap *keymap,
3012 gtk_entry_recompute (entry);
3015 /* IM Context Callbacks
3019 gtk_entry_commit_cb (GtkIMContext *context,
3023 if (entry->editable)
3024 gtk_entry_enter_text (entry, str);
3028 gtk_entry_preedit_changed_cb (GtkIMContext *context,
3031 if (entry->editable)
3033 gchar *preedit_string;
3036 gtk_im_context_get_preedit_string (entry->im_context,
3037 &preedit_string, NULL,
3039 entry->preedit_length = strlen (preedit_string);
3040 cursor_pos = CLAMP (cursor_pos, 0, g_utf8_strlen (preedit_string, -1));
3041 entry->preedit_cursor = cursor_pos;
3042 g_free (preedit_string);
3044 gtk_entry_recompute (entry);
3049 gtk_entry_retrieve_surrounding_cb (GtkIMContext *context,
3052 gtk_im_context_set_surrounding (context,
3055 g_utf8_offset_to_pointer (entry->text, entry->current_pos) - entry->text);
3061 gtk_entry_delete_surrounding_cb (GtkIMContext *slave,
3066 if (entry->editable)
3067 gtk_editable_delete_text (GTK_EDITABLE (entry),
3068 entry->current_pos + offset,
3069 entry->current_pos + offset + n_chars);
3074 /* Internal functions
3077 /* Used for im_commit_cb and inserting Unicode chars */
3079 gtk_entry_enter_text (GtkEntry *entry,
3082 GtkEditable *editable = GTK_EDITABLE (entry);
3084 gboolean old_need_im_reset;
3086 old_need_im_reset = entry->need_im_reset;
3087 entry->need_im_reset = FALSE;
3089 if (gtk_editable_get_selection_bounds (editable, NULL, NULL))
3090 gtk_editable_delete_selection (editable);
3093 if (entry->overwrite_mode)
3094 gtk_entry_delete_from_cursor (entry, GTK_DELETE_CHARS, 1);
3097 tmp_pos = entry->current_pos;
3098 gtk_editable_insert_text (editable, str, strlen (str), &tmp_pos);
3099 gtk_editable_set_position (editable, tmp_pos);
3101 entry->need_im_reset = old_need_im_reset;
3104 /* All changes to entry->current_pos and entry->selection_bound
3105 * should go through this function.
3108 gtk_entry_set_positions (GtkEntry *entry,
3110 gint selection_bound)
3112 gboolean changed = FALSE;
3114 g_object_freeze_notify (G_OBJECT (entry));
3116 if (current_pos != -1 &&
3117 entry->current_pos != current_pos)
3119 entry->current_pos = current_pos;
3122 g_object_notify (G_OBJECT (entry), "cursor-position");
3125 if (selection_bound != -1 &&
3126 entry->selection_bound != selection_bound)
3128 entry->selection_bound = selection_bound;
3131 g_object_notify (G_OBJECT (entry), "selection-bound");
3134 g_object_thaw_notify (G_OBJECT (entry));
3138 gtk_entry_move_adjustments (entry);
3139 gtk_entry_recompute (entry);
3144 gtk_entry_reset_layout (GtkEntry *entry)
3146 if (entry->cached_layout)
3148 g_object_unref (entry->cached_layout);
3149 entry->cached_layout = NULL;
3154 update_im_cursor_location (GtkEntry *entry)
3158 gint strong_xoffset;
3159 gint area_width, area_height;
3161 gtk_entry_get_cursor_locations (entry, CURSOR_STANDARD, &strong_x, NULL)
3163 get_text_area_size (entry, NULL, NULL, &area_width, &area_height);
3165 strong_xoffset = strong_x - entry->scroll_offset;
3166 if (strong_xoffset < 0)
3170 else if (strong_xoffset > area_width)
3172 strong_xoffset = area_width;
3174 area.x = strong_xoffset;
3177 area.height = area_height;
3179 gtk_im_context_set_cursor_location (entry->im_context, &area);
3183 recompute_idle_func (gpointer data)
3187 entry = GTK_ENTRY (data);
3189 entry->recompute_idle = 0;
3191 if (gtk_widget_has_screen (GTK_WIDGET (entry)))
3193 gtk_entry_adjust_scroll (entry);
3194 gtk_entry_queue_draw (entry);
3196 update_im_cursor_location (entry);
3203 gtk_entry_recompute (GtkEntry *entry)
3205 gtk_entry_reset_layout (entry);
3206 gtk_entry_check_cursor_blink (entry);
3208 if (!entry->recompute_idle)
3210 entry->recompute_idle = gdk_threads_add_idle_full (G_PRIORITY_HIGH_IDLE + 15, /* between resize and redraw */
3211 recompute_idle_func, entry, NULL);
3216 append_char (GString *str,
3224 char_len = g_unichar_to_utf8 (ch, buf);
3229 g_string_append_len (str, buf, char_len);
3235 gtk_entry_remove_password_hint (gpointer data)
3237 /* Force the string to be redrawn, but now without a visible character */
3238 gtk_entry_recompute (GTK_ENTRY (data));
3243 static PangoLayout *
3244 gtk_entry_create_layout (GtkEntry *entry,
3245 gboolean include_preedit)
3247 GtkWidget *widget = GTK_WIDGET (entry);
3248 PangoLayout *layout = gtk_widget_create_pango_layout (widget, NULL);
3249 PangoAttrList *tmp_attrs = pango_attr_list_new ();
3251 gchar *preedit_string = NULL;
3252 gint preedit_length = 0;
3253 PangoAttrList *preedit_attrs = NULL;
3255 pango_layout_set_single_paragraph_mode (layout, TRUE);
3257 if (include_preedit)
3259 gtk_im_context_get_preedit_string (entry->im_context,
3260 &preedit_string, &preedit_attrs, NULL);
3261 preedit_length = entry->preedit_length;
3266 GString *tmp_string = g_string_new (NULL);
3268 gint cursor_index = g_utf8_offset_to_pointer (entry->text, entry->current_pos) - entry->text;
3272 g_string_prepend_len (tmp_string, entry->text, entry->n_bytes);
3273 g_string_insert (tmp_string, cursor_index, preedit_string);
3278 gint preedit_len_chars;
3279 gunichar invisible_char;
3281 ch_len = g_utf8_strlen (entry->text, entry->n_bytes);
3282 preedit_len_chars = g_utf8_strlen (preedit_string, -1);
3283 ch_len += preedit_len_chars;
3285 if (entry->invisible_char != 0)
3286 invisible_char = entry->invisible_char;
3288 invisible_char = ' '; /* just pick a char */
3290 append_char (tmp_string, invisible_char, ch_len);
3292 /* Fix cursor index to point to invisible char corresponding
3293 * to the preedit, fix preedit_length to be the length of
3294 * the invisible chars representing the preedit
3297 g_utf8_offset_to_pointer (tmp_string->str, entry->current_pos) -
3301 g_unichar_to_utf8 (invisible_char, NULL);
3304 pango_layout_set_text (layout, tmp_string->str, tmp_string->len);
3306 pango_attr_list_splice (tmp_attrs, preedit_attrs,
3307 cursor_index, preedit_length);
3309 g_string_free (tmp_string, TRUE);
3313 PangoDirection pango_dir;
3316 pango_dir = pango_find_base_dir (entry->text, entry->n_bytes);
3319 pango_dir = PANGO_DIRECTION_NEUTRAL;
3321 if (pango_dir == PANGO_DIRECTION_NEUTRAL)
3323 if (GTK_WIDGET_HAS_FOCUS (widget))
3325 GdkDisplay *display = gtk_widget_get_display (widget);
3326 GdkKeymap *keymap = gdk_keymap_get_for_display (display);
3327 if (gdk_keymap_get_direction (keymap) == PANGO_DIRECTION_RTL)
3328 pango_dir = PANGO_DIRECTION_RTL;
3330 pango_dir = PANGO_DIRECTION_LTR;
3334 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
3335 pango_dir = PANGO_DIRECTION_RTL;
3337 pango_dir = PANGO_DIRECTION_LTR;
3341 pango_context_set_base_dir (gtk_widget_get_pango_context (widget),
3344 pango_layout_set_alignment (layout, pango_dir);
3346 entry->resolved_dir = pango_dir;
3350 pango_layout_set_text (layout, entry->text, entry->n_bytes);
3354 GString *str = g_string_new (NULL);
3355 gunichar invisible_char;
3356 guint password_hint_timeout;
3357 GtkEntryPasswordHint *password_hint;
3359 g_object_get (gtk_widget_get_settings (widget),
3360 "gtk-entry-password-hint-timeout", &password_hint_timeout,
3363 if (entry->invisible_char != 0)
3364 invisible_char = entry->invisible_char;
3366 invisible_char = ' '; /* just pick a char */
3368 password_hint = g_object_get_qdata (G_OBJECT (entry),
3369 quark_password_hint);
3371 if (password_hint && password_hint->password_hint_timeout_id)
3373 g_source_remove (password_hint->password_hint_timeout_id);
3374 password_hint->password_hint_timeout_id = 0;
3377 if (password_hint_timeout == 0 || password_hint == NULL ||
3378 (password_hint && password_hint->password_hint_length == 0))
3380 append_char (str, invisible_char, entry->text_length);
3382 else if (password_hint)
3384 /* Draw hidden characters upto the inserted position,
3385 * then the real thing, pad up to full length
3387 if (password_hint->password_hint_position > 1)
3388 append_char (str, invisible_char,
3389 password_hint->password_hint_position - 1);
3391 g_string_append_len (str, password_hint->password_hint,
3392 password_hint->password_hint_length);
3394 if (password_hint->password_hint_position < entry->text_length)
3395 append_char (str, invisible_char,
3396 entry->text_length -
3397 password_hint->password_hint_position);
3399 /* Now remove this last inputted character, don't need
3402 memset (password_hint->password_hint, 0, PASSWORD_HINT_MAX);
3403 password_hint->password_hint_length = 0;
3405 password_hint->password_hint_timeout_id =
3406 gdk_threads_add_timeout (password_hint_timeout,
3407 (GSourceFunc) gtk_entry_remove_password_hint,
3411 pango_layout_set_text (layout, str->str, str->len);
3412 g_string_free (str, TRUE);
3416 pango_layout_set_attributes (layout, tmp_attrs);
3418 g_free (preedit_string);
3420 pango_attr_list_unref (preedit_attrs);
3422 pango_attr_list_unref (tmp_attrs);
3427 static PangoLayout *
3428 gtk_entry_ensure_layout (GtkEntry *entry,
3429 gboolean include_preedit)
3431 if (entry->preedit_length > 0 &&
3432 !include_preedit != !entry->cache_includes_preedit)
3433 gtk_entry_reset_layout (entry);
3435 if (!entry->cached_layout)
3437 entry->cached_layout = gtk_entry_create_layout (entry, include_preedit);
3438 entry->cache_includes_preedit = include_preedit;
3441 return entry->cached_layout;
3445 get_layout_position (GtkEntry *entry,
3449 PangoLayout *layout;
3450 PangoRectangle logical_rect;
3451 gint area_width, area_height;
3452 GtkBorder inner_border;
3454 PangoLayoutLine *line;
3456 layout = gtk_entry_ensure_layout (entry, TRUE);
3458 get_text_area_size (entry, NULL, NULL, &area_width, &area_height);
3459 _gtk_entry_effective_inner_border (entry, &inner_border);
3461 area_height = PANGO_SCALE * (area_height - inner_border.top - inner_border.bottom);
3463 line = pango_layout_get_lines_readonly (layout)->data;
3464 pango_layout_line_get_extents (line, NULL, &logical_rect);
3466 /* Align primarily for locale's ascent/descent */
3467 y_pos = ((area_height - entry->ascent - entry->descent) / 2 +
3468 entry->ascent + logical_rect.y);
3470 /* Now see if we need to adjust to fit in actual drawn string */
3471 if (logical_rect.height > area_height)
3472 y_pos = (area_height - logical_rect.height) / 2;
3475 else if (y_pos + logical_rect.height > area_height)
3476 y_pos = area_height - logical_rect.height;
3478 y_pos = inner_border.top + y_pos / PANGO_SCALE;
3481 *x = inner_border.left - entry->scroll_offset;
3488 gtk_entry_draw_text (GtkEntry *entry)
3492 if (!entry->visible && entry->invisible_char == 0)
3495 if (GTK_WIDGET_DRAWABLE (entry))
3497 PangoLayout *layout = gtk_entry_ensure_layout (entry, TRUE);
3500 gint start_pos, end_pos;
3502 widget = GTK_WIDGET (entry);
3504 get_layout_position (entry, &x, &y);
3506 cr = gdk_cairo_create (entry->text_area);
3508 cairo_move_to (cr, x, y);
3509 gdk_cairo_set_source_color (cr, &widget->style->text [widget->state]);
3510 pango_cairo_show_layout (cr, layout);
3512 if (gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &start_pos, &end_pos))
3516 PangoRectangle logical_rect;
3517 GdkColor *selection_color, *text_color;
3518 GtkBorder inner_border;
3520 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
3521 gtk_entry_get_pixel_ranges (entry, &ranges, &n_ranges);
3523 if (GTK_WIDGET_HAS_FOCUS (entry))
3525 selection_color = &widget->style->base [GTK_STATE_SELECTED];
3526 text_color = &widget->style->text [GTK_STATE_SELECTED];
3530 selection_color = &widget->style->base [GTK_STATE_ACTIVE];
3531 text_color = &widget->style->text [GTK_STATE_ACTIVE];
3534 _gtk_entry_effective_inner_border (entry, &inner_border);
3536 for (i = 0; i < n_ranges; ++i)
3537 cairo_rectangle (cr,
3538 inner_border.left - entry->scroll_offset + ranges[2 * i],
3541 logical_rect.height);
3545 gdk_cairo_set_source_color (cr, selection_color);
3548 cairo_move_to (cr, x, y);
3549 gdk_cairo_set_source_color (cr, text_color);
3550 pango_cairo_show_layout (cr, layout);
3560 draw_insertion_cursor (GtkEntry *entry,
3561 GdkRectangle *cursor_location,
3562 gboolean is_primary,
3563 PangoDirection direction,
3564 gboolean draw_arrow)
3566 GtkWidget *widget = GTK_WIDGET (entry);
3567 GtkTextDirection text_dir;
3569 if (direction == PANGO_DIRECTION_LTR)
3570 text_dir = GTK_TEXT_DIR_LTR;
3572 text_dir = GTK_TEXT_DIR_RTL;
3574 gtk_draw_insertion_cursor (widget, entry->text_area, NULL,
3576 is_primary, text_dir, draw_arrow);
3580 gtk_entry_draw_cursor (GtkEntry *entry,
3583 GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (entry)));
3584 PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
3586 if (GTK_WIDGET_DRAWABLE (entry))
3588 GtkWidget *widget = GTK_WIDGET (entry);
3589 GdkRectangle cursor_location;
3590 gboolean split_cursor;
3592 GtkBorder inner_border;
3594 gint strong_x, weak_x;
3595 gint text_area_height;
3596 PangoDirection dir1 = PANGO_DIRECTION_NEUTRAL;
3597 PangoDirection dir2 = PANGO_DIRECTION_NEUTRAL;
3601 _gtk_entry_effective_inner_border (entry, &inner_border);
3603 xoffset = inner_border.left - entry->scroll_offset;
3605 gdk_drawable_get_size (entry->text_area, NULL, &text_area_height);
3607 gtk_entry_get_cursor_locations (entry, type, &strong_x, &weak_x);
3609 g_object_get (gtk_widget_get_settings (widget),
3610 "gtk-split-cursor", &split_cursor,
3613 dir1 = entry->resolved_dir;
3619 if (weak_x != strong_x)
3621 dir2 = (entry->resolved_dir == PANGO_DIRECTION_LTR) ? PANGO_DIRECTION_RTL : PANGO_DIRECTION_LTR;
3627 if (keymap_direction == entry->resolved_dir)
3633 cursor_location.x = xoffset + x1;
3634 cursor_location.y = inner_border.top;
3635 cursor_location.width = 0;
3636 cursor_location.height = text_area_height - inner_border.top - inner_border.bottom;
3638 draw_insertion_cursor (entry,
3639 &cursor_location, TRUE, dir1,
3640 dir2 != PANGO_DIRECTION_NEUTRAL);
3642 if (dir2 != PANGO_DIRECTION_NEUTRAL)
3644 cursor_location.x = xoffset + x2;
3645 draw_insertion_cursor (entry,
3646 &cursor_location, FALSE, dir2,
3653 gtk_entry_queue_draw (GtkEntry *entry)
3655 if (GTK_WIDGET_DRAWABLE (entry))
3656 gdk_window_invalidate_rect (entry->text_area, NULL, FALSE);
3660 _gtk_entry_reset_im_context (GtkEntry *entry)
3662 if (entry->need_im_reset)
3664 entry->need_im_reset = 0;
3665 gtk_im_context_reset (entry->im_context);
3670 gtk_entry_find_position (GtkEntry *entry,
3673 PangoLayout *layout;
3674 PangoLayoutLine *line;
3681 layout = gtk_entry_ensure_layout (entry, TRUE);
3682 text = pango_layout_get_text (layout);
3683 cursor_index = g_utf8_offset_to_pointer (text, entry->current_pos) - text;
3685 line = pango_layout_get_lines_readonly (layout)->data;
3686 pango_layout_line_x_to_index (line, x * PANGO_SCALE, &index, &trailing);
3688 if (index >= cursor_index && entry->preedit_length)
3690 if (index >= cursor_index + entry->preedit_length)
3691 index -= entry->preedit_length;
3694 index = cursor_index;
3699 pos = g_utf8_pointer_to_offset (text, text + index);
3706 gtk_entry_get_cursor_locations (GtkEntry *entry,
3711 if (!entry->visible && !entry->invisible_char)
3721 PangoLayout *layout = gtk_entry_ensure_layout (entry, TRUE);
3722 const gchar *text = pango_layout_get_text (layout);
3723 PangoRectangle strong_pos, weak_pos;
3726 if (type == CURSOR_STANDARD)
3728 index = g_utf8_offset_to_pointer (text, entry->current_pos + entry->preedit_cursor) - text;
3730 else /* type == CURSOR_DND */
3732 index = g_utf8_offset_to_pointer (text, entry->dnd_position) - text;
3734 if (entry->dnd_position > entry->current_pos)
3737 index += entry->preedit_length;
3740 gint preedit_len_chars = g_utf8_strlen (text, -1) - entry->text_length;
3741 index += preedit_len_chars * g_unichar_to_utf8 (entry->invisible_char, NULL);
3746 pango_layout_get_cursor_pos (layout, index, &strong_pos, &weak_pos);
3749 *strong_x = strong_pos.x / PANGO_SCALE;
3752 *weak_x = weak_pos.x / PANGO_SCALE;
3757 gtk_entry_adjust_scroll (GtkEntry *entry)
3759 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
3760 gint min_offset, max_offset;
3761 gint text_area_width, text_width;
3762 GtkBorder inner_border;
3763 gint strong_x, weak_x;
3764 gint strong_xoffset, weak_xoffset;
3766 PangoLayout *layout;
3767 PangoLayoutLine *line;
3768 PangoRectangle logical_rect;
3770 if (!GTK_WIDGET_REALIZED (entry))
3773 _gtk_entry_effective_inner_border (entry, &inner_border);
3775 gdk_drawable_get_size (entry->text_area, &text_area_width, NULL);
3776 text_area_width -= inner_border.left + inner_border.right;
3777 if (text_area_width < 0)
3778 text_area_width = 0;
3780 layout = gtk_entry_ensure_layout (entry, TRUE);
3781 line = pango_layout_get_lines_readonly (layout)->data;
3783 pango_layout_line_get_extents (line, NULL, &logical_rect);
3785 /* Display as much text as we can */
3787 if (entry->resolved_dir == PANGO_DIRECTION_LTR)
3788 xalign = priv->xalign;
3790 xalign = 1.0 - priv->xalign;
3792 text_width = PANGO_PIXELS(logical_rect.width);
3794 if (text_width > text_area_width)
3797 max_offset = text_width - text_area_width;
3801 min_offset = (text_width - text_area_width) * xalign;
3802 max_offset = min_offset;
3805 entry->scroll_offset = CLAMP (entry->scroll_offset, min_offset, max_offset);
3807 /* And make sure cursors are on screen. Note that the cursor is
3808 * actually drawn one pixel into the INNER_BORDER space on
3809 * the right, when the scroll is at the utmost right. This
3810 * looks better to to me than confining the cursor inside the
3811 * border entirely, though it means that the cursor gets one
3812 * pixel closer to the edge of the widget on the right than
3813 * on the left. This might need changing if one changed
3814 * INNER_BORDER from 2 to 1, as one would do on a
3815 * small-screen-real-estate display.
3817 * We always make sure that the strong cursor is on screen, and
3818 * put the weak cursor on screen if possible.
3821 gtk_entry_get_cursor_locations (entry, CURSOR_STANDARD, &strong_x, &weak_x);
3823 strong_xoffset = strong_x - entry->scroll_offset;
3825 if (strong_xoffset < 0)
3827 entry->scroll_offset += strong_xoffset;
3830 else if (strong_xoffset > text_area_width)
3832 entry->scroll_offset += strong_xoffset - text_area_width;
3833 strong_xoffset = text_area_width;
3836 weak_xoffset = weak_x - entry->scroll_offset;
3838 if (weak_xoffset < 0 && strong_xoffset - weak_xoffset <= text_area_width)
3840 entry->scroll_offset += weak_xoffset;
3842 else if (weak_xoffset > text_area_width &&
3843 strong_xoffset - (weak_xoffset - text_area_width) >= 0)
3845 entry->scroll_offset += weak_xoffset - text_area_width;
3848 g_object_notify (G_OBJECT (entry), "scroll-offset");
3852 gtk_entry_move_adjustments (GtkEntry *entry)
3854 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
3855 PangoContext *context;
3856 PangoFontMetrics *metrics;
3857 gint x, layout_x, border_x, border_y;
3860 if (!priv->cursor_hadjustment)
3863 /* Cursor position, layout offset, border width, and widget allocation */
3864 gtk_entry_get_cursor_locations (entry, CURSOR_STANDARD, &x, NULL);
3865 get_layout_position (entry, &layout_x, NULL);
3866 _gtk_entry_get_borders (entry, &border_x, &border_y);
3867 x += entry->widget.allocation.x + layout_x + border_x;
3869 /* Approximate width of a char, so user can see what is ahead/behind */
3870 context = gtk_widget_get_pango_context (GTK_WIDGET (entry));
3871 metrics = pango_context_get_metrics (context,
3872 entry->widget.style->font_desc,
3873 pango_context_get_language (context));
3874 char_width = pango_font_metrics_get_approximate_char_width (metrics) / PANGO_SCALE;
3877 gtk_adjustment_clamp_page (priv->cursor_hadjustment,
3878 x - (char_width + 1), /* one char + one pixel before */
3879 x + (char_width + 2)); /* one char + cursor + one pixel after */
3883 gtk_entry_move_visually (GtkEntry *entry,
3888 PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
3891 text = pango_layout_get_text (layout);
3893 index = g_utf8_offset_to_pointer (text, start) - text;
3897 int new_index, new_trailing;
3898 gboolean split_cursor;
3901 g_object_get (gtk_widget_get_settings (GTK_WIDGET (entry)),
3902 "gtk-split-cursor", &split_cursor,
3909 GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (entry)));
3910 PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
3912 strong = keymap_direction == entry->resolved_dir;
3917 pango_layout_move_cursor_visually (layout, strong, index, 0, 1, &new_index, &new_trailing);
3922 pango_layout_move_cursor_visually (layout, strong, index, 0, -1, &new_index, &new_trailing);
3928 else if (new_index != G_MAXINT)
3931 while (new_trailing--)
3932 index = g_utf8_next_char (text + index) - text;
3935 return g_utf8_pointer_to_offset (text, text + index);
3939 gtk_entry_move_logically (GtkEntry *entry,
3943 gint new_pos = start;
3945 /* Prevent any leak of information */
3946 if (!entry->visible)
3948 new_pos = CLAMP (start + count, 0, entry->text_length);
3950 else if (entry->text)
3952 PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
3953 PangoLogAttr *log_attrs;
3956 pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
3958 while (count > 0 && new_pos < entry->text_length)
3962 while (new_pos < entry->text_length && !log_attrs[new_pos].is_cursor_position);
3966 while (count < 0 && new_pos > 0)
3970 while (new_pos > 0 && !log_attrs[new_pos].is_cursor_position);
3982 gtk_entry_move_forward_word (GtkEntry *entry,
3984 gboolean allow_whitespace)
3986 gint new_pos = start;
3988 /* Prevent any leak of information */
3989 if (!entry->visible)
3991 new_pos = entry->text_length;
3993 else if (entry->text && (new_pos < entry->text_length))
3995 PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
3996 PangoLogAttr *log_attrs;
3999 pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
4001 /* Find the next word boundary */
4003 while (new_pos < n_attrs - 1 && !(log_attrs[new_pos].is_word_end ||
4004 (log_attrs[new_pos].is_word_start && allow_whitespace)))
4015 gtk_entry_move_backward_word (GtkEntry *entry,
4017 gboolean allow_whitespace)
4019 gint new_pos = start;
4021 /* Prevent any leak of information */
4022 if (!entry->visible)
4026 else if (entry->text && start > 0)
4028 PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
4029 PangoLogAttr *log_attrs;
4032 pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
4034 new_pos = start - 1;
4036 /* Find the previous word boundary */
4037 while (new_pos > 0 && !(log_attrs[new_pos].is_word_start ||
4038 (log_attrs[new_pos].is_word_end && allow_whitespace)))
4048 gtk_entry_delete_whitespace (GtkEntry *entry)
4050 PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
4051 PangoLogAttr *log_attrs;
4055 pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
4057 start = end = entry->current_pos;
4059 while (start > 0 && log_attrs[start-1].is_white)
4062 while (end < n_attrs && log_attrs[end].is_white)
4068 gtk_editable_delete_text (GTK_EDITABLE (entry), start, end);
4073 gtk_entry_select_word (GtkEntry *entry)
4075 gint start_pos = gtk_entry_move_backward_word (entry, entry->current_pos, TRUE);
4076 gint end_pos = gtk_entry_move_forward_word (entry, entry->current_pos, TRUE);
4078 gtk_editable_select_region (GTK_EDITABLE (entry), start_pos, end_pos);
4082 gtk_entry_select_line (GtkEntry *entry)
4084 gtk_editable_select_region (GTK_EDITABLE (entry), 0, -1);
4088 * Like gtk_editable_get_chars, but handle not-visible entries
4092 gtk_entry_get_public_chars (GtkEntry *entry,
4097 end = entry->text_length;
4100 return gtk_editable_get_chars (GTK_EDITABLE (entry), start, end);
4101 else if (!entry->invisible_char)
4102 return g_strdup ("");
4105 GString *str = g_string_new (NULL);
4106 append_char (str, entry->invisible_char, end - start);
4107 return g_string_free (str, FALSE);
4112 truncate_multiline (const gchar *text)
4117 text[length] && text[length] != '\n' && text[length] != '\r';
4124 paste_received (GtkClipboard *clipboard,
4128 GtkEntry *entry = GTK_ENTRY (data);
4129 GtkEditable *editable = GTK_EDITABLE (entry);
4130 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
4132 if (entry->button == 2)
4134 gint pos, start, end;
4135 pos = priv->insert_pos;
4136 gtk_editable_get_selection_bounds (editable, &start, &end);
4137 if (!((start <= pos && pos <= end) || (end <= pos && pos <= start)))
4138 gtk_editable_select_region (editable, pos, pos);
4143 gint pos, start, end;
4145 gboolean popup_completion;
4146 GtkEntryCompletion *completion;
4148 completion = gtk_entry_get_completion (entry);
4150 if (entry->truncate_multiline)
4151 length = truncate_multiline (text);
4153 /* only complete if the selection is at the end */
4154 popup_completion = (entry->text_length == MAX (entry->current_pos, entry->selection_bound));
4158 if (GTK_WIDGET_MAPPED (completion->priv->popup_window))
4159 _gtk_entry_completion_popdown (completion);
4161 if (!popup_completion && completion->priv->changed_id > 0)
4162 g_signal_handler_block (entry, completion->priv->changed_id);
4165 begin_change (entry);
4166 g_object_freeze_notify (G_OBJECT (entry));
4167 if (gtk_editable_get_selection_bounds (editable, &start, &end))
4168 gtk_editable_delete_text (editable, start, end);
4170 pos = entry->current_pos;
4171 gtk_editable_insert_text (editable, text, length, &pos);
4172 gtk_editable_set_position (editable, pos);
4173 g_object_thaw_notify (G_OBJECT (entry));
4177 !popup_completion && completion->priv->changed_id > 0)
4178 g_signal_handler_unblock (entry, completion->priv->changed_id);
4181 g_object_unref (entry);
4185 gtk_entry_paste (GtkEntry *entry,
4188 g_object_ref (entry);
4189 gtk_clipboard_request_text (gtk_widget_get_clipboard (GTK_WIDGET (entry), selection),
4190 paste_received, entry);
4194 primary_get_cb (GtkClipboard *clipboard,
4195 GtkSelectionData *selection_data,
4199 GtkEntry *entry = GTK_ENTRY (data);
4202 if (gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &start, &end))
4204 gchar *str = gtk_entry_get_public_chars (entry, start, end);
4205 gtk_selection_data_set_text (selection_data, str, -1);
4211 primary_clear_cb (GtkClipboard *clipboard,
4214 GtkEntry *entry = GTK_ENTRY (data);
4216 gtk_editable_select_region (GTK_EDITABLE (entry), entry->current_pos, entry->current_pos);
4220 gtk_entry_update_primary_selection (GtkEntry *entry)
4222 static GtkTargetEntry targets[] = {
4223 { "UTF8_STRING", 0, 0 },
4226 { "COMPOUND_TEXT", 0, 0 },
4227 { "text/plain;charset=utf-8", 0, 0 },
4229 { "text/plain", 0, 0 }
4232 GtkClipboard *clipboard;
4235 if (targets[5].target == NULL)
4237 const gchar *charset;
4239 g_get_charset (&charset);
4240 targets[5].target = g_strdup_printf ("text/plain;charset=%s", charset);
4243 if (!GTK_WIDGET_REALIZED (entry))
4246 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (entry), GDK_SELECTION_PRIMARY);
4248 if (gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &start, &end))
4250 if (!gtk_clipboard_set_with_owner (clipboard, targets, G_N_ELEMENTS (targets),
4251 primary_get_cb, primary_clear_cb, G_OBJECT (entry)))
4252 primary_clear_cb (clipboard, entry);
4256 if (gtk_clipboard_get_owner (clipboard) == G_OBJECT (entry))
4257 gtk_clipboard_clear (clipboard);
4267 * Creates a new entry.
4269 * Return value: a new #GtkEntry.
4272 gtk_entry_new (void)
4274 return g_object_new (GTK_TYPE_ENTRY, NULL);
4278 * gtk_entry_new_with_max_length:
4279 * @max: the maximum length of the entry, or 0 for no maximum.
4280 * (other than the maximum length of entries.) The value passed in will
4281 * be clamped to the range 0-65536.
4283 * Creates a new #GtkEntry widget with the given maximum length.
4285 * Note: the existence of this function is inconsistent
4286 * with the rest of the GTK+ API. The normal setup would
4287 * be to just require the user to make an extra call
4288 * to gtk_entry_set_max_length() instead. It is not
4289 * expected that this function will be removed, but
4290 * it would be better practice not to use it.
4292 * Return value: a new #GtkEntry.
4295 gtk_entry_new_with_max_length (gint max)
4299 max = CLAMP (max, 0, MAX_SIZE);
4301 entry = g_object_new (GTK_TYPE_ENTRY, NULL);
4302 entry->text_max_length = max;
4304 return GTK_WIDGET (entry);
4308 * gtk_entry_set_text:
4309 * @entry: a #GtkEntry
4310 * @text: the new text
4312 * Sets the text in the widget to the given
4313 * value, replacing the current contents.
4316 gtk_entry_set_text (GtkEntry *entry,
4320 GtkEntryCompletion *completion;
4322 g_return_if_fail (GTK_IS_ENTRY (entry));
4323 g_return_if_fail (text != NULL);
4325 /* Actually setting the text will affect the cursor and selection;
4326 * if the contents don't actually change, this will look odd to the user.
4328 if (strcmp (entry->text, text) == 0)
4331 completion = gtk_entry_get_completion (entry);
4332 if (completion && completion->priv->changed_id > 0)
4333 g_signal_handler_block (entry, completion->priv->changed_id);
4335 begin_change (entry);
4336 g_object_freeze_notify (G_OBJECT (entry));
4337 gtk_editable_delete_text (GTK_EDITABLE (entry), 0, -1);
4340 gtk_editable_insert_text (GTK_EDITABLE (entry), text, strlen (text), &tmp_pos);
4341 g_object_thaw_notify (G_OBJECT (entry));
4344 if (completion && completion->priv->changed_id > 0)
4345 g_signal_handler_unblock (entry, completion->priv->changed_id);
4349 * gtk_entry_append_text:
4350 * @entry: a #GtkEntry
4351 * @text: the text to append
4353 * Appends the given text to the contents of the widget.
4355 * Deprecated: gtk_entry_append_text() is deprecated and should not
4356 * be used in newly-written code. Use gtk_editable_insert_text()
4360 gtk_entry_append_text (GtkEntry *entry,
4365 g_return_if_fail (GTK_IS_ENTRY (entry));
4366 g_return_if_fail (text != NULL);
4368 tmp_pos = entry->text_length;
4369 gtk_editable_insert_text (GTK_EDITABLE (entry), text, -1, &tmp_pos);
4373 * gtk_entry_prepend_text:
4374 * @entry: a #GtkEntry
4375 * @text: the text to prepend
4377 * Prepends the given text to the contents of the widget.
4379 * Deprecated: gtk_entry_prepend_text() is deprecated and should not
4380 * be used in newly-written code. Use gtk_editable_insert_text()
4384 gtk_entry_prepend_text (GtkEntry *entry,
4389 g_return_if_fail (GTK_IS_ENTRY (entry));
4390 g_return_if_fail (text != NULL);
4393 gtk_editable_insert_text (GTK_EDITABLE (entry), text, -1, &tmp_pos);
4397 * gtk_entry_set_position:
4398 * @entry: a #GtkEntry
4399 * @position: the position of the cursor. The cursor is displayed
4400 * before the character with the given (base 0) index in the widget.
4401 * The value must be less than or equal to the number of characters
4402 * in the widget. A value of -1 indicates that the position should
4403 * be set after the last character in the entry. Note that this
4404 * position is in characters, not in bytes.
4406 * Sets the cursor position in an entry to the given value.
4408 * Deprecated: Use gtk_editable_set_position() instead.
4411 gtk_entry_set_position (GtkEntry *entry,
4414 g_return_if_fail (GTK_IS_ENTRY (entry));
4416 gtk_editable_set_position (GTK_EDITABLE (entry), position);
4420 * gtk_entry_set_visibility:
4421 * @entry: a #GtkEntry
4422 * @visible: %TRUE if the contents of the entry are displayed
4425 * Sets whether the contents of the entry are visible or not.
4426 * When visibility is set to %FALSE, characters are displayed
4427 * as the invisible char, and will also appear that way when
4428 * the text in the entry widget is copied elsewhere.
4430 * The default invisible char is the asterisk '*', but it can
4431 * be changed with gtk_entry_set_invisible_char().
4434 gtk_entry_set_visibility (GtkEntry *entry,
4437 g_return_if_fail (GTK_IS_ENTRY (entry));
4439 visible = visible != FALSE;
4441 if (entry->visible != visible)
4443 if (GTK_WIDGET_HAS_FOCUS (entry) && !visible)
4444 gtk_im_context_focus_out (entry->im_context);
4446 g_object_unref (entry->im_context);
4449 entry->im_context = gtk_im_multicontext_new ();
4451 entry->im_context = gtk_im_context_simple_new ();
4453 g_signal_connect (entry->im_context, "commit",
4454 G_CALLBACK (gtk_entry_commit_cb), entry);
4455 g_signal_connect (entry->im_context, "preedit_changed",
4456 G_CALLBACK (gtk_entry_preedit_changed_cb), entry);
4457 g_signal_connect (entry->im_context, "retrieve_surrounding",
4458 G_CALLBACK (gtk_entry_retrieve_surrounding_cb), entry);
4459 g_signal_connect (entry->im_context, "delete_surrounding",
4460 G_CALLBACK (gtk_entry_delete_surrounding_cb), entry);
4462 if (GTK_WIDGET_HAS_FOCUS (entry) && visible)
4463 gtk_im_context_focus_in (entry->im_context);
4465 entry->visible = visible;
4467 g_object_notify (G_OBJECT (entry), "visibility");
4468 gtk_entry_recompute (entry);
4473 * gtk_entry_get_visibility:
4474 * @entry: a #GtkEntry
4476 * Retrieves whether the text in @entry is visible. See
4477 * gtk_entry_set_visibility().
4479 * Return value: %TRUE if the text is currently visible
4482 gtk_entry_get_visibility (GtkEntry *entry)
4484 g_return_val_if_fail (GTK_IS_ENTRY (entry), FALSE);
4486 return entry->visible;
4490 * gtk_entry_set_invisible_char:
4491 * @entry: a #GtkEntry
4492 * @ch: a Unicode character
4494 * Sets the character to use in place of the actual text when
4495 * gtk_entry_set_visibility() has been called to set text visibility
4496 * to %FALSE. i.e. this is the character used in "password mode" to
4497 * show the user how many characters have been typed. The default
4498 * invisible char is an asterisk ('*'). If you set the invisible char
4499 * to 0, then the user will get no feedback at all; there will be
4500 * no text on the screen as they type.
4503 gtk_entry_set_invisible_char (GtkEntry *entry,
4506 g_return_if_fail (GTK_IS_ENTRY (entry));
4508 if (ch == entry->invisible_char)
4511 entry->invisible_char = ch;
4512 g_object_notify (G_OBJECT (entry), "invisible-char");
4513 gtk_entry_recompute (entry);
4517 * gtk_entry_get_invisible_char:
4518 * @entry: a #GtkEntry
4520 * Retrieves the character displayed in place of the real characters
4521 * for entries with visibility set to false. See gtk_entry_set_invisible_char().
4523 * Return value: the current invisible char, or 0, if the entry does not
4524 * show invisible text at all.
4527 gtk_entry_get_invisible_char (GtkEntry *entry)
4529 g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
4531 return entry->invisible_char;
4535 * gtk_entry_set_editable:
4536 * @entry: a #GtkEntry
4537 * @editable: %TRUE if the user is allowed to edit the text
4540 * Determines if the user can edit the text in the editable
4543 * Deprecated: Use gtk_editable_set_editable() instead.
4546 gtk_entry_set_editable (GtkEntry *entry,
4549 g_return_if_fail (GTK_IS_ENTRY (entry));
4551 gtk_editable_set_editable (GTK_EDITABLE (entry), editable);
4555 * gtk_entry_get_text:
4556 * @entry: a #GtkEntry
4558 * Retrieves the contents of the entry widget.
4559 * See also gtk_editable_get_chars().
4561 * Return value: a pointer to the contents of the widget as a
4562 * string. This string points to internally allocated
4563 * storage in the widget and must not be freed, modified or
4566 G_CONST_RETURN gchar*
4567 gtk_entry_get_text (GtkEntry *entry)
4569 g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
4575 * gtk_entry_select_region:
4576 * @entry: a #GtkEntry
4577 * @start: the starting position
4578 * @end: the end position
4580 * Selects a region of text. The characters that are selected are
4581 * those characters at positions from @start_pos up to, but not
4582 * including @end_pos. If @end_pos is negative, then the the characters
4583 * selected will be those characters from @start_pos to the end of
4586 * Deprecated: Use gtk_editable_select_region() instead.
4589 gtk_entry_select_region (GtkEntry *entry,
4593 gtk_editable_select_region (GTK_EDITABLE (entry), start, end);
4597 * gtk_entry_set_max_length:
4598 * @entry: a #GtkEntry
4599 * @max: the maximum length of the entry, or 0 for no maximum.
4600 * (other than the maximum length of entries.) The value passed in will
4601 * be clamped to the range 0-65536.
4603 * Sets the maximum allowed length of the contents of the widget. If
4604 * the current contents are longer than the given length, then they
4605 * will be truncated to fit.
4608 gtk_entry_set_max_length (GtkEntry *entry,
4611 g_return_if_fail (GTK_IS_ENTRY (entry));
4613 max = CLAMP (max, 0, MAX_SIZE);
4615 if (max > 0 && entry->text_length > max)
4616 gtk_editable_delete_text (GTK_EDITABLE (entry), max, -1);
4618 entry->text_max_length = max;
4619 g_object_notify (G_OBJECT (entry), "max-length");
4623 * gtk_entry_get_max_length:
4624 * @entry: a #GtkEntry
4626 * Retrieves the maximum allowed length of the text in
4627 * @entry. See gtk_entry_set_max_length().
4629 * Return value: the maximum allowed number of characters
4630 * in #GtkEntry, or 0 if there is no maximum.
4633 gtk_entry_get_max_length (GtkEntry *entry)
4635 g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
4637 return entry->text_max_length;
4641 * gtk_entry_set_activates_default:
4642 * @entry: a #GtkEntry
4643 * @setting: %TRUE to activate window's default widget on Enter keypress
4645 * If @setting is %TRUE, pressing Enter in the @entry will activate the default
4646 * widget for the window containing the entry. This usually means that
4647 * the dialog box containing the entry will be closed, since the default
4648 * widget is usually one of the dialog buttons.
4650 * (For experts: if @setting is %TRUE, the entry calls
4651 * gtk_window_activate_default() on the window containing the entry, in
4652 * the default handler for the #GtkWidget::activate signal.)
4655 gtk_entry_set_activates_default (GtkEntry *entry,
4658 g_return_if_fail (GTK_IS_ENTRY (entry));
4659 setting = setting != FALSE;
4661 if (setting != entry->activates_default)
4663 entry->activates_default = setting;
4664 g_object_notify (G_OBJECT (entry), "activates-default");
4669 * gtk_entry_get_activates_default:
4670 * @entry: a #GtkEntry
4672 * Retrieves the value set by gtk_entry_set_activates_default().
4674 * Return value: %TRUE if the entry will activate the default widget
4677 gtk_entry_get_activates_default (GtkEntry *entry)
4679 g_return_val_if_fail (GTK_IS_ENTRY (entry), FALSE);
4681 return entry->activates_default;
4685 * gtk_entry_set_width_chars:
4686 * @entry: a #GtkEntry
4687 * @n_chars: width in chars
4689 * Changes the size request of the entry to be about the right size
4690 * for @n_chars characters. Note that it changes the size
4691 * <emphasis>request</emphasis>, the size can still be affected by
4692 * how you pack the widget into containers. If @n_chars is -1, the
4693 * size reverts to the default entry size.
4696 gtk_entry_set_width_chars (GtkEntry *entry,
4699 g_return_if_fail (GTK_IS_ENTRY (entry));
4701 if (entry->width_chars != n_chars)
4703 entry->width_chars = n_chars;
4704 g_object_notify (G_OBJECT (entry), "width-chars");
4705 gtk_widget_queue_resize (GTK_WIDGET (entry));
4710 * gtk_entry_get_width_chars:
4711 * @entry: a #GtkEntry
4713 * Gets the value set by gtk_entry_set_width_chars().
4715 * Return value: number of chars to request space for, or negative if unset
4718 gtk_entry_get_width_chars (GtkEntry *entry)
4720 g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
4722 return entry->width_chars;
4726 * gtk_entry_set_has_frame:
4727 * @entry: a #GtkEntry
4728 * @setting: new value
4730 * Sets whether the entry has a beveled frame around it.
4733 gtk_entry_set_has_frame (GtkEntry *entry,
4736 g_return_if_fail (GTK_IS_ENTRY (entry));
4738 setting = (setting != FALSE);
4740 if (entry->has_frame == setting)
4743 gtk_widget_queue_resize (GTK_WIDGET (entry));
4744 entry->has_frame = setting;
4745 g_object_notify (G_OBJECT (entry), "has-frame");
4749 * gtk_entry_get_has_frame:
4750 * @entry: a #GtkEntry
4752 * Gets the value set by gtk_entry_set_has_frame().
4754 * Return value: whether the entry has a beveled frame
4757 gtk_entry_get_has_frame (GtkEntry *entry)
4759 g_return_val_if_fail (GTK_IS_ENTRY (entry), FALSE);
4761 return entry->has_frame;
4765 * gtk_entry_set_inner_border:
4766 * @entry: a #GtkEntry
4767 * @border: a #GtkBorder, or %NULL
4769 * Sets %entry's inner-border property to %border, or clears it if %NULL
4770 * is passed. The inner-border is the area around the entry's text, but
4773 * If set, this property overrides the inner-border style property.
4774 * Overriding the style-provided border is useful when you want to do
4775 * in-place editing of some text in a canvas or list widget, where
4776 * pixel-exact positioning of the entry is important.
4781 gtk_entry_set_inner_border (GtkEntry *entry,
4782 const GtkBorder *border)
4784 g_return_if_fail (GTK_IS_ENTRY (entry));
4786 gtk_widget_queue_resize (GTK_WIDGET (entry));
4789 g_object_set_qdata_full (G_OBJECT (entry), quark_inner_border,
4790 gtk_border_copy (border),
4791 (GDestroyNotify) gtk_border_free);
4793 g_object_set_qdata (G_OBJECT (entry), quark_inner_border, NULL);
4795 g_object_notify (G_OBJECT (entry), "inner-border");
4799 * gtk_entry_get_inner_border:
4800 * @entry: a #GtkEntry
4802 * This function returns the entry's #GtkEntry:inner-border property. See
4803 * gtk_entry_set_inner_border() for more information.
4805 * Return value: the entry's #GtkBorder, or %NULL if none was set.
4809 G_CONST_RETURN GtkBorder *
4810 gtk_entry_get_inner_border (GtkEntry *entry)
4812 g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
4814 return g_object_get_qdata (G_OBJECT (entry), quark_inner_border);
4818 * gtk_entry_get_layout:
4819 * @entry: a #GtkEntry
4821 * Gets the #PangoLayout used to display the entry.
4822 * The layout is useful to e.g. convert text positions to
4823 * pixel positions, in combination with gtk_entry_get_layout_offsets().
4824 * The returned layout is owned by the entry and must not be
4825 * modified or freed by the caller.
4827 * Keep in mind that the layout text may contain a preedit string, so
4828 * gtk_entry_layout_index_to_text_index() and
4829 * gtk_entry_text_index_to_layout_index() are needed to convert byte
4830 * indices in the layout to byte indices in the entry contents.
4832 * Return value: the #PangoLayout for this entry
4835 gtk_entry_get_layout (GtkEntry *entry)
4837 PangoLayout *layout;
4839 g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
4841 layout = gtk_entry_ensure_layout (entry, TRUE);
4848 * gtk_entry_layout_index_to_text_index:
4849 * @entry: a #GtkEntry
4850 * @layout_index: byte index into the entry layout text
4852 * Converts from a position in the entry contents (returned
4853 * by gtk_entry_get_text()) to a position in the
4854 * entry's #PangoLayout (returned by gtk_entry_get_layout(),
4855 * with text retrieved via pango_layout_get_text()).
4857 * Return value: byte index into the entry contents
4860 gtk_entry_layout_index_to_text_index (GtkEntry *entry,
4863 PangoLayout *layout;
4867 g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
4869 layout = gtk_entry_ensure_layout (entry, TRUE);
4870 text = pango_layout_get_text (layout);
4871 cursor_index = g_utf8_offset_to_pointer (text, entry->current_pos) - text;
4873 if (layout_index >= cursor_index && entry->preedit_length)
4875 if (layout_index >= cursor_index + entry->preedit_length)
4876 layout_index -= entry->preedit_length;
4878 layout_index = cursor_index;
4881 return layout_index;
4885 * gtk_entry_text_index_to_layout_index:
4886 * @entry: a #GtkEntry
4887 * @text_index: byte index into the entry contents
4889 * Converts from a position in the entry's #PangoLayout (returned by
4890 * gtk_entry_get_layout()) to a position in the entry contents
4891 * (returned by gtk_entry_get_text()).
4893 * Return value: byte index into the entry layout text
4896 gtk_entry_text_index_to_layout_index (GtkEntry *entry,
4899 PangoLayout *layout;
4902 g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
4904 layout = gtk_entry_ensure_layout (entry, TRUE);
4905 text = pango_layout_get_text (layout);
4906 cursor_index = g_utf8_offset_to_pointer (text, entry->current_pos) - text;
4908 if (text_index > cursor_index)
4909 text_index += entry->preedit_length;
4915 * gtk_entry_get_layout_offsets:
4916 * @entry: a #GtkEntry
4917 * @x: location to store X offset of layout, or %NULL
4918 * @y: location to store Y offset of layout, or %NULL
4921 * Obtains the position of the #PangoLayout used to render text
4922 * in the entry, in widget coordinates. Useful if you want to line
4923 * up the text in an entry with some other text, e.g. when using the
4924 * entry to implement editable cells in a sheet widget.
4926 * Also useful to convert mouse events into coordinates inside the
4927 * #PangoLayout, e.g. to take some action if some part of the entry text
4930 * Note that as the user scrolls around in the entry the offsets will
4931 * change; you'll need to connect to the "notify::scroll-offset"
4932 * signal to track this. Remember when using the #PangoLayout
4933 * functions you need to convert to and from pixels using
4934 * PANGO_PIXELS() or #PANGO_SCALE.
4936 * Keep in mind that the layout text may contain a preedit string, so
4937 * gtk_entry_layout_index_to_text_index() and
4938 * gtk_entry_text_index_to_layout_index() are needed to convert byte
4939 * indices in the layout to byte indices in the entry contents.
4942 gtk_entry_get_layout_offsets (GtkEntry *entry,
4946 gint text_area_x, text_area_y;
4948 g_return_if_fail (GTK_IS_ENTRY (entry));
4950 /* this gets coords relative to text area */
4951 get_layout_position (entry, x, y);
4953 /* convert to widget coords */
4954 get_text_area_size (entry, &text_area_x, &text_area_y, NULL, NULL);
4965 * gtk_entry_set_alignment:
4966 * @entry: a #GtkEntry
4967 * @xalign: The horizontal alignment, from 0 (left) to 1 (right).
4968 * Reversed for RTL layouts
4970 * Sets the alignment for the contents of the entry. This controls
4971 * the horizontal positioning of the contents when the displayed
4972 * text is shorter than the width of the entry.
4977 gtk_entry_set_alignment (GtkEntry *entry, gfloat xalign)
4979 GtkEntryPrivate *priv;
4981 g_return_if_fail (GTK_IS_ENTRY (entry));
4983 priv = GTK_ENTRY_GET_PRIVATE (entry);
4987 else if (xalign > 1.0)
4990 if (xalign != priv->xalign)
4992 priv->xalign = xalign;
4994 gtk_entry_recompute (entry);
4996 g_object_notify (G_OBJECT (entry), "xalign");
5001 * gtk_entry_get_alignment:
5002 * @entry: a #GtkEntry
5004 * Gets the value set by gtk_entry_set_alignment().
5006 * Return value: the alignment
5011 gtk_entry_get_alignment (GtkEntry *entry)
5013 GtkEntryPrivate *priv;
5015 g_return_val_if_fail (GTK_IS_ENTRY (entry), 0.0);
5017 priv = GTK_ENTRY_GET_PRIVATE (entry);
5019 return priv->xalign;
5022 /* Quick hack of a popup menu
5025 activate_cb (GtkWidget *menuitem,
5028 const gchar *signal = g_object_get_data (G_OBJECT (menuitem), "gtk-signal");
5029 g_signal_emit_by_name (entry, signal);
5034 gtk_entry_mnemonic_activate (GtkWidget *widget,
5035 gboolean group_cycling)
5037 gtk_widget_grab_focus (widget);
5042 append_action_signal (GtkEntry *entry,
5044 const gchar *stock_id,
5045 const gchar *signal,
5048 GtkWidget *menuitem = gtk_image_menu_item_new_from_stock (stock_id, NULL);
5050 g_object_set_data (G_OBJECT (menuitem), I_("gtk-signal"), (char *)signal);
5051 g_signal_connect (menuitem, "activate",
5052 G_CALLBACK (activate_cb), entry);
5054 gtk_widget_set_sensitive (menuitem, sensitive);
5056 gtk_widget_show (menuitem);
5057 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
5061 popup_menu_detach (GtkWidget *attach_widget,
5064 GTK_ENTRY (attach_widget)->popup_menu = NULL;
5068 popup_position_func (GtkMenu *menu,
5074 GtkEntry *entry = GTK_ENTRY (user_data);
5075 GtkWidget *widget = GTK_WIDGET (entry);
5077 GtkRequisition menu_req;
5078 GdkRectangle monitor;
5079 GtkBorder inner_border;
5080 gint monitor_num, strong_x, height;
5082 g_return_if_fail (GTK_WIDGET_REALIZED (entry));
5084 gdk_window_get_origin (entry->text_area, x, y);
5086 screen = gtk_widget_get_screen (widget);
5087 monitor_num = gdk_screen_get_monitor_at_window (screen, entry->text_area);
5088 if (monitor_num < 0)
5090 gtk_menu_set_monitor (menu, monitor_num);
5092 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
5093 gtk_widget_size_request (entry->popup_menu, &menu_req);
5094 gdk_drawable_get_size (entry->text_area, NULL, &height);
5095 gtk_entry_get_cursor_locations (entry, CURSOR_STANDARD, &strong_x, NULL);
5096 _gtk_entry_effective_inner_border (entry, &inner_border);
5098 *x += inner_border.left + strong_x - entry->scroll_offset;
5099 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
5100 *x -= menu_req.width;
5102 if ((*y + height + menu_req.height) <= monitor.y + monitor.height)
5104 else if ((*y - menu_req.height) >= monitor.y)
5105 *y -= menu_req.height;
5106 else if (monitor.y + monitor.height - (*y + height) > *y)
5109 *y -= menu_req.height;
5115 unichar_chosen_func (const char *text,
5118 GtkEntry *entry = GTK_ENTRY (data);
5120 if (entry->editable)
5121 gtk_entry_enter_text (entry, text);
5132 popup_targets_received (GtkClipboard *clipboard,
5133 GtkSelectionData *data,
5136 PopupInfo *info = user_data;
5137 GtkEntry *entry = info->entry;
5139 if (GTK_WIDGET_REALIZED (entry))
5141 gboolean clipboard_contains_text;
5142 GtkWidget *menuitem;
5144 gboolean show_input_method_menu;
5145 gboolean show_unicode_menu;
5147 clipboard_contains_text = gtk_selection_data_targets_include_text (data);
5148 if (entry->popup_menu)
5149 gtk_widget_destroy (entry->popup_menu);
5151 entry->popup_menu = gtk_menu_new ();
5153 gtk_menu_attach_to_widget (GTK_MENU (entry->popup_menu),
5157 append_action_signal (entry, entry->popup_menu, GTK_STOCK_CUT, "cut_clipboard",
5158 entry->editable && entry->current_pos != entry->selection_bound);
5159 append_action_signal (entry, entry->popup_menu, GTK_STOCK_COPY, "copy_clipboard",
5160 entry->current_pos != entry->selection_bound);
5161 append_action_signal (entry, entry->popup_menu, GTK_STOCK_PASTE, "paste_clipboard",
5162 entry->editable && clipboard_contains_text);
5164 menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_DELETE, NULL);
5165 gtk_widget_set_sensitive (menuitem, entry->editable && entry->current_pos != entry->selection_bound);
5166 g_signal_connect_swapped (menuitem, "activate",
5167 G_CALLBACK (gtk_entry_delete_cb), entry);
5168 gtk_widget_show (menuitem);
5169 gtk_menu_shell_append (GTK_MENU_SHELL (entry->popup_menu), menuitem);
5171 menuitem = gtk_separator_menu_item_new ();
5172 gtk_widget_show (menuitem);
5173 gtk_menu_shell_append (GTK_MENU_SHELL (entry->popup_menu), menuitem);
5175 menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_SELECT_ALL, NULL);
5176 g_signal_connect_swapped (menuitem, "activate",
5177 G_CALLBACK (gtk_entry_select_all), entry);
5178 gtk_widget_show (menuitem);
5179 gtk_menu_shell_append (GTK_MENU_SHELL (entry->popup_menu), menuitem);
5181 g_object_get (gtk_widget_get_settings (GTK_WIDGET (entry)),
5182 "gtk-show-input-method-menu", &show_input_method_menu,
5183 "gtk-show-unicode-menu", &show_unicode_menu,
5186 if (!entry->visible)
5187 show_input_method_menu = FALSE;
5189 if (show_input_method_menu || show_unicode_menu)
5191 menuitem = gtk_separator_menu_item_new ();
5192 gtk_widget_show (menuitem);
5193 gtk_menu_shell_append (GTK_MENU_SHELL (entry->popup_menu), menuitem);
5196 if (show_input_method_menu)
5198 menuitem = gtk_menu_item_new_with_mnemonic (_("Input _Methods"));
5199 gtk_widget_set_sensitive (menuitem, entry->editable);
5200 gtk_widget_show (menuitem);
5201 submenu = gtk_menu_new ();
5202 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
5204 gtk_menu_shell_append (GTK_MENU_SHELL (entry->popup_menu), menuitem);
5206 gtk_im_multicontext_append_menuitems (GTK_IM_MULTICONTEXT (entry->im_context),
5207 GTK_MENU_SHELL (submenu));
5210 if (show_unicode_menu)
5212 menuitem = gtk_menu_item_new_with_mnemonic (_("_Insert Unicode Control Character"));
5213 gtk_widget_set_sensitive (menuitem, entry->editable);
5214 gtk_widget_show (menuitem);
5216 submenu = gtk_menu_new ();
5217 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
5218 gtk_menu_shell_append (GTK_MENU_SHELL (entry->popup_menu), menuitem);
5220 _gtk_text_util_append_special_char_menuitems (GTK_MENU_SHELL (submenu),
5221 unichar_chosen_func,
5225 g_signal_emit (entry,
5226 signals[POPULATE_POPUP],
5232 gtk_menu_popup (GTK_MENU (entry->popup_menu), NULL, NULL,
5234 info->button, info->time);
5237 gtk_menu_popup (GTK_MENU (entry->popup_menu), NULL, NULL,
5238 popup_position_func, entry,
5239 info->button, info->time);
5240 gtk_menu_shell_select_first (GTK_MENU_SHELL (entry->popup_menu), FALSE);
5244 g_object_unref (entry);
5249 gtk_entry_do_popup (GtkEntry *entry,
5250 GdkEventButton *event)
5252 PopupInfo *info = g_new (PopupInfo, 1);
5254 /* In order to know what entries we should make sensitive, we
5255 * ask for the current targets of the clipboard, and when
5256 * we get them, then we actually pop up the menu.
5258 info->entry = g_object_ref (entry);
5262 info->button = event->button;
5263 info->time = event->time;
5268 info->time = gtk_get_current_event_time ();
5271 gtk_clipboard_request_contents (gtk_widget_get_clipboard (GTK_WIDGET (entry), GDK_SELECTION_CLIPBOARD),
5272 gdk_atom_intern_static_string ("TARGETS"),
5273 popup_targets_received,
5278 gtk_entry_popup_menu (GtkWidget *widget)
5280 gtk_entry_do_popup (GTK_ENTRY (widget), NULL);
5285 gtk_entry_drag_leave (GtkWidget *widget,
5286 GdkDragContext *context,
5289 GtkEntry *entry = GTK_ENTRY (widget);
5291 entry->dnd_position = -1;
5292 gtk_widget_queue_draw (widget);
5296 gtk_entry_drag_drop (GtkWidget *widget,
5297 GdkDragContext *context,
5302 GtkEntry *entry = GTK_ENTRY (widget);
5303 GdkAtom target = GDK_NONE;
5305 if (entry->editable)
5306 target = gtk_drag_dest_find_target (widget, context, NULL);
5308 if (target != GDK_NONE)
5309 gtk_drag_get_data (widget, context, target, time);
5311 gtk_drag_finish (context, FALSE, FALSE, time);
5317 gtk_entry_drag_motion (GtkWidget *widget,
5318 GdkDragContext *context,
5323 GtkEntry *entry = GTK_ENTRY (widget);
5324 GtkWidget *source_widget;
5325 GdkDragAction suggested_action;
5326 gint new_position, old_position;
5329 x -= widget->style->xthickness;
5330 y -= widget->style->ythickness;
5332 old_position = entry->dnd_position;
5333 new_position = gtk_entry_find_position (entry, x + entry->scroll_offset);
5335 if (entry->editable &&
5336 gtk_drag_dest_find_target (widget, context, NULL) != GDK_NONE)
5338 source_widget = gtk_drag_get_source_widget (context);
5339 suggested_action = context->suggested_action;
5341 if (!gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &sel1, &sel2) ||
5342 new_position < sel1 || new_position > sel2)
5344 if (source_widget == widget)
5346 /* Default to MOVE, unless the user has
5347 * pressed ctrl or alt to affect available actions
5349 if ((context->actions & GDK_ACTION_MOVE) != 0)
5350 suggested_action = GDK_ACTION_MOVE;
5353 entry->dnd_position = new_position;
5357 if (source_widget == widget)
5358 suggested_action = 0; /* Can't drop in selection where drag started */
5360 entry->dnd_position = -1;
5365 /* Entry not editable, or no text */
5366 suggested_action = 0;
5367 entry->dnd_position = -1;
5370 gdk_drag_status (context, suggested_action, time);
5372 if (entry->dnd_position != old_position)
5373 gtk_widget_queue_draw (widget);
5379 gtk_entry_drag_data_received (GtkWidget *widget,
5380 GdkDragContext *context,
5383 GtkSelectionData *selection_data,
5387 GtkEntry *entry = GTK_ENTRY (widget);
5388 GtkEditable *editable = GTK_EDITABLE (widget);
5391 str = (gchar *) gtk_selection_data_get_text (selection_data);
5393 x -= widget->style->xthickness;
5394 y -= widget->style->ythickness;
5396 if (str && entry->editable)
5402 if (entry->truncate_multiline)
5403 length = truncate_multiline (str);
5405 new_position = gtk_entry_find_position (entry, x + entry->scroll_offset);
5407 if (!gtk_editable_get_selection_bounds (editable, &sel1, &sel2) ||
5408 new_position < sel1 || new_position > sel2)
5410 gtk_editable_insert_text (editable, str, length, &new_position);
5414 /* Replacing selection */
5415 begin_change (entry);
5416 g_object_freeze_notify (G_OBJECT (entry));
5417 gtk_editable_delete_text (editable, sel1, sel2);
5418 gtk_editable_insert_text (editable, str, length, &sel1);
5419 g_object_thaw_notify (G_OBJECT (entry));
5423 gtk_drag_finish (context, TRUE, context->action == GDK_ACTION_MOVE, time);
5427 /* Drag and drop didn't happen! */
5428 gtk_drag_finish (context, FALSE, FALSE, time);
5435 gtk_entry_drag_data_get (GtkWidget *widget,
5436 GdkDragContext *context,
5437 GtkSelectionData *selection_data,
5441 gint sel_start, sel_end;
5443 GtkEditable *editable = GTK_EDITABLE (widget);
5445 if (gtk_editable_get_selection_bounds (editable, &sel_start, &sel_end))
5447 gchar *str = gtk_entry_get_public_chars (GTK_ENTRY (widget), sel_start, sel_end);
5449 gtk_selection_data_set_text (selection_data, str, -1);
5457 gtk_entry_drag_data_delete (GtkWidget *widget,
5458 GdkDragContext *context)
5460 gint sel_start, sel_end;
5462 GtkEditable *editable = GTK_EDITABLE (widget);
5464 if (GTK_ENTRY (widget)->editable &&
5465 gtk_editable_get_selection_bounds (editable, &sel_start, &sel_end))
5466 gtk_editable_delete_text (editable, sel_start, sel_end);
5469 /* We display the cursor when
5471 * - the selection is empty, AND
5472 * - the widget has focus
5475 #define CURSOR_ON_MULTIPLIER 2
5476 #define CURSOR_OFF_MULTIPLIER 1
5477 #define CURSOR_PEND_MULTIPLIER 3
5478 #define CURSOR_DIVIDER 3
5481 cursor_blinks (GtkEntry *entry)
5483 if (GTK_WIDGET_HAS_FOCUS (entry) &&
5485 entry->selection_bound == entry->current_pos)
5487 GtkSettings *settings;
5490 settings = gtk_widget_get_settings (GTK_WIDGET (entry));
5491 g_object_get (settings, "gtk-cursor-blink", &blink, NULL);
5500 get_cursor_time (GtkEntry *entry)
5502 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (entry));
5505 g_object_get (settings, "gtk-cursor-blink-time", &time, NULL);
5511 get_cursor_blink_timeout (GtkEntry *entry)
5513 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (entry));
5516 g_object_get (settings, "gtk-cursor-blink-timeout", &timeout, NULL);
5522 show_cursor (GtkEntry *entry)
5524 if (!entry->cursor_visible)
5526 entry->cursor_visible = TRUE;
5528 if (GTK_WIDGET_HAS_FOCUS (entry) && entry->selection_bound == entry->current_pos)
5529 gtk_widget_queue_draw (GTK_WIDGET (entry));
5534 hide_cursor (GtkEntry *entry)
5536 if (entry->cursor_visible)
5538 entry->cursor_visible = FALSE;
5540 if (GTK_WIDGET_HAS_FOCUS (entry) && entry->selection_bound == entry->current_pos)
5541 gtk_widget_queue_draw (GTK_WIDGET (entry));
5549 blink_cb (gpointer data)
5552 GtkEntryPrivate *priv;
5555 entry = GTK_ENTRY (data);
5556 priv = GTK_ENTRY_GET_PRIVATE (entry);
5558 if (!GTK_WIDGET_HAS_FOCUS (entry))
5560 g_warning ("GtkEntry - did not receive focus-out-event. If you\n"
5561 "connect a handler to this signal, it must return\n"
5562 "FALSE so the entry gets the event as well");
5564 gtk_entry_check_cursor_blink (entry);
5569 g_assert (entry->selection_bound == entry->current_pos);
5571 blink_timeout = get_cursor_blink_timeout (entry);
5572 if (priv->blink_time > 1000 * blink_timeout &&
5573 blink_timeout < G_MAXINT/1000)
5575 /* we've blinked enough without the user doing anything, stop blinking */
5576 show_cursor (entry);
5577 entry->blink_timeout = 0;
5579 else if (entry->cursor_visible)
5581 hide_cursor (entry);
5582 entry->blink_timeout = gdk_threads_add_timeout (get_cursor_time (entry) * CURSOR_OFF_MULTIPLIER / CURSOR_DIVIDER,
5588 show_cursor (entry);
5589 priv->blink_time += get_cursor_time (entry);
5590 entry->blink_timeout = gdk_threads_add_timeout (get_cursor_time (entry) * CURSOR_ON_MULTIPLIER / CURSOR_DIVIDER,
5595 /* Remove ourselves */
5600 gtk_entry_check_cursor_blink (GtkEntry *entry)
5602 GtkEntryPrivate *priv;
5604 priv = GTK_ENTRY_GET_PRIVATE (entry);
5606 if (cursor_blinks (entry))
5608 if (!entry->blink_timeout)
5610 show_cursor (entry);
5611 entry->blink_timeout = gdk_threads_add_timeout (get_cursor_time (entry) * CURSOR_ON_MULTIPLIER / CURSOR_DIVIDER,
5618 if (entry->blink_timeout)
5620 g_source_remove (entry->blink_timeout);
5621 entry->blink_timeout = 0;
5624 entry->cursor_visible = TRUE;
5630 gtk_entry_pend_cursor_blink (GtkEntry *entry)
5632 if (cursor_blinks (entry))
5634 if (entry->blink_timeout != 0)
5635 g_source_remove (entry->blink_timeout);
5637 entry->blink_timeout = gdk_threads_add_timeout (get_cursor_time (entry) * CURSOR_PEND_MULTIPLIER / CURSOR_DIVIDER,
5640 show_cursor (entry);
5645 gtk_entry_reset_blink_time (GtkEntry *entry)
5647 GtkEntryPrivate *priv;
5649 priv = GTK_ENTRY_GET_PRIVATE (entry);
5651 priv->blink_time = 0;
5657 gtk_entry_completion_timeout (gpointer data)
5659 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (data);
5661 completion->priv->completion_timeout = 0;
5663 if (completion->priv->filter_model &&
5664 g_utf8_strlen (gtk_entry_get_text (GTK_ENTRY (completion->priv->entry)), -1)
5665 >= completion->priv->minimum_key_length)
5669 GtkTreeSelection *s;
5670 gboolean popup_single;
5672 gtk_entry_completion_complete (completion);
5673 matches = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->filter_model), NULL);
5675 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view)));
5677 s = gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->action_view));
5679 gtk_tree_selection_unselect_all (s);
5681 actions = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->actions), NULL);
5683 g_object_get (completion, "popup-single-match", &popup_single, NULL);
5684 if ((matches > (popup_single ? 0: 1)) || actions > 0)
5686 if (GTK_WIDGET_VISIBLE (completion->priv->popup_window))
5687 _gtk_entry_completion_resize_popup (completion);
5689 _gtk_entry_completion_popup (completion);
5692 _gtk_entry_completion_popdown (completion);
5694 else if (GTK_WIDGET_VISIBLE (completion->priv->popup_window))
5695 _gtk_entry_completion_popdown (completion);
5700 static inline gboolean
5701 keyval_is_cursor_move (guint keyval)
5703 if (keyval == GDK_Up || keyval == GDK_KP_Up)
5706 if (keyval == GDK_Down || keyval == GDK_KP_Down)
5709 if (keyval == GDK_Page_Up)
5712 if (keyval == GDK_Page_Down)
5719 gtk_entry_completion_key_press (GtkWidget *widget,
5723 gint matches, actions = 0;
5724 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
5726 if (!GTK_WIDGET_MAPPED (completion->priv->popup_window))
5729 matches = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->filter_model), NULL);
5731 if (completion->priv->actions)
5732 actions = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->actions), NULL);
5734 if (keyval_is_cursor_move (event->keyval))
5736 GtkTreePath *path = NULL;
5738 if (event->keyval == GDK_Up || event->keyval == GDK_KP_Up)
5740 if (completion->priv->current_selected < 0)
5741 completion->priv->current_selected = matches + actions - 1;
5743 completion->priv->current_selected--;
5745 else if (event->keyval == GDK_Down || event->keyval == GDK_KP_Down)
5747 if (completion->priv->current_selected < matches + actions - 1)
5748 completion->priv->current_selected++;
5750 completion->priv->current_selected = -1;
5752 else if (event->keyval == GDK_Page_Up)
5754 if (completion->priv->current_selected < 0)
5755 completion->priv->current_selected = matches + actions - 1;
5756 else if (completion->priv->current_selected == 0)
5757 completion->priv->current_selected = -1;
5758 else if (completion->priv->current_selected < matches)
5760 completion->priv->current_selected -= 14;
5761 if (completion->priv->current_selected < 0)
5762 completion->priv->current_selected = 0;
5766 completion->priv->current_selected -= 14;
5767 if (completion->priv->current_selected < matches - 1)
5768 completion->priv->current_selected = matches - 1;
5771 else if (event->keyval == GDK_Page_Down)
5773 if (completion->priv->current_selected < 0)
5774 completion->priv->current_selected = 0;
5775 else if (completion->priv->current_selected < matches - 1)
5777 completion->priv->current_selected += 14;
5778 if (completion->priv->current_selected > matches - 1)
5779 completion->priv->current_selected = matches - 1;
5781 else if (completion->priv->current_selected == matches + actions - 1)
5783 completion->priv->current_selected = -1;
5787 completion->priv->current_selected += 14;
5788 if (completion->priv->current_selected > matches + actions - 1)
5789 completion->priv->current_selected = matches + actions - 1;
5793 if (completion->priv->current_selected < 0)
5795 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view)));
5796 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->action_view)));
5798 else if (completion->priv->current_selected < matches)
5800 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->action_view)));
5802 path = gtk_tree_path_new_from_indices (completion->priv->current_selected, -1);
5803 gtk_tree_view_set_cursor (GTK_TREE_VIEW (completion->priv->tree_view),
5806 if (completion->priv->inline_selection)
5810 GtkTreeModel *model = NULL;
5811 GtkTreeSelection *sel;
5814 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view));
5815 if (!gtk_tree_selection_get_selected (sel, &model, &iter))
5818 if (completion->priv->completion_prefix == NULL)
5820 completion->priv->completion_prefix = g_strdup (gtk_entry_get_text (GTK_ENTRY (completion->priv->entry)));
5823 g_signal_emit_by_name (completion, "cursor_on_match", model,
5827 else if (completion->priv->current_selected - matches >= 0)
5829 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view)));
5831 path = gtk_tree_path_new_from_indices (completion->priv->current_selected - matches, -1);
5832 gtk_tree_view_set_cursor (GTK_TREE_VIEW (completion->priv->action_view),
5836 gtk_tree_path_free (path);
5840 else if (event->keyval == GDK_Escape ||
5841 event->keyval == GDK_Left ||
5842 event->keyval == GDK_KP_Left ||
5843 event->keyval == GDK_Right ||
5844 event->keyval == GDK_KP_Right)
5846 _gtk_entry_reset_im_context (GTK_ENTRY (widget));
5847 _gtk_entry_completion_popdown (completion);
5849 if (completion->priv->inline_selection)
5851 /* Escape rejects the tentative completion */
5852 if (event->keyval == GDK_Escape)
5854 gtk_entry_set_text (GTK_ENTRY (completion->priv->entry), completion->priv->completion_prefix);
5857 /* Move the cursor to the end for Right/Esc, to the
5858 beginning for Left */
5859 if (event->keyval == GDK_Right ||
5860 event->keyval == GDK_KP_Right ||
5861 event->keyval == GDK_Escape)
5862 gtk_editable_set_position (GTK_EDITABLE (widget), -1);
5864 gtk_editable_set_position (GTK_EDITABLE (widget), 0);
5866 g_free (completion->priv->completion_prefix);
5867 completion->priv->completion_prefix = NULL;
5872 else if (event->keyval == GDK_Tab ||
5873 event->keyval == GDK_KP_Tab ||
5874 event->keyval == GDK_ISO_Left_Tab)
5876 GtkDirectionType dir = event->keyval == GDK_ISO_Left_Tab ?
5877 GTK_DIR_TAB_BACKWARD : GTK_DIR_TAB_FORWARD;
5879 _gtk_entry_reset_im_context (GTK_ENTRY (widget));
5880 _gtk_entry_completion_popdown (completion);
5882 if (completion->priv->completion_prefix)
5884 g_free (completion->priv->completion_prefix);
5885 completion->priv->completion_prefix = NULL;
5888 gtk_widget_child_focus (gtk_widget_get_toplevel (widget), dir);
5892 else if (event->keyval == GDK_ISO_Enter ||
5893 event->keyval == GDK_KP_Enter ||
5894 event->keyval == GDK_Return)
5896 _gtk_entry_reset_im_context (GTK_ENTRY (widget));
5897 _gtk_entry_completion_popdown (completion);
5899 if (completion->priv->current_selected < matches)
5902 GtkTreeModel *model = NULL;
5903 GtkTreeSelection *sel;
5906 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view));
5907 if (!gtk_tree_selection_get_selected (sel, &model, &iter))
5910 g_signal_handler_block (widget, completion->priv->changed_id);
5911 g_signal_emit_by_name (completion, "match_selected",
5912 model, &iter, &entry_set);
5913 g_signal_handler_unblock (widget, completion->priv->changed_id);
5919 gtk_tree_model_get (model, &iter,
5920 completion->priv->text_column, &str,
5923 gtk_entry_set_text (GTK_ENTRY (widget), str);
5925 /* move the cursor to the end */
5926 gtk_editable_set_position (GTK_EDITABLE (widget), -1);
5933 else if (completion->priv->current_selected - matches >= 0)
5937 _gtk_entry_reset_im_context (GTK_ENTRY (widget));
5939 path = gtk_tree_path_new_from_indices (completion->priv->current_selected - matches, -1);
5941 g_signal_emit_by_name (completion, "action_activated",
5942 gtk_tree_path_get_indices (path)[0]);
5943 gtk_tree_path_free (path);
5953 gtk_entry_completion_changed (GtkWidget *entry,
5956 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
5958 /* (re)install completion timeout */
5959 if (completion->priv->completion_timeout)
5960 g_source_remove (completion->priv->completion_timeout);
5962 if (!gtk_entry_get_text (GTK_ENTRY (entry)))
5965 /* no need to normalize for this test */
5966 if (completion->priv->minimum_key_length > 0 &&
5967 strcmp ("", gtk_entry_get_text (GTK_ENTRY (entry))) == 0)
5969 if (GTK_WIDGET_VISIBLE (completion->priv->popup_window))
5970 _gtk_entry_completion_popdown (completion);
5974 completion->priv->completion_timeout =
5975 gdk_threads_add_timeout (COMPLETION_TIMEOUT,
5976 gtk_entry_completion_timeout,
5981 check_completion_callback (GtkEntryCompletion *completion)
5983 completion->priv->check_completion_idle = NULL;
5985 gtk_entry_completion_complete (completion);
5986 gtk_entry_completion_insert_prefix (completion);
5992 clear_completion_callback (GtkEntry *entry,
5995 if (pspec->name == I_("cursor-position") ||
5996 pspec->name == I_("selection-bound"))
5998 GtkEntryCompletion *completion = gtk_entry_get_completion (entry);
6000 completion->priv->has_completion = FALSE;
6005 accept_completion_callback (GtkEntry *entry)
6007 GtkEntryCompletion *completion = gtk_entry_get_completion (entry);
6009 if (completion->priv->has_completion)
6010 gtk_editable_set_position (GTK_EDITABLE (entry),
6011 entry->text_length);
6017 completion_insert_text_callback (GtkEntry *entry,
6021 GtkEntryCompletion *completion)
6023 /* idle to update the selection based on the file list */
6024 if (completion->priv->check_completion_idle == NULL)
6026 completion->priv->check_completion_idle = g_idle_source_new ();
6027 g_source_set_priority (completion->priv->check_completion_idle, G_PRIORITY_HIGH);
6028 g_source_set_closure (completion->priv->check_completion_idle,
6029 g_cclosure_new_object (G_CALLBACK (check_completion_callback),
6030 G_OBJECT (completion)));
6031 g_source_attach (completion->priv->check_completion_idle, NULL);
6036 completion_changed (GtkEntryCompletion *completion,
6040 GtkEntry *entry = GTK_ENTRY (data);
6042 if (pspec->name == I_("popup-completion") ||
6043 pspec->name == I_("inline-completion"))
6045 disconnect_completion_signals (entry, completion);
6046 connect_completion_signals (entry, completion);
6051 disconnect_completion_signals (GtkEntry *entry,
6052 GtkEntryCompletion *completion)
6054 g_signal_handlers_disconnect_by_func (completion,
6055 G_CALLBACK (completion_changed), entry);
6056 if (completion->priv->changed_id > 0 &&
6057 g_signal_handler_is_connected (entry, completion->priv->changed_id))
6059 g_signal_handler_disconnect (entry, completion->priv->changed_id);
6060 completion->priv->changed_id = 0;
6062 g_signal_handlers_disconnect_by_func (entry,
6063 G_CALLBACK (gtk_entry_completion_key_press), completion);
6064 if (completion->priv->insert_text_id > 0 &&
6065 g_signal_handler_is_connected (entry, completion->priv->insert_text_id))
6067 g_signal_handler_disconnect (entry, completion->priv->insert_text_id);
6068 completion->priv->insert_text_id = 0;
6070 g_signal_handlers_disconnect_by_func (entry,
6071 G_CALLBACK (completion_insert_text_callback), completion);
6072 g_signal_handlers_disconnect_by_func (entry,
6073 G_CALLBACK (clear_completion_callback), completion);
6074 g_signal_handlers_disconnect_by_func (entry,
6075 G_CALLBACK (accept_completion_callback), completion);
6079 connect_completion_signals (GtkEntry *entry,
6080 GtkEntryCompletion *completion)
6082 if (completion->priv->popup_completion)
6084 completion->priv->changed_id =
6085 g_signal_connect (entry, "changed",
6086 G_CALLBACK (gtk_entry_completion_changed), completion);
6087 g_signal_connect (entry, "key_press_event",
6088 G_CALLBACK (gtk_entry_completion_key_press), completion);
6091 if (completion->priv->inline_completion)
6093 completion->priv->insert_text_id =
6094 g_signal_connect (entry, "insert_text",
6095 G_CALLBACK (completion_insert_text_callback), completion);
6096 g_signal_connect (entry, "notify",
6097 G_CALLBACK (clear_completion_callback), completion);
6098 g_signal_connect (entry, "activate",
6099 G_CALLBACK (accept_completion_callback), completion);
6100 g_signal_connect (entry, "focus_out_event",
6101 G_CALLBACK (accept_completion_callback), completion);
6104 g_signal_connect (completion, "notify",
6105 G_CALLBACK (completion_changed), entry);
6109 * gtk_entry_set_completion:
6110 * @entry: A #GtkEntry
6111 * @completion: The #GtkEntryCompletion or %NULL
6113 * Sets @completion to be the auxiliary completion object to use with @entry.
6114 * All further configuration of the completion mechanism is done on
6115 * @completion using the #GtkEntryCompletion API. Completion is disabled if
6116 * @completion is set to %NULL.
6121 gtk_entry_set_completion (GtkEntry *entry,
6122 GtkEntryCompletion *completion)
6124 GtkEntryCompletion *old;
6126 g_return_if_fail (GTK_IS_ENTRY (entry));
6127 g_return_if_fail (!completion || GTK_IS_ENTRY_COMPLETION (completion));
6129 old = gtk_entry_get_completion (entry);
6131 if (old == completion)
6136 if (old->priv->completion_timeout)
6138 g_source_remove (old->priv->completion_timeout);
6139 old->priv->completion_timeout = 0;
6142 if (GTK_WIDGET_MAPPED (old->priv->popup_window))
6143 _gtk_entry_completion_popdown (old);
6145 disconnect_completion_signals (entry, old);
6146 old->priv->entry = NULL;
6148 g_object_unref (old);
6153 g_object_set_data (G_OBJECT (entry), I_(GTK_ENTRY_COMPLETION_KEY), NULL);
6157 /* hook into the entry */
6158 g_object_ref (completion);
6160 connect_completion_signals (entry, completion);
6161 completion->priv->entry = GTK_WIDGET (entry);
6162 g_object_set_data (G_OBJECT (entry), I_(GTK_ENTRY_COMPLETION_KEY), completion);
6166 * gtk_entry_get_completion:
6167 * @entry: A #GtkEntry
6169 * Returns the auxiliary completion object currently in use by @entry.
6171 * Return value: The auxiliary completion object currently in use by @entry.
6175 GtkEntryCompletion *
6176 gtk_entry_get_completion (GtkEntry *entry)
6178 GtkEntryCompletion *completion;
6180 g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
6182 completion = GTK_ENTRY_COMPLETION (g_object_get_data (G_OBJECT (entry),
6183 GTK_ENTRY_COMPLETION_KEY));
6189 * gtk_entry_set_cursor_hadjustment:
6190 * @entry: a #GtkEntry
6191 * @adjustment: an adjustment which should be adjusted when the cursor
6192 * is moved, or %NULL
6194 * Hooks up an adjustment to the cursor position in an entry, so that when
6195 * the cursor is moved, the adjustment is scrolled to show that position.
6196 * See gtk_scrolled_window_get_hadjustment() for a typical way of obtaining
6199 * The adjustment has to be in pixel units and in the same coordinate system
6205 gtk_entry_set_cursor_hadjustment (GtkEntry *entry,
6206 GtkAdjustment *adjustment)
6208 GtkEntryPrivate *priv;
6210 g_return_if_fail (GTK_IS_ENTRY (entry));
6212 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
6214 priv = GTK_ENTRY_GET_PRIVATE (entry);
6215 if (priv->cursor_hadjustment)
6216 g_object_unref (priv->cursor_hadjustment);
6218 g_object_ref (adjustment);
6219 priv->cursor_hadjustment = adjustment;
6223 * gtk_entry_get_cursor_hadjustment:
6224 * @entry: a #GtkEntry
6226 * Retrieves the horizontal cursor adjustment for the entry.
6227 * See gtk_entry_set_cursor_hadjustment().
6229 * Return value: the horizontal cursor adjustment, or %NULL
6230 * if none has been set.
6235 gtk_entry_get_cursor_hadjustment (GtkEntry *entry)
6237 GtkEntryPrivate *priv;
6239 g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
6241 priv = GTK_ENTRY_GET_PRIVATE (entry);
6243 return priv->cursor_hadjustment;
6246 #define __GTK_ENTRY_C__
6247 #include "gtkaliasdef.c"