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