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