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