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
87 typedef struct _GtkEntryPasswordHint GtkEntryPasswordHint;
89 struct _GtkEntryPasswordHint
91 gchar password_hint[PASSWORD_HINT_MAX];
92 guint password_hint_timeout_id;
93 gint password_hint_length;
94 gint password_hint_position;
113 PROP_CURSOR_POSITION,
114 PROP_SELECTION_BOUND,
121 PROP_ACTIVATES_DEFAULT,
126 PROP_TRUNCATE_MULTILINE
129 static guint signals[LAST_SIGNAL] = { 0 };
136 /* GObject, GtkObject methods
138 static void gtk_entry_editable_init (GtkEditableClass *iface);
139 static void gtk_entry_cell_editable_init (GtkCellEditableIface *iface);
140 static void gtk_entry_set_property (GObject *object,
144 static void gtk_entry_get_property (GObject *object,
148 static void gtk_entry_finalize (GObject *object);
149 static void gtk_entry_destroy (GtkObject *object);
153 static void gtk_entry_realize (GtkWidget *widget);
154 static void gtk_entry_unrealize (GtkWidget *widget);
155 static void gtk_entry_size_request (GtkWidget *widget,
156 GtkRequisition *requisition);
157 static void gtk_entry_size_allocate (GtkWidget *widget,
158 GtkAllocation *allocation);
159 static void gtk_entry_draw_frame (GtkWidget *widget);
160 static gint gtk_entry_expose (GtkWidget *widget,
161 GdkEventExpose *event);
162 static gint gtk_entry_button_press (GtkWidget *widget,
163 GdkEventButton *event);
164 static gint gtk_entry_button_release (GtkWidget *widget,
165 GdkEventButton *event);
166 static gint gtk_entry_motion_notify (GtkWidget *widget,
167 GdkEventMotion *event);
168 static gint gtk_entry_key_press (GtkWidget *widget,
170 static gint gtk_entry_key_release (GtkWidget *widget,
172 static gint gtk_entry_focus_in (GtkWidget *widget,
173 GdkEventFocus *event);
174 static gint gtk_entry_focus_out (GtkWidget *widget,
175 GdkEventFocus *event);
176 static void gtk_entry_grab_focus (GtkWidget *widget);
177 static void gtk_entry_style_set (GtkWidget *widget,
178 GtkStyle *previous_style);
179 static void gtk_entry_direction_changed (GtkWidget *widget,
180 GtkTextDirection previous_dir);
181 static void gtk_entry_state_changed (GtkWidget *widget,
182 GtkStateType previous_state);
183 static void gtk_entry_screen_changed (GtkWidget *widget,
184 GdkScreen *old_screen);
186 static gboolean gtk_entry_drag_drop (GtkWidget *widget,
187 GdkDragContext *context,
191 static gboolean gtk_entry_drag_motion (GtkWidget *widget,
192 GdkDragContext *context,
196 static void gtk_entry_drag_leave (GtkWidget *widget,
197 GdkDragContext *context,
199 static void gtk_entry_drag_data_received (GtkWidget *widget,
200 GdkDragContext *context,
203 GtkSelectionData *selection_data,
206 static void gtk_entry_drag_data_get (GtkWidget *widget,
207 GdkDragContext *context,
208 GtkSelectionData *selection_data,
211 static void gtk_entry_drag_data_delete (GtkWidget *widget,
212 GdkDragContext *context);
214 /* GtkEditable method implementations
216 static void gtk_entry_insert_text (GtkEditable *editable,
217 const gchar *new_text,
218 gint new_text_length,
220 static void gtk_entry_delete_text (GtkEditable *editable,
223 static gchar * gtk_entry_get_chars (GtkEditable *editable,
226 static void gtk_entry_real_set_position (GtkEditable *editable,
228 static gint gtk_entry_get_position (GtkEditable *editable);
229 static void gtk_entry_set_selection_bounds (GtkEditable *editable,
232 static gboolean gtk_entry_get_selection_bounds (GtkEditable *editable,
236 /* GtkCellEditable method implementations
238 static void gtk_entry_start_editing (GtkCellEditable *cell_editable,
241 /* Default signal handlers
243 static void gtk_entry_real_insert_text (GtkEditable *editable,
244 const gchar *new_text,
245 gint new_text_length,
247 static void gtk_entry_real_delete_text (GtkEditable *editable,
250 static void gtk_entry_move_cursor (GtkEntry *entry,
251 GtkMovementStep step,
253 gboolean extend_selection);
254 static void gtk_entry_insert_at_cursor (GtkEntry *entry,
256 static void gtk_entry_delete_from_cursor (GtkEntry *entry,
259 static void gtk_entry_backspace (GtkEntry *entry);
260 static void gtk_entry_cut_clipboard (GtkEntry *entry);
261 static void gtk_entry_copy_clipboard (GtkEntry *entry);
262 static void gtk_entry_paste_clipboard (GtkEntry *entry);
263 static void gtk_entry_toggle_overwrite (GtkEntry *entry);
264 static void gtk_entry_select_all (GtkEntry *entry);
265 static void gtk_entry_real_activate (GtkEntry *entry);
266 static gboolean gtk_entry_popup_menu (GtkWidget *widget);
268 static void gtk_entry_keymap_direction_changed (GdkKeymap *keymap,
270 /* IM Context Callbacks
272 static void gtk_entry_commit_cb (GtkIMContext *context,
275 static void gtk_entry_preedit_changed_cb (GtkIMContext *context,
277 static gboolean gtk_entry_retrieve_surrounding_cb (GtkIMContext *context,
279 static gboolean gtk_entry_delete_surrounding_cb (GtkIMContext *context,
286 static void gtk_entry_enter_text (GtkEntry *entry,
288 static void gtk_entry_set_positions (GtkEntry *entry,
290 gint selection_bound);
291 static void gtk_entry_draw_text (GtkEntry *entry);
292 static void gtk_entry_draw_cursor (GtkEntry *entry,
294 static PangoLayout *gtk_entry_ensure_layout (GtkEntry *entry,
295 gboolean include_preedit);
296 static void gtk_entry_reset_layout (GtkEntry *entry);
297 static void gtk_entry_queue_draw (GtkEntry *entry);
298 static void gtk_entry_recompute (GtkEntry *entry);
299 static gint gtk_entry_find_position (GtkEntry *entry,
301 static void gtk_entry_get_cursor_locations (GtkEntry *entry,
305 static void gtk_entry_adjust_scroll (GtkEntry *entry);
306 static gint gtk_entry_move_visually (GtkEntry *editable,
309 static gint gtk_entry_move_logically (GtkEntry *entry,
312 static gint gtk_entry_move_forward_word (GtkEntry *entry,
314 gboolean allow_whitespace);
315 static gint gtk_entry_move_backward_word (GtkEntry *entry,
317 gboolean allow_whitespace);
318 static void gtk_entry_delete_whitespace (GtkEntry *entry);
319 static void gtk_entry_select_word (GtkEntry *entry);
320 static void gtk_entry_select_line (GtkEntry *entry);
321 static char * gtk_entry_get_public_chars (GtkEntry *entry,
324 static void gtk_entry_paste (GtkEntry *entry,
326 static void gtk_entry_update_primary_selection (GtkEntry *entry);
327 static void gtk_entry_do_popup (GtkEntry *entry,
328 GdkEventButton *event);
329 static gboolean gtk_entry_mnemonic_activate (GtkWidget *widget,
330 gboolean group_cycling);
331 static void gtk_entry_state_changed (GtkWidget *widget,
332 GtkStateType previous_state);
333 static void gtk_entry_check_cursor_blink (GtkEntry *entry);
334 static void gtk_entry_pend_cursor_blink (GtkEntry *entry);
335 static void get_text_area_size (GtkEntry *entry,
340 static void get_widget_window_size (GtkEntry *entry,
347 static gint gtk_entry_completion_timeout (gpointer data);
348 static gboolean gtk_entry_completion_key_press (GtkWidget *widget,
351 static void gtk_entry_completion_changed (GtkWidget *entry,
353 static gboolean check_completion_callback (GtkEntryCompletion *completion);
354 static void clear_completion_callback (GtkEntry *entry,
356 static gboolean accept_completion_callback (GtkEntry *entry);
357 static void completion_insert_text_callback (GtkEntry *entry,
361 GtkEntryCompletion *completion);
362 static void completion_changed (GtkEntryCompletion *completion,
365 static void disconnect_completion_signals (GtkEntry *entry,
366 GtkEntryCompletion *completion);
367 static void connect_completion_signals (GtkEntry *entry,
368 GtkEntryCompletion *completion);
371 G_DEFINE_TYPE_WITH_CODE (GtkEntry, gtk_entry, GTK_TYPE_WIDGET,
372 G_IMPLEMENT_INTERFACE (GTK_TYPE_EDITABLE,
373 gtk_entry_editable_init)
374 G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_EDITABLE,
375 gtk_entry_cell_editable_init))
378 add_move_binding (GtkBindingSet *binding_set,
381 GtkMovementStep step,
384 g_return_if_fail ((modmask & GDK_SHIFT_MASK) == 0);
386 gtk_binding_entry_add_signal (binding_set, keyval, modmask,
390 G_TYPE_BOOLEAN, FALSE);
392 /* Selection-extending version */
393 gtk_binding_entry_add_signal (binding_set, keyval, modmask | GDK_SHIFT_MASK,
397 G_TYPE_BOOLEAN, TRUE);
401 gtk_entry_class_init (GtkEntryClass *class)
403 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
404 GtkWidgetClass *widget_class;
405 GtkObjectClass *gtk_object_class;
406 GtkBindingSet *binding_set;
408 widget_class = (GtkWidgetClass*) class;
409 gtk_object_class = (GtkObjectClass *)class;
411 gobject_class->finalize = gtk_entry_finalize;
412 gobject_class->set_property = gtk_entry_set_property;
413 gobject_class->get_property = gtk_entry_get_property;
415 widget_class->realize = gtk_entry_realize;
416 widget_class->unrealize = gtk_entry_unrealize;
417 widget_class->size_request = gtk_entry_size_request;
418 widget_class->size_allocate = gtk_entry_size_allocate;
419 widget_class->expose_event = gtk_entry_expose;
420 widget_class->button_press_event = gtk_entry_button_press;
421 widget_class->button_release_event = gtk_entry_button_release;
422 widget_class->motion_notify_event = gtk_entry_motion_notify;
423 widget_class->key_press_event = gtk_entry_key_press;
424 widget_class->key_release_event = gtk_entry_key_release;
425 widget_class->focus_in_event = gtk_entry_focus_in;
426 widget_class->focus_out_event = gtk_entry_focus_out;
427 widget_class->grab_focus = gtk_entry_grab_focus;
428 widget_class->style_set = gtk_entry_style_set;
429 widget_class->direction_changed = gtk_entry_direction_changed;
430 widget_class->state_changed = gtk_entry_state_changed;
431 widget_class->screen_changed = gtk_entry_screen_changed;
432 widget_class->mnemonic_activate = gtk_entry_mnemonic_activate;
434 widget_class->drag_drop = gtk_entry_drag_drop;
435 widget_class->drag_motion = gtk_entry_drag_motion;
436 widget_class->drag_leave = gtk_entry_drag_leave;
437 widget_class->drag_data_received = gtk_entry_drag_data_received;
438 widget_class->drag_data_get = gtk_entry_drag_data_get;
439 widget_class->drag_data_delete = gtk_entry_drag_data_delete;
441 widget_class->popup_menu = gtk_entry_popup_menu;
443 gtk_object_class->destroy = gtk_entry_destroy;
445 class->move_cursor = gtk_entry_move_cursor;
446 class->insert_at_cursor = gtk_entry_insert_at_cursor;
447 class->delete_from_cursor = gtk_entry_delete_from_cursor;
448 class->backspace = gtk_entry_backspace;
449 class->cut_clipboard = gtk_entry_cut_clipboard;
450 class->copy_clipboard = gtk_entry_copy_clipboard;
451 class->paste_clipboard = gtk_entry_paste_clipboard;
452 class->toggle_overwrite = gtk_entry_toggle_overwrite;
453 class->activate = gtk_entry_real_activate;
455 quark_inner_border = g_quark_from_static_string ("gtk-entry-inner-border");
456 quark_password_hint = g_quark_from_static_string ("gtk-entry-password-hint");
458 g_object_class_install_property (gobject_class,
459 PROP_CURSOR_POSITION,
460 g_param_spec_int ("cursor-position",
461 P_("Cursor Position"),
462 P_("The current position of the insertion cursor in chars"),
466 GTK_PARAM_READABLE));
468 g_object_class_install_property (gobject_class,
469 PROP_SELECTION_BOUND,
470 g_param_spec_int ("selection-bound",
471 P_("Selection Bound"),
472 P_("The position of the opposite end of the selection from the cursor in chars"),
476 GTK_PARAM_READABLE));
478 g_object_class_install_property (gobject_class,
480 g_param_spec_boolean ("editable",
482 P_("Whether the entry contents can be edited"),
484 GTK_PARAM_READWRITE));
486 g_object_class_install_property (gobject_class,
488 g_param_spec_int ("max-length",
489 P_("Maximum length"),
490 P_("Maximum number of characters for this entry. Zero if no maximum"),
494 GTK_PARAM_READWRITE));
495 g_object_class_install_property (gobject_class,
497 g_param_spec_boolean ("visibility",
499 P_("FALSE displays the \"invisible char\" instead of the actual text (password mode)"),
501 GTK_PARAM_READWRITE));
503 g_object_class_install_property (gobject_class,
505 g_param_spec_boolean ("has-frame",
507 P_("FALSE removes outside bevel from entry"),
509 GTK_PARAM_READWRITE));
511 g_object_class_install_property (gobject_class,
513 g_param_spec_boxed ("inner-border",
515 P_("Border between text and frame. Overrides the inner-border style property"),
517 GTK_PARAM_READWRITE));
519 g_object_class_install_property (gobject_class,
521 g_param_spec_unichar ("invisible-char",
522 P_("Invisible character"),
523 P_("The character to use when masking entry contents (in \"password mode\")"),
525 GTK_PARAM_READWRITE));
527 g_object_class_install_property (gobject_class,
528 PROP_ACTIVATES_DEFAULT,
529 g_param_spec_boolean ("activates-default",
530 P_("Activates default"),
531 P_("Whether to activate the default widget (such as the default button in a dialog) when Enter is pressed"),
533 GTK_PARAM_READWRITE));
534 g_object_class_install_property (gobject_class,
536 g_param_spec_int ("width-chars",
537 P_("Width in chars"),
538 P_("Number of characters to leave space for in the entry"),
542 GTK_PARAM_READWRITE));
544 g_object_class_install_property (gobject_class,
546 g_param_spec_int ("scroll-offset",
548 P_("Number of pixels of the entry scrolled off the screen to the left"),
552 GTK_PARAM_READABLE));
554 g_object_class_install_property (gobject_class,
556 g_param_spec_string ("text",
558 P_("The contents of the entry"),
560 GTK_PARAM_READWRITE));
565 * The horizontal alignment, from 0 (left) to 1 (right).
566 * Reversed for RTL layouts.
570 g_object_class_install_property (gobject_class,
572 g_param_spec_float ("xalign",
574 P_("The horizontal alignment, from 0 (left) to 1 (right). Reversed for RTL layouts."),
578 GTK_PARAM_READWRITE));
581 * GtkEntry:truncate-multiline:
583 * When %TRUE, pasted multi-line text is truncated to the first line.
587 g_object_class_install_property (gobject_class,
588 PROP_TRUNCATE_MULTILINE,
589 g_param_spec_boolean ("truncate-multiline",
590 P_("Truncate multiline"),
591 P_("Whether to truncate multiline pastes to one line."),
593 GTK_PARAM_READWRITE));
595 signals[POPULATE_POPUP] =
596 g_signal_new (I_("populate_popup"),
597 G_OBJECT_CLASS_TYPE (gobject_class),
599 G_STRUCT_OFFSET (GtkEntryClass, populate_popup),
601 _gtk_marshal_VOID__OBJECT,
608 g_signal_new (I_("activate"),
609 G_OBJECT_CLASS_TYPE (gobject_class),
610 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
611 G_STRUCT_OFFSET (GtkEntryClass, activate),
613 _gtk_marshal_VOID__VOID,
615 widget_class->activate_signal = signals[ACTIVATE];
617 signals[MOVE_CURSOR] =
618 g_signal_new (I_("move_cursor"),
619 G_OBJECT_CLASS_TYPE (gobject_class),
620 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
621 G_STRUCT_OFFSET (GtkEntryClass, move_cursor),
623 _gtk_marshal_VOID__ENUM_INT_BOOLEAN,
625 GTK_TYPE_MOVEMENT_STEP,
629 signals[INSERT_AT_CURSOR] =
630 g_signal_new (I_("insert_at_cursor"),
631 G_OBJECT_CLASS_TYPE (gobject_class),
632 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
633 G_STRUCT_OFFSET (GtkEntryClass, insert_at_cursor),
635 _gtk_marshal_VOID__STRING,
639 signals[DELETE_FROM_CURSOR] =
640 g_signal_new (I_("delete_from_cursor"),
641 G_OBJECT_CLASS_TYPE (gobject_class),
642 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
643 G_STRUCT_OFFSET (GtkEntryClass, delete_from_cursor),
645 _gtk_marshal_VOID__ENUM_INT,
647 GTK_TYPE_DELETE_TYPE,
651 g_signal_new (I_("backspace"),
652 G_OBJECT_CLASS_TYPE (gobject_class),
653 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
654 G_STRUCT_OFFSET (GtkEntryClass, backspace),
656 _gtk_marshal_VOID__VOID,
659 signals[CUT_CLIPBOARD] =
660 g_signal_new (I_("cut_clipboard"),
661 G_OBJECT_CLASS_TYPE (gobject_class),
662 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
663 G_STRUCT_OFFSET (GtkEntryClass, cut_clipboard),
665 _gtk_marshal_VOID__VOID,
668 signals[COPY_CLIPBOARD] =
669 g_signal_new (I_("copy_clipboard"),
670 G_OBJECT_CLASS_TYPE (gobject_class),
671 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
672 G_STRUCT_OFFSET (GtkEntryClass, copy_clipboard),
674 _gtk_marshal_VOID__VOID,
677 signals[PASTE_CLIPBOARD] =
678 g_signal_new (I_("paste_clipboard"),
679 G_OBJECT_CLASS_TYPE (gobject_class),
680 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
681 G_STRUCT_OFFSET (GtkEntryClass, paste_clipboard),
683 _gtk_marshal_VOID__VOID,
686 signals[TOGGLE_OVERWRITE] =
687 g_signal_new (I_("toggle_overwrite"),
688 G_OBJECT_CLASS_TYPE (gobject_class),
689 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
690 G_STRUCT_OFFSET (GtkEntryClass, toggle_overwrite),
692 _gtk_marshal_VOID__VOID,
699 binding_set = gtk_binding_set_by_class (class);
701 /* Moving the insertion point */
702 add_move_binding (binding_set, GDK_Right, 0,
703 GTK_MOVEMENT_VISUAL_POSITIONS, 1);
705 add_move_binding (binding_set, GDK_Left, 0,
706 GTK_MOVEMENT_VISUAL_POSITIONS, -1);
708 add_move_binding (binding_set, GDK_KP_Right, 0,
709 GTK_MOVEMENT_VISUAL_POSITIONS, 1);
711 add_move_binding (binding_set, GDK_KP_Left, 0,
712 GTK_MOVEMENT_VISUAL_POSITIONS, -1);
714 add_move_binding (binding_set, GDK_Right, GDK_CONTROL_MASK,
715 GTK_MOVEMENT_WORDS, 1);
717 add_move_binding (binding_set, GDK_Left, GDK_CONTROL_MASK,
718 GTK_MOVEMENT_WORDS, -1);
720 add_move_binding (binding_set, GDK_KP_Right, GDK_CONTROL_MASK,
721 GTK_MOVEMENT_WORDS, 1);
723 add_move_binding (binding_set, GDK_KP_Left, GDK_CONTROL_MASK,
724 GTK_MOVEMENT_WORDS, -1);
726 add_move_binding (binding_set, GDK_Home, 0,
727 GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
729 add_move_binding (binding_set, GDK_End, 0,
730 GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
732 add_move_binding (binding_set, GDK_KP_Home, 0,
733 GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
735 add_move_binding (binding_set, GDK_KP_End, 0,
736 GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
738 add_move_binding (binding_set, GDK_Home, GDK_CONTROL_MASK,
739 GTK_MOVEMENT_BUFFER_ENDS, -1);
741 add_move_binding (binding_set, GDK_End, GDK_CONTROL_MASK,
742 GTK_MOVEMENT_BUFFER_ENDS, 1);
744 add_move_binding (binding_set, GDK_KP_Home, GDK_CONTROL_MASK,
745 GTK_MOVEMENT_BUFFER_ENDS, -1);
747 add_move_binding (binding_set, GDK_KP_End, GDK_CONTROL_MASK,
748 GTK_MOVEMENT_BUFFER_ENDS, 1);
752 gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_CONTROL_MASK,
754 GTK_TYPE_MOVEMENT_STEP, GTK_MOVEMENT_BUFFER_ENDS,
756 G_TYPE_BOOLEAN, FALSE);
757 gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_CONTROL_MASK,
759 GTK_TYPE_MOVEMENT_STEP, GTK_MOVEMENT_BUFFER_ENDS,
761 G_TYPE_BOOLEAN, TRUE);
763 gtk_binding_entry_add_signal (binding_set, GDK_slash, GDK_CONTROL_MASK,
765 GTK_TYPE_MOVEMENT_STEP, GTK_MOVEMENT_BUFFER_ENDS,
767 G_TYPE_BOOLEAN, FALSE);
768 gtk_binding_entry_add_signal (binding_set, GDK_slash, GDK_CONTROL_MASK,
770 GTK_TYPE_MOVEMENT_STEP, GTK_MOVEMENT_BUFFER_ENDS,
772 G_TYPE_BOOLEAN, TRUE);
775 gtk_binding_entry_add_signal (binding_set, GDK_backslash, GDK_CONTROL_MASK,
777 GTK_TYPE_MOVEMENT_STEP, GTK_MOVEMENT_VISUAL_POSITIONS,
779 G_TYPE_BOOLEAN, FALSE);
780 gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
782 GTK_TYPE_MOVEMENT_STEP, GTK_MOVEMENT_VISUAL_POSITIONS,
784 G_TYPE_BOOLEAN, FALSE);
788 gtk_binding_entry_add_signal (binding_set, GDK_Return, 0,
790 gtk_binding_entry_add_signal (binding_set, GDK_KP_Enter, 0,
794 gtk_binding_entry_add_signal (binding_set, GDK_Delete, 0,
795 "delete_from_cursor", 2,
796 G_TYPE_ENUM, GTK_DELETE_CHARS,
799 gtk_binding_entry_add_signal (binding_set, GDK_KP_Delete, 0,
800 "delete_from_cursor", 2,
801 G_TYPE_ENUM, GTK_DELETE_CHARS,
804 gtk_binding_entry_add_signal (binding_set, GDK_BackSpace, 0,
807 /* Make this do the same as Backspace, to help with mis-typing */
808 gtk_binding_entry_add_signal (binding_set, GDK_BackSpace, GDK_SHIFT_MASK,
811 gtk_binding_entry_add_signal (binding_set, GDK_Delete, GDK_CONTROL_MASK,
812 "delete_from_cursor", 2,
813 G_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
816 gtk_binding_entry_add_signal (binding_set, GDK_KP_Delete, GDK_CONTROL_MASK,
817 "delete_from_cursor", 2,
818 G_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
821 gtk_binding_entry_add_signal (binding_set, GDK_BackSpace, GDK_CONTROL_MASK,
822 "delete_from_cursor", 2,
823 G_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
828 gtk_binding_entry_add_signal (binding_set, GDK_x, GDK_CONTROL_MASK,
830 gtk_binding_entry_add_signal (binding_set, GDK_c, GDK_CONTROL_MASK,
831 "copy_clipboard", 0);
832 gtk_binding_entry_add_signal (binding_set, GDK_v, GDK_CONTROL_MASK,
833 "paste_clipboard", 0);
835 gtk_binding_entry_add_signal (binding_set, GDK_Delete, GDK_SHIFT_MASK,
837 gtk_binding_entry_add_signal (binding_set, GDK_Insert, GDK_CONTROL_MASK,
838 "copy_clipboard", 0);
839 gtk_binding_entry_add_signal (binding_set, GDK_Insert, GDK_SHIFT_MASK,
840 "paste_clipboard", 0);
843 gtk_binding_entry_add_signal (binding_set, GDK_Insert, 0,
844 "toggle_overwrite", 0);
845 gtk_binding_entry_add_signal (binding_set, GDK_KP_Insert, 0,
846 "toggle_overwrite", 0);
849 * GtkEntry:inner-border:
851 * Sets the text area's border between the text and the frame
855 gtk_widget_class_install_style_property (widget_class,
856 g_param_spec_boxed ("inner-border",
858 P_("Border between text and frame."),
860 GTK_PARAM_READABLE));
862 gtk_settings_install_property (g_param_spec_boolean ("gtk-entry-select-on-focus",
863 P_("Select on focus"),
864 P_("Whether to select the contents of an entry when it is focused"),
866 GTK_PARAM_READWRITE));
869 * GtkSettings:gtk-entry-password-hint-timeout:
871 * How long to show the last inputted character in hidden
872 * entries. This value is in milliseconds. 0 disables showing the
873 * last char. 600 is a good value for enabling it.
877 gtk_settings_install_property (g_param_spec_uint ("gtk-entry-password-hint-timeout",
878 P_("Password Hint Timeout"),
879 P_("How long to show the last inputted character in hidden entries"),
881 GTK_PARAM_READWRITE));
883 g_type_class_add_private (gobject_class, sizeof (GtkEntryPrivate));
887 gtk_entry_editable_init (GtkEditableClass *iface)
889 iface->do_insert_text = gtk_entry_insert_text;
890 iface->do_delete_text = gtk_entry_delete_text;
891 iface->insert_text = gtk_entry_real_insert_text;
892 iface->delete_text = gtk_entry_real_delete_text;
893 iface->get_chars = gtk_entry_get_chars;
894 iface->set_selection_bounds = gtk_entry_set_selection_bounds;
895 iface->get_selection_bounds = gtk_entry_get_selection_bounds;
896 iface->set_position = gtk_entry_real_set_position;
897 iface->get_position = gtk_entry_get_position;
901 gtk_entry_cell_editable_init (GtkCellEditableIface *iface)
903 iface->start_editing = gtk_entry_start_editing;
907 gtk_entry_set_property (GObject *object,
912 GtkEntry *entry = GTK_ENTRY (object);
918 gboolean new_value = g_value_get_boolean (value);
920 if (new_value != entry->editable)
924 _gtk_entry_reset_im_context (entry);
925 if (GTK_WIDGET_HAS_FOCUS (entry))
926 gtk_im_context_focus_out (entry->im_context);
928 entry->preedit_length = 0;
929 entry->preedit_cursor = 0;
932 entry->editable = new_value;
934 if (new_value && GTK_WIDGET_HAS_FOCUS (entry))
935 gtk_im_context_focus_in (entry->im_context);
937 gtk_entry_queue_draw (entry);
942 case PROP_MAX_LENGTH:
943 gtk_entry_set_max_length (entry, g_value_get_int (value));
946 case PROP_VISIBILITY:
947 gtk_entry_set_visibility (entry, g_value_get_boolean (value));
951 gtk_entry_set_has_frame (entry, g_value_get_boolean (value));
954 case PROP_INNER_BORDER:
955 gtk_entry_set_inner_border (entry, g_value_get_boxed (value));
958 case PROP_INVISIBLE_CHAR:
959 gtk_entry_set_invisible_char (entry, g_value_get_uint (value));
962 case PROP_ACTIVATES_DEFAULT:
963 gtk_entry_set_activates_default (entry, g_value_get_boolean (value));
966 case PROP_WIDTH_CHARS:
967 gtk_entry_set_width_chars (entry, g_value_get_int (value));
971 gtk_entry_set_text (entry, g_value_get_string (value));
975 gtk_entry_set_alignment (entry, g_value_get_float (value));
978 case PROP_TRUNCATE_MULTILINE:
979 entry->truncate_multiline = g_value_get_boolean (value);
982 case PROP_SCROLL_OFFSET:
983 case PROP_CURSOR_POSITION:
985 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
991 gtk_entry_get_property (GObject *object,
996 GtkEntry *entry = GTK_ENTRY (object);
1000 case PROP_CURSOR_POSITION:
1001 g_value_set_int (value, entry->current_pos);
1003 case PROP_SELECTION_BOUND:
1004 g_value_set_int (value, entry->selection_bound);
1007 g_value_set_boolean (value, entry->editable);
1009 case PROP_MAX_LENGTH:
1010 g_value_set_int (value, entry->text_max_length);
1012 case PROP_VISIBILITY:
1013 g_value_set_boolean (value, entry->visible);
1015 case PROP_HAS_FRAME:
1016 g_value_set_boolean (value, entry->has_frame);
1018 case PROP_INNER_BORDER:
1019 g_value_set_boxed (value, gtk_entry_get_inner_border (entry));
1021 case PROP_INVISIBLE_CHAR:
1022 g_value_set_uint (value, entry->invisible_char);
1024 case PROP_ACTIVATES_DEFAULT:
1025 g_value_set_boolean (value, entry->activates_default);
1027 case PROP_WIDTH_CHARS:
1028 g_value_set_int (value, entry->width_chars);
1030 case PROP_SCROLL_OFFSET:
1031 g_value_set_int (value, entry->scroll_offset);
1034 g_value_set_string (value, gtk_entry_get_text (entry));
1037 g_value_set_float (value, gtk_entry_get_alignment (entry));
1039 case PROP_TRUNCATE_MULTILINE:
1040 g_value_set_boolean (value, entry->truncate_multiline);
1044 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1050 gtk_entry_init (GtkEntry *entry)
1052 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
1054 GTK_WIDGET_SET_FLAGS (entry, GTK_CAN_FOCUS);
1056 entry->text_size = MIN_SIZE;
1057 entry->text = g_malloc (entry->text_size);
1058 entry->text[0] = '\0';
1060 entry->editable = TRUE;
1061 entry->visible = TRUE;
1062 entry->invisible_char = '*';
1063 entry->dnd_position = -1;
1064 entry->width_chars = -1;
1065 entry->is_cell_renderer = FALSE;
1066 entry->editing_canceled = FALSE;
1067 entry->has_frame = TRUE;
1068 entry->truncate_multiline = FALSE;
1071 gtk_drag_dest_set (GTK_WIDGET (entry),
1072 GTK_DEST_DEFAULT_HIGHLIGHT,
1074 GDK_ACTION_COPY | GDK_ACTION_MOVE);
1075 gtk_drag_dest_add_text_targets (GTK_WIDGET (entry));
1077 /* This object is completely private. No external entity can gain a reference
1078 * to it; so we create it here and destroy it in finalize().
1080 entry->im_context = gtk_im_multicontext_new ();
1082 g_signal_connect (entry->im_context, "commit",
1083 G_CALLBACK (gtk_entry_commit_cb), entry);
1084 g_signal_connect (entry->im_context, "preedit_changed",
1085 G_CALLBACK (gtk_entry_preedit_changed_cb), entry);
1086 g_signal_connect (entry->im_context, "retrieve_surrounding",
1087 G_CALLBACK (gtk_entry_retrieve_surrounding_cb), entry);
1088 g_signal_connect (entry->im_context, "delete_surrounding",
1089 G_CALLBACK (gtk_entry_delete_surrounding_cb), entry);
1093 * Overwrite a memory that might contain sensitive information.
1096 trash_area (gchar *area, gsize len)
1098 volatile gchar *varea = (volatile gchar *)area;
1104 gtk_entry_destroy (GtkObject *object)
1106 GtkEntry *entry = GTK_ENTRY (object);
1109 entry->current_pos = entry->selection_bound = entry->text_length = 0;
1110 _gtk_entry_reset_im_context (entry);
1111 gtk_entry_reset_layout (entry);
1113 if (entry->blink_timeout)
1115 g_source_remove (entry->blink_timeout);
1116 entry->blink_timeout = 0;
1119 if (entry->recompute_idle)
1121 g_source_remove (entry->recompute_idle);
1122 entry->recompute_idle = 0;
1125 if (!entry->visible)
1127 /* We want to trash the text here because the entry might be leaked. */
1128 trash_area (entry->text, strlen (entry->text));
1131 GTK_OBJECT_CLASS (gtk_entry_parent_class)->destroy (object);
1135 gtk_entry_finalize (GObject *object)
1137 GtkEntry *entry = GTK_ENTRY (object);
1139 gtk_entry_set_completion (entry, NULL);
1141 if (entry->cached_layout)
1142 g_object_unref (entry->cached_layout);
1144 g_object_unref (entry->im_context);
1146 if (entry->blink_timeout)
1147 g_source_remove (entry->blink_timeout);
1149 if (entry->recompute_idle)
1150 g_source_remove (entry->recompute_idle);
1152 entry->text_size = 0;
1156 if (!entry->visible)
1157 trash_area (entry->text, strlen (entry->text));
1158 g_free (entry->text);
1162 G_OBJECT_CLASS (gtk_entry_parent_class)->finalize (object);
1166 gtk_entry_realize (GtkWidget *widget)
1169 GdkWindowAttr attributes;
1170 gint attributes_mask;
1172 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
1173 entry = GTK_ENTRY (widget);
1175 attributes.window_type = GDK_WINDOW_CHILD;
1177 get_widget_window_size (entry, &attributes.x, &attributes.y, &attributes.width, &attributes.height);
1179 attributes.wclass = GDK_INPUT_OUTPUT;
1180 attributes.visual = gtk_widget_get_visual (widget);
1181 attributes.colormap = gtk_widget_get_colormap (widget);
1182 attributes.event_mask = gtk_widget_get_events (widget);
1183 attributes.event_mask |= (GDK_EXPOSURE_MASK |
1184 GDK_BUTTON_PRESS_MASK |
1185 GDK_BUTTON_RELEASE_MASK |
1186 GDK_BUTTON1_MOTION_MASK |
1187 GDK_BUTTON3_MOTION_MASK |
1188 GDK_POINTER_MOTION_HINT_MASK |
1189 GDK_POINTER_MOTION_MASK |
1190 GDK_ENTER_NOTIFY_MASK |
1191 GDK_LEAVE_NOTIFY_MASK);
1192 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
1194 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
1195 gdk_window_set_user_data (widget->window, entry);
1197 get_text_area_size (entry, &attributes.x, &attributes.y, &attributes.width, &attributes.height);
1199 attributes.cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget), GDK_XTERM);
1200 attributes_mask |= GDK_WA_CURSOR;
1202 entry->text_area = gdk_window_new (widget->window, &attributes, attributes_mask);
1203 gdk_window_set_user_data (entry->text_area, entry);
1205 gdk_cursor_unref (attributes.cursor);
1207 widget->style = gtk_style_attach (widget->style, widget->window);
1209 gdk_window_set_background (widget->window, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1210 gdk_window_set_background (entry->text_area, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1212 gdk_window_show (entry->text_area);
1214 gtk_im_context_set_client_window (entry->im_context, entry->text_area);
1216 gtk_entry_adjust_scroll (entry);
1217 gtk_entry_update_primary_selection (entry);
1221 gtk_entry_unrealize (GtkWidget *widget)
1223 GtkEntry *entry = GTK_ENTRY (widget);
1224 GtkClipboard *clipboard;
1226 gtk_entry_reset_layout (entry);
1228 gtk_im_context_set_client_window (entry->im_context, NULL);
1230 clipboard = gtk_widget_get_clipboard (widget, GDK_SELECTION_PRIMARY);
1231 if (gtk_clipboard_get_owner (clipboard) == G_OBJECT (entry))
1232 gtk_clipboard_clear (clipboard);
1234 if (entry->text_area)
1236 gdk_window_set_user_data (entry->text_area, NULL);
1237 gdk_window_destroy (entry->text_area);
1238 entry->text_area = NULL;
1241 if (entry->popup_menu)
1243 gtk_widget_destroy (entry->popup_menu);
1244 entry->popup_menu = NULL;
1247 if (GTK_WIDGET_CLASS (gtk_entry_parent_class)->unrealize)
1248 (* GTK_WIDGET_CLASS (gtk_entry_parent_class)->unrealize) (widget);
1252 _gtk_entry_get_borders (GtkEntry *entry,
1256 GtkWidget *widget = GTK_WIDGET (entry);
1258 gboolean interior_focus;
1260 gtk_widget_style_get (widget,
1261 "interior-focus", &interior_focus,
1262 "focus-line-width", &focus_width,
1265 if (entry->has_frame)
1267 *xborder = widget->style->xthickness;
1268 *yborder = widget->style->ythickness;
1276 if (!interior_focus)
1278 *xborder += focus_width;
1279 *yborder += focus_width;
1284 gtk_entry_size_request (GtkWidget *widget,
1285 GtkRequisition *requisition)
1287 GtkEntry *entry = GTK_ENTRY (widget);
1288 PangoFontMetrics *metrics;
1289 gint xborder, yborder;
1290 GtkBorder inner_border;
1291 PangoContext *context;
1293 gtk_widget_ensure_style (widget);
1294 context = gtk_widget_get_pango_context (widget);
1295 metrics = pango_context_get_metrics (context,
1296 widget->style->font_desc,
1297 pango_context_get_language (context));
1299 entry->ascent = pango_font_metrics_get_ascent (metrics);
1300 entry->descent = pango_font_metrics_get_descent (metrics);
1302 _gtk_entry_get_borders (entry, &xborder, &yborder);
1303 _gtk_entry_effective_inner_border (entry, &inner_border);
1305 if (entry->width_chars < 0)
1306 requisition->width = MIN_ENTRY_WIDTH + xborder * 2 + inner_border.left + inner_border.right;
1309 gint char_width = pango_font_metrics_get_approximate_char_width (metrics);
1310 gint digit_width = pango_font_metrics_get_approximate_digit_width (metrics);
1311 gint char_pixels = (MAX (char_width, digit_width) + PANGO_SCALE - 1) / PANGO_SCALE;
1313 requisition->width = char_pixels * entry->width_chars + xborder * 2 + inner_border.left + inner_border.right;
1316 requisition->height = PANGO_PIXELS (entry->ascent + entry->descent) + yborder * 2 + inner_border.top + inner_border.bottom;
1318 pango_font_metrics_unref (metrics);
1322 get_text_area_size (GtkEntry *entry,
1328 gint xborder, yborder;
1329 GtkRequisition requisition;
1330 GtkWidget *widget = GTK_WIDGET (entry);
1332 gtk_widget_get_child_requisition (widget, &requisition);
1334 _gtk_entry_get_borders (entry, &xborder, &yborder);
1343 *width = GTK_WIDGET (entry)->allocation.width - xborder * 2;
1346 *height = requisition.height - yborder * 2;
1350 get_widget_window_size (GtkEntry *entry,
1356 GtkRequisition requisition;
1357 GtkWidget *widget = GTK_WIDGET (entry);
1359 gtk_widget_get_child_requisition (widget, &requisition);
1362 *x = widget->allocation.x;
1366 if (entry->is_cell_renderer)
1367 *y = widget->allocation.y;
1369 *y = widget->allocation.y + (widget->allocation.height - requisition.height) / 2;
1373 *width = widget->allocation.width;
1377 if (entry->is_cell_renderer)
1378 *height = widget->allocation.height;
1380 *height = requisition.height;
1385 _gtk_entry_effective_inner_border (GtkEntry *entry,
1388 GtkBorder *tmp_border;
1390 tmp_border = g_object_get_qdata (G_OBJECT (entry), quark_inner_border);
1394 *border = *tmp_border;
1398 gtk_widget_style_get (GTK_WIDGET (entry), "inner-border", &tmp_border, NULL);
1402 *border = *tmp_border;
1403 g_free (tmp_border);
1407 *border = default_inner_border;
1411 gtk_entry_size_allocate (GtkWidget *widget,
1412 GtkAllocation *allocation)
1414 GtkEntry *entry = GTK_ENTRY (widget);
1416 widget->allocation = *allocation;
1418 if (GTK_WIDGET_REALIZED (widget))
1420 /* We call gtk_widget_get_child_requisition, since we want (for
1421 * backwards compatibility reasons) the realization here to
1422 * be affected by the usize of the entry, if set
1424 gint x, y, width, height;
1426 get_widget_window_size (entry, &x, &y, &width, &height);
1428 gdk_window_move_resize (widget->window,
1429 x, y, width, height);
1431 get_text_area_size (entry, &x, &y, &width, &height);
1433 gdk_window_move_resize (entry->text_area,
1434 x, y, width, height);
1436 gtk_entry_recompute (entry);
1441 gtk_entry_draw_frame (GtkWidget *widget)
1445 gboolean interior_focus;
1448 gtk_widget_style_get (widget,
1449 "interior-focus", &interior_focus,
1450 "focus-line-width", &focus_width,
1453 gdk_drawable_get_size (widget->window, &width, &height);
1455 if (GTK_WIDGET_HAS_FOCUS (widget) && !interior_focus)
1459 width -= 2 * focus_width;
1460 height -= 2 * focus_width;
1463 gtk_paint_shadow (widget->style, widget->window,
1464 GTK_STATE_NORMAL, GTK_SHADOW_IN,
1465 NULL, widget, "entry",
1466 x, y, width, height);
1468 if (GTK_WIDGET_HAS_FOCUS (widget) && !interior_focus)
1472 width += 2 * focus_width;
1473 height += 2 * focus_width;
1475 gtk_paint_focus (widget->style, widget->window, GTK_WIDGET_STATE (widget),
1476 NULL, widget, "entry",
1477 0, 0, width, height);
1482 gtk_entry_expose (GtkWidget *widget,
1483 GdkEventExpose *event)
1485 GtkEntry *entry = GTK_ENTRY (widget);
1487 if (widget->window == event->window)
1488 gtk_entry_draw_frame (widget);
1489 else if (entry->text_area == event->window)
1491 gint area_width, area_height;
1493 get_text_area_size (entry, NULL, NULL, &area_width, &area_height);
1495 gtk_paint_flat_box (widget->style, entry->text_area,
1496 GTK_WIDGET_STATE(widget), GTK_SHADOW_NONE,
1497 NULL, widget, "entry_bg",
1498 0, 0, area_width, area_height);
1500 if ((entry->visible || entry->invisible_char != 0) &&
1501 GTK_WIDGET_HAS_FOCUS (widget) &&
1502 entry->selection_bound == entry->current_pos && entry->cursor_visible)
1503 gtk_entry_draw_cursor (GTK_ENTRY (widget), CURSOR_STANDARD);
1505 if (entry->dnd_position != -1)
1506 gtk_entry_draw_cursor (GTK_ENTRY (widget), CURSOR_DND);
1508 gtk_entry_draw_text (GTK_ENTRY (widget));
1515 gtk_entry_get_pixel_ranges (GtkEntry *entry,
1519 gint start_char, end_char;
1521 if (gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &start_char, &end_char))
1523 PangoLayout *layout = gtk_entry_ensure_layout (entry, TRUE);
1524 PangoLayoutLine *line = pango_layout_get_lines (layout)->data;
1525 const char *text = pango_layout_get_text (layout);
1526 gint start_index = g_utf8_offset_to_pointer (text, start_char) - text;
1527 gint end_index = g_utf8_offset_to_pointer (text, end_char) - text;
1528 gint real_n_ranges, i;
1530 pango_layout_line_get_x_ranges (line, start_index, end_index, ranges, &real_n_ranges);
1536 for (i = 0; i < real_n_ranges; ++i)
1538 r[2 * i + 1] = (r[2 * i + 1] - r[2 * i]) / PANGO_SCALE;
1539 r[2 * i] = r[2 * i] / PANGO_SCALE;
1544 *n_ranges = real_n_ranges;
1556 in_selection (GtkEntry *entry,
1561 gint retval = FALSE;
1563 gtk_entry_get_pixel_ranges (entry, &ranges, &n_ranges);
1565 for (i = 0; i < n_ranges; ++i)
1567 if (x >= ranges[2 * i] && x < ranges[2 * i] + ranges[2 * i + 1])
1579 gtk_entry_button_press (GtkWidget *widget,
1580 GdkEventButton *event)
1582 GtkEntry *entry = GTK_ENTRY (widget);
1583 GtkEditable *editable = GTK_EDITABLE (widget);
1584 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
1586 gint sel_start, sel_end;
1588 if (event->window != entry->text_area ||
1589 (entry->button && event->button != entry->button))
1592 entry->button = event->button;
1594 if (!GTK_WIDGET_HAS_FOCUS (widget))
1596 entry->in_click = TRUE;
1597 gtk_widget_grab_focus (widget);
1598 entry->in_click = FALSE;
1601 tmp_pos = gtk_entry_find_position (entry, event->x + entry->scroll_offset);
1603 if (event->button == 1)
1605 gboolean have_selection = gtk_editable_get_selection_bounds (editable, &sel_start, &sel_end);
1607 entry->select_words = FALSE;
1608 entry->select_lines = FALSE;
1610 if (event->state & GDK_SHIFT_MASK)
1612 _gtk_entry_reset_im_context (entry);
1614 if (!have_selection) /* select from the current position to the clicked position */
1615 sel_start = sel_end = entry->current_pos;
1617 if (tmp_pos > sel_start && tmp_pos < sel_end)
1619 /* Truncate current selection */
1620 gtk_entry_set_positions (entry, tmp_pos, -1);
1624 gboolean extend_to_left;
1627 /* Figure out what click selects and extend current selection */
1628 switch (event->type)
1630 case GDK_BUTTON_PRESS:
1631 gtk_entry_set_positions (entry, tmp_pos, tmp_pos);
1634 case GDK_2BUTTON_PRESS:
1635 entry->select_words = TRUE;
1636 gtk_entry_select_word (entry);
1639 case GDK_3BUTTON_PRESS:
1640 entry->select_lines = TRUE;
1641 gtk_entry_select_line (entry);
1648 start = MIN (entry->current_pos, entry->selection_bound);
1649 start = MIN (sel_start, start);
1651 end = MAX (entry->current_pos, entry->selection_bound);
1652 end = MAX (sel_end, end);
1654 if (tmp_pos == sel_start || tmp_pos == sel_end)
1655 extend_to_left = (tmp_pos == start);
1657 extend_to_left = (end == sel_end);
1660 gtk_entry_set_positions (entry, start, end);
1662 gtk_entry_set_positions (entry, end, start);
1665 else /* no shift key */
1666 switch (event->type)
1668 case GDK_BUTTON_PRESS:
1669 if (in_selection (entry, event->x + entry->scroll_offset))
1671 /* Click inside the selection - we'll either start a drag, or
1672 * clear the selection
1674 entry->in_drag = TRUE;
1675 entry->drag_start_x = event->x + entry->scroll_offset;
1676 entry->drag_start_y = event->y + entry->scroll_offset;
1679 gtk_editable_set_position (editable, tmp_pos);
1682 case GDK_2BUTTON_PRESS:
1683 /* We ALWAYS receive a GDK_BUTTON_PRESS immediately before
1684 * receiving a GDK_2BUTTON_PRESS so we need to reset
1685 * entry->in_drag which may have been set above
1687 entry->in_drag = FALSE;
1688 entry->select_words = TRUE;
1689 gtk_entry_select_word (entry);
1692 case GDK_3BUTTON_PRESS:
1693 /* We ALWAYS receive a GDK_BUTTON_PRESS immediately before
1694 * receiving a GDK_3BUTTON_PRESS so we need to reset
1695 * entry->in_drag which may have been set above
1697 entry->in_drag = FALSE;
1698 entry->select_lines = TRUE;
1699 gtk_entry_select_line (entry);
1708 else if (event->button == 2 && event->type == GDK_BUTTON_PRESS && entry->editable)
1710 priv->insert_pos = tmp_pos;
1711 gtk_entry_paste (entry, GDK_SELECTION_PRIMARY);
1715 else if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
1717 gtk_entry_do_popup (entry, event);
1718 entry->button = 0; /* Don't wait for release, since the menu will gtk_grab_add */
1727 gtk_entry_button_release (GtkWidget *widget,
1728 GdkEventButton *event)
1730 GtkEntry *entry = GTK_ENTRY (widget);
1732 if (event->window != entry->text_area || entry->button != event->button)
1737 gint tmp_pos = gtk_entry_find_position (entry, entry->drag_start_x);
1739 gtk_editable_set_position (GTK_EDITABLE (entry), tmp_pos);
1746 gtk_entry_update_primary_selection (entry);
1752 _gtk_entry_get_selected_text (GtkEntry *entry)
1754 GtkEditable *editable = GTK_EDITABLE (entry);
1755 gint start_text, end_text;
1758 if (gtk_editable_get_selection_bounds (editable, &start_text, &end_text))
1759 text = gtk_editable_get_chars (editable, start_text, end_text);
1765 drag_begin_cb (GtkWidget *widget,
1766 GdkDragContext *context,
1769 g_signal_handlers_disconnect_by_func (widget, drag_begin_cb, NULL);
1773 gtk_entry_motion_notify (GtkWidget *widget,
1774 GdkEventMotion *event)
1776 GtkEntry *entry = GTK_ENTRY (widget);
1779 if (entry->mouse_cursor_obscured)
1783 cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget), GDK_XTERM);
1784 gdk_window_set_cursor (entry->text_area, cursor);
1785 gdk_cursor_unref (cursor);
1786 entry->mouse_cursor_obscured = FALSE;
1789 if (event->window != entry->text_area || entry->button != 1)
1792 if (entry->select_lines)
1795 if (event->is_hint || (entry->text_area != event->window))
1796 gdk_window_get_pointer (entry->text_area, NULL, NULL, NULL);
1800 if (gtk_drag_check_threshold (widget,
1801 entry->drag_start_x, entry->drag_start_y,
1802 event->x + entry->scroll_offset, event->y))
1804 GdkDragContext *context;
1805 GtkTargetList *target_list = gtk_target_list_new (NULL, 0);
1806 guint actions = entry->editable ? GDK_ACTION_COPY | GDK_ACTION_MOVE : GDK_ACTION_COPY;
1808 GdkPixmap *pixmap = NULL;
1810 gtk_target_list_add_text_targets (target_list, 0);
1814 text = _gtk_entry_get_selected_text (entry);
1815 pixmap = _gtk_text_util_create_drag_icon (widget, text, -1);
1818 context = gtk_drag_begin (widget, target_list, actions,
1819 entry->button, (GdkEvent *)event);
1822 gtk_drag_set_icon_pixmap (context,
1823 gdk_drawable_get_colormap (pixmap),
1828 gtk_drag_set_icon_default (context);
1831 g_object_unref (pixmap);
1834 entry->in_drag = FALSE;
1837 gtk_target_list_unref (target_list);
1843 gdk_drawable_get_size (entry->text_area, NULL, &height);
1847 else if (event->y >= height)
1848 tmp_pos = entry->text_length;
1850 tmp_pos = gtk_entry_find_position (entry, event->x + entry->scroll_offset);
1852 if (entry->select_words)
1855 gint old_min, old_max;
1858 min = gtk_entry_move_backward_word (entry, tmp_pos, TRUE);
1859 max = gtk_entry_move_forward_word (entry, tmp_pos, TRUE);
1861 pos = entry->current_pos;
1862 bound = entry->selection_bound;
1864 old_min = MIN(entry->current_pos, entry->selection_bound);
1865 old_max = MAX(entry->current_pos, entry->selection_bound);
1872 else if (old_max < max)
1877 else if (pos == old_min)
1879 if (entry->current_pos != min)
1884 if (entry->current_pos != max)
1888 gtk_entry_set_positions (entry, pos, bound);
1891 gtk_entry_set_positions (entry, tmp_pos, -1);
1898 set_invisible_cursor (GdkWindow *window)
1900 GdkBitmap *empty_bitmap;
1903 char invisible_cursor_bits[] = { 0x0 };
1905 useless.red = useless.green = useless.blue = 0;
1908 empty_bitmap = gdk_bitmap_create_from_data (window,
1909 invisible_cursor_bits,
1912 cursor = gdk_cursor_new_from_pixmap (empty_bitmap,
1917 gdk_window_set_cursor (window, cursor);
1919 gdk_cursor_unref (cursor);
1921 g_object_unref (empty_bitmap);
1925 gtk_entry_obscure_mouse_cursor (GtkEntry *entry)
1927 if (entry->mouse_cursor_obscured)
1930 set_invisible_cursor (entry->text_area);
1932 entry->mouse_cursor_obscured = TRUE;
1936 gtk_entry_key_press (GtkWidget *widget,
1939 GtkEntry *entry = GTK_ENTRY (widget);
1941 gtk_entry_pend_cursor_blink (entry);
1943 if (entry->editable)
1945 if (gtk_im_context_filter_keypress (entry->im_context, event))
1947 gtk_entry_obscure_mouse_cursor (entry);
1948 entry->need_im_reset = TRUE;
1953 if (event->keyval == GDK_Return ||
1954 event->keyval == GDK_KP_Enter ||
1955 event->keyval == GDK_ISO_Enter ||
1956 event->keyval == GDK_Escape)
1958 GtkEntryCompletion *completion = gtk_entry_get_completion (entry);
1960 if (completion && completion->priv->completion_timeout)
1962 g_source_remove (completion->priv->completion_timeout);
1963 completion->priv->completion_timeout = 0;
1966 _gtk_entry_reset_im_context (entry);
1969 if (GTK_WIDGET_CLASS (gtk_entry_parent_class)->key_press_event (widget, event))
1970 /* Activate key bindings
1978 gtk_entry_key_release (GtkWidget *widget,
1981 GtkEntry *entry = GTK_ENTRY (widget);
1983 if (entry->editable)
1985 if (gtk_im_context_filter_keypress (entry->im_context, event))
1987 entry->need_im_reset = TRUE;
1992 return GTK_WIDGET_CLASS (gtk_entry_parent_class)->key_release_event (widget, event);
1996 gtk_entry_focus_in (GtkWidget *widget,
1997 GdkEventFocus *event)
1999 GtkEntry *entry = GTK_ENTRY (widget);
2001 gtk_widget_queue_draw (widget);
2003 if (entry->editable)
2005 entry->need_im_reset = TRUE;
2006 gtk_im_context_focus_in (entry->im_context);
2009 g_signal_connect (gdk_keymap_get_for_display (gtk_widget_get_display (widget)),
2010 "direction_changed",
2011 G_CALLBACK (gtk_entry_keymap_direction_changed), entry);
2013 gtk_entry_check_cursor_blink (entry);
2019 gtk_entry_focus_out (GtkWidget *widget,
2020 GdkEventFocus *event)
2022 GtkEntry *entry = GTK_ENTRY (widget);
2023 GtkEntryCompletion *completion;
2025 gtk_widget_queue_draw (widget);
2027 if (entry->editable)
2029 entry->need_im_reset = TRUE;
2030 gtk_im_context_focus_out (entry->im_context);
2033 gtk_entry_check_cursor_blink (entry);
2035 g_signal_handlers_disconnect_by_func (gdk_keymap_get_for_display (gtk_widget_get_display (widget)),
2036 gtk_entry_keymap_direction_changed,
2039 completion = gtk_entry_get_completion (entry);
2041 _gtk_entry_completion_popdown (completion);
2047 gtk_entry_grab_focus (GtkWidget *widget)
2049 GtkEntry *entry = GTK_ENTRY (widget);
2050 gboolean select_on_focus;
2052 GTK_WIDGET_CLASS (gtk_entry_parent_class)->grab_focus (widget);
2054 g_object_get (gtk_widget_get_settings (widget),
2055 "gtk-entry-select-on-focus",
2059 if (select_on_focus && entry->editable && !entry->in_click)
2060 gtk_editable_select_region (GTK_EDITABLE (widget), 0, -1);
2064 gtk_entry_direction_changed (GtkWidget *widget,
2065 GtkTextDirection previous_dir)
2067 GtkEntry *entry = GTK_ENTRY (widget);
2069 gtk_entry_recompute (entry);
2071 GTK_WIDGET_CLASS (gtk_entry_parent_class)->direction_changed (widget, previous_dir);
2075 gtk_entry_state_changed (GtkWidget *widget,
2076 GtkStateType previous_state)
2078 GtkEntry *entry = GTK_ENTRY (widget);
2080 if (GTK_WIDGET_REALIZED (widget))
2082 gdk_window_set_background (widget->window, &widget->style->base[GTK_WIDGET_STATE (widget)]);
2083 gdk_window_set_background (entry->text_area, &widget->style->base[GTK_WIDGET_STATE (widget)]);
2086 if (!GTK_WIDGET_IS_SENSITIVE (widget))
2088 /* Clear any selection */
2089 gtk_editable_select_region (GTK_EDITABLE (entry), entry->current_pos, entry->current_pos);
2092 gtk_widget_queue_draw (widget);
2096 gtk_entry_screen_changed (GtkWidget *widget,
2097 GdkScreen *old_screen)
2099 gtk_entry_recompute (GTK_ENTRY (widget));
2102 /* GtkEditable method implementations
2105 gtk_entry_insert_text (GtkEditable *editable,
2106 const gchar *new_text,
2107 gint new_text_length,
2110 GtkEntry *entry = GTK_ENTRY (editable);
2114 if (*position < 0 || *position > entry->text_length)
2115 *position = entry->text_length;
2117 g_object_ref (editable);
2119 if (new_text_length <= 63)
2122 text = g_new (gchar, new_text_length + 1);
2124 text[new_text_length] = '\0';
2125 strncpy (text, new_text, new_text_length);
2127 g_signal_emit_by_name (editable, "insert_text", text, new_text_length, position);
2129 if (!entry->visible)
2130 trash_area (text, new_text_length);
2132 if (new_text_length > 63)
2135 g_object_unref (editable);
2139 gtk_entry_delete_text (GtkEditable *editable,
2143 GtkEntry *entry = GTK_ENTRY (editable);
2145 if (end_pos < 0 || end_pos > entry->text_length)
2146 end_pos = entry->text_length;
2149 if (start_pos > end_pos)
2150 start_pos = end_pos;
2152 g_object_ref (editable);
2154 g_signal_emit_by_name (editable, "delete_text", start_pos, end_pos);
2156 g_object_unref (editable);
2160 gtk_entry_get_chars (GtkEditable *editable,
2164 GtkEntry *entry = GTK_ENTRY (editable);
2165 gint start_index, end_index;
2168 end_pos = entry->text_length;
2170 start_pos = MIN (entry->text_length, start_pos);
2171 end_pos = MIN (entry->text_length, end_pos);
2173 start_index = g_utf8_offset_to_pointer (entry->text, start_pos) - entry->text;
2174 end_index = g_utf8_offset_to_pointer (entry->text, end_pos) - entry->text;
2176 return g_strndup (entry->text + start_index, end_index - start_index);
2180 gtk_entry_set_position_internal (GtkEntry *entry,
2184 if (position < 0 || position > entry->text_length)
2185 position = entry->text_length;
2187 if (position != entry->current_pos ||
2188 position != entry->selection_bound)
2191 _gtk_entry_reset_im_context (entry);
2192 gtk_entry_set_positions (entry, position, position);
2197 gtk_entry_real_set_position (GtkEditable *editable,
2200 gtk_entry_set_position_internal (GTK_ENTRY (editable), position, TRUE);
2204 gtk_entry_get_position (GtkEditable *editable)
2206 return GTK_ENTRY (editable)->current_pos;
2210 gtk_entry_set_selection_bounds (GtkEditable *editable,
2214 GtkEntry *entry = GTK_ENTRY (editable);
2217 start = entry->text_length;
2219 end = entry->text_length;
2221 _gtk_entry_reset_im_context (entry);
2223 gtk_entry_set_positions (entry,
2224 MIN (end, entry->text_length),
2225 MIN (start, entry->text_length));
2227 gtk_entry_update_primary_selection (entry);
2231 gtk_entry_get_selection_bounds (GtkEditable *editable,
2235 GtkEntry *entry = GTK_ENTRY (editable);
2237 *start = entry->selection_bound;
2238 *end = entry->current_pos;
2240 return (entry->selection_bound != entry->current_pos);
2244 gtk_entry_style_set (GtkWidget *widget,
2245 GtkStyle *previous_style)
2247 GtkEntry *entry = GTK_ENTRY (widget);
2249 gtk_entry_recompute (entry);
2251 if (previous_style && GTK_WIDGET_REALIZED (widget))
2253 gdk_window_set_background (widget->window, &widget->style->base[GTK_WIDGET_STATE (widget)]);
2254 gdk_window_set_background (entry->text_area, &widget->style->base[GTK_WIDGET_STATE (widget)]);
2258 /* GtkCellEditable method implementations
2261 gtk_cell_editable_entry_activated (GtkEntry *entry, gpointer data)
2263 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (entry));
2264 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (entry));
2268 gtk_cell_editable_key_press_event (GtkEntry *entry,
2269 GdkEventKey *key_event,
2272 if (key_event->keyval == GDK_Escape)
2274 entry->editing_canceled = TRUE;
2275 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (entry));
2276 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (entry));
2281 /* override focus */
2282 if (key_event->keyval == GDK_Up || key_event->keyval == GDK_Down)
2284 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (entry));
2285 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (entry));
2294 gtk_entry_start_editing (GtkCellEditable *cell_editable,
2297 GTK_ENTRY (cell_editable)->is_cell_renderer = TRUE;
2299 g_signal_connect (cell_editable, "activate",
2300 G_CALLBACK (gtk_cell_editable_entry_activated), NULL);
2301 g_signal_connect (cell_editable, "key_press_event",
2302 G_CALLBACK (gtk_cell_editable_key_press_event), NULL);
2306 gtk_entry_password_hint_free (GtkEntryPasswordHint *password_hint)
2308 if (password_hint->password_hint_timeout_id)
2309 g_source_remove (password_hint->password_hint_timeout_id);
2311 g_free (password_hint);
2314 /* Default signal handlers
2317 gtk_entry_real_insert_text (GtkEditable *editable,
2318 const gchar *new_text,
2319 gint new_text_length,
2322 GtkEntry *entry = GTK_ENTRY (editable);
2325 guint password_hint_timeout;
2327 if (new_text_length < 0)
2328 new_text_length = strlen (new_text);
2330 n_chars = g_utf8_strlen (new_text, new_text_length);
2331 if (entry->text_max_length > 0 && n_chars + entry->text_length > entry->text_max_length)
2333 gdk_display_beep (gtk_widget_get_display (GTK_WIDGET (entry)));
2334 n_chars = entry->text_max_length - entry->text_length;
2335 new_text_length = g_utf8_offset_to_pointer (new_text, n_chars) - new_text;
2338 if (new_text_length + entry->n_bytes + 1 > entry->text_size)
2340 gsize prev_size = entry->text_size;
2342 while (new_text_length + entry->n_bytes + 1 > entry->text_size)
2344 if (entry->text_size == 0)
2345 entry->text_size = MIN_SIZE;
2348 if (2 * (guint)entry->text_size < MAX_SIZE &&
2349 2 * (guint)entry->text_size > entry->text_size)
2350 entry->text_size *= 2;
2353 entry->text_size = MAX_SIZE;
2354 if (new_text_length > (gint)entry->text_size - (gint)entry->n_bytes - 1)
2356 new_text_length = (gint)entry->text_size - (gint)entry->n_bytes - 1;
2357 new_text_length = g_utf8_find_prev_char (new_text, new_text + new_text_length + 1) - new_text;
2358 n_chars = g_utf8_strlen (new_text, new_text_length);
2366 entry->text = g_realloc (entry->text, entry->text_size);
2369 /* Same thing, just slower and without leaving stuff in memory. */
2370 gchar *et_new = g_malloc (entry->text_size);
2371 memcpy (et_new, entry->text, MIN (prev_size, entry->text_size));
2372 trash_area (entry->text, prev_size);
2373 g_free (entry->text);
2374 entry->text = et_new;
2378 index = g_utf8_offset_to_pointer (entry->text, *position) - entry->text;
2380 g_memmove (entry->text + index + new_text_length, entry->text + index, entry->n_bytes - index);
2381 memcpy (entry->text + index, new_text, new_text_length);
2383 entry->n_bytes += new_text_length;
2384 entry->text_length += n_chars;
2386 /* NUL terminate for safety and convenience */
2387 entry->text[entry->n_bytes] = '\0';
2389 if (entry->current_pos > *position)
2390 entry->current_pos += n_chars;
2392 if (entry->selection_bound > *position)
2393 entry->selection_bound += n_chars;
2395 g_object_get (gtk_widget_get_settings (GTK_WIDGET (entry)),
2396 "gtk-entry-password-hint-timeout", &password_hint_timeout,
2399 if (password_hint_timeout > 0 && n_chars == 1 && !entry->visible &&
2400 (new_text_length < PASSWORD_HINT_MAX))
2402 GtkEntryPasswordHint *password_hint = g_object_get_qdata (G_OBJECT (entry),
2403 quark_password_hint);
2405 if (! password_hint)
2407 password_hint = g_new0 (GtkEntryPasswordHint, 1);
2408 g_object_set_qdata_full (G_OBJECT (entry), quark_password_hint,
2410 (GDestroyNotify) gtk_entry_password_hint_free);
2413 memset (&password_hint->password_hint, 0x0, PASSWORD_HINT_MAX);
2414 password_hint->password_hint_length = new_text_length;
2415 memcpy (&password_hint->password_hint, new_text, new_text_length);
2416 password_hint->password_hint_position = *position + n_chars;
2420 g_object_set_qdata (G_OBJECT (entry), quark_password_hint, NULL);
2423 *position += n_chars;
2425 gtk_entry_recompute (entry);
2427 g_signal_emit_by_name (editable, "changed");
2428 g_object_notify (G_OBJECT (editable), "text");
2432 gtk_entry_real_delete_text (GtkEditable *editable,
2436 GtkEntry *entry = GTK_ENTRY (editable);
2440 if (end_pos < 0 || end_pos > entry->text_length)
2441 end_pos = entry->text_length;
2443 if (start_pos < end_pos)
2445 gint start_index = g_utf8_offset_to_pointer (entry->text, start_pos) - entry->text;
2446 gint end_index = g_utf8_offset_to_pointer (entry->text, end_pos) - entry->text;
2448 gint selection_bound;
2450 g_memmove (entry->text + start_index, entry->text + end_index, entry->n_bytes + 1 - end_index);
2451 entry->text_length -= (end_pos - start_pos);
2452 entry->n_bytes -= (end_index - start_index);
2454 /* In password-mode, make sure we don't leave anything sensitive after
2455 * the terminating zero. Note, that the terminating zero already trashed
2458 if (!entry->visible)
2459 trash_area (entry->text + entry->n_bytes + 1, end_index - start_index - 1);
2461 current_pos = entry->current_pos;
2462 if (current_pos > start_pos)
2463 current_pos -= MIN (current_pos, end_pos) - start_pos;
2465 selection_bound = entry->selection_bound;
2466 if (selection_bound > start_pos)
2467 selection_bound -= MIN (selection_bound, end_pos) - start_pos;
2469 gtk_entry_set_positions (entry, current_pos, selection_bound);
2471 /* We might have deleted the selection
2473 gtk_entry_update_primary_selection (entry);
2475 gtk_entry_recompute (entry);
2477 g_signal_emit_by_name (editable, "changed");
2478 g_object_notify (G_OBJECT (editable), "text");
2482 /* Compute the X position for an offset that corresponds to the "more important
2483 * cursor position for that offset. We use this when trying to guess to which
2484 * end of the selection we should go to when the user hits the left or
2488 get_better_cursor_x (GtkEntry *entry,
2491 GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (entry)));
2492 PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
2493 gboolean split_cursor;
2495 PangoLayout *layout = gtk_entry_ensure_layout (entry, TRUE);
2496 const gchar *text = pango_layout_get_text (layout);
2497 gint index = g_utf8_offset_to_pointer (text, offset) - text;
2499 PangoRectangle strong_pos, weak_pos;
2501 g_object_get (gtk_widget_get_settings (GTK_WIDGET (entry)),
2502 "gtk-split-cursor", &split_cursor,
2505 pango_layout_get_cursor_pos (layout, index, &strong_pos, &weak_pos);
2508 return strong_pos.x / PANGO_SCALE;
2510 return (keymap_direction == entry->resolved_dir) ? strong_pos.x / PANGO_SCALE : weak_pos.x / PANGO_SCALE;
2514 gtk_entry_move_cursor (GtkEntry *entry,
2515 GtkMovementStep step,
2517 gboolean extend_selection)
2519 gint new_pos = entry->current_pos;
2521 _gtk_entry_reset_im_context (entry);
2523 if (entry->current_pos != entry->selection_bound && !extend_selection)
2525 /* If we have a current selection and aren't extending it, move to the
2526 * start/or end of the selection as appropriate
2530 case GTK_MOVEMENT_VISUAL_POSITIONS:
2532 gint current_x = get_better_cursor_x (entry, entry->current_pos);
2533 gint bound_x = get_better_cursor_x (entry, entry->selection_bound);
2536 new_pos = current_x < bound_x ? entry->current_pos : entry->selection_bound;
2538 new_pos = current_x > bound_x ? entry->current_pos : entry->selection_bound;
2542 case GTK_MOVEMENT_LOGICAL_POSITIONS:
2543 case GTK_MOVEMENT_WORDS:
2545 new_pos = MIN (entry->current_pos, entry->selection_bound);
2547 new_pos = MAX (entry->current_pos, entry->selection_bound);
2549 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
2550 case GTK_MOVEMENT_PARAGRAPH_ENDS:
2551 case GTK_MOVEMENT_BUFFER_ENDS:
2552 new_pos = count < 0 ? 0 : entry->text_length;
2554 case GTK_MOVEMENT_DISPLAY_LINES:
2555 case GTK_MOVEMENT_PARAGRAPHS:
2556 case GTK_MOVEMENT_PAGES:
2557 case GTK_MOVEMENT_HORIZONTAL_PAGES:
2565 case GTK_MOVEMENT_LOGICAL_POSITIONS:
2566 new_pos = gtk_entry_move_logically (entry, new_pos, count);
2568 case GTK_MOVEMENT_VISUAL_POSITIONS:
2569 new_pos = gtk_entry_move_visually (entry, new_pos, count);
2571 case GTK_MOVEMENT_WORDS:
2574 new_pos = gtk_entry_move_forward_word (entry, new_pos, FALSE);
2579 new_pos = gtk_entry_move_backward_word (entry, new_pos, FALSE);
2583 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
2584 case GTK_MOVEMENT_PARAGRAPH_ENDS:
2585 case GTK_MOVEMENT_BUFFER_ENDS:
2586 new_pos = count < 0 ? 0 : entry->text_length;
2588 case GTK_MOVEMENT_DISPLAY_LINES:
2589 case GTK_MOVEMENT_PARAGRAPHS:
2590 case GTK_MOVEMENT_PAGES:
2591 case GTK_MOVEMENT_HORIZONTAL_PAGES:
2596 if (extend_selection)
2597 gtk_editable_select_region (GTK_EDITABLE (entry), entry->selection_bound, new_pos);
2599 gtk_editable_set_position (GTK_EDITABLE (entry), new_pos);
2601 gtk_entry_pend_cursor_blink (entry);
2605 gtk_entry_insert_at_cursor (GtkEntry *entry,
2608 GtkEditable *editable = GTK_EDITABLE (entry);
2609 gint pos = entry->current_pos;
2611 if (entry->editable)
2613 _gtk_entry_reset_im_context (entry);
2615 gtk_editable_insert_text (editable, str, -1, &pos);
2616 gtk_editable_set_position (editable, pos);
2621 gtk_entry_delete_from_cursor (GtkEntry *entry,
2625 GtkEditable *editable = GTK_EDITABLE (entry);
2626 gint start_pos = entry->current_pos;
2627 gint end_pos = entry->current_pos;
2629 _gtk_entry_reset_im_context (entry);
2631 if (!entry->editable)
2634 if (entry->selection_bound != entry->current_pos)
2636 gtk_editable_delete_selection (editable);
2642 case GTK_DELETE_CHARS:
2643 end_pos = gtk_entry_move_logically (entry, entry->current_pos, count);
2644 gtk_editable_delete_text (editable, MIN (start_pos, end_pos), MAX (start_pos, end_pos));
2646 case GTK_DELETE_WORDS:
2649 /* Move to end of current word, or if not on a word, end of previous word */
2650 end_pos = gtk_entry_move_backward_word (entry, end_pos, FALSE);
2651 end_pos = gtk_entry_move_forward_word (entry, end_pos, FALSE);
2655 /* Move to beginning of current word, or if not on a word, begining of next word */
2656 start_pos = gtk_entry_move_forward_word (entry, start_pos, FALSE);
2657 start_pos = gtk_entry_move_backward_word (entry, start_pos, FALSE);
2661 case GTK_DELETE_WORD_ENDS:
2664 start_pos = gtk_entry_move_backward_word (entry, start_pos, FALSE);
2669 end_pos = gtk_entry_move_forward_word (entry, end_pos, FALSE);
2672 gtk_editable_delete_text (editable, start_pos, end_pos);
2674 case GTK_DELETE_DISPLAY_LINE_ENDS:
2675 case GTK_DELETE_PARAGRAPH_ENDS:
2677 gtk_editable_delete_text (editable, 0, entry->current_pos);
2679 gtk_editable_delete_text (editable, entry->current_pos, -1);
2681 case GTK_DELETE_DISPLAY_LINES:
2682 case GTK_DELETE_PARAGRAPHS:
2683 gtk_editable_delete_text (editable, 0, -1);
2685 case GTK_DELETE_WHITESPACE:
2686 gtk_entry_delete_whitespace (entry);
2690 gtk_entry_pend_cursor_blink (entry);
2694 gtk_entry_backspace (GtkEntry *entry)
2696 GtkEditable *editable = GTK_EDITABLE (entry);
2699 _gtk_entry_reset_im_context (entry);
2701 if (!entry->editable || !entry->text)
2704 if (entry->selection_bound != entry->current_pos)
2706 gtk_editable_delete_selection (editable);
2710 prev_pos = gtk_entry_move_logically(entry, entry->current_pos, -1);
2712 if (prev_pos < entry->current_pos)
2714 PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
2715 PangoLogAttr *log_attrs;
2718 pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
2720 if (log_attrs[entry->current_pos].backspace_deletes_character)
2722 gchar *cluster_text;
2723 gchar *normalized_text;
2726 cluster_text = gtk_editable_get_chars (editable,
2728 entry->current_pos);
2729 normalized_text = g_utf8_normalize (cluster_text,
2730 strlen (cluster_text),
2732 len = g_utf8_strlen (normalized_text, -1);
2734 gtk_editable_delete_text (editable, prev_pos, entry->current_pos);
2737 gint pos = entry->current_pos;
2739 gtk_editable_insert_text (editable, normalized_text,
2740 g_utf8_offset_to_pointer (normalized_text, len - 1) - normalized_text,
2742 gtk_editable_set_position (editable, pos);
2745 g_free (normalized_text);
2746 g_free (cluster_text);
2750 gtk_editable_delete_text (editable, prev_pos, entry->current_pos);
2756 gtk_entry_pend_cursor_blink (entry);
2760 gtk_entry_copy_clipboard (GtkEntry *entry)
2762 GtkEditable *editable = GTK_EDITABLE (entry);
2765 if (gtk_editable_get_selection_bounds (editable, &start, &end))
2767 gchar *str = gtk_entry_get_public_chars (entry, start, end);
2768 gtk_clipboard_set_text (gtk_widget_get_clipboard (GTK_WIDGET (entry),
2769 GDK_SELECTION_CLIPBOARD),
2776 gtk_entry_cut_clipboard (GtkEntry *entry)
2778 GtkEditable *editable = GTK_EDITABLE (entry);
2781 gtk_entry_copy_clipboard (entry);
2783 if (entry->editable)
2785 if (gtk_editable_get_selection_bounds (editable, &start, &end))
2786 gtk_editable_delete_text (editable, start, end);
2791 gtk_entry_paste_clipboard (GtkEntry *entry)
2793 if (entry->editable)
2794 gtk_entry_paste (entry, GDK_NONE);
2798 gtk_entry_delete_cb (GtkEntry *entry)
2800 GtkEditable *editable = GTK_EDITABLE (entry);
2803 if (entry->editable)
2805 if (gtk_editable_get_selection_bounds (editable, &start, &end))
2806 gtk_editable_delete_text (editable, start, end);
2811 gtk_entry_toggle_overwrite (GtkEntry *entry)
2813 entry->overwrite_mode = !entry->overwrite_mode;
2817 gtk_entry_select_all (GtkEntry *entry)
2819 gtk_entry_select_line (entry);
2823 gtk_entry_real_activate (GtkEntry *entry)
2826 GtkWidget *toplevel;
2829 widget = GTK_WIDGET (entry);
2831 if (entry->activates_default)
2833 toplevel = gtk_widget_get_toplevel (widget);
2834 if (GTK_IS_WINDOW (toplevel))
2836 window = GTK_WINDOW (toplevel);
2839 widget != window->default_widget &&
2840 !(widget == window->focus_widget &&
2841 (!window->default_widget || !GTK_WIDGET_SENSITIVE (window->default_widget))))
2842 gtk_window_activate_default (window);
2848 gtk_entry_keymap_direction_changed (GdkKeymap *keymap,
2851 gtk_entry_recompute (entry);
2854 /* IM Context Callbacks
2858 gtk_entry_commit_cb (GtkIMContext *context,
2862 if (entry->editable)
2863 gtk_entry_enter_text (entry, str);
2867 gtk_entry_preedit_changed_cb (GtkIMContext *context,
2870 if (entry->editable)
2872 gchar *preedit_string;
2875 gtk_im_context_get_preedit_string (entry->im_context,
2876 &preedit_string, NULL,
2878 entry->preedit_length = strlen (preedit_string);
2879 cursor_pos = CLAMP (cursor_pos, 0, g_utf8_strlen (preedit_string, -1));
2880 entry->preedit_cursor = cursor_pos;
2881 g_free (preedit_string);
2883 gtk_entry_recompute (entry);
2888 gtk_entry_retrieve_surrounding_cb (GtkIMContext *context,
2891 gtk_im_context_set_surrounding (context,
2894 g_utf8_offset_to_pointer (entry->text, entry->current_pos) - entry->text);
2900 gtk_entry_delete_surrounding_cb (GtkIMContext *slave,
2905 if (entry->editable)
2906 gtk_editable_delete_text (GTK_EDITABLE (entry),
2907 entry->current_pos + offset,
2908 entry->current_pos + offset + n_chars);
2913 /* Internal functions
2916 /* Used for im_commit_cb and inserting Unicode chars */
2918 gtk_entry_enter_text (GtkEntry *entry,
2921 GtkEditable *editable = GTK_EDITABLE (entry);
2924 if (gtk_editable_get_selection_bounds (editable, NULL, NULL))
2925 gtk_editable_delete_selection (editable);
2928 if (entry->overwrite_mode)
2929 gtk_entry_delete_from_cursor (entry, GTK_DELETE_CHARS, 1);
2932 tmp_pos = entry->current_pos;
2933 gtk_editable_insert_text (editable, str, strlen (str), &tmp_pos);
2934 gtk_entry_set_position_internal (entry, tmp_pos, FALSE);
2937 /* All changes to entry->current_pos and entry->selection_bound
2938 * should go through this function.
2941 gtk_entry_set_positions (GtkEntry *entry,
2943 gint selection_bound)
2945 gboolean changed = FALSE;
2947 g_object_freeze_notify (G_OBJECT (entry));
2949 if (current_pos != -1 &&
2950 entry->current_pos != current_pos)
2952 entry->current_pos = current_pos;
2955 g_object_notify (G_OBJECT (entry), "cursor-position");
2958 if (selection_bound != -1 &&
2959 entry->selection_bound != selection_bound)
2961 entry->selection_bound = selection_bound;
2964 g_object_notify (G_OBJECT (entry), "selection-bound");
2967 g_object_thaw_notify (G_OBJECT (entry));
2970 gtk_entry_recompute (entry);
2974 gtk_entry_reset_layout (GtkEntry *entry)
2976 if (entry->cached_layout)
2978 g_object_unref (entry->cached_layout);
2979 entry->cached_layout = NULL;
2984 update_im_cursor_location (GtkEntry *entry)
2988 gint strong_xoffset;
2989 gint area_width, area_height;
2991 gtk_entry_get_cursor_locations (entry, CURSOR_STANDARD, &strong_x, NULL)
2993 get_text_area_size (entry, NULL, NULL, &area_width, &area_height);
2995 strong_xoffset = strong_x - entry->scroll_offset;
2996 if (strong_xoffset < 0)
3000 else if (strong_xoffset > area_width)
3002 strong_xoffset = area_width;
3004 area.x = strong_xoffset;
3007 area.height = area_height;
3009 gtk_im_context_set_cursor_location (entry->im_context, &area);
3013 recompute_idle_func (gpointer data)
3017 GDK_THREADS_ENTER ();
3019 entry = GTK_ENTRY (data);
3021 entry->recompute_idle = 0;
3023 if (gtk_widget_has_screen (GTK_WIDGET (entry)))
3025 gtk_entry_adjust_scroll (entry);
3026 gtk_entry_queue_draw (entry);
3028 update_im_cursor_location (entry);
3031 GDK_THREADS_LEAVE ();
3037 gtk_entry_recompute (GtkEntry *entry)
3039 gtk_entry_reset_layout (entry);
3040 gtk_entry_check_cursor_blink (entry);
3042 if (!entry->recompute_idle)
3044 entry->recompute_idle = g_idle_add_full (G_PRIORITY_HIGH_IDLE + 15, /* between resize and redraw */
3045 recompute_idle_func, entry, NULL);
3050 append_char (GString *str,
3058 char_len = g_unichar_to_utf8 (ch, buf);
3063 g_string_append_len (str, buf, char_len);
3069 gtk_entry_remove_password_hint (gpointer data)
3071 GDK_THREADS_ENTER();
3073 /* Force the string to be redrawn, but now without a visible character */
3074 gtk_entry_recompute (GTK_ENTRY (data));
3076 GDK_THREADS_LEAVE();
3081 static PangoLayout *
3082 gtk_entry_create_layout (GtkEntry *entry,
3083 gboolean include_preedit)
3085 GtkWidget *widget = GTK_WIDGET (entry);
3086 PangoLayout *layout = gtk_widget_create_pango_layout (widget, NULL);
3087 PangoAttrList *tmp_attrs = pango_attr_list_new ();
3089 gchar *preedit_string = NULL;
3090 gint preedit_length = 0;
3091 PangoAttrList *preedit_attrs = NULL;
3093 pango_layout_set_single_paragraph_mode (layout, TRUE);
3095 if (include_preedit)
3097 gtk_im_context_get_preedit_string (entry->im_context,
3098 &preedit_string, &preedit_attrs, NULL);
3099 preedit_length = entry->preedit_length;
3104 GString *tmp_string = g_string_new (NULL);
3106 gint cursor_index = g_utf8_offset_to_pointer (entry->text, entry->current_pos) - entry->text;
3110 g_string_prepend_len (tmp_string, entry->text, entry->n_bytes);
3111 g_string_insert (tmp_string, cursor_index, preedit_string);
3116 gint preedit_len_chars;
3117 gunichar invisible_char;
3119 ch_len = g_utf8_strlen (entry->text, entry->n_bytes);
3120 preedit_len_chars = g_utf8_strlen (preedit_string, -1);
3121 ch_len += preedit_len_chars;
3123 if (entry->invisible_char != 0)
3124 invisible_char = entry->invisible_char;
3126 invisible_char = ' '; /* just pick a char */
3128 append_char (tmp_string, invisible_char, ch_len);
3130 /* Fix cursor index to point to invisible char corresponding
3131 * to the preedit, fix preedit_length to be the length of
3132 * the invisible chars representing the preedit
3135 g_utf8_offset_to_pointer (tmp_string->str, entry->current_pos) -
3139 g_unichar_to_utf8 (invisible_char, NULL);
3142 pango_layout_set_text (layout, tmp_string->str, tmp_string->len);
3144 pango_attr_list_splice (tmp_attrs, preedit_attrs,
3145 cursor_index, preedit_length);
3147 g_string_free (tmp_string, TRUE);
3151 PangoDirection pango_dir;
3154 pango_dir = pango_find_base_dir (entry->text, entry->n_bytes);
3157 pango_dir = PANGO_DIRECTION_NEUTRAL;
3159 if (pango_dir == PANGO_DIRECTION_NEUTRAL)
3161 if (GTK_WIDGET_HAS_FOCUS (widget))
3163 GdkDisplay *display = gtk_widget_get_display (widget);
3164 GdkKeymap *keymap = gdk_keymap_get_for_display (display);
3165 if (gdk_keymap_get_direction (keymap) == PANGO_DIRECTION_RTL)
3166 pango_dir = PANGO_DIRECTION_RTL;
3168 pango_dir = PANGO_DIRECTION_LTR;
3172 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
3173 pango_dir = PANGO_DIRECTION_RTL;
3175 pango_dir = PANGO_DIRECTION_LTR;
3179 pango_context_set_base_dir (gtk_widget_get_pango_context (widget),
3182 pango_layout_set_alignment (layout, pango_dir);
3184 entry->resolved_dir = pango_dir;
3188 pango_layout_set_text (layout, entry->text, entry->n_bytes);
3192 GString *str = g_string_new (NULL);
3193 gunichar invisible_char;
3194 guint password_hint_timeout;
3195 GtkEntryPasswordHint *password_hint;
3197 g_object_get (gtk_widget_get_settings (widget),
3198 "gtk-entry-password-hint-timeout", &password_hint_timeout,
3201 if (entry->invisible_char != 0)
3202 invisible_char = entry->invisible_char;
3204 invisible_char = ' '; /* just pick a char */
3206 password_hint = g_object_get_qdata (G_OBJECT (entry),
3207 quark_password_hint);
3209 if (password_hint && password_hint->password_hint_timeout_id)
3211 g_source_remove (password_hint->password_hint_timeout_id);
3212 password_hint->password_hint_timeout_id = 0;
3215 if (password_hint_timeout == 0 || password_hint == NULL ||
3216 (password_hint && password_hint->password_hint_length == 0))
3218 append_char (str, invisible_char, entry->text_length);
3220 else if (password_hint)
3222 /* Draw hidden characters upto the inserted position,
3223 * then the real thing, pad up to full length
3225 if (password_hint->password_hint_position > 1)
3226 append_char (str, invisible_char,
3227 password_hint->password_hint_position - 1);
3229 g_string_append_len (str, password_hint->password_hint,
3230 password_hint->password_hint_length);
3232 if (password_hint->password_hint_position < entry->text_length)
3233 append_char (str, invisible_char,
3234 entry->text_length -
3235 password_hint->password_hint_position);
3237 /* Now remove this last inputted character, don't need
3240 memset (password_hint->password_hint, 0, PASSWORD_HINT_MAX);
3241 password_hint->password_hint_length = 0;
3243 password_hint->password_hint_timeout_id =
3244 g_timeout_add (password_hint_timeout,
3245 (GSourceFunc) gtk_entry_remove_password_hint,
3249 pango_layout_set_text (layout, str->str, str->len);
3250 g_string_free (str, TRUE);
3254 pango_layout_set_attributes (layout, tmp_attrs);
3257 g_free (preedit_string);
3259 pango_attr_list_unref (preedit_attrs);
3261 pango_attr_list_unref (tmp_attrs);
3266 static PangoLayout *
3267 gtk_entry_ensure_layout (GtkEntry *entry,
3268 gboolean include_preedit)
3270 if (entry->preedit_length > 0 &&
3271 !include_preedit != !entry->cache_includes_preedit)
3272 gtk_entry_reset_layout (entry);
3274 if (!entry->cached_layout)
3276 entry->cached_layout = gtk_entry_create_layout (entry, include_preedit);
3277 entry->cache_includes_preedit = include_preedit;
3280 return entry->cached_layout;
3284 get_layout_position (GtkEntry *entry,
3288 PangoLayout *layout;
3289 PangoRectangle logical_rect;
3290 gint area_width, area_height;
3291 GtkBorder inner_border;
3293 PangoLayoutLine *line;
3295 layout = gtk_entry_ensure_layout (entry, TRUE);
3297 get_text_area_size (entry, NULL, NULL, &area_width, &area_height);
3298 _gtk_entry_effective_inner_border (entry, &inner_border);
3300 area_height = PANGO_SCALE * (area_height - inner_border.top - inner_border.bottom);
3302 line = pango_layout_get_lines (layout)->data;
3303 pango_layout_line_get_extents (line, NULL, &logical_rect);
3305 /* Align primarily for locale's ascent/descent */
3306 y_pos = ((area_height - entry->ascent - entry->descent) / 2 +
3307 entry->ascent + logical_rect.y);
3309 /* Now see if we need to adjust to fit in actual drawn string */
3310 if (logical_rect.height > area_height)
3311 y_pos = (area_height - logical_rect.height) / 2;
3314 else if (y_pos + logical_rect.height > area_height)
3315 y_pos = area_height - logical_rect.height;
3317 y_pos = inner_border.top + y_pos / PANGO_SCALE;
3320 *x = inner_border.left - entry->scroll_offset;
3327 gtk_entry_draw_text (GtkEntry *entry)
3331 if (!entry->visible && entry->invisible_char == 0)
3334 if (GTK_WIDGET_DRAWABLE (entry))
3336 PangoLayout *layout = gtk_entry_ensure_layout (entry, TRUE);
3339 gint start_pos, end_pos;
3341 widget = GTK_WIDGET (entry);
3343 get_layout_position (entry, &x, &y);
3345 cr = gdk_cairo_create (entry->text_area);
3347 cairo_move_to (cr, x, y);
3348 gdk_cairo_set_source_color (cr, &widget->style->text [widget->state]);
3349 pango_cairo_show_layout (cr, layout);
3351 if (gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &start_pos, &end_pos))
3355 PangoRectangle logical_rect;
3356 GdkColor *selection_color, *text_color;
3357 GtkBorder inner_border;
3359 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
3360 gtk_entry_get_pixel_ranges (entry, &ranges, &n_ranges);
3362 if (GTK_WIDGET_HAS_FOCUS (entry))
3364 selection_color = &widget->style->base [GTK_STATE_SELECTED];
3365 text_color = &widget->style->text [GTK_STATE_SELECTED];
3369 selection_color = &widget->style->base [GTK_STATE_ACTIVE];
3370 text_color = &widget->style->text [GTK_STATE_ACTIVE];
3373 _gtk_entry_effective_inner_border (entry, &inner_border);
3375 for (i = 0; i < n_ranges; ++i)
3376 cairo_rectangle (cr,
3377 inner_border.left - entry->scroll_offset + ranges[2 * i],
3380 logical_rect.height);
3384 gdk_cairo_set_source_color (cr, selection_color);
3387 cairo_move_to (cr, x, y);
3388 gdk_cairo_set_source_color (cr, text_color);
3389 pango_cairo_show_layout (cr, layout);
3399 draw_insertion_cursor (GtkEntry *entry,
3400 GdkRectangle *cursor_location,
3401 gboolean is_primary,
3402 PangoDirection direction,
3403 gboolean draw_arrow)
3405 GtkWidget *widget = GTK_WIDGET (entry);
3406 GtkTextDirection text_dir;
3408 if (direction == PANGO_DIRECTION_LTR)
3409 text_dir = GTK_TEXT_DIR_LTR;
3411 text_dir = GTK_TEXT_DIR_RTL;
3413 gtk_draw_insertion_cursor (widget, entry->text_area, NULL,
3415 is_primary, text_dir, draw_arrow);
3419 gtk_entry_draw_cursor (GtkEntry *entry,
3422 GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (entry)));
3423 PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
3425 if (GTK_WIDGET_DRAWABLE (entry))
3427 GtkWidget *widget = GTK_WIDGET (entry);
3428 GdkRectangle cursor_location;
3429 gboolean split_cursor;
3431 GtkBorder inner_border;
3433 gint strong_x, weak_x;
3434 gint text_area_height;
3435 PangoDirection dir1 = PANGO_DIRECTION_NEUTRAL;
3436 PangoDirection dir2 = PANGO_DIRECTION_NEUTRAL;
3440 _gtk_entry_effective_inner_border (entry, &inner_border);
3442 xoffset = inner_border.left - entry->scroll_offset;
3444 gdk_drawable_get_size (entry->text_area, NULL, &text_area_height);
3446 gtk_entry_get_cursor_locations (entry, type, &strong_x, &weak_x);
3448 g_object_get (gtk_widget_get_settings (widget),
3449 "gtk-split-cursor", &split_cursor,
3452 dir1 = entry->resolved_dir;
3458 if (weak_x != strong_x)
3460 dir2 = (entry->resolved_dir == PANGO_DIRECTION_LTR) ? PANGO_DIRECTION_RTL : PANGO_DIRECTION_LTR;
3466 if (keymap_direction == entry->resolved_dir)
3472 cursor_location.x = xoffset + x1;
3473 cursor_location.y = inner_border.top;
3474 cursor_location.width = 0;
3475 cursor_location.height = text_area_height - inner_border.top - inner_border.bottom;
3477 draw_insertion_cursor (entry,
3478 &cursor_location, TRUE, dir1,
3479 dir2 != PANGO_DIRECTION_NEUTRAL);
3481 if (dir2 != PANGO_DIRECTION_NEUTRAL)
3483 cursor_location.x = xoffset + x2;
3484 draw_insertion_cursor (entry,
3485 &cursor_location, FALSE, dir2,
3492 gtk_entry_queue_draw (GtkEntry *entry)
3494 if (GTK_WIDGET_REALIZED (entry))
3495 gdk_window_invalidate_rect (entry->text_area, NULL, FALSE);
3499 _gtk_entry_reset_im_context (GtkEntry *entry)
3501 if (entry->need_im_reset)
3503 entry->need_im_reset = 0;
3504 gtk_im_context_reset (entry->im_context);
3509 gtk_entry_find_position (GtkEntry *entry,
3512 PangoLayout *layout;
3513 PangoLayoutLine *line;
3520 layout = gtk_entry_ensure_layout (entry, TRUE);
3521 text = pango_layout_get_text (layout);
3522 cursor_index = g_utf8_offset_to_pointer (text, entry->current_pos) - text;
3524 line = pango_layout_get_lines (layout)->data;
3525 pango_layout_line_x_to_index (line, x * PANGO_SCALE, &index, &trailing);
3527 if (index >= cursor_index && entry->preedit_length)
3529 if (index >= cursor_index + entry->preedit_length)
3530 index -= entry->preedit_length;
3533 index = cursor_index;
3538 pos = g_utf8_pointer_to_offset (text, text + index);
3545 gtk_entry_get_cursor_locations (GtkEntry *entry,
3550 if (!entry->visible && !entry->invisible_char)
3560 PangoLayout *layout = gtk_entry_ensure_layout (entry, TRUE);
3561 const gchar *text = pango_layout_get_text (layout);
3562 PangoRectangle strong_pos, weak_pos;
3565 if (type == CURSOR_STANDARD)
3567 index = g_utf8_offset_to_pointer (text, entry->current_pos + entry->preedit_cursor) - text;
3569 else /* type == CURSOR_DND */
3571 index = g_utf8_offset_to_pointer (text, entry->dnd_position) - text;
3573 if (entry->dnd_position > entry->current_pos)
3576 index += entry->preedit_length;
3579 gint preedit_len_chars = g_utf8_strlen (text, -1) - entry->text_length;
3580 index += preedit_len_chars * g_unichar_to_utf8 (entry->invisible_char, NULL);
3585 pango_layout_get_cursor_pos (layout, index, &strong_pos, &weak_pos);
3588 *strong_x = strong_pos.x / PANGO_SCALE;
3591 *weak_x = weak_pos.x / PANGO_SCALE;
3596 gtk_entry_adjust_scroll (GtkEntry *entry)
3598 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
3599 gint min_offset, max_offset;
3600 gint text_area_width, text_width;
3601 GtkBorder inner_border;
3602 gint strong_x, weak_x;
3603 gint strong_xoffset, weak_xoffset;
3605 PangoLayout *layout;
3606 PangoLayoutLine *line;
3607 PangoRectangle logical_rect;
3609 if (!GTK_WIDGET_REALIZED (entry))
3612 _gtk_entry_effective_inner_border (entry, &inner_border);
3614 gdk_drawable_get_size (entry->text_area, &text_area_width, NULL);
3615 text_area_width -= inner_border.left + inner_border.right;
3616 if (text_area_width < 0)
3617 text_area_width = 0;
3619 layout = gtk_entry_ensure_layout (entry, TRUE);
3620 line = pango_layout_get_lines (layout)->data;
3622 pango_layout_line_get_extents (line, NULL, &logical_rect);
3624 /* Display as much text as we can */
3626 if (entry->resolved_dir == PANGO_DIRECTION_LTR)
3627 xalign = priv->xalign;
3629 xalign = 1.0 - priv->xalign;
3631 text_width = PANGO_PIXELS(logical_rect.width);
3633 if (text_width > text_area_width)
3636 max_offset = text_width - text_area_width;
3640 min_offset = (text_width - text_area_width) * xalign;
3641 max_offset = min_offset;
3644 entry->scroll_offset = CLAMP (entry->scroll_offset, min_offset, max_offset);
3646 /* And make sure cursors are on screen. Note that the cursor is
3647 * actually drawn one pixel into the INNER_BORDER space on
3648 * the right, when the scroll is at the utmost right. This
3649 * looks better to to me than confining the cursor inside the
3650 * border entirely, though it means that the cursor gets one
3651 * pixel closer to the edge of the widget on the right than
3652 * on the left. This might need changing if one changed
3653 * INNER_BORDER from 2 to 1, as one would do on a
3654 * small-screen-real-estate display.
3656 * We always make sure that the strong cursor is on screen, and
3657 * put the weak cursor on screen if possible.
3660 gtk_entry_get_cursor_locations (entry, CURSOR_STANDARD, &strong_x, &weak_x);
3662 strong_xoffset = strong_x - entry->scroll_offset;
3664 if (strong_xoffset < 0)
3666 entry->scroll_offset += strong_xoffset;
3669 else if (strong_xoffset > text_area_width)
3671 entry->scroll_offset += strong_xoffset - text_area_width;
3672 strong_xoffset = text_area_width;
3675 weak_xoffset = weak_x - entry->scroll_offset;
3677 if (weak_xoffset < 0 && strong_xoffset - weak_xoffset <= text_area_width)
3679 entry->scroll_offset += weak_xoffset;
3681 else if (weak_xoffset > text_area_width &&
3682 strong_xoffset - (weak_xoffset - text_area_width) >= 0)
3684 entry->scroll_offset += weak_xoffset - text_area_width;
3687 g_object_notify (G_OBJECT (entry), "scroll-offset");
3691 gtk_entry_move_visually (GtkEntry *entry,
3696 PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
3699 text = pango_layout_get_text (layout);
3701 index = g_utf8_offset_to_pointer (text, start) - text;
3705 int new_index, new_trailing;
3706 gboolean split_cursor;
3709 g_object_get (gtk_widget_get_settings (GTK_WIDGET (entry)),
3710 "gtk-split-cursor", &split_cursor,
3717 GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (entry)));
3718 PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
3720 strong = keymap_direction == entry->resolved_dir;
3725 pango_layout_move_cursor_visually (layout, strong, index, 0, 1, &new_index, &new_trailing);
3730 pango_layout_move_cursor_visually (layout, strong, index, 0, -1, &new_index, &new_trailing);
3736 else if (new_index != G_MAXINT)
3739 while (new_trailing--)
3740 index = g_utf8_next_char (text + index) - text;
3743 return g_utf8_pointer_to_offset (text, text + index);
3747 gtk_entry_move_logically (GtkEntry *entry,
3751 gint new_pos = start;
3753 /* Prevent any leak of information */
3754 if (!entry->visible)
3756 new_pos = CLAMP (start + count, 0, entry->text_length);
3758 else if (entry->text)
3760 PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
3761 PangoLogAttr *log_attrs;
3764 pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
3766 while (count > 0 && new_pos < entry->text_length)
3770 while (new_pos < entry->text_length && !log_attrs[new_pos].is_cursor_position);
3774 while (count < 0 && new_pos > 0)
3778 while (new_pos > 0 && !log_attrs[new_pos].is_cursor_position);
3790 gtk_entry_move_forward_word (GtkEntry *entry,
3792 gboolean allow_whitespace)
3794 gint new_pos = start;
3796 /* Prevent any leak of information */
3797 if (!entry->visible)
3799 new_pos = entry->text_length;
3801 else if (entry->text && (new_pos < entry->text_length))
3803 PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
3804 PangoLogAttr *log_attrs;
3807 pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
3809 /* Find the next word boundary */
3811 while (new_pos < n_attrs - 1 && !(log_attrs[new_pos].is_word_end ||
3812 (log_attrs[new_pos].is_word_start && allow_whitespace)))
3823 gtk_entry_move_backward_word (GtkEntry *entry,
3825 gboolean allow_whitespace)
3827 gint new_pos = start;
3829 /* Prevent any leak of information */
3830 if (!entry->visible)
3834 else if (entry->text && start > 0)
3836 PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
3837 PangoLogAttr *log_attrs;
3840 pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
3842 new_pos = start - 1;
3844 /* Find the previous word boundary */
3845 while (new_pos > 0 && !(log_attrs[new_pos].is_word_start ||
3846 (log_attrs[new_pos].is_word_end && allow_whitespace)))
3856 gtk_entry_delete_whitespace (GtkEntry *entry)
3858 PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
3859 PangoLogAttr *log_attrs;
3863 pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
3865 start = end = entry->current_pos;
3867 while (start > 0 && log_attrs[start-1].is_white)
3870 while (end < n_attrs && log_attrs[end].is_white)
3876 gtk_editable_delete_text (GTK_EDITABLE (entry), start, end);
3881 gtk_entry_select_word (GtkEntry *entry)
3883 gint start_pos = gtk_entry_move_backward_word (entry, entry->current_pos, TRUE);
3884 gint end_pos = gtk_entry_move_forward_word (entry, entry->current_pos, TRUE);
3886 gtk_editable_select_region (GTK_EDITABLE (entry), start_pos, end_pos);
3890 gtk_entry_select_line (GtkEntry *entry)
3892 gtk_editable_select_region (GTK_EDITABLE (entry), 0, -1);
3896 * Like gtk_editable_get_chars, but handle not-visible entries
3900 gtk_entry_get_public_chars (GtkEntry *entry,
3905 end = entry->text_length;
3908 return gtk_editable_get_chars (GTK_EDITABLE (entry), start, end);
3909 else if (!entry->invisible_char)
3910 return g_strdup ("");
3913 GString *str = g_string_new (NULL);
3914 append_char (str, entry->invisible_char, end - start);
3915 return g_string_free (str, FALSE);
3920 truncate_multiline (const gchar *text)
3925 text[length] && text[length] != '\n' && text[length] != '\r';
3932 paste_received (GtkClipboard *clipboard,
3936 GtkEntry *entry = GTK_ENTRY (data);
3937 GtkEditable *editable = GTK_EDITABLE (entry);
3938 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
3940 if (entry->button == 2)
3942 gint pos, start, end;
3943 pos = priv->insert_pos;
3944 gtk_editable_get_selection_bounds (editable, &start, &end);
3945 if (!((start <= pos && pos <= end) || (end <= pos && pos <= start)))
3946 gtk_editable_select_region (editable, pos, pos);
3951 gint pos, start, end;
3953 gboolean popup_completion;
3954 GtkEntryCompletion *completion;
3956 completion = gtk_entry_get_completion (entry);
3958 if (entry->truncate_multiline)
3959 length = truncate_multiline (text);
3961 /* only complete if the selection is at the end */
3962 popup_completion = (entry->text_length == MAX (entry->current_pos, entry->selection_bound));
3966 if (GTK_WIDGET_MAPPED (completion->priv->popup_window))
3967 _gtk_entry_completion_popdown (completion);
3969 if (!popup_completion && completion->priv->changed_id > 0)
3970 g_signal_handler_block (entry, completion->priv->changed_id);
3973 if (gtk_editable_get_selection_bounds (editable, &start, &end))
3974 gtk_editable_delete_text (editable, start, end);
3976 pos = entry->current_pos;
3977 gtk_editable_insert_text (editable, text, length, &pos);
3978 gtk_editable_set_position (editable, pos);
3981 !popup_completion && completion->priv->changed_id > 0)
3982 g_signal_handler_unblock (entry, completion->priv->changed_id);
3985 g_object_unref (entry);
3989 gtk_entry_paste (GtkEntry *entry,
3992 g_object_ref (entry);
3993 gtk_clipboard_request_text (gtk_widget_get_clipboard (GTK_WIDGET (entry), selection),
3994 paste_received, entry);
3998 primary_get_cb (GtkClipboard *clipboard,
3999 GtkSelectionData *selection_data,
4003 GtkEntry *entry = GTK_ENTRY (data);
4006 if (gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &start, &end))
4008 gchar *str = gtk_entry_get_public_chars (entry, start, end);
4009 gtk_selection_data_set_text (selection_data, str, -1);
4015 primary_clear_cb (GtkClipboard *clipboard,
4018 GtkEntry *entry = GTK_ENTRY (data);
4020 gtk_editable_select_region (GTK_EDITABLE (entry), entry->current_pos, entry->current_pos);
4024 gtk_entry_update_primary_selection (GtkEntry *entry)
4026 static GtkTargetEntry targets[] = {
4027 { "UTF8_STRING", 0, 0 },
4030 { "COMPOUND_TEXT", 0, 0 },
4031 { "text/plain;charset=utf-8", 0, 0 },
4033 { "text/plain", 0, 0 }
4036 GtkClipboard *clipboard;
4039 if (targets[5].target == NULL)
4041 const gchar *charset;
4043 g_get_charset (&charset);
4044 targets[5].target = g_strdup_printf ("text/plain;charset=%s", charset);
4047 if (!GTK_WIDGET_REALIZED (entry))
4050 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (entry), GDK_SELECTION_PRIMARY);
4052 if (gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &start, &end))
4054 if (!gtk_clipboard_set_with_owner (clipboard, targets, G_N_ELEMENTS (targets),
4055 primary_get_cb, primary_clear_cb, G_OBJECT (entry)))
4056 primary_clear_cb (clipboard, entry);
4060 if (gtk_clipboard_get_owner (clipboard) == G_OBJECT (entry))
4061 gtk_clipboard_clear (clipboard);
4071 * Creates a new entry.
4073 * Return value: a new #GtkEntry.
4076 gtk_entry_new (void)
4078 return g_object_new (GTK_TYPE_ENTRY, NULL);
4082 * gtk_entry_new_with_max_length:
4083 * @max: the maximum length of the entry, or 0 for no maximum.
4084 * (other than the maximum length of entries.) The value passed in will
4085 * be clamped to the range 0-65536.
4087 * Creates a new #GtkEntry widget with the given maximum length.
4089 * Note: the existence of this function is inconsistent
4090 * with the rest of the GTK+ API. The normal setup would
4091 * be to just require the user to make an extra call
4092 * to gtk_entry_set_max_length() instead. It is not
4093 * expected that this function will be removed, but
4094 * it would be better practice not to use it.
4096 * Return value: a new #GtkEntry.
4099 gtk_entry_new_with_max_length (gint max)
4103 max = CLAMP (max, 0, MAX_SIZE);
4105 entry = g_object_new (GTK_TYPE_ENTRY, NULL);
4106 entry->text_max_length = max;
4108 return GTK_WIDGET (entry);
4112 gtk_entry_set_text (GtkEntry *entry,
4116 GtkEntryCompletion *completion;
4118 g_return_if_fail (GTK_IS_ENTRY (entry));
4119 g_return_if_fail (text != NULL);
4121 /* Actually setting the text will affect the cursor and selection;
4122 * if the contents don't actually change, this will look odd to the user.
4124 if (strcmp (entry->text, text) == 0)
4127 completion = gtk_entry_get_completion (entry);
4129 g_signal_handler_block (entry, completion->priv->changed_id);
4131 gtk_editable_delete_text (GTK_EDITABLE (entry), 0, -1);
4134 gtk_editable_insert_text (GTK_EDITABLE (entry), text, strlen (text), &tmp_pos);
4137 g_signal_handler_unblock (entry, completion->priv->changed_id);
4141 gtk_entry_append_text (GtkEntry *entry,
4146 g_return_if_fail (GTK_IS_ENTRY (entry));
4147 g_return_if_fail (text != NULL);
4149 tmp_pos = entry->text_length;
4150 gtk_editable_insert_text (GTK_EDITABLE (entry), text, -1, &tmp_pos);
4154 gtk_entry_prepend_text (GtkEntry *entry,
4159 g_return_if_fail (GTK_IS_ENTRY (entry));
4160 g_return_if_fail (text != NULL);
4163 gtk_editable_insert_text (GTK_EDITABLE (entry), text, -1, &tmp_pos);
4167 gtk_entry_set_position (GtkEntry *entry,
4170 g_return_if_fail (GTK_IS_ENTRY (entry));
4172 gtk_editable_set_position (GTK_EDITABLE (entry), position);
4176 gtk_entry_set_visibility (GtkEntry *entry,
4179 g_return_if_fail (GTK_IS_ENTRY (entry));
4181 visible = visible != FALSE;
4183 if (entry->visible != visible)
4185 if (GTK_WIDGET_HAS_FOCUS (entry) && !visible)
4186 gtk_im_context_focus_out (entry->im_context);
4188 g_object_unref (entry->im_context);
4191 entry->im_context = gtk_im_multicontext_new ();
4193 entry->im_context = gtk_im_context_simple_new ();
4195 g_signal_connect (entry->im_context, "commit",
4196 G_CALLBACK (gtk_entry_commit_cb), entry);
4197 g_signal_connect (entry->im_context, "preedit_changed",
4198 G_CALLBACK (gtk_entry_preedit_changed_cb), entry);
4199 g_signal_connect (entry->im_context, "retrieve_surrounding",
4200 G_CALLBACK (gtk_entry_retrieve_surrounding_cb), entry);
4201 g_signal_connect (entry->im_context, "delete_surrounding",
4202 G_CALLBACK (gtk_entry_delete_surrounding_cb), entry);
4204 if (GTK_WIDGET_HAS_FOCUS (entry) && visible)
4205 gtk_im_context_focus_in (entry->im_context);
4207 entry->visible = visible;
4209 g_object_notify (G_OBJECT (entry), "visibility");
4210 gtk_entry_recompute (entry);
4215 * gtk_entry_get_visibility:
4216 * @entry: a #GtkEntry
4218 * Retrieves whether the text in @entry is visible. See
4219 * gtk_entry_set_visibility().
4221 * Return value: %TRUE if the text is currently visible
4224 gtk_entry_get_visibility (GtkEntry *entry)
4226 g_return_val_if_fail (GTK_IS_ENTRY (entry), FALSE);
4228 return entry->visible;
4232 * gtk_entry_set_invisible_char:
4233 * @entry: a #GtkEntry
4234 * @ch: a Unicode character
4236 * Sets the character to use in place of the actual text when
4237 * gtk_entry_set_visibility() has been called to set text visibility
4238 * to %FALSE. i.e. this is the character used in "password mode" to
4239 * show the user how many characters have been typed. The default
4240 * invisible char is an asterisk ('*'). If you set the invisible char
4241 * to 0, then the user will get no feedback at all; there will be
4242 * no text on the screen as they type.
4246 gtk_entry_set_invisible_char (GtkEntry *entry,
4249 g_return_if_fail (GTK_IS_ENTRY (entry));
4251 if (ch == entry->invisible_char)
4254 entry->invisible_char = ch;
4255 g_object_notify (G_OBJECT (entry), "invisible-char");
4256 gtk_entry_recompute (entry);
4260 * gtk_entry_get_invisible_char:
4261 * @entry: a #GtkEntry
4263 * Retrieves the character displayed in place of the real characters
4264 * for entries with visisbility set to false. See gtk_entry_set_invisible_char().
4266 * Return value: the current invisible char, or 0, if the entry does not
4267 * show invisible text at all.
4270 gtk_entry_get_invisible_char (GtkEntry *entry)
4272 g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
4274 return entry->invisible_char;
4278 gtk_entry_set_editable (GtkEntry *entry,
4281 g_return_if_fail (GTK_IS_ENTRY (entry));
4283 gtk_editable_set_editable (GTK_EDITABLE (entry), editable);
4287 * gtk_entry_get_text:
4288 * @entry: a #GtkEntry
4290 * Retrieves the contents of the entry widget.
4291 * See also gtk_editable_get_chars().
4293 * Return value: a pointer to the contents of the widget as a
4294 * string. This string points to internally allocated
4295 * storage in the widget and must not be freed, modified or
4298 G_CONST_RETURN gchar*
4299 gtk_entry_get_text (GtkEntry *entry)
4301 g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
4307 gtk_entry_select_region (GtkEntry *entry,
4311 gtk_editable_select_region (GTK_EDITABLE (entry), start, end);
4315 * gtk_entry_set_max_length:
4316 * @entry: a #GtkEntry.
4317 * @max: the maximum length of the entry, or 0 for no maximum.
4318 * (other than the maximum length of entries.) The value passed in will
4319 * be clamped to the range 0-65536.
4321 * Sets the maximum allowed length of the contents of the widget. If
4322 * the current contents are longer than the given length, then they
4323 * will be truncated to fit.
4326 gtk_entry_set_max_length (GtkEntry *entry,
4329 g_return_if_fail (GTK_IS_ENTRY (entry));
4331 max = CLAMP (max, 0, MAX_SIZE);
4333 if (max > 0 && entry->text_length > max)
4334 gtk_editable_delete_text (GTK_EDITABLE (entry), max, -1);
4336 entry->text_max_length = max;
4337 g_object_notify (G_OBJECT (entry), "max-length");
4341 * gtk_entry_get_max_length:
4342 * @entry: a #GtkEntry
4344 * Retrieves the maximum allowed length of the text in
4345 * @entry. See gtk_entry_set_max_length().
4347 * Return value: the maximum allowed number of characters
4348 * in #GtkEntry, or 0 if there is no maximum.
4351 gtk_entry_get_max_length (GtkEntry *entry)
4353 g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
4355 return entry->text_max_length;
4359 * gtk_entry_set_activates_default:
4360 * @entry: a #GtkEntry
4361 * @setting: %TRUE to activate window's default widget on Enter keypress
4363 * If @setting is %TRUE, pressing Enter in the @entry will activate the default
4364 * widget for the window containing the entry. This usually means that
4365 * the dialog box containing the entry will be closed, since the default
4366 * widget is usually one of the dialog buttons.
4368 * (For experts: if @setting is %TRUE, the entry calls
4369 * gtk_window_activate_default() on the window containing the entry, in
4370 * the default handler for the "activate" signal.)
4374 gtk_entry_set_activates_default (GtkEntry *entry,
4377 g_return_if_fail (GTK_IS_ENTRY (entry));
4378 setting = setting != FALSE;
4380 if (setting != entry->activates_default)
4382 entry->activates_default = setting;
4383 g_object_notify (G_OBJECT (entry), "activates-default");
4388 * gtk_entry_get_activates_default:
4389 * @entry: a #GtkEntry
4391 * Retrieves the value set by gtk_entry_set_activates_default().
4393 * Return value: %TRUE if the entry will activate the default widget
4396 gtk_entry_get_activates_default (GtkEntry *entry)
4398 g_return_val_if_fail (GTK_IS_ENTRY (entry), FALSE);
4400 return entry->activates_default;
4404 * gtk_entry_set_width_chars:
4405 * @entry: a #GtkEntry
4406 * @n_chars: width in chars
4408 * Changes the size request of the entry to be about the right size
4409 * for @n_chars characters. Note that it changes the size
4410 * <emphasis>request</emphasis>, the size can still be affected by
4411 * how you pack the widget into containers. If @n_chars is -1, the
4412 * size reverts to the default entry size.
4416 gtk_entry_set_width_chars (GtkEntry *entry,
4419 g_return_if_fail (GTK_IS_ENTRY (entry));
4421 if (entry->width_chars != n_chars)
4423 entry->width_chars = n_chars;
4424 g_object_notify (G_OBJECT (entry), "width-chars");
4425 gtk_widget_queue_resize (GTK_WIDGET (entry));
4430 * gtk_entry_get_width_chars:
4431 * @entry: a #GtkEntry
4433 * Gets the value set by gtk_entry_set_width_chars().
4435 * Return value: number of chars to request space for, or negative if unset
4438 gtk_entry_get_width_chars (GtkEntry *entry)
4440 g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
4442 return entry->width_chars;
4446 * gtk_entry_set_has_frame:
4447 * @entry: a #GtkEntry
4448 * @setting: new value
4450 * Sets whether the entry has a beveled frame around it.
4453 gtk_entry_set_has_frame (GtkEntry *entry,
4456 g_return_if_fail (GTK_IS_ENTRY (entry));
4458 setting = (setting != FALSE);
4460 if (entry->has_frame == setting)
4463 gtk_widget_queue_resize (GTK_WIDGET (entry));
4464 entry->has_frame = setting;
4465 g_object_notify (G_OBJECT (entry), "has-frame");
4469 * gtk_entry_get_has_frame:
4470 * @entry: a #GtkEntry
4472 * Gets the value set by gtk_entry_set_has_frame().
4474 * Return value: whether the entry has a beveled frame
4477 gtk_entry_get_has_frame (GtkEntry *entry)
4479 g_return_val_if_fail (GTK_IS_ENTRY (entry), FALSE);
4481 return entry->has_frame;
4485 * gtk_entry_set_inner_border:
4486 * @entry: a #GtkEntry
4487 * @border: a #GtkBorder, or %NULL
4489 * Sets %entry's inner-border property to %border, or clears it if %NULL
4490 * is passed. The inner-border is the area around the entry's text, but
4493 * If set, this property overrides the inner-border style property.
4494 * Overriding the style-provided border is useful when you want to do
4495 * in-place editing of some text in a canvas or list widget, where
4496 * pixel-exact positioning of the entry is important.
4501 gtk_entry_set_inner_border (GtkEntry *entry,
4502 const GtkBorder *border)
4504 g_return_if_fail (GTK_IS_ENTRY (entry));
4506 gtk_widget_queue_resize (GTK_WIDGET (entry));
4509 g_object_set_qdata_full (G_OBJECT (entry), quark_inner_border,
4510 gtk_border_copy (border),
4511 (GDestroyNotify) gtk_border_free);
4513 g_object_set_qdata (G_OBJECT (entry), quark_inner_border, NULL);
4515 g_object_notify (G_OBJECT (entry), "inner-border");
4519 * gtk_entry_get_inner_border:
4520 * @entry: a #GtkEntry
4522 * This function returns the entry's inner-border property. See
4523 * gtk_entry_set_inner_border() for more information.
4525 * Return value: the entry's #GtkBorder, or %NULL if none was set.
4529 G_CONST_RETURN GtkBorder *
4530 gtk_entry_get_inner_border (GtkEntry *entry)
4532 g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
4534 return g_object_get_qdata (G_OBJECT (entry), quark_inner_border);
4538 * gtk_entry_get_layout:
4539 * @entry: a #GtkEntry
4541 * Gets the #PangoLayout used to display the entry.
4542 * The layout is useful to e.g. convert text positions to
4543 * pixel positions, in combination with gtk_entry_get_layout_offsets().
4544 * The returned layout is owned by the entry and must not be
4545 * modified or freed by the caller.
4547 * Keep in mind that the layout text may contain a preedit string, so
4548 * gtk_entry_layout_index_to_text_index() and
4549 * gtk_entry_text_index_to_layout_index() are needed to convert byte
4550 * indices in the layout to byte indices in the entry contents.
4552 * Return value: the #PangoLayout for this entry
4555 gtk_entry_get_layout (GtkEntry *entry)
4557 PangoLayout *layout;
4559 g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
4561 layout = gtk_entry_ensure_layout (entry, TRUE);
4568 * gtk_entry_layout_index_to_text_index:
4569 * @entry: a #GtkEntry
4570 * @layout_index: byte index into the entry layout text
4572 * Converts from a position in the entry contents (returned
4573 * by gtk_entry_get_text()) to a position in the
4574 * entry's #PangoLayout (returned by gtk_entry_get_layout(),
4575 * with text retrieved via pango_layout_get_text()).
4577 * Return value: byte index into the entry contents
4580 gtk_entry_layout_index_to_text_index (GtkEntry *entry,
4583 PangoLayout *layout;
4587 g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
4589 layout = gtk_entry_ensure_layout (entry, TRUE);
4590 text = pango_layout_get_text (layout);
4591 cursor_index = g_utf8_offset_to_pointer (text, entry->current_pos) - text;
4593 if (layout_index >= cursor_index && entry->preedit_length)
4595 if (layout_index >= cursor_index + entry->preedit_length)
4596 layout_index -= entry->preedit_length;
4598 layout_index = cursor_index;
4601 return layout_index;
4605 * gtk_entry_text_index_to_layout_index:
4606 * @entry: a #GtkEntry
4607 * @text_index: byte index into the entry contents
4609 * Converts from a position in the entry's #PangoLayout (returned by
4610 * gtk_entry_get_layout()) to a position in the entry contents
4611 * (returned by gtk_entry_get_text()).
4613 * Return value: byte index into the entry layout text
4616 gtk_entry_text_index_to_layout_index (GtkEntry *entry,
4619 PangoLayout *layout;
4622 g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
4624 layout = gtk_entry_ensure_layout (entry, TRUE);
4625 text = pango_layout_get_text (layout);
4626 cursor_index = g_utf8_offset_to_pointer (text, entry->current_pos) - text;
4628 if (text_index > cursor_index)
4629 text_index += entry->preedit_length;
4635 * gtk_entry_get_layout_offsets:
4636 * @entry: a #GtkEntry
4637 * @x: location to store X offset of layout, or %NULL
4638 * @y: location to store Y offset of layout, or %NULL
4641 * Obtains the position of the #PangoLayout used to render text
4642 * in the entry, in widget coordinates. Useful if you want to line
4643 * up the text in an entry with some other text, e.g. when using the
4644 * entry to implement editable cells in a sheet widget.
4646 * Also useful to convert mouse events into coordinates inside the
4647 * #PangoLayout, e.g. to take some action if some part of the entry text
4650 * Note that as the user scrolls around in the entry the offsets will
4651 * change; you'll need to connect to the "notify::scroll-offset"
4652 * signal to track this. Remember when using the #PangoLayout
4653 * functions you need to convert to and from pixels using
4654 * PANGO_PIXELS() or #PANGO_SCALE.
4656 * Keep in mind that the layout text may contain a preedit string, so
4657 * gtk_entry_layout_index_to_text_index() and
4658 * gtk_entry_text_index_to_layout_index() are needed to convert byte
4659 * indices in the layout to byte indices in the entry contents.
4663 gtk_entry_get_layout_offsets (GtkEntry *entry,
4667 gint text_area_x, text_area_y;
4669 g_return_if_fail (GTK_IS_ENTRY (entry));
4671 /* this gets coords relative to text area */
4672 get_layout_position (entry, x, y);
4674 /* convert to widget coords */
4675 get_text_area_size (entry, &text_area_x, &text_area_y, NULL, NULL);
4686 * gtk_entry_set_alignment:
4687 * @entry: a #GtkEntry
4688 * @xalign: The horizontal alignment, from 0 (left) to 1 (right).
4689 * Reversed for RTL layouts
4691 * Sets the alignment for the contents of the entry. This controls
4692 * the horizontal positioning of the contents when the displayed
4693 * text is shorter than the width of the entry.
4698 gtk_entry_set_alignment (GtkEntry *entry, gfloat xalign)
4700 GtkEntryPrivate *priv;
4702 g_return_if_fail (GTK_IS_ENTRY (entry));
4704 priv = GTK_ENTRY_GET_PRIVATE (entry);
4708 else if (xalign > 1.0)
4711 if (xalign != priv->xalign)
4713 priv->xalign = xalign;
4715 gtk_entry_recompute (entry);
4717 g_object_notify (G_OBJECT (entry), "xalign");
4722 * gtk_entry_get_alignment:
4723 * @entry: a #GtkEntry
4725 * Gets the value set by gtk_entry_set_alignment().
4727 * Return value: the alignment
4732 gtk_entry_get_alignment (GtkEntry *entry)
4734 GtkEntryPrivate *priv;
4736 g_return_val_if_fail (GTK_IS_ENTRY (entry), 0.0);
4738 priv = GTK_ENTRY_GET_PRIVATE (entry);
4740 return priv->xalign;
4743 /* Quick hack of a popup menu
4746 activate_cb (GtkWidget *menuitem,
4749 const gchar *signal = g_object_get_data (G_OBJECT (menuitem), "gtk-signal");
4750 g_signal_emit_by_name (entry, signal);
4755 gtk_entry_mnemonic_activate (GtkWidget *widget,
4756 gboolean group_cycling)
4758 gtk_widget_grab_focus (widget);
4763 append_action_signal (GtkEntry *entry,
4765 const gchar *stock_id,
4766 const gchar *signal,
4769 GtkWidget *menuitem = gtk_image_menu_item_new_from_stock (stock_id, NULL);
4771 g_object_set_data (G_OBJECT (menuitem), I_("gtk-signal"), (char *)signal);
4772 g_signal_connect (menuitem, "activate",
4773 G_CALLBACK (activate_cb), entry);
4775 gtk_widget_set_sensitive (menuitem, sensitive);
4777 gtk_widget_show (menuitem);
4778 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
4782 popup_menu_detach (GtkWidget *attach_widget,
4785 GTK_ENTRY (attach_widget)->popup_menu = NULL;
4789 popup_position_func (GtkMenu *menu,
4795 GtkEntry *entry = GTK_ENTRY (user_data);
4796 GtkWidget *widget = GTK_WIDGET (entry);
4798 GtkRequisition menu_req;
4799 GdkRectangle monitor;
4800 GtkBorder inner_border;
4801 gint monitor_num, strong_x, height;
4803 g_return_if_fail (GTK_WIDGET_REALIZED (entry));
4805 gdk_window_get_origin (entry->text_area, x, y);
4807 screen = gtk_widget_get_screen (widget);
4808 monitor_num = gdk_screen_get_monitor_at_window (screen, entry->text_area);
4809 if (monitor_num < 0)
4811 gtk_menu_set_monitor (menu, monitor_num);
4813 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
4814 gtk_widget_size_request (entry->popup_menu, &menu_req);
4815 gdk_drawable_get_size (entry->text_area, NULL, &height);
4816 gtk_entry_get_cursor_locations (entry, CURSOR_STANDARD, &strong_x, NULL);
4817 _gtk_entry_effective_inner_border (entry, &inner_border);
4819 *x += inner_border.left + strong_x - entry->scroll_offset;
4820 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
4821 *x -= menu_req.width;
4823 if ((*y + height + menu_req.height) <= monitor.y + monitor.height)
4825 else if ((*y - menu_req.height) >= monitor.y)
4826 *y -= menu_req.height;
4827 else if (monitor.y + monitor.height - (*y + height) > *y)
4830 *y -= menu_req.height;
4836 unichar_chosen_func (const char *text,
4839 GtkEntry *entry = GTK_ENTRY (data);
4841 if (entry->editable)
4842 gtk_entry_enter_text (entry, text);
4853 popup_targets_received (GtkClipboard *clipboard,
4854 GtkSelectionData *data,
4857 PopupInfo *info = user_data;
4858 GtkEntry *entry = info->entry;
4860 if (GTK_WIDGET_REALIZED (entry))
4862 gboolean clipboard_contains_text;
4863 GtkWidget *menuitem;
4865 gboolean show_input_method_menu;
4866 gboolean show_unicode_menu;
4868 clipboard_contains_text = gtk_selection_data_targets_include_text (data);
4869 if (entry->popup_menu)
4870 gtk_widget_destroy (entry->popup_menu);
4872 entry->popup_menu = gtk_menu_new ();
4874 gtk_menu_attach_to_widget (GTK_MENU (entry->popup_menu),
4878 append_action_signal (entry, entry->popup_menu, GTK_STOCK_CUT, "cut_clipboard",
4879 entry->editable && entry->current_pos != entry->selection_bound);
4880 append_action_signal (entry, entry->popup_menu, GTK_STOCK_COPY, "copy_clipboard",
4881 entry->current_pos != entry->selection_bound);
4882 append_action_signal (entry, entry->popup_menu, GTK_STOCK_PASTE, "paste_clipboard",
4883 entry->editable && clipboard_contains_text);
4885 menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_DELETE, NULL);
4886 gtk_widget_set_sensitive (menuitem, entry->editable && entry->current_pos != entry->selection_bound);
4887 g_signal_connect_swapped (menuitem, "activate",
4888 G_CALLBACK (gtk_entry_delete_cb), entry);
4889 gtk_widget_show (menuitem);
4890 gtk_menu_shell_append (GTK_MENU_SHELL (entry->popup_menu), menuitem);
4892 menuitem = gtk_separator_menu_item_new ();
4893 gtk_widget_show (menuitem);
4894 gtk_menu_shell_append (GTK_MENU_SHELL (entry->popup_menu), menuitem);
4896 menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_SELECT_ALL, NULL);
4897 g_signal_connect_swapped (menuitem, "activate",
4898 G_CALLBACK (gtk_entry_select_all), entry);
4899 gtk_widget_show (menuitem);
4900 gtk_menu_shell_append (GTK_MENU_SHELL (entry->popup_menu), menuitem);
4902 g_object_get (gtk_widget_get_settings (GTK_WIDGET (entry)),
4903 "gtk-show-input-method-menu", &show_input_method_menu,
4904 "gtk-show-unicode-menu", &show_unicode_menu,
4907 if (!entry->visible)
4908 show_input_method_menu = FALSE;
4910 if (show_input_method_menu || show_unicode_menu)
4912 menuitem = gtk_separator_menu_item_new ();
4913 gtk_widget_show (menuitem);
4914 gtk_menu_shell_append (GTK_MENU_SHELL (entry->popup_menu), menuitem);
4917 if (show_input_method_menu)
4919 menuitem = gtk_menu_item_new_with_mnemonic (_("Input _Methods"));
4920 gtk_widget_set_sensitive (menuitem, entry->editable);
4921 gtk_widget_show (menuitem);
4922 submenu = gtk_menu_new ();
4923 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
4925 gtk_menu_shell_append (GTK_MENU_SHELL (entry->popup_menu), menuitem);
4927 gtk_im_multicontext_append_menuitems (GTK_IM_MULTICONTEXT (entry->im_context),
4928 GTK_MENU_SHELL (submenu));
4931 if (show_unicode_menu)
4933 menuitem = gtk_menu_item_new_with_mnemonic (_("_Insert Unicode Control Character"));
4934 gtk_widget_set_sensitive (menuitem, entry->editable);
4935 gtk_widget_show (menuitem);
4937 submenu = gtk_menu_new ();
4938 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
4939 gtk_menu_shell_append (GTK_MENU_SHELL (entry->popup_menu), menuitem);
4941 _gtk_text_util_append_special_char_menuitems (GTK_MENU_SHELL (submenu),
4942 unichar_chosen_func,
4946 g_signal_emit (entry,
4947 signals[POPULATE_POPUP],
4953 gtk_menu_popup (GTK_MENU (entry->popup_menu), NULL, NULL,
4955 info->button, info->time);
4958 gtk_menu_popup (GTK_MENU (entry->popup_menu), NULL, NULL,
4959 popup_position_func, entry,
4960 info->button, info->time);
4961 gtk_menu_shell_select_first (GTK_MENU_SHELL (entry->popup_menu), FALSE);
4965 g_object_unref (entry);
4970 gtk_entry_do_popup (GtkEntry *entry,
4971 GdkEventButton *event)
4973 PopupInfo *info = g_new (PopupInfo, 1);
4975 /* In order to know what entries we should make sensitive, we
4976 * ask for the current targets of the clipboard, and when
4977 * we get them, then we actually pop up the menu.
4979 info->entry = g_object_ref (entry);
4983 info->button = event->button;
4984 info->time = event->time;
4989 info->time = gtk_get_current_event_time ();
4992 gtk_clipboard_request_contents (gtk_widget_get_clipboard (GTK_WIDGET (entry), GDK_SELECTION_CLIPBOARD),
4993 gdk_atom_intern_static_string ("TARGETS"),
4994 popup_targets_received,
4999 gtk_entry_popup_menu (GtkWidget *widget)
5001 gtk_entry_do_popup (GTK_ENTRY (widget), NULL);
5006 gtk_entry_drag_leave (GtkWidget *widget,
5007 GdkDragContext *context,
5010 GtkEntry *entry = GTK_ENTRY (widget);
5012 entry->dnd_position = -1;
5013 gtk_widget_queue_draw (widget);
5017 gtk_entry_drag_drop (GtkWidget *widget,
5018 GdkDragContext *context,
5023 GtkEntry *entry = GTK_ENTRY (widget);
5024 GdkAtom target = GDK_NONE;
5026 if (entry->editable)
5027 target = gtk_drag_dest_find_target (widget, context, NULL);
5029 if (target != GDK_NONE)
5030 gtk_drag_get_data (widget, context, target, time);
5032 gtk_drag_finish (context, FALSE, FALSE, time);
5038 gtk_entry_drag_motion (GtkWidget *widget,
5039 GdkDragContext *context,
5044 GtkEntry *entry = GTK_ENTRY (widget);
5045 GtkWidget *source_widget;
5046 GdkDragAction suggested_action;
5047 gint new_position, old_position;
5050 x -= widget->style->xthickness;
5051 y -= widget->style->ythickness;
5053 old_position = entry->dnd_position;
5054 new_position = gtk_entry_find_position (entry, x + entry->scroll_offset);
5056 if (entry->editable &&
5057 gtk_drag_dest_find_target (widget, context, NULL) != GDK_NONE)
5059 source_widget = gtk_drag_get_source_widget (context);
5060 suggested_action = context->suggested_action;
5062 if (!gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &sel1, &sel2) ||
5063 new_position < sel1 || new_position > sel2)
5065 if (source_widget == widget)
5067 /* Default to MOVE, unless the user has
5068 * pressed ctrl or alt to affect available actions
5070 if ((context->actions & GDK_ACTION_MOVE) != 0)
5071 suggested_action = GDK_ACTION_MOVE;
5074 entry->dnd_position = new_position;
5078 if (source_widget == widget)
5079 suggested_action = 0; /* Can't drop in selection where drag started */
5081 entry->dnd_position = -1;
5086 /* Entry not editable, or no text */
5087 suggested_action = 0;
5088 entry->dnd_position = -1;
5091 gdk_drag_status (context, suggested_action, time);
5093 if (entry->dnd_position != old_position)
5094 gtk_widget_queue_draw (widget);
5100 gtk_entry_drag_data_received (GtkWidget *widget,
5101 GdkDragContext *context,
5104 GtkSelectionData *selection_data,
5108 GtkEntry *entry = GTK_ENTRY (widget);
5109 GtkEditable *editable = GTK_EDITABLE (widget);
5112 str = (gchar *) gtk_selection_data_get_text (selection_data);
5114 x -= widget->style->xthickness;
5115 y -= widget->style->ythickness;
5117 if (str && entry->editable)
5123 if (entry->truncate_multiline)
5124 length = truncate_multiline (str);
5126 new_position = gtk_entry_find_position (entry, x + entry->scroll_offset);
5128 if (!gtk_editable_get_selection_bounds (editable, &sel1, &sel2) ||
5129 new_position < sel1 || new_position > sel2)
5131 gtk_editable_insert_text (editable, str, length, &new_position);
5135 /* Replacing selection */
5136 gtk_editable_delete_text (editable, sel1, sel2);
5137 gtk_editable_insert_text (editable, str, length, &sel1);
5140 gtk_drag_finish (context, TRUE, context->action == GDK_ACTION_MOVE, time);
5144 /* Drag and drop didn't happen! */
5145 gtk_drag_finish (context, FALSE, FALSE, time);
5152 gtk_entry_drag_data_get (GtkWidget *widget,
5153 GdkDragContext *context,
5154 GtkSelectionData *selection_data,
5158 gint sel_start, sel_end;
5160 GtkEditable *editable = GTK_EDITABLE (widget);
5162 if (gtk_editable_get_selection_bounds (editable, &sel_start, &sel_end))
5164 gchar *str = gtk_entry_get_public_chars (GTK_ENTRY (widget), sel_start, sel_end);
5166 gtk_selection_data_set_text (selection_data, str, -1);
5174 gtk_entry_drag_data_delete (GtkWidget *widget,
5175 GdkDragContext *context)
5177 gint sel_start, sel_end;
5179 GtkEditable *editable = GTK_EDITABLE (widget);
5181 if (GTK_ENTRY (widget)->editable &&
5182 gtk_editable_get_selection_bounds (editable, &sel_start, &sel_end))
5183 gtk_editable_delete_text (editable, sel_start, sel_end);
5186 /* We display the cursor when
5188 * - the selection is empty, AND
5189 * - the widget has focus
5192 #define CURSOR_ON_MULTIPLIER 0.66
5193 #define CURSOR_OFF_MULTIPLIER 0.34
5194 #define CURSOR_PEND_MULTIPLIER 1.0
5197 cursor_blinks (GtkEntry *entry)
5199 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (entry));
5202 if (GTK_WIDGET_HAS_FOCUS (entry) &&
5204 entry->selection_bound == entry->current_pos)
5206 g_object_get (settings, "gtk-cursor-blink", &blink, NULL);
5214 get_cursor_time (GtkEntry *entry)
5216 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (entry));
5219 g_object_get (settings, "gtk-cursor-blink-time", &time, NULL);
5225 show_cursor (GtkEntry *entry)
5227 if (!entry->cursor_visible)
5229 entry->cursor_visible = TRUE;
5231 if (GTK_WIDGET_HAS_FOCUS (entry) && entry->selection_bound == entry->current_pos)
5232 gtk_widget_queue_draw (GTK_WIDGET (entry));
5237 hide_cursor (GtkEntry *entry)
5239 if (entry->cursor_visible)
5241 entry->cursor_visible = FALSE;
5243 if (GTK_WIDGET_HAS_FOCUS (entry) && entry->selection_bound == entry->current_pos)
5244 gtk_widget_queue_draw (GTK_WIDGET (entry));
5252 blink_cb (gpointer data)
5256 GDK_THREADS_ENTER ();
5258 entry = GTK_ENTRY (data);
5260 if (!GTK_WIDGET_HAS_FOCUS (entry))
5262 g_warning ("GtkEntry - did not receive focus-out-event. If you\n"
5263 "connect a handler to this signal, it must return\n"
5264 "FALSE so the entry gets the event as well");
5267 g_assert (GTK_WIDGET_HAS_FOCUS (entry));
5268 g_assert (entry->selection_bound == entry->current_pos);
5270 if (entry->cursor_visible)
5272 hide_cursor (entry);
5273 entry->blink_timeout = g_timeout_add (get_cursor_time (entry) * CURSOR_OFF_MULTIPLIER,
5279 show_cursor (entry);
5280 entry->blink_timeout = g_timeout_add (get_cursor_time (entry) * CURSOR_ON_MULTIPLIER,
5285 GDK_THREADS_LEAVE ();
5287 /* Remove ourselves */
5292 gtk_entry_check_cursor_blink (GtkEntry *entry)
5294 if (cursor_blinks (entry))
5296 if (!entry->blink_timeout)
5298 entry->blink_timeout = g_timeout_add (get_cursor_time (entry) * CURSOR_ON_MULTIPLIER,
5301 show_cursor (entry);
5306 if (entry->blink_timeout)
5308 g_source_remove (entry->blink_timeout);
5309 entry->blink_timeout = 0;
5312 entry->cursor_visible = TRUE;
5318 gtk_entry_pend_cursor_blink (GtkEntry *entry)
5320 if (cursor_blinks (entry))
5322 if (entry->blink_timeout != 0)
5323 g_source_remove (entry->blink_timeout);
5325 entry->blink_timeout = g_timeout_add (get_cursor_time (entry) * CURSOR_PEND_MULTIPLIER,
5328 show_cursor (entry);
5334 gtk_entry_completion_timeout (gpointer data)
5336 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (data);
5338 GDK_THREADS_ENTER ();
5340 completion->priv->completion_timeout = 0;
5342 if (completion->priv->filter_model &&
5343 g_utf8_strlen (gtk_entry_get_text (GTK_ENTRY (completion->priv->entry)), -1)
5344 >= completion->priv->minimum_key_length)
5348 GtkTreeSelection *s;
5349 gboolean popup_single;
5351 gtk_entry_completion_complete (completion);
5352 matches = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->filter_model), NULL);
5354 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view)));
5356 s = gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->action_view));
5358 gtk_tree_selection_unselect_all (s);
5360 actions = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->actions), NULL);
5362 g_object_get (completion, "popup-single-match", &popup_single, NULL);
5363 if ((matches > (popup_single ? 0: 1)) || actions > 0)
5365 if (GTK_WIDGET_VISIBLE (completion->priv->popup_window))
5366 _gtk_entry_completion_resize_popup (completion);
5368 _gtk_entry_completion_popup (completion);
5371 _gtk_entry_completion_popdown (completion);
5373 else if (GTK_WIDGET_VISIBLE (completion->priv->popup_window))
5374 _gtk_entry_completion_popdown (completion);
5376 GDK_THREADS_LEAVE ();
5381 static inline gboolean
5382 keyval_is_cursor_move (guint keyval)
5384 if (keyval == GDK_Up || keyval == GDK_KP_Up)
5387 if (keyval == GDK_Down || keyval == GDK_KP_Down)
5390 if (keyval == GDK_Page_Up)
5393 if (keyval == GDK_Page_Down)
5400 gtk_entry_completion_key_press (GtkWidget *widget,
5404 gint matches, actions = 0;
5405 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
5407 if (!GTK_WIDGET_MAPPED (completion->priv->popup_window))
5410 _gtk_entry_reset_im_context (GTK_ENTRY (widget));
5412 matches = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->filter_model), NULL);
5414 if (completion->priv->actions)
5415 actions = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->actions), NULL);
5417 if (keyval_is_cursor_move (event->keyval))
5419 GtkTreePath *path = NULL;
5421 if (event->keyval == GDK_Up || event->keyval == GDK_KP_Up)
5423 if (completion->priv->current_selected < 0)
5424 completion->priv->current_selected = matches + actions - 1;
5426 completion->priv->current_selected--;
5428 else if (event->keyval == GDK_Down || event->keyval == GDK_KP_Down)
5430 if (completion->priv->current_selected < matches + actions - 1)
5431 completion->priv->current_selected++;
5433 completion->priv->current_selected = -1;
5435 else if (event->keyval == GDK_Page_Up)
5437 if (completion->priv->current_selected < 0)
5438 completion->priv->current_selected = matches + actions - 1;
5439 else if (completion->priv->current_selected == 0)
5440 completion->priv->current_selected = -1;
5441 else if (completion->priv->current_selected < matches)
5443 completion->priv->current_selected -= 14;
5444 if (completion->priv->current_selected < 0)
5445 completion->priv->current_selected = 0;
5449 completion->priv->current_selected -= 14;
5450 if (completion->priv->current_selected < matches - 1)
5451 completion->priv->current_selected = matches - 1;
5454 else if (event->keyval == GDK_Page_Down)
5456 if (completion->priv->current_selected < 0)
5457 completion->priv->current_selected = 0;
5458 else if (completion->priv->current_selected < matches - 1)
5460 completion->priv->current_selected += 14;
5461 if (completion->priv->current_selected > matches - 1)
5462 completion->priv->current_selected = matches - 1;
5464 else if (completion->priv->current_selected == matches + actions - 1)
5466 completion->priv->current_selected = -1;
5470 completion->priv->current_selected += 14;
5471 if (completion->priv->current_selected > matches + actions - 1)
5472 completion->priv->current_selected = matches + actions - 1;
5476 if (completion->priv->current_selected < 0)
5478 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view)));
5479 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->action_view)));
5481 else if (completion->priv->current_selected < matches)
5483 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->action_view)));
5485 path = gtk_tree_path_new_from_indices (completion->priv->current_selected, -1);
5486 gtk_tree_view_set_cursor (GTK_TREE_VIEW (completion->priv->tree_view),
5489 else if (completion->priv->current_selected - matches >= 0)
5491 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view)));
5493 path = gtk_tree_path_new_from_indices (completion->priv->current_selected - matches, -1);
5494 gtk_tree_view_set_cursor (GTK_TREE_VIEW (completion->priv->action_view),
5498 gtk_tree_path_free (path);
5502 else if (event->keyval == GDK_Escape)
5504 _gtk_entry_completion_popdown (completion);
5508 else if (event->keyval == GDK_Tab ||
5509 event->keyval == GDK_KP_Tab ||
5510 event->keyval == GDK_ISO_Left_Tab)
5512 GtkDirectionType dir = event->keyval == GDK_ISO_Left_Tab ?
5513 GTK_DIR_TAB_BACKWARD : GTK_DIR_TAB_FORWARD;
5515 _gtk_entry_completion_popdown (completion);
5517 gtk_widget_child_focus (gtk_widget_get_toplevel (widget), dir);
5521 else if (event->keyval == GDK_ISO_Enter ||
5522 event->keyval == GDK_KP_Enter ||
5523 event->keyval == GDK_Return)
5525 _gtk_entry_completion_popdown (completion);
5527 if (completion->priv->current_selected < matches)
5530 GtkTreeModel *model = NULL;
5531 GtkTreeSelection *sel;
5534 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view));
5535 if (!gtk_tree_selection_get_selected (sel, &model, &iter))
5538 g_signal_handler_block (widget, completion->priv->changed_id);
5539 g_signal_emit_by_name (completion, "match_selected",
5540 model, &iter, &entry_set);
5541 g_signal_handler_unblock (widget, completion->priv->changed_id);
5547 gtk_tree_model_get (model, &iter,
5548 completion->priv->text_column, &str,
5551 g_signal_handler_block (widget, completion->priv->changed_id);
5552 gtk_entry_set_text (GTK_ENTRY (widget), str);
5553 g_signal_handler_unblock (widget, completion->priv->changed_id);
5555 /* move the cursor to the end */
5556 gtk_editable_set_position (GTK_EDITABLE (widget), -1);
5563 else if (completion->priv->current_selected - matches >= 0)
5567 path = gtk_tree_path_new_from_indices (completion->priv->current_selected - matches, -1);
5569 g_signal_emit_by_name (completion, "action_activated",
5570 gtk_tree_path_get_indices (path)[0]);
5571 gtk_tree_path_free (path);
5581 gtk_entry_completion_changed (GtkWidget *entry,
5584 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
5586 /* (re)install completion timeout */
5587 if (completion->priv->completion_timeout)
5588 g_source_remove (completion->priv->completion_timeout);
5590 if (!gtk_entry_get_text (GTK_ENTRY (entry)))
5593 /* no need to normalize for this test */
5594 if (completion->priv->minimum_key_length > 0 &&
5595 strcmp ("", gtk_entry_get_text (GTK_ENTRY (entry))) == 0)
5597 if (GTK_WIDGET_VISIBLE (completion->priv->popup_window))
5598 _gtk_entry_completion_popdown (completion);
5602 completion->priv->completion_timeout =
5603 g_timeout_add (COMPLETION_TIMEOUT,
5604 gtk_entry_completion_timeout,
5609 check_completion_callback (GtkEntryCompletion *completion)
5611 completion->priv->check_completion_idle = NULL;
5613 gtk_entry_completion_complete (completion);
5614 gtk_entry_completion_insert_prefix (completion);
5620 clear_completion_callback (GtkEntry *entry,
5623 if (pspec->name == I_("cursor-position") ||
5624 pspec->name == I_("selection-bound"))
5626 GtkEntryCompletion *completion = gtk_entry_get_completion (entry);
5628 completion->priv->has_completion = FALSE;
5633 accept_completion_callback (GtkEntry *entry)
5635 GtkEntryCompletion *completion = gtk_entry_get_completion (entry);
5637 if (completion->priv->has_completion)
5638 gtk_editable_set_position (GTK_EDITABLE (entry),
5639 entry->text_length);
5645 completion_insert_text_callback (GtkEntry *entry,
5649 GtkEntryCompletion *completion)
5651 /* idle to update the selection based on the file list */
5652 if (completion->priv->check_completion_idle == NULL)
5654 completion->priv->check_completion_idle = g_idle_source_new ();
5655 g_source_set_priority (completion->priv->check_completion_idle, G_PRIORITY_HIGH);
5656 g_source_set_closure (completion->priv->check_completion_idle,
5657 g_cclosure_new_object (G_CALLBACK (check_completion_callback),
5658 G_OBJECT (completion)));
5659 g_source_attach (completion->priv->check_completion_idle, NULL);
5664 completion_changed (GtkEntryCompletion *completion,
5668 GtkEntry *entry = GTK_ENTRY (data);
5670 if (pspec->name == I_("popup-completion") ||
5671 pspec->name == I_("inline-completion"))
5673 disconnect_completion_signals (entry, completion);
5674 connect_completion_signals (entry, completion);
5679 disconnect_completion_signals (GtkEntry *entry,
5680 GtkEntryCompletion *completion)
5682 g_signal_handlers_disconnect_by_func (completion,
5683 G_CALLBACK (completion_changed), entry);
5684 if (completion->priv->changed_id > 0 &&
5685 g_signal_handler_is_connected (entry, completion->priv->changed_id))
5686 g_signal_handler_disconnect (entry, completion->priv->changed_id);
5687 g_signal_handlers_disconnect_by_func (entry,
5688 G_CALLBACK (gtk_entry_completion_key_press), completion);
5689 if (completion->priv->insert_text_id > 0 &&
5690 g_signal_handler_is_connected (entry, completion->priv->insert_text_id))
5691 g_signal_handler_disconnect (entry, completion->priv->insert_text_id);
5692 g_signal_handlers_disconnect_by_func (entry,
5693 G_CALLBACK (completion_insert_text_callback), completion);
5694 g_signal_handlers_disconnect_by_func (entry,
5695 G_CALLBACK (clear_completion_callback), completion);
5696 g_signal_handlers_disconnect_by_func (entry,
5697 G_CALLBACK (accept_completion_callback), completion);
5701 connect_completion_signals (GtkEntry *entry,
5702 GtkEntryCompletion *completion)
5704 if (completion->priv->popup_completion)
5706 completion->priv->changed_id =
5707 g_signal_connect (entry, "changed",
5708 G_CALLBACK (gtk_entry_completion_changed), completion);
5709 g_signal_connect (entry, "key_press_event",
5710 G_CALLBACK (gtk_entry_completion_key_press), completion);
5713 if (completion->priv->inline_completion)
5715 completion->priv->insert_text_id =
5716 g_signal_connect (entry, "insert_text",
5717 G_CALLBACK (completion_insert_text_callback), completion);
5718 g_signal_connect (entry, "notify",
5719 G_CALLBACK (clear_completion_callback), completion);
5720 g_signal_connect (entry, "activate",
5721 G_CALLBACK (accept_completion_callback), completion);
5722 g_signal_connect (entry, "focus_out_event",
5723 G_CALLBACK (accept_completion_callback), completion);
5725 g_signal_connect (completion, "notify",
5726 G_CALLBACK (completion_changed), entry);
5730 * gtk_entry_set_completion:
5731 * @entry: A #GtkEntry.
5732 * @completion: The #GtkEntryCompletion or %NULL.
5734 * Sets @completion to be the auxiliary completion object to use with @entry.
5735 * All further configuration of the completion mechanism is done on
5736 * @completion using the #GtkEntryCompletion API. Completion is disabled if
5737 * @completion is set to %NULL.
5742 gtk_entry_set_completion (GtkEntry *entry,
5743 GtkEntryCompletion *completion)
5745 GtkEntryCompletion *old;
5747 g_return_if_fail (GTK_IS_ENTRY (entry));
5748 g_return_if_fail (!completion || GTK_IS_ENTRY_COMPLETION (completion));
5750 old = gtk_entry_get_completion (entry);
5752 if (old == completion)
5757 if (old->priv->completion_timeout)
5759 g_source_remove (old->priv->completion_timeout);
5760 old->priv->completion_timeout = 0;
5763 if (GTK_WIDGET_MAPPED (old->priv->popup_window))
5764 _gtk_entry_completion_popdown (old);
5766 disconnect_completion_signals (entry, old);
5767 old->priv->entry = NULL;
5769 g_object_unref (old);
5774 g_object_set_data (G_OBJECT (entry), I_(GTK_ENTRY_COMPLETION_KEY), NULL);
5778 /* hook into the entry */
5779 g_object_ref (completion);
5781 connect_completion_signals (entry, completion);
5782 completion->priv->entry = GTK_WIDGET (entry);
5783 g_object_set_data (G_OBJECT (entry), I_(GTK_ENTRY_COMPLETION_KEY), completion);
5787 * gtk_entry_get_completion:
5788 * @entry: A #GtkEntry.
5790 * Returns the auxiliary completion object currently in use by @entry.
5792 * Return value: The auxiliary completion object currently in use by @entry.
5796 GtkEntryCompletion *
5797 gtk_entry_get_completion (GtkEntry *entry)
5799 GtkEntryCompletion *completion;
5801 g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
5803 completion = GTK_ENTRY_COMPLETION (g_object_get_data (G_OBJECT (entry),
5804 GTK_ENTRY_COMPLETION_KEY));
5809 #define __GTK_ENTRY_C__
5810 #include "gtkaliasdef.c"