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