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