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