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