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