]> Pileus Git - ~andy/gtk/blob - gtk/gtkentry.c
entry: don't blindly center the text area in the allocated height
[~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_widget_get_state_flags (widget);
2465
2466   state &= ~(GTK_STATE_FLAG_PRELIGHT);
2467
2468   if ((state & GTK_STATE_FLAG_INSENSITIVE) || icon_info->insensitive)
2469     state |= GTK_STATE_FLAG_INSENSITIVE;
2470   else if (icon_info->prelight)
2471     state |= GTK_STATE_FLAG_PRELIGHT;
2472
2473   gtk_style_context_save (context);
2474
2475   gtk_style_context_set_state (context, state);
2476   gtk_style_context_add_class (context, GTK_STYLE_CLASS_IMAGE);
2477 }
2478
2479 static gint
2480 get_icon_width (GtkEntry             *entry,
2481                 GtkEntryIconPosition  icon_pos)
2482 {
2483   GtkEntryPrivate *priv = entry->priv;
2484   EntryIconInfo *icon_info = priv->icons[icon_pos];
2485   GtkStyleContext *context;
2486   gint width;
2487
2488   if (!icon_info)
2489     return 0;
2490
2491   context = gtk_widget_get_style_context (GTK_WIDGET (entry));
2492   gtk_entry_prepare_context_for_icon (entry, context, icon_pos);
2493   _gtk_icon_helper_get_size (icon_info->icon_helper, context,
2494                              &width, NULL);
2495   gtk_style_context_restore (context);
2496
2497   return width;
2498 }
2499
2500 static void
2501 get_icon_allocations (GtkEntry      *entry,
2502                       GtkAllocation *primary,
2503                       GtkAllocation *secondary)
2504
2505 {
2506   GtkEntryPrivate *priv = entry->priv;
2507   gint x, y, width, height;
2508
2509   get_text_area_size (entry, &x, &y, &width, &height);
2510
2511   if (gtk_widget_has_focus (GTK_WIDGET (entry)) && !priv->interior_focus)
2512     y += priv->focus_width;
2513
2514   primary->y = y;
2515   primary->height = height;
2516   primary->width = get_icon_width (entry, GTK_ENTRY_ICON_PRIMARY);
2517   if (primary->width > 0)
2518     primary->width += 2 * priv->icon_margin;
2519
2520   secondary->y = y;
2521   secondary->height = height;
2522   secondary->width = get_icon_width (entry, GTK_ENTRY_ICON_SECONDARY);
2523   if (secondary->width > 0)
2524     secondary->width += 2 * priv->icon_margin;
2525
2526   if (gtk_widget_get_direction (GTK_WIDGET (entry)) == GTK_TEXT_DIR_RTL)
2527     {
2528       primary->x = x + width - primary->width;
2529       secondary->x = x;
2530     }
2531   else
2532     {
2533       primary->x = x;
2534       secondary->x = x + width - secondary->width;
2535     }
2536 }
2537
2538
2539 static void
2540 begin_change (GtkEntry *entry)
2541 {
2542   GtkEntryPrivate *priv = entry->priv;
2543
2544   priv->change_count++;
2545
2546   g_object_freeze_notify (G_OBJECT (entry));
2547 }
2548
2549 static void
2550 end_change (GtkEntry *entry)
2551 {
2552   GtkEditable *editable = GTK_EDITABLE (entry);
2553   GtkEntryPrivate *priv = entry->priv;
2554
2555   g_return_if_fail (priv->change_count > 0);
2556
2557   g_object_thaw_notify (G_OBJECT (entry));
2558
2559   priv->change_count--;
2560
2561   if (priv->change_count == 0)
2562     {
2563        if (priv->real_changed)
2564          {
2565            g_signal_emit_by_name (editable, "changed");
2566            priv->real_changed = FALSE;
2567          }
2568     }
2569 }
2570
2571 static void
2572 emit_changed (GtkEntry *entry)
2573 {
2574   GtkEditable *editable = GTK_EDITABLE (entry);
2575   GtkEntryPrivate *priv = entry->priv;
2576
2577   if (priv->change_count == 0)
2578     g_signal_emit_by_name (editable, "changed");
2579   else
2580     priv->real_changed = TRUE;
2581 }
2582
2583 static void
2584 gtk_entry_destroy (GtkWidget *widget)
2585 {
2586   GtkEntry *entry = GTK_ENTRY (widget);
2587   GtkEntryPrivate *priv = entry->priv;
2588
2589   priv->current_pos = priv->selection_bound = 0;
2590   _gtk_entry_reset_im_context (entry);
2591   gtk_entry_reset_layout (entry);
2592
2593   if (priv->blink_timeout)
2594     {
2595       g_source_remove (priv->blink_timeout);
2596       priv->blink_timeout = 0;
2597     }
2598
2599   if (priv->recompute_idle)
2600     {
2601       g_source_remove (priv->recompute_idle);
2602       priv->recompute_idle = 0;
2603     }
2604
2605   GTK_WIDGET_CLASS (gtk_entry_parent_class)->destroy (widget);
2606 }
2607
2608 static void
2609 gtk_entry_dispose (GObject *object)
2610 {
2611   GtkEntry *entry = GTK_ENTRY (object);
2612   GtkEntryPrivate *priv = entry->priv;
2613   GdkKeymap *keymap;
2614
2615   gtk_entry_set_icon_from_pixbuf (entry, GTK_ENTRY_ICON_PRIMARY, NULL);
2616   gtk_entry_set_icon_tooltip_markup (entry, GTK_ENTRY_ICON_PRIMARY, NULL);
2617   gtk_entry_set_icon_from_pixbuf (entry, GTK_ENTRY_ICON_SECONDARY, NULL);
2618   gtk_entry_set_icon_tooltip_markup (entry, GTK_ENTRY_ICON_SECONDARY, NULL);
2619
2620   if (priv->buffer)
2621     {
2622       buffer_disconnect_signals (entry);
2623       g_object_unref (priv->buffer);
2624       priv->buffer = NULL;
2625     }
2626
2627   keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (object)));
2628   g_signal_handlers_disconnect_by_func (keymap, keymap_state_changed, entry);
2629   g_signal_handlers_disconnect_by_func (keymap, keymap_direction_changed, entry);
2630   G_OBJECT_CLASS (gtk_entry_parent_class)->dispose (object);
2631 }
2632
2633 static void
2634 gtk_entry_finalize (GObject *object)
2635 {
2636   GtkEntry *entry = GTK_ENTRY (object);
2637   GtkEntryPrivate *priv = entry->priv;
2638   EntryIconInfo *icon_info = NULL;
2639   gint i;
2640
2641   for (i = 0; i < MAX_ICONS; i++)
2642     {
2643       if ((icon_info = priv->icons[i]) != NULL)
2644         {
2645           if (icon_info->target_list != NULL)
2646             {
2647               gtk_target_list_unref (icon_info->target_list);
2648               icon_info->target_list = NULL;
2649             }
2650
2651           g_clear_object (&icon_info->icon_helper);
2652
2653           g_slice_free (EntryIconInfo, icon_info);
2654           priv->icons[i] = NULL;
2655         }
2656     }
2657
2658   gtk_entry_set_completion (entry, NULL);
2659
2660   if (priv->cached_layout)
2661     g_object_unref (priv->cached_layout);
2662
2663   g_object_unref (priv->im_context);
2664
2665   if (priv->blink_timeout)
2666     g_source_remove (priv->blink_timeout);
2667
2668   if (priv->recompute_idle)
2669     g_source_remove (priv->recompute_idle);
2670
2671   g_free (priv->placeholder_text);
2672   g_free (priv->im_module);
2673
2674   G_OBJECT_CLASS (gtk_entry_parent_class)->finalize (object);
2675 }
2676
2677 static DisplayMode
2678 gtk_entry_get_display_mode (GtkEntry *entry)
2679 {
2680   GtkEntryPrivate *priv = entry->priv;
2681
2682   if (priv->visible)
2683     return DISPLAY_NORMAL;
2684
2685   if (priv->invisible_char == 0 && priv->invisible_char_set)
2686     return DISPLAY_BLANK;
2687
2688   return DISPLAY_INVISIBLE;
2689 }
2690
2691 static gchar*
2692 gtk_entry_get_display_text (GtkEntry *entry,
2693                             gint      start_pos,
2694                             gint      end_pos)
2695 {
2696   GtkEntryPasswordHint *password_hint;
2697   GtkEntryPrivate *priv;
2698   gunichar invisible_char;
2699   const gchar *start;
2700   const gchar *end;
2701   const gchar *text;
2702   gchar char_str[7];
2703   gint char_len;
2704   GString *str;
2705   guint length;
2706   gint i;
2707
2708   priv = entry->priv;
2709   text = gtk_entry_buffer_get_text (get_buffer (entry));
2710   length = gtk_entry_buffer_get_length (get_buffer (entry));
2711
2712   if (end_pos < 0)
2713     end_pos = length;
2714   if (start_pos > length)
2715     start_pos = length;
2716
2717   if (end_pos <= start_pos)
2718       return g_strdup ("");
2719   else if (priv->visible)
2720     {
2721       start = g_utf8_offset_to_pointer (text, start_pos);
2722       end = g_utf8_offset_to_pointer (start, end_pos - start_pos);
2723       return g_strndup (start, end - start);
2724     }
2725   else
2726     {
2727       str = g_string_sized_new (length * 2);
2728
2729       /* Figure out what our invisible char is and encode it */
2730       if (!priv->invisible_char)
2731           invisible_char = priv->invisible_char_set ? ' ' : '*';
2732       else
2733           invisible_char = priv->invisible_char;
2734       char_len = g_unichar_to_utf8 (invisible_char, char_str);
2735
2736       /*
2737        * Add hidden characters for each character in the text
2738        * buffer. If there is a password hint, then keep that
2739        * character visible.
2740        */
2741
2742       password_hint = g_object_get_qdata (G_OBJECT (entry), quark_password_hint);
2743       for (i = start_pos; i < end_pos; ++i)
2744         {
2745           if (password_hint && i == password_hint->position)
2746             {
2747               start = g_utf8_offset_to_pointer (text, i);
2748               g_string_append_len (str, start, g_utf8_next_char (start) - start);
2749             }
2750           else
2751             {
2752               g_string_append_len (str, char_str, char_len);
2753             }
2754         }
2755
2756       return g_string_free (str, FALSE);
2757     }
2758 }
2759
2760 static void
2761 update_cursors (GtkWidget *widget)
2762 {
2763   GtkEntry *entry = GTK_ENTRY (widget);
2764   GtkEntryPrivate *priv = entry->priv;
2765   EntryIconInfo *icon_info = NULL;
2766   GdkDisplay *display;
2767   GdkCursor *cursor;
2768   gint i;
2769
2770   for (i = 0; i < MAX_ICONS; i++)
2771     {
2772       if ((icon_info = priv->icons[i]) != NULL)
2773         {
2774           if (!_gtk_icon_helper_get_is_empty (icon_info->icon_helper) && 
2775               icon_info->window != NULL)
2776             gdk_window_show_unraised (icon_info->window);
2777
2778           /* The icon windows are not children of the visible entry window,
2779            * thus we can't just inherit the xterm cursor. Slight complication 
2780            * here is that for the entry, insensitive => arrow cursor, but for 
2781            * an icon in a sensitive entry, insensitive => xterm cursor.
2782            */
2783           if (gtk_widget_is_sensitive (widget) &&
2784               (icon_info->insensitive || 
2785                (icon_info->nonactivatable && icon_info->target_list == NULL)))
2786             {
2787               display = gtk_widget_get_display (widget);
2788               cursor = gdk_cursor_new_for_display (display, GDK_XTERM);
2789               gdk_window_set_cursor (icon_info->window, cursor);
2790               g_object_unref (cursor);
2791             }
2792           else
2793             {
2794               gdk_window_set_cursor (icon_info->window, NULL);
2795             }
2796         }
2797     }
2798 }
2799
2800 static void
2801 realize_icon_info (GtkWidget            *widget, 
2802                    GtkEntryIconPosition  icon_pos)
2803 {
2804   GtkEntry *entry = GTK_ENTRY (widget);
2805   GtkEntryPrivate *priv = entry->priv;
2806   EntryIconInfo *icon_info = priv->icons[icon_pos];
2807   GdkWindowAttr attributes;
2808   gint attributes_mask;
2809
2810   g_return_if_fail (icon_info != NULL);
2811
2812   attributes.x = 0;
2813   attributes.y = 0;
2814   attributes.width = 1;
2815   attributes.height = 1;
2816   attributes.window_type = GDK_WINDOW_CHILD;
2817   attributes.wclass = GDK_INPUT_ONLY;
2818   attributes.event_mask = gtk_widget_get_events (widget);
2819   attributes.event_mask |= (GDK_BUTTON_PRESS_MASK |
2820                                 GDK_BUTTON_RELEASE_MASK |
2821                                 GDK_BUTTON1_MOTION_MASK |
2822                                 GDK_BUTTON3_MOTION_MASK |
2823                                 GDK_POINTER_MOTION_HINT_MASK |
2824                                 GDK_POINTER_MOTION_MASK |
2825                                 GDK_ENTER_NOTIFY_MASK |
2826                             GDK_LEAVE_NOTIFY_MASK);
2827   attributes_mask = GDK_WA_X | GDK_WA_Y;
2828
2829   icon_info->window = gdk_window_new (gtk_widget_get_window (widget),
2830                                       &attributes,
2831                                       attributes_mask);
2832   gdk_window_set_user_data (icon_info->window, widget);
2833
2834   gtk_widget_queue_resize (widget);
2835 }
2836
2837 static EntryIconInfo*
2838 construct_icon_info (GtkWidget            *widget, 
2839                      GtkEntryIconPosition  icon_pos)
2840 {
2841   GtkEntry *entry = GTK_ENTRY (widget);
2842   GtkEntryPrivate *priv = entry->priv;
2843   EntryIconInfo *icon_info;
2844
2845   g_return_val_if_fail (priv->icons[icon_pos] == NULL, NULL);
2846
2847   icon_info = g_slice_new0 (EntryIconInfo);
2848   priv->icons[icon_pos] = icon_info;
2849
2850   icon_info->icon_helper = _gtk_icon_helper_new ();
2851
2852   if (gtk_widget_get_realized (widget))
2853     realize_icon_info (widget, icon_pos);
2854
2855   return icon_info;
2856 }
2857
2858 static void
2859 gtk_entry_map (GtkWidget *widget)
2860 {
2861   GtkEntry *entry = GTK_ENTRY (widget);
2862   GtkEntryPrivate *priv = entry->priv;
2863   EntryIconInfo *icon_info = NULL;
2864   gint i;
2865
2866   GTK_WIDGET_CLASS (gtk_entry_parent_class)->map (widget);
2867
2868   gdk_window_show (priv->text_area);
2869
2870   for (i = 0; i < MAX_ICONS; i++)
2871     {
2872       if ((icon_info = priv->icons[i]) != NULL)
2873         {
2874           if (!_gtk_icon_helper_get_is_empty (icon_info->icon_helper) &&
2875               icon_info->window != NULL)
2876             gdk_window_show (icon_info->window);
2877         }
2878     }
2879
2880   update_cursors (widget);
2881 }
2882
2883 static void
2884 gtk_entry_unmap (GtkWidget *widget)
2885 {
2886   GtkEntry *entry = GTK_ENTRY (widget);
2887   GtkEntryPrivate *priv = entry->priv;
2888   EntryIconInfo *icon_info = NULL;
2889   gint i;
2890
2891   for (i = 0; i < MAX_ICONS; i++)
2892     {
2893       if ((icon_info = priv->icons[i]) != NULL)
2894         {
2895           if (!_gtk_icon_helper_get_is_empty (icon_info->icon_helper) && 
2896               icon_info->window != NULL)
2897             gdk_window_hide (icon_info->window);
2898         }
2899     }
2900
2901   gdk_window_hide (priv->text_area);
2902
2903   GTK_WIDGET_CLASS (gtk_entry_parent_class)->unmap (widget);
2904 }
2905
2906 static void
2907 gtk_entry_realize (GtkWidget *widget)
2908 {
2909   GtkEntry *entry;
2910   GtkEntryPrivate *priv;
2911   EntryIconInfo *icon_info;
2912   GdkWindow *window;
2913   GdkWindowAttr attributes;
2914   gint attributes_mask;
2915   gint frame_x, frame_y;
2916   int i;
2917
2918   gtk_widget_set_realized (widget, TRUE);
2919   window = gtk_widget_get_parent_window (widget);
2920   gtk_widget_set_window (widget, window);
2921   g_object_ref (window);
2922
2923   entry = GTK_ENTRY (widget);
2924   priv = entry->priv;
2925
2926   attributes.window_type = GDK_WINDOW_CHILD;
2927   attributes.wclass = GDK_INPUT_ONLY;
2928   attributes.event_mask = gtk_widget_get_events (widget);
2929   attributes.event_mask |= (GDK_BUTTON_PRESS_MASK |
2930                             GDK_BUTTON_RELEASE_MASK |
2931                             GDK_BUTTON1_MOTION_MASK |
2932                             GDK_BUTTON3_MOTION_MASK |
2933                             GDK_POINTER_MOTION_HINT_MASK |
2934                             GDK_POINTER_MOTION_MASK |
2935                             GDK_ENTER_NOTIFY_MASK |
2936                             GDK_LEAVE_NOTIFY_MASK);
2937   attributes_mask = GDK_WA_X | GDK_WA_Y;
2938
2939   get_text_area_size (entry, &attributes.x, &attributes.y, &attributes.width, &attributes.height);
2940
2941   get_frame_size (entry, TRUE, &frame_x, &frame_y, NULL, NULL);
2942   attributes.x += frame_x;
2943   attributes.y += frame_y;
2944
2945   if (gtk_widget_is_sensitive (widget))
2946     {
2947       attributes.cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget), GDK_XTERM);
2948       attributes_mask |= GDK_WA_CURSOR;
2949     }
2950
2951   priv->text_area = gdk_window_new (gtk_widget_get_window (widget),
2952                                     &attributes,
2953                                     attributes_mask);
2954
2955   gdk_window_set_user_data (priv->text_area, entry);
2956
2957   if (attributes_mask & GDK_WA_CURSOR)
2958     g_object_unref (attributes.cursor);
2959
2960   gtk_im_context_set_client_window (priv->im_context, priv->text_area);
2961
2962   gtk_entry_adjust_scroll (entry);
2963   gtk_entry_update_primary_selection (entry);
2964
2965
2966   /* If the icon positions are already setup, create their windows.
2967    * Otherwise if they don't exist yet, then construct_icon_info()
2968    * will create the windows once the widget is already realized.
2969    */
2970   for (i = 0; i < MAX_ICONS; i++)
2971     {
2972       if ((icon_info = priv->icons[i]) != NULL)
2973         {
2974           if (icon_info->window == NULL)
2975             realize_icon_info (widget, i);
2976         }
2977     }
2978 }
2979
2980 static void
2981 gtk_entry_unrealize (GtkWidget *widget)
2982 {
2983   GtkEntry *entry = GTK_ENTRY (widget);
2984   GtkEntryPrivate *priv = entry->priv;
2985   GtkClipboard *clipboard;
2986   EntryIconInfo *icon_info;
2987   gint i;
2988
2989   gtk_entry_reset_layout (entry);
2990   
2991   gtk_im_context_set_client_window (priv->im_context, NULL);
2992
2993   clipboard = gtk_widget_get_clipboard (widget, GDK_SELECTION_PRIMARY);
2994   if (gtk_clipboard_get_owner (clipboard) == G_OBJECT (entry))
2995     gtk_clipboard_clear (clipboard);
2996   
2997   if (priv->text_area)
2998     {
2999       gdk_window_set_user_data (priv->text_area, NULL);
3000       gdk_window_destroy (priv->text_area);
3001       priv->text_area = NULL;
3002     }
3003
3004   if (priv->popup_menu)
3005     {
3006       gtk_widget_destroy (priv->popup_menu);
3007       priv->popup_menu = NULL;
3008     }
3009
3010   GTK_WIDGET_CLASS (gtk_entry_parent_class)->unrealize (widget);
3011
3012   for (i = 0; i < MAX_ICONS; i++)
3013     {
3014       if ((icon_info = priv->icons[i]) != NULL)
3015         {
3016           if (icon_info->window != NULL)
3017             {
3018               gdk_window_destroy (icon_info->window);
3019               icon_info->window = NULL;
3020             }
3021         }
3022     }
3023 }
3024
3025 void
3026 _gtk_entry_get_borders (GtkEntry *entry,
3027                         GtkBorder *border_out)
3028 {
3029   GtkEntryPrivate *priv = entry->priv;
3030   GtkWidget *widget = GTK_WIDGET (entry);
3031   GtkBorder tmp = { 0, 0, 0, 0 };
3032
3033   if (priv->has_frame)
3034     {
3035       GtkStyleContext *context;
3036       GtkBorder border;
3037
3038       context = gtk_widget_get_style_context (widget);
3039       gtk_style_context_get_padding (context, 0, &tmp);
3040
3041       gtk_style_context_get_border (context, 0, &border);
3042       tmp.top += border.top;
3043       tmp.right += border.right;
3044       tmp.bottom += border.bottom;
3045       tmp.left += border.left;
3046     }
3047
3048   if (!priv->interior_focus)
3049     {
3050       tmp.top += priv->focus_width;
3051       tmp.right += priv->focus_width;
3052       tmp.bottom += priv->focus_width;
3053       tmp.left += priv->focus_width;
3054     }
3055
3056   if (border_out != NULL)
3057     *border_out = tmp;
3058 }
3059
3060 static void
3061 gtk_entry_get_preferred_width (GtkWidget *widget,
3062                                gint      *minimum,
3063                                gint      *natural)
3064 {
3065   GtkEntry *entry = GTK_ENTRY (widget);
3066   GtkEntryPrivate *priv = entry->priv;
3067   PangoFontMetrics *metrics;
3068   GtkBorder borders;
3069   GtkBorder inner_border;
3070   PangoContext *context;
3071   GtkStyleContext *style_context;
3072   GtkStateFlags state;
3073   gint icon_widths = 0;
3074   gint icon_width, i;
3075   gint width;
3076
3077   context = gtk_widget_get_pango_context (widget);
3078
3079   style_context = gtk_widget_get_style_context (widget);
3080   state = gtk_widget_get_state_flags (widget);
3081
3082   metrics = pango_context_get_metrics (context,
3083                                        gtk_style_context_get_font (style_context, state),
3084                                        pango_context_get_language (context));
3085
3086   _gtk_entry_get_borders (entry, &borders);
3087   _gtk_entry_effective_inner_border (entry, &inner_border);
3088
3089   if (priv->width_chars < 0)
3090     width = MIN_ENTRY_WIDTH + borders.left + borders.right + inner_border.left + inner_border.right;
3091   else
3092     {
3093       gint char_width = pango_font_metrics_get_approximate_char_width (metrics);
3094       gint digit_width = pango_font_metrics_get_approximate_digit_width (metrics);
3095       gint char_pixels = (MAX (char_width, digit_width) + PANGO_SCALE - 1) / PANGO_SCALE;
3096
3097       width = char_pixels * priv->width_chars + borders.left + borders.right + inner_border.left + inner_border.right;
3098     }
3099
3100   for (i = 0; i < MAX_ICONS; i++)
3101     {
3102       icon_width = get_icon_width (entry, i);
3103       if (icon_width > 0)
3104         icon_widths += icon_width + 2 * priv->icon_margin;
3105     }
3106
3107   if (icon_widths > width)
3108     width += icon_widths;
3109
3110   pango_font_metrics_unref (metrics);
3111
3112   *minimum = width;
3113   *natural = width;
3114 }
3115
3116 static void
3117 gtk_entry_get_preferred_height (GtkWidget *widget,
3118                                 gint      *minimum,
3119                                 gint      *natural)
3120 {
3121   GtkEntry *entry = GTK_ENTRY (widget);
3122   GtkEntryPrivate *priv = entry->priv;
3123   PangoFontMetrics *metrics;
3124   GtkBorder borders;
3125   GtkBorder inner_border;
3126   GtkStyleContext *style_context;
3127   GtkStateFlags state;
3128   PangoContext *context;
3129   gint height;
3130
3131   context = gtk_widget_get_pango_context (widget);
3132
3133   style_context = gtk_widget_get_style_context (widget);
3134   state = gtk_widget_get_state_flags (widget);
3135
3136   metrics = pango_context_get_metrics (context,
3137                                        gtk_style_context_get_font (style_context, state),
3138                                        pango_context_get_language (context));
3139
3140   priv->ascent = pango_font_metrics_get_ascent (metrics);
3141   priv->descent = pango_font_metrics_get_descent (metrics);
3142
3143   _gtk_entry_get_borders (entry, &borders);
3144   _gtk_entry_effective_inner_border (entry, &inner_border);
3145
3146   height = PANGO_PIXELS (priv->ascent + priv->descent) + borders.top + borders.bottom + inner_border.top + inner_border.bottom;
3147
3148   pango_font_metrics_unref (metrics);
3149
3150   *minimum = height;
3151   *natural = height;
3152 }
3153
3154 static void
3155 place_windows (GtkEntry *entry)
3156 {
3157   GtkWidget *widget = GTK_WIDGET (entry);
3158   GtkEntryPrivate *priv = entry->priv;
3159   gint x, y, width, height;
3160   gint frame_x, frame_y;
3161   GtkAllocation primary;
3162   GtkAllocation secondary;
3163   EntryIconInfo *icon_info = NULL;
3164
3165   get_frame_size (entry, TRUE, &frame_x, &frame_y, NULL, NULL);
3166   get_text_area_size (entry, &x, &y, &width, &height);
3167   get_icon_allocations (entry, &primary, &secondary);
3168
3169   if (gtk_widget_has_focus (widget) && !priv->interior_focus)
3170     y += priv->focus_width;
3171
3172   if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
3173     x += secondary.width;
3174   else
3175     x += primary.width;
3176   width -= primary.width + secondary.width;
3177
3178   x += frame_x;
3179   y += frame_y;
3180   primary.x += frame_x;
3181   primary.y += frame_y;
3182   secondary.x += frame_x;
3183   secondary.y += frame_y;
3184
3185   if ((icon_info = priv->icons[GTK_ENTRY_ICON_PRIMARY]) != NULL)
3186     gdk_window_move_resize (icon_info->window,
3187                             primary.x, primary.y,
3188                             primary.width, primary.height);
3189
3190   if ((icon_info = priv->icons[GTK_ENTRY_ICON_SECONDARY]) != NULL)
3191     gdk_window_move_resize (icon_info->window,
3192                             secondary.x, secondary.y,
3193                             secondary.width, secondary.height);
3194
3195   gdk_window_move_resize (priv->text_area, x, y, width, height);
3196 }
3197
3198 static void
3199 gtk_entry_get_text_area_size (GtkEntry *entry,
3200                               gint     *x,
3201                               gint     *y,
3202                               gint     *width,
3203                               gint     *height)
3204 {
3205   GtkEntryPrivate *priv = entry->priv;
3206   GtkWidget *widget = GTK_WIDGET (entry);
3207   GtkAllocation allocation;
3208   GtkRequisition requisition;
3209   gint req_height;
3210   gint frame_height;
3211   GtkBorder borders;
3212
3213   gtk_widget_get_preferred_size (widget, &requisition, NULL);
3214   req_height = requisition.height - gtk_widget_get_margin_top (widget) - gtk_widget_get_margin_bottom (widget);
3215
3216   gtk_widget_get_allocation (widget, &allocation);
3217   _gtk_entry_get_borders (entry, &borders);
3218
3219   if (gtk_widget_get_realized (widget))
3220     get_frame_size (entry, TRUE, NULL, NULL, NULL, &frame_height);
3221   else
3222     frame_height = req_height;
3223
3224   if (gtk_widget_has_focus (widget) && !priv->interior_focus)
3225     frame_height -= 2 * priv->focus_width;
3226
3227   if (x)
3228     *x = borders.left;
3229
3230   if (y)
3231     *y = floor ((frame_height - req_height) / 2) + borders.top;
3232
3233   if (width)
3234     *width = allocation.width - borders.left - borders.right;
3235
3236   if (height)
3237     *height = req_height - borders.top - borders.bottom;
3238 }
3239
3240 static void
3241 get_text_area_size (GtkEntry *entry,
3242                     gint     *x,
3243                     gint     *y,
3244                     gint     *width,
3245                     gint     *height)
3246 {
3247   GtkEntryClass *class;
3248
3249   g_return_if_fail (GTK_IS_ENTRY (entry));
3250
3251   class = GTK_ENTRY_GET_CLASS (entry);
3252
3253   if (class->get_text_area_size)
3254     class->get_text_area_size (entry, x, y, width, height);
3255 }
3256
3257
3258 static void
3259 get_frame_size (GtkEntry *entry,
3260                 gboolean  relative_to_window,
3261                 gint     *x,
3262                 gint     *y,
3263                 gint     *width,
3264                 gint     *height)
3265 {
3266   GtkEntryPrivate *priv = entry->priv;
3267   GtkAllocation allocation;
3268   GtkRequisition requisition;
3269   GtkWidget *widget = GTK_WIDGET (entry);
3270   gint req_height;
3271
3272   gtk_widget_get_preferred_size (widget, &requisition, NULL);
3273
3274   req_height = requisition.height - gtk_widget_get_margin_top (widget) - gtk_widget_get_margin_bottom (widget);
3275
3276   gtk_widget_get_allocation (widget, &allocation);
3277
3278   if (x)
3279     *x = relative_to_window ? allocation.x : 0;
3280
3281   if (y)
3282     {
3283       if (priv->is_cell_renderer)
3284         *y = 0;
3285       else
3286         *y = (allocation.height - req_height) / 2;
3287
3288       if (relative_to_window)
3289         *y += allocation.y;
3290     }
3291
3292   if (width)
3293     *width = allocation.width;
3294
3295   if (height)
3296     {
3297       if (priv->is_cell_renderer)
3298         *height = allocation.height;
3299       else
3300         *height = req_height;
3301     }
3302 }
3303
3304 void
3305 _gtk_entry_effective_inner_border (GtkEntry  *entry,
3306                                    GtkBorder *border)
3307 {
3308   GtkBorder *tmp_border;
3309
3310   tmp_border = g_object_get_qdata (G_OBJECT (entry), quark_inner_border);
3311
3312   if (tmp_border)
3313     {
3314       *border = *tmp_border;
3315       return;
3316     }
3317
3318   gtk_widget_style_get (GTK_WIDGET (entry), "inner-border", &tmp_border, NULL);
3319
3320   if (tmp_border)
3321     {
3322       *border = *tmp_border;
3323       gtk_border_free (tmp_border);
3324       return;
3325     }
3326
3327   *border = default_inner_border;
3328 }
3329
3330 static void
3331 gtk_entry_size_allocate (GtkWidget     *widget,
3332                          GtkAllocation *allocation)
3333 {
3334   GtkEntry *entry = GTK_ENTRY (widget);
3335
3336   gtk_widget_set_allocation (widget, allocation);
3337
3338   if (gtk_widget_get_realized (widget))
3339     {
3340       GtkEntryCompletion* completion;
3341
3342       place_windows (entry);
3343       gtk_entry_recompute (entry);
3344
3345       completion = gtk_entry_get_completion (entry);
3346       if (completion && gtk_widget_get_mapped (completion->priv->popup_window))
3347         _gtk_entry_completion_resize_popup (completion);
3348     }
3349 }
3350
3351 static gboolean
3352 should_prelight (GtkEntry             *entry,
3353                  GtkEntryIconPosition  icon_pos)
3354 {
3355   GtkEntryPrivate *priv = entry->priv;
3356   EntryIconInfo *icon_info = priv->icons[icon_pos];
3357   gboolean prelight;
3358
3359   if (!icon_info)
3360     return FALSE;
3361
3362   if (icon_info->nonactivatable && icon_info->target_list == NULL)
3363     return FALSE;
3364
3365   if (icon_info->pressed)
3366     return FALSE;
3367
3368   gtk_widget_style_get (GTK_WIDGET (entry),
3369                         "icon-prelight", &prelight,
3370                         NULL);
3371
3372   return prelight;
3373 }
3374
3375 static void
3376 draw_icon (GtkWidget            *widget,
3377            cairo_t              *cr,
3378            GtkEntryIconPosition  icon_pos)
3379 {
3380   GtkEntry *entry = GTK_ENTRY (widget);
3381   GtkEntryPrivate *priv = entry->priv;
3382   EntryIconInfo *icon_info = priv->icons[icon_pos];
3383   gint x, y, width, height, pix_width, pix_height;
3384   GtkStyleContext *context;
3385
3386   if (!icon_info)
3387     return;
3388
3389   width = gdk_window_get_width (icon_info->window);
3390   height = gdk_window_get_height (icon_info->window);
3391
3392   /* size_allocate hasn't been called yet. These are the default values.
3393    */
3394   if (width == 1 || height == 1)
3395     return;
3396
3397   cairo_save (cr);
3398   gtk_cairo_transform_to_window (cr, widget, icon_info->window);
3399
3400   context = gtk_widget_get_style_context (widget);
3401   gtk_entry_prepare_context_for_icon (entry, context, icon_pos);
3402   _gtk_icon_helper_get_size (icon_info->icon_helper, context, &pix_width, &pix_height);
3403
3404   x = MAX (0, (width  - pix_width) / 2);
3405   y = MAX (0, (height - pix_height) / 2);
3406
3407   _gtk_icon_helper_draw (icon_info->icon_helper,
3408                          context, cr,
3409                          x, y);
3410
3411   gtk_style_context_restore (context);
3412   cairo_restore (cr);
3413 }
3414
3415
3416 static void
3417 gtk_entry_draw_frame (GtkWidget       *widget,
3418                       GtkStyleContext *context,
3419                       cairo_t         *cr)
3420 {
3421   GtkEntry *entry = GTK_ENTRY (widget);
3422   GtkEntryPrivate *priv = entry->priv;
3423   gint x = 0, y = 0, width, height;
3424   gint frame_x, frame_y;
3425
3426   cairo_save (cr);
3427
3428   get_frame_size (GTK_ENTRY (widget), FALSE, &frame_x, &frame_y, &width, &height);
3429
3430   cairo_translate (cr, frame_x, frame_y);
3431
3432   /* Fix a problem with some themes which assume that entry->text_area's
3433    * width equals widget->window's width
3434    * http://bugzilla.gnome.org/show_bug.cgi?id=466000 */
3435   if (GTK_IS_SPIN_BUTTON (widget))
3436     {
3437       GtkBorder borders;
3438
3439       gtk_entry_get_text_area_size (GTK_ENTRY (widget), &x, NULL, &width, NULL);
3440       _gtk_entry_get_borders (GTK_ENTRY (widget), &borders);
3441
3442       x -= borders.left;
3443       width += borders.left + borders.right;
3444     }
3445
3446   if (gtk_widget_has_focus (widget) && !priv->interior_focus)
3447     {
3448       x += priv->focus_width;
3449       y += priv->focus_width;
3450       width -= 2 * priv->focus_width;
3451       height -= 2 * priv->focus_width;
3452     }
3453
3454   gtk_render_background (context, cr,
3455                          x, y, width, height);
3456
3457   if (priv->has_frame)
3458     gtk_render_frame (context, cr,
3459                       x, y, width, height);
3460
3461   gtk_entry_draw_progress (widget, context, cr);
3462
3463   if (gtk_widget_has_visible_focus (widget) && !priv->interior_focus)
3464     {
3465       x -= priv->focus_width;
3466       y -= priv->focus_width;
3467       width += 2 * priv->focus_width;
3468       height += 2 * priv->focus_width;
3469
3470       gtk_render_focus (context, cr,
3471                         0, 0, width, height);
3472     }
3473
3474   cairo_restore (cr);
3475 }
3476
3477 static void
3478 get_progress_area (GtkWidget *widget,
3479                    gint       *x,
3480                    gint       *y,
3481                    gint       *width,
3482                    gint       *height)
3483 {
3484   GtkEntry *entry = GTK_ENTRY (widget);
3485   GtkEntryPrivate *private = entry->priv;
3486   GtkBorder *progress_border;
3487
3488   get_text_area_size (entry, x, y, width, height);
3489
3490   if (!private->interior_focus)
3491     {
3492       *x -= private->focus_width;
3493       *y -= private->focus_width;
3494       *width += 2 * private->focus_width;
3495       *height += 2 * private->focus_width;
3496     }
3497
3498   gtk_widget_style_get (widget, "progress-border", &progress_border, NULL);
3499
3500   if (progress_border)
3501     {
3502       *x += progress_border->left;
3503       *y += progress_border->top;
3504       *width -= progress_border->left + progress_border->right;
3505       *height -= progress_border->top + progress_border->bottom;
3506
3507       gtk_border_free (progress_border);
3508     }
3509
3510   if (private->progress_pulse_mode)
3511     {
3512       gdouble value = private->progress_pulse_current;
3513
3514       *x += (gint) floor(value * (*width));
3515       *width = (gint) ceil(private->progress_pulse_fraction * (*width));
3516     }
3517   else if (private->progress_fraction > 0)
3518     {
3519       gdouble value = private->progress_fraction;
3520
3521       if (gtk_widget_get_direction (GTK_WIDGET (entry)) == GTK_TEXT_DIR_RTL)
3522         {
3523           gint bar_width;
3524
3525           bar_width = floor(value * (*width) + 0.5);
3526           *x += *width - bar_width;
3527           *width = bar_width;
3528         }
3529       else
3530         {
3531           *width = (gint) floor(value * (*width) + 0.5);
3532         }
3533     }
3534   else
3535     {
3536       *width = 0;
3537       *height = 0;
3538     }
3539 }
3540
3541 static void
3542 gtk_entry_draw_progress (GtkWidget       *widget,
3543                          GtkStyleContext *context,
3544                          cairo_t         *cr)
3545 {
3546   GtkEntry *entry = GTK_ENTRY (widget);
3547   GtkEntryPrivate *private = entry->priv;
3548   gint x, y, width, height;
3549
3550   get_progress_area (widget, &x, &y, &width, &height);
3551
3552   if ((width <= 0) || (height <= 0))
3553     return;
3554
3555   gtk_style_context_save (context);
3556   gtk_style_context_add_class (context, GTK_STYLE_CLASS_PROGRESSBAR);
3557   if (private->progress_pulse_mode)
3558     gtk_style_context_add_class (context, GTK_STYLE_CLASS_PULSE);
3559
3560   gtk_render_activity (context, cr,
3561                        x, y, width, height);
3562
3563   gtk_style_context_restore (context);
3564 }
3565
3566 static gint
3567 gtk_entry_draw (GtkWidget *widget,
3568                 cairo_t   *cr)
3569 {
3570   GtkEntry *entry = GTK_ENTRY (widget);
3571   GtkStyleContext *context;
3572   GtkEntryPrivate *priv = entry->priv;
3573   int i;
3574
3575   context = gtk_widget_get_style_context (widget);
3576
3577   /* Draw entry_bg, shadow, progress and focus */
3578   gtk_entry_draw_frame (widget, context, cr);
3579
3580   /* Draw text and cursor */
3581   cairo_save (cr);
3582
3583   gtk_cairo_transform_to_window (cr, widget, priv->text_area);
3584
3585   if (priv->dnd_position != -1)
3586     gtk_entry_draw_cursor (GTK_ENTRY (widget), cr, CURSOR_DND);
3587   
3588   gtk_entry_draw_text (GTK_ENTRY (widget), cr);
3589
3590   /* When no text is being displayed at all, don't show the cursor */
3591   if (gtk_entry_get_display_mode (entry) != DISPLAY_BLANK &&
3592       gtk_widget_has_focus (widget) &&
3593       priv->selection_bound == priv->current_pos && priv->cursor_visible)
3594     gtk_entry_draw_cursor (GTK_ENTRY (widget), cr, CURSOR_STANDARD);
3595
3596   cairo_restore (cr);
3597
3598   /* Draw icons */
3599   for (i = 0; i < MAX_ICONS; i++)
3600     {
3601       EntryIconInfo *icon_info = priv->icons[i];
3602
3603       if (icon_info != NULL)
3604         draw_icon (widget, cr, i);
3605     }
3606
3607   return FALSE;
3608 }
3609
3610 static gint
3611 gtk_entry_enter_notify (GtkWidget *widget,
3612                         GdkEventCrossing *event)
3613 {
3614   GtkEntry *entry = GTK_ENTRY (widget);
3615   GtkEntryPrivate *priv = entry->priv;
3616   gint i;
3617
3618   for (i = 0; i < MAX_ICONS; i++)
3619     {
3620       EntryIconInfo *icon_info = priv->icons[i];
3621
3622       if (icon_info != NULL && event->window == icon_info->window)
3623         {
3624           if (should_prelight (entry, i))
3625             {
3626               icon_info->prelight = TRUE;
3627               gtk_widget_queue_draw (widget);
3628             }
3629
3630           break;
3631         }
3632     }
3633
3634     return FALSE;
3635 }
3636
3637 static gint
3638 gtk_entry_leave_notify (GtkWidget        *widget,
3639                         GdkEventCrossing *event)
3640 {
3641   GtkEntry *entry = GTK_ENTRY (widget);
3642   GtkEntryPrivate *priv = entry->priv;
3643   gint i;
3644
3645   for (i = 0; i < MAX_ICONS; i++)
3646     {
3647       EntryIconInfo *icon_info = priv->icons[i];
3648
3649       if (icon_info != NULL && event->window == icon_info->window)
3650         {
3651           /* a grab means that we may never see the button release */
3652           if (event->mode == GDK_CROSSING_GRAB || event->mode == GDK_CROSSING_GTK_GRAB)
3653             icon_info->pressed = FALSE;
3654
3655           if (should_prelight (entry, i))
3656             {
3657               icon_info->prelight = FALSE;
3658               gtk_widget_queue_draw (widget);
3659             }
3660
3661           break;
3662         }
3663     }
3664
3665   return FALSE;
3666 }
3667
3668 static void
3669 gtk_entry_get_pixel_ranges (GtkEntry  *entry,
3670                             gint     **ranges,
3671                             gint      *n_ranges)
3672 {
3673   gint start_char, end_char;
3674
3675   if (gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &start_char, &end_char))
3676     {
3677       PangoLayout *layout = gtk_entry_ensure_layout (entry, TRUE);
3678       PangoLayoutLine *line = pango_layout_get_lines_readonly (layout)->data;
3679       const char *text = pango_layout_get_text (layout);
3680       gint start_index = g_utf8_offset_to_pointer (text, start_char) - text;
3681       gint end_index = g_utf8_offset_to_pointer (text, end_char) - text;
3682       gint real_n_ranges, i;
3683
3684       pango_layout_line_get_x_ranges (line, start_index, end_index, ranges, &real_n_ranges);
3685
3686       if (ranges)
3687         {
3688           gint *r = *ranges;
3689           
3690           for (i = 0; i < real_n_ranges; ++i)
3691             {
3692               r[2 * i + 1] = (r[2 * i + 1] - r[2 * i]) / PANGO_SCALE;
3693               r[2 * i] = r[2 * i] / PANGO_SCALE;
3694             }
3695         }
3696       
3697       if (n_ranges)
3698         *n_ranges = real_n_ranges;
3699     }
3700   else
3701     {
3702       if (n_ranges)
3703         *n_ranges = 0;
3704       if (ranges)
3705         *ranges = NULL;
3706     }
3707 }
3708
3709 static gboolean
3710 in_selection (GtkEntry *entry,
3711               gint      x)
3712 {
3713   gint *ranges;
3714   gint n_ranges, i;
3715   gint retval = FALSE;
3716
3717   gtk_entry_get_pixel_ranges (entry, &ranges, &n_ranges);
3718
3719   for (i = 0; i < n_ranges; ++i)
3720     {
3721       if (x >= ranges[2 * i] && x < ranges[2 * i] + ranges[2 * i + 1])
3722         {
3723           retval = TRUE;
3724           break;
3725         }
3726     }
3727
3728   g_free (ranges);
3729   return retval;
3730 }
3731               
3732 static gint
3733 gtk_entry_button_press (GtkWidget      *widget,
3734                         GdkEventButton *event)
3735 {
3736   GtkEntry *entry = GTK_ENTRY (widget);
3737   GtkEditable *editable = GTK_EDITABLE (widget);
3738   GtkEntryPrivate *priv = entry->priv;
3739   EntryIconInfo *icon_info = NULL;
3740   gint tmp_pos;
3741   gint sel_start, sel_end;
3742   gint i;
3743
3744   for (i = 0; i < MAX_ICONS; i++)
3745     {
3746       icon_info = priv->icons[i];
3747
3748       if (!icon_info || icon_info->insensitive)
3749         continue;
3750
3751       if (event->window == icon_info->window)
3752         {
3753           if (should_prelight (entry, i))
3754             {
3755               icon_info->prelight = FALSE;
3756               gtk_widget_queue_draw (widget);
3757             }
3758
3759           priv->start_x = event->x;
3760           priv->start_y = event->y;
3761           icon_info->pressed = TRUE;
3762
3763           if (!icon_info->nonactivatable)
3764             g_signal_emit (entry, signals[ICON_PRESS], 0, i, event);
3765
3766           return TRUE;
3767         }
3768     }
3769
3770   if (event->window != priv->text_area ||
3771       (priv->button && event->button != priv->button))
3772     return FALSE;
3773
3774   gtk_entry_reset_blink_time (entry);
3775
3776   priv->button = event->button;
3777   
3778   if (!gtk_widget_has_focus (widget))
3779     {
3780       priv->in_click = TRUE;
3781       gtk_widget_grab_focus (widget);
3782       priv->in_click = FALSE;
3783     }
3784
3785   tmp_pos = gtk_entry_find_position (entry, event->x + priv->scroll_offset);
3786
3787   if (gdk_event_triggers_context_menu ((GdkEvent *) event))
3788     {
3789       gtk_entry_do_popup (entry, event);
3790       priv->button = 0; /* Don't wait for release, since the menu will gtk_grab_add */
3791
3792       return TRUE;
3793     }
3794   else if (event->button == GDK_BUTTON_PRIMARY)
3795     {
3796       gboolean have_selection = gtk_editable_get_selection_bounds (editable, &sel_start, &sel_end);
3797
3798       priv->select_words = FALSE;
3799       priv->select_lines = FALSE;
3800
3801       if (event->state &
3802           gtk_widget_get_modifier_mask (widget,
3803                                         GDK_MODIFIER_INTENT_EXTEND_SELECTION))
3804         {
3805           _gtk_entry_reset_im_context (entry);
3806
3807           if (!have_selection) /* select from the current position to the clicked position */
3808             sel_start = sel_end = priv->current_pos;
3809           
3810           if (tmp_pos > sel_start && tmp_pos < sel_end)
3811             {
3812               /* Truncate current selection, but keep it as big as possible */
3813               if (tmp_pos - sel_start > sel_end - tmp_pos)
3814                 gtk_entry_set_positions (entry, sel_start, tmp_pos);
3815               else
3816                 gtk_entry_set_positions (entry, tmp_pos, sel_end);
3817             }
3818           else
3819             {
3820               gboolean extend_to_left;
3821               gint start, end;
3822
3823               /* Figure out what click selects and extend current selection */
3824               switch (event->type)
3825                 {
3826                 case GDK_BUTTON_PRESS:
3827                   gtk_entry_set_positions (entry, tmp_pos, tmp_pos);
3828                   break;
3829                   
3830                 case GDK_2BUTTON_PRESS:
3831                   priv->select_words = TRUE;
3832                   gtk_entry_select_word (entry);
3833                   break;
3834                   
3835                 case GDK_3BUTTON_PRESS:
3836                   priv->select_lines = TRUE;
3837                   gtk_entry_select_line (entry);
3838                   break;
3839
3840                 default:
3841                   break;
3842                 }
3843
3844               start = MIN (priv->current_pos, priv->selection_bound);
3845               start = MIN (sel_start, start);
3846               
3847               end = MAX (priv->current_pos, priv->selection_bound);
3848               end = MAX (sel_end, end);
3849
3850               if (tmp_pos == sel_start || tmp_pos == sel_end)
3851                 extend_to_left = (tmp_pos == start);
3852               else
3853                 extend_to_left = (end == sel_end);
3854               
3855               if (extend_to_left)
3856                 gtk_entry_set_positions (entry, start, end);
3857               else
3858                 gtk_entry_set_positions (entry, end, start);
3859             }
3860         }
3861       else /* no shift key */
3862         switch (event->type)
3863         {
3864         case GDK_BUTTON_PRESS:
3865           if (in_selection (entry, event->x + priv->scroll_offset))
3866             {
3867               /* Click inside the selection - we'll either start a drag, or
3868                * clear the selection
3869                */
3870               priv->in_drag = TRUE;
3871               priv->drag_start_x = event->x + priv->scroll_offset;
3872               priv->drag_start_y = event->y;
3873             }
3874           else
3875             gtk_editable_set_position (editable, tmp_pos);
3876           break;
3877  
3878         case GDK_2BUTTON_PRESS:
3879           /* We ALWAYS receive a GDK_BUTTON_PRESS immediately before 
3880            * receiving a GDK_2BUTTON_PRESS so we need to reset
3881            * priv->in_drag which may have been set above
3882            */
3883           priv->in_drag = FALSE;
3884           priv->select_words = TRUE;
3885           gtk_entry_select_word (entry);
3886           break;
3887         
3888         case GDK_3BUTTON_PRESS:
3889           /* We ALWAYS receive a GDK_BUTTON_PRESS immediately before
3890            * receiving a GDK_3BUTTON_PRESS so we need to reset
3891            * priv->in_drag which may have been set above
3892            */
3893           priv->in_drag = FALSE;
3894           priv->select_lines = TRUE;
3895           gtk_entry_select_line (entry);
3896           break;
3897
3898         default:
3899           break;
3900         }
3901
3902       return TRUE;
3903     }
3904   else if (event->button == GDK_BUTTON_MIDDLE && event->type == GDK_BUTTON_PRESS)
3905     {
3906       if (priv->editable)
3907         {
3908           priv->insert_pos = tmp_pos;
3909           gtk_entry_paste (entry, GDK_SELECTION_PRIMARY);
3910           return TRUE;
3911         }
3912       else
3913         {
3914           gtk_widget_error_bell (widget);
3915         }
3916     }
3917
3918   return FALSE;
3919 }
3920
3921 static gint
3922 gtk_entry_button_release (GtkWidget      *widget,
3923                           GdkEventButton *event)
3924 {
3925   GtkEntry *entry = GTK_ENTRY (widget);
3926   GtkEntryPrivate *priv = entry->priv;
3927   EntryIconInfo *icon_info = NULL;
3928   gint i;
3929
3930   for (i = 0; i < MAX_ICONS; i++)
3931     {
3932       icon_info = priv->icons[i];
3933
3934       if (!icon_info || icon_info->insensitive)
3935         continue;
3936
3937       if (event->window == icon_info->window)
3938         {
3939           icon_info->pressed = FALSE;
3940
3941           if (should_prelight (entry, i) &&
3942               event->x >= 0 && event->y >= 0 &&
3943               event->x < gdk_window_get_width (icon_info->window) &&
3944               event->y < gdk_window_get_height (icon_info->window))
3945             {
3946               icon_info->prelight = TRUE;
3947               gtk_widget_queue_draw (widget);
3948             }
3949
3950           if (!icon_info->nonactivatable)
3951             g_signal_emit (entry, signals[ICON_RELEASE], 0, i, event);
3952
3953           return TRUE;
3954         }
3955     }
3956
3957   if (event->window != priv->text_area || priv->button != event->button)
3958     return FALSE;
3959
3960   if (priv->in_drag)
3961     {
3962       gint tmp_pos = gtk_entry_find_position (entry, priv->drag_start_x);
3963
3964       gtk_editable_set_position (GTK_EDITABLE (entry), tmp_pos);
3965
3966       priv->in_drag = 0;
3967     }
3968   
3969   priv->button = 0;
3970   
3971   gtk_entry_update_primary_selection (entry);
3972               
3973   return TRUE;
3974 }
3975
3976 static gchar *
3977 _gtk_entry_get_selected_text (GtkEntry *entry)
3978 {
3979   GtkEditable *editable = GTK_EDITABLE (entry);
3980   gint         start_text, end_text;
3981   gchar       *text = NULL;
3982
3983   if (gtk_editable_get_selection_bounds (editable, &start_text, &end_text))
3984     text = gtk_editable_get_chars (editable, start_text, end_text);
3985
3986   return text;
3987 }
3988
3989 static gint
3990 gtk_entry_motion_notify (GtkWidget      *widget,
3991                          GdkEventMotion *event)
3992 {
3993   GtkEntry *entry = GTK_ENTRY (widget);
3994   GtkEntryPrivate *priv = entry->priv;
3995   EntryIconInfo *icon_info = NULL;
3996   gint tmp_pos;
3997   gint i;
3998
3999   for (i = 0; i < MAX_ICONS; i++)
4000     {
4001       icon_info = priv->icons[i];
4002
4003       if (!icon_info || icon_info->insensitive)
4004         continue;
4005
4006       if (event->window == icon_info->window)
4007         {
4008           if (icon_info->pressed &&
4009               icon_info->target_list != NULL &&
4010               gtk_drag_check_threshold (widget,
4011                                         priv->start_x,
4012                                         priv->start_y,
4013                                         event->x,
4014                                         event->y))
4015             {
4016               icon_info->in_drag = TRUE;
4017               icon_info->pressed = FALSE;
4018               gtk_drag_begin (widget,
4019                               icon_info->target_list,
4020                               icon_info->actions,
4021                               1,
4022                               (GdkEvent*)event);
4023             }
4024
4025           return TRUE;
4026         }
4027     }
4028
4029   if (priv->mouse_cursor_obscured)
4030     {
4031       GdkCursor *cursor;
4032
4033       cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget), GDK_XTERM);
4034       gdk_window_set_cursor (priv->text_area, cursor);
4035       g_object_unref (cursor);
4036       priv->mouse_cursor_obscured = FALSE;
4037     }
4038
4039   if (event->window != priv->text_area || priv->button != GDK_BUTTON_PRIMARY)
4040     return FALSE;
4041
4042   if (priv->select_lines)
4043     return TRUE;
4044
4045   gdk_event_request_motions (event);
4046
4047   if (priv->in_drag)
4048     {
4049       if (gtk_entry_get_display_mode (entry) == DISPLAY_NORMAL &&
4050           gtk_drag_check_threshold (widget,
4051                                     priv->drag_start_x, priv->drag_start_y,
4052                                     event->x + priv->scroll_offset, event->y))
4053         {
4054           GdkDragContext *context;
4055           GtkTargetList  *target_list = gtk_target_list_new (NULL, 0);
4056           guint actions = priv->editable ? GDK_ACTION_COPY | GDK_ACTION_MOVE : GDK_ACTION_COPY;
4057           gchar *text = NULL;
4058           cairo_surface_t *surface;
4059
4060           gtk_target_list_add_text_targets (target_list, 0);
4061
4062           text = _gtk_entry_get_selected_text (entry);
4063           surface = _gtk_text_util_create_drag_icon (widget, text, -1);
4064
4065           context = gtk_drag_begin (widget, target_list, actions,
4066                                     priv->button, (GdkEvent *)event);
4067           
4068           if (surface)
4069             gtk_drag_set_icon_surface (context, surface);
4070           else
4071             gtk_drag_set_icon_default (context);
4072           
4073           if (surface)
4074             cairo_surface_destroy (surface);
4075           g_free (text);
4076
4077           priv->in_drag = FALSE;
4078           priv->button = 0;
4079           
4080           gtk_target_list_unref (target_list);
4081         }
4082     }
4083   else
4084     {
4085       if (event->y < 0)
4086         tmp_pos = 0;
4087       else if (event->y >= gdk_window_get_height (priv->text_area))
4088         tmp_pos = gtk_entry_buffer_get_length (get_buffer (entry));
4089       else
4090         tmp_pos = gtk_entry_find_position (entry, event->x + priv->scroll_offset);
4091
4092       if (priv->select_words)
4093         {
4094           gint min, max;
4095           gint old_min, old_max;
4096           gint pos, bound;
4097           
4098           min = gtk_entry_move_backward_word (entry, tmp_pos, TRUE);
4099           max = gtk_entry_move_forward_word (entry, tmp_pos, TRUE);
4100
4101           pos = priv->current_pos;
4102           bound = priv->selection_bound;
4103
4104           old_min = MIN(priv->current_pos, priv->selection_bound);
4105           old_max = MAX(priv->current_pos, priv->selection_bound);
4106           
4107           if (min < old_min)
4108             {
4109               pos = min;
4110               bound = old_max;
4111             }
4112           else if (old_max < max) 
4113             {
4114               pos = max;
4115               bound = old_min;
4116             }
4117           else if (pos == old_min) 
4118             {
4119               if (priv->current_pos != min)
4120                 pos = max;
4121             }
4122           else 
4123             {
4124               if (priv->current_pos != max)
4125                 pos = min;
4126             }
4127         
4128           gtk_entry_set_positions (entry, pos, bound);
4129         }
4130       else
4131         gtk_entry_set_positions (entry, tmp_pos, -1);
4132     }
4133       
4134   return TRUE;
4135 }
4136
4137 static void
4138 set_invisible_cursor (GdkWindow *window)
4139 {
4140   GdkDisplay *display;
4141   GdkCursor *cursor;
4142
4143   display = gdk_window_get_display (window);
4144   cursor = gdk_cursor_new_for_display (display, GDK_BLANK_CURSOR);
4145
4146   gdk_window_set_cursor (window, cursor);
4147
4148   g_object_unref (cursor);
4149 }
4150
4151 static void
4152 gtk_entry_obscure_mouse_cursor (GtkEntry *entry)
4153 {
4154   GtkEntryPrivate *priv = entry->priv;
4155
4156   if (priv->mouse_cursor_obscured)
4157     return;
4158
4159   set_invisible_cursor (priv->text_area);
4160
4161   priv->mouse_cursor_obscured = TRUE;
4162 }
4163
4164 static gint
4165 gtk_entry_key_press (GtkWidget   *widget,
4166                      GdkEventKey *event)
4167 {
4168   GtkEntry *entry = GTK_ENTRY (widget);
4169   GtkEntryPrivate *priv = entry->priv;
4170
4171   gtk_entry_reset_blink_time (entry);
4172   gtk_entry_pend_cursor_blink (entry);
4173
4174   if (priv->editable)
4175     {
4176       if (gtk_im_context_filter_keypress (priv->im_context, event))
4177         {
4178           gtk_entry_obscure_mouse_cursor (entry);
4179           priv->need_im_reset = TRUE;
4180           return TRUE;
4181         }
4182     }
4183
4184   if (event->keyval == GDK_KEY_Return || 
4185       event->keyval == GDK_KEY_KP_Enter || 
4186       event->keyval == GDK_KEY_ISO_Enter || 
4187       event->keyval == GDK_KEY_Escape)
4188     {
4189       GtkEntryCompletion *completion = gtk_entry_get_completion (entry);
4190
4191       if (completion && completion->priv->completion_timeout)
4192         {
4193           g_source_remove (completion->priv->completion_timeout);
4194           completion->priv->completion_timeout = 0;
4195         }
4196
4197       _gtk_entry_reset_im_context (entry);
4198     }
4199
4200   if (GTK_WIDGET_CLASS (gtk_entry_parent_class)->key_press_event (widget, event))
4201     /* Activate key bindings
4202      */
4203     return TRUE;
4204
4205   if (!priv->editable && event->length)
4206     gtk_widget_error_bell (widget);
4207
4208   return FALSE;
4209 }
4210
4211 static gint
4212 gtk_entry_key_release (GtkWidget   *widget,
4213                        GdkEventKey *event)
4214 {
4215   GtkEntry *entry = GTK_ENTRY (widget);
4216   GtkEntryPrivate *priv = entry->priv;
4217
4218   if (priv->editable)
4219     {
4220       if (gtk_im_context_filter_keypress (priv->im_context, event))
4221         {
4222           priv->need_im_reset = TRUE;
4223           return TRUE;
4224         }
4225     }
4226
4227   return GTK_WIDGET_CLASS (gtk_entry_parent_class)->key_release_event (widget, event);
4228 }
4229
4230 static gint
4231 gtk_entry_focus_in (GtkWidget     *widget,
4232                     GdkEventFocus *event)
4233 {
4234   GtkEntry *entry = GTK_ENTRY (widget);
4235   GtkEntryPrivate *priv = entry->priv;
4236   GdkKeymap *keymap;
4237
4238   gtk_widget_queue_draw (widget);
4239
4240   keymap = gdk_keymap_get_for_display (gtk_widget_get_display (widget));
4241
4242   if (priv->editable)
4243     {
4244       priv->need_im_reset = TRUE;
4245       gtk_im_context_focus_in (priv->im_context);
4246       keymap_state_changed (keymap, entry);
4247       g_signal_connect (keymap, "state-changed", 
4248                         G_CALLBACK (keymap_state_changed), entry);
4249     }
4250
4251   g_signal_connect (keymap, "direction-changed",
4252                     G_CALLBACK (keymap_direction_changed), entry);
4253
4254   if (gtk_entry_buffer_get_bytes (get_buffer (entry)) == 0 &&
4255       priv->placeholder_text != NULL)
4256     {
4257       gtk_entry_recompute (entry);
4258     }
4259   else
4260     {
4261       gtk_entry_reset_blink_time (entry);
4262       gtk_entry_check_cursor_blink (entry);
4263     }
4264
4265   return FALSE;
4266 }
4267
4268 static gint
4269 gtk_entry_focus_out (GtkWidget     *widget,
4270                      GdkEventFocus *event)
4271 {
4272   GtkEntry *entry = GTK_ENTRY (widget);
4273   GtkEntryPrivate *priv = entry->priv;
4274   GtkEntryCompletion *completion;
4275   GdkKeymap *keymap;
4276
4277   gtk_widget_queue_draw (widget);
4278
4279   keymap = gdk_keymap_get_for_display (gtk_widget_get_display (widget));
4280
4281   if (priv->editable)
4282     {
4283       priv->need_im_reset = TRUE;
4284       gtk_im_context_focus_out (priv->im_context);
4285       remove_capslock_feedback (entry);
4286     }
4287
4288   if (gtk_entry_buffer_get_bytes (get_buffer (entry)) == 0 &&
4289       priv->placeholder_text != NULL)
4290     {
4291       gtk_entry_recompute (entry);
4292     }
4293   else
4294     {
4295       gtk_entry_check_cursor_blink (entry);
4296     }
4297
4298   g_signal_handlers_disconnect_by_func (keymap, keymap_state_changed, entry);
4299   g_signal_handlers_disconnect_by_func (keymap, keymap_direction_changed, entry);
4300
4301   completion = gtk_entry_get_completion (entry);
4302   if (completion)
4303     _gtk_entry_completion_popdown (completion);
4304
4305   return FALSE;
4306 }
4307
4308 static void
4309 gtk_entry_grab_focus (GtkWidget *widget)
4310 {
4311   GtkEntry *entry = GTK_ENTRY (widget);
4312   GtkEntryPrivate *priv = entry->priv;
4313   gboolean select_on_focus;
4314
4315   GTK_WIDGET_CLASS (gtk_entry_parent_class)->grab_focus (widget);
4316
4317   if (priv->editable && !priv->in_click)
4318     {
4319       g_object_get (gtk_widget_get_settings (widget),
4320                     "gtk-entry-select-on-focus",
4321                     &select_on_focus,
4322                     NULL);
4323
4324       if (select_on_focus)
4325         gtk_editable_select_region (GTK_EDITABLE (widget), 0, -1);
4326     }
4327 }
4328
4329 static void
4330 gtk_entry_direction_changed (GtkWidget        *widget,
4331                              GtkTextDirection  previous_dir)
4332 {
4333   GtkEntry *entry = GTK_ENTRY (widget);
4334
4335   gtk_entry_recompute (entry);
4336
4337   GTK_WIDGET_CLASS (gtk_entry_parent_class)->direction_changed (widget, previous_dir);
4338 }
4339
4340 static void
4341 gtk_entry_state_flags_changed (GtkWidget     *widget,
4342                                GtkStateFlags  previous_state)
4343 {
4344   GtkEntry *entry = GTK_ENTRY (widget);
4345   GtkEntryPrivate *priv = entry->priv;
4346   GdkCursor *cursor;
4347
4348   if (gtk_widget_get_realized (widget))
4349     {
4350       if (gtk_widget_is_sensitive (widget))
4351         cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget), GDK_XTERM);
4352       else
4353         cursor = NULL;
4354
4355       gdk_window_set_cursor (priv->text_area, cursor);
4356
4357       if (cursor)
4358         g_object_unref (cursor);
4359
4360       priv->mouse_cursor_obscured = FALSE;
4361
4362       update_cursors (widget);
4363     }
4364
4365   if (!gtk_widget_is_sensitive (widget))
4366     {
4367       /* Clear any selection */
4368       gtk_editable_select_region (GTK_EDITABLE (entry), priv->current_pos, priv->current_pos);
4369     }
4370
4371   gtk_entry_update_cached_style_values (entry);
4372 }
4373
4374 static void
4375 gtk_entry_screen_changed (GtkWidget *widget,
4376                           GdkScreen *old_screen)
4377 {
4378   gtk_entry_recompute (GTK_ENTRY (widget));
4379 }
4380
4381 /* GtkEditable method implementations
4382  */
4383 static void
4384 gtk_entry_insert_text (GtkEditable *editable,
4385                        const gchar *new_text,
4386                        gint         new_text_length,
4387                        gint        *position)
4388 {
4389   g_object_ref (editable);
4390
4391   /*
4392    * The incoming text may a password or other secret. We make sure
4393    * not to copy it into temporary buffers.
4394    */
4395
4396   g_signal_emit_by_name (editable, "insert-text", new_text, new_text_length, position);
4397
4398   g_object_unref (editable);
4399 }
4400
4401 static void
4402 gtk_entry_delete_text (GtkEditable *editable,
4403                        gint         start_pos,
4404                        gint         end_pos)
4405 {
4406   g_object_ref (editable);
4407
4408   g_signal_emit_by_name (editable, "delete-text", start_pos, end_pos);
4409
4410   g_object_unref (editable);
4411 }
4412
4413 static gchar *    
4414 gtk_entry_get_chars      (GtkEditable   *editable,
4415                           gint           start_pos,
4416                           gint           end_pos)
4417 {
4418   GtkEntry *entry = GTK_ENTRY (editable);
4419   const gchar *text;
4420   gint text_length;
4421   gint start_index, end_index;
4422
4423   text = gtk_entry_buffer_get_text (get_buffer (entry));
4424   text_length = gtk_entry_buffer_get_length (get_buffer (entry));
4425
4426   if (end_pos < 0)
4427     end_pos = text_length;
4428
4429   start_pos = MIN (text_length, start_pos);
4430   end_pos = MIN (text_length, end_pos);
4431
4432   start_index = g_utf8_offset_to_pointer (text, start_pos) - text;
4433   end_index = g_utf8_offset_to_pointer (text, end_pos) - text;
4434
4435   return g_strndup (text + start_index, end_index - start_index);
4436 }
4437
4438 static void
4439 gtk_entry_real_set_position (GtkEditable *editable,
4440                              gint         position)
4441 {
4442   GtkEntry *entry = GTK_ENTRY (editable);
4443   GtkEntryPrivate *priv = entry->priv;
4444
4445   guint length;
4446
4447   length = gtk_entry_buffer_get_length (get_buffer (entry));
4448   if (position < 0 || position > length)
4449     position = length;
4450
4451   if (position != priv->current_pos ||
4452       position != priv->selection_bound)
4453     {
4454       _gtk_entry_reset_im_context (entry);
4455       gtk_entry_set_positions (entry, position, position);
4456     }
4457 }
4458
4459 static gint
4460 gtk_entry_get_position (GtkEditable *editable)
4461 {
4462   GtkEntry *entry = GTK_ENTRY (editable);
4463   GtkEntryPrivate *priv = entry->priv;
4464
4465   return priv->current_pos;
4466 }
4467
4468 static void
4469 gtk_entry_set_selection_bounds (GtkEditable *editable,
4470                                 gint         start,
4471                                 gint         end)
4472 {
4473   GtkEntry *entry = GTK_ENTRY (editable);
4474   guint length;
4475
4476   length = gtk_entry_buffer_get_length (get_buffer (entry));
4477   if (start < 0)
4478     start = length;
4479   if (end < 0)
4480     end = length;
4481   
4482   _gtk_entry_reset_im_context (entry);
4483
4484   gtk_entry_set_positions (entry,
4485                            MIN (end, length),
4486                            MIN (start, length));
4487
4488   gtk_entry_update_primary_selection (entry);
4489 }
4490
4491 static gboolean
4492 gtk_entry_get_selection_bounds (GtkEditable *editable,
4493                                 gint        *start,
4494                                 gint        *end)
4495 {
4496   GtkEntry *entry = GTK_ENTRY (editable);
4497   GtkEntryPrivate *priv = entry->priv;
4498
4499   *start = priv->selection_bound;
4500   *end = priv->current_pos;
4501
4502   return (priv->selection_bound != priv->current_pos);
4503 }
4504
4505 static void
4506 icon_theme_changed (GtkEntry *entry)
4507 {
4508   GtkEntryPrivate *priv = entry->priv;
4509   gint i;
4510
4511   for (i = 0; i < MAX_ICONS; i++)
4512     {
4513       EntryIconInfo *icon_info = priv->icons[i];
4514       if (icon_info != NULL) 
4515         _gtk_icon_helper_invalidate (icon_info->icon_helper);
4516     }
4517
4518   gtk_widget_queue_draw (GTK_WIDGET (entry));
4519 }
4520
4521 static void
4522 icon_margin_changed (GtkEntry *entry)
4523 {
4524   GtkEntryPrivate *priv = entry->priv;
4525   GtkBorder border;
4526
4527   _gtk_entry_effective_inner_border (GTK_ENTRY (entry), &border);
4528
4529   priv->icon_margin = border.left;
4530 }
4531
4532 static void
4533 gtk_entry_update_cached_style_values (GtkEntry *entry)
4534 {
4535   GtkEntryPrivate *priv = entry->priv;
4536   gint focus_width;
4537   gboolean interior_focus;
4538
4539   gtk_widget_style_get (GTK_WIDGET (entry),
4540                         "focus-line-width", &focus_width,
4541                         "interior-focus", &interior_focus,
4542                         NULL);
4543   priv->focus_width = focus_width;
4544   priv->interior_focus = interior_focus;
4545
4546   if (!priv->invisible_char_set)
4547     {
4548       gunichar ch = find_invisible_char (GTK_WIDGET (entry));
4549
4550       if (priv->invisible_char != ch)
4551         {
4552           priv->invisible_char = ch;
4553           g_object_notify (G_OBJECT (entry), "invisible-char");
4554         }
4555     }
4556 }
4557
4558 static void 
4559 gtk_entry_style_updated (GtkWidget *widget)
4560 {
4561   GtkEntry *entry = GTK_ENTRY (widget);
4562
4563   GTK_WIDGET_CLASS (gtk_entry_parent_class)->style_updated (widget);
4564
4565   gtk_entry_update_cached_style_values (entry);
4566
4567   gtk_entry_recompute (entry);
4568
4569   icon_theme_changed (entry);
4570   icon_margin_changed (entry);
4571 }
4572
4573 /* GtkCellEditable method implementations
4574  */
4575 static void
4576 gtk_cell_editable_entry_activated (GtkEntry *entry, gpointer data)
4577 {
4578   gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (entry));
4579   gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (entry));
4580 }
4581
4582 static gboolean
4583 gtk_cell_editable_key_press_event (GtkEntry    *entry,
4584                                    GdkEventKey *key_event,
4585                                    gpointer     data)
4586 {
4587   GtkEntryPrivate *priv = entry->priv;
4588
4589   if (key_event->keyval == GDK_KEY_Escape)
4590     {
4591       priv->editing_canceled = TRUE;
4592       gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (entry));
4593       gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (entry));
4594
4595       return TRUE;
4596     }
4597
4598   /* override focus */
4599   if (key_event->keyval == GDK_KEY_Up || key_event->keyval == GDK_KEY_Down)
4600     {
4601       gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (entry));
4602       gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (entry));
4603
4604       return TRUE;
4605     }
4606
4607   return FALSE;
4608 }
4609
4610 static void
4611 gtk_entry_start_editing (GtkCellEditable *cell_editable,
4612                          GdkEvent        *event)
4613 {
4614   GtkEntry *entry = GTK_ENTRY (cell_editable);
4615   GtkEntryPrivate *priv = entry->priv;
4616
4617   priv->is_cell_renderer = TRUE;
4618
4619   g_signal_connect (cell_editable, "activate",
4620                     G_CALLBACK (gtk_cell_editable_entry_activated), NULL);
4621   g_signal_connect (cell_editable, "key-press-event",
4622                     G_CALLBACK (gtk_cell_editable_key_press_event), NULL);
4623 }
4624
4625 static void
4626 gtk_entry_password_hint_free (GtkEntryPasswordHint *password_hint)
4627 {
4628   if (password_hint->source_id)
4629     g_source_remove (password_hint->source_id);
4630
4631   g_slice_free (GtkEntryPasswordHint, password_hint);
4632 }
4633
4634
4635 static gboolean
4636 gtk_entry_remove_password_hint (gpointer data)
4637 {
4638   GtkEntryPasswordHint *password_hint = g_object_get_qdata (data, quark_password_hint);
4639   password_hint->position = -1;
4640
4641   /* Force the string to be redrawn, but now without a visible character */
4642   gtk_entry_recompute (GTK_ENTRY (data));
4643   return FALSE;
4644 }
4645
4646 /* Default signal handlers
4647  */
4648 static void
4649 gtk_entry_real_insert_text (GtkEditable *editable,
4650                             const gchar *new_text,
4651                             gint         new_text_length,
4652                             gint        *position)
4653 {
4654   guint n_inserted;
4655   gint n_chars;
4656
4657   n_chars = g_utf8_strlen (new_text, new_text_length);
4658
4659   /*
4660    * The actual insertion into the buffer. This will end up firing the
4661    * following signal handlers: buffer_inserted_text(), buffer_notify_display_text(),
4662    * buffer_notify_text(), buffer_notify_length()
4663    */
4664   begin_change (GTK_ENTRY (editable));
4665
4666   n_inserted = gtk_entry_buffer_insert_text (get_buffer (GTK_ENTRY (editable)), *position, new_text, n_chars);
4667
4668   end_change (GTK_ENTRY (editable));
4669
4670   if (n_inserted != n_chars)
4671       gtk_widget_error_bell (GTK_WIDGET (editable));
4672
4673   *position += n_inserted;
4674 }
4675
4676 static void
4677 gtk_entry_real_delete_text (GtkEditable *editable,
4678                             gint         start_pos,
4679                             gint         end_pos)
4680 {
4681   /*
4682    * The actual deletion from the buffer. This will end up firing the
4683    * following signal handlers: buffer_deleted_text(), buffer_notify_display_text(),
4684    * buffer_notify_text(), buffer_notify_length()
4685    */
4686
4687   begin_change (GTK_ENTRY (editable));
4688
4689   gtk_entry_buffer_delete_text (get_buffer (GTK_ENTRY (editable)), start_pos, end_pos - start_pos);
4690
4691   end_change (GTK_ENTRY (editable));
4692 }
4693
4694 /* GtkEntryBuffer signal handlers
4695  */
4696 static void
4697 buffer_inserted_text (GtkEntryBuffer *buffer,
4698                       guint           position,
4699                       const gchar    *chars,
4700                       guint           n_chars,
4701                       GtkEntry       *entry)
4702 {
4703   GtkEntryPrivate *priv = entry->priv;
4704   guint password_hint_timeout;
4705   guint current_pos;
4706   gint selection_bound;
4707
4708   current_pos = priv->current_pos;
4709   if (current_pos > position)
4710     current_pos += n_chars;
4711
4712   selection_bound = priv->selection_bound;
4713   if (selection_bound > position)
4714     selection_bound += n_chars;
4715
4716   gtk_entry_set_positions (entry, current_pos, selection_bound);
4717
4718   /* Calculate the password hint if it needs to be displayed. */
4719   if (n_chars == 1 && !priv->visible)
4720     {
4721       g_object_get (gtk_widget_get_settings (GTK_WIDGET (entry)),
4722                     "gtk-entry-password-hint-timeout", &password_hint_timeout,
4723                     NULL);
4724
4725       if (password_hint_timeout > 0)
4726         {
4727           GtkEntryPasswordHint *password_hint = g_object_get_qdata (G_OBJECT (entry),
4728                                                                     quark_password_hint);
4729           if (!password_hint)
4730             {
4731               password_hint = g_slice_new0 (GtkEntryPasswordHint);
4732               g_object_set_qdata_full (G_OBJECT (entry), quark_password_hint, password_hint,
4733                                        (GDestroyNotify)gtk_entry_password_hint_free);
4734             }
4735
4736           password_hint->position = position;
4737           if (password_hint->source_id)
4738             g_source_remove (password_hint->source_id);
4739           password_hint->source_id = gdk_threads_add_timeout (password_hint_timeout,
4740                                                               (GSourceFunc)gtk_entry_remove_password_hint, entry);
4741         }
4742     }
4743 }
4744
4745 static void
4746 buffer_deleted_text (GtkEntryBuffer *buffer,
4747                      guint           position,
4748                      guint           n_chars,
4749                      GtkEntry       *entry)
4750 {
4751   GtkEntryPrivate *priv = entry->priv;
4752   guint end_pos = position + n_chars;
4753   gint selection_bound;
4754   guint current_pos;
4755
4756   current_pos = priv->current_pos;
4757   if (current_pos > position)
4758     current_pos -= MIN (current_pos, end_pos) - position;
4759
4760   selection_bound = priv->selection_bound;
4761   if (selection_bound > position)
4762     selection_bound -= MIN (selection_bound, end_pos) - position;
4763
4764   gtk_entry_set_positions (entry, current_pos, selection_bound);
4765
4766   /* We might have deleted the selection */
4767   gtk_entry_update_primary_selection (entry);
4768
4769   /* Disable the password hint if one exists. */
4770   if (!priv->visible)
4771     {
4772       GtkEntryPasswordHint *password_hint = g_object_get_qdata (G_OBJECT (entry),
4773                                                                 quark_password_hint);
4774       if (password_hint)
4775         {
4776           if (password_hint->source_id)
4777             g_source_remove (password_hint->source_id);
4778           password_hint->source_id = 0;
4779           password_hint->position = -1;
4780         }
4781     }
4782 }
4783
4784 static void
4785 buffer_notify_text (GtkEntryBuffer *buffer,
4786                     GParamSpec     *spec,
4787                     GtkEntry       *entry)
4788 {
4789   gtk_entry_recompute (entry);
4790   emit_changed (entry);
4791   g_object_notify (G_OBJECT (entry), "text");
4792 }
4793
4794 static void
4795 buffer_notify_length (GtkEntryBuffer *buffer,
4796                       GParamSpec     *spec,
4797                       GtkEntry       *entry)
4798 {
4799   g_object_notify (G_OBJECT (entry), "text-length");
4800 }
4801
4802 static void
4803 buffer_notify_max_length (GtkEntryBuffer *buffer,
4804                           GParamSpec     *spec,
4805                           GtkEntry       *entry)
4806 {
4807   g_object_notify (G_OBJECT (entry), "max-length");
4808 }
4809
4810 static void
4811 buffer_connect_signals (GtkEntry *entry)
4812 {
4813   g_signal_connect (get_buffer (entry), "inserted-text", G_CALLBACK (buffer_inserted_text), entry);
4814   g_signal_connect (get_buffer (entry), "deleted-text", G_CALLBACK (buffer_deleted_text), entry);
4815   g_signal_connect (get_buffer (entry), "notify::text", G_CALLBACK (buffer_notify_text), entry);
4816   g_signal_connect (get_buffer (entry), "notify::length", G_CALLBACK (buffer_notify_length), entry);
4817   g_signal_connect (get_buffer (entry), "notify::max-length", G_CALLBACK (buffer_notify_max_length), entry);
4818 }
4819
4820 static void
4821 buffer_disconnect_signals (GtkEntry *entry)
4822 {
4823   g_signal_handlers_disconnect_by_func (get_buffer (entry), buffer_inserted_text, entry);
4824   g_signal_handlers_disconnect_by_func (get_buffer (entry), buffer_deleted_text, entry);
4825   g_signal_handlers_disconnect_by_func (get_buffer (entry), buffer_notify_text, entry);
4826   g_signal_handlers_disconnect_by_func (get_buffer (entry), buffer_notify_length, entry);
4827   g_signal_handlers_disconnect_by_func (get_buffer (entry), buffer_notify_max_length, entry);
4828 }
4829
4830 /* Compute the X position for an offset that corresponds to the "more important
4831  * cursor position for that offset. We use this when trying to guess to which
4832  * end of the selection we should go to when the user hits the left or
4833  * right arrow key.
4834  */
4835 static gint
4836 get_better_cursor_x (GtkEntry *entry,
4837                      gint      offset)
4838 {
4839   GtkEntryPrivate *priv = entry->priv;
4840   GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (entry)));
4841   PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
4842   gboolean split_cursor;
4843   
4844   PangoLayout *layout = gtk_entry_ensure_layout (entry, TRUE);
4845   const gchar *text = pango_layout_get_text (layout);
4846   gint index = g_utf8_offset_to_pointer (text, offset) - text;
4847   
4848   PangoRectangle strong_pos, weak_pos;
4849   
4850   g_object_get (gtk_widget_get_settings (GTK_WIDGET (entry)),
4851                 "gtk-split-cursor", &split_cursor,
4852                 NULL);
4853
4854   pango_layout_get_cursor_pos (layout, index, &strong_pos, &weak_pos);
4855
4856   if (split_cursor)
4857     return strong_pos.x / PANGO_SCALE;
4858   else
4859     return (keymap_direction == priv->resolved_dir) ? strong_pos.x / PANGO_SCALE : weak_pos.x / PANGO_SCALE;
4860 }
4861
4862 static void
4863 gtk_entry_move_cursor (GtkEntry       *entry,
4864                        GtkMovementStep step,
4865                        gint            count,
4866                        gboolean        extend_selection)
4867 {
4868   GtkEntryPrivate *priv = entry->priv;
4869   gint new_pos = priv->current_pos;
4870
4871   _gtk_entry_reset_im_context (entry);
4872
4873   if (priv->current_pos != priv->selection_bound && !extend_selection)
4874     {
4875       /* If we have a current selection and aren't extending it, move to the
4876        * start/or end of the selection as appropriate
4877        */
4878       switch (step)
4879         {
4880         case GTK_MOVEMENT_VISUAL_POSITIONS:
4881           {
4882             gint current_x = get_better_cursor_x (entry, priv->current_pos);
4883             gint bound_x = get_better_cursor_x (entry, priv->selection_bound);
4884
4885             if (count <= 0)
4886               new_pos = current_x < bound_x ? priv->current_pos : priv->selection_bound;
4887             else 
4888               new_pos = current_x > bound_x ? priv->current_pos : priv->selection_bound;
4889             break;
4890           }
4891         case GTK_MOVEMENT_LOGICAL_POSITIONS:
4892         case GTK_MOVEMENT_WORDS:
4893           if (count < 0)
4894             new_pos = MIN (priv->current_pos, priv->selection_bound);
4895           else
4896             new_pos = MAX (priv->current_pos, priv->selection_bound);
4897           break;
4898         case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
4899         case GTK_MOVEMENT_PARAGRAPH_ENDS:
4900         case GTK_MOVEMENT_BUFFER_ENDS:
4901           new_pos = count < 0 ? 0 : gtk_entry_buffer_get_length (get_buffer (entry));
4902           break;
4903         case GTK_MOVEMENT_DISPLAY_LINES:
4904         case GTK_MOVEMENT_PARAGRAPHS:
4905         case GTK_MOVEMENT_PAGES:
4906         case GTK_MOVEMENT_HORIZONTAL_PAGES:
4907           break;
4908         }
4909     }
4910   else
4911     {
4912       switch (step)
4913         {
4914         case GTK_MOVEMENT_LOGICAL_POSITIONS:
4915           new_pos = gtk_entry_move_logically (entry, new_pos, count);
4916           break;
4917         case GTK_MOVEMENT_VISUAL_POSITIONS:
4918           new_pos = gtk_entry_move_visually (entry, new_pos, count);
4919           if (priv->current_pos == new_pos)
4920             {
4921               if (!extend_selection)
4922                 {
4923                   if (!gtk_widget_keynav_failed (GTK_WIDGET (entry),
4924                                                  count > 0 ?
4925                                                  GTK_DIR_RIGHT : GTK_DIR_LEFT))
4926                     {
4927                       GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (entry));
4928
4929                       if (toplevel)
4930                         gtk_widget_child_focus (toplevel,
4931                                                 count > 0 ?
4932                                                 GTK_DIR_RIGHT : GTK_DIR_LEFT);
4933                     }
4934                 }
4935               else
4936                 {
4937                   gtk_widget_error_bell (GTK_WIDGET (entry));
4938                 }
4939             }
4940           break;
4941         case GTK_MOVEMENT_WORDS:
4942           while (count > 0)
4943             {
4944               new_pos = gtk_entry_move_forward_word (entry, new_pos, FALSE);
4945               count--;
4946             }
4947           while (count < 0)
4948             {
4949               new_pos = gtk_entry_move_backward_word (entry, new_pos, FALSE);
4950               count++;
4951             }
4952           if (priv->current_pos == new_pos)
4953             gtk_widget_error_bell (GTK_WIDGET (entry));
4954           break;
4955         case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
4956         case GTK_MOVEMENT_PARAGRAPH_ENDS:
4957         case GTK_MOVEMENT_BUFFER_ENDS:
4958           new_pos = count < 0 ? 0 : gtk_entry_buffer_get_length (get_buffer (entry));
4959           if (priv->current_pos == new_pos)
4960             gtk_widget_error_bell (GTK_WIDGET (entry));
4961           break;
4962         case GTK_MOVEMENT_DISPLAY_LINES:
4963         case GTK_MOVEMENT_PARAGRAPHS:
4964         case GTK_MOVEMENT_PAGES:
4965         case GTK_MOVEMENT_HORIZONTAL_PAGES:
4966           break;
4967         }
4968     }
4969
4970   if (extend_selection)
4971     gtk_editable_select_region (GTK_EDITABLE (entry), priv->selection_bound, new_pos);
4972   else
4973     gtk_editable_set_position (GTK_EDITABLE (entry), new_pos);
4974   
4975   gtk_entry_pend_cursor_blink (entry);
4976 }
4977
4978 static void
4979 gtk_entry_insert_at_cursor (GtkEntry    *entry,
4980                             const gchar *str)
4981 {
4982   GtkEntryPrivate *priv = entry->priv;
4983   GtkEditable *editable = GTK_EDITABLE (entry);
4984   gint pos = priv->current_pos;
4985
4986   if (priv->editable)
4987     {
4988       _gtk_entry_reset_im_context (entry);
4989
4990       gtk_editable_insert_text (editable, str, -1, &pos);
4991       gtk_editable_set_position (editable, pos);
4992     }
4993 }
4994
4995 static void
4996 gtk_entry_delete_from_cursor (GtkEntry       *entry,
4997                               GtkDeleteType   type,
4998                               gint            count)
4999 {
5000   GtkEntryPrivate *priv = entry->priv;
5001   GtkEditable *editable = GTK_EDITABLE (entry);
5002   gint start_pos = priv->current_pos;
5003   gint end_pos = priv->current_pos;
5004   gint old_n_bytes = gtk_entry_buffer_get_bytes (get_buffer (entry));
5005   
5006   _gtk_entry_reset_im_context (entry);
5007
5008   if (!priv->editable)
5009     {
5010       gtk_widget_error_bell (GTK_WIDGET (entry));
5011       return;
5012     }
5013
5014   if (priv->selection_bound != priv->current_pos)
5015     {
5016       gtk_editable_delete_selection (editable);
5017       return;
5018     }
5019   
5020   switch (type)
5021     {
5022     case GTK_DELETE_CHARS:
5023       end_pos = gtk_entry_move_logically (entry, priv->current_pos, count);
5024       gtk_editable_delete_text (editable, MIN (start_pos, end_pos), MAX (start_pos, end_pos));
5025       break;
5026     case GTK_DELETE_WORDS:
5027       if (count < 0)
5028         {
5029           /* Move to end of current word, or if not on a word, end of previous word */
5030           end_pos = gtk_entry_move_backward_word (entry, end_pos, FALSE);
5031           end_pos = gtk_entry_move_forward_word (entry, end_pos, FALSE);
5032         }
5033       else if (count > 0)
5034         {
5035           /* Move to beginning of current word, or if not on a word, begining of next word */
5036           start_pos = gtk_entry_move_forward_word (entry, start_pos, FALSE);
5037           start_pos = gtk_entry_move_backward_word (entry, start_pos, FALSE);
5038         }
5039         
5040       /* Fall through */
5041     case GTK_DELETE_WORD_ENDS:
5042       while (count < 0)
5043         {
5044           start_pos = gtk_entry_move_backward_word (entry, start_pos, FALSE);
5045           count++;
5046         }
5047       while (count > 0)
5048         {
5049           end_pos = gtk_entry_move_forward_word (entry, end_pos, FALSE);
5050           count--;
5051         }
5052       gtk_editable_delete_text (editable, start_pos, end_pos);
5053       break;
5054     case GTK_DELETE_DISPLAY_LINE_ENDS:
5055     case GTK_DELETE_PARAGRAPH_ENDS:
5056       if (count < 0)
5057         gtk_editable_delete_text (editable, 0, priv->current_pos);
5058       else
5059         gtk_editable_delete_text (editable, priv->current_pos, -1);
5060       break;
5061     case GTK_DELETE_DISPLAY_LINES:
5062     case GTK_DELETE_PARAGRAPHS:
5063       gtk_editable_delete_text (editable, 0, -1);  
5064       break;
5065     case GTK_DELETE_WHITESPACE:
5066       gtk_entry_delete_whitespace (entry);
5067       break;
5068     }
5069
5070   if (gtk_entry_buffer_get_bytes (get_buffer (entry)) == old_n_bytes)
5071     gtk_widget_error_bell (GTK_WIDGET (entry));
5072
5073   gtk_entry_pend_cursor_blink (entry);
5074 }
5075
5076 static void
5077 gtk_entry_backspace (GtkEntry *entry)
5078 {
5079   GtkEntryPrivate *priv = entry->priv;
5080   GtkEditable *editable = GTK_EDITABLE (entry);
5081   gint prev_pos;
5082
5083   _gtk_entry_reset_im_context (entry);
5084
5085   if (!priv->editable)
5086     {
5087       gtk_widget_error_bell (GTK_WIDGET (entry));
5088       return;
5089     }
5090
5091   if (priv->selection_bound != priv->current_pos)
5092     {
5093       gtk_editable_delete_selection (editable);
5094       return;
5095     }
5096
5097   prev_pos = gtk_entry_move_logically (entry, priv->current_pos, -1);
5098
5099   if (prev_pos < priv->current_pos)
5100     {
5101       PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
5102       PangoLogAttr *log_attrs;
5103       gint n_attrs;
5104
5105       pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
5106
5107       /* Deleting parts of characters */
5108       if (log_attrs[priv->current_pos].backspace_deletes_character)
5109         {
5110           gchar *cluster_text;
5111           gchar *normalized_text;
5112           glong  len;
5113
5114           cluster_text = gtk_entry_get_display_text (entry, prev_pos,
5115                                                      priv->current_pos);
5116           normalized_text = g_utf8_normalize (cluster_text,
5117                                               strlen (cluster_text),
5118                                               G_NORMALIZE_NFD);
5119           len = g_utf8_strlen (normalized_text, -1);
5120
5121           gtk_editable_delete_text (editable, prev_pos, priv->current_pos);
5122           if (len > 1)
5123             {
5124               gint pos = priv->current_pos;
5125
5126               gtk_editable_insert_text (editable, normalized_text,
5127                                         g_utf8_offset_to_pointer (normalized_text, len - 1) - normalized_text,
5128                                         &pos);
5129               gtk_editable_set_position (editable, pos);
5130             }
5131
5132           g_free (normalized_text);
5133           g_free (cluster_text);
5134         }
5135       else
5136         {
5137           gtk_editable_delete_text (editable, prev_pos, priv->current_pos);
5138         }
5139       
5140       g_free (log_attrs);
5141     }
5142   else
5143     {
5144       gtk_widget_error_bell (GTK_WIDGET (entry));
5145     }
5146
5147   gtk_entry_pend_cursor_blink (entry);
5148 }
5149
5150 static void
5151 gtk_entry_copy_clipboard (GtkEntry *entry)
5152 {
5153   GtkEntryPrivate *priv = entry->priv;
5154   GtkEditable *editable = GTK_EDITABLE (entry);
5155   gint start, end;
5156   gchar *str;
5157
5158   if (gtk_editable_get_selection_bounds (editable, &start, &end))
5159     {
5160       if (!priv->visible)
5161         {
5162           gtk_widget_error_bell (GTK_WIDGET (entry));
5163           return;
5164         }
5165
5166       str = gtk_entry_get_display_text (entry, start, end);
5167       gtk_clipboard_set_text (gtk_widget_get_clipboard (GTK_WIDGET (entry),
5168                                                         GDK_SELECTION_CLIPBOARD),
5169                               str, -1);
5170       g_free (str);
5171     }
5172 }
5173
5174 static void
5175 gtk_entry_cut_clipboard (GtkEntry *entry)
5176 {
5177   GtkEntryPrivate *priv = entry->priv;
5178   GtkEditable *editable = GTK_EDITABLE (entry);
5179   gint start, end;
5180
5181   if (!priv->visible)
5182     {
5183       gtk_widget_error_bell (GTK_WIDGET (entry));
5184       return;
5185     }
5186
5187   gtk_entry_copy_clipboard (entry);
5188
5189   if (priv->editable)
5190     {
5191       if (gtk_editable_get_selection_bounds (editable, &start, &end))
5192         gtk_editable_delete_text (editable, start, end);
5193     }
5194   else
5195     {
5196       gtk_widget_error_bell (GTK_WIDGET (entry));
5197     }
5198 }
5199
5200 static void
5201 gtk_entry_paste_clipboard (GtkEntry *entry)
5202 {
5203   GtkEntryPrivate *priv = entry->priv;
5204
5205   if (priv->editable)
5206     gtk_entry_paste (entry, GDK_SELECTION_CLIPBOARD);
5207   else
5208     gtk_widget_error_bell (GTK_WIDGET (entry));
5209 }
5210
5211 static void
5212 gtk_entry_delete_cb (GtkEntry *entry)
5213 {
5214   GtkEntryPrivate *priv = entry->priv;
5215   GtkEditable *editable = GTK_EDITABLE (entry);
5216   gint start, end;
5217
5218   if (priv->editable)
5219     {
5220       if (gtk_editable_get_selection_bounds (editable, &start, &end))
5221         gtk_editable_delete_text (editable, start, end);
5222     }
5223 }
5224
5225 static void
5226 gtk_entry_toggle_overwrite (GtkEntry *entry)
5227 {
5228   GtkEntryPrivate *priv = entry->priv;
5229
5230   priv->overwrite_mode = !priv->overwrite_mode;
5231   gtk_entry_pend_cursor_blink (entry);
5232   gtk_widget_queue_draw (GTK_WIDGET (entry));
5233 }
5234
5235 static void
5236 gtk_entry_select_all (GtkEntry *entry)
5237 {
5238   gtk_entry_select_line (entry);
5239 }
5240
5241 static void
5242 gtk_entry_real_activate (GtkEntry *entry)
5243 {
5244   GtkEntryPrivate *priv = entry->priv;
5245   GtkWindow *window;
5246   GtkWidget *default_widget, *focus_widget;
5247   GtkWidget *toplevel;
5248   GtkWidget *widget;
5249
5250   widget = GTK_WIDGET (entry);
5251
5252   if (priv->activates_default)
5253     {
5254       toplevel = gtk_widget_get_toplevel (widget);
5255       if (GTK_IS_WINDOW (toplevel))
5256         {
5257           window = GTK_WINDOW (toplevel);
5258
5259           if (window)
5260             {
5261               default_widget = gtk_window_get_default_widget (window);
5262               focus_widget = gtk_window_get_focus (window);
5263               if (widget != default_widget &&
5264                   !(widget == focus_widget && (!default_widget || !gtk_widget_get_sensitive (default_widget))))
5265                 gtk_window_activate_default (window);
5266             }
5267         }
5268     }
5269 }
5270
5271 static void
5272 keymap_direction_changed (GdkKeymap *keymap,
5273                           GtkEntry  *entry)
5274 {
5275   gtk_entry_recompute (entry);
5276 }
5277
5278 /* IM Context Callbacks
5279  */
5280
5281 static void
5282 gtk_entry_commit_cb (GtkIMContext *context,
5283                      const gchar  *str,
5284                      GtkEntry     *entry)
5285 {
5286   GtkEntryPrivate *priv = entry->priv;
5287
5288   if (priv->editable)
5289     gtk_entry_enter_text (entry, str);
5290 }
5291
5292 static void 
5293 gtk_entry_preedit_changed_cb (GtkIMContext *context,
5294                               GtkEntry     *entry)
5295 {
5296   GtkEntryPrivate *priv = entry->priv;
5297
5298   if (priv->editable)
5299     {
5300       gchar *preedit_string;
5301       gint cursor_pos;
5302
5303       gtk_im_context_get_preedit_string (priv->im_context,
5304                                          &preedit_string, NULL,
5305                                          &cursor_pos);
5306       g_signal_emit (entry, signals[PREEDIT_CHANGED], 0, preedit_string);
5307       priv->preedit_length = strlen (preedit_string);
5308       cursor_pos = CLAMP (cursor_pos, 0, g_utf8_strlen (preedit_string, -1));
5309       priv->preedit_cursor = cursor_pos;
5310       g_free (preedit_string);
5311     
5312       gtk_entry_recompute (entry);
5313     }
5314 }
5315
5316 static gboolean
5317 gtk_entry_retrieve_surrounding_cb (GtkIMContext *context,
5318                                    GtkEntry     *entry)
5319 {
5320   GtkEntryPrivate *priv = entry->priv;
5321   gchar *text;
5322
5323   /* XXXX ??? does this even make sense when text is not visible? Should we return FALSE? */
5324   text = gtk_entry_get_display_text (entry, 0, -1);
5325   gtk_im_context_set_surrounding (context, text, strlen (text), /* Length in bytes */
5326                                   g_utf8_offset_to_pointer (text, priv->current_pos) - text);
5327   g_free (text);
5328
5329   return TRUE;
5330 }
5331
5332 static gboolean
5333 gtk_entry_delete_surrounding_cb (GtkIMContext *slave,
5334                                  gint          offset,
5335                                  gint          n_chars,
5336                                  GtkEntry     *entry)
5337 {
5338   GtkEntryPrivate *priv = entry->priv;
5339
5340   if (priv->editable)
5341     gtk_editable_delete_text (GTK_EDITABLE (entry),
5342                               priv->current_pos + offset,
5343                               priv->current_pos + offset + n_chars);
5344
5345   return TRUE;
5346 }
5347
5348 /* Internal functions
5349  */
5350
5351 /* Used for im_commit_cb and inserting Unicode chars */
5352 static void
5353 gtk_entry_enter_text (GtkEntry       *entry,
5354                       const gchar    *str)
5355 {
5356   GtkEntryPrivate *priv = entry->priv;
5357   GtkEditable *editable = GTK_EDITABLE (entry);
5358   gint tmp_pos;
5359   gboolean old_need_im_reset;
5360
5361   old_need_im_reset = priv->need_im_reset;
5362   priv->need_im_reset = FALSE;
5363
5364   if (gtk_editable_get_selection_bounds (editable, NULL, NULL))
5365     gtk_editable_delete_selection (editable);
5366   else
5367     {
5368       if (priv->overwrite_mode)
5369         gtk_entry_delete_from_cursor (entry, GTK_DELETE_CHARS, 1);
5370     }
5371
5372   tmp_pos = priv->current_pos;
5373   gtk_editable_insert_text (editable, str, strlen (str), &tmp_pos);
5374   gtk_editable_set_position (editable, tmp_pos);
5375
5376   priv->need_im_reset = old_need_im_reset;
5377 }
5378
5379 /* All changes to priv->current_pos and priv->selection_bound
5380  * should go through this function.
5381  */
5382 static void
5383 gtk_entry_set_positions (GtkEntry *entry,
5384                          gint      current_pos,
5385                          gint      selection_bound)
5386 {
5387   GtkEntryPrivate *priv = entry->priv;
5388   gboolean changed = FALSE;
5389
5390   g_object_freeze_notify (G_OBJECT (entry));
5391   
5392   if (current_pos != -1 &&
5393       priv->current_pos != current_pos)
5394     {
5395       priv->current_pos = current_pos;
5396       changed = TRUE;
5397
5398       g_object_notify (G_OBJECT (entry), "cursor-position");
5399     }
5400
5401   if (selection_bound != -1 &&
5402       priv->selection_bound != selection_bound)
5403     {
5404       priv->selection_bound = selection_bound;
5405       changed = TRUE;
5406       
5407       g_object_notify (G_OBJECT (entry), "selection-bound");
5408     }
5409
5410   g_object_thaw_notify (G_OBJECT (entry));
5411
5412   if (changed) 
5413     {
5414       gtk_entry_move_adjustments (entry);
5415       gtk_entry_recompute (entry);
5416     }
5417 }
5418
5419 static void
5420 gtk_entry_reset_layout (GtkEntry *entry)
5421 {
5422   GtkEntryPrivate *priv = entry->priv;
5423
5424   if (priv->cached_layout)
5425     {
5426       g_object_unref (priv->cached_layout);
5427       priv->cached_layout = NULL;
5428     }
5429 }
5430
5431 static void
5432 update_im_cursor_location (GtkEntry *entry)
5433 {
5434   GtkEntryPrivate *priv = entry->priv;
5435   GdkRectangle area;
5436   gint strong_x;
5437   gint strong_xoffset;
5438   gint area_width, area_height;
5439
5440   gtk_entry_get_cursor_locations (entry, CURSOR_STANDARD, &strong_x, NULL);
5441   gtk_entry_get_text_area_size (entry, NULL, NULL, &area_width, &area_height);
5442
5443   strong_xoffset = strong_x - priv->scroll_offset;
5444   if (strong_xoffset < 0)
5445     {
5446       strong_xoffset = 0;
5447     }
5448   else if (strong_xoffset > area_width)
5449     {
5450       strong_xoffset = area_width;
5451     }
5452   area.x = strong_xoffset;
5453   area.y = 0;
5454   area.width = 0;
5455   area.height = area_height;
5456
5457   gtk_im_context_set_cursor_location (priv->im_context, &area);
5458 }
5459
5460 static gboolean
5461 recompute_idle_func (gpointer data)
5462 {
5463   GtkEntry *entry = GTK_ENTRY (data);
5464   GtkEntryPrivate *priv = entry->priv;
5465
5466   priv->recompute_idle = 0;
5467
5468   if (gtk_widget_has_screen (GTK_WIDGET (entry)))
5469     {
5470       gtk_entry_adjust_scroll (entry);
5471       gtk_widget_queue_draw (GTK_WIDGET (entry));
5472
5473       update_im_cursor_location (entry);
5474     }
5475
5476   return FALSE;
5477 }
5478
5479 static void
5480 gtk_entry_recompute (GtkEntry *entry)
5481 {
5482   GtkEntryPrivate *priv = entry->priv;
5483
5484   gtk_entry_reset_layout (entry);
5485   gtk_entry_check_cursor_blink (entry);
5486
5487   if (!priv->recompute_idle)
5488     {
5489       priv->recompute_idle = gdk_threads_add_idle_full (G_PRIORITY_HIGH_IDLE + 15, /* between resize and redraw */
5490                                                recompute_idle_func, entry, NULL); 
5491     }
5492 }
5493
5494 static void
5495 gtk_entry_get_placeholder_text_color (GtkEntry   *entry,
5496                                       PangoColor *color)
5497 {
5498   GtkWidget *widget = GTK_WIDGET (entry);
5499   GtkStyleContext *context;
5500   GdkRGBA fg = { 0.5, 0.5, 0.5 };
5501
5502   context = gtk_widget_get_style_context (widget);
5503   gtk_style_context_lookup_color (context, "placeholder_text_color", &fg);
5504
5505   color->red = CLAMP (fg.red * 65535. + 0.5, 0, 65535);
5506   color->green = CLAMP (fg.green * 65535. + 0.5, 0, 65535);
5507   color->blue = CLAMP (fg.blue * 65535. + 0.5, 0, 65535);
5508 }
5509
5510 static inline gboolean
5511 show_placeholder_text (GtkEntry *entry)
5512 {
5513   GtkEntryPrivate *priv = entry->priv;
5514
5515   if (!gtk_widget_has_focus (GTK_WIDGET (entry)) &&
5516       gtk_entry_buffer_get_bytes (get_buffer (entry)) == 0 &&
5517       priv->placeholder_text != NULL)
5518     return TRUE;
5519
5520   return FALSE;
5521 }
5522
5523 static PangoLayout *
5524 gtk_entry_create_layout (GtkEntry *entry,
5525                          gboolean  include_preedit)
5526 {
5527   GtkEntryPrivate *priv = entry->priv;
5528   GtkWidget *widget = GTK_WIDGET (entry);
5529   PangoLayout *layout = gtk_widget_create_pango_layout (widget, NULL);
5530   PangoAttrList *tmp_attrs = pango_attr_list_new ();
5531   gboolean placeholder_layout = show_placeholder_text (entry);
5532
5533   gchar *preedit_string = NULL;
5534   gint preedit_length = 0;
5535   PangoAttrList *preedit_attrs = NULL;
5536
5537   gchar *display;
5538   guint n_bytes;
5539
5540   pango_layout_set_single_paragraph_mode (layout, TRUE);
5541
5542   display = placeholder_layout ? g_strdup (priv->placeholder_text) : gtk_entry_get_display_text (entry, 0, -1);
5543   n_bytes = strlen (display);
5544
5545   if (!placeholder_layout && include_preedit)
5546     {
5547       gtk_im_context_get_preedit_string (priv->im_context,
5548                                          &preedit_string, &preedit_attrs, NULL);
5549       preedit_length = priv->preedit_length;
5550     }
5551   else if (placeholder_layout)
5552     {
5553       PangoColor color;
5554       PangoAttribute *attr;
5555
5556       gtk_entry_get_placeholder_text_color (entry, &color);
5557       attr = pango_attr_foreground_new (color.red, color.green, color.blue);
5558       attr->start_index = 0;
5559       attr->end_index = G_MAXINT;
5560       pango_attr_list_insert (tmp_attrs, attr);
5561     }
5562
5563   if (preedit_length)
5564     {
5565       GString *tmp_string = g_string_new (display);
5566       gint cursor_index = g_utf8_offset_to_pointer (display, priv->current_pos) - display;
5567
5568       g_string_insert (tmp_string, cursor_index, preedit_string);
5569       
5570       pango_layout_set_text (layout, tmp_string->str, tmp_string->len);
5571       
5572       pango_attr_list_splice (tmp_attrs, preedit_attrs,
5573                               cursor_index, preedit_length);
5574       
5575       g_string_free (tmp_string, TRUE);
5576     }
5577   else
5578     {
5579       PangoDirection pango_dir;
5580       
5581       if (gtk_entry_get_display_mode (entry) == DISPLAY_NORMAL)
5582         pango_dir = pango_find_base_dir (display, n_bytes);
5583       else
5584         pango_dir = PANGO_DIRECTION_NEUTRAL;
5585
5586       if (pango_dir == PANGO_DIRECTION_NEUTRAL)
5587         {
5588           if (gtk_widget_has_focus (widget))
5589             {
5590               GdkDisplay *display = gtk_widget_get_display (widget);
5591               GdkKeymap *keymap = gdk_keymap_get_for_display (display);
5592               if (gdk_keymap_get_direction (keymap) == PANGO_DIRECTION_RTL)
5593                 pango_dir = PANGO_DIRECTION_RTL;
5594               else
5595                 pango_dir = PANGO_DIRECTION_LTR;
5596             }
5597           else
5598             {
5599               if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
5600                 pango_dir = PANGO_DIRECTION_RTL;
5601               else
5602                 pango_dir = PANGO_DIRECTION_LTR;
5603             }
5604         }
5605
5606       pango_context_set_base_dir (gtk_widget_get_pango_context (widget),
5607                                   pango_dir);
5608
5609       priv->resolved_dir = pango_dir;
5610
5611       pango_layout_set_text (layout, display, n_bytes);
5612     }
5613       
5614   pango_layout_set_attributes (layout, tmp_attrs);
5615
5616   g_free (preedit_string);
5617   g_free (display);
5618
5619   if (preedit_attrs)
5620     pango_attr_list_unref (preedit_attrs);
5621       
5622   pango_attr_list_unref (tmp_attrs);
5623
5624   return layout;
5625 }
5626
5627 static PangoLayout *
5628 gtk_entry_ensure_layout (GtkEntry *entry,
5629                          gboolean  include_preedit)
5630 {
5631   GtkEntryPrivate *priv = entry->priv;
5632
5633   if (priv->preedit_length > 0 &&
5634       !include_preedit != !priv->cache_includes_preedit)
5635     gtk_entry_reset_layout (entry);
5636
5637   if (!priv->cached_layout)
5638     {
5639       priv->cached_layout = gtk_entry_create_layout (entry, include_preedit);
5640       priv->cache_includes_preedit = include_preedit;
5641     }
5642
5643   return priv->cached_layout;
5644 }
5645
5646 static void
5647 get_layout_position (GtkEntry *entry,
5648                      gint     *x,
5649                      gint     *y)
5650 {
5651   GtkEntryPrivate *priv = entry->priv;
5652   PangoLayout *layout;
5653   PangoRectangle logical_rect;
5654   gint area_width, area_height;
5655   GtkBorder inner_border;
5656   gint y_pos;
5657   PangoLayoutLine *line;
5658   
5659   layout = gtk_entry_ensure_layout (entry, TRUE);
5660
5661   gtk_entry_get_text_area_size (entry, NULL, NULL, &area_width, &area_height);
5662   _gtk_entry_effective_inner_border (entry, &inner_border);
5663
5664   area_height = PANGO_SCALE * (area_height - inner_border.top - inner_border.bottom);
5665
5666   line = pango_layout_get_lines_readonly (layout)->data;
5667   pango_layout_line_get_extents (line, NULL, &logical_rect);
5668   
5669   /* Align primarily for locale's ascent/descent */
5670   y_pos = ((area_height - priv->ascent - priv->descent) / 2 +
5671            priv->ascent + logical_rect.y);
5672
5673   /* Now see if we need to adjust to fit in actual drawn string */
5674   if (logical_rect.height > area_height)
5675     y_pos = (area_height - logical_rect.height) / 2;
5676   else if (y_pos < 0)
5677     y_pos = 0;
5678   else if (y_pos + logical_rect.height > area_height)
5679     y_pos = area_height - logical_rect.height;
5680   
5681   y_pos = inner_border.top + y_pos / PANGO_SCALE;
5682
5683   if (x)
5684     *x = inner_border.left - priv->scroll_offset;
5685
5686   if (y)
5687     *y = y_pos;
5688 }
5689
5690 static void
5691 draw_text_with_color (GtkEntry *entry,
5692                       cairo_t  *cr,
5693                       GdkRGBA  *default_color)
5694 {
5695   GtkEntryPrivate *priv = entry->priv;
5696   PangoLayout *layout = gtk_entry_ensure_layout (entry, TRUE);
5697   GtkWidget *widget;
5698   gint x, y;
5699   gint start_pos, end_pos;
5700
5701   widget = GTK_WIDGET (entry);
5702
5703   cairo_save (cr);
5704
5705   get_layout_position (entry, &x, &y);
5706
5707   cairo_move_to (cr, x, y);
5708   gdk_cairo_set_source_rgba (cr, default_color);
5709   pango_cairo_show_layout (cr, layout);
5710
5711   if (gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &start_pos, &end_pos))
5712     {
5713       gint *ranges;
5714       gint n_ranges, i;
5715       PangoRectangle logical_rect;
5716       GdkRGBA selection_color, text_color;
5717       GtkBorder inner_border;
5718       GtkStyleContext *context;
5719       GtkStateFlags state;
5720
5721       context = gtk_widget_get_style_context (widget);
5722       pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
5723       gtk_entry_get_pixel_ranges (entry, &ranges, &n_ranges);
5724
5725       state = gtk_widget_get_state_flags (widget);
5726       state |= GTK_STATE_FLAG_SELECTED;
5727
5728       gtk_style_context_get_background_color (context, state, &selection_color);
5729       gtk_style_context_get_color (context, state, &text_color);
5730
5731       _gtk_entry_effective_inner_border (entry, &inner_border);
5732
5733       for (i = 0; i < n_ranges; ++i)
5734         cairo_rectangle (cr,
5735                          inner_border.left - priv->scroll_offset + ranges[2 * i],
5736                          y,
5737                          ranges[2 * i + 1],
5738                          logical_rect.height);
5739
5740       cairo_clip (cr);
5741           
5742       gdk_cairo_set_source_rgba (cr, &selection_color);
5743       cairo_paint (cr);
5744
5745       cairo_move_to (cr, x, y);
5746       gdk_cairo_set_source_rgba (cr, &text_color);
5747       pango_cairo_show_layout (cr, layout);
5748   
5749       g_free (ranges);
5750     }
5751   cairo_restore (cr);
5752 }
5753
5754 static void
5755 gtk_entry_draw_text (GtkEntry *entry,
5756                      cairo_t  *cr)
5757 {
5758   GtkEntryPrivate *priv = entry->priv;
5759   GtkWidget *widget = GTK_WIDGET (entry);
5760   GtkStateFlags state = 0;
5761   GdkRGBA text_color, bar_text_color;
5762   GtkStyleContext *context;
5763   gint width, height;
5764   gint progress_x, progress_y, progress_width, progress_height;
5765   gint clip_width, clip_height;
5766
5767   /* Nothing to display at all */
5768   if (gtk_entry_get_display_mode (entry) == DISPLAY_BLANK)
5769     return;
5770
5771   state = gtk_widget_get_state_flags (widget);
5772   context = gtk_widget_get_style_context (widget);
5773
5774   gtk_style_context_get_color (context, state, &text_color);
5775
5776   /* Get foreground color for progressbars */
5777   gtk_style_context_save (context);
5778   gtk_style_context_add_class (context, GTK_STYLE_CLASS_PROGRESSBAR);
5779   gtk_style_context_get_color (context, state, &bar_text_color);
5780   gtk_style_context_restore (context);
5781
5782   get_progress_area (widget,
5783                      &progress_x, &progress_y,
5784                      &progress_width, &progress_height);
5785
5786   cairo_save (cr);
5787
5788   clip_width = gdk_window_get_width (priv->text_area);
5789   clip_height = gdk_window_get_height (priv->text_area);
5790   cairo_rectangle (cr, 0, 0, clip_width, clip_height);
5791   cairo_clip (cr);
5792
5793   /* If the color is the same, or the progress area has a zero
5794    * size, then we only need to draw once. */
5795   if (gdk_rgba_equal (&text_color, &bar_text_color) ||
5796       ((progress_width == 0) || (progress_height == 0)))
5797     {
5798       draw_text_with_color (entry, cr, &text_color);
5799     }
5800   else
5801     {
5802       int frame_x, frame_y, area_x, area_y;
5803
5804       width = gdk_window_get_width (priv->text_area);
5805       height = gdk_window_get_height (priv->text_area);
5806
5807       cairo_save (cr);
5808
5809       cairo_set_fill_rule (cr, CAIRO_FILL_RULE_EVEN_ODD);
5810       cairo_rectangle (cr, 0, 0, width, height);
5811
5812       /* progres area is frame-relative, we need it text-area window
5813        * relative */
5814       get_frame_size (entry, TRUE, &frame_x, &frame_y, NULL, NULL);
5815       gdk_window_get_position (priv->text_area, &area_x, &area_y);
5816       progress_x += frame_x - area_x;
5817       progress_y += frame_y - area_y;
5818
5819       cairo_rectangle (cr, progress_x, progress_y,
5820                        progress_width, progress_height);
5821       cairo_clip (cr);
5822       cairo_set_fill_rule (cr, CAIRO_FILL_RULE_WINDING);
5823   
5824       draw_text_with_color (entry, cr, &text_color);
5825       cairo_restore (cr);
5826
5827       cairo_save (cr);
5828
5829       cairo_rectangle (cr, progress_x, progress_y,
5830                        progress_width, progress_height);
5831       cairo_clip (cr);
5832
5833       draw_text_with_color (entry, cr, &bar_text_color);
5834
5835       cairo_restore (cr);
5836     }
5837
5838   cairo_restore (cr);
5839 }
5840
5841 static void
5842 gtk_entry_draw_cursor (GtkEntry  *entry,
5843                        cairo_t   *cr,
5844                        CursorType type)
5845 {
5846   GtkEntryPrivate *priv = entry->priv;
5847   GtkWidget *widget = GTK_WIDGET (entry);
5848   GtkStyleContext *context;
5849   PangoRectangle cursor_rect;
5850   gint cursor_index;
5851   gboolean block;
5852   gboolean block_at_line_end;
5853   PangoLayout *layout;
5854   const char *text;
5855
5856   context = gtk_widget_get_style_context (widget);
5857
5858   layout = gtk_entry_ensure_layout (entry, TRUE);
5859   text = pango_layout_get_text (layout);
5860   cursor_index = g_utf8_offset_to_pointer (text, priv->current_pos + priv->preedit_cursor) - text;
5861
5862   if (!priv->overwrite_mode)
5863     block = FALSE;
5864   else
5865     block = _gtk_text_util_get_block_cursor_location (layout,
5866                                                       cursor_index, &cursor_rect, &block_at_line_end);
5867
5868   if (!block)
5869     {
5870       GtkBorder inner_border;
5871
5872       _gtk_entry_effective_inner_border (entry, &inner_border);
5873
5874       gtk_render_insertion_cursor (context, cr,
5875                                    inner_border.left - priv->scroll_offset, inner_border.top,
5876                                    layout, cursor_index, priv->resolved_dir);
5877     }
5878   else /* overwrite_mode */
5879     {
5880       GdkRGBA cursor_color;
5881       GdkRectangle rect;
5882       gint x, y;
5883
5884       cairo_save (cr);
5885
5886       get_layout_position (entry, &x, &y);
5887
5888       rect.x = PANGO_PIXELS (cursor_rect.x) + x;
5889       rect.y = PANGO_PIXELS (cursor_rect.y) + y;
5890       rect.width = PANGO_PIXELS (cursor_rect.width);
5891       rect.height = PANGO_PIXELS (cursor_rect.height);
5892
5893       _gtk_style_context_get_cursor_color (context, &cursor_color, NULL);
5894       gdk_cairo_set_source_rgba (cr, &cursor_color);
5895       gdk_cairo_rectangle (cr, &rect);
5896       cairo_fill (cr);
5897
5898       if (!block_at_line_end)
5899         {
5900           GtkStateFlags state;
5901           GdkRGBA color;
5902
5903           state = gtk_widget_get_state_flags (widget);
5904           gtk_style_context_get_background_color (context, state, &color);
5905
5906           gdk_cairo_rectangle (cr, &rect);
5907           cairo_clip (cr);
5908           cairo_move_to (cr, x, y);
5909           gdk_cairo_set_source_rgba (cr, &color);
5910           pango_cairo_show_layout (cr, layout);
5911         }
5912
5913       cairo_restore (cr);
5914     }
5915 }
5916
5917 void
5918 _gtk_entry_reset_im_context (GtkEntry *entry)
5919 {
5920   GtkEntryPrivate *priv = entry->priv;
5921
5922   if (priv->need_im_reset)
5923     {
5924       priv->need_im_reset = FALSE;
5925       gtk_im_context_reset (priv->im_context);
5926     }
5927 }
5928
5929 /**
5930  * gtk_entry_reset_im_context:
5931  * @entry: a #GtkEntry
5932  *
5933  * Reset the input method context of the entry if needed.
5934  *
5935  * This can be necessary in the case where modifying the buffer
5936  * would confuse on-going input method behavior.
5937  *
5938  * Since: 2.22
5939  */
5940 void
5941 gtk_entry_reset_im_context (GtkEntry *entry)
5942 {
5943   g_return_if_fail (GTK_IS_ENTRY (entry));
5944
5945   _gtk_entry_reset_im_context (entry);
5946 }
5947
5948 /**
5949  * gtk_entry_im_context_filter_keypress:
5950  * @entry: a #GtkEntry
5951  * @event: (type Gdk.EventKey): the key event
5952  *
5953  * Allow the #GtkEntry input method to internally handle key press
5954  * and release events. If this function returns %TRUE, then no further
5955  * processing should be done for this key event. See
5956  * gtk_im_context_filter_keypress().
5957  *
5958  * Note that you are expected to call this function from your handler
5959  * when overriding key event handling. This is needed in the case when
5960  * you need to insert your own key handling between the input method
5961  * and the default key event handling of the #GtkEntry.
5962  * See gtk_text_view_reset_im_context() for an example of use.
5963  *
5964  * Return value: %TRUE if the input method handled the key event.
5965  *
5966  * Since: 2.22
5967  */
5968 gboolean
5969 gtk_entry_im_context_filter_keypress (GtkEntry    *entry,
5970                                       GdkEventKey *event)
5971 {
5972   GtkEntryPrivate *priv;
5973
5974   g_return_val_if_fail (GTK_IS_ENTRY (entry), FALSE);
5975
5976   priv = entry->priv;
5977
5978   return gtk_im_context_filter_keypress (priv->im_context, event);
5979 }
5980
5981 GtkIMContext*
5982 _gtk_entry_get_im_context (GtkEntry *entry)
5983 {
5984   return entry->priv->im_context;
5985 }
5986
5987 static gint
5988 gtk_entry_find_position (GtkEntry *entry,
5989                          gint      x)
5990 {
5991   GtkEntryPrivate *priv = entry->priv;
5992   PangoLayout *layout;
5993   PangoLayoutLine *line;
5994   gint index;
5995   gint pos;
5996   gint trailing;
5997   const gchar *text;
5998   gint cursor_index;
5999   
6000   layout = gtk_entry_ensure_layout (entry, TRUE);
6001   text = pango_layout_get_text (layout);
6002   cursor_index = g_utf8_offset_to_pointer (text, priv->current_pos) - text;
6003
6004   line = pango_layout_get_lines_readonly (layout)->data;
6005   pango_layout_line_x_to_index (line, x * PANGO_SCALE, &index, &trailing);
6006
6007   if (index >= cursor_index && priv->preedit_length)
6008     {
6009       if (index >= cursor_index + priv->preedit_length)
6010         index -= priv->preedit_length;
6011       else
6012         {
6013           index = cursor_index;
6014           trailing = 0;
6015         }
6016     }
6017
6018   pos = g_utf8_pointer_to_offset (text, text + index);
6019   pos += trailing;
6020
6021   return pos;
6022 }
6023
6024 static void
6025 gtk_entry_get_cursor_locations (GtkEntry   *entry,
6026                                 CursorType  type,
6027                                 gint       *strong_x,
6028                                 gint       *weak_x)
6029 {
6030   GtkEntryPrivate *priv = entry->priv;
6031   DisplayMode mode = gtk_entry_get_display_mode (entry);
6032
6033   /* Nothing to display at all, so no cursor is relevant */
6034   if (mode == DISPLAY_BLANK)
6035     {
6036       if (strong_x)
6037         *strong_x = 0;
6038       
6039       if (weak_x)
6040         *weak_x = 0;
6041     }
6042   else
6043     {
6044       PangoLayout *layout = gtk_entry_ensure_layout (entry, TRUE);
6045       const gchar *text = pango_layout_get_text (layout);
6046       PangoRectangle strong_pos, weak_pos;
6047       gint index;
6048   
6049       if (type == CURSOR_STANDARD)
6050         {
6051           index = g_utf8_offset_to_pointer (text, priv->current_pos + priv->preedit_cursor) - text;
6052         }
6053       else /* type == CURSOR_DND */
6054         {
6055           index = g_utf8_offset_to_pointer (text, priv->dnd_position) - text;
6056
6057           if (priv->dnd_position > priv->current_pos)
6058             {
6059               if (mode == DISPLAY_NORMAL)
6060                 index += priv->preedit_length;
6061               else
6062                 {
6063                   gint preedit_len_chars = g_utf8_strlen (text, -1) - gtk_entry_buffer_get_length (get_buffer (entry));
6064                   index += preedit_len_chars * g_unichar_to_utf8 (priv->invisible_char, NULL);
6065                 }
6066             }
6067         }
6068       
6069       pango_layout_get_cursor_pos (layout, index, &strong_pos, &weak_pos);
6070       
6071       if (strong_x)
6072         *strong_x = strong_pos.x / PANGO_SCALE;
6073       
6074       if (weak_x)
6075         *weak_x = weak_pos.x / PANGO_SCALE;
6076     }
6077 }
6078
6079 static void
6080 gtk_entry_adjust_scroll (GtkEntry *entry)
6081 {
6082   GtkEntryPrivate *priv = entry->priv;
6083   gint min_offset, max_offset;
6084   gint text_area_width, text_width;
6085   GtkBorder inner_border;
6086   gint strong_x, weak_x;
6087   gint strong_xoffset, weak_xoffset;
6088   gfloat xalign;
6089   PangoLayout *layout;
6090   PangoLayoutLine *line;
6091   PangoRectangle logical_rect;
6092
6093   if (!gtk_widget_get_realized (GTK_WIDGET (entry)))
6094     return;
6095
6096   _gtk_entry_effective_inner_border (entry, &inner_border);
6097
6098   text_area_width = gdk_window_get_width (priv->text_area);
6099   text_area_width -= inner_border.left + inner_border.right;
6100   if (text_area_width < 0)
6101     text_area_width = 0;
6102
6103   layout = gtk_entry_ensure_layout (entry, TRUE);
6104   line = pango_layout_get_lines_readonly (layout)->data;
6105
6106   pango_layout_line_get_extents (line, NULL, &logical_rect);
6107
6108   /* Display as much text as we can */
6109
6110   if (priv->resolved_dir == PANGO_DIRECTION_LTR)
6111       xalign = priv->xalign;
6112   else
6113       xalign = 1.0 - priv->xalign;
6114
6115   text_width = PANGO_PIXELS(logical_rect.width);
6116
6117   if (text_width > text_area_width)
6118     {
6119       min_offset = 0;
6120       max_offset = text_width - text_area_width;
6121     }
6122   else
6123     {
6124       min_offset = (text_width - text_area_width) * xalign;
6125       max_offset = min_offset;
6126     }
6127
6128   priv->scroll_offset = CLAMP (priv->scroll_offset, min_offset, max_offset);
6129
6130   /* And make sure cursors are on screen. Note that the cursor is
6131    * actually drawn one pixel into the INNER_BORDER space on
6132    * the right, when the scroll is at the utmost right. This
6133    * looks better to to me than confining the cursor inside the
6134    * border entirely, though it means that the cursor gets one
6135    * pixel closer to the edge of the widget on the right than
6136    * on the left. This might need changing if one changed
6137    * INNER_BORDER from 2 to 1, as one would do on a
6138    * small-screen-real-estate display.
6139    *
6140    * We always make sure that the strong cursor is on screen, and
6141    * put the weak cursor on screen if possible.
6142    */
6143
6144   gtk_entry_get_cursor_locations (entry, CURSOR_STANDARD, &strong_x, &weak_x);
6145   
6146   strong_xoffset = strong_x - priv->scroll_offset;
6147
6148   if (strong_xoffset < 0)
6149     {
6150       priv->scroll_offset += strong_xoffset;
6151       strong_xoffset = 0;
6152     }
6153   else if (strong_xoffset > text_area_width)
6154     {
6155       priv->scroll_offset += strong_xoffset - text_area_width;
6156       strong_xoffset = text_area_width;
6157     }
6158
6159   weak_xoffset = weak_x - priv->scroll_offset;
6160
6161   if (weak_xoffset < 0 && strong_xoffset - weak_xoffset <= text_area_width)
6162     {
6163       priv->scroll_offset += weak_xoffset;
6164     }
6165   else if (weak_xoffset > text_area_width &&
6166            strong_xoffset - (weak_xoffset - text_area_width) >= 0)
6167     {
6168       priv->scroll_offset += weak_xoffset - text_area_width;
6169     }
6170
6171   g_object_notify (G_OBJECT (entry), "scroll-offset");
6172 }
6173
6174 static void
6175 gtk_entry_move_adjustments (GtkEntry *entry)
6176 {
6177   GtkWidget *widget = GTK_WIDGET (entry);
6178   GtkAllocation allocation;
6179   GtkAdjustment *adjustment;
6180   PangoContext *context;
6181   PangoFontMetrics *metrics;
6182   GtkStyleContext *style_context;
6183   GtkStateFlags state;
6184   GtkBorder borders;
6185   gint x, layout_x;
6186   gint char_width;
6187
6188   adjustment = g_object_get_qdata (G_OBJECT (entry), quark_cursor_hadjustment);
6189   if (!adjustment)
6190     return;
6191
6192   gtk_widget_get_allocation (widget, &allocation);
6193
6194   /* Cursor position, layout offset, border width, and widget allocation */
6195   gtk_entry_get_cursor_locations (entry, CURSOR_STANDARD, &x, NULL);
6196   get_layout_position (entry, &layout_x, NULL);
6197   _gtk_entry_get_borders (entry, &borders);
6198   x += allocation.x + layout_x + borders.left;
6199
6200   /* Approximate width of a char, so user can see what is ahead/behind */
6201   context = gtk_widget_get_pango_context (widget);
6202   style_context = gtk_widget_get_style_context (widget);
6203   state = gtk_widget_get_state_flags (widget);
6204
6205   metrics = pango_context_get_metrics (context,
6206                                        gtk_style_context_get_font (style_context, state),
6207                                        pango_context_get_language (context));
6208   char_width = pango_font_metrics_get_approximate_char_width (metrics) / PANGO_SCALE;
6209
6210   /* Scroll it */
6211   gtk_adjustment_clamp_page (adjustment, 
6212                              x - (char_width + 1),   /* one char + one pixel before */
6213                              x + (char_width + 2));  /* one char + cursor + one pixel after */
6214 }
6215
6216 static gint
6217 gtk_entry_move_visually (GtkEntry *entry,
6218                          gint      start,
6219                          gint      count)
6220 {
6221   GtkEntryPrivate *priv = entry->priv;
6222   gint index;
6223   PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
6224   const gchar *text;
6225
6226   text = pango_layout_get_text (layout);
6227   
6228   index = g_utf8_offset_to_pointer (text, start) - text;
6229
6230   while (count != 0)
6231     {
6232       int new_index, new_trailing;
6233       gboolean split_cursor;
6234       gboolean strong;
6235
6236       g_object_get (gtk_widget_get_settings (GTK_WIDGET (entry)),
6237                     "gtk-split-cursor", &split_cursor,
6238                     NULL);
6239
6240       if (split_cursor)
6241         strong = TRUE;
6242       else
6243         {
6244           GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (entry)));
6245           PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
6246
6247           strong = keymap_direction == priv->resolved_dir;
6248         }
6249       
6250       if (count > 0)
6251         {
6252           pango_layout_move_cursor_visually (layout, strong, index, 0, 1, &new_index, &new_trailing);
6253           count--;
6254         }
6255       else
6256         {
6257           pango_layout_move_cursor_visually (layout, strong, index, 0, -1, &new_index, &new_trailing);
6258           count++;
6259         }
6260
6261       if (new_index < 0)
6262         index = 0;
6263       else if (new_index != G_MAXINT)
6264         index = new_index;
6265       
6266       while (new_trailing--)
6267         index = g_utf8_next_char (text + index) - text;
6268     }
6269   
6270   return g_utf8_pointer_to_offset (text, text + index);
6271 }
6272
6273 static gint
6274 gtk_entry_move_logically (GtkEntry *entry,
6275                           gint      start,
6276                           gint      count)
6277 {
6278   gint new_pos = start;
6279   guint length;
6280
6281   length = gtk_entry_buffer_get_length (get_buffer (entry));
6282
6283   /* Prevent any leak of information */
6284   if (gtk_entry_get_display_mode (entry) != DISPLAY_NORMAL)
6285     {
6286       new_pos = CLAMP (start + count, 0, length);
6287     }
6288   else
6289     {
6290       PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
6291       PangoLogAttr *log_attrs;
6292       gint n_attrs;
6293
6294       pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
6295
6296       while (count > 0 && new_pos < length)
6297         {
6298           do
6299             new_pos++;
6300           while (new_pos < length && !log_attrs[new_pos].is_cursor_position);
6301           
6302           count--;
6303         }
6304       while (count < 0 && new_pos > 0)
6305         {
6306           do
6307             new_pos--;
6308           while (new_pos > 0 && !log_attrs[new_pos].is_cursor_position);
6309           
6310           count++;
6311         }
6312       
6313       g_free (log_attrs);
6314     }
6315
6316   return new_pos;
6317 }
6318
6319 static gint
6320 gtk_entry_move_forward_word (GtkEntry *entry,
6321                              gint      start,
6322                              gboolean  allow_whitespace)
6323 {
6324   gint new_pos = start;
6325   guint length;
6326
6327   length = gtk_entry_buffer_get_length (get_buffer (entry));
6328
6329   /* Prevent any leak of information */
6330   if (gtk_entry_get_display_mode (entry) != DISPLAY_NORMAL)
6331     {
6332       new_pos = length;
6333     }
6334   else if (new_pos < length)
6335     {
6336       PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
6337       PangoLogAttr *log_attrs;
6338       gint n_attrs;
6339
6340       pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
6341       
6342       /* Find the next word boundary */
6343       new_pos++;
6344       while (new_pos < n_attrs - 1 && !(log_attrs[new_pos].is_word_end ||
6345                                         (log_attrs[new_pos].is_word_start && allow_whitespace)))
6346         new_pos++;
6347
6348       g_free (log_attrs);
6349     }
6350
6351   return new_pos;
6352 }
6353
6354
6355 static gint
6356 gtk_entry_move_backward_word (GtkEntry *entry,
6357                               gint      start,
6358                               gboolean  allow_whitespace)
6359 {
6360   gint new_pos = start;
6361
6362   /* Prevent any leak of information */
6363   if (gtk_entry_get_display_mode (entry) != DISPLAY_NORMAL)
6364     {
6365       new_pos = 0;
6366     }
6367   else if (start > 0)
6368     {
6369       PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
6370       PangoLogAttr *log_attrs;
6371       gint n_attrs;
6372
6373       pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
6374
6375       new_pos = start - 1;
6376
6377       /* Find the previous word boundary */
6378       while (new_pos > 0 && !(log_attrs[new_pos].is_word_start || 
6379                               (log_attrs[new_pos].is_word_end && allow_whitespace)))
6380         new_pos--;
6381
6382       g_free (log_attrs);
6383     }
6384
6385   return new_pos;
6386 }
6387
6388 static void
6389 gtk_entry_delete_whitespace (GtkEntry *entry)
6390 {
6391   GtkEntryPrivate *priv = entry->priv;
6392   PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
6393   PangoLogAttr *log_attrs;
6394   gint n_attrs;
6395   gint start, end;
6396
6397   pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
6398
6399   start = end = priv->current_pos;
6400   
6401   while (start > 0 && log_attrs[start-1].is_white)
6402     start--;
6403
6404   while (end < n_attrs && log_attrs[end].is_white)
6405     end++;
6406
6407   g_free (log_attrs);
6408
6409   if (start != end)
6410     gtk_editable_delete_text (GTK_EDITABLE (entry), start, end);
6411 }
6412
6413
6414 static void
6415 gtk_entry_select_word (GtkEntry *entry)
6416 {
6417   GtkEntryPrivate *priv = entry->priv;
6418   gint start_pos = gtk_entry_move_backward_word (entry, priv->current_pos, TRUE);
6419   gint end_pos = gtk_entry_move_forward_word (entry, priv->current_pos, TRUE);
6420
6421   gtk_editable_select_region (GTK_EDITABLE (entry), start_pos, end_pos);
6422 }
6423
6424 static void
6425 gtk_entry_select_line (GtkEntry *entry)
6426 {
6427   gtk_editable_select_region (GTK_EDITABLE (entry), 0, -1);
6428 }
6429
6430 static gint
6431 truncate_multiline (const gchar *text)
6432 {
6433   gint length;
6434
6435   for (length = 0;
6436        text[length] && text[length] != '\n' && text[length] != '\r';
6437        length++);
6438
6439   return length;
6440 }
6441
6442 static void
6443 paste_received (GtkClipboard *clipboard,
6444                 const gchar  *text,
6445                 gpointer      data)
6446 {
6447   GtkEntry *entry = GTK_ENTRY (data);
6448   GtkEditable *editable = GTK_EDITABLE (entry);
6449   GtkEntryPrivate *priv = entry->priv;
6450
6451   if (priv->button == GDK_BUTTON_MIDDLE)
6452     {
6453       gint pos, start, end;
6454       pos = priv->insert_pos;
6455       gtk_editable_get_selection_bounds (editable, &start, &end);
6456       if (!((start <= pos && pos <= end) || (end <= pos && pos <= start)))
6457         gtk_editable_select_region (editable, pos, pos);
6458     }
6459       
6460   if (text)
6461     {
6462       gint pos, start, end;
6463       gint length = -1;
6464       gboolean popup_completion;
6465       GtkEntryCompletion *completion;
6466
6467       completion = gtk_entry_get_completion (entry);
6468
6469       if (priv->truncate_multiline)
6470         length = truncate_multiline (text);
6471
6472       /* only complete if the selection is at the end */
6473       popup_completion = (gtk_entry_buffer_get_length (get_buffer (entry)) ==
6474                           MAX (priv->current_pos, priv->selection_bound));
6475
6476       if (completion)
6477         {
6478           if (gtk_widget_get_mapped (completion->priv->popup_window))
6479             _gtk_entry_completion_popdown (completion);
6480
6481           if (!popup_completion && completion->priv->changed_id > 0)
6482             g_signal_handler_block (entry, completion->priv->changed_id);
6483         }
6484
6485       begin_change (entry);
6486       if (gtk_editable_get_selection_bounds (editable, &start, &end))
6487         gtk_editable_delete_text (editable, start, end);
6488
6489       pos = priv->current_pos;
6490       gtk_editable_insert_text (editable, text, length, &pos);
6491       gtk_editable_set_position (editable, pos);
6492       end_change (entry);
6493
6494       if (completion &&
6495           !popup_completion && completion->priv->changed_id > 0)
6496         g_signal_handler_unblock (entry, completion->priv->changed_id);
6497     }
6498
6499   g_object_unref (entry);
6500 }
6501
6502 static void
6503 gtk_entry_paste (GtkEntry *entry,
6504                  GdkAtom   selection)
6505 {
6506   g_object_ref (entry);
6507   gtk_clipboard_request_text (gtk_widget_get_clipboard (GTK_WIDGET (entry), selection),
6508                               paste_received, entry);
6509 }
6510
6511 static void
6512 primary_get_cb (GtkClipboard     *clipboard,
6513                 GtkSelectionData *selection_data,
6514                 guint             info,
6515                 gpointer          data)
6516 {
6517   GtkEntry *entry = GTK_ENTRY (data);
6518   gint start, end;
6519   
6520   if (gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &start, &end))
6521     {
6522       gchar *str = gtk_entry_get_display_text (entry, start, end);
6523       gtk_selection_data_set_text (selection_data, str, -1);
6524       g_free (str);
6525     }
6526 }
6527
6528 static void
6529 primary_clear_cb (GtkClipboard *clipboard,
6530                   gpointer      data)
6531 {
6532   GtkEntry *entry = GTK_ENTRY (data);
6533   GtkEntryPrivate *priv = entry->priv;
6534
6535   gtk_editable_select_region (GTK_EDITABLE (entry), priv->current_pos, priv->current_pos);
6536 }
6537
6538 static void
6539 gtk_entry_update_primary_selection (GtkEntry *entry)
6540 {
6541   GtkTargetList *list;
6542   GtkTargetEntry *targets;
6543   GtkClipboard *clipboard;
6544   gint start, end;
6545   gint n_targets;
6546
6547   if (!gtk_widget_get_realized (GTK_WIDGET (entry)))
6548     return;
6549
6550   list = gtk_target_list_new (NULL, 0);
6551   gtk_target_list_add_text_targets (list, 0);
6552
6553   targets = gtk_target_table_new_from_list (list, &n_targets);
6554
6555   clipboard = gtk_widget_get_clipboard (GTK_WIDGET (entry), GDK_SELECTION_PRIMARY);
6556   
6557   if (gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &start, &end))
6558     {
6559       if (!gtk_clipboard_set_with_owner (clipboard, targets, n_targets,
6560                                          primary_get_cb, primary_clear_cb, G_OBJECT (entry)))
6561         primary_clear_cb (clipboard, entry);
6562     }
6563   else
6564     {
6565       if (gtk_clipboard_get_owner (clipboard) == G_OBJECT (entry))
6566         gtk_clipboard_clear (clipboard);
6567     }
6568
6569   gtk_target_table_free (targets, n_targets);
6570   gtk_target_list_unref (list);
6571 }
6572
6573 static void
6574 gtk_entry_clear (GtkEntry             *entry,
6575                  GtkEntryIconPosition  icon_pos)
6576 {
6577   GtkEntryPrivate *priv = entry->priv;
6578   EntryIconInfo *icon_info = priv->icons[icon_pos];
6579   GtkImageType storage_type;
6580
6581   if (icon_info && _gtk_icon_helper_get_is_empty (icon_info->icon_helper))
6582     return;
6583
6584   g_object_freeze_notify (G_OBJECT (entry));
6585
6586   /* Explicitly check, as the pointer may become invalidated
6587    * during destruction.
6588    */
6589   if (GDK_IS_WINDOW (icon_info->window))
6590     gdk_window_hide (icon_info->window);
6591
6592   storage_type = _gtk_icon_helper_get_storage_type (icon_info->icon_helper);
6593
6594   switch (storage_type)
6595     {
6596     case GTK_IMAGE_PIXBUF:
6597       g_object_notify (G_OBJECT (entry),
6598                        icon_pos == GTK_ENTRY_ICON_PRIMARY ? "primary-icon-pixbuf" : "secondary-icon-pixbuf");
6599       break;
6600
6601     case GTK_IMAGE_STOCK:
6602       g_object_notify (G_OBJECT (entry),
6603                        icon_pos == GTK_ENTRY_ICON_PRIMARY ? "primary-icon-stock" : "secondary-icon-stock");
6604       break;
6605
6606     case GTK_IMAGE_ICON_NAME:
6607       g_object_notify (G_OBJECT (entry),
6608                        icon_pos == GTK_ENTRY_ICON_PRIMARY ? "primary-icon-name" : "secondary-icon-name");
6609       break;
6610
6611     case GTK_IMAGE_GICON:
6612       g_object_notify (G_OBJECT (entry),
6613                        icon_pos == GTK_ENTRY_ICON_PRIMARY ? "primary-icon-gicon" : "secondary-icon-gicon");
6614       break;
6615
6616     default:
6617       g_assert_not_reached ();
6618       break;
6619     }
6620
6621   _gtk_icon_helper_clear (icon_info->icon_helper);
6622
6623   g_object_notify (G_OBJECT (entry),
6624                    icon_pos == GTK_ENTRY_ICON_PRIMARY ? "primary-icon-storage-type" : "secondary-icon-storage-type");
6625
6626   g_object_thaw_notify (G_OBJECT (entry));
6627 }
6628
6629 static GdkPixbuf *
6630 gtk_entry_ensure_pixbuf (GtkEntry             *entry,
6631                          GtkEntryIconPosition  icon_pos)
6632 {
6633   GtkEntryPrivate *priv = entry->priv;
6634   EntryIconInfo *icon_info = priv->icons[icon_pos];
6635   GtkStyleContext *context;
6636   GdkPixbuf *pix;
6637
6638   context = gtk_widget_get_style_context (GTK_WIDGET (entry));
6639   gtk_entry_prepare_context_for_icon (entry, context, icon_pos);
6640
6641   pix = _gtk_icon_helper_ensure_pixbuf (icon_info->icon_helper,
6642                                         context);
6643
6644   gtk_style_context_restore (context);
6645
6646   return pix;
6647 }
6648
6649 /* Public API
6650  */
6651
6652 /**
6653  * gtk_entry_new:
6654  *
6655  * Creates a new entry.
6656  *
6657  * Return value: a new #GtkEntry.
6658  */
6659 GtkWidget*
6660 gtk_entry_new (void)
6661 {
6662   return g_object_new (GTK_TYPE_ENTRY, NULL);
6663 }
6664
6665 /**
6666  * gtk_entry_new_with_buffer:
6667  * @buffer: The buffer to use for the new #GtkEntry.
6668  *
6669  * Creates a new entry with the specified text buffer.
6670  *
6671  * Return value: a new #GtkEntry
6672  *
6673  * Since: 2.18
6674  */
6675 GtkWidget*
6676 gtk_entry_new_with_buffer (GtkEntryBuffer *buffer)
6677 {
6678   g_return_val_if_fail (GTK_IS_ENTRY_BUFFER (buffer), NULL);
6679   return g_object_new (GTK_TYPE_ENTRY, "buffer", buffer, NULL);
6680 }
6681
6682 static GtkEntryBuffer*
6683 get_buffer (GtkEntry *entry)
6684 {
6685   GtkEntryPrivate *priv = entry->priv;
6686
6687   if (priv->buffer == NULL)
6688     {
6689       GtkEntryBuffer *buffer;
6690       buffer = gtk_entry_buffer_new (NULL, 0);
6691       gtk_entry_set_buffer (entry, buffer);
6692       g_object_unref (buffer);
6693     }
6694
6695   return priv->buffer;
6696 }
6697
6698 /**
6699  * gtk_entry_get_buffer:
6700  * @entry: a #GtkEntry
6701  *
6702  * Get the #GtkEntryBuffer object which holds the text for
6703  * this widget.
6704  *
6705  * Since: 2.18
6706  *
6707  * Returns: (transfer none): A #GtkEntryBuffer object.
6708  */
6709 GtkEntryBuffer*
6710 gtk_entry_get_buffer (GtkEntry *entry)
6711 {
6712   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
6713
6714   return get_buffer (entry);
6715 }
6716
6717 /**
6718  * gtk_entry_set_buffer:
6719  * @entry: a #GtkEntry
6720  * @buffer: a #GtkEntryBuffer
6721  *
6722  * Set the #GtkEntryBuffer object which holds the text for
6723  * this widget.
6724  *
6725  * Since: 2.18
6726  */
6727 void
6728 gtk_entry_set_buffer (GtkEntry       *entry,
6729                       GtkEntryBuffer *buffer)
6730 {
6731   GtkEntryPrivate *priv;
6732   GObject *obj;
6733
6734   g_return_if_fail (GTK_IS_ENTRY (entry));
6735
6736   priv = entry->priv;
6737
6738   if (buffer)
6739     {
6740       g_return_if_fail (GTK_IS_ENTRY_BUFFER (buffer));
6741       g_object_ref (buffer);
6742     }
6743
6744   if (priv->buffer)
6745     {
6746       buffer_disconnect_signals (entry);
6747       g_object_unref (priv->buffer);
6748     }
6749
6750   priv->buffer = buffer;
6751
6752   if (priv->buffer)
6753      buffer_connect_signals (entry);
6754
6755   obj = G_OBJECT (entry);
6756   g_object_freeze_notify (obj);
6757   g_object_notify (obj, "buffer");
6758   g_object_notify (obj, "text");
6759   g_object_notify (obj, "text-length");
6760   g_object_notify (obj, "max-length");
6761   g_object_notify (obj, "visibility");
6762   g_object_notify (obj, "invisible-char");
6763   g_object_notify (obj, "invisible-char-set");
6764   g_object_thaw_notify (obj);
6765
6766   gtk_editable_set_position (GTK_EDITABLE (entry), 0);
6767   gtk_entry_recompute (entry);
6768 }
6769
6770 /**
6771  * gtk_entry_get_text_area:
6772  * @entry: a #GtkEntry
6773  * @text_area: (out): Return location for the text area.
6774  *
6775  * Gets the area where the entry's text is drawn. This function is
6776  * useful when drawing something to the entry in a draw callback.
6777  *
6778  * If the entry is not realized, @text_area is filled with zeros.
6779  *
6780  * See also gtk_entry_get_icon_area().
6781  *
6782  * Since: 3.0
6783  **/
6784 void
6785 gtk_entry_get_text_area (GtkEntry     *entry,
6786                          GdkRectangle *text_area)
6787 {
6788   GtkEntryPrivate *priv;
6789
6790   g_return_if_fail (GTK_IS_ENTRY (entry));
6791   g_return_if_fail (text_area != NULL);
6792
6793   priv = entry->priv;
6794
6795   if (priv->text_area)
6796     {
6797       GtkAllocation allocation;
6798       gint x, y;
6799
6800       gtk_widget_get_allocation (GTK_WIDGET (entry), &allocation);
6801       gdk_window_get_position (priv->text_area, &x, &y);
6802
6803       text_area->x = x - allocation.x;
6804       text_area->y = y - allocation.y;
6805       text_area->width = gdk_window_get_width (priv->text_area);
6806       text_area->height = gdk_window_get_height (priv->text_area);
6807     }
6808   else
6809     {
6810       text_area->x = 0;
6811       text_area->y = 0;
6812       text_area->width = 0;
6813       text_area->height = 0;
6814     }
6815 }
6816
6817 /**
6818  * gtk_entry_set_text:
6819  * @entry: a #GtkEntry
6820  * @text: the new text
6821  *
6822  * Sets the text in the widget to the given
6823  * value, replacing the current contents.
6824  *
6825  * See gtk_entry_buffer_set_text().
6826  */
6827 void
6828 gtk_entry_set_text (GtkEntry    *entry,
6829                     const gchar *text)
6830 {
6831   gint tmp_pos;
6832   GtkEntryCompletion *completion;
6833
6834   g_return_if_fail (GTK_IS_ENTRY (entry));
6835   g_return_if_fail (text != NULL);
6836
6837   /* Actually setting the text will affect the cursor and selection;
6838    * if the contents don't actually change, this will look odd to the user.
6839    */
6840   if (strcmp (gtk_entry_buffer_get_text (get_buffer (entry)), text) == 0)
6841     return;
6842
6843   completion = gtk_entry_get_completion (entry);
6844   if (completion && completion->priv->changed_id > 0)
6845     g_signal_handler_block (entry, completion->priv->changed_id);
6846
6847   begin_change (entry);
6848   gtk_editable_delete_text (GTK_EDITABLE (entry), 0, -1);
6849   tmp_pos = 0;
6850   gtk_editable_insert_text (GTK_EDITABLE (entry), text, strlen (text), &tmp_pos);
6851   end_change (entry);
6852
6853   if (completion && completion->priv->changed_id > 0)
6854     g_signal_handler_unblock (entry, completion->priv->changed_id);
6855 }
6856
6857 /**
6858  * gtk_entry_set_visibility:
6859  * @entry: a #GtkEntry
6860  * @visible: %TRUE if the contents of the entry are displayed
6861  *           as plaintext
6862  *
6863  * Sets whether the contents of the entry are visible or not. 
6864  * When visibility is set to %FALSE, characters are displayed 
6865  * as the invisible char, and will also appear that way when 
6866  * the text in the entry widget is copied elsewhere.
6867  *
6868  * By default, GTK+ picks the best invisible character available
6869  * in the current font, but it can be changed with
6870  * gtk_entry_set_invisible_char().
6871  */
6872 void
6873 gtk_entry_set_visibility (GtkEntry *entry,
6874                           gboolean visible)
6875 {
6876   GtkEntryPrivate *priv;
6877
6878   g_return_if_fail (GTK_IS_ENTRY (entry));
6879
6880   priv = entry->priv;
6881
6882   visible = visible != FALSE;
6883
6884   if (priv->visible != visible)
6885     {
6886       priv->visible = visible;
6887
6888       g_object_notify (G_OBJECT (entry), "visibility");
6889       gtk_entry_recompute (entry);
6890     }
6891 }
6892
6893 /**
6894  * gtk_entry_get_visibility:
6895  * @entry: a #GtkEntry
6896  *
6897  * Retrieves whether the text in @entry is visible. See
6898  * gtk_entry_set_visibility().
6899  *
6900  * Return value: %TRUE if the text is currently visible
6901  **/
6902 gboolean
6903 gtk_entry_get_visibility (GtkEntry *entry)
6904 {
6905   g_return_val_if_fail (GTK_IS_ENTRY (entry), FALSE);
6906
6907   return entry->priv->visible;
6908 }
6909
6910 /**
6911  * gtk_entry_set_invisible_char:
6912  * @entry: a #GtkEntry
6913  * @ch: a Unicode character
6914  * 
6915  * Sets the character to use in place of the actual text when
6916  * gtk_entry_set_visibility() has been called to set text visibility
6917  * to %FALSE. i.e. this is the character used in "password mode" to
6918  * show the user how many characters have been typed. By default, GTK+
6919  * picks the best invisible char available in the current font. If you
6920  * set the invisible char to 0, then the user will get no feedback
6921  * at all; there will be no text on the screen as they type.
6922  **/
6923 void
6924 gtk_entry_set_invisible_char (GtkEntry *entry,
6925                               gunichar  ch)
6926 {
6927   GtkEntryPrivate *priv;
6928
6929   g_return_if_fail (GTK_IS_ENTRY (entry));
6930
6931   priv = entry->priv;
6932
6933   if (!priv->invisible_char_set)
6934     {
6935       priv->invisible_char_set = TRUE;
6936       g_object_notify (G_OBJECT (entry), "invisible-char-set");
6937     }
6938
6939   if (ch == priv->invisible_char)
6940     return;
6941
6942   priv->invisible_char = ch;
6943   g_object_notify (G_OBJECT (entry), "invisible-char");
6944   gtk_entry_recompute (entry);  
6945 }
6946
6947 /**
6948  * gtk_entry_get_invisible_char:
6949  * @entry: a #GtkEntry
6950  *
6951  * Retrieves the character displayed in place of the real characters
6952  * for entries with visibility set to false. See gtk_entry_set_invisible_char().
6953  *
6954  * Return value: the current invisible char, or 0, if the entry does not
6955  *               show invisible text at all. 
6956  **/
6957 gunichar
6958 gtk_entry_get_invisible_char (GtkEntry *entry)
6959 {
6960   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
6961
6962   return entry->priv->invisible_char;
6963 }
6964
6965 /**
6966  * gtk_entry_unset_invisible_char:
6967  * @entry: a #GtkEntry
6968  *
6969  * Unsets the invisible char previously set with
6970  * gtk_entry_set_invisible_char(). So that the
6971  * default invisible char is used again.
6972  *
6973  * Since: 2.16
6974  **/
6975 void
6976 gtk_entry_unset_invisible_char (GtkEntry *entry)
6977 {
6978   GtkEntryPrivate *priv;
6979   gunichar ch;
6980
6981   g_return_if_fail (GTK_IS_ENTRY (entry));
6982
6983   priv = entry->priv;
6984
6985   if (!priv->invisible_char_set)
6986     return;
6987
6988   priv->invisible_char_set = FALSE;
6989   ch = find_invisible_char (GTK_WIDGET (entry));
6990
6991   if (priv->invisible_char != ch)
6992     {
6993       priv->invisible_char = ch;
6994       g_object_notify (G_OBJECT (entry), "invisible-char");
6995     }
6996
6997   g_object_notify (G_OBJECT (entry), "invisible-char-set");
6998   gtk_entry_recompute (entry);
6999 }
7000
7001 /**
7002  * gtk_entry_set_overwrite_mode:
7003  * @entry: a #GtkEntry
7004  * @overwrite: new value
7005  * 
7006  * Sets whether the text is overwritten when typing in the #GtkEntry.
7007  *
7008  * Since: 2.14
7009  **/
7010 void
7011 gtk_entry_set_overwrite_mode (GtkEntry *entry,
7012                               gboolean  overwrite)
7013 {
7014   GtkEntryPrivate *priv = entry->priv;
7015
7016   g_return_if_fail (GTK_IS_ENTRY (entry));
7017
7018   if (priv->overwrite_mode == overwrite)
7019     return;
7020   
7021   gtk_entry_toggle_overwrite (entry);
7022
7023   g_object_notify (G_OBJECT (entry), "overwrite-mode");
7024 }
7025
7026 /**
7027  * gtk_entry_get_overwrite_mode:
7028  * @entry: a #GtkEntry
7029  * 
7030  * Gets the value set by gtk_entry_set_overwrite_mode().
7031  * 
7032  * Return value: whether the text is overwritten when typing.
7033  *
7034  * Since: 2.14
7035  **/
7036 gboolean
7037 gtk_entry_get_overwrite_mode (GtkEntry *entry)
7038 {
7039   g_return_val_if_fail (GTK_IS_ENTRY (entry), FALSE);
7040
7041   return entry->priv->overwrite_mode;
7042 }
7043
7044 /**
7045  * gtk_entry_get_text:
7046  * @entry: a #GtkEntry
7047  *
7048  * Retrieves the contents of the entry widget.
7049  * See also gtk_editable_get_chars().
7050  *
7051  * This is equivalent to:
7052  *
7053  * <informalexample><programlisting>
7054  * gtk_entry_buffer_get_text (gtk_entry_get_buffer (entry));
7055  * </programlisting></informalexample>
7056  *
7057  * Return value: a pointer to the contents of the widget as a
7058  *      string. This string points to internally allocated
7059  *      storage in the widget and must not be freed, modified or
7060  *      stored.
7061  **/
7062 const gchar*
7063 gtk_entry_get_text (GtkEntry *entry)
7064 {
7065   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
7066
7067   return gtk_entry_buffer_get_text (get_buffer (entry));
7068 }
7069
7070 /**
7071  * gtk_entry_set_max_length:
7072  * @entry: a #GtkEntry
7073  * @max: the maximum length of the entry, or 0 for no maximum.
7074  *   (other than the maximum length of entries.) The value passed in will
7075  *   be clamped to the range 0-65536.
7076  * 
7077  * Sets the maximum allowed length of the contents of the widget. If
7078  * the current contents are longer than the given length, then they
7079  * will be truncated to fit.
7080  *
7081  * This is equivalent to:
7082  *
7083  * <informalexample><programlisting>
7084  * gtk_entry_buffer_set_max_length (gtk_entry_get_buffer (entry), max);
7085  * </programlisting></informalexample>
7086  **/
7087 void
7088 gtk_entry_set_max_length (GtkEntry     *entry,
7089                           gint          max)
7090 {
7091   g_return_if_fail (GTK_IS_ENTRY (entry));
7092   gtk_entry_buffer_set_max_length (get_buffer (entry), max);
7093 }
7094
7095 /**
7096  * gtk_entry_get_max_length:
7097  * @entry: a #GtkEntry
7098  *
7099  * Retrieves the maximum allowed length of the text in
7100  * @entry. See gtk_entry_set_max_length().
7101  *
7102  * This is equivalent to:
7103  *
7104  * <informalexample><programlisting>
7105  * gtk_entry_buffer_get_max_length (gtk_entry_get_buffer (entry));
7106  * </programlisting></informalexample>
7107  *
7108  * Return value: the maximum allowed number of characters
7109  *               in #GtkEntry, or 0 if there is no maximum.
7110  **/
7111 gint
7112 gtk_entry_get_max_length (GtkEntry *entry)
7113 {
7114   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
7115
7116   return gtk_entry_buffer_get_max_length (get_buffer (entry));
7117 }
7118
7119 /**
7120  * gtk_entry_get_text_length:
7121  * @entry: a #GtkEntry
7122  *
7123  * Retrieves the current length of the text in
7124  * @entry. 
7125  *
7126  * This is equivalent to:
7127  *
7128  * <informalexample><programlisting>
7129  * gtk_entry_buffer_get_length (gtk_entry_get_buffer (entry));
7130  * </programlisting></informalexample>
7131  *
7132  * Return value: the current number of characters
7133  *               in #GtkEntry, or 0 if there are none.
7134  *
7135  * Since: 2.14
7136  **/
7137 guint16
7138 gtk_entry_get_text_length (GtkEntry *entry)
7139 {
7140   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
7141
7142   return gtk_entry_buffer_get_length (get_buffer (entry));
7143 }
7144
7145 /**
7146  * gtk_entry_set_activates_default:
7147  * @entry: a #GtkEntry
7148  * @setting: %TRUE to activate window's default widget on Enter keypress
7149  *
7150  * If @setting is %TRUE, pressing Enter in the @entry will activate the default
7151  * widget for the window containing the entry. This usually means that
7152  * the dialog box containing the entry will be closed, since the default
7153  * widget is usually one of the dialog buttons.
7154  *
7155  * (For experts: if @setting is %TRUE, the entry calls
7156  * gtk_window_activate_default() on the window containing the entry, in
7157  * the default handler for the #GtkWidget::activate signal.)
7158  **/
7159 void
7160 gtk_entry_set_activates_default (GtkEntry *entry,
7161                                  gboolean  setting)
7162 {
7163   GtkEntryPrivate *priv;
7164
7165   g_return_if_fail (GTK_IS_ENTRY (entry));
7166
7167   priv = entry->priv;
7168
7169   setting = setting != FALSE;
7170
7171   if (setting != priv->activates_default)
7172     {
7173       priv->activates_default = setting;
7174       g_object_notify (G_OBJECT (entry), "activates-default");
7175     }
7176 }
7177
7178 /**
7179  * gtk_entry_get_activates_default:
7180  * @entry: a #GtkEntry
7181  * 
7182  * Retrieves the value set by gtk_entry_set_activates_default().
7183  * 
7184  * Return value: %TRUE if the entry will activate the default widget
7185  **/
7186 gboolean
7187 gtk_entry_get_activates_default (GtkEntry *entry)
7188 {
7189   g_return_val_if_fail (GTK_IS_ENTRY (entry), FALSE);
7190
7191   return entry->priv->activates_default;
7192 }
7193
7194 /**
7195  * gtk_entry_set_width_chars:
7196  * @entry: a #GtkEntry
7197  * @n_chars: width in chars
7198  *
7199  * Changes the size request of the entry to be about the right size
7200  * for @n_chars characters. Note that it changes the size
7201  * <emphasis>request</emphasis>, the size can still be affected by
7202  * how you pack the widget into containers. If @n_chars is -1, the
7203  * size reverts to the default entry size.
7204  **/
7205 void
7206 gtk_entry_set_width_chars (GtkEntry *entry,
7207                            gint      n_chars)
7208 {
7209   GtkEntryPrivate *priv;
7210
7211   g_return_if_fail (GTK_IS_ENTRY (entry));
7212
7213   priv = entry->priv;
7214
7215   if (priv->width_chars != n_chars)
7216     {
7217       priv->width_chars = n_chars;
7218       g_object_notify (G_OBJECT (entry), "width-chars");
7219       gtk_widget_queue_resize (GTK_WIDGET (entry));
7220     }
7221 }
7222
7223 /**
7224  * gtk_entry_get_width_chars:
7225  * @entry: a #GtkEntry
7226  * 
7227  * Gets the value set by gtk_entry_set_width_chars().
7228  * 
7229  * Return value: number of chars to request space for, or negative if unset
7230  **/
7231 gint
7232 gtk_entry_get_width_chars (GtkEntry *entry)
7233 {
7234   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
7235
7236   return entry->priv->width_chars;
7237 }
7238
7239 /**
7240  * gtk_entry_set_has_frame:
7241  * @entry: a #GtkEntry
7242  * @setting: new value
7243  * 
7244  * Sets whether the entry has a beveled frame around it.
7245  **/
7246 void
7247 gtk_entry_set_has_frame (GtkEntry *entry,
7248                          gboolean  setting)
7249 {
7250   GtkEntryPrivate *priv;
7251
7252   g_return_if_fail (GTK_IS_ENTRY (entry));
7253
7254   priv = entry->priv;
7255
7256   setting = (setting != FALSE);
7257
7258   if (priv->has_frame == setting)
7259     return;
7260
7261   gtk_widget_queue_resize (GTK_WIDGET (entry));
7262   priv->has_frame = setting;
7263   g_object_notify (G_OBJECT (entry), "has-frame");
7264 }
7265
7266 /**
7267  * gtk_entry_get_has_frame:
7268  * @entry: a #GtkEntry
7269  * 
7270  * Gets the value set by gtk_entry_set_has_frame().
7271  * 
7272  * Return value: whether the entry has a beveled frame
7273  **/
7274 gboolean
7275 gtk_entry_get_has_frame (GtkEntry *entry)
7276 {
7277   g_return_val_if_fail (GTK_IS_ENTRY (entry), FALSE);
7278
7279   return entry->priv->has_frame;
7280 }
7281
7282 /**
7283  * gtk_entry_set_inner_border:
7284  * @entry: a #GtkEntry
7285  * @border: (allow-none): a #GtkBorder, or %NULL
7286  *
7287  * Sets %entry's inner-border property to %border, or clears it if %NULL
7288  * is passed. The inner-border is the area around the entry's text, but
7289  * inside its frame.
7290  *
7291  * If set, this property overrides the inner-border style property.
7292  * Overriding the style-provided border is useful when you want to do
7293  * in-place editing of some text in a canvas or list widget, where
7294  * pixel-exact positioning of the entry is important.
7295  *
7296  * Since: 2.10
7297  **/
7298 void
7299 gtk_entry_set_inner_border (GtkEntry        *entry,
7300                             const GtkBorder *border)
7301 {
7302   g_return_if_fail (GTK_IS_ENTRY (entry));
7303
7304   gtk_widget_queue_resize (GTK_WIDGET (entry));
7305
7306   if (border)
7307     g_object_set_qdata_full (G_OBJECT (entry), quark_inner_border,
7308                              gtk_border_copy (border),
7309                              (GDestroyNotify) gtk_border_free);
7310   else
7311     g_object_set_qdata (G_OBJECT (entry), quark_inner_border, NULL);
7312
7313   g_object_notify (G_OBJECT (entry), "inner-border");
7314 }
7315
7316 /**
7317  * gtk_entry_get_inner_border:
7318  * @entry: a #GtkEntry
7319  *
7320  * This function returns the entry's #GtkEntry:inner-border property. See
7321  * gtk_entry_set_inner_border() for more information.
7322  *
7323  * Return value: (transfer none): the entry's #GtkBorder, or %NULL if none was set.
7324  *
7325  * Since: 2.10
7326  **/
7327 const GtkBorder *
7328 gtk_entry_get_inner_border (GtkEntry *entry)
7329 {
7330   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
7331
7332   return g_object_get_qdata (G_OBJECT (entry), quark_inner_border);
7333 }
7334
7335 /**
7336  * gtk_entry_get_layout:
7337  * @entry: a #GtkEntry
7338  * 
7339  * Gets the #PangoLayout used to display the entry.
7340  * The layout is useful to e.g. convert text positions to
7341  * pixel positions, in combination with gtk_entry_get_layout_offsets().
7342  * The returned layout is owned by the entry and must not be 
7343  * modified or freed by the caller.
7344  *
7345  * Keep in mind that the layout text may contain a preedit string, so
7346  * gtk_entry_layout_index_to_text_index() and
7347  * gtk_entry_text_index_to_layout_index() are needed to convert byte
7348  * indices in the layout to byte indices in the entry contents.
7349  *
7350  * Return value: (transfer none): the #PangoLayout for this entry
7351  **/
7352 PangoLayout*
7353 gtk_entry_get_layout (GtkEntry *entry)
7354 {
7355   PangoLayout *layout;
7356   
7357   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
7358
7359   layout = gtk_entry_ensure_layout (entry, TRUE);
7360
7361   return layout;
7362 }
7363
7364
7365 /**
7366  * gtk_entry_layout_index_to_text_index:
7367  * @entry: a #GtkEntry
7368  * @layout_index: byte index into the entry layout text
7369  * 
7370  * Converts from a position in the entry contents (returned
7371  * by gtk_entry_get_text()) to a position in the
7372  * entry's #PangoLayout (returned by gtk_entry_get_layout(),
7373  * with text retrieved via pango_layout_get_text()).
7374  * 
7375  * Return value: byte index into the entry contents
7376  **/
7377 gint
7378 gtk_entry_layout_index_to_text_index (GtkEntry *entry,
7379                                       gint      layout_index)
7380 {
7381   GtkEntryPrivate *priv;
7382   PangoLayout *layout;
7383   const gchar *text;
7384   gint cursor_index;
7385   
7386   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
7387
7388   priv = entry->priv;
7389
7390   layout = gtk_entry_ensure_layout (entry, TRUE);
7391   text = pango_layout_get_text (layout);
7392   cursor_index = g_utf8_offset_to_pointer (text, priv->current_pos) - text;
7393
7394   if (layout_index >= cursor_index && priv->preedit_length)
7395     {
7396       if (layout_index >= cursor_index + priv->preedit_length)
7397         layout_index -= priv->preedit_length;
7398       else
7399         layout_index = cursor_index;
7400     }
7401
7402   return layout_index;
7403 }
7404
7405 /**
7406  * gtk_entry_text_index_to_layout_index:
7407  * @entry: a #GtkEntry
7408  * @text_index: byte index into the entry contents
7409  * 
7410  * Converts from a position in the entry's #PangoLayout (returned by
7411  * gtk_entry_get_layout()) to a position in the entry contents
7412  * (returned by gtk_entry_get_text()).
7413  * 
7414  * Return value: byte index into the entry layout text
7415  **/
7416 gint
7417 gtk_entry_text_index_to_layout_index (GtkEntry *entry,
7418                                       gint      text_index)
7419 {
7420   GtkEntryPrivate *priv;
7421   PangoLayout *layout;
7422   const gchar *text;
7423   gint cursor_index;
7424
7425   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
7426
7427   priv = entry->priv;
7428
7429   layout = gtk_entry_ensure_layout (entry, TRUE);
7430   text = pango_layout_get_text (layout);
7431   cursor_index = g_utf8_offset_to_pointer (text, priv->current_pos) - text;
7432
7433   if (text_index > cursor_index)
7434     text_index += priv->preedit_length;
7435
7436   return text_index;
7437 }
7438
7439 /**
7440  * gtk_entry_get_layout_offsets:
7441  * @entry: a #GtkEntry
7442  * @x: (out) (allow-none): location to store X offset of layout, or %NULL
7443  * @y: (out) (allow-none): location to store Y offset of layout, or %NULL
7444  *
7445  *
7446  * Obtains the position of the #PangoLayout used to render text
7447  * in the entry, in widget coordinates. Useful if you want to line
7448  * up the text in an entry with some other text, e.g. when using the
7449  * entry to implement editable cells in a sheet widget.
7450  *
7451  * Also useful to convert mouse events into coordinates inside the
7452  * #PangoLayout, e.g. to take some action if some part of the entry text
7453  * is clicked.
7454  * 
7455  * Note that as the user scrolls around in the entry the offsets will
7456  * change; you'll need to connect to the "notify::scroll-offset"
7457  * signal to track this. Remember when using the #PangoLayout
7458  * functions you need to convert to and from pixels using
7459  * PANGO_PIXELS() or #PANGO_SCALE.
7460  *
7461  * Keep in mind that the layout text may contain a preedit string, so
7462  * gtk_entry_layout_index_to_text_index() and
7463  * gtk_entry_text_index_to_layout_index() are needed to convert byte
7464  * indices in the layout to byte indices in the entry contents.
7465  **/
7466 void
7467 gtk_entry_get_layout_offsets (GtkEntry *entry,
7468                               gint     *x,
7469                               gint     *y)
7470 {
7471   gint text_area_x, text_area_y;
7472   
7473   g_return_if_fail (GTK_IS_ENTRY (entry));
7474
7475   /* this gets coords relative to text area */
7476   get_layout_position (entry, x, y);
7477
7478   /* convert to widget coords */
7479   gtk_entry_get_text_area_size (entry, &text_area_x, &text_area_y, NULL, NULL);
7480   
7481   if (x)
7482     *x += text_area_x;
7483
7484   if (y)
7485     *y += text_area_y;
7486 }
7487
7488
7489 /**
7490  * gtk_entry_set_alignment:
7491  * @entry: a #GtkEntry
7492  * @xalign: The horizontal alignment, from 0 (left) to 1 (right).
7493  *          Reversed for RTL layouts
7494  * 
7495  * Sets the alignment for the contents of the entry. This controls
7496  * the horizontal positioning of the contents when the displayed
7497  * text is shorter than the width of the entry.
7498  *
7499  * Since: 2.4
7500  **/
7501 void
7502 gtk_entry_set_alignment (GtkEntry *entry, gfloat xalign)
7503 {
7504   GtkEntryPrivate *priv;
7505
7506   g_return_if_fail (GTK_IS_ENTRY (entry));
7507
7508   priv = entry->priv;
7509
7510   if (xalign < 0.0)
7511     xalign = 0.0;
7512   else if (xalign > 1.0)
7513     xalign = 1.0;
7514
7515   if (xalign != priv->xalign)
7516     {
7517       priv->xalign = xalign;
7518
7519       gtk_entry_recompute (entry);
7520
7521       g_object_notify (G_OBJECT (entry), "xalign");
7522     }
7523 }
7524
7525 /**
7526  * gtk_entry_get_alignment:
7527  * @entry: a #GtkEntry
7528  * 
7529  * Gets the value set by gtk_entry_set_alignment().
7530  * 
7531  * Return value: the alignment
7532  *
7533  * Since: 2.4
7534  **/
7535 gfloat
7536 gtk_entry_get_alignment (GtkEntry *entry)
7537 {
7538   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0.0);
7539
7540   return entry->priv->xalign;
7541 }
7542
7543 /**
7544  * gtk_entry_set_icon_from_pixbuf:
7545  * @entry: a #GtkEntry
7546  * @icon_pos: Icon position
7547  * @pixbuf: (allow-none): A #GdkPixbuf, or %NULL
7548  *
7549  * Sets the icon shown in the specified position using a pixbuf.
7550  *
7551  * If @pixbuf is %NULL, no icon will be shown in the specified position.
7552  *
7553  * Since: 2.16
7554  */
7555 void
7556 gtk_entry_set_icon_from_pixbuf (GtkEntry             *entry,
7557                                 GtkEntryIconPosition  icon_pos,
7558                                 GdkPixbuf            *pixbuf)
7559 {
7560   GtkEntryPrivate *priv;
7561   EntryIconInfo *icon_info;
7562
7563   g_return_if_fail (GTK_IS_ENTRY (entry));
7564   g_return_if_fail (IS_VALID_ICON_POSITION (icon_pos));
7565
7566   priv = entry->priv;
7567
7568   if ((icon_info = priv->icons[icon_pos]) == NULL)
7569     icon_info = construct_icon_info (GTK_WIDGET (entry), icon_pos);
7570
7571   g_object_freeze_notify (G_OBJECT (entry));
7572
7573   if (pixbuf)
7574     g_object_ref (pixbuf);
7575
7576   gtk_entry_clear (entry, icon_pos);
7577
7578   if (pixbuf)
7579     {
7580       _gtk_icon_helper_set_pixbuf (icon_info->icon_helper, pixbuf);
7581
7582       if (icon_pos == GTK_ENTRY_ICON_PRIMARY)
7583         {
7584           g_object_notify (G_OBJECT (entry), "primary-icon-pixbuf");
7585           g_object_notify (G_OBJECT (entry), "primary-icon-storage-type");
7586         }
7587       else
7588         {
7589           g_object_notify (G_OBJECT (entry), "secondary-icon-pixbuf");
7590           g_object_notify (G_OBJECT (entry), "secondary-icon-storage-type");
7591         }
7592
7593       if (gtk_widget_get_mapped (GTK_WIDGET (entry)))
7594           gdk_window_show_unraised (icon_info->window);
7595
7596       g_object_unref (pixbuf);
7597     }
7598   
7599   if (gtk_widget_get_visible (GTK_WIDGET (entry)))
7600     gtk_widget_queue_resize (GTK_WIDGET (entry));
7601
7602   g_object_thaw_notify (G_OBJECT (entry));
7603 }
7604
7605 /**
7606  * gtk_entry_set_icon_from_stock:
7607  * @entry: A #GtkEntry
7608  * @icon_pos: Icon position
7609  * @stock_id: (allow-none): The name of the stock item, or %NULL
7610  *
7611  * Sets the icon shown in the entry at the specified position from
7612  * a stock image.
7613  *
7614  * If @stock_id is %NULL, no icon will be shown in the specified position.
7615  *
7616  * Since: 2.16
7617  */
7618 void
7619 gtk_entry_set_icon_from_stock (GtkEntry             *entry,
7620                                GtkEntryIconPosition  icon_pos,
7621                                const gchar          *stock_id)
7622 {
7623   GtkEntryPrivate *priv;
7624   EntryIconInfo *icon_info;
7625   gchar *new_id;
7626
7627   g_return_if_fail (GTK_IS_ENTRY (entry));
7628   g_return_if_fail (IS_VALID_ICON_POSITION (icon_pos));
7629
7630   priv = entry->priv;
7631
7632   if ((icon_info = priv->icons[icon_pos]) == NULL)
7633     icon_info = construct_icon_info (GTK_WIDGET (entry), icon_pos);
7634
7635   g_object_freeze_notify (G_OBJECT (entry));
7636
7637   /* need to dup before clearing */
7638   new_id = g_strdup (stock_id);
7639
7640   gtk_entry_clear (entry, icon_pos);
7641
7642   if (new_id != NULL)
7643     {
7644       _gtk_icon_helper_set_stock_id (icon_info->icon_helper, new_id, GTK_ICON_SIZE_MENU);
7645
7646       if (icon_pos == GTK_ENTRY_ICON_PRIMARY)
7647         {
7648           g_object_notify (G_OBJECT (entry), "primary-icon-stock");
7649           g_object_notify (G_OBJECT (entry), "primary-icon-storage-type");
7650         }
7651       else
7652         {
7653           g_object_notify (G_OBJECT (entry), "secondary-icon-stock");
7654           g_object_notify (G_OBJECT (entry), "secondary-icon-storage-type");
7655         }
7656
7657       if (gtk_widget_get_mapped (GTK_WIDGET (entry)))
7658           gdk_window_show_unraised (icon_info->window);
7659
7660       g_free (new_id);
7661     }
7662
7663   if (gtk_widget_get_visible (GTK_WIDGET (entry)))
7664     gtk_widget_queue_resize (GTK_WIDGET (entry));
7665
7666   g_object_thaw_notify (G_OBJECT (entry));
7667 }
7668
7669 /**
7670  * gtk_entry_set_icon_from_icon_name:
7671  * @entry: A #GtkEntry
7672  * @icon_pos: The position at which to set the icon
7673  * @icon_name: (allow-none): An icon name, or %NULL
7674  *
7675  * Sets the icon shown in the entry at the specified position
7676  * from the current icon theme.
7677  *
7678  * If the icon name isn't known, a "broken image" icon will be displayed
7679  * instead.
7680  *
7681  * If @icon_name is %NULL, no icon will be shown in the specified position.
7682  *
7683  * Since: 2.16
7684  */
7685 void
7686 gtk_entry_set_icon_from_icon_name (GtkEntry             *entry,
7687                                    GtkEntryIconPosition  icon_pos,
7688                                    const gchar          *icon_name)
7689 {
7690   GtkEntryPrivate *priv;
7691   EntryIconInfo *icon_info;
7692   gchar *new_name;
7693
7694   g_return_if_fail (GTK_IS_ENTRY (entry));
7695   g_return_if_fail (IS_VALID_ICON_POSITION (icon_pos));
7696
7697   priv = entry->priv;
7698
7699   if ((icon_info = priv->icons[icon_pos]) == NULL)
7700     icon_info = construct_icon_info (GTK_WIDGET (entry), icon_pos);
7701
7702   g_object_freeze_notify (G_OBJECT (entry));
7703
7704   /* need to dup before clearing */
7705   new_name = g_strdup (icon_name);
7706
7707   gtk_entry_clear (entry, icon_pos);
7708
7709   if (new_name != NULL)
7710     {
7711       _gtk_icon_helper_set_icon_name (icon_info->icon_helper, new_name, GTK_ICON_SIZE_MENU);
7712
7713       if (icon_pos == GTK_ENTRY_ICON_PRIMARY)
7714         {
7715           g_object_notify (G_OBJECT (entry), "primary-icon-name");
7716           g_object_notify (G_OBJECT (entry), "primary-icon-storage-type");
7717         }
7718       else
7719         {
7720           g_object_notify (G_OBJECT (entry), "secondary-icon-name");
7721           g_object_notify (G_OBJECT (entry), "secondary-icon-storage-type");
7722         }
7723
7724       if (gtk_widget_get_mapped (GTK_WIDGET (entry)))
7725           gdk_window_show_unraised (icon_info->window);
7726
7727       g_free (new_name);
7728     }
7729
7730   if (gtk_widget_get_visible (GTK_WIDGET (entry)))
7731     gtk_widget_queue_resize (GTK_WIDGET (entry));
7732
7733   g_object_thaw_notify (G_OBJECT (entry));
7734 }
7735
7736 /**
7737  * gtk_entry_set_icon_from_gicon:
7738  * @entry: A #GtkEntry
7739  * @icon_pos: The position at which to set the icon
7740  * @icon: (allow-none): The icon to set, or %NULL
7741  *
7742  * Sets the icon shown in the entry at the specified position
7743  * from the current icon theme.
7744  * If the icon isn't known, a "broken image" icon will be displayed
7745  * instead.
7746  *
7747  * If @icon is %NULL, no icon will be shown in the specified position.
7748  *
7749  * Since: 2.16
7750  */
7751 void
7752 gtk_entry_set_icon_from_gicon (GtkEntry             *entry,
7753                                GtkEntryIconPosition  icon_pos,
7754                                GIcon                *icon)
7755 {
7756   GtkEntryPrivate *priv;
7757   EntryIconInfo *icon_info;
7758
7759   g_return_if_fail (GTK_IS_ENTRY (entry));
7760   g_return_if_fail (IS_VALID_ICON_POSITION (icon_pos));
7761
7762   priv = entry->priv;
7763
7764   if ((icon_info = priv->icons[icon_pos]) == NULL)
7765     icon_info = construct_icon_info (GTK_WIDGET (entry), icon_pos);
7766
7767   g_object_freeze_notify (G_OBJECT (entry));
7768
7769   /* need to ref before clearing */
7770   if (icon)
7771     g_object_ref (icon);
7772
7773   gtk_entry_clear (entry, icon_pos);
7774
7775   if (icon)
7776     {
7777       _gtk_icon_helper_set_gicon (icon_info->icon_helper, icon, GTK_ICON_SIZE_MENU);
7778
7779       if (icon_pos == GTK_ENTRY_ICON_PRIMARY)
7780         {
7781           g_object_notify (G_OBJECT (entry), "primary-icon-gicon");
7782           g_object_notify (G_OBJECT (entry), "primary-icon-storage-type");
7783         }
7784       else
7785         {
7786           g_object_notify (G_OBJECT (entry), "secondary-icon-gicon");
7787           g_object_notify (G_OBJECT (entry), "secondary-icon-storage-type");
7788         }
7789
7790       if (gtk_widget_get_mapped (GTK_WIDGET (entry)))
7791           gdk_window_show_unraised (icon_info->window);
7792
7793       g_object_unref (icon);
7794     }
7795
7796   if (gtk_widget_get_visible (GTK_WIDGET (entry)))
7797     gtk_widget_queue_resize (GTK_WIDGET (entry));
7798
7799   g_object_thaw_notify (G_OBJECT (entry));
7800 }
7801
7802 /**
7803  * gtk_entry_set_icon_activatable:
7804  * @entry: A #GtkEntry
7805  * @icon_pos: Icon position
7806  * @activatable: %TRUE if the icon should be activatable
7807  *
7808  * Sets whether the icon is activatable.
7809  *
7810  * Since: 2.16
7811  */
7812 void
7813 gtk_entry_set_icon_activatable (GtkEntry             *entry,
7814                                 GtkEntryIconPosition  icon_pos,
7815                                 gboolean              activatable)
7816 {
7817   GtkEntryPrivate *priv;
7818   EntryIconInfo *icon_info;
7819
7820   g_return_if_fail (GTK_IS_ENTRY (entry));
7821   g_return_if_fail (IS_VALID_ICON_POSITION (icon_pos));
7822
7823   priv = entry->priv;
7824
7825   if ((icon_info = priv->icons[icon_pos]) == NULL)
7826     icon_info = construct_icon_info (GTK_WIDGET (entry), icon_pos);
7827
7828   activatable = activatable != FALSE;
7829
7830   if (icon_info->nonactivatable != !activatable)
7831     {
7832       icon_info->nonactivatable = !activatable;
7833
7834       if (gtk_widget_get_realized (GTK_WIDGET (entry)))
7835         update_cursors (GTK_WIDGET (entry));
7836
7837       g_object_notify (G_OBJECT (entry),
7838                        icon_pos == GTK_ENTRY_ICON_PRIMARY ? "primary-icon-activatable" : "secondary-icon-activatable");
7839     }
7840 }
7841
7842 /**
7843  * gtk_entry_get_icon_activatable:
7844  * @entry: a #GtkEntry
7845  * @icon_pos: Icon position
7846  *
7847  * Returns whether the icon is activatable.
7848  *
7849  * Returns: %TRUE if the icon is activatable.
7850  *
7851  * Since: 2.16
7852  */
7853 gboolean
7854 gtk_entry_get_icon_activatable (GtkEntry             *entry,
7855                                 GtkEntryIconPosition  icon_pos)
7856 {
7857   GtkEntryPrivate *priv;
7858   EntryIconInfo *icon_info;
7859
7860   g_return_val_if_fail (GTK_IS_ENTRY (entry), FALSE);
7861   g_return_val_if_fail (IS_VALID_ICON_POSITION (icon_pos), FALSE);
7862
7863   priv = entry->priv;
7864   icon_info = priv->icons[icon_pos];
7865
7866   return (!icon_info || !icon_info->nonactivatable);
7867 }
7868
7869 /**
7870  * gtk_entry_get_icon_pixbuf:
7871  * @entry: A #GtkEntry
7872  * @icon_pos: Icon position
7873  *
7874  * Retrieves the image used for the icon.
7875  *
7876  * Unlike the other methods of setting and getting icon data, this
7877  * method will work regardless of whether the icon was set using a
7878  * #GdkPixbuf, a #GIcon, a stock item, or an icon name.
7879  *
7880  * Returns: (transfer none): A #GdkPixbuf, or %NULL if no icon is
7881  *     set for this position.
7882  *
7883  * Since: 2.16
7884  */
7885 GdkPixbuf *
7886 gtk_entry_get_icon_pixbuf (GtkEntry             *entry,
7887                            GtkEntryIconPosition  icon_pos)
7888 {
7889   GtkEntryPrivate *priv;
7890   EntryIconInfo *icon_info;
7891   GdkPixbuf *pixbuf;
7892
7893   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
7894   g_return_val_if_fail (IS_VALID_ICON_POSITION (icon_pos), NULL);
7895
7896   priv = entry->priv;
7897
7898   icon_info = priv->icons[icon_pos];
7899
7900   if (!icon_info)
7901     return NULL;
7902
7903   /* HACK: unfortunately this is transfer none, so we need to return
7904    * the icon helper's cache ref directly.
7905    */
7906   pixbuf = gtk_entry_ensure_pixbuf (entry, icon_pos);
7907   g_object_unref (pixbuf);
7908
7909   return pixbuf;
7910 }
7911
7912 /**
7913  * gtk_entry_get_icon_gicon:
7914  * @entry: A #GtkEntry
7915  * @icon_pos: Icon position
7916  *
7917  * Retrieves the #GIcon used for the icon, or %NULL if there is
7918  * no icon or if the icon was set by some other method (e.g., by
7919  * stock, pixbuf, or icon name).
7920  *
7921  * Returns: (transfer none): A #GIcon, or %NULL if no icon is set
7922  *     or if the icon is not a #GIcon
7923  *
7924  * Since: 2.16
7925  */
7926 GIcon *
7927 gtk_entry_get_icon_gicon (GtkEntry             *entry,
7928                           GtkEntryIconPosition  icon_pos)
7929 {
7930   GtkEntryPrivate *priv;
7931   EntryIconInfo *icon_info;
7932
7933   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
7934   g_return_val_if_fail (IS_VALID_ICON_POSITION (icon_pos), NULL);
7935
7936   priv = entry->priv;
7937   icon_info = priv->icons[icon_pos];
7938
7939   if (!icon_info)
7940     return NULL;
7941
7942   return _gtk_icon_helper_peek_gicon (icon_info->icon_helper);
7943 }
7944
7945 /**
7946  * gtk_entry_get_icon_stock:
7947  * @entry: A #GtkEntry
7948  * @icon_pos: Icon position
7949  *
7950  * Retrieves the stock id used for the icon, or %NULL if there is
7951  * no icon or if the icon was set by some other method (e.g., by
7952  * pixbuf, icon name or gicon).
7953  *
7954  * Returns: A stock id, or %NULL if no icon is set or if the icon
7955  *          wasn't set from a stock id
7956  *
7957  * Since: 2.16
7958  */
7959 const gchar *
7960 gtk_entry_get_icon_stock (GtkEntry             *entry,
7961                           GtkEntryIconPosition  icon_pos)
7962 {
7963   GtkEntryPrivate *priv;
7964   EntryIconInfo *icon_info;
7965
7966   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
7967   g_return_val_if_fail (IS_VALID_ICON_POSITION (icon_pos), NULL);
7968
7969   priv = entry->priv;
7970   icon_info = priv->icons[icon_pos];
7971
7972   if (!icon_info)
7973     return NULL;
7974
7975   return _gtk_icon_helper_get_stock_id (icon_info->icon_helper);
7976 }
7977
7978 /**
7979  * gtk_entry_get_icon_name:
7980  * @entry: A #GtkEntry
7981  * @icon_pos: Icon position
7982  *
7983  * Retrieves the icon name used for the icon, or %NULL if there is
7984  * no icon or if the icon was set by some other method (e.g., by
7985  * pixbuf, stock or gicon).
7986  *
7987  * Returns: An icon name, or %NULL if no icon is set or if the icon
7988  *          wasn't set from an icon name
7989  *
7990  * Since: 2.16
7991  */
7992 const gchar *
7993 gtk_entry_get_icon_name (GtkEntry             *entry,
7994                          GtkEntryIconPosition  icon_pos)
7995 {
7996   GtkEntryPrivate *priv;
7997   EntryIconInfo *icon_info;
7998
7999   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
8000   g_return_val_if_fail (IS_VALID_ICON_POSITION (icon_pos), NULL);
8001
8002   priv = entry->priv;
8003   icon_info = priv->icons[icon_pos];
8004
8005   if (!icon_info)
8006     return NULL;
8007
8008   return _gtk_icon_helper_get_icon_name (icon_info->icon_helper);
8009 }
8010
8011 /**
8012  * gtk_entry_set_icon_sensitive:
8013  * @entry: A #GtkEntry
8014  * @icon_pos: Icon position
8015  * @sensitive: Specifies whether the icon should appear
8016  *             sensitive or insensitive
8017  *
8018  * Sets the sensitivity for the specified icon.
8019  *
8020  * Since: 2.16
8021  */
8022 void
8023 gtk_entry_set_icon_sensitive (GtkEntry             *entry,
8024                               GtkEntryIconPosition  icon_pos,
8025                               gboolean              sensitive)
8026 {
8027   GtkEntryPrivate *priv;
8028   EntryIconInfo *icon_info;
8029
8030   g_return_if_fail (GTK_IS_ENTRY (entry));
8031   g_return_if_fail (IS_VALID_ICON_POSITION (icon_pos));
8032
8033   priv = entry->priv;
8034
8035   if ((icon_info = priv->icons[icon_pos]) == NULL)
8036     icon_info = construct_icon_info (GTK_WIDGET (entry), icon_pos);
8037
8038   if (icon_info->insensitive != !sensitive)
8039     {
8040       icon_info->insensitive = !sensitive;
8041
8042       icon_info->pressed = FALSE;
8043       icon_info->prelight = FALSE;
8044
8045       if (gtk_widget_get_realized (GTK_WIDGET (entry)))
8046         update_cursors (GTK_WIDGET (entry));
8047
8048       gtk_widget_queue_draw (GTK_WIDGET (entry));
8049
8050       g_object_notify (G_OBJECT (entry),
8051                        icon_pos == GTK_ENTRY_ICON_PRIMARY ? "primary-icon-sensitive" : "secondary-icon-sensitive");
8052     }
8053 }
8054
8055 /**
8056  * gtk_entry_get_icon_sensitive:
8057  * @entry: a #GtkEntry
8058  * @icon_pos: Icon position
8059  *
8060  * Returns whether the icon appears sensitive or insensitive.
8061  *
8062  * Returns: %TRUE if the icon is sensitive.
8063  *
8064  * Since: 2.16
8065  */
8066 gboolean
8067 gtk_entry_get_icon_sensitive (GtkEntry             *entry,
8068                               GtkEntryIconPosition  icon_pos)
8069 {
8070   GtkEntryPrivate *priv;
8071   EntryIconInfo *icon_info;
8072
8073   g_return_val_if_fail (GTK_IS_ENTRY (entry), TRUE);
8074   g_return_val_if_fail (IS_VALID_ICON_POSITION (icon_pos), TRUE);
8075
8076   priv = entry->priv;
8077
8078   icon_info = priv->icons[icon_pos];
8079
8080   return (!icon_info || !icon_info->insensitive);
8081
8082 }
8083
8084 /**
8085  * gtk_entry_get_icon_storage_type:
8086  * @entry: a #GtkEntry
8087  * @icon_pos: Icon position
8088  *
8089  * Gets the type of representation being used by the icon
8090  * to store image data. If the icon has no image data,
8091  * the return value will be %GTK_IMAGE_EMPTY.
8092  *
8093  * Return value: image representation being used
8094  *
8095  * Since: 2.16
8096  **/
8097 GtkImageType
8098 gtk_entry_get_icon_storage_type (GtkEntry             *entry,
8099                                  GtkEntryIconPosition  icon_pos)
8100 {
8101   GtkEntryPrivate *priv;
8102   EntryIconInfo *icon_info;
8103
8104   g_return_val_if_fail (GTK_IS_ENTRY (entry), GTK_IMAGE_EMPTY);
8105   g_return_val_if_fail (IS_VALID_ICON_POSITION (icon_pos), GTK_IMAGE_EMPTY);
8106
8107   priv = entry->priv;
8108
8109   icon_info = priv->icons[icon_pos];
8110
8111   if (!icon_info)
8112     return GTK_IMAGE_EMPTY;
8113
8114   return _gtk_icon_helper_get_storage_type (icon_info->icon_helper);
8115 }
8116
8117 /**
8118  * gtk_entry_get_icon_at_pos:
8119  * @entry: a #GtkEntry
8120  * @x: the x coordinate of the position to find
8121  * @y: the y coordinate of the position to find
8122  *
8123  * Finds the icon at the given position and return its index. The
8124  * position's coordinates are relative to the @entry's top left corner.
8125  * If @x, @y doesn't lie inside an icon, -1 is returned.
8126  * This function is intended for use in a #GtkWidget::query-tooltip
8127  * signal handler.
8128  *
8129  * Returns: the index of the icon at the given position, or -1
8130  *
8131  * Since: 2.16
8132  */
8133 gint
8134 gtk_entry_get_icon_at_pos (GtkEntry *entry,
8135                            gint      x,
8136                            gint      y)
8137 {
8138   GtkAllocation primary;
8139   GtkAllocation secondary;
8140   gint frame_x, frame_y;
8141
8142   g_return_val_if_fail (GTK_IS_ENTRY (entry), -1);
8143
8144   get_frame_size (entry, FALSE, &frame_x, &frame_y, NULL, NULL);
8145   x -= frame_x;
8146   y -= frame_y;
8147
8148   get_icon_allocations (entry, &primary, &secondary);
8149
8150   if (primary.x <= x && x < primary.x + primary.width &&
8151       primary.y <= y && y < primary.y + primary.height)
8152     return GTK_ENTRY_ICON_PRIMARY;
8153
8154   if (secondary.x <= x && x < secondary.x + secondary.width &&
8155       secondary.y <= y && y < secondary.y + secondary.height)
8156     return GTK_ENTRY_ICON_SECONDARY;
8157
8158   return -1;
8159 }
8160
8161 /**
8162  * gtk_entry_set_icon_drag_source:
8163  * @entry: a #GtkIconEntry
8164  * @icon_pos: icon position
8165  * @target_list: the targets (data formats) in which the data can be provided
8166  * @actions: a bitmask of the allowed drag actions
8167  *
8168  * Sets up the icon at the given position so that GTK+ will start a drag
8169  * operation when the user clicks and drags the icon.
8170  *
8171  * To handle the drag operation, you need to connect to the usual
8172  * #GtkWidget::drag-data-get (or possibly #GtkWidget::drag-data-delete)
8173  * signal, and use gtk_entry_get_current_icon_drag_source() in
8174  * your signal handler to find out if the drag was started from
8175  * an icon.
8176  *
8177  * By default, GTK+ uses the icon as the drag icon. You can use the 
8178  * #GtkWidget::drag-begin signal to set a different icon. Note that you 
8179  * have to use g_signal_connect_after() to ensure that your signal handler
8180  * gets executed after the default handler.
8181  *
8182  * Since: 2.16
8183  */
8184 void
8185 gtk_entry_set_icon_drag_source (GtkEntry             *entry,
8186                                 GtkEntryIconPosition  icon_pos,
8187                                 GtkTargetList        *target_list,
8188                                 GdkDragAction         actions)
8189 {
8190   GtkEntryPrivate *priv;
8191   EntryIconInfo *icon_info;
8192
8193   g_return_if_fail (GTK_IS_ENTRY (entry));
8194   g_return_if_fail (IS_VALID_ICON_POSITION (icon_pos));
8195
8196   priv = entry->priv;
8197
8198   if ((icon_info = priv->icons[icon_pos]) == NULL)
8199     icon_info = construct_icon_info (GTK_WIDGET (entry), icon_pos);
8200
8201   if (icon_info->target_list)
8202     gtk_target_list_unref (icon_info->target_list);
8203   icon_info->target_list = target_list;
8204   if (icon_info->target_list)
8205     gtk_target_list_ref (icon_info->target_list);
8206
8207   icon_info->actions = actions;
8208 }
8209
8210 /**
8211  * gtk_entry_get_current_icon_drag_source:
8212  * @entry: a #GtkIconEntry
8213  *
8214  * Returns the index of the icon which is the source of the current
8215  * DND operation, or -1.
8216  *
8217  * This function is meant to be used in a #GtkWidget::drag-data-get
8218  * callback.
8219  *
8220  * Returns: index of the icon which is the source of the current
8221  *          DND operation, or -1.
8222  *
8223  * Since: 2.16
8224  */
8225 gint
8226 gtk_entry_get_current_icon_drag_source (GtkEntry *entry)
8227 {
8228   GtkEntryPrivate *priv;
8229   EntryIconInfo *icon_info = NULL;
8230   gint i;
8231
8232   g_return_val_if_fail (GTK_IS_ENTRY (entry), -1);
8233
8234   priv = entry->priv;
8235
8236   for (i = 0; i < MAX_ICONS; i++)
8237     {
8238       if ((icon_info = priv->icons[i]))
8239         {
8240           if (icon_info->in_drag)
8241             return i;
8242         }
8243     }
8244
8245   return -1;
8246 }
8247
8248 /**
8249  * gtk_entry_get_icon_area:
8250  * @entry: A #GtkEntry
8251  * @icon_pos: Icon position
8252  * @icon_area: (out): Return location for the icon's area
8253  *
8254  * Gets the area where entry's icon at @icon_pos is drawn.
8255  * This function is useful when drawing something to the
8256  * entry in a draw callback.
8257  *
8258  * If the entry is not realized or has no icon at the given position,
8259  * @icon_area is filled with zeros.
8260  *
8261  * See also gtk_entry_get_text_area()
8262  *
8263  * Since: 3.0
8264  */
8265 void
8266 gtk_entry_get_icon_area (GtkEntry             *entry,
8267                          GtkEntryIconPosition  icon_pos,
8268                          GdkRectangle         *icon_area)
8269 {
8270   GtkEntryPrivate *priv;
8271   EntryIconInfo *icon_info;
8272
8273   g_return_if_fail (GTK_IS_ENTRY (entry));
8274   g_return_if_fail (icon_area != NULL);
8275
8276   priv = entry->priv;
8277
8278   icon_info = priv->icons[icon_pos];
8279
8280   if (icon_info)
8281     {
8282       GtkAllocation primary;
8283       GtkAllocation secondary;
8284
8285       get_icon_allocations (entry, &primary, &secondary);
8286
8287       if (icon_pos == GTK_ENTRY_ICON_PRIMARY)
8288         *icon_area = primary;
8289       else
8290         *icon_area = secondary;
8291     }
8292   else
8293     {
8294       icon_area->x = 0;
8295       icon_area->y = 0;
8296       icon_area->width = 0;
8297       icon_area->height = 0;
8298     }
8299 }
8300
8301 static void
8302 ensure_has_tooltip (GtkEntry *entry)
8303 {
8304   GtkEntryPrivate *priv;
8305   EntryIconInfo *icon_info;
8306   int i;
8307   gboolean has_tooltip = FALSE;
8308
8309   priv = entry->priv;
8310
8311   for (i = 0; i < MAX_ICONS; i++)
8312     {
8313       if ((icon_info = priv->icons[i]) != NULL)
8314         {
8315           if (icon_info->tooltip != NULL)
8316             {
8317               has_tooltip = TRUE;
8318               break;
8319             }
8320         }
8321     }
8322
8323   gtk_widget_set_has_tooltip (GTK_WIDGET (entry), has_tooltip);
8324 }
8325
8326 /**
8327  * gtk_entry_get_icon_tooltip_text:
8328  * @entry: a #GtkEntry
8329  * @icon_pos: the icon position
8330  *
8331  * Gets the contents of the tooltip on the icon at the specified 
8332  * position in @entry.
8333  * 
8334  * Returns: the tooltip text, or %NULL. Free the returned string
8335  *     with g_free() when done.
8336  * 
8337  * Since: 2.16
8338  */
8339 gchar *
8340 gtk_entry_get_icon_tooltip_text (GtkEntry             *entry,
8341                                  GtkEntryIconPosition  icon_pos)
8342 {
8343   GtkEntryPrivate *priv;
8344   EntryIconInfo *icon_info;
8345   gchar *text = NULL;
8346
8347   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
8348   g_return_val_if_fail (IS_VALID_ICON_POSITION (icon_pos), NULL);
8349
8350   priv = entry->priv;
8351
8352   icon_info = priv->icons[icon_pos];
8353
8354   if (!icon_info)
8355     return NULL;
8356  
8357   if (icon_info->tooltip && 
8358       !pango_parse_markup (icon_info->tooltip, -1, 0, NULL, &text, NULL, NULL))
8359     g_assert (NULL == text); /* text should still be NULL in case of markup errors */
8360
8361   return text;
8362 }
8363
8364 /**
8365  * gtk_entry_set_icon_tooltip_text:
8366  * @entry: a #GtkEntry
8367  * @icon_pos: the icon position
8368  * @tooltip: (allow-none): the contents of the tooltip for the icon, or %NULL
8369  *
8370  * Sets @tooltip as the contents of the tooltip for the icon
8371  * at the specified position.
8372  *
8373  * Use %NULL for @tooltip to remove an existing tooltip.
8374  *
8375  * See also gtk_widget_set_tooltip_text() and 
8376  * gtk_entry_set_icon_tooltip_markup().
8377  *
8378  * Since: 2.16
8379  */
8380 void
8381 gtk_entry_set_icon_tooltip_text (GtkEntry             *entry,
8382                                  GtkEntryIconPosition  icon_pos,
8383                                  const gchar          *tooltip)
8384 {
8385   GtkEntryPrivate *priv;
8386   EntryIconInfo *icon_info;
8387
8388   g_return_if_fail (GTK_IS_ENTRY (entry));
8389   g_return_if_fail (IS_VALID_ICON_POSITION (icon_pos));
8390
8391   priv = entry->priv;
8392
8393   if ((icon_info = priv->icons[icon_pos]) == NULL)
8394     icon_info = construct_icon_info (GTK_WIDGET (entry), icon_pos);
8395
8396   if (icon_info->tooltip)
8397     g_free (icon_info->tooltip);
8398
8399   /* Treat an empty string as a NULL string,
8400    * because an empty string would be useless for a tooltip:
8401    */
8402   if (tooltip && tooltip[0] == '\0')
8403     tooltip = NULL;
8404
8405   icon_info->tooltip = tooltip ? g_markup_escape_text (tooltip, -1) : NULL;
8406
8407   ensure_has_tooltip (entry);
8408 }
8409
8410 /**
8411  * gtk_entry_get_icon_tooltip_markup:
8412  * @entry: a #GtkEntry
8413  * @icon_pos: the icon position
8414  *
8415  * Gets the contents of the tooltip on the icon at the specified 
8416  * position in @entry.
8417  * 
8418  * Returns: the tooltip text, or %NULL. Free the returned string
8419  *     with g_free() when done.
8420  * 
8421  * Since: 2.16
8422  */
8423 gchar *
8424 gtk_entry_get_icon_tooltip_markup (GtkEntry             *entry,
8425                                    GtkEntryIconPosition  icon_pos)
8426 {
8427   GtkEntryPrivate *priv;
8428   EntryIconInfo *icon_info;
8429
8430   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
8431   g_return_val_if_fail (IS_VALID_ICON_POSITION (icon_pos), NULL);
8432
8433   priv = entry->priv;
8434
8435   icon_info = priv->icons[icon_pos];
8436
8437   if (!icon_info)
8438     return NULL;
8439  
8440   return g_strdup (icon_info->tooltip);
8441 }
8442
8443 /**
8444  * gtk_entry_set_icon_tooltip_markup:
8445  * @entry: a #GtkEntry
8446  * @icon_pos: the icon position
8447  * @tooltip: (allow-none): the contents of the tooltip for the icon, or %NULL
8448  *
8449  * Sets @tooltip as the contents of the tooltip for the icon at
8450  * the specified position. @tooltip is assumed to be marked up with
8451  * the <link linkend="PangoMarkupFormat">Pango text markup language</link>.
8452  *
8453  * Use %NULL for @tooltip to remove an existing tooltip.
8454  *
8455  * See also gtk_widget_set_tooltip_markup() and 
8456  * gtk_enty_set_icon_tooltip_text().
8457  *
8458  * Since: 2.16
8459  */
8460 void
8461 gtk_entry_set_icon_tooltip_markup (GtkEntry             *entry,
8462                                    GtkEntryIconPosition  icon_pos,
8463                                    const gchar          *tooltip)
8464 {
8465   GtkEntryPrivate *priv;
8466   EntryIconInfo *icon_info;
8467
8468   g_return_if_fail (GTK_IS_ENTRY (entry));
8469   g_return_if_fail (IS_VALID_ICON_POSITION (icon_pos));
8470
8471   priv = entry->priv;
8472
8473   if ((icon_info = priv->icons[icon_pos]) == NULL)
8474     icon_info = construct_icon_info (GTK_WIDGET (entry), icon_pos);
8475
8476   if (icon_info->tooltip)
8477     g_free (icon_info->tooltip);
8478
8479   /* Treat an empty string as a NULL string,
8480    * because an empty string would be useless for a tooltip:
8481    */
8482   if (tooltip && tooltip[0] == '\0')
8483     tooltip = NULL;
8484
8485   icon_info->tooltip = g_strdup (tooltip);
8486
8487   ensure_has_tooltip (entry);
8488 }
8489
8490 static gboolean
8491 gtk_entry_query_tooltip (GtkWidget  *widget,
8492                          gint        x,
8493                          gint        y,
8494                          gboolean    keyboard_tip,
8495                          GtkTooltip *tooltip)
8496 {
8497   GtkEntry *entry;
8498   GtkEntryPrivate *priv;
8499   EntryIconInfo *icon_info;
8500   gint icon_pos;
8501
8502   entry = GTK_ENTRY (widget);
8503   priv = entry->priv;
8504
8505   if (!keyboard_tip)
8506     {
8507       icon_pos = gtk_entry_get_icon_at_pos (entry, x, y);
8508       if (icon_pos != -1)
8509         {
8510           if ((icon_info = priv->icons[icon_pos]) != NULL)
8511             {
8512               if (icon_info->tooltip)
8513                 {
8514                   gtk_tooltip_set_markup (tooltip, icon_info->tooltip);
8515                   return TRUE;
8516                 }
8517
8518               return FALSE;
8519             }
8520         }
8521     }
8522
8523   return GTK_WIDGET_CLASS (gtk_entry_parent_class)->query_tooltip (widget,
8524                                                                    x, y,
8525                                                                    keyboard_tip,
8526                                                                    tooltip);
8527 }
8528
8529
8530 /* Quick hack of a popup menu
8531  */
8532 static void
8533 activate_cb (GtkWidget *menuitem,
8534              GtkEntry  *entry)
8535 {
8536   const gchar *signal = g_object_get_data (G_OBJECT (menuitem), "gtk-signal");
8537   g_signal_emit_by_name (entry, signal);
8538 }
8539
8540
8541 static gboolean
8542 gtk_entry_mnemonic_activate (GtkWidget *widget,
8543                              gboolean   group_cycling)
8544 {
8545   gtk_widget_grab_focus (widget);
8546   return TRUE;
8547 }
8548
8549 static void
8550 append_action_signal (GtkEntry     *entry,
8551                       GtkWidget    *menu,
8552                       const gchar  *stock_id,
8553                       const gchar  *signal,
8554                       gboolean      sensitive)
8555 {
8556   GtkWidget *menuitem = gtk_image_menu_item_new_from_stock (stock_id, NULL);
8557
8558   g_object_set_data (G_OBJECT (menuitem), I_("gtk-signal"), (char *)signal);
8559   g_signal_connect (menuitem, "activate",
8560                     G_CALLBACK (activate_cb), entry);
8561
8562   gtk_widget_set_sensitive (menuitem, sensitive);
8563   
8564   gtk_widget_show (menuitem);
8565   gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
8566 }
8567         
8568 static void
8569 popup_menu_detach (GtkWidget *attach_widget,
8570                    GtkMenu   *menu)
8571 {
8572   GtkEntry *entry_attach = GTK_ENTRY (attach_widget);
8573   GtkEntryPrivate *priv_attach = entry_attach->priv;
8574
8575   priv_attach->popup_menu = NULL;
8576 }
8577
8578 static void
8579 popup_position_func (GtkMenu   *menu,
8580                      gint      *x,
8581                      gint      *y,
8582                      gboolean  *push_in,
8583                      gpointer   user_data)
8584 {
8585   GtkEntry *entry = GTK_ENTRY (user_data);
8586   GtkEntryPrivate *priv = entry->priv;
8587   GtkWidget *widget = GTK_WIDGET (entry);
8588   GdkScreen *screen;
8589   GtkRequisition menu_req;
8590   GdkRectangle monitor;
8591   GtkBorder inner_border;
8592   gint monitor_num, strong_x, height;
8593  
8594   g_return_if_fail (gtk_widget_get_realized (widget));
8595
8596   gdk_window_get_origin (priv->text_area, x, y);
8597
8598   screen = gtk_widget_get_screen (widget);
8599   monitor_num = gdk_screen_get_monitor_at_window (screen, priv->text_area);
8600   if (monitor_num < 0)
8601     monitor_num = 0;
8602   gtk_menu_set_monitor (menu, monitor_num);
8603
8604   gdk_screen_get_monitor_workarea (screen, monitor_num, &monitor);
8605   gtk_widget_get_preferred_size (priv->popup_menu,
8606                                  &menu_req, NULL);
8607   height = gdk_window_get_height (priv->text_area);
8608   gtk_entry_get_cursor_locations (entry, CURSOR_STANDARD, &strong_x, NULL);
8609   _gtk_entry_effective_inner_border (entry, &inner_border);
8610
8611   *x += inner_border.left + strong_x - priv->scroll_offset;
8612   if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
8613     *x -= menu_req.width;
8614
8615   if ((*y + height + menu_req.height) <= monitor.y + monitor.height)
8616     *y += height;
8617   else if ((*y - menu_req.height) >= monitor.y)
8618     *y -= menu_req.height;
8619   else if (monitor.y + monitor.height - (*y + height) > *y)
8620     *y += height;
8621   else
8622     *y -= menu_req.height;
8623
8624   *push_in = FALSE;
8625 }
8626
8627 static void
8628 unichar_chosen_func (const char *text,
8629                      gpointer    data)
8630 {
8631   GtkEntry *entry = GTK_ENTRY (data);
8632   GtkEntryPrivate *priv = entry->priv;
8633
8634   if (priv->editable)
8635     gtk_entry_enter_text (entry, text);
8636 }
8637
8638 typedef struct
8639 {
8640   GtkEntry *entry;
8641   gint button;
8642   guint time;
8643   GdkDevice *device;
8644 } PopupInfo;
8645
8646 static void
8647 popup_targets_received (GtkClipboard     *clipboard,
8648                         GtkSelectionData *data,
8649                         gpointer          user_data)
8650 {
8651   PopupInfo *info = user_data;
8652   GtkEntry *entry = info->entry;
8653   GtkEntryPrivate *info_entry_priv = entry->priv;
8654
8655   if (gtk_widget_get_realized (GTK_WIDGET (entry)))
8656     {
8657       DisplayMode mode;
8658       gboolean clipboard_contains_text;
8659       GtkWidget *menuitem;
8660       GtkWidget *submenu;
8661       gboolean show_input_method_menu;
8662       gboolean show_unicode_menu;
8663       
8664       clipboard_contains_text = gtk_selection_data_targets_include_text (data);
8665       if (info_entry_priv->popup_menu)
8666         gtk_widget_destroy (info_entry_priv->popup_menu);
8667
8668       info_entry_priv->popup_menu = gtk_menu_new ();
8669
8670       gtk_menu_attach_to_widget (GTK_MENU (info_entry_priv->popup_menu),
8671                                  GTK_WIDGET (entry),
8672                                  popup_menu_detach);
8673       
8674       mode = gtk_entry_get_display_mode (entry);
8675       append_action_signal (entry, info_entry_priv->popup_menu, GTK_STOCK_CUT, "cut-clipboard",
8676                             info_entry_priv->editable && mode == DISPLAY_NORMAL &&
8677                             info_entry_priv->current_pos != info_entry_priv->selection_bound);
8678
8679       append_action_signal (entry, info_entry_priv->popup_menu, GTK_STOCK_COPY, "copy-clipboard",
8680                             mode == DISPLAY_NORMAL &&
8681                             info_entry_priv->current_pos != info_entry_priv->selection_bound);
8682
8683       append_action_signal (entry, info_entry_priv->popup_menu, GTK_STOCK_PASTE, "paste-clipboard",
8684                             info_entry_priv->editable && clipboard_contains_text);
8685
8686       menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_DELETE, NULL);
8687       gtk_widget_set_sensitive (menuitem, info_entry_priv->editable && info_entry_priv->current_pos != info_entry_priv->selection_bound);
8688       g_signal_connect_swapped (menuitem, "activate",
8689                                 G_CALLBACK (gtk_entry_delete_cb), entry);
8690       gtk_widget_show (menuitem);
8691       gtk_menu_shell_append (GTK_MENU_SHELL (info_entry_priv->popup_menu), menuitem);
8692
8693       menuitem = gtk_separator_menu_item_new ();
8694       gtk_widget_show (menuitem);
8695       gtk_menu_shell_append (GTK_MENU_SHELL (info_entry_priv->popup_menu), menuitem);
8696       
8697       menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_SELECT_ALL, NULL);
8698       gtk_widget_set_sensitive (menuitem, gtk_entry_buffer_get_length (info_entry_priv->buffer) > 0);
8699       g_signal_connect_swapped (menuitem, "activate",
8700                                 G_CALLBACK (gtk_entry_select_all), entry);
8701       gtk_widget_show (menuitem);
8702       gtk_menu_shell_append (GTK_MENU_SHELL (info_entry_priv->popup_menu), menuitem);
8703       
8704       g_object_get (gtk_widget_get_settings (GTK_WIDGET (entry)),
8705                     "gtk-show-input-method-menu", &show_input_method_menu,
8706                     "gtk-show-unicode-menu", &show_unicode_menu,
8707                     NULL);
8708
8709       if (show_input_method_menu || show_unicode_menu)
8710         {
8711           menuitem = gtk_separator_menu_item_new ();
8712           gtk_widget_show (menuitem);
8713           gtk_menu_shell_append (GTK_MENU_SHELL (info_entry_priv->popup_menu), menuitem);
8714         }
8715       
8716       if (show_input_method_menu)
8717         {
8718           menuitem = gtk_menu_item_new_with_mnemonic (_("Input _Methods"));
8719           gtk_widget_set_sensitive (menuitem, info_entry_priv->editable);
8720           gtk_widget_show (menuitem);
8721           submenu = gtk_menu_new ();
8722           gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
8723
8724           gtk_menu_shell_append (GTK_MENU_SHELL (info_entry_priv->popup_menu), menuitem);
8725
8726           gtk_im_multicontext_append_menuitems (GTK_IM_MULTICONTEXT (info_entry_priv->im_context),
8727                                                 GTK_MENU_SHELL (submenu));
8728         }
8729       
8730       if (show_unicode_menu)
8731         {
8732           menuitem = gtk_menu_item_new_with_mnemonic (_("_Insert Unicode Control Character"));
8733           gtk_widget_set_sensitive (menuitem, info_entry_priv->editable);
8734           gtk_widget_show (menuitem);
8735           
8736           submenu = gtk_menu_new ();
8737           gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
8738           gtk_menu_shell_append (GTK_MENU_SHELL (info_entry_priv->popup_menu), menuitem);
8739
8740           _gtk_text_util_append_special_char_menuitems (GTK_MENU_SHELL (submenu),
8741                                                         unichar_chosen_func,
8742                                                         entry);
8743         }
8744       
8745       g_signal_emit (entry,
8746                      signals[POPULATE_POPUP],
8747                      0,
8748                      info_entry_priv->popup_menu);
8749
8750
8751       if (info->device)
8752         gtk_menu_popup_for_device (GTK_MENU (info_entry_priv->popup_menu),
8753                         info->device, NULL, NULL, NULL, NULL, NULL,
8754                         info->button, info->time);
8755       else
8756         {
8757           gtk_menu_popup (GTK_MENU (info_entry_priv->popup_menu), NULL, NULL,
8758                           popup_position_func, entry,
8759                           0, gtk_get_current_event_time ());
8760           gtk_menu_shell_select_first (GTK_MENU_SHELL (info_entry_priv->popup_menu), FALSE);
8761         }
8762     }
8763
8764   g_object_unref (entry);
8765   g_slice_free (PopupInfo, info);
8766 }
8767                         
8768 static void
8769 gtk_entry_do_popup (GtkEntry       *entry,
8770                     GdkEventButton *event)
8771 {
8772   PopupInfo *info = g_slice_new (PopupInfo);
8773
8774   /* In order to know what entries we should make sensitive, we
8775    * ask for the current targets of the clipboard, and when
8776    * we get them, then we actually pop up the menu.
8777    */
8778   info->entry = g_object_ref (entry);
8779   
8780   if (event)
8781     {
8782       info->button = event->button;
8783       info->time = event->time;
8784       info->device = event->device;
8785     }
8786   else
8787     {
8788       info->button = 0;
8789       info->time = gtk_get_current_event_time ();
8790       info->device = NULL;
8791     }
8792
8793   gtk_clipboard_request_contents (gtk_widget_get_clipboard (GTK_WIDGET (entry), GDK_SELECTION_CLIPBOARD),
8794                                   gdk_atom_intern_static_string ("TARGETS"),
8795                                   popup_targets_received,
8796                                   info);
8797 }
8798
8799 static gboolean
8800 gtk_entry_popup_menu (GtkWidget *widget)
8801 {
8802   gtk_entry_do_popup (GTK_ENTRY (widget), NULL);
8803   return TRUE;
8804 }
8805
8806 static void
8807 gtk_entry_drag_begin (GtkWidget      *widget,
8808                       GdkDragContext *context)
8809 {
8810   GtkEntry *entry = GTK_ENTRY (widget);
8811   GtkEntryPrivate *priv = entry->priv;
8812   gint i;
8813
8814   for (i = 0; i < MAX_ICONS; i++)
8815     {
8816       EntryIconInfo *icon_info = priv->icons[i];
8817       
8818       if (icon_info != NULL)
8819         {
8820           if (icon_info->in_drag) 
8821             {
8822               GdkPixbuf *pix;
8823
8824               pix = _gtk_icon_helper_ensure_pixbuf
8825                 (icon_info->icon_helper,
8826                  gtk_widget_get_style_context (GTK_WIDGET (entry)));
8827               gtk_drag_set_icon_pixbuf (context, pix, -2, -2);
8828
8829               g_object_unref (pix);
8830             }
8831         }
8832     }
8833 }
8834
8835 static void
8836 gtk_entry_drag_end (GtkWidget      *widget,
8837                     GdkDragContext *context)
8838 {
8839   GtkEntry *entry = GTK_ENTRY (widget);
8840   GtkEntryPrivate *priv = entry->priv;
8841   gint i;
8842
8843   for (i = 0; i < MAX_ICONS; i++)
8844     {
8845       EntryIconInfo *icon_info = priv->icons[i];
8846
8847       if (icon_info != NULL)
8848         icon_info->in_drag = 0;
8849     }
8850 }
8851
8852 static void
8853 gtk_entry_drag_leave (GtkWidget        *widget,
8854                       GdkDragContext   *context,
8855                       guint             time)
8856 {
8857   GtkEntry *entry = GTK_ENTRY (widget);
8858   GtkEntryPrivate *priv = entry->priv;
8859
8860   priv->dnd_position = -1;
8861   gtk_widget_queue_draw (widget);
8862 }
8863
8864 static gboolean
8865 gtk_entry_drag_drop  (GtkWidget        *widget,
8866                       GdkDragContext   *context,
8867                       gint              x,
8868                       gint              y,
8869                       guint             time)
8870 {
8871   GtkEntry *entry = GTK_ENTRY (widget);
8872   GtkEntryPrivate *priv = entry->priv;
8873   GdkAtom target = GDK_NONE;
8874
8875   if (priv->editable)
8876     target = gtk_drag_dest_find_target (widget, context, NULL);
8877
8878   if (target != GDK_NONE)
8879     gtk_drag_get_data (widget, context, target, time);
8880   else
8881     gtk_drag_finish (context, FALSE, FALSE, time);
8882   
8883   return TRUE;
8884 }
8885
8886 static gboolean
8887 gtk_entry_drag_motion (GtkWidget        *widget,
8888                        GdkDragContext   *context,
8889                        gint              x,
8890                        gint              y,
8891                        guint             time)
8892 {
8893   GtkEntry *entry = GTK_ENTRY (widget);
8894   GtkEntryPrivate *priv = entry->priv;
8895   GtkStyleContext *style_context;
8896   GtkWidget *source_widget;
8897   GdkDragAction suggested_action;
8898   gint new_position, old_position;
8899   gint sel1, sel2;
8900   GtkBorder padding;
8901
8902   style_context = gtk_widget_get_style_context (widget);
8903   gtk_style_context_get_padding (style_context, 0, &padding);
8904   x -= padding.left;
8905   y -= padding.top;
8906
8907   old_position = priv->dnd_position;
8908   new_position = gtk_entry_find_position (entry, x + priv->scroll_offset);
8909
8910   if (priv->editable &&
8911       gtk_drag_dest_find_target (widget, context, NULL) != GDK_NONE)
8912     {
8913       source_widget = gtk_drag_get_source_widget (context);
8914       suggested_action = gdk_drag_context_get_suggested_action (context);
8915
8916       if (!gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &sel1, &sel2) ||
8917           new_position < sel1 || new_position > sel2)
8918         {
8919           if (source_widget == widget)
8920             {
8921               /* Default to MOVE, unless the user has
8922                * pressed ctrl or alt to affect available actions
8923                */
8924               if ((gdk_drag_context_get_actions (context) & GDK_ACTION_MOVE) != 0)
8925                 suggested_action = GDK_ACTION_MOVE;
8926             }
8927
8928           priv->dnd_position = new_position;
8929         }
8930       else
8931         {
8932           if (source_widget == widget)
8933             suggested_action = 0;       /* Can't drop in selection where drag started */
8934
8935           priv->dnd_position = -1;
8936         }
8937     }
8938   else
8939     {
8940       /* Entry not editable, or no text */
8941       suggested_action = 0;
8942       priv->dnd_position = -1;
8943     }
8944   
8945   gdk_drag_status (context, suggested_action, time);
8946
8947   if (priv->dnd_position != old_position)
8948     gtk_widget_queue_draw (widget);
8949
8950   return TRUE;
8951 }
8952
8953 static void
8954 gtk_entry_drag_data_received (GtkWidget        *widget,
8955                               GdkDragContext   *context,
8956                               gint              x,
8957                               gint              y,
8958                               GtkSelectionData *selection_data,
8959                               guint             info,
8960                               guint             time)
8961 {
8962   GtkEntry *entry = GTK_ENTRY (widget);
8963   GtkEntryPrivate *priv = entry->priv;
8964   GtkEditable *editable = GTK_EDITABLE (widget);
8965   GtkStyleContext *style_context;
8966   GtkBorder padding;
8967   gchar *str;
8968
8969   str = (gchar *) gtk_selection_data_get_text (selection_data);
8970
8971   style_context = gtk_widget_get_style_context (widget);
8972   gtk_style_context_get_padding (style_context, 0, &padding);
8973   x -= padding.left;
8974   y -= padding.top;
8975
8976   if (str && priv->editable)
8977     {
8978       gint new_position;
8979       gint sel1, sel2;
8980       gint length = -1;
8981
8982       if (priv->truncate_multiline)
8983         length = truncate_multiline (str);
8984
8985       new_position = gtk_entry_find_position (entry, x + priv->scroll_offset);
8986
8987       if (!gtk_editable_get_selection_bounds (editable, &sel1, &sel2) ||
8988           new_position < sel1 || new_position > sel2)
8989         {
8990           gtk_editable_insert_text (editable, str, length, &new_position);
8991         }
8992       else
8993         {
8994           /* Replacing selection */
8995           begin_change (entry);
8996           gtk_editable_delete_text (editable, sel1, sel2);
8997           gtk_editable_insert_text (editable, str, length, &sel1);
8998           end_change (entry);
8999         }
9000       
9001       gtk_drag_finish (context, TRUE, gdk_drag_context_get_selected_action (context) == GDK_ACTION_MOVE, time);
9002     }
9003   else
9004     {
9005       /* Drag and drop didn't happen! */
9006       gtk_drag_finish (context, FALSE, FALSE, time);
9007     }
9008
9009   g_free (str);
9010 }
9011
9012 static void
9013 gtk_entry_drag_data_get (GtkWidget        *widget,
9014                          GdkDragContext   *context,
9015                          GtkSelectionData *selection_data,
9016                          guint             info,
9017                          guint             time)
9018 {
9019   GtkEntry *entry = GTK_ENTRY (widget);
9020   GtkEntryPrivate *priv = entry->priv;
9021   GtkEditable *editable = GTK_EDITABLE (widget);
9022   gint sel_start, sel_end;
9023   gint i;
9024
9025   /* If there is an icon drag going on, exit early. */
9026   for (i = 0; i < MAX_ICONS; i++)
9027     {
9028       EntryIconInfo *icon_info = priv->icons[i];
9029
9030       if (icon_info != NULL)
9031         {
9032           if (icon_info->in_drag)
9033             return;
9034         }
9035     }
9036
9037   if (gtk_editable_get_selection_bounds (editable, &sel_start, &sel_end))
9038     {
9039       gchar *str = gtk_entry_get_display_text (GTK_ENTRY (widget), sel_start, sel_end);
9040
9041       gtk_selection_data_set_text (selection_data, str, -1);
9042       
9043       g_free (str);
9044     }
9045
9046 }
9047
9048 static void
9049 gtk_entry_drag_data_delete (GtkWidget      *widget,
9050                             GdkDragContext *context)
9051 {
9052   GtkEntry *entry = GTK_ENTRY (widget);
9053   GtkEntryPrivate *priv = entry->priv;
9054   GtkEditable *editable = GTK_EDITABLE (widget);
9055   gint sel_start, sel_end;
9056   gint i;
9057
9058   /* If there is an icon drag going on, exit early. */
9059   for (i = 0; i < MAX_ICONS; i++)
9060     {
9061       EntryIconInfo *icon_info = priv->icons[i];
9062
9063       if (icon_info != NULL)
9064         {
9065           if (icon_info->in_drag)
9066             return;
9067         }
9068     }
9069
9070   if (priv->editable &&
9071       gtk_editable_get_selection_bounds (editable, &sel_start, &sel_end))
9072     gtk_editable_delete_text (editable, sel_start, sel_end);
9073 }
9074
9075 /* We display the cursor when
9076  *
9077  *  - the selection is empty, AND
9078  *  - the widget has focus
9079  */
9080
9081 #define CURSOR_ON_MULTIPLIER 2
9082 #define CURSOR_OFF_MULTIPLIER 1
9083 #define CURSOR_PEND_MULTIPLIER 3
9084 #define CURSOR_DIVIDER 3
9085
9086 static gboolean
9087 cursor_blinks (GtkEntry *entry)
9088 {
9089   GtkEntryPrivate *priv = entry->priv;
9090
9091   if (gtk_widget_has_focus (GTK_WIDGET (entry)) &&
9092       priv->editable &&
9093       priv->selection_bound == priv->current_pos)
9094     {
9095       GtkSettings *settings;
9096       gboolean blink;
9097
9098       settings = gtk_widget_get_settings (GTK_WIDGET (entry));
9099       g_object_get (settings, "gtk-cursor-blink", &blink, NULL);
9100
9101       return blink;
9102     }
9103   else
9104     return FALSE;
9105 }
9106
9107 static gint
9108 get_cursor_time (GtkEntry *entry)
9109 {
9110   GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (entry));
9111   gint time;
9112
9113   g_object_get (settings, "gtk-cursor-blink-time", &time, NULL);
9114
9115   return time;
9116 }
9117
9118 static gint
9119 get_cursor_blink_timeout (GtkEntry *entry)
9120 {
9121   GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (entry));
9122   gint timeout;
9123
9124   g_object_get (settings, "gtk-cursor-blink-timeout", &timeout, NULL);
9125
9126   return timeout;
9127 }
9128
9129 static void
9130 show_cursor (GtkEntry *entry)
9131 {
9132   GtkEntryPrivate *priv = entry->priv;
9133   GtkWidget *widget;
9134
9135   if (!priv->cursor_visible)
9136     {
9137       priv->cursor_visible = TRUE;
9138
9139       widget = GTK_WIDGET (entry);
9140       if (gtk_widget_has_focus (widget) && priv->selection_bound == priv->current_pos)
9141         gtk_widget_queue_draw (widget);
9142     }
9143 }
9144
9145 static void
9146 hide_cursor (GtkEntry *entry)
9147 {
9148   GtkEntryPrivate *priv = entry->priv;
9149   GtkWidget *widget;
9150
9151   if (priv->cursor_visible)
9152     {
9153       priv->cursor_visible = FALSE;
9154
9155       widget = GTK_WIDGET (entry);
9156       if (gtk_widget_has_focus (widget) && priv->selection_bound == priv->current_pos)
9157         gtk_widget_queue_draw (widget);
9158     }
9159 }
9160
9161 /*
9162  * Blink!
9163  */
9164 static gint
9165 blink_cb (gpointer data)
9166 {
9167   GtkEntry *entry;
9168   GtkEntryPrivate *priv; 
9169   gint blink_timeout;
9170
9171   entry = GTK_ENTRY (data);
9172   priv = entry->priv;
9173  
9174   if (!gtk_widget_has_focus (GTK_WIDGET (entry)))
9175     {
9176       g_warning ("GtkEntry - did not receive focus-out-event. If you\n"
9177                  "connect a handler to this signal, it must return\n"
9178                  "FALSE so the entry gets the event as well");
9179
9180       gtk_entry_check_cursor_blink (entry);
9181
9182       return FALSE;
9183     }
9184   
9185   g_assert (priv->selection_bound == priv->current_pos);
9186   
9187   blink_timeout = get_cursor_blink_timeout (entry);
9188   if (priv->blink_time > 1000 * blink_timeout && 
9189       blink_timeout < G_MAXINT/1000) 
9190     {
9191       /* we've blinked enough without the user doing anything, stop blinking */
9192       show_cursor (entry);
9193       priv->blink_timeout = 0;
9194     } 
9195   else if (priv->cursor_visible)
9196     {
9197       hide_cursor (entry);
9198       priv->blink_timeout = gdk_threads_add_timeout (get_cursor_time (entry) * CURSOR_OFF_MULTIPLIER / CURSOR_DIVIDER,
9199                                             blink_cb,
9200                                             entry);
9201     }
9202   else
9203     {
9204       show_cursor (entry);
9205       priv->blink_time += get_cursor_time (entry);
9206       priv->blink_timeout = gdk_threads_add_timeout (get_cursor_time (entry) * CURSOR_ON_MULTIPLIER / CURSOR_DIVIDER,
9207                                             blink_cb,
9208                                             entry);
9209     }
9210
9211   /* Remove ourselves */
9212   return FALSE;
9213 }
9214
9215 static void
9216 gtk_entry_check_cursor_blink (GtkEntry *entry)
9217 {
9218   GtkEntryPrivate *priv = entry->priv;
9219
9220   if (cursor_blinks (entry))
9221     {
9222       if (!priv->blink_timeout)
9223         {
9224           show_cursor (entry);
9225           priv->blink_timeout = gdk_threads_add_timeout (get_cursor_time (entry) * CURSOR_ON_MULTIPLIER / CURSOR_DIVIDER,
9226                                                 blink_cb,
9227                                                 entry);
9228         }
9229     }
9230   else
9231     {
9232       if (priv->blink_timeout)
9233         {
9234           g_source_remove (priv->blink_timeout);
9235           priv->blink_timeout = 0;
9236         }
9237
9238       priv->cursor_visible = TRUE;
9239     }
9240 }
9241
9242 static void
9243 gtk_entry_pend_cursor_blink (GtkEntry *entry)
9244 {
9245   GtkEntryPrivate *priv = entry->priv;
9246
9247   if (cursor_blinks (entry))
9248     {
9249       if (priv->blink_timeout != 0)
9250         g_source_remove (priv->blink_timeout);
9251
9252       priv->blink_timeout = gdk_threads_add_timeout (get_cursor_time (entry) * CURSOR_PEND_MULTIPLIER / CURSOR_DIVIDER,
9253                                                      blink_cb,
9254                                                      entry);
9255       show_cursor (entry);
9256     }
9257 }
9258
9259 static void
9260 gtk_entry_reset_blink_time (GtkEntry *entry)
9261 {
9262   GtkEntryPrivate *priv = entry->priv;
9263
9264   priv->blink_time = 0;
9265 }
9266
9267
9268 /* completion */
9269 static gint
9270 gtk_entry_completion_timeout (gpointer data)
9271 {
9272   GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (data);
9273   GtkEntryPrivate *completion_entry_priv = GTK_ENTRY (completion->priv->entry)->priv;
9274
9275   completion->priv->completion_timeout = 0;
9276
9277   if (completion->priv->filter_model &&
9278       g_utf8_strlen (gtk_entry_get_text (GTK_ENTRY (completion->priv->entry)), -1)
9279       >= completion->priv->minimum_key_length)
9280     {
9281       gint matches;
9282       gint actions;
9283       GtkTreeSelection *s;
9284       gboolean popup_single;
9285
9286       gtk_entry_completion_complete (completion);
9287       matches = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->filter_model), NULL);
9288
9289       gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view)));
9290
9291       s = gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->action_view));
9292
9293       gtk_tree_selection_unselect_all (s);
9294
9295       actions = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->actions), NULL);
9296
9297       g_object_get (completion, "popup-single-match", &popup_single, NULL);
9298       if ((matches > (popup_single ? 0: 1)) || actions > 0)
9299         { 
9300           if (gtk_widget_get_visible (completion->priv->popup_window))
9301             _gtk_entry_completion_resize_popup (completion);
9302           else
9303             _gtk_entry_completion_popup (completion, completion_entry_priv->completion_device);
9304         }
9305       else 
9306         _gtk_entry_completion_popdown (completion);
9307     }
9308   else if (gtk_widget_get_visible (completion->priv->popup_window))
9309     _gtk_entry_completion_popdown (completion);
9310
9311   return FALSE;
9312 }
9313
9314 static inline gboolean
9315 keyval_is_cursor_move (guint keyval)
9316 {
9317   if (keyval == GDK_KEY_Up || keyval == GDK_KEY_KP_Up)
9318     return TRUE;
9319
9320   if (keyval == GDK_KEY_Down || keyval == GDK_KEY_KP_Down)
9321     return TRUE;
9322
9323   if (keyval == GDK_KEY_Page_Up)
9324     return TRUE;
9325
9326   if (keyval == GDK_KEY_Page_Down)
9327     return TRUE;
9328
9329   return FALSE;
9330 }
9331
9332 static gboolean
9333 gtk_entry_completion_key_press (GtkWidget   *widget,
9334                                 GdkEventKey *event,
9335                                 gpointer     user_data)
9336 {
9337   gint matches, actions = 0;
9338   GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
9339
9340   if (!gtk_widget_get_mapped (completion->priv->popup_window))
9341     return FALSE;
9342
9343   matches = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->filter_model), NULL);
9344
9345   if (completion->priv->actions)
9346     actions = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->actions), NULL);
9347
9348   if (keyval_is_cursor_move (event->keyval))
9349     {
9350       GtkTreePath *path = NULL;
9351       
9352       if (event->keyval == GDK_KEY_Up || event->keyval == GDK_KEY_KP_Up)
9353         {
9354           if (completion->priv->current_selected < 0)
9355             completion->priv->current_selected = matches + actions - 1;
9356           else
9357             completion->priv->current_selected--;
9358         }
9359       else if (event->keyval == GDK_KEY_Down || event->keyval == GDK_KEY_KP_Down)
9360         {
9361           if (completion->priv->current_selected < matches + actions - 1)
9362             completion->priv->current_selected++;
9363           else
9364             completion->priv->current_selected = -1;
9365         }
9366       else if (event->keyval == GDK_KEY_Page_Up)
9367         {
9368           if (completion->priv->current_selected < 0)
9369             completion->priv->current_selected = matches + actions - 1;
9370           else if (completion->priv->current_selected == 0)
9371             completion->priv->current_selected = -1;
9372           else if (completion->priv->current_selected < matches) 
9373             {
9374               completion->priv->current_selected -= PAGE_STEP;
9375               if (completion->priv->current_selected < 0)
9376                 completion->priv->current_selected = 0;
9377             }
9378           else 
9379             {
9380               completion->priv->current_selected -= PAGE_STEP;
9381               if (completion->priv->current_selected < matches - 1)
9382                 completion->priv->current_selected = matches - 1;
9383             }
9384         }
9385       else if (event->keyval == GDK_KEY_Page_Down)
9386         {
9387           if (completion->priv->current_selected < 0)
9388             completion->priv->current_selected = 0;
9389           else if (completion->priv->current_selected < matches - 1)
9390             {
9391               completion->priv->current_selected += PAGE_STEP;
9392               if (completion->priv->current_selected > matches - 1)
9393                 completion->priv->current_selected = matches - 1;
9394             }
9395           else if (completion->priv->current_selected == matches + actions - 1)
9396             {
9397               completion->priv->current_selected = -1;
9398             }
9399           else
9400             {
9401               completion->priv->current_selected += PAGE_STEP;
9402               if (completion->priv->current_selected > matches + actions - 1)
9403                 completion->priv->current_selected = matches + actions - 1;
9404             }
9405         }
9406
9407       if (completion->priv->current_selected < 0)
9408         {
9409           gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view)));
9410           gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->action_view)));
9411
9412           if (completion->priv->inline_selection &&
9413               completion->priv->completion_prefix)
9414             {
9415               gtk_entry_set_text (GTK_ENTRY (completion->priv->entry), 
9416                                   completion->priv->completion_prefix);
9417               gtk_editable_set_position (GTK_EDITABLE (widget), -1);
9418             }
9419         }
9420       else if (completion->priv->current_selected < matches)
9421         {
9422           gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->action_view)));
9423
9424           path = gtk_tree_path_new_from_indices (completion->priv->current_selected, -1);
9425           gtk_tree_view_set_cursor (GTK_TREE_VIEW (completion->priv->tree_view),
9426                                     path, NULL, FALSE);
9427
9428           if (completion->priv->inline_selection)
9429             {
9430
9431               GtkTreeIter iter;
9432               GtkTreeIter child_iter;
9433               GtkTreeModel *model = NULL;
9434               GtkTreeSelection *sel;
9435               gboolean entry_set;
9436
9437               sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view));
9438               if (!gtk_tree_selection_get_selected (sel, &model, &iter))
9439                 return FALSE;
9440
9441               gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_iter, &iter);
9442               model = gtk_tree_model_filter_get_model (GTK_TREE_MODEL_FILTER (model));
9443               
9444               if (completion->priv->completion_prefix == NULL)
9445                 completion->priv->completion_prefix = g_strdup (gtk_entry_get_text (GTK_ENTRY (completion->priv->entry)));
9446
9447               g_signal_emit_by_name (completion, "cursor-on-match", model,
9448                                      &child_iter, &entry_set);
9449             }
9450         }
9451       else if (completion->priv->current_selected - matches >= 0)
9452         {
9453           gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view)));
9454
9455           path = gtk_tree_path_new_from_indices (completion->priv->current_selected - matches, -1);
9456           gtk_tree_view_set_cursor (GTK_TREE_VIEW (completion->priv->action_view),
9457                                     path, NULL, FALSE);
9458
9459           if (completion->priv->inline_selection &&
9460               completion->priv->completion_prefix)
9461             {
9462               gtk_entry_set_text (GTK_ENTRY (completion->priv->entry), 
9463                                   completion->priv->completion_prefix);
9464               gtk_editable_set_position (GTK_EDITABLE (widget), -1);
9465             }
9466         }
9467
9468       gtk_tree_path_free (path);
9469
9470       return TRUE;
9471     }
9472   else if (event->keyval == GDK_KEY_Escape ||
9473            event->keyval == GDK_KEY_Left ||
9474            event->keyval == GDK_KEY_KP_Left ||
9475            event->keyval == GDK_KEY_Right ||
9476            event->keyval == GDK_KEY_KP_Right) 
9477     {
9478       gboolean retval = TRUE;
9479
9480       _gtk_entry_reset_im_context (GTK_ENTRY (widget));
9481       _gtk_entry_completion_popdown (completion);
9482
9483       if (completion->priv->current_selected < 0)
9484         {
9485           retval = FALSE;
9486           goto keypress_completion_out;
9487         }
9488       else if (completion->priv->inline_selection)
9489         {
9490           /* Escape rejects the tentative completion */
9491           if (event->keyval == GDK_KEY_Escape)
9492             {
9493               if (completion->priv->completion_prefix)
9494                 gtk_entry_set_text (GTK_ENTRY (completion->priv->entry), 
9495                                     completion->priv->completion_prefix);
9496               else 
9497                 gtk_entry_set_text (GTK_ENTRY (completion->priv->entry), "");
9498             }
9499
9500           /* Move the cursor to the end for Right/Esc, to the
9501              beginning for Left */
9502           if (event->keyval == GDK_KEY_Right ||
9503               event->keyval == GDK_KEY_KP_Right ||
9504               event->keyval == GDK_KEY_Escape)
9505             gtk_editable_set_position (GTK_EDITABLE (widget), -1);
9506           else
9507             gtk_editable_set_position (GTK_EDITABLE (widget), 0);
9508         }
9509
9510 keypress_completion_out:
9511       if (completion->priv->inline_selection)
9512         {
9513           g_free (completion->priv->completion_prefix);
9514           completion->priv->completion_prefix = NULL;
9515         }
9516
9517       return retval;
9518     }
9519   else if (event->keyval == GDK_KEY_Tab || 
9520            event->keyval == GDK_KEY_KP_Tab ||
9521            event->keyval == GDK_KEY_ISO_Left_Tab) 
9522     {
9523       _gtk_entry_reset_im_context (GTK_ENTRY (widget));
9524       _gtk_entry_completion_popdown (completion);
9525
9526       g_free (completion->priv->completion_prefix);
9527       completion->priv->completion_prefix = NULL;
9528
9529       return FALSE;
9530     }
9531   else if (event->keyval == GDK_KEY_ISO_Enter ||
9532            event->keyval == GDK_KEY_KP_Enter ||
9533            event->keyval == GDK_KEY_Return)
9534     {
9535       GtkTreeIter iter;
9536       GtkTreeModel *model = NULL;
9537       GtkTreeModel *child_model;
9538       GtkTreeIter child_iter;
9539       GtkTreeSelection *sel;
9540       gboolean retval = TRUE;
9541
9542       _gtk_entry_reset_im_context (GTK_ENTRY (widget));
9543       _gtk_entry_completion_popdown (completion);
9544
9545       if (completion->priv->current_selected < matches)
9546         {
9547           gboolean entry_set;
9548
9549           sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view));
9550           if (gtk_tree_selection_get_selected (sel, &model, &iter))
9551             {
9552               gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), &child_iter, &iter);
9553               child_model = gtk_tree_model_filter_get_model (GTK_TREE_MODEL_FILTER (model));
9554               g_signal_handler_block (widget, completion->priv->changed_id);
9555               g_signal_emit_by_name (completion, "match-selected",
9556                                      child_model, &child_iter, &entry_set);
9557               g_signal_handler_unblock (widget, completion->priv->changed_id);
9558
9559               if (!entry_set)
9560                 {
9561                   gchar *str = NULL;
9562
9563                   gtk_tree_model_get (model, &iter,
9564                                       completion->priv->text_column, &str,
9565                                       -1);
9566
9567                   gtk_entry_set_text (GTK_ENTRY (widget), str);
9568
9569                   /* move the cursor to the end */
9570                   gtk_editable_set_position (GTK_EDITABLE (widget), -1);
9571
9572                   g_free (str);
9573                 }
9574             }
9575           else
9576             retval = FALSE;
9577         }
9578       else if (completion->priv->current_selected - matches >= 0)
9579         {
9580           sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->action_view));
9581           if (gtk_tree_selection_get_selected (sel, &model, &iter))
9582             {
9583               GtkTreePath *path;
9584
9585               path = gtk_tree_path_new_from_indices (completion->priv->current_selected - matches, -1);
9586               g_signal_emit_by_name (completion, "action-activated",
9587                                      gtk_tree_path_get_indices (path)[0]);
9588               gtk_tree_path_free (path);
9589             }
9590           else
9591             retval = FALSE;
9592         }
9593
9594       g_free (completion->priv->completion_prefix);
9595       completion->priv->completion_prefix = NULL;
9596
9597       return retval;
9598     }
9599
9600   return FALSE;
9601 }
9602
9603 static void
9604 gtk_entry_completion_changed (GtkWidget *widget,
9605                               gpointer   user_data)
9606 {
9607   GtkEntryCompletion *completion = GTK_ENTRY_COMPLETION (user_data);
9608   GtkEntry *entry = GTK_ENTRY (widget);
9609   GtkEntryPrivate *priv = entry->priv;
9610   GdkDevice *device;
9611
9612   /* (re)install completion timeout */
9613   if (completion->priv->completion_timeout)
9614     g_source_remove (completion->priv->completion_timeout);
9615
9616   if (!gtk_entry_get_text (entry))
9617     return;
9618
9619   /* no need to normalize for this test */
9620   if (completion->priv->minimum_key_length > 0 &&
9621       strcmp ("", gtk_entry_get_text (entry)) == 0)
9622     {
9623       if (gtk_widget_get_visible (completion->priv->popup_window))
9624         _gtk_entry_completion_popdown (completion);
9625       return;
9626     }
9627
9628   device = gtk_get_current_event_device ();
9629
9630   if (device && gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
9631     device = gdk_device_get_associated_device (device);
9632
9633   if (device)
9634     priv->completion_device = device;
9635
9636   completion->priv->completion_timeout =
9637     gdk_threads_add_timeout (COMPLETION_TIMEOUT,
9638                    gtk_entry_completion_timeout,
9639                    completion);
9640 }
9641
9642 static gboolean
9643 check_completion_callback (GtkEntryCompletion *completion)
9644 {
9645   completion->priv->check_completion_idle = NULL;
9646   
9647   gtk_entry_completion_complete (completion);
9648   gtk_entry_completion_insert_prefix (completion);
9649
9650   return FALSE;
9651 }
9652
9653 static void
9654 clear_completion_callback (GtkEntry   *entry,
9655                            GParamSpec *pspec)
9656 {
9657   if (pspec->name == I_("cursor-position") ||
9658       pspec->name == I_("selection-bound"))
9659     {
9660       GtkEntryCompletion *completion = gtk_entry_get_completion (entry);
9661       
9662       completion->priv->has_completion = FALSE;
9663     }
9664 }
9665
9666 static gboolean
9667 accept_completion_callback (GtkEntry *entry)
9668 {
9669   GtkEntryCompletion *completion = gtk_entry_get_completion (entry);
9670
9671   if (completion->priv->has_completion)
9672     gtk_editable_set_position (GTK_EDITABLE (entry),
9673                                gtk_entry_buffer_get_length (get_buffer (entry)));
9674
9675   return FALSE;
9676 }
9677
9678 static void
9679 completion_insert_text_callback (GtkEntry           *entry,
9680                                  const gchar        *text,
9681                                  gint                length,
9682                                  gint                position,
9683                                  GtkEntryCompletion *completion)
9684 {
9685   /* idle to update the selection based on the file list */
9686   if (completion->priv->check_completion_idle == NULL)
9687     {
9688       completion->priv->check_completion_idle = g_idle_source_new ();
9689       g_source_set_priority (completion->priv->check_completion_idle, G_PRIORITY_HIGH);
9690       g_source_set_closure (completion->priv->check_completion_idle,
9691                             g_cclosure_new_object (G_CALLBACK (check_completion_callback),
9692                                                    G_OBJECT (completion)));
9693       g_source_attach (completion->priv->check_completion_idle, NULL);
9694     }
9695 }
9696
9697 static void
9698 completion_changed (GtkEntryCompletion *completion,
9699                     GParamSpec         *pspec,
9700                     gpointer            data)
9701 {
9702   GtkEntry *entry = GTK_ENTRY (data);
9703
9704   if (pspec->name == I_("popup-completion") ||
9705       pspec->name == I_("inline-completion"))
9706     {
9707       disconnect_completion_signals (entry, completion);
9708       connect_completion_signals (entry, completion);
9709     }
9710 }
9711
9712 static void
9713 disconnect_completion_signals (GtkEntry           *entry,
9714                                GtkEntryCompletion *completion)
9715 {
9716   g_signal_handlers_disconnect_by_func (completion, 
9717                                        G_CALLBACK (completion_changed), entry);
9718   if (completion->priv->changed_id > 0 &&
9719       g_signal_handler_is_connected (entry, completion->priv->changed_id))
9720     {
9721       g_signal_handler_disconnect (entry, completion->priv->changed_id);
9722       completion->priv->changed_id = 0;
9723     }
9724   g_signal_handlers_disconnect_by_func (entry, 
9725                                         G_CALLBACK (gtk_entry_completion_key_press), completion);
9726   if (completion->priv->insert_text_id > 0 &&
9727       g_signal_handler_is_connected (entry, completion->priv->insert_text_id))
9728     {
9729       g_signal_handler_disconnect (entry, completion->priv->insert_text_id);
9730       completion->priv->insert_text_id = 0;
9731     }
9732   g_signal_handlers_disconnect_by_func (entry, 
9733                                         G_CALLBACK (completion_insert_text_callback), completion);
9734   g_signal_handlers_disconnect_by_func (entry, 
9735                                         G_CALLBACK (clear_completion_callback), completion);
9736   g_signal_handlers_disconnect_by_func (entry, 
9737                                         G_CALLBACK (accept_completion_callback), completion);
9738 }
9739
9740 static void
9741 connect_completion_signals (GtkEntry           *entry,
9742                             GtkEntryCompletion *completion)
9743 {
9744   if (completion->priv->popup_completion)
9745     {
9746       completion->priv->changed_id =
9747         g_signal_connect (entry, "changed",
9748                           G_CALLBACK (gtk_entry_completion_changed), completion);
9749       g_signal_connect (entry, "key-press-event",
9750                         G_CALLBACK (gtk_entry_completion_key_press), completion);
9751     }
9752  
9753   if (completion->priv->inline_completion)
9754     {
9755       completion->priv->insert_text_id =
9756         g_signal_connect (entry, "insert-text",
9757                           G_CALLBACK (completion_insert_text_callback), completion);
9758       g_signal_connect (entry, "notify",
9759                         G_CALLBACK (clear_completion_callback), completion);
9760       g_signal_connect (entry, "activate",
9761                         G_CALLBACK (accept_completion_callback), completion);
9762       g_signal_connect (entry, "focus-out-event",
9763                         G_CALLBACK (accept_completion_callback), completion);
9764     }
9765
9766   g_signal_connect (completion, "notify",
9767                     G_CALLBACK (completion_changed), entry);
9768 }
9769
9770 /**
9771  * gtk_entry_set_completion:
9772  * @entry: A #GtkEntry
9773  * @completion: (allow-none): The #GtkEntryCompletion or %NULL
9774  *
9775  * Sets @completion to be the auxiliary completion object to use with @entry.
9776  * All further configuration of the completion mechanism is done on
9777  * @completion using the #GtkEntryCompletion API. Completion is disabled if
9778  * @completion is set to %NULL.
9779  *
9780  * Since: 2.4
9781  */
9782 void
9783 gtk_entry_set_completion (GtkEntry           *entry,
9784                           GtkEntryCompletion *completion)
9785 {
9786   GtkEntryCompletion *old;
9787
9788   g_return_if_fail (GTK_IS_ENTRY (entry));
9789   g_return_if_fail (!completion || GTK_IS_ENTRY_COMPLETION (completion));
9790
9791   old = gtk_entry_get_completion (entry);
9792
9793   if (old == completion)
9794     return;
9795   
9796   if (old)
9797     {
9798       if (old->priv->completion_timeout)
9799         {
9800           g_source_remove (old->priv->completion_timeout);
9801           old->priv->completion_timeout = 0;
9802         }
9803
9804       if (old->priv->check_completion_idle)
9805         {
9806           g_source_destroy (old->priv->check_completion_idle);
9807           old->priv->check_completion_idle = NULL;
9808         }
9809
9810       if (gtk_widget_get_mapped (old->priv->popup_window))
9811         _gtk_entry_completion_popdown (old);
9812
9813       disconnect_completion_signals (entry, old);
9814       old->priv->entry = NULL;
9815
9816       g_object_unref (old);
9817     }
9818
9819   if (!completion)
9820     {
9821       g_object_set_data (G_OBJECT (entry), I_(GTK_ENTRY_COMPLETION_KEY), NULL);
9822       return;
9823     }
9824
9825   /* hook into the entry */
9826   g_object_ref (completion);
9827
9828   connect_completion_signals (entry, completion);    
9829   completion->priv->entry = GTK_WIDGET (entry);
9830   g_object_set_data (G_OBJECT (entry), I_(GTK_ENTRY_COMPLETION_KEY), completion);
9831
9832   g_object_notify (G_OBJECT (entry), "completion");
9833 }
9834
9835 /**
9836  * gtk_entry_get_completion:
9837  * @entry: A #GtkEntry
9838  *
9839  * Returns the auxiliary completion object currently in use by @entry.
9840  *
9841  * Return value: (transfer none): The auxiliary completion object currently
9842  *     in use by @entry.
9843  *
9844  * Since: 2.4
9845  */
9846 GtkEntryCompletion *
9847 gtk_entry_get_completion (GtkEntry *entry)
9848 {
9849   GtkEntryCompletion *completion;
9850
9851   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
9852
9853   completion = GTK_ENTRY_COMPLETION (g_object_get_data (G_OBJECT (entry),
9854                                      GTK_ENTRY_COMPLETION_KEY));
9855
9856   return completion;
9857 }
9858
9859 /**
9860  * gtk_entry_set_cursor_hadjustment:
9861  * @entry: a #GtkEntry
9862  * @adjustment: an adjustment which should be adjusted when the cursor 
9863  *              is moved, or %NULL
9864  *
9865  * Hooks up an adjustment to the cursor position in an entry, so that when 
9866  * the cursor is moved, the adjustment is scrolled to show that position. 
9867  * See gtk_scrolled_window_get_hadjustment() for a typical way of obtaining 
9868  * the adjustment.
9869  *
9870  * The adjustment has to be in pixel units and in the same coordinate system 
9871  * as the entry. 
9872  * 
9873  * Since: 2.12
9874  */
9875 void
9876 gtk_entry_set_cursor_hadjustment (GtkEntry      *entry,
9877                                   GtkAdjustment *adjustment)
9878 {
9879   g_return_if_fail (GTK_IS_ENTRY (entry));
9880   if (adjustment)
9881     g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
9882
9883   if (adjustment)
9884     g_object_ref (adjustment);
9885
9886   g_object_set_qdata_full (G_OBJECT (entry), 
9887                            quark_cursor_hadjustment,
9888                            adjustment, 
9889                            g_object_unref);
9890 }
9891
9892 /**
9893  * gtk_entry_get_cursor_hadjustment:
9894  * @entry: a #GtkEntry
9895  *
9896  * Retrieves the horizontal cursor adjustment for the entry. 
9897  * See gtk_entry_set_cursor_hadjustment().
9898  *
9899  * Return value: (transfer none): the horizontal cursor adjustment, or %NULL
9900  *   if none has been set.
9901  *
9902  * Since: 2.12
9903  */
9904 GtkAdjustment*
9905 gtk_entry_get_cursor_hadjustment (GtkEntry *entry)
9906 {
9907   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
9908
9909   return g_object_get_qdata (G_OBJECT (entry), quark_cursor_hadjustment);
9910 }
9911
9912 /**
9913  * gtk_entry_set_progress_fraction:
9914  * @entry: a #GtkEntry
9915  * @fraction: fraction of the task that's been completed
9916  *
9917  * Causes the entry's progress indicator to "fill in" the given
9918  * fraction of the bar. The fraction should be between 0.0 and 1.0,
9919  * inclusive.
9920  *
9921  * Since: 2.16
9922  */
9923 void
9924 gtk_entry_set_progress_fraction (GtkEntry *entry,
9925                                  gdouble   fraction)
9926 {
9927   GtkWidget       *widget;
9928   GtkEntryPrivate *private;
9929   gdouble          old_fraction;
9930   gint x, y, width, height;
9931   gint old_x, old_y, old_width, old_height;
9932
9933   g_return_if_fail (GTK_IS_ENTRY (entry));
9934
9935   widget = GTK_WIDGET (entry);
9936   private = entry->priv;
9937
9938   if (private->progress_pulse_mode)
9939     old_fraction = -1;
9940   else
9941     old_fraction = private->progress_fraction;
9942
9943   if (gtk_widget_is_drawable (widget))
9944     get_progress_area (widget, &old_x, &old_y, &old_width, &old_height);
9945
9946   fraction = CLAMP (fraction, 0.0, 1.0);
9947
9948   private->progress_fraction = fraction;
9949   private->progress_pulse_mode = FALSE;
9950   private->progress_pulse_current = 0.0;
9951
9952   if (gtk_widget_is_drawable (widget))
9953     {
9954       get_progress_area (widget, &x, &y, &width, &height);
9955
9956       if ((x != old_x) || (y != old_y) || (width != old_width) || (height != old_height))
9957         gtk_widget_queue_draw (widget);
9958     }
9959
9960   if (fraction != old_fraction)
9961     g_object_notify (G_OBJECT (entry), "progress-fraction");
9962 }
9963
9964 /**
9965  * gtk_entry_get_progress_fraction:
9966  * @entry: a #GtkEntry
9967  *
9968  * Returns the current fraction of the task that's been completed.
9969  * See gtk_entry_set_progress_fraction().
9970  *
9971  * Return value: a fraction from 0.0 to 1.0
9972  *
9973  * Since: 2.16
9974  */
9975 gdouble
9976 gtk_entry_get_progress_fraction (GtkEntry *entry)
9977 {
9978   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0.0);
9979
9980   return entry->priv->progress_fraction;
9981 }
9982
9983 /**
9984  * gtk_entry_set_progress_pulse_step:
9985  * @entry: a #GtkEntry
9986  * @fraction: fraction between 0.0 and 1.0
9987  *
9988  * Sets the fraction of total entry width to move the progress
9989  * bouncing block for each call to gtk_entry_progress_pulse().
9990  *
9991  * Since: 2.16
9992  */
9993 void
9994 gtk_entry_set_progress_pulse_step (GtkEntry *entry,
9995                                    gdouble   fraction)
9996 {
9997   GtkEntryPrivate *private;
9998
9999   g_return_if_fail (GTK_IS_ENTRY (entry));
10000
10001   private = entry->priv;
10002
10003   fraction = CLAMP (fraction, 0.0, 1.0);
10004
10005   if (fraction != private->progress_pulse_fraction)
10006     {
10007       private->progress_pulse_fraction = fraction;
10008
10009       gtk_widget_queue_draw (GTK_WIDGET (entry));
10010
10011       g_object_notify (G_OBJECT (entry), "progress-pulse-step");
10012     }
10013 }
10014
10015 /**
10016  * gtk_entry_get_progress_pulse_step:
10017  * @entry: a #GtkEntry
10018  *
10019  * Retrieves the pulse step set with gtk_entry_set_progress_pulse_step().
10020  *
10021  * Return value: a fraction from 0.0 to 1.0
10022  *
10023  * Since: 2.16
10024  */
10025 gdouble
10026 gtk_entry_get_progress_pulse_step (GtkEntry *entry)
10027 {
10028   g_return_val_if_fail (GTK_IS_ENTRY (entry), 0.0);
10029
10030   return entry->priv->progress_pulse_fraction;
10031 }
10032
10033 /**
10034  * gtk_entry_progress_pulse:
10035  * @entry: a #GtkEntry
10036  *
10037  * Indicates that some progress is made, but you don't know how much.
10038  * Causes the entry's progress indicator to enter "activity mode,"
10039  * where a block bounces back and forth. Each call to
10040  * gtk_entry_progress_pulse() causes the block to move by a little bit
10041  * (the amount of movement per pulse is determined by
10042  * gtk_entry_set_progress_pulse_step()).
10043  *
10044  * Since: 2.16
10045  */
10046 void
10047 gtk_entry_progress_pulse (GtkEntry *entry)
10048 {
10049   GtkEntryPrivate *private;
10050
10051   g_return_if_fail (GTK_IS_ENTRY (entry));
10052
10053   private = entry->priv;
10054
10055   if (private->progress_pulse_mode)
10056     {
10057       if (private->progress_pulse_way_back)
10058         {
10059           private->progress_pulse_current -= private->progress_pulse_fraction;
10060
10061           if (private->progress_pulse_current < 0.0)
10062             {
10063               private->progress_pulse_current = 0.0;
10064               private->progress_pulse_way_back = FALSE;
10065             }
10066         }
10067       else
10068         {
10069           private->progress_pulse_current += private->progress_pulse_fraction;
10070
10071           if (private->progress_pulse_current > 1.0 - private->progress_pulse_fraction)
10072             {
10073               private->progress_pulse_current = 1.0 - private->progress_pulse_fraction;
10074               private->progress_pulse_way_back = TRUE;
10075             }
10076         }
10077     }
10078   else
10079     {
10080       private->progress_fraction = 0.0;
10081       private->progress_pulse_mode = TRUE;
10082       private->progress_pulse_way_back = FALSE;
10083       private->progress_pulse_current = 0.0;
10084     }
10085
10086   gtk_widget_queue_draw (GTK_WIDGET (entry));
10087 }
10088
10089 /**
10090  * gtk_entry_set_placeholder_text:
10091  * @entry: a #GtkEntry
10092  * @text: a string to be displayed when @entry is empty an unfocused, or %NULL
10093  *
10094  * Sets text to be displayed in @entry when it is empty and unfocused.
10095  * This can be used to give a visual hint of the expected contents of
10096  * the #GtkEntry.
10097  *
10098  * Note that since the placeholder text gets removed when the entry
10099  * received focus, using this feature is a bit problematic if the entry
10100  * is given the initial focus in a window. Sometimes this can be
10101  * worked around by delaying the initial focus setting until the
10102  * first key event arrives.
10103  *
10104  * Since: 3.2
10105  **/
10106 void
10107 gtk_entry_set_placeholder_text (GtkEntry    *entry,
10108                                 const gchar *text)
10109 {
10110   GtkEntryPrivate *priv;
10111
10112   g_return_if_fail (GTK_IS_ENTRY (entry));
10113
10114   priv = entry->priv;
10115
10116   if (g_strcmp0 (priv->placeholder_text, text) == 0)
10117     return;
10118
10119   g_free (priv->placeholder_text);
10120   priv->placeholder_text = g_strdup (text);
10121
10122   gtk_entry_recompute (entry);
10123
10124   g_object_notify (G_OBJECT (entry), "placeholder-text");
10125 }
10126
10127 /**
10128  * gtk_entry_get_placeholder_text:
10129  * @entry: a #GtkEntry
10130  *
10131  * Retrieves the text that will be displayed when @entry is empty and unfocused
10132  *
10133  * Returns: a pointer to the placeholder text as a string. This string points to internally allocated
10134  * storage in the widget and must not be freed, modified or stored.
10135  *
10136  * Since: 3.2
10137  **/
10138 const gchar *
10139 gtk_entry_get_placeholder_text (GtkEntry *entry)
10140 {
10141   GtkEntryPrivate *priv;
10142
10143   g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
10144
10145   priv = entry->priv;
10146
10147   return priv->placeholder_text;
10148 }
10149
10150 /* Caps Lock warning for password entries */
10151
10152 static void
10153 show_capslock_feedback (GtkEntry    *entry,
10154                         const gchar *text)
10155 {
10156   GtkEntryPrivate *priv = entry->priv;
10157
10158   if (gtk_entry_get_icon_storage_type (entry, GTK_ENTRY_ICON_SECONDARY) == GTK_IMAGE_EMPTY)
10159     {
10160       gtk_entry_set_icon_from_stock (entry, GTK_ENTRY_ICON_SECONDARY, GTK_STOCK_CAPS_LOCK_WARNING);
10161       gtk_entry_set_icon_activatable (entry, GTK_ENTRY_ICON_SECONDARY, FALSE);
10162       priv->caps_lock_warning_shown = TRUE;
10163     }
10164
10165   if (priv->caps_lock_warning_shown)
10166     gtk_entry_set_icon_tooltip_text (entry, GTK_ENTRY_ICON_SECONDARY, text);
10167   else
10168     g_warning ("Can't show Caps Lock warning, since secondary icon is set");
10169 }
10170
10171 static void
10172 remove_capslock_feedback (GtkEntry *entry)
10173 {
10174   GtkEntryPrivate *priv = entry->priv;
10175
10176   if (priv->caps_lock_warning_shown)
10177     {
10178       gtk_entry_set_icon_from_stock (entry, GTK_ENTRY_ICON_SECONDARY, NULL);
10179       priv->caps_lock_warning_shown = FALSE;
10180     } 
10181 }
10182
10183 static void
10184 keymap_state_changed (GdkKeymap *keymap, 
10185                       GtkEntry  *entry)
10186 {
10187   GtkEntryPrivate *priv = entry->priv;
10188   char *text = NULL;
10189
10190   if (gtk_entry_get_display_mode (entry) != DISPLAY_NORMAL && priv->caps_lock_warning)
10191     { 
10192       if (gdk_keymap_get_num_lock_state (keymap)
10193           && gdk_keymap_get_caps_lock_state (keymap))
10194         text = _("Caps Lock and Num Lock are on");
10195       else if (gdk_keymap_get_num_lock_state (keymap))
10196         text = _("Num Lock is on");
10197       else if (gdk_keymap_get_caps_lock_state (keymap))
10198         text = _("Caps Lock is on");
10199     }
10200
10201   if (text)
10202     show_capslock_feedback (entry, text);
10203   else
10204     remove_capslock_feedback (entry);
10205 }
10206
10207 /*
10208  * _gtk_entry_set_is_cell_renderer:
10209  * @entry: a #GtkEntry
10210  * @is_cell_renderer: new value
10211  *
10212  * This is a helper function for GtkComboBox. A GtkEntry in a GtkComboBox
10213  * is supposed to behave like a GtkCellEditable when placed in a combo box.
10214  *
10215  * I.e take up its allocation and get GtkEntry->is_cell_renderer = TRUE.
10216  *
10217  */
10218 void
10219 _gtk_entry_set_is_cell_renderer (GtkEntry *entry,
10220                                  gboolean  is_cell_renderer)
10221 {
10222   entry->priv->is_cell_renderer = is_cell_renderer;
10223 }