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