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