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