1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* GTK - The GIMP Toolkit
3 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
22 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
23 * file for a list of people on the GTK+ Team. See the ChangeLog
24 * files for a list of changes. These files are distributed with
25 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
31 #include <pango/pango.h>
33 #include "gdk/gdkkeysyms.h"
34 #include "gtkbindings.h"
35 #include "gtkcelleditable.h"
36 #include "gtkclipboard.h"
39 #include "gtkimagemenuitem.h"
40 #include "gtkimcontextsimple.h"
41 #include "gtkimmulticontext.h"
44 #include "gtkmarshalers.h"
46 #include "gtkmenuitem.h"
47 #include "gtkseparatormenuitem.h"
48 #include "gtkselection.h"
49 #include "gtksettings.h"
51 #include "gtktextutil.h"
52 #include "gtkwindow.h"
53 #include "gtktreeview.h"
54 #include "gtktreeselection.h"
55 #include "gtkprivate.h"
56 #include "gtkentryprivate.h"
57 #include "gtkcelllayout.h"
60 #define GTK_ENTRY_COMPLETION_KEY "gtk-entry-completion-key"
62 #define MIN_ENTRY_WIDTH 150
63 #define DRAW_TIMEOUT 20
64 #define COMPLETION_TIMEOUT 300
65 #define PASSWORD_HINT_MAX 8
67 /* Initial size of buffer, in bytes */
70 /* Maximum size of text buffer, in bytes */
71 #define MAX_SIZE G_MAXUSHORT
73 static const GtkBorder default_inner_border = { 2, 2, 2, 2 };
74 static GQuark quark_inner_border = 0;
75 static GQuark quark_password_hint = 0;
77 typedef struct _GtkEntryPrivate GtkEntryPrivate;
79 #define GTK_ENTRY_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_ENTRY, GtkEntryPrivate))
81 struct _GtkEntryPrivate
85 guint blink_time; /* time in msec the cursor has blinked since last user event */
86 guint real_changed : 1;
87 guint change_count : 8;
90 gboolean interior_focus;
91 GtkShadowType shadow_type;
93 GtkAdjustment *cursor_hadjustment;
96 typedef struct _GtkEntryPasswordHint GtkEntryPasswordHint;
98 struct _GtkEntryPasswordHint
100 gchar password_hint[PASSWORD_HINT_MAX];
101 guint password_hint_timeout_id;
102 gint password_hint_length;
103 gint password_hint_position;
122 PROP_CURSOR_POSITION,
123 PROP_SELECTION_BOUND,
130 PROP_ACTIVATES_DEFAULT,
135 PROP_TRUNCATE_MULTILINE,
139 static guint signals[LAST_SIGNAL] = { 0 };
146 /* GObject, GtkObject methods
148 static void gtk_entry_editable_init (GtkEditableClass *iface);
149 static void gtk_entry_cell_editable_init (GtkCellEditableIface *iface);
150 static void gtk_entry_set_property (GObject *object,
154 static void gtk_entry_get_property (GObject *object,
158 static void gtk_entry_finalize (GObject *object);
159 static void gtk_entry_destroy (GtkObject *object);
163 static void gtk_entry_realize (GtkWidget *widget);
164 static void gtk_entry_unrealize (GtkWidget *widget);
165 static void gtk_entry_size_request (GtkWidget *widget,
166 GtkRequisition *requisition);
167 static void gtk_entry_size_allocate (GtkWidget *widget,
168 GtkAllocation *allocation);
169 static void gtk_entry_draw_frame (GtkWidget *widget,
171 static gint gtk_entry_expose (GtkWidget *widget,
172 GdkEventExpose *event);
173 static gint gtk_entry_button_press (GtkWidget *widget,
174 GdkEventButton *event);
175 static gint gtk_entry_button_release (GtkWidget *widget,
176 GdkEventButton *event);
177 static gint gtk_entry_motion_notify (GtkWidget *widget,
178 GdkEventMotion *event);
179 static gint gtk_entry_key_press (GtkWidget *widget,
181 static gint gtk_entry_key_release (GtkWidget *widget,
183 static gint gtk_entry_focus_in (GtkWidget *widget,
184 GdkEventFocus *event);
185 static gint gtk_entry_focus_out (GtkWidget *widget,
186 GdkEventFocus *event);
187 static void gtk_entry_grab_focus (GtkWidget *widget);
188 static void gtk_entry_style_set (GtkWidget *widget,
189 GtkStyle *previous_style);
190 static void gtk_entry_direction_changed (GtkWidget *widget,
191 GtkTextDirection previous_dir);
192 static void gtk_entry_state_changed (GtkWidget *widget,
193 GtkStateType previous_state);
194 static void gtk_entry_screen_changed (GtkWidget *widget,
195 GdkScreen *old_screen);
197 static gboolean gtk_entry_drag_drop (GtkWidget *widget,
198 GdkDragContext *context,
202 static gboolean gtk_entry_drag_motion (GtkWidget *widget,
203 GdkDragContext *context,
207 static void gtk_entry_drag_leave (GtkWidget *widget,
208 GdkDragContext *context,
210 static void gtk_entry_drag_data_received (GtkWidget *widget,
211 GdkDragContext *context,
214 GtkSelectionData *selection_data,
217 static void gtk_entry_drag_data_get (GtkWidget *widget,
218 GdkDragContext *context,
219 GtkSelectionData *selection_data,
222 static void gtk_entry_drag_data_delete (GtkWidget *widget,
223 GdkDragContext *context);
225 /* GtkEditable method implementations
227 static void gtk_entry_insert_text (GtkEditable *editable,
228 const gchar *new_text,
229 gint new_text_length,
231 static void gtk_entry_delete_text (GtkEditable *editable,
234 static gchar * gtk_entry_get_chars (GtkEditable *editable,
237 static void gtk_entry_real_set_position (GtkEditable *editable,
239 static gint gtk_entry_get_position (GtkEditable *editable);
240 static void gtk_entry_set_selection_bounds (GtkEditable *editable,
243 static gboolean gtk_entry_get_selection_bounds (GtkEditable *editable,
247 /* GtkCellEditable method implementations
249 static void gtk_entry_start_editing (GtkCellEditable *cell_editable,
252 /* Default signal handlers
254 static void gtk_entry_real_insert_text (GtkEditable *editable,
255 const gchar *new_text,
256 gint new_text_length,
258 static void gtk_entry_real_delete_text (GtkEditable *editable,
261 static void gtk_entry_move_cursor (GtkEntry *entry,
262 GtkMovementStep step,
264 gboolean extend_selection);
265 static void gtk_entry_insert_at_cursor (GtkEntry *entry,
267 static void gtk_entry_delete_from_cursor (GtkEntry *entry,
270 static void gtk_entry_backspace (GtkEntry *entry);
271 static void gtk_entry_cut_clipboard (GtkEntry *entry);
272 static void gtk_entry_copy_clipboard (GtkEntry *entry);
273 static void gtk_entry_paste_clipboard (GtkEntry *entry);
274 static void gtk_entry_toggle_overwrite (GtkEntry *entry);
275 static void gtk_entry_select_all (GtkEntry *entry);
276 static void gtk_entry_real_activate (GtkEntry *entry);
277 static gboolean gtk_entry_popup_menu (GtkWidget *widget);
279 static void gtk_entry_keymap_direction_changed (GdkKeymap *keymap,
281 /* IM Context Callbacks
283 static void gtk_entry_commit_cb (GtkIMContext *context,
286 static void gtk_entry_preedit_changed_cb (GtkIMContext *context,
288 static gboolean gtk_entry_retrieve_surrounding_cb (GtkIMContext *context,
290 static gboolean gtk_entry_delete_surrounding_cb (GtkIMContext *context,
297 static void gtk_entry_enter_text (GtkEntry *entry,
299 static void gtk_entry_set_positions (GtkEntry *entry,
301 gint selection_bound);
302 static void gtk_entry_draw_text (GtkEntry *entry);
303 static void gtk_entry_draw_cursor (GtkEntry *entry,
305 static PangoLayout *gtk_entry_ensure_layout (GtkEntry *entry,
306 gboolean include_preedit);
307 static void gtk_entry_reset_layout (GtkEntry *entry);
308 static void gtk_entry_queue_draw (GtkEntry *entry);
309 static void gtk_entry_recompute (GtkEntry *entry);
310 static gint gtk_entry_find_position (GtkEntry *entry,
312 static void gtk_entry_get_cursor_locations (GtkEntry *entry,
316 static void gtk_entry_adjust_scroll (GtkEntry *entry);
317 static gint gtk_entry_move_visually (GtkEntry *editable,
320 static gint gtk_entry_move_logically (GtkEntry *entry,
323 static gint gtk_entry_move_forward_word (GtkEntry *entry,
325 gboolean allow_whitespace);
326 static gint gtk_entry_move_backward_word (GtkEntry *entry,
328 gboolean allow_whitespace);
329 static void gtk_entry_delete_whitespace (GtkEntry *entry);
330 static void gtk_entry_select_word (GtkEntry *entry);
331 static void gtk_entry_select_line (GtkEntry *entry);
332 static char * gtk_entry_get_public_chars (GtkEntry *entry,
335 static void gtk_entry_paste (GtkEntry *entry,
337 static void gtk_entry_update_primary_selection (GtkEntry *entry);
338 static void gtk_entry_do_popup (GtkEntry *entry,
339 GdkEventButton *event);
340 static gboolean gtk_entry_mnemonic_activate (GtkWidget *widget,
341 gboolean group_cycling);
342 static void gtk_entry_state_changed (GtkWidget *widget,
343 GtkStateType previous_state);
344 static void gtk_entry_check_cursor_blink (GtkEntry *entry);
345 static void gtk_entry_pend_cursor_blink (GtkEntry *entry);
346 static void gtk_entry_reset_blink_time (GtkEntry *entry);
347 static void get_text_area_size (GtkEntry *entry,
352 static void get_widget_window_size (GtkEntry *entry,
357 static void gtk_entry_move_adjustments (GtkEntry *entry);
360 static gint gtk_entry_completion_timeout (gpointer data);
361 static gboolean gtk_entry_completion_key_press (GtkWidget *widget,
364 static void gtk_entry_completion_changed (GtkWidget *entry,
366 static gboolean check_completion_callback (GtkEntryCompletion *completion);
367 static void clear_completion_callback (GtkEntry *entry,
369 static gboolean accept_completion_callback (GtkEntry *entry);
370 static void completion_insert_text_callback (GtkEntry *entry,
374 GtkEntryCompletion *completion);
375 static void completion_changed (GtkEntryCompletion *completion,
378 static void disconnect_completion_signals (GtkEntry *entry,
379 GtkEntryCompletion *completion);
380 static void connect_completion_signals (GtkEntry *entry,
381 GtkEntryCompletion *completion);
383 static void begin_change (GtkEntry *entry);
384 static void end_change (GtkEntry *entry);
385 static void emit_changed (GtkEntry *entry);
387 G_DEFINE_TYPE_WITH_CODE (GtkEntry, gtk_entry, GTK_TYPE_WIDGET,
388 G_IMPLEMENT_INTERFACE (GTK_TYPE_EDITABLE,
389 gtk_entry_editable_init)
390 G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_EDITABLE,
391 gtk_entry_cell_editable_init))
394 add_move_binding (GtkBindingSet *binding_set,
397 GtkMovementStep step,
400 g_return_if_fail ((modmask & GDK_SHIFT_MASK) == 0);
402 gtk_binding_entry_add_signal (binding_set, keyval, modmask,
406 G_TYPE_BOOLEAN, FALSE);
408 /* Selection-extending version */
409 gtk_binding_entry_add_signal (binding_set, keyval, modmask | GDK_SHIFT_MASK,
413 G_TYPE_BOOLEAN, TRUE);
417 gtk_entry_class_init (GtkEntryClass *class)
419 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
420 GtkWidgetClass *widget_class;
421 GtkObjectClass *gtk_object_class;
422 GtkBindingSet *binding_set;
424 widget_class = (GtkWidgetClass*) class;
425 gtk_object_class = (GtkObjectClass *)class;
427 gobject_class->finalize = gtk_entry_finalize;
428 gobject_class->set_property = gtk_entry_set_property;
429 gobject_class->get_property = gtk_entry_get_property;
431 widget_class->realize = gtk_entry_realize;
432 widget_class->unrealize = gtk_entry_unrealize;
433 widget_class->size_request = gtk_entry_size_request;
434 widget_class->size_allocate = gtk_entry_size_allocate;
435 widget_class->expose_event = gtk_entry_expose;
436 widget_class->button_press_event = gtk_entry_button_press;
437 widget_class->button_release_event = gtk_entry_button_release;
438 widget_class->motion_notify_event = gtk_entry_motion_notify;
439 widget_class->key_press_event = gtk_entry_key_press;
440 widget_class->key_release_event = gtk_entry_key_release;
441 widget_class->focus_in_event = gtk_entry_focus_in;
442 widget_class->focus_out_event = gtk_entry_focus_out;
443 widget_class->grab_focus = gtk_entry_grab_focus;
444 widget_class->style_set = gtk_entry_style_set;
445 widget_class->direction_changed = gtk_entry_direction_changed;
446 widget_class->state_changed = gtk_entry_state_changed;
447 widget_class->screen_changed = gtk_entry_screen_changed;
448 widget_class->mnemonic_activate = gtk_entry_mnemonic_activate;
450 widget_class->drag_drop = gtk_entry_drag_drop;
451 widget_class->drag_motion = gtk_entry_drag_motion;
452 widget_class->drag_leave = gtk_entry_drag_leave;
453 widget_class->drag_data_received = gtk_entry_drag_data_received;
454 widget_class->drag_data_get = gtk_entry_drag_data_get;
455 widget_class->drag_data_delete = gtk_entry_drag_data_delete;
457 widget_class->popup_menu = gtk_entry_popup_menu;
459 gtk_object_class->destroy = gtk_entry_destroy;
461 class->move_cursor = gtk_entry_move_cursor;
462 class->insert_at_cursor = gtk_entry_insert_at_cursor;
463 class->delete_from_cursor = gtk_entry_delete_from_cursor;
464 class->backspace = gtk_entry_backspace;
465 class->cut_clipboard = gtk_entry_cut_clipboard;
466 class->copy_clipboard = gtk_entry_copy_clipboard;
467 class->paste_clipboard = gtk_entry_paste_clipboard;
468 class->toggle_overwrite = gtk_entry_toggle_overwrite;
469 class->activate = gtk_entry_real_activate;
471 quark_inner_border = g_quark_from_static_string ("gtk-entry-inner-border");
472 quark_password_hint = g_quark_from_static_string ("gtk-entry-password-hint");
474 g_object_class_install_property (gobject_class,
475 PROP_CURSOR_POSITION,
476 g_param_spec_int ("cursor-position",
477 P_("Cursor Position"),
478 P_("The current position of the insertion cursor in chars"),
482 GTK_PARAM_READABLE));
484 g_object_class_install_property (gobject_class,
485 PROP_SELECTION_BOUND,
486 g_param_spec_int ("selection-bound",
487 P_("Selection Bound"),
488 P_("The position of the opposite end of the selection from the cursor in chars"),
492 GTK_PARAM_READABLE));
494 g_object_class_install_property (gobject_class,
496 g_param_spec_boolean ("editable",
498 P_("Whether the entry contents can be edited"),
500 GTK_PARAM_READWRITE));
502 g_object_class_install_property (gobject_class,
504 g_param_spec_int ("max-length",
505 P_("Maximum length"),
506 P_("Maximum number of characters for this entry. Zero if no maximum"),
510 GTK_PARAM_READWRITE));
511 g_object_class_install_property (gobject_class,
513 g_param_spec_boolean ("visibility",
515 P_("FALSE displays the \"invisible char\" instead of the actual text (password mode)"),
517 GTK_PARAM_READWRITE));
519 g_object_class_install_property (gobject_class,
521 g_param_spec_boolean ("has-frame",
523 P_("FALSE removes outside bevel from entry"),
525 GTK_PARAM_READWRITE));
527 g_object_class_install_property (gobject_class,
529 g_param_spec_boxed ("inner-border",
531 P_("Border between text and frame. Overrides the inner-border style property"),
533 GTK_PARAM_READWRITE));
535 g_object_class_install_property (gobject_class,
537 g_param_spec_unichar ("invisible-char",
538 P_("Invisible character"),
539 P_("The character to use when masking entry contents (in \"password mode\")"),
541 GTK_PARAM_READWRITE));
543 g_object_class_install_property (gobject_class,
544 PROP_ACTIVATES_DEFAULT,
545 g_param_spec_boolean ("activates-default",
546 P_("Activates default"),
547 P_("Whether to activate the default widget (such as the default button in a dialog) when Enter is pressed"),
549 GTK_PARAM_READWRITE));
550 g_object_class_install_property (gobject_class,
552 g_param_spec_int ("width-chars",
553 P_("Width in chars"),
554 P_("Number of characters to leave space for in the entry"),
558 GTK_PARAM_READWRITE));
560 g_object_class_install_property (gobject_class,
562 g_param_spec_int ("scroll-offset",
564 P_("Number of pixels of the entry scrolled off the screen to the left"),
568 GTK_PARAM_READABLE));
570 g_object_class_install_property (gobject_class,
572 g_param_spec_string ("text",
574 P_("The contents of the entry"),
576 GTK_PARAM_READWRITE));
581 * The horizontal alignment, from 0 (left) to 1 (right).
582 * Reversed for RTL layouts.
586 g_object_class_install_property (gobject_class,
588 g_param_spec_float ("xalign",
590 P_("The horizontal alignment, from 0 (left) to 1 (right). Reversed for RTL layouts."),
594 GTK_PARAM_READWRITE));
597 * GtkEntry:truncate-multiline:
599 * When %TRUE, pasted multi-line text is truncated to the first line.
603 g_object_class_install_property (gobject_class,
604 PROP_TRUNCATE_MULTILINE,
605 g_param_spec_boolean ("truncate-multiline",
606 P_("Truncate multiline"),
607 P_("Whether to truncate multiline pastes to one line."),
609 GTK_PARAM_READWRITE));
612 * GtkEntry:shadow-type:
614 * Which kind of shadow to draw around the entry when has-frame is set.
618 g_object_class_install_property (gobject_class,
620 g_param_spec_enum ("shadow-type",
622 P_("Which kind of shadow to draw around the entry when has-frame is set"),
623 GTK_TYPE_SHADOW_TYPE,
625 GTK_PARAM_READWRITE));
627 signals[POPULATE_POPUP] =
628 g_signal_new (I_("populate_popup"),
629 G_OBJECT_CLASS_TYPE (gobject_class),
631 G_STRUCT_OFFSET (GtkEntryClass, populate_popup),
633 _gtk_marshal_VOID__OBJECT,
640 g_signal_new (I_("activate"),
641 G_OBJECT_CLASS_TYPE (gobject_class),
642 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
643 G_STRUCT_OFFSET (GtkEntryClass, activate),
645 _gtk_marshal_VOID__VOID,
647 widget_class->activate_signal = signals[ACTIVATE];
649 signals[MOVE_CURSOR] =
650 g_signal_new (I_("move_cursor"),
651 G_OBJECT_CLASS_TYPE (gobject_class),
652 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
653 G_STRUCT_OFFSET (GtkEntryClass, move_cursor),
655 _gtk_marshal_VOID__ENUM_INT_BOOLEAN,
657 GTK_TYPE_MOVEMENT_STEP,
661 signals[INSERT_AT_CURSOR] =
662 g_signal_new (I_("insert_at_cursor"),
663 G_OBJECT_CLASS_TYPE (gobject_class),
664 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
665 G_STRUCT_OFFSET (GtkEntryClass, insert_at_cursor),
667 _gtk_marshal_VOID__STRING,
671 signals[DELETE_FROM_CURSOR] =
672 g_signal_new (I_("delete_from_cursor"),
673 G_OBJECT_CLASS_TYPE (gobject_class),
674 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
675 G_STRUCT_OFFSET (GtkEntryClass, delete_from_cursor),
677 _gtk_marshal_VOID__ENUM_INT,
679 GTK_TYPE_DELETE_TYPE,
683 g_signal_new (I_("backspace"),
684 G_OBJECT_CLASS_TYPE (gobject_class),
685 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
686 G_STRUCT_OFFSET (GtkEntryClass, backspace),
688 _gtk_marshal_VOID__VOID,
691 signals[CUT_CLIPBOARD] =
692 g_signal_new (I_("cut_clipboard"),
693 G_OBJECT_CLASS_TYPE (gobject_class),
694 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
695 G_STRUCT_OFFSET (GtkEntryClass, cut_clipboard),
697 _gtk_marshal_VOID__VOID,
700 signals[COPY_CLIPBOARD] =
701 g_signal_new (I_("copy_clipboard"),
702 G_OBJECT_CLASS_TYPE (gobject_class),
703 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
704 G_STRUCT_OFFSET (GtkEntryClass, copy_clipboard),
706 _gtk_marshal_VOID__VOID,
709 signals[PASTE_CLIPBOARD] =
710 g_signal_new (I_("paste_clipboard"),
711 G_OBJECT_CLASS_TYPE (gobject_class),
712 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
713 G_STRUCT_OFFSET (GtkEntryClass, paste_clipboard),
715 _gtk_marshal_VOID__VOID,
718 signals[TOGGLE_OVERWRITE] =
719 g_signal_new (I_("toggle_overwrite"),
720 G_OBJECT_CLASS_TYPE (gobject_class),
721 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
722 G_STRUCT_OFFSET (GtkEntryClass, toggle_overwrite),
724 _gtk_marshal_VOID__VOID,
731 binding_set = gtk_binding_set_by_class (class);
733 /* Moving the insertion point */
734 add_move_binding (binding_set, GDK_Right, 0,
735 GTK_MOVEMENT_VISUAL_POSITIONS, 1);
737 add_move_binding (binding_set, GDK_Left, 0,
738 GTK_MOVEMENT_VISUAL_POSITIONS, -1);
740 add_move_binding (binding_set, GDK_KP_Right, 0,
741 GTK_MOVEMENT_VISUAL_POSITIONS, 1);
743 add_move_binding (binding_set, GDK_KP_Left, 0,
744 GTK_MOVEMENT_VISUAL_POSITIONS, -1);
746 add_move_binding (binding_set, GDK_Right, GDK_CONTROL_MASK,
747 GTK_MOVEMENT_WORDS, 1);
749 add_move_binding (binding_set, GDK_Left, GDK_CONTROL_MASK,
750 GTK_MOVEMENT_WORDS, -1);
752 add_move_binding (binding_set, GDK_KP_Right, GDK_CONTROL_MASK,
753 GTK_MOVEMENT_WORDS, 1);
755 add_move_binding (binding_set, GDK_KP_Left, GDK_CONTROL_MASK,
756 GTK_MOVEMENT_WORDS, -1);
758 add_move_binding (binding_set, GDK_Home, 0,
759 GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
761 add_move_binding (binding_set, GDK_End, 0,
762 GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
764 add_move_binding (binding_set, GDK_KP_Home, 0,
765 GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
767 add_move_binding (binding_set, GDK_KP_End, 0,
768 GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
770 add_move_binding (binding_set, GDK_Home, GDK_CONTROL_MASK,
771 GTK_MOVEMENT_BUFFER_ENDS, -1);
773 add_move_binding (binding_set, GDK_End, GDK_CONTROL_MASK,
774 GTK_MOVEMENT_BUFFER_ENDS, 1);
776 add_move_binding (binding_set, GDK_KP_Home, GDK_CONTROL_MASK,
777 GTK_MOVEMENT_BUFFER_ENDS, -1);
779 add_move_binding (binding_set, GDK_KP_End, GDK_CONTROL_MASK,
780 GTK_MOVEMENT_BUFFER_ENDS, 1);
784 gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_CONTROL_MASK,
786 GTK_TYPE_MOVEMENT_STEP, GTK_MOVEMENT_BUFFER_ENDS,
788 G_TYPE_BOOLEAN, FALSE);
789 gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_CONTROL_MASK,
791 GTK_TYPE_MOVEMENT_STEP, GTK_MOVEMENT_BUFFER_ENDS,
793 G_TYPE_BOOLEAN, TRUE);
795 gtk_binding_entry_add_signal (binding_set, GDK_slash, GDK_CONTROL_MASK,
797 GTK_TYPE_MOVEMENT_STEP, GTK_MOVEMENT_BUFFER_ENDS,
799 G_TYPE_BOOLEAN, FALSE);
800 gtk_binding_entry_add_signal (binding_set, GDK_slash, GDK_CONTROL_MASK,
802 GTK_TYPE_MOVEMENT_STEP, GTK_MOVEMENT_BUFFER_ENDS,
804 G_TYPE_BOOLEAN, TRUE);
807 gtk_binding_entry_add_signal (binding_set, GDK_backslash, GDK_CONTROL_MASK,
809 GTK_TYPE_MOVEMENT_STEP, GTK_MOVEMENT_VISUAL_POSITIONS,
811 G_TYPE_BOOLEAN, FALSE);
812 gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
814 GTK_TYPE_MOVEMENT_STEP, GTK_MOVEMENT_VISUAL_POSITIONS,
816 G_TYPE_BOOLEAN, FALSE);
820 gtk_binding_entry_add_signal (binding_set, GDK_Return, 0,
822 gtk_binding_entry_add_signal (binding_set, GDK_KP_Enter, 0,
826 gtk_binding_entry_add_signal (binding_set, GDK_Delete, 0,
827 "delete_from_cursor", 2,
828 G_TYPE_ENUM, GTK_DELETE_CHARS,
831 gtk_binding_entry_add_signal (binding_set, GDK_KP_Delete, 0,
832 "delete_from_cursor", 2,
833 G_TYPE_ENUM, GTK_DELETE_CHARS,
836 gtk_binding_entry_add_signal (binding_set, GDK_BackSpace, 0,
839 /* Make this do the same as Backspace, to help with mis-typing */
840 gtk_binding_entry_add_signal (binding_set, GDK_BackSpace, GDK_SHIFT_MASK,
843 gtk_binding_entry_add_signal (binding_set, GDK_Delete, GDK_CONTROL_MASK,
844 "delete_from_cursor", 2,
845 G_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
848 gtk_binding_entry_add_signal (binding_set, GDK_KP_Delete, GDK_CONTROL_MASK,
849 "delete_from_cursor", 2,
850 G_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
853 gtk_binding_entry_add_signal (binding_set, GDK_BackSpace, GDK_CONTROL_MASK,
854 "delete_from_cursor", 2,
855 G_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
860 gtk_binding_entry_add_signal (binding_set, GDK_x, GDK_CONTROL_MASK,
862 gtk_binding_entry_add_signal (binding_set, GDK_c, GDK_CONTROL_MASK,
863 "copy_clipboard", 0);
864 gtk_binding_entry_add_signal (binding_set, GDK_v, GDK_CONTROL_MASK,
865 "paste_clipboard", 0);
867 gtk_binding_entry_add_signal (binding_set, GDK_Delete, GDK_SHIFT_MASK,
869 gtk_binding_entry_add_signal (binding_set, GDK_Insert, GDK_CONTROL_MASK,
870 "copy_clipboard", 0);
871 gtk_binding_entry_add_signal (binding_set, GDK_Insert, GDK_SHIFT_MASK,
872 "paste_clipboard", 0);
875 gtk_binding_entry_add_signal (binding_set, GDK_Insert, 0,
876 "toggle_overwrite", 0);
877 gtk_binding_entry_add_signal (binding_set, GDK_KP_Insert, 0,
878 "toggle_overwrite", 0);
881 * GtkEntry:inner-border:
883 * Sets the text area's border between the text and the frame
887 gtk_widget_class_install_style_property (widget_class,
888 g_param_spec_boxed ("inner-border",
890 P_("Border between text and frame."),
892 GTK_PARAM_READABLE));
894 gtk_settings_install_property (g_param_spec_boolean ("gtk-entry-select-on-focus",
895 P_("Select on focus"),
896 P_("Whether to select the contents of an entry when it is focused"),
898 GTK_PARAM_READWRITE));
901 * GtkSettings:gtk-entry-password-hint-timeout:
903 * How long to show the last inputted character in hidden
904 * entries. This value is in milliseconds. 0 disables showing the
905 * last char. 600 is a good value for enabling it.
909 gtk_settings_install_property (g_param_spec_uint ("gtk-entry-password-hint-timeout",
910 P_("Password Hint Timeout"),
911 P_("How long to show the last inputted character in hidden entries"),
913 GTK_PARAM_READWRITE));
915 g_type_class_add_private (gobject_class, sizeof (GtkEntryPrivate));
919 gtk_entry_editable_init (GtkEditableClass *iface)
921 iface->do_insert_text = gtk_entry_insert_text;
922 iface->do_delete_text = gtk_entry_delete_text;
923 iface->insert_text = gtk_entry_real_insert_text;
924 iface->delete_text = gtk_entry_real_delete_text;
925 iface->get_chars = gtk_entry_get_chars;
926 iface->set_selection_bounds = gtk_entry_set_selection_bounds;
927 iface->get_selection_bounds = gtk_entry_get_selection_bounds;
928 iface->set_position = gtk_entry_real_set_position;
929 iface->get_position = gtk_entry_get_position;
933 gtk_entry_cell_editable_init (GtkCellEditableIface *iface)
935 iface->start_editing = gtk_entry_start_editing;
939 gtk_entry_set_property (GObject *object,
944 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (object);
945 GtkEntry *entry = GTK_ENTRY (object);
951 gboolean new_value = g_value_get_boolean (value);
953 if (new_value != entry->editable)
957 _gtk_entry_reset_im_context (entry);
958 if (GTK_WIDGET_HAS_FOCUS (entry))
959 gtk_im_context_focus_out (entry->im_context);
961 entry->preedit_length = 0;
962 entry->preedit_cursor = 0;
965 entry->editable = new_value;
967 if (new_value && GTK_WIDGET_HAS_FOCUS (entry))
968 gtk_im_context_focus_in (entry->im_context);
970 gtk_entry_queue_draw (entry);
975 case PROP_MAX_LENGTH:
976 gtk_entry_set_max_length (entry, g_value_get_int (value));
979 case PROP_VISIBILITY:
980 gtk_entry_set_visibility (entry, g_value_get_boolean (value));
984 gtk_entry_set_has_frame (entry, g_value_get_boolean (value));
987 case PROP_INNER_BORDER:
988 gtk_entry_set_inner_border (entry, g_value_get_boxed (value));
991 case PROP_INVISIBLE_CHAR:
992 gtk_entry_set_invisible_char (entry, g_value_get_uint (value));
995 case PROP_ACTIVATES_DEFAULT:
996 gtk_entry_set_activates_default (entry, g_value_get_boolean (value));
999 case PROP_WIDTH_CHARS:
1000 gtk_entry_set_width_chars (entry, g_value_get_int (value));
1004 gtk_entry_set_text (entry, g_value_get_string (value));
1008 gtk_entry_set_alignment (entry, g_value_get_float (value));
1011 case PROP_TRUNCATE_MULTILINE:
1012 entry->truncate_multiline = g_value_get_boolean (value);
1015 case PROP_SHADOW_TYPE:
1016 priv->shadow_type = g_value_get_enum (value);
1019 case PROP_SCROLL_OFFSET:
1020 case PROP_CURSOR_POSITION:
1022 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1028 gtk_entry_get_property (GObject *object,
1033 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (object);
1034 GtkEntry *entry = GTK_ENTRY (object);
1038 case PROP_CURSOR_POSITION:
1039 g_value_set_int (value, entry->current_pos);
1041 case PROP_SELECTION_BOUND:
1042 g_value_set_int (value, entry->selection_bound);
1045 g_value_set_boolean (value, entry->editable);
1047 case PROP_MAX_LENGTH:
1048 g_value_set_int (value, entry->text_max_length);
1050 case PROP_VISIBILITY:
1051 g_value_set_boolean (value, entry->visible);
1053 case PROP_HAS_FRAME:
1054 g_value_set_boolean (value, entry->has_frame);
1056 case PROP_INNER_BORDER:
1057 g_value_set_boxed (value, gtk_entry_get_inner_border (entry));
1059 case PROP_INVISIBLE_CHAR:
1060 g_value_set_uint (value, entry->invisible_char);
1062 case PROP_ACTIVATES_DEFAULT:
1063 g_value_set_boolean (value, entry->activates_default);
1065 case PROP_WIDTH_CHARS:
1066 g_value_set_int (value, entry->width_chars);
1068 case PROP_SCROLL_OFFSET:
1069 g_value_set_int (value, entry->scroll_offset);
1072 g_value_set_string (value, gtk_entry_get_text (entry));
1075 g_value_set_float (value, gtk_entry_get_alignment (entry));
1077 case PROP_TRUNCATE_MULTILINE:
1078 g_value_set_boolean (value, entry->truncate_multiline);
1080 case PROP_SHADOW_TYPE:
1081 g_value_set_enum (value, priv->shadow_type);
1085 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1091 gtk_entry_init (GtkEntry *entry)
1093 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
1095 GTK_WIDGET_SET_FLAGS (entry, GTK_CAN_FOCUS);
1097 entry->text_size = MIN_SIZE;
1098 entry->text = g_malloc (entry->text_size);
1099 entry->text[0] = '\0';
1101 entry->editable = TRUE;
1102 entry->visible = TRUE;
1103 entry->invisible_char = '*';
1104 entry->dnd_position = -1;
1105 entry->width_chars = -1;
1106 entry->is_cell_renderer = FALSE;
1107 entry->editing_canceled = FALSE;
1108 entry->has_frame = TRUE;
1109 entry->truncate_multiline = FALSE;
1110 priv->shadow_type = GTK_SHADOW_IN;
1113 gtk_drag_dest_set (GTK_WIDGET (entry),
1114 GTK_DEST_DEFAULT_HIGHLIGHT,
1116 GDK_ACTION_COPY | GDK_ACTION_MOVE);
1117 gtk_drag_dest_add_text_targets (GTK_WIDGET (entry));
1119 /* This object is completely private. No external entity can gain a reference
1120 * to it; so we create it here and destroy it in finalize().
1122 entry->im_context = gtk_im_multicontext_new ();
1124 g_signal_connect (entry->im_context, "commit",
1125 G_CALLBACK (gtk_entry_commit_cb), entry);
1126 g_signal_connect (entry->im_context, "preedit_changed",
1127 G_CALLBACK (gtk_entry_preedit_changed_cb), entry);
1128 g_signal_connect (entry->im_context, "retrieve_surrounding",
1129 G_CALLBACK (gtk_entry_retrieve_surrounding_cb), entry);
1130 g_signal_connect (entry->im_context, "delete_surrounding",
1131 G_CALLBACK (gtk_entry_delete_surrounding_cb), entry);
1135 begin_change (GtkEntry *entry)
1137 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
1139 priv->change_count++;
1143 end_change (GtkEntry *entry)
1145 GtkEditable *editable = GTK_EDITABLE (entry);
1146 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
1148 g_return_if_fail (priv->change_count > 0);
1150 priv->change_count--;
1152 if (priv->change_count == 0)
1154 if (priv->real_changed)
1156 g_signal_emit_by_name (editable, "changed");
1157 priv->real_changed = FALSE;
1163 emit_changed (GtkEntry *entry)
1165 GtkEditable *editable = GTK_EDITABLE (entry);
1166 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
1168 if (priv->change_count == 0)
1169 g_signal_emit_by_name (editable, "changed");
1171 priv->real_changed = TRUE;
1175 * Overwrite a memory that might contain sensitive information.
1178 trash_area (gchar *area, gsize len)
1180 volatile gchar *varea = (volatile gchar *)area;
1186 gtk_entry_destroy (GtkObject *object)
1188 GtkEntry *entry = GTK_ENTRY (object);
1191 entry->current_pos = entry->selection_bound = entry->text_length = 0;
1192 _gtk_entry_reset_im_context (entry);
1193 gtk_entry_reset_layout (entry);
1195 if (entry->blink_timeout)
1197 g_source_remove (entry->blink_timeout);
1198 entry->blink_timeout = 0;
1201 if (entry->recompute_idle)
1203 g_source_remove (entry->recompute_idle);
1204 entry->recompute_idle = 0;
1207 if (!entry->visible)
1209 /* We want to trash the text here because the entry might be leaked. */
1210 trash_area (entry->text, strlen (entry->text));
1213 GTK_OBJECT_CLASS (gtk_entry_parent_class)->destroy (object);
1217 gtk_entry_finalize (GObject *object)
1219 GtkEntry *entry = GTK_ENTRY (object);
1221 gtk_entry_set_completion (entry, NULL);
1223 if (entry->cached_layout)
1224 g_object_unref (entry->cached_layout);
1226 g_object_unref (entry->im_context);
1228 if (entry->blink_timeout)
1229 g_source_remove (entry->blink_timeout);
1231 if (entry->recompute_idle)
1232 g_source_remove (entry->recompute_idle);
1234 entry->text_size = 0;
1238 if (!entry->visible)
1239 trash_area (entry->text, strlen (entry->text));
1240 g_free (entry->text);
1244 G_OBJECT_CLASS (gtk_entry_parent_class)->finalize (object);
1248 gtk_entry_realize (GtkWidget *widget)
1251 GdkWindowAttr attributes;
1252 gint attributes_mask;
1254 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
1255 entry = GTK_ENTRY (widget);
1257 attributes.window_type = GDK_WINDOW_CHILD;
1259 get_widget_window_size (entry, &attributes.x, &attributes.y, &attributes.width, &attributes.height);
1261 attributes.wclass = GDK_INPUT_OUTPUT;
1262 attributes.visual = gtk_widget_get_visual (widget);
1263 attributes.colormap = gtk_widget_get_colormap (widget);
1264 attributes.event_mask = gtk_widget_get_events (widget);
1265 attributes.event_mask |= (GDK_EXPOSURE_MASK |
1266 GDK_BUTTON_PRESS_MASK |
1267 GDK_BUTTON_RELEASE_MASK |
1268 GDK_BUTTON1_MOTION_MASK |
1269 GDK_BUTTON3_MOTION_MASK |
1270 GDK_POINTER_MOTION_HINT_MASK |
1271 GDK_POINTER_MOTION_MASK |
1272 GDK_ENTER_NOTIFY_MASK |
1273 GDK_LEAVE_NOTIFY_MASK);
1274 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
1276 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
1277 gdk_window_set_user_data (widget->window, entry);
1279 get_text_area_size (entry, &attributes.x, &attributes.y, &attributes.width, &attributes.height);
1281 if (GTK_WIDGET_IS_SENSITIVE (widget))
1283 attributes.cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget), GDK_XTERM);
1284 attributes_mask |= GDK_WA_CURSOR;
1287 entry->text_area = gdk_window_new (widget->window, &attributes, attributes_mask);
1289 gdk_window_set_user_data (entry->text_area, entry);
1291 if (attributes_mask & GDK_WA_CURSOR)
1292 gdk_cursor_unref (attributes.cursor);
1294 widget->style = gtk_style_attach (widget->style, widget->window);
1296 gdk_window_set_background (widget->window, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1297 gdk_window_set_background (entry->text_area, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1299 gdk_window_show (entry->text_area);
1301 gtk_im_context_set_client_window (entry->im_context, entry->text_area);
1303 gtk_entry_adjust_scroll (entry);
1304 gtk_entry_update_primary_selection (entry);
1308 gtk_entry_unrealize (GtkWidget *widget)
1310 GtkEntry *entry = GTK_ENTRY (widget);
1311 GtkClipboard *clipboard;
1313 gtk_entry_reset_layout (entry);
1315 gtk_im_context_set_client_window (entry->im_context, NULL);
1317 clipboard = gtk_widget_get_clipboard (widget, GDK_SELECTION_PRIMARY);
1318 if (gtk_clipboard_get_owner (clipboard) == G_OBJECT (entry))
1319 gtk_clipboard_clear (clipboard);
1321 if (entry->text_area)
1323 gdk_window_set_user_data (entry->text_area, NULL);
1324 gdk_window_destroy (entry->text_area);
1325 entry->text_area = NULL;
1328 if (entry->popup_menu)
1330 gtk_widget_destroy (entry->popup_menu);
1331 entry->popup_menu = NULL;
1334 if (GTK_WIDGET_CLASS (gtk_entry_parent_class)->unrealize)
1335 (* GTK_WIDGET_CLASS (gtk_entry_parent_class)->unrealize) (widget);
1339 _gtk_entry_get_borders (GtkEntry *entry,
1343 GtkWidget *widget = GTK_WIDGET (entry);
1344 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (widget);
1346 if (entry->has_frame)
1348 *xborder = widget->style->xthickness;
1349 *yborder = widget->style->ythickness;
1357 if (!priv->interior_focus)
1359 *xborder += priv->focus_width;
1360 *yborder += priv->focus_width;
1365 gtk_entry_size_request (GtkWidget *widget,
1366 GtkRequisition *requisition)
1368 GtkEntry *entry = GTK_ENTRY (widget);
1369 PangoFontMetrics *metrics;
1370 gint xborder, yborder;
1371 GtkBorder inner_border;
1372 PangoContext *context;
1374 gtk_widget_ensure_style (widget);
1375 context = gtk_widget_get_pango_context (widget);
1376 metrics = pango_context_get_metrics (context,
1377 widget->style->font_desc,
1378 pango_context_get_language (context));
1380 entry->ascent = pango_font_metrics_get_ascent (metrics);
1381 entry->descent = pango_font_metrics_get_descent (metrics);
1383 _gtk_entry_get_borders (entry, &xborder, &yborder);
1384 _gtk_entry_effective_inner_border (entry, &inner_border);
1386 if (entry->width_chars < 0)
1387 requisition->width = MIN_ENTRY_WIDTH + xborder * 2 + inner_border.left + inner_border.right;
1390 gint char_width = pango_font_metrics_get_approximate_char_width (metrics);
1391 gint digit_width = pango_font_metrics_get_approximate_digit_width (metrics);
1392 gint char_pixels = (MAX (char_width, digit_width) + PANGO_SCALE - 1) / PANGO_SCALE;
1394 requisition->width = char_pixels * entry->width_chars + xborder * 2 + inner_border.left + inner_border.right;
1397 requisition->height = PANGO_PIXELS (entry->ascent + entry->descent) + yborder * 2 + inner_border.top + inner_border.bottom;
1399 pango_font_metrics_unref (metrics);
1403 get_text_area_size (GtkEntry *entry,
1410 gint xborder, yborder;
1411 GtkRequisition requisition;
1412 GtkWidget *widget = GTK_WIDGET (entry);
1413 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (widget);
1415 gtk_widget_get_child_requisition (widget, &requisition);
1416 _gtk_entry_get_borders (entry, &xborder, &yborder);
1418 if (GTK_WIDGET_REALIZED (widget))
1419 gdk_drawable_get_size (widget->window, NULL, &frame_height);
1421 frame_height = requisition.height;
1423 if (GTK_WIDGET_HAS_FOCUS (widget) && !priv->interior_focus)
1424 frame_height -= 2 * priv->focus_width;
1430 *y = frame_height / 2 - (requisition.height - yborder * 2) / 2;
1433 *width = GTK_WIDGET (entry)->allocation.width - xborder * 2;
1436 *height = requisition.height - yborder * 2;
1440 get_widget_window_size (GtkEntry *entry,
1446 GtkRequisition requisition;
1447 GtkWidget *widget = GTK_WIDGET (entry);
1449 gtk_widget_get_child_requisition (widget, &requisition);
1452 *x = widget->allocation.x;
1456 if (entry->is_cell_renderer)
1457 *y = widget->allocation.y;
1459 *y = widget->allocation.y + (widget->allocation.height - requisition.height) / 2;
1463 *width = widget->allocation.width;
1467 if (entry->is_cell_renderer)
1468 *height = widget->allocation.height;
1470 *height = requisition.height;
1475 _gtk_entry_effective_inner_border (GtkEntry *entry,
1478 GtkBorder *tmp_border;
1480 tmp_border = g_object_get_qdata (G_OBJECT (entry), quark_inner_border);
1484 *border = *tmp_border;
1488 gtk_widget_style_get (GTK_WIDGET (entry), "inner-border", &tmp_border, NULL);
1492 *border = *tmp_border;
1493 gtk_border_free (tmp_border);
1497 *border = default_inner_border;
1501 gtk_entry_size_allocate (GtkWidget *widget,
1502 GtkAllocation *allocation)
1504 GtkEntry *entry = GTK_ENTRY (widget);
1506 widget->allocation = *allocation;
1508 if (GTK_WIDGET_REALIZED (widget))
1510 /* We call gtk_widget_get_child_requisition, since we want (for
1511 * backwards compatibility reasons) the realization here to
1512 * be affected by the usize of the entry, if set
1514 gint x, y, width, height;
1516 get_widget_window_size (entry, &x, &y, &width, &height);
1518 gdk_window_move_resize (widget->window,
1519 x, y, width, height);
1521 get_text_area_size (entry, &x, &y, &width, &height);
1523 gdk_window_move_resize (entry->text_area,
1524 x, y, width, height);
1526 gtk_entry_recompute (entry);
1531 gtk_entry_draw_frame (GtkWidget *widget,
1534 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (widget);
1535 gint x = 0, y = 0, width, height;
1537 gdk_drawable_get_size (widget->window, &width, &height);
1539 if (GTK_WIDGET_HAS_FOCUS (widget) && !priv->interior_focus)
1541 x += priv->focus_width;
1542 y += priv->focus_width;
1543 width -= 2 * priv->focus_width;
1544 height -= 2 * priv->focus_width;
1547 gtk_paint_shadow (widget->style, widget->window,
1548 GTK_STATE_NORMAL, priv->shadow_type,
1549 area, widget, "entry", x, y, width, height);
1551 if (GTK_WIDGET_HAS_FOCUS (widget) && !priv->interior_focus)
1553 x -= priv->focus_width;
1554 y -= priv->focus_width;
1555 width += 2 * priv->focus_width;
1556 height += 2 * priv->focus_width;
1558 gtk_paint_focus (widget->style, widget->window, GTK_WIDGET_STATE (widget),
1559 area, widget, "entry",
1560 0, 0, width, height);
1565 gtk_entry_expose (GtkWidget *widget,
1566 GdkEventExpose *event)
1568 GtkEntry *entry = GTK_ENTRY (widget);
1570 if (widget->window == event->window)
1571 gtk_entry_draw_frame (widget, &event->area);
1572 else if (entry->text_area == event->window)
1574 gint area_width, area_height;
1576 get_text_area_size (entry, NULL, NULL, &area_width, &area_height);
1578 gtk_paint_flat_box (widget->style, entry->text_area,
1579 GTK_WIDGET_STATE(widget), GTK_SHADOW_NONE,
1580 &event->area, widget, "entry_bg",
1581 0, 0, area_width, area_height);
1583 if ((entry->visible || entry->invisible_char != 0) &&
1584 GTK_WIDGET_HAS_FOCUS (widget) &&
1585 entry->selection_bound == entry->current_pos && entry->cursor_visible)
1586 gtk_entry_draw_cursor (GTK_ENTRY (widget), CURSOR_STANDARD);
1588 if (entry->dnd_position != -1)
1589 gtk_entry_draw_cursor (GTK_ENTRY (widget), CURSOR_DND);
1591 gtk_entry_draw_text (GTK_ENTRY (widget));
1598 gtk_entry_get_pixel_ranges (GtkEntry *entry,
1602 gint start_char, end_char;
1604 if (gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &start_char, &end_char))
1606 PangoLayout *layout = gtk_entry_ensure_layout (entry, TRUE);
1607 PangoLayoutLine *line = pango_layout_get_lines_readonly (layout)->data;
1608 const char *text = pango_layout_get_text (layout);
1609 gint start_index = g_utf8_offset_to_pointer (text, start_char) - text;
1610 gint end_index = g_utf8_offset_to_pointer (text, end_char) - text;
1611 gint real_n_ranges, i;
1613 pango_layout_line_get_x_ranges (line, start_index, end_index, ranges, &real_n_ranges);
1619 for (i = 0; i < real_n_ranges; ++i)
1621 r[2 * i + 1] = (r[2 * i + 1] - r[2 * i]) / PANGO_SCALE;
1622 r[2 * i] = r[2 * i] / PANGO_SCALE;
1627 *n_ranges = real_n_ranges;
1639 in_selection (GtkEntry *entry,
1644 gint retval = FALSE;
1646 gtk_entry_get_pixel_ranges (entry, &ranges, &n_ranges);
1648 for (i = 0; i < n_ranges; ++i)
1650 if (x >= ranges[2 * i] && x < ranges[2 * i] + ranges[2 * i + 1])
1662 gtk_entry_button_press (GtkWidget *widget,
1663 GdkEventButton *event)
1665 GtkEntry *entry = GTK_ENTRY (widget);
1666 GtkEditable *editable = GTK_EDITABLE (widget);
1667 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
1669 gint sel_start, sel_end;
1671 if (event->window != entry->text_area ||
1672 (entry->button && event->button != entry->button))
1675 gtk_entry_reset_blink_time (entry);
1677 entry->button = event->button;
1679 if (!GTK_WIDGET_HAS_FOCUS (widget))
1681 entry->in_click = TRUE;
1682 gtk_widget_grab_focus (widget);
1683 entry->in_click = FALSE;
1686 tmp_pos = gtk_entry_find_position (entry, event->x + entry->scroll_offset);
1688 if (event->button == 1)
1690 gboolean have_selection = gtk_editable_get_selection_bounds (editable, &sel_start, &sel_end);
1692 entry->select_words = FALSE;
1693 entry->select_lines = FALSE;
1695 if (event->state & GDK_SHIFT_MASK)
1697 _gtk_entry_reset_im_context (entry);
1699 if (!have_selection) /* select from the current position to the clicked position */
1700 sel_start = sel_end = entry->current_pos;
1702 if (tmp_pos > sel_start && tmp_pos < sel_end)
1704 /* Truncate current selection, but keep it as big as possible */
1705 if (tmp_pos - sel_start > sel_end - tmp_pos)
1706 gtk_entry_set_positions (entry, sel_start, tmp_pos);
1708 gtk_entry_set_positions (entry, tmp_pos, sel_end);
1712 gboolean extend_to_left;
1715 /* Figure out what click selects and extend current selection */
1716 switch (event->type)
1718 case GDK_BUTTON_PRESS:
1719 gtk_entry_set_positions (entry, tmp_pos, tmp_pos);
1722 case GDK_2BUTTON_PRESS:
1723 entry->select_words = TRUE;
1724 gtk_entry_select_word (entry);
1727 case GDK_3BUTTON_PRESS:
1728 entry->select_lines = TRUE;
1729 gtk_entry_select_line (entry);
1736 start = MIN (entry->current_pos, entry->selection_bound);
1737 start = MIN (sel_start, start);
1739 end = MAX (entry->current_pos, entry->selection_bound);
1740 end = MAX (sel_end, end);
1742 if (tmp_pos == sel_start || tmp_pos == sel_end)
1743 extend_to_left = (tmp_pos == start);
1745 extend_to_left = (end == sel_end);
1748 gtk_entry_set_positions (entry, start, end);
1750 gtk_entry_set_positions (entry, end, start);
1753 else /* no shift key */
1754 switch (event->type)
1756 case GDK_BUTTON_PRESS:
1757 if (in_selection (entry, event->x + entry->scroll_offset))
1759 /* Click inside the selection - we'll either start a drag, or
1760 * clear the selection
1762 entry->in_drag = TRUE;
1763 entry->drag_start_x = event->x + entry->scroll_offset;
1764 entry->drag_start_y = event->y;
1767 gtk_editable_set_position (editable, tmp_pos);
1770 case GDK_2BUTTON_PRESS:
1771 /* We ALWAYS receive a GDK_BUTTON_PRESS immediately before
1772 * receiving a GDK_2BUTTON_PRESS so we need to reset
1773 * entry->in_drag which may have been set above
1775 entry->in_drag = FALSE;
1776 entry->select_words = TRUE;
1777 gtk_entry_select_word (entry);
1780 case GDK_3BUTTON_PRESS:
1781 /* We ALWAYS receive a GDK_BUTTON_PRESS immediately before
1782 * receiving a GDK_3BUTTON_PRESS so we need to reset
1783 * entry->in_drag which may have been set above
1785 entry->in_drag = FALSE;
1786 entry->select_lines = TRUE;
1787 gtk_entry_select_line (entry);
1796 else if (event->button == 2 && event->type == GDK_BUTTON_PRESS)
1798 if (entry->editable)
1800 priv->insert_pos = tmp_pos;
1801 gtk_entry_paste (entry, GDK_SELECTION_PRIMARY);
1806 gtk_widget_error_bell (widget);
1809 else if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
1811 gtk_entry_do_popup (entry, event);
1812 entry->button = 0; /* Don't wait for release, since the menu will gtk_grab_add */
1821 gtk_entry_button_release (GtkWidget *widget,
1822 GdkEventButton *event)
1824 GtkEntry *entry = GTK_ENTRY (widget);
1826 if (event->window != entry->text_area || entry->button != event->button)
1831 gint tmp_pos = gtk_entry_find_position (entry, entry->drag_start_x);
1833 gtk_editable_set_position (GTK_EDITABLE (entry), tmp_pos);
1840 gtk_entry_update_primary_selection (entry);
1846 _gtk_entry_get_selected_text (GtkEntry *entry)
1848 GtkEditable *editable = GTK_EDITABLE (entry);
1849 gint start_text, end_text;
1852 if (gtk_editable_get_selection_bounds (editable, &start_text, &end_text))
1853 text = gtk_editable_get_chars (editable, start_text, end_text);
1859 drag_begin_cb (GtkWidget *widget,
1860 GdkDragContext *context,
1863 g_signal_handlers_disconnect_by_func (widget, drag_begin_cb, NULL);
1867 gtk_entry_motion_notify (GtkWidget *widget,
1868 GdkEventMotion *event)
1870 GtkEntry *entry = GTK_ENTRY (widget);
1873 if (entry->mouse_cursor_obscured)
1877 cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget), GDK_XTERM);
1878 gdk_window_set_cursor (entry->text_area, cursor);
1879 gdk_cursor_unref (cursor);
1880 entry->mouse_cursor_obscured = FALSE;
1883 if (event->window != entry->text_area || entry->button != 1)
1886 if (entry->select_lines)
1889 gdk_event_request_motions (event);
1893 if (gtk_drag_check_threshold (widget,
1894 entry->drag_start_x, entry->drag_start_y,
1895 event->x + entry->scroll_offset, event->y))
1897 GdkDragContext *context;
1898 GtkTargetList *target_list = gtk_target_list_new (NULL, 0);
1899 guint actions = entry->editable ? GDK_ACTION_COPY | GDK_ACTION_MOVE : GDK_ACTION_COPY;
1901 GdkPixmap *pixmap = NULL;
1903 gtk_target_list_add_text_targets (target_list, 0);
1907 text = _gtk_entry_get_selected_text (entry);
1908 pixmap = _gtk_text_util_create_drag_icon (widget, text, -1);
1911 context = gtk_drag_begin (widget, target_list, actions,
1912 entry->button, (GdkEvent *)event);
1915 gtk_drag_set_icon_pixmap (context,
1916 gdk_drawable_get_colormap (pixmap),
1921 gtk_drag_set_icon_default (context);
1924 g_object_unref (pixmap);
1927 entry->in_drag = FALSE;
1930 gtk_target_list_unref (target_list);
1936 gdk_drawable_get_size (entry->text_area, NULL, &height);
1940 else if (event->y >= height)
1941 tmp_pos = entry->text_length;
1943 tmp_pos = gtk_entry_find_position (entry, event->x + entry->scroll_offset);
1945 if (entry->select_words)
1948 gint old_min, old_max;
1951 min = gtk_entry_move_backward_word (entry, tmp_pos, TRUE);
1952 max = gtk_entry_move_forward_word (entry, tmp_pos, TRUE);
1954 pos = entry->current_pos;
1955 bound = entry->selection_bound;
1957 old_min = MIN(entry->current_pos, entry->selection_bound);
1958 old_max = MAX(entry->current_pos, entry->selection_bound);
1965 else if (old_max < max)
1970 else if (pos == old_min)
1972 if (entry->current_pos != min)
1977 if (entry->current_pos != max)
1981 gtk_entry_set_positions (entry, pos, bound);
1984 gtk_entry_set_positions (entry, tmp_pos, -1);
1991 set_invisible_cursor (GdkWindow *window)
1993 GdkBitmap *empty_bitmap;
1996 char invisible_cursor_bits[] = { 0x0 };
1998 useless.red = useless.green = useless.blue = 0;
2001 empty_bitmap = gdk_bitmap_create_from_data (window,
2002 invisible_cursor_bits,
2005 cursor = gdk_cursor_new_from_pixmap (empty_bitmap,
2010 gdk_window_set_cursor (window, cursor);
2012 gdk_cursor_unref (cursor);
2014 g_object_unref (empty_bitmap);
2018 gtk_entry_obscure_mouse_cursor (GtkEntry *entry)
2020 if (entry->mouse_cursor_obscured)
2023 set_invisible_cursor (entry->text_area);
2025 entry->mouse_cursor_obscured = TRUE;
2029 gtk_entry_key_press (GtkWidget *widget,
2032 GtkEntry *entry = GTK_ENTRY (widget);
2034 gtk_entry_reset_blink_time (entry);
2035 gtk_entry_pend_cursor_blink (entry);
2037 if (entry->editable)
2039 if (gtk_im_context_filter_keypress (entry->im_context, event))
2041 gtk_entry_obscure_mouse_cursor (entry);
2042 entry->need_im_reset = TRUE;
2047 if (event->keyval == GDK_Return ||
2048 event->keyval == GDK_KP_Enter ||
2049 event->keyval == GDK_ISO_Enter ||
2050 event->keyval == GDK_Escape)
2052 GtkEntryCompletion *completion = gtk_entry_get_completion (entry);
2054 if (completion && completion->priv->completion_timeout)
2056 g_source_remove (completion->priv->completion_timeout);
2057 completion->priv->completion_timeout = 0;
2060 _gtk_entry_reset_im_context (entry);
2063 if (GTK_WIDGET_CLASS (gtk_entry_parent_class)->key_press_event (widget, event))
2064 /* Activate key bindings
2068 if (!entry->editable && event->length)
2069 gtk_widget_error_bell (widget);
2075 gtk_entry_key_release (GtkWidget *widget,
2078 GtkEntry *entry = GTK_ENTRY (widget);
2080 if (entry->editable)
2082 if (gtk_im_context_filter_keypress (entry->im_context, event))
2084 entry->need_im_reset = TRUE;
2089 return GTK_WIDGET_CLASS (gtk_entry_parent_class)->key_release_event (widget, event);
2093 gtk_entry_focus_in (GtkWidget *widget,
2094 GdkEventFocus *event)
2096 GtkEntry *entry = GTK_ENTRY (widget);
2098 gtk_widget_queue_draw (widget);
2100 if (entry->editable)
2102 entry->need_im_reset = TRUE;
2103 gtk_im_context_focus_in (entry->im_context);
2106 g_signal_connect (gdk_keymap_get_for_display (gtk_widget_get_display (widget)),
2107 "direction_changed",
2108 G_CALLBACK (gtk_entry_keymap_direction_changed), entry);
2110 gtk_entry_reset_blink_time (entry);
2111 gtk_entry_check_cursor_blink (entry);
2117 gtk_entry_focus_out (GtkWidget *widget,
2118 GdkEventFocus *event)
2120 GtkEntry *entry = GTK_ENTRY (widget);
2121 GtkEntryCompletion *completion;
2123 gtk_widget_queue_draw (widget);
2125 if (entry->editable)
2127 entry->need_im_reset = TRUE;
2128 gtk_im_context_focus_out (entry->im_context);
2131 gtk_entry_check_cursor_blink (entry);
2133 g_signal_handlers_disconnect_by_func (gdk_keymap_get_for_display (gtk_widget_get_display (widget)),
2134 gtk_entry_keymap_direction_changed,
2137 completion = gtk_entry_get_completion (entry);
2139 _gtk_entry_completion_popdown (completion);
2145 gtk_entry_grab_focus (GtkWidget *widget)
2147 GtkEntry *entry = GTK_ENTRY (widget);
2148 gboolean select_on_focus;
2150 GTK_WIDGET_CLASS (gtk_entry_parent_class)->grab_focus (widget);
2152 if (entry->editable && !entry->in_click)
2154 g_object_get (gtk_widget_get_settings (widget),
2155 "gtk-entry-select-on-focus",
2159 if (select_on_focus)
2160 gtk_editable_select_region (GTK_EDITABLE (widget), 0, -1);
2165 gtk_entry_direction_changed (GtkWidget *widget,
2166 GtkTextDirection previous_dir)
2168 GtkEntry *entry = GTK_ENTRY (widget);
2170 gtk_entry_recompute (entry);
2172 GTK_WIDGET_CLASS (gtk_entry_parent_class)->direction_changed (widget, previous_dir);
2176 gtk_entry_state_changed (GtkWidget *widget,
2177 GtkStateType previous_state)
2179 GtkEntry *entry = GTK_ENTRY (widget);
2182 if (GTK_WIDGET_REALIZED (widget))
2184 gdk_window_set_background (widget->window, &widget->style->base[GTK_WIDGET_STATE (widget)]);
2185 gdk_window_set_background (entry->text_area, &widget->style->base[GTK_WIDGET_STATE (widget)]);
2187 if (GTK_WIDGET_IS_SENSITIVE (widget))
2188 cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget), GDK_XTERM);
2192 gdk_window_set_cursor (entry->text_area, cursor);
2195 gdk_cursor_unref (cursor);
2197 entry->mouse_cursor_obscured = FALSE;
2200 if (!GTK_WIDGET_IS_SENSITIVE (widget))
2202 /* Clear any selection */
2203 gtk_editable_select_region (GTK_EDITABLE (entry), entry->current_pos, entry->current_pos);
2206 gtk_widget_queue_draw (widget);
2210 gtk_entry_screen_changed (GtkWidget *widget,
2211 GdkScreen *old_screen)
2213 gtk_entry_recompute (GTK_ENTRY (widget));
2216 /* GtkEditable method implementations
2219 gtk_entry_insert_text (GtkEditable *editable,
2220 const gchar *new_text,
2221 gint new_text_length,
2224 GtkEntry *entry = GTK_ENTRY (editable);
2228 if (*position < 0 || *position > entry->text_length)
2229 *position = entry->text_length;
2231 g_object_ref (editable);
2233 if (new_text_length <= 63)
2236 text = g_new (gchar, new_text_length + 1);
2238 text[new_text_length] = '\0';
2239 strncpy (text, new_text, new_text_length);
2241 g_signal_emit_by_name (editable, "insert_text", text, new_text_length, position);
2243 if (!entry->visible)
2244 trash_area (text, new_text_length);
2246 if (new_text_length > 63)
2249 g_object_unref (editable);
2253 gtk_entry_delete_text (GtkEditable *editable,
2257 GtkEntry *entry = GTK_ENTRY (editable);
2259 if (end_pos < 0 || end_pos > entry->text_length)
2260 end_pos = entry->text_length;
2263 if (start_pos > end_pos)
2264 start_pos = end_pos;
2266 g_object_ref (editable);
2268 g_signal_emit_by_name (editable, "delete_text", start_pos, end_pos);
2270 g_object_unref (editable);
2274 gtk_entry_get_chars (GtkEditable *editable,
2278 GtkEntry *entry = GTK_ENTRY (editable);
2279 gint start_index, end_index;
2282 end_pos = entry->text_length;
2284 start_pos = MIN (entry->text_length, start_pos);
2285 end_pos = MIN (entry->text_length, end_pos);
2287 start_index = g_utf8_offset_to_pointer (entry->text, start_pos) - entry->text;
2288 end_index = g_utf8_offset_to_pointer (entry->text, end_pos) - entry->text;
2290 return g_strndup (entry->text + start_index, end_index - start_index);
2294 gtk_entry_real_set_position (GtkEditable *editable,
2297 GtkEntry *entry = GTK_ENTRY (editable);
2299 if (position < 0 || position > entry->text_length)
2300 position = entry->text_length;
2302 if (position != entry->current_pos ||
2303 position != entry->selection_bound)
2305 _gtk_entry_reset_im_context (entry);
2306 gtk_entry_set_positions (entry, position, position);
2311 gtk_entry_get_position (GtkEditable *editable)
2313 return GTK_ENTRY (editable)->current_pos;
2317 gtk_entry_set_selection_bounds (GtkEditable *editable,
2321 GtkEntry *entry = GTK_ENTRY (editable);
2324 start = entry->text_length;
2326 end = entry->text_length;
2328 _gtk_entry_reset_im_context (entry);
2330 gtk_entry_set_positions (entry,
2331 MIN (end, entry->text_length),
2332 MIN (start, entry->text_length));
2334 gtk_entry_update_primary_selection (entry);
2338 gtk_entry_get_selection_bounds (GtkEditable *editable,
2342 GtkEntry *entry = GTK_ENTRY (editable);
2344 *start = entry->selection_bound;
2345 *end = entry->current_pos;
2347 return (entry->selection_bound != entry->current_pos);
2351 gtk_entry_style_set (GtkWidget *widget,
2352 GtkStyle *previous_style)
2354 GtkEntry *entry = GTK_ENTRY (widget);
2355 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
2357 gtk_widget_style_get (widget,
2358 "focus-line-width", &priv->focus_width,
2359 "interior-focus", &priv->interior_focus,
2362 gtk_entry_recompute (entry);
2364 if (previous_style && GTK_WIDGET_REALIZED (widget))
2366 gdk_window_set_background (widget->window, &widget->style->base[GTK_WIDGET_STATE (widget)]);
2367 gdk_window_set_background (entry->text_area, &widget->style->base[GTK_WIDGET_STATE (widget)]);
2371 /* GtkCellEditable method implementations
2374 gtk_cell_editable_entry_activated (GtkEntry *entry, gpointer data)
2376 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (entry));
2377 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (entry));
2381 gtk_cell_editable_key_press_event (GtkEntry *entry,
2382 GdkEventKey *key_event,
2385 if (key_event->keyval == GDK_Escape)
2387 entry->editing_canceled = TRUE;
2388 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (entry));
2389 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (entry));
2394 /* override focus */
2395 if (key_event->keyval == GDK_Up || key_event->keyval == GDK_Down)
2397 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (entry));
2398 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (entry));
2407 gtk_entry_start_editing (GtkCellEditable *cell_editable,
2410 GTK_ENTRY (cell_editable)->is_cell_renderer = TRUE;
2412 g_signal_connect (cell_editable, "activate",
2413 G_CALLBACK (gtk_cell_editable_entry_activated), NULL);
2414 g_signal_connect (cell_editable, "key_press_event",
2415 G_CALLBACK (gtk_cell_editable_key_press_event), NULL);
2419 gtk_entry_password_hint_free (GtkEntryPasswordHint *password_hint)
2421 if (password_hint->password_hint_timeout_id)
2422 g_source_remove (password_hint->password_hint_timeout_id);
2424 g_free (password_hint);
2427 /* Default signal handlers
2430 gtk_entry_real_insert_text (GtkEditable *editable,
2431 const gchar *new_text,
2432 gint new_text_length,
2435 GtkEntry *entry = GTK_ENTRY (editable);
2439 if (new_text_length < 0)
2440 new_text_length = strlen (new_text);
2442 n_chars = g_utf8_strlen (new_text, new_text_length);
2443 if (entry->text_max_length > 0 && n_chars + entry->text_length > entry->text_max_length)
2445 gtk_widget_error_bell (GTK_WIDGET (entry));
2446 n_chars = entry->text_max_length - entry->text_length;
2447 new_text_length = g_utf8_offset_to_pointer (new_text, n_chars) - new_text;
2450 if (new_text_length + entry->n_bytes + 1 > entry->text_size)
2452 gsize prev_size = entry->text_size;
2454 while (new_text_length + entry->n_bytes + 1 > entry->text_size)
2456 if (entry->text_size == 0)
2457 entry->text_size = MIN_SIZE;
2460 if (2 * (guint)entry->text_size < MAX_SIZE &&
2461 2 * (guint)entry->text_size > entry->text_size)
2462 entry->text_size *= 2;
2465 entry->text_size = MAX_SIZE;
2466 if (new_text_length > (gint)entry->text_size - (gint)entry->n_bytes - 1)
2468 new_text_length = (gint)entry->text_size - (gint)entry->n_bytes - 1;
2469 new_text_length = g_utf8_find_prev_char (new_text, new_text + new_text_length + 1) - new_text;
2470 n_chars = g_utf8_strlen (new_text, new_text_length);
2478 entry->text = g_realloc (entry->text, entry->text_size);
2481 /* Same thing, just slower and without leaving stuff in memory. */
2482 gchar *et_new = g_malloc (entry->text_size);
2483 memcpy (et_new, entry->text, MIN (prev_size, entry->text_size));
2484 trash_area (entry->text, prev_size);
2485 g_free (entry->text);
2486 entry->text = et_new;
2490 index = g_utf8_offset_to_pointer (entry->text, *position) - entry->text;
2492 g_memmove (entry->text + index + new_text_length, entry->text + index, entry->n_bytes - index);
2493 memcpy (entry->text + index, new_text, new_text_length);
2495 entry->n_bytes += new_text_length;
2496 entry->text_length += n_chars;
2498 /* NUL terminate for safety and convenience */
2499 entry->text[entry->n_bytes] = '\0';
2501 if (entry->current_pos > *position)
2502 entry->current_pos += n_chars;
2504 if (entry->selection_bound > *position)
2505 entry->selection_bound += n_chars;
2507 if (n_chars == 1 && !entry->visible && (new_text_length < PASSWORD_HINT_MAX))
2509 guint password_hint_timeout;
2511 g_object_get (gtk_widget_get_settings (GTK_WIDGET (entry)),
2512 "gtk-entry-password-hint-timeout", &password_hint_timeout,
2515 if (password_hint_timeout > 0)
2517 GtkEntryPasswordHint *password_hint = g_object_get_qdata (G_OBJECT (entry),
2518 quark_password_hint);
2522 password_hint = g_new0 (GtkEntryPasswordHint, 1);
2523 g_object_set_qdata_full (G_OBJECT (entry), quark_password_hint,
2525 (GDestroyNotify) gtk_entry_password_hint_free);
2528 memset (&password_hint->password_hint, 0x0, PASSWORD_HINT_MAX);
2529 password_hint->password_hint_length = new_text_length;
2530 memcpy (&password_hint->password_hint, new_text, new_text_length);
2531 password_hint->password_hint_position = *position + n_chars;
2536 g_object_set_qdata (G_OBJECT (entry), quark_password_hint, NULL);
2539 *position += n_chars;
2541 gtk_entry_recompute (entry);
2543 emit_changed (entry);
2544 g_object_notify (G_OBJECT (editable), "text");
2548 gtk_entry_real_delete_text (GtkEditable *editable,
2552 GtkEntry *entry = GTK_ENTRY (editable);
2556 if (end_pos < 0 || end_pos > entry->text_length)
2557 end_pos = entry->text_length;
2559 if (start_pos < end_pos)
2561 gint start_index = g_utf8_offset_to_pointer (entry->text, start_pos) - entry->text;
2562 gint end_index = g_utf8_offset_to_pointer (entry->text, end_pos) - entry->text;
2564 gint selection_bound;
2566 g_memmove (entry->text + start_index, entry->text + end_index, entry->n_bytes + 1 - end_index);
2567 entry->text_length -= (end_pos - start_pos);
2568 entry->n_bytes -= (end_index - start_index);
2570 /* In password-mode, make sure we don't leave anything sensitive after
2571 * the terminating zero. Note, that the terminating zero already trashed
2574 if (!entry->visible)
2575 trash_area (entry->text + entry->n_bytes + 1, end_index - start_index - 1);
2577 current_pos = entry->current_pos;
2578 if (current_pos > start_pos)
2579 current_pos -= MIN (current_pos, end_pos) - start_pos;
2581 selection_bound = entry->selection_bound;
2582 if (selection_bound > start_pos)
2583 selection_bound -= MIN (selection_bound, end_pos) - start_pos;
2585 gtk_entry_set_positions (entry, current_pos, selection_bound);
2587 /* We might have deleted the selection
2589 gtk_entry_update_primary_selection (entry);
2591 gtk_entry_recompute (entry);
2593 emit_changed (entry);
2594 g_object_notify (G_OBJECT (editable), "text");
2598 /* Compute the X position for an offset that corresponds to the "more important
2599 * cursor position for that offset. We use this when trying to guess to which
2600 * end of the selection we should go to when the user hits the left or
2604 get_better_cursor_x (GtkEntry *entry,
2607 GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (entry)));
2608 PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
2609 gboolean split_cursor;
2611 PangoLayout *layout = gtk_entry_ensure_layout (entry, TRUE);
2612 const gchar *text = pango_layout_get_text (layout);
2613 gint index = g_utf8_offset_to_pointer (text, offset) - text;
2615 PangoRectangle strong_pos, weak_pos;
2617 g_object_get (gtk_widget_get_settings (GTK_WIDGET (entry)),
2618 "gtk-split-cursor", &split_cursor,
2621 pango_layout_get_cursor_pos (layout, index, &strong_pos, &weak_pos);
2624 return strong_pos.x / PANGO_SCALE;
2626 return (keymap_direction == entry->resolved_dir) ? strong_pos.x / PANGO_SCALE : weak_pos.x / PANGO_SCALE;
2630 gtk_entry_move_cursor (GtkEntry *entry,
2631 GtkMovementStep step,
2633 gboolean extend_selection)
2635 gint new_pos = entry->current_pos;
2637 _gtk_entry_reset_im_context (entry);
2639 if (entry->current_pos != entry->selection_bound && !extend_selection)
2641 /* If we have a current selection and aren't extending it, move to the
2642 * start/or end of the selection as appropriate
2646 case GTK_MOVEMENT_VISUAL_POSITIONS:
2648 gint current_x = get_better_cursor_x (entry, entry->current_pos);
2649 gint bound_x = get_better_cursor_x (entry, entry->selection_bound);
2652 new_pos = current_x < bound_x ? entry->current_pos : entry->selection_bound;
2654 new_pos = current_x > bound_x ? entry->current_pos : entry->selection_bound;
2657 case GTK_MOVEMENT_LOGICAL_POSITIONS:
2658 case GTK_MOVEMENT_WORDS:
2660 new_pos = MIN (entry->current_pos, entry->selection_bound);
2662 new_pos = MAX (entry->current_pos, entry->selection_bound);
2664 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
2665 case GTK_MOVEMENT_PARAGRAPH_ENDS:
2666 case GTK_MOVEMENT_BUFFER_ENDS:
2667 new_pos = count < 0 ? 0 : entry->text_length;
2669 case GTK_MOVEMENT_DISPLAY_LINES:
2670 case GTK_MOVEMENT_PARAGRAPHS:
2671 case GTK_MOVEMENT_PAGES:
2672 case GTK_MOVEMENT_HORIZONTAL_PAGES:
2680 case GTK_MOVEMENT_LOGICAL_POSITIONS:
2681 new_pos = gtk_entry_move_logically (entry, new_pos, count);
2683 case GTK_MOVEMENT_VISUAL_POSITIONS:
2684 new_pos = gtk_entry_move_visually (entry, new_pos, count);
2685 if (entry->current_pos == new_pos)
2687 if (!extend_selection)
2689 if (!gtk_widget_keynav_failed (GTK_WIDGET (entry),
2691 GTK_DIR_RIGHT : GTK_DIR_LEFT))
2693 GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (entry));
2696 gtk_widget_child_focus (toplevel,
2698 GTK_DIR_RIGHT : GTK_DIR_LEFT);
2703 gtk_widget_error_bell (GTK_WIDGET (entry));
2707 case GTK_MOVEMENT_WORDS:
2710 new_pos = gtk_entry_move_forward_word (entry, new_pos, FALSE);
2715 new_pos = gtk_entry_move_backward_word (entry, new_pos, FALSE);
2718 if (entry->current_pos == new_pos)
2719 gtk_widget_error_bell (GTK_WIDGET (entry));
2721 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
2722 case GTK_MOVEMENT_PARAGRAPH_ENDS:
2723 case GTK_MOVEMENT_BUFFER_ENDS:
2724 new_pos = count < 0 ? 0 : entry->text_length;
2725 if (entry->current_pos == new_pos)
2726 gtk_widget_error_bell (GTK_WIDGET (entry));
2728 case GTK_MOVEMENT_DISPLAY_LINES:
2729 case GTK_MOVEMENT_PARAGRAPHS:
2730 case GTK_MOVEMENT_PAGES:
2731 case GTK_MOVEMENT_HORIZONTAL_PAGES:
2736 if (extend_selection)
2737 gtk_editable_select_region (GTK_EDITABLE (entry), entry->selection_bound, new_pos);
2739 gtk_editable_set_position (GTK_EDITABLE (entry), new_pos);
2741 gtk_entry_pend_cursor_blink (entry);
2745 gtk_entry_insert_at_cursor (GtkEntry *entry,
2748 GtkEditable *editable = GTK_EDITABLE (entry);
2749 gint pos = entry->current_pos;
2751 if (entry->editable)
2753 _gtk_entry_reset_im_context (entry);
2755 gtk_editable_insert_text (editable, str, -1, &pos);
2756 gtk_editable_set_position (editable, pos);
2761 gtk_entry_delete_from_cursor (GtkEntry *entry,
2765 GtkEditable *editable = GTK_EDITABLE (entry);
2766 gint start_pos = entry->current_pos;
2767 gint end_pos = entry->current_pos;
2768 gint old_n_bytes = entry->n_bytes;
2770 _gtk_entry_reset_im_context (entry);
2772 if (!entry->editable)
2774 gtk_widget_error_bell (GTK_WIDGET (entry));
2778 if (entry->selection_bound != entry->current_pos)
2780 gtk_editable_delete_selection (editable);
2786 case GTK_DELETE_CHARS:
2787 end_pos = gtk_entry_move_logically (entry, entry->current_pos, count);
2788 gtk_editable_delete_text (editable, MIN (start_pos, end_pos), MAX (start_pos, end_pos));
2790 case GTK_DELETE_WORDS:
2793 /* Move to end of current word, or if not on a word, end of previous word */
2794 end_pos = gtk_entry_move_backward_word (entry, end_pos, FALSE);
2795 end_pos = gtk_entry_move_forward_word (entry, end_pos, FALSE);
2799 /* Move to beginning of current word, or if not on a word, begining of next word */
2800 start_pos = gtk_entry_move_forward_word (entry, start_pos, FALSE);
2801 start_pos = gtk_entry_move_backward_word (entry, start_pos, FALSE);
2805 case GTK_DELETE_WORD_ENDS:
2808 start_pos = gtk_entry_move_backward_word (entry, start_pos, FALSE);
2813 end_pos = gtk_entry_move_forward_word (entry, end_pos, FALSE);
2816 gtk_editable_delete_text (editable, start_pos, end_pos);
2818 case GTK_DELETE_DISPLAY_LINE_ENDS:
2819 case GTK_DELETE_PARAGRAPH_ENDS:
2821 gtk_editable_delete_text (editable, 0, entry->current_pos);
2823 gtk_editable_delete_text (editable, entry->current_pos, -1);
2825 case GTK_DELETE_DISPLAY_LINES:
2826 case GTK_DELETE_PARAGRAPHS:
2827 gtk_editable_delete_text (editable, 0, -1);
2829 case GTK_DELETE_WHITESPACE:
2830 gtk_entry_delete_whitespace (entry);
2834 if (entry->n_bytes == old_n_bytes)
2835 gtk_widget_error_bell (GTK_WIDGET (entry));
2837 gtk_entry_pend_cursor_blink (entry);
2841 gtk_entry_backspace (GtkEntry *entry)
2843 GtkEditable *editable = GTK_EDITABLE (entry);
2846 _gtk_entry_reset_im_context (entry);
2848 if (!entry->editable || !entry->text)
2850 gtk_widget_error_bell (GTK_WIDGET (entry));
2854 if (entry->selection_bound != entry->current_pos)
2856 gtk_editable_delete_selection (editable);
2860 prev_pos = gtk_entry_move_logically(entry, entry->current_pos, -1);
2862 if (prev_pos < entry->current_pos)
2864 PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
2865 PangoLogAttr *log_attrs;
2868 pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
2870 if (log_attrs[entry->current_pos].backspace_deletes_character)
2872 gchar *cluster_text;
2873 gchar *normalized_text;
2876 cluster_text = gtk_editable_get_chars (editable,
2878 entry->current_pos);
2879 normalized_text = g_utf8_normalize (cluster_text,
2880 strlen (cluster_text),
2882 len = g_utf8_strlen (normalized_text, -1);
2884 gtk_editable_delete_text (editable, prev_pos, entry->current_pos);
2887 gint pos = entry->current_pos;
2889 gtk_editable_insert_text (editable, normalized_text,
2890 g_utf8_offset_to_pointer (normalized_text, len - 1) - normalized_text,
2892 gtk_editable_set_position (editable, pos);
2895 g_free (normalized_text);
2896 g_free (cluster_text);
2900 gtk_editable_delete_text (editable, prev_pos, entry->current_pos);
2907 gtk_widget_error_bell (GTK_WIDGET (entry));
2910 gtk_entry_pend_cursor_blink (entry);
2914 gtk_entry_copy_clipboard (GtkEntry *entry)
2916 GtkEditable *editable = GTK_EDITABLE (entry);
2919 if (gtk_editable_get_selection_bounds (editable, &start, &end))
2921 gchar *str = gtk_entry_get_public_chars (entry, start, end);
2922 gtk_clipboard_set_text (gtk_widget_get_clipboard (GTK_WIDGET (entry),
2923 GDK_SELECTION_CLIPBOARD),
2930 gtk_entry_cut_clipboard (GtkEntry *entry)
2932 GtkEditable *editable = GTK_EDITABLE (entry);
2935 gtk_entry_copy_clipboard (entry);
2937 if (entry->editable)
2939 if (gtk_editable_get_selection_bounds (editable, &start, &end))
2940 gtk_editable_delete_text (editable, start, end);
2944 gtk_widget_error_bell (GTK_WIDGET (entry));
2949 gtk_entry_paste_clipboard (GtkEntry *entry)
2951 if (entry->editable)
2952 gtk_entry_paste (entry, GDK_NONE);
2954 gtk_widget_error_bell (GTK_WIDGET (entry));
2958 gtk_entry_delete_cb (GtkEntry *entry)
2960 GtkEditable *editable = GTK_EDITABLE (entry);
2963 if (entry->editable)
2965 if (gtk_editable_get_selection_bounds (editable, &start, &end))
2966 gtk_editable_delete_text (editable, start, end);
2971 gtk_entry_toggle_overwrite (GtkEntry *entry)
2973 entry->overwrite_mode = !entry->overwrite_mode;
2977 gtk_entry_select_all (GtkEntry *entry)
2979 gtk_entry_select_line (entry);
2983 gtk_entry_real_activate (GtkEntry *entry)
2986 GtkWidget *toplevel;
2989 widget = GTK_WIDGET (entry);
2991 if (entry->activates_default)
2993 toplevel = gtk_widget_get_toplevel (widget);
2994 if (GTK_IS_WINDOW (toplevel))
2996 window = GTK_WINDOW (toplevel);
2999 widget != window->default_widget &&
3000 !(widget == window->focus_widget &&
3001 (!window->default_widget || !GTK_WIDGET_SENSITIVE (window->default_widget))))
3002 gtk_window_activate_default (window);
3008 gtk_entry_keymap_direction_changed (GdkKeymap *keymap,
3011 gtk_entry_recompute (entry);
3014 /* IM Context Callbacks
3018 gtk_entry_commit_cb (GtkIMContext *context,
3022 if (entry->editable)
3023 gtk_entry_enter_text (entry, str);
3027 gtk_entry_preedit_changed_cb (GtkIMContext *context,
3030 if (entry->editable)
3032 gchar *preedit_string;
3035 gtk_im_context_get_preedit_string (entry->im_context,
3036 &preedit_string, NULL,
3038 entry->preedit_length = strlen (preedit_string);
3039 cursor_pos = CLAMP (cursor_pos, 0, g_utf8_strlen (preedit_string, -1));
3040 entry->preedit_cursor = cursor_pos;
3041 g_free (preedit_string);
3043 gtk_entry_recompute (entry);
3048 gtk_entry_retrieve_surrounding_cb (GtkIMContext *context,
3051 gtk_im_context_set_surrounding (context,
3054 g_utf8_offset_to_pointer (entry->text, entry->current_pos) - entry->text);
3060 gtk_entry_delete_surrounding_cb (GtkIMContext *slave,
3065 if (entry->editable)
3066 gtk_editable_delete_text (GTK_EDITABLE (entry),
3067 entry->current_pos + offset,
3068 entry->current_pos + offset + n_chars);
3073 /* Internal functions
3076 /* Used for im_commit_cb and inserting Unicode chars */
3078 gtk_entry_enter_text (GtkEntry *entry,
3081 GtkEditable *editable = GTK_EDITABLE (entry);
3083 gboolean old_need_im_reset;
3085 old_need_im_reset = entry->need_im_reset;
3086 entry->need_im_reset = FALSE;
3088 if (gtk_editable_get_selection_bounds (editable, NULL, NULL))
3089 gtk_editable_delete_selection (editable);
3092 if (entry->overwrite_mode)
3093 gtk_entry_delete_from_cursor (entry, GTK_DELETE_CHARS, 1);
3096 tmp_pos = entry->current_pos;
3097 gtk_editable_insert_text (editable, str, strlen (str), &tmp_pos);
3098 gtk_editable_set_position (editable, tmp_pos);
3100 entry->need_im_reset = old_need_im_reset;
3103 /* All changes to entry->current_pos and entry->selection_bound
3104 * should go through this function.
3107 gtk_entry_set_positions (GtkEntry *entry,
3109 gint selection_bound)
3111 gboolean changed = FALSE;
3113 g_object_freeze_notify (G_OBJECT (entry));
3115 if (current_pos != -1 &&
3116 entry->current_pos != current_pos)
3118 entry->current_pos = current_pos;
3121 g_object_notify (G_OBJECT (entry), "cursor-position");
3124 if (selection_bound != -1 &&
3125 entry->selection_bound != selection_bound)
3127 entry->selection_bound = selection_bound;
3130 g_object_notify (G_OBJECT (entry), "selection-bound");
3133 g_object_thaw_notify (G_OBJECT (entry));
3137 gtk_entry_move_adjustments (entry);
3138 gtk_entry_recompute (entry);
3143 gtk_entry_reset_layout (GtkEntry *entry)
3145 if (entry->cached_layout)
3147 g_object_unref (entry->cached_layout);
3148 entry->cached_layout = NULL;
3153 update_im_cursor_location (GtkEntry *entry)
3157 gint strong_xoffset;
3158 gint area_width, area_height;
3160 gtk_entry_get_cursor_locations (entry, CURSOR_STANDARD, &strong_x, NULL)
3162 get_text_area_size (entry, NULL, NULL, &area_width, &area_height);
3164 strong_xoffset = strong_x - entry->scroll_offset;
3165 if (strong_xoffset < 0)
3169 else if (strong_xoffset > area_width)
3171 strong_xoffset = area_width;
3173 area.x = strong_xoffset;
3176 area.height = area_height;
3178 gtk_im_context_set_cursor_location (entry->im_context, &area);
3182 recompute_idle_func (gpointer data)
3186 entry = GTK_ENTRY (data);
3188 entry->recompute_idle = 0;
3190 if (gtk_widget_has_screen (GTK_WIDGET (entry)))
3192 gtk_entry_adjust_scroll (entry);
3193 gtk_entry_queue_draw (entry);
3195 update_im_cursor_location (entry);
3202 gtk_entry_recompute (GtkEntry *entry)
3204 gtk_entry_reset_layout (entry);
3205 gtk_entry_check_cursor_blink (entry);
3207 if (!entry->recompute_idle)
3209 entry->recompute_idle = gdk_threads_add_idle_full (G_PRIORITY_HIGH_IDLE + 15, /* between resize and redraw */
3210 recompute_idle_func, entry, NULL);
3215 append_char (GString *str,
3223 char_len = g_unichar_to_utf8 (ch, buf);
3228 g_string_append_len (str, buf, char_len);
3234 gtk_entry_remove_password_hint (gpointer data)
3236 /* Force the string to be redrawn, but now without a visible character */
3237 gtk_entry_recompute (GTK_ENTRY (data));
3242 static PangoLayout *
3243 gtk_entry_create_layout (GtkEntry *entry,
3244 gboolean include_preedit)
3246 GtkWidget *widget = GTK_WIDGET (entry);
3247 PangoLayout *layout = gtk_widget_create_pango_layout (widget, NULL);
3248 PangoAttrList *tmp_attrs = pango_attr_list_new ();
3250 gchar *preedit_string = NULL;
3251 gint preedit_length = 0;
3252 PangoAttrList *preedit_attrs = NULL;
3254 pango_layout_set_single_paragraph_mode (layout, TRUE);
3256 if (include_preedit)
3258 gtk_im_context_get_preedit_string (entry->im_context,
3259 &preedit_string, &preedit_attrs, NULL);
3260 preedit_length = entry->preedit_length;
3265 GString *tmp_string = g_string_new (NULL);
3267 gint cursor_index = g_utf8_offset_to_pointer (entry->text, entry->current_pos) - entry->text;
3271 g_string_prepend_len (tmp_string, entry->text, entry->n_bytes);
3272 g_string_insert (tmp_string, cursor_index, preedit_string);
3277 gint preedit_len_chars;
3278 gunichar invisible_char;
3280 ch_len = g_utf8_strlen (entry->text, entry->n_bytes);
3281 preedit_len_chars = g_utf8_strlen (preedit_string, -1);
3282 ch_len += preedit_len_chars;
3284 if (entry->invisible_char != 0)
3285 invisible_char = entry->invisible_char;
3287 invisible_char = ' '; /* just pick a char */
3289 append_char (tmp_string, invisible_char, ch_len);
3291 /* Fix cursor index to point to invisible char corresponding
3292 * to the preedit, fix preedit_length to be the length of
3293 * the invisible chars representing the preedit
3296 g_utf8_offset_to_pointer (tmp_string->str, entry->current_pos) -
3300 g_unichar_to_utf8 (invisible_char, NULL);
3303 pango_layout_set_text (layout, tmp_string->str, tmp_string->len);
3305 pango_attr_list_splice (tmp_attrs, preedit_attrs,
3306 cursor_index, preedit_length);
3308 g_string_free (tmp_string, TRUE);
3312 PangoDirection pango_dir;
3315 pango_dir = pango_find_base_dir (entry->text, entry->n_bytes);
3318 pango_dir = PANGO_DIRECTION_NEUTRAL;
3320 if (pango_dir == PANGO_DIRECTION_NEUTRAL)
3322 if (GTK_WIDGET_HAS_FOCUS (widget))
3324 GdkDisplay *display = gtk_widget_get_display (widget);
3325 GdkKeymap *keymap = gdk_keymap_get_for_display (display);
3326 if (gdk_keymap_get_direction (keymap) == PANGO_DIRECTION_RTL)
3327 pango_dir = PANGO_DIRECTION_RTL;
3329 pango_dir = PANGO_DIRECTION_LTR;
3333 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
3334 pango_dir = PANGO_DIRECTION_RTL;
3336 pango_dir = PANGO_DIRECTION_LTR;
3340 pango_context_set_base_dir (gtk_widget_get_pango_context (widget),
3343 pango_layout_set_alignment (layout, pango_dir);
3345 entry->resolved_dir = pango_dir;
3349 pango_layout_set_text (layout, entry->text, entry->n_bytes);
3353 GString *str = g_string_new (NULL);
3354 gunichar invisible_char;
3355 guint password_hint_timeout;
3356 GtkEntryPasswordHint *password_hint;
3358 g_object_get (gtk_widget_get_settings (widget),
3359 "gtk-entry-password-hint-timeout", &password_hint_timeout,
3362 if (entry->invisible_char != 0)
3363 invisible_char = entry->invisible_char;
3365 invisible_char = ' '; /* just pick a char */
3367 password_hint = g_object_get_qdata (G_OBJECT (entry),
3368 quark_password_hint);
3370 if (password_hint && password_hint->password_hint_timeout_id)
3372 g_source_remove (password_hint->password_hint_timeout_id);
3373 password_hint->password_hint_timeout_id = 0;
3376 if (password_hint_timeout == 0 || password_hint == NULL ||
3377 (password_hint && password_hint->password_hint_length == 0))
3379 append_char (str, invisible_char, entry->text_length);
3381 else if (password_hint)
3383 /* Draw hidden characters upto the inserted position,
3384 * then the real thing, pad up to full length
3386 if (password_hint->password_hint_position > 1)
3387 append_char (str, invisible_char,
3388 password_hint->password_hint_position - 1);
3390 g_string_append_len (str, password_hint->password_hint,
3391 password_hint->password_hint_length);
3393 if (password_hint->password_hint_position < entry->text_length)
3394 append_char (str, invisible_char,
3395 entry->text_length -
3396 password_hint->password_hint_position);
3398 /* Now remove this last inputted character, don't need
3401 memset (password_hint->password_hint, 0, PASSWORD_HINT_MAX);
3402 password_hint->password_hint_length = 0;
3404 password_hint->password_hint_timeout_id =
3405 gdk_threads_add_timeout (password_hint_timeout,
3406 (GSourceFunc) gtk_entry_remove_password_hint,
3410 pango_layout_set_text (layout, str->str, str->len);
3411 g_string_free (str, TRUE);
3415 pango_layout_set_attributes (layout, tmp_attrs);
3417 g_free (preedit_string);
3419 pango_attr_list_unref (preedit_attrs);
3421 pango_attr_list_unref (tmp_attrs);
3426 static PangoLayout *
3427 gtk_entry_ensure_layout (GtkEntry *entry,
3428 gboolean include_preedit)
3430 if (entry->preedit_length > 0 &&
3431 !include_preedit != !entry->cache_includes_preedit)
3432 gtk_entry_reset_layout (entry);
3434 if (!entry->cached_layout)
3436 entry->cached_layout = gtk_entry_create_layout (entry, include_preedit);
3437 entry->cache_includes_preedit = include_preedit;
3440 return entry->cached_layout;
3444 get_layout_position (GtkEntry *entry,
3448 PangoLayout *layout;
3449 PangoRectangle logical_rect;
3450 gint area_width, area_height;
3451 GtkBorder inner_border;
3453 PangoLayoutLine *line;
3455 layout = gtk_entry_ensure_layout (entry, TRUE);
3457 get_text_area_size (entry, NULL, NULL, &area_width, &area_height);
3458 _gtk_entry_effective_inner_border (entry, &inner_border);
3460 area_height = PANGO_SCALE * (area_height - inner_border.top - inner_border.bottom);
3462 line = pango_layout_get_lines_readonly (layout)->data;
3463 pango_layout_line_get_extents (line, NULL, &logical_rect);
3465 /* Align primarily for locale's ascent/descent */
3466 y_pos = ((area_height - entry->ascent - entry->descent) / 2 +
3467 entry->ascent + logical_rect.y);
3469 /* Now see if we need to adjust to fit in actual drawn string */
3470 if (logical_rect.height > area_height)
3471 y_pos = (area_height - logical_rect.height) / 2;
3474 else if (y_pos + logical_rect.height > area_height)
3475 y_pos = area_height - logical_rect.height;
3477 y_pos = inner_border.top + y_pos / PANGO_SCALE;
3480 *x = inner_border.left - entry->scroll_offset;
3487 gtk_entry_draw_text (GtkEntry *entry)
3491 if (!entry->visible && entry->invisible_char == 0)
3494 if (GTK_WIDGET_DRAWABLE (entry))
3496 PangoLayout *layout = gtk_entry_ensure_layout (entry, TRUE);
3499 gint start_pos, end_pos;
3501 widget = GTK_WIDGET (entry);
3503 get_layout_position (entry, &x, &y);
3505 cr = gdk_cairo_create (entry->text_area);
3507 cairo_move_to (cr, x, y);
3508 gdk_cairo_set_source_color (cr, &widget->style->text [widget->state]);
3509 pango_cairo_show_layout (cr, layout);
3511 if (gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &start_pos, &end_pos))
3515 PangoRectangle logical_rect;
3516 GdkColor *selection_color, *text_color;
3517 GtkBorder inner_border;
3519 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
3520 gtk_entry_get_pixel_ranges (entry, &ranges, &n_ranges);
3522 if (GTK_WIDGET_HAS_FOCUS (entry))
3524 selection_color = &widget->style->base [GTK_STATE_SELECTED];
3525 text_color = &widget->style->text [GTK_STATE_SELECTED];
3529 selection_color = &widget->style->base [GTK_STATE_ACTIVE];
3530 text_color = &widget->style->text [GTK_STATE_ACTIVE];
3533 _gtk_entry_effective_inner_border (entry, &inner_border);
3535 for (i = 0; i < n_ranges; ++i)
3536 cairo_rectangle (cr,
3537 inner_border.left - entry->scroll_offset + ranges[2 * i],
3540 logical_rect.height);
3544 gdk_cairo_set_source_color (cr, selection_color);
3547 cairo_move_to (cr, x, y);
3548 gdk_cairo_set_source_color (cr, text_color);
3549 pango_cairo_show_layout (cr, layout);
3559 draw_insertion_cursor (GtkEntry *entry,
3560 GdkRectangle *cursor_location,
3561 gboolean is_primary,
3562 PangoDirection direction,
3563 gboolean draw_arrow)
3565 GtkWidget *widget = GTK_WIDGET (entry);
3566 GtkTextDirection text_dir;
3568 if (direction == PANGO_DIRECTION_LTR)
3569 text_dir = GTK_TEXT_DIR_LTR;
3571 text_dir = GTK_TEXT_DIR_RTL;
3573 gtk_draw_insertion_cursor (widget, entry->text_area, NULL,
3575 is_primary, text_dir, draw_arrow);
3579 gtk_entry_draw_cursor (GtkEntry *entry,
3582 GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (entry)));
3583 PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
3585 if (GTK_WIDGET_DRAWABLE (entry))
3587 GtkWidget *widget = GTK_WIDGET (entry);
3588 GdkRectangle cursor_location;
3589 gboolean split_cursor;
3591 GtkBorder inner_border;
3593 gint strong_x, weak_x;
3594 gint text_area_height;
3595 PangoDirection dir1 = PANGO_DIRECTION_NEUTRAL;
3596 PangoDirection dir2 = PANGO_DIRECTION_NEUTRAL;
3600 _gtk_entry_effective_inner_border (entry, &inner_border);
3602 xoffset = inner_border.left - entry->scroll_offset;
3604 gdk_drawable_get_size (entry->text_area, NULL, &text_area_height);
3606 gtk_entry_get_cursor_locations (entry, type, &strong_x, &weak_x);
3608 g_object_get (gtk_widget_get_settings (widget),
3609 "gtk-split-cursor", &split_cursor,
3612 dir1 = entry->resolved_dir;
3618 if (weak_x != strong_x)
3620 dir2 = (entry->resolved_dir == PANGO_DIRECTION_LTR) ? PANGO_DIRECTION_RTL : PANGO_DIRECTION_LTR;
3626 if (keymap_direction == entry->resolved_dir)
3632 cursor_location.x = xoffset + x1;
3633 cursor_location.y = inner_border.top;
3634 cursor_location.width = 0;
3635 cursor_location.height = text_area_height - inner_border.top - inner_border.bottom;
3637 draw_insertion_cursor (entry,
3638 &cursor_location, TRUE, dir1,
3639 dir2 != PANGO_DIRECTION_NEUTRAL);
3641 if (dir2 != PANGO_DIRECTION_NEUTRAL)
3643 cursor_location.x = xoffset + x2;
3644 draw_insertion_cursor (entry,
3645 &cursor_location, FALSE, dir2,
3652 gtk_entry_queue_draw (GtkEntry *entry)
3654 if (GTK_WIDGET_DRAWABLE (entry))
3655 gdk_window_invalidate_rect (entry->text_area, NULL, FALSE);
3659 _gtk_entry_reset_im_context (GtkEntry *entry)
3661 if (entry->need_im_reset)
3663 entry->need_im_reset = 0;
3664 gtk_im_context_reset (entry->im_context);
3669 gtk_entry_find_position (GtkEntry *entry,
3672 PangoLayout *layout;
3673 PangoLayoutLine *line;
3680 layout = gtk_entry_ensure_layout (entry, TRUE);
3681 text = pango_layout_get_text (layout);
3682 cursor_index = g_utf8_offset_to_pointer (text, entry->current_pos) - text;
3684 line = pango_layout_get_lines_readonly (layout)->data;
3685 pango_layout_line_x_to_index (line, x * PANGO_SCALE, &index, &trailing);
3687 if (index >= cursor_index && entry->preedit_length)
3689 if (index >= cursor_index + entry->preedit_length)
3690 index -= entry->preedit_length;
3693 index = cursor_index;
3698 pos = g_utf8_pointer_to_offset (text, text + index);
3705 gtk_entry_get_cursor_locations (GtkEntry *entry,
3710 if (!entry->visible && !entry->invisible_char)
3720 PangoLayout *layout = gtk_entry_ensure_layout (entry, TRUE);
3721 const gchar *text = pango_layout_get_text (layout);
3722 PangoRectangle strong_pos, weak_pos;
3725 if (type == CURSOR_STANDARD)
3727 index = g_utf8_offset_to_pointer (text, entry->current_pos + entry->preedit_cursor) - text;
3729 else /* type == CURSOR_DND */
3731 index = g_utf8_offset_to_pointer (text, entry->dnd_position) - text;
3733 if (entry->dnd_position > entry->current_pos)
3736 index += entry->preedit_length;
3739 gint preedit_len_chars = g_utf8_strlen (text, -1) - entry->text_length;
3740 index += preedit_len_chars * g_unichar_to_utf8 (entry->invisible_char, NULL);
3745 pango_layout_get_cursor_pos (layout, index, &strong_pos, &weak_pos);
3748 *strong_x = strong_pos.x / PANGO_SCALE;
3751 *weak_x = weak_pos.x / PANGO_SCALE;
3756 gtk_entry_adjust_scroll (GtkEntry *entry)
3758 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
3759 gint min_offset, max_offset;
3760 gint text_area_width, text_width;
3761 GtkBorder inner_border;
3762 gint strong_x, weak_x;
3763 gint strong_xoffset, weak_xoffset;
3765 PangoLayout *layout;
3766 PangoLayoutLine *line;
3767 PangoRectangle logical_rect;
3769 if (!GTK_WIDGET_REALIZED (entry))
3772 _gtk_entry_effective_inner_border (entry, &inner_border);
3774 gdk_drawable_get_size (entry->text_area, &text_area_width, NULL);
3775 text_area_width -= inner_border.left + inner_border.right;
3776 if (text_area_width < 0)
3777 text_area_width = 0;
3779 layout = gtk_entry_ensure_layout (entry, TRUE);
3780 line = pango_layout_get_lines_readonly (layout)->data;
3782 pango_layout_line_get_extents (line, NULL, &logical_rect);
3784 /* Display as much text as we can */
3786 if (entry->resolved_dir == PANGO_DIRECTION_LTR)
3787 xalign = priv->xalign;
3789 xalign = 1.0 - priv->xalign;
3791 text_width = PANGO_PIXELS(logical_rect.width);
3793 if (text_width > text_area_width)
3796 max_offset = text_width - text_area_width;
3800 min_offset = (text_width - text_area_width) * xalign;
3801 max_offset = min_offset;
3804 entry->scroll_offset = CLAMP (entry->scroll_offset, min_offset, max_offset);
3806 /* And make sure cursors are on screen. Note that the cursor is
3807 * actually drawn one pixel into the INNER_BORDER space on
3808 * the right, when the scroll is at the utmost right. This
3809 * looks better to to me than confining the cursor inside the
3810 * border entirely, though it means that the cursor gets one
3811 * pixel closer to the edge of the widget on the right than
3812 * on the left. This might need changing if one changed
3813 * INNER_BORDER from 2 to 1, as one would do on a
3814 * small-screen-real-estate display.
3816 * We always make sure that the strong cursor is on screen, and
3817 * put the weak cursor on screen if possible.
3820 gtk_entry_get_cursor_locations (entry, CURSOR_STANDARD, &strong_x, &weak_x);
3822 strong_xoffset = strong_x - entry->scroll_offset;
3824 if (strong_xoffset < 0)
3826 entry->scroll_offset += strong_xoffset;
3829 else if (strong_xoffset > text_area_width)
3831 entry->scroll_offset += strong_xoffset - text_area_width;
3832 strong_xoffset = text_area_width;
3835 weak_xoffset = weak_x - entry->scroll_offset;
3837 if (weak_xoffset < 0 && strong_xoffset - weak_xoffset <= text_area_width)
3839 entry->scroll_offset += weak_xoffset;
3841 else if (weak_xoffset > text_area_width &&
3842 strong_xoffset - (weak_xoffset - text_area_width) >= 0)
3844 entry->scroll_offset += weak_xoffset - text_area_width;
3847 g_object_notify (G_OBJECT (entry), "scroll-offset");
3851 gtk_entry_move_adjustments (GtkEntry *entry)
3853 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
3854 PangoContext *context;
3855 PangoFontMetrics *metrics;
3856 gint x, layout_x, border_x, border_y;
3859 if (!priv->cursor_hadjustment)
3862 /* Cursor position, layout offset, border width, and widget allocation */
3863 gtk_entry_get_cursor_locations (entry, CURSOR_STANDARD, &x, NULL);
3864 get_layout_position (entry, &layout_x, NULL);
3865 _gtk_entry_get_borders (entry, &border_x, &border_y);
3866 x += entry->widget.allocation.x + layout_x + border_x;
3868 /* Approximate width of a char, so user can see what is ahead/behind */
3869 context = gtk_widget_get_pango_context (GTK_WIDGET (entry));
3870 metrics = pango_context_get_metrics (context,
3871 entry->widget.style->font_desc,
3872 pango_context_get_language (context));
3873 char_width = pango_font_metrics_get_approximate_char_width (metrics) / PANGO_SCALE;
3876 gtk_adjustment_clamp_page (priv->cursor_hadjustment,
3877 x - (char_width + 1), /* one char + one pixel before */
3878 x + (char_width + 2)); /* one char + cursor + one pixel after */
3882 gtk_entry_move_visually (GtkEntry *entry,
3887 PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
3890 text = pango_layout_get_text (layout);
3892 index = g_utf8_offset_to_pointer (text, start) - text;
3896 int new_index, new_trailing;
3897 gboolean split_cursor;
3900 g_object_get (gtk_widget_get_settings (GTK_WIDGET (entry)),
3901 "gtk-split-cursor", &split_cursor,
3908 GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (entry)));
3909 PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
3911 strong = keymap_direction == entry->resolved_dir;
3916 pango_layout_move_cursor_visually (layout, strong, index, 0, 1, &new_index, &new_trailing);
3921 pango_layout_move_cursor_visually (layout, strong, index, 0, -1, &new_index, &new_trailing);
3927 else if (new_index != G_MAXINT)
3930 while (new_trailing--)
3931 index = g_utf8_next_char (text + index) - text;
3934 return g_utf8_pointer_to_offset (text, text + index);
3938 gtk_entry_move_logically (GtkEntry *entry,
3942 gint new_pos = start;
3944 /* Prevent any leak of information */
3945 if (!entry->visible)
3947 new_pos = CLAMP (start + count, 0, entry->text_length);
3949 else if (entry->text)
3951 PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
3952 PangoLogAttr *log_attrs;
3955 pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
3957 while (count > 0 && new_pos < entry->text_length)
3961 while (new_pos < entry->text_length && !log_attrs[new_pos].is_cursor_position);
3965 while (count < 0 && new_pos > 0)
3969 while (new_pos > 0 && !log_attrs[new_pos].is_cursor_position);
3981 gtk_entry_move_forward_word (GtkEntry *entry,
3983 gboolean allow_whitespace)
3985 gint new_pos = start;
3987 /* Prevent any leak of information */
3988 if (!entry->visible)
3990 new_pos = entry->text_length;
3992 else if (entry->text && (new_pos < entry->text_length))
3994 PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
3995 PangoLogAttr *log_attrs;
3998 pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
4000 /* Find the next word boundary */
4002 while (new_pos < n_attrs - 1 && !(log_attrs[new_pos].is_word_end ||
4003 (log_attrs[new_pos].is_word_start && allow_whitespace)))
4014 gtk_entry_move_backward_word (GtkEntry *entry,
4016 gboolean allow_whitespace)
4018 gint new_pos = start;
4020 /* Prevent any leak of information */
4021 if (!entry->visible)
4025 else if (entry->text && start > 0)
4027 PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
4028 PangoLogAttr *log_attrs;
4031 pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
4033 new_pos = start - 1;
4035 /* Find the previous word boundary */
4036 while (new_pos > 0 && !(log_attrs[new_pos].is_word_start ||
4037 (log_attrs[new_pos].is_word_end && allow_whitespace)))
4047 gtk_entry_delete_whitespace (GtkEntry *entry)
4049 PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
4050 PangoLogAttr *log_attrs;
4054 pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
4056 start = end = entry->current_pos;
4058 while (start > 0 && log_attrs[start-1].is_white)
4061 while (end < n_attrs && log_attrs[end].is_white)
4067 gtk_editable_delete_text (GTK_EDITABLE (entry), start, end);
4072 gtk_entry_select_word (GtkEntry *entry)
4074 gint start_pos = gtk_entry_move_backward_word (entry, entry->current_pos, TRUE);
4075 gint end_pos = gtk_entry_move_forward_word (entry, entry->current_pos, TRUE);
4077 gtk_editable_select_region (GTK_EDITABLE (entry), start_pos, end_pos);
4081 gtk_entry_select_line (GtkEntry *entry)
4083 gtk_editable_select_region (GTK_EDITABLE (entry), 0, -1);
4087 * Like gtk_editable_get_chars, but handle not-visible entries
4091 gtk_entry_get_public_chars (GtkEntry *entry,
4096 end = entry->text_length;
4099 return gtk_editable_get_chars (GTK_EDITABLE (entry), start, end);
4100 else if (!entry->invisible_char)
4101 return g_strdup ("");
4104 GString *str = g_string_new (NULL);
4105 append_char (str, entry->invisible_char, end - start);
4106 return g_string_free (str, FALSE);
4111 truncate_multiline (const gchar *text)
4116 text[length] && text[length] != '\n' && text[length] != '\r';
4123 paste_received (GtkClipboard *clipboard,
4127 GtkEntry *entry = GTK_ENTRY (data);
4128 GtkEditable *editable = GTK_EDITABLE (entry);
4129 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
4131 if (entry->button == 2)
4133 gint pos, start, end;
4134 pos = priv->insert_pos;
4135 gtk_editable_get_selection_bounds (editable, &start, &end);
4136 if (!((start <= pos && pos <= end) || (end <= pos && pos <= start)))
4137 gtk_editable_select_region (editable, pos, pos);
4142 gint pos, start, end;
4144 gboolean popup_completion;
4145 GtkEntryCompletion *completion;
4147 completion = gtk_entry_get_completion (entry);
4149 if (entry->truncate_multiline)
4150 length = truncate_multiline (text);
4152 /* only complete if the selection is at the end */
4153 popup_completion = (entry->text_length == MAX (entry->current_pos, entry->selection_bound));
4157 if (GTK_WIDGET_MAPPED (completion->priv->popup_window))
4158 _gtk_entry_completion_popdown (completion);
4160 if (!popup_completion && completion->priv->changed_id > 0)
4161 g_signal_handler_block (entry, completion->priv->changed_id);
4164 begin_change (entry);
4165 g_object_freeze_notify (G_OBJECT (entry));
4166 if (gtk_editable_get_selection_bounds (editable, &start, &end))
4167 gtk_editable_delete_text (editable, start, end);
4169 pos = entry->current_pos;
4170 gtk_editable_insert_text (editable, text, length, &pos);
4171 gtk_editable_set_position (editable, pos);
4172 g_object_thaw_notify (G_OBJECT (entry));
4176 !popup_completion && completion->priv->changed_id > 0)
4177 g_signal_handler_unblock (entry, completion->priv->changed_id);
4180 g_object_unref (entry);
4184 gtk_entry_paste (GtkEntry *entry,
4187 g_object_ref (entry);
4188 gtk_clipboard_request_text (gtk_widget_get_clipboard (GTK_WIDGET (entry), selection),
4189 paste_received, entry);
4193 primary_get_cb (GtkClipboard *clipboard,
4194 GtkSelectionData *selection_data,
4198 GtkEntry *entry = GTK_ENTRY (data);
4201 if (gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &start, &end))
4203 gchar *str = gtk_entry_get_public_chars (entry, start, end);
4204 gtk_selection_data_set_text (selection_data, str, -1);
4210 primary_clear_cb (GtkClipboard *clipboard,
4213 GtkEntry *entry = GTK_ENTRY (data);
4215 gtk_editable_select_region (GTK_EDITABLE (entry), entry->current_pos, entry->current_pos);
4219 gtk_entry_update_primary_selection (GtkEntry *entry)
4221 static GtkTargetEntry targets[] = {
4222 { "UTF8_STRING", 0, 0 },
4225 { "COMPOUND_TEXT", 0, 0 },
4226 { "text/plain;charset=utf-8", 0, 0 },
4228 { "text/plain", 0, 0 }
4231 GtkClipboard *clipboard;
4234 if (targets[5].target == NULL)
4236 const gchar *charset;
4238 g_get_charset (&charset);
4239 targets[5].target = g_strdup_printf ("text/plain;charset=%s", charset);
4242 if (!GTK_WIDGET_REALIZED (entry))
4245 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (entry), GDK_SELECTION_PRIMARY);
4247 if (gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &start, &end))
4249 if (!gtk_clipboard_set_with_owner (clipboard, targets, G_N_ELEMENTS (targets),
4250 primary_get_cb, primary_clear_cb, G_OBJECT (entry)))
4251 primary_clear_cb (clipboard, entry);
4255 if (gtk_clipboard_get_owner (clipboard) == G_OBJECT (entry))
4256 gtk_clipboard_clear (clipboard);
4266 * Creates a new entry.
4268 * Return value: a new #GtkEntry.
4271 gtk_entry_new (void)
4273 return g_object_new (GTK_TYPE_ENTRY, NULL);
4277 * gtk_entry_new_with_max_length:
4278 * @max: the maximum length of the entry, or 0 for no maximum.
4279 * (other than the maximum length of entries.) The value passed in will
4280 * be clamped to the range 0-65536.
4282 * Creates a new #GtkEntry widget with the given maximum length.
4284 * Note: the existence of this function is inconsistent
4285 * with the rest of the GTK+ API. The normal setup would
4286 * be to just require the user to make an extra call
4287 * to gtk_entry_set_max_length() instead. It is not
4288 * expected that this function will be removed, but
4289 * it would be better practice not to use it.
4291 * Return value: a new #GtkEntry.
4294 gtk_entry_new_with_max_length (gint max)
4298 max = CLAMP (max, 0, MAX_SIZE);
4300 entry = g_object_new (GTK_TYPE_ENTRY, NULL);
4301 entry->text_max_length = max;
4303 return GTK_WIDGET (entry);
4307 gtk_entry_set_text (GtkEntry *entry,
4311 GtkEntryCompletion *completion;
4313 g_return_if_fail (GTK_IS_ENTRY (entry));
4314 g_return_if_fail (text != NULL);
4316 /* Actually setting the text will affect the cursor and selection;
4317 * if the contents don't actually change, this will look odd to the user.
4319 if (strcmp (entry->text, text) == 0)
4322 completion = gtk_entry_get_completion (entry);
4323 if (completion && completion->priv->changed_id > 0)
4324 g_signal_handler_block (entry, completion->priv->changed_id);
4326 begin_change (entry);
4327 g_object_freeze_notify (G_OBJECT (entry));
4328 gtk_editable_delete_text (GTK_EDITABLE (entry), 0, -1);
4331 gtk_editable_insert_text (GTK_EDITABLE (entry), text, strlen (text), &tmp_pos);
4332 g_object_thaw_notify (G_OBJECT (entry));
4335 if (completion && completion->priv->changed_id > 0)
4336 g_signal_handler_unblock (entry, completion->priv->changed_id);
4340 gtk_entry_append_text (GtkEntry *entry,
4345 g_return_if_fail (GTK_IS_ENTRY (entry));
4346 g_return_if_fail (text != NULL);
4348 tmp_pos = entry->text_length;
4349 gtk_editable_insert_text (GTK_EDITABLE (entry), text, -1, &tmp_pos);
4353 gtk_entry_prepend_text (GtkEntry *entry,
4358 g_return_if_fail (GTK_IS_ENTRY (entry));
4359 g_return_if_fail (text != NULL);
4362 gtk_editable_insert_text (GTK_EDITABLE (entry), text, -1, &tmp_pos);
4366 gtk_entry_set_position (GtkEntry *entry,
4369 g_return_if_fail (GTK_IS_ENTRY (entry));
4371 gtk_editable_set_position (GTK_EDITABLE (entry), position);
4375 gtk_entry_set_visibility (GtkEntry *entry,
4378 g_return_if_fail (GTK_IS_ENTRY (entry));
4380 visible = visible != FALSE;
4382 if (entry->visible != visible)
4384 if (GTK_WIDGET_HAS_FOCUS (entry) && !visible)
4385 gtk_im_context_focus_out (entry->im_context);
4387 g_object_unref (entry->im_context);
4390 entry->im_context = gtk_im_multicontext_new ();
4392 entry->im_context = gtk_im_context_simple_new ();
4394 g_signal_connect (entry->im_context, "commit",
4395 G_CALLBACK (gtk_entry_commit_cb), entry);
4396 g_signal_connect (entry->im_context, "preedit_changed",
4397 G_CALLBACK (gtk_entry_preedit_changed_cb), entry);
4398 g_signal_connect (entry->im_context, "retrieve_surrounding",
4399 G_CALLBACK (gtk_entry_retrieve_surrounding_cb), entry);
4400 g_signal_connect (entry->im_context, "delete_surrounding",
4401 G_CALLBACK (gtk_entry_delete_surrounding_cb), entry);
4403 if (GTK_WIDGET_HAS_FOCUS (entry) && visible)
4404 gtk_im_context_focus_in (entry->im_context);
4406 entry->visible = visible;
4408 g_object_notify (G_OBJECT (entry), "visibility");
4409 gtk_entry_recompute (entry);
4414 * gtk_entry_get_visibility:
4415 * @entry: a #GtkEntry
4417 * Retrieves whether the text in @entry is visible. See
4418 * gtk_entry_set_visibility().
4420 * Return value: %TRUE if the text is currently visible
4423 gtk_entry_get_visibility (GtkEntry *entry)
4425 g_return_val_if_fail (GTK_IS_ENTRY (entry), FALSE);
4427 return entry->visible;
4431 * gtk_entry_set_invisible_char:
4432 * @entry: a #GtkEntry
4433 * @ch: a Unicode character
4435 * Sets the character to use in place of the actual text when
4436 * gtk_entry_set_visibility() has been called to set text visibility
4437 * to %FALSE. i.e. this is the character used in "password mode" to
4438 * show the user how many characters have been typed. The default
4439 * invisible char is an asterisk ('*'). If you set the invisible char
4440 * to 0, then the user will get no feedback at all; there will be
4441 * no text on the screen as they type.
4445 gtk_entry_set_invisible_char (GtkEntry *entry,
4448 g_return_if_fail (GTK_IS_ENTRY (entry));
4450 if (ch == entry->invisible_char)
4453 entry->invisible_char = ch;
4454 g_object_notify (G_OBJECT (entry), "invisible-char");
4455 gtk_entry_recompute (entry);
4459 * gtk_entry_get_invisible_char:
4460 * @entry: a #GtkEntry
4462 * Retrieves the character displayed in place of the real characters
4463 * for entries with visisbility set to false. See gtk_entry_set_invisible_char().
4465 * Return value: the current invisible char, or 0, if the entry does not
4466 * show invisible text at all.
4469 gtk_entry_get_invisible_char (GtkEntry *entry)
4471 g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
4473 return entry->invisible_char;
4477 gtk_entry_set_editable (GtkEntry *entry,
4480 g_return_if_fail (GTK_IS_ENTRY (entry));
4482 gtk_editable_set_editable (GTK_EDITABLE (entry), editable);
4486 * gtk_entry_get_text:
4487 * @entry: a #GtkEntry
4489 * Retrieves the contents of the entry widget.
4490 * See also gtk_editable_get_chars().
4492 * Return value: a pointer to the contents of the widget as a
4493 * string. This string points to internally allocated
4494 * storage in the widget and must not be freed, modified or
4497 G_CONST_RETURN gchar*
4498 gtk_entry_get_text (GtkEntry *entry)
4500 g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
4506 gtk_entry_select_region (GtkEntry *entry,
4510 gtk_editable_select_region (GTK_EDITABLE (entry), start, end);
4514 * gtk_entry_set_max_length:
4515 * @entry: a #GtkEntry.
4516 * @max: the maximum length of the entry, or 0 for no maximum.
4517 * (other than the maximum length of entries.) The value passed in will
4518 * be clamped to the range 0-65536.
4520 * Sets the maximum allowed length of the contents of the widget. If
4521 * the current contents are longer than the given length, then they
4522 * will be truncated to fit.
4525 gtk_entry_set_max_length (GtkEntry *entry,
4528 g_return_if_fail (GTK_IS_ENTRY (entry));
4530 max = CLAMP (max, 0, MAX_SIZE);
4532 if (max > 0 && entry->text_length > max)
4533 gtk_editable_delete_text (GTK_EDITABLE (entry), max, -1);
4535 entry->text_max_length = max;
4536 g_object_notify (G_OBJECT (entry), "max-length");
4540 * gtk_entry_get_max_length:
4541 * @entry: a #GtkEntry
4543 * Retrieves the maximum allowed length of the text in
4544 * @entry. See gtk_entry_set_max_length().
4546 * Return value: the maximum allowed number of characters
4547 * in #GtkEntry, or 0 if there is no maximum.
4550 gtk_entry_get_max_length (GtkEntry *entry)
4552 g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
4554 return entry->text_max_length;
4558 * gtk_entry_set_activates_default:
4559 * @entry: a #GtkEntry
4560 * @setting: %TRUE to activate window's default widget on Enter keypress
4562 * If @setting is %TRUE, pressing Enter in the @entry will activate the default
4563 * widget for the window containing the entry. This usually means that
4564 * the dialog box containing the entry will be closed, since the default
4565 * widget is usually one of the dialog buttons.
4567 * (For experts: if @setting is %TRUE, the entry calls
4568 * gtk_window_activate_default() on the window containing the entry, in
4569 * the default handler for the "activate" signal.)
4573 gtk_entry_set_activates_default (GtkEntry *entry,
4576 g_return_if_fail (GTK_IS_ENTRY (entry));
4577 setting = setting != FALSE;
4579 if (setting != entry->activates_default)
4581 entry->activates_default = setting;
4582 g_object_notify (G_OBJECT (entry), "activates-default");
4587 * gtk_entry_get_activates_default:
4588 * @entry: a #GtkEntry
4590 * Retrieves the value set by gtk_entry_set_activates_default().
4592 * Return value: %TRUE if the entry will activate the default widget
4595 gtk_entry_get_activates_default (GtkEntry *entry)
4597 g_return_val_if_fail (GTK_IS_ENTRY (entry), FALSE);
4599 return entry->activates_default;
4603 * gtk_entry_set_width_chars:
4604 * @entry: a #GtkEntry
4605 * @n_chars: width in chars
4607 * Changes the size request of the entry to be about the right size
4608 * for @n_chars characters. Note that it changes the size
4609 * <emphasis>request</emphasis>, the size can still be affected by
4610 * how you pack the widget into containers. If @n_chars is -1, the
4611 * size reverts to the default entry size.
4615 gtk_entry_set_width_chars (GtkEntry *entry,
4618 g_return_if_fail (GTK_IS_ENTRY (entry));
4620 if (entry->width_chars != n_chars)
4622 entry->width_chars = n_chars;
4623 g_object_notify (G_OBJECT (entry), "width-chars");
4624 gtk_widget_queue_resize (GTK_WIDGET (entry));
4629 * gtk_entry_get_width_chars:
4630 * @entry: a #GtkEntry
4632 * Gets the value set by gtk_entry_set_width_chars().
4634 * Return value: number of chars to request space for, or negative if unset
4637 gtk_entry_get_width_chars (GtkEntry *entry)
4639 g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
4641 return entry->width_chars;
4645 * gtk_entry_set_has_frame:
4646 * @entry: a #GtkEntry
4647 * @setting: new value
4649 * Sets whether the entry has a beveled frame around it.
4652 gtk_entry_set_has_frame (GtkEntry *entry,
4655 g_return_if_fail (GTK_IS_ENTRY (entry));
4657 setting = (setting != FALSE);
4659 if (entry->has_frame == setting)
4662 gtk_widget_queue_resize (GTK_WIDGET (entry));
4663 entry->has_frame = setting;
4664 g_object_notify (G_OBJECT (entry), "has-frame");
4668 * gtk_entry_get_has_frame:
4669 * @entry: a #GtkEntry
4671 * Gets the value set by gtk_entry_set_has_frame().
4673 * Return value: whether the entry has a beveled frame
4676 gtk_entry_get_has_frame (GtkEntry *entry)
4678 g_return_val_if_fail (GTK_IS_ENTRY (entry), FALSE);
4680 return entry->has_frame;
4684 * gtk_entry_set_inner_border:
4685 * @entry: a #GtkEntry
4686 * @border: a #GtkBorder, or %NULL
4688 * Sets %entry's inner-border property to %border, or clears it if %NULL
4689 * is passed. The inner-border is the area around the entry's text, but
4692 * If set, this property overrides the inner-border style property.
4693 * Overriding the style-provided border is useful when you want to do
4694 * in-place editing of some text in a canvas or list widget, where
4695 * pixel-exact positioning of the entry is important.
4700 gtk_entry_set_inner_border (GtkEntry *entry,
4701 const GtkBorder *border)
4703 g_return_if_fail (GTK_IS_ENTRY (entry));
4705 gtk_widget_queue_resize (GTK_WIDGET (entry));
4708 g_object_set_qdata_full (G_OBJECT (entry), quark_inner_border,
4709 gtk_border_copy (border),
4710 (GDestroyNotify) gtk_border_free);
4712 g_object_set_qdata (G_OBJECT (entry), quark_inner_border, NULL);
4714 g_object_notify (G_OBJECT (entry), "inner-border");
4718 * gtk_entry_get_inner_border:
4719 * @entry: a #GtkEntry
4721 * This function returns the entry's inner-border property. See
4722 * gtk_entry_set_inner_border() for more information.
4724 * Return value: the entry's #GtkBorder, or %NULL if none was set.
4728 G_CONST_RETURN GtkBorder *
4729 gtk_entry_get_inner_border (GtkEntry *entry)
4731 g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
4733 return g_object_get_qdata (G_OBJECT (entry), quark_inner_border);
4737 * gtk_entry_get_layout:
4738 * @entry: a #GtkEntry
4740 * Gets the #PangoLayout used to display the entry.
4741 * The layout is useful to e.g. convert text positions to
4742 * pixel positions, in combination with gtk_entry_get_layout_offsets().
4743 * The returned layout is owned by the entry and must not be
4744 * modified or freed by the caller.
4746 * Keep in mind that the layout text may contain a preedit string, so
4747 * gtk_entry_layout_index_to_text_index() and
4748 * gtk_entry_text_index_to_layout_index() are needed to convert byte
4749 * indices in the layout to byte indices in the entry contents.
4751 * Return value: the #PangoLayout for this entry
4754 gtk_entry_get_layout (GtkEntry *entry)
4756 PangoLayout *layout;
4758 g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
4760 layout = gtk_entry_ensure_layout (entry, TRUE);
4767 * gtk_entry_layout_index_to_text_index:
4768 * @entry: a #GtkEntry
4769 * @layout_index: byte index into the entry layout text
4771 * Converts from a position in the entry contents (returned
4772 * by gtk_entry_get_text()) to a position in the
4773 * entry's #PangoLayout (returned by gtk_entry_get_layout(),
4774 * with text retrieved via pango_layout_get_text()).
4776 * Return value: byte index into the entry contents
4779 gtk_entry_layout_index_to_text_index (GtkEntry *entry,
4782 PangoLayout *layout;
4786 g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
4788 layout = gtk_entry_ensure_layout (entry, TRUE);
4789 text = pango_layout_get_text (layout);
4790 cursor_index = g_utf8_offset_to_pointer (text, entry->current_pos) - text;
4792 if (layout_index >= cursor_index && entry->preedit_length)
4794 if (layout_index >= cursor_index + entry->preedit_length)
4795 layout_index -= entry->preedit_length;
4797 layout_index = cursor_index;
4800 return layout_index;
4804 * gtk_entry_text_index_to_layout_index:
4805 * @entry: a #GtkEntry
4806 * @text_index: byte index into the entry contents
4808 * Converts from a position in the entry's #PangoLayout (returned by
4809 * gtk_entry_get_layout()) to a position in the entry contents
4810 * (returned by gtk_entry_get_text()).
4812 * Return value: byte index into the entry layout text
4815 gtk_entry_text_index_to_layout_index (GtkEntry *entry,
4818 PangoLayout *layout;
4821 g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
4823 layout = gtk_entry_ensure_layout (entry, TRUE);
4824 text = pango_layout_get_text (layout);
4825 cursor_index = g_utf8_offset_to_pointer (text, entry->current_pos) - text;
4827 if (text_index > cursor_index)
4828 text_index += entry->preedit_length;
4834 * gtk_entry_get_layout_offsets:
4835 * @entry: a #GtkEntry
4836 * @x: location to store X offset of layout, or %NULL
4837 * @y: location to store Y offset of layout, or %NULL
4840 * Obtains the position of the #PangoLayout used to render text
4841 * in the entry, in widget coordinates. Useful if you want to line
4842 * up the text in an entry with some other text, e.g. when using the
4843 * entry to implement editable cells in a sheet widget.
4845 * Also useful to convert mouse events into coordinates inside the
4846 * #PangoLayout, e.g. to take some action if some part of the entry text
4849 * Note that as the user scrolls around in the entry the offsets will
4850 * change; you'll need to connect to the "notify::scroll-offset"
4851 * signal to track this. Remember when using the #PangoLayout
4852 * functions you need to convert to and from pixels using
4853 * PANGO_PIXELS() or #PANGO_SCALE.
4855 * Keep in mind that the layout text may contain a preedit string, so
4856 * gtk_entry_layout_index_to_text_index() and
4857 * gtk_entry_text_index_to_layout_index() are needed to convert byte
4858 * indices in the layout to byte indices in the entry contents.
4862 gtk_entry_get_layout_offsets (GtkEntry *entry,
4866 gint text_area_x, text_area_y;
4868 g_return_if_fail (GTK_IS_ENTRY (entry));
4870 /* this gets coords relative to text area */
4871 get_layout_position (entry, x, y);
4873 /* convert to widget coords */
4874 get_text_area_size (entry, &text_area_x, &text_area_y, NULL, NULL);
4885 * gtk_entry_set_alignment:
4886 * @entry: a #GtkEntry
4887 * @xalign: The horizontal alignment, from 0 (left) to 1 (right).
4888 * Reversed for RTL layouts
4890 * Sets the alignment for the contents of the entry. This controls
4891 * the horizontal positioning of the contents when the displayed
4892 * text is shorter than the width of the entry.
4897 gtk_entry_set_alignment (GtkEntry *entry, gfloat xalign)
4899 GtkEntryPrivate *priv;
4901 g_return_if_fail (GTK_IS_ENTRY (entry));
4903 priv = GTK_ENTRY_GET_PRIVATE (entry);
4907 else if (xalign > 1.0)
4910 if (xalign != priv->xalign)
4912 priv->xalign = xalign;
4914 gtk_entry_recompute (entry);
4916 g_object_notify (G_OBJECT (entry), "xalign");
4921 * gtk_entry_get_alignment:
4922 * @entry: a #GtkEntry
4924 * Gets the value set by gtk_entry_set_alignment().
4926 * Return value: the alignment
4931 gtk_entry_get_alignment (GtkEntry *entry)
4933 GtkEntryPrivate *priv;
4935 g_return_val_if_fail (GTK_IS_ENTRY (entry), 0.0);
4937 priv = GTK_ENTRY_GET_PRIVATE (entry);
4939 return priv->xalign;
4942 /* Quick hack of a popup menu
4945 activate_cb (GtkWidget *menuitem,
4948 const gchar *signal = g_object_get_data (G_OBJECT (menuitem), "gtk-signal");
4949 g_signal_emit_by_name (entry, signal);
4954 gtk_entry_mnemonic_activate (GtkWidget *widget,
4955 gboolean group_cycling)
4957 gtk_widget_grab_focus (widget);
4962 append_action_signal (GtkEntry *entry,
4964 const gchar *stock_id,
4965 const gchar *signal,
4968 GtkWidget *menuitem = gtk_image_menu_item_new_from_stock (stock_id, NULL);
4970 g_object_set_data (G_OBJECT (menuitem), I_("gtk-signal"), (char *)signal);
4971 g_signal_connect (menuitem, "activate",
4972 G_CALLBACK (activate_cb), entry);
4974 gtk_widget_set_sensitive (menuitem, sensitive);
4976 gtk_widget_show (menuitem);
4977 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
4981 popup_menu_detach (GtkWidget *attach_widget,
4984 GTK_ENTRY (attach_widget)->popup_menu = NULL;
4988 popup_position_func (GtkMenu *menu,
4994 GtkEntry *entry = GTK_ENTRY (user_data);
4995 GtkWidget *widget = GTK_WIDGET (entry);
4997 GtkRequisition menu_req;
4998 GdkRectangle monitor;
4999 GtkBorder inner_border;
5000 gint monitor_num, strong_x, height;
5002 g_return_if_fail (GTK_WIDGET_REALIZED (entry));
5004 gdk_window_get_origin (entry->text_area, x, y);
5006 screen = gtk_widget_get_screen (widget);
5007 monitor_num = gdk_screen_get_monitor_at_window (screen, entry->text_area);
5008 if (monitor_num < 0)
5010 gtk_menu_set_monitor (menu, monitor_num);
5012 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
5013 gtk_widget_size_request (entry->popup_menu, &menu_req);
5014 gdk_drawable_get_size (entry->text_area, NULL, &height);
5015 gtk_entry_get_cursor_locations (entry, CURSOR_STANDARD, &strong_x, NULL);
5016 _gtk_entry_effective_inner_border (entry, &inner_border);
5018 *x += inner_border.left + strong_x - entry->scroll_offset;
5019 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
5020 *x -= menu_req.width;
5022 if ((*y + height + menu_req.height) <= monitor.y + monitor.height)
5024 else if ((*y - menu_req.height) >= monitor.y)
5025 *y -= menu_req.height;
5026 else if (monitor.y + monitor.height - (*y + height) > *y)
5029 *y -= menu_req.height;
5035 unichar_chosen_func (const char *text,
5038 GtkEntry *entry = GTK_ENTRY (data);
5040 if (entry->editable)
5041 gtk_entry_enter_text (entry, text);
5052 popup_targets_received (GtkClipboard *clipboard,
5053 GtkSelectionData *data,
5056 PopupInfo *info = user_data;
5057 GtkEntry *entry = info->entry;
5059 if (GTK_WIDGET_REALIZED (entry))
5061 gboolean clipboard_contains_text;
5062 GtkWidget *menuitem;
5064 gboolean show_input_method_menu;
5065 gboolean show_unicode_menu;
5067 clipboard_contains_text = gtk_selection_data_targets_include_text (data);
5068 if (entry->popup_menu)
5069 gtk_widget_destroy (entry->popup_menu);
5071 entry->popup_menu = gtk_menu_new ();
5073 gtk_menu_attach_to_widget (GTK_MENU (entry->popup_menu),
5077 append_action_signal (entry, entry->popup_menu, GTK_STOCK_CUT, "cut_clipboard",
5078 entry->editable && entry->current_pos != entry->selection_bound);
5079 append_action_signal (entry, entry->popup_menu, GTK_STOCK_COPY, "copy_clipboard",
5080 entry->current_pos != entry->selection_bound);
5081 append_action_signal (entry, entry->popup_menu, GTK_STOCK_PASTE, "paste_clipboard",
5082 entry->editable && clipboard_contains_text);
5084 menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_DELETE, NULL);
5085 gtk_widget_set_sensitive (menuitem, entry->editable && entry->current_pos != entry->selection_bound);
5086 g_signal_connect_swapped (menuitem, "activate",
5087 G_CALLBACK (gtk_entry_delete_cb), entry);
5088 gtk_widget_show (menuitem);
5089 gtk_menu_shell_append (GTK_MENU_SHELL (entry->popup_menu), menuitem);
5091 menuitem = gtk_separator_menu_item_new ();
5092 gtk_widget_show (menuitem);
5093 gtk_menu_shell_append (GTK_MENU_SHELL (entry->popup_menu), menuitem);
5095 menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_SELECT_ALL, NULL);
5096 g_signal_connect_swapped (menuitem, "activate",
5097 G_CALLBACK (gtk_entry_select_all), entry);
5098 gtk_widget_show (menuitem);
5099 gtk_menu_shell_append (GTK_MENU_SHELL (entry->popup_menu), menuitem);
5101 g_object_get (gtk_widget_get_settings (GTK_WIDGET (entry)),
5102 "gtk-show-input-method-menu", &show_input_method_menu,
5103 "gtk-show-unicode-menu", &show_unicode_menu,
5106 if (!entry->visible)
5107 show_input_method_menu = FALSE;
5109 if (show_input_method_menu || show_unicode_menu)
5111 menuitem = gtk_separator_menu_item_new ();
5112 gtk_widget_show (menuitem);
5113 gtk_menu_shell_append (GTK_MENU_SHELL (entry->popup_menu), menuitem);
5116 if (show_input_method_menu)
5118 menuitem = gtk_menu_item_new_with_mnemonic (_("Input _Methods"));
5119 gtk_widget_set_sensitive (menuitem, entry->editable);
5120 gtk_widget_show (menuitem);
5121 submenu = gtk_menu_new ();
5122 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
5124 gtk_menu_shell_append (GTK_MENU_SHELL (entry->popup_menu), menuitem);
5126 gtk_im_multicontext_append_menuitems (GTK_IM_MULTICONTEXT (entry->im_context),
5127 GTK_MENU_SHELL (submenu));
5130 if (show_unicode_menu)
5132 menuitem = gtk_menu_item_new_with_mnemonic (_("_Insert Unicode Control Character"));
5133 gtk_widget_set_sensitive (menuitem, entry->editable);
5134 gtk_widget_show (menuitem);
5136 submenu = gtk_menu_new ();
5137 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
5138 gtk_menu_shell_append (GTK_MENU_SHELL (entry->popup_menu), menuitem);
5140 _gtk_text_util_append_special_char_menuitems (GTK_MENU_SHELL (submenu),
5141 unichar_chosen_func,
5145 g_signal_emit (entry,
5146 signals[POPULATE_POPUP],
5152 gtk_menu_popup (GTK_MENU (entry->popup_menu), NULL, NULL,
5154 info->button, info->time);
5157 gtk_menu_popup (GTK_MENU (entry->popup_menu), NULL, NULL,
5158 popup_position_func, entry,
5159 info->button, info->time);
5160 gtk_menu_shell_select_first (GTK_MENU_SHELL (entry->popup_menu), FALSE);
5164 g_object_unref (entry);
5169 gtk_entry_do_popup (GtkEntry *entry,
5170 GdkEventButton *event)
5172 PopupInfo *info = g_new (PopupInfo, 1);
5174 /* In order to know what entries we should make sensitive, we
5175 * ask for the current targets of the clipboard, and when
5176 * we get them, then we actually pop up the menu.
5178 info->entry = g_object_ref (entry);
5182 info->button = event->button;
5183 info->time = event->time;
5188 info->time = gtk_get_current_event_time ();
5191 gtk_clipboard_request_contents (gtk_widget_get_clipboard (GTK_WIDGET (entry), GDK_SELECTION_CLIPBOARD),
5192 gdk_atom_intern_static_string ("TARGETS"),
5193 popup_targets_received,
5198 gtk_entry_popup_menu (GtkWidget *widget)
5200 gtk_entry_do_popup (GTK_ENTRY (widget), NULL);
5205 gtk_entry_drag_leave (GtkWidget *widget,
5206 GdkDragContext *context,
5209 GtkEntry *entry = GTK_ENTRY (widget);
5211 entry->dnd_position = -1;
5212 gtk_widget_queue_draw (widget);
5216 gtk_entry_drag_drop (GtkWidget *widget,
5217 GdkDragContext *context,
5222 GtkEntry *entry = GTK_ENTRY (widget);
5223 GdkAtom target = GDK_NONE;
5225 if (entry->editable)
5226 target = gtk_drag_dest_find_target (widget, context, NULL);
5228 if (target != GDK_NONE)
5229 gtk_drag_get_data (widget, context, target, time);
5231 gtk_drag_finish (context, FALSE, FALSE, time);
5237 gtk_entry_drag_motion (GtkWidget *widget,
5238 GdkDragContext *context,
5243 GtkEntry *entry = GTK_ENTRY (widget);
5244 GtkWidget *source_widget;
5245 GdkDragAction suggested_action;
5246 gint new_position, old_position;
5249 x -= widget->style->xthickness;
5250 y -= widget->style->ythickness;
5252 old_position = entry->dnd_position;
5253 new_position = gtk_entry_find_position (entry, x + entry->scroll_offset);
5255 if (entry->editable &&
5256 gtk_drag_dest_find_target (widget, context, NULL) != GDK_NONE)
5258 source_widget = gtk_drag_get_source_widget (context);
5259 suggested_action = context->suggested_action;
5261 if (!gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &sel1, &sel2) ||
5262 new_position < sel1 || new_position > sel2)
5264 if (source_widget == widget)
5266 /* Default to MOVE, unless the user has
5267 * pressed ctrl or alt to affect available actions
5269 if ((context->actions & GDK_ACTION_MOVE) != 0)
5270 suggested_action = GDK_ACTION_MOVE;
5273 entry->dnd_position = new_position;
5277 if (source_widget == widget)
5278 suggested_action = 0; /* Can't drop in selection where drag started */
5280 entry->dnd_position = -1;
5285 /* Entry not editable, or no text */
5286 suggested_action = 0;
5287 entry->dnd_position = -1;
5290 gdk_drag_status (context, suggested_action, time);
5292 if (entry->dnd_position != old_position)
5293 gtk_widget_queue_draw (widget);
5299 gtk_entry_drag_data_received (GtkWidget *widget,
5300 GdkDragContext *context,
5303 GtkSelectionData *selection_data,
5307 GtkEntry *entry = GTK_ENTRY (widget);
5308 GtkEditable *editable = GTK_EDITABLE (widget);
5311 str = (gchar *) gtk_selection_data_get_text (selection_data);
5313 x -= widget->style->xthickness;
5314 y -= widget->style->ythickness;
5316 if (str && entry->editable)
5322 if (entry->truncate_multiline)
5323 length = truncate_multiline (str);
5325 new_position = gtk_entry_find_position (entry, x + entry->scroll_offset);
5327 if (!gtk_editable_get_selection_bounds (editable, &sel1, &sel2) ||
5328 new_position < sel1 || new_position > sel2)
5330 gtk_editable_insert_text (editable, str, length, &new_position);
5334 /* Replacing selection */
5335 begin_change (entry);
5336 g_object_freeze_notify (G_OBJECT (entry));
5337 gtk_editable_delete_text (editable, sel1, sel2);
5338 gtk_editable_insert_text (editable, str, length, &sel1);
5339 g_object_thaw_notify (G_OBJECT (entry));
5343 gtk_drag_finish (context, TRUE, context->action == GDK_ACTION_MOVE, time);
5347 /* Drag and drop didn't happen! */
5348 gtk_drag_finish (context, FALSE, FALSE, time);
5355 gtk_entry_drag_data_get (GtkWidget *widget,
5356 GdkDragContext *context,
5357 GtkSelectionData *selection_data,
5361 gint sel_start, sel_end;
5363 GtkEditable *editable = GTK_EDITABLE (widget);
5365 if (gtk_editable_get_selection_bounds (editable, &sel_start, &sel_end))
5367 gchar *str = gtk_entry_get_public_chars (GTK_ENTRY (widget), sel_start, sel_end);
5369 gtk_selection_data_set_text (selection_data, str, -1);
5377 gtk_entry_drag_data_delete (GtkWidget *widget,
5378 GdkDragContext *context)
5380 gint sel_start, sel_end;
5382 GtkEditable *editable = GTK_EDITABLE (widget);
5384 if (GTK_ENTRY (widget)->editable &&
5385 gtk_editable_get_selection_bounds (editable, &sel_start, &sel_end))
5386 gtk_editable_delete_text (editable, sel_start, sel_end);
5389 /* We display the cursor when
5391 * - the selection is empty, AND
5392 * - the widget has focus
5395 #define CURSOR_ON_MULTIPLIER 2
5396 #define CURSOR_OFF_MULTIPLIER 1
5397 #define CURSOR_PEND_MULTIPLIER 3
5398 #define CURSOR_DIVIDER 3
5401 cursor_blinks (GtkEntry *entry)
5403 if (GTK_WIDGET_HAS_FOCUS (entry) &&
5405 entry->selection_bound == entry->current_pos)
5407 GtkSettings *settings;
5410 settings = gtk_widget_get_settings (GTK_WIDGET (entry));
5411 g_object_get (settings, "gtk-cursor-blink", &blink, NULL);
5420 get_cursor_time (GtkEntry *entry)
5422 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (entry));
5425 g_object_get (settings, "gtk-cursor-blink-time", &time, NULL);
5431 get_cursor_blink_timeout (GtkEntry *entry)
5433 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (entry));
5436 g_object_get (settings, "gtk-cursor-blink-timeout", &timeout, NULL);
5442 show_cursor (GtkEntry *entry)
5444 if (!entry->cursor_visible)
5446 entry->cursor_visible = TRUE;
5448 if (GTK_WIDGET_HAS_FOCUS (entry) && entry->selection_bound == entry->current_pos)
5449 gtk_widget_queue_draw (GTK_WIDGET (entry));
5454 hide_cursor (GtkEntry *entry)
5456 if (entry->cursor_visible)
5458 entry->cursor_visible = FALSE;
5460 if (GTK_WIDGET_HAS_FOCUS (entry) && entry->selection_bound == entry->current_pos)
5461 gtk_widget_queue_draw (GTK_WIDGET (entry));
5469 blink_cb (gpointer data)
5472 GtkEntryPrivate *priv;
5475 entry = GTK_ENTRY (data);
5476 priv = GTK_ENTRY_GET_PRIVATE (entry);
5478 if (!GTK_WIDGET_HAS_FOCUS (entry))
5480 g_warning ("GtkEntry - did not receive focus-out-event. If you\n"
5481 "connect a handler to this signal, it must return\n"
5482 "FALSE so the entry gets the event as well");
5484 gtk_entry_check_cursor_blink (entry);
5489 g_assert (entry->selection_bound == entry->current_pos);
5491 blink_timeout = get_cursor_blink_timeout (entry);
5492 if (priv->blink_time > 1000 * blink_timeout &&
5493 blink_timeout < G_MAXINT/1000)
5495 /* we've blinked enough without the user doing anything, stop blinking */
5496 show_cursor (entry);
5497 entry->blink_timeout = 0;
5499 else if (entry->cursor_visible)
5501 hide_cursor (entry);
5502 entry->blink_timeout = gdk_threads_add_timeout (get_cursor_time (entry) * CURSOR_OFF_MULTIPLIER / CURSOR_DIVIDER,
5508 show_cursor (entry);
5509 priv->blink_time += get_cursor_time (entry);
5510 entry->blink_timeout = gdk_threads_add_timeout (get_cursor_time (entry) * CURSOR_ON_MULTIPLIER / CURSOR_DIVIDER,
5515 /* Remove ourselves */
5520 gtk_entry_check_cursor_blink (GtkEntry *entry)
5522 GtkEntryPrivate *priv;
5524 priv = GTK_ENTRY_GET_PRIVATE (entry);
5526 if (cursor_blinks (entry))
5528 if (!entry->blink_timeout)
5530 show_cursor (entry);
5531 entry->blink_timeout = gdk_threads_add_timeout (get_cursor_time (entry) * CURSOR_ON_MULTIPLIER / CURSOR_DIVIDER,
5538 if (entry->blink_timeout)
5540 g_source_remove (entry->blink_timeout);
5541 entry->blink_timeout = 0;
5544 entry->cursor_visible = TRUE;
5550 gtk_entry_pend_cursor_blink (GtkEntry *entry)
5552 if (cursor_blinks (entry))
5554 if (entry->blink_timeout != 0)
5555 g_source_remove (entry->blink_timeout);
5557 entry->blink_timeout = gdk_threads_add_timeout (get_cursor_time (entry) * CURSOR_PEND_MULTIPLIER / CURSOR_DIVIDER,
5560 show_cursor (entry);
5565 gtk_entry_reset_blink_time (GtkEntry *entry)
5567 GtkEntryPrivate *priv;
5569 priv = GTK_ENTRY_GET_PRIVATE (entry);
5571 priv->blink_time = 0;
5577 gtk_entry_completion_timeout (gpointer data)
5579 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (data);
5581 completion->priv->completion_timeout = 0;
5583 if (completion->priv->filter_model &&
5584 g_utf8_strlen (gtk_entry_get_text (GTK_ENTRY (completion->priv->entry)), -1)
5585 >= completion->priv->minimum_key_length)
5589 GtkTreeSelection *s;
5590 gboolean popup_single;
5592 gtk_entry_completion_complete (completion);
5593 matches = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->filter_model), NULL);
5595 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view)));
5597 s = gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->action_view));
5599 gtk_tree_selection_unselect_all (s);
5601 actions = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->actions), NULL);
5603 g_object_get (completion, "popup-single-match", &popup_single, NULL);
5604 if ((matches > (popup_single ? 0: 1)) || actions > 0)
5606 if (GTK_WIDGET_VISIBLE (completion->priv->popup_window))
5607 _gtk_entry_completion_resize_popup (completion);
5609 _gtk_entry_completion_popup (completion);
5612 _gtk_entry_completion_popdown (completion);
5614 else if (GTK_WIDGET_VISIBLE (completion->priv->popup_window))
5615 _gtk_entry_completion_popdown (completion);
5620 static inline gboolean
5621 keyval_is_cursor_move (guint keyval)
5623 if (keyval == GDK_Up || keyval == GDK_KP_Up)
5626 if (keyval == GDK_Down || keyval == GDK_KP_Down)
5629 if (keyval == GDK_Page_Up)
5632 if (keyval == GDK_Page_Down)
5639 gtk_entry_completion_key_press (GtkWidget *widget,
5643 gint matches, actions = 0;
5644 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
5646 if (!GTK_WIDGET_MAPPED (completion->priv->popup_window))
5649 matches = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->filter_model), NULL);
5651 if (completion->priv->actions)
5652 actions = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->actions), NULL);
5654 if (keyval_is_cursor_move (event->keyval))
5656 GtkTreePath *path = NULL;
5658 if (event->keyval == GDK_Up || event->keyval == GDK_KP_Up)
5660 if (completion->priv->current_selected < 0)
5661 completion->priv->current_selected = matches + actions - 1;
5663 completion->priv->current_selected--;
5665 else if (event->keyval == GDK_Down || event->keyval == GDK_KP_Down)
5667 if (completion->priv->current_selected < matches + actions - 1)
5668 completion->priv->current_selected++;
5670 completion->priv->current_selected = -1;
5672 else if (event->keyval == GDK_Page_Up)
5674 if (completion->priv->current_selected < 0)
5675 completion->priv->current_selected = matches + actions - 1;
5676 else if (completion->priv->current_selected == 0)
5677 completion->priv->current_selected = -1;
5678 else if (completion->priv->current_selected < matches)
5680 completion->priv->current_selected -= 14;
5681 if (completion->priv->current_selected < 0)
5682 completion->priv->current_selected = 0;
5686 completion->priv->current_selected -= 14;
5687 if (completion->priv->current_selected < matches - 1)
5688 completion->priv->current_selected = matches - 1;
5691 else if (event->keyval == GDK_Page_Down)
5693 if (completion->priv->current_selected < 0)
5694 completion->priv->current_selected = 0;
5695 else if (completion->priv->current_selected < matches - 1)
5697 completion->priv->current_selected += 14;
5698 if (completion->priv->current_selected > matches - 1)
5699 completion->priv->current_selected = matches - 1;
5701 else if (completion->priv->current_selected == matches + actions - 1)
5703 completion->priv->current_selected = -1;
5707 completion->priv->current_selected += 14;
5708 if (completion->priv->current_selected > matches + actions - 1)
5709 completion->priv->current_selected = matches + actions - 1;
5713 if (completion->priv->current_selected < 0)
5715 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view)));
5716 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->action_view)));
5718 else if (completion->priv->current_selected < matches)
5720 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->action_view)));
5722 path = gtk_tree_path_new_from_indices (completion->priv->current_selected, -1);
5723 gtk_tree_view_set_cursor (GTK_TREE_VIEW (completion->priv->tree_view),
5726 if (completion->priv->inline_selection)
5730 GtkTreeModel *model = NULL;
5731 GtkTreeSelection *sel;
5734 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view));
5735 if (!gtk_tree_selection_get_selected (sel, &model, &iter))
5738 if (completion->priv->completion_prefix == NULL)
5740 completion->priv->completion_prefix = g_strdup (gtk_entry_get_text (GTK_ENTRY (completion->priv->entry)));
5743 g_signal_emit_by_name (completion, "cursor_on_match", model,
5747 else if (completion->priv->current_selected - matches >= 0)
5749 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view)));
5751 path = gtk_tree_path_new_from_indices (completion->priv->current_selected - matches, -1);
5752 gtk_tree_view_set_cursor (GTK_TREE_VIEW (completion->priv->action_view),
5756 gtk_tree_path_free (path);
5760 else if (event->keyval == GDK_Escape ||
5761 event->keyval == GDK_Left ||
5762 event->keyval == GDK_KP_Left ||
5763 event->keyval == GDK_Right ||
5764 event->keyval == GDK_KP_Right)
5766 _gtk_entry_reset_im_context (GTK_ENTRY (widget));
5767 _gtk_entry_completion_popdown (completion);
5769 if (completion->priv->inline_selection)
5771 /* Escape rejects the tentative completion */
5772 if (event->keyval == GDK_Escape)
5774 gtk_entry_set_text (GTK_ENTRY (completion->priv->entry), completion->priv->completion_prefix);
5777 /* Move the cursor to the end for Right/Esc, to the
5778 beginning for Left */
5779 if (event->keyval == GDK_Right ||
5780 event->keyval == GDK_KP_Right ||
5781 event->keyval == GDK_Escape)
5782 gtk_editable_set_position (GTK_EDITABLE (widget), -1);
5784 gtk_editable_set_position (GTK_EDITABLE (widget), 0);
5786 g_free (completion->priv->completion_prefix);
5787 completion->priv->completion_prefix = NULL;
5792 else if (event->keyval == GDK_Tab ||
5793 event->keyval == GDK_KP_Tab ||
5794 event->keyval == GDK_ISO_Left_Tab)
5796 GtkDirectionType dir = event->keyval == GDK_ISO_Left_Tab ?
5797 GTK_DIR_TAB_BACKWARD : GTK_DIR_TAB_FORWARD;
5799 _gtk_entry_reset_im_context (GTK_ENTRY (widget));
5800 _gtk_entry_completion_popdown (completion);
5802 if (completion->priv->completion_prefix)
5804 g_free (completion->priv->completion_prefix);
5805 completion->priv->completion_prefix = NULL;
5808 gtk_widget_child_focus (gtk_widget_get_toplevel (widget), dir);
5812 else if (event->keyval == GDK_ISO_Enter ||
5813 event->keyval == GDK_KP_Enter ||
5814 event->keyval == GDK_Return)
5816 _gtk_entry_reset_im_context (GTK_ENTRY (widget));
5817 _gtk_entry_completion_popdown (completion);
5819 if (completion->priv->current_selected < matches)
5822 GtkTreeModel *model = NULL;
5823 GtkTreeSelection *sel;
5826 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view));
5827 if (!gtk_tree_selection_get_selected (sel, &model, &iter))
5830 g_signal_handler_block (widget, completion->priv->changed_id);
5831 g_signal_emit_by_name (completion, "match_selected",
5832 model, &iter, &entry_set);
5833 g_signal_handler_unblock (widget, completion->priv->changed_id);
5839 gtk_tree_model_get (model, &iter,
5840 completion->priv->text_column, &str,
5843 gtk_entry_set_text (GTK_ENTRY (widget), str);
5845 /* move the cursor to the end */
5846 gtk_editable_set_position (GTK_EDITABLE (widget), -1);
5853 else if (completion->priv->current_selected - matches >= 0)
5857 _gtk_entry_reset_im_context (GTK_ENTRY (widget));
5859 path = gtk_tree_path_new_from_indices (completion->priv->current_selected - matches, -1);
5861 g_signal_emit_by_name (completion, "action_activated",
5862 gtk_tree_path_get_indices (path)[0]);
5863 gtk_tree_path_free (path);
5873 gtk_entry_completion_changed (GtkWidget *entry,
5876 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
5878 /* (re)install completion timeout */
5879 if (completion->priv->completion_timeout)
5880 g_source_remove (completion->priv->completion_timeout);
5882 if (!gtk_entry_get_text (GTK_ENTRY (entry)))
5885 /* no need to normalize for this test */
5886 if (completion->priv->minimum_key_length > 0 &&
5887 strcmp ("", gtk_entry_get_text (GTK_ENTRY (entry))) == 0)
5889 if (GTK_WIDGET_VISIBLE (completion->priv->popup_window))
5890 _gtk_entry_completion_popdown (completion);
5894 completion->priv->completion_timeout =
5895 gdk_threads_add_timeout (COMPLETION_TIMEOUT,
5896 gtk_entry_completion_timeout,
5901 check_completion_callback (GtkEntryCompletion *completion)
5903 completion->priv->check_completion_idle = NULL;
5905 gtk_entry_completion_complete (completion);
5906 gtk_entry_completion_insert_prefix (completion);
5912 clear_completion_callback (GtkEntry *entry,
5915 if (pspec->name == I_("cursor-position") ||
5916 pspec->name == I_("selection-bound"))
5918 GtkEntryCompletion *completion = gtk_entry_get_completion (entry);
5920 completion->priv->has_completion = FALSE;
5925 accept_completion_callback (GtkEntry *entry)
5927 GtkEntryCompletion *completion = gtk_entry_get_completion (entry);
5929 if (completion->priv->has_completion)
5930 gtk_editable_set_position (GTK_EDITABLE (entry),
5931 entry->text_length);
5937 completion_insert_text_callback (GtkEntry *entry,
5941 GtkEntryCompletion *completion)
5943 /* idle to update the selection based on the file list */
5944 if (completion->priv->check_completion_idle == NULL)
5946 completion->priv->check_completion_idle = g_idle_source_new ();
5947 g_source_set_priority (completion->priv->check_completion_idle, G_PRIORITY_HIGH);
5948 g_source_set_closure (completion->priv->check_completion_idle,
5949 g_cclosure_new_object (G_CALLBACK (check_completion_callback),
5950 G_OBJECT (completion)));
5951 g_source_attach (completion->priv->check_completion_idle, NULL);
5956 completion_changed (GtkEntryCompletion *completion,
5960 GtkEntry *entry = GTK_ENTRY (data);
5962 if (pspec->name == I_("popup-completion") ||
5963 pspec->name == I_("inline-completion"))
5965 disconnect_completion_signals (entry, completion);
5966 connect_completion_signals (entry, completion);
5971 disconnect_completion_signals (GtkEntry *entry,
5972 GtkEntryCompletion *completion)
5974 g_signal_handlers_disconnect_by_func (completion,
5975 G_CALLBACK (completion_changed), entry);
5976 if (completion->priv->changed_id > 0 &&
5977 g_signal_handler_is_connected (entry, completion->priv->changed_id))
5979 g_signal_handler_disconnect (entry, completion->priv->changed_id);
5980 completion->priv->changed_id = 0;
5982 g_signal_handlers_disconnect_by_func (entry,
5983 G_CALLBACK (gtk_entry_completion_key_press), completion);
5984 if (completion->priv->insert_text_id > 0 &&
5985 g_signal_handler_is_connected (entry, completion->priv->insert_text_id))
5987 g_signal_handler_disconnect (entry, completion->priv->insert_text_id);
5988 completion->priv->insert_text_id = 0;
5990 g_signal_handlers_disconnect_by_func (entry,
5991 G_CALLBACK (completion_insert_text_callback), completion);
5992 g_signal_handlers_disconnect_by_func (entry,
5993 G_CALLBACK (clear_completion_callback), completion);
5994 g_signal_handlers_disconnect_by_func (entry,
5995 G_CALLBACK (accept_completion_callback), completion);
5999 connect_completion_signals (GtkEntry *entry,
6000 GtkEntryCompletion *completion)
6002 if (completion->priv->popup_completion)
6004 completion->priv->changed_id =
6005 g_signal_connect (entry, "changed",
6006 G_CALLBACK (gtk_entry_completion_changed), completion);
6007 g_signal_connect (entry, "key_press_event",
6008 G_CALLBACK (gtk_entry_completion_key_press), completion);
6011 if (completion->priv->inline_completion)
6013 completion->priv->insert_text_id =
6014 g_signal_connect (entry, "insert_text",
6015 G_CALLBACK (completion_insert_text_callback), completion);
6016 g_signal_connect (entry, "notify",
6017 G_CALLBACK (clear_completion_callback), completion);
6018 g_signal_connect (entry, "activate",
6019 G_CALLBACK (accept_completion_callback), completion);
6020 g_signal_connect (entry, "focus_out_event",
6021 G_CALLBACK (accept_completion_callback), completion);
6024 g_signal_connect (completion, "notify",
6025 G_CALLBACK (completion_changed), entry);
6029 * gtk_entry_set_completion:
6030 * @entry: A #GtkEntry.
6031 * @completion: The #GtkEntryCompletion or %NULL.
6033 * Sets @completion to be the auxiliary completion object to use with @entry.
6034 * All further configuration of the completion mechanism is done on
6035 * @completion using the #GtkEntryCompletion API. Completion is disabled if
6036 * @completion is set to %NULL.
6041 gtk_entry_set_completion (GtkEntry *entry,
6042 GtkEntryCompletion *completion)
6044 GtkEntryCompletion *old;
6046 g_return_if_fail (GTK_IS_ENTRY (entry));
6047 g_return_if_fail (!completion || GTK_IS_ENTRY_COMPLETION (completion));
6049 old = gtk_entry_get_completion (entry);
6051 if (old == completion)
6056 if (old->priv->completion_timeout)
6058 g_source_remove (old->priv->completion_timeout);
6059 old->priv->completion_timeout = 0;
6062 if (GTK_WIDGET_MAPPED (old->priv->popup_window))
6063 _gtk_entry_completion_popdown (old);
6065 disconnect_completion_signals (entry, old);
6066 old->priv->entry = NULL;
6068 g_object_unref (old);
6073 g_object_set_data (G_OBJECT (entry), I_(GTK_ENTRY_COMPLETION_KEY), NULL);
6077 /* hook into the entry */
6078 g_object_ref (completion);
6080 connect_completion_signals (entry, completion);
6081 completion->priv->entry = GTK_WIDGET (entry);
6082 g_object_set_data (G_OBJECT (entry), I_(GTK_ENTRY_COMPLETION_KEY), completion);
6086 * gtk_entry_get_completion:
6087 * @entry: A #GtkEntry.
6089 * Returns the auxiliary completion object currently in use by @entry.
6091 * Return value: The auxiliary completion object currently in use by @entry.
6095 GtkEntryCompletion *
6096 gtk_entry_get_completion (GtkEntry *entry)
6098 GtkEntryCompletion *completion;
6100 g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
6102 completion = GTK_ENTRY_COMPLETION (g_object_get_data (G_OBJECT (entry),
6103 GTK_ENTRY_COMPLETION_KEY));
6109 * gtk_entry_set_cursor_hadjustment:
6110 * @entry: a #GtkEntry
6111 * @adjustment: an adjustment which should be adjusted when the cursor is moved,
6114 * Hooks up an adjustment to the cursor position in an entry, so that when
6115 * the cursor is moved, the adjustment is scrolled to show that position.
6116 * See gtk_scrolled_window_get_hadjustment() for a typical way of obtaining
6119 * The adjustment has to be in pixel units and in the same coordinate system
6125 gtk_entry_set_cursor_hadjustment (GtkEntry *entry,
6126 GtkAdjustment *adjustment)
6128 GtkEntryPrivate *priv;
6130 g_return_if_fail (GTK_IS_ENTRY (entry));
6132 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
6134 priv = GTK_ENTRY_GET_PRIVATE (entry);
6135 if (priv->cursor_hadjustment)
6136 g_object_unref (priv->cursor_hadjustment);
6138 g_object_ref (adjustment);
6139 priv->cursor_hadjustment = adjustment;
6143 * gtk_entry_get_cursor_hadjustment:
6144 * @entry: a #GtkEntry
6146 * Retrieves the horizontal cursor adjustment for the entry.
6147 * See gtk_entry_set_cursor_hadjustment().
6149 * Return value: the horizontal cursor adjustment, or %NULL
6150 * if none has been set.
6155 gtk_entry_get_cursor_hadjustment (GtkEntry *entry)
6157 GtkEntryPrivate *priv;
6159 g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
6161 priv = GTK_ENTRY_GET_PRIVATE (entry);
6163 return priv->cursor_hadjustment;
6166 #define __GTK_ENTRY_C__
6167 #include "gtkaliasdef.c"