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_class_init (GtkEntryClass *klass);
139 static void gtk_entry_editable_init (GtkEditableClass *iface);
140 static void gtk_entry_cell_editable_init (GtkCellEditableIface *iface);
141 static void gtk_entry_init (GtkEntry *entry);
142 static void gtk_entry_set_property (GObject *object,
146 static void gtk_entry_get_property (GObject *object,
150 static void gtk_entry_finalize (GObject *object);
151 static void gtk_entry_destroy (GtkObject *object);
155 static void gtk_entry_realize (GtkWidget *widget);
156 static void gtk_entry_unrealize (GtkWidget *widget);
157 static void gtk_entry_size_request (GtkWidget *widget,
158 GtkRequisition *requisition);
159 static void gtk_entry_size_allocate (GtkWidget *widget,
160 GtkAllocation *allocation);
161 static void gtk_entry_draw_frame (GtkWidget *widget);
162 static gint gtk_entry_expose (GtkWidget *widget,
163 GdkEventExpose *event);
164 static gint gtk_entry_button_press (GtkWidget *widget,
165 GdkEventButton *event);
166 static gint gtk_entry_button_release (GtkWidget *widget,
167 GdkEventButton *event);
168 static gint gtk_entry_motion_notify (GtkWidget *widget,
169 GdkEventMotion *event);
170 static gint gtk_entry_key_press (GtkWidget *widget,
172 static gint gtk_entry_key_release (GtkWidget *widget,
174 static gint gtk_entry_focus_in (GtkWidget *widget,
175 GdkEventFocus *event);
176 static gint gtk_entry_focus_out (GtkWidget *widget,
177 GdkEventFocus *event);
178 static void gtk_entry_grab_focus (GtkWidget *widget);
179 static void gtk_entry_style_set (GtkWidget *widget,
180 GtkStyle *previous_style);
181 static void gtk_entry_direction_changed (GtkWidget *widget,
182 GtkTextDirection previous_dir);
183 static void gtk_entry_state_changed (GtkWidget *widget,
184 GtkStateType previous_state);
185 static void gtk_entry_screen_changed (GtkWidget *widget,
186 GdkScreen *old_screen);
188 static gboolean gtk_entry_drag_drop (GtkWidget *widget,
189 GdkDragContext *context,
193 static gboolean gtk_entry_drag_motion (GtkWidget *widget,
194 GdkDragContext *context,
198 static void gtk_entry_drag_leave (GtkWidget *widget,
199 GdkDragContext *context,
201 static void gtk_entry_drag_data_received (GtkWidget *widget,
202 GdkDragContext *context,
205 GtkSelectionData *selection_data,
208 static void gtk_entry_drag_data_get (GtkWidget *widget,
209 GdkDragContext *context,
210 GtkSelectionData *selection_data,
213 static void gtk_entry_drag_data_delete (GtkWidget *widget,
214 GdkDragContext *context);
216 /* GtkEditable method implementations
218 static void gtk_entry_insert_text (GtkEditable *editable,
219 const gchar *new_text,
220 gint new_text_length,
222 static void gtk_entry_delete_text (GtkEditable *editable,
225 static gchar * gtk_entry_get_chars (GtkEditable *editable,
228 static void gtk_entry_real_set_position (GtkEditable *editable,
230 static gint gtk_entry_get_position (GtkEditable *editable);
231 static void gtk_entry_set_selection_bounds (GtkEditable *editable,
234 static gboolean gtk_entry_get_selection_bounds (GtkEditable *editable,
238 /* GtkCellEditable method implementations
240 static void gtk_entry_start_editing (GtkCellEditable *cell_editable,
243 /* Default signal handlers
245 static void gtk_entry_real_insert_text (GtkEditable *editable,
246 const gchar *new_text,
247 gint new_text_length,
249 static void gtk_entry_real_delete_text (GtkEditable *editable,
252 static void gtk_entry_move_cursor (GtkEntry *entry,
253 GtkMovementStep step,
255 gboolean extend_selection);
256 static void gtk_entry_insert_at_cursor (GtkEntry *entry,
258 static void gtk_entry_delete_from_cursor (GtkEntry *entry,
261 static void gtk_entry_backspace (GtkEntry *entry);
262 static void gtk_entry_cut_clipboard (GtkEntry *entry);
263 static void gtk_entry_copy_clipboard (GtkEntry *entry);
264 static void gtk_entry_paste_clipboard (GtkEntry *entry);
265 static void gtk_entry_toggle_overwrite (GtkEntry *entry);
266 static void gtk_entry_select_all (GtkEntry *entry);
267 static void gtk_entry_real_activate (GtkEntry *entry);
268 static gboolean gtk_entry_popup_menu (GtkWidget *widget);
270 static void gtk_entry_keymap_direction_changed (GdkKeymap *keymap,
272 /* IM Context Callbacks
274 static void gtk_entry_commit_cb (GtkIMContext *context,
277 static void gtk_entry_preedit_changed_cb (GtkIMContext *context,
279 static gboolean gtk_entry_retrieve_surrounding_cb (GtkIMContext *context,
281 static gboolean gtk_entry_delete_surrounding_cb (GtkIMContext *context,
288 static void gtk_entry_enter_text (GtkEntry *entry,
290 static void gtk_entry_set_positions (GtkEntry *entry,
292 gint selection_bound);
293 static void gtk_entry_draw_text (GtkEntry *entry);
294 static void gtk_entry_draw_cursor (GtkEntry *entry,
296 static PangoLayout *gtk_entry_ensure_layout (GtkEntry *entry,
297 gboolean include_preedit);
298 static void gtk_entry_reset_layout (GtkEntry *entry);
299 static void gtk_entry_queue_draw (GtkEntry *entry);
300 static void gtk_entry_reset_im_context (GtkEntry *entry);
301 static void gtk_entry_recompute (GtkEntry *entry);
302 static gint gtk_entry_find_position (GtkEntry *entry,
304 static void gtk_entry_get_cursor_locations (GtkEntry *entry,
308 static void gtk_entry_adjust_scroll (GtkEntry *entry);
309 static gint gtk_entry_move_visually (GtkEntry *editable,
312 static gint gtk_entry_move_logically (GtkEntry *entry,
315 static gint gtk_entry_move_forward_word (GtkEntry *entry,
317 gboolean allow_whitespace);
318 static gint gtk_entry_move_backward_word (GtkEntry *entry,
320 gboolean allow_whitespace);
321 static void gtk_entry_delete_whitespace (GtkEntry *entry);
322 static void gtk_entry_select_word (GtkEntry *entry);
323 static void gtk_entry_select_line (GtkEntry *entry);
324 static char * gtk_entry_get_public_chars (GtkEntry *entry,
327 static void gtk_entry_paste (GtkEntry *entry,
329 static void gtk_entry_update_primary_selection (GtkEntry *entry);
330 static void gtk_entry_do_popup (GtkEntry *entry,
331 GdkEventButton *event);
332 static gboolean gtk_entry_mnemonic_activate (GtkWidget *widget,
333 gboolean group_cycling);
334 static void gtk_entry_state_changed (GtkWidget *widget,
335 GtkStateType previous_state);
336 static void gtk_entry_check_cursor_blink (GtkEntry *entry);
337 static void gtk_entry_pend_cursor_blink (GtkEntry *entry);
338 static void get_text_area_size (GtkEntry *entry,
343 static void get_widget_window_size (GtkEntry *entry,
348 static void get_inner_border (GtkEntry *entry,
352 static gint gtk_entry_completion_timeout (gpointer data);
353 static gboolean gtk_entry_completion_key_press (GtkWidget *widget,
356 static void gtk_entry_completion_changed (GtkWidget *entry,
358 static gboolean check_completion_callback (GtkEntryCompletion *completion);
359 static void clear_completion_callback (GtkEntry *entry,
361 static gboolean accept_completion_callback (GtkEntry *entry);
362 static void completion_insert_text_callback (GtkEntry *entry,
366 GtkEntryCompletion *completion);
367 static void completion_changed (GtkEntryCompletion *completion,
370 static void disconnect_completion_signals (GtkEntry *entry,
371 GtkEntryCompletion *completion);
372 static void connect_completion_signals (GtkEntry *entry,
373 GtkEntryCompletion *completion);
376 static GtkWidgetClass *parent_class = NULL;
379 gtk_entry_get_type (void)
381 static GType entry_type = 0;
385 static const GTypeInfo entry_info =
387 sizeof (GtkEntryClass),
388 NULL, /* base_init */
389 NULL, /* base_finalize */
390 (GClassInitFunc) gtk_entry_class_init,
391 NULL, /* class_finalize */
392 NULL, /* class_data */
395 (GInstanceInitFunc) gtk_entry_init,
398 static const GInterfaceInfo editable_info =
400 (GInterfaceInitFunc) gtk_entry_editable_init, /* interface_init */
401 NULL, /* interface_finalize */
402 NULL /* interface_data */
405 static const GInterfaceInfo cell_editable_info =
407 (GInterfaceInitFunc) gtk_entry_cell_editable_init, /* interface_init */
408 NULL, /* interface_finalize */
409 NULL /* interface_data */
412 entry_type = g_type_register_static (GTK_TYPE_WIDGET, I_("GtkEntry"),
415 g_type_add_interface_static (entry_type,
418 g_type_add_interface_static (entry_type,
419 GTK_TYPE_CELL_EDITABLE,
420 &cell_editable_info);
427 add_move_binding (GtkBindingSet *binding_set,
430 GtkMovementStep step,
433 g_return_if_fail ((modmask & GDK_SHIFT_MASK) == 0);
435 gtk_binding_entry_add_signal (binding_set, keyval, modmask,
439 G_TYPE_BOOLEAN, FALSE);
441 /* Selection-extending version */
442 gtk_binding_entry_add_signal (binding_set, keyval, modmask | GDK_SHIFT_MASK,
446 G_TYPE_BOOLEAN, TRUE);
450 gtk_entry_class_init (GtkEntryClass *class)
452 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
453 GtkWidgetClass *widget_class;
454 GtkObjectClass *gtk_object_class;
455 GtkBindingSet *binding_set;
457 widget_class = (GtkWidgetClass*) class;
458 gtk_object_class = (GtkObjectClass *)class;
459 parent_class = g_type_class_peek_parent (class);
461 gobject_class->finalize = gtk_entry_finalize;
462 gobject_class->set_property = gtk_entry_set_property;
463 gobject_class->get_property = gtk_entry_get_property;
465 widget_class->realize = gtk_entry_realize;
466 widget_class->unrealize = gtk_entry_unrealize;
467 widget_class->size_request = gtk_entry_size_request;
468 widget_class->size_allocate = gtk_entry_size_allocate;
469 widget_class->expose_event = gtk_entry_expose;
470 widget_class->button_press_event = gtk_entry_button_press;
471 widget_class->button_release_event = gtk_entry_button_release;
472 widget_class->motion_notify_event = gtk_entry_motion_notify;
473 widget_class->key_press_event = gtk_entry_key_press;
474 widget_class->key_release_event = gtk_entry_key_release;
475 widget_class->focus_in_event = gtk_entry_focus_in;
476 widget_class->focus_out_event = gtk_entry_focus_out;
477 widget_class->grab_focus = gtk_entry_grab_focus;
478 widget_class->style_set = gtk_entry_style_set;
479 widget_class->direction_changed = gtk_entry_direction_changed;
480 widget_class->state_changed = gtk_entry_state_changed;
481 widget_class->screen_changed = gtk_entry_screen_changed;
482 widget_class->mnemonic_activate = gtk_entry_mnemonic_activate;
484 widget_class->drag_drop = gtk_entry_drag_drop;
485 widget_class->drag_motion = gtk_entry_drag_motion;
486 widget_class->drag_leave = gtk_entry_drag_leave;
487 widget_class->drag_data_received = gtk_entry_drag_data_received;
488 widget_class->drag_data_get = gtk_entry_drag_data_get;
489 widget_class->drag_data_delete = gtk_entry_drag_data_delete;
491 widget_class->popup_menu = gtk_entry_popup_menu;
493 gtk_object_class->destroy = gtk_entry_destroy;
495 class->move_cursor = gtk_entry_move_cursor;
496 class->insert_at_cursor = gtk_entry_insert_at_cursor;
497 class->delete_from_cursor = gtk_entry_delete_from_cursor;
498 class->backspace = gtk_entry_backspace;
499 class->cut_clipboard = gtk_entry_cut_clipboard;
500 class->copy_clipboard = gtk_entry_copy_clipboard;
501 class->paste_clipboard = gtk_entry_paste_clipboard;
502 class->toggle_overwrite = gtk_entry_toggle_overwrite;
503 class->activate = gtk_entry_real_activate;
505 quark_inner_border = g_quark_from_static_string ("gtk-entry-inner-border");
506 quark_password_hint = g_quark_from_static_string ("gtk-entry-password-hint");
508 g_object_class_install_property (gobject_class,
509 PROP_CURSOR_POSITION,
510 g_param_spec_int ("cursor-position",
511 P_("Cursor Position"),
512 P_("The current position of the insertion cursor in chars"),
516 GTK_PARAM_READABLE));
518 g_object_class_install_property (gobject_class,
519 PROP_SELECTION_BOUND,
520 g_param_spec_int ("selection-bound",
521 P_("Selection Bound"),
522 P_("The position of the opposite end of the selection from the cursor in chars"),
526 GTK_PARAM_READABLE));
528 g_object_class_install_property (gobject_class,
530 g_param_spec_boolean ("editable",
532 P_("Whether the entry contents can be edited"),
534 GTK_PARAM_READWRITE));
536 g_object_class_install_property (gobject_class,
538 g_param_spec_int ("max-length",
539 P_("Maximum length"),
540 P_("Maximum number of characters for this entry. Zero if no maximum"),
544 GTK_PARAM_READWRITE));
545 g_object_class_install_property (gobject_class,
547 g_param_spec_boolean ("visibility",
549 P_("FALSE displays the \"invisible char\" instead of the actual text (password mode)"),
551 GTK_PARAM_READWRITE));
553 g_object_class_install_property (gobject_class,
555 g_param_spec_boolean ("has-frame",
557 P_("FALSE removes outside bevel from entry"),
559 GTK_PARAM_READWRITE));
561 g_object_class_install_property (gobject_class,
563 g_param_spec_boxed ("inner-border",
565 P_("Border between text and frame. Overrides the inner-border style propety"),
567 GTK_PARAM_READWRITE));
569 g_object_class_install_property (gobject_class,
571 g_param_spec_unichar ("invisible-char",
572 P_("Invisible character"),
573 P_("The character to use when masking entry contents (in \"password mode\")"),
575 GTK_PARAM_READWRITE));
577 g_object_class_install_property (gobject_class,
578 PROP_ACTIVATES_DEFAULT,
579 g_param_spec_boolean ("activates-default",
580 P_("Activates default"),
581 P_("Whether to activate the default widget (such as the default button in a dialog) when Enter is pressed"),
583 GTK_PARAM_READWRITE));
584 g_object_class_install_property (gobject_class,
586 g_param_spec_int ("width-chars",
587 P_("Width in chars"),
588 P_("Number of characters to leave space for in the entry"),
592 GTK_PARAM_READWRITE));
594 g_object_class_install_property (gobject_class,
596 g_param_spec_int ("scroll-offset",
598 P_("Number of pixels of the entry scrolled off the screen to the left"),
602 GTK_PARAM_READABLE));
604 g_object_class_install_property (gobject_class,
606 g_param_spec_string ("text",
608 P_("The contents of the entry"),
610 GTK_PARAM_READWRITE));
615 * The horizontal alignment, from 0 (left) to 1 (right).
616 * Reversed for RTL layouts.
620 g_object_class_install_property (gobject_class,
622 g_param_spec_float ("xalign",
624 P_("The horizontal alignment, from 0 (left) to 1 (right). Reversed for RTL layouts."),
628 GTK_PARAM_READWRITE));
631 * GtkEntry:truncate-multiline:
633 * When %TRUE, pasted multi-line text is truncated to the first line.
637 g_object_class_install_property (gobject_class,
638 PROP_TRUNCATE_MULTILINE,
639 g_param_spec_boolean ("truncate-multiline",
640 P_("Truncate multiline"),
641 P_("Whether to truncate multiline pastes to one line."),
643 GTK_PARAM_READWRITE));
645 signals[POPULATE_POPUP] =
646 g_signal_new (I_("populate_popup"),
647 G_OBJECT_CLASS_TYPE (gobject_class),
649 G_STRUCT_OFFSET (GtkEntryClass, populate_popup),
651 _gtk_marshal_VOID__OBJECT,
658 g_signal_new (I_("activate"),
659 G_OBJECT_CLASS_TYPE (gobject_class),
660 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
661 G_STRUCT_OFFSET (GtkEntryClass, activate),
663 _gtk_marshal_VOID__VOID,
665 widget_class->activate_signal = signals[ACTIVATE];
667 signals[MOVE_CURSOR] =
668 g_signal_new (I_("move_cursor"),
669 G_OBJECT_CLASS_TYPE (gobject_class),
670 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
671 G_STRUCT_OFFSET (GtkEntryClass, move_cursor),
673 _gtk_marshal_VOID__ENUM_INT_BOOLEAN,
675 GTK_TYPE_MOVEMENT_STEP,
679 signals[INSERT_AT_CURSOR] =
680 g_signal_new (I_("insert_at_cursor"),
681 G_OBJECT_CLASS_TYPE (gobject_class),
682 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
683 G_STRUCT_OFFSET (GtkEntryClass, insert_at_cursor),
685 _gtk_marshal_VOID__STRING,
689 signals[DELETE_FROM_CURSOR] =
690 g_signal_new (I_("delete_from_cursor"),
691 G_OBJECT_CLASS_TYPE (gobject_class),
692 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
693 G_STRUCT_OFFSET (GtkEntryClass, delete_from_cursor),
695 _gtk_marshal_VOID__ENUM_INT,
697 GTK_TYPE_DELETE_TYPE,
701 g_signal_new (I_("backspace"),
702 G_OBJECT_CLASS_TYPE (gobject_class),
703 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
704 G_STRUCT_OFFSET (GtkEntryClass, backspace),
706 _gtk_marshal_VOID__VOID,
709 signals[CUT_CLIPBOARD] =
710 g_signal_new (I_("cut_clipboard"),
711 G_OBJECT_CLASS_TYPE (gobject_class),
712 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
713 G_STRUCT_OFFSET (GtkEntryClass, cut_clipboard),
715 _gtk_marshal_VOID__VOID,
718 signals[COPY_CLIPBOARD] =
719 g_signal_new (I_("copy_clipboard"),
720 G_OBJECT_CLASS_TYPE (gobject_class),
721 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
722 G_STRUCT_OFFSET (GtkEntryClass, copy_clipboard),
724 _gtk_marshal_VOID__VOID,
727 signals[PASTE_CLIPBOARD] =
728 g_signal_new (I_("paste_clipboard"),
729 G_OBJECT_CLASS_TYPE (gobject_class),
730 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
731 G_STRUCT_OFFSET (GtkEntryClass, paste_clipboard),
733 _gtk_marshal_VOID__VOID,
736 signals[TOGGLE_OVERWRITE] =
737 g_signal_new (I_("toggle_overwrite"),
738 G_OBJECT_CLASS_TYPE (gobject_class),
739 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
740 G_STRUCT_OFFSET (GtkEntryClass, toggle_overwrite),
742 _gtk_marshal_VOID__VOID,
749 binding_set = gtk_binding_set_by_class (class);
751 /* Moving the insertion point */
752 add_move_binding (binding_set, GDK_Right, 0,
753 GTK_MOVEMENT_VISUAL_POSITIONS, 1);
755 add_move_binding (binding_set, GDK_Left, 0,
756 GTK_MOVEMENT_VISUAL_POSITIONS, -1);
758 add_move_binding (binding_set, GDK_KP_Right, 0,
759 GTK_MOVEMENT_VISUAL_POSITIONS, 1);
761 add_move_binding (binding_set, GDK_KP_Left, 0,
762 GTK_MOVEMENT_VISUAL_POSITIONS, -1);
764 add_move_binding (binding_set, GDK_Right, GDK_CONTROL_MASK,
765 GTK_MOVEMENT_WORDS, 1);
767 add_move_binding (binding_set, GDK_Left, GDK_CONTROL_MASK,
768 GTK_MOVEMENT_WORDS, -1);
770 add_move_binding (binding_set, GDK_KP_Right, GDK_CONTROL_MASK,
771 GTK_MOVEMENT_WORDS, 1);
773 add_move_binding (binding_set, GDK_KP_Left, GDK_CONTROL_MASK,
774 GTK_MOVEMENT_WORDS, -1);
776 add_move_binding (binding_set, GDK_Home, 0,
777 GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
779 add_move_binding (binding_set, GDK_End, 0,
780 GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
782 add_move_binding (binding_set, GDK_KP_Home, 0,
783 GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
785 add_move_binding (binding_set, GDK_KP_End, 0,
786 GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
788 add_move_binding (binding_set, GDK_Home, GDK_CONTROL_MASK,
789 GTK_MOVEMENT_BUFFER_ENDS, -1);
791 add_move_binding (binding_set, GDK_End, GDK_CONTROL_MASK,
792 GTK_MOVEMENT_BUFFER_ENDS, 1);
794 add_move_binding (binding_set, GDK_KP_Home, GDK_CONTROL_MASK,
795 GTK_MOVEMENT_BUFFER_ENDS, -1);
797 add_move_binding (binding_set, GDK_KP_End, GDK_CONTROL_MASK,
798 GTK_MOVEMENT_BUFFER_ENDS, 1);
802 gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_CONTROL_MASK,
804 GTK_TYPE_MOVEMENT_STEP, GTK_MOVEMENT_BUFFER_ENDS,
806 G_TYPE_BOOLEAN, FALSE);
807 gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_CONTROL_MASK,
809 GTK_TYPE_MOVEMENT_STEP, GTK_MOVEMENT_BUFFER_ENDS,
811 G_TYPE_BOOLEAN, TRUE);
813 gtk_binding_entry_add_signal (binding_set, GDK_slash, GDK_CONTROL_MASK,
815 GTK_TYPE_MOVEMENT_STEP, GTK_MOVEMENT_BUFFER_ENDS,
817 G_TYPE_BOOLEAN, FALSE);
818 gtk_binding_entry_add_signal (binding_set, GDK_slash, GDK_CONTROL_MASK,
820 GTK_TYPE_MOVEMENT_STEP, GTK_MOVEMENT_BUFFER_ENDS,
822 G_TYPE_BOOLEAN, TRUE);
825 gtk_binding_entry_add_signal (binding_set, GDK_backslash, GDK_CONTROL_MASK,
827 GTK_TYPE_MOVEMENT_STEP, GTK_MOVEMENT_VISUAL_POSITIONS,
829 G_TYPE_BOOLEAN, FALSE);
830 gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
832 GTK_TYPE_MOVEMENT_STEP, GTK_MOVEMENT_VISUAL_POSITIONS,
834 G_TYPE_BOOLEAN, FALSE);
838 gtk_binding_entry_add_signal (binding_set, GDK_Return, 0,
840 gtk_binding_entry_add_signal (binding_set, GDK_KP_Enter, 0,
844 gtk_binding_entry_add_signal (binding_set, GDK_Delete, 0,
845 "delete_from_cursor", 2,
846 G_TYPE_ENUM, GTK_DELETE_CHARS,
849 gtk_binding_entry_add_signal (binding_set, GDK_KP_Delete, 0,
850 "delete_from_cursor", 2,
851 G_TYPE_ENUM, GTK_DELETE_CHARS,
854 gtk_binding_entry_add_signal (binding_set, GDK_BackSpace, 0,
857 /* Make this do the same as Backspace, to help with mis-typing */
858 gtk_binding_entry_add_signal (binding_set, GDK_BackSpace, GDK_SHIFT_MASK,
861 gtk_binding_entry_add_signal (binding_set, GDK_Delete, GDK_CONTROL_MASK,
862 "delete_from_cursor", 2,
863 G_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
866 gtk_binding_entry_add_signal (binding_set, GDK_KP_Delete, GDK_CONTROL_MASK,
867 "delete_from_cursor", 2,
868 G_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
871 gtk_binding_entry_add_signal (binding_set, GDK_BackSpace, GDK_CONTROL_MASK,
872 "delete_from_cursor", 2,
873 G_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
878 gtk_binding_entry_add_signal (binding_set, GDK_x, GDK_CONTROL_MASK,
880 gtk_binding_entry_add_signal (binding_set, GDK_c, GDK_CONTROL_MASK,
881 "copy_clipboard", 0);
882 gtk_binding_entry_add_signal (binding_set, GDK_v, GDK_CONTROL_MASK,
883 "paste_clipboard", 0);
885 gtk_binding_entry_add_signal (binding_set, GDK_Delete, GDK_SHIFT_MASK,
887 gtk_binding_entry_add_signal (binding_set, GDK_Insert, GDK_CONTROL_MASK,
888 "copy_clipboard", 0);
889 gtk_binding_entry_add_signal (binding_set, GDK_Insert, GDK_SHIFT_MASK,
890 "paste_clipboard", 0);
893 gtk_binding_entry_add_signal (binding_set, GDK_Insert, 0,
894 "toggle_overwrite", 0);
895 gtk_binding_entry_add_signal (binding_set, GDK_KP_Insert, 0,
896 "toggle_overwrite", 0);
899 * GtkEntry:inner-border:
901 * Sets the text area's border between the text and the frame
905 gtk_widget_class_install_style_property (widget_class,
906 g_param_spec_boxed ("inner-border",
908 P_("Border between text and frame."),
910 GTK_PARAM_READABLE));
912 gtk_settings_install_property (g_param_spec_boolean ("gtk-entry-select-on-focus",
913 P_("Select on focus"),
914 P_("Whether to select the contents of an entry when it is focused"),
916 GTK_PARAM_READWRITE));
919 * GtkSettings:gtk-entry-password-hint-timeout:
921 * How long to show the last inputted character in hidden
922 * entries. This value is in milliseconds. 0 disables showing the
923 * last char. 600 is a good value for enabling it.
927 gtk_settings_install_property (g_param_spec_uint ("gtk-entry-password-hint-timeout",
928 P_("Password Hint Timeout"),
929 P_("How long to show the last inputted character in hidden entries"),
931 GTK_PARAM_READWRITE));
933 g_type_class_add_private (gobject_class, sizeof (GtkEntryPrivate));
937 gtk_entry_editable_init (GtkEditableClass *iface)
939 iface->do_insert_text = gtk_entry_insert_text;
940 iface->do_delete_text = gtk_entry_delete_text;
941 iface->insert_text = gtk_entry_real_insert_text;
942 iface->delete_text = gtk_entry_real_delete_text;
943 iface->get_chars = gtk_entry_get_chars;
944 iface->set_selection_bounds = gtk_entry_set_selection_bounds;
945 iface->get_selection_bounds = gtk_entry_get_selection_bounds;
946 iface->set_position = gtk_entry_real_set_position;
947 iface->get_position = gtk_entry_get_position;
951 gtk_entry_cell_editable_init (GtkCellEditableIface *iface)
953 iface->start_editing = gtk_entry_start_editing;
957 gtk_entry_set_property (GObject *object,
962 GtkEntry *entry = GTK_ENTRY (object);
968 gboolean new_value = g_value_get_boolean (value);
970 if (new_value != entry->editable)
974 gtk_entry_reset_im_context (entry);
975 if (GTK_WIDGET_HAS_FOCUS (entry))
976 gtk_im_context_focus_out (entry->im_context);
978 entry->preedit_length = 0;
979 entry->preedit_cursor = 0;
982 entry->editable = new_value;
984 if (new_value && GTK_WIDGET_HAS_FOCUS (entry))
985 gtk_im_context_focus_in (entry->im_context);
987 gtk_entry_queue_draw (entry);
992 case PROP_MAX_LENGTH:
993 gtk_entry_set_max_length (entry, g_value_get_int (value));
996 case PROP_VISIBILITY:
997 gtk_entry_set_visibility (entry, g_value_get_boolean (value));
1000 case PROP_HAS_FRAME:
1001 gtk_entry_set_has_frame (entry, g_value_get_boolean (value));
1004 case PROP_INNER_BORDER:
1005 gtk_entry_set_inner_border (entry, g_value_get_boxed (value));
1008 case PROP_INVISIBLE_CHAR:
1009 gtk_entry_set_invisible_char (entry, g_value_get_uint (value));
1012 case PROP_ACTIVATES_DEFAULT:
1013 gtk_entry_set_activates_default (entry, g_value_get_boolean (value));
1016 case PROP_WIDTH_CHARS:
1017 gtk_entry_set_width_chars (entry, g_value_get_int (value));
1021 gtk_entry_set_text (entry, g_value_get_string (value));
1025 gtk_entry_set_alignment (entry, g_value_get_float (value));
1028 case PROP_TRUNCATE_MULTILINE:
1029 entry->truncate_multiline = g_value_get_boolean (value);
1032 case PROP_SCROLL_OFFSET:
1033 case PROP_CURSOR_POSITION:
1035 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1041 gtk_entry_get_property (GObject *object,
1046 GtkEntry *entry = GTK_ENTRY (object);
1050 case PROP_CURSOR_POSITION:
1051 g_value_set_int (value, entry->current_pos);
1053 case PROP_SELECTION_BOUND:
1054 g_value_set_int (value, entry->selection_bound);
1057 g_value_set_boolean (value, entry->editable);
1059 case PROP_MAX_LENGTH:
1060 g_value_set_int (value, entry->text_max_length);
1062 case PROP_VISIBILITY:
1063 g_value_set_boolean (value, entry->visible);
1065 case PROP_HAS_FRAME:
1066 g_value_set_boolean (value, entry->has_frame);
1068 case PROP_INNER_BORDER:
1069 g_value_set_boxed (value, gtk_entry_get_inner_border (entry));
1071 case PROP_INVISIBLE_CHAR:
1072 g_value_set_uint (value, entry->invisible_char);
1074 case PROP_ACTIVATES_DEFAULT:
1075 g_value_set_boolean (value, entry->activates_default);
1077 case PROP_WIDTH_CHARS:
1078 g_value_set_int (value, entry->width_chars);
1080 case PROP_SCROLL_OFFSET:
1081 g_value_set_int (value, entry->scroll_offset);
1084 g_value_set_string (value, gtk_entry_get_text (entry));
1087 g_value_set_float (value, gtk_entry_get_alignment (entry));
1089 case PROP_TRUNCATE_MULTILINE:
1090 g_value_set_boolean (value, entry->truncate_multiline);
1094 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1100 gtk_entry_init (GtkEntry *entry)
1102 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
1104 GTK_WIDGET_SET_FLAGS (entry, GTK_CAN_FOCUS);
1106 entry->text_size = MIN_SIZE;
1107 entry->text = g_malloc (entry->text_size);
1108 entry->text[0] = '\0';
1110 entry->editable = TRUE;
1111 entry->visible = TRUE;
1112 entry->invisible_char = '*';
1113 entry->dnd_position = -1;
1114 entry->width_chars = -1;
1115 entry->is_cell_renderer = FALSE;
1116 entry->editing_canceled = FALSE;
1117 entry->has_frame = TRUE;
1118 entry->truncate_multiline = FALSE;
1121 gtk_drag_dest_set (GTK_WIDGET (entry),
1122 GTK_DEST_DEFAULT_HIGHLIGHT,
1124 GDK_ACTION_COPY | GDK_ACTION_MOVE);
1125 gtk_drag_dest_add_text_targets (GTK_WIDGET (entry));
1127 /* This object is completely private. No external entity can gain a reference
1128 * to it; so we create it here and destroy it in finalize().
1130 entry->im_context = gtk_im_multicontext_new ();
1132 g_signal_connect (entry->im_context, "commit",
1133 G_CALLBACK (gtk_entry_commit_cb), entry);
1134 g_signal_connect (entry->im_context, "preedit_changed",
1135 G_CALLBACK (gtk_entry_preedit_changed_cb), entry);
1136 g_signal_connect (entry->im_context, "retrieve_surrounding",
1137 G_CALLBACK (gtk_entry_retrieve_surrounding_cb), entry);
1138 g_signal_connect (entry->im_context, "delete_surrounding",
1139 G_CALLBACK (gtk_entry_delete_surrounding_cb), entry);
1143 * Overwrite a memory that might contain sensitive information.
1146 trash_area (gchar *area, gsize len)
1148 volatile gchar *varea = (volatile gchar *)area;
1154 gtk_entry_destroy (GtkObject *object)
1156 GtkEntry *entry = GTK_ENTRY (object);
1159 entry->current_pos = entry->selection_bound = entry->text_length = 0;
1160 gtk_entry_reset_im_context (entry);
1161 gtk_entry_reset_layout (entry);
1163 if (entry->blink_timeout)
1165 g_source_remove (entry->blink_timeout);
1166 entry->blink_timeout = 0;
1169 if (entry->recompute_idle)
1171 g_source_remove (entry->recompute_idle);
1172 entry->recompute_idle = 0;
1175 if (!entry->visible)
1177 /* We want to trash the text here because the entry might be leaked. */
1178 trash_area (entry->text, strlen (entry->text));
1181 GTK_OBJECT_CLASS (parent_class)->destroy (object);
1185 gtk_entry_finalize (GObject *object)
1187 GtkEntry *entry = GTK_ENTRY (object);
1189 gtk_entry_set_completion (entry, NULL);
1191 if (entry->cached_layout)
1192 g_object_unref (entry->cached_layout);
1194 g_object_unref (entry->im_context);
1196 if (entry->blink_timeout)
1197 g_source_remove (entry->blink_timeout);
1199 if (entry->recompute_idle)
1200 g_source_remove (entry->recompute_idle);
1202 entry->text_size = 0;
1206 if (!entry->visible)
1207 trash_area (entry->text, strlen (entry->text));
1208 g_free (entry->text);
1212 G_OBJECT_CLASS (parent_class)->finalize (object);
1216 gtk_entry_realize (GtkWidget *widget)
1219 GdkWindowAttr attributes;
1220 gint attributes_mask;
1222 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
1223 entry = GTK_ENTRY (widget);
1225 attributes.window_type = GDK_WINDOW_CHILD;
1227 get_widget_window_size (entry, &attributes.x, &attributes.y, &attributes.width, &attributes.height);
1229 attributes.wclass = GDK_INPUT_OUTPUT;
1230 attributes.visual = gtk_widget_get_visual (widget);
1231 attributes.colormap = gtk_widget_get_colormap (widget);
1232 attributes.event_mask = gtk_widget_get_events (widget);
1233 attributes.event_mask |= (GDK_EXPOSURE_MASK |
1234 GDK_BUTTON_PRESS_MASK |
1235 GDK_BUTTON_RELEASE_MASK |
1236 GDK_BUTTON1_MOTION_MASK |
1237 GDK_BUTTON3_MOTION_MASK |
1238 GDK_POINTER_MOTION_HINT_MASK |
1239 GDK_POINTER_MOTION_MASK |
1240 GDK_ENTER_NOTIFY_MASK |
1241 GDK_LEAVE_NOTIFY_MASK);
1242 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
1244 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
1245 gdk_window_set_user_data (widget->window, entry);
1247 get_text_area_size (entry, &attributes.x, &attributes.y, &attributes.width, &attributes.height);
1249 attributes.cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget), GDK_XTERM);
1250 attributes_mask |= GDK_WA_CURSOR;
1252 entry->text_area = gdk_window_new (widget->window, &attributes, attributes_mask);
1253 gdk_window_set_user_data (entry->text_area, entry);
1255 gdk_cursor_unref (attributes.cursor);
1257 widget->style = gtk_style_attach (widget->style, widget->window);
1259 gdk_window_set_background (widget->window, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1260 gdk_window_set_background (entry->text_area, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1262 gdk_window_show (entry->text_area);
1264 gtk_im_context_set_client_window (entry->im_context, entry->text_area);
1266 gtk_entry_adjust_scroll (entry);
1267 gtk_entry_update_primary_selection (entry);
1271 gtk_entry_unrealize (GtkWidget *widget)
1273 GtkEntry *entry = GTK_ENTRY (widget);
1274 GtkClipboard *clipboard;
1276 gtk_entry_reset_layout (entry);
1278 gtk_im_context_set_client_window (entry->im_context, NULL);
1280 clipboard = gtk_widget_get_clipboard (widget, GDK_SELECTION_PRIMARY);
1281 if (gtk_clipboard_get_owner (clipboard) == G_OBJECT (entry))
1282 gtk_clipboard_clear (clipboard);
1284 if (entry->text_area)
1286 gdk_window_set_user_data (entry->text_area, NULL);
1287 gdk_window_destroy (entry->text_area);
1288 entry->text_area = NULL;
1291 if (entry->popup_menu)
1293 gtk_widget_destroy (entry->popup_menu);
1294 entry->popup_menu = NULL;
1297 if (GTK_WIDGET_CLASS (parent_class)->unrealize)
1298 (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
1302 _gtk_entry_get_borders (GtkEntry *entry,
1306 GtkWidget *widget = GTK_WIDGET (entry);
1308 gboolean interior_focus;
1310 gtk_widget_style_get (widget,
1311 "interior-focus", &interior_focus,
1312 "focus-line-width", &focus_width,
1315 if (entry->has_frame)
1317 *xborder = widget->style->xthickness;
1318 *yborder = widget->style->ythickness;
1326 if (!interior_focus)
1328 *xborder += focus_width;
1329 *yborder += focus_width;
1334 gtk_entry_size_request (GtkWidget *widget,
1335 GtkRequisition *requisition)
1337 GtkEntry *entry = GTK_ENTRY (widget);
1338 PangoFontMetrics *metrics;
1339 gint xborder, yborder;
1340 GtkBorder inner_border;
1341 PangoContext *context;
1343 gtk_widget_ensure_style (widget);
1344 context = gtk_widget_get_pango_context (widget);
1345 metrics = pango_context_get_metrics (context,
1346 widget->style->font_desc,
1347 pango_context_get_language (context));
1349 entry->ascent = pango_font_metrics_get_ascent (metrics);
1350 entry->descent = pango_font_metrics_get_descent (metrics);
1352 _gtk_entry_get_borders (entry, &xborder, &yborder);
1353 get_inner_border (entry, &inner_border);
1355 if (entry->width_chars < 0)
1356 requisition->width = MIN_ENTRY_WIDTH + xborder * 2 + inner_border.left + inner_border.right;
1359 gint char_width = pango_font_metrics_get_approximate_char_width (metrics);
1360 gint digit_width = pango_font_metrics_get_approximate_digit_width (metrics);
1361 gint char_pixels = (MAX (char_width, digit_width) + PANGO_SCALE - 1) / PANGO_SCALE;
1363 requisition->width = char_pixels * entry->width_chars + xborder * 2 + inner_border.left + inner_border.right;
1366 requisition->height = PANGO_PIXELS (entry->ascent + entry->descent) + yborder * 2 + inner_border.top + inner_border.bottom;
1368 pango_font_metrics_unref (metrics);
1372 get_text_area_size (GtkEntry *entry,
1378 gint xborder, yborder;
1379 GtkRequisition requisition;
1380 GtkWidget *widget = GTK_WIDGET (entry);
1382 gtk_widget_get_child_requisition (widget, &requisition);
1384 _gtk_entry_get_borders (entry, &xborder, &yborder);
1393 *width = GTK_WIDGET (entry)->allocation.width - xborder * 2;
1396 *height = requisition.height - yborder * 2;
1400 get_widget_window_size (GtkEntry *entry,
1406 GtkRequisition requisition;
1407 GtkWidget *widget = GTK_WIDGET (entry);
1409 gtk_widget_get_child_requisition (widget, &requisition);
1412 *x = widget->allocation.x;
1416 if (entry->is_cell_renderer)
1417 *y = widget->allocation.y;
1419 *y = widget->allocation.y + (widget->allocation.height - requisition.height) / 2;
1423 *width = widget->allocation.width;
1427 if (entry->is_cell_renderer)
1428 *height = widget->allocation.height;
1430 *height = requisition.height;
1435 get_inner_border (GtkEntry *entry,
1438 GtkBorder *tmp_border;
1440 tmp_border = g_object_get_qdata (G_OBJECT (entry), quark_inner_border);
1444 *border = *tmp_border;
1448 gtk_widget_style_get (GTK_WIDGET (entry), "inner-border", &tmp_border, NULL);
1452 *border = *tmp_border;
1453 g_free (tmp_border);
1457 *border = default_inner_border;
1461 gtk_entry_size_allocate (GtkWidget *widget,
1462 GtkAllocation *allocation)
1464 GtkEntry *entry = GTK_ENTRY (widget);
1466 widget->allocation = *allocation;
1468 if (GTK_WIDGET_REALIZED (widget))
1470 /* We call gtk_widget_get_child_requisition, since we want (for
1471 * backwards compatibility reasons) the realization here to
1472 * be affected by the usize of the entry, if set
1474 gint x, y, width, height;
1476 get_widget_window_size (entry, &x, &y, &width, &height);
1478 gdk_window_move_resize (widget->window,
1479 x, y, width, height);
1481 get_text_area_size (entry, &x, &y, &width, &height);
1483 gdk_window_move_resize (entry->text_area,
1484 x, y, width, height);
1486 gtk_entry_recompute (entry);
1491 gtk_entry_draw_frame (GtkWidget *widget)
1495 gboolean interior_focus;
1498 gtk_widget_style_get (widget,
1499 "interior-focus", &interior_focus,
1500 "focus-line-width", &focus_width,
1503 gdk_drawable_get_size (widget->window, &width, &height);
1505 if (GTK_WIDGET_HAS_FOCUS (widget) && !interior_focus)
1509 width -= 2 * focus_width;
1510 height -= 2 * focus_width;
1513 gtk_paint_shadow (widget->style, widget->window,
1514 GTK_STATE_NORMAL, GTK_SHADOW_IN,
1515 NULL, widget, "entry",
1516 x, y, width, height);
1518 if (GTK_WIDGET_HAS_FOCUS (widget) && !interior_focus)
1522 width += 2 * focus_width;
1523 height += 2 * focus_width;
1525 gtk_paint_focus (widget->style, widget->window, GTK_WIDGET_STATE (widget),
1526 NULL, widget, "entry",
1527 0, 0, width, height);
1532 gtk_entry_expose (GtkWidget *widget,
1533 GdkEventExpose *event)
1535 GtkEntry *entry = GTK_ENTRY (widget);
1537 if (widget->window == event->window)
1538 gtk_entry_draw_frame (widget);
1539 else if (entry->text_area == event->window)
1541 gint area_width, area_height;
1543 get_text_area_size (entry, NULL, NULL, &area_width, &area_height);
1545 gtk_paint_flat_box (widget->style, entry->text_area,
1546 GTK_WIDGET_STATE(widget), GTK_SHADOW_NONE,
1547 NULL, widget, "entry_bg",
1548 0, 0, area_width, area_height);
1550 if ((entry->visible || entry->invisible_char != 0) &&
1551 GTK_WIDGET_HAS_FOCUS (widget) &&
1552 entry->selection_bound == entry->current_pos && entry->cursor_visible)
1553 gtk_entry_draw_cursor (GTK_ENTRY (widget), CURSOR_STANDARD);
1555 if (entry->dnd_position != -1)
1556 gtk_entry_draw_cursor (GTK_ENTRY (widget), CURSOR_DND);
1558 gtk_entry_draw_text (GTK_ENTRY (widget));
1565 gtk_entry_get_pixel_ranges (GtkEntry *entry,
1569 gint start_char, end_char;
1571 if (gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &start_char, &end_char))
1573 PangoLayout *layout = gtk_entry_ensure_layout (entry, TRUE);
1574 PangoLayoutLine *line = pango_layout_get_lines (layout)->data;
1575 const char *text = pango_layout_get_text (layout);
1576 gint start_index = g_utf8_offset_to_pointer (text, start_char) - text;
1577 gint end_index = g_utf8_offset_to_pointer (text, end_char) - text;
1578 gint real_n_ranges, i;
1580 pango_layout_line_get_x_ranges (line, start_index, end_index, ranges, &real_n_ranges);
1586 for (i = 0; i < real_n_ranges; ++i)
1588 r[2 * i + 1] = (r[2 * i + 1] - r[2 * i]) / PANGO_SCALE;
1589 r[2 * i] = r[2 * i] / PANGO_SCALE;
1594 *n_ranges = real_n_ranges;
1606 in_selection (GtkEntry *entry,
1611 gint retval = FALSE;
1613 gtk_entry_get_pixel_ranges (entry, &ranges, &n_ranges);
1615 for (i = 0; i < n_ranges; ++i)
1617 if (x >= ranges[2 * i] && x < ranges[2 * i] + ranges[2 * i + 1])
1629 gtk_entry_button_press (GtkWidget *widget,
1630 GdkEventButton *event)
1632 GtkEntry *entry = GTK_ENTRY (widget);
1633 GtkEditable *editable = GTK_EDITABLE (widget);
1634 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
1636 gint sel_start, sel_end;
1638 if (event->window != entry->text_area ||
1639 (entry->button && event->button != entry->button))
1642 entry->button = event->button;
1644 if (!GTK_WIDGET_HAS_FOCUS (widget))
1646 entry->in_click = TRUE;
1647 gtk_widget_grab_focus (widget);
1648 entry->in_click = FALSE;
1651 tmp_pos = gtk_entry_find_position (entry, event->x + entry->scroll_offset);
1653 if (event->button == 1)
1655 gboolean have_selection = gtk_editable_get_selection_bounds (editable, &sel_start, &sel_end);
1657 entry->select_words = FALSE;
1658 entry->select_lines = FALSE;
1660 if (event->state & GDK_SHIFT_MASK)
1662 gtk_entry_reset_im_context (entry);
1664 if (!have_selection) /* select from the current position to the clicked position */
1665 sel_start = sel_end = entry->current_pos;
1667 if (tmp_pos > sel_start && tmp_pos < sel_end)
1669 /* Truncate current selection */
1670 gtk_entry_set_positions (entry, tmp_pos, -1);
1674 gboolean extend_to_left;
1677 /* Figure out what click selects and extend current selection */
1678 switch (event->type)
1680 case GDK_BUTTON_PRESS:
1681 gtk_entry_set_positions (entry, tmp_pos, tmp_pos);
1684 case GDK_2BUTTON_PRESS:
1685 entry->select_words = TRUE;
1686 gtk_entry_select_word (entry);
1689 case GDK_3BUTTON_PRESS:
1690 entry->select_lines = TRUE;
1691 gtk_entry_select_line (entry);
1698 start = MIN (entry->current_pos, entry->selection_bound);
1699 start = MIN (sel_start, start);
1701 end = MAX (entry->current_pos, entry->selection_bound);
1702 end = MAX (sel_end, end);
1704 if (tmp_pos == sel_start || tmp_pos == sel_end)
1705 extend_to_left = (tmp_pos == start);
1707 extend_to_left = (end == sel_end);
1710 gtk_entry_set_positions (entry, start, end);
1712 gtk_entry_set_positions (entry, end, start);
1715 else /* no shift key */
1716 switch (event->type)
1718 case GDK_BUTTON_PRESS:
1719 if (in_selection (entry, event->x + entry->scroll_offset))
1721 /* Click inside the selection - we'll either start a drag, or
1722 * clear the selection
1724 entry->in_drag = TRUE;
1725 entry->drag_start_x = event->x + entry->scroll_offset;
1726 entry->drag_start_y = event->y + entry->scroll_offset;
1729 gtk_editable_set_position (editable, tmp_pos);
1732 case GDK_2BUTTON_PRESS:
1733 /* We ALWAYS receive a GDK_BUTTON_PRESS immediately before
1734 * receiving a GDK_2BUTTON_PRESS so we need to reset
1735 * entry->in_drag which may have been set above
1737 entry->in_drag = FALSE;
1738 entry->select_words = TRUE;
1739 gtk_entry_select_word (entry);
1742 case GDK_3BUTTON_PRESS:
1743 /* We ALWAYS receive a GDK_BUTTON_PRESS immediately before
1744 * receiving a GDK_3BUTTON_PRESS so we need to reset
1745 * entry->in_drag which may have been set above
1747 entry->in_drag = FALSE;
1748 entry->select_lines = TRUE;
1749 gtk_entry_select_line (entry);
1758 else if (event->button == 2 && event->type == GDK_BUTTON_PRESS && entry->editable)
1760 priv->insert_pos = tmp_pos;
1761 gtk_entry_paste (entry, GDK_SELECTION_PRIMARY);
1765 else if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
1767 gtk_entry_do_popup (entry, event);
1768 entry->button = 0; /* Don't wait for release, since the menu will gtk_grab_add */
1777 gtk_entry_button_release (GtkWidget *widget,
1778 GdkEventButton *event)
1780 GtkEntry *entry = GTK_ENTRY (widget);
1782 if (event->window != entry->text_area || entry->button != event->button)
1787 gint tmp_pos = gtk_entry_find_position (entry, entry->drag_start_x);
1789 gtk_editable_set_position (GTK_EDITABLE (entry), tmp_pos);
1796 gtk_entry_update_primary_selection (entry);
1802 _gtk_entry_get_selected_text (GtkEntry *entry)
1804 GtkEditable *editable = GTK_EDITABLE (entry);
1805 gint start_text, end_text;
1808 if (gtk_editable_get_selection_bounds (editable, &start_text, &end_text))
1809 text = gtk_editable_get_chars (editable, start_text, end_text);
1815 drag_begin_cb (GtkWidget *widget,
1816 GdkDragContext *context,
1819 g_signal_handlers_disconnect_by_func (widget, drag_begin_cb, NULL);
1823 gtk_entry_motion_notify (GtkWidget *widget,
1824 GdkEventMotion *event)
1826 GtkEntry *entry = GTK_ENTRY (widget);
1829 if (entry->mouse_cursor_obscured)
1833 cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget), GDK_XTERM);
1834 gdk_window_set_cursor (entry->text_area, cursor);
1835 gdk_cursor_unref (cursor);
1836 entry->mouse_cursor_obscured = FALSE;
1839 if (event->window != entry->text_area || entry->button != 1)
1842 if (entry->select_lines)
1845 if (event->is_hint || (entry->text_area != event->window))
1846 gdk_window_get_pointer (entry->text_area, NULL, NULL, NULL);
1850 if (gtk_drag_check_threshold (widget,
1851 entry->drag_start_x, entry->drag_start_y,
1852 event->x + entry->scroll_offset, event->y))
1854 GdkDragContext *context;
1855 GtkTargetList *target_list = gtk_target_list_new (NULL, 0);
1856 guint actions = entry->editable ? GDK_ACTION_COPY | GDK_ACTION_MOVE : GDK_ACTION_COPY;
1858 GdkPixmap *pixmap = NULL;
1860 gtk_target_list_add_text_targets (target_list, 0);
1864 text = _gtk_entry_get_selected_text (entry);
1865 pixmap = _gtk_text_util_create_drag_icon (widget, text, -1);
1868 context = gtk_drag_begin (widget, target_list, actions,
1869 entry->button, (GdkEvent *)event);
1872 gtk_drag_set_icon_pixmap (context,
1873 gdk_drawable_get_colormap (pixmap),
1878 gtk_drag_set_icon_default (context);
1881 g_object_unref (pixmap);
1884 entry->in_drag = FALSE;
1887 gtk_target_list_unref (target_list);
1893 gdk_drawable_get_size (entry->text_area, NULL, &height);
1897 else if (event->y >= height)
1898 tmp_pos = entry->text_length;
1900 tmp_pos = gtk_entry_find_position (entry, event->x + entry->scroll_offset);
1902 if (entry->select_words)
1905 gint old_min, old_max;
1908 min = gtk_entry_move_backward_word (entry, tmp_pos, TRUE);
1909 max = gtk_entry_move_forward_word (entry, tmp_pos, TRUE);
1911 pos = entry->current_pos;
1912 bound = entry->selection_bound;
1914 old_min = MIN(entry->current_pos, entry->selection_bound);
1915 old_max = MAX(entry->current_pos, entry->selection_bound);
1922 else if (old_max < max)
1927 else if (pos == old_min)
1929 if (entry->current_pos != min)
1934 if (entry->current_pos != max)
1938 gtk_entry_set_positions (entry, pos, bound);
1941 gtk_entry_set_positions (entry, tmp_pos, -1);
1948 set_invisible_cursor (GdkWindow *window)
1950 GdkBitmap *empty_bitmap;
1953 char invisible_cursor_bits[] = { 0x0 };
1955 useless.red = useless.green = useless.blue = 0;
1958 empty_bitmap = gdk_bitmap_create_from_data (window,
1959 invisible_cursor_bits,
1962 cursor = gdk_cursor_new_from_pixmap (empty_bitmap,
1967 gdk_window_set_cursor (window, cursor);
1969 gdk_cursor_unref (cursor);
1971 g_object_unref (empty_bitmap);
1975 gtk_entry_obscure_mouse_cursor (GtkEntry *entry)
1977 if (entry->mouse_cursor_obscured)
1980 set_invisible_cursor (entry->text_area);
1982 entry->mouse_cursor_obscured = TRUE;
1986 gtk_entry_key_press (GtkWidget *widget,
1989 GtkEntry *entry = GTK_ENTRY (widget);
1991 gtk_entry_pend_cursor_blink (entry);
1993 if (entry->editable)
1995 if (gtk_im_context_filter_keypress (entry->im_context, event))
1997 gtk_entry_obscure_mouse_cursor (entry);
1998 entry->need_im_reset = TRUE;
2003 if (event->keyval == GDK_Return ||
2004 event->keyval == GDK_KP_Enter ||
2005 event->keyval == GDK_ISO_Enter ||
2006 event->keyval == GDK_Escape)
2008 GtkEntryCompletion *completion = gtk_entry_get_completion (entry);
2010 if (completion && completion->priv->completion_timeout)
2012 g_source_remove (completion->priv->completion_timeout);
2013 completion->priv->completion_timeout = 0;
2016 gtk_entry_reset_im_context (entry);
2019 if (GTK_WIDGET_CLASS (parent_class)->key_press_event (widget, event))
2020 /* Activate key bindings
2028 gtk_entry_key_release (GtkWidget *widget,
2031 GtkEntry *entry = GTK_ENTRY (widget);
2033 if (entry->editable)
2035 if (gtk_im_context_filter_keypress (entry->im_context, event))
2037 entry->need_im_reset = TRUE;
2042 return GTK_WIDGET_CLASS (parent_class)->key_release_event (widget, event);
2046 gtk_entry_focus_in (GtkWidget *widget,
2047 GdkEventFocus *event)
2049 GtkEntry *entry = GTK_ENTRY (widget);
2051 gtk_widget_queue_draw (widget);
2053 if (entry->editable)
2055 entry->need_im_reset = TRUE;
2056 gtk_im_context_focus_in (entry->im_context);
2059 g_signal_connect (gdk_keymap_get_for_display (gtk_widget_get_display (widget)),
2060 "direction_changed",
2061 G_CALLBACK (gtk_entry_keymap_direction_changed), entry);
2063 gtk_entry_check_cursor_blink (entry);
2069 gtk_entry_focus_out (GtkWidget *widget,
2070 GdkEventFocus *event)
2072 GtkEntry *entry = GTK_ENTRY (widget);
2073 GtkEntryCompletion *completion;
2075 gtk_widget_queue_draw (widget);
2077 if (entry->editable)
2079 entry->need_im_reset = TRUE;
2080 gtk_im_context_focus_out (entry->im_context);
2083 gtk_entry_check_cursor_blink (entry);
2085 g_signal_handlers_disconnect_by_func (gdk_keymap_get_for_display (gtk_widget_get_display (widget)),
2086 gtk_entry_keymap_direction_changed,
2089 completion = gtk_entry_get_completion (entry);
2091 _gtk_entry_completion_popdown (completion);
2097 gtk_entry_grab_focus (GtkWidget *widget)
2099 GtkEntry *entry = GTK_ENTRY (widget);
2100 gboolean select_on_focus;
2102 GTK_WIDGET_CLASS (parent_class)->grab_focus (widget);
2104 g_object_get (gtk_widget_get_settings (widget),
2105 "gtk-entry-select-on-focus",
2109 if (select_on_focus && entry->editable && !entry->in_click)
2110 gtk_editable_select_region (GTK_EDITABLE (widget), 0, -1);
2114 gtk_entry_direction_changed (GtkWidget *widget,
2115 GtkTextDirection previous_dir)
2117 GtkEntry *entry = GTK_ENTRY (widget);
2119 gtk_entry_recompute (entry);
2121 GTK_WIDGET_CLASS (parent_class)->direction_changed (widget, previous_dir);
2125 gtk_entry_state_changed (GtkWidget *widget,
2126 GtkStateType previous_state)
2128 GtkEntry *entry = GTK_ENTRY (widget);
2130 if (GTK_WIDGET_REALIZED (widget))
2132 gdk_window_set_background (widget->window, &widget->style->base[GTK_WIDGET_STATE (widget)]);
2133 gdk_window_set_background (entry->text_area, &widget->style->base[GTK_WIDGET_STATE (widget)]);
2136 if (!GTK_WIDGET_IS_SENSITIVE (widget))
2138 /* Clear any selection */
2139 gtk_editable_select_region (GTK_EDITABLE (entry), entry->current_pos, entry->current_pos);
2142 gtk_widget_queue_draw (widget);
2146 gtk_entry_screen_changed (GtkWidget *widget,
2147 GdkScreen *old_screen)
2149 gtk_entry_recompute (GTK_ENTRY (widget));
2152 /* GtkEditable method implementations
2155 gtk_entry_insert_text (GtkEditable *editable,
2156 const gchar *new_text,
2157 gint new_text_length,
2160 GtkEntry *entry = GTK_ENTRY (editable);
2164 if (*position < 0 || *position > entry->text_length)
2165 *position = entry->text_length;
2167 g_object_ref (editable);
2169 if (new_text_length <= 63)
2172 text = g_new (gchar, new_text_length + 1);
2174 text[new_text_length] = '\0';
2175 strncpy (text, new_text, new_text_length);
2177 g_signal_emit_by_name (editable, "insert_text", text, new_text_length, position);
2179 if (!entry->visible)
2180 trash_area (text, new_text_length);
2182 if (new_text_length > 63)
2185 g_object_unref (editable);
2189 gtk_entry_delete_text (GtkEditable *editable,
2193 GtkEntry *entry = GTK_ENTRY (editable);
2195 if (end_pos < 0 || end_pos > entry->text_length)
2196 end_pos = entry->text_length;
2199 if (start_pos > end_pos)
2200 start_pos = end_pos;
2202 g_object_ref (editable);
2204 g_signal_emit_by_name (editable, "delete_text", start_pos, end_pos);
2206 g_object_unref (editable);
2210 gtk_entry_get_chars (GtkEditable *editable,
2214 GtkEntry *entry = GTK_ENTRY (editable);
2215 gint start_index, end_index;
2218 end_pos = entry->text_length;
2220 start_pos = MIN (entry->text_length, start_pos);
2221 end_pos = MIN (entry->text_length, end_pos);
2223 start_index = g_utf8_offset_to_pointer (entry->text, start_pos) - entry->text;
2224 end_index = g_utf8_offset_to_pointer (entry->text, end_pos) - entry->text;
2226 return g_strndup (entry->text + start_index, end_index - start_index);
2230 gtk_entry_set_position_internal (GtkEntry *entry,
2234 if (position < 0 || position > entry->text_length)
2235 position = entry->text_length;
2237 if (position != entry->current_pos ||
2238 position != entry->selection_bound)
2241 gtk_entry_reset_im_context (entry);
2242 gtk_entry_set_positions (entry, position, position);
2247 gtk_entry_real_set_position (GtkEditable *editable,
2250 gtk_entry_set_position_internal (GTK_ENTRY (editable), position, TRUE);
2254 gtk_entry_get_position (GtkEditable *editable)
2256 return GTK_ENTRY (editable)->current_pos;
2260 gtk_entry_set_selection_bounds (GtkEditable *editable,
2264 GtkEntry *entry = GTK_ENTRY (editable);
2267 start = entry->text_length;
2269 end = entry->text_length;
2271 gtk_entry_reset_im_context (entry);
2273 gtk_entry_set_positions (entry,
2274 MIN (end, entry->text_length),
2275 MIN (start, entry->text_length));
2277 gtk_entry_update_primary_selection (entry);
2281 gtk_entry_get_selection_bounds (GtkEditable *editable,
2285 GtkEntry *entry = GTK_ENTRY (editable);
2287 *start = entry->selection_bound;
2288 *end = entry->current_pos;
2290 return (entry->selection_bound != entry->current_pos);
2294 gtk_entry_style_set (GtkWidget *widget,
2295 GtkStyle *previous_style)
2297 GtkEntry *entry = GTK_ENTRY (widget);
2299 gtk_entry_recompute (entry);
2301 if (previous_style && GTK_WIDGET_REALIZED (widget))
2303 gdk_window_set_background (widget->window, &widget->style->base[GTK_WIDGET_STATE (widget)]);
2304 gdk_window_set_background (entry->text_area, &widget->style->base[GTK_WIDGET_STATE (widget)]);
2308 /* GtkCellEditable method implementations
2311 gtk_cell_editable_entry_activated (GtkEntry *entry, gpointer data)
2313 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (entry));
2314 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (entry));
2318 gtk_cell_editable_key_press_event (GtkEntry *entry,
2319 GdkEventKey *key_event,
2322 if (key_event->keyval == GDK_Escape)
2324 entry->editing_canceled = TRUE;
2325 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (entry));
2326 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (entry));
2331 /* override focus */
2332 if (key_event->keyval == GDK_Up || key_event->keyval == GDK_Down)
2334 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (entry));
2335 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (entry));
2344 gtk_entry_start_editing (GtkCellEditable *cell_editable,
2347 GTK_ENTRY (cell_editable)->is_cell_renderer = TRUE;
2349 g_signal_connect (cell_editable, "activate",
2350 G_CALLBACK (gtk_cell_editable_entry_activated), NULL);
2351 g_signal_connect (cell_editable, "key_press_event",
2352 G_CALLBACK (gtk_cell_editable_key_press_event), NULL);
2356 gtk_entry_password_hint_free (GtkEntryPasswordHint *password_hint)
2358 if (password_hint->password_hint_timeout_id)
2359 g_source_remove (password_hint->password_hint_timeout_id);
2361 g_free (password_hint);
2364 /* Default signal handlers
2367 gtk_entry_real_insert_text (GtkEditable *editable,
2368 const gchar *new_text,
2369 gint new_text_length,
2372 GtkEntry *entry = GTK_ENTRY (editable);
2375 guint password_hint_timeout;
2377 if (new_text_length < 0)
2378 new_text_length = strlen (new_text);
2380 n_chars = g_utf8_strlen (new_text, new_text_length);
2381 if (entry->text_max_length > 0 && n_chars + entry->text_length > entry->text_max_length)
2383 gdk_display_beep (gtk_widget_get_display (GTK_WIDGET (entry)));
2384 n_chars = entry->text_max_length - entry->text_length;
2385 new_text_length = g_utf8_offset_to_pointer (new_text, n_chars) - new_text;
2388 if (new_text_length + entry->n_bytes + 1 > entry->text_size)
2390 gsize prev_size = entry->text_size;
2392 while (new_text_length + entry->n_bytes + 1 > entry->text_size)
2394 if (entry->text_size == 0)
2395 entry->text_size = MIN_SIZE;
2398 if (2 * (guint)entry->text_size < MAX_SIZE &&
2399 2 * (guint)entry->text_size > entry->text_size)
2400 entry->text_size *= 2;
2403 entry->text_size = MAX_SIZE;
2404 if (new_text_length > (gint)entry->text_size - (gint)entry->n_bytes - 1)
2406 new_text_length = (gint)entry->text_size - (gint)entry->n_bytes - 1;
2407 new_text_length = g_utf8_find_prev_char (new_text, new_text + new_text_length + 1) - new_text;
2408 n_chars = g_utf8_strlen (new_text, new_text_length);
2416 entry->text = g_realloc (entry->text, entry->text_size);
2419 /* Same thing, just slower and without leaving stuff in memory. */
2420 gchar *et_new = g_malloc (entry->text_size);
2421 memcpy (et_new, entry->text, MIN (prev_size, entry->text_size));
2422 trash_area (entry->text, prev_size);
2423 g_free (entry->text);
2424 entry->text = et_new;
2428 index = g_utf8_offset_to_pointer (entry->text, *position) - entry->text;
2430 g_memmove (entry->text + index + new_text_length, entry->text + index, entry->n_bytes - index);
2431 memcpy (entry->text + index, new_text, new_text_length);
2433 entry->n_bytes += new_text_length;
2434 entry->text_length += n_chars;
2436 /* NUL terminate for safety and convenience */
2437 entry->text[entry->n_bytes] = '\0';
2439 if (entry->current_pos > *position)
2440 entry->current_pos += n_chars;
2442 if (entry->selection_bound > *position)
2443 entry->selection_bound += n_chars;
2445 g_object_get (gtk_widget_get_settings (GTK_WIDGET (entry)),
2446 "gtk-entry-password-hint-timeout", &password_hint_timeout,
2449 if (password_hint_timeout > 0 && n_chars == 1 && !entry->visible &&
2450 (new_text_length < PASSWORD_HINT_MAX))
2452 GtkEntryPasswordHint *password_hint = g_object_get_qdata (G_OBJECT (entry),
2453 quark_password_hint);
2455 if (! password_hint)
2457 password_hint = g_new0 (GtkEntryPasswordHint, 1);
2458 g_object_set_qdata_full (G_OBJECT (entry), quark_password_hint,
2460 (GDestroyNotify) gtk_entry_password_hint_free);
2463 memset (&password_hint->password_hint, 0x0, PASSWORD_HINT_MAX);
2464 password_hint->password_hint_length = new_text_length;
2465 memcpy (&password_hint->password_hint, new_text, new_text_length);
2466 password_hint->password_hint_position = *position + n_chars;
2470 g_object_set_qdata (G_OBJECT (entry), quark_password_hint, NULL);
2473 *position += n_chars;
2475 gtk_entry_recompute (entry);
2477 g_signal_emit_by_name (editable, "changed");
2478 g_object_notify (G_OBJECT (editable), "text");
2482 gtk_entry_real_delete_text (GtkEditable *editable,
2486 GtkEntry *entry = GTK_ENTRY (editable);
2490 if (end_pos < 0 || end_pos > entry->text_length)
2491 end_pos = entry->text_length;
2493 if (start_pos < end_pos)
2495 gint start_index = g_utf8_offset_to_pointer (entry->text, start_pos) - entry->text;
2496 gint end_index = g_utf8_offset_to_pointer (entry->text, end_pos) - entry->text;
2498 gint selection_bound;
2500 g_memmove (entry->text + start_index, entry->text + end_index, entry->n_bytes + 1 - end_index);
2501 entry->text_length -= (end_pos - start_pos);
2502 entry->n_bytes -= (end_index - start_index);
2504 /* In password-mode, make sure we don't leave anything sensitive after
2505 * the terminating zero. Note, that the terminating zero already trashed
2508 if (!entry->visible)
2509 trash_area (entry->text + entry->n_bytes + 1, end_index - start_index - 1);
2511 current_pos = entry->current_pos;
2512 if (current_pos > start_pos)
2513 current_pos -= MIN (current_pos, end_pos) - start_pos;
2515 selection_bound = entry->selection_bound;
2516 if (selection_bound > start_pos)
2517 selection_bound -= MIN (selection_bound, end_pos) - start_pos;
2519 gtk_entry_set_positions (entry, current_pos, selection_bound);
2521 /* We might have deleted the selection
2523 gtk_entry_update_primary_selection (entry);
2525 gtk_entry_recompute (entry);
2527 g_signal_emit_by_name (editable, "changed");
2528 g_object_notify (G_OBJECT (editable), "text");
2532 /* Compute the X position for an offset that corresponds to the "more important
2533 * cursor position for that offset. We use this when trying to guess to which
2534 * end of the selection we should go to when the user hits the left or
2538 get_better_cursor_x (GtkEntry *entry,
2541 GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (entry)));
2542 PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
2543 gboolean split_cursor;
2545 PangoLayout *layout = gtk_entry_ensure_layout (entry, TRUE);
2546 const gchar *text = pango_layout_get_text (layout);
2547 gint index = g_utf8_offset_to_pointer (text, offset) - text;
2549 PangoRectangle strong_pos, weak_pos;
2551 g_object_get (gtk_widget_get_settings (GTK_WIDGET (entry)),
2552 "gtk-split-cursor", &split_cursor,
2555 pango_layout_get_cursor_pos (layout, index, &strong_pos, &weak_pos);
2558 return strong_pos.x / PANGO_SCALE;
2560 return (keymap_direction == entry->resolved_dir) ? strong_pos.x / PANGO_SCALE : weak_pos.x / PANGO_SCALE;
2564 gtk_entry_move_cursor (GtkEntry *entry,
2565 GtkMovementStep step,
2567 gboolean extend_selection)
2569 gint new_pos = entry->current_pos;
2571 gtk_entry_reset_im_context (entry);
2573 if (entry->current_pos != entry->selection_bound && !extend_selection)
2575 /* If we have a current selection and aren't extending it, move to the
2576 * start/or end of the selection as appropriate
2580 case GTK_MOVEMENT_VISUAL_POSITIONS:
2582 gint current_x = get_better_cursor_x (entry, entry->current_pos);
2583 gint bound_x = get_better_cursor_x (entry, entry->selection_bound);
2586 new_pos = current_x < bound_x ? entry->current_pos : entry->selection_bound;
2588 new_pos = current_x > bound_x ? entry->current_pos : entry->selection_bound;
2592 case GTK_MOVEMENT_LOGICAL_POSITIONS:
2593 case GTK_MOVEMENT_WORDS:
2595 new_pos = MIN (entry->current_pos, entry->selection_bound);
2597 new_pos = MAX (entry->current_pos, entry->selection_bound);
2599 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
2600 case GTK_MOVEMENT_PARAGRAPH_ENDS:
2601 case GTK_MOVEMENT_BUFFER_ENDS:
2602 new_pos = count < 0 ? 0 : entry->text_length;
2604 case GTK_MOVEMENT_DISPLAY_LINES:
2605 case GTK_MOVEMENT_PARAGRAPHS:
2606 case GTK_MOVEMENT_PAGES:
2607 case GTK_MOVEMENT_HORIZONTAL_PAGES:
2615 case GTK_MOVEMENT_LOGICAL_POSITIONS:
2616 new_pos = gtk_entry_move_logically (entry, new_pos, count);
2618 case GTK_MOVEMENT_VISUAL_POSITIONS:
2619 new_pos = gtk_entry_move_visually (entry, new_pos, count);
2621 case GTK_MOVEMENT_WORDS:
2624 new_pos = gtk_entry_move_forward_word (entry, new_pos, FALSE);
2629 new_pos = gtk_entry_move_backward_word (entry, new_pos, FALSE);
2633 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
2634 case GTK_MOVEMENT_PARAGRAPH_ENDS:
2635 case GTK_MOVEMENT_BUFFER_ENDS:
2636 new_pos = count < 0 ? 0 : entry->text_length;
2638 case GTK_MOVEMENT_DISPLAY_LINES:
2639 case GTK_MOVEMENT_PARAGRAPHS:
2640 case GTK_MOVEMENT_PAGES:
2641 case GTK_MOVEMENT_HORIZONTAL_PAGES:
2646 if (extend_selection)
2647 gtk_editable_select_region (GTK_EDITABLE (entry), entry->selection_bound, new_pos);
2649 gtk_editable_set_position (GTK_EDITABLE (entry), new_pos);
2651 gtk_entry_pend_cursor_blink (entry);
2655 gtk_entry_insert_at_cursor (GtkEntry *entry,
2658 GtkEditable *editable = GTK_EDITABLE (entry);
2659 gint pos = entry->current_pos;
2661 if (entry->editable)
2663 gtk_entry_reset_im_context (entry);
2665 gtk_editable_insert_text (editable, str, -1, &pos);
2666 gtk_editable_set_position (editable, pos);
2671 gtk_entry_delete_from_cursor (GtkEntry *entry,
2675 GtkEditable *editable = GTK_EDITABLE (entry);
2676 gint start_pos = entry->current_pos;
2677 gint end_pos = entry->current_pos;
2679 gtk_entry_reset_im_context (entry);
2681 if (!entry->editable)
2684 if (entry->selection_bound != entry->current_pos)
2686 gtk_editable_delete_selection (editable);
2692 case GTK_DELETE_CHARS:
2693 end_pos = gtk_entry_move_logically (entry, entry->current_pos, count);
2694 gtk_editable_delete_text (editable, MIN (start_pos, end_pos), MAX (start_pos, end_pos));
2696 case GTK_DELETE_WORDS:
2699 /* Move to end of current word, or if not on a word, end of previous word */
2700 end_pos = gtk_entry_move_backward_word (entry, end_pos, FALSE);
2701 end_pos = gtk_entry_move_forward_word (entry, end_pos, FALSE);
2705 /* Move to beginning of current word, or if not on a word, begining of next word */
2706 start_pos = gtk_entry_move_forward_word (entry, start_pos, FALSE);
2707 start_pos = gtk_entry_move_backward_word (entry, start_pos, FALSE);
2711 case GTK_DELETE_WORD_ENDS:
2714 start_pos = gtk_entry_move_backward_word (entry, start_pos, FALSE);
2719 end_pos = gtk_entry_move_forward_word (entry, end_pos, FALSE);
2722 gtk_editable_delete_text (editable, start_pos, end_pos);
2724 case GTK_DELETE_DISPLAY_LINE_ENDS:
2725 case GTK_DELETE_PARAGRAPH_ENDS:
2727 gtk_editable_delete_text (editable, 0, entry->current_pos);
2729 gtk_editable_delete_text (editable, entry->current_pos, -1);
2731 case GTK_DELETE_DISPLAY_LINES:
2732 case GTK_DELETE_PARAGRAPHS:
2733 gtk_editable_delete_text (editable, 0, -1);
2735 case GTK_DELETE_WHITESPACE:
2736 gtk_entry_delete_whitespace (entry);
2740 gtk_entry_pend_cursor_blink (entry);
2744 gtk_entry_backspace (GtkEntry *entry)
2746 GtkEditable *editable = GTK_EDITABLE (entry);
2749 gtk_entry_reset_im_context (entry);
2751 if (!entry->editable || !entry->text)
2754 if (entry->selection_bound != entry->current_pos)
2756 gtk_editable_delete_selection (editable);
2760 prev_pos = gtk_entry_move_logically(entry, entry->current_pos, -1);
2762 if (prev_pos < entry->current_pos)
2764 PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
2765 PangoLogAttr *log_attrs;
2768 pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
2770 if (log_attrs[entry->current_pos].backspace_deletes_character)
2772 gchar *cluster_text;
2773 gchar *normalized_text;
2776 cluster_text = gtk_editable_get_chars (editable,
2778 entry->current_pos);
2779 normalized_text = g_utf8_normalize (cluster_text,
2780 strlen (cluster_text),
2782 len = g_utf8_strlen (normalized_text, -1);
2784 gtk_editable_delete_text (editable, prev_pos, entry->current_pos);
2787 gint pos = entry->current_pos;
2789 gtk_editable_insert_text (editable, normalized_text,
2790 g_utf8_offset_to_pointer (normalized_text, len - 1) - normalized_text,
2792 gtk_editable_set_position (editable, pos);
2795 g_free (normalized_text);
2796 g_free (cluster_text);
2800 gtk_editable_delete_text (editable, prev_pos, entry->current_pos);
2806 gtk_entry_pend_cursor_blink (entry);
2810 gtk_entry_copy_clipboard (GtkEntry *entry)
2812 GtkEditable *editable = GTK_EDITABLE (entry);
2815 if (gtk_editable_get_selection_bounds (editable, &start, &end))
2817 gchar *str = gtk_entry_get_public_chars (entry, start, end);
2818 gtk_clipboard_set_text (gtk_widget_get_clipboard (GTK_WIDGET (entry),
2819 GDK_SELECTION_CLIPBOARD),
2826 gtk_entry_cut_clipboard (GtkEntry *entry)
2828 GtkEditable *editable = GTK_EDITABLE (entry);
2831 gtk_entry_copy_clipboard (entry);
2833 if (entry->editable)
2835 if (gtk_editable_get_selection_bounds (editable, &start, &end))
2836 gtk_editable_delete_text (editable, start, end);
2841 gtk_entry_paste_clipboard (GtkEntry *entry)
2843 if (entry->editable)
2844 gtk_entry_paste (entry, GDK_NONE);
2848 gtk_entry_delete_cb (GtkEntry *entry)
2850 GtkEditable *editable = GTK_EDITABLE (entry);
2853 if (entry->editable)
2855 if (gtk_editable_get_selection_bounds (editable, &start, &end))
2856 gtk_editable_delete_text (editable, start, end);
2861 gtk_entry_toggle_overwrite (GtkEntry *entry)
2863 entry->overwrite_mode = !entry->overwrite_mode;
2867 gtk_entry_select_all (GtkEntry *entry)
2869 gtk_entry_select_line (entry);
2873 gtk_entry_real_activate (GtkEntry *entry)
2876 GtkWidget *toplevel;
2879 widget = GTK_WIDGET (entry);
2881 if (entry->activates_default)
2883 toplevel = gtk_widget_get_toplevel (widget);
2884 if (GTK_IS_WINDOW (toplevel))
2886 window = GTK_WINDOW (toplevel);
2889 widget != window->default_widget &&
2890 !(widget == window->focus_widget &&
2891 (!window->default_widget || !GTK_WIDGET_SENSITIVE (window->default_widget))))
2892 gtk_window_activate_default (window);
2898 gtk_entry_keymap_direction_changed (GdkKeymap *keymap,
2901 gtk_entry_recompute (entry);
2904 /* IM Context Callbacks
2908 gtk_entry_commit_cb (GtkIMContext *context,
2912 if (entry->editable)
2913 gtk_entry_enter_text (entry, str);
2917 gtk_entry_preedit_changed_cb (GtkIMContext *context,
2920 if (entry->editable)
2922 gchar *preedit_string;
2925 gtk_im_context_get_preedit_string (entry->im_context,
2926 &preedit_string, NULL,
2928 entry->preedit_length = strlen (preedit_string);
2929 cursor_pos = CLAMP (cursor_pos, 0, g_utf8_strlen (preedit_string, -1));
2930 entry->preedit_cursor = cursor_pos;
2931 g_free (preedit_string);
2933 gtk_entry_recompute (entry);
2938 gtk_entry_retrieve_surrounding_cb (GtkIMContext *context,
2941 gtk_im_context_set_surrounding (context,
2944 g_utf8_offset_to_pointer (entry->text, entry->current_pos) - entry->text);
2950 gtk_entry_delete_surrounding_cb (GtkIMContext *slave,
2955 if (entry->editable)
2956 gtk_editable_delete_text (GTK_EDITABLE (entry),
2957 entry->current_pos + offset,
2958 entry->current_pos + offset + n_chars);
2963 /* Internal functions
2966 /* Used for im_commit_cb and inserting Unicode chars */
2968 gtk_entry_enter_text (GtkEntry *entry,
2971 GtkEditable *editable = GTK_EDITABLE (entry);
2974 if (gtk_editable_get_selection_bounds (editable, NULL, NULL))
2975 gtk_editable_delete_selection (editable);
2978 if (entry->overwrite_mode)
2979 gtk_entry_delete_from_cursor (entry, GTK_DELETE_CHARS, 1);
2982 tmp_pos = entry->current_pos;
2983 gtk_editable_insert_text (editable, str, strlen (str), &tmp_pos);
2984 gtk_entry_set_position_internal (entry, tmp_pos, FALSE);
2987 /* All changes to entry->current_pos and entry->selection_bound
2988 * should go through this function.
2991 gtk_entry_set_positions (GtkEntry *entry,
2993 gint selection_bound)
2995 gboolean changed = FALSE;
2997 g_object_freeze_notify (G_OBJECT (entry));
2999 if (current_pos != -1 &&
3000 entry->current_pos != current_pos)
3002 entry->current_pos = current_pos;
3005 g_object_notify (G_OBJECT (entry), "cursor-position");
3008 if (selection_bound != -1 &&
3009 entry->selection_bound != selection_bound)
3011 entry->selection_bound = selection_bound;
3014 g_object_notify (G_OBJECT (entry), "selection-bound");
3017 g_object_thaw_notify (G_OBJECT (entry));
3020 gtk_entry_recompute (entry);
3024 gtk_entry_reset_layout (GtkEntry *entry)
3026 if (entry->cached_layout)
3028 g_object_unref (entry->cached_layout);
3029 entry->cached_layout = NULL;
3034 update_im_cursor_location (GtkEntry *entry)
3038 gint strong_xoffset;
3039 gint area_width, area_height;
3041 gtk_entry_get_cursor_locations (entry, CURSOR_STANDARD, &strong_x, NULL)
3043 get_text_area_size (entry, NULL, NULL, &area_width, &area_height);
3045 strong_xoffset = strong_x - entry->scroll_offset;
3046 if (strong_xoffset < 0)
3050 else if (strong_xoffset > area_width)
3052 strong_xoffset = area_width;
3054 area.x = strong_xoffset;
3057 area.height = area_height;
3059 gtk_im_context_set_cursor_location (entry->im_context, &area);
3063 recompute_idle_func (gpointer data)
3067 GDK_THREADS_ENTER ();
3069 entry = GTK_ENTRY (data);
3071 entry->recompute_idle = 0;
3073 if (gtk_widget_has_screen (GTK_WIDGET (entry)))
3075 gtk_entry_adjust_scroll (entry);
3076 gtk_entry_queue_draw (entry);
3078 update_im_cursor_location (entry);
3081 GDK_THREADS_LEAVE ();
3087 gtk_entry_recompute (GtkEntry *entry)
3089 gtk_entry_reset_layout (entry);
3090 gtk_entry_check_cursor_blink (entry);
3092 if (!entry->recompute_idle)
3094 entry->recompute_idle = g_idle_add_full (G_PRIORITY_HIGH_IDLE + 15, /* between resize and redraw */
3095 recompute_idle_func, entry, NULL);
3100 append_char (GString *str,
3108 char_len = g_unichar_to_utf8 (ch, buf);
3113 g_string_append_len (str, buf, char_len);
3119 gtk_entry_remove_password_hint (gpointer data)
3121 GDK_THREADS_ENTER();
3123 /* Force the string to be redrawn, but now without a visible character */
3124 gtk_entry_recompute (GTK_ENTRY (data));
3126 GDK_THREADS_LEAVE();
3131 static PangoLayout *
3132 gtk_entry_create_layout (GtkEntry *entry,
3133 gboolean include_preedit)
3135 GtkWidget *widget = GTK_WIDGET (entry);
3136 PangoLayout *layout = gtk_widget_create_pango_layout (widget, NULL);
3137 PangoAttrList *tmp_attrs = pango_attr_list_new ();
3139 gchar *preedit_string = NULL;
3140 gint preedit_length = 0;
3141 PangoAttrList *preedit_attrs = NULL;
3143 pango_layout_set_single_paragraph_mode (layout, TRUE);
3145 if (include_preedit)
3147 gtk_im_context_get_preedit_string (entry->im_context,
3148 &preedit_string, &preedit_attrs, NULL);
3149 preedit_length = entry->preedit_length;
3154 GString *tmp_string = g_string_new (NULL);
3156 gint cursor_index = g_utf8_offset_to_pointer (entry->text, entry->current_pos) - entry->text;
3160 g_string_prepend_len (tmp_string, entry->text, entry->n_bytes);
3161 g_string_insert (tmp_string, cursor_index, preedit_string);
3166 gint preedit_len_chars;
3167 gunichar invisible_char;
3169 ch_len = g_utf8_strlen (entry->text, entry->n_bytes);
3170 preedit_len_chars = g_utf8_strlen (preedit_string, -1);
3171 ch_len += preedit_len_chars;
3173 if (entry->invisible_char != 0)
3174 invisible_char = entry->invisible_char;
3176 invisible_char = ' '; /* just pick a char */
3178 append_char (tmp_string, invisible_char, ch_len);
3180 /* Fix cursor index to point to invisible char corresponding
3181 * to the preedit, fix preedit_length to be the length of
3182 * the invisible chars representing the preedit
3185 g_utf8_offset_to_pointer (tmp_string->str, entry->current_pos) -
3189 g_unichar_to_utf8 (invisible_char, NULL);
3192 pango_layout_set_text (layout, tmp_string->str, tmp_string->len);
3194 pango_attr_list_splice (tmp_attrs, preedit_attrs,
3195 cursor_index, preedit_length);
3197 g_string_free (tmp_string, TRUE);
3201 PangoDirection pango_dir;
3204 pango_dir = pango_find_base_dir (entry->text, entry->n_bytes);
3207 pango_dir = PANGO_DIRECTION_NEUTRAL;
3209 if (pango_dir == PANGO_DIRECTION_NEUTRAL)
3211 if (GTK_WIDGET_HAS_FOCUS (widget))
3213 GdkDisplay *display = gtk_widget_get_display (widget);
3214 GdkKeymap *keymap = gdk_keymap_get_for_display (display);
3215 if (gdk_keymap_get_direction (keymap) == PANGO_DIRECTION_RTL)
3216 pango_dir = PANGO_DIRECTION_RTL;
3218 pango_dir = PANGO_DIRECTION_LTR;
3222 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
3223 pango_dir = PANGO_DIRECTION_RTL;
3225 pango_dir = PANGO_DIRECTION_LTR;
3229 pango_context_set_base_dir (gtk_widget_get_pango_context (widget),
3232 pango_layout_set_alignment (layout, pango_dir);
3234 entry->resolved_dir = pango_dir;
3238 pango_layout_set_text (layout, entry->text, entry->n_bytes);
3242 GString *str = g_string_new (NULL);
3243 gunichar invisible_char;
3244 guint password_hint_timeout;
3245 GtkEntryPasswordHint *password_hint;
3247 g_object_get (gtk_widget_get_settings (widget),
3248 "gtk-entry-password-hint-timeout", &password_hint_timeout,
3251 if (entry->invisible_char != 0)
3252 invisible_char = entry->invisible_char;
3254 invisible_char = ' '; /* just pick a char */
3256 password_hint = g_object_get_qdata (G_OBJECT (entry),
3257 quark_password_hint);
3259 if (password_hint && password_hint->password_hint_timeout_id)
3261 g_source_remove (password_hint->password_hint_timeout_id);
3262 password_hint->password_hint_timeout_id = 0;
3265 if (password_hint_timeout == 0 || password_hint == NULL ||
3266 (password_hint && password_hint->password_hint_length == 0))
3268 append_char (str, invisible_char, entry->text_length);
3270 else if (password_hint)
3272 /* Draw hidden characters upto the inserted position,
3273 * then the real thing, pad up to full length
3275 if (password_hint->password_hint_position > 1)
3276 append_char (str, invisible_char,
3277 password_hint->password_hint_position - 1);
3279 g_string_append_len (str, password_hint->password_hint,
3280 password_hint->password_hint_length);
3282 if (password_hint->password_hint_position < entry->text_length)
3283 append_char (str, invisible_char,
3284 entry->text_length -
3285 password_hint->password_hint_position);
3287 /* Now remove this last inputted character, don't need
3290 memset (password_hint->password_hint, 0, PASSWORD_HINT_MAX);
3291 password_hint->password_hint_length = 0;
3293 password_hint->password_hint_timeout_id =
3294 g_timeout_add (password_hint_timeout,
3295 (GSourceFunc) gtk_entry_remove_password_hint,
3299 pango_layout_set_text (layout, str->str, str->len);
3300 g_string_free (str, TRUE);
3304 pango_layout_set_attributes (layout, tmp_attrs);
3307 g_free (preedit_string);
3309 pango_attr_list_unref (preedit_attrs);
3311 pango_attr_list_unref (tmp_attrs);
3316 static PangoLayout *
3317 gtk_entry_ensure_layout (GtkEntry *entry,
3318 gboolean include_preedit)
3320 if (entry->preedit_length > 0 &&
3321 !include_preedit != !entry->cache_includes_preedit)
3322 gtk_entry_reset_layout (entry);
3324 if (!entry->cached_layout)
3326 entry->cached_layout = gtk_entry_create_layout (entry, include_preedit);
3327 entry->cache_includes_preedit = include_preedit;
3330 return entry->cached_layout;
3334 get_layout_position (GtkEntry *entry,
3338 PangoLayout *layout;
3339 PangoRectangle logical_rect;
3340 gint area_width, area_height;
3341 GtkBorder inner_border;
3343 PangoLayoutLine *line;
3345 layout = gtk_entry_ensure_layout (entry, TRUE);
3347 get_text_area_size (entry, NULL, NULL, &area_width, &area_height);
3348 get_inner_border (entry, &inner_border);
3350 area_height = PANGO_SCALE * (area_height - inner_border.top - inner_border.bottom);
3352 line = pango_layout_get_lines (layout)->data;
3353 pango_layout_line_get_extents (line, NULL, &logical_rect);
3355 /* Align primarily for locale's ascent/descent */
3356 y_pos = ((area_height - entry->ascent - entry->descent) / 2 +
3357 entry->ascent + logical_rect.y);
3359 /* Now see if we need to adjust to fit in actual drawn string */
3360 if (logical_rect.height > area_height)
3361 y_pos = (area_height - logical_rect.height) / 2;
3364 else if (y_pos + logical_rect.height > area_height)
3365 y_pos = area_height - logical_rect.height;
3367 y_pos = inner_border.top + y_pos / PANGO_SCALE;
3370 *x = inner_border.left - entry->scroll_offset;
3377 gtk_entry_draw_text (GtkEntry *entry)
3381 if (!entry->visible && entry->invisible_char == 0)
3384 if (GTK_WIDGET_DRAWABLE (entry))
3386 PangoLayout *layout = gtk_entry_ensure_layout (entry, TRUE);
3389 gint start_pos, end_pos;
3391 widget = GTK_WIDGET (entry);
3393 get_layout_position (entry, &x, &y);
3395 cr = gdk_cairo_create (entry->text_area);
3397 cairo_move_to (cr, x, y);
3398 gdk_cairo_set_source_color (cr, &widget->style->text [widget->state]);
3399 pango_cairo_show_layout (cr, layout);
3401 if (gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &start_pos, &end_pos))
3405 PangoRectangle logical_rect;
3406 GdkColor *selection_color, *text_color;
3407 GtkBorder inner_border;
3409 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
3410 gtk_entry_get_pixel_ranges (entry, &ranges, &n_ranges);
3412 if (GTK_WIDGET_HAS_FOCUS (entry))
3414 selection_color = &widget->style->base [GTK_STATE_SELECTED];
3415 text_color = &widget->style->text [GTK_STATE_SELECTED];
3419 selection_color = &widget->style->base [GTK_STATE_ACTIVE];
3420 text_color = &widget->style->text [GTK_STATE_ACTIVE];
3423 get_inner_border (entry, &inner_border);
3425 for (i = 0; i < n_ranges; ++i)
3426 cairo_rectangle (cr,
3427 inner_border.left - entry->scroll_offset + ranges[2 * i],
3430 logical_rect.height);
3434 gdk_cairo_set_source_color (cr, selection_color);
3437 cairo_move_to (cr, x, y);
3438 gdk_cairo_set_source_color (cr, text_color);
3439 pango_cairo_show_layout (cr, layout);
3449 draw_insertion_cursor (GtkEntry *entry,
3450 GdkRectangle *cursor_location,
3451 gboolean is_primary,
3452 PangoDirection direction,
3453 gboolean draw_arrow)
3455 GtkWidget *widget = GTK_WIDGET (entry);
3456 GtkTextDirection text_dir;
3458 if (direction == PANGO_DIRECTION_LTR)
3459 text_dir = GTK_TEXT_DIR_LTR;
3461 text_dir = GTK_TEXT_DIR_RTL;
3463 gtk_draw_insertion_cursor (widget, entry->text_area, NULL,
3465 is_primary, text_dir, draw_arrow);
3469 gtk_entry_draw_cursor (GtkEntry *entry,
3472 GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (entry)));
3473 PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
3475 if (GTK_WIDGET_DRAWABLE (entry))
3477 GtkWidget *widget = GTK_WIDGET (entry);
3478 GdkRectangle cursor_location;
3479 gboolean split_cursor;
3481 GtkBorder inner_border;
3483 gint strong_x, weak_x;
3484 gint text_area_height;
3485 PangoDirection dir1 = PANGO_DIRECTION_NEUTRAL;
3486 PangoDirection dir2 = PANGO_DIRECTION_NEUTRAL;
3490 get_inner_border (entry, &inner_border);
3492 xoffset = inner_border.left - entry->scroll_offset;
3494 gdk_drawable_get_size (entry->text_area, NULL, &text_area_height);
3496 gtk_entry_get_cursor_locations (entry, type, &strong_x, &weak_x);
3498 g_object_get (gtk_widget_get_settings (widget),
3499 "gtk-split-cursor", &split_cursor,
3502 dir1 = entry->resolved_dir;
3508 if (weak_x != strong_x)
3510 dir2 = (entry->resolved_dir == PANGO_DIRECTION_LTR) ? PANGO_DIRECTION_RTL : PANGO_DIRECTION_LTR;
3516 if (keymap_direction == entry->resolved_dir)
3522 cursor_location.x = xoffset + x1;
3523 cursor_location.y = inner_border.top;
3524 cursor_location.width = 0;
3525 cursor_location.height = text_area_height - inner_border.top - inner_border.bottom;
3527 draw_insertion_cursor (entry,
3528 &cursor_location, TRUE, dir1,
3529 dir2 != PANGO_DIRECTION_NEUTRAL);
3531 if (dir2 != PANGO_DIRECTION_NEUTRAL)
3533 cursor_location.x = xoffset + x2;
3534 draw_insertion_cursor (entry,
3535 &cursor_location, FALSE, dir2,
3542 gtk_entry_queue_draw (GtkEntry *entry)
3544 if (GTK_WIDGET_REALIZED (entry))
3545 gdk_window_invalidate_rect (entry->text_area, NULL, FALSE);
3549 gtk_entry_reset_im_context (GtkEntry *entry)
3551 if (entry->need_im_reset)
3553 entry->need_im_reset = 0;
3554 gtk_im_context_reset (entry->im_context);
3559 gtk_entry_find_position (GtkEntry *entry,
3562 PangoLayout *layout;
3563 PangoLayoutLine *line;
3570 layout = gtk_entry_ensure_layout (entry, TRUE);
3571 text = pango_layout_get_text (layout);
3572 cursor_index = g_utf8_offset_to_pointer (text, entry->current_pos) - text;
3574 line = pango_layout_get_lines (layout)->data;
3575 pango_layout_line_x_to_index (line, x * PANGO_SCALE, &index, &trailing);
3577 if (index >= cursor_index && entry->preedit_length)
3579 if (index >= cursor_index + entry->preedit_length)
3580 index -= entry->preedit_length;
3583 index = cursor_index;
3588 pos = g_utf8_pointer_to_offset (text, text + index);
3595 gtk_entry_get_cursor_locations (GtkEntry *entry,
3600 if (!entry->visible && !entry->invisible_char)
3610 PangoLayout *layout = gtk_entry_ensure_layout (entry, TRUE);
3611 const gchar *text = pango_layout_get_text (layout);
3612 PangoRectangle strong_pos, weak_pos;
3615 if (type == CURSOR_STANDARD)
3617 index = g_utf8_offset_to_pointer (text, entry->current_pos + entry->preedit_cursor) - text;
3619 else /* type == CURSOR_DND */
3621 index = g_utf8_offset_to_pointer (text, entry->dnd_position) - text;
3623 if (entry->dnd_position > entry->current_pos)
3626 index += entry->preedit_length;
3629 gint preedit_len_chars = g_utf8_strlen (text, -1) - entry->text_length;
3630 index += preedit_len_chars * g_unichar_to_utf8 (entry->invisible_char, NULL);
3635 pango_layout_get_cursor_pos (layout, index, &strong_pos, &weak_pos);
3638 *strong_x = strong_pos.x / PANGO_SCALE;
3641 *weak_x = weak_pos.x / PANGO_SCALE;
3646 gtk_entry_adjust_scroll (GtkEntry *entry)
3648 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
3649 gint min_offset, max_offset;
3650 gint text_area_width, text_width;
3651 GtkBorder inner_border;
3652 gint strong_x, weak_x;
3653 gint strong_xoffset, weak_xoffset;
3655 PangoLayout *layout;
3656 PangoLayoutLine *line;
3657 PangoRectangle logical_rect;
3659 if (!GTK_WIDGET_REALIZED (entry))
3662 get_inner_border (entry, &inner_border);
3664 gdk_drawable_get_size (entry->text_area, &text_area_width, NULL);
3665 text_area_width -= inner_border.left + inner_border.right;
3666 if (text_area_width < 0)
3667 text_area_width = 0;
3669 layout = gtk_entry_ensure_layout (entry, TRUE);
3670 line = pango_layout_get_lines (layout)->data;
3672 pango_layout_line_get_extents (line, NULL, &logical_rect);
3674 /* Display as much text as we can */
3676 if (entry->resolved_dir == PANGO_DIRECTION_LTR)
3677 xalign = priv->xalign;
3679 xalign = 1.0 - priv->xalign;
3681 text_width = PANGO_PIXELS(logical_rect.width);
3683 if (text_width > text_area_width)
3686 max_offset = text_width - text_area_width;
3690 min_offset = (text_width - text_area_width) * xalign;
3691 max_offset = min_offset;
3694 entry->scroll_offset = CLAMP (entry->scroll_offset, min_offset, max_offset);
3696 /* And make sure cursors are on screen. Note that the cursor is
3697 * actually drawn one pixel into the INNER_BORDER space on
3698 * the right, when the scroll is at the utmost right. This
3699 * looks better to to me than confining the cursor inside the
3700 * border entirely, though it means that the cursor gets one
3701 * pixel closer to the edge of the widget on the right than
3702 * on the left. This might need changing if one changed
3703 * INNER_BORDER from 2 to 1, as one would do on a
3704 * small-screen-real-estate display.
3706 * We always make sure that the strong cursor is on screen, and
3707 * put the weak cursor on screen if possible.
3710 gtk_entry_get_cursor_locations (entry, CURSOR_STANDARD, &strong_x, &weak_x);
3712 strong_xoffset = strong_x - entry->scroll_offset;
3714 if (strong_xoffset < 0)
3716 entry->scroll_offset += strong_xoffset;
3719 else if (strong_xoffset > text_area_width)
3721 entry->scroll_offset += strong_xoffset - text_area_width;
3722 strong_xoffset = text_area_width;
3725 weak_xoffset = weak_x - entry->scroll_offset;
3727 if (weak_xoffset < 0 && strong_xoffset - weak_xoffset <= text_area_width)
3729 entry->scroll_offset += weak_xoffset;
3731 else if (weak_xoffset > text_area_width &&
3732 strong_xoffset - (weak_xoffset - text_area_width) >= 0)
3734 entry->scroll_offset += weak_xoffset - text_area_width;
3737 g_object_notify (G_OBJECT (entry), "scroll-offset");
3741 gtk_entry_move_visually (GtkEntry *entry,
3746 PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
3749 text = pango_layout_get_text (layout);
3751 index = g_utf8_offset_to_pointer (text, start) - text;
3755 int new_index, new_trailing;
3756 gboolean split_cursor;
3759 g_object_get (gtk_widget_get_settings (GTK_WIDGET (entry)),
3760 "gtk-split-cursor", &split_cursor,
3767 GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (entry)));
3768 PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
3770 strong = keymap_direction == entry->resolved_dir;
3775 pango_layout_move_cursor_visually (layout, strong, index, 0, 1, &new_index, &new_trailing);
3780 pango_layout_move_cursor_visually (layout, strong, index, 0, -1, &new_index, &new_trailing);
3786 else if (new_index != G_MAXINT)
3789 while (new_trailing--)
3790 index = g_utf8_next_char (text + index) - text;
3793 return g_utf8_pointer_to_offset (text, text + index);
3797 gtk_entry_move_logically (GtkEntry *entry,
3801 gint new_pos = start;
3803 /* Prevent any leak of information */
3804 if (!entry->visible)
3806 new_pos = CLAMP (start + count, 0, entry->text_length);
3808 else if (entry->text)
3810 PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
3811 PangoLogAttr *log_attrs;
3814 pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
3816 while (count > 0 && new_pos < entry->text_length)
3820 while (new_pos < entry->text_length && !log_attrs[new_pos].is_cursor_position);
3824 while (count < 0 && new_pos > 0)
3828 while (new_pos > 0 && !log_attrs[new_pos].is_cursor_position);
3840 gtk_entry_move_forward_word (GtkEntry *entry,
3842 gboolean allow_whitespace)
3844 gint new_pos = start;
3846 /* Prevent any leak of information */
3847 if (!entry->visible)
3849 new_pos = entry->text_length;
3851 else if (entry->text && (new_pos < entry->text_length))
3853 PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
3854 PangoLogAttr *log_attrs;
3857 pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
3859 /* Find the next word boundary */
3861 while (new_pos < n_attrs - 1 && !(log_attrs[new_pos].is_word_end ||
3862 (log_attrs[new_pos].is_word_start && allow_whitespace)))
3873 gtk_entry_move_backward_word (GtkEntry *entry,
3875 gboolean allow_whitespace)
3877 gint new_pos = start;
3879 /* Prevent any leak of information */
3880 if (!entry->visible)
3884 else if (entry->text && start > 0)
3886 PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
3887 PangoLogAttr *log_attrs;
3890 pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
3892 new_pos = start - 1;
3894 /* Find the previous word boundary */
3895 while (new_pos > 0 && !(log_attrs[new_pos].is_word_start ||
3896 (log_attrs[new_pos].is_word_end && allow_whitespace)))
3906 gtk_entry_delete_whitespace (GtkEntry *entry)
3908 PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
3909 PangoLogAttr *log_attrs;
3913 pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
3915 start = end = entry->current_pos;
3917 while (start > 0 && log_attrs[start-1].is_white)
3920 while (end < n_attrs && log_attrs[end].is_white)
3926 gtk_editable_delete_text (GTK_EDITABLE (entry), start, end);
3931 gtk_entry_select_word (GtkEntry *entry)
3933 gint start_pos = gtk_entry_move_backward_word (entry, entry->current_pos, TRUE);
3934 gint end_pos = gtk_entry_move_forward_word (entry, entry->current_pos, TRUE);
3936 gtk_editable_select_region (GTK_EDITABLE (entry), start_pos, end_pos);
3940 gtk_entry_select_line (GtkEntry *entry)
3942 gtk_editable_select_region (GTK_EDITABLE (entry), 0, -1);
3946 * Like gtk_editable_get_chars, but handle not-visible entries
3950 gtk_entry_get_public_chars (GtkEntry *entry,
3955 end = entry->text_length;
3958 return gtk_editable_get_chars (GTK_EDITABLE (entry), start, end);
3959 else if (!entry->invisible_char)
3960 return g_strdup ("");
3963 GString *str = g_string_new (NULL);
3964 append_char (str, entry->invisible_char, end - start);
3965 return g_string_free (str, FALSE);
3970 truncate_multiline (const gchar *text)
3975 text[length] && text[length] != '\n' && text[length] != '\r';
3982 paste_received (GtkClipboard *clipboard,
3986 GtkEntry *entry = GTK_ENTRY (data);
3987 GtkEditable *editable = GTK_EDITABLE (entry);
3988 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
3990 if (entry->button == 2)
3992 gint pos, start, end;
3993 pos = priv->insert_pos;
3994 gtk_editable_get_selection_bounds (editable, &start, &end);
3995 if (!((start <= pos && pos <= end) || (end <= pos && pos <= start)))
3996 gtk_editable_select_region (editable, pos, pos);
4001 gint pos, start, end;
4003 GtkEntryCompletion *completion = gtk_entry_get_completion (entry);
4005 if (entry->truncate_multiline)
4006 length = truncate_multiline (text);
4010 g_signal_handler_block (entry, completion->priv->changed_id);
4011 if (GTK_WIDGET_MAPPED (completion->priv->popup_window))
4012 _gtk_entry_completion_popdown (completion);
4015 if (gtk_editable_get_selection_bounds (editable, &start, &end))
4016 gtk_editable_delete_text (editable, start, end);
4018 pos = entry->current_pos;
4019 gtk_editable_insert_text (editable, text, length, &pos);
4020 gtk_editable_set_position (editable, pos);
4023 g_signal_handler_unblock (entry, completion->priv->changed_id);
4026 g_object_unref (entry);
4030 gtk_entry_paste (GtkEntry *entry,
4033 g_object_ref (entry);
4034 gtk_clipboard_request_text (gtk_widget_get_clipboard (GTK_WIDGET (entry), selection),
4035 paste_received, entry);
4039 primary_get_cb (GtkClipboard *clipboard,
4040 GtkSelectionData *selection_data,
4044 GtkEntry *entry = GTK_ENTRY (data);
4047 if (gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &start, &end))
4049 gchar *str = gtk_entry_get_public_chars (entry, start, end);
4050 gtk_selection_data_set_text (selection_data, str, -1);
4056 primary_clear_cb (GtkClipboard *clipboard,
4059 GtkEntry *entry = GTK_ENTRY (data);
4061 gtk_editable_select_region (GTK_EDITABLE (entry), entry->current_pos, entry->current_pos);
4065 gtk_entry_update_primary_selection (GtkEntry *entry)
4067 static GtkTargetEntry targets[] = {
4068 { "UTF8_STRING", 0, 0 },
4071 { "COMPOUND_TEXT", 0, 0 },
4072 { "text/plain;charset=utf-8", 0, 0 },
4074 { "text/plain", 0, 0 }
4077 GtkClipboard *clipboard;
4080 if (targets[5].target == NULL)
4082 const gchar *charset;
4084 g_get_charset (&charset);
4085 targets[5].target = g_strdup_printf ("text/plain;charset=%s", charset);
4088 if (!GTK_WIDGET_REALIZED (entry))
4091 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (entry), GDK_SELECTION_PRIMARY);
4093 if (gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &start, &end))
4095 if (!gtk_clipboard_set_with_owner (clipboard, targets, G_N_ELEMENTS (targets),
4096 primary_get_cb, primary_clear_cb, G_OBJECT (entry)))
4097 primary_clear_cb (clipboard, entry);
4101 if (gtk_clipboard_get_owner (clipboard) == G_OBJECT (entry))
4102 gtk_clipboard_clear (clipboard);
4110 gtk_entry_new (void)
4112 return g_object_new (GTK_TYPE_ENTRY, NULL);
4116 * gtk_entry_new_with_max_length:
4117 * @max: the maximum length of the entry, or 0 for no maximum.
4118 * (other than the maximum length of entries.) The value passed in will
4119 * be clamped to the range 0-65536.
4121 * Creates a new #GtkEntry widget with the given maximum length.
4123 * Note: the existence of this function is inconsistent
4124 * with the rest of the GTK+ API. The normal setup would
4125 * be to just require the user to make an extra call
4126 * to gtk_entry_set_max_length() instead. It is not
4127 * expected that this function will be removed, but
4128 * it would be better practice not to use it.
4130 * Return value: a new #GtkEntry.
4133 gtk_entry_new_with_max_length (gint max)
4137 max = CLAMP (max, 0, MAX_SIZE);
4139 entry = g_object_new (GTK_TYPE_ENTRY, NULL);
4140 entry->text_max_length = max;
4142 return GTK_WIDGET (entry);
4146 gtk_entry_set_text (GtkEntry *entry,
4150 GtkEntryCompletion *completion;
4152 g_return_if_fail (GTK_IS_ENTRY (entry));
4153 g_return_if_fail (text != NULL);
4155 /* Actually setting the text will affect the cursor and selection;
4156 * if the contents don't actually change, this will look odd to the user.
4158 if (strcmp (entry->text, text) == 0)
4161 completion = gtk_entry_get_completion (entry);
4163 g_signal_handler_block (entry, completion->priv->changed_id);
4165 gtk_editable_delete_text (GTK_EDITABLE (entry), 0, -1);
4168 gtk_editable_insert_text (GTK_EDITABLE (entry), text, strlen (text), &tmp_pos);
4171 g_signal_handler_unblock (entry, completion->priv->changed_id);
4175 gtk_entry_append_text (GtkEntry *entry,
4180 g_return_if_fail (GTK_IS_ENTRY (entry));
4181 g_return_if_fail (text != NULL);
4183 tmp_pos = entry->text_length;
4184 gtk_editable_insert_text (GTK_EDITABLE (entry), text, -1, &tmp_pos);
4188 gtk_entry_prepend_text (GtkEntry *entry,
4193 g_return_if_fail (GTK_IS_ENTRY (entry));
4194 g_return_if_fail (text != NULL);
4197 gtk_editable_insert_text (GTK_EDITABLE (entry), text, -1, &tmp_pos);
4201 gtk_entry_set_position (GtkEntry *entry,
4204 g_return_if_fail (GTK_IS_ENTRY (entry));
4206 gtk_editable_set_position (GTK_EDITABLE (entry), position);
4210 gtk_entry_set_visibility (GtkEntry *entry,
4213 g_return_if_fail (GTK_IS_ENTRY (entry));
4215 visible = visible != FALSE;
4217 if (entry->visible != visible)
4219 if (GTK_WIDGET_HAS_FOCUS (entry) && !visible)
4220 gtk_im_context_focus_out (entry->im_context);
4222 g_object_unref (entry->im_context);
4225 entry->im_context = gtk_im_multicontext_new ();
4227 entry->im_context = gtk_im_context_simple_new ();
4229 g_signal_connect (entry->im_context, "commit",
4230 G_CALLBACK (gtk_entry_commit_cb), entry);
4231 g_signal_connect (entry->im_context, "preedit_changed",
4232 G_CALLBACK (gtk_entry_preedit_changed_cb), entry);
4233 g_signal_connect (entry->im_context, "retrieve_surrounding",
4234 G_CALLBACK (gtk_entry_retrieve_surrounding_cb), entry);
4235 g_signal_connect (entry->im_context, "delete_surrounding",
4236 G_CALLBACK (gtk_entry_delete_surrounding_cb), entry);
4238 if (GTK_WIDGET_HAS_FOCUS (entry) && visible)
4239 gtk_im_context_focus_in (entry->im_context);
4241 entry->visible = visible;
4243 g_object_notify (G_OBJECT (entry), "visibility");
4244 gtk_entry_recompute (entry);
4249 * gtk_entry_get_visibility:
4250 * @entry: a #GtkEntry
4252 * Retrieves whether the text in @entry is visible. See
4253 * gtk_entry_set_visibility().
4255 * Return value: %TRUE if the text is currently visible
4258 gtk_entry_get_visibility (GtkEntry *entry)
4260 g_return_val_if_fail (GTK_IS_ENTRY (entry), FALSE);
4262 return entry->visible;
4266 * gtk_entry_set_invisible_char:
4267 * @entry: a #GtkEntry
4268 * @ch: a Unicode character
4270 * Sets the character to use in place of the actual text when
4271 * gtk_entry_set_visibility() has been called to set text visibility
4272 * to %FALSE. i.e. this is the character used in "password mode" to
4273 * show the user how many characters have been typed. The default
4274 * invisible char is an asterisk ('*'). If you set the invisible char
4275 * to 0, then the user will get no feedback at all; there will be
4276 * no text on the screen as they type.
4280 gtk_entry_set_invisible_char (GtkEntry *entry,
4283 g_return_if_fail (GTK_IS_ENTRY (entry));
4285 if (ch == entry->invisible_char)
4288 entry->invisible_char = ch;
4289 g_object_notify (G_OBJECT (entry), "invisible-char");
4290 gtk_entry_recompute (entry);
4294 * gtk_entry_get_invisible_char:
4295 * @entry: a #GtkEntry
4297 * Retrieves the character displayed in place of the real characters
4298 * for entries with visisbility set to false. See gtk_entry_set_invisible_char().
4300 * Return value: the current invisible char, or 0, if the entry does not
4301 * show invisible text at all.
4304 gtk_entry_get_invisible_char (GtkEntry *entry)
4306 g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
4308 return entry->invisible_char;
4312 gtk_entry_set_editable (GtkEntry *entry,
4315 g_return_if_fail (GTK_IS_ENTRY (entry));
4317 gtk_editable_set_editable (GTK_EDITABLE (entry), editable);
4321 * gtk_entry_get_text:
4322 * @entry: a #GtkEntry
4324 * Retrieves the contents of the entry widget.
4325 * See also gtk_editable_get_chars().
4327 * Return value: a pointer to the contents of the widget as a
4328 * string. This string points to internally allocated
4329 * storage in the widget and must not be freed, modified or
4332 G_CONST_RETURN gchar*
4333 gtk_entry_get_text (GtkEntry *entry)
4335 g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
4341 gtk_entry_select_region (GtkEntry *entry,
4345 gtk_editable_select_region (GTK_EDITABLE (entry), start, end);
4349 * gtk_entry_set_max_length:
4350 * @entry: a #GtkEntry.
4351 * @max: the maximum length of the entry, or 0 for no maximum.
4352 * (other than the maximum length of entries.) The value passed in will
4353 * be clamped to the range 0-65536.
4355 * Sets the maximum allowed length of the contents of the widget. If
4356 * the current contents are longer than the given length, then they
4357 * will be truncated to fit.
4360 gtk_entry_set_max_length (GtkEntry *entry,
4363 g_return_if_fail (GTK_IS_ENTRY (entry));
4365 max = CLAMP (max, 0, MAX_SIZE);
4367 if (max > 0 && entry->text_length > max)
4368 gtk_editable_delete_text (GTK_EDITABLE (entry), max, -1);
4370 entry->text_max_length = max;
4371 g_object_notify (G_OBJECT (entry), "max-length");
4375 * gtk_entry_get_max_length:
4376 * @entry: a #GtkEntry
4378 * Retrieves the maximum allowed length of the text in
4379 * @entry. See gtk_entry_set_max_length().
4381 * Return value: the maximum allowed number of characters
4382 * in #GtkEntry, or 0 if there is no maximum.
4385 gtk_entry_get_max_length (GtkEntry *entry)
4387 g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
4389 return entry->text_max_length;
4393 * gtk_entry_set_activates_default:
4394 * @entry: a #GtkEntry
4395 * @setting: %TRUE to activate window's default widget on Enter keypress
4397 * If @setting is %TRUE, pressing Enter in the @entry will activate the default
4398 * widget for the window containing the entry. This usually means that
4399 * the dialog box containing the entry will be closed, since the default
4400 * widget is usually one of the dialog buttons.
4402 * (For experts: if @setting is %TRUE, the entry calls
4403 * gtk_window_activate_default() on the window containing the entry, in
4404 * the default handler for the "activate" signal.)
4408 gtk_entry_set_activates_default (GtkEntry *entry,
4411 g_return_if_fail (GTK_IS_ENTRY (entry));
4412 setting = setting != FALSE;
4414 if (setting != entry->activates_default)
4416 entry->activates_default = setting;
4417 g_object_notify (G_OBJECT (entry), "activates-default");
4422 * gtk_entry_get_activates_default:
4423 * @entry: a #GtkEntry
4425 * Retrieves the value set by gtk_entry_set_activates_default().
4427 * Return value: %TRUE if the entry will activate the default widget
4430 gtk_entry_get_activates_default (GtkEntry *entry)
4432 g_return_val_if_fail (GTK_IS_ENTRY (entry), FALSE);
4434 return entry->activates_default;
4438 * gtk_entry_set_width_chars:
4439 * @entry: a #GtkEntry
4440 * @n_chars: width in chars
4442 * Changes the size request of the entry to be about the right size
4443 * for @n_chars characters. Note that it changes the size
4444 * <emphasis>request</emphasis>, the size can still be affected by
4445 * how you pack the widget into containers. If @n_chars is -1, the
4446 * size reverts to the default entry size.
4450 gtk_entry_set_width_chars (GtkEntry *entry,
4453 g_return_if_fail (GTK_IS_ENTRY (entry));
4455 if (entry->width_chars != n_chars)
4457 entry->width_chars = n_chars;
4458 g_object_notify (G_OBJECT (entry), "width-chars");
4459 gtk_widget_queue_resize (GTK_WIDGET (entry));
4464 * gtk_entry_get_width_chars:
4465 * @entry: a #GtkEntry
4467 * Gets the value set by gtk_entry_set_width_chars().
4469 * Return value: number of chars to request space for, or negative if unset
4472 gtk_entry_get_width_chars (GtkEntry *entry)
4474 g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
4476 return entry->width_chars;
4480 * gtk_entry_set_has_frame:
4481 * @entry: a #GtkEntry
4482 * @setting: new value
4484 * Sets whether the entry has a beveled frame around it.
4487 gtk_entry_set_has_frame (GtkEntry *entry,
4490 g_return_if_fail (GTK_IS_ENTRY (entry));
4492 setting = (setting != FALSE);
4494 if (entry->has_frame == setting)
4497 gtk_widget_queue_resize (GTK_WIDGET (entry));
4498 entry->has_frame = setting;
4499 g_object_notify (G_OBJECT (entry), "has-frame");
4503 * gtk_entry_get_has_frame:
4504 * @entry: a #GtkEntry
4506 * Gets the value set by gtk_entry_set_has_frame().
4508 * Return value: whether the entry has a beveled frame
4511 gtk_entry_get_has_frame (GtkEntry *entry)
4513 g_return_val_if_fail (GTK_IS_ENTRY (entry), FALSE);
4515 return entry->has_frame;
4519 * gtk_entry_set_inner_border:
4520 * @entry: a #GtkEntry
4521 * @border: a #GtkBorder, or %NULL
4523 * Sets %entry's inner-border property to %border, or clears it if %NULL
4524 * is passed. The inner-border is the area around the entry's text, but
4527 * If set, this property overrides the inner-border style property.
4528 * Overriding the style-provided border is useful when you want to do
4529 * in-place editing of some text in a canvas or list widget, where
4530 * pixel-exact positioning of the entry is important.
4535 gtk_entry_set_inner_border (GtkEntry *entry,
4536 const GtkBorder *border)
4538 g_return_if_fail (GTK_IS_ENTRY (entry));
4540 gtk_widget_queue_resize (GTK_WIDGET (entry));
4543 g_object_set_qdata_full (G_OBJECT (entry), quark_inner_border,
4544 gtk_border_copy (border),
4545 (GDestroyNotify) gtk_border_free);
4547 g_object_set_qdata (G_OBJECT (entry), quark_inner_border, NULL);
4549 g_object_notify (G_OBJECT (entry), "inner-border");
4553 * gtk_entry_get_inner_border:
4554 * @entry: a #GtkEntry
4556 * This function returns the entry's inner-border property. See
4557 * gtk_entry_set_inner_border() for more information.
4559 * Return value: the entry's #GtkBorder, or %NULL if none was set.
4563 G_CONST_RETURN GtkBorder *
4564 gtk_entry_get_inner_border (GtkEntry *entry)
4566 g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
4568 return g_object_get_qdata (G_OBJECT (entry), quark_inner_border);
4572 * gtk_entry_get_layout:
4573 * @entry: a #GtkEntry
4575 * Gets the #PangoLayout used to display the entry.
4576 * The layout is useful to e.g. convert text positions to
4577 * pixel positions, in combination with gtk_entry_get_layout_offsets().
4578 * The returned layout is owned by the entry and must not be
4579 * modified or freed by the caller.
4581 * Keep in mind that the layout text may contain a preedit string, so
4582 * gtk_entry_layout_index_to_text_index() and
4583 * gtk_entry_text_index_to_layout_index() are needed to convert byte
4584 * indices in the layout to byte indices in the entry contents.
4586 * Return value: the #PangoLayout for this entry
4589 gtk_entry_get_layout (GtkEntry *entry)
4591 PangoLayout *layout;
4593 g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
4595 layout = gtk_entry_ensure_layout (entry, TRUE);
4602 * gtk_entry_layout_index_to_text_index:
4603 * @entry: a #GtkEntry
4604 * @layout_index: byte index into the entry layout text
4606 * Converts from a position in the entry contents (returned
4607 * by gtk_entry_get_text()) to a position in the
4608 * entry's #PangoLayout (returned by gtk_entry_get_layout(),
4609 * with text retrieved via pango_layout_get_text()).
4611 * Return value: byte index into the entry contents
4614 gtk_entry_layout_index_to_text_index (GtkEntry *entry,
4617 PangoLayout *layout;
4621 g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
4623 layout = gtk_entry_ensure_layout (entry, TRUE);
4624 text = pango_layout_get_text (layout);
4625 cursor_index = g_utf8_offset_to_pointer (text, entry->current_pos) - text;
4627 if (layout_index >= cursor_index && entry->preedit_length)
4629 if (layout_index >= cursor_index + entry->preedit_length)
4630 layout_index -= entry->preedit_length;
4632 layout_index = cursor_index;
4635 return layout_index;
4639 * gtk_entry_text_index_to_layout_index:
4640 * @entry: a #GtkEntry
4641 * @text_index: byte index into the entry contents
4643 * Converts from a position in the entry's #PangoLayout (returned by
4644 * gtk_entry_get_layout()) to a position in the entry contents
4645 * (returned by gtk_entry_get_text()).
4647 * Return value: byte index into the entry layout text
4650 gtk_entry_text_index_to_layout_index (GtkEntry *entry,
4653 PangoLayout *layout;
4656 g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
4658 layout = gtk_entry_ensure_layout (entry, TRUE);
4659 text = pango_layout_get_text (layout);
4660 cursor_index = g_utf8_offset_to_pointer (text, entry->current_pos) - text;
4662 if (text_index > cursor_index)
4663 text_index += entry->preedit_length;
4669 * gtk_entry_get_layout_offsets:
4670 * @entry: a #GtkEntry
4671 * @x: location to store X offset of layout, or %NULL
4672 * @y: location to store Y offset of layout, or %NULL
4675 * Obtains the position of the #PangoLayout used to render text
4676 * in the entry, in widget coordinates. Useful if you want to line
4677 * up the text in an entry with some other text, e.g. when using the
4678 * entry to implement editable cells in a sheet widget.
4680 * Also useful to convert mouse events into coordinates inside the
4681 * #PangoLayout, e.g. to take some action if some part of the entry text
4684 * Note that as the user scrolls around in the entry the offsets will
4685 * change; you'll need to connect to the "notify::scroll-offset"
4686 * signal to track this. Remember when using the #PangoLayout
4687 * functions you need to convert to and from pixels using
4688 * PANGO_PIXELS() or #PANGO_SCALE.
4690 * Keep in mind that the layout text may contain a preedit string, so
4691 * gtk_entry_layout_index_to_text_index() and
4692 * gtk_entry_text_index_to_layout_index() are needed to convert byte
4693 * indices in the layout to byte indices in the entry contents.
4697 gtk_entry_get_layout_offsets (GtkEntry *entry,
4701 gint text_area_x, text_area_y;
4703 g_return_if_fail (GTK_IS_ENTRY (entry));
4705 /* this gets coords relative to text area */
4706 get_layout_position (entry, x, y);
4708 /* convert to widget coords */
4709 get_text_area_size (entry, &text_area_x, &text_area_y, NULL, NULL);
4720 * gtk_entry_set_alignment:
4721 * @entry: a #GtkEntry
4722 * @xalign: The horizontal alignment, from 0 (left) to 1 (right).
4723 * Reversed for RTL layouts
4725 * Sets the alignment for the contents of the entry. This controls
4726 * the horizontal positioning of the contents when the displayed
4727 * text is shorter than the width of the entry.
4732 gtk_entry_set_alignment (GtkEntry *entry, gfloat xalign)
4734 GtkEntryPrivate *priv;
4736 g_return_if_fail (GTK_IS_ENTRY (entry));
4738 priv = GTK_ENTRY_GET_PRIVATE (entry);
4742 else if (xalign > 1.0)
4745 if (xalign != priv->xalign)
4747 priv->xalign = xalign;
4749 gtk_entry_recompute (entry);
4751 g_object_notify (G_OBJECT (entry), "xalign");
4756 * gtk_entry_get_alignment:
4757 * @entry: a #GtkEntry
4759 * Gets the value set by gtk_entry_set_alignment().
4761 * Return value: the alignment
4766 gtk_entry_get_alignment (GtkEntry *entry)
4768 GtkEntryPrivate *priv;
4770 g_return_val_if_fail (GTK_IS_ENTRY (entry), 0.0);
4772 priv = GTK_ENTRY_GET_PRIVATE (entry);
4774 return priv->xalign;
4777 /* Quick hack of a popup menu
4780 activate_cb (GtkWidget *menuitem,
4783 const gchar *signal = g_object_get_data (G_OBJECT (menuitem), "gtk-signal");
4784 g_signal_emit_by_name (entry, signal);
4789 gtk_entry_mnemonic_activate (GtkWidget *widget,
4790 gboolean group_cycling)
4792 gtk_widget_grab_focus (widget);
4797 append_action_signal (GtkEntry *entry,
4799 const gchar *stock_id,
4800 const gchar *signal,
4803 GtkWidget *menuitem = gtk_image_menu_item_new_from_stock (stock_id, NULL);
4805 g_object_set_data (G_OBJECT (menuitem), I_("gtk-signal"), (char *)signal);
4806 g_signal_connect (menuitem, "activate",
4807 G_CALLBACK (activate_cb), entry);
4809 gtk_widget_set_sensitive (menuitem, sensitive);
4811 gtk_widget_show (menuitem);
4812 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
4816 popup_menu_detach (GtkWidget *attach_widget,
4819 GTK_ENTRY (attach_widget)->popup_menu = NULL;
4823 popup_position_func (GtkMenu *menu,
4829 GtkEntry *entry = GTK_ENTRY (user_data);
4830 GtkWidget *widget = GTK_WIDGET (entry);
4831 GdkScreen *screen = gtk_widget_get_screen (widget);
4834 GdkRectangle monitor;
4836 g_return_if_fail (GTK_WIDGET_REALIZED (entry));
4838 gdk_window_get_origin (widget->window, x, y);
4840 gtk_widget_size_request (entry->popup_menu, &req);
4842 *x += widget->allocation.width / 2;
4843 *y += widget->allocation.height;
4845 monitor_num = gdk_screen_get_monitor_at_point (screen, *x, *y);
4846 gtk_menu_set_monitor (menu, monitor_num);
4847 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
4849 *x = CLAMP (*x, monitor.x, monitor.x + MAX (0, monitor.width - req.width));
4850 *y = CLAMP (*y, monitor.y, monitor.y + MAX (0, monitor.height - req.height));
4857 unichar_chosen_func (const char *text,
4860 GtkEntry *entry = GTK_ENTRY (data);
4862 if (entry->editable)
4863 gtk_entry_enter_text (entry, text);
4874 popup_targets_received (GtkClipboard *clipboard,
4875 GtkSelectionData *data,
4878 PopupInfo *info = user_data;
4879 GtkEntry *entry = info->entry;
4881 if (GTK_WIDGET_REALIZED (entry))
4883 gboolean clipboard_contains_text;
4884 GtkWidget *menuitem;
4886 gboolean show_input_method_menu;
4887 gboolean show_unicode_menu;
4889 clipboard_contains_text = gtk_selection_data_targets_include_text (data);
4890 if (entry->popup_menu)
4891 gtk_widget_destroy (entry->popup_menu);
4893 entry->popup_menu = gtk_menu_new ();
4895 gtk_menu_attach_to_widget (GTK_MENU (entry->popup_menu),
4899 append_action_signal (entry, entry->popup_menu, GTK_STOCK_CUT, "cut_clipboard",
4900 entry->editable && entry->current_pos != entry->selection_bound);
4901 append_action_signal (entry, entry->popup_menu, GTK_STOCK_COPY, "copy_clipboard",
4902 entry->current_pos != entry->selection_bound);
4903 append_action_signal (entry, entry->popup_menu, GTK_STOCK_PASTE, "paste_clipboard",
4904 entry->editable && clipboard_contains_text);
4906 menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_DELETE, NULL);
4907 gtk_widget_set_sensitive (menuitem, entry->current_pos != entry->selection_bound);
4908 g_signal_connect_swapped (menuitem, "activate",
4909 G_CALLBACK (gtk_entry_delete_cb), entry);
4910 gtk_widget_show (menuitem);
4911 gtk_menu_shell_append (GTK_MENU_SHELL (entry->popup_menu), menuitem);
4913 menuitem = gtk_separator_menu_item_new ();
4914 gtk_widget_show (menuitem);
4915 gtk_menu_shell_append (GTK_MENU_SHELL (entry->popup_menu), menuitem);
4917 menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_SELECT_ALL, NULL);
4918 g_signal_connect_swapped (menuitem, "activate",
4919 G_CALLBACK (gtk_entry_select_all), entry);
4920 gtk_widget_show (menuitem);
4921 gtk_menu_shell_append (GTK_MENU_SHELL (entry->popup_menu), menuitem);
4923 g_object_get (gtk_widget_get_settings (GTK_WIDGET (entry)),
4924 "gtk-show-input-method-menu", &show_input_method_menu,
4925 "gtk-show-unicode-menu", &show_unicode_menu,
4928 if (!entry->visible)
4929 show_input_method_menu = FALSE;
4931 if (show_input_method_menu || show_unicode_menu)
4933 menuitem = gtk_separator_menu_item_new ();
4934 gtk_widget_show (menuitem);
4935 gtk_menu_shell_append (GTK_MENU_SHELL (entry->popup_menu), menuitem);
4938 if (show_input_method_menu)
4940 menuitem = gtk_menu_item_new_with_mnemonic (_("Input _Methods"));
4941 gtk_widget_set_sensitive (menuitem, entry->editable);
4942 gtk_widget_show (menuitem);
4943 submenu = gtk_menu_new ();
4944 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
4946 gtk_menu_shell_append (GTK_MENU_SHELL (entry->popup_menu), menuitem);
4948 gtk_im_multicontext_append_menuitems (GTK_IM_MULTICONTEXT (entry->im_context),
4949 GTK_MENU_SHELL (submenu));
4952 if (show_unicode_menu)
4954 menuitem = gtk_menu_item_new_with_mnemonic (_("_Insert Unicode Control Character"));
4955 gtk_widget_set_sensitive (menuitem, entry->editable);
4956 gtk_widget_show (menuitem);
4958 submenu = gtk_menu_new ();
4959 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
4960 gtk_menu_shell_append (GTK_MENU_SHELL (entry->popup_menu), menuitem);
4962 _gtk_text_util_append_special_char_menuitems (GTK_MENU_SHELL (submenu),
4963 unichar_chosen_func,
4967 g_signal_emit (entry,
4968 signals[POPULATE_POPUP],
4974 gtk_menu_popup (GTK_MENU (entry->popup_menu), NULL, NULL,
4976 info->button, info->time);
4979 gtk_menu_popup (GTK_MENU (entry->popup_menu), NULL, NULL,
4980 popup_position_func, entry,
4981 info->button, info->time);
4982 gtk_menu_shell_select_first (GTK_MENU_SHELL (entry->popup_menu), FALSE);
4986 g_object_unref (entry);
4991 gtk_entry_do_popup (GtkEntry *entry,
4992 GdkEventButton *event)
4994 PopupInfo *info = g_new (PopupInfo, 1);
4996 /* In order to know what entries we should make sensitive, we
4997 * ask for the current targets of the clipboard, and when
4998 * we get them, then we actually pop up the menu.
5000 info->entry = g_object_ref (entry);
5004 info->button = event->button;
5005 info->time = event->time;
5010 info->time = gtk_get_current_event_time ();
5013 gtk_clipboard_request_contents (gtk_widget_get_clipboard (GTK_WIDGET (entry), GDK_SELECTION_CLIPBOARD),
5014 gdk_atom_intern_static_string ("TARGETS"),
5015 popup_targets_received,
5020 gtk_entry_popup_menu (GtkWidget *widget)
5022 gtk_entry_do_popup (GTK_ENTRY (widget), NULL);
5027 gtk_entry_drag_leave (GtkWidget *widget,
5028 GdkDragContext *context,
5031 GtkEntry *entry = GTK_ENTRY (widget);
5033 entry->dnd_position = -1;
5034 gtk_widget_queue_draw (widget);
5038 gtk_entry_drag_drop (GtkWidget *widget,
5039 GdkDragContext *context,
5044 GtkEntry *entry = GTK_ENTRY (widget);
5045 GdkAtom target = GDK_NONE;
5047 if (entry->editable)
5048 target = gtk_drag_dest_find_target (widget, context, NULL);
5050 if (target != GDK_NONE)
5051 gtk_drag_get_data (widget, context, target, time);
5053 gtk_drag_finish (context, FALSE, FALSE, time);
5059 gtk_entry_drag_motion (GtkWidget *widget,
5060 GdkDragContext *context,
5065 GtkEntry *entry = GTK_ENTRY (widget);
5066 GtkWidget *source_widget;
5067 GdkDragAction suggested_action;
5068 gint new_position, old_position;
5071 x -= widget->style->xthickness;
5072 y -= widget->style->ythickness;
5074 old_position = entry->dnd_position;
5075 new_position = gtk_entry_find_position (entry, x + entry->scroll_offset);
5077 if (entry->editable &&
5078 gtk_drag_dest_find_target (widget, context, NULL) != GDK_NONE)
5080 source_widget = gtk_drag_get_source_widget (context);
5081 suggested_action = context->suggested_action;
5083 if (!gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &sel1, &sel2) ||
5084 new_position < sel1 || new_position > sel2)
5086 if (source_widget == widget)
5088 /* Default to MOVE, unless the user has
5089 * pressed ctrl or alt to affect available actions
5091 if ((context->actions & GDK_ACTION_MOVE) != 0)
5092 suggested_action = GDK_ACTION_MOVE;
5095 entry->dnd_position = new_position;
5099 if (source_widget == widget)
5100 suggested_action = 0; /* Can't drop in selection where drag started */
5102 entry->dnd_position = -1;
5107 /* Entry not editable, or no text */
5108 suggested_action = 0;
5109 entry->dnd_position = -1;
5112 gdk_drag_status (context, suggested_action, time);
5114 if (entry->dnd_position != old_position)
5115 gtk_widget_queue_draw (widget);
5121 gtk_entry_drag_data_received (GtkWidget *widget,
5122 GdkDragContext *context,
5125 GtkSelectionData *selection_data,
5129 GtkEntry *entry = GTK_ENTRY (widget);
5130 GtkEditable *editable = GTK_EDITABLE (widget);
5133 str = (gchar *) gtk_selection_data_get_text (selection_data);
5135 if (str && entry->editable)
5141 if (entry->truncate_multiline)
5142 length = truncate_multiline (str);
5144 new_position = gtk_entry_find_position (entry, x + entry->scroll_offset);
5146 if (!gtk_editable_get_selection_bounds (editable, &sel1, &sel2) ||
5147 new_position < sel1 || new_position > sel2)
5149 gtk_editable_insert_text (editable, str, length, &new_position);
5153 /* Replacing selection */
5154 gtk_editable_delete_text (editable, sel1, sel2);
5155 gtk_editable_insert_text (editable, str, length, &sel1);
5159 gtk_drag_finish (context, TRUE, context->action == GDK_ACTION_MOVE, time);
5163 /* Drag and drop didn't happen! */
5164 gtk_drag_finish (context, FALSE, FALSE, time);
5169 gtk_entry_drag_data_get (GtkWidget *widget,
5170 GdkDragContext *context,
5171 GtkSelectionData *selection_data,
5175 gint sel_start, sel_end;
5177 GtkEditable *editable = GTK_EDITABLE (widget);
5179 if (gtk_editable_get_selection_bounds (editable, &sel_start, &sel_end))
5181 gchar *str = gtk_entry_get_public_chars (GTK_ENTRY (widget), sel_start, sel_end);
5183 gtk_selection_data_set_text (selection_data, str, -1);
5191 gtk_entry_drag_data_delete (GtkWidget *widget,
5192 GdkDragContext *context)
5194 gint sel_start, sel_end;
5196 GtkEditable *editable = GTK_EDITABLE (widget);
5198 if (GTK_ENTRY (widget)->editable &&
5199 gtk_editable_get_selection_bounds (editable, &sel_start, &sel_end))
5200 gtk_editable_delete_text (editable, sel_start, sel_end);
5203 /* We display the cursor when
5205 * - the selection is empty, AND
5206 * - the widget has focus
5209 #define CURSOR_ON_MULTIPLIER 0.66
5210 #define CURSOR_OFF_MULTIPLIER 0.34
5211 #define CURSOR_PEND_MULTIPLIER 1.0
5214 cursor_blinks (GtkEntry *entry)
5216 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (entry));
5219 if (GTK_WIDGET_HAS_FOCUS (entry) &&
5221 entry->selection_bound == entry->current_pos)
5223 g_object_get (settings, "gtk-cursor-blink", &blink, NULL);
5231 get_cursor_time (GtkEntry *entry)
5233 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (entry));
5236 g_object_get (settings, "gtk-cursor-blink-time", &time, NULL);
5242 show_cursor (GtkEntry *entry)
5244 if (!entry->cursor_visible)
5246 entry->cursor_visible = TRUE;
5248 if (GTK_WIDGET_HAS_FOCUS (entry) && entry->selection_bound == entry->current_pos)
5249 gtk_widget_queue_draw (GTK_WIDGET (entry));
5254 hide_cursor (GtkEntry *entry)
5256 if (entry->cursor_visible)
5258 entry->cursor_visible = FALSE;
5260 if (GTK_WIDGET_HAS_FOCUS (entry) && entry->selection_bound == entry->current_pos)
5261 gtk_widget_queue_draw (GTK_WIDGET (entry));
5269 blink_cb (gpointer data)
5273 GDK_THREADS_ENTER ();
5275 entry = GTK_ENTRY (data);
5277 if (!GTK_WIDGET_HAS_FOCUS (entry))
5279 g_warning ("GtkEntry - did not receive focus-out-event. If you\n"
5280 "connect a handler to this signal, it must return\n"
5281 "FALSE so the entry gets the event as well");
5284 g_assert (GTK_WIDGET_HAS_FOCUS (entry));
5285 g_assert (entry->selection_bound == entry->current_pos);
5287 if (entry->cursor_visible)
5289 hide_cursor (entry);
5290 entry->blink_timeout = g_timeout_add (get_cursor_time (entry) * CURSOR_OFF_MULTIPLIER,
5296 show_cursor (entry);
5297 entry->blink_timeout = g_timeout_add (get_cursor_time (entry) * CURSOR_ON_MULTIPLIER,
5302 GDK_THREADS_LEAVE ();
5304 /* Remove ourselves */
5309 gtk_entry_check_cursor_blink (GtkEntry *entry)
5311 if (cursor_blinks (entry))
5313 if (!entry->blink_timeout)
5315 entry->blink_timeout = g_timeout_add (get_cursor_time (entry) * CURSOR_ON_MULTIPLIER,
5318 show_cursor (entry);
5323 if (entry->blink_timeout)
5325 g_source_remove (entry->blink_timeout);
5326 entry->blink_timeout = 0;
5329 entry->cursor_visible = TRUE;
5335 gtk_entry_pend_cursor_blink (GtkEntry *entry)
5337 if (cursor_blinks (entry))
5339 if (entry->blink_timeout != 0)
5340 g_source_remove (entry->blink_timeout);
5342 entry->blink_timeout = g_timeout_add (get_cursor_time (entry) * CURSOR_PEND_MULTIPLIER,
5345 show_cursor (entry);
5351 gtk_entry_completion_timeout (gpointer data)
5353 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (data);
5355 GDK_THREADS_ENTER ();
5357 completion->priv->completion_timeout = 0;
5359 if (completion->priv->filter_model &&
5360 g_utf8_strlen (gtk_entry_get_text (GTK_ENTRY (completion->priv->entry)), -1)
5361 >= completion->priv->minimum_key_length)
5365 GtkTreeSelection *s;
5366 gboolean popup_single;
5368 gtk_entry_completion_complete (completion);
5369 matches = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->filter_model), NULL);
5371 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view)));
5373 s = gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->action_view));
5375 gtk_tree_selection_unselect_all (s);
5377 actions = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->actions), NULL);
5379 g_object_get (completion, "popup-single-match", &popup_single, NULL);
5380 if ((matches > (popup_single ? 0: 1)) || actions > 0)
5382 if (GTK_WIDGET_VISIBLE (completion->priv->popup_window))
5383 _gtk_entry_completion_resize_popup (completion);
5385 _gtk_entry_completion_popup (completion);
5388 _gtk_entry_completion_popdown (completion);
5390 else if (GTK_WIDGET_VISIBLE (completion->priv->popup_window))
5391 _gtk_entry_completion_popdown (completion);
5393 GDK_THREADS_LEAVE ();
5398 static inline gboolean
5399 keyval_is_cursor_move (guint keyval)
5401 if (keyval == GDK_Up || keyval == GDK_KP_Up)
5404 if (keyval == GDK_Down || keyval == GDK_KP_Down)
5407 if (keyval == GDK_Page_Up)
5410 if (keyval == GDK_Page_Down)
5417 gtk_entry_completion_key_press (GtkWidget *widget,
5421 gint matches, actions = 0;
5422 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
5424 if (!GTK_WIDGET_MAPPED (completion->priv->popup_window))
5427 matches = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->filter_model), NULL);
5429 if (completion->priv->actions)
5430 actions = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->actions), NULL);
5432 if (keyval_is_cursor_move (event->keyval))
5434 GtkTreePath *path = NULL;
5436 if (event->keyval == GDK_Up || event->keyval == GDK_KP_Up)
5438 if (completion->priv->current_selected < 0)
5439 completion->priv->current_selected = matches + actions - 1;
5441 completion->priv->current_selected--;
5443 else if (event->keyval == GDK_Down || event->keyval == GDK_KP_Down)
5445 if (completion->priv->current_selected < matches + actions - 1)
5446 completion->priv->current_selected++;
5448 completion->priv->current_selected = -1;
5450 else if (event->keyval == GDK_Page_Up)
5452 if (completion->priv->current_selected < 0)
5453 completion->priv->current_selected = matches + actions - 1;
5454 else if (completion->priv->current_selected == 0)
5455 completion->priv->current_selected = -1;
5456 else if (completion->priv->current_selected < matches)
5458 completion->priv->current_selected -= 14;
5459 if (completion->priv->current_selected < 0)
5460 completion->priv->current_selected = 0;
5464 completion->priv->current_selected -= 14;
5465 if (completion->priv->current_selected < matches - 1)
5466 completion->priv->current_selected = matches - 1;
5469 else if (event->keyval == GDK_Page_Down)
5471 if (completion->priv->current_selected < 0)
5472 completion->priv->current_selected = 0;
5473 else if (completion->priv->current_selected < matches - 1)
5475 completion->priv->current_selected += 14;
5476 if (completion->priv->current_selected > matches - 1)
5477 completion->priv->current_selected = matches - 1;
5479 else if (completion->priv->current_selected == matches + actions - 1)
5481 completion->priv->current_selected = -1;
5485 completion->priv->current_selected += 14;
5486 if (completion->priv->current_selected > matches + actions - 1)
5487 completion->priv->current_selected = matches + actions - 1;
5491 if (completion->priv->current_selected < 0)
5493 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view)));
5494 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->action_view)));
5496 else if (completion->priv->current_selected < matches)
5498 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->action_view)));
5500 path = gtk_tree_path_new_from_indices (completion->priv->current_selected, -1);
5501 gtk_tree_view_set_cursor (GTK_TREE_VIEW (completion->priv->tree_view),
5504 else if (completion->priv->current_selected - matches >= 0)
5506 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view)));
5508 path = gtk_tree_path_new_from_indices (completion->priv->current_selected - matches, -1);
5509 gtk_tree_view_set_cursor (GTK_TREE_VIEW (completion->priv->action_view),
5513 gtk_tree_path_free (path);
5517 else if (event->keyval == GDK_Escape)
5519 _gtk_entry_completion_popdown (completion);
5523 else if (event->keyval == GDK_Tab ||
5524 event->keyval == GDK_KP_Tab ||
5525 event->keyval == GDK_ISO_Left_Tab)
5528 GtkDirectionType dir = event->keyval == GDK_ISO_Left_Tab ?
5529 GTK_DIR_TAB_BACKWARD : GTK_DIR_TAB_FORWARD;
5531 _gtk_entry_completion_popdown (completion);
5533 entry = gtk_entry_completion_get_entry (completion);
5535 gtk_widget_child_focus (gtk_widget_get_toplevel (entry), dir);
5539 else if (event->keyval == GDK_ISO_Enter ||
5540 event->keyval == GDK_KP_Enter ||
5541 event->keyval == GDK_Return)
5543 _gtk_entry_completion_popdown (completion);
5545 if (completion->priv->current_selected < matches)
5548 GtkTreeModel *model = NULL;
5549 GtkTreeSelection *sel;
5552 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view));
5553 if (!gtk_tree_selection_get_selected (sel, &model, &iter))
5556 g_signal_handler_block (completion->priv->entry,
5557 completion->priv->changed_id);
5558 g_signal_emit_by_name (completion, "match_selected",
5559 model, &iter, &entry_set);
5560 g_signal_handler_unblock (completion->priv->entry,
5561 completion->priv->changed_id);
5567 gtk_tree_model_get (model, &iter,
5568 completion->priv->text_column, &str,
5571 g_signal_handler_block (widget, completion->priv->changed_id);
5572 gtk_entry_set_text (GTK_ENTRY (widget), str);
5573 g_signal_handler_unblock (widget, completion->priv->changed_id);
5575 /* move the cursor to the end */
5576 gtk_editable_set_position (GTK_EDITABLE (widget), -1);
5583 else if (completion->priv->current_selected - matches >= 0)
5587 path = gtk_tree_path_new_from_indices (completion->priv->current_selected - matches, -1);
5589 g_signal_emit_by_name (completion, "action_activated",
5590 gtk_tree_path_get_indices (path)[0]);
5591 gtk_tree_path_free (path);
5601 gtk_entry_completion_changed (GtkWidget *entry,
5604 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
5606 /* (re)install completion timeout */
5607 if (completion->priv->completion_timeout)
5608 g_source_remove (completion->priv->completion_timeout);
5610 if (!gtk_entry_get_text (GTK_ENTRY (entry)))
5613 /* no need to normalize for this test */
5614 if (completion->priv->minimum_key_length > 0 &&
5615 strcmp ("", gtk_entry_get_text (GTK_ENTRY (entry))) == 0)
5617 if (GTK_WIDGET_VISIBLE (completion->priv->popup_window))
5618 _gtk_entry_completion_popdown (completion);
5622 completion->priv->completion_timeout =
5623 g_timeout_add (COMPLETION_TIMEOUT,
5624 gtk_entry_completion_timeout,
5629 check_completion_callback (GtkEntryCompletion *completion)
5631 completion->priv->check_completion_idle = NULL;
5633 gtk_entry_completion_complete (completion);
5634 gtk_entry_completion_insert_prefix (completion);
5640 clear_completion_callback (GtkEntry *entry,
5643 if (pspec->name == I_("cursor-position") ||
5644 pspec->name == I_("selection-bound"))
5646 GtkEntryCompletion *completion = gtk_entry_get_completion (entry);
5648 completion->priv->has_completion = FALSE;
5653 accept_completion_callback (GtkEntry *entry)
5655 GtkEntryCompletion *completion = gtk_entry_get_completion (entry);
5657 if (completion->priv->has_completion)
5658 gtk_editable_set_position (GTK_EDITABLE (entry),
5659 entry->text_length);
5665 completion_insert_text_callback (GtkEntry *entry,
5669 GtkEntryCompletion *completion)
5671 /* idle to update the selection based on the file list */
5672 if (completion->priv->check_completion_idle == NULL)
5674 completion->priv->check_completion_idle = g_idle_source_new ();
5675 g_source_set_priority (completion->priv->check_completion_idle, G_PRIORITY_HIGH);
5676 g_source_set_closure (completion->priv->check_completion_idle,
5677 g_cclosure_new_object (G_CALLBACK (check_completion_callback),
5678 G_OBJECT (completion)));
5679 g_source_attach (completion->priv->check_completion_idle, NULL);
5684 completion_changed (GtkEntryCompletion *completion,
5688 GtkEntry *entry = GTK_ENTRY (data);
5690 if (pspec->name == I_("popup-completion") ||
5691 pspec->name == I_("inline-completion"))
5693 disconnect_completion_signals (entry, completion);
5694 connect_completion_signals (entry, completion);
5699 disconnect_completion_signals (GtkEntry *entry,
5700 GtkEntryCompletion *completion)
5702 g_signal_handlers_disconnect_by_func (completion,
5703 G_CALLBACK (completion_changed), entry);
5704 if (completion->priv->changed_id > 0 &&
5705 g_signal_handler_is_connected (entry, completion->priv->changed_id))
5706 g_signal_handler_disconnect (entry, completion->priv->changed_id);
5707 g_signal_handlers_disconnect_by_func (entry,
5708 G_CALLBACK (gtk_entry_completion_key_press), completion);
5709 if (completion->priv->insert_text_id > 0 &&
5710 g_signal_handler_is_connected (entry, completion->priv->insert_text_id))
5711 g_signal_handler_disconnect (entry, completion->priv->insert_text_id);
5712 g_signal_handlers_disconnect_by_func (entry,
5713 G_CALLBACK (completion_insert_text_callback), completion);
5714 g_signal_handlers_disconnect_by_func (entry,
5715 G_CALLBACK (clear_completion_callback), completion);
5716 g_signal_handlers_disconnect_by_func (entry,
5717 G_CALLBACK (accept_completion_callback), completion);
5721 connect_completion_signals (GtkEntry *entry,
5722 GtkEntryCompletion *completion)
5724 if (completion->priv->popup_completion)
5726 completion->priv->changed_id =
5727 g_signal_connect (entry, "changed",
5728 G_CALLBACK (gtk_entry_completion_changed), completion);
5729 g_signal_connect (entry, "key_press_event",
5730 G_CALLBACK (gtk_entry_completion_key_press), completion);
5733 if (completion->priv->inline_completion)
5735 completion->priv->insert_text_id =
5736 g_signal_connect (entry, "insert_text",
5737 G_CALLBACK (completion_insert_text_callback), completion);
5738 g_signal_connect (entry, "notify",
5739 G_CALLBACK (clear_completion_callback), completion);
5740 g_signal_connect (entry, "activate",
5741 G_CALLBACK (accept_completion_callback), completion);
5742 g_signal_connect (entry, "focus_out_event",
5743 G_CALLBACK (accept_completion_callback), completion);
5745 g_signal_connect (completion, "notify",
5746 G_CALLBACK (completion_changed), entry);
5750 * gtk_entry_set_completion:
5751 * @entry: A #GtkEntry.
5752 * @completion: The #GtkEntryCompletion or %NULL.
5754 * Sets @completion to be the auxiliary completion object to use with @entry.
5755 * All further configuration of the completion mechanism is done on
5756 * @completion using the #GtkEntryCompletion API. Completion is disabled if
5757 * @completion is set to %NULL.
5762 gtk_entry_set_completion (GtkEntry *entry,
5763 GtkEntryCompletion *completion)
5765 GtkEntryCompletion *old;
5767 g_return_if_fail (GTK_IS_ENTRY (entry));
5768 g_return_if_fail (!completion || GTK_IS_ENTRY_COMPLETION (completion));
5770 old = gtk_entry_get_completion (entry);
5772 if (old == completion)
5777 if (old->priv->completion_timeout)
5779 g_source_remove (old->priv->completion_timeout);
5780 old->priv->completion_timeout = 0;
5783 if (GTK_WIDGET_MAPPED (old->priv->popup_window))
5784 _gtk_entry_completion_popdown (old);
5786 disconnect_completion_signals (entry, old);
5787 old->priv->entry = NULL;
5789 g_object_unref (old);
5794 g_object_set_data (G_OBJECT (entry), I_(GTK_ENTRY_COMPLETION_KEY), NULL);
5798 /* hook into the entry */
5799 g_object_ref (completion);
5801 connect_completion_signals (entry, completion);
5802 completion->priv->entry = GTK_WIDGET (entry);
5803 g_object_set_data (G_OBJECT (entry), I_(GTK_ENTRY_COMPLETION_KEY), completion);
5807 * gtk_entry_get_completion:
5808 * @entry: A #GtkEntry.
5810 * Returns the auxiliary completion object currently in use by @entry.
5812 * Return value: The auxiliary completion object currently in use by @entry.
5816 GtkEntryCompletion *
5817 gtk_entry_get_completion (GtkEntry *entry)
5819 GtkEntryCompletion *completion;
5821 g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
5823 completion = GTK_ENTRY_COMPLETION (g_object_get_data (G_OBJECT (entry),
5824 GTK_ENTRY_COMPLETION_KEY));
5829 #define __GTK_ENTRY_C__
5830 #include "gtkaliasdef.c"