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