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
4 * Copyright (C) 2004-2006 Christian Hammond
5 * Copyright (C) 2008 Cody Russell
6 * Copyright (C) 2008 Red Hat, Inc.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
25 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
26 * file for a list of people on the GTK+ Team. See the ChangeLog
27 * files for a list of changes. These files are distributed with
28 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
36 #include "gdk/gdkkeysyms.h"
37 #include "gtkalignment.h"
38 #include "gtkbindings.h"
39 #include "gtkcelleditable.h"
40 #include "gtkclipboard.h"
43 #include "gtkimagemenuitem.h"
44 #include "gtkimcontextsimple.h"
45 #include "gtkimmulticontext.h"
49 #include "gtkmarshalers.h"
51 #include "gtkmenuitem.h"
52 #include "gtkseparatormenuitem.h"
53 #include "gtkselection.h"
54 #include "gtksettings.h"
55 #include "gtkspinbutton.h"
57 #include "gtktextutil.h"
58 #include "gtkwindow.h"
59 #include "gtktreeview.h"
60 #include "gtktreeselection.h"
61 #include "gtkprivate.h"
62 #include "gtkentryprivate.h"
63 #include "gtkcelllayout.h"
64 #include "gtktooltip.h"
65 #include "gtkiconfactory.h"
66 #include "gtkicontheme.h"
69 #define GTK_ENTRY_COMPLETION_KEY "gtk-entry-completion-key"
71 #define MIN_ENTRY_WIDTH 150
72 #define DRAW_TIMEOUT 20
73 #define COMPLETION_TIMEOUT 300
74 #define PASSWORD_HINT_MAX 8
76 /* Initial size of buffer, in bytes */
79 /* Maximum size of text buffer, in bytes */
80 #define MAX_SIZE G_MAXUSHORT
84 #define IS_VALID_ICON_POSITION(pos) \
85 ((pos) == GTK_ENTRY_ICON_PRIMARY || \
86 (pos) == GTK_ENTRY_ICON_SECONDARY)
88 static const GtkBorder default_inner_border = { 2, 2, 2, 2 };
89 static GQuark quark_inner_border = 0;
90 static GQuark quark_password_hint = 0;
91 static GQuark quark_cursor_hadjustment = 0;
92 static GQuark quark_capslock_feedback = 0;
94 typedef struct _GtkEntryPrivate GtkEntryPrivate;
96 #define GTK_ENTRY_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_ENTRY, GtkEntryPrivate))
102 guint insensitive : 1;
103 guint nonactivatable : 1;
108 GtkImageType storage_type;
114 GtkTargetList *target_list;
115 GdkDragAction actions;
118 struct _GtkEntryPrivate
122 guint blink_time; /* time in msec the cursor has blinked since last user event */
123 guint interior_focus : 1;
124 guint real_changed : 1;
125 guint invisible_char_set : 1;
126 guint caps_lock_warning : 1;
127 guint change_count : 8;
128 guint progress_pulse_mode : 1;
129 guint progress_pulse_way_back : 1;
132 GtkShadowType shadow_type;
134 gdouble progress_fraction;
135 gdouble progress_pulse_fraction;
136 gdouble progress_pulse_current;
138 EntryIconInfo *icons[MAX_ICONS];
144 typedef struct _GtkEntryPasswordHint GtkEntryPasswordHint;
146 struct _GtkEntryPasswordHint
148 gchar password_hint[PASSWORD_HINT_MAX];
149 guint password_hint_timeout_id;
150 gint password_hint_length;
151 gint password_hint_position;
154 typedef struct _GtkEntryCapslockFeedback GtkEntryCapslockFeedback;
156 struct _GtkEntryCapslockFeedback
181 PROP_CURSOR_POSITION,
182 PROP_SELECTION_BOUND,
189 PROP_ACTIVATES_DEFAULT,
194 PROP_TRUNCATE_MULTILINE,
198 PROP_INVISIBLE_CHAR_SET,
199 PROP_CAPS_LOCK_WARNING,
200 PROP_PROGRESS_FRACTION,
201 PROP_PROGRESS_PULSE_STEP,
203 PROP_PIXBUF_SECONDARY,
205 PROP_STOCK_SECONDARY,
206 PROP_ICON_NAME_PRIMARY,
207 PROP_ICON_NAME_SECONDARY,
209 PROP_GICON_SECONDARY,
210 PROP_STORAGE_TYPE_PRIMARY,
211 PROP_STORAGE_TYPE_SECONDARY,
212 PROP_ACTIVATABLE_PRIMARY,
213 PROP_ACTIVATABLE_SECONDARY,
214 PROP_SENSITIVE_PRIMARY,
215 PROP_SENSITIVE_SECONDARY
218 static guint signals[LAST_SIGNAL] = { 0 };
225 /* GObject, GtkObject methods
227 static void gtk_entry_editable_init (GtkEditableClass *iface);
228 static void gtk_entry_cell_editable_init (GtkCellEditableIface *iface);
229 static void gtk_entry_set_property (GObject *object,
233 static void gtk_entry_get_property (GObject *object,
237 static void gtk_entry_finalize (GObject *object);
238 static void gtk_entry_destroy (GtkObject *object);
239 static void gtk_entry_dispose (GObject *object);
243 static void gtk_entry_realize (GtkWidget *widget);
244 static void gtk_entry_unrealize (GtkWidget *widget);
245 static void gtk_entry_map (GtkWidget *widget);
246 static void gtk_entry_unmap (GtkWidget *widget);
247 static void gtk_entry_size_request (GtkWidget *widget,
248 GtkRequisition *requisition);
249 static void gtk_entry_size_allocate (GtkWidget *widget,
250 GtkAllocation *allocation);
251 static void gtk_entry_draw_frame (GtkWidget *widget,
253 static void gtk_entry_draw_progress (GtkWidget *widget,
254 GdkEventExpose *event);
255 static gint gtk_entry_expose (GtkWidget *widget,
256 GdkEventExpose *event);
257 static gint gtk_entry_button_press (GtkWidget *widget,
258 GdkEventButton *event);
259 static gint gtk_entry_button_release (GtkWidget *widget,
260 GdkEventButton *event);
261 static gint gtk_entry_enter_notify (GtkWidget *widget,
262 GdkEventCrossing *event);
263 static gint gtk_entry_leave_notify (GtkWidget *widget,
264 GdkEventCrossing *event);
265 static gint gtk_entry_motion_notify (GtkWidget *widget,
266 GdkEventMotion *event);
267 static gint gtk_entry_key_press (GtkWidget *widget,
269 static gint gtk_entry_key_release (GtkWidget *widget,
271 static gint gtk_entry_focus_in (GtkWidget *widget,
272 GdkEventFocus *event);
273 static gint gtk_entry_focus_out (GtkWidget *widget,
274 GdkEventFocus *event);
275 static void gtk_entry_grab_focus (GtkWidget *widget);
276 static void gtk_entry_style_set (GtkWidget *widget,
277 GtkStyle *previous_style);
278 static gboolean gtk_entry_query_tooltip (GtkWidget *widget,
281 gboolean keyboard_tip,
282 GtkTooltip *tooltip);
283 static void gtk_entry_direction_changed (GtkWidget *widget,
284 GtkTextDirection previous_dir);
285 static void gtk_entry_state_changed (GtkWidget *widget,
286 GtkStateType previous_state);
287 static void gtk_entry_screen_changed (GtkWidget *widget,
288 GdkScreen *old_screen);
290 static gboolean gtk_entry_drag_drop (GtkWidget *widget,
291 GdkDragContext *context,
295 static gboolean gtk_entry_drag_motion (GtkWidget *widget,
296 GdkDragContext *context,
300 static void gtk_entry_drag_leave (GtkWidget *widget,
301 GdkDragContext *context,
303 static void gtk_entry_drag_data_received (GtkWidget *widget,
304 GdkDragContext *context,
307 GtkSelectionData *selection_data,
310 static void gtk_entry_drag_data_get (GtkWidget *widget,
311 GdkDragContext *context,
312 GtkSelectionData *selection_data,
315 static void gtk_entry_drag_data_delete (GtkWidget *widget,
316 GdkDragContext *context);
317 static void gtk_entry_drag_begin (GtkWidget *widget,
318 GdkDragContext *context);
319 static void gtk_entry_drag_end (GtkWidget *widget,
320 GdkDragContext *context);
323 /* GtkEditable method implementations
325 static void gtk_entry_insert_text (GtkEditable *editable,
326 const gchar *new_text,
327 gint new_text_length,
329 static void gtk_entry_delete_text (GtkEditable *editable,
332 static gchar * gtk_entry_get_chars (GtkEditable *editable,
335 static void gtk_entry_real_set_position (GtkEditable *editable,
337 static gint gtk_entry_get_position (GtkEditable *editable);
338 static void gtk_entry_set_selection_bounds (GtkEditable *editable,
341 static gboolean gtk_entry_get_selection_bounds (GtkEditable *editable,
345 /* GtkCellEditable method implementations
347 static void gtk_entry_start_editing (GtkCellEditable *cell_editable,
350 /* Default signal handlers
352 static void gtk_entry_real_insert_text (GtkEditable *editable,
353 const gchar *new_text,
354 gint new_text_length,
356 static void gtk_entry_real_delete_text (GtkEditable *editable,
359 static void gtk_entry_move_cursor (GtkEntry *entry,
360 GtkMovementStep step,
362 gboolean extend_selection);
363 static void gtk_entry_insert_at_cursor (GtkEntry *entry,
365 static void gtk_entry_delete_from_cursor (GtkEntry *entry,
368 static void gtk_entry_backspace (GtkEntry *entry);
369 static void gtk_entry_cut_clipboard (GtkEntry *entry);
370 static void gtk_entry_copy_clipboard (GtkEntry *entry);
371 static void gtk_entry_paste_clipboard (GtkEntry *entry);
372 static void gtk_entry_toggle_overwrite (GtkEntry *entry);
373 static void gtk_entry_select_all (GtkEntry *entry);
374 static void gtk_entry_real_activate (GtkEntry *entry);
375 static gboolean gtk_entry_popup_menu (GtkWidget *widget);
377 static void keymap_direction_changed (GdkKeymap *keymap,
379 static void keymap_state_changed (GdkKeymap *keymap,
381 static void remove_capslock_feedback (GtkEntry *entry);
383 /* IM Context Callbacks
385 static void gtk_entry_commit_cb (GtkIMContext *context,
388 static void gtk_entry_preedit_changed_cb (GtkIMContext *context,
390 static gboolean gtk_entry_retrieve_surrounding_cb (GtkIMContext *context,
392 static gboolean gtk_entry_delete_surrounding_cb (GtkIMContext *context,
399 static void gtk_entry_enter_text (GtkEntry *entry,
401 static void gtk_entry_set_positions (GtkEntry *entry,
403 gint selection_bound);
404 static void gtk_entry_draw_text (GtkEntry *entry);
405 static void gtk_entry_draw_cursor (GtkEntry *entry,
407 static PangoLayout *gtk_entry_ensure_layout (GtkEntry *entry,
408 gboolean include_preedit);
409 static void gtk_entry_reset_layout (GtkEntry *entry);
410 static void gtk_entry_queue_draw (GtkEntry *entry);
411 static void gtk_entry_recompute (GtkEntry *entry);
412 static gint gtk_entry_find_position (GtkEntry *entry,
414 static void gtk_entry_get_cursor_locations (GtkEntry *entry,
418 static void gtk_entry_adjust_scroll (GtkEntry *entry);
419 static gint gtk_entry_move_visually (GtkEntry *editable,
422 static gint gtk_entry_move_logically (GtkEntry *entry,
425 static gint gtk_entry_move_forward_word (GtkEntry *entry,
427 gboolean allow_whitespace);
428 static gint gtk_entry_move_backward_word (GtkEntry *entry,
430 gboolean allow_whitespace);
431 static void gtk_entry_delete_whitespace (GtkEntry *entry);
432 static void gtk_entry_select_word (GtkEntry *entry);
433 static void gtk_entry_select_line (GtkEntry *entry);
434 static char * gtk_entry_get_public_chars (GtkEntry *entry,
437 static void gtk_entry_paste (GtkEntry *entry,
439 static void gtk_entry_update_primary_selection (GtkEntry *entry);
440 static void gtk_entry_do_popup (GtkEntry *entry,
441 GdkEventButton *event);
442 static gboolean gtk_entry_mnemonic_activate (GtkWidget *widget,
443 gboolean group_cycling);
444 static void gtk_entry_state_changed (GtkWidget *widget,
445 GtkStateType previous_state);
446 static void gtk_entry_check_cursor_blink (GtkEntry *entry);
447 static void gtk_entry_pend_cursor_blink (GtkEntry *entry);
448 static void gtk_entry_reset_blink_time (GtkEntry *entry);
449 static void get_text_area_size (GtkEntry *entry,
450 GtkAllocation *alloc);
451 static void gtk_entry_get_text_area_size (GtkEntry *entry,
456 static void get_widget_window_size (GtkEntry *entry,
461 static void gtk_entry_move_adjustments (GtkEntry *entry);
462 static void gtk_entry_ensure_pixbuf (GtkEntry *entry,
463 GtkEntryIconPosition icon_pos);
467 static gint gtk_entry_completion_timeout (gpointer data);
468 static gboolean gtk_entry_completion_key_press (GtkWidget *widget,
471 static void gtk_entry_completion_changed (GtkWidget *entry,
473 static gboolean check_completion_callback (GtkEntryCompletion *completion);
474 static void clear_completion_callback (GtkEntry *entry,
476 static gboolean accept_completion_callback (GtkEntry *entry);
477 static void completion_insert_text_callback (GtkEntry *entry,
481 GtkEntryCompletion *completion);
482 static void completion_changed (GtkEntryCompletion *completion,
485 static void disconnect_completion_signals (GtkEntry *entry,
486 GtkEntryCompletion *completion);
487 static void connect_completion_signals (GtkEntry *entry,
488 GtkEntryCompletion *completion);
490 static void begin_change (GtkEntry *entry);
491 static void end_change (GtkEntry *entry);
492 static void emit_changed (GtkEntry *entry);
495 G_DEFINE_TYPE_WITH_CODE (GtkEntry, gtk_entry, GTK_TYPE_WIDGET,
496 G_IMPLEMENT_INTERFACE (GTK_TYPE_EDITABLE,
497 gtk_entry_editable_init)
498 G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_EDITABLE,
499 gtk_entry_cell_editable_init))
502 add_move_binding (GtkBindingSet *binding_set,
505 GtkMovementStep step,
508 g_return_if_fail ((modmask & GDK_SHIFT_MASK) == 0);
510 gtk_binding_entry_add_signal (binding_set, keyval, modmask,
514 G_TYPE_BOOLEAN, FALSE);
516 /* Selection-extending version */
517 gtk_binding_entry_add_signal (binding_set, keyval, modmask | GDK_SHIFT_MASK,
521 G_TYPE_BOOLEAN, TRUE);
525 gtk_entry_class_init (GtkEntryClass *class)
527 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
528 GtkWidgetClass *widget_class;
529 GtkObjectClass *gtk_object_class;
530 GtkBindingSet *binding_set;
532 widget_class = (GtkWidgetClass*) class;
533 gtk_object_class = (GtkObjectClass *)class;
535 gobject_class->dispose = gtk_entry_dispose;
536 gobject_class->finalize = gtk_entry_finalize;
537 gobject_class->set_property = gtk_entry_set_property;
538 gobject_class->get_property = gtk_entry_get_property;
540 widget_class->map = gtk_entry_map;
541 widget_class->unmap = gtk_entry_unmap;
542 widget_class->realize = gtk_entry_realize;
543 widget_class->unrealize = gtk_entry_unrealize;
544 widget_class->size_request = gtk_entry_size_request;
545 widget_class->size_allocate = gtk_entry_size_allocate;
546 widget_class->expose_event = gtk_entry_expose;
547 widget_class->enter_notify_event = gtk_entry_enter_notify;
548 widget_class->leave_notify_event = gtk_entry_leave_notify;
549 widget_class->button_press_event = gtk_entry_button_press;
550 widget_class->button_release_event = gtk_entry_button_release;
551 widget_class->motion_notify_event = gtk_entry_motion_notify;
552 widget_class->key_press_event = gtk_entry_key_press;
553 widget_class->key_release_event = gtk_entry_key_release;
554 widget_class->focus_in_event = gtk_entry_focus_in;
555 widget_class->focus_out_event = gtk_entry_focus_out;
556 widget_class->grab_focus = gtk_entry_grab_focus;
557 widget_class->style_set = gtk_entry_style_set;
558 widget_class->query_tooltip = gtk_entry_query_tooltip;
559 widget_class->drag_begin = gtk_entry_drag_begin;
560 widget_class->drag_end = gtk_entry_drag_end;
561 widget_class->direction_changed = gtk_entry_direction_changed;
562 widget_class->state_changed = gtk_entry_state_changed;
563 widget_class->screen_changed = gtk_entry_screen_changed;
564 widget_class->mnemonic_activate = gtk_entry_mnemonic_activate;
566 widget_class->drag_drop = gtk_entry_drag_drop;
567 widget_class->drag_motion = gtk_entry_drag_motion;
568 widget_class->drag_leave = gtk_entry_drag_leave;
569 widget_class->drag_data_received = gtk_entry_drag_data_received;
570 widget_class->drag_data_get = gtk_entry_drag_data_get;
571 widget_class->drag_data_delete = gtk_entry_drag_data_delete;
573 widget_class->popup_menu = gtk_entry_popup_menu;
575 gtk_object_class->destroy = gtk_entry_destroy;
577 class->move_cursor = gtk_entry_move_cursor;
578 class->insert_at_cursor = gtk_entry_insert_at_cursor;
579 class->delete_from_cursor = gtk_entry_delete_from_cursor;
580 class->backspace = gtk_entry_backspace;
581 class->cut_clipboard = gtk_entry_cut_clipboard;
582 class->copy_clipboard = gtk_entry_copy_clipboard;
583 class->paste_clipboard = gtk_entry_paste_clipboard;
584 class->toggle_overwrite = gtk_entry_toggle_overwrite;
585 class->activate = gtk_entry_real_activate;
586 class->get_text_area_size = gtk_entry_get_text_area_size;
588 quark_inner_border = g_quark_from_static_string ("gtk-entry-inner-border");
589 quark_password_hint = g_quark_from_static_string ("gtk-entry-password-hint");
590 quark_cursor_hadjustment = g_quark_from_static_string ("gtk-hadjustment");
591 quark_capslock_feedback = g_quark_from_static_string ("gtk-entry-capslock-feedback");
593 g_object_class_install_property (gobject_class,
594 PROP_CURSOR_POSITION,
595 g_param_spec_int ("cursor-position",
596 P_("Cursor Position"),
597 P_("The current position of the insertion cursor in chars"),
601 GTK_PARAM_READABLE));
603 g_object_class_install_property (gobject_class,
604 PROP_SELECTION_BOUND,
605 g_param_spec_int ("selection-bound",
606 P_("Selection Bound"),
607 P_("The position of the opposite end of the selection from the cursor in chars"),
611 GTK_PARAM_READABLE));
613 g_object_class_install_property (gobject_class,
615 g_param_spec_boolean ("editable",
617 P_("Whether the entry contents can be edited"),
619 GTK_PARAM_READWRITE));
621 g_object_class_install_property (gobject_class,
623 g_param_spec_int ("max-length",
624 P_("Maximum length"),
625 P_("Maximum number of characters for this entry. Zero if no maximum"),
629 GTK_PARAM_READWRITE));
630 g_object_class_install_property (gobject_class,
632 g_param_spec_boolean ("visibility",
634 P_("FALSE displays the \"invisible char\" instead of the actual text (password mode)"),
636 GTK_PARAM_READWRITE));
638 g_object_class_install_property (gobject_class,
640 g_param_spec_boolean ("has-frame",
642 P_("FALSE removes outside bevel from entry"),
644 GTK_PARAM_READWRITE));
646 g_object_class_install_property (gobject_class,
648 g_param_spec_boxed ("inner-border",
650 P_("Border between text and frame. Overrides the inner-border style property"),
652 GTK_PARAM_READWRITE));
654 g_object_class_install_property (gobject_class,
656 g_param_spec_unichar ("invisible-char",
657 P_("Invisible character"),
658 P_("The character to use when masking entry contents (in \"password mode\")"),
660 GTK_PARAM_READWRITE));
662 g_object_class_install_property (gobject_class,
663 PROP_ACTIVATES_DEFAULT,
664 g_param_spec_boolean ("activates-default",
665 P_("Activates default"),
666 P_("Whether to activate the default widget (such as the default button in a dialog) when Enter is pressed"),
668 GTK_PARAM_READWRITE));
669 g_object_class_install_property (gobject_class,
671 g_param_spec_int ("width-chars",
672 P_("Width in chars"),
673 P_("Number of characters to leave space for in the entry"),
677 GTK_PARAM_READWRITE));
679 g_object_class_install_property (gobject_class,
681 g_param_spec_int ("scroll-offset",
683 P_("Number of pixels of the entry scrolled off the screen to the left"),
687 GTK_PARAM_READABLE));
689 g_object_class_install_property (gobject_class,
691 g_param_spec_string ("text",
693 P_("The contents of the entry"),
695 GTK_PARAM_READWRITE));
700 * The horizontal alignment, from 0 (left) to 1 (right).
701 * Reversed for RTL layouts.
705 g_object_class_install_property (gobject_class,
707 g_param_spec_float ("xalign",
709 P_("The horizontal alignment, from 0 (left) to 1 (right). Reversed for RTL layouts."),
713 GTK_PARAM_READWRITE));
716 * GtkEntry:truncate-multiline:
718 * When %TRUE, pasted multi-line text is truncated to the first line.
722 g_object_class_install_property (gobject_class,
723 PROP_TRUNCATE_MULTILINE,
724 g_param_spec_boolean ("truncate-multiline",
725 P_("Truncate multiline"),
726 P_("Whether to truncate multiline pastes to one line."),
728 GTK_PARAM_READWRITE));
731 * GtkEntry:shadow-type:
733 * Which kind of shadow to draw around the entry when
734 * #GtkEntry:has-frame is set to %TRUE.
738 g_object_class_install_property (gobject_class,
740 g_param_spec_enum ("shadow-type",
742 P_("Which kind of shadow to draw around the entry when has-frame is set"),
743 GTK_TYPE_SHADOW_TYPE,
745 GTK_PARAM_READWRITE));
748 * GtkEntry:overwrite-mode:
750 * If text is overwritten when typing in the #GtkEntry.
754 g_object_class_install_property (gobject_class,
756 g_param_spec_boolean ("overwrite-mode",
757 P_("Overwrite mode"),
758 P_("Whether new text overwrites existing text"),
760 GTK_PARAM_READWRITE));
763 * GtkEntry:text-length:
765 * The length of the text in the #GtkEntry.
769 g_object_class_install_property (gobject_class,
771 g_param_spec_uint ("text-length",
773 P_("Length of the text currently in the entry"),
777 GTK_PARAM_READABLE));
779 * GtkEntry:invisible-char-set:
781 * Whether the invisible char has been set for the #GtkEntry.
785 g_object_class_install_property (gobject_class,
786 PROP_INVISIBLE_CHAR_SET,
787 g_param_spec_boolean ("invisible-char-set",
788 P_("Invisible char set"),
789 P_("Whether the invisible char has been set"),
791 GTK_PARAM_READWRITE));
794 * GtkEntry:caps-lock-warning
796 * Whether password entries will show a warning when Caps Lock is on
797 * or an input method is active.
801 g_object_class_install_property (gobject_class,
802 PROP_CAPS_LOCK_WARNING,
803 g_param_spec_boolean ("caps-lock-warning",
804 P_("Caps Lock warning"),
805 P_("Whether password entries will show a warning when Caps Lock is on or an input method is active"),
807 GTK_PARAM_READWRITE));
810 * GtkEntry:progress-fraction:
812 * The current fraction of the task that's been completed.
816 g_object_class_install_property (gobject_class,
817 PROP_PROGRESS_FRACTION,
818 g_param_spec_double ("progress-fraction",
819 P_("Progress Fraction"),
820 P_("The current fraction of the task that's been completed"),
824 GTK_PARAM_READWRITE));
827 * GtkEntry:progress-pulse-step:
829 * The fraction of total entry width to move the progress
830 * bouncing block for each call to gtk_entry_progress_pulse().
834 g_object_class_install_property (gobject_class,
835 PROP_PROGRESS_PULSE_STEP,
836 g_param_spec_double ("progress-pulse-step",
837 P_("Progress Pulse Step"),
838 P_("The fraction of total entry width to move the progress bouncing block for each call to gtk_entry_progress_pulse()"),
842 GTK_PARAM_READWRITE));
845 * GtkEntry:pixbuf-primary:
847 * A pixbuf to use as the primary icon for the entry.
851 g_object_class_install_property (gobject_class,
853 g_param_spec_object ("pixbuf-primary",
854 P_("Primary pixbuf"),
855 P_("Primary pixbuf for the entry"),
857 GTK_PARAM_READWRITE));
860 * GtkEntry:pixbuf-secondary:
862 * An pixbuf to use as the secondary icon for the entry.
866 g_object_class_install_property (gobject_class,
867 PROP_PIXBUF_SECONDARY,
868 g_param_spec_object ("pixbuf-secondary",
869 P_("Secondary pixbuf"),
870 P_("Secondary pixbuf for the entry"),
872 GTK_PARAM_READWRITE));
875 * GtkEntry:stock-primary:
877 * The stock id to use for the primary icon for the entry.
881 g_object_class_install_property (gobject_class,
883 g_param_spec_string ("stock-primary",
884 P_("Primary stock ID"),
885 P_("Stock ID for primary icon"),
887 GTK_PARAM_WRITABLE));
890 * GtkEntry:stock-secondary:
892 * The stock id to use for the secondary icon for the entry.
896 g_object_class_install_property (gobject_class,
897 PROP_STOCK_SECONDARY,
898 g_param_spec_string ("stock-secondary",
899 P_("Secondary stock ID"),
900 P_("Stock ID for secondary icon"),
902 GTK_PARAM_WRITABLE));
905 * GtkEntry:icon-name-primary:
907 * The icon name to use for the primary icon for the entry.
911 g_object_class_install_property (gobject_class,
912 PROP_ICON_NAME_PRIMARY,
913 g_param_spec_string ("icon-name-primary",
914 P_("Primary icon name"),
915 P_("Icon name for primary icon"),
917 GTK_PARAM_WRITABLE));
920 * GtkEntry:icon-name-secondary:
922 * The icon name to use for the secondary icon for the entry.
926 g_object_class_install_property (gobject_class,
927 PROP_ICON_NAME_SECONDARY,
928 g_param_spec_string ("icon-name-secondary",
929 P_("Secondary icon name"),
930 P_("Icon name for secondary icon"),
932 GTK_PARAM_WRITABLE));
935 * GtkEntry:gicon-primary:
937 * The #GIcon to use for the primary icon for the entry.
941 g_object_class_install_property (gobject_class,
943 g_param_spec_object ("gicon-primary",
945 P_("GIcon for primary icon"),
947 GTK_PARAM_READWRITE));
950 * GtkEntry:gicon-secondary:
952 * The #GIcon to use for the secondary icon for the entry.
956 g_object_class_install_property (gobject_class,
957 PROP_GICON_SECONDARY,
958 g_param_spec_object ("gicon-secondary",
959 P_("Secondary GIcon"),
960 P_("GIcon for secondary icon"),
962 GTK_PARAM_READWRITE));
965 * GtkEntry:storage-type-primary:
967 * The representation which is used for the primary icon of the entry.
971 g_object_class_install_property (gobject_class,
972 PROP_STORAGE_TYPE_PRIMARY,
973 g_param_spec_enum ("storage-type-primary",
974 P_("Primary storage type"),
975 P_("The representation being used for primary icon"),
978 GTK_PARAM_READABLE));
981 * GtkEntry:storage-type-secondary:
983 * The representation which is used for the secondary icon of the entry.
987 g_object_class_install_property (gobject_class,
988 PROP_STORAGE_TYPE_SECONDARY,
989 g_param_spec_enum ("storage-type-secondary",
990 P_("Secondary storage type"),
991 P_("The representation being used for secondary icon"),
994 GTK_PARAM_READABLE));
997 * GtkEntry:activatable-primary:
999 * Whether the primary icon is activatable.
1001 * GTK+ emits the #GtkEntry::icon-pressed and
1002 * #GtkEntry::icon-released signals only on
1003 * sensitive, activatable icons.
1007 g_object_class_install_property (gobject_class,
1008 PROP_ACTIVATABLE_PRIMARY,
1009 g_param_spec_boolean ("activatable-primary",
1010 P_("Primary icon activatable"),
1011 P_("Whether the primary icon is activatable"),
1013 GTK_PARAM_READWRITE));
1016 * GtkEntry:activatable-secondary:
1018 * Whether the secondary icon is activatable.
1020 * GTK+ emits the #GtkEntry::icon-pressed and
1021 * #GtkEntry::icon-released signals only on
1022 * sensitive, activatable icons.
1026 g_object_class_install_property (gobject_class,
1027 PROP_ACTIVATABLE_SECONDARY,
1028 g_param_spec_boolean ("activatable-secondary",
1029 P_("Secondary icon activatable"),
1030 P_("Whether the secondary icon is activatable"),
1032 GTK_PARAM_READWRITE));
1036 * GtkEntry:sensitive-primary:
1038 * Whether the primary icon is sensitive.
1040 * An insensitive icon will appear grayed out, it will not emit
1041 * #GtkEntry::icon-pressed signal, not allow DND and not change the cursor.
1045 g_object_class_install_property (gobject_class,
1046 PROP_SENSITIVE_PRIMARY,
1047 g_param_spec_boolean ("sensitive-primary",
1048 P_("Primary icon sensitvitiy"),
1049 P_("Whether the primary icon is sensitive"),
1051 GTK_PARAM_READWRITE));
1054 * GtkEntry:sensitive-secondary:
1056 * Whether the secondary icon is sensitive.
1058 * An insensitive icon will appear grayed out, it will not emit
1059 * #GtkEntry::icon-pressed signal, not allow DND and not change the cursor.
1063 g_object_class_install_property (gobject_class,
1064 PROP_SENSITIVE_SECONDARY,
1065 g_param_spec_boolean ("sensitive-secondary",
1066 P_("Secondary icon sensitivity"),
1067 P_("Whether the secondary icon is sensitive"),
1069 GTK_PARAM_READWRITE));
1072 * GtkEntry:prelight:
1074 * The prelight style property determines whether activatable
1075 * icons prelight on mouseover.
1079 gtk_widget_class_install_style_property (widget_class,
1080 g_param_spec_boolean ("prelight",
1082 P_("Whether activatable icons should prelight when hovered"),
1084 GTK_PARAM_READABLE));
1086 signals[POPULATE_POPUP] =
1087 g_signal_new (I_("populate-popup"),
1088 G_OBJECT_CLASS_TYPE (gobject_class),
1090 G_STRUCT_OFFSET (GtkEntryClass, populate_popup),
1092 _gtk_marshal_VOID__OBJECT,
1096 /* Action signals */
1099 g_signal_new (I_("activate"),
1100 G_OBJECT_CLASS_TYPE (gobject_class),
1101 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1102 G_STRUCT_OFFSET (GtkEntryClass, activate),
1104 _gtk_marshal_VOID__VOID,
1106 widget_class->activate_signal = signals[ACTIVATE];
1108 signals[MOVE_CURSOR] =
1109 g_signal_new (I_("move-cursor"),
1110 G_OBJECT_CLASS_TYPE (gobject_class),
1111 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1112 G_STRUCT_OFFSET (GtkEntryClass, move_cursor),
1114 _gtk_marshal_VOID__ENUM_INT_BOOLEAN,
1116 GTK_TYPE_MOVEMENT_STEP,
1120 signals[INSERT_AT_CURSOR] =
1121 g_signal_new (I_("insert-at-cursor"),
1122 G_OBJECT_CLASS_TYPE (gobject_class),
1123 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1124 G_STRUCT_OFFSET (GtkEntryClass, insert_at_cursor),
1126 _gtk_marshal_VOID__STRING,
1130 signals[DELETE_FROM_CURSOR] =
1131 g_signal_new (I_("delete-from-cursor"),
1132 G_OBJECT_CLASS_TYPE (gobject_class),
1133 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1134 G_STRUCT_OFFSET (GtkEntryClass, delete_from_cursor),
1136 _gtk_marshal_VOID__ENUM_INT,
1138 GTK_TYPE_DELETE_TYPE,
1141 signals[BACKSPACE] =
1142 g_signal_new (I_("backspace"),
1143 G_OBJECT_CLASS_TYPE (gobject_class),
1144 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1145 G_STRUCT_OFFSET (GtkEntryClass, backspace),
1147 _gtk_marshal_VOID__VOID,
1150 signals[CUT_CLIPBOARD] =
1151 g_signal_new (I_("cut-clipboard"),
1152 G_OBJECT_CLASS_TYPE (gobject_class),
1153 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1154 G_STRUCT_OFFSET (GtkEntryClass, cut_clipboard),
1156 _gtk_marshal_VOID__VOID,
1159 signals[COPY_CLIPBOARD] =
1160 g_signal_new (I_("copy-clipboard"),
1161 G_OBJECT_CLASS_TYPE (gobject_class),
1162 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1163 G_STRUCT_OFFSET (GtkEntryClass, copy_clipboard),
1165 _gtk_marshal_VOID__VOID,
1168 signals[PASTE_CLIPBOARD] =
1169 g_signal_new (I_("paste-clipboard"),
1170 G_OBJECT_CLASS_TYPE (gobject_class),
1171 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1172 G_STRUCT_OFFSET (GtkEntryClass, paste_clipboard),
1174 _gtk_marshal_VOID__VOID,
1177 signals[TOGGLE_OVERWRITE] =
1178 g_signal_new (I_("toggle-overwrite"),
1179 G_OBJECT_CLASS_TYPE (gobject_class),
1180 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1181 G_STRUCT_OFFSET (GtkEntryClass, toggle_overwrite),
1183 _gtk_marshal_VOID__VOID,
1187 * GtkEntry::icon-pressed:
1188 * @entry: The entry on which the signal is emitted
1189 * @icon_pos: The position of the clicked icon
1190 * @event: the button press event
1192 * The ::icon-pressed signal is emitted when an icon is clicked.
1196 signals[ICON_PRESSED] =
1197 g_signal_new (I_("icon-pressed"),
1198 G_TYPE_FROM_CLASS (gobject_class),
1199 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1202 _gtk_marshal_VOID__ENUM_BOXED,
1204 GTK_TYPE_ENTRY_ICON_POSITION,
1208 * GtkEntry::icon-released:
1209 * @entry: The entry on which the signal is emitted
1210 * @icon_pos: The position of the clicked icon
1211 * @event: the button release event
1213 * The ::icon-released signal is emitted on the button release from a
1218 signals[ICON_RELEASED] =
1219 g_signal_new (I_("icon-released"),
1220 G_TYPE_FROM_CLASS (gobject_class),
1221 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1224 _gtk_marshal_VOID__ENUM_BOXED,
1226 GTK_TYPE_ENTRY_ICON_POSITION,
1234 binding_set = gtk_binding_set_by_class (class);
1236 /* Moving the insertion point */
1237 add_move_binding (binding_set, GDK_Right, 0,
1238 GTK_MOVEMENT_VISUAL_POSITIONS, 1);
1240 add_move_binding (binding_set, GDK_Left, 0,
1241 GTK_MOVEMENT_VISUAL_POSITIONS, -1);
1243 add_move_binding (binding_set, GDK_KP_Right, 0,
1244 GTK_MOVEMENT_VISUAL_POSITIONS, 1);
1246 add_move_binding (binding_set, GDK_KP_Left, 0,
1247 GTK_MOVEMENT_VISUAL_POSITIONS, -1);
1249 add_move_binding (binding_set, GDK_Right, GDK_CONTROL_MASK,
1250 GTK_MOVEMENT_WORDS, 1);
1252 add_move_binding (binding_set, GDK_Left, GDK_CONTROL_MASK,
1253 GTK_MOVEMENT_WORDS, -1);
1255 add_move_binding (binding_set, GDK_KP_Right, GDK_CONTROL_MASK,
1256 GTK_MOVEMENT_WORDS, 1);
1258 add_move_binding (binding_set, GDK_KP_Left, GDK_CONTROL_MASK,
1259 GTK_MOVEMENT_WORDS, -1);
1261 add_move_binding (binding_set, GDK_Home, 0,
1262 GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
1264 add_move_binding (binding_set, GDK_End, 0,
1265 GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
1267 add_move_binding (binding_set, GDK_KP_Home, 0,
1268 GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
1270 add_move_binding (binding_set, GDK_KP_End, 0,
1271 GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
1273 add_move_binding (binding_set, GDK_Home, GDK_CONTROL_MASK,
1274 GTK_MOVEMENT_BUFFER_ENDS, -1);
1276 add_move_binding (binding_set, GDK_End, GDK_CONTROL_MASK,
1277 GTK_MOVEMENT_BUFFER_ENDS, 1);
1279 add_move_binding (binding_set, GDK_KP_Home, GDK_CONTROL_MASK,
1280 GTK_MOVEMENT_BUFFER_ENDS, -1);
1282 add_move_binding (binding_set, GDK_KP_End, GDK_CONTROL_MASK,
1283 GTK_MOVEMENT_BUFFER_ENDS, 1);
1287 gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_CONTROL_MASK,
1289 GTK_TYPE_MOVEMENT_STEP, GTK_MOVEMENT_BUFFER_ENDS,
1291 G_TYPE_BOOLEAN, FALSE);
1292 gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_CONTROL_MASK,
1294 GTK_TYPE_MOVEMENT_STEP, GTK_MOVEMENT_BUFFER_ENDS,
1296 G_TYPE_BOOLEAN, TRUE);
1298 gtk_binding_entry_add_signal (binding_set, GDK_slash, GDK_CONTROL_MASK,
1300 GTK_TYPE_MOVEMENT_STEP, GTK_MOVEMENT_BUFFER_ENDS,
1302 G_TYPE_BOOLEAN, FALSE);
1303 gtk_binding_entry_add_signal (binding_set, GDK_slash, GDK_CONTROL_MASK,
1305 GTK_TYPE_MOVEMENT_STEP, GTK_MOVEMENT_BUFFER_ENDS,
1307 G_TYPE_BOOLEAN, TRUE);
1310 gtk_binding_entry_add_signal (binding_set, GDK_backslash, GDK_CONTROL_MASK,
1312 GTK_TYPE_MOVEMENT_STEP, GTK_MOVEMENT_VISUAL_POSITIONS,
1314 G_TYPE_BOOLEAN, FALSE);
1315 gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
1317 GTK_TYPE_MOVEMENT_STEP, GTK_MOVEMENT_VISUAL_POSITIONS,
1319 G_TYPE_BOOLEAN, FALSE);
1323 gtk_binding_entry_add_signal (binding_set, GDK_Return, 0,
1325 gtk_binding_entry_add_signal (binding_set, GDK_ISO_Enter, 0,
1327 gtk_binding_entry_add_signal (binding_set, GDK_KP_Enter, 0,
1331 gtk_binding_entry_add_signal (binding_set, GDK_Delete, 0,
1332 "delete-from-cursor", 2,
1333 G_TYPE_ENUM, GTK_DELETE_CHARS,
1336 gtk_binding_entry_add_signal (binding_set, GDK_KP_Delete, 0,
1337 "delete-from-cursor", 2,
1338 G_TYPE_ENUM, GTK_DELETE_CHARS,
1341 gtk_binding_entry_add_signal (binding_set, GDK_BackSpace, 0,
1344 /* Make this do the same as Backspace, to help with mis-typing */
1345 gtk_binding_entry_add_signal (binding_set, GDK_BackSpace, GDK_SHIFT_MASK,
1348 gtk_binding_entry_add_signal (binding_set, GDK_Delete, GDK_CONTROL_MASK,
1349 "delete-from-cursor", 2,
1350 G_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
1353 gtk_binding_entry_add_signal (binding_set, GDK_KP_Delete, GDK_CONTROL_MASK,
1354 "delete-from-cursor", 2,
1355 G_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
1358 gtk_binding_entry_add_signal (binding_set, GDK_BackSpace, GDK_CONTROL_MASK,
1359 "delete-from-cursor", 2,
1360 G_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
1363 /* Cut/copy/paste */
1365 gtk_binding_entry_add_signal (binding_set, GDK_x, GDK_CONTROL_MASK,
1366 "cut-clipboard", 0);
1367 gtk_binding_entry_add_signal (binding_set, GDK_c, GDK_CONTROL_MASK,
1368 "copy-clipboard", 0);
1369 gtk_binding_entry_add_signal (binding_set, GDK_v, GDK_CONTROL_MASK,
1370 "paste-clipboard", 0);
1372 gtk_binding_entry_add_signal (binding_set, GDK_Delete, GDK_SHIFT_MASK,
1373 "cut-clipboard", 0);
1374 gtk_binding_entry_add_signal (binding_set, GDK_Insert, GDK_CONTROL_MASK,
1375 "copy-clipboard", 0);
1376 gtk_binding_entry_add_signal (binding_set, GDK_Insert, GDK_SHIFT_MASK,
1377 "paste-clipboard", 0);
1380 gtk_binding_entry_add_signal (binding_set, GDK_Insert, 0,
1381 "toggle-overwrite", 0);
1382 gtk_binding_entry_add_signal (binding_set, GDK_KP_Insert, 0,
1383 "toggle-overwrite", 0);
1386 * GtkEntry:inner-border:
1388 * Sets the text area's border between the text and the frame.
1392 gtk_widget_class_install_style_property (widget_class,
1393 g_param_spec_boxed ("inner-border",
1395 P_("Border between text and frame."),
1397 GTK_PARAM_READABLE));
1400 * GtkEntry:state-hint:
1402 * Indicates whether to pass a proper widget state when
1403 * drawing the shadow and the widget background.
1407 gtk_widget_class_install_style_property (widget_class,
1408 g_param_spec_boolean ("state-hint",
1410 P_("Whether to pass a proper state when drawing shadow or background"),
1412 GTK_PARAM_READABLE));
1414 gtk_settings_install_property (g_param_spec_boolean ("gtk-entry-select-on-focus",
1415 P_("Select on focus"),
1416 P_("Whether to select the contents of an entry when it is focused"),
1418 GTK_PARAM_READWRITE));
1421 * GtkSettings:gtk-entry-password-hint-timeout:
1423 * How long to show the last input character in hidden
1424 * entries. This value is in milliseconds. 0 disables showing the
1425 * last char. 600 is a good value for enabling it.
1429 gtk_settings_install_property (g_param_spec_uint ("gtk-entry-password-hint-timeout",
1430 P_("Password Hint Timeout"),
1431 P_("How long to show the last input character in hidden entries"),
1433 GTK_PARAM_READWRITE));
1435 g_type_class_add_private (gobject_class, sizeof (GtkEntryPrivate));
1439 gtk_entry_editable_init (GtkEditableClass *iface)
1441 iface->do_insert_text = gtk_entry_insert_text;
1442 iface->do_delete_text = gtk_entry_delete_text;
1443 iface->insert_text = gtk_entry_real_insert_text;
1444 iface->delete_text = gtk_entry_real_delete_text;
1445 iface->get_chars = gtk_entry_get_chars;
1446 iface->set_selection_bounds = gtk_entry_set_selection_bounds;
1447 iface->get_selection_bounds = gtk_entry_get_selection_bounds;
1448 iface->set_position = gtk_entry_real_set_position;
1449 iface->get_position = gtk_entry_get_position;
1453 gtk_entry_cell_editable_init (GtkCellEditableIface *iface)
1455 iface->start_editing = gtk_entry_start_editing;
1459 gtk_entry_set_property (GObject *object,
1461 const GValue *value,
1464 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (object);
1465 GtkEntry *entry = GTK_ENTRY (object);
1471 gboolean new_value = g_value_get_boolean (value);
1473 if (new_value != entry->editable)
1477 _gtk_entry_reset_im_context (entry);
1478 if (GTK_WIDGET_HAS_FOCUS (entry))
1479 gtk_im_context_focus_out (entry->im_context);
1481 entry->preedit_length = 0;
1482 entry->preedit_cursor = 0;
1485 entry->editable = new_value;
1487 if (new_value && GTK_WIDGET_HAS_FOCUS (entry))
1488 gtk_im_context_focus_in (entry->im_context);
1490 gtk_entry_queue_draw (entry);
1495 case PROP_MAX_LENGTH:
1496 gtk_entry_set_max_length (entry, g_value_get_int (value));
1499 case PROP_VISIBILITY:
1500 gtk_entry_set_visibility (entry, g_value_get_boolean (value));
1503 case PROP_HAS_FRAME:
1504 gtk_entry_set_has_frame (entry, g_value_get_boolean (value));
1507 case PROP_INNER_BORDER:
1508 gtk_entry_set_inner_border (entry, g_value_get_boxed (value));
1511 case PROP_INVISIBLE_CHAR:
1512 gtk_entry_set_invisible_char (entry, g_value_get_uint (value));
1515 case PROP_ACTIVATES_DEFAULT:
1516 gtk_entry_set_activates_default (entry, g_value_get_boolean (value));
1519 case PROP_WIDTH_CHARS:
1520 gtk_entry_set_width_chars (entry, g_value_get_int (value));
1524 gtk_entry_set_text (entry, g_value_get_string (value));
1528 gtk_entry_set_alignment (entry, g_value_get_float (value));
1531 case PROP_TRUNCATE_MULTILINE:
1532 entry->truncate_multiline = g_value_get_boolean (value);
1535 case PROP_SHADOW_TYPE:
1536 priv->shadow_type = g_value_get_enum (value);
1539 case PROP_OVERWRITE_MODE:
1540 gtk_entry_set_overwrite_mode (entry, g_value_get_boolean (value));
1543 case PROP_INVISIBLE_CHAR_SET:
1544 if (g_value_get_boolean (value))
1545 priv->invisible_char_set = TRUE;
1547 gtk_entry_unset_invisible_char (entry);
1550 case PROP_CAPS_LOCK_WARNING:
1551 priv->caps_lock_warning = g_value_get_boolean (value);
1554 case PROP_PROGRESS_FRACTION:
1555 gtk_entry_set_progress_fraction (entry, g_value_get_double (value));
1558 case PROP_PROGRESS_PULSE_STEP:
1559 gtk_entry_set_progress_pulse_step (entry, g_value_get_double (value));
1561 case PROP_PIXBUF_PRIMARY:
1562 gtk_entry_set_icon_from_pixbuf (entry,
1563 GTK_ENTRY_ICON_PRIMARY,
1564 g_value_get_object (value));
1567 case PROP_PIXBUF_SECONDARY:
1568 gtk_entry_set_icon_from_pixbuf (entry,
1569 GTK_ENTRY_ICON_SECONDARY,
1570 g_value_get_object (value));
1573 case PROP_STOCK_PRIMARY:
1574 gtk_entry_set_icon_from_stock (entry,
1575 GTK_ENTRY_ICON_PRIMARY,
1576 g_value_get_string (value));
1579 case PROP_STOCK_SECONDARY:
1580 gtk_entry_set_icon_from_stock (entry,
1581 GTK_ENTRY_ICON_SECONDARY,
1582 g_value_get_string (value));
1585 case PROP_ICON_NAME_PRIMARY:
1586 gtk_entry_set_icon_from_icon_name (entry,
1587 GTK_ENTRY_ICON_PRIMARY,
1588 g_value_get_string (value));
1591 case PROP_ICON_NAME_SECONDARY:
1592 gtk_entry_set_icon_from_icon_name (entry,
1593 GTK_ENTRY_ICON_SECONDARY,
1594 g_value_get_string (value));
1597 case PROP_GICON_PRIMARY:
1598 gtk_entry_set_icon_from_gicon (entry,
1599 GTK_ENTRY_ICON_PRIMARY,
1600 g_value_get_object (value));
1603 case PROP_GICON_SECONDARY:
1604 gtk_entry_set_icon_from_gicon (entry,
1605 GTK_ENTRY_ICON_SECONDARY,
1606 g_value_get_object (value));
1609 case PROP_ACTIVATABLE_PRIMARY:
1610 gtk_entry_set_icon_activatable (entry,
1611 GTK_ENTRY_ICON_PRIMARY,
1612 g_value_get_boolean (value));
1615 case PROP_ACTIVATABLE_SECONDARY:
1616 gtk_entry_set_icon_activatable (entry,
1617 GTK_ENTRY_ICON_SECONDARY,
1618 g_value_get_boolean (value));
1621 case PROP_SENSITIVE_PRIMARY:
1622 gtk_entry_set_icon_sensitive (entry,
1623 GTK_ENTRY_ICON_PRIMARY,
1624 g_value_get_boolean (value));
1627 case PROP_SENSITIVE_SECONDARY:
1628 gtk_entry_set_icon_sensitive (entry,
1629 GTK_ENTRY_ICON_SECONDARY,
1630 g_value_get_boolean (value));
1633 case PROP_SCROLL_OFFSET:
1634 case PROP_CURSOR_POSITION:
1636 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1642 gtk_entry_get_property (GObject *object,
1647 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (object);
1648 GtkEntry *entry = GTK_ENTRY (object);
1652 case PROP_CURSOR_POSITION:
1653 g_value_set_int (value, entry->current_pos);
1656 case PROP_SELECTION_BOUND:
1657 g_value_set_int (value, entry->selection_bound);
1661 g_value_set_boolean (value, entry->editable);
1664 case PROP_MAX_LENGTH:
1665 g_value_set_int (value, entry->text_max_length);
1668 case PROP_VISIBILITY:
1669 g_value_set_boolean (value, entry->visible);
1672 case PROP_HAS_FRAME:
1673 g_value_set_boolean (value, entry->has_frame);
1676 case PROP_INNER_BORDER:
1677 g_value_set_boxed (value, gtk_entry_get_inner_border (entry));
1680 case PROP_INVISIBLE_CHAR:
1681 g_value_set_uint (value, entry->invisible_char);
1684 case PROP_ACTIVATES_DEFAULT:
1685 g_value_set_boolean (value, entry->activates_default);
1688 case PROP_WIDTH_CHARS:
1689 g_value_set_int (value, entry->width_chars);
1692 case PROP_SCROLL_OFFSET:
1693 g_value_set_int (value, entry->scroll_offset);
1697 g_value_set_string (value, gtk_entry_get_text (entry));
1701 g_value_set_float (value, gtk_entry_get_alignment (entry));
1704 case PROP_TRUNCATE_MULTILINE:
1705 g_value_set_boolean (value, entry->truncate_multiline);
1708 case PROP_SHADOW_TYPE:
1709 g_value_set_enum (value, priv->shadow_type);
1712 case PROP_OVERWRITE_MODE:
1713 g_value_set_boolean (value, entry->overwrite_mode);
1716 case PROP_TEXT_LENGTH:
1717 g_value_set_uint (value, entry->text_length);
1720 case PROP_INVISIBLE_CHAR_SET:
1721 g_value_set_boolean (value, priv->invisible_char_set);
1724 case PROP_CAPS_LOCK_WARNING:
1725 g_value_set_boolean (value, priv->caps_lock_warning);
1728 case PROP_PROGRESS_FRACTION:
1729 g_value_set_double (value, priv->progress_fraction);
1732 case PROP_PROGRESS_PULSE_STEP:
1733 g_value_set_double (value, priv->progress_pulse_fraction);
1735 case PROP_PIXBUF_PRIMARY:
1736 g_value_set_object (value,
1737 gtk_entry_get_pixbuf (entry,
1738 GTK_ENTRY_ICON_PRIMARY));
1741 case PROP_PIXBUF_SECONDARY:
1742 g_value_set_object (value,
1743 gtk_entry_get_pixbuf (entry,
1744 GTK_ENTRY_ICON_SECONDARY));
1747 case PROP_STOCK_PRIMARY:
1748 g_value_set_string (value,
1749 gtk_entry_get_stock (entry,
1750 GTK_ENTRY_ICON_PRIMARY));
1753 case PROP_STOCK_SECONDARY:
1754 g_value_set_string (value,
1755 gtk_entry_get_stock (entry,
1756 GTK_ENTRY_ICON_SECONDARY));
1759 case PROP_ICON_NAME_PRIMARY:
1760 g_value_set_string (value,
1761 gtk_entry_get_icon_name (entry,
1762 GTK_ENTRY_ICON_PRIMARY));
1765 case PROP_ICON_NAME_SECONDARY:
1766 g_value_set_string (value,
1767 gtk_entry_get_icon_name (entry,
1768 GTK_ENTRY_ICON_SECONDARY));
1771 case PROP_GICON_PRIMARY:
1772 g_value_set_object (value,
1773 gtk_entry_get_gicon (entry,
1774 GTK_ENTRY_ICON_PRIMARY));
1777 case PROP_GICON_SECONDARY:
1778 g_value_set_object (value,
1779 gtk_entry_get_gicon (entry,
1780 GTK_ENTRY_ICON_SECONDARY));
1782 case PROP_STORAGE_TYPE_PRIMARY:
1783 g_value_set_enum (value,
1784 gtk_entry_get_storage_type (entry, GTK_ENTRY_ICON_PRIMARY));
1787 case PROP_STORAGE_TYPE_SECONDARY:
1788 g_value_set_enum (value,
1789 gtk_entry_get_storage_type (entry, GTK_ENTRY_ICON_SECONDARY));
1792 case PROP_ACTIVATABLE_PRIMARY:
1793 g_value_set_boolean (value,
1794 gtk_entry_get_icon_activatable (entry, GTK_ENTRY_ICON_PRIMARY));
1797 case PROP_ACTIVATABLE_SECONDARY:
1798 g_value_set_boolean (value,
1799 gtk_entry_get_icon_activatable (entry, GTK_ENTRY_ICON_SECONDARY));
1802 case PROP_SENSITIVE_PRIMARY:
1803 g_value_set_boolean (value,
1804 gtk_entry_get_icon_sensitive (entry, GTK_ENTRY_ICON_PRIMARY));
1807 case PROP_SENSITIVE_SECONDARY:
1808 g_value_set_boolean (value,
1809 gtk_entry_get_icon_sensitive (entry, GTK_ENTRY_ICON_SECONDARY));
1813 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1819 find_invisible_char (GtkWidget *widget)
1821 PangoLayout *layout;
1822 PangoAttrList *attr_list;
1824 gunichar invisible_chars [] = {
1825 0x25cf, /* BLACK CIRCLE */
1826 0x2022, /* BULLET */
1827 0x2731, /* HEAVY ASTERISK */
1828 0x273a /* SIXTEEN POINTED ASTERISK */
1831 layout = gtk_widget_create_pango_layout (widget, NULL);
1833 attr_list = pango_attr_list_new ();
1834 pango_attr_list_insert (attr_list, pango_attr_fallback_new (FALSE));
1836 pango_layout_set_attributes (layout, attr_list);
1837 pango_attr_list_unref (attr_list);
1839 for (i = 0; i < G_N_ELEMENTS (invisible_chars); i++)
1841 gchar text[7] = { 0, };
1844 len = g_unichar_to_utf8 (invisible_chars[i], text);
1845 pango_layout_set_text (layout, text, len);
1847 count = pango_layout_get_unknown_glyphs_count (layout);
1851 g_object_unref (layout);
1852 return invisible_chars[i];
1856 g_object_unref (layout);
1861 gtk_entry_init (GtkEntry *entry)
1863 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
1865 GTK_WIDGET_SET_FLAGS (entry, GTK_CAN_FOCUS);
1867 entry->text_size = MIN_SIZE;
1868 entry->text = g_malloc (entry->text_size);
1869 entry->text[0] = '\0';
1871 entry->editable = TRUE;
1872 entry->visible = TRUE;
1873 entry->invisible_char = find_invisible_char (GTK_WIDGET (entry));
1874 entry->dnd_position = -1;
1875 entry->width_chars = -1;
1876 entry->is_cell_renderer = FALSE;
1877 entry->editing_canceled = FALSE;
1878 entry->has_frame = TRUE;
1879 entry->truncate_multiline = FALSE;
1880 priv->shadow_type = GTK_SHADOW_IN;
1882 priv->caps_lock_warning = TRUE;
1883 priv->progress_fraction = 0.0;
1884 priv->progress_pulse_fraction = 0.1;
1886 gtk_drag_dest_set (GTK_WIDGET (entry),
1887 GTK_DEST_DEFAULT_HIGHLIGHT,
1889 GDK_ACTION_COPY | GDK_ACTION_MOVE);
1890 gtk_drag_dest_add_text_targets (GTK_WIDGET (entry));
1892 /* This object is completely private. No external entity can gain a reference
1893 * to it; so we create it here and destroy it in finalize().
1895 entry->im_context = gtk_im_multicontext_new ();
1897 g_signal_connect (entry->im_context, "commit",
1898 G_CALLBACK (gtk_entry_commit_cb), entry);
1899 g_signal_connect (entry->im_context, "preedit-changed",
1900 G_CALLBACK (gtk_entry_preedit_changed_cb), entry);
1901 g_signal_connect (entry->im_context, "retrieve-surrounding",
1902 G_CALLBACK (gtk_entry_retrieve_surrounding_cb), entry);
1903 g_signal_connect (entry->im_context, "delete-surrounding",
1904 G_CALLBACK (gtk_entry_delete_surrounding_cb), entry);
1908 get_icon_width (GtkEntry *entry,
1909 GtkEntryIconPosition icon_pos)
1911 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
1912 EntryIconInfo *icon_info;
1914 GtkSettings *settings;
1915 gint menu_icon_width;
1917 if (!(icon_info = priv->icons[icon_pos]))
1920 if (icon_info->pixbuf == NULL)
1923 if (priv->icons[icon_pos] == NULL)
1926 screen = gtk_widget_get_screen (GTK_WIDGET (entry));
1927 settings = gtk_settings_get_for_screen (screen);
1929 gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU,
1930 &menu_icon_width, NULL);
1932 return MAX (gdk_pixbuf_get_width (icon_info->pixbuf), menu_icon_width);
1936 get_icon_allocation (GtkEntry *entry,
1938 GtkAllocation *widget_alloc,
1939 GtkAllocation *text_area_alloc,
1940 GtkAllocation *allocation,
1941 GtkEntryIconPosition *icon_pos)
1943 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
1946 rtl = (gtk_widget_get_direction (GTK_WIDGET (entry)) == GTK_TEXT_DIR_RTL);
1949 *icon_pos = (rtl ? GTK_ENTRY_ICON_SECONDARY : GTK_ENTRY_ICON_PRIMARY);
1951 *icon_pos = (rtl ? GTK_ENTRY_ICON_PRIMARY : GTK_ENTRY_ICON_SECONDARY);
1953 allocation->y = text_area_alloc->y;
1954 allocation->width = get_icon_width (entry, *icon_pos);
1955 allocation->height = text_area_alloc->height;
1957 if (allocation->width > 0)
1958 allocation->width += 2 * priv->icon_margin;
1961 allocation->x = text_area_alloc->x;
1963 allocation->x = text_area_alloc->x + text_area_alloc->width - allocation->width;
1968 begin_change (GtkEntry *entry)
1970 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
1972 priv->change_count++;
1976 end_change (GtkEntry *entry)
1978 GtkEditable *editable = GTK_EDITABLE (entry);
1979 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
1981 g_return_if_fail (priv->change_count > 0);
1983 priv->change_count--;
1985 if (priv->change_count == 0)
1987 if (priv->real_changed)
1989 g_signal_emit_by_name (editable, "changed");
1990 priv->real_changed = FALSE;
1996 emit_changed (GtkEntry *entry)
1998 GtkEditable *editable = GTK_EDITABLE (entry);
1999 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
2001 if (priv->change_count == 0)
2002 g_signal_emit_by_name (editable, "changed");
2004 priv->real_changed = TRUE;
2008 * Overwrite a memory that might contain sensitive information.
2011 trash_area (gchar *area, gsize len)
2013 volatile gchar *varea = (volatile gchar *)area;
2019 gtk_entry_destroy (GtkObject *object)
2021 GtkEntry *entry = GTK_ENTRY (object);
2024 entry->current_pos = entry->selection_bound = entry->text_length = 0;
2025 _gtk_entry_reset_im_context (entry);
2026 gtk_entry_reset_layout (entry);
2028 if (entry->blink_timeout)
2030 g_source_remove (entry->blink_timeout);
2031 entry->blink_timeout = 0;
2034 if (entry->recompute_idle)
2036 g_source_remove (entry->recompute_idle);
2037 entry->recompute_idle = 0;
2040 if (!entry->visible)
2042 /* We want to trash the text here because the entry might be leaked. */
2043 trash_area (entry->text, strlen (entry->text));
2046 GTK_OBJECT_CLASS (gtk_entry_parent_class)->destroy (object);
2050 gtk_entry_dispose (GObject *object)
2052 GtkEntry *entry = GTK_ENTRY (object);
2054 gtk_entry_set_icon_from_pixbuf (entry, GTK_ENTRY_ICON_PRIMARY, NULL);
2055 gtk_entry_set_icon_tooltip_markup (entry, GTK_ENTRY_ICON_PRIMARY, NULL);
2056 gtk_entry_set_icon_from_pixbuf (entry, GTK_ENTRY_ICON_SECONDARY, NULL);
2057 gtk_entry_set_icon_tooltip_markup (entry, GTK_ENTRY_ICON_SECONDARY, NULL);
2059 G_OBJECT_CLASS (gtk_entry_parent_class)->dispose (object);
2063 gtk_entry_finalize (GObject *object)
2065 GtkEntry *entry = GTK_ENTRY (object);
2066 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
2067 EntryIconInfo *icon_info = NULL;
2070 for (i = 0; i < MAX_ICONS; i++)
2072 if ((icon_info = priv->icons[i]) != NULL)
2074 if (icon_info->target_list != NULL)
2076 gtk_target_list_unref (icon_info->target_list);
2077 icon_info->target_list = NULL;
2080 g_slice_free (EntryIconInfo, icon_info);
2081 priv->icons[i] = NULL;
2085 gtk_entry_set_completion (entry, NULL);
2087 if (entry->cached_layout)
2088 g_object_unref (entry->cached_layout);
2090 g_object_unref (entry->im_context);
2092 if (entry->blink_timeout)
2093 g_source_remove (entry->blink_timeout);
2095 if (entry->recompute_idle)
2096 g_source_remove (entry->recompute_idle);
2098 entry->text_size = 0;
2102 if (!entry->visible)
2103 trash_area (entry->text, strlen (entry->text));
2104 g_free (entry->text);
2108 G_OBJECT_CLASS (gtk_entry_parent_class)->finalize (object);
2112 update_cursors (GtkWidget *widget)
2114 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (widget);
2115 EntryIconInfo *icon_info = NULL;
2116 GdkDisplay *display;
2120 for (i = 0; i < MAX_ICONS; i++)
2122 if ((icon_info = priv->icons[i]) != NULL)
2124 if (icon_info->pixbuf != NULL)
2125 gdk_window_show (icon_info->window);
2127 if (icon_info->insensitive ||
2128 (icon_info->nonactivatable && icon_info->target_list == NULL))
2130 display = gtk_widget_get_display (widget);
2131 cursor = gdk_cursor_new_for_display (display, GDK_XTERM);
2132 gdk_window_set_cursor (icon_info->window, cursor);
2133 gdk_cursor_unref (cursor);
2137 gdk_window_set_cursor (icon_info->window, NULL);
2144 realize_icon_info (GtkWidget *widget, GtkEntryIconPosition icon_pos)
2146 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (widget);
2147 EntryIconInfo *icon_info = priv->icons[icon_pos];
2148 GdkWindowAttr attributes;
2149 gint attributes_mask;
2151 g_return_if_fail (icon_info != NULL);
2155 attributes.width = 1;
2156 attributes.height = 1;
2157 attributes.window_type = GDK_WINDOW_CHILD;
2158 attributes.wclass = GDK_INPUT_OUTPUT;
2159 attributes.visual = gtk_widget_get_visual (widget);
2160 attributes.colormap = gtk_widget_get_colormap (widget);
2161 attributes.event_mask = gtk_widget_get_events (widget);
2162 attributes.event_mask |= (GDK_EXPOSURE_MASK |
2163 GDK_BUTTON_PRESS_MASK |
2164 GDK_BUTTON_RELEASE_MASK |
2165 GDK_BUTTON1_MOTION_MASK |
2166 GDK_BUTTON3_MOTION_MASK |
2167 GDK_POINTER_MOTION_HINT_MASK |
2168 GDK_POINTER_MOTION_MASK |
2169 GDK_ENTER_NOTIFY_MASK |
2170 GDK_LEAVE_NOTIFY_MASK);
2171 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
2173 icon_info->window = gdk_window_new (widget->window,
2176 gdk_window_set_user_data (icon_info->window, widget);
2177 gdk_window_set_background (icon_info->window,
2178 &widget->style->base[GTK_WIDGET_STATE (widget)]);
2180 gtk_widget_queue_resize (widget);
2183 static EntryIconInfo*
2184 construct_icon_info (GtkWidget *widget, GtkEntryIconPosition icon_pos)
2186 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (widget);
2187 EntryIconInfo *icon_info;
2189 g_return_val_if_fail (priv->icons[icon_pos] == NULL, NULL);
2191 icon_info = g_slice_new0 (EntryIconInfo);
2192 priv->icons[icon_pos] = icon_info;
2194 if (GTK_WIDGET_REALIZED (widget))
2196 realize_icon_info (widget, icon_pos);
2199 if (GTK_WIDGET_MAPPED (widget))
2200 gdk_window_show (icon_info->window);
2206 gtk_entry_map (GtkWidget *widget)
2208 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (widget);
2209 EntryIconInfo *icon_info = NULL;
2212 if (GTK_WIDGET_REALIZED (widget) && !GTK_WIDGET_MAPPED (widget))
2214 GTK_WIDGET_CLASS (gtk_entry_parent_class)->map (widget);
2216 for (i = 0; i < MAX_ICONS; i++)
2218 if ((icon_info = priv->icons[i]) != NULL)
2220 if (icon_info->pixbuf != NULL && icon_info->window != NULL)
2221 gdk_window_show (icon_info->window);
2225 update_cursors (widget);
2230 gtk_entry_unmap (GtkWidget *widget)
2232 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (widget);
2233 EntryIconInfo *icon_info = NULL;
2236 if (GTK_WIDGET_MAPPED (widget))
2238 for (i = 0; i < MAX_ICONS; i++)
2240 if ((icon_info = priv->icons[i]) != NULL)
2242 if (icon_info->pixbuf != NULL && icon_info->window != NULL)
2243 gdk_window_hide (icon_info->window);
2247 GTK_WIDGET_CLASS (gtk_entry_parent_class)->unmap (widget);
2252 gtk_entry_realize (GtkWidget *widget)
2255 GtkEntryPrivate *priv;
2256 EntryIconInfo *icon_info;
2257 GdkWindowAttr attributes;
2258 gint attributes_mask;
2261 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
2262 entry = GTK_ENTRY (widget);
2263 priv = GTK_ENTRY_GET_PRIVATE (entry);
2265 attributes.window_type = GDK_WINDOW_CHILD;
2267 get_widget_window_size (entry, &attributes.x, &attributes.y, &attributes.width, &attributes.height);
2269 attributes.wclass = GDK_INPUT_OUTPUT;
2270 attributes.visual = gtk_widget_get_visual (widget);
2271 attributes.colormap = gtk_widget_get_colormap (widget);
2272 attributes.event_mask = gtk_widget_get_events (widget);
2273 attributes.event_mask |= (GDK_EXPOSURE_MASK |
2274 GDK_BUTTON_PRESS_MASK |
2275 GDK_BUTTON_RELEASE_MASK |
2276 GDK_BUTTON1_MOTION_MASK |
2277 GDK_BUTTON3_MOTION_MASK |
2278 GDK_POINTER_MOTION_HINT_MASK |
2279 GDK_POINTER_MOTION_MASK |
2280 GDK_ENTER_NOTIFY_MASK |
2281 GDK_LEAVE_NOTIFY_MASK);
2282 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
2284 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
2285 gdk_window_set_user_data (widget->window, entry);
2287 gtk_entry_get_text_area_size (entry, &attributes.x, &attributes.y, &attributes.width, &attributes.height);
2289 if (GTK_WIDGET_IS_SENSITIVE (widget))
2291 attributes.cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget), GDK_XTERM);
2292 attributes_mask |= GDK_WA_CURSOR;
2295 entry->text_area = gdk_window_new (widget->window, &attributes, attributes_mask);
2297 gdk_window_set_user_data (entry->text_area, entry);
2299 if (attributes_mask & GDK_WA_CURSOR)
2300 gdk_cursor_unref (attributes.cursor);
2302 widget->style = gtk_style_attach (widget->style, widget->window);
2304 gdk_window_set_background (widget->window, &widget->style->base[GTK_WIDGET_STATE (widget)]);
2305 gdk_window_set_background (entry->text_area, &widget->style->base[GTK_WIDGET_STATE (widget)]);
2307 gdk_window_show (entry->text_area);
2309 gtk_im_context_set_client_window (entry->im_context, entry->text_area);
2311 gtk_entry_adjust_scroll (entry);
2312 gtk_entry_update_primary_selection (entry);
2315 /* If the icon positions are already setup, create their windows.
2316 * Otherwise if they don't exist yet, then construct_icon_info()
2317 * will create the windows once the widget is already realized.
2319 for (i = 0; i < MAX_ICONS; i++)
2321 if ((icon_info = priv->icons[i]) != NULL)
2323 if (icon_info->window == NULL)
2324 realize_icon_info (widget, i);
2328 gtk_widget_queue_resize (widget);
2332 gtk_entry_unrealize (GtkWidget *widget)
2334 GtkEntry *entry = GTK_ENTRY (widget);
2335 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
2336 GtkClipboard *clipboard;
2337 EntryIconInfo *icon_info;
2340 gtk_entry_reset_layout (entry);
2342 gtk_im_context_set_client_window (entry->im_context, NULL);
2344 clipboard = gtk_widget_get_clipboard (widget, GDK_SELECTION_PRIMARY);
2345 if (gtk_clipboard_get_owner (clipboard) == G_OBJECT (entry))
2346 gtk_clipboard_clear (clipboard);
2348 if (entry->text_area)
2350 gdk_window_set_user_data (entry->text_area, NULL);
2351 gdk_window_destroy (entry->text_area);
2352 entry->text_area = NULL;
2355 if (entry->popup_menu)
2357 gtk_widget_destroy (entry->popup_menu);
2358 entry->popup_menu = NULL;
2361 GTK_WIDGET_CLASS (gtk_entry_parent_class)->unrealize (widget);
2363 for (i = 0; i < MAX_ICONS; i++)
2365 if ((icon_info = priv->icons[i]) != NULL)
2367 if (icon_info->window != NULL)
2369 gdk_window_destroy (icon_info->window);
2370 icon_info->window = NULL;
2377 _gtk_entry_get_borders (GtkEntry *entry,
2381 GtkWidget *widget = GTK_WIDGET (entry);
2382 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (widget);
2384 if (entry->has_frame)
2386 *xborder = widget->style->xthickness;
2387 *yborder = widget->style->ythickness;
2395 if (!priv->interior_focus)
2397 *xborder += priv->focus_width;
2398 *yborder += priv->focus_width;
2403 gtk_entry_size_request (GtkWidget *widget,
2404 GtkRequisition *requisition)
2406 GtkEntry *entry = GTK_ENTRY (widget);
2407 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
2408 PangoFontMetrics *metrics;
2409 gint xborder, yborder;
2410 GtkBorder inner_border;
2411 PangoContext *context;
2412 int icon_widths = 0;
2415 gtk_widget_ensure_style (widget);
2416 context = gtk_widget_get_pango_context (widget);
2417 metrics = pango_context_get_metrics (context,
2418 widget->style->font_desc,
2419 pango_context_get_language (context));
2421 entry->ascent = pango_font_metrics_get_ascent (metrics);
2422 entry->descent = pango_font_metrics_get_descent (metrics);
2424 _gtk_entry_get_borders (entry, &xborder, &yborder);
2425 _gtk_entry_effective_inner_border (entry, &inner_border);
2427 if (entry->width_chars < 0)
2428 requisition->width = MIN_ENTRY_WIDTH + xborder * 2 + inner_border.left + inner_border.right;
2431 gint char_width = pango_font_metrics_get_approximate_char_width (metrics);
2432 gint digit_width = pango_font_metrics_get_approximate_digit_width (metrics);
2433 gint char_pixels = (MAX (char_width, digit_width) + PANGO_SCALE - 1) / PANGO_SCALE;
2435 requisition->width = char_pixels * entry->width_chars + xborder * 2 + inner_border.left + inner_border.right;
2438 requisition->height = PANGO_PIXELS (entry->ascent + entry->descent) + yborder * 2 + inner_border.top + inner_border.bottom;
2440 for (i = 0; i < MAX_ICONS; i++)
2442 icon_width = get_icon_width (entry, i);
2444 icon_widths += icon_width + 2 * priv->icon_margin;
2447 if (icon_widths > requisition->width)
2448 requisition->width += icon_widths;
2450 pango_font_metrics_unref (metrics);
2454 place_windows (GtkEntry *entry,
2455 GtkAllocation *widget_alloc)
2457 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
2458 GtkEntryIconPosition left_icon_pos;
2459 GtkEntryIconPosition right_icon_pos;
2460 GtkAllocation left_icon_alloc;
2461 GtkAllocation right_icon_alloc;
2462 GtkAllocation text_area_alloc;
2463 EntryIconInfo *icon_info = NULL;
2465 get_text_area_size (entry, &text_area_alloc);
2466 get_icon_allocation (entry, TRUE, widget_alloc, &text_area_alloc,
2467 &left_icon_alloc, &left_icon_pos);
2468 get_icon_allocation (entry, FALSE, widget_alloc, &text_area_alloc,
2469 &right_icon_alloc, &right_icon_pos);
2471 if (left_icon_alloc.width > 0)
2473 text_area_alloc.x = left_icon_alloc.x + left_icon_alloc.width;
2474 text_area_alloc.width -= left_icon_alloc.width;
2477 if (right_icon_alloc.width > 0)
2478 text_area_alloc.width -= right_icon_alloc.width;
2480 if ((icon_info = priv->icons[left_icon_pos]) != NULL)
2481 gdk_window_move_resize (icon_info->window,
2482 left_icon_alloc.x, left_icon_alloc.y,
2483 left_icon_alloc.width, left_icon_alloc.height);
2485 if ((icon_info = priv->icons[right_icon_pos]) != NULL)
2486 gdk_window_move_resize (icon_info->window,
2487 right_icon_alloc.x, right_icon_alloc.y,
2488 right_icon_alloc.width, right_icon_alloc.height);
2490 gdk_window_move_resize (GTK_ENTRY (entry)->text_area,
2491 text_area_alloc.x, text_area_alloc.y,
2492 text_area_alloc.width, text_area_alloc.height);
2494 gtk_entry_adjust_scroll (GTK_ENTRY (entry));
2498 get_text_area_size (GtkEntry *entry,
2499 GtkAllocation *alloc)
2501 gtk_entry_get_text_area_size (entry,
2509 gtk_entry_get_text_area_size (GtkEntry *entry,
2516 gint xborder, yborder;
2517 GtkRequisition requisition;
2518 GtkWidget *widget = GTK_WIDGET (entry);
2519 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (widget);
2521 gtk_widget_get_child_requisition (widget, &requisition);
2522 _gtk_entry_get_borders (entry, &xborder, &yborder);
2524 if (GTK_WIDGET_REALIZED (widget))
2525 gdk_drawable_get_size (widget->window, NULL, &frame_height);
2527 frame_height = requisition.height;
2529 if (GTK_WIDGET_HAS_FOCUS (widget) && !priv->interior_focus)
2530 frame_height -= 2 * priv->focus_width;
2536 *y = frame_height / 2 - (requisition.height - yborder * 2) / 2;
2539 *width = GTK_WIDGET (entry)->allocation.width - xborder * 2;
2542 *height = requisition.height - yborder * 2;
2546 get_widget_window_size (GtkEntry *entry,
2552 GtkRequisition requisition;
2553 GtkWidget *widget = GTK_WIDGET (entry);
2555 gtk_widget_get_child_requisition (widget, &requisition);
2558 *x = widget->allocation.x;
2562 if (entry->is_cell_renderer)
2563 *y = widget->allocation.y;
2565 *y = widget->allocation.y + (widget->allocation.height - requisition.height) / 2;
2569 *width = widget->allocation.width;
2573 if (entry->is_cell_renderer)
2574 *height = widget->allocation.height;
2576 *height = requisition.height;
2581 _gtk_entry_effective_inner_border (GtkEntry *entry,
2584 GtkBorder *tmp_border;
2586 tmp_border = g_object_get_qdata (G_OBJECT (entry), quark_inner_border);
2590 *border = *tmp_border;
2594 gtk_widget_style_get (GTK_WIDGET (entry), "inner-border", &tmp_border, NULL);
2598 *border = *tmp_border;
2599 gtk_border_free (tmp_border);
2603 *border = default_inner_border;
2607 gtk_entry_size_allocate (GtkWidget *widget,
2608 GtkAllocation *allocation)
2610 GtkEntry *entry = GTK_ENTRY (widget);
2612 widget->allocation = *allocation;
2614 if (GTK_WIDGET_REALIZED (widget))
2616 /* We call gtk_widget_get_child_requisition, since we want (for
2617 * backwards compatibility reasons) the realization here to
2618 * be affected by the usize of the entry, if set
2620 gint x, y, width, height;
2621 GtkEntryCompletion* completion;
2623 get_widget_window_size (entry, &x, &y, &width, &height);
2625 gdk_window_move_resize (widget->window,
2626 x, y, width, height);
2628 gtk_entry_get_text_area_size (entry, &x, &y, &width, &height);
2630 gdk_window_move_resize (entry->text_area,
2631 x, y, width, height);
2633 gtk_entry_recompute (entry);
2635 completion = gtk_entry_get_completion (entry);
2636 if (completion && GTK_WIDGET_MAPPED (completion->priv->popup_window))
2637 _gtk_entry_completion_resize_popup (completion);
2639 place_windows (entry, allocation);
2643 /* Kudos to the gnome-panel guys. */
2645 colorshift_pixbuf (GdkPixbuf *dest,
2650 gint width, height, has_alpha, src_rowstride, dest_rowstride;
2651 guchar *target_pixels;
2652 guchar *original_pixels;
2658 has_alpha = gdk_pixbuf_get_has_alpha (src);
2659 width = gdk_pixbuf_get_width (src);
2660 height = gdk_pixbuf_get_height (src);
2661 src_rowstride = gdk_pixbuf_get_rowstride (src);
2662 dest_rowstride = gdk_pixbuf_get_rowstride (dest);
2663 original_pixels = gdk_pixbuf_get_pixels (src);
2664 target_pixels = gdk_pixbuf_get_pixels (dest);
2666 for (i = 0; i < height; i++)
2668 pix_dest = target_pixels + i * dest_rowstride;
2669 pix_src = original_pixels + i * src_rowstride;
2671 for (j = 0; j < width; j++)
2678 *(pix_dest++) = CLAMP (val, 0, 255);
2681 *(pix_dest++) = CLAMP (val, 0, 255);
2684 *(pix_dest++) = CLAMP (val, 0, 255);
2687 *(pix_dest++) = *(pix_src++);
2693 should_prelight (GtkEntry *entry,
2694 GtkEntryIconPosition icon_pos)
2696 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
2697 EntryIconInfo *icon_info = NULL;
2700 if (!(icon_info = priv->icons[icon_pos]))
2703 if (icon_info->nonactivatable && icon_info->target_list == NULL)
2706 if (icon_info->pressed)
2709 gtk_widget_style_get (GTK_WIDGET (entry),
2710 "prelight", &prelight,
2717 draw_icon (GtkWidget *widget,
2718 GtkEntryIconPosition icon_pos)
2720 GtkEntry *entry = GTK_ENTRY (widget);
2721 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
2722 EntryIconInfo *icon_info = NULL;
2724 gint x, y, width, height;
2726 if (!(icon_info = priv->icons[icon_pos]))
2729 gtk_entry_ensure_pixbuf (entry, icon_pos);
2731 if (icon_info->pixbuf == NULL)
2734 gdk_drawable_get_size (icon_info->window, &width, &height);
2736 /* size_allocate hasn't been called yet. These are the default values.
2738 if (width == 1 || height == 1)
2741 pixbuf = icon_info->pixbuf;
2742 g_object_ref (pixbuf);
2744 if (gdk_pixbuf_get_height (pixbuf) > height)
2746 GdkPixbuf *temp_pixbuf;
2749 scale = height - 2 * priv->icon_margin;
2750 temp_pixbuf = gdk_pixbuf_scale_simple (pixbuf, scale, scale,
2751 GDK_INTERP_BILINEAR);
2752 g_object_unref (pixbuf);
2753 pixbuf = temp_pixbuf;
2756 x = (width - gdk_pixbuf_get_width (pixbuf)) / 2;
2757 y = (height - gdk_pixbuf_get_height (pixbuf)) / 2;
2759 if (icon_info->insensitive)
2761 GdkPixbuf *temp_pixbuf;
2763 temp_pixbuf = gdk_pixbuf_copy (pixbuf);
2764 gdk_pixbuf_saturate_and_pixelate (pixbuf,
2768 g_object_unref (pixbuf);
2769 pixbuf = temp_pixbuf;
2771 else if (icon_info->prelight)
2773 GdkPixbuf *temp_pixbuf;
2775 temp_pixbuf = gdk_pixbuf_copy (pixbuf);
2776 colorshift_pixbuf (temp_pixbuf, pixbuf, 30);
2777 g_object_unref (pixbuf);
2778 pixbuf = temp_pixbuf;
2781 gdk_draw_pixbuf (icon_info->window, widget->style->black_gc, pixbuf,
2783 GDK_RGB_DITHER_NORMAL, 0, 0);
2785 g_object_unref (pixbuf);
2790 gtk_entry_draw_frame (GtkWidget *widget,
2793 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (widget);
2794 gint x = 0, y = 0, width, height;
2795 gboolean state_hint;
2798 gdk_drawable_get_size (widget->window, &width, &height);
2800 /* Fix a problem with some themes which assume that entry->text_area's
2801 * width equals widget->window's width */
2802 if (GTK_IS_SPIN_BUTTON (widget))
2804 gint xborder, yborder;
2806 gtk_entry_get_text_area_size (GTK_ENTRY (widget), &x, NULL, &width, NULL);
2807 _gtk_entry_get_borders (GTK_ENTRY (widget), &xborder, &yborder);
2810 width += xborder * 2;
2813 if (GTK_WIDGET_HAS_FOCUS (widget) && !priv->interior_focus)
2815 x += priv->focus_width;
2816 y += priv->focus_width;
2817 width -= 2 * priv->focus_width;
2818 height -= 2 * priv->focus_width;
2821 gtk_widget_style_get (widget, "state-hint", &state_hint, NULL);
2823 state = GTK_WIDGET_HAS_FOCUS (widget) ?
2824 GTK_STATE_ACTIVE : GTK_WIDGET_STATE (widget);
2826 state = GTK_STATE_NORMAL;
2828 gtk_paint_shadow (widget->style, widget->window,
2829 state, priv->shadow_type,
2830 area, widget, "entry", x, y, width, height);
2832 if (GTK_WIDGET_HAS_FOCUS (widget) && !priv->interior_focus)
2834 x -= priv->focus_width;
2835 y -= priv->focus_width;
2836 width += 2 * priv->focus_width;
2837 height += 2 * priv->focus_width;
2839 gtk_paint_focus (widget->style, widget->window, GTK_WIDGET_STATE (widget),
2840 area, widget, "entry",
2841 0, 0, width, height);
2846 gtk_entry_draw_progress (GtkWidget *widget,
2847 GdkEventExpose *event)
2849 GtkEntryPrivate *private = GTK_ENTRY_GET_PRIVATE (widget);
2850 GtkEntry *entry = GTK_ENTRY (widget);
2852 if (private->progress_pulse_mode)
2854 gdouble value = private->progress_pulse_current;
2855 gint area_width, area_height;
2857 gdk_drawable_get_size (entry->text_area, &area_width, &area_height);
2859 gtk_paint_box (widget->style, entry->text_area,
2860 GTK_STATE_SELECTED, GTK_SHADOW_OUT,
2861 &event->area, widget, "entry-progress",
2862 value * area_width, 0,
2863 private->progress_pulse_fraction * area_width, area_height);
2865 else if (private->progress_fraction > 0)
2867 gdouble value = private->progress_fraction;
2868 gint area_width, area_height;
2870 gdk_drawable_get_size (entry->text_area, &area_width, &area_height);
2872 if (gtk_widget_get_direction (GTK_WIDGET (entry)) == GTK_TEXT_DIR_RTL)
2874 gtk_paint_box (widget->style, entry->text_area,
2875 GTK_STATE_SELECTED, GTK_SHADOW_OUT,
2876 &event->area, widget, "entry-progress",
2877 area_width - value * area_width, 0,
2878 value * area_width, area_height);
2882 gtk_paint_box (widget->style, entry->text_area,
2883 GTK_STATE_SELECTED, GTK_SHADOW_OUT,
2884 &event->area, widget, "entry-progress",
2886 value * area_width, area_height);
2892 gtk_entry_expose (GtkWidget *widget,
2893 GdkEventExpose *event)
2895 GtkEntry *entry = GTK_ENTRY (widget);
2896 gboolean state_hint;
2898 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
2900 if (widget->window == event->window)
2902 gtk_entry_draw_frame (widget, &event->area);
2904 else if (entry->text_area == event->window)
2906 gint area_width, area_height;
2907 gdk_drawable_get_size (entry->text_area, &area_width, &area_height);
2909 gtk_widget_style_get (widget, "state-hint", &state_hint, NULL);
2911 state = GTK_WIDGET_HAS_FOCUS (widget) ?
2912 GTK_STATE_ACTIVE : GTK_WIDGET_STATE (widget);
2914 state = GTK_WIDGET_STATE(widget);
2916 gtk_paint_flat_box (widget->style, entry->text_area,
2917 state, GTK_SHADOW_NONE,
2918 &event->area, widget, "entry_bg",
2919 0, 0, area_width, area_height);
2921 gtk_entry_draw_progress (widget, event);
2923 if (entry->dnd_position != -1)
2924 gtk_entry_draw_cursor (GTK_ENTRY (widget), CURSOR_DND);
2926 gtk_entry_draw_text (GTK_ENTRY (widget));
2928 if ((entry->visible || entry->invisible_char != 0) &&
2929 GTK_WIDGET_HAS_FOCUS (widget) &&
2930 entry->selection_bound == entry->current_pos && entry->cursor_visible)
2931 gtk_entry_draw_cursor (GTK_ENTRY (widget), CURSOR_STANDARD);
2935 gboolean found = FALSE;
2938 for (i = 0; i < MAX_ICONS && !found; i++)
2940 EntryIconInfo *icon_info = NULL;
2942 if ((icon_info = priv->icons[i]) != NULL)
2944 if (event->window == icon_info->window)
2947 GtkAllocation text_area_alloc;
2949 get_text_area_size (entry, &text_area_alloc);
2950 gdk_drawable_get_size (icon_info->window, &width, NULL);
2952 gtk_paint_flat_box (widget->style, icon_info->window,
2953 GTK_WIDGET_STATE (widget), GTK_SHADOW_NONE,
2954 NULL, widget, "entry_bg",
2955 0, 0, width, text_area_alloc.height);
2957 draw_icon (widget, i);
2969 gtk_entry_enter_notify (GtkWidget *widget,
2970 GdkEventCrossing *event)
2972 GtkEntry *entry = GTK_ENTRY (widget);
2973 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
2974 EntryIconInfo *icon_info = NULL;
2977 for (i = 0; i < MAX_ICONS; i++)
2979 if ((icon_info = priv->icons[i]) != NULL)
2981 if (event->window == icon_info->window)
2983 if (should_prelight (entry, i))
2985 icon_info->prelight = TRUE;
2986 gtk_widget_queue_draw (widget);
2997 gtk_entry_leave_notify (GtkWidget *widget,
2998 GdkEventCrossing *event)
3000 GtkEntry *entry = GTK_ENTRY (widget);
3001 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
3002 EntryIconInfo *icon_info = NULL;
3005 for (i = 0; i < MAX_ICONS; i++)
3007 if ((icon_info = priv->icons[i]) != NULL)
3009 if (event->window == icon_info->window)
3011 if (should_prelight (entry, i))
3013 icon_info->prelight = FALSE;
3014 gtk_widget_queue_draw (widget);
3025 gtk_entry_get_pixel_ranges (GtkEntry *entry,
3029 gint start_char, end_char;
3031 if (gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &start_char, &end_char))
3033 PangoLayout *layout = gtk_entry_ensure_layout (entry, TRUE);
3034 PangoLayoutLine *line = pango_layout_get_lines_readonly (layout)->data;
3035 const char *text = pango_layout_get_text (layout);
3036 gint start_index = g_utf8_offset_to_pointer (text, start_char) - text;
3037 gint end_index = g_utf8_offset_to_pointer (text, end_char) - text;
3038 gint real_n_ranges, i;
3040 pango_layout_line_get_x_ranges (line, start_index, end_index, ranges, &real_n_ranges);
3046 for (i = 0; i < real_n_ranges; ++i)
3048 r[2 * i + 1] = (r[2 * i + 1] - r[2 * i]) / PANGO_SCALE;
3049 r[2 * i] = r[2 * i] / PANGO_SCALE;
3054 *n_ranges = real_n_ranges;
3066 in_selection (GtkEntry *entry,
3071 gint retval = FALSE;
3073 gtk_entry_get_pixel_ranges (entry, &ranges, &n_ranges);
3075 for (i = 0; i < n_ranges; ++i)
3077 if (x >= ranges[2 * i] && x < ranges[2 * i] + ranges[2 * i + 1])
3089 gtk_entry_button_press (GtkWidget *widget,
3090 GdkEventButton *event)
3092 GtkEntry *entry = GTK_ENTRY (widget);
3093 GtkEditable *editable = GTK_EDITABLE (widget);
3094 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
3095 EntryIconInfo *icon_info = NULL;
3097 gint sel_start, sel_end;
3100 for (i = 0; i < MAX_ICONS; i++)
3102 icon_info = priv->icons[i];
3104 if (!icon_info || icon_info->insensitive)
3107 if (event->window == icon_info->window)
3109 if (should_prelight (entry, i))
3111 icon_info->prelight = FALSE;
3112 gtk_widget_queue_draw (widget);
3115 if (icon_info->target_list != NULL)
3117 priv->start_x = event->x;
3118 priv->start_y = event->y;
3120 icon_info->pressed = TRUE;
3123 g_signal_emit (entry, signals[ICON_PRESSED], 0, i, event);
3129 if (event->window != entry->text_area ||
3130 (entry->button && event->button != entry->button))
3133 gtk_entry_reset_blink_time (entry);
3135 entry->button = event->button;
3137 if (!GTK_WIDGET_HAS_FOCUS (widget))
3139 entry->in_click = TRUE;
3140 gtk_widget_grab_focus (widget);
3141 entry->in_click = FALSE;
3144 tmp_pos = gtk_entry_find_position (entry, event->x + entry->scroll_offset);
3146 if (event->button == 1)
3148 gboolean have_selection = gtk_editable_get_selection_bounds (editable, &sel_start, &sel_end);
3150 entry->select_words = FALSE;
3151 entry->select_lines = FALSE;
3153 if (event->state & GDK_SHIFT_MASK)
3155 _gtk_entry_reset_im_context (entry);
3157 if (!have_selection) /* select from the current position to the clicked position */
3158 sel_start = sel_end = entry->current_pos;
3160 if (tmp_pos > sel_start && tmp_pos < sel_end)
3162 /* Truncate current selection, but keep it as big as possible */
3163 if (tmp_pos - sel_start > sel_end - tmp_pos)
3164 gtk_entry_set_positions (entry, sel_start, tmp_pos);
3166 gtk_entry_set_positions (entry, tmp_pos, sel_end);
3170 gboolean extend_to_left;
3173 /* Figure out what click selects and extend current selection */
3174 switch (event->type)
3176 case GDK_BUTTON_PRESS:
3177 gtk_entry_set_positions (entry, tmp_pos, tmp_pos);
3180 case GDK_2BUTTON_PRESS:
3181 entry->select_words = TRUE;
3182 gtk_entry_select_word (entry);
3185 case GDK_3BUTTON_PRESS:
3186 entry->select_lines = TRUE;
3187 gtk_entry_select_line (entry);
3194 start = MIN (entry->current_pos, entry->selection_bound);
3195 start = MIN (sel_start, start);
3197 end = MAX (entry->current_pos, entry->selection_bound);
3198 end = MAX (sel_end, end);
3200 if (tmp_pos == sel_start || tmp_pos == sel_end)
3201 extend_to_left = (tmp_pos == start);
3203 extend_to_left = (end == sel_end);
3206 gtk_entry_set_positions (entry, start, end);
3208 gtk_entry_set_positions (entry, end, start);
3211 else /* no shift key */
3212 switch (event->type)
3214 case GDK_BUTTON_PRESS:
3215 if (in_selection (entry, event->x + entry->scroll_offset))
3217 /* Click inside the selection - we'll either start a drag, or
3218 * clear the selection
3220 entry->in_drag = TRUE;
3221 entry->drag_start_x = event->x + entry->scroll_offset;
3222 entry->drag_start_y = event->y;
3225 gtk_editable_set_position (editable, tmp_pos);
3228 case GDK_2BUTTON_PRESS:
3229 /* We ALWAYS receive a GDK_BUTTON_PRESS immediately before
3230 * receiving a GDK_2BUTTON_PRESS so we need to reset
3231 * entry->in_drag which may have been set above
3233 entry->in_drag = FALSE;
3234 entry->select_words = TRUE;
3235 gtk_entry_select_word (entry);
3238 case GDK_3BUTTON_PRESS:
3239 /* We ALWAYS receive a GDK_BUTTON_PRESS immediately before
3240 * receiving a GDK_3BUTTON_PRESS so we need to reset
3241 * entry->in_drag which may have been set above
3243 entry->in_drag = FALSE;
3244 entry->select_lines = TRUE;
3245 gtk_entry_select_line (entry);
3254 else if (event->button == 2 && event->type == GDK_BUTTON_PRESS)
3256 if (entry->editable)
3258 priv->insert_pos = tmp_pos;
3259 gtk_entry_paste (entry, GDK_SELECTION_PRIMARY);
3264 gtk_widget_error_bell (widget);
3267 else if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
3269 gtk_entry_do_popup (entry, event);
3270 entry->button = 0; /* Don't wait for release, since the menu will gtk_grab_add */
3279 gtk_entry_button_release (GtkWidget *widget,
3280 GdkEventButton *event)
3282 GtkEntry *entry = GTK_ENTRY (widget);
3283 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (widget);
3284 EntryIconInfo *icon_info = NULL;
3287 for (i = 0; i < MAX_ICONS; i++)
3289 if ((icon_info = priv->icons[i]) != NULL)
3291 GdkWindow *icon_window = icon_info->window;
3293 if (icon_info->insensitive)
3296 if (event->window == icon_window)
3300 gdk_drawable_get_size (icon_window, &width, &height);
3302 icon_info->pressed = FALSE;
3304 if (should_prelight (entry, i) &&
3305 event->x >= 0 && event->y >= 0 &&
3306 event->x < width && event->y < height)
3308 icon_info->prelight = TRUE;
3309 gtk_widget_queue_draw (widget);
3312 g_signal_emit (entry, signals[ICON_RELEASED], 0, i, event);
3319 if (event->window != entry->text_area || entry->button != event->button)
3324 gint tmp_pos = gtk_entry_find_position (entry, entry->drag_start_x);
3326 gtk_editable_set_position (GTK_EDITABLE (entry), tmp_pos);
3333 gtk_entry_update_primary_selection (entry);
3339 _gtk_entry_get_selected_text (GtkEntry *entry)
3341 GtkEditable *editable = GTK_EDITABLE (entry);
3342 gint start_text, end_text;
3345 if (gtk_editable_get_selection_bounds (editable, &start_text, &end_text))
3346 text = gtk_editable_get_chars (editable, start_text, end_text);
3352 gtk_entry_motion_notify (GtkWidget *widget,
3353 GdkEventMotion *event)
3355 GtkEntry *entry = GTK_ENTRY (widget);
3356 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
3357 EntryIconInfo *icon_info = NULL;
3358 GdkDragContext *context;
3362 for (i = 0; i < MAX_ICONS; i++)
3364 icon_info = priv->icons[i];
3366 if (!icon_info || icon_info->insensitive)
3369 if (event->window == icon_info->window)
3371 if (icon_info->pressed &&
3372 icon_info->target_list != NULL &&
3373 gtk_drag_check_threshold (widget,
3379 icon_info->in_drag = TRUE;
3380 icon_info->pressed = FALSE;
3381 context = gtk_drag_begin (widget,
3382 icon_info->target_list,
3392 if (entry->mouse_cursor_obscured)
3396 cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget), GDK_XTERM);
3397 gdk_window_set_cursor (entry->text_area, cursor);
3398 gdk_cursor_unref (cursor);
3399 entry->mouse_cursor_obscured = FALSE;
3402 if (event->window != entry->text_area || entry->button != 1)
3405 if (entry->select_lines)
3408 gdk_event_request_motions (event);
3412 if (entry->visible &&
3413 gtk_drag_check_threshold (widget,
3414 entry->drag_start_x, entry->drag_start_y,
3415 event->x + entry->scroll_offset, event->y))
3417 GdkDragContext *context;
3418 GtkTargetList *target_list = gtk_target_list_new (NULL, 0);
3419 guint actions = entry->editable ? GDK_ACTION_COPY | GDK_ACTION_MOVE : GDK_ACTION_COPY;
3421 GdkPixmap *pixmap = NULL;
3423 gtk_target_list_add_text_targets (target_list, 0);
3425 text = _gtk_entry_get_selected_text (entry);
3426 pixmap = _gtk_text_util_create_drag_icon (widget, text, -1);
3428 context = gtk_drag_begin (widget, target_list, actions,
3429 entry->button, (GdkEvent *)event);
3432 gtk_drag_set_icon_pixmap (context,
3433 gdk_drawable_get_colormap (pixmap),
3438 gtk_drag_set_icon_default (context);
3441 g_object_unref (pixmap);
3444 entry->in_drag = FALSE;
3447 gtk_target_list_unref (target_list);
3453 gdk_drawable_get_size (entry->text_area, NULL, &height);
3457 else if (event->y >= height)
3458 tmp_pos = entry->text_length;
3460 tmp_pos = gtk_entry_find_position (entry, event->x + entry->scroll_offset);
3462 if (entry->select_words)
3465 gint old_min, old_max;
3468 min = gtk_entry_move_backward_word (entry, tmp_pos, TRUE);
3469 max = gtk_entry_move_forward_word (entry, tmp_pos, TRUE);
3471 pos = entry->current_pos;
3472 bound = entry->selection_bound;
3474 old_min = MIN(entry->current_pos, entry->selection_bound);
3475 old_max = MAX(entry->current_pos, entry->selection_bound);
3482 else if (old_max < max)
3487 else if (pos == old_min)
3489 if (entry->current_pos != min)
3494 if (entry->current_pos != max)
3498 gtk_entry_set_positions (entry, pos, bound);
3501 gtk_entry_set_positions (entry, tmp_pos, -1);
3508 set_invisible_cursor (GdkWindow *window)
3510 GdkBitmap *empty_bitmap;
3513 char invisible_cursor_bits[] = { 0x0 };
3515 useless.red = useless.green = useless.blue = 0;
3518 empty_bitmap = gdk_bitmap_create_from_data (window,
3519 invisible_cursor_bits,
3522 cursor = gdk_cursor_new_from_pixmap (empty_bitmap,
3527 gdk_window_set_cursor (window, cursor);
3529 gdk_cursor_unref (cursor);
3531 g_object_unref (empty_bitmap);
3535 gtk_entry_obscure_mouse_cursor (GtkEntry *entry)
3537 if (entry->mouse_cursor_obscured)
3540 set_invisible_cursor (entry->text_area);
3542 entry->mouse_cursor_obscured = TRUE;
3546 gtk_entry_key_press (GtkWidget *widget,
3549 GtkEntry *entry = GTK_ENTRY (widget);
3551 gtk_entry_reset_blink_time (entry);
3552 gtk_entry_pend_cursor_blink (entry);
3554 if (entry->editable)
3556 if (gtk_im_context_filter_keypress (entry->im_context, event))
3558 gtk_entry_obscure_mouse_cursor (entry);
3559 entry->need_im_reset = TRUE;
3564 if (event->keyval == GDK_Return ||
3565 event->keyval == GDK_KP_Enter ||
3566 event->keyval == GDK_ISO_Enter ||
3567 event->keyval == GDK_Escape)
3569 GtkEntryCompletion *completion = gtk_entry_get_completion (entry);
3571 if (completion && completion->priv->completion_timeout)
3573 g_source_remove (completion->priv->completion_timeout);
3574 completion->priv->completion_timeout = 0;
3577 _gtk_entry_reset_im_context (entry);
3580 if (GTK_WIDGET_CLASS (gtk_entry_parent_class)->key_press_event (widget, event))
3581 /* Activate key bindings
3585 if (!entry->editable && event->length)
3586 gtk_widget_error_bell (widget);
3592 gtk_entry_key_release (GtkWidget *widget,
3595 GtkEntry *entry = GTK_ENTRY (widget);
3597 if (entry->editable)
3599 if (gtk_im_context_filter_keypress (entry->im_context, event))
3601 entry->need_im_reset = TRUE;
3606 return GTK_WIDGET_CLASS (gtk_entry_parent_class)->key_release_event (widget, event);
3610 gtk_entry_focus_in (GtkWidget *widget,
3611 GdkEventFocus *event)
3613 GtkEntry *entry = GTK_ENTRY (widget);
3616 gtk_widget_queue_draw (widget);
3618 keymap = gdk_keymap_get_for_display (gtk_widget_get_display (widget));
3620 if (entry->editable)
3622 entry->need_im_reset = TRUE;
3623 gtk_im_context_focus_in (entry->im_context);
3624 keymap_state_changed (keymap, entry);
3625 g_signal_connect (keymap, "state-changed",
3626 G_CALLBACK (keymap_state_changed), entry);
3629 g_signal_connect (keymap, "direction-changed",
3630 G_CALLBACK (keymap_direction_changed), entry);
3632 gtk_entry_reset_blink_time (entry);
3633 gtk_entry_check_cursor_blink (entry);
3639 gtk_entry_focus_out (GtkWidget *widget,
3640 GdkEventFocus *event)
3642 GtkEntry *entry = GTK_ENTRY (widget);
3643 GtkEntryCompletion *completion;
3646 gtk_widget_queue_draw (widget);
3648 keymap = gdk_keymap_get_for_display (gtk_widget_get_display (widget));
3650 if (entry->editable)
3652 entry->need_im_reset = TRUE;
3653 gtk_im_context_focus_out (entry->im_context);
3654 remove_capslock_feedback (entry);
3657 gtk_entry_check_cursor_blink (entry);
3659 g_signal_handlers_disconnect_by_func (keymap, keymap_state_changed, entry);
3660 g_signal_handlers_disconnect_by_func (keymap, keymap_direction_changed, entry);
3662 completion = gtk_entry_get_completion (entry);
3664 _gtk_entry_completion_popdown (completion);
3670 gtk_entry_grab_focus (GtkWidget *widget)
3672 GtkEntry *entry = GTK_ENTRY (widget);
3673 gboolean select_on_focus;
3675 GTK_WIDGET_CLASS (gtk_entry_parent_class)->grab_focus (widget);
3677 if (entry->editable && !entry->in_click)
3679 g_object_get (gtk_widget_get_settings (widget),
3680 "gtk-entry-select-on-focus",
3684 if (select_on_focus)
3685 gtk_editable_select_region (GTK_EDITABLE (widget), 0, -1);
3690 gtk_entry_direction_changed (GtkWidget *widget,
3691 GtkTextDirection previous_dir)
3693 GtkEntry *entry = GTK_ENTRY (widget);
3695 gtk_entry_recompute (entry);
3697 GTK_WIDGET_CLASS (gtk_entry_parent_class)->direction_changed (widget, previous_dir);
3701 gtk_entry_state_changed (GtkWidget *widget,
3702 GtkStateType previous_state)
3704 GtkEntry *entry = GTK_ENTRY (widget);
3707 if (GTK_WIDGET_REALIZED (widget))
3709 gdk_window_set_background (widget->window, &widget->style->base[GTK_WIDGET_STATE (widget)]);
3710 gdk_window_set_background (entry->text_area, &widget->style->base[GTK_WIDGET_STATE (widget)]);
3712 if (GTK_WIDGET_IS_SENSITIVE (widget))
3713 cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget), GDK_XTERM);
3717 gdk_window_set_cursor (entry->text_area, cursor);
3720 gdk_cursor_unref (cursor);
3722 entry->mouse_cursor_obscured = FALSE;
3725 if (!GTK_WIDGET_IS_SENSITIVE (widget))
3727 /* Clear any selection */
3728 gtk_editable_select_region (GTK_EDITABLE (entry), entry->current_pos, entry->current_pos);
3731 gtk_widget_queue_draw (widget);
3735 gtk_entry_screen_changed (GtkWidget *widget,
3736 GdkScreen *old_screen)
3738 gtk_entry_recompute (GTK_ENTRY (widget));
3741 /* GtkEditable method implementations
3744 gtk_entry_insert_text (GtkEditable *editable,
3745 const gchar *new_text,
3746 gint new_text_length,
3749 GtkEntry *entry = GTK_ENTRY (editable);
3753 if (*position < 0 || *position > entry->text_length)
3754 *position = entry->text_length;
3756 g_object_ref (editable);
3758 if (new_text_length <= 63)
3761 text = g_new (gchar, new_text_length + 1);
3763 text[new_text_length] = '\0';
3764 strncpy (text, new_text, new_text_length);
3766 g_signal_emit_by_name (editable, "insert-text", text, new_text_length, position);
3768 if (!entry->visible)
3769 trash_area (text, new_text_length);
3771 if (new_text_length > 63)
3774 g_object_unref (editable);
3778 gtk_entry_delete_text (GtkEditable *editable,
3782 GtkEntry *entry = GTK_ENTRY (editable);
3784 if (end_pos < 0 || end_pos > entry->text_length)
3785 end_pos = entry->text_length;
3788 if (start_pos > end_pos)
3789 start_pos = end_pos;
3791 g_object_ref (editable);
3793 g_signal_emit_by_name (editable, "delete-text", start_pos, end_pos);
3795 g_object_unref (editable);
3799 gtk_entry_get_chars (GtkEditable *editable,
3803 GtkEntry *entry = GTK_ENTRY (editable);
3804 gint start_index, end_index;
3807 end_pos = entry->text_length;
3809 start_pos = MIN (entry->text_length, start_pos);
3810 end_pos = MIN (entry->text_length, end_pos);
3812 start_index = g_utf8_offset_to_pointer (entry->text, start_pos) - entry->text;
3813 end_index = g_utf8_offset_to_pointer (entry->text, end_pos) - entry->text;
3815 return g_strndup (entry->text + start_index, end_index - start_index);
3819 gtk_entry_real_set_position (GtkEditable *editable,
3822 GtkEntry *entry = GTK_ENTRY (editable);
3824 if (position < 0 || position > entry->text_length)
3825 position = entry->text_length;
3827 if (position != entry->current_pos ||
3828 position != entry->selection_bound)
3830 _gtk_entry_reset_im_context (entry);
3831 gtk_entry_set_positions (entry, position, position);
3836 gtk_entry_get_position (GtkEditable *editable)
3838 return GTK_ENTRY (editable)->current_pos;
3842 gtk_entry_set_selection_bounds (GtkEditable *editable,
3846 GtkEntry *entry = GTK_ENTRY (editable);
3849 start = entry->text_length;
3851 end = entry->text_length;
3853 _gtk_entry_reset_im_context (entry);
3855 gtk_entry_set_positions (entry,
3856 MIN (end, entry->text_length),
3857 MIN (start, entry->text_length));
3859 gtk_entry_update_primary_selection (entry);
3863 gtk_entry_get_selection_bounds (GtkEditable *editable,
3867 GtkEntry *entry = GTK_ENTRY (editable);
3869 *start = entry->selection_bound;
3870 *end = entry->current_pos;
3872 return (entry->selection_bound != entry->current_pos);
3876 icon_theme_changed (GtkEntry *entry)
3878 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
3879 EntryIconInfo *icon_info = NULL;
3882 for (i = 0; i < MAX_ICONS; i++)
3884 if ((icon_info = priv->icons[i]) != NULL)
3886 if (icon_info->storage_type == GTK_IMAGE_ICON_NAME)
3887 gtk_entry_set_icon_from_icon_name (entry, i, icon_info->icon_name);
3888 else if (icon_info->storage_type == GTK_IMAGE_STOCK)
3889 gtk_entry_set_icon_from_stock (entry, i, icon_info->stock_id);
3890 else if (icon_info->storage_type == GTK_IMAGE_GICON)
3891 gtk_entry_set_icon_from_gicon (entry, i, icon_info->gicon);
3895 gtk_widget_queue_draw (GTK_WIDGET (entry));
3899 icon_margin_changed (GtkEntry *entry)
3901 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
3904 _gtk_entry_effective_inner_border (GTK_ENTRY (entry), &border);
3906 priv->icon_margin = border.left;
3910 gtk_entry_style_set (GtkWidget *widget,
3911 GtkStyle *previous_style)
3913 GtkEntry *entry = GTK_ENTRY (widget);
3914 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
3916 gboolean interior_focus;
3918 gtk_widget_style_get (widget,
3919 "focus-line-width", &focus_width,
3920 "interior-focus", &interior_focus,
3923 priv->focus_width = focus_width;
3924 priv->interior_focus = interior_focus;
3926 if (!priv->invisible_char_set)
3927 entry->invisible_char = find_invisible_char (GTK_WIDGET (entry));
3929 gtk_entry_recompute (entry);
3931 if (previous_style && GTK_WIDGET_REALIZED (widget))
3933 gdk_window_set_background (widget->window, &widget->style->base[GTK_WIDGET_STATE (widget)]);
3934 gdk_window_set_background (entry->text_area, &widget->style->base[GTK_WIDGET_STATE (widget)]);
3937 icon_theme_changed (entry);
3938 icon_margin_changed (entry);
3941 /* GtkCellEditable method implementations
3944 gtk_cell_editable_entry_activated (GtkEntry *entry, gpointer data)
3946 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (entry));
3947 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (entry));
3951 gtk_cell_editable_key_press_event (GtkEntry *entry,
3952 GdkEventKey *key_event,
3955 if (key_event->keyval == GDK_Escape)
3957 entry->editing_canceled = TRUE;
3958 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (entry));
3959 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (entry));
3964 /* override focus */
3965 if (key_event->keyval == GDK_Up || key_event->keyval == GDK_Down)
3967 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (entry));
3968 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (entry));
3977 gtk_entry_start_editing (GtkCellEditable *cell_editable,
3980 GTK_ENTRY (cell_editable)->is_cell_renderer = TRUE;
3982 g_signal_connect (cell_editable, "activate",
3983 G_CALLBACK (gtk_cell_editable_entry_activated), NULL);
3984 g_signal_connect (cell_editable, "key-press-event",
3985 G_CALLBACK (gtk_cell_editable_key_press_event), NULL);
3989 gtk_entry_password_hint_free (GtkEntryPasswordHint *password_hint)
3991 if (password_hint->password_hint_timeout_id)
3992 g_source_remove (password_hint->password_hint_timeout_id);
3994 g_slice_free (GtkEntryPasswordHint, password_hint);
3997 /* Default signal handlers
4000 gtk_entry_real_insert_text (GtkEditable *editable,
4001 const gchar *new_text,
4002 gint new_text_length,
4005 GtkEntry *entry = GTK_ENTRY (editable);
4009 if (new_text_length < 0)
4010 new_text_length = strlen (new_text);
4012 n_chars = g_utf8_strlen (new_text, new_text_length);
4013 if (entry->text_max_length > 0 && n_chars + entry->text_length > entry->text_max_length)
4015 gtk_widget_error_bell (GTK_WIDGET (entry));
4016 n_chars = entry->text_max_length - entry->text_length;
4017 new_text_length = g_utf8_offset_to_pointer (new_text, n_chars) - new_text;
4020 if (new_text_length + entry->n_bytes + 1 > entry->text_size)
4022 gsize prev_size = entry->text_size;
4024 while (new_text_length + entry->n_bytes + 1 > entry->text_size)
4026 if (entry->text_size == 0)
4027 entry->text_size = MIN_SIZE;
4030 if (2 * (guint)entry->text_size < MAX_SIZE &&
4031 2 * (guint)entry->text_size > entry->text_size)
4032 entry->text_size *= 2;
4035 entry->text_size = MAX_SIZE;
4036 if (new_text_length > (gint)entry->text_size - (gint)entry->n_bytes - 1)
4038 new_text_length = (gint)entry->text_size - (gint)entry->n_bytes - 1;
4039 new_text_length = g_utf8_find_prev_char (new_text, new_text + new_text_length + 1) - new_text;
4040 n_chars = g_utf8_strlen (new_text, new_text_length);
4048 entry->text = g_realloc (entry->text, entry->text_size);
4051 /* Same thing, just slower and without leaving stuff in memory. */
4052 gchar *et_new = g_malloc (entry->text_size);
4053 memcpy (et_new, entry->text, MIN (prev_size, entry->text_size));
4054 trash_area (entry->text, prev_size);
4055 g_free (entry->text);
4056 entry->text = et_new;
4060 index = g_utf8_offset_to_pointer (entry->text, *position) - entry->text;
4062 g_memmove (entry->text + index + new_text_length, entry->text + index, entry->n_bytes - index);
4063 memcpy (entry->text + index, new_text, new_text_length);
4065 entry->n_bytes += new_text_length;
4066 entry->text_length += n_chars;
4068 /* NUL terminate for safety and convenience */
4069 entry->text[entry->n_bytes] = '\0';
4071 if (entry->current_pos > *position)
4072 entry->current_pos += n_chars;
4074 if (entry->selection_bound > *position)
4075 entry->selection_bound += n_chars;
4077 if (n_chars == 1 && !entry->visible && (new_text_length < PASSWORD_HINT_MAX))
4079 guint password_hint_timeout;
4081 g_object_get (gtk_widget_get_settings (GTK_WIDGET (entry)),
4082 "gtk-entry-password-hint-timeout", &password_hint_timeout,
4085 if (password_hint_timeout > 0)
4087 GtkEntryPasswordHint *password_hint = g_object_get_qdata (G_OBJECT (entry),
4088 quark_password_hint);
4092 password_hint = g_slice_new0 (GtkEntryPasswordHint);
4093 g_object_set_qdata_full (G_OBJECT (entry), quark_password_hint,
4095 (GDestroyNotify) gtk_entry_password_hint_free);
4098 memset (&password_hint->password_hint, 0x0, PASSWORD_HINT_MAX);
4099 password_hint->password_hint_length = new_text_length;
4100 memcpy (&password_hint->password_hint, new_text, new_text_length);
4101 password_hint->password_hint_position = *position + n_chars;
4106 g_object_set_qdata (G_OBJECT (entry), quark_password_hint, NULL);
4109 *position += n_chars;
4111 gtk_entry_recompute (entry);
4113 emit_changed (entry);
4114 g_object_notify (G_OBJECT (editable), "text");
4115 g_object_notify (G_OBJECT (editable), "text-length");
4119 gtk_entry_real_delete_text (GtkEditable *editable,
4123 GtkEntry *entry = GTK_ENTRY (editable);
4127 if (end_pos < 0 || end_pos > entry->text_length)
4128 end_pos = entry->text_length;
4130 if (start_pos < end_pos)
4132 gint start_index = g_utf8_offset_to_pointer (entry->text, start_pos) - entry->text;
4133 gint end_index = g_utf8_offset_to_pointer (entry->text, end_pos) - entry->text;
4135 gint selection_bound;
4137 g_memmove (entry->text + start_index, entry->text + end_index, entry->n_bytes + 1 - end_index);
4138 entry->text_length -= (end_pos - start_pos);
4139 entry->n_bytes -= (end_index - start_index);
4141 /* In password-mode, make sure we don't leave anything sensitive after
4142 * the terminating zero. Note, that the terminating zero already trashed
4145 if (!entry->visible)
4146 trash_area (entry->text + entry->n_bytes + 1, end_index - start_index - 1);
4148 current_pos = entry->current_pos;
4149 if (current_pos > start_pos)
4150 current_pos -= MIN (current_pos, end_pos) - start_pos;
4152 selection_bound = entry->selection_bound;
4153 if (selection_bound > start_pos)
4154 selection_bound -= MIN (selection_bound, end_pos) - start_pos;
4156 gtk_entry_set_positions (entry, current_pos, selection_bound);
4158 /* We might have deleted the selection
4160 gtk_entry_update_primary_selection (entry);
4162 gtk_entry_recompute (entry);
4164 emit_changed (entry);
4165 g_object_notify (G_OBJECT (editable), "text");
4166 g_object_notify (G_OBJECT (editable), "text-length");
4170 /* Compute the X position for an offset that corresponds to the "more important
4171 * cursor position for that offset. We use this when trying to guess to which
4172 * end of the selection we should go to when the user hits the left or
4176 get_better_cursor_x (GtkEntry *entry,
4179 GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (entry)));
4180 PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
4181 gboolean split_cursor;
4183 PangoLayout *layout = gtk_entry_ensure_layout (entry, TRUE);
4184 const gchar *text = pango_layout_get_text (layout);
4185 gint index = g_utf8_offset_to_pointer (text, offset) - text;
4187 PangoRectangle strong_pos, weak_pos;
4189 g_object_get (gtk_widget_get_settings (GTK_WIDGET (entry)),
4190 "gtk-split-cursor", &split_cursor,
4193 pango_layout_get_cursor_pos (layout, index, &strong_pos, &weak_pos);
4196 return strong_pos.x / PANGO_SCALE;
4198 return (keymap_direction == entry->resolved_dir) ? strong_pos.x / PANGO_SCALE : weak_pos.x / PANGO_SCALE;
4202 gtk_entry_move_cursor (GtkEntry *entry,
4203 GtkMovementStep step,
4205 gboolean extend_selection)
4207 gint new_pos = entry->current_pos;
4209 _gtk_entry_reset_im_context (entry);
4211 if (entry->current_pos != entry->selection_bound && !extend_selection)
4213 /* If we have a current selection and aren't extending it, move to the
4214 * start/or end of the selection as appropriate
4218 case GTK_MOVEMENT_VISUAL_POSITIONS:
4220 gint current_x = get_better_cursor_x (entry, entry->current_pos);
4221 gint bound_x = get_better_cursor_x (entry, entry->selection_bound);
4224 new_pos = current_x < bound_x ? entry->current_pos : entry->selection_bound;
4226 new_pos = current_x > bound_x ? entry->current_pos : entry->selection_bound;
4229 case GTK_MOVEMENT_LOGICAL_POSITIONS:
4230 case GTK_MOVEMENT_WORDS:
4232 new_pos = MIN (entry->current_pos, entry->selection_bound);
4234 new_pos = MAX (entry->current_pos, entry->selection_bound);
4236 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
4237 case GTK_MOVEMENT_PARAGRAPH_ENDS:
4238 case GTK_MOVEMENT_BUFFER_ENDS:
4239 new_pos = count < 0 ? 0 : entry->text_length;
4241 case GTK_MOVEMENT_DISPLAY_LINES:
4242 case GTK_MOVEMENT_PARAGRAPHS:
4243 case GTK_MOVEMENT_PAGES:
4244 case GTK_MOVEMENT_HORIZONTAL_PAGES:
4252 case GTK_MOVEMENT_LOGICAL_POSITIONS:
4253 new_pos = gtk_entry_move_logically (entry, new_pos, count);
4255 case GTK_MOVEMENT_VISUAL_POSITIONS:
4256 new_pos = gtk_entry_move_visually (entry, new_pos, count);
4257 if (entry->current_pos == new_pos)
4259 if (!extend_selection)
4261 if (!gtk_widget_keynav_failed (GTK_WIDGET (entry),
4263 GTK_DIR_RIGHT : GTK_DIR_LEFT))
4265 GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (entry));
4268 gtk_widget_child_focus (toplevel,
4270 GTK_DIR_RIGHT : GTK_DIR_LEFT);
4275 gtk_widget_error_bell (GTK_WIDGET (entry));
4279 case GTK_MOVEMENT_WORDS:
4282 new_pos = gtk_entry_move_forward_word (entry, new_pos, FALSE);
4287 new_pos = gtk_entry_move_backward_word (entry, new_pos, FALSE);
4290 if (entry->current_pos == new_pos)
4291 gtk_widget_error_bell (GTK_WIDGET (entry));
4293 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
4294 case GTK_MOVEMENT_PARAGRAPH_ENDS:
4295 case GTK_MOVEMENT_BUFFER_ENDS:
4296 new_pos = count < 0 ? 0 : entry->text_length;
4297 if (entry->current_pos == new_pos)
4298 gtk_widget_error_bell (GTK_WIDGET (entry));
4300 case GTK_MOVEMENT_DISPLAY_LINES:
4301 case GTK_MOVEMENT_PARAGRAPHS:
4302 case GTK_MOVEMENT_PAGES:
4303 case GTK_MOVEMENT_HORIZONTAL_PAGES:
4308 if (extend_selection)
4309 gtk_editable_select_region (GTK_EDITABLE (entry), entry->selection_bound, new_pos);
4311 gtk_editable_set_position (GTK_EDITABLE (entry), new_pos);
4313 gtk_entry_pend_cursor_blink (entry);
4317 gtk_entry_insert_at_cursor (GtkEntry *entry,
4320 GtkEditable *editable = GTK_EDITABLE (entry);
4321 gint pos = entry->current_pos;
4323 if (entry->editable)
4325 _gtk_entry_reset_im_context (entry);
4327 gtk_editable_insert_text (editable, str, -1, &pos);
4328 gtk_editable_set_position (editable, pos);
4333 gtk_entry_delete_from_cursor (GtkEntry *entry,
4337 GtkEditable *editable = GTK_EDITABLE (entry);
4338 gint start_pos = entry->current_pos;
4339 gint end_pos = entry->current_pos;
4340 gint old_n_bytes = entry->n_bytes;
4342 _gtk_entry_reset_im_context (entry);
4344 if (!entry->editable)
4346 gtk_widget_error_bell (GTK_WIDGET (entry));
4350 if (entry->selection_bound != entry->current_pos)
4352 gtk_editable_delete_selection (editable);
4358 case GTK_DELETE_CHARS:
4359 end_pos = gtk_entry_move_logically (entry, entry->current_pos, count);
4360 gtk_editable_delete_text (editable, MIN (start_pos, end_pos), MAX (start_pos, end_pos));
4362 case GTK_DELETE_WORDS:
4365 /* Move to end of current word, or if not on a word, end of previous word */
4366 end_pos = gtk_entry_move_backward_word (entry, end_pos, FALSE);
4367 end_pos = gtk_entry_move_forward_word (entry, end_pos, FALSE);
4371 /* Move to beginning of current word, or if not on a word, begining of next word */
4372 start_pos = gtk_entry_move_forward_word (entry, start_pos, FALSE);
4373 start_pos = gtk_entry_move_backward_word (entry, start_pos, FALSE);
4377 case GTK_DELETE_WORD_ENDS:
4380 start_pos = gtk_entry_move_backward_word (entry, start_pos, FALSE);
4385 end_pos = gtk_entry_move_forward_word (entry, end_pos, FALSE);
4388 gtk_editable_delete_text (editable, start_pos, end_pos);
4390 case GTK_DELETE_DISPLAY_LINE_ENDS:
4391 case GTK_DELETE_PARAGRAPH_ENDS:
4393 gtk_editable_delete_text (editable, 0, entry->current_pos);
4395 gtk_editable_delete_text (editable, entry->current_pos, -1);
4397 case GTK_DELETE_DISPLAY_LINES:
4398 case GTK_DELETE_PARAGRAPHS:
4399 gtk_editable_delete_text (editable, 0, -1);
4401 case GTK_DELETE_WHITESPACE:
4402 gtk_entry_delete_whitespace (entry);
4406 if (entry->n_bytes == old_n_bytes)
4407 gtk_widget_error_bell (GTK_WIDGET (entry));
4409 gtk_entry_pend_cursor_blink (entry);
4413 gtk_entry_backspace (GtkEntry *entry)
4415 GtkEditable *editable = GTK_EDITABLE (entry);
4418 _gtk_entry_reset_im_context (entry);
4420 if (!entry->editable || !entry->text)
4422 gtk_widget_error_bell (GTK_WIDGET (entry));
4426 if (entry->selection_bound != entry->current_pos)
4428 gtk_editable_delete_selection (editable);
4432 prev_pos = gtk_entry_move_logically (entry, entry->current_pos, -1);
4434 if (prev_pos < entry->current_pos)
4436 PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
4437 PangoLogAttr *log_attrs;
4440 pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
4442 if (entry->visible &&
4443 log_attrs[entry->current_pos].backspace_deletes_character)
4445 gchar *cluster_text;
4446 gchar *normalized_text;
4449 cluster_text = gtk_editable_get_chars (editable,
4451 entry->current_pos);
4452 normalized_text = g_utf8_normalize (cluster_text,
4453 strlen (cluster_text),
4455 len = g_utf8_strlen (normalized_text, -1);
4457 gtk_editable_delete_text (editable, prev_pos, entry->current_pos);
4460 gint pos = entry->current_pos;
4462 gtk_editable_insert_text (editable, normalized_text,
4463 g_utf8_offset_to_pointer (normalized_text, len - 1) - normalized_text,
4465 gtk_editable_set_position (editable, pos);
4468 g_free (normalized_text);
4469 g_free (cluster_text);
4473 gtk_editable_delete_text (editable, prev_pos, entry->current_pos);
4480 gtk_widget_error_bell (GTK_WIDGET (entry));
4483 gtk_entry_pend_cursor_blink (entry);
4487 gtk_entry_copy_clipboard (GtkEntry *entry)
4489 GtkEditable *editable = GTK_EDITABLE (entry);
4493 if (gtk_editable_get_selection_bounds (editable, &start, &end))
4495 if (!entry->visible)
4497 gtk_widget_error_bell (GTK_WIDGET (entry));
4501 str = gtk_entry_get_public_chars (entry, start, end);
4502 gtk_clipboard_set_text (gtk_widget_get_clipboard (GTK_WIDGET (entry),
4503 GDK_SELECTION_CLIPBOARD),
4510 gtk_entry_cut_clipboard (GtkEntry *entry)
4512 GtkEditable *editable = GTK_EDITABLE (entry);
4515 if (!entry->visible)
4517 gtk_widget_error_bell (GTK_WIDGET (entry));
4521 gtk_entry_copy_clipboard (entry);
4523 if (entry->editable)
4525 if (gtk_editable_get_selection_bounds (editable, &start, &end))
4526 gtk_editable_delete_text (editable, start, end);
4530 gtk_widget_error_bell (GTK_WIDGET (entry));
4535 gtk_entry_paste_clipboard (GtkEntry *entry)
4537 if (entry->editable)
4538 gtk_entry_paste (entry, GDK_NONE);
4540 gtk_widget_error_bell (GTK_WIDGET (entry));
4544 gtk_entry_delete_cb (GtkEntry *entry)
4546 GtkEditable *editable = GTK_EDITABLE (entry);
4549 if (entry->editable)
4551 if (gtk_editable_get_selection_bounds (editable, &start, &end))
4552 gtk_editable_delete_text (editable, start, end);
4557 gtk_entry_toggle_overwrite (GtkEntry *entry)
4559 entry->overwrite_mode = !entry->overwrite_mode;
4560 gtk_entry_pend_cursor_blink (entry);
4561 gtk_widget_queue_draw (GTK_WIDGET (entry));
4565 gtk_entry_select_all (GtkEntry *entry)
4567 gtk_entry_select_line (entry);
4571 gtk_entry_real_activate (GtkEntry *entry)
4574 GtkWidget *toplevel;
4577 widget = GTK_WIDGET (entry);
4579 if (entry->activates_default)
4581 toplevel = gtk_widget_get_toplevel (widget);
4582 if (GTK_IS_WINDOW (toplevel))
4584 window = GTK_WINDOW (toplevel);
4587 widget != window->default_widget &&
4588 !(widget == window->focus_widget &&
4589 (!window->default_widget || !GTK_WIDGET_SENSITIVE (window->default_widget))))
4590 gtk_window_activate_default (window);
4596 keymap_direction_changed (GdkKeymap *keymap,
4599 gtk_entry_recompute (entry);
4602 /* IM Context Callbacks
4606 gtk_entry_commit_cb (GtkIMContext *context,
4610 if (entry->editable)
4611 gtk_entry_enter_text (entry, str);
4615 gtk_entry_preedit_changed_cb (GtkIMContext *context,
4618 if (entry->editable)
4620 gchar *preedit_string;
4623 gtk_im_context_get_preedit_string (entry->im_context,
4624 &preedit_string, NULL,
4626 entry->preedit_length = strlen (preedit_string);
4627 cursor_pos = CLAMP (cursor_pos, 0, g_utf8_strlen (preedit_string, -1));
4628 entry->preedit_cursor = cursor_pos;
4629 g_free (preedit_string);
4631 gtk_entry_recompute (entry);
4636 gtk_entry_retrieve_surrounding_cb (GtkIMContext *context,
4639 gtk_im_context_set_surrounding (context,
4642 g_utf8_offset_to_pointer (entry->text, entry->current_pos) - entry->text);
4648 gtk_entry_delete_surrounding_cb (GtkIMContext *slave,
4653 if (entry->editable)
4654 gtk_editable_delete_text (GTK_EDITABLE (entry),
4655 entry->current_pos + offset,
4656 entry->current_pos + offset + n_chars);
4661 /* Internal functions
4664 /* Used for im_commit_cb and inserting Unicode chars */
4666 gtk_entry_enter_text (GtkEntry *entry,
4669 GtkEditable *editable = GTK_EDITABLE (entry);
4671 gboolean old_need_im_reset;
4673 old_need_im_reset = entry->need_im_reset;
4674 entry->need_im_reset = FALSE;
4676 if (gtk_editable_get_selection_bounds (editable, NULL, NULL))
4677 gtk_editable_delete_selection (editable);
4680 if (entry->overwrite_mode)
4681 gtk_entry_delete_from_cursor (entry, GTK_DELETE_CHARS, 1);
4684 tmp_pos = entry->current_pos;
4685 gtk_editable_insert_text (editable, str, strlen (str), &tmp_pos);
4686 gtk_editable_set_position (editable, tmp_pos);
4688 entry->need_im_reset = old_need_im_reset;
4691 /* All changes to entry->current_pos and entry->selection_bound
4692 * should go through this function.
4695 gtk_entry_set_positions (GtkEntry *entry,
4697 gint selection_bound)
4699 gboolean changed = FALSE;
4701 g_object_freeze_notify (G_OBJECT (entry));
4703 if (current_pos != -1 &&
4704 entry->current_pos != current_pos)
4706 entry->current_pos = current_pos;
4709 g_object_notify (G_OBJECT (entry), "cursor-position");
4712 if (selection_bound != -1 &&
4713 entry->selection_bound != selection_bound)
4715 entry->selection_bound = selection_bound;
4718 g_object_notify (G_OBJECT (entry), "selection-bound");
4721 g_object_thaw_notify (G_OBJECT (entry));
4725 gtk_entry_move_adjustments (entry);
4726 gtk_entry_recompute (entry);
4731 gtk_entry_reset_layout (GtkEntry *entry)
4733 if (entry->cached_layout)
4735 g_object_unref (entry->cached_layout);
4736 entry->cached_layout = NULL;
4741 update_im_cursor_location (GtkEntry *entry)
4745 gint strong_xoffset;
4746 gint area_width, area_height;
4748 gtk_entry_get_cursor_locations (entry, CURSOR_STANDARD, &strong_x, NULL);
4749 gtk_entry_get_text_area_size (entry, NULL, NULL, &area_width, &area_height);
4751 strong_xoffset = strong_x - entry->scroll_offset;
4752 if (strong_xoffset < 0)
4756 else if (strong_xoffset > area_width)
4758 strong_xoffset = area_width;
4760 area.x = strong_xoffset;
4763 area.height = area_height;
4765 gtk_im_context_set_cursor_location (entry->im_context, &area);
4769 recompute_idle_func (gpointer data)
4773 entry = GTK_ENTRY (data);
4775 entry->recompute_idle = 0;
4777 if (gtk_widget_has_screen (GTK_WIDGET (entry)))
4779 gtk_entry_adjust_scroll (entry);
4780 gtk_entry_queue_draw (entry);
4782 update_im_cursor_location (entry);
4789 gtk_entry_recompute (GtkEntry *entry)
4791 gtk_entry_reset_layout (entry);
4792 gtk_entry_check_cursor_blink (entry);
4794 if (!entry->recompute_idle)
4796 entry->recompute_idle = gdk_threads_add_idle_full (G_PRIORITY_HIGH_IDLE + 15, /* between resize and redraw */
4797 recompute_idle_func, entry, NULL);
4802 append_char (GString *str,
4810 char_len = g_unichar_to_utf8 (ch, buf);
4815 g_string_append_len (str, buf, char_len);
4821 gtk_entry_remove_password_hint (gpointer data)
4823 /* Force the string to be redrawn, but now without a visible character */
4824 gtk_entry_recompute (GTK_ENTRY (data));
4829 static PangoLayout *
4830 gtk_entry_create_layout (GtkEntry *entry,
4831 gboolean include_preedit)
4833 GtkWidget *widget = GTK_WIDGET (entry);
4834 PangoLayout *layout = gtk_widget_create_pango_layout (widget, NULL);
4835 PangoAttrList *tmp_attrs = pango_attr_list_new ();
4837 gchar *preedit_string = NULL;
4838 gint preedit_length = 0;
4839 PangoAttrList *preedit_attrs = NULL;
4841 pango_layout_set_single_paragraph_mode (layout, TRUE);
4843 if (include_preedit)
4845 gtk_im_context_get_preedit_string (entry->im_context,
4846 &preedit_string, &preedit_attrs, NULL);
4847 preedit_length = entry->preedit_length;
4852 GString *tmp_string = g_string_new (NULL);
4854 gint cursor_index = g_utf8_offset_to_pointer (entry->text, entry->current_pos) - entry->text;
4858 g_string_prepend_len (tmp_string, entry->text, entry->n_bytes);
4859 g_string_insert (tmp_string, cursor_index, preedit_string);
4864 gunichar invisible_char;
4866 if (entry->invisible_char != 0)
4867 invisible_char = entry->invisible_char;
4869 invisible_char = ' '; /* just pick a char */
4871 ch_len = g_utf8_strlen (entry->text, entry->n_bytes);
4872 append_char (tmp_string, invisible_char, ch_len);
4874 g_utf8_offset_to_pointer (tmp_string->str, entry->current_pos) -
4876 g_string_insert (tmp_string, cursor_index, preedit_string);
4879 pango_layout_set_text (layout, tmp_string->str, tmp_string->len);
4881 pango_attr_list_splice (tmp_attrs, preedit_attrs,
4882 cursor_index, preedit_length);
4884 g_string_free (tmp_string, TRUE);
4888 PangoDirection pango_dir;
4891 pango_dir = pango_find_base_dir (entry->text, entry->n_bytes);
4894 pango_dir = PANGO_DIRECTION_NEUTRAL;
4896 if (pango_dir == PANGO_DIRECTION_NEUTRAL)
4898 if (GTK_WIDGET_HAS_FOCUS (widget))
4900 GdkDisplay *display = gtk_widget_get_display (widget);
4901 GdkKeymap *keymap = gdk_keymap_get_for_display (display);
4902 if (gdk_keymap_get_direction (keymap) == PANGO_DIRECTION_RTL)
4903 pango_dir = PANGO_DIRECTION_RTL;
4905 pango_dir = PANGO_DIRECTION_LTR;
4909 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
4910 pango_dir = PANGO_DIRECTION_RTL;
4912 pango_dir = PANGO_DIRECTION_LTR;
4916 pango_context_set_base_dir (gtk_widget_get_pango_context (widget),
4919 entry->resolved_dir = pango_dir;
4923 pango_layout_set_text (layout, entry->text, entry->n_bytes);
4927 GString *str = g_string_new (NULL);
4928 gunichar invisible_char;
4929 guint password_hint_timeout;
4930 GtkEntryPasswordHint *password_hint;
4932 g_object_get (gtk_widget_get_settings (widget),
4933 "gtk-entry-password-hint-timeout", &password_hint_timeout,
4936 if (entry->invisible_char != 0)
4937 invisible_char = entry->invisible_char;
4939 invisible_char = ' '; /* just pick a char */
4941 password_hint = g_object_get_qdata (G_OBJECT (entry),
4942 quark_password_hint);
4944 if (password_hint && password_hint->password_hint_timeout_id)
4946 g_source_remove (password_hint->password_hint_timeout_id);
4947 password_hint->password_hint_timeout_id = 0;
4950 if (password_hint_timeout == 0 || password_hint == NULL ||
4951 (password_hint && password_hint->password_hint_length == 0))
4953 append_char (str, invisible_char, entry->text_length);
4955 else if (password_hint)
4957 /* Draw hidden characters upto the inserted position,
4958 * then the real thing, pad up to full length
4960 if (password_hint->password_hint_position > 1)
4961 append_char (str, invisible_char,
4962 password_hint->password_hint_position - 1);
4964 g_string_append_len (str, password_hint->password_hint,
4965 password_hint->password_hint_length);
4967 if (password_hint->password_hint_position < entry->text_length)
4968 append_char (str, invisible_char,
4969 entry->text_length -
4970 password_hint->password_hint_position);
4972 /* Now remove this last input character, don't need
4975 memset (password_hint->password_hint, 0, PASSWORD_HINT_MAX);
4976 password_hint->password_hint_length = 0;
4978 password_hint->password_hint_timeout_id =
4979 gdk_threads_add_timeout (password_hint_timeout,
4980 (GSourceFunc) gtk_entry_remove_password_hint,
4984 pango_layout_set_text (layout, str->str, str->len);
4985 g_string_free (str, TRUE);
4989 pango_layout_set_attributes (layout, tmp_attrs);
4991 g_free (preedit_string);
4993 pango_attr_list_unref (preedit_attrs);
4995 pango_attr_list_unref (tmp_attrs);
5000 static PangoLayout *
5001 gtk_entry_ensure_layout (GtkEntry *entry,
5002 gboolean include_preedit)
5004 if (entry->preedit_length > 0 &&
5005 !include_preedit != !entry->cache_includes_preedit)
5006 gtk_entry_reset_layout (entry);
5008 if (!entry->cached_layout)
5010 entry->cached_layout = gtk_entry_create_layout (entry, include_preedit);
5011 entry->cache_includes_preedit = include_preedit;
5014 return entry->cached_layout;
5018 get_layout_position (GtkEntry *entry,
5022 PangoLayout *layout;
5023 PangoRectangle logical_rect;
5024 gint area_width, area_height;
5025 GtkBorder inner_border;
5027 PangoLayoutLine *line;
5029 layout = gtk_entry_ensure_layout (entry, TRUE);
5031 gtk_entry_get_text_area_size (entry, NULL, NULL, &area_width, &area_height);
5032 _gtk_entry_effective_inner_border (entry, &inner_border);
5034 area_height = PANGO_SCALE * (area_height - inner_border.top - inner_border.bottom);
5036 line = pango_layout_get_lines_readonly (layout)->data;
5037 pango_layout_line_get_extents (line, NULL, &logical_rect);
5039 /* Align primarily for locale's ascent/descent */
5040 y_pos = ((area_height - entry->ascent - entry->descent) / 2 +
5041 entry->ascent + logical_rect.y);
5043 /* Now see if we need to adjust to fit in actual drawn string */
5044 if (logical_rect.height > area_height)
5045 y_pos = (area_height - logical_rect.height) / 2;
5048 else if (y_pos + logical_rect.height > area_height)
5049 y_pos = area_height - logical_rect.height;
5051 y_pos = inner_border.top + y_pos / PANGO_SCALE;
5054 *x = inner_border.left - entry->scroll_offset;
5061 gtk_entry_draw_text (GtkEntry *entry)
5065 if (!entry->visible && entry->invisible_char == 0)
5068 if (GTK_WIDGET_DRAWABLE (entry))
5070 PangoLayout *layout = gtk_entry_ensure_layout (entry, TRUE);
5073 gint start_pos, end_pos;
5075 widget = GTK_WIDGET (entry);
5077 get_layout_position (entry, &x, &y);
5079 cr = gdk_cairo_create (entry->text_area);
5081 cairo_move_to (cr, x, y);
5082 gdk_cairo_set_source_color (cr, &widget->style->text [widget->state]);
5083 pango_cairo_show_layout (cr, layout);
5085 if (gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &start_pos, &end_pos))
5089 PangoRectangle logical_rect;
5090 GdkColor *selection_color, *text_color;
5091 GtkBorder inner_border;
5093 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
5094 gtk_entry_get_pixel_ranges (entry, &ranges, &n_ranges);
5096 if (GTK_WIDGET_HAS_FOCUS (entry))
5098 selection_color = &widget->style->base [GTK_STATE_SELECTED];
5099 text_color = &widget->style->text [GTK_STATE_SELECTED];
5103 selection_color = &widget->style->base [GTK_STATE_ACTIVE];
5104 text_color = &widget->style->text [GTK_STATE_ACTIVE];
5107 _gtk_entry_effective_inner_border (entry, &inner_border);
5109 for (i = 0; i < n_ranges; ++i)
5110 cairo_rectangle (cr,
5111 inner_border.left - entry->scroll_offset + ranges[2 * i],
5114 logical_rect.height);
5118 gdk_cairo_set_source_color (cr, selection_color);
5121 cairo_move_to (cr, x, y);
5122 gdk_cairo_set_source_color (cr, text_color);
5123 pango_cairo_show_layout (cr, layout);
5133 draw_insertion_cursor (GtkEntry *entry,
5134 GdkRectangle *cursor_location,
5135 gboolean is_primary,
5136 PangoDirection direction,
5137 gboolean draw_arrow)
5139 GtkWidget *widget = GTK_WIDGET (entry);
5140 GtkTextDirection text_dir;
5142 if (direction == PANGO_DIRECTION_LTR)
5143 text_dir = GTK_TEXT_DIR_LTR;
5145 text_dir = GTK_TEXT_DIR_RTL;
5147 gtk_draw_insertion_cursor (widget, entry->text_area, NULL,
5149 is_primary, text_dir, draw_arrow);
5153 gtk_entry_draw_cursor (GtkEntry *entry,
5156 GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (entry)));
5157 PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
5159 if (GTK_WIDGET_DRAWABLE (entry))
5161 GtkWidget *widget = GTK_WIDGET (entry);
5162 GdkRectangle cursor_location;
5163 gboolean split_cursor;
5164 PangoRectangle cursor_rect;
5165 GtkBorder inner_border;
5167 gint text_area_height;
5170 gboolean block_at_line_end;
5171 PangoLayout *layout;
5174 _gtk_entry_effective_inner_border (entry, &inner_border);
5176 xoffset = inner_border.left - entry->scroll_offset;
5178 gdk_drawable_get_size (entry->text_area, NULL, &text_area_height);
5180 layout = gtk_entry_ensure_layout (entry, TRUE);
5181 text = pango_layout_get_text (layout);
5182 cursor_index = g_utf8_offset_to_pointer (text, entry->current_pos + entry->preedit_cursor) - text;
5183 if (!entry->overwrite_mode)
5186 block = _gtk_text_util_get_block_cursor_location (layout,
5187 cursor_index, &cursor_rect, &block_at_line_end);
5191 gint strong_x, weak_x;
5192 PangoDirection dir1 = PANGO_DIRECTION_NEUTRAL;
5193 PangoDirection dir2 = PANGO_DIRECTION_NEUTRAL;
5197 gtk_entry_get_cursor_locations (entry, type, &strong_x, &weak_x);
5199 g_object_get (gtk_widget_get_settings (widget),
5200 "gtk-split-cursor", &split_cursor,
5203 dir1 = entry->resolved_dir;
5209 if (weak_x != strong_x)
5211 dir2 = (entry->resolved_dir == PANGO_DIRECTION_LTR) ? PANGO_DIRECTION_RTL : PANGO_DIRECTION_LTR;
5217 if (keymap_direction == entry->resolved_dir)
5223 cursor_location.x = xoffset + x1;
5224 cursor_location.y = inner_border.top;
5225 cursor_location.width = 0;
5226 cursor_location.height = text_area_height - inner_border.top - inner_border.bottom;
5228 draw_insertion_cursor (entry,
5229 &cursor_location, TRUE, dir1,
5230 dir2 != PANGO_DIRECTION_NEUTRAL);
5232 if (dir2 != PANGO_DIRECTION_NEUTRAL)
5234 cursor_location.x = xoffset + x2;
5235 draw_insertion_cursor (entry,
5236 &cursor_location, FALSE, dir2,
5240 else /* overwrite_mode */
5242 GdkColor cursor_color;
5247 get_layout_position (entry, &x, &y);
5249 rect.x = PANGO_PIXELS (cursor_rect.x) + x;
5250 rect.y = PANGO_PIXELS (cursor_rect.y) + y;
5251 rect.width = PANGO_PIXELS (cursor_rect.width);
5252 rect.height = PANGO_PIXELS (cursor_rect.height);
5254 cr = gdk_cairo_create (entry->text_area);
5256 _gtk_widget_get_cursor_color (widget, &cursor_color);
5257 gdk_cairo_set_source_color (cr, &cursor_color);
5258 gdk_cairo_rectangle (cr, &rect);
5261 if (!block_at_line_end)
5263 gdk_cairo_rectangle (cr, &rect);
5265 cairo_move_to (cr, x, y);
5266 gdk_cairo_set_source_color (cr, &widget->style->base[widget->state]);
5267 pango_cairo_show_layout (cr, layout);
5276 gtk_entry_queue_draw (GtkEntry *entry)
5278 if (GTK_WIDGET_DRAWABLE (entry))
5279 gdk_window_invalidate_rect (entry->text_area, NULL, FALSE);
5283 _gtk_entry_reset_im_context (GtkEntry *entry)
5285 if (entry->need_im_reset)
5287 entry->need_im_reset = FALSE;
5288 gtk_im_context_reset (entry->im_context);
5293 gtk_entry_find_position (GtkEntry *entry,
5296 PangoLayout *layout;
5297 PangoLayoutLine *line;
5304 layout = gtk_entry_ensure_layout (entry, TRUE);
5305 text = pango_layout_get_text (layout);
5306 cursor_index = g_utf8_offset_to_pointer (text, entry->current_pos) - text;
5308 line = pango_layout_get_lines_readonly (layout)->data;
5309 pango_layout_line_x_to_index (line, x * PANGO_SCALE, &index, &trailing);
5311 if (index >= cursor_index && entry->preedit_length)
5313 if (index >= cursor_index + entry->preedit_length)
5314 index -= entry->preedit_length;
5317 index = cursor_index;
5322 pos = g_utf8_pointer_to_offset (text, text + index);
5329 gtk_entry_get_cursor_locations (GtkEntry *entry,
5334 if (!entry->visible && !entry->invisible_char)
5344 PangoLayout *layout = gtk_entry_ensure_layout (entry, TRUE);
5345 const gchar *text = pango_layout_get_text (layout);
5346 PangoRectangle strong_pos, weak_pos;
5349 if (type == CURSOR_STANDARD)
5351 index = g_utf8_offset_to_pointer (text, entry->current_pos + entry->preedit_cursor) - text;
5353 else /* type == CURSOR_DND */
5355 index = g_utf8_offset_to_pointer (text, entry->dnd_position) - text;
5357 if (entry->dnd_position > entry->current_pos)
5360 index += entry->preedit_length;
5363 gint preedit_len_chars = g_utf8_strlen (text, -1) - entry->text_length;
5364 index += preedit_len_chars * g_unichar_to_utf8 (entry->invisible_char, NULL);
5369 pango_layout_get_cursor_pos (layout, index, &strong_pos, &weak_pos);
5372 *strong_x = strong_pos.x / PANGO_SCALE;
5375 *weak_x = weak_pos.x / PANGO_SCALE;
5380 gtk_entry_adjust_scroll (GtkEntry *entry)
5382 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
5383 gint min_offset, max_offset;
5384 gint text_area_width, text_width;
5385 GtkBorder inner_border;
5386 gint strong_x, weak_x;
5387 gint strong_xoffset, weak_xoffset;
5389 PangoLayout *layout;
5390 PangoLayoutLine *line;
5391 PangoRectangle logical_rect;
5393 if (!GTK_WIDGET_REALIZED (entry))
5396 _gtk_entry_effective_inner_border (entry, &inner_border);
5398 gdk_drawable_get_size (entry->text_area, &text_area_width, NULL);
5399 text_area_width -= inner_border.left + inner_border.right;
5400 if (text_area_width < 0)
5401 text_area_width = 0;
5403 layout = gtk_entry_ensure_layout (entry, TRUE);
5404 line = pango_layout_get_lines_readonly (layout)->data;
5406 pango_layout_line_get_extents (line, NULL, &logical_rect);
5408 /* Display as much text as we can */
5410 if (entry->resolved_dir == PANGO_DIRECTION_LTR)
5411 xalign = priv->xalign;
5413 xalign = 1.0 - priv->xalign;
5415 text_width = PANGO_PIXELS(logical_rect.width);
5417 if (text_width > text_area_width)
5420 max_offset = text_width - text_area_width;
5424 min_offset = (text_width - text_area_width) * xalign;
5425 max_offset = min_offset;
5428 entry->scroll_offset = CLAMP (entry->scroll_offset, min_offset, max_offset);
5430 /* And make sure cursors are on screen. Note that the cursor is
5431 * actually drawn one pixel into the INNER_BORDER space on
5432 * the right, when the scroll is at the utmost right. This
5433 * looks better to to me than confining the cursor inside the
5434 * border entirely, though it means that the cursor gets one
5435 * pixel closer to the edge of the widget on the right than
5436 * on the left. This might need changing if one changed
5437 * INNER_BORDER from 2 to 1, as one would do on a
5438 * small-screen-real-estate display.
5440 * We always make sure that the strong cursor is on screen, and
5441 * put the weak cursor on screen if possible.
5444 gtk_entry_get_cursor_locations (entry, CURSOR_STANDARD, &strong_x, &weak_x);
5446 strong_xoffset = strong_x - entry->scroll_offset;
5448 if (strong_xoffset < 0)
5450 entry->scroll_offset += strong_xoffset;
5453 else if (strong_xoffset > text_area_width)
5455 entry->scroll_offset += strong_xoffset - text_area_width;
5456 strong_xoffset = text_area_width;
5459 weak_xoffset = weak_x - entry->scroll_offset;
5461 if (weak_xoffset < 0 && strong_xoffset - weak_xoffset <= text_area_width)
5463 entry->scroll_offset += weak_xoffset;
5465 else if (weak_xoffset > text_area_width &&
5466 strong_xoffset - (weak_xoffset - text_area_width) >= 0)
5468 entry->scroll_offset += weak_xoffset - text_area_width;
5471 g_object_notify (G_OBJECT (entry), "scroll-offset");
5475 gtk_entry_move_adjustments (GtkEntry *entry)
5477 PangoContext *context;
5478 PangoFontMetrics *metrics;
5479 gint x, layout_x, border_x, border_y;
5481 GtkAdjustment *adjustment;
5483 adjustment = g_object_get_qdata (G_OBJECT (entry), quark_cursor_hadjustment);
5487 /* Cursor position, layout offset, border width, and widget allocation */
5488 gtk_entry_get_cursor_locations (entry, CURSOR_STANDARD, &x, NULL);
5489 get_layout_position (entry, &layout_x, NULL);
5490 _gtk_entry_get_borders (entry, &border_x, &border_y);
5491 x += entry->widget.allocation.x + layout_x + border_x;
5493 /* Approximate width of a char, so user can see what is ahead/behind */
5494 context = gtk_widget_get_pango_context (GTK_WIDGET (entry));
5495 metrics = pango_context_get_metrics (context,
5496 entry->widget.style->font_desc,
5497 pango_context_get_language (context));
5498 char_width = pango_font_metrics_get_approximate_char_width (metrics) / PANGO_SCALE;
5501 gtk_adjustment_clamp_page (adjustment,
5502 x - (char_width + 1), /* one char + one pixel before */
5503 x + (char_width + 2)); /* one char + cursor + one pixel after */
5507 gtk_entry_move_visually (GtkEntry *entry,
5512 PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
5515 text = pango_layout_get_text (layout);
5517 index = g_utf8_offset_to_pointer (text, start) - text;
5521 int new_index, new_trailing;
5522 gboolean split_cursor;
5525 g_object_get (gtk_widget_get_settings (GTK_WIDGET (entry)),
5526 "gtk-split-cursor", &split_cursor,
5533 GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (entry)));
5534 PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
5536 strong = keymap_direction == entry->resolved_dir;
5541 pango_layout_move_cursor_visually (layout, strong, index, 0, 1, &new_index, &new_trailing);
5546 pango_layout_move_cursor_visually (layout, strong, index, 0, -1, &new_index, &new_trailing);
5552 else if (new_index != G_MAXINT)
5555 while (new_trailing--)
5556 index = g_utf8_next_char (text + index) - text;
5559 return g_utf8_pointer_to_offset (text, text + index);
5563 gtk_entry_move_logically (GtkEntry *entry,
5567 gint new_pos = start;
5569 /* Prevent any leak of information */
5570 if (!entry->visible)
5572 new_pos = CLAMP (start + count, 0, entry->text_length);
5574 else if (entry->text)
5576 PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
5577 PangoLogAttr *log_attrs;
5580 pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
5582 while (count > 0 && new_pos < entry->text_length)
5586 while (new_pos < entry->text_length && !log_attrs[new_pos].is_cursor_position);
5590 while (count < 0 && new_pos > 0)
5594 while (new_pos > 0 && !log_attrs[new_pos].is_cursor_position);
5606 gtk_entry_move_forward_word (GtkEntry *entry,
5608 gboolean allow_whitespace)
5610 gint new_pos = start;
5612 /* Prevent any leak of information */
5613 if (!entry->visible)
5615 new_pos = entry->text_length;
5617 else if (entry->text && (new_pos < entry->text_length))
5619 PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
5620 PangoLogAttr *log_attrs;
5623 pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
5625 /* Find the next word boundary */
5627 while (new_pos < n_attrs - 1 && !(log_attrs[new_pos].is_word_end ||
5628 (log_attrs[new_pos].is_word_start && allow_whitespace)))
5639 gtk_entry_move_backward_word (GtkEntry *entry,
5641 gboolean allow_whitespace)
5643 gint new_pos = start;
5645 /* Prevent any leak of information */
5646 if (!entry->visible)
5650 else if (entry->text && start > 0)
5652 PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
5653 PangoLogAttr *log_attrs;
5656 pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
5658 new_pos = start - 1;
5660 /* Find the previous word boundary */
5661 while (new_pos > 0 && !(log_attrs[new_pos].is_word_start ||
5662 (log_attrs[new_pos].is_word_end && allow_whitespace)))
5672 gtk_entry_delete_whitespace (GtkEntry *entry)
5674 PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
5675 PangoLogAttr *log_attrs;
5679 pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
5681 start = end = entry->current_pos;
5683 while (start > 0 && log_attrs[start-1].is_white)
5686 while (end < n_attrs && log_attrs[end].is_white)
5692 gtk_editable_delete_text (GTK_EDITABLE (entry), start, end);
5697 gtk_entry_select_word (GtkEntry *entry)
5699 gint start_pos = gtk_entry_move_backward_word (entry, entry->current_pos, TRUE);
5700 gint end_pos = gtk_entry_move_forward_word (entry, entry->current_pos, TRUE);
5702 gtk_editable_select_region (GTK_EDITABLE (entry), start_pos, end_pos);
5706 gtk_entry_select_line (GtkEntry *entry)
5708 gtk_editable_select_region (GTK_EDITABLE (entry), 0, -1);
5712 * Like gtk_editable_get_chars, but handle not-visible entries
5716 gtk_entry_get_public_chars (GtkEntry *entry,
5721 end = entry->text_length;
5724 return gtk_editable_get_chars (GTK_EDITABLE (entry), start, end);
5725 else if (!entry->invisible_char)
5726 return g_strdup ("");
5729 GString *str = g_string_new (NULL);
5730 append_char (str, entry->invisible_char, end - start);
5731 return g_string_free (str, FALSE);
5736 truncate_multiline (const gchar *text)
5741 text[length] && text[length] != '\n' && text[length] != '\r';
5748 paste_received (GtkClipboard *clipboard,
5752 GtkEntry *entry = GTK_ENTRY (data);
5753 GtkEditable *editable = GTK_EDITABLE (entry);
5754 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
5756 if (entry->button == 2)
5758 gint pos, start, end;
5759 pos = priv->insert_pos;
5760 gtk_editable_get_selection_bounds (editable, &start, &end);
5761 if (!((start <= pos && pos <= end) || (end <= pos && pos <= start)))
5762 gtk_editable_select_region (editable, pos, pos);
5767 gint pos, start, end;
5769 gboolean popup_completion;
5770 GtkEntryCompletion *completion;
5772 completion = gtk_entry_get_completion (entry);
5774 if (entry->truncate_multiline)
5775 length = truncate_multiline (text);
5777 /* only complete if the selection is at the end */
5778 popup_completion = (entry->text_length == MAX (entry->current_pos, entry->selection_bound));
5782 if (GTK_WIDGET_MAPPED (completion->priv->popup_window))
5783 _gtk_entry_completion_popdown (completion);
5785 if (!popup_completion && completion->priv->changed_id > 0)
5786 g_signal_handler_block (entry, completion->priv->changed_id);
5789 begin_change (entry);
5790 g_object_freeze_notify (G_OBJECT (entry));
5791 if (gtk_editable_get_selection_bounds (editable, &start, &end))
5792 gtk_editable_delete_text (editable, start, end);
5794 pos = entry->current_pos;
5795 gtk_editable_insert_text (editable, text, length, &pos);
5796 gtk_editable_set_position (editable, pos);
5797 g_object_thaw_notify (G_OBJECT (entry));
5801 !popup_completion && completion->priv->changed_id > 0)
5802 g_signal_handler_unblock (entry, completion->priv->changed_id);
5805 g_object_unref (entry);
5809 gtk_entry_paste (GtkEntry *entry,
5812 g_object_ref (entry);
5813 gtk_clipboard_request_text (gtk_widget_get_clipboard (GTK_WIDGET (entry), selection),
5814 paste_received, entry);
5818 primary_get_cb (GtkClipboard *clipboard,
5819 GtkSelectionData *selection_data,
5823 GtkEntry *entry = GTK_ENTRY (data);
5826 if (gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &start, &end))
5828 gchar *str = gtk_entry_get_public_chars (entry, start, end);
5829 gtk_selection_data_set_text (selection_data, str, -1);
5835 primary_clear_cb (GtkClipboard *clipboard,
5838 GtkEntry *entry = GTK_ENTRY (data);
5840 gtk_editable_select_region (GTK_EDITABLE (entry), entry->current_pos, entry->current_pos);
5844 gtk_entry_update_primary_selection (GtkEntry *entry)
5846 GtkTargetList *list;
5847 GtkTargetEntry *targets;
5848 GtkClipboard *clipboard;
5852 if (!GTK_WIDGET_REALIZED (entry))
5855 list = gtk_target_list_new (NULL, 0);
5856 gtk_target_list_add_text_targets (list, 0);
5858 targets = gtk_target_table_new_from_list (list, &n_targets);
5860 clipboard = gtk_widget_get_clipboard (GTK_WIDGET (entry), GDK_SELECTION_PRIMARY);
5862 if (gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &start, &end))
5864 if (!gtk_clipboard_set_with_owner (clipboard, targets, n_targets,
5865 primary_get_cb, primary_clear_cb, G_OBJECT (entry)))
5866 primary_clear_cb (clipboard, entry);
5870 if (gtk_clipboard_get_owner (clipboard) == G_OBJECT (entry))
5871 gtk_clipboard_clear (clipboard);
5874 gtk_target_table_free (targets, n_targets);
5875 gtk_target_list_unref (list);
5879 gtk_entry_clear (GtkEntry *entry,
5880 GtkEntryIconPosition icon_pos)
5882 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
5883 EntryIconInfo *icon_info = priv->icons[icon_pos];
5885 if (!icon_info || icon_info->storage_type == GTK_IMAGE_EMPTY)
5888 g_object_freeze_notify (G_OBJECT (entry));
5890 /* Explicitly check, as the pointer may become invalidated
5891 * during destruction.
5893 if (GDK_IS_WINDOW (icon_info->window))
5894 gdk_window_hide (icon_info->window);
5896 if (icon_info->pixbuf)
5898 g_object_unref (icon_info->pixbuf);
5899 icon_info->pixbuf = NULL;
5902 switch (icon_info->storage_type)
5904 case GTK_IMAGE_PIXBUF:
5905 g_object_notify (G_OBJECT (entry),
5906 icon_pos == GTK_ENTRY_ICON_PRIMARY ? "pixbuf-primary" : "pixbuf-secondary");
5909 case GTK_IMAGE_STOCK:
5910 g_free (icon_info->stock_id);
5911 icon_info->stock_id = NULL;
5912 g_object_notify (G_OBJECT (entry),
5913 icon_pos == GTK_ENTRY_ICON_PRIMARY ? "stock-primary" : "stock-secondary");
5916 case GTK_IMAGE_ICON_NAME:
5917 g_free (icon_info->icon_name);
5918 icon_info->icon_name = NULL;
5919 g_object_notify (G_OBJECT (entry),
5920 icon_pos == GTK_ENTRY_ICON_PRIMARY ? "icon-name-primary" : "icon-name-secondary");
5923 case GTK_IMAGE_GICON:
5924 if (icon_info->gicon)
5926 g_object_unref (icon_info->gicon);
5927 icon_info->gicon = NULL;
5929 g_object_notify (G_OBJECT (entry),
5930 icon_pos == GTK_ENTRY_ICON_PRIMARY ? "gicon-primary" : "gicon-secondary");
5934 g_assert_not_reached ();
5938 icon_info->storage_type = GTK_IMAGE_EMPTY;
5939 g_object_notify (G_OBJECT (entry),
5940 icon_pos == GTK_ENTRY_ICON_PRIMARY ? "storage-type-primary" : "storage-type-secondary");
5942 g_object_thaw_notify (G_OBJECT (entry));
5946 gtk_entry_ensure_pixbuf (GtkEntry *entry,
5947 GtkEntryIconPosition icon_pos)
5949 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
5950 EntryIconInfo *icon_info = priv->icons[icon_pos];
5952 GtkIconTheme *icon_theme;
5953 GtkSettings *settings;
5957 if (!icon_info || icon_info->pixbuf)
5960 switch (icon_info->storage_type)
5962 case GTK_IMAGE_EMPTY:
5963 case GTK_IMAGE_PIXBUF:
5965 case GTK_IMAGE_STOCK:
5966 icon_info->pixbuf = gtk_widget_render_icon (GTK_WIDGET (entry),
5967 icon_info->stock_id,
5972 case GTK_IMAGE_ICON_NAME:
5973 screen = gtk_widget_get_screen (GTK_WIDGET (entry));
5976 icon_theme = gtk_icon_theme_get_for_screen (screen);
5977 settings = gtk_settings_get_for_screen (screen);
5979 gtk_icon_size_lookup_for_settings (settings,
5983 icon_info->pixbuf = gtk_icon_theme_load_icon (icon_theme,
5984 icon_info->icon_name,
5985 MIN (width, height),
5988 if (icon_info->pixbuf == NULL)
5989 icon_info->pixbuf = gtk_widget_render_icon (GTK_WIDGET (entry),
5990 GTK_STOCK_MISSING_IMAGE,
5996 case GTK_IMAGE_GICON:
5997 screen = gtk_widget_get_screen (GTK_WIDGET (entry));
6000 icon_theme = gtk_icon_theme_get_for_screen (screen);
6001 settings = gtk_settings_get_for_screen (screen);
6003 gtk_icon_size_lookup_for_settings (settings,
6007 info = gtk_icon_theme_lookup_by_gicon (icon_theme,
6009 MIN (width, height),
6010 GTK_ICON_LOOKUP_USE_BUILTIN);
6013 icon_info->pixbuf = gtk_icon_info_load_icon (info, NULL);
6014 gtk_icon_info_free (info);
6017 if (icon_info->pixbuf == NULL)
6018 icon_info->pixbuf = gtk_widget_render_icon (GTK_WIDGET (entry),
6019 GTK_STOCK_MISSING_IMAGE,
6026 g_assert_not_reached ();
6030 if (GDK_IS_WINDOW (icon_info->window))
6031 gdk_window_show (icon_info->window);
6033 gtk_widget_queue_resize (GTK_WIDGET (entry));
6043 * Creates a new entry.
6045 * Return value: a new #GtkEntry.
6048 gtk_entry_new (void)
6050 return g_object_new (GTK_TYPE_ENTRY, NULL);
6054 * gtk_entry_new_with_max_length:
6055 * @max: the maximum length of the entry, or 0 for no maximum.
6056 * (other than the maximum length of entries.) The value passed in will
6057 * be clamped to the range 0-65536.
6059 * Creates a new #GtkEntry widget with the given maximum length.
6061 * Return value: a new #GtkEntry
6063 * Deprecated: Use gtk_entry_set_max_length() instead.
6066 gtk_entry_new_with_max_length (gint max)
6070 max = CLAMP (max, 0, MAX_SIZE);
6072 entry = g_object_new (GTK_TYPE_ENTRY, NULL);
6073 entry->text_max_length = max;
6075 return GTK_WIDGET (entry);
6079 * gtk_entry_set_text:
6080 * @entry: a #GtkEntry
6081 * @text: the new text
6083 * Sets the text in the widget to the given
6084 * value, replacing the current contents.
6087 gtk_entry_set_text (GtkEntry *entry,
6091 GtkEntryCompletion *completion;
6093 g_return_if_fail (GTK_IS_ENTRY (entry));
6094 g_return_if_fail (text != NULL);
6096 /* Actually setting the text will affect the cursor and selection;
6097 * if the contents don't actually change, this will look odd to the user.
6099 if (strcmp (entry->text, text) == 0)
6102 completion = gtk_entry_get_completion (entry);
6103 if (completion && completion->priv->changed_id > 0)
6104 g_signal_handler_block (entry, completion->priv->changed_id);
6106 begin_change (entry);
6107 g_object_freeze_notify (G_OBJECT (entry));
6108 gtk_editable_delete_text (GTK_EDITABLE (entry), 0, -1);
6111 gtk_editable_insert_text (GTK_EDITABLE (entry), text, strlen (text), &tmp_pos);
6112 g_object_thaw_notify (G_OBJECT (entry));
6115 if (completion && completion->priv->changed_id > 0)
6116 g_signal_handler_unblock (entry, completion->priv->changed_id);
6120 * gtk_entry_append_text:
6121 * @entry: a #GtkEntry
6122 * @text: the text to append
6124 * Appends the given text to the contents of the widget.
6126 * Deprecated: 2.0: Use gtk_editable_insert_text() instead.
6129 gtk_entry_append_text (GtkEntry *entry,
6134 g_return_if_fail (GTK_IS_ENTRY (entry));
6135 g_return_if_fail (text != NULL);
6137 tmp_pos = entry->text_length;
6138 gtk_editable_insert_text (GTK_EDITABLE (entry), text, -1, &tmp_pos);
6142 * gtk_entry_prepend_text:
6143 * @entry: a #GtkEntry
6144 * @text: the text to prepend
6146 * Prepends the given text to the contents of the widget.
6148 * Deprecated: 2.0: Use gtk_editable_insert_text() instead.
6151 gtk_entry_prepend_text (GtkEntry *entry,
6156 g_return_if_fail (GTK_IS_ENTRY (entry));
6157 g_return_if_fail (text != NULL);
6160 gtk_editable_insert_text (GTK_EDITABLE (entry), text, -1, &tmp_pos);
6164 * gtk_entry_set_position:
6165 * @entry: a #GtkEntry
6166 * @position: the position of the cursor. The cursor is displayed
6167 * before the character with the given (base 0) index in the widget.
6168 * The value must be less than or equal to the number of characters
6169 * in the widget. A value of -1 indicates that the position should
6170 * be set after the last character in the entry. Note that this
6171 * position is in characters, not in bytes.
6173 * Sets the cursor position in an entry to the given value.
6175 * Deprecated: 2.0: Use gtk_editable_set_position() instead.
6178 gtk_entry_set_position (GtkEntry *entry,
6181 g_return_if_fail (GTK_IS_ENTRY (entry));
6183 gtk_editable_set_position (GTK_EDITABLE (entry), position);
6187 * gtk_entry_set_visibility:
6188 * @entry: a #GtkEntry
6189 * @visible: %TRUE if the contents of the entry are displayed
6192 * Sets whether the contents of the entry are visible or not.
6193 * When visibility is set to %FALSE, characters are displayed
6194 * as the invisible char, and will also appear that way when
6195 * the text in the entry widget is copied elsewhere.
6197 * By default, GTK+ picks the best invisible character available
6198 * in the current font, but it can be changed with
6199 * gtk_entry_set_invisible_char().
6202 gtk_entry_set_visibility (GtkEntry *entry,
6205 g_return_if_fail (GTK_IS_ENTRY (entry));
6207 visible = visible != FALSE;
6209 if (entry->visible != visible)
6211 entry->visible = visible;
6213 g_object_notify (G_OBJECT (entry), "visibility");
6214 gtk_entry_recompute (entry);
6219 * gtk_entry_get_visibility:
6220 * @entry: a #GtkEntry
6222 * Retrieves whether the text in @entry is visible. See
6223 * gtk_entry_set_visibility().
6225 * Return value: %TRUE if the text is currently visible
6228 gtk_entry_get_visibility (GtkEntry *entry)
6230 g_return_val_if_fail (GTK_IS_ENTRY (entry), FALSE);
6232 return entry->visible;
6236 * gtk_entry_set_invisible_char:
6237 * @entry: a #GtkEntry
6238 * @ch: a Unicode character
6240 * Sets the character to use in place of the actual text when
6241 * gtk_entry_set_visibility() has been called to set text visibility
6242 * to %FALSE. i.e. this is the character used in "password mode" to
6243 * show the user how many characters have been typed. By default, GTK+
6244 * picks the best invisible char available in the current font. If you
6245 * set the invisible char to 0, then the user will get no feedback
6246 * at all; there will be no text on the screen as they type.
6249 gtk_entry_set_invisible_char (GtkEntry *entry,
6252 GtkEntryPrivate *priv;
6254 g_return_if_fail (GTK_IS_ENTRY (entry));
6256 priv = GTK_ENTRY_GET_PRIVATE (entry);
6258 if (!priv->invisible_char_set)
6260 priv->invisible_char_set = TRUE;
6261 g_object_notify (G_OBJECT (entry), "invisible-char-set");
6264 if (ch == entry->invisible_char)
6267 entry->invisible_char = ch;
6268 g_object_notify (G_OBJECT (entry), "invisible-char");
6269 gtk_entry_recompute (entry);
6273 * gtk_entry_get_invisible_char:
6274 * @entry: a #GtkEntry
6276 * Retrieves the character displayed in place of the real characters
6277 * for entries with visibility set to false. See gtk_entry_set_invisible_char().
6279 * Return value: the current invisible char, or 0, if the entry does not
6280 * show invisible text at all.
6283 gtk_entry_get_invisible_char (GtkEntry *entry)
6285 g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
6287 return entry->invisible_char;
6291 * gtk_entry_unset_invisible_char:
6292 * @entry: a #GtkEntry
6294 * Unsets the invisible char previously set with
6295 * gtk_entry_set_invisible_char(). So that the
6296 * default invisible char is used again.
6301 gtk_entry_unset_invisible_char (GtkEntry *entry)
6303 GtkEntryPrivate *priv;
6306 g_return_if_fail (GTK_IS_ENTRY (entry));
6308 priv = GTK_ENTRY_GET_PRIVATE (entry);
6310 if (!priv->invisible_char_set)
6313 priv->invisible_char_set = FALSE;
6314 ch = find_invisible_char (GTK_WIDGET (entry));
6316 if (entry->invisible_char != ch)
6318 entry->invisible_char = ch;
6319 g_object_notify (G_OBJECT (entry), "invisible-char");
6322 g_object_notify (G_OBJECT (entry), "invisible-char-set");
6323 gtk_entry_recompute (entry);
6327 * gtk_entry_set_editable:
6328 * @entry: a #GtkEntry
6329 * @editable: %TRUE if the user is allowed to edit the text
6332 * Determines if the user can edit the text in the editable
6335 * Deprecated: 2.0: Use gtk_editable_set_editable() instead.
6338 gtk_entry_set_editable (GtkEntry *entry,
6341 g_return_if_fail (GTK_IS_ENTRY (entry));
6343 gtk_editable_set_editable (GTK_EDITABLE (entry), editable);
6347 * gtk_entry_set_overwrite_mode:
6348 * @entry: a #GtkEntry
6349 * @overwrite: new value
6351 * Sets whether the text is overwritten when typing in the #GtkEntry.
6356 gtk_entry_set_overwrite_mode (GtkEntry *entry,
6359 g_return_if_fail (GTK_IS_ENTRY (entry));
6361 if (entry->overwrite_mode == overwrite)
6364 gtk_entry_toggle_overwrite (entry);
6366 g_object_notify (G_OBJECT (entry), "overwrite-mode");
6370 * gtk_entry_get_overwrite_mode:
6371 * @entry: a #GtkEntry
6373 * Gets the value set by gtk_entry_set_overwrite_mode().
6375 * Return value: whether the text is overwritten when typing.
6380 gtk_entry_get_overwrite_mode (GtkEntry *entry)
6382 g_return_val_if_fail (GTK_IS_ENTRY (entry), FALSE);
6384 return entry->overwrite_mode;
6388 * gtk_entry_get_text:
6389 * @entry: a #GtkEntry
6391 * Retrieves the contents of the entry widget.
6392 * See also gtk_editable_get_chars().
6394 * Return value: a pointer to the contents of the widget as a
6395 * string. This string points to internally allocated
6396 * storage in the widget and must not be freed, modified or
6399 G_CONST_RETURN gchar*
6400 gtk_entry_get_text (GtkEntry *entry)
6402 g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
6408 * gtk_entry_select_region:
6409 * @entry: a #GtkEntry
6410 * @start: the starting position
6411 * @end: the end position
6413 * Selects a region of text. The characters that are selected are
6414 * those characters at positions from @start_pos up to, but not
6415 * including @end_pos. If @end_pos is negative, then the the characters
6416 * selected will be those characters from @start_pos to the end of
6419 * Deprecated: 2.0: Use gtk_editable_select_region() instead.
6422 gtk_entry_select_region (GtkEntry *entry,
6426 gtk_editable_select_region (GTK_EDITABLE (entry), start, end);
6430 * gtk_entry_set_max_length:
6431 * @entry: a #GtkEntry
6432 * @max: the maximum length of the entry, or 0 for no maximum.
6433 * (other than the maximum length of entries.) The value passed in will
6434 * be clamped to the range 0-65536.
6436 * Sets the maximum allowed length of the contents of the widget. If
6437 * the current contents are longer than the given length, then they
6438 * will be truncated to fit.
6441 gtk_entry_set_max_length (GtkEntry *entry,
6444 g_return_if_fail (GTK_IS_ENTRY (entry));
6446 max = CLAMP (max, 0, MAX_SIZE);
6448 if (max > 0 && entry->text_length > max)
6449 gtk_editable_delete_text (GTK_EDITABLE (entry), max, -1);
6451 entry->text_max_length = max;
6452 g_object_notify (G_OBJECT (entry), "max-length");
6456 * gtk_entry_get_max_length:
6457 * @entry: a #GtkEntry
6459 * Retrieves the maximum allowed length of the text in
6460 * @entry. See gtk_entry_set_max_length().
6462 * Return value: the maximum allowed number of characters
6463 * in #GtkEntry, or 0 if there is no maximum.
6466 gtk_entry_get_max_length (GtkEntry *entry)
6468 g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
6470 return entry->text_max_length;
6474 * gtk_entry_get_text_length:
6475 * @entry: a #GtkEntry
6477 * Retrieves the current length of the text in
6480 * Return value: the current number of characters
6481 * in #GtkEntry, or 0 if there are none.
6486 gtk_entry_get_text_length (GtkEntry *entry)
6488 g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
6490 return entry->text_length;
6494 * gtk_entry_set_activates_default:
6495 * @entry: a #GtkEntry
6496 * @setting: %TRUE to activate window's default widget on Enter keypress
6498 * If @setting is %TRUE, pressing Enter in the @entry will activate the default
6499 * widget for the window containing the entry. This usually means that
6500 * the dialog box containing the entry will be closed, since the default
6501 * widget is usually one of the dialog buttons.
6503 * (For experts: if @setting is %TRUE, the entry calls
6504 * gtk_window_activate_default() on the window containing the entry, in
6505 * the default handler for the #GtkWidget::activate signal.)
6508 gtk_entry_set_activates_default (GtkEntry *entry,
6511 g_return_if_fail (GTK_IS_ENTRY (entry));
6512 setting = setting != FALSE;
6514 if (setting != entry->activates_default)
6516 entry->activates_default = setting;
6517 g_object_notify (G_OBJECT (entry), "activates-default");
6522 * gtk_entry_get_activates_default:
6523 * @entry: a #GtkEntry
6525 * Retrieves the value set by gtk_entry_set_activates_default().
6527 * Return value: %TRUE if the entry will activate the default widget
6530 gtk_entry_get_activates_default (GtkEntry *entry)
6532 g_return_val_if_fail (GTK_IS_ENTRY (entry), FALSE);
6534 return entry->activates_default;
6538 * gtk_entry_set_width_chars:
6539 * @entry: a #GtkEntry
6540 * @n_chars: width in chars
6542 * Changes the size request of the entry to be about the right size
6543 * for @n_chars characters. Note that it changes the size
6544 * <emphasis>request</emphasis>, the size can still be affected by
6545 * how you pack the widget into containers. If @n_chars is -1, the
6546 * size reverts to the default entry size.
6549 gtk_entry_set_width_chars (GtkEntry *entry,
6552 g_return_if_fail (GTK_IS_ENTRY (entry));
6554 if (entry->width_chars != n_chars)
6556 entry->width_chars = n_chars;
6557 g_object_notify (G_OBJECT (entry), "width-chars");
6558 gtk_widget_queue_resize (GTK_WIDGET (entry));
6563 * gtk_entry_get_width_chars:
6564 * @entry: a #GtkEntry
6566 * Gets the value set by gtk_entry_set_width_chars().
6568 * Return value: number of chars to request space for, or negative if unset
6571 gtk_entry_get_width_chars (GtkEntry *entry)
6573 g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
6575 return entry->width_chars;
6579 * gtk_entry_set_has_frame:
6580 * @entry: a #GtkEntry
6581 * @setting: new value
6583 * Sets whether the entry has a beveled frame around it.
6586 gtk_entry_set_has_frame (GtkEntry *entry,
6589 g_return_if_fail (GTK_IS_ENTRY (entry));
6591 setting = (setting != FALSE);
6593 if (entry->has_frame == setting)
6596 gtk_widget_queue_resize (GTK_WIDGET (entry));
6597 entry->has_frame = setting;
6598 g_object_notify (G_OBJECT (entry), "has-frame");
6602 * gtk_entry_get_has_frame:
6603 * @entry: a #GtkEntry
6605 * Gets the value set by gtk_entry_set_has_frame().
6607 * Return value: whether the entry has a beveled frame
6610 gtk_entry_get_has_frame (GtkEntry *entry)
6612 g_return_val_if_fail (GTK_IS_ENTRY (entry), FALSE);
6614 return entry->has_frame;
6618 * gtk_entry_set_inner_border:
6619 * @entry: a #GtkEntry
6620 * @border: a #GtkBorder, or %NULL
6622 * Sets %entry's inner-border property to %border, or clears it if %NULL
6623 * is passed. The inner-border is the area around the entry's text, but
6626 * If set, this property overrides the inner-border style property.
6627 * Overriding the style-provided border is useful when you want to do
6628 * in-place editing of some text in a canvas or list widget, where
6629 * pixel-exact positioning of the entry is important.
6634 gtk_entry_set_inner_border (GtkEntry *entry,
6635 const GtkBorder *border)
6637 g_return_if_fail (GTK_IS_ENTRY (entry));
6639 gtk_widget_queue_resize (GTK_WIDGET (entry));
6642 g_object_set_qdata_full (G_OBJECT (entry), quark_inner_border,
6643 gtk_border_copy (border),
6644 (GDestroyNotify) gtk_border_free);
6646 g_object_set_qdata (G_OBJECT (entry), quark_inner_border, NULL);
6648 g_object_notify (G_OBJECT (entry), "inner-border");
6652 * gtk_entry_get_inner_border:
6653 * @entry: a #GtkEntry
6655 * This function returns the entry's #GtkEntry:inner-border property. See
6656 * gtk_entry_set_inner_border() for more information.
6658 * Return value: the entry's #GtkBorder, or %NULL if none was set.
6662 G_CONST_RETURN GtkBorder *
6663 gtk_entry_get_inner_border (GtkEntry *entry)
6665 g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
6667 return g_object_get_qdata (G_OBJECT (entry), quark_inner_border);
6671 * gtk_entry_get_layout:
6672 * @entry: a #GtkEntry
6674 * Gets the #PangoLayout used to display the entry.
6675 * The layout is useful to e.g. convert text positions to
6676 * pixel positions, in combination with gtk_entry_get_layout_offsets().
6677 * The returned layout is owned by the entry and must not be
6678 * modified or freed by the caller.
6680 * Keep in mind that the layout text may contain a preedit string, so
6681 * gtk_entry_layout_index_to_text_index() and
6682 * gtk_entry_text_index_to_layout_index() are needed to convert byte
6683 * indices in the layout to byte indices in the entry contents.
6685 * Return value: the #PangoLayout for this entry
6688 gtk_entry_get_layout (GtkEntry *entry)
6690 PangoLayout *layout;
6692 g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
6694 layout = gtk_entry_ensure_layout (entry, TRUE);
6701 * gtk_entry_layout_index_to_text_index:
6702 * @entry: a #GtkEntry
6703 * @layout_index: byte index into the entry layout text
6705 * Converts from a position in the entry contents (returned
6706 * by gtk_entry_get_text()) to a position in the
6707 * entry's #PangoLayout (returned by gtk_entry_get_layout(),
6708 * with text retrieved via pango_layout_get_text()).
6710 * Return value: byte index into the entry contents
6713 gtk_entry_layout_index_to_text_index (GtkEntry *entry,
6716 PangoLayout *layout;
6720 g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
6722 layout = gtk_entry_ensure_layout (entry, TRUE);
6723 text = pango_layout_get_text (layout);
6724 cursor_index = g_utf8_offset_to_pointer (text, entry->current_pos) - text;
6726 if (layout_index >= cursor_index && entry->preedit_length)
6728 if (layout_index >= cursor_index + entry->preedit_length)
6729 layout_index -= entry->preedit_length;
6731 layout_index = cursor_index;
6734 return layout_index;
6738 * gtk_entry_text_index_to_layout_index:
6739 * @entry: a #GtkEntry
6740 * @text_index: byte index into the entry contents
6742 * Converts from a position in the entry's #PangoLayout (returned by
6743 * gtk_entry_get_layout()) to a position in the entry contents
6744 * (returned by gtk_entry_get_text()).
6746 * Return value: byte index into the entry layout text
6749 gtk_entry_text_index_to_layout_index (GtkEntry *entry,
6752 PangoLayout *layout;
6755 g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
6757 layout = gtk_entry_ensure_layout (entry, TRUE);
6758 text = pango_layout_get_text (layout);
6759 cursor_index = g_utf8_offset_to_pointer (text, entry->current_pos) - text;
6761 if (text_index > cursor_index)
6762 text_index += entry->preedit_length;
6768 * gtk_entry_get_layout_offsets:
6769 * @entry: a #GtkEntry
6770 * @x: location to store X offset of layout, or %NULL
6771 * @y: location to store Y offset of layout, or %NULL
6774 * Obtains the position of the #PangoLayout used to render text
6775 * in the entry, in widget coordinates. Useful if you want to line
6776 * up the text in an entry with some other text, e.g. when using the
6777 * entry to implement editable cells in a sheet widget.
6779 * Also useful to convert mouse events into coordinates inside the
6780 * #PangoLayout, e.g. to take some action if some part of the entry text
6783 * Note that as the user scrolls around in the entry the offsets will
6784 * change; you'll need to connect to the "notify::scroll-offset"
6785 * signal to track this. Remember when using the #PangoLayout
6786 * functions you need to convert to and from pixels using
6787 * PANGO_PIXELS() or #PANGO_SCALE.
6789 * Keep in mind that the layout text may contain a preedit string, so
6790 * gtk_entry_layout_index_to_text_index() and
6791 * gtk_entry_text_index_to_layout_index() are needed to convert byte
6792 * indices in the layout to byte indices in the entry contents.
6795 gtk_entry_get_layout_offsets (GtkEntry *entry,
6799 gint text_area_x, text_area_y;
6801 g_return_if_fail (GTK_IS_ENTRY (entry));
6803 /* this gets coords relative to text area */
6804 get_layout_position (entry, x, y);
6806 /* convert to widget coords */
6807 gtk_entry_get_text_area_size (entry, &text_area_x, &text_area_y, NULL, NULL);
6818 * gtk_entry_set_alignment:
6819 * @entry: a #GtkEntry
6820 * @xalign: The horizontal alignment, from 0 (left) to 1 (right).
6821 * Reversed for RTL layouts
6823 * Sets the alignment for the contents of the entry. This controls
6824 * the horizontal positioning of the contents when the displayed
6825 * text is shorter than the width of the entry.
6830 gtk_entry_set_alignment (GtkEntry *entry, gfloat xalign)
6832 GtkEntryPrivate *priv;
6834 g_return_if_fail (GTK_IS_ENTRY (entry));
6836 priv = GTK_ENTRY_GET_PRIVATE (entry);
6840 else if (xalign > 1.0)
6843 if (xalign != priv->xalign)
6845 priv->xalign = xalign;
6847 gtk_entry_recompute (entry);
6849 g_object_notify (G_OBJECT (entry), "xalign");
6854 * gtk_entry_get_alignment:
6855 * @entry: a #GtkEntry
6857 * Gets the value set by gtk_entry_set_alignment().
6859 * Return value: the alignment
6864 gtk_entry_get_alignment (GtkEntry *entry)
6866 GtkEntryPrivate *priv;
6868 g_return_val_if_fail (GTK_IS_ENTRY (entry), 0.0);
6870 priv = GTK_ENTRY_GET_PRIVATE (entry);
6872 return priv->xalign;
6876 * gtk_entry_set_icon_from_pixbuf:
6877 * @entry: a #GtkEntry
6878 * @position: Icon position
6879 * @pixbuf: A #GdkPixbuf, or %NULL
6881 * Sets the icon shown in the specified position using a pixbuf.
6883 * If @pixbuf is %NULL, no icon will be shown in the specified position.
6888 gtk_entry_set_icon_from_pixbuf (GtkEntry *entry,
6889 GtkEntryIconPosition icon_pos,
6892 GtkEntryPrivate *priv;
6893 EntryIconInfo *icon_info;
6895 g_return_if_fail (GTK_IS_ENTRY (entry));
6896 g_return_if_fail (IS_VALID_ICON_POSITION (icon_pos));
6898 priv = GTK_ENTRY_GET_PRIVATE (entry);
6900 if (!(icon_info = priv->icons[icon_pos]))
6901 icon_info = construct_icon_info (GTK_WIDGET (entry), icon_pos);
6903 g_object_freeze_notify (G_OBJECT (entry));
6906 g_object_ref (pixbuf);
6908 gtk_entry_clear (entry, icon_pos);
6912 icon_info->storage_type = GTK_IMAGE_PIXBUF;
6913 icon_info->pixbuf = pixbuf;
6915 if (icon_pos == GTK_ENTRY_ICON_PRIMARY)
6917 g_object_notify (G_OBJECT (entry), "pixbuf-primary");
6918 g_object_notify (G_OBJECT (entry), "storage-type-primary");
6922 g_object_notify (G_OBJECT (entry), "pixbuf-secondary");
6923 g_object_notify (G_OBJECT (entry), "storage-type-secondary");
6927 gtk_entry_ensure_pixbuf (entry, icon_pos);
6929 g_object_thaw_notify (G_OBJECT (entry));
6933 * gtk_entry_set_icon_from_stock:
6934 * @entry: A #GtkEntry
6935 * @position: Icon position
6936 * @stock_id: The name of the stock item, or %NULL
6938 * Sets the icon shown in the entry at the specified position from
6941 * If @stock_id is %NULL, no icon will be shown in the specified position.
6946 gtk_entry_set_icon_from_stock (GtkEntry *entry,
6947 GtkEntryIconPosition icon_pos,
6948 const gchar *stock_id)
6950 GtkEntryPrivate *priv;
6951 EntryIconInfo *icon_info;
6954 g_return_if_fail (GTK_IS_ENTRY (entry));
6955 g_return_if_fail (IS_VALID_ICON_POSITION (icon_pos));
6957 priv = GTK_ENTRY_GET_PRIVATE (entry);
6959 if (!(icon_info = priv->icons[icon_pos]))
6960 icon_info = construct_icon_info (GTK_WIDGET (entry), icon_pos);
6962 g_object_freeze_notify (G_OBJECT (entry));
6964 gtk_widget_ensure_style (GTK_WIDGET (entry));
6966 /* need to dup before clearing */
6967 new_id = g_strdup (stock_id);
6969 gtk_entry_clear (entry, icon_pos);
6973 icon_info->storage_type = GTK_IMAGE_STOCK;
6974 icon_info->stock_id = new_id;
6976 if (icon_pos == GTK_ENTRY_ICON_PRIMARY)
6978 g_object_notify (G_OBJECT (entry), "stock-primary");
6979 g_object_notify (G_OBJECT (entry), "storage-type-primary");
6983 g_object_notify (G_OBJECT (entry), "stock-secondary");
6984 g_object_notify (G_OBJECT (entry), "storage-type-secondary");
6988 gtk_entry_ensure_pixbuf (entry, icon_pos);
6990 g_object_thaw_notify (G_OBJECT (entry));
6994 * gtk_entry_set_icon_from_icon_name:
6995 * @entry: A #GtkEntry
6996 * @icon_pos: The position at which to set the icon
6997 * @icon_name: An icon name, or %NULL
6999 * Sets the icon shown in the entry at the specified position
7000 * from the current icon theme.
7002 * If the icon name isn't known, a "broken image" icon will be displayed
7005 * If @icon_name is %NULL, no icon will be shown in the specified position.
7010 gtk_entry_set_icon_from_icon_name (GtkEntry *entry,
7011 GtkEntryIconPosition icon_pos,
7012 const gchar *icon_name)
7014 GtkEntryPrivate *priv;
7015 EntryIconInfo *icon_info;
7018 g_return_if_fail (GTK_IS_ENTRY (entry));
7019 g_return_if_fail (IS_VALID_ICON_POSITION (icon_pos));
7021 priv = GTK_ENTRY_GET_PRIVATE (entry);
7023 if (!(icon_info = priv->icons[icon_pos]))
7024 icon_info = construct_icon_info (GTK_WIDGET (entry), icon_pos);
7026 g_object_freeze_notify (G_OBJECT (entry));
7028 gtk_widget_ensure_style (GTK_WIDGET (entry));
7030 /* need to dup before clearing */
7031 new_name = g_strdup (icon_name);
7033 gtk_entry_clear (entry, icon_pos);
7035 if (new_name != NULL)
7037 icon_info->storage_type = GTK_IMAGE_ICON_NAME;
7038 icon_info->icon_name = new_name;
7040 if (icon_pos == GTK_ENTRY_ICON_PRIMARY)
7042 g_object_notify (G_OBJECT (entry), "icon-name-primary");
7043 g_object_notify (G_OBJECT (entry), "storage-type-primary");
7047 g_object_notify (G_OBJECT (entry), "icon-name-secondary");
7048 g_object_notify (G_OBJECT (entry), "storage-type-secondary");
7052 gtk_entry_ensure_pixbuf (entry, icon_pos);
7054 g_object_thaw_notify (G_OBJECT (entry));
7058 * gtk_entry_set_icon_from_gicon:
7059 * @entry: A #GtkEntry
7060 * @icon_pos: The position at which to set the icon
7061 * @icon: The icon to set, or %NULL
7063 * Sets the icon shown in the entry at the specified position
7064 * from the current icon theme.
7065 * If the icon isn't known, a "broken image" icon will be displayed
7068 * If @icon is %NULL, no icon will be shown in the specified position.
7073 gtk_entry_set_icon_from_gicon (GtkEntry *entry,
7074 GtkEntryIconPosition icon_pos,
7077 GtkEntryPrivate *priv;
7078 EntryIconInfo *icon_info;
7080 g_return_if_fail (GTK_IS_ENTRY (entry));
7081 g_return_if_fail (IS_VALID_ICON_POSITION (icon_pos));
7083 priv = GTK_ENTRY_GET_PRIVATE (entry);
7085 if (!(icon_info = priv->icons[icon_pos]))
7086 icon_info = construct_icon_info (GTK_WIDGET (entry), icon_pos);
7088 g_object_freeze_notify (G_OBJECT (entry));
7090 /* need to ref before clearing */
7092 g_object_ref (icon);
7094 gtk_entry_clear (entry, icon_pos);
7098 icon_info->storage_type = GTK_IMAGE_GICON;
7099 icon_info->gicon = icon;
7101 if (icon_pos == GTK_ENTRY_ICON_PRIMARY)
7103 g_object_notify (G_OBJECT (entry), "gicon-primary");
7104 g_object_notify (G_OBJECT (entry), "storage-type-primary");
7108 g_object_notify (G_OBJECT (entry), "gicon-secondary");
7109 g_object_notify (G_OBJECT (entry), "storage-type-secondary");
7113 gtk_entry_ensure_pixbuf (entry, icon_pos);
7115 g_object_thaw_notify (G_OBJECT (entry));
7119 * gtk_entry_set_icon_activatable:
7120 * @entry: A #GtkEntry
7121 * @position: Icon position
7122 * @activatable: %TRUE if the icon should be activatable
7124 * Sets whether the icon is activatable.
7129 gtk_entry_set_icon_activatable (GtkEntry *entry,
7130 GtkEntryIconPosition icon_pos,
7131 gboolean activatable)
7133 GtkEntryPrivate *priv;
7134 EntryIconInfo *icon_info;
7136 g_return_if_fail (GTK_IS_ENTRY (entry));
7137 g_return_if_fail (IS_VALID_ICON_POSITION (icon_pos));
7139 priv = GTK_ENTRY_GET_PRIVATE (entry);
7141 if (!(icon_info = priv->icons[icon_pos]))
7142 icon_info = construct_icon_info (GTK_WIDGET (entry), icon_pos);
7144 activatable = activatable != FALSE;
7146 if (icon_info->nonactivatable != !activatable)
7148 icon_info->nonactivatable = !activatable;
7150 if (GTK_WIDGET_REALIZED (GTK_WIDGET (entry)))
7151 update_cursors (GTK_WIDGET (entry));
7153 g_object_notify (G_OBJECT (entry),
7154 icon_pos == GTK_ENTRY_ICON_PRIMARY ? "activatable-primary" : "activatable-secondary");
7159 * gtk_entry_get_icon_activtable:
7160 * @entry: a #GtkEntry
7161 * @icon_pos: Icon position
7163 * Returns whether the icon is activatable.
7165 * Returns: %TRUE if the icon is activatable.
7170 gtk_entry_get_icon_activatable (GtkEntry *entry,
7171 GtkEntryIconPosition icon_pos)
7173 GtkEntryPrivate *priv;
7174 EntryIconInfo *icon_info;
7176 g_return_val_if_fail (GTK_IS_ENTRY (entry), FALSE);
7177 g_return_val_if_fail (IS_VALID_ICON_POSITION (icon_pos), FALSE);
7179 priv = GTK_ENTRY_GET_PRIVATE (entry);
7180 icon_info = priv->icons[icon_pos];
7182 return (icon_info != NULL && !icon_info->nonactivatable);
7186 * gtk_entry_get_pixbuf:
7187 * @entry: A #GtkEntry
7188 * @position: Icon position
7190 * Retrieves the image used for the icon.
7192 * Unlike the other methods of setting and getting icon data, this
7193 * method will work regardless of whether the icon was set using a
7194 * #GdkPixbuf, a #GIcon, a stock item, or an icon name.
7196 * Returns: A #GdkPixbuf, or %NULL if no icon is set for this position.
7201 gtk_entry_get_pixbuf (GtkEntry *entry,
7202 GtkEntryIconPosition icon_pos)
7204 GtkEntryPrivate *priv;
7205 EntryIconInfo *icon_info;
7207 g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
7208 g_return_val_if_fail (IS_VALID_ICON_POSITION (icon_pos), NULL);
7210 priv = GTK_ENTRY_GET_PRIVATE (entry);
7211 icon_info = priv->icons[icon_pos];
7216 gtk_entry_ensure_pixbuf (entry, icon_pos);
7218 return icon_info->pixbuf;
7222 * gtk_entry_get_gicon:
7223 * @entry: A #GtkEntry
7224 * @position: Icon position
7226 * Retrieves the #GIcon used for the icon, or %NULL if there is
7227 * no icon or if the icon was set by some other method (e.g., by
7228 * stock, pixbuf, or icon name).
7230 * Returns: A #GIcon, or %NULL if no icon is set or if the icon
7236 gtk_entry_get_gicon (GtkEntry *entry,
7237 GtkEntryIconPosition icon_pos)
7239 GtkEntryPrivate *priv;
7240 EntryIconInfo *icon_info;
7242 g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
7243 g_return_val_if_fail (IS_VALID_ICON_POSITION (icon_pos), NULL);
7245 priv = GTK_ENTRY_GET_PRIVATE (entry);
7246 icon_info = priv->icons[icon_pos];
7251 return icon_info->storage_type == GTK_IMAGE_GICON ? icon_info->gicon : NULL;
7255 * gtk_entry_get_stock:
7256 * @entry: A #GtkEntry
7257 * @position: Icon position
7259 * Retrieves the stock id used for the icon, or %NULL if there is
7260 * no icon or if the icon was set by some other method (e.g., by
7261 * pixbuf, icon name or gicon).
7263 * Returns: A stock id, or %NULL if no icon is set or if the icon
7264 * wasn't set from a stock id
7269 gtk_entry_get_stock (GtkEntry *entry,
7270 GtkEntryIconPosition icon_pos)
7272 GtkEntryPrivate *priv;
7273 EntryIconInfo *icon_info;
7275 g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
7276 g_return_val_if_fail (IS_VALID_ICON_POSITION (icon_pos), NULL);
7278 priv = GTK_ENTRY_GET_PRIVATE (entry);
7279 icon_info = priv->icons[icon_pos];
7284 return icon_info->storage_type == GTK_IMAGE_STOCK ? icon_info->stock_id : NULL;
7288 * gtk_entry_get_icon_name:
7289 * @entry: A #GtkEntry
7290 * @position: Icon position
7292 * Retrieves the icon name used for the icon, or %NULL if there is
7293 * no icon or if the icon was set by some other method (e.g., by
7294 * pixbuf, stock or gicon).
7296 * Returns: An icon name, or %NULL if no icon is set or if the icon
7297 * wasn't set from an icon name
7302 gtk_entry_get_icon_name (GtkEntry *entry,
7303 GtkEntryIconPosition icon_pos)
7305 GtkEntryPrivate *priv;
7306 EntryIconInfo *icon_info;
7308 g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
7309 g_return_val_if_fail (IS_VALID_ICON_POSITION (icon_pos), NULL);
7311 priv = GTK_ENTRY_GET_PRIVATE (entry);
7312 icon_info = priv->icons[icon_pos];
7317 return icon_info->storage_type == GTK_IMAGE_ICON_NAME ? icon_info->icon_name : NULL;
7321 * gtk_entry_set_icon_sensitive:
7322 * @entry: A #GtkEntry
7323 * @position: Icon position
7324 * @sensitive: Specifies whether the icon should appear
7325 * sensitive or insensitive
7327 * Sets the sensitivity for the specified icon.
7332 gtk_entry_set_icon_sensitive (GtkEntry *entry,
7333 GtkEntryIconPosition icon_pos,
7336 GtkEntryPrivate *priv;
7337 EntryIconInfo *icon_info;
7339 g_return_if_fail (GTK_IS_ENTRY (entry));
7340 g_return_if_fail (IS_VALID_ICON_POSITION (icon_pos));
7342 priv = GTK_ENTRY_GET_PRIVATE (entry);
7344 if (!(icon_info = priv->icons[icon_pos]))
7345 icon_info = construct_icon_info (GTK_WIDGET (entry), icon_pos);
7347 if (icon_info->insensitive != !sensitive)
7349 icon_info->insensitive = !sensitive;
7351 if (GTK_WIDGET_REALIZED (GTK_WIDGET (entry)))
7352 update_cursors (GTK_WIDGET (entry));
7354 g_object_notify (G_OBJECT (entry),
7355 icon_pos == GTK_ENTRY_ICON_PRIMARY ? "sensitive-primary" : "sensitive-secondary");
7360 * gtk_entry_get_icon_sensitive:
7361 * @entry: a #GtkEntry
7362 * @icon_pos: Icon position
7364 * Returns whether the icon appears sensitive or insensitive.
7366 * Returns: %TRUE if the icon is sensitive.
7371 gtk_entry_get_icon_sensitive (GtkEntry *entry,
7372 GtkEntryIconPosition icon_pos)
7374 GtkEntryPrivate *priv;
7375 EntryIconInfo *icon_info;
7377 g_return_val_if_fail (GTK_IS_ENTRY (entry), TRUE);
7378 g_return_val_if_fail (IS_VALID_ICON_POSITION (icon_pos), TRUE);
7380 priv = GTK_ENTRY_GET_PRIVATE (entry);
7381 icon_info = priv->icons[icon_pos];
7383 return (icon_info != NULL && !icon_info->insensitive);
7387 * gtk_entry_get_storage_type:
7388 * @image: a #GtkImage
7390 * Gets the type of representation being used by the icon
7391 * to store image data. If the icon has no image data,
7392 * the return value will be %GTK_IMAGE_EMPTY.
7394 * Return value: image representation being used
7399 gtk_entry_get_storage_type (GtkEntry *entry,
7400 GtkEntryIconPosition icon_pos)
7402 GtkEntryPrivate *priv;
7403 EntryIconInfo *icon_info;
7405 g_return_val_if_fail (GTK_IS_ENTRY (entry), GTK_IMAGE_EMPTY);
7406 g_return_val_if_fail (IS_VALID_ICON_POSITION (icon_pos), GTK_IMAGE_EMPTY);
7408 priv = GTK_ENTRY_GET_PRIVATE (entry);
7409 icon_info = priv->icons[icon_pos];
7412 return GTK_IMAGE_EMPTY;
7414 return icon_info->storage_type;
7418 * gtk_entry_get_icon_at_pos:
7419 * @entry: a #GtkEntry
7420 * @x: the x coordinate of the position to find
7421 * @y: the y coordinate of the position to find
7423 * Finds the icon at the given position and return its index.
7424 * If @x, @y doesn't lie inside an icon, -1 is returned.
7425 * This function is intended for use in a #GtkWidget::query-tooltip
7428 * Returns: the index of the icon at the given position, or -1
7433 gtk_entry_get_icon_at_pos (GtkEntry *entry,
7437 GtkEntryPrivate *priv;
7438 GtkEntryIconPosition left_icon_pos;
7439 GtkEntryIconPosition right_icon_pos;
7440 GtkAllocation left_icon_alloc;
7441 GtkAllocation right_icon_alloc;
7442 GtkAllocation text_area_alloc;
7443 GtkAllocation *widget_alloc;
7445 g_return_val_if_fail (GTK_IS_ENTRY (entry), -1);
7447 priv = GTK_ENTRY_GET_PRIVATE (entry);
7449 widget_alloc = >K_WIDGET (entry)->allocation;
7451 get_text_area_size (entry, &text_area_alloc);
7452 get_icon_allocation (entry, TRUE, widget_alloc, &text_area_alloc,
7453 &left_icon_alloc, &left_icon_pos);
7454 get_icon_allocation (entry, FALSE, widget_alloc, &text_area_alloc,
7455 &right_icon_alloc, &right_icon_pos);
7457 if (left_icon_alloc.width > 0)
7458 text_area_alloc.x = left_icon_alloc.x + left_icon_alloc.width + priv->icon_margin;
7460 if (right_icon_alloc.width > 0)
7461 text_area_alloc.width -= right_icon_alloc.width + priv->icon_margin;
7463 text_area_alloc.width -= text_area_alloc.x;
7465 if (left_icon_alloc.x <= x &&
7466 left_icon_alloc.y <= y &&
7467 x < left_icon_alloc.x + left_icon_alloc.width &&
7468 y < left_icon_alloc.y + left_icon_alloc.height)
7469 return left_icon_pos;
7471 if (right_icon_alloc.x <= x &&
7472 right_icon_alloc.y <= y &&
7473 x < right_icon_alloc.x + right_icon_alloc.width &&
7474 y < right_icon_alloc.y + right_icon_alloc.height)
7475 return right_icon_pos;
7481 * gtk_icon_entry_set_icon_drag_source:
7482 * @entry: a #GtkIconEntry
7483 * @icon_pos: icon position
7484 * @target_list: the targets (data formats) in which the data can be provided
7485 * @actions: a bitmask of the allowed drag actions
7487 * Sets up the icon at the given position so that GTK+ will start a drag
7488 * operation when the user clicks and drags the icon.
7490 * To handle the drag operation, you need to connect to the usual
7491 * #GtkWidget::drag-data-get (or possibly #GtkWidget::drag-data-delete)
7492 * signal, and use gtk_icon_entry_get_current_icon_drag_source() in
7493 * your signal handler to find out if the drag was started from
7497 gtk_entry_set_icon_drag_source (GtkEntry *entry,
7498 GtkEntryIconPosition icon_pos,
7499 GtkTargetList *target_list,
7500 GdkDragAction actions)
7502 GtkEntryPrivate *priv;
7503 EntryIconInfo *icon_info;
7505 g_return_if_fail (GTK_IS_ENTRY (entry));
7506 g_return_if_fail (IS_VALID_ICON_POSITION (icon_pos));
7508 priv = GTK_ENTRY_GET_PRIVATE (entry);
7510 if (!(icon_info = priv->icons[icon_pos]))
7511 icon_info = construct_icon_info (GTK_WIDGET (entry), icon_pos);
7513 if (icon_info->target_list)
7514 gtk_target_list_unref (icon_info->target_list);
7515 icon_info->target_list = target_list;
7516 if (icon_info->target_list)
7517 gtk_target_list_ref (icon_info->target_list);
7519 icon_info->actions = actions;
7523 * gtk_icon_entry_get_current_icon_drag_source:
7524 * @entry: a #GtkIconEntry
7526 * Returns the index of the icon which is the source of the current
7527 * DND operation, or -1.
7529 * This function is meant to be used in a #GtkWidget::drag-data-get
7532 * Returns: index of the icon which is the source of the current
7533 * DND operation, or -1.
7536 gtk_entry_get_current_icon_drag_source (GtkEntry *entry)
7538 GtkEntryPrivate *priv;
7539 EntryIconInfo *icon_info = NULL;
7542 g_return_val_if_fail (GTK_IS_ENTRY (entry), -1);
7544 priv = GTK_ENTRY_GET_PRIVATE (entry);
7546 for (i = 0; i < MAX_ICONS; i++)
7548 if ((icon_info = priv->icons[i]))
7550 if (icon_info->in_drag)
7559 ensure_has_tooltip (GtkEntry *entry)
7561 GtkEntryPrivate *priv;
7562 EntryIconInfo *icon_info;
7564 gboolean has_tooltip = FALSE;
7566 priv = GTK_ENTRY_GET_PRIVATE (entry);
7568 for (i = 0; i < MAX_ICONS; i++)
7570 if ((icon_info = priv->icons[i]) != NULL)
7572 if (icon_info->tooltip != NULL)
7580 gtk_widget_set_has_tooltip (GTK_WIDGET (entry), has_tooltip);
7584 * gtk_entry_set_icon_tooltip_text:
7585 * @entry: a #GtkEntry
7586 * @icon_pos: the icon position
7587 * @tooltip: the contents of the tooltip for the icon, or %NULL
7589 * Sets @tooltip as the contents of the tooltip for the icon
7590 * at the specified position.
7592 * Use %NULL for @tooltip to remove an existing tooltip.
7594 * See also gtk_widget_set_tooltip_text().
7599 gtk_entry_set_icon_tooltip_text (GtkEntry *entry,
7600 GtkEntryIconPosition icon_pos,
7601 const gchar *tooltip)
7603 GtkEntryPrivate *priv;
7604 EntryIconInfo *icon_info;
7606 g_return_if_fail (GTK_IS_ENTRY (entry));
7607 g_return_if_fail (IS_VALID_ICON_POSITION (icon_pos));
7609 priv = GTK_ENTRY_GET_PRIVATE (entry);
7611 if (!(icon_info = priv->icons[icon_pos]))
7612 icon_info = priv->icons[icon_pos];
7614 if (icon_info->tooltip)
7615 g_free (icon_info->tooltip);
7617 icon_info->tooltip = tooltip ? g_markup_escape_text (tooltip, -1) : NULL;
7619 ensure_has_tooltip (entry);
7623 * gtk_entry_set_icon_tooltip_markup:
7624 * @entry: a #GtkEntry
7625 * @icon_pos: the icon position
7626 * @tooltip: the contents of the tooltip for the icon, or %NULL
7628 * Sets @tooltip as the contents of the tooltip for the icon at
7629 * the specified position. @tooltip is assumed to be marked up with
7630 * the <link linkend="PangoMarkupFormat">Pango text markup language</link>.
7632 * Use %NULL for @tooltip to remove an existing tooltip.
7634 * See also gtk_widget_set_tooltip_markup().
7639 gtk_entry_set_icon_tooltip_markup (GtkEntry *entry,
7640 GtkEntryIconPosition icon_pos,
7641 const gchar *tooltip)
7643 GtkEntryPrivate *priv;
7644 EntryIconInfo *icon_info;
7646 g_return_if_fail (GTK_IS_ENTRY (entry));
7647 g_return_if_fail (IS_VALID_ICON_POSITION (icon_pos));
7649 priv = GTK_ENTRY_GET_PRIVATE (entry);
7651 if (!(icon_info = priv->icons[icon_pos]))
7652 icon_info = priv->icons[icon_pos];
7654 if (icon_info->tooltip)
7655 g_free (icon_info->tooltip);
7657 icon_info->tooltip = g_strdup (tooltip);
7659 ensure_has_tooltip (entry);
7663 gtk_entry_query_tooltip (GtkWidget *widget,
7666 gboolean keyboard_tip,
7667 GtkTooltip *tooltip)
7670 GtkEntryPrivate *priv;
7671 EntryIconInfo *icon_info;
7674 entry = GTK_ENTRY (widget);
7675 priv = GTK_ENTRY_GET_PRIVATE (entry);
7679 icon_pos = gtk_entry_get_icon_at_pos (entry, x, y);
7682 if ((icon_info = priv->icons[icon_pos]) != NULL)
7684 if (icon_info->tooltip)
7686 gtk_tooltip_set_markup (tooltip, icon_info->tooltip);
7695 return GTK_WIDGET_CLASS (gtk_entry_parent_class)->query_tooltip (widget,
7702 /* Quick hack of a popup menu
7705 activate_cb (GtkWidget *menuitem,
7708 const gchar *signal = g_object_get_data (G_OBJECT (menuitem), "gtk-signal");
7709 g_signal_emit_by_name (entry, signal);
7714 gtk_entry_mnemonic_activate (GtkWidget *widget,
7715 gboolean group_cycling)
7717 gtk_widget_grab_focus (widget);
7722 append_action_signal (GtkEntry *entry,
7724 const gchar *stock_id,
7725 const gchar *signal,
7728 GtkWidget *menuitem = gtk_image_menu_item_new_from_stock (stock_id, NULL);
7730 g_object_set_data (G_OBJECT (menuitem), I_("gtk-signal"), (char *)signal);
7731 g_signal_connect (menuitem, "activate",
7732 G_CALLBACK (activate_cb), entry);
7734 gtk_widget_set_sensitive (menuitem, sensitive);
7736 gtk_widget_show (menuitem);
7737 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
7741 popup_menu_detach (GtkWidget *attach_widget,
7744 GTK_ENTRY (attach_widget)->popup_menu = NULL;
7748 popup_position_func (GtkMenu *menu,
7754 GtkEntry *entry = GTK_ENTRY (user_data);
7755 GtkWidget *widget = GTK_WIDGET (entry);
7757 GtkRequisition menu_req;
7758 GdkRectangle monitor;
7759 GtkBorder inner_border;
7760 gint monitor_num, strong_x, height;
7762 g_return_if_fail (GTK_WIDGET_REALIZED (entry));
7764 gdk_window_get_origin (entry->text_area, x, y);
7766 screen = gtk_widget_get_screen (widget);
7767 monitor_num = gdk_screen_get_monitor_at_window (screen, entry->text_area);
7768 if (monitor_num < 0)
7770 gtk_menu_set_monitor (menu, monitor_num);
7772 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
7773 gtk_widget_size_request (entry->popup_menu, &menu_req);
7774 gdk_drawable_get_size (entry->text_area, NULL, &height);
7775 gtk_entry_get_cursor_locations (entry, CURSOR_STANDARD, &strong_x, NULL);
7776 _gtk_entry_effective_inner_border (entry, &inner_border);
7778 *x += inner_border.left + strong_x - entry->scroll_offset;
7779 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
7780 *x -= menu_req.width;
7782 if ((*y + height + menu_req.height) <= monitor.y + monitor.height)
7784 else if ((*y - menu_req.height) >= monitor.y)
7785 *y -= menu_req.height;
7786 else if (monitor.y + monitor.height - (*y + height) > *y)
7789 *y -= menu_req.height;
7795 unichar_chosen_func (const char *text,
7798 GtkEntry *entry = GTK_ENTRY (data);
7800 if (entry->editable)
7801 gtk_entry_enter_text (entry, text);
7812 popup_targets_received (GtkClipboard *clipboard,
7813 GtkSelectionData *data,
7816 PopupInfo *info = user_data;
7817 GtkEntry *entry = info->entry;
7819 if (GTK_WIDGET_REALIZED (entry))
7821 gboolean clipboard_contains_text;
7822 GtkWidget *menuitem;
7824 gboolean show_input_method_menu;
7825 gboolean show_unicode_menu;
7827 clipboard_contains_text = gtk_selection_data_targets_include_text (data);
7828 if (entry->popup_menu)
7829 gtk_widget_destroy (entry->popup_menu);
7831 entry->popup_menu = gtk_menu_new ();
7833 gtk_menu_attach_to_widget (GTK_MENU (entry->popup_menu),
7837 append_action_signal (entry, entry->popup_menu, GTK_STOCK_CUT, "cut-clipboard",
7838 entry->editable && entry->visible && entry->current_pos != entry->selection_bound);
7839 append_action_signal (entry, entry->popup_menu, GTK_STOCK_COPY, "copy-clipboard",
7840 entry->visible && entry->current_pos != entry->selection_bound);
7841 append_action_signal (entry, entry->popup_menu, GTK_STOCK_PASTE, "paste-clipboard",
7842 entry->editable && clipboard_contains_text);
7844 menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_DELETE, NULL);
7845 gtk_widget_set_sensitive (menuitem, entry->editable && entry->current_pos != entry->selection_bound);
7846 g_signal_connect_swapped (menuitem, "activate",
7847 G_CALLBACK (gtk_entry_delete_cb), entry);
7848 gtk_widget_show (menuitem);
7849 gtk_menu_shell_append (GTK_MENU_SHELL (entry->popup_menu), menuitem);
7851 menuitem = gtk_separator_menu_item_new ();
7852 gtk_widget_show (menuitem);
7853 gtk_menu_shell_append (GTK_MENU_SHELL (entry->popup_menu), menuitem);
7855 menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_SELECT_ALL, NULL);
7856 g_signal_connect_swapped (menuitem, "activate",
7857 G_CALLBACK (gtk_entry_select_all), entry);
7858 gtk_widget_show (menuitem);
7859 gtk_menu_shell_append (GTK_MENU_SHELL (entry->popup_menu), menuitem);
7861 g_object_get (gtk_widget_get_settings (GTK_WIDGET (entry)),
7862 "gtk-show-input-method-menu", &show_input_method_menu,
7863 "gtk-show-unicode-menu", &show_unicode_menu,
7866 if (show_input_method_menu || show_unicode_menu)
7868 menuitem = gtk_separator_menu_item_new ();
7869 gtk_widget_show (menuitem);
7870 gtk_menu_shell_append (GTK_MENU_SHELL (entry->popup_menu), menuitem);
7873 if (show_input_method_menu)
7875 menuitem = gtk_menu_item_new_with_mnemonic (_("Input _Methods"));
7876 gtk_widget_set_sensitive (menuitem, entry->editable);
7877 gtk_widget_show (menuitem);
7878 submenu = gtk_menu_new ();
7879 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
7881 gtk_menu_shell_append (GTK_MENU_SHELL (entry->popup_menu), menuitem);
7883 gtk_im_multicontext_append_menuitems (GTK_IM_MULTICONTEXT (entry->im_context),
7884 GTK_MENU_SHELL (submenu));
7887 if (show_unicode_menu)
7889 menuitem = gtk_menu_item_new_with_mnemonic (_("_Insert Unicode Control Character"));
7890 gtk_widget_set_sensitive (menuitem, entry->editable);
7891 gtk_widget_show (menuitem);
7893 submenu = gtk_menu_new ();
7894 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
7895 gtk_menu_shell_append (GTK_MENU_SHELL (entry->popup_menu), menuitem);
7897 _gtk_text_util_append_special_char_menuitems (GTK_MENU_SHELL (submenu),
7898 unichar_chosen_func,
7902 g_signal_emit (entry,
7903 signals[POPULATE_POPUP],
7909 gtk_menu_popup (GTK_MENU (entry->popup_menu), NULL, NULL,
7911 info->button, info->time);
7914 gtk_menu_popup (GTK_MENU (entry->popup_menu), NULL, NULL,
7915 popup_position_func, entry,
7916 info->button, info->time);
7917 gtk_menu_shell_select_first (GTK_MENU_SHELL (entry->popup_menu), FALSE);
7921 g_object_unref (entry);
7922 g_slice_free (PopupInfo, info);
7926 gtk_entry_do_popup (GtkEntry *entry,
7927 GdkEventButton *event)
7929 PopupInfo *info = g_slice_new (PopupInfo);
7931 /* In order to know what entries we should make sensitive, we
7932 * ask for the current targets of the clipboard, and when
7933 * we get them, then we actually pop up the menu.
7935 info->entry = g_object_ref (entry);
7939 info->button = event->button;
7940 info->time = event->time;
7945 info->time = gtk_get_current_event_time ();
7948 gtk_clipboard_request_contents (gtk_widget_get_clipboard (GTK_WIDGET (entry), GDK_SELECTION_CLIPBOARD),
7949 gdk_atom_intern_static_string ("TARGETS"),
7950 popup_targets_received,
7955 gtk_entry_popup_menu (GtkWidget *widget)
7957 gtk_entry_do_popup (GTK_ENTRY (widget), NULL);
7962 gtk_entry_drag_begin (GtkWidget *widget,
7963 GdkDragContext *context)
7965 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (widget);
7966 EntryIconInfo *icon_info;
7969 for (i = 0; i < MAX_ICONS; i++)
7971 if ((icon_info = priv->icons[i]) != NULL)
7973 if (icon_info->in_drag)
7975 switch (icon_info->storage_type)
7977 case GTK_IMAGE_STOCK:
7978 gtk_drag_set_icon_stock (context, icon_info->stock_id, -2, -2);
7981 case GTK_IMAGE_ICON_NAME:
7982 gtk_drag_set_icon_name (context, icon_info->icon_name, -2, -2);
7985 /* FIXME: No GIcon support for dnd icons */
7986 case GTK_IMAGE_GICON:
7987 case GTK_IMAGE_PIXBUF:
7988 gtk_drag_set_icon_pixbuf (context, icon_info->pixbuf, -2, -2);
7991 g_assert_not_reached ();
7999 gtk_entry_drag_end (GtkWidget *widget,
8000 GdkDragContext *context)
8002 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (widget);
8005 for (i = 0; i < MAX_ICONS; i++)
8007 EntryIconInfo *icon_info;
8009 if ((icon_info = priv->icons[i]) != NULL)
8010 icon_info->in_drag = 0;
8015 gtk_entry_drag_leave (GtkWidget *widget,
8016 GdkDragContext *context,
8019 GtkEntry *entry = GTK_ENTRY (widget);
8021 entry->dnd_position = -1;
8022 gtk_widget_queue_draw (widget);
8026 gtk_entry_drag_drop (GtkWidget *widget,
8027 GdkDragContext *context,
8032 GtkEntry *entry = GTK_ENTRY (widget);
8033 GdkAtom target = GDK_NONE;
8035 if (entry->editable)
8036 target = gtk_drag_dest_find_target (widget, context, NULL);
8038 if (target != GDK_NONE)
8039 gtk_drag_get_data (widget, context, target, time);
8041 gtk_drag_finish (context, FALSE, FALSE, time);
8047 gtk_entry_drag_motion (GtkWidget *widget,
8048 GdkDragContext *context,
8053 GtkEntry *entry = GTK_ENTRY (widget);
8054 GtkWidget *source_widget;
8055 GdkDragAction suggested_action;
8056 gint new_position, old_position;
8059 x -= widget->style->xthickness;
8060 y -= widget->style->ythickness;
8062 old_position = entry->dnd_position;
8063 new_position = gtk_entry_find_position (entry, x + entry->scroll_offset);
8065 if (entry->editable &&
8066 gtk_drag_dest_find_target (widget, context, NULL) != GDK_NONE)
8068 source_widget = gtk_drag_get_source_widget (context);
8069 suggested_action = context->suggested_action;
8071 if (!gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &sel1, &sel2) ||
8072 new_position < sel1 || new_position > sel2)
8074 if (source_widget == widget)
8076 /* Default to MOVE, unless the user has
8077 * pressed ctrl or alt to affect available actions
8079 if ((context->actions & GDK_ACTION_MOVE) != 0)
8080 suggested_action = GDK_ACTION_MOVE;
8083 entry->dnd_position = new_position;
8087 if (source_widget == widget)
8088 suggested_action = 0; /* Can't drop in selection where drag started */
8090 entry->dnd_position = -1;
8095 /* Entry not editable, or no text */
8096 suggested_action = 0;
8097 entry->dnd_position = -1;
8100 gdk_drag_status (context, suggested_action, time);
8102 if (entry->dnd_position != old_position)
8103 gtk_widget_queue_draw (widget);
8109 gtk_entry_drag_data_received (GtkWidget *widget,
8110 GdkDragContext *context,
8113 GtkSelectionData *selection_data,
8117 GtkEntry *entry = GTK_ENTRY (widget);
8118 GtkEditable *editable = GTK_EDITABLE (widget);
8121 str = (gchar *) gtk_selection_data_get_text (selection_data);
8123 x -= widget->style->xthickness;
8124 y -= widget->style->ythickness;
8126 if (str && entry->editable)
8132 if (entry->truncate_multiline)
8133 length = truncate_multiline (str);
8135 new_position = gtk_entry_find_position (entry, x + entry->scroll_offset);
8137 if (!gtk_editable_get_selection_bounds (editable, &sel1, &sel2) ||
8138 new_position < sel1 || new_position > sel2)
8140 gtk_editable_insert_text (editable, str, length, &new_position);
8144 /* Replacing selection */
8145 begin_change (entry);
8146 g_object_freeze_notify (G_OBJECT (entry));
8147 gtk_editable_delete_text (editable, sel1, sel2);
8148 gtk_editable_insert_text (editable, str, length, &sel1);
8149 g_object_thaw_notify (G_OBJECT (entry));
8153 gtk_drag_finish (context, TRUE, context->action == GDK_ACTION_MOVE, time);
8157 /* Drag and drop didn't happen! */
8158 gtk_drag_finish (context, FALSE, FALSE, time);
8165 gtk_entry_drag_data_get (GtkWidget *widget,
8166 GdkDragContext *context,
8167 GtkSelectionData *selection_data,
8171 gint sel_start, sel_end;
8174 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (widget);
8175 GtkEditable *editable = GTK_EDITABLE (widget);
8176 EntryIconInfo *icon_info = NULL;
8178 /* If there is an icon drag going on, exit early. */
8179 for (i = 0; i < MAX_ICONS; i++)
8181 if ((icon_info = priv->icons[i]) != NULL)
8183 if (icon_info->in_drag)
8188 if (gtk_editable_get_selection_bounds (editable, &sel_start, &sel_end))
8190 gchar *str = gtk_entry_get_public_chars (GTK_ENTRY (widget), sel_start, sel_end);
8192 gtk_selection_data_set_text (selection_data, str, -1);
8200 gtk_entry_drag_data_delete (GtkWidget *widget,
8201 GdkDragContext *context)
8203 gint sel_start, sel_end;
8206 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (widget);
8207 GtkEditable *editable = GTK_EDITABLE (widget);
8208 EntryIconInfo *icon_info = NULL;
8210 /* If there is an icon drag going on, exit early. */
8211 for (i = 0; i < MAX_ICONS; i++)
8213 if ((icon_info = priv->icons[i]) != NULL)
8215 if (icon_info->in_drag)
8220 if (GTK_ENTRY (widget)->editable &&
8221 gtk_editable_get_selection_bounds (editable, &sel_start, &sel_end))
8222 gtk_editable_delete_text (editable, sel_start, sel_end);
8225 /* We display the cursor when
8227 * - the selection is empty, AND
8228 * - the widget has focus
8231 #define CURSOR_ON_MULTIPLIER 2
8232 #define CURSOR_OFF_MULTIPLIER 1
8233 #define CURSOR_PEND_MULTIPLIER 3
8234 #define CURSOR_DIVIDER 3
8237 cursor_blinks (GtkEntry *entry)
8239 if (GTK_WIDGET_HAS_FOCUS (entry) &&
8241 entry->selection_bound == entry->current_pos)
8243 GtkSettings *settings;
8246 settings = gtk_widget_get_settings (GTK_WIDGET (entry));
8247 g_object_get (settings, "gtk-cursor-blink", &blink, NULL);
8256 get_cursor_time (GtkEntry *entry)
8258 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (entry));
8261 g_object_get (settings, "gtk-cursor-blink-time", &time, NULL);
8267 get_cursor_blink_timeout (GtkEntry *entry)
8269 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (entry));
8272 g_object_get (settings, "gtk-cursor-blink-timeout", &timeout, NULL);
8278 show_cursor (GtkEntry *entry)
8280 if (!entry->cursor_visible)
8282 entry->cursor_visible = TRUE;
8284 if (GTK_WIDGET_HAS_FOCUS (entry) && entry->selection_bound == entry->current_pos)
8285 gtk_widget_queue_draw (GTK_WIDGET (entry));
8290 hide_cursor (GtkEntry *entry)
8292 if (entry->cursor_visible)
8294 entry->cursor_visible = FALSE;
8296 if (GTK_WIDGET_HAS_FOCUS (entry) && entry->selection_bound == entry->current_pos)
8297 gtk_widget_queue_draw (GTK_WIDGET (entry));
8305 blink_cb (gpointer data)
8308 GtkEntryPrivate *priv;
8311 entry = GTK_ENTRY (data);
8312 priv = GTK_ENTRY_GET_PRIVATE (entry);
8314 if (!GTK_WIDGET_HAS_FOCUS (entry))
8316 g_warning ("GtkEntry - did not receive focus-out-event. If you\n"
8317 "connect a handler to this signal, it must return\n"
8318 "FALSE so the entry gets the event as well");
8320 gtk_entry_check_cursor_blink (entry);
8325 g_assert (entry->selection_bound == entry->current_pos);
8327 blink_timeout = get_cursor_blink_timeout (entry);
8328 if (priv->blink_time > 1000 * blink_timeout &&
8329 blink_timeout < G_MAXINT/1000)
8331 /* we've blinked enough without the user doing anything, stop blinking */
8332 show_cursor (entry);
8333 entry->blink_timeout = 0;
8335 else if (entry->cursor_visible)
8337 hide_cursor (entry);
8338 entry->blink_timeout = gdk_threads_add_timeout (get_cursor_time (entry) * CURSOR_OFF_MULTIPLIER / CURSOR_DIVIDER,
8344 show_cursor (entry);
8345 priv->blink_time += get_cursor_time (entry);
8346 entry->blink_timeout = gdk_threads_add_timeout (get_cursor_time (entry) * CURSOR_ON_MULTIPLIER / CURSOR_DIVIDER,
8351 /* Remove ourselves */
8356 gtk_entry_check_cursor_blink (GtkEntry *entry)
8358 GtkEntryPrivate *priv;
8360 priv = GTK_ENTRY_GET_PRIVATE (entry);
8362 if (cursor_blinks (entry))
8364 if (!entry->blink_timeout)
8366 show_cursor (entry);
8367 entry->blink_timeout = gdk_threads_add_timeout (get_cursor_time (entry) * CURSOR_ON_MULTIPLIER / CURSOR_DIVIDER,
8374 if (entry->blink_timeout)
8376 g_source_remove (entry->blink_timeout);
8377 entry->blink_timeout = 0;
8380 entry->cursor_visible = TRUE;
8386 gtk_entry_pend_cursor_blink (GtkEntry *entry)
8388 if (cursor_blinks (entry))
8390 if (entry->blink_timeout != 0)
8391 g_source_remove (entry->blink_timeout);
8393 entry->blink_timeout = gdk_threads_add_timeout (get_cursor_time (entry) * CURSOR_PEND_MULTIPLIER / CURSOR_DIVIDER,
8396 show_cursor (entry);
8401 gtk_entry_reset_blink_time (GtkEntry *entry)
8403 GtkEntryPrivate *priv;
8405 priv = GTK_ENTRY_GET_PRIVATE (entry);
8407 priv->blink_time = 0;
8413 gtk_entry_completion_timeout (gpointer data)
8415 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (data);
8417 completion->priv->completion_timeout = 0;
8419 if (completion->priv->filter_model &&
8420 g_utf8_strlen (gtk_entry_get_text (GTK_ENTRY (completion->priv->entry)), -1)
8421 >= completion->priv->minimum_key_length)
8425 GtkTreeSelection *s;
8426 gboolean popup_single;
8428 gtk_entry_completion_complete (completion);
8429 matches = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->filter_model), NULL);
8431 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view)));
8433 s = gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->action_view));
8435 gtk_tree_selection_unselect_all (s);
8437 actions = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->actions), NULL);
8439 g_object_get (completion, "popup-single-match", &popup_single, NULL);
8440 if ((matches > (popup_single ? 0: 1)) || actions > 0)
8442 if (GTK_WIDGET_VISIBLE (completion->priv->popup_window))
8443 _gtk_entry_completion_resize_popup (completion);
8445 _gtk_entry_completion_popup (completion);
8448 _gtk_entry_completion_popdown (completion);
8450 else if (GTK_WIDGET_VISIBLE (completion->priv->popup_window))
8451 _gtk_entry_completion_popdown (completion);
8456 static inline gboolean
8457 keyval_is_cursor_move (guint keyval)
8459 if (keyval == GDK_Up || keyval == GDK_KP_Up)
8462 if (keyval == GDK_Down || keyval == GDK_KP_Down)
8465 if (keyval == GDK_Page_Up)
8468 if (keyval == GDK_Page_Down)
8475 gtk_entry_completion_key_press (GtkWidget *widget,
8479 gint matches, actions = 0;
8480 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
8482 if (!GTK_WIDGET_MAPPED (completion->priv->popup_window))
8485 matches = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->filter_model), NULL);
8487 if (completion->priv->actions)
8488 actions = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->actions), NULL);
8490 if (keyval_is_cursor_move (event->keyval))
8492 GtkTreePath *path = NULL;
8494 if (event->keyval == GDK_Up || event->keyval == GDK_KP_Up)
8496 if (completion->priv->current_selected < 0)
8497 completion->priv->current_selected = matches + actions - 1;
8499 completion->priv->current_selected--;
8501 else if (event->keyval == GDK_Down || event->keyval == GDK_KP_Down)
8503 if (completion->priv->current_selected < matches + actions - 1)
8504 completion->priv->current_selected++;
8506 completion->priv->current_selected = -1;
8508 else if (event->keyval == GDK_Page_Up)
8510 if (completion->priv->current_selected < 0)
8511 completion->priv->current_selected = matches + actions - 1;
8512 else if (completion->priv->current_selected == 0)
8513 completion->priv->current_selected = -1;
8514 else if (completion->priv->current_selected < matches)
8516 completion->priv->current_selected -= 14;
8517 if (completion->priv->current_selected < 0)
8518 completion->priv->current_selected = 0;
8522 completion->priv->current_selected -= 14;
8523 if (completion->priv->current_selected < matches - 1)
8524 completion->priv->current_selected = matches - 1;
8527 else if (event->keyval == GDK_Page_Down)
8529 if (completion->priv->current_selected < 0)
8530 completion->priv->current_selected = 0;
8531 else if (completion->priv->current_selected < matches - 1)
8533 completion->priv->current_selected += 14;
8534 if (completion->priv->current_selected > matches - 1)
8535 completion->priv->current_selected = matches - 1;
8537 else if (completion->priv->current_selected == matches + actions - 1)
8539 completion->priv->current_selected = -1;
8543 completion->priv->current_selected += 14;
8544 if (completion->priv->current_selected > matches + actions - 1)
8545 completion->priv->current_selected = matches + actions - 1;
8549 if (completion->priv->current_selected < 0)
8551 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view)));
8552 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->action_view)));
8554 if (completion->priv->inline_selection &&
8555 completion->priv->completion_prefix)
8557 gtk_entry_set_text (GTK_ENTRY (completion->priv->entry),
8558 completion->priv->completion_prefix);
8559 gtk_editable_set_position (GTK_EDITABLE (widget), -1);
8562 else if (completion->priv->current_selected < matches)
8564 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->action_view)));
8566 path = gtk_tree_path_new_from_indices (completion->priv->current_selected, -1);
8567 gtk_tree_view_set_cursor (GTK_TREE_VIEW (completion->priv->tree_view),
8570 if (completion->priv->inline_selection)
8574 GtkTreeModel *model = NULL;
8575 GtkTreeSelection *sel;
8578 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view));
8579 if (!gtk_tree_selection_get_selected (sel, &model, &iter))
8582 if (completion->priv->completion_prefix == NULL)
8583 completion->priv->completion_prefix = g_strdup (gtk_entry_get_text (GTK_ENTRY (completion->priv->entry)));
8585 g_signal_emit_by_name (completion, "cursor-on-match", model,
8589 else if (completion->priv->current_selected - matches >= 0)
8591 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view)));
8593 path = gtk_tree_path_new_from_indices (completion->priv->current_selected - matches, -1);
8594 gtk_tree_view_set_cursor (GTK_TREE_VIEW (completion->priv->action_view),
8597 if (completion->priv->inline_selection &&
8598 completion->priv->completion_prefix)
8600 gtk_entry_set_text (GTK_ENTRY (completion->priv->entry),
8601 completion->priv->completion_prefix);
8602 gtk_editable_set_position (GTK_EDITABLE (widget), -1);
8606 gtk_tree_path_free (path);
8610 else if (event->keyval == GDK_Escape ||
8611 event->keyval == GDK_Left ||
8612 event->keyval == GDK_KP_Left ||
8613 event->keyval == GDK_Right ||
8614 event->keyval == GDK_KP_Right)
8616 gboolean retval = TRUE;
8618 _gtk_entry_reset_im_context (GTK_ENTRY (widget));
8619 _gtk_entry_completion_popdown (completion);
8621 if (completion->priv->current_selected < 0)
8624 goto keypress_completion_out;
8626 else if (completion->priv->inline_selection)
8628 /* Escape rejects the tentative completion */
8629 if (event->keyval == GDK_Escape)
8631 if (completion->priv->completion_prefix)
8632 gtk_entry_set_text (GTK_ENTRY (completion->priv->entry),
8633 completion->priv->completion_prefix);
8635 gtk_entry_set_text (GTK_ENTRY (completion->priv->entry), "");
8638 /* Move the cursor to the end for Right/Esc, to the
8639 beginning for Left */
8640 if (event->keyval == GDK_Right ||
8641 event->keyval == GDK_KP_Right ||
8642 event->keyval == GDK_Escape)
8643 gtk_editable_set_position (GTK_EDITABLE (widget), -1);
8645 gtk_editable_set_position (GTK_EDITABLE (widget), 0);
8648 keypress_completion_out:
8649 if (completion->priv->inline_selection)
8651 g_free (completion->priv->completion_prefix);
8652 completion->priv->completion_prefix = NULL;
8657 else if (event->keyval == GDK_Tab ||
8658 event->keyval == GDK_KP_Tab ||
8659 event->keyval == GDK_ISO_Left_Tab)
8661 GtkDirectionType dir = event->keyval == GDK_ISO_Left_Tab ?
8662 GTK_DIR_TAB_BACKWARD : GTK_DIR_TAB_FORWARD;
8664 _gtk_entry_reset_im_context (GTK_ENTRY (widget));
8665 _gtk_entry_completion_popdown (completion);
8667 g_free (completion->priv->completion_prefix);
8668 completion->priv->completion_prefix = NULL;
8670 gtk_widget_child_focus (gtk_widget_get_toplevel (widget), dir);
8674 else if (event->keyval == GDK_ISO_Enter ||
8675 event->keyval == GDK_KP_Enter ||
8676 event->keyval == GDK_Return)
8678 gboolean retval = TRUE;
8680 _gtk_entry_reset_im_context (GTK_ENTRY (widget));
8681 _gtk_entry_completion_popdown (completion);
8683 if (completion->priv->current_selected < matches)
8686 GtkTreeModel *model = NULL;
8687 GtkTreeSelection *sel;
8690 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view));
8691 if (gtk_tree_selection_get_selected (sel, &model, &iter))
8693 g_signal_handler_block (widget, completion->priv->changed_id);
8694 g_signal_emit_by_name (completion, "match-selected",
8695 model, &iter, &entry_set);
8696 g_signal_handler_unblock (widget, completion->priv->changed_id);
8702 gtk_tree_model_get (model, &iter,
8703 completion->priv->text_column, &str,
8706 gtk_entry_set_text (GTK_ENTRY (widget), str);
8708 /* move the cursor to the end */
8709 gtk_editable_set_position (GTK_EDITABLE (widget), -1);
8717 else if (completion->priv->current_selected - matches >= 0)
8721 _gtk_entry_reset_im_context (GTK_ENTRY (widget));
8723 path = gtk_tree_path_new_from_indices (completion->priv->current_selected - matches, -1);
8725 g_signal_emit_by_name (completion, "action-activated",
8726 gtk_tree_path_get_indices (path)[0]);
8727 gtk_tree_path_free (path);
8730 g_free (completion->priv->completion_prefix);
8731 completion->priv->completion_prefix = NULL;
8740 gtk_entry_completion_changed (GtkWidget *entry,
8743 GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
8745 /* (re)install completion timeout */
8746 if (completion->priv->completion_timeout)
8747 g_source_remove (completion->priv->completion_timeout);
8749 if (!gtk_entry_get_text (GTK_ENTRY (entry)))
8752 /* no need to normalize for this test */
8753 if (completion->priv->minimum_key_length > 0 &&
8754 strcmp ("", gtk_entry_get_text (GTK_ENTRY (entry))) == 0)
8756 if (GTK_WIDGET_VISIBLE (completion->priv->popup_window))
8757 _gtk_entry_completion_popdown (completion);
8761 completion->priv->completion_timeout =
8762 gdk_threads_add_timeout (COMPLETION_TIMEOUT,
8763 gtk_entry_completion_timeout,
8768 check_completion_callback (GtkEntryCompletion *completion)
8770 completion->priv->check_completion_idle = NULL;
8772 gtk_entry_completion_complete (completion);
8773 gtk_entry_completion_insert_prefix (completion);
8779 clear_completion_callback (GtkEntry *entry,
8782 if (pspec->name == I_("cursor-position") ||
8783 pspec->name == I_("selection-bound"))
8785 GtkEntryCompletion *completion = gtk_entry_get_completion (entry);
8787 completion->priv->has_completion = FALSE;
8792 accept_completion_callback (GtkEntry *entry)
8794 GtkEntryCompletion *completion = gtk_entry_get_completion (entry);
8796 if (completion->priv->has_completion)
8797 gtk_editable_set_position (GTK_EDITABLE (entry),
8798 entry->text_length);
8804 completion_insert_text_callback (GtkEntry *entry,
8808 GtkEntryCompletion *completion)
8810 /* idle to update the selection based on the file list */
8811 if (completion->priv->check_completion_idle == NULL)
8813 completion->priv->check_completion_idle = g_idle_source_new ();
8814 g_source_set_priority (completion->priv->check_completion_idle, G_PRIORITY_HIGH);
8815 g_source_set_closure (completion->priv->check_completion_idle,
8816 g_cclosure_new_object (G_CALLBACK (check_completion_callback),
8817 G_OBJECT (completion)));
8818 g_source_attach (completion->priv->check_completion_idle, NULL);
8823 completion_changed (GtkEntryCompletion *completion,
8827 GtkEntry *entry = GTK_ENTRY (data);
8829 if (pspec->name == I_("popup-completion") ||
8830 pspec->name == I_("inline-completion"))
8832 disconnect_completion_signals (entry, completion);
8833 connect_completion_signals (entry, completion);
8838 disconnect_completion_signals (GtkEntry *entry,
8839 GtkEntryCompletion *completion)
8841 g_signal_handlers_disconnect_by_func (completion,
8842 G_CALLBACK (completion_changed), entry);
8843 if (completion->priv->changed_id > 0 &&
8844 g_signal_handler_is_connected (entry, completion->priv->changed_id))
8846 g_signal_handler_disconnect (entry, completion->priv->changed_id);
8847 completion->priv->changed_id = 0;
8849 g_signal_handlers_disconnect_by_func (entry,
8850 G_CALLBACK (gtk_entry_completion_key_press), completion);
8851 if (completion->priv->insert_text_id > 0 &&
8852 g_signal_handler_is_connected (entry, completion->priv->insert_text_id))
8854 g_signal_handler_disconnect (entry, completion->priv->insert_text_id);
8855 completion->priv->insert_text_id = 0;
8857 g_signal_handlers_disconnect_by_func (entry,
8858 G_CALLBACK (completion_insert_text_callback), completion);
8859 g_signal_handlers_disconnect_by_func (entry,
8860 G_CALLBACK (clear_completion_callback), completion);
8861 g_signal_handlers_disconnect_by_func (entry,
8862 G_CALLBACK (accept_completion_callback), completion);
8866 connect_completion_signals (GtkEntry *entry,
8867 GtkEntryCompletion *completion)
8869 if (completion->priv->popup_completion)
8871 completion->priv->changed_id =
8872 g_signal_connect (entry, "changed",
8873 G_CALLBACK (gtk_entry_completion_changed), completion);
8874 g_signal_connect (entry, "key-press-event",
8875 G_CALLBACK (gtk_entry_completion_key_press), completion);
8878 if (completion->priv->inline_completion)
8880 completion->priv->insert_text_id =
8881 g_signal_connect (entry, "insert-text",
8882 G_CALLBACK (completion_insert_text_callback), completion);
8883 g_signal_connect (entry, "notify",
8884 G_CALLBACK (clear_completion_callback), completion);
8885 g_signal_connect (entry, "activate",
8886 G_CALLBACK (accept_completion_callback), completion);
8887 g_signal_connect (entry, "focus-out-event",
8888 G_CALLBACK (accept_completion_callback), completion);
8891 g_signal_connect (completion, "notify",
8892 G_CALLBACK (completion_changed), entry);
8896 * gtk_entry_set_completion:
8897 * @entry: A #GtkEntry
8898 * @completion: The #GtkEntryCompletion or %NULL
8900 * Sets @completion to be the auxiliary completion object to use with @entry.
8901 * All further configuration of the completion mechanism is done on
8902 * @completion using the #GtkEntryCompletion API. Completion is disabled if
8903 * @completion is set to %NULL.
8908 gtk_entry_set_completion (GtkEntry *entry,
8909 GtkEntryCompletion *completion)
8911 GtkEntryCompletion *old;
8913 g_return_if_fail (GTK_IS_ENTRY (entry));
8914 g_return_if_fail (!completion || GTK_IS_ENTRY_COMPLETION (completion));
8916 old = gtk_entry_get_completion (entry);
8918 if (old == completion)
8923 if (old->priv->completion_timeout)
8925 g_source_remove (old->priv->completion_timeout);
8926 old->priv->completion_timeout = 0;
8929 if (GTK_WIDGET_MAPPED (old->priv->popup_window))
8930 _gtk_entry_completion_popdown (old);
8932 disconnect_completion_signals (entry, old);
8933 old->priv->entry = NULL;
8935 g_object_unref (old);
8940 g_object_set_data (G_OBJECT (entry), I_(GTK_ENTRY_COMPLETION_KEY), NULL);
8944 /* hook into the entry */
8945 g_object_ref (completion);
8947 connect_completion_signals (entry, completion);
8948 completion->priv->entry = GTK_WIDGET (entry);
8949 g_object_set_data (G_OBJECT (entry), I_(GTK_ENTRY_COMPLETION_KEY), completion);
8953 * gtk_entry_get_completion:
8954 * @entry: A #GtkEntry
8956 * Returns the auxiliary completion object currently in use by @entry.
8958 * Return value: The auxiliary completion object currently in use by @entry.
8962 GtkEntryCompletion *
8963 gtk_entry_get_completion (GtkEntry *entry)
8965 GtkEntryCompletion *completion;
8967 g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
8969 completion = GTK_ENTRY_COMPLETION (g_object_get_data (G_OBJECT (entry),
8970 GTK_ENTRY_COMPLETION_KEY));
8976 * gtk_entry_set_cursor_hadjustment:
8977 * @entry: a #GtkEntry
8978 * @adjustment: an adjustment which should be adjusted when the cursor
8979 * is moved, or %NULL
8981 * Hooks up an adjustment to the cursor position in an entry, so that when
8982 * the cursor is moved, the adjustment is scrolled to show that position.
8983 * See gtk_scrolled_window_get_hadjustment() for a typical way of obtaining
8986 * The adjustment has to be in pixel units and in the same coordinate system
8992 gtk_entry_set_cursor_hadjustment (GtkEntry *entry,
8993 GtkAdjustment *adjustment)
8995 g_return_if_fail (GTK_IS_ENTRY (entry));
8997 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
9000 g_object_ref (adjustment);
9002 g_object_set_qdata_full (G_OBJECT (entry),
9003 quark_cursor_hadjustment,
9009 * gtk_entry_get_cursor_hadjustment:
9010 * @entry: a #GtkEntry
9012 * Retrieves the horizontal cursor adjustment for the entry.
9013 * See gtk_entry_set_cursor_hadjustment().
9015 * Return value: the horizontal cursor adjustment, or %NULL
9016 * if none has been set.
9021 gtk_entry_get_cursor_hadjustment (GtkEntry *entry)
9023 g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
9025 return g_object_get_qdata (G_OBJECT (entry), quark_cursor_hadjustment);
9029 * gtk_entry_set_progress_fraction:
9030 * @entry: a #GtkEntry
9031 * @fraction: fraction of the task that's been completed
9033 * Causes the entry's progress indicator to "fill in" the given
9034 * fraction of the bar. The fraction should be between 0.0 and 1.0,
9040 gtk_entry_set_progress_fraction (GtkEntry *entry,
9043 GtkEntryPrivate *private;
9044 gdouble old_fraction;
9046 g_return_if_fail (GTK_IS_ENTRY (entry));
9048 private = GTK_ENTRY_GET_PRIVATE (entry);
9050 if (private->progress_pulse_mode)
9053 old_fraction = private->progress_fraction;
9055 fraction = CLAMP (fraction, 0.0, 1.0);
9057 private->progress_fraction = fraction;
9058 private->progress_pulse_mode = FALSE;
9059 private->progress_pulse_current = 0.0;
9061 if (fabs (fraction - old_fraction) > 0.0001)
9062 gtk_entry_queue_draw (entry);
9064 if (fraction != old_fraction)
9065 g_object_notify (G_OBJECT (entry), "progress-fraction");
9069 * gtk_entry_get_progress_fraction:
9070 * @entry: a #GtkEntry
9072 * Returns the current fraction of the task that's been completed.
9073 * See gtk_entry_set_progress_fraction().
9075 * Return value: a fraction from 0.0 to 1.0
9080 gtk_entry_get_progress_fraction (GtkEntry *entry)
9082 GtkEntryPrivate *private;
9084 g_return_val_if_fail (GTK_IS_ENTRY (entry), 0.0);
9086 private = GTK_ENTRY_GET_PRIVATE (entry);
9088 return private->progress_fraction;
9092 * gtk_entry_set_progress_pulse_step:
9093 * @entry: a #GtkEntry
9094 * @fraction: fraction between 0.0 and 1.0
9096 * Sets the fraction of total entry width to move the progress
9097 * bouncing block for each call to gtk_entry_progress_pulse().
9102 gtk_entry_set_progress_pulse_step (GtkEntry *entry,
9105 GtkEntryPrivate *private;
9107 g_return_if_fail (GTK_IS_ENTRY (entry));
9109 private = GTK_ENTRY_GET_PRIVATE (entry);
9111 fraction = CLAMP (fraction, 0.0, 1.0);
9113 if (fraction != private->progress_pulse_fraction)
9115 private->progress_pulse_fraction = fraction;
9117 gtk_entry_queue_draw (entry);
9119 g_object_notify (G_OBJECT (entry), "progress-pulse-step");
9124 * gtk_entry_get_progress_pulse_step:
9125 * @entry: a #GtkEntry
9127 * Retrieves the pulse step set with gtk_entry_set_progress_pulse_step().
9129 * Return value: a fraction from 0.0 to 1.0
9134 gtk_entry_get_progress_pulse_step (GtkEntry *entry)
9136 GtkEntryPrivate *private;
9138 g_return_val_if_fail (GTK_IS_ENTRY (entry), 0.0);
9140 private = GTK_ENTRY_GET_PRIVATE (entry);
9142 return private->progress_pulse_fraction;
9146 * gtk_entry_progress_pulse:
9147 * @entry: a #GtkEntry
9149 * Indicates that some progress is made, but you don't know how much.
9150 * Causes the entry's progress indicator to enter "activity mode,"
9151 * where a block bounces back and forth. Each call to
9152 * gtk_entry_progress_pulse() causes the block to move by a little bit
9153 * (the amount of movement per pulse is determined by
9154 * gtk_entry_set_progress_pulse_step()).
9159 gtk_entry_progress_pulse (GtkEntry *entry)
9161 GtkEntryPrivate *private;
9163 g_return_if_fail (GTK_IS_ENTRY (entry));
9165 private = GTK_ENTRY_GET_PRIVATE (entry);
9167 if (private->progress_pulse_mode)
9169 if (private->progress_pulse_way_back)
9171 private->progress_pulse_current -= private->progress_pulse_fraction;
9173 if (private->progress_pulse_current < 0.0)
9175 private->progress_pulse_current = 0.0;
9176 private->progress_pulse_way_back = FALSE;
9181 private->progress_pulse_current += private->progress_pulse_fraction;
9183 if (private->progress_pulse_current > 1.0 - private->progress_pulse_fraction)
9185 private->progress_pulse_current = 1.0 - private->progress_pulse_fraction;
9186 private->progress_pulse_way_back = TRUE;
9192 private->progress_fraction = 0.0;
9193 private->progress_pulse_mode = TRUE;
9194 private->progress_pulse_way_back = FALSE;
9195 private->progress_pulse_current = 0.0;
9198 gtk_entry_queue_draw (entry);
9201 /* Caps Lock warning for password entries */
9204 capslock_feedback_free (GtkEntryCapslockFeedback *feedback)
9206 if (feedback->window)
9207 gtk_widget_destroy (feedback->window);
9209 g_slice_free (GtkEntryCapslockFeedback, feedback);
9213 capslock_feedback_expose_event_cb (GtkWidget *widget,
9214 GdkEventExpose *event,
9215 GtkEntryCapslockFeedback *feedback)
9217 gtk_paint_flat_box (feedback->window->style,
9218 feedback->window->window,
9225 feedback->window->allocation.width,
9226 feedback->window->allocation.height);
9231 capslock_feedback_enter_notify (GtkWidget *widget,
9232 GdkEventCrossing *event,
9233 GtkEntryCapslockFeedback *feedback)
9235 remove_capslock_feedback (GTK_ENTRY (feedback->entry));
9240 create_capslock_feedback_window (GtkEntryCapslockFeedback *feedback)
9243 GtkWidget *alignment;
9246 /* Keep in sync with gtk_tooltip_init() */
9247 window = feedback->window = gtk_window_new (GTK_WINDOW_POPUP);
9248 gtk_window_set_type_hint (GTK_WINDOW (window), GDK_WINDOW_TYPE_HINT_TOOLTIP);
9249 gtk_widget_set_app_paintable (window, TRUE);
9250 gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
9251 gtk_widget_set_name (window, "gtk-tooltip");
9253 alignment = gtk_alignment_new (0.5, 0.5, 1.0, 1.0);
9254 gtk_alignment_set_padding (GTK_ALIGNMENT (alignment),
9255 window->style->ythickness,
9256 window->style->ythickness,
9257 window->style->xthickness,
9258 window->style->xthickness);
9259 gtk_container_add (GTK_CONTAINER (window), alignment);
9260 gtk_widget_show (alignment);
9262 g_signal_connect (window, "expose-event",
9263 G_CALLBACK (capslock_feedback_expose_event_cb), feedback);
9264 g_signal_connect (window, "enter-notify-event",
9265 G_CALLBACK (capslock_feedback_enter_notify), feedback);
9267 label = feedback->label = gtk_label_new (NULL);
9268 gtk_container_add (GTK_CONTAINER (alignment), label);
9269 gtk_widget_show (label);
9273 show_capslock_feedback_window (GtkEntryCapslockFeedback *feedback)
9275 GtkRequisition feedback_req;
9276 gint entry_x, entry_y;
9277 GtkAllocation *entry_allocation;
9278 int feedback_x, feedback_y;
9281 GdkRectangle monitor;
9284 entry = feedback->entry;
9285 screen = gtk_widget_get_screen (entry);
9286 monitor_num = gdk_screen_get_monitor_at_window (screen, entry->window);
9287 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
9289 feedback = g_object_get_qdata (G_OBJECT (entry), quark_capslock_feedback);
9291 gtk_widget_size_request (feedback->window, &feedback_req);
9293 gdk_window_get_origin (entry->window, &entry_x, &entry_y);
9294 entry_allocation = &(entry->allocation);
9296 if (entry_x + feedback_req.width <= monitor.x + monitor.width)
9297 feedback_x = MAX (entry_x, monitor.x);
9299 feedback_x = monitor.x + monitor.width - feedback_req.width;
9301 if (entry_y + entry_allocation->height + feedback_req.height <= monitor.y + monitor.height)
9302 feedback_y = entry_y + entry_allocation->height;
9304 feedback_y = MAX (entry_y - feedback_req.height, monitor.y);
9306 gtk_window_move (GTK_WINDOW (feedback->window), feedback_x, feedback_y);
9307 gtk_widget_show (feedback->window);
9311 pop_up_capslock_feedback (GtkEntry *entry,
9314 GtkEntryCapslockFeedback *feedback;
9316 feedback = g_object_get_qdata (G_OBJECT (entry), quark_capslock_feedback);
9318 if (feedback == NULL)
9320 feedback = g_slice_new0 (GtkEntryCapslockFeedback);
9321 feedback->entry = GTK_WIDGET (entry);
9322 g_object_set_qdata_full (G_OBJECT (entry), quark_capslock_feedback,
9324 (GDestroyNotify) capslock_feedback_free);
9325 create_capslock_feedback_window (feedback);
9328 gtk_label_set_text (GTK_LABEL (feedback->label), text);
9329 show_capslock_feedback_window (feedback);
9333 remove_capslock_feedback (GtkEntry *entry)
9335 g_object_set_qdata (G_OBJECT (entry), quark_capslock_feedback, NULL);
9339 keymap_state_changed (GdkKeymap *keymap,
9342 GtkEntryPrivate *priv = GTK_ENTRY_GET_PRIVATE (entry);
9345 if (!entry->visible && priv->caps_lock_warning)
9347 gboolean capslock_on;
9350 capslock_on = gdk_keymap_get_caps_lock_state (keymap);
9351 im_on = g_strcmp0 (gtk_im_multicontext_get_context_id (GTK_IM_MULTICONTEXT (entry->im_context)), "gtk-im-context-simple") != 0;
9352 if (capslock_on && im_on)
9353 text = _("You have the Caps Lock key on\nand an active input method");
9354 else if (capslock_on)
9355 text = _("You have the Caps Lock key on");
9357 text = _("You have an active input method");
9361 pop_up_capslock_feedback (entry, text);
9363 remove_capslock_feedback (entry);
9366 #define __GTK_ENTRY_C__
9367 #include "gtkaliasdef.c"