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