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