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