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