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