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