]> Pileus Git - ~andy/gtk/blob - gtk/gtkentry.c
0694a7585090161e6355166d9959af5632e3805d
[~andy/gtk] / gtk / gtkentry.c
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.
7  *
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.
12  *
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.
17  *
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.
22  */
23
24 /*
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/.
29  */
30
31 #include "config.h"
32
33 #include <math.h>
34 #include <string.h>
35
36 #include "gtkbindings.h"
37 #include "gtkcelleditable.h"
38 #include "gtkclipboard.h"
39 #include "gtkdnd.h"
40 #include "gtkentry.h"
41 #include "gtkentrybuffer.h"
42 #include "gtkiconhelperprivate.h"
43 #include "gtkimagemenuitem.h"
44 #include "gtkimcontextsimple.h"
45 #include "gtkimmulticontext.h"
46 #include "gtkintl.h"
47 #include "gtklabel.h"
48 #include "gtkmain.h"
49 #include "gtkmarshalers.h"
50 #include "gtkmenu.h"
51 #include "gtkmenuitem.h"
52 #include "gtkseparatormenuitem.h"
53 #include "gtkselection.h"
54 #include "gtksettings.h"
55 #include "gtkspinbutton.h"
56 #include "gtkstock.h"
57 #include "gtktextutil.h"
58 #include "gtkwindow.h"
59 #include "gtktreeview.h"
60 #include "gtktreeselection.h"
61 #include "gtktypebuiltins.h"
62 #include "gtkprivate.h"
63 #include "gtkentryprivate.h"
64 #include "gtkcelllayout.h"
65 #include "gtktooltip.h"
66 #include "gtkiconfactory.h"
67 #include "gtkicontheme.h"
68 #include "gtkwidgetprivate.h"
69 #include "gtkstylecontextprivate.h"
70
71 #include "a11y/gtkentryaccessible.h"
72
73 /**
74  * SECTION:gtkentry
75  * @Short_description: A single line text entry field
76  * @Title: GtkEntry
77  * @See_also: #GtkTextView, #GtkEntryCompletion
78  *
79  * The #GtkEntry widget is a single line text entry
80  * widget. A fairly large set of key bindings are supported
81  * by default. If the entered text is longer than the allocation
82  * of the widget, the widget will scroll so that the cursor
83  * position is visible.
84  *
85  * When using an entry for passwords and other sensitive information,
86  * it can be put into "password mode" using gtk_entry_set_visibility().
87  * In this mode, entered text is displayed using a 'invisible' character.
88  * By default, GTK+ picks the best invisible character that is available
89  * in the current font, but it can be changed with
90  * gtk_entry_set_invisible_char(). Since 2.16, GTK+ displays a warning
91  * when Caps Lock or input methods might interfere with entering text in
92  * a password entry. The warning can be turned off with the
93  * #GtkEntry:caps-lock-warning property.
94  *
95  * Since 2.16, GtkEntry has the ability to display progress or activity
96  * information behind the text. To make an entry display such information,
97  * use gtk_entry_set_progress_fraction() or gtk_entry_set_progress_pulse_step().
98  *
99  * Additionally, GtkEntry can show icons at either side of the entry. These
100  * icons can be activatable by clicking, can be set up as drag source and
101  * can have tooltips. To add an icon, use gtk_entry_set_icon_from_gicon() or
102  * one of the various other functions that set an icon from a stock id, an
103  * icon name or a pixbuf. To trigger an action when the user clicks an icon,
104  * connect to the #GtkEntry::icon-press signal. To allow DND operations
105  * from an icon, use gtk_entry_set_icon_drag_source(). To set a tooltip on
106  * an icon, use gtk_entry_set_icon_tooltip_text() or the corresponding function
107  * for markup.
108  *
109  * Note that functionality or information that is only available by clicking
110  * on an icon in an entry may not be accessible at all to users which are not
111  * able to use a mouse or other pointing device. It is therefore recommended
112  * that any such functionality should also be available by other means, e.g.
113  * via the context menu of the entry.
114  */
115
116
117 #define GTK_ENTRY_COMPLETION_KEY "gtk-entry-completion-key"
118
119 #define MIN_ENTRY_WIDTH  150
120 #define DRAW_TIMEOUT     20
121 #define COMPLETION_TIMEOUT 300
122 #define PASSWORD_HINT_MAX 8
123 #define PAGE_STEP 14
124
125 #define MAX_ICONS 2
126
127 #define IS_VALID_ICON_POSITION(pos)               \
128   ((pos) == GTK_ENTRY_ICON_PRIMARY ||                   \
129    (pos) == GTK_ENTRY_ICON_SECONDARY)
130
131 static const GtkBorder default_inner_border = { 2, 2, 2, 2 };
132 static GQuark          quark_inner_border   = 0;
133 static GQuark          quark_password_hint  = 0;
134 static GQuark          quark_cursor_hadjustment = 0;
135 static GQuark          quark_capslock_feedback = 0;
136
137 typedef struct _EntryIconInfo EntryIconInfo;
138 typedef struct _GtkEntryPasswordHint GtkEntryPasswordHint;
139 typedef struct _GtkEntryCapslockFeedback GtkEntryCapslockFeedback;
140
141 struct _GtkEntryPrivate
142 {
143   EntryIconInfo         *icons[MAX_ICONS];
144
145   GtkEntryBuffer        *buffer;
146   GtkIMContext          *im_context;
147   GtkShadowType          shadow_type;
148   GtkWidget             *popup_menu;
149
150   GdkDevice             *completion_device;
151   GdkWindow             *text_area;
152
153   PangoLayout           *cached_layout;
154
155   gchar        *im_module;
156
157   gdouble       progress_fraction;
158   gdouble       progress_pulse_fraction;
159   gdouble       progress_pulse_current;
160
161   gchar        *placeholder_text;
162
163   gfloat        xalign;
164
165   gint          ascent;                     /* font ascent in pango units  */
166   gint          current_pos;
167   gint          descent;                    /* font descent in pango units */
168   gint          dnd_position;               /* In chars, -1 == no DND cursor */
169   gint          drag_start_x;
170   gint          drag_start_y;
171   gint          focus_width;
172   gint          insert_pos;
173   gint          selection_bound;
174   gint          scroll_offset;
175   gint          start_x;
176   gint          start_y;
177   gint          width_chars;
178
179   gunichar      invisible_char;
180
181   guint         button;
182   guint         blink_time;                  /* time in msec the cursor has blinked since last user event */
183   guint         blink_timeout;
184   guint         recompute_idle;
185
186   guint16       x_text_size;                 /* allocated size, in bytes */
187   guint16       x_n_bytes;                   /* length in use, in bytes */
188
189   guint16       preedit_length;              /* length of preedit string, in bytes */
190   guint16       preedit_cursor;              /* offset of cursor within preedit string, in chars */
191
192   guint         editable                : 1;
193   guint         in_drag                 : 1;
194   guint         overwrite_mode          : 1;
195   guint         visible                 : 1;
196
197   guint         activates_default       : 1;
198   guint         cache_includes_preedit  : 1;
199   guint         caps_lock_warning       : 1;
200   guint         caps_lock_warning_shown : 1;
201   guint         change_count            : 8;
202   guint         cursor_visible          : 1;
203   guint         editing_canceled        : 1; /* Only used by GtkCellRendererText */
204   guint         has_frame               : 1;
205   guint         in_click                : 1; /* Flag so we don't select all when clicking in entry to focus in */
206   guint         is_cell_renderer        : 1;
207   guint         invisible_char_set      : 1;
208   guint         interior_focus          : 1;
209   guint         mouse_cursor_obscured   : 1;
210   guint         need_im_reset           : 1;
211   guint         progress_pulse_mode     : 1;
212   guint         progress_pulse_way_back : 1;
213   guint         real_changed            : 1;
214   guint         resolved_dir            : 4; /* PangoDirection */
215   guint         select_words            : 1;
216   guint         select_lines            : 1;
217   guint         truncate_multiline      : 1;
218 };
219
220 struct _EntryIconInfo
221 {
222   GdkWindow *window;
223   gchar *tooltip;
224   guint insensitive    : 1;
225   guint nonactivatable : 1;
226   guint prelight       : 1;
227   guint in_drag        : 1;
228   guint pressed        : 1;
229
230   GtkIconHelper *icon_helper;
231
232   GtkTargetList *target_list;
233   GdkDragAction actions;
234 };
235
236 struct _GtkEntryPasswordHint
237 {
238   gint position;      /* Position (in text) of the last password hint */
239   guint source_id;    /* Timeout source id */
240 };
241
242 struct _GtkEntryCapslockFeedback
243 {
244   GtkWidget *entry;
245   GtkWidget *window;
246   GtkWidget *label;
247 };
248
249 enum {
250   ACTIVATE,
251   POPULATE_POPUP,
252   MOVE_CURSOR,
253   INSERT_AT_CURSOR,
254   DELETE_FROM_CURSOR,
255   BACKSPACE,
256   CUT_CLIPBOARD,
257   COPY_CLIPBOARD,
258   PASTE_CLIPBOARD,
259   TOGGLE_OVERWRITE,
260   ICON_PRESS,
261   ICON_RELEASE,
262   PREEDIT_CHANGED,
263   LAST_SIGNAL
264 };
265
266 enum {
267   PROP_0,
268   PROP_BUFFER,
269   PROP_CURSOR_POSITION,
270   PROP_SELECTION_BOUND,
271   PROP_EDITABLE,
272   PROP_MAX_LENGTH,
273   PROP_VISIBILITY,
274   PROP_HAS_FRAME,
275   PROP_INNER_BORDER,
276   PROP_INVISIBLE_CHAR,
277   PROP_ACTIVATES_DEFAULT,
278   PROP_WIDTH_CHARS,
279   PROP_SCROLL_OFFSET,
280   PROP_TEXT,
281   PROP_XALIGN,
282   PROP_TRUNCATE_MULTILINE,
283   PROP_SHADOW_TYPE,
284   PROP_OVERWRITE_MODE,
285   PROP_TEXT_LENGTH,
286   PROP_INVISIBLE_CHAR_SET,
287   PROP_CAPS_LOCK_WARNING,
288   PROP_PROGRESS_FRACTION,
289   PROP_PROGRESS_PULSE_STEP,
290   PROP_PIXBUF_PRIMARY,
291   PROP_PIXBUF_SECONDARY,
292   PROP_STOCK_PRIMARY,
293   PROP_STOCK_SECONDARY,
294   PROP_ICON_NAME_PRIMARY,
295   PROP_ICON_NAME_SECONDARY,
296   PROP_GICON_PRIMARY,
297   PROP_GICON_SECONDARY,
298   PROP_STORAGE_TYPE_PRIMARY,
299   PROP_STORAGE_TYPE_SECONDARY,
300   PROP_ACTIVATABLE_PRIMARY,
301   PROP_ACTIVATABLE_SECONDARY,
302   PROP_SENSITIVE_PRIMARY,
303   PROP_SENSITIVE_SECONDARY,
304   PROP_TOOLTIP_TEXT_PRIMARY,
305   PROP_TOOLTIP_TEXT_SECONDARY,
306   PROP_TOOLTIP_MARKUP_PRIMARY,
307   PROP_TOOLTIP_MARKUP_SECONDARY,
308   PROP_IM_MODULE,
309   PROP_EDITING_CANCELED,
310   PROP_PLACEHOLDER_TEXT,
311   PROP_COMPLETION
312 };
313
314 static guint signals[LAST_SIGNAL] = { 0 };
315
316 typedef enum {
317   CURSOR_STANDARD,
318   CURSOR_DND
319 } CursorType;
320
321 typedef enum
322 {
323   DISPLAY_NORMAL,       /* The entry text is being shown */
324   DISPLAY_INVISIBLE,    /* In invisible mode, text replaced by (eg) bullets */
325   DISPLAY_BLANK         /* In invisible mode, nothing shown at all */
326 } DisplayMode;
327
328 /* GObject methods
329  */
330 static void   gtk_entry_editable_init        (GtkEditableInterface *iface);
331 static void   gtk_entry_cell_editable_init   (GtkCellEditableIface *iface);
332 static void   gtk_entry_set_property         (GObject          *object,
333                                               guint             prop_id,
334                                               const GValue     *value,
335                                               GParamSpec       *pspec);
336 static void   gtk_entry_get_property         (GObject          *object,
337                                               guint             prop_id,
338                                               GValue           *value,
339                                               GParamSpec       *pspec);
340 static void   gtk_entry_finalize             (GObject          *object);
341 static void   gtk_entry_dispose              (GObject          *object);
342
343 /* GtkWidget methods
344  */
345 static void   gtk_entry_destroy              (GtkWidget        *widget);
346 static void   gtk_entry_realize              (GtkWidget        *widget);
347 static void   gtk_entry_unrealize            (GtkWidget        *widget);
348 static void   gtk_entry_map                  (GtkWidget        *widget);
349 static void   gtk_entry_unmap                (GtkWidget        *widget);
350 static void   gtk_entry_get_preferred_width  (GtkWidget        *widget,
351                                               gint             *minimum,
352                                               gint             *natural);
353 static void   gtk_entry_get_preferred_height (GtkWidget        *widget,
354                                               gint             *minimum,
355                                               gint             *natural);
356 static void   gtk_entry_size_allocate        (GtkWidget        *widget,
357                                               GtkAllocation    *allocation);
358 static void   gtk_entry_draw_frame           (GtkWidget        *widget,
359                                               GtkStyleContext  *context,
360                                               cairo_t          *cr);
361 static void   gtk_entry_draw_progress        (GtkWidget        *widget,
362                                               GtkStyleContext  *context,
363                                               cairo_t          *cr);
364 static gint   gtk_entry_draw                 (GtkWidget        *widget,
365                                               cairo_t          *cr);
366 static gint   gtk_entry_button_press         (GtkWidget        *widget,
367                                               GdkEventButton   *event);
368 static gint   gtk_entry_button_release       (GtkWidget        *widget,
369                                               GdkEventButton   *event);
370 static gint   gtk_entry_enter_notify         (GtkWidget        *widget,
371                                               GdkEventCrossing *event);
372 static gint   gtk_entry_leave_notify         (GtkWidget        *widget,
373                                               GdkEventCrossing *event);
374 static gint   gtk_entry_motion_notify        (GtkWidget        *widget,
375                                               GdkEventMotion   *event);
376 static gint   gtk_entry_key_press            (GtkWidget        *widget,
377                                               GdkEventKey      *event);
378 static gint   gtk_entry_key_release          (GtkWidget        *widget,
379                                               GdkEventKey      *event);
380 static gint   gtk_entry_focus_in             (GtkWidget        *widget,
381                                               GdkEventFocus    *event);
382 static gint   gtk_entry_focus_out            (GtkWidget        *widget,
383                                               GdkEventFocus    *event);
384 static void   gtk_entry_grab_focus           (GtkWidget        *widget);
385 static void   gtk_entry_style_updated        (GtkWidget        *widget);
386 static gboolean gtk_entry_query_tooltip      (GtkWidget        *widget,
387                                               gint              x,
388                                               gint              y,
389                                               gboolean          keyboard_tip,
390                                               GtkTooltip       *tooltip);
391 static void   gtk_entry_direction_changed    (GtkWidget        *widget,
392                                               GtkTextDirection  previous_dir);
393 static void   gtk_entry_state_flags_changed  (GtkWidget        *widget,
394                                               GtkStateFlags     previous_state);
395 static void   gtk_entry_screen_changed       (GtkWidget        *widget,
396                                               GdkScreen        *old_screen);
397
398 static gboolean gtk_entry_drag_drop          (GtkWidget        *widget,
399                                               GdkDragContext   *context,
400                                               gint              x,
401                                               gint              y,
402                                               guint             time);
403 static gboolean gtk_entry_drag_motion        (GtkWidget        *widget,
404                                               GdkDragContext   *context,
405                                               gint              x,
406                                               gint              y,
407                                               guint             time);
408 static void     gtk_entry_drag_leave         (GtkWidget        *widget,
409                                               GdkDragContext   *context,
410                                               guint             time);
411 static void     gtk_entry_drag_data_received (GtkWidget        *widget,
412                                               GdkDragContext   *context,
413                                               gint              x,
414                                               gint              y,
415                                               GtkSelectionData *selection_data,
416                                               guint             info,
417                                               guint             time);
418 static void     gtk_entry_drag_data_get      (GtkWidget        *widget,
419                                               GdkDragContext   *context,
420                                               GtkSelectionData *selection_data,
421                                               guint             info,
422                                               guint             time);
423 static void     gtk_entry_drag_data_delete   (GtkWidget        *widget,
424                                               GdkDragContext   *context);
425 static void     gtk_entry_drag_begin         (GtkWidget        *widget,
426                                               GdkDragContext   *context);
427 static void     gtk_entry_drag_end           (GtkWidget        *widget,
428                                               GdkDragContext   *context);
429
430
431 /* GtkEditable method implementations
432  */
433 static void     gtk_entry_insert_text          (GtkEditable *editable,
434                                                 const gchar *new_text,
435                                                 gint         new_text_length,
436                                                 gint        *position);
437 static void     gtk_entry_delete_text          (GtkEditable *editable,
438                                                 gint         start_pos,
439                                                 gint         end_pos);
440 static gchar *  gtk_entry_get_chars            (GtkEditable *editable,
441                                                 gint         start_pos,
442                                                 gint         end_pos);
443 static void     gtk_entry_real_set_position    (GtkEditable *editable,
444                                                 gint         position);
445 static gint     gtk_entry_get_position         (GtkEditable *editable);
446 static void     gtk_entry_set_selection_bounds (GtkEditable *editable,
447                                                 gint         start,
448                                                 gint         end);
449 static gboolean gtk_entry_get_selection_bounds (GtkEditable *editable,
450                                                 gint        *start,
451                                                 gint        *end);
452
453 /* GtkCellEditable method implementations
454  */
455 static void gtk_entry_start_editing (GtkCellEditable *cell_editable,
456                                      GdkEvent        *event);
457
458 /* Default signal handlers
459  */
460 static void gtk_entry_real_insert_text   (GtkEditable     *editable,
461                                           const gchar     *new_text,
462                                           gint             new_text_length,
463                                           gint            *position);
464 static void gtk_entry_real_delete_text   (GtkEditable     *editable,
465                                           gint             start_pos,
466                                           gint             end_pos);
467 static void gtk_entry_move_cursor        (GtkEntry        *entry,
468                                           GtkMovementStep  step,
469                                           gint             count,
470                                           gboolean         extend_selection);
471 static void gtk_entry_insert_at_cursor   (GtkEntry        *entry,
472                                           const gchar     *str);
473 static void gtk_entry_delete_from_cursor (GtkEntry        *entry,
474                                           GtkDeleteType    type,
475                                           gint             count);
476 static void gtk_entry_backspace          (GtkEntry        *entry);
477 static void gtk_entry_cut_clipboard      (GtkEntry        *entry);
478 static void gtk_entry_copy_clipboard     (GtkEntry        *entry);
479 static void gtk_entry_paste_clipboard    (GtkEntry        *entry);
480 static void gtk_entry_toggle_overwrite   (GtkEntry        *entry);
481 static void gtk_entry_select_all         (GtkEntry        *entry);
482 static void gtk_entry_real_activate      (GtkEntry        *entry);
483 static gboolean gtk_entry_popup_menu     (GtkWidget       *widget);
484
485 static void keymap_direction_changed     (GdkKeymap       *keymap,
486                                           GtkEntry        *entry);
487 static void keymap_state_changed         (GdkKeymap       *keymap,
488                                           GtkEntry        *entry);
489 static void remove_capslock_feedback     (GtkEntry        *entry);
490
491 /* IM Context Callbacks
492  */
493 static void     gtk_entry_commit_cb               (GtkIMContext *context,
494                                                    const gchar  *str,
495                                                    GtkEntry     *entry);
496 static void     gtk_entry_preedit_changed_cb      (GtkIMContext *context,
497                                                    GtkEntry     *entry);
498 static gboolean gtk_entry_retrieve_surrounding_cb (GtkIMContext *context,
499                                                    GtkEntry     *entry);
500 static gboolean gtk_entry_delete_surrounding_cb   (GtkIMContext *context,
501                                                    gint          offset,
502                                                    gint          n_chars,
503                                                    GtkEntry     *entry);
504
505 /* Internal routines
506  */
507 static void         gtk_entry_enter_text               (GtkEntry       *entry,
508                                                         const gchar    *str);
509 static void         gtk_entry_set_positions            (GtkEntry       *entry,
510                                                         gint            current_pos,
511                                                         gint            selection_bound);
512 static void         gtk_entry_draw_text                (GtkEntry       *entry,
513                                                         cairo_t        *cr);
514 static void         gtk_entry_draw_cursor              (GtkEntry       *entry,
515                                                         cairo_t        *cr,
516                                                         CursorType      type);
517 static PangoLayout *gtk_entry_ensure_layout            (GtkEntry       *entry,
518                                                         gboolean        include_preedit);
519 static void         gtk_entry_reset_layout             (GtkEntry       *entry);
520 static void         gtk_entry_recompute                (GtkEntry       *entry);
521 static gint         gtk_entry_find_position            (GtkEntry       *entry,
522                                                         gint            x);
523 static void         gtk_entry_get_cursor_locations     (GtkEntry       *entry,
524                                                         CursorType      type,
525                                                         gint           *strong_x,
526                                                         gint           *weak_x);
527 static void         gtk_entry_adjust_scroll            (GtkEntry       *entry);
528 static gint         gtk_entry_move_visually            (GtkEntry       *editable,
529                                                         gint            start,
530                                                         gint            count);
531 static gint         gtk_entry_move_logically           (GtkEntry       *entry,
532                                                         gint            start,
533                                                         gint            count);
534 static gint         gtk_entry_move_forward_word        (GtkEntry       *entry,
535                                                         gint            start,
536                                                         gboolean        allow_whitespace);
537 static gint         gtk_entry_move_backward_word       (GtkEntry       *entry,
538                                                         gint            start,
539                                                         gboolean        allow_whitespace);
540 static void         gtk_entry_delete_whitespace        (GtkEntry       *entry);
541 static void         gtk_entry_select_word              (GtkEntry       *entry);
542 static void         gtk_entry_select_line              (GtkEntry       *entry);
543 static void         gtk_entry_paste                    (GtkEntry       *entry,
544                                                         GdkAtom         selection);
545 static void         gtk_entry_update_primary_selection (GtkEntry       *entry);
546 static void         gtk_entry_do_popup                 (GtkEntry       *entry,
547                                                         GdkEventButton *event);
548 static gboolean     gtk_entry_mnemonic_activate        (GtkWidget      *widget,
549                                                         gboolean        group_cycling);
550 static void         gtk_entry_check_cursor_blink       (GtkEntry       *entry);
551 static void         gtk_entry_pend_cursor_blink        (GtkEntry       *entry);
552 static void         gtk_entry_reset_blink_time         (GtkEntry       *entry);
553 static void         gtk_entry_get_text_area_size       (GtkEntry       *entry,
554                                                         gint           *x,
555                                                         gint           *y,
556                                                         gint           *width,
557                                                         gint           *height);
558 static void         get_text_area_size                 (GtkEntry       *entry,
559                                                         gint           *x,
560                                                         gint           *y,
561                                                         gint           *width,
562                                                         gint           *height);
563 static void         get_frame_size                     (GtkEntry       *entry,
564                                                         gboolean        relative_to_window,
565                                                         gint           *x,
566                                                         gint           *y,
567                                                         gint           *width,
568                                                         gint           *height);
569 static void         gtk_entry_move_adjustments         (GtkEntry             *entry);
570 static GdkPixbuf *  gtk_entry_ensure_pixbuf            (GtkEntry             *entry,
571                                                         GtkEntryIconPosition  icon_pos);
572 static void         gtk_entry_update_cached_style_values(GtkEntry      *entry);
573
574 /* Completion */
575 static gint         gtk_entry_completion_timeout       (gpointer            data);
576 static gboolean     gtk_entry_completion_key_press     (GtkWidget          *widget,
577                                                         GdkEventKey        *event,
578                                                         gpointer            user_data);
579 static void         gtk_entry_completion_changed       (GtkWidget          *entry,
580                                                         gpointer            user_data);
581 static gboolean     check_completion_callback          (GtkEntryCompletion *completion);
582 static void         clear_completion_callback          (GtkEntry           *entry,
583                                                         GParamSpec         *pspec);
584 static gboolean     accept_completion_callback         (GtkEntry           *entry);
585 static void         completion_insert_text_callback    (GtkEntry           *entry,
586                                                         const gchar        *text,
587                                                         gint                length,
588                                                         gint                position,
589                                                         GtkEntryCompletion *completion);
590 static void         completion_changed                 (GtkEntryCompletion *completion,
591                                                         GParamSpec         *pspec,
592                                                         gpointer            data);
593 static void         disconnect_completion_signals      (GtkEntry           *entry,
594                                                         GtkEntryCompletion *completion);
595 static void         connect_completion_signals         (GtkEntry           *entry,
596                                                         GtkEntryCompletion *completion);
597
598 static void         begin_change                       (GtkEntry *entry);
599 static void         end_change                         (GtkEntry *entry);
600 static void         emit_changed                       (GtkEntry *entry);
601
602 static void         buffer_inserted_text               (GtkEntryBuffer *buffer, 
603                                                         guint           position,
604                                                         const gchar    *chars,
605                                                         guint           n_chars,
606                                                         GtkEntry       *entry);
607 static void         buffer_deleted_text                (GtkEntryBuffer *buffer, 
608                                                         guint           position,
609                                                         guint           n_chars,
610                                                         GtkEntry       *entry);
611 static void         buffer_notify_text                 (GtkEntryBuffer *buffer, 
612                                                         GParamSpec     *spec,
613                                                         GtkEntry       *entry);
614 static void         buffer_notify_length               (GtkEntryBuffer *buffer, 
615                                                         GParamSpec     *spec,
616                                                         GtkEntry       *entry);
617 static void         buffer_notify_max_length           (GtkEntryBuffer *buffer, 
618                                                         GParamSpec     *spec,
619                                                         GtkEntry       *entry);
620 static void         buffer_connect_signals             (GtkEntry       *entry);
621 static void         buffer_disconnect_signals          (GtkEntry       *entry);
622 static GtkEntryBuffer *get_buffer                      (GtkEntry       *entry);
623
624 G_DEFINE_TYPE_WITH_CODE (GtkEntry, gtk_entry, GTK_TYPE_WIDGET,
625                          G_IMPLEMENT_INTERFACE (GTK_TYPE_EDITABLE,
626                                                 gtk_entry_editable_init)
627                          G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_EDITABLE,
628                                                 gtk_entry_cell_editable_init))
629
630 static void
631 add_move_binding (GtkBindingSet  *binding_set,
632                   guint           keyval,
633                   guint           modmask,
634                   GtkMovementStep step,
635                   gint            count)
636 {
637   g_return_if_fail ((modmask & GDK_SHIFT_MASK) == 0);
638   
639   gtk_binding_entry_add_signal (binding_set, keyval, modmask,
640                                 "move-cursor", 3,
641                                 G_TYPE_ENUM, step,
642                                 G_TYPE_INT, count,
643                                 G_TYPE_BOOLEAN, FALSE);
644
645   /* Selection-extending version */
646   gtk_binding_entry_add_signal (binding_set, keyval, modmask | GDK_SHIFT_MASK,
647                                 "move-cursor", 3,
648                                 G_TYPE_ENUM, step,
649                                 G_TYPE_INT, count,
650                                 G_TYPE_BOOLEAN, TRUE);
651 }
652
653 static void
654 gtk_entry_class_init (GtkEntryClass *class)
655 {
656   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
657   GtkWidgetClass *widget_class;
658   GtkBindingSet *binding_set;
659
660   widget_class = (GtkWidgetClass*) class;
661
662   gobject_class->dispose = gtk_entry_dispose;
663   gobject_class->finalize = gtk_entry_finalize;
664   gobject_class->set_property = gtk_entry_set_property;
665   gobject_class->get_property = gtk_entry_get_property;
666
667   widget_class->destroy = gtk_entry_destroy;
668   widget_class->map = gtk_entry_map;
669   widget_class->unmap = gtk_entry_unmap;
670   widget_class->realize = gtk_entry_realize;
671   widget_class->unrealize = gtk_entry_unrealize;
672   widget_class->get_preferred_width = gtk_entry_get_preferred_width;
673   widget_class->get_preferred_height = gtk_entry_get_preferred_height;
674   widget_class->size_allocate = gtk_entry_size_allocate;
675   widget_class->draw = gtk_entry_draw;
676   widget_class->enter_notify_event = gtk_entry_enter_notify;
677   widget_class->leave_notify_event = gtk_entry_leave_notify;
678   widget_class->button_press_event = gtk_entry_button_press;
679   widget_class->button_release_event = gtk_entry_button_release;
680   widget_class->motion_notify_event = gtk_entry_motion_notify;
681   widget_class->key_press_event = gtk_entry_key_press;
682   widget_class->key_release_event = gtk_entry_key_release;
683   widget_class->focus_in_event = gtk_entry_focus_in;
684   widget_class->focus_out_event = gtk_entry_focus_out;
685   widget_class->grab_focus = gtk_entry_grab_focus;
686   widget_class->style_updated = gtk_entry_style_updated;
687   widget_class->query_tooltip = gtk_entry_query_tooltip;
688   widget_class->drag_begin = gtk_entry_drag_begin;
689   widget_class->drag_end = gtk_entry_drag_end;
690   widget_class->direction_changed = gtk_entry_direction_changed;
691   widget_class->state_flags_changed = gtk_entry_state_flags_changed;
692   widget_class->screen_changed = gtk_entry_screen_changed;
693   widget_class->mnemonic_activate = gtk_entry_mnemonic_activate;
694
695   widget_class->drag_drop = gtk_entry_drag_drop;
696   widget_class->drag_motion = gtk_entry_drag_motion;
697   widget_class->drag_leave = gtk_entry_drag_leave;
698   widget_class->drag_data_received = gtk_entry_drag_data_received;
699   widget_class->drag_data_get = gtk_entry_drag_data_get;
700   widget_class->drag_data_delete = gtk_entry_drag_data_delete;
701
702   widget_class->popup_menu = gtk_entry_popup_menu;
703
704   class->move_cursor = gtk_entry_move_cursor;
705   class->insert_at_cursor = gtk_entry_insert_at_cursor;
706   class->delete_from_cursor = gtk_entry_delete_from_cursor;
707   class->backspace = gtk_entry_backspace;
708   class->cut_clipboard = gtk_entry_cut_clipboard;
709   class->copy_clipboard = gtk_entry_copy_clipboard;
710   class->paste_clipboard = gtk_entry_paste_clipboard;
711   class->toggle_overwrite = gtk_entry_toggle_overwrite;
712   class->activate = gtk_entry_real_activate;
713   class->get_text_area_size = gtk_entry_get_text_area_size;
714   
715   quark_inner_border = g_quark_from_static_string ("gtk-entry-inner-border");
716   quark_password_hint = g_quark_from_static_string ("gtk-entry-password-hint");
717   quark_cursor_hadjustment = g_quark_from_static_string ("gtk-hadjustment");
718   quark_capslock_feedback = g_quark_from_static_string ("gtk-entry-capslock-feedback");
719
720   g_object_class_override_property (gobject_class,
721                                     PROP_EDITING_CANCELED,
722                                     "editing-canceled");
723
724   g_object_class_install_property (gobject_class,
725                                    PROP_BUFFER,
726                                    g_param_spec_object ("buffer",
727                                                         P_("Text Buffer"),
728                                                         P_("Text buffer object which actually stores entry text"),
729                                                         GTK_TYPE_ENTRY_BUFFER,
730                                                         GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
731
732   g_object_class_install_property (gobject_class,
733                                    PROP_CURSOR_POSITION,
734                                    g_param_spec_int ("cursor-position",
735                                                      P_("Cursor Position"),
736                                                      P_("The current position of the insertion cursor in chars"),
737                                                      0,
738                                                      GTK_ENTRY_BUFFER_MAX_SIZE,
739                                                      0,
740                                                      GTK_PARAM_READABLE));
741   
742   g_object_class_install_property (gobject_class,
743                                    PROP_SELECTION_BOUND,
744                                    g_param_spec_int ("selection-bound",
745                                                      P_("Selection Bound"),
746                                                      P_("The position of the opposite end of the selection from the cursor in chars"),
747                                                      0,
748                                                      GTK_ENTRY_BUFFER_MAX_SIZE,
749                                                      0,
750                                                      GTK_PARAM_READABLE));
751   
752   g_object_class_install_property (gobject_class,
753                                    PROP_EDITABLE,
754                                    g_param_spec_boolean ("editable",
755                                                          P_("Editable"),
756                                                          P_("Whether the entry contents can be edited"),
757                                                          TRUE,
758                                                          GTK_PARAM_READWRITE));
759   
760   g_object_class_install_property (gobject_class,
761                                    PROP_MAX_LENGTH,
762                                    g_param_spec_int ("max-length",
763                                                      P_("Maximum length"),
764                                                      P_("Maximum number of characters for this entry. Zero if no maximum"),
765                                                      0,
766                                                      GTK_ENTRY_BUFFER_MAX_SIZE,
767                                                      0,
768                                                      GTK_PARAM_READWRITE));
769   g_object_class_install_property (gobject_class,
770                                    PROP_VISIBILITY,
771                                    g_param_spec_boolean ("visibility",
772                                                          P_("Visibility"),
773                                                          P_("FALSE displays the \"invisible char\" instead of the actual text (password mode)"),
774                                                          TRUE,
775                                                          GTK_PARAM_READWRITE));
776
777   g_object_class_install_property (gobject_class,
778                                    PROP_HAS_FRAME,
779                                    g_param_spec_boolean ("has-frame",
780                                                          P_("Has Frame"),
781                                                          P_("FALSE removes outside bevel from entry"),
782                                                          TRUE,
783                                                          GTK_PARAM_READWRITE));
784
785   /**
786    * GtkEntry:inner-border:
787    *
788    * Sets the text area's border between the text and the frame.
789    *
790    * Deprecated: 3.4: Use the standard border and padding CSS properties;
791    *   the value of this style property is ignored.
792    */
793   g_object_class_install_property (gobject_class,
794                                    PROP_INNER_BORDER,
795                                    g_param_spec_boxed ("inner-border",
796                                                        P_("Inner Border"),
797                                                        P_("Border between text and frame. Overrides the inner-border style property"),
798                                                        GTK_TYPE_BORDER,
799                                                        GTK_PARAM_READWRITE |
800                                                        G_PARAM_DEPRECATED));
801
802   g_object_class_install_property (gobject_class,
803                                    PROP_INVISIBLE_CHAR,
804                                    g_param_spec_unichar ("invisible-char",
805                                                          P_("Invisible character"),
806                                                          P_("The character to use when masking entry contents (in \"password mode\")"),
807                                                          '*',
808                                                          GTK_PARAM_READWRITE));
809
810   g_object_class_install_property (gobject_class,
811                                    PROP_ACTIVATES_DEFAULT,
812                                    g_param_spec_boolean ("activates-default",
813                                                          P_("Activates default"),
814                                                          P_("Whether to activate the default widget (such as the default button in a dialog) when Enter is pressed"),
815                                                          FALSE,
816                                                          GTK_PARAM_READWRITE));
817   g_object_class_install_property (gobject_class,
818                                    PROP_WIDTH_CHARS,
819                                    g_param_spec_int ("width-chars",
820                                                      P_("Width in chars"),
821                                                      P_("Number of characters to leave space for in the entry"),
822                                                      -1,
823                                                      G_MAXINT,
824                                                      -1,
825                                                      GTK_PARAM_READWRITE));
826
827   g_object_class_install_property (gobject_class,
828                                    PROP_SCROLL_OFFSET,
829                                    g_param_spec_int ("scroll-offset",
830                                                      P_("Scroll offset"),
831                                                      P_("Number of pixels of the entry scrolled off the screen to the left"),
832                                                      0,
833                                                      G_MAXINT,
834                                                      0,
835                                                      GTK_PARAM_READABLE));
836
837   g_object_class_install_property (gobject_class,
838                                    PROP_TEXT,
839                                    g_param_spec_string ("text",
840                                                         P_("Text"),
841                                                         P_("The contents of the entry"),
842                                                         "",
843                                                         GTK_PARAM_READWRITE));
844
845   /**
846    * GtkEntry:xalign:
847    *
848    * The horizontal alignment, from 0 (left) to 1 (right). 
849    * Reversed for RTL layouts.
850    * 
851    * Since: 2.4
852    */
853   g_object_class_install_property (gobject_class,
854                                    PROP_XALIGN,
855                                    g_param_spec_float ("xalign",
856                                                        P_("X align"),
857                                                        P_("The horizontal alignment, from 0 (left) to 1 (right). Reversed for RTL layouts."),
858                                                        0.0,
859                                                        1.0,
860                                                        0.0,
861                                                        GTK_PARAM_READWRITE));
862
863   /**
864    * GtkEntry:truncate-multiline:
865    *
866    * When %TRUE, pasted multi-line text is truncated to the first line.
867    *
868    * Since: 2.10
869    */
870   g_object_class_install_property (gobject_class,
871                                    PROP_TRUNCATE_MULTILINE,
872                                    g_param_spec_boolean ("truncate-multiline",
873                                                          P_("Truncate multiline"),
874                                                          P_("Whether to truncate multiline pastes to one line."),
875                                                          FALSE,
876                                                          GTK_PARAM_READWRITE));
877
878   /**
879    * GtkEntry:shadow-type:
880    *
881    * Which kind of shadow to draw around the entry when 
882    * #GtkEntry:has-frame is set to %TRUE.
883    *
884    * Since: 2.12
885    */
886   g_object_class_install_property (gobject_class,
887                                    PROP_SHADOW_TYPE,
888                                    g_param_spec_enum ("shadow-type",
889                                                       P_("Shadow type"),
890                                                       P_("Which kind of shadow to draw around the entry when has-frame is set"),
891                                                       GTK_TYPE_SHADOW_TYPE,
892                                                       GTK_SHADOW_IN,
893                                                       GTK_PARAM_READWRITE));
894
895   /**
896    * GtkEntry:overwrite-mode:
897    *
898    * If text is overwritten when typing in the #GtkEntry.
899    *
900    * Since: 2.14
901    */
902   g_object_class_install_property (gobject_class,
903                                    PROP_OVERWRITE_MODE,
904                                    g_param_spec_boolean ("overwrite-mode",
905                                                          P_("Overwrite mode"),
906                                                          P_("Whether new text overwrites existing text"),
907                                                          FALSE,
908                                                          GTK_PARAM_READWRITE));
909
910   /**
911    * GtkEntry:text-length:
912    *
913    * The length of the text in the #GtkEntry.
914    *
915    * Since: 2.14
916    */
917   g_object_class_install_property (gobject_class,
918                                    PROP_TEXT_LENGTH,
919                                    g_param_spec_uint ("text-length",
920                                                       P_("Text length"),
921                                                       P_("Length of the text currently in the entry"),
922                                                       0, 
923                                                       G_MAXUINT16,
924                                                       0,
925                                                       GTK_PARAM_READABLE));
926   /**
927    * GtkEntry:invisible-char-set:
928    *
929    * Whether the invisible char has been set for the #GtkEntry.
930    *
931    * Since: 2.16
932    */
933   g_object_class_install_property (gobject_class,
934                                    PROP_INVISIBLE_CHAR_SET,
935                                    g_param_spec_boolean ("invisible-char-set",
936                                                          P_("Invisible character set"),
937                                                          P_("Whether the invisible character has been set"),
938                                                          FALSE,
939                                                          GTK_PARAM_READWRITE));
940
941   /**
942    * GtkEntry:caps-lock-warning
943    *
944    * Whether password entries will show a warning when Caps Lock is on.
945    *
946    * Note that the warning is shown using a secondary icon, and thus
947    * does not work if you are using the secondary icon position for some 
948    * other purpose.
949    *
950    * Since: 2.16
951    */
952   g_object_class_install_property (gobject_class,
953                                    PROP_CAPS_LOCK_WARNING,
954                                    g_param_spec_boolean ("caps-lock-warning",
955                                                          P_("Caps Lock warning"),
956                                                          P_("Whether password entries will show a warning when Caps Lock is on"),
957                                                          TRUE,
958                                                          GTK_PARAM_READWRITE));
959
960   /**
961    * GtkEntry:progress-fraction:
962    *
963    * The current fraction of the task that's been completed.
964    *
965    * Since: 2.16
966    */
967   g_object_class_install_property (gobject_class,
968                                    PROP_PROGRESS_FRACTION,
969                                    g_param_spec_double ("progress-fraction",
970                                                         P_("Progress Fraction"),
971                                                         P_("The current fraction of the task that's been completed"),
972                                                         0.0,
973                                                         1.0,
974                                                         0.0,
975                                                         GTK_PARAM_READWRITE));
976
977   /**
978    * GtkEntry:progress-pulse-step:
979    *
980    * The fraction of total entry width to move the progress
981    * bouncing block for each call to gtk_entry_progress_pulse().
982    *
983    * Since: 2.16
984    */
985   g_object_class_install_property (gobject_class,
986                                    PROP_PROGRESS_PULSE_STEP,
987                                    g_param_spec_double ("progress-pulse-step",
988                                                         P_("Progress Pulse Step"),
989                                                         P_("The fraction of total entry width to move the progress bouncing block for each call to gtk_entry_progress_pulse()"),
990                                                         0.0,
991                                                         1.0,
992                                                         0.1,
993                                                         GTK_PARAM_READWRITE));
994
995   /**
996   * GtkEntry:placeholder-text:
997   *
998   * The text that will be displayed in the #GtkEntry when it is empty
999   * and unfocused.
1000   *
1001   * Since: 3.2
1002   */
1003  g_object_class_install_property (gobject_class,
1004                                   PROP_PLACEHOLDER_TEXT,
1005                                   g_param_spec_string ("placeholder-text",
1006                                                        P_("Placeholder text"),
1007                                                        P_("Show text in the entry when it's empty and unfocused"),
1008                                                        NULL,
1009                                                        GTK_PARAM_READWRITE));
1010
1011    /**
1012    * GtkEntry:primary-icon-pixbuf:
1013    *
1014    * A pixbuf to use as the primary icon for the entry.
1015    *
1016    * Since: 2.16
1017    */
1018   g_object_class_install_property (gobject_class,
1019                                    PROP_PIXBUF_PRIMARY,
1020                                    g_param_spec_object ("primary-icon-pixbuf",
1021                                                         P_("Primary pixbuf"),
1022                                                         P_("Primary pixbuf for the entry"),
1023                                                         GDK_TYPE_PIXBUF,
1024                                                         GTK_PARAM_READWRITE));
1025   
1026   /**
1027    * GtkEntry:secondary-icon-pixbuf:
1028    *
1029    * An pixbuf to use as the secondary icon for the entry.
1030    *
1031    * Since: 2.16
1032    */
1033   g_object_class_install_property (gobject_class,
1034                                    PROP_PIXBUF_SECONDARY,
1035                                    g_param_spec_object ("secondary-icon-pixbuf",
1036                                                         P_("Secondary pixbuf"),
1037                                                         P_("Secondary pixbuf for the entry"),
1038                                                         GDK_TYPE_PIXBUF,
1039                                                         GTK_PARAM_READWRITE));
1040
1041   /**
1042    * GtkEntry:primary-icon-stock:
1043    *
1044    * The stock id to use for the primary icon for the entry.
1045    *
1046    * Since: 2.16
1047    */
1048   g_object_class_install_property (gobject_class,
1049                                    PROP_STOCK_PRIMARY,
1050                                    g_param_spec_string ("primary-icon-stock",
1051                                                         P_("Primary stock ID"),
1052                                                         P_("Stock ID for primary icon"),
1053                                                         NULL,
1054                                                         GTK_PARAM_READWRITE));
1055
1056   /**
1057    * GtkEntry:secondary-icon-stock:
1058    *
1059    * The stock id to use for the secondary icon for the entry.
1060    *
1061    * Since: 2.16
1062    */
1063   g_object_class_install_property (gobject_class,
1064                                    PROP_STOCK_SECONDARY,
1065                                    g_param_spec_string ("secondary-icon-stock",
1066                                                         P_("Secondary stock ID"),
1067                                                         P_("Stock ID for secondary icon"),
1068                                                         NULL,
1069                                                         GTK_PARAM_READWRITE));
1070   
1071   /**
1072    * GtkEntry:primary-icon-name:
1073    *
1074    * The icon name to use for the primary icon for the entry.
1075    *
1076    * Since: 2.16
1077    */
1078   g_object_class_install_property (gobject_class,
1079                                    PROP_ICON_NAME_PRIMARY,
1080                                    g_param_spec_string ("primary-icon-name",
1081                                                         P_("Primary icon name"),
1082                                                         P_("Icon name for primary icon"),
1083                                                         NULL,
1084                                                         GTK_PARAM_READWRITE));
1085   
1086   /**
1087    * GtkEntry:secondary-icon-name:
1088    *
1089    * The icon name to use for the secondary icon for the entry.
1090    *
1091    * Since: 2.16
1092    */
1093   g_object_class_install_property (gobject_class,
1094                                    PROP_ICON_NAME_SECONDARY,
1095                                    g_param_spec_string ("secondary-icon-name",
1096                                                         P_("Secondary icon name"),
1097                                                         P_("Icon name for secondary icon"),
1098                                                         NULL,
1099                                                         GTK_PARAM_READWRITE));
1100   
1101   /**
1102    * GtkEntry:primary-icon-gicon:
1103    *
1104    * The #GIcon to use for the primary icon for the entry.
1105    *
1106    * Since: 2.16
1107    */
1108   g_object_class_install_property (gobject_class,
1109                                    PROP_GICON_PRIMARY,
1110                                    g_param_spec_object ("primary-icon-gicon",
1111                                                         P_("Primary GIcon"),
1112                                                         P_("GIcon for primary icon"),
1113                                                         G_TYPE_ICON,
1114                                                         GTK_PARAM_READWRITE));
1115   
1116   /**
1117    * GtkEntry:secondary-icon-gicon:
1118    *
1119    * The #GIcon to use for the secondary icon for the entry.
1120    *
1121    * Since: 2.16
1122    */
1123   g_object_class_install_property (gobject_class,
1124                                    PROP_GICON_SECONDARY,
1125                                    g_param_spec_object ("secondary-icon-gicon",
1126                                                         P_("Secondary GIcon"),
1127                                                         P_("GIcon for secondary icon"),
1128                                                         G_TYPE_ICON,
1129                                                         GTK_PARAM_READWRITE));
1130   
1131   /**
1132    * GtkEntry:primary-icon-storage-type:
1133    *
1134    * The representation which is used for the primary icon of the entry.
1135    *
1136    * Since: 2.16
1137    */
1138   g_object_class_install_property (gobject_class,
1139                                    PROP_STORAGE_TYPE_PRIMARY,
1140                                    g_param_spec_enum ("primary-icon-storage-type",
1141                                                       P_("Primary storage type"),
1142                                                       P_("The representation being used for primary icon"),
1143                                                       GTK_TYPE_IMAGE_TYPE,
1144                                                       GTK_IMAGE_EMPTY,
1145                                                       GTK_PARAM_READABLE));
1146   
1147   /**
1148    * GtkEntry:secondary-icon-storage-type:
1149    *
1150    * The representation which is used for the secondary icon of the entry.
1151    *
1152    * Since: 2.16
1153    */
1154   g_object_class_install_property (gobject_class,
1155                                    PROP_STORAGE_TYPE_SECONDARY,
1156                                    g_param_spec_enum ("secondary-icon-storage-type",
1157                                                       P_("Secondary storage type"),
1158                                                       P_("The representation being used for secondary icon"),
1159                                                       GTK_TYPE_IMAGE_TYPE,
1160                                                       GTK_IMAGE_EMPTY,
1161                                                       GTK_PARAM_READABLE));
1162   
1163   /**
1164    * GtkEntry:primary-icon-activatable:
1165    *
1166    * Whether the primary icon is activatable.
1167    *
1168    * GTK+ emits the #GtkEntry::icon-press and #GtkEntry::icon-release 
1169    * signals only on sensitive, activatable icons. 
1170    *
1171    * Sensitive, but non-activatable icons can be used for purely 
1172    * informational purposes.
1173    *
1174    * Since: 2.16
1175    */
1176   g_object_class_install_property (gobject_class,
1177                                    PROP_ACTIVATABLE_PRIMARY,
1178                                    g_param_spec_boolean ("primary-icon-activatable",
1179                                                          P_("Primary icon activatable"),
1180                                                          P_("Whether the primary icon is activatable"),
1181                                                          TRUE,
1182                                                          GTK_PARAM_READWRITE));
1183   
1184   /**
1185    * GtkEntry:secondary-icon-activatable:
1186    *
1187    * Whether the secondary icon is activatable.
1188    *
1189    * GTK+ emits the #GtkEntry::icon-press and #GtkEntry::icon-release 
1190    * signals only on sensitive, activatable icons.
1191    *
1192    * Sensitive, but non-activatable icons can be used for purely 
1193    * informational purposes.
1194    *
1195    * Since: 2.16
1196    */
1197   g_object_class_install_property (gobject_class,
1198                                    PROP_ACTIVATABLE_SECONDARY,
1199                                    g_param_spec_boolean ("secondary-icon-activatable",
1200                                                          P_("Secondary icon activatable"),
1201                                                          P_("Whether the secondary icon is activatable"),
1202                                                          TRUE,
1203                                                          GTK_PARAM_READWRITE));
1204   
1205   
1206   /**
1207    * GtkEntry:primary-icon-sensitive:
1208    *
1209    * Whether the primary icon is sensitive.
1210    *
1211    * An insensitive icon appears grayed out. GTK+ does not emit the 
1212    * #GtkEntry::icon-press and #GtkEntry::icon-release signals and 
1213    * does not allow DND from insensitive icons.
1214    *
1215    * An icon should be set insensitive if the action that would trigger
1216    * when clicked is currently not available.
1217    * 
1218    * Since: 2.16
1219    */
1220   g_object_class_install_property (gobject_class,
1221                                    PROP_SENSITIVE_PRIMARY,
1222                                    g_param_spec_boolean ("primary-icon-sensitive",
1223                                                          P_("Primary icon sensitive"),
1224                                                          P_("Whether the primary icon is sensitive"),
1225                                                          TRUE,
1226                                                          GTK_PARAM_READWRITE));
1227   
1228   /**
1229    * GtkEntry:secondary-icon-sensitive:
1230    *
1231    * Whether the secondary icon is sensitive.
1232    *
1233    * An insensitive icon appears grayed out. GTK+ does not emit the 
1234    * #GtkEntry::icon-press and #GtkEntry::icon-release signals and 
1235    * does not allow DND from insensitive icons.
1236    *
1237    * An icon should be set insensitive if the action that would trigger
1238    * when clicked is currently not available.
1239    *
1240    * Since: 2.16
1241    */
1242   g_object_class_install_property (gobject_class,
1243                                    PROP_SENSITIVE_SECONDARY,
1244                                    g_param_spec_boolean ("secondary-icon-sensitive",
1245                                                          P_("Secondary icon sensitive"),
1246                                                          P_("Whether the secondary icon is sensitive"),
1247                                                          TRUE,
1248                                                          GTK_PARAM_READWRITE));
1249   
1250   /**
1251    * GtkEntry:primary-icon-tooltip-text:
1252    * 
1253    * The contents of the tooltip on the primary icon.
1254    *
1255    * Also see gtk_entry_set_icon_tooltip_text().
1256    *
1257    * Since: 2.16
1258    */
1259   g_object_class_install_property (gobject_class,
1260                                    PROP_TOOLTIP_TEXT_PRIMARY,
1261                                    g_param_spec_string ("primary-icon-tooltip-text",
1262                                                         P_("Primary icon tooltip text"),
1263                                                         P_("The contents of the tooltip on the primary icon"),                              
1264                                                         NULL,
1265                                                         GTK_PARAM_READWRITE));
1266   
1267   /**
1268    * GtkEntry:secondary-icon-tooltip-text:
1269    * 
1270    * The contents of the tooltip on the secondary icon.
1271    *
1272    * Also see gtk_entry_set_icon_tooltip_text().
1273    *
1274    * Since: 2.16
1275    */
1276   g_object_class_install_property (gobject_class,
1277                                    PROP_TOOLTIP_TEXT_SECONDARY,
1278                                    g_param_spec_string ("secondary-icon-tooltip-text",
1279                                                         P_("Secondary icon tooltip text"),
1280                                                         P_("The contents of the tooltip on the secondary icon"),                              
1281                                                         NULL,
1282                                                         GTK_PARAM_READWRITE));
1283
1284   /**
1285    * GtkEntry:primary-icon-tooltip-markup:
1286    * 
1287    * The contents of the tooltip on the primary icon, which is marked up
1288    * with the <link linkend="PangoMarkupFormat">Pango text markup 
1289    * language</link>.
1290    *
1291    * Also see gtk_entry_set_icon_tooltip_markup().
1292    *
1293    * Since: 2.16
1294    */
1295   g_object_class_install_property (gobject_class,
1296                                    PROP_TOOLTIP_MARKUP_PRIMARY,
1297                                    g_param_spec_string ("primary-icon-tooltip-markup",
1298                                                         P_("Primary icon tooltip markup"),
1299                                                         P_("The contents of the tooltip on the primary icon"),                              
1300                                                         NULL,
1301                                                         GTK_PARAM_READWRITE));
1302
1303   /**
1304    * GtkEntry:secondary-icon-tooltip-markup:
1305    * 
1306    * The contents of the tooltip on the secondary icon, which is marked up
1307    * with the <link linkend="PangoMarkupFormat">Pango text markup 
1308    * language</link>.
1309    *
1310    * Also see gtk_entry_set_icon_tooltip_markup().
1311    *
1312    * Since: 2.16
1313    */
1314   g_object_class_install_property (gobject_class,
1315                                    PROP_TOOLTIP_MARKUP_SECONDARY,
1316                                    g_param_spec_string ("secondary-icon-tooltip-markup",
1317                                                         P_("Secondary icon tooltip markup"),
1318                                                         P_("The contents of the tooltip on the secondary icon"),                              
1319                                                         NULL,
1320                                                         GTK_PARAM_READWRITE));
1321
1322   /**
1323    * GtkEntry:im-module:
1324    *
1325    * Which IM (input method) module should be used for this entry. 
1326    * See #GtkIMContext.
1327    * 
1328    * Setting this to a non-%NULL value overrides the
1329    * system-wide IM module setting. See the GtkSettings 
1330    * #GtkSettings:gtk-im-module property.
1331    *
1332    * Since: 2.16
1333    */  
1334   g_object_class_install_property (gobject_class,
1335                                    PROP_IM_MODULE,
1336                                    g_param_spec_string ("im-module",
1337                                                         P_("IM module"),
1338                                                         P_("Which IM module should be used"),
1339                                                         NULL,
1340                                                         GTK_PARAM_READWRITE));
1341
1342   /**
1343    * GtkEntry:completion:
1344    *
1345    * The auxiliary completion object to use with the entry.
1346    *
1347    * Since: 3.2
1348    */     
1349   g_object_class_install_property (gobject_class,
1350                                    PROP_COMPLETION,
1351                                    g_param_spec_object ("completion",
1352                                                         P_("Completion"),
1353                                                         P_("The auxiliary completion object"),
1354                                                         GTK_TYPE_ENTRY_COMPLETION,
1355                                                         GTK_PARAM_READWRITE));
1356
1357   /**
1358    * GtkEntry:icon-prelight:
1359    *
1360    * The prelight style property determines whether activatable
1361    * icons prelight on mouseover.
1362    *
1363    * Since: 2.16
1364    */
1365   gtk_widget_class_install_style_property (widget_class,
1366                                            g_param_spec_boolean ("icon-prelight",
1367                                                                  P_("Icon Prelight"),
1368                                                                  P_("Whether activatable icons should prelight when hovered"),
1369                                                                  TRUE,
1370                                                                  GTK_PARAM_READABLE));
1371
1372   /**
1373    * GtkEntry:progress-border:
1374    *
1375    * The border around the progress bar in the entry.
1376    *
1377    * Since: 2.16
1378    *
1379    * Deprecated: 3.4: Use the standard margin CSS property;
1380    *   the value of this style property is ignored.
1381    */
1382   gtk_widget_class_install_style_property (widget_class,
1383                                            g_param_spec_boxed ("progress-border",
1384                                                                P_("Progress Border"),
1385                                                                P_("Border around the progress bar"),
1386                                                                GTK_TYPE_BORDER,
1387                                                                GTK_PARAM_READABLE |
1388                                                                G_PARAM_DEPRECATED));
1389   
1390   /**
1391    * GtkEntry:invisible-char:
1392    *
1393    * The invisible character is used when masking entry contents (in
1394    * \"password mode\")"). When it is not explicitly set with the
1395    * #GtkEntry:invisible-char property, GTK+ determines the character
1396    * to use from a list of possible candidates, depending on availability
1397    * in the current font.
1398    *
1399    * This style property allows the theme to prepend a character
1400    * to the list of candidates.
1401    *
1402    * Since: 2.18
1403    */
1404   gtk_widget_class_install_style_property (widget_class,
1405                                            g_param_spec_unichar ("invisible-char",
1406                                                                  P_("Invisible character"),
1407                                                                  P_("The character to use when masking entry contents (in \"password mode\")"),
1408                                                                  0,
1409                                                                  GTK_PARAM_READABLE));
1410   
1411   /**
1412    * GtkEntry::populate-popup:
1413    * @entry: The entry on which the signal is emitted
1414    * @menu: the menu that is being populated
1415    *
1416    * The ::populate-popup signal gets emitted before showing the 
1417    * context menu of the entry. 
1418    *
1419    * If you need to add items to the context menu, connect
1420    * to this signal and append your menuitems to the @menu.
1421    */
1422   signals[POPULATE_POPUP] =
1423     g_signal_new (I_("populate-popup"),
1424                   G_OBJECT_CLASS_TYPE (gobject_class),
1425                   G_SIGNAL_RUN_LAST,
1426                   G_STRUCT_OFFSET (GtkEntryClass, populate_popup),
1427                   NULL, NULL,
1428                   _gtk_marshal_VOID__OBJECT,
1429                   G_TYPE_NONE, 1,
1430                   GTK_TYPE_MENU);
1431   
1432  /* Action signals */
1433   
1434   /**
1435    * GtkEntry::activate:
1436    * @entry: The entry on which the signal is emitted
1437    *
1438    * A  <link linkend="keybinding-signals">keybinding signal</link>
1439    * which gets emitted when the user activates the entry.
1440    * 
1441    * Applications should not connect to it, but may emit it with
1442    * g_signal_emit_by_name() if they need to control activation 
1443    * programmatically.
1444    *
1445    * The default bindings for this signal are all forms of the Enter key.
1446    */
1447   signals[ACTIVATE] =
1448     g_signal_new (I_("activate"),
1449                   G_OBJECT_CLASS_TYPE (gobject_class),
1450                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1451                   G_STRUCT_OFFSET (GtkEntryClass, activate),
1452                   NULL, NULL,
1453                   _gtk_marshal_VOID__VOID,
1454                   G_TYPE_NONE, 0);
1455   widget_class->activate_signal = signals[ACTIVATE];
1456
1457   /**
1458    * GtkEntry::move-cursor:
1459    * @entry: the object which received the signal
1460    * @step: the granularity of the move, as a #GtkMovementStep
1461    * @count: the number of @step units to move
1462    * @extend_selection: %TRUE if the move should extend the selection
1463    *
1464    * The ::move-cursor signal is a
1465    * <link linkend="keybinding-signals">keybinding signal</link>
1466    * which gets emitted when the user initiates a cursor movement.
1467    * If the cursor is not visible in @entry, this signal causes
1468    * the viewport to be moved instead.
1469    *
1470    * Applications should not connect to it, but may emit it with
1471    * g_signal_emit_by_name() if they need to control the cursor
1472    * programmatically.
1473    *
1474    * The default bindings for this signal come in two variants,
1475    * the variant with the Shift modifier extends the selection,
1476    * the variant without the Shift modifer does not.
1477    * There are too many key combinations to list them all here.
1478    * <itemizedlist>
1479    * <listitem>Arrow keys move by individual characters/lines</listitem>
1480    * <listitem>Ctrl-arrow key combinations move by words/paragraphs</listitem>
1481    * <listitem>Home/End keys move to the ends of the buffer</listitem>
1482    * </itemizedlist>
1483    */
1484   signals[MOVE_CURSOR] = 
1485     g_signal_new (I_("move-cursor"),
1486                   G_OBJECT_CLASS_TYPE (gobject_class),
1487                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1488                   G_STRUCT_OFFSET (GtkEntryClass, move_cursor),
1489                   NULL, NULL,
1490                   _gtk_marshal_VOID__ENUM_INT_BOOLEAN,
1491                   G_TYPE_NONE, 3,
1492                   GTK_TYPE_MOVEMENT_STEP,
1493                   G_TYPE_INT,
1494                   G_TYPE_BOOLEAN);
1495
1496   /**
1497    * GtkEntry::insert-at-cursor:
1498    * @entry: the object which received the signal
1499    * @string: the string to insert
1500    *
1501    * The ::insert-at-cursor signal is a
1502    * <link linkend="keybinding-signals">keybinding signal</link>
1503    * which gets emitted when the user initiates the insertion of a
1504    * fixed string at the cursor.
1505    *
1506    * This signal has no default bindings.
1507    */
1508   signals[INSERT_AT_CURSOR] = 
1509     g_signal_new (I_("insert-at-cursor"),
1510                   G_OBJECT_CLASS_TYPE (gobject_class),
1511                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1512                   G_STRUCT_OFFSET (GtkEntryClass, insert_at_cursor),
1513                   NULL, NULL,
1514                   _gtk_marshal_VOID__STRING,
1515                   G_TYPE_NONE, 1,
1516                   G_TYPE_STRING);
1517
1518   /**
1519    * GtkEntry::delete-from-cursor:
1520    * @entry: the object which received the signal
1521    * @type: the granularity of the deletion, as a #GtkDeleteType
1522    * @count: the number of @type units to delete
1523    *
1524    * The ::delete-from-cursor signal is a
1525    * <link linkend="keybinding-signals">keybinding signal</link>
1526    * which gets emitted when the user initiates a text deletion.
1527    *
1528    * If the @type is %GTK_DELETE_CHARS, GTK+ deletes the selection
1529    * if there is one, otherwise it deletes the requested number
1530    * of characters.
1531    *
1532    * The default bindings for this signal are
1533    * Delete for deleting a character and Ctrl-Delete for
1534    * deleting a word.
1535    */
1536   signals[DELETE_FROM_CURSOR] = 
1537     g_signal_new (I_("delete-from-cursor"),
1538                   G_OBJECT_CLASS_TYPE (gobject_class),
1539                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1540                   G_STRUCT_OFFSET (GtkEntryClass, delete_from_cursor),
1541                   NULL, NULL,
1542                   _gtk_marshal_VOID__ENUM_INT,
1543                   G_TYPE_NONE, 2,
1544                   GTK_TYPE_DELETE_TYPE,
1545                   G_TYPE_INT);
1546
1547   /**
1548    * GtkEntry::backspace:
1549    * @entry: the object which received the signal
1550    *
1551    * The ::backspace signal is a
1552    * <link linkend="keybinding-signals">keybinding signal</link>
1553    * which gets emitted when the user asks for it.
1554    *
1555    * The default bindings for this signal are
1556    * Backspace and Shift-Backspace.
1557    */
1558   signals[BACKSPACE] =
1559     g_signal_new (I_("backspace"),
1560                   G_OBJECT_CLASS_TYPE (gobject_class),
1561                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1562                   G_STRUCT_OFFSET (GtkEntryClass, backspace),
1563                   NULL, NULL,
1564                   _gtk_marshal_VOID__VOID,
1565                   G_TYPE_NONE, 0);
1566
1567   /**
1568    * GtkEntry::cut-clipboard:
1569    * @entry: the object which received the signal
1570    *
1571    * The ::cut-clipboard signal is a
1572    * <link linkend="keybinding-signals">keybinding signal</link>
1573    * which gets emitted to cut the selection to the clipboard.
1574    *
1575    * The default bindings for this signal are
1576    * Ctrl-x and Shift-Delete.
1577    */
1578   signals[CUT_CLIPBOARD] =
1579     g_signal_new (I_("cut-clipboard"),
1580                   G_OBJECT_CLASS_TYPE (gobject_class),
1581                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1582                   G_STRUCT_OFFSET (GtkEntryClass, cut_clipboard),
1583                   NULL, NULL,
1584                   _gtk_marshal_VOID__VOID,
1585                   G_TYPE_NONE, 0);
1586
1587   /**
1588    * GtkEntry::copy-clipboard:
1589    * @entry: the object which received the signal
1590    *
1591    * The ::copy-clipboard signal is a
1592    * <link linkend="keybinding-signals">keybinding signal</link>
1593    * which gets emitted to copy the selection to the clipboard.
1594    *
1595    * The default bindings for this signal are
1596    * Ctrl-c and Ctrl-Insert.
1597    */
1598   signals[COPY_CLIPBOARD] =
1599     g_signal_new (I_("copy-clipboard"),
1600                   G_OBJECT_CLASS_TYPE (gobject_class),
1601                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1602                   G_STRUCT_OFFSET (GtkEntryClass, copy_clipboard),
1603                   NULL, NULL,
1604                   _gtk_marshal_VOID__VOID,
1605                   G_TYPE_NONE, 0);
1606
1607   /**
1608    * GtkEntry::paste-clipboard:
1609    * @entry: the object which received the signal
1610    *
1611    * The ::paste-clipboard signal is a
1612    * <link linkend="keybinding-signals">keybinding signal</link>
1613    * which gets emitted to paste the contents of the clipboard
1614    * into the text view.
1615    *
1616    * The default bindings for this signal are
1617    * Ctrl-v and Shift-Insert.
1618    */
1619   signals[PASTE_CLIPBOARD] =
1620     g_signal_new (I_("paste-clipboard"),
1621                   G_OBJECT_CLASS_TYPE (gobject_class),
1622                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1623                   G_STRUCT_OFFSET (GtkEntryClass, paste_clipboard),
1624                   NULL, NULL,
1625                   _gtk_marshal_VOID__VOID,
1626                   G_TYPE_NONE, 0);
1627
1628   /**
1629    * GtkEntry::toggle-overwrite:
1630    * @entry: the object which received the signal
1631    *
1632    * The ::toggle-overwrite signal is a
1633    * <link linkend="keybinding-signals">keybinding signal</link>
1634    * which gets emitted to toggle the overwrite mode of the entry.
1635    *
1636    * The default bindings for this signal is Insert.
1637    */
1638   signals[TOGGLE_OVERWRITE] =
1639     g_signal_new (I_("toggle-overwrite"),
1640                   G_OBJECT_CLASS_TYPE (gobject_class),
1641                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1642                   G_STRUCT_OFFSET (GtkEntryClass, toggle_overwrite),
1643                   NULL, NULL,
1644                   _gtk_marshal_VOID__VOID,
1645                   G_TYPE_NONE, 0);
1646
1647   /**
1648    * GtkEntry::icon-press:
1649    * @entry: The entry on which the signal is emitted
1650    * @icon_pos: The position of the clicked icon
1651    * @event: (type Gdk.EventButton): the button press event
1652    *
1653    * The ::icon-press signal is emitted when an activatable icon
1654    * is clicked.
1655    *
1656    * Since: 2.16
1657    */
1658   signals[ICON_PRESS] =
1659     g_signal_new (I_("icon-press"),
1660                   G_TYPE_FROM_CLASS (gobject_class),
1661                   G_SIGNAL_RUN_LAST,
1662                   0,
1663                   NULL, NULL,
1664                   _gtk_marshal_VOID__ENUM_BOXED,
1665                   G_TYPE_NONE, 2,
1666                   GTK_TYPE_ENTRY_ICON_POSITION,
1667                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
1668   
1669   /**
1670    * GtkEntry::icon-release:
1671    * @entry: The entry on which the signal is emitted
1672    * @icon_pos: The position of the clicked icon
1673    * @event: (type Gdk.EventButton): the button release event
1674    *
1675    * The ::icon-release signal is emitted on the button release from a
1676    * mouse click over an activatable icon.
1677    *
1678    * Since: 2.16
1679    */
1680   signals[ICON_RELEASE] =
1681     g_signal_new (I_("icon-release"),
1682                   G_TYPE_FROM_CLASS (gobject_class),
1683                   G_SIGNAL_RUN_LAST,
1684                   0,
1685                   NULL, NULL,
1686                   _gtk_marshal_VOID__ENUM_BOXED,
1687                   G_TYPE_NONE, 2,
1688                   GTK_TYPE_ENTRY_ICON_POSITION,
1689                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
1690
1691   /**
1692    * GtkEntry::preedit-changed:
1693    * @entry: the object which received the signal
1694    * @preedit: the current preedit string
1695    *
1696    * If an input method is used, the typed text will not immediately
1697    * be committed to the buffer. So if you are interested in the text,
1698    * connect to this signal.
1699    *
1700    * Since: 2.20
1701    */
1702   signals[PREEDIT_CHANGED] =
1703     g_signal_new_class_handler (I_("preedit-changed"),
1704                                 G_OBJECT_CLASS_TYPE (gobject_class),
1705                                 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1706                                 NULL,
1707                                 NULL, NULL,
1708                                 _gtk_marshal_VOID__STRING,
1709                                 G_TYPE_NONE, 1,
1710                                 G_TYPE_STRING);
1711
1712
1713   /*
1714    * Key bindings
1715    */
1716
1717   binding_set = gtk_binding_set_by_class (class);
1718
1719   /* Moving the insertion point */
1720   add_move_binding (binding_set, GDK_KEY_Right, 0,
1721                     GTK_MOVEMENT_VISUAL_POSITIONS, 1);
1722   
1723   add_move_binding (binding_set, GDK_KEY_Left, 0,
1724                     GTK_MOVEMENT_VISUAL_POSITIONS, -1);
1725
1726   add_move_binding (binding_set, GDK_KEY_KP_Right, 0,
1727                     GTK_MOVEMENT_VISUAL_POSITIONS, 1);
1728   
1729   add_move_binding (binding_set, GDK_KEY_KP_Left, 0,
1730                     GTK_MOVEMENT_VISUAL_POSITIONS, -1);
1731   
1732   add_move_binding (binding_set, GDK_KEY_Right, GDK_CONTROL_MASK,
1733                     GTK_MOVEMENT_WORDS, 1);
1734
1735   add_move_binding (binding_set, GDK_KEY_Left, GDK_CONTROL_MASK,
1736                     GTK_MOVEMENT_WORDS, -1);
1737
1738   add_move_binding (binding_set, GDK_KEY_KP_Right, GDK_CONTROL_MASK,
1739                     GTK_MOVEMENT_WORDS, 1);
1740
1741   add_move_binding (binding_set, GDK_KEY_KP_Left, GDK_CONTROL_MASK,
1742                     GTK_MOVEMENT_WORDS, -1);
1743   
1744   add_move_binding (binding_set, GDK_KEY_Home, 0,
1745                     GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
1746
1747   add_move_binding (binding_set, GDK_KEY_End, 0,
1748                     GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
1749
1750   add_move_binding (binding_set, GDK_KEY_KP_Home, 0,
1751                     GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
1752
1753   add_move_binding (binding_set, GDK_KEY_KP_End, 0,
1754                     GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
1755   
1756   add_move_binding (binding_set, GDK_KEY_Home, GDK_CONTROL_MASK,
1757                     GTK_MOVEMENT_BUFFER_ENDS, -1);
1758
1759   add_move_binding (binding_set, GDK_KEY_End, GDK_CONTROL_MASK,
1760                     GTK_MOVEMENT_BUFFER_ENDS, 1);
1761
1762   add_move_binding (binding_set, GDK_KEY_KP_Home, GDK_CONTROL_MASK,
1763                     GTK_MOVEMENT_BUFFER_ENDS, -1);
1764
1765   add_move_binding (binding_set, GDK_KEY_KP_End, GDK_CONTROL_MASK,
1766                     GTK_MOVEMENT_BUFFER_ENDS, 1);
1767
1768   /* Select all
1769    */
1770   gtk_binding_entry_add_signal (binding_set, GDK_KEY_a, GDK_CONTROL_MASK,
1771                                 "move-cursor", 3,
1772                                 GTK_TYPE_MOVEMENT_STEP, GTK_MOVEMENT_BUFFER_ENDS,
1773                                 G_TYPE_INT, -1,
1774                                 G_TYPE_BOOLEAN, FALSE);
1775   gtk_binding_entry_add_signal (binding_set, GDK_KEY_a, GDK_CONTROL_MASK,
1776                                 "move-cursor", 3,
1777                                 GTK_TYPE_MOVEMENT_STEP, GTK_MOVEMENT_BUFFER_ENDS,
1778                                 G_TYPE_INT, 1,
1779                                 G_TYPE_BOOLEAN, TRUE);  
1780
1781   gtk_binding_entry_add_signal (binding_set, GDK_KEY_slash, GDK_CONTROL_MASK,
1782                                 "move-cursor", 3,
1783                                 GTK_TYPE_MOVEMENT_STEP, GTK_MOVEMENT_BUFFER_ENDS,
1784                                 G_TYPE_INT, -1,
1785                                 G_TYPE_BOOLEAN, FALSE);
1786   gtk_binding_entry_add_signal (binding_set, GDK_KEY_slash, GDK_CONTROL_MASK,
1787                                 "move-cursor", 3,
1788                                 GTK_TYPE_MOVEMENT_STEP, GTK_MOVEMENT_BUFFER_ENDS,
1789                                 G_TYPE_INT, 1,
1790                                 G_TYPE_BOOLEAN, TRUE);  
1791   /* Unselect all 
1792    */
1793   gtk_binding_entry_add_signal (binding_set, GDK_KEY_backslash, GDK_CONTROL_MASK,
1794                                 "move-cursor", 3,
1795                                 GTK_TYPE_MOVEMENT_STEP, GTK_MOVEMENT_VISUAL_POSITIONS,
1796                                 G_TYPE_INT, 0,
1797                                 G_TYPE_BOOLEAN, FALSE);
1798   gtk_binding_entry_add_signal (binding_set, GDK_KEY_a, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
1799                                 "move-cursor", 3,
1800                                 GTK_TYPE_MOVEMENT_STEP, GTK_MOVEMENT_VISUAL_POSITIONS,
1801                                 G_TYPE_INT, 0,
1802                                 G_TYPE_BOOLEAN, FALSE);
1803
1804   /* Activate
1805    */
1806   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Return, 0,
1807                                 "activate", 0);
1808   gtk_binding_entry_add_signal (binding_set, GDK_KEY_ISO_Enter, 0,
1809                                 "activate", 0);
1810   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Enter, 0,
1811                                 "activate", 0);
1812   
1813   /* Deleting text */
1814   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Delete, 0,
1815                                 "delete-from-cursor", 2,
1816                                 G_TYPE_ENUM, GTK_DELETE_CHARS,
1817                                 G_TYPE_INT, 1);
1818
1819   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Delete, 0,
1820                                 "delete-from-cursor", 2,
1821                                 G_TYPE_ENUM, GTK_DELETE_CHARS,
1822                                 G_TYPE_INT, 1);
1823   
1824   gtk_binding_entry_add_signal (binding_set, GDK_KEY_BackSpace, 0,
1825                                 "backspace", 0);
1826
1827   /* Make this do the same as Backspace, to help with mis-typing */
1828   gtk_binding_entry_add_signal (binding_set, GDK_KEY_BackSpace, GDK_SHIFT_MASK,
1829                                 "backspace", 0);
1830
1831   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Delete, GDK_CONTROL_MASK,
1832                                 "delete-from-cursor", 2,
1833                                 G_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
1834                                 G_TYPE_INT, 1);
1835
1836   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Delete, GDK_CONTROL_MASK,
1837                                 "delete-from-cursor", 2,
1838                                 G_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
1839                                 G_TYPE_INT, 1);
1840   
1841   gtk_binding_entry_add_signal (binding_set, GDK_KEY_BackSpace, GDK_CONTROL_MASK,
1842                                 "delete-from-cursor", 2,
1843                                 G_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
1844                                 G_TYPE_INT, -1);
1845
1846   /* Cut/copy/paste */
1847
1848   gtk_binding_entry_add_signal (binding_set, GDK_KEY_x, GDK_CONTROL_MASK,
1849                                 "cut-clipboard", 0);
1850   gtk_binding_entry_add_signal (binding_set, GDK_KEY_c, GDK_CONTROL_MASK,
1851                                 "copy-clipboard", 0);
1852   gtk_binding_entry_add_signal (binding_set, GDK_KEY_v, GDK_CONTROL_MASK,
1853                                 "paste-clipboard", 0);
1854
1855   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Delete, GDK_SHIFT_MASK,
1856                                 "cut-clipboard", 0);
1857   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Insert, GDK_CONTROL_MASK,
1858                                 "copy-clipboard", 0);
1859   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Insert, GDK_SHIFT_MASK,
1860                                 "paste-clipboard", 0);
1861
1862   /* Overwrite */
1863   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Insert, 0,
1864                                 "toggle-overwrite", 0);
1865   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Insert, 0,
1866                                 "toggle-overwrite", 0);
1867
1868   /**
1869    * GtkEntry:inner-border:
1870    *
1871    * Sets the text area's border between the text and the frame.
1872    *
1873    * Since: 2.10
1874    *
1875    * Deprecated: 3.4: Use the standard border and padding CSS properties;
1876    *   the value of this style property is ignored.
1877    */
1878   gtk_widget_class_install_style_property (widget_class,
1879                                            g_param_spec_boxed ("inner-border",
1880                                                                P_("Inner Border"),
1881                                                                P_("Border between text and frame."),
1882                                                                GTK_TYPE_BORDER,
1883                                                                GTK_PARAM_READABLE |
1884                                                                G_PARAM_DEPRECATED));
1885
1886   g_type_class_add_private (gobject_class, sizeof (GtkEntryPrivate));
1887
1888   gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_ENTRY_ACCESSIBLE);
1889 }
1890
1891 static void
1892 gtk_entry_editable_init (GtkEditableInterface *iface)
1893 {
1894   iface->do_insert_text = gtk_entry_insert_text;
1895   iface->do_delete_text = gtk_entry_delete_text;
1896   iface->insert_text = gtk_entry_real_insert_text;
1897   iface->delete_text = gtk_entry_real_delete_text;
1898   iface->get_chars = gtk_entry_get_chars;
1899   iface->set_selection_bounds = gtk_entry_set_selection_bounds;
1900   iface->get_selection_bounds = gtk_entry_get_selection_bounds;
1901   iface->set_position = gtk_entry_real_set_position;
1902   iface->get_position = gtk_entry_get_position;
1903 }
1904
1905 static void
1906 gtk_entry_cell_editable_init (GtkCellEditableIface *iface)
1907 {
1908   iface->start_editing = gtk_entry_start_editing;
1909 }
1910
1911 /* for deprecated properties */
1912 static void
1913 gtk_entry_do_set_inner_border (GtkEntry *entry,
1914                                const GtkBorder *border)
1915 {
1916   if (border)
1917     g_object_set_qdata_full (G_OBJECT (entry), quark_inner_border,
1918                              gtk_border_copy (border),
1919                              (GDestroyNotify) gtk_border_free);
1920   else
1921     g_object_set_qdata (G_OBJECT (entry), quark_inner_border, NULL);
1922
1923   g_object_notify (G_OBJECT (entry), "inner-border");
1924 }
1925
1926 static const GtkBorder *
1927 gtk_entry_do_get_inner_border (GtkEntry *entry)
1928 {
1929   return g_object_get_qdata (G_OBJECT (entry), quark_inner_border);
1930 }
1931
1932 static void
1933 gtk_entry_set_property (GObject         *object,
1934                         guint            prop_id,
1935                         const GValue    *value,
1936                         GParamSpec      *pspec)
1937 {
1938   GtkEntry *entry = GTK_ENTRY (object);
1939   GtkEntryPrivate *priv = entry->priv;
1940
1941   switch (prop_id)
1942     {
1943     case PROP_BUFFER:
1944       gtk_entry_set_buffer (entry, g_value_get_object (value));
1945       break;
1946
1947     case PROP_EDITABLE:
1948       {
1949         gboolean new_value = g_value_get_boolean (value);
1950
1951         if (new_value != priv->editable)
1952           {
1953             GtkWidget *widget = GTK_WIDGET (entry);
1954
1955             if (!new_value)
1956               {
1957                 _gtk_entry_reset_im_context (entry);
1958                 if (gtk_widget_has_focus (widget))
1959                   gtk_im_context_focus_out (priv->im_context);
1960
1961                 priv->preedit_length = 0;
1962                 priv->preedit_cursor = 0;
1963               }
1964
1965             priv->editable = new_value;
1966
1967             if (new_value && gtk_widget_has_focus (widget))
1968               gtk_im_context_focus_in (priv->im_context);
1969
1970             gtk_widget_queue_draw (widget);
1971           }
1972       }
1973       break;
1974
1975     case PROP_MAX_LENGTH:
1976       gtk_entry_set_max_length (entry, g_value_get_int (value));
1977       break;
1978       
1979     case PROP_VISIBILITY:
1980       gtk_entry_set_visibility (entry, g_value_get_boolean (value));
1981       break;
1982
1983     case PROP_HAS_FRAME:
1984       gtk_entry_set_has_frame (entry, g_value_get_boolean (value));
1985       break;
1986
1987     case PROP_INNER_BORDER:
1988       gtk_entry_do_set_inner_border (entry, g_value_get_boxed (value));
1989       break;
1990
1991     case PROP_INVISIBLE_CHAR:
1992       gtk_entry_set_invisible_char (entry, g_value_get_uint (value));
1993       break;
1994
1995     case PROP_ACTIVATES_DEFAULT:
1996       gtk_entry_set_activates_default (entry, g_value_get_boolean (value));
1997       break;
1998
1999     case PROP_WIDTH_CHARS:
2000       gtk_entry_set_width_chars (entry, g_value_get_int (value));
2001       break;
2002
2003     case PROP_TEXT:
2004       gtk_entry_set_text (entry, g_value_get_string (value));
2005       break;
2006
2007     case PROP_XALIGN:
2008       gtk_entry_set_alignment (entry, g_value_get_float (value));
2009       break;
2010
2011     case PROP_TRUNCATE_MULTILINE:
2012       priv->truncate_multiline = g_value_get_boolean (value);
2013       break;
2014
2015     case PROP_SHADOW_TYPE:
2016       priv->shadow_type = g_value_get_enum (value);
2017       break;
2018
2019     case PROP_OVERWRITE_MODE:
2020       gtk_entry_set_overwrite_mode (entry, g_value_get_boolean (value));
2021       break;
2022
2023     case PROP_INVISIBLE_CHAR_SET:
2024       if (g_value_get_boolean (value))
2025         priv->invisible_char_set = TRUE;
2026       else
2027         gtk_entry_unset_invisible_char (entry);
2028       break;
2029
2030     case PROP_CAPS_LOCK_WARNING:
2031       priv->caps_lock_warning = g_value_get_boolean (value);
2032       break;
2033
2034     case PROP_PROGRESS_FRACTION:
2035       gtk_entry_set_progress_fraction (entry, g_value_get_double (value));
2036       break;
2037
2038     case PROP_PROGRESS_PULSE_STEP:
2039       gtk_entry_set_progress_pulse_step (entry, g_value_get_double (value));
2040       break;
2041
2042     case PROP_PLACEHOLDER_TEXT:
2043       gtk_entry_set_placeholder_text (entry, g_value_get_string (value));
2044       break;
2045
2046     case PROP_PIXBUF_PRIMARY:
2047       gtk_entry_set_icon_from_pixbuf (entry,
2048                                       GTK_ENTRY_ICON_PRIMARY,
2049                                       g_value_get_object (value));
2050       break;
2051
2052     case PROP_PIXBUF_SECONDARY:
2053       gtk_entry_set_icon_from_pixbuf (entry,
2054                                       GTK_ENTRY_ICON_SECONDARY,
2055                                       g_value_get_object (value));
2056       break;
2057
2058     case PROP_STOCK_PRIMARY:
2059       gtk_entry_set_icon_from_stock (entry,
2060                                      GTK_ENTRY_ICON_PRIMARY,
2061                                      g_value_get_string (value));
2062       break;
2063
2064     case PROP_STOCK_SECONDARY:
2065       gtk_entry_set_icon_from_stock (entry,
2066                                      GTK_ENTRY_ICON_SECONDARY,
2067                                      g_value_get_string (value));
2068       break;
2069
2070     case PROP_ICON_NAME_PRIMARY:
2071       gtk_entry_set_icon_from_icon_name (entry,
2072                                          GTK_ENTRY_ICON_PRIMARY,
2073                                          g_value_get_string (value));
2074       break;
2075
2076     case PROP_ICON_NAME_SECONDARY:
2077       gtk_entry_set_icon_from_icon_name (entry,
2078                                          GTK_ENTRY_ICON_SECONDARY,
2079                                          g_value_get_string (value));
2080       break;
2081
2082     case PROP_GICON_PRIMARY:
2083       gtk_entry_set_icon_from_gicon (entry,
2084                                      GTK_ENTRY_ICON_PRIMARY,
2085                                      g_value_get_object (value));
2086       break;
2087
2088     case PROP_GICON_SECONDARY:
2089       gtk_entry_set_icon_from_gicon (entry,
2090                                      GTK_ENTRY_ICON_SECONDARY,
2091                                      g_value_get_object (value));
2092       break;
2093
2094     case PROP_ACTIVATABLE_PRIMARY:
2095       gtk_entry_set_icon_activatable (entry,
2096                                       GTK_ENTRY_ICON_PRIMARY,
2097                                       g_value_get_boolean (value));
2098       break;
2099
2100     case PROP_ACTIVATABLE_SECONDARY:
2101       gtk_entry_set_icon_activatable (entry,
2102                                       GTK_ENTRY_ICON_SECONDARY,
2103                                       g_value_get_boolean (value));
2104       break;
2105
2106     case PROP_SENSITIVE_PRIMARY:
2107       gtk_entry_set_icon_sensitive (entry,
2108                                     GTK_ENTRY_ICON_PRIMARY,
2109                                     g_value_get_boolean (value));
2110       break;
2111
2112     case PROP_SENSITIVE_SECONDARY:
2113       gtk_entry_set_icon_sensitive (entry,
2114                                     GTK_ENTRY_ICON_SECONDARY,
2115                                     g_value_get_boolean (value));
2116       break;
2117
2118     case PROP_TOOLTIP_TEXT_PRIMARY:
2119       gtk_entry_set_icon_tooltip_text (entry,
2120                                        GTK_ENTRY_ICON_PRIMARY,
2121                                        g_value_get_string (value));
2122       break;
2123
2124     case PROP_TOOLTIP_TEXT_SECONDARY:
2125       gtk_entry_set_icon_tooltip_text (entry,
2126                                        GTK_ENTRY_ICON_SECONDARY,
2127                                        g_value_get_string (value));
2128       break;
2129
2130     case PROP_TOOLTIP_MARKUP_PRIMARY:
2131       gtk_entry_set_icon_tooltip_markup (entry,
2132                                          GTK_ENTRY_ICON_PRIMARY,
2133                                          g_value_get_string (value));
2134       break;
2135
2136     case PROP_TOOLTIP_MARKUP_SECONDARY:
2137       gtk_entry_set_icon_tooltip_markup (entry,
2138                                          GTK_ENTRY_ICON_SECONDARY,
2139                                          g_value_get_string (value));
2140       break;
2141
2142     case PROP_IM_MODULE:
2143       g_free (priv->im_module);
2144       priv->im_module = g_value_dup_string (value);
2145       if (GTK_IS_IM_MULTICONTEXT (priv->im_context))
2146         gtk_im_multicontext_set_context_id (GTK_IM_MULTICONTEXT (priv->im_context), priv->im_module);
2147       break;
2148
2149     case PROP_EDITING_CANCELED:
2150       priv->editing_canceled = g_value_get_boolean (value);
2151       break;
2152
2153     case PROP_COMPLETION:
2154       gtk_entry_set_completion (entry, GTK_ENTRY_COMPLETION (g_value_get_object (value)));
2155       break;
2156
2157     case PROP_SCROLL_OFFSET:
2158     case PROP_CURSOR_POSITION:
2159     default:
2160       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2161       break;
2162     }
2163 }
2164
2165 static void
2166 gtk_entry_get_property (GObject         *object,
2167                         guint            prop_id,
2168                         GValue          *value,
2169                         GParamSpec      *pspec)
2170 {
2171   GtkEntry *entry = GTK_ENTRY (object);
2172   GtkEntryPrivate *priv = entry->priv;
2173
2174   switch (prop_id)
2175     {
2176     case PROP_BUFFER:
2177       g_value_set_object (value, gtk_entry_get_buffer (entry));
2178       break;
2179
2180     case PROP_CURSOR_POSITION:
2181       g_value_set_int (value, priv->current_pos);
2182       break;
2183
2184     case PROP_SELECTION_BOUND:
2185       g_value_set_int (value, priv->selection_bound);
2186       break;
2187
2188     case PROP_EDITABLE:
2189       g_value_set_boolean (value, priv->editable);
2190       break;
2191
2192     case PROP_MAX_LENGTH:
2193       g_value_set_int (value, gtk_entry_buffer_get_max_length (get_buffer (entry)));
2194       break;
2195
2196     case PROP_VISIBILITY:
2197       g_value_set_boolean (value, priv->visible);
2198       break;
2199
2200     case PROP_HAS_FRAME:
2201       g_value_set_boolean (value, priv->has_frame);
2202       break;
2203
2204     case PROP_INNER_BORDER:
2205       g_value_set_boxed (value, gtk_entry_do_get_inner_border (entry));
2206       break;
2207
2208     case PROP_INVISIBLE_CHAR:
2209       g_value_set_uint (value, priv->invisible_char);
2210       break;
2211
2212     case PROP_ACTIVATES_DEFAULT:
2213       g_value_set_boolean (value, priv->activates_default);
2214       break;
2215
2216     case PROP_WIDTH_CHARS:
2217       g_value_set_int (value, priv->width_chars);
2218       break;
2219
2220     case PROP_SCROLL_OFFSET:
2221       g_value_set_int (value, priv->scroll_offset);
2222       break;
2223
2224     case PROP_TEXT:
2225       g_value_set_string (value, gtk_entry_get_text (entry));
2226       break;
2227
2228     case PROP_XALIGN:
2229       g_value_set_float (value, gtk_entry_get_alignment (entry));
2230       break;
2231
2232     case PROP_TRUNCATE_MULTILINE:
2233       g_value_set_boolean (value, priv->truncate_multiline);
2234       break;
2235
2236     case PROP_SHADOW_TYPE:
2237       g_value_set_enum (value, priv->shadow_type);
2238       break;
2239
2240     case PROP_OVERWRITE_MODE:
2241       g_value_set_boolean (value, priv->overwrite_mode);
2242       break;
2243
2244     case PROP_TEXT_LENGTH:
2245       g_value_set_uint (value, gtk_entry_buffer_get_length (get_buffer (entry)));
2246       break;
2247
2248     case PROP_INVISIBLE_CHAR_SET:
2249       g_value_set_boolean (value, priv->invisible_char_set);
2250       break;
2251
2252     case PROP_IM_MODULE:
2253       g_value_set_string (value, priv->im_module);
2254       break;
2255
2256     case PROP_CAPS_LOCK_WARNING:
2257       g_value_set_boolean (value, priv->caps_lock_warning);
2258       break;
2259
2260     case PROP_PROGRESS_FRACTION:
2261       g_value_set_double (value, priv->progress_fraction);
2262       break;
2263
2264     case PROP_PROGRESS_PULSE_STEP:
2265       g_value_set_double (value, priv->progress_pulse_fraction);
2266       break;
2267
2268     case PROP_PLACEHOLDER_TEXT:
2269       g_value_set_string (value, gtk_entry_get_placeholder_text (entry));
2270       break;
2271
2272     case PROP_PIXBUF_PRIMARY:
2273       g_value_set_object (value,
2274                           gtk_entry_get_icon_pixbuf (entry,
2275                                                      GTK_ENTRY_ICON_PRIMARY));
2276       break;
2277
2278     case PROP_PIXBUF_SECONDARY:
2279       g_value_set_object (value,
2280                           gtk_entry_get_icon_pixbuf (entry,
2281                                                      GTK_ENTRY_ICON_SECONDARY));
2282       break;
2283
2284     case PROP_STOCK_PRIMARY:
2285       g_value_set_string (value,
2286                           gtk_entry_get_icon_stock (entry,
2287                                                     GTK_ENTRY_ICON_PRIMARY));
2288       break;
2289
2290     case PROP_STOCK_SECONDARY:
2291       g_value_set_string (value,
2292                           gtk_entry_get_icon_stock (entry,
2293                                                     GTK_ENTRY_ICON_SECONDARY));
2294       break;
2295
2296     case PROP_ICON_NAME_PRIMARY:
2297       g_value_set_string (value,
2298                           gtk_entry_get_icon_name (entry,
2299                                                    GTK_ENTRY_ICON_PRIMARY));
2300       break;
2301
2302     case PROP_ICON_NAME_SECONDARY:
2303       g_value_set_string (value,
2304                           gtk_entry_get_icon_name (entry,
2305                                                    GTK_ENTRY_ICON_SECONDARY));
2306       break;
2307
2308     case PROP_GICON_PRIMARY:
2309       g_value_set_object (value,
2310                           gtk_entry_get_icon_gicon (entry,
2311                                                     GTK_ENTRY_ICON_PRIMARY));
2312       break;
2313
2314     case PROP_GICON_SECONDARY:
2315       g_value_set_object (value,
2316                           gtk_entry_get_icon_gicon (entry,
2317                                                     GTK_ENTRY_ICON_SECONDARY));
2318       break;
2319
2320     case PROP_STORAGE_TYPE_PRIMARY:
2321       g_value_set_enum (value,
2322                         gtk_entry_get_icon_storage_type (entry, 
2323                                                          GTK_ENTRY_ICON_PRIMARY));
2324       break;
2325
2326     case PROP_STORAGE_TYPE_SECONDARY:
2327       g_value_set_enum (value,
2328                         gtk_entry_get_icon_storage_type (entry, 
2329                                                          GTK_ENTRY_ICON_SECONDARY));
2330       break;
2331
2332     case PROP_ACTIVATABLE_PRIMARY:
2333       g_value_set_boolean (value,
2334                            gtk_entry_get_icon_activatable (entry, GTK_ENTRY_ICON_PRIMARY));
2335       break;
2336
2337     case PROP_ACTIVATABLE_SECONDARY:
2338       g_value_set_boolean (value,
2339                            gtk_entry_get_icon_activatable (entry, GTK_ENTRY_ICON_SECONDARY));
2340       break;
2341
2342     case PROP_SENSITIVE_PRIMARY:
2343       g_value_set_boolean (value,
2344                            gtk_entry_get_icon_sensitive (entry, GTK_ENTRY_ICON_PRIMARY));
2345       break;
2346
2347     case PROP_SENSITIVE_SECONDARY:
2348       g_value_set_boolean (value,
2349                            gtk_entry_get_icon_sensitive (entry, GTK_ENTRY_ICON_SECONDARY));
2350       break;
2351
2352     case PROP_TOOLTIP_TEXT_PRIMARY:
2353       g_value_take_string (value,
2354                            gtk_entry_get_icon_tooltip_text (entry, GTK_ENTRY_ICON_PRIMARY));
2355       break;
2356
2357     case PROP_TOOLTIP_TEXT_SECONDARY:
2358       g_value_take_string (value,
2359                            gtk_entry_get_icon_tooltip_text (entry, GTK_ENTRY_ICON_SECONDARY));
2360       break;
2361
2362     case PROP_TOOLTIP_MARKUP_PRIMARY:
2363       g_value_take_string (value,
2364                            gtk_entry_get_icon_tooltip_markup (entry, GTK_ENTRY_ICON_PRIMARY));
2365       break;
2366
2367     case PROP_TOOLTIP_MARKUP_SECONDARY:
2368       g_value_take_string (value,
2369                            gtk_entry_get_icon_tooltip_markup (entry, GTK_ENTRY_ICON_SECONDARY));
2370       break;
2371
2372     case PROP_EDITING_CANCELED:
2373       g_value_set_boolean (value,
2374                            priv->editing_canceled);
2375       break;
2376
2377     case PROP_COMPLETION:
2378       g_value_set_object (value, G_OBJECT (gtk_entry_get_completion (entry)));
2379       break;
2380
2381     default:
2382       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2383       break;
2384     }
2385 }
2386
2387 static gunichar
2388 find_invisible_char (GtkWidget *widget)
2389 {
2390   PangoLayout *layout;
2391   PangoAttrList *attr_list;
2392   gint i;
2393   gunichar invisible_chars [] = {
2394     0,
2395     0x25cf, /* BLACK CIRCLE */
2396     0x2022, /* BULLET */
2397     0x2731, /* HEAVY ASTERISK */
2398     0x273a  /* SIXTEEN POINTED ASTERISK */
2399   };
2400
2401   gtk_widget_style_get (widget,
2402                         "invisible-char", &invisible_chars[0],
2403                         NULL);
2404
2405   layout = gtk_widget_create_pango_layout (widget, NULL);
2406
2407   attr_list = pango_attr_list_new ();
2408   pango_attr_list_insert (attr_list, pango_attr_fallback_new (FALSE));
2409
2410   pango_layout_set_attributes (layout, attr_list);
2411   pango_attr_list_unref (attr_list);
2412
2413   for (i = (invisible_chars[0] != 0 ? 0 : 1); i < G_N_ELEMENTS (invisible_chars); i++)
2414     {
2415       gchar text[7] = { 0, };
2416       gint len, count;
2417
2418       len = g_unichar_to_utf8 (invisible_chars[i], text);
2419       pango_layout_set_text (layout, text, len);
2420
2421       count = pango_layout_get_unknown_glyphs_count (layout);
2422
2423       if (count == 0)
2424         {
2425           g_object_unref (layout);
2426           return invisible_chars[i];
2427         }
2428     }
2429
2430   g_object_unref (layout);
2431
2432   return '*';
2433 }
2434
2435 static void
2436 gtk_entry_init (GtkEntry *entry)
2437 {
2438   GtkStyleContext *context;
2439   GtkEntryPrivate *priv;
2440
2441   entry->priv = G_TYPE_INSTANCE_GET_PRIVATE (entry,
2442                                              GTK_TYPE_ENTRY,
2443                                              GtkEntryPrivate);
2444   priv = entry->priv;
2445
2446   gtk_widget_set_can_focus (GTK_WIDGET (entry), TRUE);
2447   gtk_widget_set_has_window (GTK_WIDGET (entry), FALSE);
2448
2449   priv->editable = TRUE;
2450   priv->visible = TRUE;
2451   priv->dnd_position = -1;
2452   priv->width_chars = -1;
2453   priv->is_cell_renderer = FALSE;
2454   priv->editing_canceled = FALSE;
2455   priv->has_frame = TRUE;
2456   priv->truncate_multiline = FALSE;
2457   priv->shadow_type = GTK_SHADOW_IN;
2458   priv->xalign = 0.0;
2459   priv->caps_lock_warning = TRUE;
2460   priv->caps_lock_warning_shown = FALSE;
2461   priv->progress_fraction = 0.0;
2462   priv->progress_pulse_fraction = 0.1;
2463
2464   gtk_drag_dest_set (GTK_WIDGET (entry),
2465                      GTK_DEST_DEFAULT_HIGHLIGHT,
2466                      NULL, 0,
2467                      GDK_ACTION_COPY | GDK_ACTION_MOVE);
2468   gtk_drag_dest_add_text_targets (GTK_WIDGET (entry));
2469
2470   /* This object is completely private. No external entity can gain a reference
2471    * to it; so we create it here and destroy it in finalize().
2472    */
2473   priv->im_context = gtk_im_multicontext_new ();
2474
2475   g_signal_connect (priv->im_context, "commit",
2476                     G_CALLBACK (gtk_entry_commit_cb), entry);
2477   g_signal_connect (priv->im_context, "preedit-changed",
2478                     G_CALLBACK (gtk_entry_preedit_changed_cb), entry);
2479   g_signal_connect (priv->im_context, "retrieve-surrounding",
2480                     G_CALLBACK (gtk_entry_retrieve_surrounding_cb), entry);
2481   g_signal_connect (priv->im_context, "delete-surrounding",
2482                     G_CALLBACK (gtk_entry_delete_surrounding_cb), entry);
2483
2484   context = gtk_widget_get_style_context (GTK_WIDGET (entry));
2485   gtk_style_context_add_class (context, GTK_STYLE_CLASS_ENTRY);
2486
2487   gtk_entry_update_cached_style_values (entry);
2488 }
2489
2490 static void
2491 gtk_entry_prepare_context_for_icon (GtkEntry             *entry,
2492                                     GtkStyleContext      *context,
2493                                     GtkEntryIconPosition  icon_pos)
2494 {
2495   GtkEntryPrivate *priv = entry->priv;
2496   EntryIconInfo *icon_info = priv->icons[icon_pos];
2497   GtkWidget *widget;
2498   GtkStateFlags state;
2499
2500   widget = GTK_WIDGET (entry);
2501   state = gtk_widget_get_state_flags (widget);
2502
2503   state &= ~(GTK_STATE_FLAG_PRELIGHT);
2504
2505   if ((state & GTK_STATE_FLAG_INSENSITIVE) || icon_info->insensitive)
2506     state |= GTK_STATE_FLAG_INSENSITIVE;
2507   else if (icon_info->prelight)
2508     state |= GTK_STATE_FLAG_PRELIGHT;
2509
2510   gtk_style_context_save (context);
2511
2512   gtk_style_context_set_state (context, state);
2513   gtk_style_context_add_class (context, GTK_STYLE_CLASS_IMAGE);
2514
2515   if (gtk_widget_get_direction (GTK_WIDGET (entry)) == GTK_TEXT_DIR_RTL) 
2516     {
2517       if (icon_pos == GTK_ENTRY_ICON_PRIMARY)
2518         gtk_style_context_add_class (context, GTK_STYLE_CLASS_RIGHT);
2519       else
2520         gtk_style_context_add_class (context, GTK_STYLE_CLASS_LEFT);
2521     }
2522   else
2523     {
2524       if (icon_pos == GTK_ENTRY_ICON_PRIMARY)
2525         gtk_style_context_add_class (context, GTK_STYLE_CLASS_LEFT);
2526       else
2527         gtk_style_context_add_class (context, GTK_STYLE_CLASS_RIGHT);
2528     }
2529 }
2530
2531 static gint
2532 get_icon_width (GtkEntry             *entry,
2533                 GtkEntryIconPosition  icon_pos)
2534 {
2535   GtkEntryPrivate *priv = entry->priv;
2536   EntryIconInfo *icon_info = priv->icons[icon_pos];
2537   GtkStyleContext *context;
2538   GtkBorder padding;
2539   gint width;
2540
2541   if (!icon_info)
2542     return 0;
2543
2544   context = gtk_widget_get_style_context (GTK_WIDGET (entry));
2545   gtk_entry_prepare_context_for_icon (entry, context, icon_pos);
2546   gtk_style_context_get_padding (context, 0, &padding);
2547
2548   _gtk_icon_helper_get_size (icon_info->icon_helper, context,
2549                              &width, NULL);
2550   gtk_style_context_restore (context);
2551
2552   if (width > 0)
2553     width += padding.left + padding.right;
2554
2555   return width;
2556 }
2557
2558 static void
2559 get_icon_allocations (GtkEntry      *entry,
2560                       GtkAllocation *primary,
2561                       GtkAllocation *secondary)
2562
2563 {
2564   GtkEntryPrivate *priv = entry->priv;
2565   gint x, y, width, height;
2566
2567   get_text_area_size (entry, &x, &y, &width, &height);
2568
2569   if (gtk_widget_has_focus (GTK_WIDGET (entry)) && !priv->interior_focus)
2570     y += priv->focus_width;
2571
2572   primary->y = y;
2573   primary->height = height;
2574   primary->width = get_icon_width (entry, GTK_ENTRY_ICON_PRIMARY);
2575
2576   secondary->y = y;
2577   secondary->height = height;
2578   secondary->width = get_icon_width (entry, GTK_ENTRY_ICON_SECONDARY);
2579
2580   if (gtk_widget_get_direction (GTK_WIDGET (entry)) == GTK_TEXT_DIR_RTL)
2581     {
2582       primary->x = x + width - primary->width;
2583       secondary->x = x;
2584     }
2585   else
2586     {
2587       primary->x = x;
2588       secondary->x = x + width - secondary->width;
2589     }
2590 }
2591
2592
2593 static void
2594 begin_change (GtkEntry *entry)
2595 {
2596   GtkEntryPrivate *priv = entry->priv;
2597
2598   priv->change_count++;
2599
2600   g_object_freeze_notify (G_OBJECT (entry));
2601 }
2602
2603 static void
2604 end_change (GtkEntry *entry)
2605 {
2606   GtkEditable *editable = GTK_EDITABLE (entry);
2607   GtkEntryPrivate *priv = entry->priv;
2608
2609   g_return_if_fail (priv->change_count > 0);
2610
2611   g_object_thaw_notify (G_OBJECT (entry));
2612
2613   priv->change_count--;
2614
2615   if (priv->change_count == 0)
2616     {
2617        if (priv->real_changed)
2618          {
2619            g_signal_emit_by_name (editable, "changed");
2620            priv->real_changed = FALSE;
2621          }
2622     }
2623 }
2624
2625 static void
2626 emit_changed (GtkEntry *entry)
2627 {
2628   GtkEditable *editable = GTK_EDITABLE (entry);
2629   GtkEntryPrivate *priv = entry->priv;
2630
2631   if (priv->change_count == 0)
2632     g_signal_emit_by_name (editable, "changed");
2633   else
2634     priv->real_changed = TRUE;
2635 }
2636
2637 static void
2638 gtk_entry_destroy (GtkWidget *widget)
2639 {
2640   GtkEntry *entry = GTK_ENTRY (widget);
2641   GtkEntryPrivate *priv = entry->priv;
2642
2643   priv->current_pos = priv->selection_bound = 0;
2644   _gtk_entry_reset_im_context (entry);
2645   gtk_entry_reset_layout (entry);
2646
2647   if (priv->blink_timeout)
2648     {
2649       g_source_remove (priv->blink_timeout);
2650       priv->blink_timeout = 0;
2651     }
2652
2653   if (priv->recompute_idle)
2654     {
2655       g_source_remove (priv->recompute_idle);
2656       priv->recompute_idle = 0;
2657     }
2658
2659   GTK_WIDGET_CLASS (gtk_entry_parent_class)->destroy (widget);
2660 }
2661
2662 static void
2663 gtk_entry_dispose (GObject *object)
2664 {
2665   GtkEntry *entry = GTK_ENTRY (object);
2666   GtkEntryPrivate *priv = entry->priv;
2667   GdkKeymap *keymap;
2668
2669   gtk_entry_set_icon_from_pixbuf (entry, GTK_ENTRY_ICON_PRIMARY, NULL);
2670   gtk_entry_set_icon_tooltip_markup (entry, GTK_ENTRY_ICON_PRIMARY, NULL);
2671   gtk_entry_set_icon_from_pixbuf (entry, GTK_ENTRY_ICON_SECONDARY, NULL);
2672   gtk_entry_set_icon_tooltip_markup (entry, GTK_ENTRY_ICON_SECONDARY, NULL);
2673
2674   if (priv->buffer)
2675     {
2676       buffer_disconnect_signals (entry);
2677       g_object_unref (priv->buffer);
2678       priv->buffer = NULL;
2679     }
2680
2681   keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (object)));
2682   g_signal_handlers_disconnect_by_func (keymap, keymap_state_changed, entry);
2683   g_signal_handlers_disconnect_by_func (keymap, keymap_direction_changed, entry);
2684   G_OBJECT_CLASS (gtk_entry_parent_class)->dispose (object);
2685 }
2686
2687 static void
2688 gtk_entry_finalize (GObject *object)
2689 {
2690   GtkEntry *entry = GTK_ENTRY (object);
2691   GtkEntryPrivate *priv = entry->priv;
2692   EntryIconInfo *icon_info = NULL;
2693   gint i;
2694
2695   for (i = 0; i < MAX_ICONS; i++)
2696     {
2697       if ((icon_info = priv->icons[i]) != NULL)
2698         {
2699           if (icon_info->target_list != NULL)
2700             {
2701               gtk_target_list_unref (icon_info->target_list);
2702               icon_info->target_list = NULL;
2703             }
2704
2705           g_clear_object (&icon_info->icon_helper);
2706
2707           g_slice_free (EntryIconInfo, icon_info);
2708           priv->icons[i] = NULL;
2709         }
2710     }
2711
2712   gtk_entry_set_completion (entry, NULL);
2713
2714   if (priv->cached_layout)
2715     g_object_unref (priv->cached_layout);
2716
2717   g_object_unref (priv->im_context);
2718
2719   if (priv->blink_timeout)
2720     g_source_remove (priv->blink_timeout);
2721
2722   if (priv->recompute_idle)
2723     g_source_remove (priv->recompute_idle);
2724
2725   g_free (priv->placeholder_text);
2726   g_free (priv->im_module);
2727
2728   G_OBJECT_CLASS (gtk_entry_parent_class)->finalize (object);
2729 }
2730
2731 static DisplayMode
2732 gtk_entry_get_display_mode (GtkEntry *entry)
2733 {
2734   GtkEntryPrivate *priv = entry->priv;
2735
2736   if (priv->visible)
2737     return DISPLAY_NORMAL;
2738
2739   if (priv->invisible_char == 0 && priv->invisible_char_set)
2740     return DISPLAY_BLANK;
2741
2742   return DISPLAY_INVISIBLE;
2743 }
2744
2745 static gchar*
2746 gtk_entry_get_display_text (GtkEntry *entry,
2747                             gint      start_pos,
2748                             gint      end_pos)
2749 {
2750   GtkEntryPasswordHint *password_hint;
2751   GtkEntryPrivate *priv;
2752   gunichar invisible_char;
2753   const gchar *start;
2754   const gchar *end;
2755   const gchar *text;
2756   gchar char_str[7];
2757   gint char_len;
2758   GString *str;
2759   guint length;
2760   gint i;
2761
2762   priv = entry->priv;
2763   text = gtk_entry_buffer_get_text (get_buffer (entry));
2764   length = gtk_entry_buffer_get_length (get_buffer (entry));
2765
2766   if (end_pos < 0)
2767     end_pos = length;
2768   if (start_pos > length)
2769     start_pos = length;
2770
2771   if (end_pos <= start_pos)
2772       return g_strdup ("");
2773   else if (priv->visible)
2774     {
2775       start = g_utf8_offset_to_pointer (text, start_pos);
2776       end = g_utf8_offset_to_pointer (start, end_pos - start_pos);
2777       return g_strndup (start, end - start);
2778     }
2779   else
2780     {
2781       str = g_string_sized_new (length * 2);
2782
2783       /* Figure out what our invisible char is and encode it */
2784       if (!priv->invisible_char)
2785           invisible_char = priv->invisible_char_set ? ' ' : '*';
2786       else
2787           invisible_char = priv->invisible_char;
2788       char_len = g_unichar_to_utf8 (invisible_char, char_str);
2789
2790       /*
2791        * Add hidden characters for each character in the text
2792        * buffer. If there is a password hint, then keep that
2793        * character visible.
2794        */
2795
2796       password_hint = g_object_get_qdata (G_OBJECT (entry), quark_password_hint);
2797       for (i = start_pos; i < end_pos; ++i)
2798         {
2799           if (password_hint && i == password_hint->position)
2800             {
2801               start = g_utf8_offset_to_pointer (text, i);
2802               g_string_append_len (str, start, g_utf8_next_char (start) - start);
2803             }
2804           else
2805             {
2806               g_string_append_len (str, char_str, char_len);
2807             }
2808         }
2809
2810       return g_string_free (str, FALSE);
2811     }
2812 }
2813
2814 static void
2815 update_cursors (GtkWidget *widget)
2816 {
2817   GtkEntry *entry = GTK_ENTRY (widget);
2818   GtkEntryPrivate *priv = entry->priv;
2819   EntryIconInfo *icon_info = NULL;
2820   GdkDisplay *display;
2821   GdkCursor *cursor;
2822   gint i;
2823
2824   for (i = 0; i < MAX_ICONS; i++)
2825     {
2826       if ((icon_info = priv->icons[i]) != NULL)
2827         {
2828           if (!_gtk_icon_helper_get_is_empty (icon_info->icon_helper) && 
2829               icon_info->window != NULL)
2830             gdk_window_show_unraised (icon_info->window);
2831
2832           /* The icon windows are not children of the visible entry window,
2833            * thus we can't just inherit the xterm cursor. Slight complication 
2834            * here is that for the entry, insensitive => arrow cursor, but for 
2835            * an icon in a sensitive entry, insensitive => xterm cursor.
2836            */
2837           if (gtk_widget_is_sensitive (widget) &&
2838               (icon_info->insensitive || 
2839                (icon_info->nonactivatable && icon_info->target_list == NULL)))
2840             {
2841               display = gtk_widget_get_display (widget);
2842               cursor = gdk_cursor_new_for_display (display, GDK_XTERM);
2843               gdk_window_set_cursor (icon_info->window, cursor);
2844               g_object_unref (cursor);
2845             }
2846           else
2847             {
2848               gdk_window_set_cursor (icon_info->window, NULL);
2849             }
2850         }
2851     }
2852 }
2853
2854 static void
2855 realize_icon_info (GtkWidget            *widget, 
2856                    GtkEntryIconPosition  icon_pos)
2857 {
2858   GtkEntry *entry = GTK_ENTRY (widget);
2859   GtkEntryPrivate *priv = entry->priv;
2860   EntryIconInfo *icon_info = priv->icons[icon_pos];
2861   GdkWindowAttr attributes;
2862   gint attributes_mask;
2863
2864   g_return_if_fail (icon_info != NULL);
2865
2866   attributes.x = 0;
2867   attributes.y = 0;
2868   attributes.width = 1;
2869   attributes.height = 1;
2870   attributes.window_type = GDK_WINDOW_CHILD;
2871   attributes.wclass = GDK_INPUT_ONLY;
2872   attributes.event_mask = gtk_widget_get_events (widget);
2873   attributes.event_mask |= (GDK_BUTTON_PRESS_MASK |
2874                                 GDK_BUTTON_RELEASE_MASK |
2875                                 GDK_BUTTON1_MOTION_MASK |
2876                                 GDK_BUTTON3_MOTION_MASK |
2877                                 GDK_POINTER_MOTION_HINT_MASK |
2878                                 GDK_POINTER_MOTION_MASK |
2879                                 GDK_ENTER_NOTIFY_MASK |
2880                             GDK_LEAVE_NOTIFY_MASK);
2881   attributes_mask = GDK_WA_X | GDK_WA_Y;
2882
2883   icon_info->window = gdk_window_new (gtk_widget_get_window (widget),
2884                                       &attributes,
2885                                       attributes_mask);
2886   gdk_window_set_user_data (icon_info->window, widget);
2887
2888   gtk_widget_queue_resize (widget);
2889 }
2890
2891 static EntryIconInfo*
2892 construct_icon_info (GtkWidget            *widget, 
2893                      GtkEntryIconPosition  icon_pos)
2894 {
2895   GtkEntry *entry = GTK_ENTRY (widget);
2896   GtkEntryPrivate *priv = entry->priv;
2897   EntryIconInfo *icon_info;
2898
2899   g_return_val_if_fail (priv->icons[icon_pos] == NULL, NULL);
2900
2901   icon_info = g_slice_new0 (EntryIconInfo);
2902   priv->icons[icon_pos] = icon_info;
2903
2904   icon_info->icon_helper = _gtk_icon_helper_new ();
2905
2906   if (gtk_widget_get_realized (widget))
2907     realize_icon_info (widget, icon_pos);
2908
2909   return icon_info;
2910 }
2911
2912 static void
2913 gtk_entry_map (GtkWidget *widget)
2914 {
2915   GtkEntry *entry = GTK_ENTRY (widget);
2916   GtkEntryPrivate *priv = entry->priv;
2917   EntryIconInfo *icon_info = NULL;
2918   gint i;
2919
2920   GTK_WIDGET_CLASS (gtk_entry_parent_class)->map (widget);
2921
2922   gdk_window_show (priv->text_area);
2923
2924   for (i = 0; i < MAX_ICONS; i++)
2925     {
2926       if ((icon_info = priv->icons[i]) != NULL)
2927         {
2928           if (!_gtk_icon_helper_get_is_empty (icon_info->icon_helper) &&
2929               icon_info->window != NULL)
2930             gdk_window_show (icon_info->window);
2931         }
2932     }
2933
2934   update_cursors (widget);
2935 }
2936
2937 static void
2938 gtk_entry_unmap (GtkWidget *widget)
2939 {
2940   GtkEntry *entry = GTK_ENTRY (widget);
2941   GtkEntryPrivate *priv = entry->priv;
2942   EntryIconInfo *icon_info = NULL;
2943   gint i;
2944
2945   for (i = 0; i < MAX_ICONS; i++)
2946     {
2947       if ((icon_info = priv->icons[i]) != NULL)
2948         {
2949           if (!_gtk_icon_helper_get_is_empty (icon_info->icon_helper) && 
2950               icon_info->window != NULL)
2951             gdk_window_hide (icon_info->window);
2952         }
2953     }
2954
2955   gdk_window_hide (priv->text_area);
2956
2957   GTK_WIDGET_CLASS (gtk_entry_parent_class)->unmap (widget);
2958 }
2959
2960 static void
2961 gtk_entry_realize (GtkWidget *widget)
2962 {
2963   GtkEntry *entry;
2964   GtkEntryPrivate *priv;
2965   EntryIconInfo *icon_info;
2966   GdkWindow *window;
2967   GdkWindowAttr attributes;
2968   gint attributes_mask;
2969   gint frame_x, frame_y;
2970   int i;
2971
2972   gtk_widget_set_realized (widget, TRUE);
2973   window = gtk_widget_get_parent_window (widget);
2974   gtk_widget_set_window (widget, window);
2975   g_object_ref (window);
2976
2977   entry = GTK_ENTRY (widget);
2978   priv = entry->priv;
2979
2980   attributes.window_type = GDK_WINDOW_CHILD;
2981   attributes.wclass = GDK_INPUT_ONLY;
2982   attributes.event_mask = gtk_widget_get_events (widget);
2983   attributes.event_mask |= (GDK_BUTTON_PRESS_MASK |
2984                             GDK_BUTTON_RELEASE_MASK |
2985                             GDK_BUTTON1_MOTION_MASK |
2986                             GDK_BUTTON3_MOTION_MASK |
2987                             GDK_POINTER_MOTION_HINT_MASK |
2988                             GDK_POINTER_MOTION_MASK |
2989                             GDK_ENTER_NOTIFY_MASK |
2990                             GDK_LEAVE_NOTIFY_MASK);
2991   attributes_mask = GDK_WA_X | GDK_WA_Y;
2992
2993   get_text_area_size (entry, &attributes.x, &attributes.y, &attributes.width, &attributes.height);
2994
2995   get_frame_size (entry, TRUE, &frame_x, &frame_y, NULL, NULL);
2996   attributes.x += frame_x;
2997   attributes.y += frame_y;
2998
2999   if (gtk_widget_is_sensitive (widget))
3000     {
3001       attributes.cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget), GDK_XTERM);
3002       attributes_mask |= GDK_WA_CURSOR;
3003     }
3004
3005   priv->text_area = gdk_window_new (gtk_widget_get_window (widget),
3006                                     &attributes,
3007                                     attributes_mask);
3008
3009   gdk_window_set_user_data (priv->text_area, entry);
3010
3011   if (attributes_mask & GDK_WA_CURSOR)
3012     g_object_unref (attributes.cursor);
3013
3014   gtk_im_context_set_client_window (priv->im_context, priv->text_area);
3015
3016   gtk_entry_adjust_scroll (entry);
3017   gtk_entry_update_primary_selection (entry);
3018
3019
3020   /* If the icon positions are already setup, create their windows.
3021    * Otherwise if they don't exist yet, then construct_icon_info()
3022    * will create the windows once the widget is already realized.
3023    */
3024   for (i = 0; i < MAX_ICONS; i++)
3025     {
3026       if ((icon_info = priv->icons[i]) != NULL)
3027         {
3028           if (icon_info->window == NULL)
3029             realize_icon_info (widget, i);
3030         }
3031     }
3032 }
3033
3034 static void
3035 gtk_entry_unrealize (GtkWidget *widget)
3036 {
3037   GtkEntry *entry = GTK_ENTRY (widget);
3038   GtkEntryPrivate *priv = entry->priv;
3039   GtkClipboard *clipboard;
3040   EntryIconInfo *icon_info;
3041   gint i;
3042
3043   gtk_entry_reset_layout (entry);
3044   
3045   gtk_im_context_set_client_window (priv->im_context, NULL);
3046
3047   clipboard = gtk_widget_get_clipboard (widget, GDK_SELECTION_PRIMARY);
3048   if (gtk_clipboard_get_owner (clipboard) == G_OBJECT (entry))
3049     gtk_clipboard_clear (clipboard);
3050   
3051   if (priv->text_area)
3052     {
3053       gdk_window_set_user_data (priv->text_area, NULL);
3054       gdk_window_destroy (priv->text_area);
3055       priv->text_area = NULL;
3056     }
3057
3058   if (priv->popup_menu)
3059     {
3060       gtk_widget_destroy (priv->popup_menu);
3061       priv->popup_menu = NULL;
3062     }
3063
3064   GTK_WIDGET_CLASS (gtk_entry_parent_class)->unrealize (widget);
3065
3066   for (i = 0; i < MAX_ICONS; i++)
3067     {
3068       if ((icon_info = priv->icons[i]) != NULL)
3069         {
3070           if (icon_info->window != NULL)
3071             {
3072               gdk_window_destroy (icon_info->window);
3073               icon_info->window = NULL;
3074             }
3075         }
3076     }
3077 }
3078
3079 void
3080 _gtk_entry_get_borders (GtkEntry *entry,
3081                         GtkBorder *border_out)
3082 {
3083   GtkEntryPrivate *priv = entry->priv;
3084   GtkWidget *widget = GTK_WIDGET (entry);
3085   GtkBorder tmp = { 0, 0, 0, 0 };
3086   GtkStyleContext *context;
3087
3088   context = gtk_widget_get_style_context (widget);
3089   gtk_style_context_get_padding (context, 0, &tmp);
3090
3091   if (priv->has_frame)
3092     {
3093       GtkBorder border;
3094
3095       gtk_style_context_get_border (context, 0, &border);
3096       tmp.top += border.top;
3097       tmp.right += border.right;
3098       tmp.bottom += border.bottom;
3099       tmp.left += border.left;
3100     }
3101
3102   if (!priv->interior_focus)
3103     {
3104       tmp.top += priv->focus_width;
3105       tmp.right += priv->focus_width;
3106       tmp.bottom += priv->focus_width;
3107       tmp.left += priv->focus_width;
3108     }
3109
3110   if (border_out != NULL)
3111     *border_out = tmp;
3112 }
3113
3114 static void
3115 gtk_entry_get_preferred_width (GtkWidget *widget,
3116                                gint      *minimum,
3117                                gint      *natural)
3118 {
3119   GtkEntry *entry = GTK_ENTRY (widget);
3120   GtkEntryPrivate *priv = entry->priv;
3121   PangoFontMetrics *metrics;
3122   GtkBorder borders;
3123   PangoContext *context;
3124   GtkStyleContext *style_context;
3125   GtkStateFlags state;
3126   gint icon_widths = 0;
3127   gint icon_width, i;
3128   gint width;
3129
3130   context = gtk_widget_get_pango_context (widget);
3131
3132   style_context = gtk_widget_get_style_context (widget);
3133   state = gtk_widget_get_state_flags (widget);
3134
3135   metrics = pango_context_get_metrics (context,
3136                                        gtk_style_context_get_font (style_context, state),
3137                                        pango_context_get_language (context));
3138
3139   _gtk_entry_get_borders (entry, &borders);
3140
3141   if (priv->width_chars < 0)
3142     width = MIN_ENTRY_WIDTH + borders.left + borders.right;
3143   else
3144     {
3145       gint char_width = pango_font_metrics_get_approximate_char_width (metrics);
3146       gint digit_width = pango_font_metrics_get_approximate_digit_width (metrics);
3147       gint char_pixels = (MAX (char_width, digit_width) + PANGO_SCALE - 1) / PANGO_SCALE;
3148
3149       width = char_pixels * priv->width_chars + borders.left + borders.right;
3150     }
3151
3152   for (i = 0; i < MAX_ICONS; i++)
3153     {
3154       icon_width = get_icon_width (entry, i);
3155       if (icon_width > 0)
3156         icon_widths += icon_width;
3157     }
3158
3159   if (icon_widths > width)
3160     width += icon_widths;
3161
3162   pango_font_metrics_unref (metrics);
3163
3164   *minimum = width;
3165   *natural = width;
3166 }
3167
3168 static void
3169 gtk_entry_get_preferred_height (GtkWidget *widget,
3170                                 gint      *minimum,
3171                                 gint      *natural)
3172 {
3173   GtkEntry *entry = GTK_ENTRY (widget);
3174   GtkEntryPrivate *priv = entry->priv;
3175   PangoFontMetrics *metrics;
3176   GtkBorder borders;
3177   GtkStyleContext *style_context;
3178   GtkStateFlags state;
3179   PangoContext *context;
3180   gint height;
3181
3182   context = gtk_widget_get_pango_context (widget);
3183
3184   style_context = gtk_widget_get_style_context (widget);
3185   state = gtk_widget_get_state_flags (widget);
3186
3187   metrics = pango_context_get_metrics (context,
3188                                        gtk_style_context_get_font (style_context, state),
3189                                        pango_context_get_language (context));
3190
3191   priv->ascent = pango_font_metrics_get_ascent (metrics);
3192   priv->descent = pango_font_metrics_get_descent (metrics);
3193
3194   _gtk_entry_get_borders (entry, &borders);
3195
3196   height = PANGO_PIXELS (priv->ascent + priv->descent) + borders.top + borders.bottom;
3197
3198   pango_font_metrics_unref (metrics);
3199
3200   *minimum = height;
3201   *natural = height;
3202 }
3203
3204 static void
3205 place_windows (GtkEntry *entry)
3206 {
3207   GtkWidget *widget = GTK_WIDGET (entry);
3208   GtkEntryPrivate *priv = entry->priv;
3209   gint x, y, width, height;
3210   gint frame_x, frame_y;
3211   GtkAllocation primary;
3212   GtkAllocation secondary;
3213   EntryIconInfo *icon_info = NULL;
3214
3215   get_frame_size (entry, TRUE, &frame_x, &frame_y, NULL, NULL);
3216   get_text_area_size (entry, &x, &y, &width, &height);
3217   get_icon_allocations (entry, &primary, &secondary);
3218
3219   if (gtk_widget_has_focus (widget) && !priv->interior_focus)
3220     y += priv->focus_width;
3221
3222   if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
3223     x += secondary.width;
3224   else
3225     x += primary.width;
3226   width -= primary.width + secondary.width;
3227
3228   x += frame_x;
3229   y += frame_y;
3230   primary.x += frame_x;
3231   primary.y += frame_y;
3232   secondary.x += frame_x;
3233   secondary.y += frame_y;
3234
3235   if ((icon_info = priv->icons[GTK_ENTRY_ICON_PRIMARY]) != NULL)
3236     gdk_window_move_resize (icon_info->window,
3237                             primary.x, primary.y,
3238                             primary.width, primary.height);
3239
3240   if ((icon_info = priv->icons[GTK_ENTRY_ICON_SECONDARY]) != NULL)
3241     gdk_window_move_resize (icon_info->window,
3242                             secondary.x, secondary.y,
3243                             secondary.width, secondary.height);
3244
3245   gdk_window_move_resize (priv->text_area, x, y, width, height);
3246 }
3247
3248 static void
3249 gtk_entry_get_text_area_size (GtkEntry *entry,
3250                               gint     *x,
3251                               gint     *y,
3252                               gint     *width,
3253                               gint     *height)
3254 {
3255   GtkEntryPrivate *priv = entry->priv;
3256   GtkWidget *widget = GTK_WIDGET (entry);
3257   GtkAllocation allocation;
3258   GtkRequisition requisition;
3259   gint req_height;
3260   gint frame_height;
3261   GtkBorder borders;
3262
3263   gtk_widget_get_preferred_size (widget, &requisition, NULL);
3264   req_height = requisition.height - gtk_widget_get_margin_top (widget) - gtk_widget_get_margin_bottom (widget);
3265
3266   gtk_widget_get_allocation (widget, &allocation);
3267   _gtk_entry_get_borders (entry, &borders);
3268
3269   if (gtk_widget_get_realized (widget))
3270     get_frame_size (entry, TRUE, NULL, NULL, NULL, &frame_height);
3271   else
3272     frame_height = req_height;
3273
3274   if (gtk_widget_has_focus (widget) && !priv->interior_focus)
3275     frame_height -= 2 * priv->focus_width;
3276
3277   if (x)
3278     *x = borders.left;
3279
3280   if (y)
3281     *y = floor ((frame_height - req_height) / 2) + borders.top;
3282
3283   if (width)
3284     *width = allocation.width - borders.left - borders.right;
3285
3286   if (height)
3287     *height = req_height - borders.top - borders.bottom;
3288 }
3289
3290 static void
3291 get_text_area_size (GtkEntry *entry,
3292                     gint     *x,
3293                     gint     *y,
3294                     gint     *width,
3295                     gint     *height)
3296 {
3297   GtkEntryClass *class;
3298
3299   g_return_if_fail (GTK_IS_ENTRY (entry));
3300
3301   class = GTK_ENTRY_GET_CLASS (entry);
3302
3303   if (class->get_text_area_size)
3304     class->get_text_area_size (entry, x, y, width, height);
3305 }
3306
3307
3308 static void
3309 get_frame_size (GtkEntry *entry,
3310                 gboolean  relative_to_window,
3311                 gint     *x,
3312                 gint     *y,
3313                 gint     *width,
3314                 gint     *height)
3315 {
3316   GtkEntryPrivate *priv = entry->priv;
3317   GtkAllocation allocation;
3318   GtkRequisition requisition;
3319   GtkWidget *widget = GTK_WIDGET (entry);
3320   gint req_height;
3321
3322   gtk_widget_get_preferred_size (widget, &requisition, NULL);
3323
3324   req_height = requisition.height - gtk_widget_get_margin_top (widget) - gtk_widget_get_margin_bottom (widget);
3325
3326   gtk_widget_get_allocation (widget, &allocation);
3327
3328   if (x)
3329     *x = relative_to_window ? allocation.x : 0;
3330
3331   if (y)
3332     {
3333       if (priv->is_cell_renderer)
3334         *y = 0;
3335       else
3336         *y = (allocation.height - req_height) / 2;
3337
3338       if (relative_to_window)
3339         *y += allocation.y;
3340     }
3341
3342   if (width)
3343     *width = allocation.width;
3344
3345   if (height)
3346     {
3347       if (priv->is_cell_renderer)
3348         *height = allocation.height;
3349       else
3350         *height = req_height;
3351     }
3352 }
3353
3354 static void
3355 gtk_entry_size_allocate (GtkWidget     *widget,
3356                          GtkAllocation *allocation)
3357 {
3358   GtkEntry *entry = GTK_ENTRY (widget);
3359
3360   gtk_widget_set_allocation (widget, allocation);
3361
3362   if (gtk_widget_get_realized (widget))
3363     {
3364       GtkEntryCompletion* completion;
3365
3366       place_windows (entry);
3367       gtk_entry_recompute (entry);
3368
3369       completion = gtk_entry_get_completion (entry);
3370       if (completion && gtk_widget_get_mapped (completion->priv->popup_window))
3371         _gtk_entry_completion_resize_popup (completion);
3372     }
3373 }
3374
3375 static gboolean
3376 should_prelight (GtkEntry             *entry,
3377                  GtkEntryIconPosition  icon_pos)
3378 {
3379   GtkEntryPrivate *priv = entry->priv;
3380   EntryIconInfo *icon_info = priv->icons[icon_pos];
3381   gboolean prelight;
3382
3383   if (!icon_info)
3384     return FALSE;
3385
3386   if (icon_info->nonactivatable && icon_info->target_list == NULL)
3387     return FALSE;
3388
3389   if (icon_info->pressed)
3390     return FALSE;
3391
3392   gtk_widget_style_get (GTK_WIDGET (entry),
3393                         "icon-prelight", &prelight,
3394                         NULL);
3395
3396   return prelight;
3397 }
3398
3399 static void
3400 draw_icon (GtkWidget            *widget,
3401            cairo_t              *cr,
3402            GtkEntryIconPosition  icon_pos)
3403 {
3404   GtkEntry *entry = GTK_ENTRY (widget);
3405   GtkEntryPrivate *priv = entry->priv;
3406   EntryIconInfo *icon_info = priv->icons[icon_pos];
3407   gint x, y, width, height, pix_width, pix_height;
3408   GtkStyleContext *context;
3409   GtkBorder padding;
3410
3411   if (!icon_info)
3412     return;
3413
3414   width = gdk_window_get_width (icon_info->window);
3415   height = gdk_window_get_height (icon_info->window);
3416
3417   /* size_allocate hasn't been called yet. These are the default values.
3418    */
3419   if (width == 1 || height == 1)
3420     return;
3421
3422   cairo_save (cr);
3423   gtk_cairo_transform_to_window (cr, widget, icon_info->window);
3424
3425   context = gtk_widget_get_style_context (widget);
3426   gtk_entry_prepare_context_for_icon (entry, context, icon_pos);
3427   _gtk_icon_helper_get_size (icon_info->icon_helper, context,
3428                              &pix_width, &pix_height);
3429   gtk_style_context_get_padding (context, 0, &padding);
3430
3431   x = MAX (0, padding.left);
3432   y = MAX (0, (height - pix_height) / 2);
3433
3434   _gtk_icon_helper_draw (icon_info->icon_helper,
3435                          context, cr,
3436                          x, y);
3437
3438   gtk_style_context_restore (context);
3439   cairo_restore (cr);
3440 }
3441
3442
3443 static void
3444 gtk_entry_draw_frame (GtkWidget       *widget,
3445                       GtkStyleContext *context,
3446                       cairo_t         *cr)
3447 {
3448   GtkEntry *entry = GTK_ENTRY (widget);
3449   GtkEntryPrivate *priv = entry->priv;
3450   gint x = 0, y = 0, width, height;
3451   gint frame_x, frame_y;
3452
3453   cairo_save (cr);
3454
3455   get_frame_size (GTK_ENTRY (widget), FALSE, &frame_x, &frame_y, &width, &height);
3456
3457   cairo_translate (cr, frame_x, frame_y);
3458
3459   /* Fix a problem with some themes which assume that entry->text_area's
3460    * width equals widget->window's width
3461    * http://bugzilla.gnome.org/show_bug.cgi?id=466000 */
3462   if (GTK_IS_SPIN_BUTTON (widget))
3463     {
3464       GtkBorder borders;
3465
3466       gtk_entry_get_text_area_size (GTK_ENTRY (widget), &x, NULL, &width, NULL);
3467       _gtk_entry_get_borders (GTK_ENTRY (widget), &borders);
3468
3469       x -= borders.left;
3470       width += borders.left + borders.right;
3471     }
3472
3473   if (gtk_widget_has_focus (widget) && !priv->interior_focus)
3474     {
3475       x += priv->focus_width;
3476       y += priv->focus_width;
3477       width -= 2 * priv->focus_width;
3478       height -= 2 * priv->focus_width;
3479     }
3480
3481   gtk_render_background (context, cr,
3482                          x, y, width, height);
3483
3484   if (priv->has_frame)
3485     gtk_render_frame (context, cr,
3486                       x, y, width, height);
3487
3488   gtk_entry_draw_progress (widget, context, cr);
3489
3490   if (gtk_widget_has_visible_focus (widget) && !priv->interior_focus)
3491     {
3492       x -= priv->focus_width;
3493       y -= priv->focus_width;
3494       width += 2 * priv->focus_width;
3495       height += 2 * priv->focus_width;
3496
3497       gtk_render_focus (context, cr,
3498                         0, 0, width, height);
3499     }
3500
3501   cairo_restore (cr);
3502 }
3503
3504 static void
3505 gtk_entry_prepare_context_for_progress (GtkEntry *entry,
3506                                         GtkStyleContext *context)
3507 {
3508   GtkEntryPrivate *private = entry->priv;
3509
3510   gtk_style_context_save (context);
3511   gtk_style_context_add_class (context, GTK_STYLE_CLASS_PROGRESSBAR);
3512   if (private->progress_pulse_mode)
3513     gtk_style_context_add_class (context, GTK_STYLE_CLASS_PULSE);
3514 }
3515
3516 static void
3517 get_progress_area (GtkWidget *widget,
3518                    gint       *x,
3519                    gint       *y,
3520                    gint       *width,
3521                    gint       *height)
3522 {
3523   GtkEntry *entry = GTK_ENTRY (widget);
3524   GtkEntryPrivate *private = entry->priv;
3525   GtkStyleContext *context;
3526   GtkBorder margin, border, entry_borders;
3527   gint frame_width, text_area_width, text_area_height;
3528
3529   context = gtk_widget_get_style_context (widget);
3530   _gtk_entry_get_borders (entry, &entry_borders);
3531   get_text_area_size (entry,
3532                       NULL, NULL,
3533                       &text_area_width, &text_area_height);
3534   get_frame_size (entry, FALSE,
3535                   NULL, NULL,
3536                   &frame_width, NULL);
3537
3538   *x = 0;
3539   *y = 0;
3540   *width = text_area_width + entry_borders.left + entry_borders.right;
3541   *height = text_area_height + entry_borders.top + entry_borders.bottom;
3542
3543   if (!private->interior_focus)
3544     {
3545       *x -= private->focus_width;
3546       *y -= private->focus_width;
3547     }
3548
3549   /* if the text area got resized by a subclass, subtract the left/right
3550    * border width, so that the progress bar won't extend over the resized
3551    * text area.
3552    */
3553   if (frame_width > *width)
3554     {
3555       gtk_style_context_get_border (context, 0, &border);
3556       if (gtk_widget_get_direction (GTK_WIDGET (entry)) == GTK_TEXT_DIR_RTL)
3557         {
3558           *x = (frame_width - *width) + border.left;
3559           *width -= border.left;
3560         }
3561       else
3562         {
3563           *width -= border.right;
3564         }
3565     }
3566
3567   gtk_entry_prepare_context_for_progress (entry, context);
3568   gtk_style_context_get_margin (context, 0, &margin);
3569
3570   gtk_style_context_restore (context);
3571
3572   *x += margin.left;
3573   *y += margin.top;
3574   *width -= margin.left + margin.right;
3575   *height -= margin.top + margin.bottom;
3576
3577   if (private->progress_pulse_mode)
3578     {
3579       gdouble value = private->progress_pulse_current;
3580
3581       *x += (gint) floor(value * (*width));
3582       *width = (gint) ceil(private->progress_pulse_fraction * (*width));
3583     }
3584   else if (private->progress_fraction > 0)
3585     {
3586       gdouble value = private->progress_fraction;
3587
3588       if (gtk_widget_get_direction (GTK_WIDGET (entry)) == GTK_TEXT_DIR_RTL)
3589         {
3590           gint bar_width;
3591
3592           bar_width = floor(value * (*width) + 0.5);
3593           *x += *width - bar_width;
3594           *width = bar_width;
3595         }
3596       else
3597         {
3598           *width = (gint) floor(value * (*width) + 0.5);
3599         }
3600     }
3601   else
3602     {
3603       *width = 0;
3604       *height = 0;
3605     }
3606 }
3607
3608 static void
3609 gtk_entry_draw_progress (GtkWidget       *widget,
3610                          GtkStyleContext *context,
3611                          cairo_t         *cr)
3612 {
3613   GtkEntry *entry = GTK_ENTRY (widget);
3614   gint x, y, width, height;
3615
3616   get_progress_area (widget, &x, &y, &width, &height);
3617
3618   if ((width <= 0) || (height <= 0))
3619     return;
3620  
3621   gtk_entry_prepare_context_for_progress (entry, context);
3622   gtk_render_activity (context, cr,
3623                        x, y, width, height);
3624
3625   gtk_style_context_restore (context);
3626 }
3627
3628 static gint
3629 gtk_entry_draw (GtkWidget *widget,
3630                 cairo_t   *cr)
3631 {
3632   GtkEntry *entry = GTK_ENTRY (widget);
3633   GtkStyleContext *context;
3634   GtkEntryPrivate *priv = entry->priv;
3635   int i;
3636
3637   context = gtk_widget_get_style_context (widget);
3638
3639   /* Draw entry_bg, shadow, progress and focus */
3640   gtk_entry_draw_frame (widget, context, cr);
3641
3642   /* Draw text and cursor */
3643   cairo_save (cr);
3644
3645   gtk_cairo_transform_to_window (cr, widget, priv->text_area);
3646
3647   if (priv->dnd_position != -1)
3648     gtk_entry_draw_cursor (GTK_ENTRY (widget), cr, CURSOR_DND);
3649   
3650   gtk_entry_draw_text (GTK_ENTRY (widget), cr);
3651
3652   /* When no text is being displayed at all, don't show the cursor */
3653   if (gtk_entry_get_display_mode (entry) != DISPLAY_BLANK &&
3654       gtk_widget_has_focus (widget) &&
3655       priv->selection_bound == priv->current_pos && priv->cursor_visible)
3656     gtk_entry_draw_cursor (GTK_ENTRY (widget), cr, CURSOR_STANDARD);
3657
3658   cairo_restore (cr);
3659
3660   /* Draw icons */
3661   for (i = 0; i < MAX_ICONS; i++)
3662     {
3663       EntryIconInfo *icon_info = priv->icons[i];
3664
3665       if (icon_info != NULL)
3666         draw_icon (widget, cr, i);
3667     }
3668
3669   return FALSE;
3670 }
3671
3672 static gint
3673 gtk_entry_enter_notify (GtkWidget *widget,
3674                         GdkEventCrossing *event)
3675 {
3676   GtkEntry *entry = GTK_ENTRY (widget);
3677   GtkEntryPrivate *priv = entry->priv;
3678   gint i;
3679
3680   for (i = 0; i < MAX_ICONS; i++)
3681     {
3682       EntryIconInfo *icon_info = priv->icons[i];
3683
3684       if (icon_info != NULL && event->window == icon_info->window)
3685         {
3686           if (should_prelight (entry, i))
3687             {
3688               icon_info->prelight = TRUE;
3689               gtk_widget_queue_draw (widget);
3690             }
3691
3692           break;
3693         }
3694     }
3695
3696     return FALSE;
3697 }
3698
3699 static gint
3700 gtk_entry_leave_notify (GtkWidget        *widget,
3701                         GdkEventCrossing *event)
3702 {
3703   GtkEntry *entry = GTK_ENTRY (widget);
3704   GtkEntryPrivate *priv = entry->priv;
3705   gint i;
3706
3707   for (i = 0; i < MAX_ICONS; i++)
3708     {
3709       EntryIconInfo *icon_info = priv->icons[i];
3710
3711       if (icon_info != NULL && event->window == icon_info->window)
3712         {
3713           /* a grab means that we may never see the button release */
3714           if (event->mode == GDK_CROSSING_GRAB || event->mode == GDK_CROSSING_GTK_GRAB)
3715             icon_info->pressed = FALSE;
3716
3717           if (should_prelight (entry, i))
3718             {
3719               icon_info->prelight = FALSE;
3720               gtk_widget_queue_draw (widget);
3721             }
3722
3723           break;
3724         }
3725     }
3726
3727   return FALSE;
3728 }
3729
3730 static void
3731 gtk_entry_get_pixel_ranges (GtkEntry  *entry,
3732                             gint     **ranges,
3733                             gint      *n_ranges)
3734 {
3735   gint start_char, end_char;
3736
3737   if (gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &start_char, &end_char))
3738     {
3739       PangoLayout *layout = gtk_entry_ensure_layout (entry, TRUE);
3740       PangoLayoutLine *line = pango_layout_get_lines_readonly (layout)->data;
3741       const char *text = pango_layout_get_text (layout);
3742       gint start_index = g_utf8_offset_to_pointer (text, start_char) - text;
3743       gint end_index = g_utf8_offset_to_pointer (text, end_char) - text;
3744       gint real_n_ranges, i;
3745
3746       pango_layout_line_get_x_ranges (line, start_index, end_index, ranges, &real_n_ranges);
3747
3748       if (ranges)
3749         {
3750           gint *r = *ranges;
3751           
3752           for (i = 0; i < real_n_ranges; ++i)
3753             {
3754               r[2 * i + 1] = (r[2 * i + 1] - r[2 * i]) / PANGO_SCALE;
3755               r[2 * i] = r[2 * i] / PANGO_SCALE;
3756             }
3757         }
3758       
3759       if (n_ranges)
3760         *n_ranges = real_n_ranges;
3761     }
3762   else
3763     {
3764       if (n_ranges)
3765         *n_ranges = 0;
3766       if (ranges)
3767         *ranges = NULL;
3768     }
3769 }
3770
3771 static gboolean
3772 in_selection (GtkEntry *entry,
3773               gint      x)
3774 {
3775   gint *ranges;
3776   gint n_ranges, i;
3777   gint retval = FALSE;
3778
3779   gtk_entry_get_pixel_ranges (entry, &ranges, &n_ranges);
3780
3781   for (i = 0; i < n_ranges; ++i)
3782     {
3783       if (x >= ranges[2 * i] && x < ranges[2 * i] + ranges[2 * i + 1])
3784         {
3785           retval = TRUE;
3786           break;
3787         }
3788     }
3789
3790   g_free (ranges);
3791   return retval;
3792 }
3793               
3794 static gint
3795 gtk_entry_button_press (GtkWidget      *widget,
3796                         GdkEventButton *event)
3797 {
3798   GtkEntry *entry = GTK_ENTRY (widget);
3799   GtkEditable *editable = GTK_EDITABLE (widget);
3800   GtkEntryPrivate *priv = entry->priv;
3801   EntryIconInfo *icon_info = NULL;
3802   gint tmp_pos;
3803   gint sel_start, sel_end;
3804   gint i;
3805
3806   for (i = 0; i < MAX_ICONS; i++)
3807     {
3808       icon_info = priv->icons[i];
3809
3810       if (!icon_info || icon_info->insensitive)
3811         continue;
3812
3813       if (event->window == icon_info->window)
3814         {
3815           if (should_prelight (entry, i))
3816             {
3817               icon_info->prelight = FALSE;
3818               gtk_widget_queue_draw (widget);
3819             }
3820
3821           priv->start_x = event->x;
3822           priv->start_y = event->y;
3823           icon_info->pressed = TRUE;
3824
3825           if (!icon_info->nonactivatable)
3826             g_signal_emit (entry, signals[ICON_PRESS], 0, i, event);
3827
3828           return TRUE;
3829         }
3830     }
3831
3832   if (event->window != priv->text_area ||
3833       (priv->button && event->button != priv->button))
3834     return FALSE;
3835
3836   gtk_entry_reset_blink_time (entry);
3837
3838   priv->button = event->button;
3839   
3840   if (!gtk_widget_has_focus (widget))
3841     {
3842       priv->in_click = TRUE;
3843       gtk_widget_grab_focus (widget);
3844       priv->in_click = FALSE;
3845     }
3846
3847   tmp_pos = gtk_entry_find_position (entry, event->x + priv->scroll_offset);
3848
3849   if (gdk_event_triggers_context_menu ((GdkEvent *) event))
3850     {
3851       gtk_entry_do_popup (entry, event);
3852       priv->button = 0; /* Don't wait for release, since the menu will gtk_grab_add */
3853
3854       return TRUE;
3855     }
3856   else if (event->button == GDK_BUTTON_PRIMARY)
3857     {
3858       gboolean have_selection = gtk_editable_get_selection_bounds (editable, &sel_start, &sel_end);
3859
3860       priv->select_words = FALSE;
3861       priv->select_lines = FALSE;
3862
3863       if (event->state &
3864           gtk_widget_get_modifier_mask (widget,
3865                                         GDK_MODIFIER_INTENT_EXTEND_SELECTION))
3866         {
3867           _gtk_entry_reset_im_context (entry);
3868
3869           if (!have_selection) /* select from the current position to the clicked position */
3870             sel_start = sel_end = priv->current_pos;
3871           
3872           if (tmp_pos > sel_start && tmp_pos < sel_end)
3873             {
3874               /* Truncate current selection, but keep it as big as possible */
3875               if (tmp_pos - sel_start > sel_end - tmp_pos)
3876                 gtk_entry_set_positions (entry, sel_start, tmp_pos);
3877               else
3878                 gtk_entry_set_positions (entry, tmp_pos, sel_end);
3879             }
3880           else
3881             {
3882               gboolean extend_to_left;
3883               gint start, end;
3884
3885               /* Figure out what click selects and extend current selection */
3886               switch (event->type)
3887                 {
3888                 case GDK_BUTTON_PRESS:
3889                   gtk_entry_set_positions (entry, tmp_pos, tmp_pos);
3890                   break;
3891                   
3892                 case GDK_2BUTTON_PRESS:
3893                   priv->select_words = TRUE;
3894                   gtk_entry_select_word (entry);
3895                   break;
3896                   
3897                 case GDK_3BUTTON_PRESS:
3898                   priv->select_lines = TRUE;
3899                   gtk_entry_select_line (entry);
3900                   break;
3901
3902                 default:
3903                   break;
3904                 }
3905
3906               start = MIN (priv->current_pos, priv->selection_bound);
3907               start = MIN (sel_start, start);
3908               
3909               end = MAX (priv->current_pos, priv->selection_bound);
3910               end = MAX (sel_end, end);
3911
3912               if (tmp_pos == sel_start || tmp_pos == sel_end)
3913                 extend_to_left = (tmp_pos == start);
3914               else
3915                 extend_to_left = (end == sel_end);
3916               
3917               if (extend_to_left)
3918                 gtk_entry_set_positions (entry, start, end);
3919               else
3920                 gtk_entry_set_positions (entry, end, start);
3921             }
3922         }
3923       else /* no shift key */
3924         switch (event->type)
3925         {
3926         case GDK_BUTTON_PRESS:
3927           if (in_selection (entry, event->x + priv->scroll_offset))
3928             {
3929               /* Click inside the selection - we'll either start a drag, or
3930                * clear the selection
3931                */
3932               priv->in_drag = TRUE;
3933               priv->drag_start_x = event->x + priv->scroll_offset;
3934               priv->drag_start_y = event->y;
3935             }
3936           else
3937             gtk_editable_set_position (editable, tmp_pos);
3938           break;
3939  
3940         case GDK_2BUTTON_PRESS:
3941           /* We ALWAYS receive a GDK_BUTTON_PRESS immediately before 
3942            * receiving a GDK_2BUTTON_PRESS so we need to reset
3943            * priv->in_drag which may have been set above
3944            */
3945           priv->in_drag = FALSE;
3946           priv->select_words = TRUE;
3947           gtk_entry_select_word (entry);
3948           break;
3949         
3950         case GDK_3BUTTON_PRESS:
3951           /* We ALWAYS receive a GDK_BUTTON_PRESS immediately before
3952            * receiving a GDK_3BUTTON_PRESS so we need to reset
3953            * priv->in_drag which may have been set above
3954            */
3955           priv->in_drag = FALSE;
3956           priv->select_lines = TRUE;
3957           gtk_entry_select_line (entry);
3958           break;
3959
3960         default:
3961           break;
3962         }
3963
3964       return TRUE;
3965     }
3966   else if (event->button == GDK_BUTTON_MIDDLE && event->type == GDK_BUTTON_PRESS)
3967     {
3968       if (priv->editable)
3969         {
3970           priv->insert_pos = tmp_pos;
3971           gtk_entry_paste (entry, GDK_SELECTION_PRIMARY);
3972           return TRUE;
3973         }
3974       else
3975         {
3976           gtk_widget_error_bell (widget);
3977         }
3978     }
3979
3980   return FALSE;
3981 }
3982
3983 static gint
3984 gtk_entry_button_release (GtkWidget      *widget,
3985                           GdkEventButton *event)
3986 {
3987   GtkEntry *entry = GTK_ENTRY (widget);
3988   GtkEntryPrivate *priv = entry->priv;
3989   EntryIconInfo *icon_info = NULL;
3990   gint i;
3991
3992   for (i = 0; i < MAX_ICONS; i++)
3993     {
3994       icon_info = priv->icons[i];
3995
3996       if (!icon_info || icon_info->insensitive)
3997         continue;
3998
3999       if (event->window == icon_info->window)
4000         {
4001           icon_info->pressed = FALSE;
4002
4003           if (should_prelight (entry, i) &&
4004               event->x >= 0 && event->y >= 0 &&
4005               event->x < gdk_window_get_width (icon_info->window) &&
4006               event->y < gdk_window_get_height (icon_info->window))
4007             {
4008               icon_info->prelight = TRUE;
4009               gtk_widget_queue_draw (widget);
4010             }
4011
4012           if (!icon_info->nonactivatable)
4013             g_signal_emit (entry, signals[ICON_RELEASE], 0, i, event);
4014
4015           return TRUE;
4016         }
4017     }
4018
4019   if (event->window != priv->text_area || priv->button != event->button)
4020     return FALSE;
4021
4022   if (priv->in_drag)
4023     {
4024       gint tmp_pos = gtk_entry_find_position (entry, priv->drag_start_x);
4025
4026       gtk_editable_set_position (GTK_EDITABLE (entry), tmp_pos);
4027
4028       priv->in_drag = 0;
4029     }
4030   
4031   priv->button = 0;
4032   
4033   gtk_entry_update_primary_selection (entry);
4034               
4035   return TRUE;
4036 }
4037
4038 static gchar *
4039 _gtk_entry_get_selected_text (GtkEntry *entry)
4040 {
4041   GtkEditable *editable = GTK_EDITABLE (entry);
4042   gint         start_text, end_text;
4043   gchar       *text = NULL;
4044
4045   if (gtk_editable_get_selection_bounds (editable, &start_text, &end_text))
4046     text = gtk_editable_get_chars (editable, start_text, end_text);
4047
4048   return text;
4049 }
4050
4051 static gint
4052 gtk_entry_motion_notify (GtkWidget      *widget,
4053                          GdkEventMotion *event)
4054 {
4055   GtkEntry *entry = GTK_ENTRY (widget);
4056   GtkEntryPrivate *priv = entry->priv;
4057   EntryIconInfo *icon_info = NULL;
4058   gint tmp_pos;
4059   gint i;
4060
4061   for (i = 0; i < MAX_ICONS; i++)
4062     {
4063       icon_info = priv->icons[i];
4064
4065       if (!icon_info || icon_info->insensitive)
4066         continue;
4067
4068       if (event->window == icon_info->window)
4069         {
4070           if (icon_info->pressed &&
4071               icon_info->target_list != NULL &&
4072               gtk_drag_check_threshold (widget,
4073                                         priv->start_x,
4074                                         priv->start_y,
4075                                         event->x,
4076                                         event->y))
4077             {
4078               icon_info->in_drag = TRUE;
4079               icon_info->pressed = FALSE;
4080               gtk_drag_begin (widget,
4081                               icon_info->target_list,
4082                               icon_info->actions,
4083                               1,
4084                               (GdkEvent*)event);
4085             }
4086
4087           return TRUE;
4088         }
4089     }
4090
4091   if (priv->mouse_cursor_obscured)
4092     {
4093       GdkCursor *cursor;
4094
4095       cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget), GDK_XTERM);
4096       gdk_window_set_cursor (priv->text_area, cursor);
4097       g_object_unref (cursor);
4098       priv->mouse_cursor_obscured = FALSE;
4099     }
4100
4101   if (event->window != priv->text_area || priv->button != GDK_BUTTON_PRIMARY)
4102     return FALSE;
4103
4104   if (priv->select_lines)
4105     return TRUE;
4106
4107   gdk_event_request_motions (event);
4108
4109   if (priv->in_drag)
4110     {
4111       if (gtk_entry_get_display_mode (entry) == DISPLAY_NORMAL &&
4112           gtk_drag_check_threshold (widget,
4113                                     priv->drag_start_x, priv->drag_start_y,
4114                                     event->x + priv->scroll_offset, event->y))
4115         {
4116           GdkDragContext *context;
4117           GtkTargetList  *target_list = gtk_target_list_new (NULL, 0);
4118           guint actions = priv->editable ? GDK_ACTION_COPY | GDK_ACTION_MOVE : GDK_ACTION_COPY;
4119           gchar *text = NULL;
4120           cairo_surface_t *surface;
4121
4122           gtk_target_list_add_text_targets (target_list, 0);
4123
4124           text = _gtk_entry_get_selected_text (entry);
4125           surface = _gtk_text_util_create_drag_icon (widget, text, -1);
4126
4127           context = gtk_drag_begin (widget, target_list, actions,
4128                                     priv->button, (GdkEvent *)event);
4129           
4130           if (surface)
4131             gtk_drag_set_icon_surface (context, surface);
4132           else
4133             gtk_drag_set_icon_default (context);
4134           
4135           if (surface)
4136             cairo_surface_destroy (surface);
4137           g_free (text);
4138
4139           priv->in_drag = FALSE;
4140           priv->button = 0;
4141           
4142           gtk_target_list_unref (target_list);
4143         }
4144     }
4145   else
4146     {
4147       if (event->y < 0)
4148         tmp_pos = 0;
4149       else if (event->y >= gdk_window_get_height (priv->text_area))
4150         tmp_pos = gtk_entry_buffer_get_length (get_buffer (entry));
4151       else
4152         tmp_pos = gtk_entry_find_position (entry, event->x + priv->scroll_offset);
4153
4154       if (priv->select_words)
4155         {
4156           gint min, max;
4157           gint old_min, old_max;
4158           gint pos, bound;
4159           
4160           min = gtk_entry_move_backward_word (entry, tmp_pos, TRUE);
4161           max = gtk_entry_move_forward_word (entry, tmp_pos, TRUE);
4162
4163           pos = priv->current_pos;
4164           bound = priv->selection_bound;
4165
4166           old_min = MIN(priv->current_pos, priv->selection_bound);
4167           old_max = MAX(priv->current_pos, priv->selection_bound);
4168           
4169           if (min < old_min)
4170             {
4171               pos = min;
4172               bound = old_max;
4173             }
4174           else if (old_max < max) 
4175             {
4176               pos = max;
4177               bound = old_min;
4178             }
4179           else if (pos == old_min) 
4180             {
4181               if (priv->current_pos != min)
4182                 pos = max;
4183             }
4184           else 
4185             {
4186               if (priv->current_pos != max)
4187                 pos = min;
4188             }
4189         
4190           gtk_entry_set_positions (entry, pos, bound);
4191         }
4192       else
4193         gtk_entry_set_positions (entry, tmp_pos, -1);
4194     }
4195       
4196   return TRUE;
4197 }
4198
4199 static void
4200 set_invisible_cursor (GdkWindow *window)
4201 {
4202   GdkDisplay *display;
4203   GdkCursor *cursor;
4204
4205   display = gdk_window_get_display (window);
4206   cursor = gdk_cursor_new_for_display (display, GDK_BLANK_CURSOR);
4207
4208   gdk_window_set_cursor (window, cursor);
4209
4210   g_object_unref (cursor);
4211 }
4212
4213 static void
4214 gtk_entry_obscure_mouse_cursor (GtkEntry *entry)
4215 {
4216   GtkEntryPrivate *priv = entry->priv;
4217
4218   if (priv->mouse_cursor_obscured)
4219     return;
4220
4221   set_invisible_cursor (priv->text_area);
4222
4223   priv->mouse_cursor_obscured = TRUE;
4224 }
4225
4226 static gint
4227 gtk_entry_key_press (GtkWidget   *widget,
4228                      GdkEventKey *event)
4229 {
4230   GtkEntry *entry = GTK_ENTRY (widget);
4231   GtkEntryPrivate *priv = entry->priv;
4232
4233   gtk_entry_reset_blink_time (entry);
4234   gtk_entry_pend_cursor_blink (entry);
4235
4236   if (priv->editable)
4237     {
4238       if (gtk_im_context_filter_keypress (priv->im_context, event))
4239         {
4240           gtk_entry_obscure_mouse_cursor (entry);
4241           priv->need_im_reset = TRUE;
4242           return TRUE;
4243         }
4244     }
4245
4246   if (event->keyval == GDK_KEY_Return || 
4247       event->keyval == GDK_KEY_KP_Enter || 
4248       event->keyval == GDK_KEY_ISO_Enter || 
4249       event->keyval == GDK_KEY_Escape)
4250     {
4251       GtkEntryCompletion *completion = gtk_entry_get_completion (entry);
4252
4253       if (completion && completion->priv->completion_timeout)
4254         {
4255           g_source_remove (completion->priv->completion_timeout);
4256           completion->priv->completion_timeout = 0;
4257         }
4258
4259       _gtk_entry_reset_im_context (entry);
4260     }
4261
4262   if (GTK_WIDGET_CLASS (gtk_entry_parent_class)->key_press_event (widget, event))
4263     /* Activate key bindings
4264      */
4265     return TRUE;
4266
4267   if (!priv->editable && event->length)
4268     gtk_widget_error_bell (widget);
4269
4270   return FALSE;
4271 }
4272
4273 static gint
4274 gtk_entry_key_release (GtkWidget   *widget,
4275                        GdkEventKey *event)
4276 {
4277   GtkEntry *entry = GTK_ENTRY (widget);
4278   GtkEntryPrivate *priv = entry->priv;
4279
4280   if (priv->editable)
4281     {
4282       if (gtk_im_context_filter_keypress (priv->im_context, event))
4283         {
4284           priv->need_im_reset = TRUE;
4285           return TRUE;
4286         }
4287     }
4288
4289   return GTK_WIDGET_CLASS (gtk_entry_parent_class)->key_release_event (widget, event);
4290 }
4291
4292 static gint
4293 gtk_entry_focus_in (GtkWidget     *widget,
4294                     GdkEventFocus *event)
4295 {
4296   GtkEntry *entry = GTK_ENTRY (widget);
4297   GtkEntryPrivate *priv = entry->priv;
4298   GdkKeymap *keymap;
4299
4300   gtk_widget_queue_draw (widget);
4301
4302   keymap = gdk_keymap_get_for_display (gtk_widget_get_display (widget));
4303
4304   if (priv->editable)
4305     {
4306       priv->need_im_reset = TRUE;
4307       gtk_im_context_focus_in (priv->im_context);
4308       keymap_state_changed (keymap, entry);
4309       g_signal_connect (keymap, "state-changed", 
4310                         G_CALLBACK (keymap_state_changed), entry);
4311     }
4312
4313   g_signal_connect (keymap, "direction-changed",
4314                     G_CALLBACK (keymap_direction_changed), entry);
4315
4316   if (gtk_entry_buffer_get_bytes (get_buffer (entry)) == 0 &&
4317       priv->placeholder_text != NULL)
4318     {
4319       gtk_entry_recompute (entry);
4320     }
4321   else
4322     {
4323       gtk_entry_reset_blink_time (entry);
4324       gtk_entry_check_cursor_blink (entry);
4325     }
4326
4327   return FALSE;
4328 }
4329
4330 static gint
4331 gtk_entry_focus_out (GtkWidget     *widget,
4332                      GdkEventFocus *event)
4333 {
4334   GtkEntry *entry = GTK_ENTRY (widget);
4335   GtkEntryPrivate *priv = entry->priv;
4336   GtkEntryCompletion *completion;
4337   GdkKeymap *keymap;
4338
4339   gtk_widget_queue_draw (widget);
4340
4341   keymap = gdk_keymap_get_for_display (gtk_widget_get_display (widget));
4342
4343   if (priv->editable)
4344     {
4345       priv->need_im_reset = TRUE;
4346       gtk_im_context_focus_out (priv->im_context);
4347       remove_capslock_feedback (entry);
4348     }
4349
4350   if (gtk_entry_buffer_get_bytes (get_buffer (entry)) == 0 &&
4351       priv->placeholder_text != NULL)
4352     {
4353       gtk_entry_recompute (entry);
4354     }
4355   else
4356     {
4357       gtk_entry_check_cursor_blink (entry);
4358     }
4359
4360   g_signal_handlers_disconnect_by_func (keymap, keymap_state_changed, entry);
4361   g_signal_handlers_disconnect_by_func (keymap, keymap_direction_changed, entry);
4362
4363   completion = gtk_entry_get_completion (entry);
4364   if (completion)
4365     _gtk_entry_completion_popdown (completion);
4366
4367   return FALSE;
4368 }
4369
4370 static void
4371 gtk_entry_grab_focus (GtkWidget *widget)
4372 {
4373   GtkEntry *entry = GTK_ENTRY (widget);
4374   GtkEntryPrivate *priv = entry->priv;
4375   gboolean select_on_focus;
4376
4377   GTK_WIDGET_CLASS (gtk_entry_parent_class)->grab_focus (widget);
4378
4379   if (priv->editable && !priv->in_click)
4380     {
4381       g_object_get (gtk_widget_get_settings (widget),
4382                     "gtk-entry-select-on-focus",
4383                     &select_on_focus,
4384                     NULL);
4385
4386       if (select_on_focus)
4387         gtk_editable_select_region (GTK_EDITABLE (widget), 0, -1);
4388     }
4389 }
4390
4391 static void
4392 gtk_entry_direction_changed (GtkWidget        *widget,
4393                              GtkTextDirection  previous_dir)
4394 {
4395   GtkEntry *entry = GTK_ENTRY (widget);
4396
4397   gtk_entry_recompute (entry);
4398
4399   GTK_WIDGET_CLASS (gtk_entry_parent_class)->direction_changed (widget, previous_dir);
4400 }
4401
4402 static void
4403 gtk_entry_state_flags_changed (GtkWidget     *widget,
4404                                GtkStateFlags  previous_state)
4405 {
4406   GtkEntry *entry = GTK_ENTRY (widget);
4407   GtkEntryPrivate *priv = entry->priv;
4408   GdkCursor *cursor;
4409
4410   if (gtk_widget_get_realized (widget))
4411     {
4412       if (gtk_widget_is_sensitive (widget))
4413         cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget), GDK_XTERM);
4414       else
4415         cursor = NULL;
4416
4417       gdk_window_set_cursor (priv->text_area, cursor);
4418
4419       if (cursor)
4420         g_object_unref (cursor);
4421
4422       priv->mouse_cursor_obscured = FALSE;
4423
4424       update_cursors (widget);
4425     }
4426
4427   if (!gtk_widget_is_sensitive (widget))
4428     {
4429       /* Clear any selection */
4430       gtk_editable_select_region (GTK_EDITABLE (entry), priv->current_pos, priv->current_pos);
4431     }
4432
4433   gtk_entry_update_cached_style_values (entry);
4434 }
4435
4436 static void
4437 gtk_entry_screen_changed (GtkWidget *widget,
4438                           GdkScreen *old_screen)
4439 {
4440   gtk_entry_recompute (GTK_ENTRY (widget));
4441 }
4442
4443 /* GtkEditable method implementations
4444  */
4445 static void
4446 gtk_entry_insert_text (GtkEditable *editable,
4447                        const gchar *new_text,
4448                        gint         new_text_length,
4449                        gint        *position)
4450 {
4451   g_object_ref (editable);
4452
4453   /*
4454    * The incoming text may a password or other secret. We make sure
4455    * not to copy it into temporary buffers.
4456    */
4457
4458   g_signal_emit_by_name (editable, "insert-text", new_text, new_text_length, position);
4459
4460   g_object_unref (editable);
4461 }
4462
4463 static void
4464 gtk_entry_delete_text (GtkEditable *editable,
4465                        gint         start_pos,
4466                        gint         end_pos)
4467 {
4468   g_object_ref (editable);
4469
4470   g_signal_emit_by_name (editable, "delete-text", start_pos, end_pos);
4471
4472   g_object_unref (editable);
4473 }
4474
4475 static gchar *    
4476 gtk_entry_get_chars      (GtkEditable   *editable,
4477                           gint           start_pos,
4478                           gint           end_pos)
4479 {
4480   GtkEntry *entry = GTK_ENTRY (editable);
4481   const gchar *text;
4482   gint text_length;
4483   gint start_index, end_index;
4484
4485   text = gtk_entry_buffer_get_text (get_buffer (entry));
4486   text_length = gtk_entry_buffer_get_length (get_buffer (entry));
4487
4488   if (end_pos < 0)
4489     end_pos = text_length;
4490
4491   start_pos = MIN (text_length, start_pos);
4492   end_pos = MIN (text_length, end_pos);
4493
4494   start_index = g_utf8_offset_to_pointer (text, start_pos) - text;
4495   end_index = g_utf8_offset_to_pointer (text, end_pos) - text;
4496
4497   return g_strndup (text + start_index, end_index - start_index);
4498 }
4499
4500 static void
4501 gtk_entry_real_set_position (GtkEditable *editable,
4502                              gint         position)
4503 {
4504   GtkEntry *entry = GTK_ENTRY (editable);
4505   GtkEntryPrivate *priv = entry->priv;
4506
4507   guint length;
4508
4509   length = gtk_entry_buffer_get_length (get_buffer (entry));
4510   if (position < 0 || position > length)
4511     position = length;
4512
4513   if (position != priv->current_pos ||
4514       position != priv->selection_bound)
4515     {
4516       _gtk_entry_reset_im_context (entry);
4517       gtk_entry_set_positions (entry, position, position);
4518     }
4519 }
4520
4521 static gint
4522 gtk_entry_get_position (GtkEditable *editable)
4523 {
4524   GtkEntry *entry = GTK_ENTRY (editable);
4525   GtkEntryPrivate *priv = entry->priv;
4526
4527   return priv->current_pos;
4528 }
4529
4530 static void
4531 gtk_entry_set_selection_bounds (GtkEditable *editable,
4532                                 gint         start,
4533                                 gint         end)
4534 {
4535   GtkEntry *entry = GTK_ENTRY (editable);
4536   guint length;
4537
4538   length = gtk_entry_buffer_get_length (get_buffer (entry));
4539   if (start < 0)
4540     start = length;
4541   if (end < 0)
4542     end = length;
4543   
4544   _gtk_entry_reset_im_context (entry);
4545
4546   gtk_entry_set_positions (entry,
4547                            MIN (end, length),
4548                            MIN (start, length));
4549
4550   gtk_entry_update_primary_selection (entry);
4551 }
4552
4553 static gboolean
4554 gtk_entry_get_selection_bounds (GtkEditable *editable,
4555                                 gint        *start,
4556                                 gint        *end)
4557 {
4558   GtkEntry *entry = GTK_ENTRY (editable);
4559   GtkEntryPrivate *priv = entry->priv;
4560
4561   *start = priv->selection_bound;
4562   *end = priv->current_pos;
4563
4564   return (priv->selection_bound != priv->current_pos);
4565 }
4566
4567 static void
4568 icon_theme_changed (GtkEntry *entry)
4569 {
4570   GtkEntryPrivate *priv = entry->priv;
4571   gint i;
4572
4573   for (i = 0; i < MAX_ICONS; i++)
4574     {
4575       EntryIconInfo *icon_info = priv->icons[i];
4576       if (icon_info != NULL) 
4577         _gtk_icon_helper_invalidate (icon_info->icon_helper);
4578     }
4579
4580   gtk_widget_queue_draw (GTK_WIDGET (entry));
4581 }
4582
4583 static void
4584 gtk_entry_update_cached_style_values (GtkEntry *entry)
4585 {
4586   GtkEntryPrivate *priv = entry->priv;
4587   gint focus_width;
4588   gboolean interior_focus;
4589
4590   gtk_widget_style_get (GTK_WIDGET (entry),
4591                         "focus-line-width", &focus_width,
4592                         "interior-focus", &interior_focus,
4593                         NULL);
4594   priv->focus_width = focus_width;
4595   priv->interior_focus = interior_focus;
4596
4597   if (!priv->invisible_char_set)
4598     {
4599       gunichar ch = find_invisible_char (GTK_WIDGET (entry));
4600
4601       if (priv->invisible_char != ch)
4602         {
4603           priv->invisible_char = ch;
4604           g_object_notify (G_OBJECT (entry), "invisible-char");
4605         }
4606     }
4607 }
4608
4609 static void 
4610 gtk_entry_style_updated (GtkWidget *widget)
4611 {
4612   GtkEntry *entry = GTK_ENTRY (widget);
4613
4614   GTK_WIDGET_CLASS (gtk_entry_parent_class)->style_updated (widget);
4615
4616   gtk_entry_update_cached_style_values (entry);
4617
4618   gtk_entry_recompute (entry);
4619
4620   icon_theme_changed (entry);
4621 }
4622
4623 /* GtkCellEditable method implementations
4624  */
4625 static void
4626 gtk_cell_editable_entry_activated (GtkEntry *entry, gpointer data)
4627 {
4628   gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (entry));
4629   gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (entry));
4630 }
4631
4632 static gboolean
4633 gtk_cell_editable_key_press_event (GtkEntry    *entry,
4634                                    GdkEventKey *key_event,
4635                                    gpointer     data)
4636 {
4637   GtkEntryPrivate *priv = entry->priv;
4638
4639   if (key_event->keyval == GDK_KEY_Escape)
4640     {
4641       priv->editing_canceled = TRUE;
4642       gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (entry));
4643       gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (entry));
4644
4645       return TRUE;
4646     }
4647
4648   /* override focus */
4649   if (key_event->keyval == GDK_KEY_Up || key_event->keyval == GDK_KEY_Down)
4650     {
4651       gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (entry));
4652       gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (entry));
4653
4654       return TRUE;
4655     }
4656
4657   return FALSE;
4658 }
4659
4660 static void
4661 gtk_entry_start_editing (GtkCellEditable *cell_editable,
4662                          GdkEvent        *event)
4663 {
4664   GtkEntry *entry = GTK_ENTRY (cell_editable);
4665   GtkEntryPrivate *priv = entry->priv;
4666
4667   priv->is_cell_renderer = TRUE;
4668
4669   g_signal_connect (cell_editable, "activate",
4670                     G_CALLBACK (gtk_cell_editable_entry_activated), NULL);
4671   g_signal_connect (cell_editable, "key-press-event",
4672                     G_CALLBACK (gtk_cell_editable_key_press_event), NULL);
4673 }
4674
4675 static void
4676 gtk_entry_password_hint_free (GtkEntryPasswordHint *password_hint)
4677 {
4678   if (password_hint->source_id)
4679     g_source_remove (password_hint->source_id);
4680
4681   g_slice_free (GtkEntryPasswordHint, password_hint);
4682 }
4683
4684
4685 static gboolean
4686 gtk_entry_remove_password_hint (gpointer data)
4687 {
4688   GtkEntryPasswordHint *password_hint = g_object_get_qdata (data, quark_password_hint);
4689   password_hint->position = -1;
4690
4691   /* Force the string to be redrawn, but now without a visible character */
4692   gtk_entry_recompute (GTK_ENTRY (data));
4693   return FALSE;
4694 }
4695
4696 /* Default signal handlers
4697  */
4698 static void
4699 gtk_entry_real_insert_text (GtkEditable *editable,
4700                             const gchar *new_text,
4701                             gint         new_text_length,
4702                             gint        *position)
4703 {
4704   guint n_inserted;
4705   gint n_chars;
4706
4707   n_chars = g_utf8_strlen (new_text, new_text_length);
4708
4709   /*
4710    * The actual insertion into the buffer. This will end up firing the
4711    * following signal handlers: buffer_inserted_text(), buffer_notify_display_text(),
4712    * buffer_notify_text(), buffer_notify_length()
4713    */
4714   begin_change (GTK_ENTRY (editable));
4715
4716   n_inserted = gtk_entry_buffer_insert_text (get_buffer (GTK_ENTRY (editable)), *position, new_text, n_chars);
4717
4718   end_change (GTK_ENTRY (editable));
4719
4720   if (n_inserted != n_chars)
4721       gtk_widget_error_bell (GTK_WIDGET (editable));
4722
4723   *position += n_inserted;
4724 }
4725
4726 static void
4727 gtk_entry_real_delete_text (GtkEditable *editable,
4728                             gint         start_pos,
4729                             gint         end_pos)
4730 {
4731   /*
4732    * The actual deletion from the buffer. This will end up firing the
4733    * following signal handlers: buffer_deleted_text(), buffer_notify_display_text(),
4734    * buffer_notify_text(), buffer_notify_length()
4735    */
4736
4737   begin_change (GTK_ENTRY (editable));
4738
4739   gtk_entry_buffer_delete_text (get_buffer (GTK_ENTRY (editable)), start_pos, end_pos - start_pos);
4740
4741   end_change (GTK_ENTRY (editable));
4742 }
4743
4744 /* GtkEntryBuffer signal handlers
4745  */
4746 static void
4747 buffer_inserted_text (GtkEntryBuffer *buffer,
4748                       guint           position,
4749                       const gchar    *chars,
4750                       guint           n_chars,
4751                       GtkEntry       *entry)
4752 {
4753   GtkEntryPrivate *priv = entry->priv;
4754   guint password_hint_timeout;
4755   guint current_pos;
4756   gint selection_bound;
4757
4758   current_pos = priv->current_pos;
4759   if (current_pos > position)
4760     current_pos += n_chars;
4761
4762   selection_bound = priv->selection_bound;
4763   if (selection_bound > position)
4764     selection_bound += n_chars;
4765
4766   gtk_entry_set_positions (entry, current_pos, selection_bound);
4767
4768   /* Calculate the password hint if it needs to be displayed. */
4769   if (n_chars == 1 && !priv->visible)
4770     {
4771       g_object_get (gtk_widget_get_settings (GTK_WIDGET (entry)),
4772                     "gtk-entry-password-hint-timeout", &password_hint_timeout,
4773                     NULL);
4774
4775       if (password_hint_timeout > 0)
4776         {
4777           GtkEntryPasswordHint *password_hint = g_object_get_qdata (G_OBJECT (entry),
4778                                                                     quark_password_hint);
4779           if (!password_hint)
4780             {
4781               password_hint = g_slice_new0 (GtkEntryPasswordHint);
4782               g_object_set_qdata_full (G_OBJECT (entry), quark_password_hint, password_hint,
4783                                        (GDestroyNotify)gtk_entry_password_hint_free);
4784             }
4785
4786           password_hint->position = position;
4787           if (password_hint->source_id)
4788             g_source_remove (password_hint->source_id);
4789           password_hint->source_id = gdk_threads_add_timeout (password_hint_timeout,
4790                                                               (GSourceFunc)gtk_entry_remove_password_hint, entry);
4791         }
4792     }
4793 }
4794
4795 static void
4796 buffer_deleted_text (GtkEntryBuffer *buffer,
4797                      guint           position,
4798                      guint           n_chars,
4799                      GtkEntry       *entry)
4800 {
4801   GtkEntryPrivate *priv = entry->priv;
4802   guint end_pos = position + n_chars;
4803   gint selection_bound;
4804   guint current_pos;
4805
4806   current_pos = priv->current_pos;
4807   if (current_pos > position)
4808     current_pos -= MIN (current_pos, end_pos) - position;
4809
4810   selection_bound = priv->selection_bound;
4811   if (selection_bound > position)
4812     selection_bound -= MIN (selection_bound, end_pos) - position;
4813
4814   gtk_entry_set_positions (entry, current_pos, selection_bound);
4815
4816   /* We might have deleted the selection */
4817   gtk_entry_update_primary_selection (entry);
4818
4819   /* Disable the password hint if one exists. */
4820   if (!priv->visible)
4821     {
4822       GtkEntryPasswordHint *password_hint = g_object_get_qdata (G_OBJECT (entry),
4823                                                                 quark_password_hint);
4824       if (password_hint)
4825         {
4826           if (password_hint->source_id)
4827             g_source_remove (password_hint->source_id);
4828           password_hint->source_id = 0;
4829           password_hint->position = -1;
4830         }
4831     }
4832 }
4833
4834 static void
4835 buffer_notify_text (GtkEntryBuffer *buffer,
4836                     GParamSpec     *spec,
4837                     GtkEntry       *entry)
4838 {
4839   gtk_entry_recompute (entry);
4840   emit_changed (entry);
4841   g_object_notify (G_OBJECT (entry), "text");
4842 }
4843
4844 static void
4845 buffer_notify_length (GtkEntryBuffer *buffer,
4846                       GParamSpec     *spec,
4847                       GtkEntry       *entry)
4848 {
4849   g_object_notify (G_OBJECT (entry), "text-length");
4850 }
4851
4852 static void
4853 buffer_notify_max_length (GtkEntryBuffer *buffer,
4854                           GParamSpec     *spec,
4855                           GtkEntry       *entry)
4856 {
4857   g_object_notify (G_OBJECT (entry), "max-length");
4858 }
4859
4860 static void
4861 buffer_connect_signals (GtkEntry *entry)
4862 {
4863   g_signal_connect (get_buffer (entry), "inserted-text", G_CALLBACK (buffer_inserted_text), entry);
4864   g_signal_connect (get_buffer (entry), "deleted-text", G_CALLBACK (buffer_deleted_text), entry);
4865   g_signal_connect (get_buffer (entry), "notify::text", G_CALLBACK (buffer_notify_text), entry);
4866   g_signal_connect (get_buffer (entry), "notify::length", G_CALLBACK (buffer_notify_length), entry);
4867   g_signal_connect (get_buffer (entry), "notify::max-length", G_CALLBACK (buffer_notify_max_length), entry);
4868 }
4869
4870 static void
4871 buffer_disconnect_signals (GtkEntry *entry)
4872 {
4873   g_signal_handlers_disconnect_by_func (get_buffer (entry), buffer_inserted_text, entry);
4874   g_signal_handlers_disconnect_by_func (get_buffer (entry), buffer_deleted_text, entry);
4875   g_signal_handlers_disconnect_by_func (get_buffer (entry), buffer_notify_text, entry);
4876   g_signal_handlers_disconnect_by_func (get_buffer (entry), buffer_notify_length, entry);
4877   g_signal_handlers_disconnect_by_func (get_buffer (entry), buffer_notify_max_length, entry);
4878 }
4879
4880 /* Compute the X position for an offset that corresponds to the "more important
4881  * cursor position for that offset. We use this when trying to guess to which
4882  * end of the selection we should go to when the user hits the left or
4883  * right arrow key.
4884  */
4885 static gint
4886 get_better_cursor_x (GtkEntry *entry,
4887                      gint      offset)
4888 {
4889   GtkEntryPrivate *priv = entry->priv;
4890   GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (entry)));
4891   PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
4892   gboolean split_cursor;
4893   
4894   PangoLayout *layout = gtk_entry_ensure_layout (entry, TRUE);
4895   const gchar *text = pango_layout_get_text (layout);
4896   gint index = g_utf8_offset_to_pointer (text, offset) - text;
4897   
4898   PangoRectangle strong_pos, weak_pos;
4899   
4900   g_object_get (gtk_widget_get_settings (GTK_WIDGET (entry)),
4901                 "gtk-split-cursor", &split_cursor,
4902                 NULL);
4903
4904   pango_layout_get_cursor_pos (layout, index, &strong_pos, &weak_pos);
4905
4906   if (split_cursor)
4907     return strong_pos.x / PANGO_SCALE;
4908   else
4909     return (keymap_direction == priv->resolved_dir) ? strong_pos.x / PANGO_SCALE : weak_pos.x / PANGO_SCALE;
4910 }
4911
4912 static void
4913 gtk_entry_move_cursor (GtkEntry       *entry,
4914                        GtkMovementStep step,
4915                        gint            count,
4916                        gboolean        extend_selection)
4917 {
4918   GtkEntryPrivate *priv = entry->priv;
4919   gint new_pos = priv->current_pos;
4920
4921   _gtk_entry_reset_im_context (entry);
4922
4923   if (priv->current_pos != priv->selection_bound && !extend_selection)
4924     {
4925       /* If we have a current selection and aren't extending it, move to the
4926        * start/or end of the selection as appropriate
4927        */
4928       switch (step)
4929         {
4930         case GTK_MOVEMENT_VISUAL_POSITIONS:
4931           {
4932             gint current_x = get_better_cursor_x (entry, priv->current_pos);
4933             gint bound_x = get_better_cursor_x (entry, priv->selection_bound);
4934
4935             if (count <= 0)
4936               new_pos = current_x < bound_x ? priv->current_pos : priv->selection_bound;
4937             else 
4938               new_pos = current_x > bound_x ? priv->current_pos : priv->selection_bound;
4939             break;
4940           }
4941         case GTK_MOVEMENT_LOGICAL_POSITIONS:
4942         case GTK_MOVEMENT_WORDS:
4943           if (count < 0)
4944             new_pos = MIN (priv->current_pos, priv->selection_bound);
4945           else
4946             new_pos = MAX (priv->current_pos, priv->selection_bound);
4947           break;
4948         case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
4949         case GTK_MOVEMENT_PARAGRAPH_ENDS:
4950         case GTK_MOVEMENT_BUFFER_ENDS:
4951           new_pos = count < 0 ? 0 : gtk_entry_buffer_get_length (get_buffer (entry));
4952           break;
4953         case GTK_MOVEMENT_DISPLAY_LINES:
4954         case GTK_MOVEMENT_PARAGRAPHS:
4955         case GTK_MOVEMENT_PAGES:
4956         case GTK_MOVEMENT_HORIZONTAL_PAGES:
4957           break;
4958         }
4959     }
4960   else
4961     {
4962       switch (step)
4963         {
4964         case GTK_MOVEMENT_LOGICAL_POSITIONS:
4965           new_pos = gtk_entry_move_logically (entry, new_pos, count);
4966           break;
4967         case GTK_MOVEMENT_VISUAL_POSITIONS:
4968           new_pos = gtk_entry_move_visually (entry, new_pos, count);
4969           if (priv->current_pos == new_pos)
4970             {
4971               if (!extend_selection)
4972                 {
4973                   if (!gtk_widget_keynav_failed (GTK_WIDGET (entry),
4974                                                  count > 0 ?
4975                                                  GTK_DIR_RIGHT : GTK_DIR_LEFT))
4976                     {
4977                       GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (entry));
4978
4979                       if (toplevel)
4980                         gtk_widget_child_focus (toplevel,
4981                                                 count > 0 ?
4982                                                 GTK_DIR_RIGHT : GTK_DIR_LEFT);
4983                     }
4984                 }
4985               else
4986                 {
4987                   gtk_widget_error_bell (GTK_WIDGET (entry));
4988                 }
4989             }
4990           break;
4991         case GTK_MOVEMENT_WORDS:
4992           while (count > 0)
4993             {
4994               new_pos = gtk_entry_move_forward_word (entry, new_pos, FALSE);
4995               count--;
4996             }
4997           while (count < 0)
4998             {
4999               new_pos = gtk_entry_move_backward_word (entry, new_pos, FALSE);
5000               count++;
5001             }
5002           if (priv->current_pos == new_pos)
5003             gtk_widget_error_bell (GTK_WIDGET (entry));
5004           break;
5005         case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
5006         case GTK_MOVEMENT_PARAGRAPH_ENDS:
5007         case GTK_MOVEMENT_BUFFER_ENDS:
5008           new_pos = count < 0 ? 0 : gtk_entry_buffer_get_length (get_buffer (entry));
5009           if (priv->current_pos == new_pos)
5010             gtk_widget_error_bell (GTK_WIDGET (entry));
5011           break;
5012         case GTK_MOVEMENT_DISPLAY_LINES:
5013         case GTK_MOVEMENT_PARAGRAPHS:
5014         case GTK_MOVEMENT_PAGES:
5015         case GTK_MOVEMENT_HORIZONTAL_PAGES:
5016           break;
5017         }
5018     }
5019
5020   if (extend_selection)
5021     gtk_editable_select_region (GTK_EDITABLE (entry), priv->selection_bound, new_pos);
5022   else
5023     gtk_editable_set_position (GTK_EDITABLE (entry), new_pos);
5024   
5025   gtk_entry_pend_cursor_blink (entry);
5026 }
5027
5028 static void
5029 gtk_entry_insert_at_cursor (GtkEntry    *entry,
5030                             const gchar *str)
5031 {
5032   GtkEntryPrivate *priv = entry->priv;
5033   GtkEditable *editable = GTK_EDITABLE (entry);
5034   gint pos = priv->current_pos;
5035
5036   if (priv->editable)
5037     {
5038       _gtk_entry_reset_im_context (entry);
5039
5040       gtk_editable_insert_text (editable, str, -1, &pos);
5041       gtk_editable_set_position (editable, pos);
5042     }
5043 }
5044
5045 static void
5046 gtk_entry_delete_from_cursor (GtkEntry       *entry,
5047                               GtkDeleteType   type,
5048                               gint            count)
5049 {
5050   GtkEntryPrivate *priv = entry->priv;
5051   GtkEditable *editable = GTK_EDITABLE (entry);
5052   gint start_pos = priv->current_pos;
5053   gint end_pos = priv->current_pos;
5054   gint old_n_bytes = gtk_entry_buffer_get_bytes (get_buffer (entry));
5055   
5056   _gtk_entry_reset_im_context (entry);
5057
5058   if (!priv->editable)
5059     {
5060       gtk_widget_error_bell (GTK_WIDGET (entry));
5061       return;
5062     }
5063
5064   if (priv->selection_bound != priv->current_pos)
5065     {
5066       gtk_editable_delete_selection (editable);
5067       return;
5068     }
5069   
5070   switch (type)
5071     {
5072     case GTK_DELETE_CHARS:
5073       end_pos = gtk_entry_move_logically (entry, priv->current_pos, count);
5074       gtk_editable_delete_text (editable, MIN (start_pos, end_pos), MAX (start_pos, end_pos));
5075       break;
5076     case GTK_DELETE_WORDS:
5077       if (count < 0)
5078         {
5079           /* Move to end of current word, or if not on a word, end of previous word */
5080           end_pos = gtk_entry_move_backward_word (entry, end_pos, FALSE);
5081           end_pos = gtk_entry_move_forward_word (entry, end_pos, FALSE);
5082         }
5083       else if (count > 0)
5084         {
5085           /* Move to beginning of current word, or if not on a word, begining of next word */
5086           start_pos = gtk_entry_move_forward_word (entry, start_pos, FALSE);
5087           start_pos = gtk_entry_move_backward_word (entry, start_pos, FALSE);
5088         }
5089         
5090       /* Fall through */
5091     case GTK_DELETE_WORD_ENDS:
5092       while (count < 0)
5093         {
5094           start_pos = gtk_entry_move_backward_word (entry, start_pos, FALSE);
5095           count++;
5096         }
5097       while (count > 0)
5098         {
5099           end_pos = gtk_entry_move_forward_word (entry, end_pos, FALSE);
5100           count--;
5101         }
5102       gtk_editable_delete_text (editable, start_pos, end_pos);
5103       break;
5104     case GTK_DELETE_DISPLAY_LINE_ENDS:
5105     case GTK_DELETE_PARAGRAPH_ENDS:
5106       if (count < 0)
5107         gtk_editable_delete_text (editable, 0, priv->current_pos);
5108       else
5109         gtk_editable_delete_text (editable, priv->current_pos, -1);
5110       break;
5111     case GTK_DELETE_DISPLAY_LINES:
5112     case GTK_DELETE_PARAGRAPHS:
5113       gtk_editable_delete_text (editable, 0, -1);  
5114       break;
5115     case GTK_DELETE_WHITESPACE:
5116       gtk_entry_delete_whitespace (entry);
5117       break;
5118     }
5119
5120   if (gtk_entry_buffer_get_bytes (get_buffer (entry)) == old_n_bytes)
5121     gtk_widget_error_bell (GTK_WIDGET (entry));
5122
5123   gtk_entry_pend_cursor_blink (entry);
5124 }
5125
5126 static void
5127 gtk_entry_backspace (GtkEntry *entry)
5128 {
5129   GtkEntryPrivate *priv = entry->priv;
5130   GtkEditable *editable = GTK_EDITABLE (entry);
5131   gint prev_pos;
5132
5133   _gtk_entry_reset_im_context (entry);
5134
5135   if (!priv->editable)
5136     {
5137       gtk_widget_error_bell (GTK_WIDGET (entry));
5138       return;
5139     }
5140
5141   if (priv->selection_bound != priv->current_pos)
5142     {
5143       gtk_editable_delete_selection (editable);
5144       return;
5145     }
5146
5147   prev_pos = gtk_entry_move_logically (entry, priv->current_pos, -1);
5148
5149   if (prev_pos < priv->current_pos)
5150     {
5151       PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
5152       PangoLogAttr *log_attrs;
5153       gint n_attrs;
5154
5155       pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
5156
5157       /* Deleting parts of characters */
5158       if (log_attrs[priv->current_pos].backspace_deletes_character)
5159         {
5160           gchar *cluster_text;
5161           gchar *normalized_text;
5162           glong  len;
5163
5164           cluster_text = gtk_entry_get_display_text (entry, prev_pos,
5165                                                      priv->current_pos);
5166           normalized_text = g_utf8_normalize (cluster_text,
5167                                               strlen (cluster_text),
5168                                               G_NORMALIZE_NFD);
5169           len = g_utf8_strlen (normalized_text, -1);
5170
5171           gtk_editable_delete_text (editable, prev_pos, priv->current_pos);
5172           if (len > 1)
5173             {
5174               gint pos = priv->current_pos;
5175
5176               gtk_editable_insert_text (editable, normalized_text,
5177                                         g_utf8_offset_to_pointer (normalized_text, len - 1) - normalized_text,
5178                                         &pos);
5179               gtk_editable_set_position (editable, pos);
5180             }
5181
5182           g_free (normalized_text);
5183           g_free (cluster_text);
5184         }
5185       else
5186         {
5187           gtk_editable_delete_text (editable, prev_pos, priv->current_pos);
5188         }
5189       
5190       g_free (log_attrs);
5191     }
5192   else
5193     {
5194       gtk_widget_error_bell (GTK_WIDGET (entry));
5195     }
5196
5197   gtk_entry_pend_cursor_blink (entry);
5198 }
5199
5200 static void
5201 gtk_entry_copy_clipboard (GtkEntry *entry)
5202 {
5203   GtkEntryPrivate *priv = entry->priv;
5204   GtkEditable *editable = GTK_EDITABLE (entry);
5205   gint start, end;
5206   gchar *str;
5207
5208   if (gtk_editable_get_selection_bounds (editable, &start, &end))
5209     {
5210       if (!priv->visible)
5211         {
5212           gtk_widget_error_bell (GTK_WIDGET (entry));
5213           return;
5214         }
5215
5216       str = gtk_entry_get_display_text (entry, start, end);
5217       gtk_clipboard_set_text (gtk_widget_get_clipboard (GTK_WIDGET (entry),
5218                                                         GDK_SELECTION_CLIPBOARD),
5219                               str, -1);
5220       g_free (str);
5221     }
5222 }
5223
5224 static void
5225 gtk_entry_cut_clipboard (GtkEntry *entry)
5226 {
5227   GtkEntryPrivate *priv = entry->priv;
5228   GtkEditable *editable = GTK_EDITABLE (entry);
5229   gint start, end;
5230
5231   if (!priv->visible)
5232     {
5233       gtk_widget_error_bell (GTK_WIDGET (entry));
5234       return;
5235     }
5236
5237   gtk_entry_copy_clipboard (entry);
5238
5239   if (priv->editable)
5240     {
5241       if (gtk_editable_get_selection_bounds (editable, &start, &end))
5242         gtk_editable_delete_text (editable, start, end);
5243     }
5244   else
5245     {
5246       gtk_widget_error_bell (GTK_WIDGET (entry));
5247     }
5248 }
5249
5250 static void
5251 gtk_entry_paste_clipboard (GtkEntry *entry)
5252 {
5253   GtkEntryPrivate *priv = entry->priv;
5254
5255   if (priv->editable)
5256     gtk_entry_paste (entry, GDK_SELECTION_CLIPBOARD);
5257   else
5258     gtk_widget_error_bell (GTK_WIDGET (entry));
5259 }
5260
5261 static void
5262 gtk_entry_delete_cb (GtkEntry *entry)
5263 {
5264   GtkEntryPrivate *priv = entry->priv;
5265   GtkEditable *editable = GTK_EDITABLE (entry);
5266   gint start, end;
5267
5268   if (priv->editable)
5269     {
5270       if (gtk_editable_get_selection_bounds (editable, &start, &end))
5271         gtk_editable_delete_text (editable, start, end);
5272     }
5273 }
5274
5275 static void
5276 gtk_entry_toggle_overwrite (GtkEntry *entry)
5277 {
5278   GtkEntryPrivate *priv = entry->priv;
5279
5280   priv->overwrite_mode = !priv->overwrite_mode;
5281   gtk_entry_pend_cursor_blink (entry);
5282   gtk_widget_queue_draw (GTK_WIDGET (entry));
5283 }
5284
5285 static void
5286 gtk_entry_select_all (GtkEntry *entry)
5287 {
5288   gtk_entry_select_line (entry);
5289 }
5290
5291 static void
5292 gtk_entry_real_activate (GtkEntry *entry)
5293 {
5294   GtkEntryPrivate *priv = entry->priv;
5295   GtkWindow *window;
5296   GtkWidget *default_widget, *focus_widget;
5297   GtkWidget *toplevel;
5298   GtkWidget *widget;
5299
5300   widget = GTK_WIDGET (entry);
5301
5302   if (priv->activates_default)
5303     {
5304       toplevel = gtk_widget_get_toplevel (widget);
5305       if (GTK_IS_WINDOW (toplevel))
5306         {
5307           window = GTK_WINDOW (toplevel);
5308
5309           if (window)
5310             {
5311               default_widget = gtk_window_get_default_widget (window);
5312               focus_widget = gtk_window_get_focus (window);
5313               if (widget != default_widget &&
5314                   !(widget == focus_widget && (!default_widget || !gtk_widget_get_sensitive (default_widget))))
5315                 gtk_window_activate_default (window);
5316             }
5317         }
5318     }
5319 }
5320
5321 static void
5322 keymap_direction_changed (GdkKeymap *keymap,
5323                           GtkEntry  *entry)
5324 {
5325   gtk_entry_recompute (entry);
5326 }
5327
5328 /* IM Context Callbacks
5329  */
5330
5331 static void
5332 gtk_entry_commit_cb (GtkIMContext *context,
5333                      const gchar  *str,
5334                      GtkEntry     *entry)
5335 {
5336   GtkEntryPrivate *priv = entry->priv;
5337
5338   if (priv->editable)
5339     gtk_entry_enter_text (entry, str);
5340 }
5341
5342 static void 
5343 gtk_entry_preedit_changed_cb (GtkIMContext *context,
5344                               GtkEntry     *entry)
5345 {
5346   GtkEntryPrivate *priv = entry->priv;
5347
5348   if (priv->editable)
5349     {
5350       gchar *preedit_string;
5351       gint cursor_pos;
5352
5353       gtk_im_context_get_preedit_string (priv->im_context,
5354                                          &preedit_string, NULL,
5355                                          &cursor_pos);
5356       g_signal_emit (entry, signals[PREEDIT_CHANGED], 0, preedit_string);
5357       priv->preedit_length = strlen (preedit_string);
5358       cursor_pos = CLAMP (cursor_pos, 0, g_utf8_strlen (preedit_string, -1));
5359       priv->preedit_cursor = cursor_pos;
5360       g_free (preedit_string);
5361     
5362       gtk_entry_recompute (entry);
5363     }
5364 }
5365
5366 static gboolean
5367 gtk_entry_retrieve_surrounding_cb (GtkIMContext *context,
5368                                    GtkEntry     *entry)
5369 {
5370   GtkEntryPrivate *priv = entry->priv;
5371   gchar *text;
5372
5373   /* XXXX ??? does this even make sense when text is not visible? Should we return FALSE? */
5374   text = gtk_entry_get_display_text (entry, 0, -1);
5375   gtk_im_context_set_surrounding (context, text, strlen (text), /* Length in bytes */
5376                                   g_utf8_offset_to_pointer (text, priv->current_pos) - text);
5377   g_free (text);
5378
5379   return TRUE;
5380 }
5381
5382 static gboolean
5383 gtk_entry_delete_surrounding_cb (GtkIMContext *slave,
5384                                  gint          offset,
5385                                  gint          n_chars,
5386                                  GtkEntry     *entry)
5387 {
5388   GtkEntryPrivate *priv = entry->priv;
5389
5390   if (priv->editable)
5391     gtk_editable_delete_text (GTK_EDITABLE (entry),
5392                               priv->current_pos + offset,
5393                               priv->current_pos + offset + n_chars);
5394
5395   return TRUE;
5396 }
5397
5398 /* Internal functions
5399  */
5400
5401 /* Used for im_commit_cb and inserting Unicode chars */
5402 static void
5403 gtk_entry_enter_text (GtkEntry       *entry,
5404                       const gchar    *str)
5405 {
5406   GtkEntryPrivate *priv = entry->priv;
5407   GtkEditable *editable = GTK_EDITABLE (entry);
5408   gint tmp_pos;
5409   gboolean old_need_im_reset;
5410
5411   old_need_im_reset = priv->need_im_reset;
5412   priv->need_im_reset = FALSE;
5413
5414   if (gtk_editable_get_selection_bounds (editable, NULL, NULL))
5415     gtk_editable_delete_selection (editable);
5416   else
5417     {
5418       if (priv->overwrite_mode)
5419         gtk_entry_delete_from_cursor (entry, GTK_DELETE_CHARS, 1);
5420     }
5421
5422   tmp_pos = priv->current_pos;
5423   gtk_editable_insert_text (editable, str, strlen (str), &tmp_pos);
5424   gtk_editable_set_position (editable, tmp_pos);
5425
5426   priv->need_im_reset = old_need_im_reset;
5427 }
5428
5429 /* All changes to priv->current_pos and priv->selection_bound
5430  * should go through this function.
5431  */
5432 static void
5433 gtk_entry_set_positions (GtkEntry *entry,
5434                          gint      current_pos,
5435                          gint      selection_bound)
5436 {
5437   GtkEntryPrivate *priv = entry->priv;
5438   gboolean changed = FALSE;
5439
5440   g_object_freeze_notify (G_OBJECT (entry));
5441   
5442   if (current_pos != -1 &&
5443       priv->current_pos != current_pos)
5444     {
5445       priv->current_pos = current_pos;
5446       changed = TRUE;
5447
5448       g_object_notify (G_OBJECT (entry), "cursor-position");
5449     }
5450
5451   if (selection_bound != -1 &&
5452       priv->selection_bound != selection_bound)
5453     {
5454       priv->selection_bound = selection_bound;
5455       changed = TRUE;
5456       
5457       g_object_notify (G_OBJECT (entry), "selection-bound");
5458     }
5459
5460   g_object_thaw_notify (G_OBJECT (entry));
5461
5462   if (changed) 
5463     {
5464       gtk_entry_move_adjustments (entry);
5465       gtk_entry_recompute (entry);
5466     }
5467 }
5468
5469 static void
5470 gtk_entry_reset_layout (GtkEntry *entry)
5471 {
5472   GtkEntryPrivate *priv = entry->priv;
5473
5474   if (priv->cached_layout)
5475     {
5476       g_object_unref (priv->cached_layout);
5477       priv->cached_layout = NULL;
5478     }
5479 }
5480
5481 static void
5482 update_im_cursor_location (GtkEntry *entry)
5483 {
5484   GtkEntryPrivate *priv = entry->priv;
5485   GdkRectangle area;
5486   gint strong_x;
5487   gint strong_xoffset;
5488   gint area_width, area_height;
5489
5490   gtk_entry_get_cursor_locations (entry, CURSOR_STANDARD, &strong_x, NULL);
5491   gtk_entry_get_text_area_size (entry, NULL, NULL, &area_width, &area_height);
5492
5493   strong_xoffset = strong_x - priv->scroll_offset;
5494   if (strong_xoffset < 0)
5495     {
5496       strong_xoffset = 0;
5497     }
5498   else if (strong_xoffset > area_width)
5499     {
5500       strong_xoffset = area_width;
5501     }
5502   area.x = strong_xoffset;
5503   area.y = 0;
5504   area.width = 0;
5505   area.height = area_height;
5506
5507   gtk_im_context_set_cursor_location (priv->im_context, &area);
5508 }
5509
5510 static gboolean
5511 recompute_idle_func (gpointer data)
5512 {
5513   GtkEntry *entry = GTK_ENTRY (data);
5514   GtkEntryPrivate *priv = entry->priv;
5515
5516   priv->recompute_idle = 0;
5517
5518   if (gtk_widget_has_screen (GTK_WIDGET (entry)))
5519     {
5520       gtk_entry_adjust_scroll (entry);
5521       gtk_widget_queue_draw (GTK_WIDGET (entry));
5522
5523       update_im_cursor_location (entry);
5524     }
5525
5526   return FALSE;
5527 }
5528
5529 static void
5530 gtk_entry_recompute (GtkEntry *entry)
5531 {
5532   GtkEntryPrivate *priv = entry->priv;
5533
5534   gtk_entry_reset_layout (entry);
5535   gtk_entry_check_cursor_blink (entry);
5536
5537   if (!priv->recompute_idle)
5538     {
5539       priv->recompute_idle = gdk_threads_add_idle_full (G_PRIORITY_HIGH_IDLE + 15, /* between resize and redraw */
5540                                                recompute_idle_func, entry, NULL); 
5541     }
5542 }
5543
5544 static void
5545 gtk_entry_get_placeholder_text_color (GtkEntry   *entry,
5546                                       PangoColor *color)
5547 {
5548   GtkWidget *widget = GTK_WIDGET (entry);
5549   GtkStyleContext *context;
5550   GdkRGBA fg = { 0.5, 0.5, 0.5 };
5551
5552   context = gtk_widget_get_style_context (widget);
5553   gtk_style_context_lookup_color (context, "placeholder_text_color", &fg);
5554
5555   color->red = CLAMP (fg.red * 65535. + 0.5, 0, 65535);
5556   color->green = CLAMP (fg.green * 65535. + 0.5, 0, 65535);
5557   color->blue = CLAMP (fg.blue * 65535. + 0.5, 0, 65535);
5558 }
5559
5560 static inline gboolean
5561 show_placeholder_text (GtkEntry *entry)
5562 {
5563   GtkEntryPrivate *priv = entry->priv;
5564
5565   if (!gtk_widget_has_focus (GTK_WIDGET (entry)) &&
5566       gtk_entry_buffer_get_bytes (get_buffer (entry)) == 0 &&
5567       priv->placeholder_text != NULL)
5568     return TRUE;
5569
5570   return FALSE;
5571 }
5572
5573 static PangoLayout *
5574 gtk_entry_create_layout (GtkEntry *entry,
5575                          gboolean  include_preedit)
5576 {
5577   GtkEntryPrivate *priv = entry->priv;
5578   GtkWidget *widget = GTK_WIDGET (entry);
5579   PangoLayout *layout = gtk_widget_create_pango_layout (widget, NULL);
5580   PangoAttrList *tmp_attrs = pango_attr_list_new ();
5581   gboolean placeholder_layout = show_placeholder_text (entry);
5582
5583   gchar *preedit_string = NULL;
5584   gint preedit_length = 0;
5585   PangoAttrList *preedit_attrs = NULL;
5586
5587   gchar *display;
5588   guint n_bytes;
5589
5590   pango_layout_set_single_paragraph_mode (layout, TRUE);
5591
5592   display = placeholder_layout ? g_strdup (priv->placeholder_text) : gtk_entry_get_display_text (entry, 0, -1);
5593   n_bytes = strlen (display);
5594
5595   if (!placeholder_layout && include_preedit)
5596     {
5597       gtk_im_context_get_preedit_string (priv->im_context,
5598                                          &preedit_string, &preedit_attrs, NULL);
5599       preedit_length = priv->preedit_length;
5600     }
5601   else if (placeholder_layout)
5602     {
5603       PangoColor color;
5604       PangoAttribute *attr;
5605
5606       gtk_entry_get_placeholder_text_color (entry, &color);
5607       attr = pango_attr_foreground_new (color.red, color.green, color.blue);
5608       attr->start_index = 0;
5609       attr->end_index = G_MAXINT;
5610       pango_attr_list_insert (tmp_attrs, attr);
5611     }
5612
5613   if (preedit_length)
5614     {
5615       GString *tmp_string = g_string_new (display);
5616       gint cursor_index = g_utf8_offset_to_pointer (display, priv->current_pos) - display;
5617
5618       g_string_insert (tmp_string, cursor_index, preedit_string);
5619       
5620       pango_layout_set_text (layout, tmp_string->str, tmp_string->len);
5621       
5622       pango_attr_list_splice (tmp_attrs, preedit_attrs,
5623                               cursor_index, preedit_length);
5624       
5625       g_string_free (tmp_string, TRUE);
5626     }
5627   else
5628     {
5629       PangoDirection pango_dir;
5630       
5631       if (gtk_entry_get_display_mode (entry) == DISPLAY_NORMAL)
5632         pango_dir = pango_find_base_dir (display, n_bytes);
5633       else
5634         pango_dir = PANGO_DIRECTION_NEUTRAL;
5635
5636       if (pango_dir == PANGO_DIRECTION_NEUTRAL)
5637         {
5638           if (gtk_widget_has_focus (widget))
5639             {
5640               GdkDisplay *display = gtk_widget_get_display (widget);
5641               GdkKeymap *keymap = gdk_keymap_get_for_display (display);
5642               if (gdk_keymap_get_direction (keymap) == PANGO_DIRECTION_RTL)
5643                 pango_dir = PANGO_DIRECTION_RTL;
5644               else
5645                 pango_dir = PANGO_DIRECTION_LTR;
5646             }
5647           else
5648             {
5649               if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
5650                 pango_dir = PANGO_DIRECTION_RTL;
5651               else
5652                 pango_dir = PANGO_DIRECTION_LTR;
5653             }
5654         }
5655
5656       pango_context_set_base_dir (gtk_widget_get_pango_context (widget),
5657                                   pango_dir);
5658
5659       priv->resolved_dir = pango_dir;
5660
5661       pango_layout_set_text (layout, display, n_bytes);
5662     }
5663       
5664   pango_layout_set_attributes (layout, tmp_attrs);
5665
5666   g_free (preedit_string);
5667   g_free (display);
5668
5669   if (preedit_attrs)
5670     pango_attr_list_unref (preedit_attrs);
5671       
5672   pango_attr_list_unref (tmp_attrs);
5673
5674   return layout;
5675 }
5676
5677 static PangoLayout *
5678 gtk_entry_ensure_layout (GtkEntry *entry,
5679                          gboolean  include_preedit)
5680 {
5681   GtkEntryPrivate *priv = entry->priv;
5682
5683   if (priv->preedit_length > 0 &&
5684       !include_preedit != !priv->cache_includes_preedit)
5685     gtk_entry_reset_layout (entry);
5686
5687   if (!priv->cached_layout)
5688     {
5689       priv->cached_layout = gtk_entry_create_layout (entry, include_preedit);
5690       priv->cache_includes_preedit = include_preedit;
5691     }
5692
5693   return priv->cached_layout;
5694 }
5695
5696 static void
5697 get_layout_position (GtkEntry *entry,
5698                      gint     *x,
5699                      gint     *y)
5700 {
5701   GtkEntryPrivate *priv = entry->priv;
5702   PangoLayout *layout;
5703   PangoRectangle logical_rect;
5704   gint area_width, area_height;
5705   gint y_pos;
5706   PangoLayoutLine *line;
5707   
5708   layout = gtk_entry_ensure_layout (entry, TRUE);
5709
5710   gtk_entry_get_text_area_size (entry, NULL, NULL, &area_width, &area_height);
5711   area_height = PANGO_SCALE * area_height;
5712
5713   line = pango_layout_get_lines_readonly (layout)->data;
5714   pango_layout_line_get_extents (line, NULL, &logical_rect);
5715   
5716   /* Align primarily for locale's ascent/descent */
5717   y_pos = ((area_height - priv->ascent - priv->descent) / 2 +
5718            priv->ascent + logical_rect.y);
5719
5720   /* Now see if we need to adjust to fit in actual drawn string */
5721   if (logical_rect.height > area_height)
5722     y_pos = (area_height - logical_rect.height) / 2;
5723   else if (y_pos < 0)
5724     y_pos = 0;
5725   else if (y_pos + logical_rect.height > area_height)
5726     y_pos = area_height - logical_rect.height;
5727   
5728   y_pos = y_pos / PANGO_SCALE;
5729
5730   if (x)
5731     *x = - priv->scroll_offset;
5732
5733   if (y)
5734     *y = y_pos;
5735 }
5736
5737 static void
5738 draw_text_with_color (GtkEntry *entry,
5739                       cairo_t  *cr,
5740                       GdkRGBA  *default_color)
5741 {
5742   GtkEntryPrivate *priv = entry->priv;
5743   PangoLayout *layout = gtk_entry_ensure_layout (entry, TRUE);
5744   GtkWidget *widget;
5745   gint x, y;
5746   gint start_pos, end_pos;
5747
5748   widget = GTK_WIDGET (entry);
5749
5750   cairo_save (cr);
5751
5752   get_layout_position (entry, &x, &y);
5753
5754   cairo_move_to (cr, x, y);
5755   gdk_cairo_set_source_rgba (cr, default_color);
5756   pango_cairo_show_layout (cr, layout);
5757
5758   if (gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &start_pos, &end_pos))
5759     {
5760       gint *ranges;
5761       gint n_ranges, i;
5762       PangoRectangle logical_rect;
5763       GdkRGBA selection_color, text_color;
5764       GtkStyleContext *context;
5765       GtkStateFlags state;
5766
5767       context = gtk_widget_get_style_context (widget);
5768       pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
5769       gtk_entry_get_pixel_ranges (entry, &ranges, &n_ranges);
5770
5771       state = gtk_widget_get_state_flags (widget);
5772       state |= GTK_STATE_FLAG_SELECTED;
5773
5774       gtk_style_context_get_background_color (context, state, &selection_color);
5775       gtk_style_context_get_color (context, state, &text_color);
5776
5777       for (i = 0; i < n_ranges; ++i)
5778         cairo_rectangle (cr,
5779                          - priv->scroll_offset + ranges[2 * i],
5780                          y,
5781                          ranges[2 * i + 1],
5782                          logical_rect.height);
5783
5784       cairo_clip (cr);
5785           
5786       gdk_cairo_set_source_rgba (cr, &selection_color);
5787       cairo_paint (cr);
5788
5789       cairo_move_to (cr, x, y);
5790       gdk_cairo_set_source_rgba (cr, &text_color);
5791       pango_cairo_show_layout (cr, layout);
5792   
5793       g_free (ranges);
5794     }
5795   cairo_restore (cr);
5796 }
5797
5798 static void
5799 gtk_entry_draw_text (GtkEntry *entry,
5800                      cairo_t  *cr)
5801 {
5802   GtkEntryPrivate *priv = entry->priv;
5803   GtkWidget *widget = GTK_WIDGET (entry);
5804   GtkStateFlags state = 0;
5805   GdkRGBA text_color, bar_text_color;
5806   GtkStyleContext *context;
5807   gint width, height;
5808   gint progress_x, progress_y, progress_width, progress_height;
5809   gint clip_width, clip_height;
5810
5811   /* Nothing to display at all */
5812   if (gtk_entry_get_display_mode (entry) == DISPLAY_BLANK)
5813     return;
5814
5815   state = gtk_widget_get_state_flags (widget);
5816   context = gtk_widget_get_style_context (widget);
5817
5818   gtk_style_context_get_color (context, state, &text_color);
5819
5820   /* Get foreground color for progressbars */
5821   gtk_entry_prepare_context_for_progress (entry, context);
5822   gtk_style_context_get_color (context, state, &bar_text_color);
5823   gtk_style_context_restore (context);
5824
5825   get_progress_area (widget,
5826                      &progress_x, &progress_y,
5827                      &progress_width, &progress_height);
5828
5829   cairo_save (cr);
5830
5831   clip_width = gdk_window_get_width (priv->text_area);
5832   clip_height = gdk_window_get_height (priv->text_area);
5833   cairo_rectangle (cr, 0, 0, clip_width, clip_height);
5834   cairo_clip (cr);
5835
5836   /* If the color is the same, or the progress area has a zero
5837    * size, then we only need to draw once. */
5838   if (gdk_rgba_equal (&text_color, &bar_text_color) ||
5839       ((progress_width == 0) || (progress_height == 0)))
5840     {
5841       draw_text_with_color (entry, cr, &text_color);
5842     }
5843   else
5844     {
5845       int frame_x, frame_y, area_x, area_y;
5846
5847       width = gdk_window_get_width (priv->text_area);
5848       height = gdk_window_get_height (priv->text_area);
5849
5850       cairo_save (cr);
5851
5852       cairo_set_fill_rule (cr, CAIRO_FILL_RULE_EVEN_ODD);
5853       cairo_rectangle (cr, 0, 0, width, height);
5854
5855       /* progres area is frame-relative, we need it text-area window
5856        * relative */
5857       get_frame_size (entry, TRUE, &frame_x, &frame_y, NULL, NULL);
5858       gdk_window_get_position (priv->text_area, &area_x, &area_y);
5859       progress_x += frame_x - area_x;
5860       progress_y += frame_y - area_y;
5861
5862       cairo_rectangle (cr, progress_x, progress_y,
5863                        progress_width, progress_height);
5864       cairo_clip (cr);
5865       cairo_set_fill_rule (cr, CAIRO_FILL_RULE_WINDING);
5866   
5867       draw_text_with_color (entry, cr, &text_color);
5868       cairo_restore (cr);
5869
5870       cairo_save (cr);
5871
5872       cairo_rectangle (cr, progress_x, progress_y,
5873                        progress_width, progress_height);
5874       cairo_clip (cr);
5875
5876       draw_text_with_color (entry, cr, &bar_text_color);
5877
5878       cairo_restore (cr);
5879     }
5880
5881   cairo_restore (cr);
5882 }
5883
5884 static void
5885 gtk_entry_draw_cursor (GtkEntry  *entry,
5886                        cairo_t   *cr,
5887                        CursorType type)
5888 {
5889   GtkEntryPrivate *priv = entry->priv;
5890   GtkWidget *widget = GTK_WIDGET (entry);
5891   GtkStyleContext *context;
5892   PangoRectangle cursor_rect;
5893   gint cursor_index;
5894   gboolean block;
5895   gboolean block_at_line_end;
5896   PangoLayout *layout;
5897   const char *text;
5898   gint x, y;
5899
5900   context = gtk_widget_get_style_context (widget);
5901
5902   layout = gtk_entry_ensure_layout (entry, TRUE);
5903   text = pango_layout_get_text (layout);
5904   cursor_index = g_utf8_offset_to_pointer (text, priv->current_pos + priv->preedit_cursor) - text;
5905   get_layout_position (entry, &x, &y);
5906
5907   if (!priv->overwrite_mode)
5908     block = FALSE;
5909   else
5910     block = _gtk_text_util_get_block_cursor_location (layout,
5911                                                       cursor_index, &cursor_rect, &block_at_line_end);
5912
5913   if (!block)
5914     {
5915       gtk_render_insertion_cursor (context, cr,
5916                                    x, y,
5917                                    layout, cursor_index, priv->resolved_dir);
5918     }
5919   else /* overwrite_mode */
5920     {
5921       GdkRGBA cursor_color;
5922       GdkRectangle rect;
5923
5924       cairo_save (cr);
5925
5926       rect.x = PANGO_PIXELS (cursor_rect.x) + x;
5927       rect.y = PANGO_PIXELS (cursor_rect.y) + y;
5928       rect.width = PANGO_PIXELS (cursor_rect.width);
5929       rect.height = PANGO_PIXELS (cursor_rect.height);
5930
5931       _gtk_style_context_get_cursor_color (context, &cursor_color, NULL);
5932       gdk_cairo_set_source_rgba (cr, &cursor_color);
5933       gdk_cairo_rectangle (cr, &rect);
5934       cairo_fill (cr);
5935
5936       if (!block_at_line_end)
5937         {
5938           GtkStateFlags state;
5939           GdkRGBA color;
5940
5941           state = gtk_widget_get_state_flags (widget);
5942           gtk_style_context_get_background_color (context, state, &color);
5943
5944           gdk_cairo_rectangle (cr, &rect);
5945           cairo_clip (cr);
5946           cairo_move_to (cr, x, y);
5947           gdk_cairo_set_source_rgba (cr, &color);
5948           pango_cairo_show_layout (cr, layout);
5949         }
5950
5951       cairo_restore (cr);
5952     }
5953 }
5954
5955 void
5956 _gtk_entry_reset_im_context (GtkEntry *entry)
5957 {
5958   GtkEntryPrivate *priv = entry->priv;
5959
5960   if (priv->need_im_reset)
5961     {
5962       priv->need_im_reset = FALSE;
5963       gtk_im_context_reset (priv->im_context);
5964     }
5965 }
5966
5967 /**
5968  * gtk_entry_reset_im_context:
5969  * @entry: a #GtkEntry
5970  *
5971  * Reset the input method context of the entry if needed.
5972  *
5973  * This can be necessary in the case where modifying the buffer
5974  * would confuse on-going input method behavior.
5975  *
5976  * Since: 2.22
5977  */
5978 void
5979 gtk_entry_reset_im_context (GtkEntry *entry)
5980 {
5981   g_return_if_fail (GTK_IS_ENTRY (entry));
5982
5983   _gtk_entry_reset_im_context (entry);
5984 }
5985
5986 /**
5987  * gtk_entry_im_context_filter_keypress:
5988  * @entry: a #GtkEntry
5989  * @event: (type Gdk.EventKey): the key event
5990  *
5991  * Allow the #GtkEntry input method to internally handle key press
5992  * and release events. If this function returns %TRUE, then no further
5993  * processing should be done for this key event. See
5994  * gtk_im_context_filter_keypress().
5995  *
5996  * Note that you are expected to call this function from your handler
5997  * when overriding key event handling. This is needed in the case when
5998  * you need to insert your own key handling between the input method
5999  * and the default key event handling of the #GtkEntry.
6000  * See gtk_text_view_reset_im_context() for an example of use.
6001  *
6002  * Return value: %TRUE if the input method handled the key event.
6003  *
6004  * Since: 2.22
6005  */
6006 gboolean
6007 gtk_entry_im_context_filter_keypress (GtkEntry    *entry,
6008                                       GdkEventKey *event)
6009 {
6010   GtkEntryPrivate *priv;
6011
6012   g_return_val_if_fail (GTK_IS_ENTRY (entry), FALSE);
6013
6014   priv = entry->priv;
6015
6016   return gtk_im_context_filter_keypress (priv->im_context, event);
6017 }
6018
6019 GtkIMContext*
6020 _gtk_entry_get_im_context (GtkEntry *entry)
6021 {
6022   return entry->priv->im_context;
6023 }
6024
6025 static gint
6026 gtk_entry_find_position (GtkEntry *entry,
6027                          gint      x)
6028 {
6029   GtkEntryPrivate *priv = entry->priv;
6030   PangoLayout *layout;
6031   PangoLayoutLine *line;
6032   gint index;
6033   gint pos;
6034   gint trailing;
6035   const gchar *text;
6036   gint cursor_index;
6037   
6038   layout = gtk_entry_ensure_layout (entry, TRUE);
6039   text = pango_layout_get_text (layout);
6040   cursor_index = g_utf8_offset_to_pointer (text, priv->current_pos) - text;
6041
6042   line = pango_layout_get_lines_readonly (layout)->data;
6043   pango_layout_line_x_to_index (line, x * PANGO_SCALE, &index, &trailing);
6044
6045   if (index >= cursor_index && priv->preedit_length)
6046     {
6047       if (index >= cursor_index + priv->preedit_length)
6048         index -= priv->preedit_length;
6049       else
6050         {
6051           index = cursor_index;
6052           trailing = 0;
6053         }
6054     }
6055
6056   pos = g_utf8_pointer_to_offset (text, text + index);
6057   pos += trailing;
6058
6059   return pos;
6060 }
6061
6062 static void
6063 gtk_entry_get_cursor_locations (GtkEntry   *entry,
6064                                 CursorType  type,
6065                                 gint       *strong_x,
6066                                 gint       *weak_x)
6067 {
6068   GtkEntryPrivate *priv = entry->priv;
6069   DisplayMode mode = gtk_entry_get_display_mode (entry);
6070
6071   /* Nothing to display at all, so no cursor is relevant */
6072   if (mode == DISPLAY_BLANK)
6073     {
6074       if (strong_x)
6075         *strong_x = 0;
6076       
6077       if (weak_x)
6078         *weak_x = 0;
6079     }
6080   else
6081     {
6082       PangoLayout *layout = gtk_entry_ensure_layout (entry, TRUE);
6083       const gchar *text = pango_layout_get_text (layout);
6084       PangoRectangle strong_pos, weak_pos;
6085       gint index;
6086   
6087       if (type == CURSOR_STANDARD)
6088         {
6089           index = g_utf8_offset_to_pointer (text, priv->current_pos + priv->preedit_cursor) - text;
6090         }
6091       else /* type == CURSOR_DND */
6092         {
6093           index = g_utf8_offset_to_pointer (text, priv->dnd_position) - text;
6094
6095           if (priv->dnd_position > priv->current_pos)
6096             {
6097               if (mode == DISPLAY_NORMAL)
6098                 index += priv->preedit_length;
6099               else
6100                 {
6101                   gint preedit_len_chars = g_utf8_strlen (text, -1) - gtk_entry_buffer_get_length (get_buffer (entry));
6102                   index += preedit_len_chars * g_unichar_to_utf8 (priv->invisible_char, NULL);
6103                 }
6104             }
6105         }
6106       
6107       pango_layout_get_cursor_pos (layout, index, &strong_pos, &weak_pos);
6108       
6109       if (strong_x)
6110         *strong_x = strong_pos.x / PANGO_SCALE;
6111       
6112       if (weak_x)
6113         *weak_x = weak_pos.x / PANGO_SCALE;
6114     }
6115 }
6116
6117 static void
6118 gtk_entry_adjust_scroll (GtkEntry *entry)
6119 {
6120   GtkEntryPrivate *priv = entry->priv;
6121   gint min_offset, max_offset;
6122   gint text_area_width, text_width;
6123   gint strong_x, weak_x;
6124   gint strong_xoffset, weak_xoffset;
6125   gfloat xalign;
6126   PangoLayout *layout;
6127   PangoLayoutLine *line;
6128   PangoRectangle logical_rect;
6129
6130   if (!gtk_widget_get_realized (GTK_WIDGET (entry)))
6131     return;
6132
6133   text_area_width = gdk_window_get_width (priv->text_area);
6134
6135   if (text_area_width < 0)
6136     text_area_width = 0;
6137
6138   layout = gtk_entry_ensure_layout (entry, TRUE);
6139   line = pango_layout_get_lines_readonly (layout)->data;
6140
6141   pango_layout_line_get_extents (line, NULL, &logical_rect);
6142
6143   /* Display as much text as we can */
6144
6145   if (priv->resolved_dir == PANGO_DIRECTION_LTR)
6146       xalign = priv->xalign;
6147   else
6148       xalign = 1.0 - priv->xalign;
6149
6150   text_width = PANGO_PIXELS(logical_rect.width);
6151
6152   if (text_width > text_area_width)
6153     {
6154       min_offset = 0;
6155       max_offset = text_width - text_area_width;
6156     }
6157   else
6158     {
6159       min_offset = (text_width - text_area_width) * xalign;
6160       max_offset = min_offset;
6161     }
6162
6163   priv->scroll_offset = CLAMP (priv->scroll_offset, min_offset, max_offset);
6164
6165   /* And make sure cursors are on screen. Note that the cursor is
6166    * actually drawn one pixel into the INNER_BORDER space on
6167    * the right, when the scroll is at the utmost right. This
6168    * looks better to to me than confining the cursor inside the
6169    * border entirely, though it means that the cursor gets one
6170    * pixel closer to the edge of the widget on the right than
6171    * on the left. This might need changing if one changed
6172    * INNER_BORDER from 2 to 1, as one would do on a
6173    * small-screen-real-estate display.
6174    *
6175    * We always make sure that the strong cursor is on screen, and
6176    * put the weak cursor on screen if possible.
6177    */
6178
6179   gtk_entry_get_cursor_locations (entry, CURSOR_STANDARD, &strong_x, &weak_x);
6180   
6181   strong_xoffset = strong_x - priv->scroll_offset;
6182
6183   if (strong_xoffset < 0)
6184     {
6185       priv->scroll_offset += strong_xoffset;
6186       strong_xoffset = 0;
6187     }
6188   else if (strong_xoffset > text_area_width)
6189     {
6190       priv->scroll_offset += strong_xoffset - text_area_width;
6191       strong_xoffset = text_area_width;
6192     }
6193
6194   weak_xoffset = weak_x - priv->scroll_offset;
6195
6196   if (weak_xoffset < 0 && strong_xoffset - weak_xoffset <= text_area_width)
6197     {
6198       priv->scroll_offset += weak_xoffset;
6199     }
6200   else if (weak_xoffset > text_area_width &&
6201            strong_xoffset - (weak_xoffset - text_area_width) >= 0)
6202     {
6203       priv->scroll_offset += weak_xoffset - text_area_width;
6204     }
6205
6206   g_object_notify (G_OBJECT (entry), "scroll-offset");
6207 }
6208
6209 static void
6210 gtk_entry_move_adjustments (GtkEntry *entry)
6211 {
6212   GtkWidget *widget = GTK_WIDGET (entry);
6213   GtkAllocation allocation;
6214   GtkAdjustment *adjustment;
6215   PangoContext *context;
6216   PangoFontMetrics *metrics;
6217   GtkStyleContext *style_context;
6218   GtkStateFlags state;
6219   GtkBorder borders;
6220   gint x, layout_x;
6221   gint char_width;
6222
6223   adjustment = g_object_get_qdata (G_OBJECT (entry), quark_cursor_hadjustment);
6224   if (!adjustment)
6225     return;
6226
6227   gtk_widget_get_allocation (widget, &allocation);
6228
6229   /* Cursor position, layout offset, border width, and widget allocation */
6230   gtk_entry_get_cursor_locations (entry, CURSOR_STANDARD, &x, NULL);
6231   get_layout_position (entry, &layout_x, NULL);
6232   _gtk_entry_get_borders (entry, &borders);
6233   x += allocation.x + layout_x + borders.left;
6234
6235   /* Approximate width of a char, so user can see what is ahead/behind */
6236   context = gtk_widget_get_pango_context (widget);
6237   style_context = gtk_widget_get_style_context (widget);
6238   state = gtk_widget_get_state_flags (widget);
6239
6240   metrics = pango_context_get_metrics (context,
6241                                        gtk_style_context_get_font (style_context, state),
6242                                        pango_context_get_language (context));
6243   char_width = pango_font_metrics_get_approximate_char_width (metrics) / PANGO_SCALE;
6244
6245   /* Scroll it */
6246   gtk_adjustment_clamp_page (adjustment, 
6247                              x - (char_width + 1),   /* one char + one pixel before */
6248                              x + (char_width + 2));  /* one char + cursor + one pixel after */
6249 }
6250
6251 static gint
6252 gtk_entry_move_visually (GtkEntry *entry,
6253                          gint      start,
6254                          gint      count)
6255 {
6256   GtkEntryPrivate *priv = entry->priv;
6257   gint index;
6258   PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
6259   const gchar *text;
6260
6261   text = pango_layout_get_text (layout);
6262   
6263   index = g_utf8_offset_to_pointer (text, start) - text;
6264
6265   while (count != 0)
6266     {
6267       int new_index, new_trailing;
6268       gboolean split_cursor;
6269       gboolean strong;
6270
6271       g_object_get (gtk_widget_get_settings (GTK_WIDGET (entry)),
6272                     "gtk-split-cursor", &split_cursor,
6273                     NULL);
6274
6275       if (split_cursor)
6276         strong = TRUE;
6277       else
6278         {
6279           GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (entry)));
6280           PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
6281
6282           strong = keymap_direction == priv->resolved_dir;
6283         }
6284       
6285       if (count > 0)
6286         {
6287           pango_layout_move_cursor_visually (layout, strong, index, 0, 1, &new_index, &new_trailing);
6288           count--;
6289         }
6290       else
6291         {
6292           pango_layout_move_cursor_visually (layout, strong, index, 0, -1, &new_index, &new_trailing);
6293           count++;
6294         }
6295
6296       if (new_index < 0)
6297         index = 0;
6298       else if (new_index != G_MAXINT)
6299         index = new_index;
6300       
6301       while (new_trailing--)
6302         index = g_utf8_next_char (text + index) - text;
6303     }
6304   
6305   return g_utf8_pointer_to_offset (text, text + index);
6306 }
6307
6308 static gint
6309 gtk_entry_move_logically (GtkEntry *entry,
6310                           gint      start,
6311                           gint      count)
6312 {
6313   gint new_pos = start;
6314   guint length;
6315
6316   length = gtk_entry_buffer_get_length (get_buffer (entry));
6317
6318   /* Prevent any leak of information */
6319   if (gtk_entry_get_display_mode (entry) != DISPLAY_NORMAL)
6320     {
6321       new_pos = CLAMP (start + count, 0, length);
6322     }
6323   else
6324     {
6325       PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
6326       PangoLogAttr *log_attrs;
6327       gint n_attrs;
6328
6329       pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
6330
6331       while (count > 0 && new_pos < length)
6332         {
6333           do
6334             new_pos++;
6335           while (new_pos < length && !log_attrs[new_pos].is_cursor_position);
6336           
6337           count--;
6338         }
6339       while (count < 0 && new_pos > 0)
6340         {
6341           do
6342             new_pos--;
6343           while (new_pos > 0 && !log_attrs[new_pos].is_cursor_position);
6344           
6345           count++;
6346         }
6347       
6348       g_free (log_attrs);
6349     }
6350
6351   return new_pos;
6352 }
6353
6354 static gint
6355 gtk_entry_move_forward_word (GtkEntry *entry,
6356                              gint      start,
6357                              gboolean  allow_whitespace)
6358 {
6359   gint new_pos = start;
6360   guint length;
6361
6362   length = gtk_entry_buffer_get_length (get_buffer (entry));
6363
6364   /* Prevent any leak of information */
6365   if (gtk_entry_get_display_mode (entry) != DISPLAY_NORMAL)
6366     {
6367       new_pos = length;
6368     }
6369   else if (new_pos < length)
6370     {
6371       PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
6372       PangoLogAttr *log_attrs;
6373       gint n_attrs;
6374
6375       pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
6376       
6377       /* Find the next word boundary */
6378       new_pos++;
6379       while (new_pos < n_attrs - 1 && !(log_attrs[new_pos].is_word_end ||
6380                                         (log_attrs[new_pos].is_word_start && allow_whitespace)))
6381         new_pos++;
6382
6383       g_free (log_attrs);
6384     }
6385
6386   return new_pos;
6387 }
6388
6389
6390 static gint
6391 gtk_entry_move_backward_word (GtkEntry *entry,
6392                               gint      start,
6393                               gboolean  allow_whitespace)
6394 {
6395   gint new_pos = start;
6396
6397   /* Prevent any leak of information */
6398   if (gtk_entry_get_display_mode (entry) != DISPLAY_NORMAL)
6399     {
6400       new_pos = 0;
6401     }
6402   else if (start > 0)
6403     {
6404       PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
6405       PangoLogAttr *log_attrs;
6406       gint n_attrs;
6407
6408       pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
6409
6410       new_pos = start - 1;
6411
6412       /* Find the previous word boundary */
6413       while (new_pos > 0 && !(log_attrs[new_pos].is_word_start || 
6414                               (log_attrs[new_pos].is_word_end && allow_whitespace)))
6415         new_pos--;
6416
6417       g_free (log_attrs);
6418     }
6419
6420   return new_pos;
6421 }
6422
6423 static void
6424 gtk_entry_delete_whitespace (GtkEntry *entry)
6425 {
6426   GtkEntryPrivate *priv = entry->priv;
6427   PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
6428   PangoLogAttr *log_attrs;
6429   gint n_attrs;
6430   gint start, end;
6431
6432   pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
6433
6434   start = end = priv->current_pos;
6435   
6436   while (start > 0 && log_attrs[start-1].is_white)
6437     start--;
6438
6439   while (end < n_attrs && log_attrs[end].is_white)
6440     end++;
6441
6442   g_free (log_attrs);
6443
6444   if (start != end)
6445     gtk_editable_delete_text (GTK_EDITABLE (entry), start, end);
6446 }
6447
6448
6449 static void
6450 gtk_entry_select_word (GtkEntry *entry)
6451 {
6452   GtkEntryPrivate *priv = entry->priv;
6453   gint start_pos = gtk_entry_move_backward_word (entry, priv->current_pos, TRUE);
6454   gint end_pos = gtk_entry_move_forward_word (entry, priv->current_pos, TRUE);
6455
6456   gtk_editable_select_region (GTK_EDITABLE (entry), start_pos, end_pos);
6457 }
6458
6459 static void
6460 gtk_entry_select_line (GtkEntry *entry)
6461 {
6462   gtk_editable_select_region (GTK_EDITABLE (entry), 0, -1);
6463 }
6464
6465 static gint
6466 truncate_multiline (const gchar *text)
6467 {
6468   gint length;
6469
6470   for (length = 0;
6471        text[length] && text[length] != '\n' && text[length] != '\r';
6472        length++);
6473
6474   return length;
6475 }
6476
6477 static void
6478 paste_received (GtkClipboard *clipboard,
6479                 const gchar  *text,
6480                 gpointer      data)
6481 {
6482   GtkEntry *entry = GTK_ENTRY (data);
6483   GtkEditable *editable = GTK_EDITABLE (entry);
6484   GtkEntryPrivate *priv = entry->priv;
6485
6486   if (priv->button == GDK_BUTTON_MIDDLE)
6487     {
6488       gint pos, start, end;
6489       pos = priv->insert_pos;
6490       gtk_editable_get_selection_bounds (editable, &start, &end);
6491       if (!((start <= pos && pos <= end) || (end <= pos && pos <= start)))
6492         gtk_editable_select_region (editable, pos, pos);
6493     }
6494       
6495   if (text)
6496     {
6497       gint pos, start, end;
6498       gint length = -1;
6499       gboolean popup_completion;
6500       GtkEntryCompletion *completion;
6501
6502       completion = gtk_entry_get_completion (entry);
6503
6504       if (priv->truncate_multiline)
6505         length = truncate_multiline (text);
6506
6507       /* only complete if the selection is at the end */
6508       popup_completion = (gtk_entry_buffer_get_length (get_buffer (entry)) ==
6509                           MAX (priv->current_pos, priv->selection_bound));
6510
6511       if (completion)
6512         {
6513           if (gtk_widget_get_mapped (completion->priv->popup_window))
6514             _gtk_entry_completion_popdown (completion);
6515
6516           if (!popup_completion && completion->priv->changed_id > 0)
6517             g_signal_handler_block (entry, completion->priv->changed_id);
6518         }
6519
6520       begin_change (entry);
6521       if (gtk_editable_get_selection_bounds (editable, &start, &end))
6522         gtk_editable_delete_text (editable, start, end);
6523
6524       pos = priv->current_pos;
6525       gtk_editable_insert_text (editable, text, length, &pos);
6526       gtk_editable_set_position (editable, pos);
6527       end_change (entry);
6528
6529       if (completion &&
6530           !popup_completion && completion->priv->changed_id > 0)
6531         g_signal_handler_unblock (entry, completion->priv->changed_id);
6532     }
6533
6534   g_object_unref (entry);
6535 }
6536
6537 static void
6538 gtk_entry_paste (GtkEntry *entry,
6539                  GdkAtom   selection)
6540 {
6541   g_object_ref (entry);
6542   gtk_clipboard_request_text (gtk_widget_get_clipboard (GTK_WIDGET (entry), selection),
6543                               paste_received, entry);
6544 }
6545
6546 static void
6547 primary_get_cb (GtkClipboard     *clipboard,
6548                 GtkSelectionData *selection_data,
6549                 guint             info,
6550                 gpointer          data)
6551 {
6552   GtkEntry *entry = GTK_ENTRY (data);
6553   gint start, end;
6554   
6555   if (gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &start, &end))
6556     {
6557       gchar *str = gtk_entry_get_display_text (entry, start, end);
6558       gtk_selection_data_set_text (selection_data, str, -1);
6559       g_free (str);
6560     }
6561 }
6562
6563 static void
6564 primary_clear_cb (GtkClipboard *clipboard,
6565                   gpointer      data)
6566 {
6567   GtkEntry *entry = GTK_ENTRY (data);
6568   GtkEntryPrivate *priv = entry->priv;
6569
6570   gtk_editable_select_region (GTK_EDITABLE (entry), priv->current_pos, priv->current_pos);
6571 }
6572
6573 static void
6574 gtk_entry_update_primary_selection (GtkEntry *entry)
6575 {
6576   GtkTargetList *list;
6577   GtkTargetEntry *targets;
6578   GtkClipboard *clipboard;
6579   gint start, end;
6580   gint n_targets;
6581
6582   if (!gtk_widget_get_realized (GTK_WIDGET (entry)))
6583     return;
6584
6585   list = gtk_target_list_new (NULL, 0);
6586   gtk_target_list_add_text_targets (list, 0);
6587
6588   targets = gtk_target_table_new_from_list (list, &n_targets);
6589
6590   clipboard = gtk_widget_get_clipboard (GTK_WIDGET (entry), GDK_SELECTION_PRIMARY);
6591   
6592   if (gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &start, &end))
6593     {
6594       if (!gtk_clipboard_set_with_owner (clipboard, targets, n_targets,
6595                                          primary_get_cb, primary_clear_cb, G_OBJECT (entry)))
6596         primary_clear_cb (clipboard, entry);
6597     }
6598   else
6599     {
6600       if (gtk_clipboard_get_owner (clipboard) == G_OBJECT (entry))
6601         gtk_clipboard_clear (clipboard);
6602     }
6603
6604   gtk_target_table_free (targets, n_targets);
6605   gtk_target_list_unref (list);
6606 }
6607
6608 static void
6609 gtk_entry_clear (GtkEntry             *entry,
6610                  GtkEntryIconPosition  icon_pos)
6611 {
6612   GtkEntryPrivate *priv = entry->priv;
6613   EntryIconInfo *icon_info = priv->icons[icon_pos];
6614   GtkImageType storage_type;
6615
6616   if (icon_info && _gtk_icon_helper_get_is_empty (icon_info->icon_helper))
6617     return;
6618
6619   g_object_freeze_notify (G_OBJECT (entry));
6620
6621   /* Explicitly check, as the pointer may become invalidated
6622    * during destruction.
6623    */
6624   if (GDK_IS_WINDOW (icon_info->window))
6625     gdk_window_hide (icon_info->window);
6626
6627   storage_type = _gtk_icon_helper_get_storage_type (icon_info->icon_helper);
6628
6629   switch (storage_type)
6630     {
6631     case GTK_IMAGE_PIXBUF:
6632       g_object_notify (G_OBJECT (entry),
6633                        icon_pos == GTK_ENTRY_ICON_PRIMARY ? "primary-icon-pixbuf" : "secondary-icon-pixbuf");
6634       break;
6635
6636     case GTK_IMAGE_STOCK:
6637       g_object_notify (G_OBJECT (entry),
6638                        icon_pos == GTK_ENTRY_ICON_PRIMARY ? "primary-icon-stock" : "secondary-icon-stock");
6639       break;
6640
6641     case GTK_IMAGE_ICON_NAME:
6642       g_object_notify (G_OBJECT (entry),
6643                        icon_pos == GTK_ENTRY_ICON_PRIMARY ? "primary-icon-name" : "secondary-icon-name");
6644       break;
6645
6646     case GTK_IMAGE_GICON:
6647       g_object_notify (G_OBJECT (entry),
6648                        icon_pos == GTK_ENTRY_ICON_PRIMARY ? "primary-icon-gicon" : "secondary-icon-gicon");
6649       break;
6650
6651     default:
6652       g_assert_not_reached ();
6653       break;
6654     }
6655
6656   _gtk_icon_helper_clear (icon_info->icon_helper);
6657
6658   g_object_notify (G_OBJECT (entry),
6659                    icon_pos == GTK_ENTRY_ICON_PRIMARY ? "primary-icon-storage-type" : "secondary-icon-storage-type");
6660
6661   g_object_thaw_notify (G_OBJECT (entry));
6662 }
6663
6664 static GdkPixbuf *
6665 gtk_entry_ensure_pixbuf (GtkEntry             *entry,
6666                          GtkEntryIconPosition  icon_pos)
6667 {
6668   GtkEntryPrivate *priv = entry->priv;
6669   EntryIconInfo *icon_info = priv->icons[icon_pos];
6670   GtkStyleContext *context;
6671   GdkPixbuf *pix;
6672
6673   context = gtk_widget_get_style_context (GTK_WIDGET (entry));
6674   gtk_entry_prepare_context_for_icon (entry, context, icon_pos);
6675
6676   pix = _gtk_icon_helper_ensure_pixbuf (icon_info->icon_helper,
6677                                         context);
6678
6679   gtk_style_context_restore (context);
6680
6681   return pix;
6682 }
6683
6684 /* Public API
6685  */
6686
6687 /**
6688  * gtk_entry_new:
6689  *
6690  * Creates a new entry.
6691  *
6692  * Return value: a new #GtkEntry.
6693  */
6694 GtkWidget*
6695 gtk_entry_new (void)
6696 {
6697   return g_object_new (GTK_TYPE_ENTRY, NULL);
6698 }
6699
6700 /**
6701  * gtk_entry_new_with_buffer:
6702  * @buffer: The buffer to use for the new #GtkEntry.
6703  *
6704  * Creates a new entry with the specified text buffer.
6705  *
6706  * Return value: a new #GtkEntry
6707  *
6708  * Since: 2.18
6709  */
6710 GtkWidget*
6711 gtk_entry_new_with_buffer (GtkEntryBuffer *buffer)
6712 {
6713   g_return_val_if_fail (GTK_IS_ENTRY_BUFFER (buffer), NULL);
6714   return g_object_new (GTK_TYPE_ENTRY, "buffer", buffer, NULL);
6715 }
6716
6717 static GtkEntryBuffer*
6718 get_buffer (GtkEntry *entry)
6719 {
6720   GtkEntryPrivate *priv = entry->priv;
6721
6722   if (priv->buffer == NULL)
6723     {
6724       GtkEntryBuffer *buffer;
6725       buffer = gtk_entry_buffer_new (NULL, 0);
6726       gtk_entry_set_buffer (entry, buffer);
6727       g_object_unref (buffer);
6728     }
6729
6730   return priv->buffer;
6731 }
6732
6733 /**
6734  * gtk_entry_get_buffer:
6735  * @entry: a #GtkEntry
6736  *
6737  * Get the #GtkEntryBuffer object which holds the text for
6738  * this widget.
6739  *
6740  * Since: 2.18
6741  *
6742  * Returns: (transfer none): A #GtkEntryBuffer object.
6743  */
6744 GtkEntryBuffer*
6745 gtk_entry_get_buffer (GtkEntry *entry)
6746 {
6747   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
6748
6749   return get_buffer (entry);
6750 }
6751
6752 /**
6753  * gtk_entry_set_buffer:
6754  * @entry: a #GtkEntry
6755  * @buffer: a #GtkEntryBuffer
6756  *
6757  * Set the #GtkEntryBuffer object which holds the text for
6758  * this widget.
6759  *
6760  * Since: 2.18
6761  */
6762 void
6763 gtk_entry_set_buffer (GtkEntry       *entry,
6764                       GtkEntryBuffer *buffer)
6765 {
6766   GtkEntryPrivate *priv;
6767   GObject *obj;
6768
6769   g_return_if_fail (GTK_IS_ENTRY (entry));
6770
6771   priv = entry->priv;
6772
6773   if (buffer)
6774     {
6775       g_return_if_fail (GTK_IS_ENTRY_BUFFER (buffer));
6776       g_object_ref (buffer);
6777     }
6778
6779   if (priv->buffer)
6780     {
6781       buffer_disconnect_signals (entry);
6782       g_object_unref (priv->buffer);
6783     }
6784
6785   priv->buffer = buffer;
6786
6787   if (priv->buffer)
6788      buffer_connect_signals (entry);
6789
6790   obj = G_OBJECT (entry);
6791   g_object_freeze_notify (obj);
6792   g_object_notify (obj, "buffer");
6793   g_object_notify (obj, "text");
6794   g_object_notify (obj, "text-length");
6795   g_object_notify (obj, "max-length");
6796   g_object_notify (obj, "visibility");
6797   g_object_notify (obj, "invisible-char");
6798   g_object_notify (obj, "invisible-char-set");
6799   g_object_thaw_notify (obj);
6800
6801   gtk_editable_set_position (GTK_EDITABLE (entry), 0);
6802   gtk_entry_recompute (entry);
6803 }
6804
6805 /**
6806  * gtk_entry_get_text_area:
6807  * @entry: a #GtkEntry
6808  * @text_area: (out): Return location for the text area.
6809  *
6810  * Gets the area where the entry's text is drawn. This function is
6811  * useful when drawing something to the entry in a draw callback.
6812  *
6813  * If the entry is not realized, @text_area is filled with zeros.
6814  *
6815  * See also gtk_entry_get_icon_area().
6816  *
6817  * Since: 3.0
6818  **/
6819 void
6820 gtk_entry_get_text_area (GtkEntry     *entry,
6821                          GdkRectangle *text_area)
6822 {
6823   GtkEntryPrivate *priv;
6824
6825   g_return_if_fail (GTK_IS_ENTRY (entry));
6826   g_return_if_fail (text_area != NULL);
6827
6828   priv = entry->priv;
6829
6830   if (priv->text_area)
6831     {
6832       GtkAllocation allocation;
6833       gint x, y;
6834
6835       gtk_widget_get_allocation (GTK_WIDGET (entry), &allocation);
6836       gdk_window_get_position (priv->text_area, &x, &y);
6837
6838       text_area->x = x - allocation.x;
6839       text_area->y = y - allocation.y;
6840       text_area->width = gdk_window_get_width (priv->text_area);
6841       text_area->height = gdk_window_get_height (priv->text_area);
6842     }
6843   else
6844     {
6845       text_area->x = 0;
6846       text_area->y = 0;
6847       text_area->width = 0;
6848       text_area->height = 0;
6849     }
6850 }
6851
6852 /**
6853  * gtk_entry_set_text:
6854  * @entry: a #GtkEntry
6855  * @text: the new text
6856  *
6857  * Sets the text in the widget to the given
6858  * value, replacing the current contents.
6859  *
6860  * See gtk_entry_buffer_set_text().
6861  */
6862 void
6863 gtk_entry_set_text (GtkEntry    *entry,
6864                     const gchar *text)
6865 {
6866   gint tmp_pos;
6867   GtkEntryCompletion *completion;
6868
6869   g_return_if_fail (GTK_IS_ENTRY (entry));
6870   g_return_if_fail (text != NULL);
6871
6872   /* Actually setting the text will affect the cursor and selection;
6873    * if the contents don't actually change, this will look odd to the user.
6874    */
6875   if (strcmp (gtk_entry_buffer_get_text (get_buffer (entry)), text) == 0)
6876     return;
6877
6878   completion = gtk_entry_get_completion (entry);
6879   if (completion && completion->priv->changed_id > 0)
6880     g_signal_handler_block (entry, completion->priv->changed_id);
6881
6882   begin_change (entry);
6883   gtk_editable_delete_text (GTK_EDITABLE (entry), 0, -1);
6884   tmp_pos = 0;
6885   gtk_editable_insert_text (GTK_EDITABLE (entry), text, strlen (text), &tmp_pos);
6886   end_change (entry);
6887
6888   if (completion && completion->priv->changed_id > 0)
6889     g_signal_handler_unblock (entry, completion->priv->changed_id);
6890 }
6891
6892 /**
6893  * gtk_entry_set_visibility:
6894  * @entry: a #GtkEntry
6895  * @visible: %TRUE if the contents of the entry are displayed
6896  *           as plaintext
6897  *
6898  * Sets whether the contents of the entry are visible or not. 
6899  * When visibility is set to %FALSE, characters are displayed 
6900  * as the invisible char, and will also appear that way when 
6901  * the text in the entry widget is copied elsewhere.
6902  *
6903  * By default, GTK+ picks the best invisible character available
6904  * in the current font, but it can be changed with
6905  * gtk_entry_set_invisible_char().
6906  */
6907 void
6908 gtk_entry_set_visibility (GtkEntry *entry,
6909                           gboolean visible)
6910 {
6911   GtkEntryPrivate *priv;
6912
6913   g_return_if_fail (GTK_IS_ENTRY (entry));
6914
6915   priv = entry->priv;
6916
6917   visible = visible != FALSE;
6918
6919   if (priv->visible != visible)
6920     {
6921       priv->visible = visible;
6922
6923       g_object_notify (G_OBJECT (entry), "visibility");
6924       gtk_entry_recompute (entry);
6925     }
6926 }
6927
6928 /**
6929  * gtk_entry_get_visibility:
6930  * @entry: a #GtkEntry
6931  *
6932  * Retrieves whether the text in @entry is visible. See
6933  * gtk_entry_set_visibility().
6934  *
6935  * Return value: %TRUE if the text is currently visible
6936  **/
6937 gboolean
6938 gtk_entry_get_visibility (GtkEntry *entry)
6939 {
6940   g_return_val_if_fail (GTK_IS_ENTRY (entry), FALSE);
6941
6942   return entry->priv->visible;
6943 }
6944
6945 /**
6946  * gtk_entry_set_invisible_char:
6947  * @entry: a #GtkEntry
6948  * @ch: a Unicode character
6949  * 
6950  * Sets the character to use in place of the actual text when
6951  * gtk_entry_set_visibility() has been called to set text visibility
6952  * to %FALSE. i.e. this is the character used in "password mode" to
6953  * show the user how many characters have been typed. By default, GTK+
6954  * picks the best invisible char available in the current font. If you
6955  * set the invisible char to 0, then the user will get no feedback
6956  * at all; there will be no text on the screen as they type.
6957  **/
6958 void
6959 gtk_entry_set_invisible_char (GtkEntry *entry,
6960                               gunichar  ch)
6961 {
6962   GtkEntryPrivate *priv;
6963
6964   g_return_if_fail (GTK_IS_ENTRY (entry));
6965
6966   priv = entry->priv;
6967
6968   if (!priv->invisible_char_set)
6969     {
6970       priv->invisible_char_set = TRUE;
6971       g_object_notify (G_OBJECT (entry), "invisible-char-set");
6972     }
6973
6974   if (ch == priv->invisible_char)
6975     return;
6976
6977   priv->invisible_char = ch;
6978   g_object_notify (G_OBJECT (entry), "invisible-char");
6979   gtk_entry_recompute (entry);  
6980 }
6981
6982 /**
6983  * gtk_entry_get_invisible_char:
6984  * @entry: a #GtkEntry
6985  *
6986  * Retrieves the character displayed in place of the real characters
6987  * for entries with visibility set to false. See gtk_entry_set_invisible_char().
6988  *
6989  * Return value: the current invisible char, or 0, if the entry does not
6990  *               show invisible text at all. 
6991  **/
6992 gunichar
6993 gtk_entry_get_invisible_char (GtkEntry *entry)
6994 {
6995   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
6996
6997   return entry->priv->invisible_char;
6998 }
6999
7000 /**
7001  * gtk_entry_unset_invisible_char:
7002  * @entry: a #GtkEntry
7003  *
7004  * Unsets the invisible char previously set with
7005  * gtk_entry_set_invisible_char(). So that the
7006  * default invisible char is used again.
7007  *
7008  * Since: 2.16
7009  **/
7010 void
7011 gtk_entry_unset_invisible_char (GtkEntry *entry)
7012 {
7013   GtkEntryPrivate *priv;
7014   gunichar ch;
7015
7016   g_return_if_fail (GTK_IS_ENTRY (entry));
7017
7018   priv = entry->priv;
7019
7020   if (!priv->invisible_char_set)
7021     return;
7022
7023   priv->invisible_char_set = FALSE;
7024   ch = find_invisible_char (GTK_WIDGET (entry));
7025
7026   if (priv->invisible_char != ch)
7027     {
7028       priv->invisible_char = ch;
7029       g_object_notify (G_OBJECT (entry), "invisible-char");
7030     }
7031
7032   g_object_notify (G_OBJECT (entry), "invisible-char-set");
7033   gtk_entry_recompute (entry);
7034 }
7035
7036 /**
7037  * gtk_entry_set_overwrite_mode:
7038  * @entry: a #GtkEntry
7039  * @overwrite: new value
7040  * 
7041  * Sets whether the text is overwritten when typing in the #GtkEntry.
7042  *
7043  * Since: 2.14
7044  **/
7045 void
7046 gtk_entry_set_overwrite_mode (GtkEntry *entry,
7047                               gboolean  overwrite)
7048 {
7049   GtkEntryPrivate *priv = entry->priv;
7050
7051   g_return_if_fail (GTK_IS_ENTRY (entry));
7052
7053   if (priv->overwrite_mode == overwrite)
7054     return;
7055   
7056   gtk_entry_toggle_overwrite (entry);
7057
7058   g_object_notify (G_OBJECT (entry), "overwrite-mode");
7059 }
7060
7061 /**
7062  * gtk_entry_get_overwrite_mode:
7063  * @entry: a #GtkEntry
7064  * 
7065  * Gets the value set by gtk_entry_set_overwrite_mode().
7066  * 
7067  * Return value: whether the text is overwritten when typing.
7068  *
7069  * Since: 2.14
7070  **/
7071 gboolean
7072 gtk_entry_get_overwrite_mode (GtkEntry *entry)
7073 {
7074   g_return_val_if_fail (GTK_IS_ENTRY (entry), FALSE);
7075
7076   return entry->priv->overwrite_mode;
7077 }
7078
7079 /**
7080  * gtk_entry_get_text:
7081  * @entry: a #GtkEntry
7082  *
7083  * Retrieves the contents of the entry widget.
7084  * See also gtk_editable_get_chars().
7085  *
7086  * This is equivalent to:
7087  *
7088  * <informalexample><programlisting>
7089  * gtk_entry_buffer_get_text (gtk_entry_get_buffer (entry));
7090  * </programlisting></informalexample>
7091  *
7092  * Return value: a pointer to the contents of the widget as a
7093  *      string. This string points to internally allocated
7094  *      storage in the widget and must not be freed, modified or
7095  *      stored.
7096  **/
7097 const gchar*
7098 gtk_entry_get_text (GtkEntry *entry)
7099 {
7100   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
7101
7102   return gtk_entry_buffer_get_text (get_buffer (entry));
7103 }
7104
7105 /**
7106  * gtk_entry_set_max_length:
7107  * @entry: a #GtkEntry
7108  * @max: the maximum length of the entry, or 0 for no maximum.
7109  *   (other than the maximum length of entries.) The value passed in will
7110  *   be clamped to the range 0-65536.
7111  * 
7112  * Sets the maximum allowed length of the contents of the widget. If
7113  * the current contents are longer than the given length, then they
7114  * will be truncated to fit.
7115  *
7116  * This is equivalent to:
7117  *
7118  * <informalexample><programlisting>
7119  * gtk_entry_buffer_set_max_length (gtk_entry_get_buffer (entry), max);
7120  * </programlisting></informalexample>
7121  **/
7122 void
7123 gtk_entry_set_max_length (GtkEntry     *entry,
7124                           gint          max)
7125 {
7126   g_return_if_fail (GTK_IS_ENTRY (entry));
7127   gtk_entry_buffer_set_max_length (get_buffer (entry), max);
7128 }
7129
7130 /**
7131  * gtk_entry_get_max_length:
7132  * @entry: a #GtkEntry
7133  *
7134  * Retrieves the maximum allowed length of the text in
7135  * @entry. See gtk_entry_set_max_length().
7136  *
7137  * This is equivalent to:
7138  *
7139  * <informalexample><programlisting>
7140  * gtk_entry_buffer_get_max_length (gtk_entry_get_buffer (entry));
7141  * </programlisting></informalexample>
7142  *
7143  * Return value: the maximum allowed number of characters
7144  *               in #GtkEntry, or 0 if there is no maximum.
7145  **/
7146 gint
7147 gtk_entry_get_max_length (GtkEntry *entry)
7148 {
7149   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
7150
7151   return gtk_entry_buffer_get_max_length (get_buffer (entry));
7152 }
7153
7154 /**
7155  * gtk_entry_get_text_length:
7156  * @entry: a #GtkEntry
7157  *
7158  * Retrieves the current length of the text in
7159  * @entry. 
7160  *
7161  * This is equivalent to:
7162  *
7163  * <informalexample><programlisting>
7164  * gtk_entry_buffer_get_length (gtk_entry_get_buffer (entry));
7165  * </programlisting></informalexample>
7166  *
7167  * Return value: the current number of characters
7168  *               in #GtkEntry, or 0 if there are none.
7169  *
7170  * Since: 2.14
7171  **/
7172 guint16
7173 gtk_entry_get_text_length (GtkEntry *entry)
7174 {
7175   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
7176
7177   return gtk_entry_buffer_get_length (get_buffer (entry));
7178 }
7179
7180 /**
7181  * gtk_entry_set_activates_default:
7182  * @entry: a #GtkEntry
7183  * @setting: %TRUE to activate window's default widget on Enter keypress
7184  *
7185  * If @setting is %TRUE, pressing Enter in the @entry will activate the default
7186  * widget for the window containing the entry. This usually means that
7187  * the dialog box containing the entry will be closed, since the default
7188  * widget is usually one of the dialog buttons.
7189  *
7190  * (For experts: if @setting is %TRUE, the entry calls
7191  * gtk_window_activate_default() on the window containing the entry, in
7192  * the default handler for the #GtkWidget::activate signal.)
7193  **/
7194 void
7195 gtk_entry_set_activates_default (GtkEntry *entry,
7196                                  gboolean  setting)
7197 {
7198   GtkEntryPrivate *priv;
7199
7200   g_return_if_fail (GTK_IS_ENTRY (entry));
7201
7202   priv = entry->priv;
7203
7204   setting = setting != FALSE;
7205
7206   if (setting != priv->activates_default)
7207     {
7208       priv->activates_default = setting;
7209       g_object_notify (G_OBJECT (entry), "activates-default");
7210     }
7211 }
7212
7213 /**
7214  * gtk_entry_get_activates_default:
7215  * @entry: a #GtkEntry
7216  * 
7217  * Retrieves the value set by gtk_entry_set_activates_default().
7218  * 
7219  * Return value: %TRUE if the entry will activate the default widget
7220  **/
7221 gboolean
7222 gtk_entry_get_activates_default (GtkEntry *entry)
7223 {
7224   g_return_val_if_fail (GTK_IS_ENTRY (entry), FALSE);
7225
7226   return entry->priv->activates_default;
7227 }
7228
7229 /**
7230  * gtk_entry_set_width_chars:
7231  * @entry: a #GtkEntry
7232  * @n_chars: width in chars
7233  *
7234  * Changes the size request of the entry to be about the right size
7235  * for @n_chars characters. Note that it changes the size
7236  * <emphasis>request</emphasis>, the size can still be affected by
7237  * how you pack the widget into containers. If @n_chars is -1, the
7238  * size reverts to the default entry size.
7239  **/
7240 void
7241 gtk_entry_set_width_chars (GtkEntry *entry,
7242                            gint      n_chars)
7243 {
7244   GtkEntryPrivate *priv;
7245
7246   g_return_if_fail (GTK_IS_ENTRY (entry));
7247
7248   priv = entry->priv;
7249
7250   if (priv->width_chars != n_chars)
7251     {
7252       priv->width_chars = n_chars;
7253       g_object_notify (G_OBJECT (entry), "width-chars");
7254       gtk_widget_queue_resize (GTK_WIDGET (entry));
7255     }
7256 }
7257
7258 /**
7259  * gtk_entry_get_width_chars:
7260  * @entry: a #GtkEntry
7261  * 
7262  * Gets the value set by gtk_entry_set_width_chars().
7263  * 
7264  * Return value: number of chars to request space for, or negative if unset
7265  **/
7266 gint
7267 gtk_entry_get_width_chars (GtkEntry *entry)
7268 {
7269   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
7270
7271   return entry->priv->width_chars;
7272 }
7273
7274 /**
7275  * gtk_entry_set_has_frame:
7276  * @entry: a #GtkEntry
7277  * @setting: new value
7278  * 
7279  * Sets whether the entry has a beveled frame around it.
7280  **/
7281 void
7282 gtk_entry_set_has_frame (GtkEntry *entry,
7283                          gboolean  setting)
7284 {
7285   GtkEntryPrivate *priv;
7286
7287   g_return_if_fail (GTK_IS_ENTRY (entry));
7288
7289   priv = entry->priv;
7290
7291   setting = (setting != FALSE);
7292
7293   if (priv->has_frame == setting)
7294     return;
7295
7296   gtk_widget_queue_resize (GTK_WIDGET (entry));
7297   priv->has_frame = setting;
7298   g_object_notify (G_OBJECT (entry), "has-frame");
7299 }
7300
7301 /**
7302  * gtk_entry_get_has_frame:
7303  * @entry: a #GtkEntry
7304  * 
7305  * Gets the value set by gtk_entry_set_has_frame().
7306  * 
7307  * Return value: whether the entry has a beveled frame
7308  **/
7309 gboolean
7310 gtk_entry_get_has_frame (GtkEntry *entry)
7311 {
7312   g_return_val_if_fail (GTK_IS_ENTRY (entry), FALSE);
7313
7314   return entry->priv->has_frame;
7315 }
7316
7317 /**
7318  * gtk_entry_set_inner_border:
7319  * @entry: a #GtkEntry
7320  * @border: (allow-none): a #GtkBorder, or %NULL
7321  *
7322  * Sets %entry's inner-border property to %border, or clears it if %NULL
7323  * is passed. The inner-border is the area around the entry's text, but
7324  * inside its frame.
7325  *
7326  * If set, this property overrides the inner-border style property.
7327  * Overriding the style-provided border is useful when you want to do
7328  * in-place editing of some text in a canvas or list widget, where
7329  * pixel-exact positioning of the entry is important.
7330  *
7331  * Since: 2.10
7332  *
7333  * Deprecated: 3.4: Use the standard border and padding CSS properties;
7334  *   the value set with this function is ignored by #GtkEntry.
7335  **/
7336 void
7337 gtk_entry_set_inner_border (GtkEntry        *entry,
7338                             const GtkBorder *border)
7339 {
7340   g_return_if_fail (GTK_IS_ENTRY (entry));
7341
7342   gtk_entry_do_set_inner_border (entry, border);
7343 }
7344
7345 /**
7346  * gtk_entry_get_inner_border:
7347  * @entry: a #GtkEntry
7348  *
7349  * This function returns the entry's #GtkEntry:inner-border property. See
7350  * gtk_entry_set_inner_border() for more information.
7351  *
7352  * Return value: (transfer none): the entry's #GtkBorder, or %NULL if none was set.
7353  *
7354  * Since: 2.10
7355  *
7356  * Deprecated: 3.4: Use the standard border and padding CSS properties;
7357  *   the value returned by this function is ignored by #GtkEntry.
7358  **/
7359 const GtkBorder *
7360 gtk_entry_get_inner_border (GtkEntry *entry)
7361 {
7362   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
7363
7364   return gtk_entry_do_get_inner_border (entry);
7365 }
7366
7367 /**
7368  * gtk_entry_get_layout:
7369  * @entry: a #GtkEntry
7370  * 
7371  * Gets the #PangoLayout used to display the entry.
7372  * The layout is useful to e.g. convert text positions to
7373  * pixel positions, in combination with gtk_entry_get_layout_offsets().
7374  * The returned layout is owned by the entry and must not be 
7375  * modified or freed by the caller.
7376  *
7377  * Keep in mind that the layout text may contain a preedit string, so
7378  * gtk_entry_layout_index_to_text_index() and
7379  * gtk_entry_text_index_to_layout_index() are needed to convert byte
7380  * indices in the layout to byte indices in the entry contents.
7381  *
7382  * Return value: (transfer none): the #PangoLayout for this entry
7383  **/
7384 PangoLayout*
7385 gtk_entry_get_layout (GtkEntry *entry)
7386 {
7387   PangoLayout *layout;
7388   
7389   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
7390
7391   layout = gtk_entry_ensure_layout (entry, TRUE);
7392
7393   return layout;
7394 }
7395
7396
7397 /**
7398  * gtk_entry_layout_index_to_text_index:
7399  * @entry: a #GtkEntry
7400  * @layout_index: byte index into the entry layout text
7401  * 
7402  * Converts from a position in the entry contents (returned
7403  * by gtk_entry_get_text()) to a position in the
7404  * entry's #PangoLayout (returned by gtk_entry_get_layout(),
7405  * with text retrieved via pango_layout_get_text()).
7406  * 
7407  * Return value: byte index into the entry contents
7408  **/
7409 gint
7410 gtk_entry_layout_index_to_text_index (GtkEntry *entry,
7411                                       gint      layout_index)
7412 {
7413   GtkEntryPrivate *priv;
7414   PangoLayout *layout;
7415   const gchar *text;
7416   gint cursor_index;
7417   
7418   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
7419
7420   priv = entry->priv;
7421
7422   layout = gtk_entry_ensure_layout (entry, TRUE);
7423   text = pango_layout_get_text (layout);
7424   cursor_index = g_utf8_offset_to_pointer (text, priv->current_pos) - text;
7425
7426   if (layout_index >= cursor_index && priv->preedit_length)
7427     {
7428       if (layout_index >= cursor_index + priv->preedit_length)
7429         layout_index -= priv->preedit_length;
7430       else
7431         layout_index = cursor_index;
7432     }
7433
7434   return layout_index;
7435 }
7436
7437 /**
7438  * gtk_entry_text_index_to_layout_index:
7439  * @entry: a #GtkEntry
7440  * @text_index: byte index into the entry contents
7441  * 
7442  * Converts from a position in the entry's #PangoLayout (returned by
7443  * gtk_entry_get_layout()) to a position in the entry contents
7444  * (returned by gtk_entry_get_text()).
7445  * 
7446  * Return value: byte index into the entry layout text
7447  **/
7448 gint
7449 gtk_entry_text_index_to_layout_index (GtkEntry *entry,
7450                                       gint      text_index)
7451 {
7452   GtkEntryPrivate *priv;
7453   PangoLayout *layout;
7454   const gchar *text;
7455   gint cursor_index;
7456
7457   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
7458
7459   priv = entry->priv;
7460
7461   layout = gtk_entry_ensure_layout (entry, TRUE);
7462   text = pango_layout_get_text (layout);
7463   cursor_index = g_utf8_offset_to_pointer (text, priv->current_pos) - text;
7464
7465   if (text_index > cursor_index)
7466     text_index += priv->preedit_length;
7467
7468   return text_index;
7469 }
7470
7471 /**
7472  * gtk_entry_get_layout_offsets:
7473  * @entry: a #GtkEntry
7474  * @x: (out) (allow-none): location to store X offset of layout, or %NULL
7475  * @y: (out) (allow-none): location to store Y offset of layout, or %NULL
7476  *
7477  *
7478  * Obtains the position of the #PangoLayout used to render text
7479  * in the entry, in widget coordinates. Useful if you want to line
7480  * up the text in an entry with some other text, e.g. when using the
7481  * entry to implement editable cells in a sheet widget.
7482  *
7483  * Also useful to convert mouse events into coordinates inside the
7484  * #PangoLayout, e.g. to take some action if some part of the entry text
7485  * is clicked.
7486  * 
7487  * Note that as the user scrolls around in the entry the offsets will
7488  * change; you'll need to connect to the "notify::scroll-offset"
7489  * signal to track this. Remember when using the #PangoLayout
7490  * functions you need to convert to and from pixels using
7491  * PANGO_PIXELS() or #PANGO_SCALE.
7492  *
7493  * Keep in mind that the layout text may contain a preedit string, so
7494  * gtk_entry_layout_index_to_text_index() and
7495  * gtk_entry_text_index_to_layout_index() are needed to convert byte
7496  * indices in the layout to byte indices in the entry contents.
7497  **/
7498 void
7499 gtk_entry_get_layout_offsets (GtkEntry *entry,
7500                               gint     *x,
7501                               gint     *y)
7502 {
7503   gint text_area_x, text_area_y;
7504   
7505   g_return_if_fail (GTK_IS_ENTRY (entry));
7506
7507   /* this gets coords relative to text area */
7508   get_layout_position (entry, x, y);
7509
7510   /* convert to widget coords */
7511   gtk_entry_get_text_area_size (entry, &text_area_x, &text_area_y, NULL, NULL);
7512   
7513   if (x)
7514     *x += text_area_x;
7515
7516   if (y)
7517     *y += text_area_y;
7518 }
7519
7520
7521 /**
7522  * gtk_entry_set_alignment:
7523  * @entry: a #GtkEntry
7524  * @xalign: The horizontal alignment, from 0 (left) to 1 (right).
7525  *          Reversed for RTL layouts
7526  * 
7527  * Sets the alignment for the contents of the entry. This controls
7528  * the horizontal positioning of the contents when the displayed
7529  * text is shorter than the width of the entry.
7530  *
7531  * Since: 2.4
7532  **/
7533 void
7534 gtk_entry_set_alignment (GtkEntry *entry, gfloat xalign)
7535 {
7536   GtkEntryPrivate *priv;
7537
7538   g_return_if_fail (GTK_IS_ENTRY (entry));
7539
7540   priv = entry->priv;
7541
7542   if (xalign < 0.0)
7543     xalign = 0.0;
7544   else if (xalign > 1.0)
7545     xalign = 1.0;
7546
7547   if (xalign != priv->xalign)
7548     {
7549       priv->xalign = xalign;
7550
7551       gtk_entry_recompute (entry);
7552
7553       g_object_notify (G_OBJECT (entry), "xalign");
7554     }
7555 }
7556
7557 /**
7558  * gtk_entry_get_alignment:
7559  * @entry: a #GtkEntry
7560  * 
7561  * Gets the value set by gtk_entry_set_alignment().
7562  * 
7563  * Return value: the alignment
7564  *
7565  * Since: 2.4
7566  **/
7567 gfloat
7568 gtk_entry_get_alignment (GtkEntry *entry)
7569 {
7570   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0.0);
7571
7572   return entry->priv->xalign;
7573 }
7574
7575 /**
7576  * gtk_entry_set_icon_from_pixbuf:
7577  * @entry: a #GtkEntry
7578  * @icon_pos: Icon position
7579  * @pixbuf: (allow-none): A #GdkPixbuf, or %NULL
7580  *
7581  * Sets the icon shown in the specified position using a pixbuf.
7582  *
7583  * If @pixbuf is %NULL, no icon will be shown in the specified position.
7584  *
7585  * Since: 2.16
7586  */
7587 void
7588 gtk_entry_set_icon_from_pixbuf (GtkEntry             *entry,
7589                                 GtkEntryIconPosition  icon_pos,
7590                                 GdkPixbuf            *pixbuf)
7591 {
7592   GtkEntryPrivate *priv;
7593   EntryIconInfo *icon_info;
7594
7595   g_return_if_fail (GTK_IS_ENTRY (entry));
7596   g_return_if_fail (IS_VALID_ICON_POSITION (icon_pos));
7597
7598   priv = entry->priv;
7599
7600   if ((icon_info = priv->icons[icon_pos]) == NULL)
7601     icon_info = construct_icon_info (GTK_WIDGET (entry), icon_pos);
7602
7603   g_object_freeze_notify (G_OBJECT (entry));
7604
7605   if (pixbuf)
7606     g_object_ref (pixbuf);
7607
7608   gtk_entry_clear (entry, icon_pos);
7609
7610   if (pixbuf)
7611     {
7612       _gtk_icon_helper_set_pixbuf (icon_info->icon_helper, pixbuf);
7613
7614       if (icon_pos == GTK_ENTRY_ICON_PRIMARY)
7615         {
7616           g_object_notify (G_OBJECT (entry), "primary-icon-pixbuf");
7617           g_object_notify (G_OBJECT (entry), "primary-icon-storage-type");
7618         }
7619       else
7620         {
7621           g_object_notify (G_OBJECT (entry), "secondary-icon-pixbuf");
7622           g_object_notify (G_OBJECT (entry), "secondary-icon-storage-type");
7623         }
7624
7625       if (gtk_widget_get_mapped (GTK_WIDGET (entry)))
7626           gdk_window_show_unraised (icon_info->window);
7627
7628       g_object_unref (pixbuf);
7629     }
7630   
7631   if (gtk_widget_get_visible (GTK_WIDGET (entry)))
7632     gtk_widget_queue_resize (GTK_WIDGET (entry));
7633
7634   g_object_thaw_notify (G_OBJECT (entry));
7635 }
7636
7637 /**
7638  * gtk_entry_set_icon_from_stock:
7639  * @entry: A #GtkEntry
7640  * @icon_pos: Icon position
7641  * @stock_id: (allow-none): The name of the stock item, or %NULL
7642  *
7643  * Sets the icon shown in the entry at the specified position from
7644  * a stock image.
7645  *
7646  * If @stock_id is %NULL, no icon will be shown in the specified position.
7647  *
7648  * Since: 2.16
7649  */
7650 void
7651 gtk_entry_set_icon_from_stock (GtkEntry             *entry,
7652                                GtkEntryIconPosition  icon_pos,
7653                                const gchar          *stock_id)
7654 {
7655   GtkEntryPrivate *priv;
7656   EntryIconInfo *icon_info;
7657   gchar *new_id;
7658
7659   g_return_if_fail (GTK_IS_ENTRY (entry));
7660   g_return_if_fail (IS_VALID_ICON_POSITION (icon_pos));
7661
7662   priv = entry->priv;
7663
7664   if ((icon_info = priv->icons[icon_pos]) == NULL)
7665     icon_info = construct_icon_info (GTK_WIDGET (entry), icon_pos);
7666
7667   g_object_freeze_notify (G_OBJECT (entry));
7668
7669   /* need to dup before clearing */
7670   new_id = g_strdup (stock_id);
7671
7672   gtk_entry_clear (entry, icon_pos);
7673
7674   if (new_id != NULL)
7675     {
7676       _gtk_icon_helper_set_stock_id (icon_info->icon_helper, new_id, GTK_ICON_SIZE_MENU);
7677
7678       if (icon_pos == GTK_ENTRY_ICON_PRIMARY)
7679         {
7680           g_object_notify (G_OBJECT (entry), "primary-icon-stock");
7681           g_object_notify (G_OBJECT (entry), "primary-icon-storage-type");
7682         }
7683       else
7684         {
7685           g_object_notify (G_OBJECT (entry), "secondary-icon-stock");
7686           g_object_notify (G_OBJECT (entry), "secondary-icon-storage-type");
7687         }
7688
7689       if (gtk_widget_get_mapped (GTK_WIDGET (entry)))
7690           gdk_window_show_unraised (icon_info->window);
7691
7692       g_free (new_id);
7693     }
7694
7695   if (gtk_widget_get_visible (GTK_WIDGET (entry)))
7696     gtk_widget_queue_resize (GTK_WIDGET (entry));
7697
7698   g_object_thaw_notify (G_OBJECT (entry));
7699 }
7700
7701 /**
7702  * gtk_entry_set_icon_from_icon_name:
7703  * @entry: A #GtkEntry
7704  * @icon_pos: The position at which to set the icon
7705  * @icon_name: (allow-none): An icon name, or %NULL
7706  *
7707  * Sets the icon shown in the entry at the specified position
7708  * from the current icon theme.
7709  *
7710  * If the icon name isn't known, a "broken image" icon will be displayed
7711  * instead.
7712  *
7713  * If @icon_name is %NULL, no icon will be shown in the specified position.
7714  *
7715  * Since: 2.16
7716  */
7717 void
7718 gtk_entry_set_icon_from_icon_name (GtkEntry             *entry,
7719                                    GtkEntryIconPosition  icon_pos,
7720                                    const gchar          *icon_name)
7721 {
7722   GtkEntryPrivate *priv;
7723   EntryIconInfo *icon_info;
7724   gchar *new_name;
7725
7726   g_return_if_fail (GTK_IS_ENTRY (entry));
7727   g_return_if_fail (IS_VALID_ICON_POSITION (icon_pos));
7728
7729   priv = entry->priv;
7730
7731   if ((icon_info = priv->icons[icon_pos]) == NULL)
7732     icon_info = construct_icon_info (GTK_WIDGET (entry), icon_pos);
7733
7734   g_object_freeze_notify (G_OBJECT (entry));
7735
7736   /* need to dup before clearing */
7737   new_name = g_strdup (icon_name);
7738
7739   gtk_entry_clear (entry, icon_pos);
7740
7741   if (new_name != NULL)
7742     {
7743       _gtk_icon_helper_set_icon_name (icon_info->icon_helper, new_name, GTK_ICON_SIZE_MENU);
7744
7745       if (icon_pos == GTK_ENTRY_ICON_PRIMARY)
7746         {
7747           g_object_notify (G_OBJECT (entry), "primary-icon-name");
7748           g_object_notify (G_OBJECT (entry), "primary-icon-storage-type");
7749         }
7750       else
7751         {
7752           g_object_notify (G_OBJECT (entry), "secondary-icon-name");
7753           g_object_notify (G_OBJECT (entry), "secondary-icon-storage-type");
7754         }
7755
7756       if (gtk_widget_get_mapped (GTK_WIDGET (entry)))
7757           gdk_window_show_unraised (icon_info->window);
7758
7759       g_free (new_name);
7760     }
7761
7762   if (gtk_widget_get_visible (GTK_WIDGET (entry)))
7763     gtk_widget_queue_resize (GTK_WIDGET (entry));
7764
7765   g_object_thaw_notify (G_OBJECT (entry));
7766 }
7767
7768 /**
7769  * gtk_entry_set_icon_from_gicon:
7770  * @entry: A #GtkEntry
7771  * @icon_pos: The position at which to set the icon
7772  * @icon: (allow-none): The icon to set, or %NULL
7773  *
7774  * Sets the icon shown in the entry at the specified position
7775  * from the current icon theme.
7776  * If the icon isn't known, a "broken image" icon will be displayed
7777  * instead.
7778  *
7779  * If @icon is %NULL, no icon will be shown in the specified position.
7780  *
7781  * Since: 2.16
7782  */
7783 void
7784 gtk_entry_set_icon_from_gicon (GtkEntry             *entry,
7785                                GtkEntryIconPosition  icon_pos,
7786                                GIcon                *icon)
7787 {
7788   GtkEntryPrivate *priv;
7789   EntryIconInfo *icon_info;
7790
7791   g_return_if_fail (GTK_IS_ENTRY (entry));
7792   g_return_if_fail (IS_VALID_ICON_POSITION (icon_pos));
7793
7794   priv = entry->priv;
7795
7796   if ((icon_info = priv->icons[icon_pos]) == NULL)
7797     icon_info = construct_icon_info (GTK_WIDGET (entry), icon_pos);
7798
7799   g_object_freeze_notify (G_OBJECT (entry));
7800
7801   /* need to ref before clearing */
7802   if (icon)
7803     g_object_ref (icon);
7804
7805   gtk_entry_clear (entry, icon_pos);
7806
7807   if (icon)
7808     {
7809       _gtk_icon_helper_set_gicon (icon_info->icon_helper, icon, GTK_ICON_SIZE_MENU);
7810
7811       if (icon_pos == GTK_ENTRY_ICON_PRIMARY)
7812         {
7813           g_object_notify (G_OBJECT (entry), "primary-icon-gicon");
7814           g_object_notify (G_OBJECT (entry), "primary-icon-storage-type");
7815         }
7816       else
7817         {
7818           g_object_notify (G_OBJECT (entry), "secondary-icon-gicon");
7819           g_object_notify (G_OBJECT (entry), "secondary-icon-storage-type");
7820         }
7821
7822       if (gtk_widget_get_mapped (GTK_WIDGET (entry)))
7823           gdk_window_show_unraised (icon_info->window);
7824
7825       g_object_unref (icon);
7826     }
7827
7828   if (gtk_widget_get_visible (GTK_WIDGET (entry)))
7829     gtk_widget_queue_resize (GTK_WIDGET (entry));
7830
7831   g_object_thaw_notify (G_OBJECT (entry));
7832 }
7833
7834 /**
7835  * gtk_entry_set_icon_activatable:
7836  * @entry: A #GtkEntry
7837  * @icon_pos: Icon position
7838  * @activatable: %TRUE if the icon should be activatable
7839  *
7840  * Sets whether the icon is activatable.
7841  *
7842  * Since: 2.16
7843  */
7844 void
7845 gtk_entry_set_icon_activatable (GtkEntry             *entry,
7846                                 GtkEntryIconPosition  icon_pos,
7847                                 gboolean              activatable)
7848 {
7849   GtkEntryPrivate *priv;
7850   EntryIconInfo *icon_info;
7851
7852   g_return_if_fail (GTK_IS_ENTRY (entry));
7853   g_return_if_fail (IS_VALID_ICON_POSITION (icon_pos));
7854
7855   priv = entry->priv;
7856
7857   if ((icon_info = priv->icons[icon_pos]) == NULL)
7858     icon_info = construct_icon_info (GTK_WIDGET (entry), icon_pos);
7859
7860   activatable = activatable != FALSE;
7861
7862   if (icon_info->nonactivatable != !activatable)
7863     {
7864       icon_info->nonactivatable = !activatable;
7865
7866       if (gtk_widget_get_realized (GTK_WIDGET (entry)))
7867         update_cursors (GTK_WIDGET (entry));
7868
7869       g_object_notify (G_OBJECT (entry),
7870                        icon_pos == GTK_ENTRY_ICON_PRIMARY ? "primary-icon-activatable" : "secondary-icon-activatable");
7871     }
7872 }
7873
7874 /**
7875  * gtk_entry_get_icon_activatable:
7876  * @entry: a #GtkEntry
7877  * @icon_pos: Icon position
7878  *
7879  * Returns whether the icon is activatable.
7880  *
7881  * Returns: %TRUE if the icon is activatable.
7882  *
7883  * Since: 2.16
7884  */
7885 gboolean
7886 gtk_entry_get_icon_activatable (GtkEntry             *entry,
7887                                 GtkEntryIconPosition  icon_pos)
7888 {
7889   GtkEntryPrivate *priv;
7890   EntryIconInfo *icon_info;
7891
7892   g_return_val_if_fail (GTK_IS_ENTRY (entry), FALSE);
7893   g_return_val_if_fail (IS_VALID_ICON_POSITION (icon_pos), FALSE);
7894
7895   priv = entry->priv;
7896   icon_info = priv->icons[icon_pos];
7897
7898   return (!icon_info || !icon_info->nonactivatable);
7899 }
7900
7901 /**
7902  * gtk_entry_get_icon_pixbuf:
7903  * @entry: A #GtkEntry
7904  * @icon_pos: Icon position
7905  *
7906  * Retrieves the image used for the icon.
7907  *
7908  * Unlike the other methods of setting and getting icon data, this
7909  * method will work regardless of whether the icon was set using a
7910  * #GdkPixbuf, a #GIcon, a stock item, or an icon name.
7911  *
7912  * Returns: (transfer none): A #GdkPixbuf, or %NULL if no icon is
7913  *     set for this position.
7914  *
7915  * Since: 2.16
7916  */
7917 GdkPixbuf *
7918 gtk_entry_get_icon_pixbuf (GtkEntry             *entry,
7919                            GtkEntryIconPosition  icon_pos)
7920 {
7921   GtkEntryPrivate *priv;
7922   EntryIconInfo *icon_info;
7923   GdkPixbuf *pixbuf;
7924
7925   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
7926   g_return_val_if_fail (IS_VALID_ICON_POSITION (icon_pos), NULL);
7927
7928   priv = entry->priv;
7929
7930   icon_info = priv->icons[icon_pos];
7931
7932   if (!icon_info)
7933     return NULL;
7934
7935   /* HACK: unfortunately this is transfer none, so we need to return
7936    * the icon helper's cache ref directly.
7937    */
7938   pixbuf = gtk_entry_ensure_pixbuf (entry, icon_pos);
7939   g_object_unref (pixbuf);
7940
7941   return pixbuf;
7942 }
7943
7944 /**
7945  * gtk_entry_get_icon_gicon:
7946  * @entry: A #GtkEntry
7947  * @icon_pos: Icon position
7948  *
7949  * Retrieves the #GIcon used for the icon, or %NULL if there is
7950  * no icon or if the icon was set by some other method (e.g., by
7951  * stock, pixbuf, or icon name).
7952  *
7953  * Returns: (transfer none): A #GIcon, or %NULL if no icon is set
7954  *     or if the icon is not a #GIcon
7955  *
7956  * Since: 2.16
7957  */
7958 GIcon *
7959 gtk_entry_get_icon_gicon (GtkEntry             *entry,
7960                           GtkEntryIconPosition  icon_pos)
7961 {
7962   GtkEntryPrivate *priv;
7963   EntryIconInfo *icon_info;
7964
7965   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
7966   g_return_val_if_fail (IS_VALID_ICON_POSITION (icon_pos), NULL);
7967
7968   priv = entry->priv;
7969   icon_info = priv->icons[icon_pos];
7970
7971   if (!icon_info)
7972     return NULL;
7973
7974   return _gtk_icon_helper_peek_gicon (icon_info->icon_helper);
7975 }
7976
7977 /**
7978  * gtk_entry_get_icon_stock:
7979  * @entry: A #GtkEntry
7980  * @icon_pos: Icon position
7981  *
7982  * Retrieves the stock id used for the icon, or %NULL if there is
7983  * no icon or if the icon was set by some other method (e.g., by
7984  * pixbuf, icon name or gicon).
7985  *
7986  * Returns: A stock id, or %NULL if no icon is set or if the icon
7987  *          wasn't set from a stock id
7988  *
7989  * Since: 2.16
7990  */
7991 const gchar *
7992 gtk_entry_get_icon_stock (GtkEntry             *entry,
7993                           GtkEntryIconPosition  icon_pos)
7994 {
7995   GtkEntryPrivate *priv;
7996   EntryIconInfo *icon_info;
7997
7998   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
7999   g_return_val_if_fail (IS_VALID_ICON_POSITION (icon_pos), NULL);
8000
8001   priv = entry->priv;
8002   icon_info = priv->icons[icon_pos];
8003
8004   if (!icon_info)
8005     return NULL;
8006
8007   return _gtk_icon_helper_get_stock_id (icon_info->icon_helper);
8008 }
8009
8010 /**
8011  * gtk_entry_get_icon_name:
8012  * @entry: A #GtkEntry
8013  * @icon_pos: Icon position
8014  *
8015  * Retrieves the icon name used for the icon, or %NULL if there is
8016  * no icon or if the icon was set by some other method (e.g., by
8017  * pixbuf, stock or gicon).
8018  *
8019  * Returns: An icon name, or %NULL if no icon is set or if the icon
8020  *          wasn't set from an icon name
8021  *
8022  * Since: 2.16
8023  */
8024 const gchar *
8025 gtk_entry_get_icon_name (GtkEntry             *entry,
8026                          GtkEntryIconPosition  icon_pos)
8027 {
8028   GtkEntryPrivate *priv;
8029   EntryIconInfo *icon_info;
8030
8031   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
8032   g_return_val_if_fail (IS_VALID_ICON_POSITION (icon_pos), NULL);
8033
8034   priv = entry->priv;
8035   icon_info = priv->icons[icon_pos];
8036
8037   if (!icon_info)
8038     return NULL;
8039
8040   return _gtk_icon_helper_get_icon_name (icon_info->icon_helper);
8041 }
8042
8043 /**
8044  * gtk_entry_set_icon_sensitive:
8045  * @entry: A #GtkEntry
8046  * @icon_pos: Icon position
8047  * @sensitive: Specifies whether the icon should appear
8048  *             sensitive or insensitive
8049  *
8050  * Sets the sensitivity for the specified icon.
8051  *
8052  * Since: 2.16
8053  */
8054 void
8055 gtk_entry_set_icon_sensitive (GtkEntry             *entry,
8056                               GtkEntryIconPosition  icon_pos,
8057                               gboolean              sensitive)
8058 {
8059   GtkEntryPrivate *priv;
8060   EntryIconInfo *icon_info;
8061
8062   g_return_if_fail (GTK_IS_ENTRY (entry));
8063   g_return_if_fail (IS_VALID_ICON_POSITION (icon_pos));
8064
8065   priv = entry->priv;
8066
8067   if ((icon_info = priv->icons[icon_pos]) == NULL)
8068     icon_info = construct_icon_info (GTK_WIDGET (entry), icon_pos);
8069
8070   if (icon_info->insensitive != !sensitive)
8071     {
8072       icon_info->insensitive = !sensitive;
8073
8074       icon_info->pressed = FALSE;
8075       icon_info->prelight = FALSE;
8076
8077       if (gtk_widget_get_realized (GTK_WIDGET (entry)))
8078         update_cursors (GTK_WIDGET (entry));
8079
8080       gtk_widget_queue_draw (GTK_WIDGET (entry));
8081
8082       g_object_notify (G_OBJECT (entry),
8083                        icon_pos == GTK_ENTRY_ICON_PRIMARY ? "primary-icon-sensitive" : "secondary-icon-sensitive");
8084     }
8085 }
8086
8087 /**
8088  * gtk_entry_get_icon_sensitive:
8089  * @entry: a #GtkEntry
8090  * @icon_pos: Icon position
8091  *
8092  * Returns whether the icon appears sensitive or insensitive.
8093  *
8094  * Returns: %TRUE if the icon is sensitive.
8095  *
8096  * Since: 2.16
8097  */
8098 gboolean
8099 gtk_entry_get_icon_sensitive (GtkEntry             *entry,
8100                               GtkEntryIconPosition  icon_pos)
8101 {
8102   GtkEntryPrivate *priv;
8103   EntryIconInfo *icon_info;
8104
8105   g_return_val_if_fail (GTK_IS_ENTRY (entry), TRUE);
8106   g_return_val_if_fail (IS_VALID_ICON_POSITION (icon_pos), TRUE);
8107
8108   priv = entry->priv;
8109
8110   icon_info = priv->icons[icon_pos];
8111
8112   return (!icon_info || !icon_info->insensitive);
8113
8114 }
8115
8116 /**
8117  * gtk_entry_get_icon_storage_type:
8118  * @entry: a #GtkEntry
8119  * @icon_pos: Icon position
8120  *
8121  * Gets the type of representation being used by the icon
8122  * to store image data. If the icon has no image data,
8123  * the return value will be %GTK_IMAGE_EMPTY.
8124  *
8125  * Return value: image representation being used
8126  *
8127  * Since: 2.16
8128  **/
8129 GtkImageType
8130 gtk_entry_get_icon_storage_type (GtkEntry             *entry,
8131                                  GtkEntryIconPosition  icon_pos)
8132 {
8133   GtkEntryPrivate *priv;
8134   EntryIconInfo *icon_info;
8135
8136   g_return_val_if_fail (GTK_IS_ENTRY (entry), GTK_IMAGE_EMPTY);
8137   g_return_val_if_fail (IS_VALID_ICON_POSITION (icon_pos), GTK_IMAGE_EMPTY);
8138
8139   priv = entry->priv;
8140
8141   icon_info = priv->icons[icon_pos];
8142
8143   if (!icon_info)
8144     return GTK_IMAGE_EMPTY;
8145
8146   return _gtk_icon_helper_get_storage_type (icon_info->icon_helper);
8147 }
8148
8149 /**
8150  * gtk_entry_get_icon_at_pos:
8151  * @entry: a #GtkEntry
8152  * @x: the x coordinate of the position to find
8153  * @y: the y coordinate of the position to find
8154  *
8155  * Finds the icon at the given position and return its index. The
8156  * position's coordinates are relative to the @entry's top left corner.
8157  * If @x, @y doesn't lie inside an icon, -1 is returned.
8158  * This function is intended for use in a #GtkWidget::query-tooltip
8159  * signal handler.
8160  *
8161  * Returns: the index of the icon at the given position, or -1
8162  *
8163  * Since: 2.16
8164  */
8165 gint
8166 gtk_entry_get_icon_at_pos (GtkEntry *entry,
8167                            gint      x,
8168                            gint      y)
8169 {
8170   GtkAllocation primary;
8171   GtkAllocation secondary;
8172   gint frame_x, frame_y;
8173
8174   g_return_val_if_fail (GTK_IS_ENTRY (entry), -1);
8175
8176   get_frame_size (entry, FALSE, &frame_x, &frame_y, NULL, NULL);
8177   x -= frame_x;
8178   y -= frame_y;
8179
8180   get_icon_allocations (entry, &primary, &secondary);
8181
8182   if (primary.x <= x && x < primary.x + primary.width &&
8183       primary.y <= y && y < primary.y + primary.height)
8184     return GTK_ENTRY_ICON_PRIMARY;
8185
8186   if (secondary.x <= x && x < secondary.x + secondary.width &&
8187       secondary.y <= y && y < secondary.y + secondary.height)
8188     return GTK_ENTRY_ICON_SECONDARY;
8189
8190   return -1;
8191 }
8192
8193 /**
8194  * gtk_entry_set_icon_drag_source:
8195  * @entry: a #GtkIconEntry
8196  * @icon_pos: icon position
8197  * @target_list: the targets (data formats) in which the data can be provided
8198  * @actions: a bitmask of the allowed drag actions
8199  *
8200  * Sets up the icon at the given position so that GTK+ will start a drag
8201  * operation when the user clicks and drags the icon.
8202  *
8203  * To handle the drag operation, you need to connect to the usual
8204  * #GtkWidget::drag-data-get (or possibly #GtkWidget::drag-data-delete)
8205  * signal, and use gtk_entry_get_current_icon_drag_source() in
8206  * your signal handler to find out if the drag was started from
8207  * an icon.
8208  *
8209  * By default, GTK+ uses the icon as the drag icon. You can use the 
8210  * #GtkWidget::drag-begin signal to set a different icon. Note that you 
8211  * have to use g_signal_connect_after() to ensure that your signal handler
8212  * gets executed after the default handler.
8213  *
8214  * Since: 2.16
8215  */
8216 void
8217 gtk_entry_set_icon_drag_source (GtkEntry             *entry,
8218                                 GtkEntryIconPosition  icon_pos,
8219                                 GtkTargetList        *target_list,
8220                                 GdkDragAction         actions)
8221 {
8222   GtkEntryPrivate *priv;
8223   EntryIconInfo *icon_info;
8224
8225   g_return_if_fail (GTK_IS_ENTRY (entry));
8226   g_return_if_fail (IS_VALID_ICON_POSITION (icon_pos));
8227
8228   priv = entry->priv;
8229
8230   if ((icon_info = priv->icons[icon_pos]) == NULL)
8231     icon_info = construct_icon_info (GTK_WIDGET (entry), icon_pos);
8232
8233   if (icon_info->target_list)
8234     gtk_target_list_unref (icon_info->target_list);
8235   icon_info->target_list = target_list;
8236   if (icon_info->target_list)
8237     gtk_target_list_ref (icon_info->target_list);
8238
8239   icon_info->actions = actions;
8240 }
8241
8242 /**
8243  * gtk_entry_get_current_icon_drag_source:
8244  * @entry: a #GtkIconEntry
8245  *
8246  * Returns the index of the icon which is the source of the current
8247  * DND operation, or -1.
8248  *
8249  * This function is meant to be used in a #GtkWidget::drag-data-get
8250  * callback.
8251  *
8252  * Returns: index of the icon which is the source of the current
8253  *          DND operation, or -1.
8254  *
8255  * Since: 2.16
8256  */
8257 gint
8258 gtk_entry_get_current_icon_drag_source (GtkEntry *entry)
8259 {
8260   GtkEntryPrivate *priv;
8261   EntryIconInfo *icon_info = NULL;
8262   gint i;
8263
8264   g_return_val_if_fail (GTK_IS_ENTRY (entry), -1);
8265
8266   priv = entry->priv;
8267
8268   for (i = 0; i < MAX_ICONS; i++)
8269     {
8270       if ((icon_info = priv->icons[i]))
8271         {
8272           if (icon_info->in_drag)
8273             return i;
8274         }
8275     }
8276
8277   return -1;
8278 }
8279
8280 /**
8281  * gtk_entry_get_icon_area:
8282  * @entry: A #GtkEntry
8283  * @icon_pos: Icon position
8284  * @icon_area: (out): Return location for the icon's area
8285  *
8286  * Gets the area where entry's icon at @icon_pos is drawn.
8287  * This function is useful when drawing something to the
8288  * entry in a draw callback.
8289  *
8290  * If the entry is not realized or has no icon at the given position,
8291  * @icon_area is filled with zeros.
8292  *
8293  * See also gtk_entry_get_text_area()
8294  *
8295  * Since: 3.0
8296  */
8297 void
8298 gtk_entry_get_icon_area (GtkEntry             *entry,
8299                          GtkEntryIconPosition  icon_pos,
8300                          GdkRectangle         *icon_area)
8301 {
8302   GtkEntryPrivate *priv;
8303   EntryIconInfo *icon_info;
8304
8305   g_return_if_fail (GTK_IS_ENTRY (entry));
8306   g_return_if_fail (icon_area != NULL);
8307
8308   priv = entry->priv;
8309
8310   icon_info = priv->icons[icon_pos];
8311
8312   if (icon_info)
8313     {
8314       GtkAllocation primary;
8315       GtkAllocation secondary;
8316
8317       get_icon_allocations (entry, &primary, &secondary);
8318
8319       if (icon_pos == GTK_ENTRY_ICON_PRIMARY)
8320         *icon_area = primary;
8321       else
8322         *icon_area = secondary;
8323     }
8324   else
8325     {
8326       icon_area->x = 0;
8327       icon_area->y = 0;
8328       icon_area->width = 0;
8329       icon_area->height = 0;
8330     }
8331 }
8332
8333 static void
8334 ensure_has_tooltip (GtkEntry *entry)
8335 {
8336   GtkEntryPrivate *priv;
8337   EntryIconInfo *icon_info;
8338   int i;
8339   gboolean has_tooltip = FALSE;
8340
8341   priv = entry->priv;
8342
8343   for (i = 0; i < MAX_ICONS; i++)
8344     {
8345       if ((icon_info = priv->icons[i]) != NULL)
8346         {
8347           if (icon_info->tooltip != NULL)
8348             {
8349               has_tooltip = TRUE;
8350               break;
8351             }
8352         }
8353     }
8354
8355   gtk_widget_set_has_tooltip (GTK_WIDGET (entry), has_tooltip);
8356 }
8357
8358 /**
8359  * gtk_entry_get_icon_tooltip_text:
8360  * @entry: a #GtkEntry
8361  * @icon_pos: the icon position
8362  *
8363  * Gets the contents of the tooltip on the icon at the specified 
8364  * position in @entry.
8365  * 
8366  * Returns: the tooltip text, or %NULL. Free the returned string
8367  *     with g_free() when done.
8368  * 
8369  * Since: 2.16
8370  */
8371 gchar *
8372 gtk_entry_get_icon_tooltip_text (GtkEntry             *entry,
8373                                  GtkEntryIconPosition  icon_pos)
8374 {
8375   GtkEntryPrivate *priv;
8376   EntryIconInfo *icon_info;
8377   gchar *text = NULL;
8378
8379   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
8380   g_return_val_if_fail (IS_VALID_ICON_POSITION (icon_pos), NULL);
8381
8382   priv = entry->priv;
8383
8384   icon_info = priv->icons[icon_pos];
8385
8386   if (!icon_info)
8387     return NULL;
8388  
8389   if (icon_info->tooltip && 
8390       !pango_parse_markup (icon_info->tooltip, -1, 0, NULL, &text, NULL, NULL))
8391     g_assert (NULL == text); /* text should still be NULL in case of markup errors */
8392
8393   return text;
8394 }
8395
8396 /**
8397  * gtk_entry_set_icon_tooltip_text:
8398  * @entry: a #GtkEntry
8399  * @icon_pos: the icon position
8400  * @tooltip: (allow-none): the contents of the tooltip for the icon, or %NULL
8401  *
8402  * Sets @tooltip as the contents of the tooltip for the icon
8403  * at the specified position.
8404  *
8405  * Use %NULL for @tooltip to remove an existing tooltip.
8406  *
8407  * See also gtk_widget_set_tooltip_text() and 
8408  * gtk_entry_set_icon_tooltip_markup().
8409  *
8410  * Since: 2.16
8411  */
8412 void
8413 gtk_entry_set_icon_tooltip_text (GtkEntry             *entry,
8414                                  GtkEntryIconPosition  icon_pos,
8415                                  const gchar          *tooltip)
8416 {
8417   GtkEntryPrivate *priv;
8418   EntryIconInfo *icon_info;
8419
8420   g_return_if_fail (GTK_IS_ENTRY (entry));
8421   g_return_if_fail (IS_VALID_ICON_POSITION (icon_pos));
8422
8423   priv = entry->priv;
8424
8425   if ((icon_info = priv->icons[icon_pos]) == NULL)
8426     icon_info = construct_icon_info (GTK_WIDGET (entry), icon_pos);
8427
8428   if (icon_info->tooltip)
8429     g_free (icon_info->tooltip);
8430
8431   /* Treat an empty string as a NULL string,
8432    * because an empty string would be useless for a tooltip:
8433    */
8434   if (tooltip && tooltip[0] == '\0')
8435     tooltip = NULL;
8436
8437   icon_info->tooltip = tooltip ? g_markup_escape_text (tooltip, -1) : NULL;
8438
8439   ensure_has_tooltip (entry);
8440 }
8441
8442 /**
8443  * gtk_entry_get_icon_tooltip_markup:
8444  * @entry: a #GtkEntry
8445  * @icon_pos: the icon position
8446  *
8447  * Gets the contents of the tooltip on the icon at the specified 
8448  * position in @entry.
8449  * 
8450  * Returns: the tooltip text, or %NULL. Free the returned string
8451  *     with g_free() when done.
8452  * 
8453  * Since: 2.16
8454  */
8455 gchar *
8456 gtk_entry_get_icon_tooltip_markup (GtkEntry             *entry,
8457                                    GtkEntryIconPosition  icon_pos)
8458 {
8459   GtkEntryPrivate *priv;
8460   EntryIconInfo *icon_info;
8461
8462   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
8463   g_return_val_if_fail (IS_VALID_ICON_POSITION (icon_pos), NULL);
8464
8465   priv = entry->priv;
8466
8467   icon_info = priv->icons[icon_pos];
8468
8469   if (!icon_info)
8470     return NULL;
8471  
8472   return g_strdup (icon_info->tooltip);
8473 }
8474
8475 /**
8476  * gtk_entry_set_icon_tooltip_markup:
8477  * @entry: a #GtkEntry
8478  * @icon_pos: the icon position
8479  * @tooltip: (allow-none): the contents of the tooltip for the icon, or %NULL
8480  *
8481  * Sets @tooltip as the contents of the tooltip for the icon at
8482  * the specified position. @tooltip is assumed to be marked up with
8483  * the <link linkend="PangoMarkupFormat">Pango text markup language</link>.
8484  *
8485  * Use %NULL for @tooltip to remove an existing tooltip.
8486  *
8487  * See also gtk_widget_set_tooltip_markup() and 
8488  * gtk_enty_set_icon_tooltip_text().
8489  *
8490  * Since: 2.16
8491  */
8492 void
8493 gtk_entry_set_icon_tooltip_markup (GtkEntry             *entry,
8494                                    GtkEntryIconPosition  icon_pos,
8495                                    const gchar          *tooltip)
8496 {
8497   GtkEntryPrivate *priv;
8498   EntryIconInfo *icon_info;
8499
8500   g_return_if_fail (GTK_IS_ENTRY (entry));
8501   g_return_if_fail (IS_VALID_ICON_POSITION (icon_pos));
8502
8503   priv = entry->priv;
8504
8505   if ((icon_info = priv->icons[icon_pos]) == NULL)
8506     icon_info = construct_icon_info (GTK_WIDGET (entry), icon_pos);
8507
8508   if (icon_info->tooltip)
8509     g_free (icon_info->tooltip);
8510
8511   /* Treat an empty string as a NULL string,
8512    * because an empty string would be useless for a tooltip:
8513    */
8514   if (tooltip && tooltip[0] == '\0')
8515     tooltip = NULL;
8516
8517   icon_info->tooltip = g_strdup (tooltip);
8518
8519   ensure_has_tooltip (entry);
8520 }
8521
8522 static gboolean
8523 gtk_entry_query_tooltip (GtkWidget  *widget,
8524                          gint        x,
8525                          gint        y,
8526                          gboolean    keyboard_tip,
8527                          GtkTooltip *tooltip)
8528 {
8529   GtkEntry *entry;
8530   GtkEntryPrivate *priv;
8531   EntryIconInfo *icon_info;
8532   gint icon_pos;
8533
8534   entry = GTK_ENTRY (widget);
8535   priv = entry->priv;
8536
8537   if (!keyboard_tip)
8538     {
8539       icon_pos = gtk_entry_get_icon_at_pos (entry, x, y);
8540       if (icon_pos != -1)
8541         {
8542           if ((icon_info = priv->icons[icon_pos]) != NULL)
8543             {
8544               if (icon_info->tooltip)
8545                 {
8546                   gtk_tooltip_set_markup (tooltip, icon_info->tooltip);
8547                   return TRUE;
8548                 }
8549
8550               return FALSE;
8551             }
8552         }
8553     }
8554
8555   return GTK_WIDGET_CLASS (gtk_entry_parent_class)->query_tooltip (widget,
8556                                                                    x, y,
8557                                                                    keyboard_tip,
8558                                                                    tooltip);
8559 }
8560
8561
8562 /* Quick hack of a popup menu
8563  */
8564 static void
8565 activate_cb (GtkWidget *menuitem,
8566              GtkEntry  *entry)
8567 {
8568   const gchar *signal = g_object_get_data (G_OBJECT (menuitem), "gtk-signal");
8569   g_signal_emit_by_name (entry, signal);
8570 }
8571
8572
8573 static gboolean
8574 gtk_entry_mnemonic_activate (GtkWidget *widget,
8575                              gboolean   group_cycling)
8576 {
8577   gtk_widget_grab_focus (widget);
8578   return TRUE;
8579 }
8580
8581 static void
8582 append_action_signal (GtkEntry     *entry,
8583                       GtkWidget    *menu,
8584                       const gchar  *stock_id,
8585                       const gchar  *signal,
8586                       gboolean      sensitive)
8587 {
8588   GtkWidget *menuitem = gtk_image_menu_item_new_from_stock (stock_id, NULL);
8589
8590   g_object_set_data (G_OBJECT (menuitem), I_("gtk-signal"), (char *)signal);
8591   g_signal_connect (menuitem, "activate",
8592                     G_CALLBACK (activate_cb), entry);
8593
8594   gtk_widget_set_sensitive (menuitem, sensitive);
8595   
8596   gtk_widget_show (menuitem);
8597   gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
8598 }
8599         
8600 static void
8601 popup_menu_detach (GtkWidget *attach_widget,
8602                    GtkMenu   *menu)
8603 {
8604   GtkEntry *entry_attach = GTK_ENTRY (attach_widget);
8605   GtkEntryPrivate *priv_attach = entry_attach->priv;
8606
8607   priv_attach->popup_menu = NULL;
8608 }
8609
8610 static void
8611 popup_position_func (GtkMenu   *menu,
8612                      gint      *x,
8613                      gint      *y,
8614                      gboolean  *push_in,
8615                      gpointer   user_data)
8616 {
8617   GtkEntry *entry = GTK_ENTRY (user_data);
8618   GtkEntryPrivate *priv = entry->priv;
8619   GtkWidget *widget = GTK_WIDGET (entry);
8620   GdkScreen *screen;
8621   GtkRequisition menu_req;
8622   GdkRectangle monitor;
8623   GtkBorder borders;
8624   gint monitor_num, strong_x, height;
8625  
8626   g_return_if_fail (gtk_widget_get_realized (widget));
8627
8628   gdk_window_get_origin (priv->text_area, x, y);
8629
8630   screen = gtk_widget_get_screen (widget);
8631   monitor_num = gdk_screen_get_monitor_at_window (screen, priv->text_area);
8632   if (monitor_num < 0)
8633     monitor_num = 0;
8634   gtk_menu_set_monitor (menu, monitor_num);
8635
8636   gdk_screen_get_monitor_workarea (screen, monitor_num, &monitor);
8637   gtk_widget_get_preferred_size (priv->popup_menu,
8638                                  &menu_req, NULL);
8639   height = gdk_window_get_height (priv->text_area);
8640   gtk_entry_get_cursor_locations (entry, CURSOR_STANDARD, &strong_x, NULL);
8641   _gtk_entry_get_borders (entry, &borders);
8642
8643   *x += borders.left + strong_x - priv->scroll_offset;
8644   if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
8645     *x -= menu_req.width;
8646
8647   if ((*y + height + menu_req.height) <= monitor.y + monitor.height)
8648     *y += height;
8649   else if ((*y - menu_req.height) >= monitor.y)
8650     *y -= menu_req.height;
8651   else if (monitor.y + monitor.height - (*y + height) > *y)
8652     *y += height;
8653   else
8654     *y -= menu_req.height;
8655
8656   *push_in = FALSE;
8657 }
8658
8659 static void
8660 unichar_chosen_func (const char *text,
8661                      gpointer    data)
8662 {
8663   GtkEntry *entry = GTK_ENTRY (data);
8664   GtkEntryPrivate *priv = entry->priv;
8665
8666   if (priv->editable)
8667     gtk_entry_enter_text (entry, text);
8668 }
8669
8670 typedef struct
8671 {
8672   GtkEntry *entry;
8673   gint button;
8674   guint time;
8675   GdkDevice *device;
8676 } PopupInfo;
8677
8678 static void
8679 popup_targets_received (GtkClipboard     *clipboard,
8680                         GtkSelectionData *data,
8681                         gpointer          user_data)
8682 {
8683   PopupInfo *info = user_data;
8684   GtkEntry *entry = info->entry;
8685   GtkEntryPrivate *info_entry_priv = entry->priv;
8686
8687   if (gtk_widget_get_realized (GTK_WIDGET (entry)))
8688     {
8689       DisplayMode mode;
8690       gboolean clipboard_contains_text;
8691       GtkWidget *menuitem;
8692       GtkWidget *submenu;
8693       gboolean show_input_method_menu;
8694       gboolean show_unicode_menu;
8695       
8696       clipboard_contains_text = gtk_selection_data_targets_include_text (data);
8697       if (info_entry_priv->popup_menu)
8698         gtk_widget_destroy (info_entry_priv->popup_menu);
8699
8700       info_entry_priv->popup_menu = gtk_menu_new ();
8701
8702       gtk_menu_attach_to_widget (GTK_MENU (info_entry_priv->popup_menu),
8703                                  GTK_WIDGET (entry),
8704                                  popup_menu_detach);
8705       
8706       mode = gtk_entry_get_display_mode (entry);
8707       append_action_signal (entry, info_entry_priv->popup_menu, GTK_STOCK_CUT, "cut-clipboard",
8708                             info_entry_priv->editable && mode == DISPLAY_NORMAL &&
8709                             info_entry_priv->current_pos != info_entry_priv->selection_bound);
8710
8711       append_action_signal (entry, info_entry_priv->popup_menu, GTK_STOCK_COPY, "copy-clipboard",
8712                             mode == DISPLAY_NORMAL &&
8713                             info_entry_priv->current_pos != info_entry_priv->selection_bound);
8714
8715       append_action_signal (entry, info_entry_priv->popup_menu, GTK_STOCK_PASTE, "paste-clipboard",
8716                             info_entry_priv->editable && clipboard_contains_text);
8717
8718       menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_DELETE, NULL);
8719       gtk_widget_set_sensitive (menuitem, info_entry_priv->editable && info_entry_priv->current_pos != info_entry_priv->selection_bound);
8720       g_signal_connect_swapped (menuitem, "activate",
8721                                 G_CALLBACK (gtk_entry_delete_cb), entry);
8722       gtk_widget_show (menuitem);
8723       gtk_menu_shell_append (GTK_MENU_SHELL (info_entry_priv->popup_menu), menuitem);
8724
8725       menuitem = gtk_separator_menu_item_new ();
8726       gtk_widget_show (menuitem);
8727       gtk_menu_shell_append (GTK_MENU_SHELL (info_entry_priv->popup_menu), menuitem);
8728       
8729       menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_SELECT_ALL, NULL);
8730       gtk_widget_set_sensitive (menuitem, gtk_entry_buffer_get_length (info_entry_priv->buffer) > 0);
8731       g_signal_connect_swapped (menuitem, "activate",
8732                                 G_CALLBACK (gtk_entry_select_all), entry);
8733       gtk_widget_show (menuitem);
8734       gtk_menu_shell_append (GTK_MENU_SHELL (info_entry_priv->popup_menu), menuitem);
8735       
8736       g_object_get (gtk_widget_get_settings (GTK_WIDGET (entry)),
8737                     "gtk-show-input-method-menu", &show_input_method_menu,
8738                     "gtk-show-unicode-menu", &show_unicode_menu,
8739                     NULL);
8740
8741       if (show_input_method_menu || show_unicode_menu)
8742         {
8743           menuitem = gtk_separator_menu_item_new ();
8744           gtk_widget_show (menuitem);
8745           gtk_menu_shell_append (GTK_MENU_SHELL (info_entry_priv->popup_menu), menuitem);
8746         }
8747       
8748       if (show_input_method_menu)
8749         {
8750           menuitem = gtk_menu_item_new_with_mnemonic (_("Input _Methods"));
8751           gtk_widget_set_sensitive (menuitem, info_entry_priv->editable);
8752           gtk_widget_show (menuitem);
8753           submenu = gtk_menu_new ();
8754           gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
8755
8756           gtk_menu_shell_append (GTK_MENU_SHELL (info_entry_priv->popup_menu), menuitem);
8757
8758           gtk_im_multicontext_append_menuitems (GTK_IM_MULTICONTEXT (info_entry_priv->im_context),
8759                                                 GTK_MENU_SHELL (submenu));
8760         }
8761       
8762       if (show_unicode_menu)
8763         {
8764           menuitem = gtk_menu_item_new_with_mnemonic (_("_Insert Unicode Control Character"));
8765           gtk_widget_set_sensitive (menuitem, info_entry_priv->editable);
8766           gtk_widget_show (menuitem);
8767           
8768           submenu = gtk_menu_new ();
8769           gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
8770           gtk_menu_shell_append (GTK_MENU_SHELL (info_entry_priv->popup_menu), menuitem);
8771
8772           _gtk_text_util_append_special_char_menuitems (GTK_MENU_SHELL (submenu),
8773                                                         unichar_chosen_func,
8774                                                         entry);
8775         }
8776       
8777       g_signal_emit (entry,
8778                      signals[POPULATE_POPUP],
8779                      0,
8780                      info_entry_priv->popup_menu);
8781
8782
8783       if (info->device)
8784         gtk_menu_popup_for_device (GTK_MENU (info_entry_priv->popup_menu),
8785                         info->device, NULL, NULL, NULL, NULL, NULL,
8786                         info->button, info->time);
8787       else
8788         {
8789           gtk_menu_popup (GTK_MENU (info_entry_priv->popup_menu), NULL, NULL,
8790                           popup_position_func, entry,
8791                           0, gtk_get_current_event_time ());
8792           gtk_menu_shell_select_first (GTK_MENU_SHELL (info_entry_priv->popup_menu), FALSE);
8793         }
8794     }
8795
8796   g_object_unref (entry);
8797   g_slice_free (PopupInfo, info);
8798 }
8799                         
8800 static void
8801 gtk_entry_do_popup (GtkEntry       *entry,
8802                     GdkEventButton *event)
8803 {
8804   PopupInfo *info = g_slice_new (PopupInfo);
8805
8806   /* In order to know what entries we should make sensitive, we
8807    * ask for the current targets of the clipboard, and when
8808    * we get them, then we actually pop up the menu.
8809    */
8810   info->entry = g_object_ref (entry);
8811   
8812   if (event)
8813     {
8814       info->button = event->button;
8815       info->time = event->time;
8816       info->device = event->device;
8817     }
8818   else
8819     {
8820       info->button = 0;
8821       info->time = gtk_get_current_event_time ();
8822       info->device = NULL;
8823     }
8824
8825   gtk_clipboard_request_contents (gtk_widget_get_clipboard (GTK_WIDGET (entry), GDK_SELECTION_CLIPBOARD),
8826                                   gdk_atom_intern_static_string ("TARGETS"),
8827                                   popup_targets_received,
8828                                   info);
8829 }
8830
8831 static gboolean
8832 gtk_entry_popup_menu (GtkWidget *widget)
8833 {
8834   gtk_entry_do_popup (GTK_ENTRY (widget), NULL);
8835   return TRUE;
8836 }
8837
8838 static void
8839 gtk_entry_drag_begin (GtkWidget      *widget,
8840                       GdkDragContext *context)
8841 {
8842   GtkEntry *entry = GTK_ENTRY (widget);
8843   GtkEntryPrivate *priv = entry->priv;
8844   gint i;
8845
8846   for (i = 0; i < MAX_ICONS; i++)
8847     {
8848       EntryIconInfo *icon_info = priv->icons[i];
8849       
8850       if (icon_info != NULL)
8851         {
8852           if (icon_info->in_drag) 
8853             {
8854               GdkPixbuf *pix;
8855
8856               pix = _gtk_icon_helper_ensure_pixbuf
8857                 (icon_info->icon_helper,
8858                  gtk_widget_get_style_context (GTK_WIDGET (entry)));
8859               gtk_drag_set_icon_pixbuf (context, pix, -2, -2);
8860
8861               g_object_unref (pix);
8862             }
8863         }
8864     }
8865 }
8866
8867 static void
8868 gtk_entry_drag_end (GtkWidget      *widget,
8869                     GdkDragContext *context)
8870 {
8871   GtkEntry *entry = GTK_ENTRY (widget);
8872   GtkEntryPrivate *priv = entry->priv;
8873   gint i;
8874
8875   for (i = 0; i < MAX_ICONS; i++)
8876     {
8877       EntryIconInfo *icon_info = priv->icons[i];
8878
8879       if (icon_info != NULL)
8880         icon_info->in_drag = 0;
8881     }
8882 }
8883
8884 static void
8885 gtk_entry_drag_leave (GtkWidget        *widget,
8886                       GdkDragContext   *context,
8887                       guint             time)
8888 {
8889   GtkEntry *entry = GTK_ENTRY (widget);
8890   GtkEntryPrivate *priv = entry->priv;
8891
8892   priv->dnd_position = -1;
8893   gtk_widget_queue_draw (widget);
8894 }
8895
8896 static gboolean
8897 gtk_entry_drag_drop  (GtkWidget        *widget,
8898                       GdkDragContext   *context,
8899                       gint              x,
8900                       gint              y,
8901                       guint             time)
8902 {
8903   GtkEntry *entry = GTK_ENTRY (widget);
8904   GtkEntryPrivate *priv = entry->priv;
8905   GdkAtom target = GDK_NONE;
8906
8907   if (priv->editable)
8908     target = gtk_drag_dest_find_target (widget, context, NULL);
8909
8910   if (target != GDK_NONE)
8911     gtk_drag_get_data (widget, context, target, time);
8912   else
8913     gtk_drag_finish (context, FALSE, FALSE, time);
8914   
8915   return TRUE;
8916 }
8917
8918 static gboolean
8919 gtk_entry_drag_motion (GtkWidget        *widget,
8920                        GdkDragContext   *context,
8921                        gint              x,
8922                        gint              y,
8923                        guint             time)
8924 {
8925   GtkEntry *entry = GTK_ENTRY (widget);
8926   GtkEntryPrivate *priv = entry->priv;
8927   GtkStyleContext *style_context;
8928   GtkWidget *source_widget;
8929   GdkDragAction suggested_action;
8930   gint new_position, old_position;
8931   gint sel1, sel2;
8932   GtkBorder padding;
8933
8934   style_context = gtk_widget_get_style_context (widget);
8935   gtk_style_context_get_padding (style_context, 0, &padding);
8936   x -= padding.left;
8937   y -= padding.top;
8938
8939   old_position = priv->dnd_position;
8940   new_position = gtk_entry_find_position (entry, x + priv->scroll_offset);
8941
8942   if (priv->editable &&
8943       gtk_drag_dest_find_target (widget, context, NULL) != GDK_NONE)
8944     {
8945       source_widget = gtk_drag_get_source_widget (context);
8946       suggested_action = gdk_drag_context_get_suggested_action (context);
8947
8948       if (!gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &sel1, &sel2) ||
8949           new_position < sel1 || new_position > sel2)
8950         {
8951           if (source_widget == widget)
8952             {
8953               /* Default to MOVE, unless the user has
8954                * pressed ctrl or alt to affect available actions
8955                */
8956               if ((gdk_drag_context_get_actions (context) & GDK_ACTION_MOVE) != 0)
8957                 suggested_action = GDK_ACTION_MOVE;
8958             }
8959
8960           priv->dnd_position = new_position;
8961         }
8962       else
8963         {
8964           if (source_widget == widget)
8965             suggested_action = 0;       /* Can't drop in selection where drag started */
8966
8967           priv->dnd_position = -1;
8968         }
8969     }
8970   else
8971     {
8972       /* Entry not editable, or no text */
8973       suggested_action = 0;
8974       priv->dnd_position = -1;
8975     }
8976   
8977   gdk_drag_status (context, suggested_action, time);
8978
8979   if (priv->dnd_position != old_position)
8980     gtk_widget_queue_draw (widget);
8981
8982   return TRUE;
8983 }
8984
8985 static void
8986 gtk_entry_drag_data_received (GtkWidget        *widget,
8987                               GdkDragContext   *context,
8988                               gint              x,
8989                               gint              y,
8990                               GtkSelectionData *selection_data,
8991                               guint             info,
8992                               guint             time)
8993 {
8994   GtkEntry *entry = GTK_ENTRY (widget);
8995   GtkEntryPrivate *priv = entry->priv;
8996   GtkEditable *editable = GTK_EDITABLE (widget);
8997   GtkStyleContext *style_context;
8998   GtkBorder padding;
8999   gchar *str;
9000
9001   str = (gchar *) gtk_selection_data_get_text (selection_data);
9002
9003   style_context = gtk_widget_get_style_context (widget);
9004   gtk_style_context_get_padding (style_context, 0, &padding);
9005   x -= padding.left;
9006   y -= padding.top;
9007
9008   if (str && priv->editable)
9009     {
9010       gint new_position;
9011       gint sel1, sel2;
9012       gint length = -1;
9013
9014       if (priv->truncate_multiline)
9015         length = truncate_multiline (str);
9016
9017       new_position = gtk_entry_find_position (entry, x + priv->scroll_offset);
9018
9019       if (!gtk_editable_get_selection_bounds (editable, &sel1, &sel2) ||
9020           new_position < sel1 || new_position > sel2)
9021         {
9022           gtk_editable_insert_text (editable, str, length, &new_position);
9023         }
9024       else
9025         {
9026           /* Replacing selection */
9027           begin_change (entry);
9028           gtk_editable_delete_text (editable, sel1, sel2);
9029           gtk_editable_insert_text (editable, str, length, &sel1);
9030           end_change (entry);
9031         }
9032       
9033       gtk_drag_finish (context, TRUE, gdk_drag_context_get_selected_action (context) == GDK_ACTION_MOVE, time);
9034     }
9035   else
9036     {
9037       /* Drag and drop didn't happen! */
9038       gtk_drag_finish (context, FALSE, FALSE, time);
9039     }
9040
9041   g_free (str);
9042 }
9043
9044 static void
9045 gtk_entry_drag_data_get (GtkWidget        *widget,
9046                          GdkDragContext   *context,
9047                          GtkSelectionData *selection_data,
9048                          guint             info,
9049                          guint             time)
9050 {
9051   GtkEntry *entry = GTK_ENTRY (widget);
9052   GtkEntryPrivate *priv = entry->priv;
9053   GtkEditable *editable = GTK_EDITABLE (widget);
9054   gint sel_start, sel_end;
9055   gint i;
9056
9057   /* If there is an icon drag going on, exit early. */
9058   for (i = 0; i < MAX_ICONS; i++)
9059     {
9060       EntryIconInfo *icon_info = priv->icons[i];
9061
9062       if (icon_info != NULL)
9063         {
9064           if (icon_info->in_drag)
9065             return;
9066         }
9067     }
9068
9069   if (gtk_editable_get_selection_bounds (editable, &sel_start, &sel_end))
9070     {
9071       gchar *str = gtk_entry_get_display_text (GTK_ENTRY (widget), sel_start, sel_end);
9072
9073       gtk_selection_data_set_text (selection_data, str, -1);
9074       
9075       g_free (str);
9076     }
9077
9078 }
9079
9080 static void
9081 gtk_entry_drag_data_delete (GtkWidget      *widget,
9082                             GdkDragContext *context)
9083 {
9084   GtkEntry *entry = GTK_ENTRY (widget);
9085   GtkEntryPrivate *priv = entry->priv;
9086   GtkEditable *editable = GTK_EDITABLE (widget);
9087   gint sel_start, sel_end;
9088   gint i;
9089
9090   /* If there is an icon drag going on, exit early. */
9091   for (i = 0; i < MAX_ICONS; i++)
9092     {
9093       EntryIconInfo *icon_info = priv->icons[i];
9094
9095       if (icon_info != NULL)
9096         {
9097           if (icon_info->in_drag)
9098             return;
9099         }
9100     }
9101
9102   if (priv->editable &&
9103       gtk_editable_get_selection_bounds (editable, &sel_start, &sel_end))
9104     gtk_editable_delete_text (editable, sel_start, sel_end);
9105 }
9106
9107 /* We display the cursor when
9108  *
9109  *  - the selection is empty, AND
9110  *  - the widget has focus
9111  */
9112
9113 #define CURSOR_ON_MULTIPLIER 2
9114 #define CURSOR_OFF_MULTIPLIER 1
9115 #define CURSOR_PEND_MULTIPLIER 3
9116 #define CURSOR_DIVIDER 3
9117
9118 static gboolean
9119 cursor_blinks (GtkEntry *entry)
9120 {
9121   GtkEntryPrivate *priv = entry->priv;
9122
9123   if (gtk_widget_has_focus (GTK_WIDGET (entry)) &&
9124       priv->editable &&
9125       priv->selection_bound == priv->current_pos)
9126     {
9127       GtkSettings *settings;
9128       gboolean blink;
9129
9130       settings = gtk_widget_get_settings (GTK_WIDGET (entry));
9131       g_object_get (settings, "gtk-cursor-blink", &blink, NULL);
9132
9133       return blink;
9134     }
9135   else
9136     return FALSE;
9137 }
9138
9139 static gint
9140 get_cursor_time (GtkEntry *entry)
9141 {
9142   GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (entry));
9143   gint time;
9144
9145   g_object_get (settings, "gtk-cursor-blink-time", &time, NULL);
9146
9147   return time;
9148 }
9149
9150 static gint
9151 get_cursor_blink_timeout (GtkEntry *entry)
9152 {
9153   GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (entry));
9154   gint timeout;
9155
9156   g_object_get (settings, "gtk-cursor-blink-timeout", &timeout, NULL);
9157
9158   return timeout;
9159 }
9160
9161 static void
9162 show_cursor (GtkEntry *entry)
9163 {
9164   GtkEntryPrivate *priv = entry->priv;
9165   GtkWidget *widget;
9166
9167   if (!priv->cursor_visible)
9168     {
9169       priv->cursor_visible = TRUE;
9170
9171       widget = GTK_WIDGET (entry);
9172       if (gtk_widget_has_focus (widget) && priv->selection_bound == priv->current_pos)
9173         gtk_widget_queue_draw (widget);
9174     }
9175 }
9176
9177 static void
9178 hide_cursor (GtkEntry *entry)
9179 {
9180   GtkEntryPrivate *priv = entry->priv;
9181   GtkWidget *widget;
9182
9183   if (priv->cursor_visible)
9184     {
9185       priv->cursor_visible = FALSE;
9186
9187       widget = GTK_WIDGET (entry);
9188       if (gtk_widget_has_focus (widget) && priv->selection_bound == priv->current_pos)
9189         gtk_widget_queue_draw (widget);
9190     }
9191 }
9192
9193 /*
9194  * Blink!
9195  */
9196 static gint
9197 blink_cb (gpointer data)
9198 {
9199   GtkEntry *entry;
9200   GtkEntryPrivate *priv; 
9201   gint blink_timeout;
9202
9203   entry = GTK_ENTRY (data);
9204   priv = entry->priv;
9205  
9206   if (!gtk_widget_has_focus (GTK_WIDGET (entry)))
9207     {
9208       g_warning ("GtkEntry - did not receive focus-out-event. If you\n"
9209                  "connect a handler to this signal, it must return\n"
9210                  "FALSE so the entry gets the event as well");
9211
9212       gtk_entry_check_cursor_blink (entry);
9213
9214       return FALSE;
9215     }
9216   
9217   g_assert (priv->selection_bound == priv->current_pos);
9218   
9219   blink_timeout = get_cursor_blink_timeout (entry);
9220   if (priv->blink_time > 1000 * blink_timeout && 
9221       blink_timeout < G_MAXINT/1000) 
9222     {
9223       /* we've blinked enough without the user doing anything, stop blinking */
9224       show_cursor (entry);
9225       priv->blink_timeout = 0;
9226     } 
9227   else if (priv->cursor_visible)
9228     {
9229       hide_cursor (entry);
9230       priv->blink_timeout = gdk_threads_add_timeout (get_cursor_time (entry) * CURSOR_OFF_MULTIPLIER / CURSOR_DIVIDER,
9231                                             blink_cb,
9232                                             entry);
9233     }
9234   else
9235     {
9236       show_cursor (entry);
9237       priv->blink_time += get_cursor_time (entry);
9238       priv->blink_timeout = gdk_threads_add_timeout (get_cursor_time (entry) * CURSOR_ON_MULTIPLIER / CURSOR_DIVIDER,
9239                                             blink_cb,
9240                                             entry);
9241     }
9242
9243   /* Remove ourselves */
9244   return FALSE;
9245 }
9246
9247 static void
9248 gtk_entry_check_cursor_blink (GtkEntry *entry)
9249 {
9250   GtkEntryPrivate *priv = entry->priv;
9251
9252   if (cursor_blinks (entry))
9253     {
9254       if (!priv->blink_timeout)
9255         {
9256           show_cursor (entry);
9257           priv->blink_timeout = gdk_threads_add_timeout (get_cursor_time (entry) * CURSOR_ON_MULTIPLIER / CURSOR_DIVIDER,
9258                                                 blink_cb,
9259                                                 entry);
9260         }
9261     }
9262   else
9263     {
9264       if (priv->blink_timeout)
9265         {
9266           g_source_remove (priv->blink_timeout);
9267           priv->blink_timeout = 0;
9268         }
9269
9270       priv->cursor_visible = TRUE;
9271     }
9272 }
9273
9274 static void
9275 gtk_entry_pend_cursor_blink (GtkEntry *entry)
9276 {
9277   GtkEntryPrivate *priv = entry->priv;
9278
9279   if (cursor_blinks (entry))
9280     {
9281       if (priv->blink_timeout != 0)
9282         g_source_remove (priv->blink_timeout);
9283
9284       priv->blink_timeout = gdk_threads_add_timeout (get_cursor_time (entry) * CURSOR_PEND_MULTIPLIER / CURSOR_DIVIDER,
9285                                                      blink_cb,
9286                                                      entry);
9287       show_cursor (entry);
9288     }
9289 }
9290
9291 static void
9292 gtk_entry_reset_blink_time (GtkEntry *entry)
9293 {
9294   GtkEntryPrivate *priv = entry->priv;
9295
9296   priv->blink_time = 0;
9297 }
9298
9299
9300 /* completion */
9301 static gint
9302 gtk_entry_completion_timeout (gpointer data)
9303 {
9304   GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (data);
9305   GtkEntryPrivate *completion_entry_priv = GTK_ENTRY (completion->priv->entry)->priv;
9306
9307   completion->priv->completion_timeout = 0;
9308
9309   if (completion->priv->filter_model &&
9310       g_utf8_strlen (gtk_entry_get_text (GTK_ENTRY (completion->priv->entry)), -1)
9311       >= completion->priv->minimum_key_length)
9312     {
9313       gint matches;
9314       gint actions;
9315       GtkTreeSelection *s;
9316       gboolean popup_single;
9317
9318       gtk_entry_completion_complete (completion);
9319       matches = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->filter_model), NULL);
9320
9321       gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view)));
9322
9323       s = gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->action_view));
9324
9325       gtk_tree_selection_unselect_all (s);
9326
9327       actions = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->actions), NULL);
9328
9329       g_object_get (completion, "popup-single-match", &popup_single, NULL);
9330       if ((matches > (popup_single ? 0: 1)) || actions > 0)
9331         { 
9332           if (gtk_widget_get_visible (completion->priv->popup_window))
9333             _gtk_entry_completion_resize_popup (completion);
9334           else
9335             _gtk_entry_completion_popup (completion, completion_entry_priv->completion_device);
9336         }
9337       else 
9338         _gtk_entry_completion_popdown (completion);
9339     }
9340   else if (gtk_widget_get_visible (completion->priv->popup_window))
9341     _gtk_entry_completion_popdown (completion);
9342
9343   return FALSE;
9344 }
9345
9346 static inline gboolean
9347 keyval_is_cursor_move (guint keyval)
9348 {
9349   if (keyval == GDK_KEY_Up || keyval == GDK_KEY_KP_Up)
9350     return TRUE;
9351
9352   if (keyval == GDK_KEY_Down || keyval == GDK_KEY_KP_Down)
9353     return TRUE;
9354
9355   if (keyval == GDK_KEY_Page_Up)
9356     return TRUE;
9357
9358   if (keyval == GDK_KEY_Page_Down)
9359     return TRUE;
9360
9361   return FALSE;
9362 }
9363
9364 static gboolean
9365 gtk_entry_completion_key_press (GtkWidget   *widget,
9366                                 GdkEventKey *event,
9367                                 gpointer     user_data)
9368 {
9369   gint matches, actions = 0;
9370   GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
9371
9372   if (!gtk_widget_get_mapped (completion->priv->popup_window))
9373     return FALSE;
9374
9375   matches = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->filter_model), NULL);
9376
9377   if (completion->priv->actions)
9378     actions = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->actions), NULL);
9379
9380   if (keyval_is_cursor_move (event->keyval))
9381     {
9382       GtkTreePath *path = NULL;
9383       
9384       if (event->keyval == GDK_KEY_Up || event->keyval == GDK_KEY_KP_Up)
9385         {
9386           if (completion->priv->current_selected < 0)
9387             completion->priv->current_selected = matches + actions - 1;
9388           else
9389             completion->priv->current_selected--;
9390         }
9391       else if (event->keyval == GDK_KEY_Down || event->keyval == GDK_KEY_KP_Down)
9392         {
9393           if (completion->priv->current_selected < matches + actions - 1)
9394             completion->priv->current_selected++;
9395           else
9396             completion->priv->current_selected = -1;
9397         }
9398       else if (event->keyval == GDK_KEY_Page_Up)
9399         {
9400           if (completion->priv->current_selected < 0)
9401             completion->priv->current_selected = matches + actions - 1;
9402           else if (completion->priv->current_selected == 0)
9403             completion->priv->current_selected = -1;
9404           else if (completion->priv->current_selected < matches) 
9405             {
9406               completion->priv->current_selected -= PAGE_STEP;
9407               if (completion->priv->current_selected < 0)
9408                 completion->priv->current_selected = 0;
9409             }
9410           else 
9411             {
9412               completion->priv->current_selected -= PAGE_STEP;
9413               if (completion->priv->current_selected < matches - 1)
9414                 completion->priv->current_selected = matches - 1;
9415             }
9416         }
9417       else if (event->keyval == GDK_KEY_Page_Down)
9418         {
9419           if (completion->priv->current_selected < 0)
9420             completion->priv->current_selected = 0;
9421           else if (completion->priv->current_selected < matches - 1)
9422             {
9423               completion->priv->current_selected += PAGE_STEP;
9424               if (completion->priv->current_selected > matches - 1)
9425                 completion->priv->current_selected = matches - 1;
9426             }
9427           else if (completion->priv->current_selected == matches + actions - 1)
9428             {
9429               completion->priv->current_selected = -1;
9430             }
9431           else
9432             {
9433               completion->priv->current_selected += PAGE_STEP;
9434               if (completion->priv->current_selected > matches + actions - 1)
9435                 completion->priv->current_selected = matches + actions - 1;
9436             }
9437         }
9438
9439       if (completion->priv->current_selected < 0)
9440         {
9441           gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view)));
9442           gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->action_view)));
9443
9444           if (completion->priv->inline_selection &&
9445               completion->priv->completion_prefix)
9446             {
9447               gtk_entry_set_text (GTK_ENTRY (completion->priv->entry), 
9448                                   completion->priv->completion_prefix);
9449               gtk_editable_set_position (GTK_EDITABLE (widget), -1);
9450             }
9451         }
9452       else if (completion->priv->current_selected < matches)
9453         {
9454           gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->action_view)));
9455
9456           path = gtk_tree_path_new_from_indices (completion->priv->current_selected, -1);
9457           gtk_tree_view_set_cursor (GTK_TREE_VIEW (completion->priv->tree_view),
9458                                     path, NULL, FALSE);
9459
9460           if (completion->priv->inline_selection)
9461             {
9462
9463               GtkTreeIter iter;
9464               GtkTreeIter child_iter;
9465               GtkTreeModel *model = NULL;
9466               GtkTreeSelection *sel;
9467               gboolean entry_set;
9468
9469               sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view));
9470               if (!gtk_tree_selection_get_selected (sel, &model, &iter))
9471                 return FALSE;
9472
9473               gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_iter, &iter);
9474               model = gtk_tree_model_filter_get_model (GTK_TREE_MODEL_FILTER (model));
9475               
9476               if (completion->priv->completion_prefix == NULL)
9477                 completion->priv->completion_prefix = g_strdup (gtk_entry_get_text (GTK_ENTRY (completion->priv->entry)));
9478
9479               g_signal_emit_by_name (completion, "cursor-on-match", model,
9480                                      &child_iter, &entry_set);
9481             }
9482         }
9483       else if (completion->priv->current_selected - matches >= 0)
9484         {
9485           gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view)));
9486
9487           path = gtk_tree_path_new_from_indices (completion->priv->current_selected - matches, -1);
9488           gtk_tree_view_set_cursor (GTK_TREE_VIEW (completion->priv->action_view),
9489                                     path, NULL, FALSE);
9490
9491           if (completion->priv->inline_selection &&
9492               completion->priv->completion_prefix)
9493             {
9494               gtk_entry_set_text (GTK_ENTRY (completion->priv->entry), 
9495                                   completion->priv->completion_prefix);
9496               gtk_editable_set_position (GTK_EDITABLE (widget), -1);
9497             }
9498         }
9499
9500       gtk_tree_path_free (path);
9501
9502       return TRUE;
9503     }
9504   else if (event->keyval == GDK_KEY_Escape ||
9505            event->keyval == GDK_KEY_Left ||
9506            event->keyval == GDK_KEY_KP_Left ||
9507            event->keyval == GDK_KEY_Right ||
9508            event->keyval == GDK_KEY_KP_Right) 
9509     {
9510       gboolean retval = TRUE;
9511
9512       _gtk_entry_reset_im_context (GTK_ENTRY (widget));
9513       _gtk_entry_completion_popdown (completion);
9514
9515       if (completion->priv->current_selected < 0)
9516         {
9517           retval = FALSE;
9518           goto keypress_completion_out;
9519         }
9520       else if (completion->priv->inline_selection)
9521         {
9522           /* Escape rejects the tentative completion */
9523           if (event->keyval == GDK_KEY_Escape)
9524             {
9525               if (completion->priv->completion_prefix)
9526                 gtk_entry_set_text (GTK_ENTRY (completion->priv->entry), 
9527                                     completion->priv->completion_prefix);
9528               else 
9529                 gtk_entry_set_text (GTK_ENTRY (completion->priv->entry), "");
9530             }
9531
9532           /* Move the cursor to the end for Right/Esc, to the
9533              beginning for Left */
9534           if (event->keyval == GDK_KEY_Right ||
9535               event->keyval == GDK_KEY_KP_Right ||
9536               event->keyval == GDK_KEY_Escape)
9537             gtk_editable_set_position (GTK_EDITABLE (widget), -1);
9538           else
9539             gtk_editable_set_position (GTK_EDITABLE (widget), 0);
9540         }
9541
9542 keypress_completion_out:
9543       if (completion->priv->inline_selection)
9544         {
9545           g_free (completion->priv->completion_prefix);
9546           completion->priv->completion_prefix = NULL;
9547         }
9548
9549       return retval;
9550     }
9551   else if (event->keyval == GDK_KEY_Tab || 
9552            event->keyval == GDK_KEY_KP_Tab ||
9553            event->keyval == GDK_KEY_ISO_Left_Tab) 
9554     {
9555       _gtk_entry_reset_im_context (GTK_ENTRY (widget));
9556       _gtk_entry_completion_popdown (completion);
9557
9558       g_free (completion->priv->completion_prefix);
9559       completion->priv->completion_prefix = NULL;
9560
9561       return FALSE;
9562     }
9563   else if (event->keyval == GDK_KEY_ISO_Enter ||
9564            event->keyval == GDK_KEY_KP_Enter ||
9565            event->keyval == GDK_KEY_Return)
9566     {
9567       GtkTreeIter iter;
9568       GtkTreeModel *model = NULL;
9569       GtkTreeModel *child_model;
9570       GtkTreeIter child_iter;
9571       GtkTreeSelection *sel;
9572       gboolean retval = TRUE;
9573
9574       _gtk_entry_reset_im_context (GTK_ENTRY (widget));
9575       _gtk_entry_completion_popdown (completion);
9576
9577       if (completion->priv->current_selected < matches)
9578         {
9579           gboolean entry_set;
9580
9581           sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view));
9582           if (gtk_tree_selection_get_selected (sel, &model, &iter))
9583             {
9584               gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_iter, &iter);
9585               child_model = gtk_tree_model_filter_get_model (GTK_TREE_MODEL_FILTER (model));
9586               g_signal_handler_block (widget, completion->priv->changed_id);
9587               g_signal_emit_by_name (completion, "match-selected",
9588                                      child_model, &child_iter, &entry_set);
9589               g_signal_handler_unblock (widget, completion->priv->changed_id);
9590
9591               if (!entry_set)
9592                 {
9593                   gchar *str = NULL;
9594
9595                   gtk_tree_model_get (model, &iter,
9596                                       completion->priv->text_column, &str,
9597                                       -1);
9598
9599                   gtk_entry_set_text (GTK_ENTRY (widget), str);
9600
9601                   /* move the cursor to the end */
9602                   gtk_editable_set_position (GTK_EDITABLE (widget), -1);
9603
9604                   g_free (str);
9605                 }
9606             }
9607           else
9608             retval = FALSE;
9609         }
9610       else if (completion->priv->current_selected - matches >= 0)
9611         {
9612           sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->action_view));
9613           if (gtk_tree_selection_get_selected (sel, &model, &iter))
9614             {
9615               GtkTreePath *path;
9616
9617               path = gtk_tree_path_new_from_indices (completion->priv->current_selected - matches, -1);
9618               g_signal_emit_by_name (completion, "action-activated",
9619                                      gtk_tree_path_get_indices (path)[0]);
9620               gtk_tree_path_free (path);
9621             }
9622           else
9623             retval = FALSE;
9624         }
9625
9626       g_free (completion->priv->completion_prefix);
9627       completion->priv->completion_prefix = NULL;
9628
9629       return retval;
9630     }
9631
9632   return FALSE;
9633 }
9634
9635 static void
9636 gtk_entry_completion_changed (GtkWidget *widget,
9637                               gpointer   user_data)
9638 {
9639   GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
9640   GtkEntry *entry = GTK_ENTRY (widget);
9641   GtkEntryPrivate *priv = entry->priv;
9642   GdkDevice *device;
9643
9644   /* (re)install completion timeout */
9645   if (completion->priv->completion_timeout)
9646     g_source_remove (completion->priv->completion_timeout);
9647
9648   if (!gtk_entry_get_text (entry))
9649     return;
9650
9651   /* no need to normalize for this test */
9652   if (completion->priv->minimum_key_length > 0 &&
9653       strcmp ("", gtk_entry_get_text (entry)) == 0)
9654     {
9655       if (gtk_widget_get_visible (completion->priv->popup_window))
9656         _gtk_entry_completion_popdown (completion);
9657       return;
9658     }
9659
9660   device = gtk_get_current_event_device ();
9661
9662   if (device && gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
9663     device = gdk_device_get_associated_device (device);
9664
9665   if (device)
9666     priv->completion_device = device;
9667
9668   completion->priv->completion_timeout =
9669     gdk_threads_add_timeout (COMPLETION_TIMEOUT,
9670                    gtk_entry_completion_timeout,
9671                    completion);
9672 }
9673
9674 static gboolean
9675 check_completion_callback (GtkEntryCompletion *completion)
9676 {
9677   completion->priv->check_completion_idle = NULL;
9678   
9679   gtk_entry_completion_complete (completion);
9680   gtk_entry_completion_insert_prefix (completion);
9681
9682   return FALSE;
9683 }
9684
9685 static void
9686 clear_completion_callback (GtkEntry   *entry,
9687                            GParamSpec *pspec)
9688 {
9689   if (pspec->name == I_("cursor-position") ||
9690       pspec->name == I_("selection-bound"))
9691     {
9692       GtkEntryCompletion *completion = gtk_entry_get_completion (entry);
9693       
9694       completion->priv->has_completion = FALSE;
9695     }
9696 }
9697
9698 static gboolean
9699 accept_completion_callback (GtkEntry *entry)
9700 {
9701   GtkEntryCompletion *completion = gtk_entry_get_completion (entry);
9702
9703   if (completion->priv->has_completion)
9704     gtk_editable_set_position (GTK_EDITABLE (entry),
9705                                gtk_entry_buffer_get_length (get_buffer (entry)));
9706
9707   return FALSE;
9708 }
9709
9710 static void
9711 completion_insert_text_callback (GtkEntry           *entry,
9712                                  const gchar        *text,
9713                                  gint                length,
9714                                  gint                position,
9715                                  GtkEntryCompletion *completion)
9716 {
9717   /* idle to update the selection based on the file list */
9718   if (completion->priv->check_completion_idle == NULL)
9719     {
9720       completion->priv->check_completion_idle = g_idle_source_new ();
9721       g_source_set_priority (completion->priv->check_completion_idle, G_PRIORITY_HIGH);
9722       g_source_set_closure (completion->priv->check_completion_idle,
9723                             g_cclosure_new_object (G_CALLBACK (check_completion_callback),
9724                                                    G_OBJECT (completion)));
9725       g_source_attach (completion->priv->check_completion_idle, NULL);
9726     }
9727 }
9728
9729 static void
9730 completion_changed (GtkEntryCompletion *completion,
9731                     GParamSpec         *pspec,
9732                     gpointer            data)
9733 {
9734   GtkEntry *entry = GTK_ENTRY (data);
9735
9736   if (pspec->name == I_("popup-completion") ||
9737       pspec->name == I_("inline-completion"))
9738     {
9739       disconnect_completion_signals (entry, completion);
9740       connect_completion_signals (entry, completion);
9741     }
9742 }
9743
9744 static void
9745 disconnect_completion_signals (GtkEntry           *entry,
9746                                GtkEntryCompletion *completion)
9747 {
9748   g_signal_handlers_disconnect_by_func (completion, 
9749                                        G_CALLBACK (completion_changed), entry);
9750   if (completion->priv->changed_id > 0 &&
9751       g_signal_handler_is_connected (entry, completion->priv->changed_id))
9752     {
9753       g_signal_handler_disconnect (entry, completion->priv->changed_id);
9754       completion->priv->changed_id = 0;
9755     }
9756   g_signal_handlers_disconnect_by_func (entry, 
9757                                         G_CALLBACK (gtk_entry_completion_key_press), completion);
9758   if (completion->priv->insert_text_id > 0 &&
9759       g_signal_handler_is_connected (entry, completion->priv->insert_text_id))
9760     {
9761       g_signal_handler_disconnect (entry, completion->priv->insert_text_id);
9762       completion->priv->insert_text_id = 0;
9763     }
9764   g_signal_handlers_disconnect_by_func (entry, 
9765                                         G_CALLBACK (completion_insert_text_callback), completion);
9766   g_signal_handlers_disconnect_by_func (entry, 
9767                                         G_CALLBACK (clear_completion_callback), completion);
9768   g_signal_handlers_disconnect_by_func (entry, 
9769                                         G_CALLBACK (accept_completion_callback), completion);
9770 }
9771
9772 static void
9773 connect_completion_signals (GtkEntry           *entry,
9774                             GtkEntryCompletion *completion)
9775 {
9776   if (completion->priv->popup_completion)
9777     {
9778       completion->priv->changed_id =
9779         g_signal_connect (entry, "changed",
9780                           G_CALLBACK (gtk_entry_completion_changed), completion);
9781       g_signal_connect (entry, "key-press-event",
9782                         G_CALLBACK (gtk_entry_completion_key_press), completion);
9783     }
9784  
9785   if (completion->priv->inline_completion)
9786     {
9787       completion->priv->insert_text_id =
9788         g_signal_connect (entry, "insert-text",
9789                           G_CALLBACK (completion_insert_text_callback), completion);
9790       g_signal_connect (entry, "notify",
9791                         G_CALLBACK (clear_completion_callback), completion);
9792       g_signal_connect (entry, "activate",
9793                         G_CALLBACK (accept_completion_callback), completion);
9794       g_signal_connect (entry, "focus-out-event",
9795                         G_CALLBACK (accept_completion_callback), completion);
9796     }
9797
9798   g_signal_connect (completion, "notify",
9799                     G_CALLBACK (completion_changed), entry);
9800 }
9801
9802 /**
9803  * gtk_entry_set_completion:
9804  * @entry: A #GtkEntry
9805  * @completion: (allow-none): The #GtkEntryCompletion or %NULL
9806  *
9807  * Sets @completion to be the auxiliary completion object to use with @entry.
9808  * All further configuration of the completion mechanism is done on
9809  * @completion using the #GtkEntryCompletion API. Completion is disabled if
9810  * @completion is set to %NULL.
9811  *
9812  * Since: 2.4
9813  */
9814 void
9815 gtk_entry_set_completion (GtkEntry           *entry,
9816                           GtkEntryCompletion *completion)
9817 {
9818   GtkEntryCompletion *old;
9819
9820   g_return_if_fail (GTK_IS_ENTRY (entry));
9821   g_return_if_fail (!completion || GTK_IS_ENTRY_COMPLETION (completion));
9822
9823   old = gtk_entry_get_completion (entry);
9824
9825   if (old == completion)
9826     return;
9827   
9828   if (old)
9829     {
9830       if (old->priv->completion_timeout)
9831         {
9832           g_source_remove (old->priv->completion_timeout);
9833           old->priv->completion_timeout = 0;
9834         }
9835
9836       if (old->priv->check_completion_idle)
9837         {
9838           g_source_destroy (old->priv->check_completion_idle);
9839           old->priv->check_completion_idle = NULL;
9840         }
9841
9842       if (gtk_widget_get_mapped (old->priv->popup_window))
9843         _gtk_entry_completion_popdown (old);
9844
9845       disconnect_completion_signals (entry, old);
9846       old->priv->entry = NULL;
9847
9848       g_object_unref (old);
9849     }
9850
9851   if (!completion)
9852     {
9853       g_object_set_data (G_OBJECT (entry), I_(GTK_ENTRY_COMPLETION_KEY), NULL);
9854       return;
9855     }
9856
9857   /* hook into the entry */
9858   g_object_ref (completion);
9859
9860   connect_completion_signals (entry, completion);    
9861   completion->priv->entry = GTK_WIDGET (entry);
9862   g_object_set_data (G_OBJECT (entry), I_(GTK_ENTRY_COMPLETION_KEY), completion);
9863
9864   g_object_notify (G_OBJECT (entry), "completion");
9865 }
9866
9867 /**
9868  * gtk_entry_get_completion:
9869  * @entry: A #GtkEntry
9870  *
9871  * Returns the auxiliary completion object currently in use by @entry.
9872  *
9873  * Return value: (transfer none): The auxiliary completion object currently
9874  *     in use by @entry.
9875  *
9876  * Since: 2.4
9877  */
9878 GtkEntryCompletion *
9879 gtk_entry_get_completion (GtkEntry *entry)
9880 {
9881   GtkEntryCompletion *completion;
9882
9883   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
9884
9885   completion = GTK_ENTRY_COMPLETION (g_object_get_data (G_OBJECT (entry),
9886                                      GTK_ENTRY_COMPLETION_KEY));
9887
9888   return completion;
9889 }
9890
9891 /**
9892  * gtk_entry_set_cursor_hadjustment:
9893  * @entry: a #GtkEntry
9894  * @adjustment: an adjustment which should be adjusted when the cursor 
9895  *              is moved, or %NULL
9896  *
9897  * Hooks up an adjustment to the cursor position in an entry, so that when 
9898  * the cursor is moved, the adjustment is scrolled to show that position. 
9899  * See gtk_scrolled_window_get_hadjustment() for a typical way of obtaining 
9900  * the adjustment.
9901  *
9902  * The adjustment has to be in pixel units and in the same coordinate system 
9903  * as the entry. 
9904  * 
9905  * Since: 2.12
9906  */
9907 void
9908 gtk_entry_set_cursor_hadjustment (GtkEntry      *entry,
9909                                   GtkAdjustment *adjustment)
9910 {
9911   g_return_if_fail (GTK_IS_ENTRY (entry));
9912   if (adjustment)
9913     g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
9914
9915   if (adjustment)
9916     g_object_ref (adjustment);
9917
9918   g_object_set_qdata_full (G_OBJECT (entry), 
9919                            quark_cursor_hadjustment,
9920                            adjustment, 
9921                            g_object_unref);
9922 }
9923
9924 /**
9925  * gtk_entry_get_cursor_hadjustment:
9926  * @entry: a #GtkEntry
9927  *
9928  * Retrieves the horizontal cursor adjustment for the entry. 
9929  * See gtk_entry_set_cursor_hadjustment().
9930  *
9931  * Return value: (transfer none): the horizontal cursor adjustment, or %NULL
9932  *   if none has been set.
9933  *
9934  * Since: 2.12
9935  */
9936 GtkAdjustment*
9937 gtk_entry_get_cursor_hadjustment (GtkEntry *entry)
9938 {
9939   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
9940
9941   return g_object_get_qdata (G_OBJECT (entry), quark_cursor_hadjustment);
9942 }
9943
9944 /**
9945  * gtk_entry_set_progress_fraction:
9946  * @entry: a #GtkEntry
9947  * @fraction: fraction of the task that's been completed
9948  *
9949  * Causes the entry's progress indicator to "fill in" the given
9950  * fraction of the bar. The fraction should be between 0.0 and 1.0,
9951  * inclusive.
9952  *
9953  * Since: 2.16
9954  */
9955 void
9956 gtk_entry_set_progress_fraction (GtkEntry *entry,
9957                                  gdouble   fraction)
9958 {
9959   GtkWidget       *widget;
9960   GtkEntryPrivate *private;
9961   gdouble          old_fraction;
9962   gint x, y, width, height;
9963   gint old_x, old_y, old_width, old_height;
9964
9965   g_return_if_fail (GTK_IS_ENTRY (entry));
9966
9967   widget = GTK_WIDGET (entry);
9968   private = entry->priv;
9969
9970   if (private->progress_pulse_mode)
9971     old_fraction = -1;
9972   else
9973     old_fraction = private->progress_fraction;
9974
9975   if (gtk_widget_is_drawable (widget))
9976     get_progress_area (widget, &old_x, &old_y, &old_width, &old_height);
9977
9978   fraction = CLAMP (fraction, 0.0, 1.0);
9979
9980   private->progress_fraction = fraction;
9981   private->progress_pulse_mode = FALSE;
9982   private->progress_pulse_current = 0.0;
9983
9984   if (gtk_widget_is_drawable (widget))
9985     {
9986       get_progress_area (widget, &x, &y, &width, &height);
9987
9988       if ((x != old_x) || (y != old_y) || (width != old_width) || (height != old_height))
9989         gtk_widget_queue_draw (widget);
9990     }
9991
9992   if (fraction != old_fraction)
9993     g_object_notify (G_OBJECT (entry), "progress-fraction");
9994 }
9995
9996 /**
9997  * gtk_entry_get_progress_fraction:
9998  * @entry: a #GtkEntry
9999  *
10000  * Returns the current fraction of the task that's been completed.
10001  * See gtk_entry_set_progress_fraction().
10002  *
10003  * Return value: a fraction from 0.0 to 1.0
10004  *
10005  * Since: 2.16
10006  */
10007 gdouble
10008 gtk_entry_get_progress_fraction (GtkEntry *entry)
10009 {
10010   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0.0);
10011
10012   return entry->priv->progress_fraction;
10013 }
10014
10015 /**
10016  * gtk_entry_set_progress_pulse_step:
10017  * @entry: a #GtkEntry
10018  * @fraction: fraction between 0.0 and 1.0
10019  *
10020  * Sets the fraction of total entry width to move the progress
10021  * bouncing block for each call to gtk_entry_progress_pulse().
10022  *
10023  * Since: 2.16
10024  */
10025 void
10026 gtk_entry_set_progress_pulse_step (GtkEntry *entry,
10027                                    gdouble   fraction)
10028 {
10029   GtkEntryPrivate *private;
10030
10031   g_return_if_fail (GTK_IS_ENTRY (entry));
10032
10033   private = entry->priv;
10034
10035   fraction = CLAMP (fraction, 0.0, 1.0);
10036
10037   if (fraction != private->progress_pulse_fraction)
10038     {
10039       private->progress_pulse_fraction = fraction;
10040
10041       gtk_widget_queue_draw (GTK_WIDGET (entry));
10042
10043       g_object_notify (G_OBJECT (entry), "progress-pulse-step");
10044     }
10045 }
10046
10047 /**
10048  * gtk_entry_get_progress_pulse_step:
10049  * @entry: a #GtkEntry
10050  *
10051  * Retrieves the pulse step set with gtk_entry_set_progress_pulse_step().
10052  *
10053  * Return value: a fraction from 0.0 to 1.0
10054  *
10055  * Since: 2.16
10056  */
10057 gdouble
10058 gtk_entry_get_progress_pulse_step (GtkEntry *entry)
10059 {
10060   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0.0);
10061
10062   return entry->priv->progress_pulse_fraction;
10063 }
10064
10065 /**
10066  * gtk_entry_progress_pulse:
10067  * @entry: a #GtkEntry
10068  *
10069  * Indicates that some progress is made, but you don't know how much.
10070  * Causes the entry's progress indicator to enter "activity mode,"
10071  * where a block bounces back and forth. Each call to
10072  * gtk_entry_progress_pulse() causes the block to move by a little bit
10073  * (the amount of movement per pulse is determined by
10074  * gtk_entry_set_progress_pulse_step()).
10075  *
10076  * Since: 2.16
10077  */
10078 void
10079 gtk_entry_progress_pulse (GtkEntry *entry)
10080 {
10081   GtkEntryPrivate *private;
10082
10083   g_return_if_fail (GTK_IS_ENTRY (entry));
10084
10085   private = entry->priv;
10086
10087   if (private->progress_pulse_mode)
10088     {
10089       if (private->progress_pulse_way_back)
10090         {
10091           private->progress_pulse_current -= private->progress_pulse_fraction;
10092
10093           if (private->progress_pulse_current < 0.0)
10094             {
10095               private->progress_pulse_current = 0.0;
10096               private->progress_pulse_way_back = FALSE;
10097             }
10098         }
10099       else
10100         {
10101           private->progress_pulse_current += private->progress_pulse_fraction;
10102
10103           if (private->progress_pulse_current > 1.0 - private->progress_pulse_fraction)
10104             {
10105               private->progress_pulse_current = 1.0 - private->progress_pulse_fraction;
10106               private->progress_pulse_way_back = TRUE;
10107             }
10108         }
10109     }
10110   else
10111     {
10112       private->progress_fraction = 0.0;
10113       private->progress_pulse_mode = TRUE;
10114       private->progress_pulse_way_back = FALSE;
10115       private->progress_pulse_current = 0.0;
10116     }
10117
10118   gtk_widget_queue_draw (GTK_WIDGET (entry));
10119 }
10120
10121 /**
10122  * gtk_entry_set_placeholder_text:
10123  * @entry: a #GtkEntry
10124  * @text: a string to be displayed when @entry is empty an unfocused, or %NULL
10125  *
10126  * Sets text to be displayed in @entry when it is empty and unfocused.
10127  * This can be used to give a visual hint of the expected contents of
10128  * the #GtkEntry.
10129  *
10130  * Note that since the placeholder text gets removed when the entry
10131  * received focus, using this feature is a bit problematic if the entry
10132  * is given the initial focus in a window. Sometimes this can be
10133  * worked around by delaying the initial focus setting until the
10134  * first key event arrives.
10135  *
10136  * Since: 3.2
10137  **/
10138 void
10139 gtk_entry_set_placeholder_text (GtkEntry    *entry,
10140                                 const gchar *text)
10141 {
10142   GtkEntryPrivate *priv;
10143
10144   g_return_if_fail (GTK_IS_ENTRY (entry));
10145
10146   priv = entry->priv;
10147
10148   if (g_strcmp0 (priv->placeholder_text, text) == 0)
10149     return;
10150
10151   g_free (priv->placeholder_text);
10152   priv->placeholder_text = g_strdup (text);
10153
10154   gtk_entry_recompute (entry);
10155
10156   g_object_notify (G_OBJECT (entry), "placeholder-text");
10157 }
10158
10159 /**
10160  * gtk_entry_get_placeholder_text:
10161  * @entry: a #GtkEntry
10162  *
10163  * Retrieves the text that will be displayed when @entry is empty and unfocused
10164  *
10165  * Returns: a pointer to the placeholder text as a string. This string points to internally allocated
10166  * storage in the widget and must not be freed, modified or stored.
10167  *
10168  * Since: 3.2
10169  **/
10170 const gchar *
10171 gtk_entry_get_placeholder_text (GtkEntry *entry)
10172 {
10173   GtkEntryPrivate *priv;
10174
10175   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
10176
10177   priv = entry->priv;
10178
10179   return priv->placeholder_text;
10180 }
10181
10182 /* Caps Lock warning for password entries */
10183
10184 static void
10185 show_capslock_feedback (GtkEntry    *entry,
10186                         const gchar *text)
10187 {
10188   GtkEntryPrivate *priv = entry->priv;
10189
10190   if (gtk_entry_get_icon_storage_type (entry, GTK_ENTRY_ICON_SECONDARY) == GTK_IMAGE_EMPTY)
10191     {
10192       gtk_entry_set_icon_from_stock (entry, GTK_ENTRY_ICON_SECONDARY, GTK_STOCK_CAPS_LOCK_WARNING);
10193       gtk_entry_set_icon_activatable (entry, GTK_ENTRY_ICON_SECONDARY, FALSE);
10194       priv->caps_lock_warning_shown = TRUE;
10195     }
10196
10197   if (priv->caps_lock_warning_shown)
10198     gtk_entry_set_icon_tooltip_text (entry, GTK_ENTRY_ICON_SECONDARY, text);
10199   else
10200     g_warning ("Can't show Caps Lock warning, since secondary icon is set");
10201 }
10202
10203 static void
10204 remove_capslock_feedback (GtkEntry *entry)
10205 {
10206   GtkEntryPrivate *priv = entry->priv;
10207
10208   if (priv->caps_lock_warning_shown)
10209     {
10210       gtk_entry_set_icon_from_stock (entry, GTK_ENTRY_ICON_SECONDARY, NULL);
10211       priv->caps_lock_warning_shown = FALSE;
10212     } 
10213 }
10214
10215 static void
10216 keymap_state_changed (GdkKeymap *keymap, 
10217                       GtkEntry  *entry)
10218 {
10219   GtkEntryPrivate *priv = entry->priv;
10220   char *text = NULL;
10221
10222   if (gtk_entry_get_display_mode (entry) != DISPLAY_NORMAL && priv->caps_lock_warning)
10223     { 
10224       if (gdk_keymap_get_num_lock_state (keymap)
10225           && gdk_keymap_get_caps_lock_state (keymap))
10226         text = _("Caps Lock and Num Lock are on");
10227       else if (gdk_keymap_get_num_lock_state (keymap))
10228         text = _("Num Lock is on");
10229       else if (gdk_keymap_get_caps_lock_state (keymap))
10230         text = _("Caps Lock is on");
10231     }
10232
10233   if (text)
10234     show_capslock_feedback (entry, text);
10235   else
10236     remove_capslock_feedback (entry);
10237 }
10238
10239 /*
10240  * _gtk_entry_set_is_cell_renderer:
10241  * @entry: a #GtkEntry
10242  * @is_cell_renderer: new value
10243  *
10244  * This is a helper function for GtkComboBox. A GtkEntry in a GtkComboBox
10245  * is supposed to behave like a GtkCellEditable when placed in a combo box.
10246  *
10247  * I.e take up its allocation and get GtkEntry->is_cell_renderer = TRUE.
10248  *
10249  */
10250 void
10251 _gtk_entry_set_is_cell_renderer (GtkEntry *entry,
10252                                  gboolean  is_cell_renderer)
10253 {
10254   entry->priv->is_cell_renderer = is_cell_renderer;
10255 }