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